disaster_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: disaster_cmd.cpp 15583 2009-02-25 21:29:50Z frosch $ */
00002 
00019 #include "stdafx.h"
00020 #include "landscape.h"
00021 
00022 #include "industry_map.h"
00023 #include "station_map.h"
00024 #include "command_func.h"
00025 #include "news_func.h"
00026 #include "town.h"
00027 #include "company_func.h"
00028 #include "variables.h"
00029 #include "strings_func.h"
00030 #include "date_func.h"
00031 #include "functions.h"
00032 #include "vehicle_func.h"
00033 #include "sound_func.h"
00034 #include "effectvehicle_func.h"
00035 #include "roadveh.h"
00036 #include "ai/ai.hpp"
00037 
00038 #include "table/strings.h"
00039 #include "table/sprites.h"
00040 
00041 enum DisasterSubType {
00042   ST_Zeppeliner,
00043   ST_Zeppeliner_Shadow,
00044   ST_Small_Ufo,
00045   ST_Small_Ufo_Shadow,
00046   ST_Airplane,
00047   ST_Airplane_Shadow,
00048   ST_Helicopter,
00049   ST_Helicopter_Shadow,
00050   ST_Helicopter_Rotors,
00051   ST_Big_Ufo,
00052   ST_Big_Ufo_Shadow,
00053   ST_Big_Ufo_Destroyer,
00054   ST_Big_Ufo_Destroyer_Shadow,
00055   ST_Small_Submarine,
00056   ST_Big_Submarine,
00057 };
00058 
00059 static void DisasterClearSquare(TileIndex tile)
00060 {
00061   if (!EnsureNoVehicleOnGround(tile)) return;
00062 
00063   switch (GetTileType(tile)) {
00064     case MP_RAILWAY:
00065       if (IsHumanCompany(GetTileOwner(tile)) && !IsRailWaypoint(tile)) {
00066         CompanyID old_company = _current_company;
00067         _current_company = OWNER_WATER;
00068         DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00069         _current_company = old_company;
00070 
00071         /* update signals in buffer */
00072         UpdateSignalsInBuffer();
00073       }
00074       break;
00075 
00076     case MP_HOUSE: {
00077       CompanyID old_company = _current_company;
00078       _current_company = OWNER_NONE;
00079       DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00080       _current_company = old_company;
00081       break;
00082     }
00083 
00084     case MP_TREES:
00085     case MP_CLEAR:
00086       DoClearSquare(tile);
00087       break;
00088 
00089     default:
00090       break;
00091   }
00092 }
00093 
00094 static const SpriteID _disaster_images_1[] = {SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP};
00095 static const SpriteID _disaster_images_2[] = {SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT};
00096 static const SpriteID _disaster_images_3[] = {SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15};
00097 static const SpriteID _disaster_images_4[] = {SPR_SUB_SMALL_NE, SPR_SUB_SMALL_NE, SPR_SUB_SMALL_SE, SPR_SUB_SMALL_SE, SPR_SUB_SMALL_SW, SPR_SUB_SMALL_SW, SPR_SUB_SMALL_NW, SPR_SUB_SMALL_NW};
00098 static const SpriteID _disaster_images_5[] = {SPR_SUB_LARGE_NE, SPR_SUB_LARGE_NE, SPR_SUB_LARGE_SE, SPR_SUB_LARGE_SE, SPR_SUB_LARGE_SW, SPR_SUB_LARGE_SW, SPR_SUB_LARGE_NW, SPR_SUB_LARGE_NW};
00099 static const SpriteID _disaster_images_6[] = {SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER};
00100 static const SpriteID _disaster_images_7[] = {SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER};
00101 static const SpriteID _disaster_images_8[] = {SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A};
00102 static const SpriteID _disaster_images_9[] = {SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1};
00103 
00104 static const SpriteID * const _disaster_images[] = {
00105   _disaster_images_1, _disaster_images_1,                     
00106   _disaster_images_2, _disaster_images_2,                     
00107   _disaster_images_3, _disaster_images_3,                     
00108   _disaster_images_8, _disaster_images_8, _disaster_images_9, 
00109   _disaster_images_6, _disaster_images_6,                     
00110   _disaster_images_7, _disaster_images_7,                     
00111   _disaster_images_4, _disaster_images_5,                     
00112 };
00113 
00114 static void DisasterVehicleUpdateImage(Vehicle *v)
00115 {
00116   SpriteID img = v->u.disaster.image_override;
00117   if (img == 0) img = _disaster_images[v->subtype][v->direction];
00118   v->cur_image = img;
00119 }
00120 
00123 static void InitializeDisasterVehicle(Vehicle *v, int x, int y, byte z, Direction direction, byte subtype)
00124 {
00125   v->x_pos = x;
00126   v->y_pos = y;
00127   v->z_pos = z;
00128   v->tile = TileVirtXY(x, y);
00129   v->direction = direction;
00130   v->subtype = subtype;
00131   v->UpdateDeltaXY(INVALID_DIR);
00132   v->owner = OWNER_NONE;
00133   v->vehstatus = VS_UNCLICKABLE;
00134   v->u.disaster.image_override = 0;
00135   v->current_order.Free();
00136 
00137   DisasterVehicleUpdateImage(v);
00138   VehiclePositionChanged(v);
00139   MarkSingleVehicleDirty(v);
00140 }
00141 
00142 static void SetDisasterVehiclePos(Vehicle *v, int x, int y, byte z)
00143 {
00144   Vehicle *u;
00145 
00146   BeginVehicleMove(v);
00147   v->x_pos = x;
00148   v->y_pos = y;
00149   v->z_pos = z;
00150   v->tile = TileVirtXY(x, y);
00151 
00152   DisasterVehicleUpdateImage(v);
00153   VehiclePositionChanged(v);
00154   EndVehicleMove(v);
00155 
00156   if ((u = v->Next()) != NULL) {
00157     int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
00158     int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
00159     BeginVehicleMove(u);
00160 
00161     u->x_pos = x;
00162     u->y_pos = y - 1 - (max(z - GetSlopeZ(safe_x, safe_y), 0U) >> 3);
00163     safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
00164     u->z_pos = GetSlopeZ(safe_x, safe_y);
00165     u->direction = v->direction;
00166 
00167     DisasterVehicleUpdateImage(u);
00168     VehiclePositionChanged(u);
00169     EndVehicleMove(u);
00170 
00171     if ((u = u->Next()) != NULL) {
00172       BeginVehicleMove(u);
00173       u->x_pos = x;
00174       u->y_pos = y;
00175       u->z_pos = z + 5;
00176       VehiclePositionChanged(u);
00177       EndVehicleMove(u);
00178     }
00179   }
00180 }
00181 
00190 static void DisasterTick_Zeppeliner(Vehicle *v)
00191 {
00192   Station *st;
00193   int x, y;
00194   byte z;
00195   TileIndex tile;
00196 
00197   v->tick_counter++;
00198 
00199   if (v->current_order.GetDestination() < 2) {
00200     if (HasBit(v->tick_counter, 0)) return;
00201 
00202     GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00203 
00204     SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00205 
00206     if (v->current_order.GetDestination() == 1) {
00207       if (++v->age == 38) {
00208         v->current_order.SetDestination(2);
00209         v->age = 0;
00210       }
00211 
00212       if (GB(v->tick_counter, 0, 3) == 0) CreateEffectVehicleRel(v, 0, -17, 2, EV_SMOKE);
00213 
00214     } else if (v->current_order.GetDestination() == 0) {
00215       tile = v->tile;
00216 
00217       if (IsValidTile(tile) &&
00218           IsTileType(tile, MP_STATION) &&
00219           IsAirport(tile)) {
00220         v->current_order.SetDestination(1);
00221         v->age = 0;
00222 
00223         SetDParam(0, GetStationIndex(tile));
00224         AddNewsItem(STR_B000_ZEPPELIN_DISASTER_AT,
00225           NS_ACCIDENT_VEHICLE,
00226           v->index,
00227           0);
00228         AI::NewEvent(GetTileOwner(tile), new AIEventDisasterZeppelinerCrashed(GetStationIndex(tile)));
00229       }
00230     }
00231 
00232     if (v->y_pos >= ((int)MapSizeY() + 9) * TILE_SIZE - 1) delete v;
00233     return;
00234   }
00235 
00236   if (v->current_order.GetDestination() > 2) {
00237     if (++v->age <= 13320) return;
00238 
00239     tile = v->tile;
00240 
00241     if (IsValidTile(tile) &&
00242         IsTileType(tile, MP_STATION) &&
00243         IsAirport(tile)) {
00244       st = GetStationByTile(tile);
00245       CLRBITS(st->airport_flags, RUNWAY_IN_block);
00246       AI::NewEvent(GetTileOwner(tile), new AIEventDisasterZeppelinerCleared(st->index));
00247     }
00248 
00249     SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos);
00250     delete v;
00251     return;
00252   }
00253 
00254   x = v->x_pos;
00255   y = v->y_pos;
00256   z = GetSlopeZ(x, y);
00257   if (z < v->z_pos) z = v->z_pos - 1;
00258   SetDisasterVehiclePos(v, x, y, z);
00259 
00260   if (++v->age == 1) {
00261     CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00262     SndPlayVehicleFx(SND_12_EXPLOSION, v);
00263     v->u.disaster.image_override = SPR_BLIMP_CRASHING;
00264   } else if (v->age == 70) {
00265     v->u.disaster.image_override = SPR_BLIMP_CRASHED;
00266   } else if (v->age <= 300) {
00267     if (GB(v->tick_counter, 0, 3) == 0) {
00268       uint32 r = Random();
00269 
00270       CreateEffectVehicleRel(v,
00271         GB(r, 0, 4) - 7,
00272         GB(r, 4, 4) - 7,
00273         GB(r, 8, 3) + 5,
00274         EV_EXPLOSION_SMALL);
00275     }
00276   } else if (v->age == 350) {
00277     v->current_order.SetDestination(3);
00278     v->age = 0;
00279   }
00280 
00281   tile = v->tile;
00282   if (IsValidTile(tile) &&
00283       IsTileType(tile, MP_STATION) &&
00284       IsAirport(tile)) {
00285     st = GetStationByTile(tile);
00286     SETBITS(st->airport_flags, RUNWAY_IN_block);
00287   }
00288 }
00289 
00296 static void DisasterTick_Ufo(Vehicle *v)
00297 {
00298   Vehicle *u;
00299   uint dist;
00300   byte z;
00301 
00302   v->u.disaster.image_override = (HasBit(++v->tick_counter, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER : SPR_UFO_SMALL_SCOUT;
00303 
00304   if (v->current_order.GetDestination() == 0) {
00305     /* Fly around randomly */
00306     int x = TileX(v->dest_tile) * TILE_SIZE;
00307     int y = TileY(v->dest_tile) * TILE_SIZE;
00308     if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
00309       v->direction = GetDirectionTowards(v, x, y);
00310       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00311       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00312       return;
00313     }
00314     if (++v->age < 6) {
00315       v->dest_tile = RandomTile();
00316       return;
00317     }
00318     v->current_order.SetDestination(1);
00319 
00320     FOR_ALL_VEHICLES(u) {
00321       if (u->type == VEH_ROAD && IsRoadVehFront(u)) {
00322         v->dest_tile = u->index;
00323         v->age = 0;
00324         return;
00325       }
00326     }
00327 
00328     delete v;
00329   } else {
00330     /* Target a vehicle */
00331     u = GetVehicle(v->dest_tile);
00332     if (u->type != VEH_ROAD || !IsRoadVehFront(u)) {
00333       delete v;
00334       return;
00335     }
00336 
00337     dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
00338 
00339     if (dist < TILE_SIZE && !(u->vehstatus & VS_HIDDEN) && u->breakdown_ctr == 0) {
00340       u->breakdown_ctr = 3;
00341       u->breakdown_delay = 140;
00342     }
00343 
00344     v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
00345     GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00346 
00347     z = v->z_pos;
00348     if (dist <= TILE_SIZE && z > u->z_pos) z--;
00349     SetDisasterVehiclePos(v, gp.x, gp.y, z);
00350 
00351     if (z <= u->z_pos && (u->vehstatus & VS_HIDDEN) == 0) {
00352       v->age++;
00353       if (u->u.road.crashed_ctr == 0) {
00354         u->u.road.crashed_ctr++;
00355 
00356         AddNewsItem(STR_B001_ROAD_VEHICLE_DESTROYED,
00357           NS_ACCIDENT_VEHICLE,
00358           u->index,
00359           0);
00360 
00361         AI::NewEvent(u->owner, new AIEventVehicleCrashed(u->index, u->tile, AIEventVehicleCrashed::CRASH_RV_UFO));
00362 
00363         for (Vehicle *w = u; w != NULL; w = w->Next()) {
00364           w->vehstatus |= VS_CRASHED;
00365           MarkSingleVehicleDirty(w);
00366         }
00367       }
00368     }
00369 
00370     /* Destroy? */
00371     if (v->age > 50) {
00372       CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00373       SndPlayVehicleFx(SND_12_EXPLOSION, v);
00374       delete v;
00375     }
00376   }
00377 }
00378 
00379 static void DestructIndustry(Industry *i)
00380 {
00381   TileIndex tile;
00382 
00383   for (tile = 0; tile != MapSize(); tile++) {
00384     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == i->index) {
00385       ResetIndustryConstructionStage(tile);
00386       MarkTileDirtyByTile(tile);
00387     }
00388   }
00389 }
00390 
00399 static void DisasterTick_Airplane(Vehicle *v)
00400 {
00401   v->tick_counter++;
00402   v->u.disaster.image_override =
00403     (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_F_15_FIRING : 0;
00404 
00405   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00406   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00407 
00408   if (gp.x < (-10 * TILE_SIZE)) {
00409     delete v;
00410     return;
00411   }
00412 
00413   if (v->current_order.GetDestination() == 2) {
00414     if (GB(v->tick_counter, 0, 2) == 0) {
00415       Industry *i = GetIndustry(v->dest_tile);
00416       int x = TileX(i->xy) * TILE_SIZE;
00417       int y = TileY(i->xy) * TILE_SIZE;
00418       uint32 r = Random();
00419 
00420       CreateEffectVehicleAbove(
00421         GB(r,  0, 6) + x,
00422         GB(r,  6, 6) + y,
00423         GB(r, 12, 4),
00424         EV_EXPLOSION_SMALL);
00425 
00426       if (++v->age >= 55) v->current_order.SetDestination(3);
00427     }
00428   } else if (v->current_order.GetDestination() == 1) {
00429     if (++v->age == 112) {
00430       Industry *i;
00431 
00432       v->current_order.SetDestination(2);
00433       v->age = 0;
00434 
00435       i = GetIndustry(v->dest_tile);
00436       DestructIndustry(i);
00437 
00438       SetDParam(0, i->town->index);
00439       AddNewsItem(STR_B002_OIL_REFINERY_EXPLOSION, NS_ACCIDENT_TILE, i->xy, 0);
00440       SndPlayTileFx(SND_12_EXPLOSION, i->xy);
00441     }
00442   } else if (v->current_order.GetDestination() == 0) {
00443     int x, y;
00444     TileIndex tile;
00445     uint ind;
00446 
00447     x = v->x_pos - (15 * TILE_SIZE);
00448     y = v->y_pos;
00449 
00450     if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
00451 
00452     tile = TileVirtXY(x, y);
00453     if (!IsTileType(tile, MP_INDUSTRY)) return;
00454 
00455     ind = GetIndustryIndex(tile);
00456     v->dest_tile = ind;
00457 
00458     if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) {
00459       v->current_order.SetDestination(1);
00460       v->age = 0;
00461     }
00462   }
00463 }
00464 
00472 static void DisasterTick_Helicopter(Vehicle *v)
00473 {
00474   v->tick_counter++;
00475   v->u.disaster.image_override =
00476     (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_AH_64A_FIRING : 0;
00477 
00478   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00479   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00480 
00481   if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
00482     delete v;
00483     return;
00484   }
00485 
00486   if (v->current_order.GetDestination() == 2) {
00487     if (GB(v->tick_counter, 0, 2) == 0) {
00488       Industry *i = GetIndustry(v->dest_tile);
00489       int x = TileX(i->xy) * TILE_SIZE;
00490       int y = TileY(i->xy) * TILE_SIZE;
00491       uint32 r = Random();
00492 
00493       CreateEffectVehicleAbove(
00494         GB(r,  0, 6) + x,
00495         GB(r,  6, 6) + y,
00496         GB(r, 12, 4),
00497         EV_EXPLOSION_SMALL);
00498 
00499       if (++v->age >= 55) v->current_order.SetDestination(3);
00500     }
00501   } else if (v->current_order.GetDestination() == 1) {
00502     if (++v->age == 112) {
00503       Industry *i;
00504 
00505       v->current_order.SetDestination(2);
00506       v->age = 0;
00507 
00508       i = GetIndustry(v->dest_tile);
00509       DestructIndustry(i);
00510 
00511       SetDParam(0, i->town->index);
00512       AddNewsItem(STR_B003_FACTORY_DESTROYED_IN_SUSPICIOUS, NS_ACCIDENT_TILE, i->xy, 0);
00513       SndPlayTileFx(SND_12_EXPLOSION, i->xy);
00514     }
00515   } else if (v->current_order.GetDestination() == 0) {
00516     int x, y;
00517     TileIndex tile;
00518     uint ind;
00519 
00520     x = v->x_pos + (15 * TILE_SIZE);
00521     y = v->y_pos;
00522 
00523     if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
00524 
00525     tile = TileVirtXY(x, y);
00526     if (!IsTileType(tile, MP_INDUSTRY)) return;
00527 
00528     ind = GetIndustryIndex(tile);
00529     v->dest_tile = ind;
00530 
00531     if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) {
00532       v->current_order.SetDestination(1);
00533       v->age = 0;
00534     }
00535   }
00536 }
00537 
00539 static void DisasterTick_Helicopter_Rotors(Vehicle *v)
00540 {
00541   v->tick_counter++;
00542   if (HasBit(v->tick_counter, 0)) return;
00543 
00544   if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
00545 
00546   VehiclePositionChanged(v);
00547   MarkSingleVehicleDirty(v);
00548 }
00549 
00556 static void DisasterTick_Big_Ufo(Vehicle *v)
00557 {
00558   byte z;
00559   Town *t;
00560   TileIndex tile;
00561   TileIndex tile_org;
00562 
00563   v->tick_counter++;
00564 
00565   if (v->current_order.GetDestination() == 1) {
00566     int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00567     int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00568     if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
00569       v->direction = GetDirectionTowards(v, x, y);
00570 
00571       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00572       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00573       return;
00574     }
00575 
00576     z = GetSlopeZ(v->x_pos, v->y_pos);
00577     if (z < v->z_pos) {
00578       SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos - 1);
00579       return;
00580     }
00581 
00582     v->current_order.SetDestination(2);
00583 
00584     Vehicle *u;
00585     FOR_ALL_VEHICLES(u) {
00586       if (u->type == VEH_TRAIN || u->type == VEH_ROAD) {
00587         if (Delta(u->x_pos, v->x_pos) + Delta(u->y_pos, v->y_pos) <= 12 * TILE_SIZE) {
00588           u->breakdown_ctr = 5;
00589           u->breakdown_delay = 0xF0;
00590         }
00591       }
00592     }
00593 
00594     t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
00595     SetDParam(0, t->index);
00596     AddNewsItem(STR_B004_UFO_LANDS_NEAR,
00597       NS_ACCIDENT_TILE,
00598       v->tile,
00599       0);
00600 
00601     if (!Vehicle::CanAllocateItem(2)) {
00602       delete v;
00603       return;
00604     }
00605     u = new DisasterVehicle();
00606 
00607     InitializeDisasterVehicle(u, -6 * TILE_SIZE, v->y_pos, 135, DIR_SW, ST_Big_Ufo_Destroyer);
00608     u->u.disaster.big_ufo_destroyer_target = v->index;
00609 
00610     Vehicle *w = new DisasterVehicle();
00611 
00612     u->SetNext(w);
00613     InitializeDisasterVehicle(w, -6 * TILE_SIZE, v->y_pos, 0, DIR_SW, ST_Big_Ufo_Destroyer_Shadow);
00614     w->vehstatus |= VS_SHADOW;
00615   } else if (v->current_order.GetDestination() == 0) {
00616     int x = TileX(v->dest_tile) * TILE_SIZE;
00617     int y = TileY(v->dest_tile) * TILE_SIZE;
00618     if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
00619       v->direction = GetDirectionTowards(v, x, y);
00620       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00621       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00622       return;
00623     }
00624 
00625     if (++v->age < 6) {
00626       v->dest_tile = RandomTile();
00627       return;
00628     }
00629     v->current_order.SetDestination(1);
00630 
00631     tile_org = tile = RandomTile();
00632     do {
00633       if (IsTileType(tile, MP_RAILWAY) &&
00634           IsPlainRailTile(tile) &&
00635           IsHumanCompany(GetTileOwner(tile))) {
00636         break;
00637       }
00638       tile = TILE_MASK(tile + 1);
00639     } while (tile != tile_org);
00640     v->dest_tile = tile;
00641     v->age = 0;
00642   } else {
00643     return;
00644   }
00645 }
00646 
00651 static void DisasterTick_Big_Ufo_Destroyer(Vehicle *v)
00652 {
00653   Vehicle *u;
00654   int i;
00655 
00656   v->tick_counter++;
00657 
00658   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00659   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00660 
00661   if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
00662     delete v;
00663     return;
00664   }
00665 
00666   if (v->current_order.GetDestination() == 0) {
00667     u = GetVehicle(v->u.disaster.big_ufo_destroyer_target);
00668     if (Delta(v->x_pos, u->x_pos) > TILE_SIZE) return;
00669     v->current_order.SetDestination(1);
00670 
00671     CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
00672     SndPlayVehicleFx(SND_12_EXPLOSION, u);
00673 
00674     delete u;
00675 
00676     for (i = 0; i != 80; i++) {
00677       uint32 r = Random();
00678       CreateEffectVehicleAbove(
00679         GB(r, 0, 6) + v->x_pos - 32,
00680         GB(r, 5, 6) + v->y_pos - 32,
00681         0,
00682         EV_EXPLOSION_SMALL);
00683     }
00684 
00685     for (int dy = -3; dy < 3; dy++) {
00686       for (int dx = -3; dx < 3; dx++) {
00687         TileIndex tile = TileAddWrap(v->tile, dx, dy);
00688         if (tile != INVALID_TILE) DisasterClearSquare(tile);
00689       }
00690     }
00691   }
00692 }
00693 
00698 static void DisasterTick_Submarine(Vehicle *v)
00699 {
00700   TileIndex tile;
00701 
00702   v->tick_counter++;
00703 
00704   if (++v->age > 8880) {
00705     VehiclePositionChanged(v);
00706     MarkSingleVehicleDirty(v);
00707     delete v;
00708     return;
00709   }
00710 
00711   if (!HasBit(v->tick_counter, 0)) return;
00712 
00713   tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
00714   if (IsValidTile(tile)) {
00715     TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00716     if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
00717       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00718       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00719       return;
00720     }
00721   }
00722 
00723   v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
00724 }
00725 
00726 
00727 static void DisasterTick_NULL(Vehicle *v) {}
00728 typedef void DisasterVehicleTickProc(Vehicle *v);
00729 
00730 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
00731   DisasterTick_Zeppeliner, DisasterTick_NULL,
00732   DisasterTick_Ufo,        DisasterTick_NULL,
00733   DisasterTick_Airplane,   DisasterTick_NULL,
00734   DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
00735   DisasterTick_Big_Ufo,    DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
00736   DisasterTick_NULL,
00737   DisasterTick_Submarine,
00738   DisasterTick_Submarine,
00739 };
00740 
00741 
00742 void DisasterVehicle::Tick()
00743 {
00744   _disastervehicle_tick_procs[this->subtype](this);
00745 }
00746 
00747 typedef void DisasterInitProc();
00748 
00749 
00752 static void Disaster_Zeppeliner_Init()
00753 {
00754   if (!Vehicle::CanAllocateItem(2)) return;
00755 
00756   Vehicle *v = new DisasterVehicle();
00757   Station *st;
00758 
00759   /* Pick a random place, unless we find a small airport */
00760   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00761 
00762   FOR_ALL_STATIONS(st) {
00763     if (st->airport_tile != INVALID_TILE && (st->airport_type == AT_SMALL || st->airport_type == AT_LARGE)) {
00764       x = (TileX(st->airport_tile) + 2) * TILE_SIZE;
00765       break;
00766     }
00767   }
00768 
00769   InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_Zeppeliner);
00770 
00771   /* Allocate shadow */
00772   Vehicle *u = new DisasterVehicle();
00773   v->SetNext(u);
00774   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_Zeppeliner_Shadow);
00775   u->vehstatus |= VS_SHADOW;
00776 }
00777 
00778 
00781 static void Disaster_Small_Ufo_Init()
00782 {
00783   if (!Vehicle::CanAllocateItem(2)) return;
00784 
00785   Vehicle *v = new DisasterVehicle();
00786   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00787 
00788   InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_Small_Ufo);
00789   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00790   v->age = 0;
00791 
00792   /* Allocate shadow */
00793   Vehicle *u = new DisasterVehicle();
00794   v->SetNext(u);
00795   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_Small_Ufo_Shadow);
00796   u->vehstatus |= VS_SHADOW;
00797 }
00798 
00799 
00800 /* Combat airplane which destroys an oil refinery */
00801 static void Disaster_Airplane_Init()
00802 {
00803   if (!Vehicle::CanAllocateItem(2)) return;
00804 
00805   Industry *i, *found = NULL;
00806 
00807   FOR_ALL_INDUSTRIES(i) {
00808     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
00809         (found == NULL || Chance16(1, 2))) {
00810       found = i;
00811     }
00812   }
00813 
00814   if (found == NULL) return;
00815 
00816   Vehicle *v = new DisasterVehicle();
00817 
00818   /* Start from the bottom (south side) of the map */
00819   int x = (MapSizeX() + 9) * TILE_SIZE - 1;
00820   int y = TileY(found->xy) * TILE_SIZE + 37;
00821 
00822   InitializeDisasterVehicle(v, x, y, 135, DIR_NE, ST_Airplane);
00823 
00824   Vehicle *u = new DisasterVehicle();
00825   v->SetNext(u);
00826   InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_Airplane_Shadow);
00827   u->vehstatus |= VS_SHADOW;
00828 }
00829 
00830 
00832 static void Disaster_Helicopter_Init()
00833 {
00834   if (!Vehicle::CanAllocateItem(3)) return;
00835 
00836   Industry *i, *found = NULL;
00837 
00838   FOR_ALL_INDUSTRIES(i) {
00839     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
00840         (found == NULL || Chance16(1, 2))) {
00841       found = i;
00842     }
00843   }
00844 
00845   if (found == NULL) return;
00846 
00847   Vehicle *v = new DisasterVehicle();
00848 
00849   int x = -16 * TILE_SIZE;
00850   int y = TileY(found->xy) * TILE_SIZE + 37;
00851 
00852   InitializeDisasterVehicle(v, x, y, 135, DIR_SW, ST_Helicopter);
00853 
00854   Vehicle *u = new DisasterVehicle();
00855   v->SetNext(u);
00856   InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_Helicopter_Shadow);
00857   u->vehstatus |= VS_SHADOW;
00858 
00859   Vehicle *w = new DisasterVehicle();
00860   u->SetNext(w);
00861   InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_Helicopter_Rotors);
00862 }
00863 
00864 
00865 /* Big Ufo which lands on a piece of rail and will consequently be shot
00866  * down by a combat airplane, destroying the surroundings */
00867 static void Disaster_Big_Ufo_Init()
00868 {
00869   if (!Vehicle::CanAllocateItem(2)) return;
00870 
00871   Vehicle *v = new DisasterVehicle();
00872   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00873   int y = MapMaxX() * TILE_SIZE - 1;
00874 
00875   InitializeDisasterVehicle(v, x, y, 135, DIR_NW, ST_Big_Ufo);
00876   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00877   v->age = 0;
00878 
00879   /* Allocate shadow */
00880   Vehicle *u = new DisasterVehicle();
00881   v->SetNext(u);
00882   InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_Big_Ufo_Shadow);
00883   u->vehstatus |= VS_SHADOW;
00884 }
00885 
00886 
00887 /* Curious submarine #1, just floats around */
00888 static void Disaster_Small_Submarine_Init()
00889 {
00890   if (!Vehicle::CanAllocateItem()) return;
00891 
00892   Vehicle *v = new DisasterVehicle();
00893   int y;
00894   Direction dir;
00895   uint32 r = Random();
00896   int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
00897 
00898   if (HasBit(r, 31)) {
00899     y = MapMaxX() * TILE_SIZE - TILE_SIZE / 2 - 1;
00900     dir = DIR_NW;
00901   } else {
00902     y = TILE_SIZE / 2;
00903     dir = DIR_SE;
00904   }
00905   InitializeDisasterVehicle(v, x, y, 0, dir, ST_Small_Submarine);
00906   v->age = 0;
00907 }
00908 
00909 
00910 /* Curious submarine #2, just floats around */
00911 static void Disaster_Big_Submarine_Init()
00912 {
00913   if (!Vehicle::CanAllocateItem()) return;
00914 
00915   Vehicle *v = new DisasterVehicle();
00916   int y;
00917   Direction dir;
00918   uint32 r = Random();
00919   int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
00920 
00921   if (HasBit(r, 31)) {
00922     y = MapMaxX() * TILE_SIZE - TILE_SIZE / 2 - 1;
00923     dir = DIR_NW;
00924   } else {
00925     y = TILE_SIZE / 2;
00926     dir = DIR_SE;
00927   }
00928   InitializeDisasterVehicle(v, x, y, 0, dir, ST_Big_Submarine);
00929   v->age = 0;
00930 }
00931 
00932 
00935 static void Disaster_CoalMine_Init()
00936 {
00937   int index = GB(Random(), 0, 4);
00938   uint m;
00939 
00940   for (m = 0; m < 15; m++) {
00941     const Industry *i;
00942 
00943     FOR_ALL_INDUSTRIES(i) {
00944       if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
00945         SetDParam(0, i->town->index);
00946         AddNewsItem(STR_B005_COAL_MINE_SUBSIDENCE_LEAVES,
00947           NS_ACCIDENT_TILE, i->xy + TileDiffXY(1, 1), 0);
00948 
00949         {
00950           TileIndex tile = i->xy;
00951           TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
00952 
00953           for (uint n = 0; n < 30; n++) {
00954             DisasterClearSquare(tile);
00955             tile += step;
00956             if (!IsValidTile(tile)) break;
00957           }
00958         }
00959         return;
00960       }
00961     }
00962   }
00963 }
00964 
00965 static DisasterInitProc * const _disaster_initprocs[] = {
00966   Disaster_Zeppeliner_Init,
00967   Disaster_Small_Ufo_Init,
00968   Disaster_Airplane_Init,
00969   Disaster_Helicopter_Init,
00970   Disaster_Big_Ufo_Init,
00971   Disaster_Small_Submarine_Init,
00972   Disaster_Big_Submarine_Init,
00973   Disaster_CoalMine_Init,
00974 };
00975 
00976 static const struct {
00977   Year min;
00978   Year max;
00979 } _dis_years[] = {
00980   { 1930, 1955 }, 
00981   { 1940, 1970 }, 
00982   { 1960, 1990 }, 
00983   { 1970, 2000 }, 
00984   { 2000, 2100 }, 
00985   { 1940, 1965 }, 
00986   { 1975, 2010 }, 
00987   { 1950, 1985 }  
00988 };
00989 
00990 
00991 static void DoDisaster()
00992 {
00993   byte buf[lengthof(_dis_years)];
00994   uint i;
00995   uint j;
00996 
00997   j = 0;
00998   for (i = 0; i != lengthof(_dis_years); i++) {
00999     if (_cur_year >= _dis_years[i].min && _cur_year < _dis_years[i].max) buf[j++] = i;
01000   }
01001 
01002   if (j == 0) return;
01003 
01004   _disaster_initprocs[buf[RandomRange(j)]]();
01005 }
01006 
01007 
01008 static void ResetDisasterDelay()
01009 {
01010   _disaster_delay = GB(Random(), 0, 9) + 730;
01011 }
01012 
01013 void DisasterDailyLoop()
01014 {
01015   if (--_disaster_delay != 0) return;
01016 
01017   ResetDisasterDelay();
01018 
01019   if (_settings_game.difficulty.disasters != 0) DoDisaster();
01020 }
01021 
01022 void StartupDisasters()
01023 {
01024   ResetDisasterDelay();
01025 }
01026 
01031 void ReleaseDisastersTargetingIndustry(IndustryID i)
01032 {
01033   Vehicle *v;
01034   FOR_ALL_VEHICLES(v) {
01035     /* primary disaster vehicles that have chosen target */
01036     if (v->type == VEH_DISASTER && (v->subtype == ST_Airplane || v->subtype == ST_Helicopter)) {
01037       /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
01038       if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
01039     }
01040   }
01041 }
01042 
01043 void DisasterVehicle::UpdateDeltaXY(Direction direction)
01044 {
01045   this->x_offs        = -1;
01046   this->y_offs        = -1;
01047   this->x_extent      =  2;
01048   this->y_extent      =  2;
01049   this->z_extent      =  5;
01050 }

Generated on Mon Mar 9 23:33:46 2009 for openttd by  doxygen 1.5.6