# disaster_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: disaster_cmd.cpp 16539 2009-06-08 22:53:39Z rubidium $ */
    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   VehicleMove(v, false);
    00139   MarkSingleVehicleDirty(v);
    00140 }
    00141
    00142 static void SetDisasterVehiclePos(Vehicle *v, int x, int y, byte z)
    00143 {
    00144   Vehicle *u;
    00145
    00146   v->x_pos = x;
    00147   v->y_pos = y;
    00148   v->z_pos = z;
    00149   v->tile = TileVirtXY(x, y);
    00150
    00151   DisasterVehicleUpdateImage(v);
    00152   VehicleMove(v, true);
    00153
    00154   if ((u = v->Next()) != NULL) {
    00155     int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
    00156     int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
    00157
    00158     u->x_pos = x;
    00159     u->y_pos = y - 1 - (max(z - GetSlopeZ(safe_x, safe_y), 0U) >> 3);
    00160     safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
    00161     u->z_pos = GetSlopeZ(safe_x, safe_y);
    00162     u->direction = v->direction;
    00163
    00164     DisasterVehicleUpdateImage(u);
    00165     VehicleMove(u, true);
    00166
    00167     if ((u = u->Next()) != NULL) {
    00168       u->x_pos = x;
    00169       u->y_pos = y;
    00170       u->z_pos = z + 5;
    00171       VehicleMove(u, true);
    00172     }
    00173   }
    00174 }
    00175
    00184 static void DisasterTick_Zeppeliner(Vehicle *v)
    00185 {
    00186   Station *st;
    00187   int x, y;
    00188   byte z;
    00189   TileIndex tile;
    00190
    00191   v->tick_counter++;
    00192
    00193   if (v->current_order.GetDestination() < 2) {
    00194     if (HasBit(v->tick_counter, 0)) return;
    00195
    00196     GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00197
    00198     SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00199
    00200     if (v->current_order.GetDestination() == 1) {
    00201       if (++v->age == 38) {
    00202         v->current_order.SetDestination(2);
    00203         v->age = 0;
    00204       }
    00205
    00206       if (GB(v->tick_counter, 0, 3) == 0) CreateEffectVehicleRel(v, 0, -17, 2, EV_SMOKE);
    00207
    00208     } else if (v->current_order.GetDestination() == 0) {
    00209       tile = v->tile;
    00210
    00211       if (IsValidTile(tile) &&
    00212           IsTileType(tile, MP_STATION) &&
    00213           IsAirport(tile)) {
    00214         v->current_order.SetDestination(1);
    00215         v->age = 0;
    00216
    00217         SetDParam(0, GetStationIndex(tile));
    00218         AddNewsItem(STR_B000_ZEPPELIN_DISASTER_AT,
    00219           NS_ACCIDENT_VEHICLE,
    00220           v->index,
    00221           0);
    00222         AI::NewEvent(GetTileOwner(tile), new AIEventDisasterZeppelinerCrashed(GetStationIndex(tile)));
    00223       }
    00224     }
    00225
    00226     if (v->y_pos >= ((int)MapSizeY() + 9) * TILE_SIZE - 1) delete v;
    00227     return;
    00228   }
    00229
    00230   if (v->current_order.GetDestination() > 2) {
    00231     if (++v->age <= 13320) return;
    00232
    00233     tile = v->tile;
    00234
    00235     if (IsValidTile(tile) &&
    00236         IsTileType(tile, MP_STATION) &&
    00237         IsAirport(tile)) {
    00238       st = GetStationByTile(tile);
    00239       CLRBITS(st->airport_flags, RUNWAY_IN_block);
    00240       AI::NewEvent(GetTileOwner(tile), new AIEventDisasterZeppelinerCleared(st->index));
    00241     }
    00242
    00243     SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos);
    00244     delete v;
    00245     return;
    00246   }
    00247
    00248   x = v->x_pos;
    00249   y = v->y_pos;
    00250   z = GetSlopeZ(x, y);
    00251   if (z < v->z_pos) z = v->z_pos - 1;
    00252   SetDisasterVehiclePos(v, x, y, z);
    00253
    00254   if (++v->age == 1) {
    00255     CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
    00256     SndPlayVehicleFx(SND_12_EXPLOSION, v);
    00257     v->u.disaster.image_override = SPR_BLIMP_CRASHING;
    00258   } else if (v->age == 70) {
    00259     v->u.disaster.image_override = SPR_BLIMP_CRASHED;
    00260   } else if (v->age <= 300) {
    00261     if (GB(v->tick_counter, 0, 3) == 0) {
    00262       uint32 r = Random();
    00263
    00264       CreateEffectVehicleRel(v,
    00265         GB(r, 0, 4) - 7,
    00266         GB(r, 4, 4) - 7,
    00267         GB(r, 8, 3) + 5,
    00268         EV_EXPLOSION_SMALL);
    00269     }
    00270   } else if (v->age == 350) {
    00271     v->current_order.SetDestination(3);
    00272     v->age = 0;
    00273   }
    00274
    00275   tile = v->tile;
    00276   if (IsValidTile(tile) &&
    00277       IsTileType(tile, MP_STATION) &&
    00278       IsAirport(tile)) {
    00279     st = GetStationByTile(tile);
    00280     SETBITS(st->airport_flags, RUNWAY_IN_block);
    00281   }
    00282 }
    00283
    00290 static void DisasterTick_Ufo(Vehicle *v)
    00291 {
    00292   Vehicle *u;
    00293   uint dist;
    00294   byte z;
    00295
    00296   v->u.disaster.image_override = (HasBit(++v->tick_counter, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER : SPR_UFO_SMALL_SCOUT;
    00297
    00298   if (v->current_order.GetDestination() == 0) {
    00299     /* Fly around randomly */
    00300     int x = TileX(v->dest_tile) * TILE_SIZE;
    00301     int y = TileY(v->dest_tile) * TILE_SIZE;
    00302     if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
    00303       v->direction = GetDirectionTowards(v, x, y);
    00304       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00305       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00306       return;
    00307     }
    00308     if (++v->age < 6) {
    00309       v->dest_tile = RandomTile();
    00310       return;
    00311     }
    00312     v->current_order.SetDestination(1);
    00313
    00314     FOR_ALL_VEHICLES(u) {
    00315       if (u->type == VEH_ROAD && IsRoadVehFront(u)) {
    00316         v->dest_tile = u->index;
    00317         v->age = 0;
    00318         return;
    00319       }
    00320     }
    00321
    00322     delete v;
    00323   } else {
    00324     /* Target a vehicle */
    00325     u = GetVehicle(v->dest_tile);
    00326     assert(u->type == VEH_ROAD && IsRoadVehFront(u));
    00327
    00328     dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
    00329
    00330     if (dist < TILE_SIZE && !(u->vehstatus & VS_HIDDEN) && u->breakdown_ctr == 0) {
    00331       u->breakdown_ctr = 3;
    00332       u->breakdown_delay = 140;
    00333     }
    00334
    00335     v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
    00336     GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00337
    00338     z = v->z_pos;
    00339     if (dist <= TILE_SIZE && z > u->z_pos) z--;
    00340     SetDisasterVehiclePos(v, gp.x, gp.y, z);
    00341
    00342     if (z <= u->z_pos && (u->vehstatus & VS_HIDDEN) == 0) {
    00343       v->age++;
    00344       if (u->u.road.crashed_ctr == 0) {
    00345         u->u.road.crashed_ctr++;
    00346
    00347         AddNewsItem(STR_B001_ROAD_VEHICLE_DESTROYED,
    00348           NS_ACCIDENT_VEHICLE,
    00349           u->index,
    00350           0);
    00351
    00352         AI::NewEvent(u->owner, new AIEventVehicleCrashed(u->index, u->tile, AIEventVehicleCrashed::CRASH_RV_UFO));
    00353
    00354         for (Vehicle *w = u; w != NULL; w = w->Next()) {
    00355           w->vehstatus |= VS_CRASHED;
    00356           MarkSingleVehicleDirty(w);
    00357         }
    00358       }
    00359     }
    00360
    00361     /* Destroy? */
    00362     if (v->age > 50) {
    00363       CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
    00364       SndPlayVehicleFx(SND_12_EXPLOSION, v);
    00365       delete v;
    00366     }
    00367   }
    00368 }
    00369
    00370 static void DestructIndustry(Industry *i)
    00371 {
    00372   TileIndex tile;
    00373
    00374   for (tile = 0; tile != MapSize(); tile++) {
    00375     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == i->index) {
    00376       ResetIndustryConstructionStage(tile);
    00377       MarkTileDirtyByTile(tile);
    00378     }
    00379   }
    00380 }
    00381
    00390 static void DisasterTick_Airplane(Vehicle *v)
    00391 {
    00392   v->tick_counter++;
    00393   v->u.disaster.image_override =
    00394     (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_F_15_FIRING : 0;
    00395
    00396   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00397   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00398
    00399   if (gp.x < (-10 * TILE_SIZE)) {
    00400     delete v;
    00401     return;
    00402   }
    00403
    00404   if (v->current_order.GetDestination() == 2) {
    00405     if (GB(v->tick_counter, 0, 2) == 0) {
    00406       Industry *i = GetIndustry(v->dest_tile);
    00407       int x = TileX(i->xy) * TILE_SIZE;
    00408       int y = TileY(i->xy) * TILE_SIZE;
    00409       uint32 r = Random();
    00410
    00411       CreateEffectVehicleAbove(
    00412         GB(r,  0, 6) + x,
    00413         GB(r,  6, 6) + y,
    00414         GB(r, 12, 4),
    00415         EV_EXPLOSION_SMALL);
    00416
    00417       if (++v->age >= 55) v->current_order.SetDestination(3);
    00418     }
    00419   } else if (v->current_order.GetDestination() == 1) {
    00420     if (++v->age == 112) {
    00421       Industry *i;
    00422
    00423       v->current_order.SetDestination(2);
    00424       v->age = 0;
    00425
    00426       i = GetIndustry(v->dest_tile);
    00427       DestructIndustry(i);
    00428
    00429       SetDParam(0, i->town->index);
    00430       AddNewsItem(STR_B002_OIL_REFINERY_EXPLOSION, NS_ACCIDENT_TILE, i->xy, 0);
    00431       SndPlayTileFx(SND_12_EXPLOSION, i->xy);
    00432     }
    00433   } else if (v->current_order.GetDestination() == 0) {
    00434     int x, y;
    00435     TileIndex tile;
    00436     uint ind;
    00437
    00438     x = v->x_pos - (15 * TILE_SIZE);
    00439     y = v->y_pos;
    00440
    00441     if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
    00442
    00443     tile = TileVirtXY(x, y);
    00444     if (!IsTileType(tile, MP_INDUSTRY)) return;
    00445
    00446     ind = GetIndustryIndex(tile);
    00447     v->dest_tile = ind;
    00448
    00449     if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) {
    00450       v->current_order.SetDestination(1);
    00451       v->age = 0;
    00452     }
    00453   }
    00454 }
    00455
    00463 static void DisasterTick_Helicopter(Vehicle *v)
    00464 {
    00465   v->tick_counter++;
    00466   v->u.disaster.image_override =
    00467     (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_AH_64A_FIRING : 0;
    00468
    00469   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00470   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00471
    00472   if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
    00473     delete v;
    00474     return;
    00475   }
    00476
    00477   if (v->current_order.GetDestination() == 2) {
    00478     if (GB(v->tick_counter, 0, 2) == 0) {
    00479       Industry *i = GetIndustry(v->dest_tile);
    00480       int x = TileX(i->xy) * TILE_SIZE;
    00481       int y = TileY(i->xy) * TILE_SIZE;
    00482       uint32 r = Random();
    00483
    00484       CreateEffectVehicleAbove(
    00485         GB(r,  0, 6) + x,
    00486         GB(r,  6, 6) + y,
    00487         GB(r, 12, 4),
    00488         EV_EXPLOSION_SMALL);
    00489
    00490       if (++v->age >= 55) v->current_order.SetDestination(3);
    00491     }
    00492   } else if (v->current_order.GetDestination() == 1) {
    00493     if (++v->age == 112) {
    00494       Industry *i;
    00495
    00496       v->current_order.SetDestination(2);
    00497       v->age = 0;
    00498
    00499       i = GetIndustry(v->dest_tile);
    00500       DestructIndustry(i);
    00501
    00502       SetDParam(0, i->town->index);
    00503       AddNewsItem(STR_B003_FACTORY_DESTROYED_IN_SUSPICIOUS, NS_ACCIDENT_TILE, i->xy, 0);
    00504       SndPlayTileFx(SND_12_EXPLOSION, i->xy);
    00505     }
    00506   } else if (v->current_order.GetDestination() == 0) {
    00507     int x, y;
    00508     TileIndex tile;
    00509     uint ind;
    00510
    00511     x = v->x_pos + (15 * TILE_SIZE);
    00512     y = v->y_pos;
    00513
    00514     if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
    00515
    00516     tile = TileVirtXY(x, y);
    00517     if (!IsTileType(tile, MP_INDUSTRY)) return;
    00518
    00519     ind = GetIndustryIndex(tile);
    00520     v->dest_tile = ind;
    00521
    00522     if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) {
    00523       v->current_order.SetDestination(1);
    00524       v->age = 0;
    00525     }
    00526   }
    00527 }
    00528
    00530 static void DisasterTick_Helicopter_Rotors(Vehicle *v)
    00531 {
    00532   v->tick_counter++;
    00533   if (HasBit(v->tick_counter, 0)) return;
    00534
    00535   if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
    00536
    00537   VehicleMove(v, true);
    00538 }
    00539
    00546 static void DisasterTick_Big_Ufo(Vehicle *v)
    00547 {
    00548   byte z;
    00549   Town *t;
    00550   TileIndex tile;
    00551   TileIndex tile_org;
    00552
    00553   v->tick_counter++;
    00554
    00555   if (v->current_order.GetDestination() == 1) {
    00556     int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
    00557     int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
    00558     if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
    00559       v->direction = GetDirectionTowards(v, x, y);
    00560
    00561       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00562       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00563       return;
    00564     }
    00565
    00566     if (!IsValidTile(v->dest_tile)) {
    00567       /* Make sure we don't land outside the map. */
    00568       delete v;
    00569       return;
    00570     }
    00571
    00572     z = GetSlopeZ(v->x_pos, v->y_pos);
    00573     if (z < v->z_pos) {
    00574       SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos - 1);
    00575       return;
    00576     }
    00577
    00578     v->current_order.SetDestination(2);
    00579
    00580     Vehicle *u;
    00581     FOR_ALL_VEHICLES(u) {
    00582       if (u->type == VEH_TRAIN || u->type == VEH_ROAD) {
    00583         if (Delta(u->x_pos, v->x_pos) + Delta(u->y_pos, v->y_pos) <= 12 * TILE_SIZE) {
    00584           u->breakdown_ctr = 5;
    00585           u->breakdown_delay = 0xF0;
    00586         }
    00587       }
    00588     }
    00589
    00590     t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
    00591     SetDParam(0, t->index);
    00592     AddNewsItem(STR_B004_UFO_LANDS_NEAR,
    00593       NS_ACCIDENT_TILE,
    00594       v->tile,
    00595       0);
    00596
    00597     if (!Vehicle::CanAllocateItem(2)) {
    00598       delete v;
    00599       return;
    00600     }
    00601     u = new DisasterVehicle();
    00602
    00603     InitializeDisasterVehicle(u, -6 * TILE_SIZE, v->y_pos, 135, DIR_SW, ST_BIG_UFO_DESTROYER);
    00604     u->u.disaster.big_ufo_destroyer_target = v->index;
    00605
    00606     Vehicle *w = new DisasterVehicle();
    00607
    00608     u->SetNext(w);
    00609     InitializeDisasterVehicle(w, -6 * TILE_SIZE, v->y_pos, 0, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
    00610     w->vehstatus |= VS_SHADOW;
    00611   } else if (v->current_order.GetDestination() == 0) {
    00612     int x = TileX(v->dest_tile) * TILE_SIZE;
    00613     int y = TileY(v->dest_tile) * TILE_SIZE;
    00614     if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
    00615       v->direction = GetDirectionTowards(v, x, y);
    00616       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00617       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00618       return;
    00619     }
    00620
    00621     if (++v->age < 6) {
    00622       v->dest_tile = RandomTile();
    00623       return;
    00624     }
    00625     v->current_order.SetDestination(1);
    00626
    00627     tile_org = tile = RandomTile();
    00628     do {
    00629       if (IsTileType(tile, MP_RAILWAY) &&
    00630           IsPlainRailTile(tile) &&
    00631           IsHumanCompany(GetTileOwner(tile))) {
    00632         break;
    00633       }
    00634       tile = TILE_MASK(tile + 1);
    00635     } while (tile != tile_org);
    00636     v->dest_tile = tile;
    00637     v->age = 0;
    00638   } else {
    00639     return;
    00640   }
    00641 }
    00642
    00647 static void DisasterTick_Big_Ufo_Destroyer(Vehicle *v)
    00648 {
    00649   Vehicle *u;
    00650   int i;
    00651
    00652   v->tick_counter++;
    00653
    00654   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00655   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00656
    00657   if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
    00658     delete v;
    00659     return;
    00660   }
    00661
    00662   if (v->current_order.GetDestination() == 0) {
    00663     u = GetVehicle(v->u.disaster.big_ufo_destroyer_target);
    00664     if (Delta(v->x_pos, u->x_pos) > TILE_SIZE) return;
    00665     v->current_order.SetDestination(1);
    00666
    00667     CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
    00668     SndPlayVehicleFx(SND_12_EXPLOSION, u);
    00669
    00670     delete u;
    00671
    00672     for (i = 0; i != 80; i++) {
    00673       uint32 r = Random();
    00674       CreateEffectVehicleAbove(
    00675         GB(r, 0, 6) + v->x_pos - 32,
    00676         GB(r, 5, 6) + v->y_pos - 32,
    00677         0,
    00678         EV_EXPLOSION_SMALL);
    00679     }
    00680
    00681     for (int dy = -3; dy < 3; dy++) {
    00682       for (int dx = -3; dx < 3; dx++) {
    00683         TileIndex tile = TileAddWrap(v->tile, dx, dy);
    00684         if (tile != INVALID_TILE) DisasterClearSquare(tile);
    00685       }
    00686     }
    00687   }
    00688 }
    00689
    00694 static void DisasterTick_Submarine(Vehicle *v)
    00695 {
    00696   TileIndex tile;
    00697
    00698   v->tick_counter++;
    00699
    00700   if (++v->age > 8880) {
    00701     delete v;
    00702     return;
    00703   }
    00704
    00705   if (!HasBit(v->tick_counter, 0)) return;
    00706
    00707   tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
    00708   if (IsValidTile(tile)) {
    00709     TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
    00710     if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
    00711       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00712       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00713       return;
    00714     }
    00715   }
    00716
    00717   v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
    00718 }
    00719
    00720
    00721 static void DisasterTick_NULL(Vehicle *v) {}
    00722 typedef void DisasterVehicleTickProc(Vehicle *v);
    00723
    00724 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
    00725   DisasterTick_Zeppeliner, DisasterTick_NULL,
    00726   DisasterTick_Ufo,        DisasterTick_NULL,
    00727   DisasterTick_Airplane,   DisasterTick_NULL,
    00728   DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
    00729   DisasterTick_Big_Ufo,    DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
    00730   DisasterTick_NULL,
    00731   DisasterTick_Submarine,
    00732   DisasterTick_Submarine,
    00733 };
    00734
    00735
    00736 void DisasterVehicle::Tick()
    00737 {
    00738   _disastervehicle_tick_procs[this->subtype](this);
    00739 }
    00740
    00741 typedef void DisasterInitProc();
    00742
    00743
    00746 static void Disaster_Zeppeliner_Init()
    00747 {
    00748   if (!Vehicle::CanAllocateItem(2)) return;
    00749
    00750   Vehicle *v = new DisasterVehicle();
    00751   Station *st;
    00752
    00753   /* Pick a random place, unless we find a small airport */
    00754   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
    00755
    00756   FOR_ALL_STATIONS(st) {
    00757     if (st->airport_tile != INVALID_TILE && (st->airport_type == AT_SMALL || st->airport_type == AT_LARGE)) {
    00758       x = (TileX(st->airport_tile) + 2) * TILE_SIZE;
    00759       break;
    00760     }
    00761   }
    00762
    00763   InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_ZEPPELINER);
    00764
    00765   /* Allocate shadow */
    00766   Vehicle *u = new DisasterVehicle();
    00767   v->SetNext(u);
    00768   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
    00769   u->vehstatus |= VS_SHADOW;
    00770 }
    00771
    00772
    00775 static void Disaster_Small_Ufo_Init()
    00776 {
    00777   if (!Vehicle::CanAllocateItem(2)) return;
    00778
    00779   Vehicle *v = new DisasterVehicle();
    00780   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
    00781
    00782   InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_SMALL_UFO);
    00783   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
    00784   v->age = 0;
    00785
    00786   /* Allocate shadow */
    00787   Vehicle *u = new DisasterVehicle();
    00788   v->SetNext(u);
    00789   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
    00790   u->vehstatus |= VS_SHADOW;
    00791 }
    00792
    00793
    00794 /* Combat airplane which destroys an oil refinery */
    00795 static void Disaster_Airplane_Init()
    00796 {
    00797   if (!Vehicle::CanAllocateItem(2)) return;
    00798
    00799   Industry *i, *found = NULL;
    00800
    00801   FOR_ALL_INDUSTRIES(i) {
    00802     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
    00803         (found == NULL || Chance16(1, 2))) {
    00804       found = i;
    00805     }
    00806   }
    00807
    00808   if (found == NULL) return;
    00809
    00810   Vehicle *v = new DisasterVehicle();
    00811
    00812   /* Start from the bottom (south side) of the map */
    00813   int x = (MapSizeX() + 9) * TILE_SIZE - 1;
    00814   int y = TileY(found->xy) * TILE_SIZE + 37;
    00815
    00816   InitializeDisasterVehicle(v, x, y, 135, DIR_NE, ST_AIRPLANE);
    00817
    00818   Vehicle *u = new DisasterVehicle();
    00819   v->SetNext(u);
    00820   InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_AIRPLANE_SHADOW);
    00821   u->vehstatus |= VS_SHADOW;
    00822 }
    00823
    00824
    00826 static void Disaster_Helicopter_Init()
    00827 {
    00828   if (!Vehicle::CanAllocateItem(3)) return;
    00829
    00830   Industry *i, *found = NULL;
    00831
    00832   FOR_ALL_INDUSTRIES(i) {
    00833     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
    00834         (found == NULL || Chance16(1, 2))) {
    00835       found = i;
    00836     }
    00837   }
    00838
    00839   if (found == NULL) return;
    00840
    00841   Vehicle *v = new DisasterVehicle();
    00842
    00843   int x = -16 * TILE_SIZE;
    00844   int y = TileY(found->xy) * TILE_SIZE + 37;
    00845
    00846   InitializeDisasterVehicle(v, x, y, 135, DIR_SW, ST_HELICOPTER);
    00847
    00848   Vehicle *u = new DisasterVehicle();
    00849   v->SetNext(u);
    00850   InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_HELICOPTER_SHADOW);
    00851   u->vehstatus |= VS_SHADOW;
    00852
    00853   Vehicle *w = new DisasterVehicle();
    00854   u->SetNext(w);
    00855   InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_HELICOPTER_ROTORS);
    00856 }
    00857
    00858
    00859 /* Big Ufo which lands on a piece of rail and will consequently be shot
    00860  * down by a combat airplane, destroying the surroundings */
    00861 static void Disaster_Big_Ufo_Init()
    00862 {
    00863   if (!Vehicle::CanAllocateItem(2)) return;
    00864
    00865   Vehicle *v = new DisasterVehicle();
    00866   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
    00867   int y = MapMaxX() * TILE_SIZE - 1;
    00868
    00869   InitializeDisasterVehicle(v, x, y, 135, DIR_NW, ST_BIG_UFO);
    00870   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
    00871   v->age = 0;
    00872
    00873   /* Allocate shadow */
    00874   Vehicle *u = new DisasterVehicle();
    00875   v->SetNext(u);
    00876   InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_BIG_UFO_SHADOW);
    00877   u->vehstatus |= VS_SHADOW;
    00878 }
    00879
    00880
    00881 static void Disaster_Submarine_Init(DisasterSubType subtype)
    00882 {
    00883   if (!Vehicle::CanAllocateItem()) return;
    00884
    00885   int y;
    00886   Direction dir;
    00887   uint32 r = Random();
    00888   int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
    00889
    00890   if (HasBit(r, 31)) {
    00891     y = MapMaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
    00892     dir = DIR_NW;
    00893   } else {
    00894     y = TILE_SIZE / 2;
    00895     if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
    00896     dir = DIR_SE;
    00897   }
    00898   if (!IsWaterTile(TileVirtXY(x, y))) return;
    00899
    00900   Vehicle *v = new DisasterVehicle();
    00901   InitializeDisasterVehicle(v, x, y, 0, dir, subtype);
    00902   v->age = 0;
    00903 }
    00904
    00905 /* Curious submarine #1, just floats around */
    00906 static void Disaster_Small_Submarine_Init()
    00907 {
    00908   Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
    00909 }
    00910
    00911
    00912 /* Curious submarine #2, just floats around */
    00913 static void Disaster_Big_Submarine_Init()
    00914 {
    00915   Disaster_Submarine_Init(ST_BIG_SUBMARINE);
    00916 }
    00917
    00918
    00921 static void Disaster_CoalMine_Init()
    00922 {
    00923   int index = GB(Random(), 0, 4);
    00924   uint m;
    00925
    00926   for (m = 0; m < 15; m++) {
    00927     const Industry *i;
    00928
    00929     FOR_ALL_INDUSTRIES(i) {
    00930       if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
    00931         SetDParam(0, i->town->index);
    00932         AddNewsItem(STR_B005_COAL_MINE_SUBSIDENCE_LEAVES,
    00933           NS_ACCIDENT_TILE, i->xy + TileDiffXY(1, 1), 0);
    00934
    00935         {
    00936           TileIndex tile = i->xy;
    00937           TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
    00938
    00939           for (uint n = 0; n < 30; n++) {
    00940             DisasterClearSquare(tile);
    00941             tile += step;
    00942             if (!IsValidTile(tile)) break;
    00943           }
    00944         }
    00945         return;
    00946       }
    00947     }
    00948   }
    00949 }
    00950
    00951 static DisasterInitProc * const _disaster_initprocs[] = {
    00952   Disaster_Zeppeliner_Init,
    00953   Disaster_Small_Ufo_Init,
    00954   Disaster_Airplane_Init,
    00955   Disaster_Helicopter_Init,
    00956   Disaster_Big_Ufo_Init,
    00957   Disaster_Small_Submarine_Init,
    00958   Disaster_Big_Submarine_Init,
    00959   Disaster_CoalMine_Init,
    00960 };
    00961
    00962 static const struct {
    00963   Year min;
    00964   Year max;
    00965 } _dis_years[] = {
    00966   { 1930, 1955 },
    00967   { 1940, 1970 },
    00968   { 1960, 1990 },
    00969   { 1970, 2000 },
    00970   { 2000, 2100 },
    00971   { 1940, 1965 },
    00972   { 1975, 2010 },
    00973   { 1950, 1985 }
    00974 };
    00975
    00976
    00977 static void DoDisaster()
    00978 {
    00979   byte buf[lengthof(_dis_years)];
    00980   uint i;
    00981   uint j;
    00982
    00983   j = 0;
    00984   for (i = 0; i != lengthof(_dis_years); i++) {
    00985     if (_cur_year >= _dis_years[i].min && _cur_year < _dis_years[i].max) buf[j++] = i;
    00986   }
    00987
    00988   if (j == 0) return;
    00989
    00990   _disaster_initprocs[buf[RandomRange(j)]]();
    00991 }
    00992
    00993
    00994 static void ResetDisasterDelay()
    00995 {
    00996   _disaster_delay = GB(Random(), 0, 9) + 730;
    00997 }
    00998
    00999 void DisasterDailyLoop()
    01000 {
    01001   if (--_disaster_delay != 0) return;
    01002
    01003   ResetDisasterDelay();
    01004
    01005   if (_settings_game.difficulty.disasters != 0) DoDisaster();
    01006 }
    01007
    01008 void StartupDisasters()
    01009 {
    01010   ResetDisasterDelay();
    01011 }
    01012
    01017 void ReleaseDisastersTargetingIndustry(IndustryID i)
    01018 {
    01019   Vehicle *v;
    01020   FOR_ALL_VEHICLES(v) {
    01021     /* primary disaster vehicles that have chosen target */
    01022     if (v->type == VEH_DISASTER && (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER)) {
    01023       /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
    01024       if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
    01025     }
    01026   }
    01027 }
    01028
    01032 void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
    01033 {
    01034   Vehicle *v;
    01035   FOR_ALL_VEHICLES(v) {
    01036     /* primary disaster vehicles that have chosen target */
    01037     if (v->type == VEH_DISASTER && v->subtype == ST_SMALL_UFO) {
    01038       if (v->current_order.GetDestination() != 0 && v->dest_tile == vehicle) {
    01039         /* Revert to target-searching */
    01040         v->current_order.SetDestination(0);
    01041         v->dest_tile = RandomTile();
    01042         v->z_pos = 135;
    01043         v->age = 0;
    01044       }
    01045     }
    01046   }
    01047 }
    01048
    01049 void DisasterVehicle::UpdateDeltaXY(Direction direction)
    01050 {
    01051   this->x_offs        = -1;
    01052   this->y_offs        = -1;
    01053   this->x_extent      =  2;
    01054   this->y_extent      =  2;
    01055   this->z_extent      =  5;
    01056 }