# disaster_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: disaster_cmd.cpp 15764 2009-03-18 19:47:05Z 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     if (u->type != VEH_ROAD || !IsRoadVehFront(u)) {
    00327       delete v;
    00328       return;
    00329     }
    00330
    00331     dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
    00332
    00333     if (dist < TILE_SIZE && !(u->vehstatus & VS_HIDDEN) && u->breakdown_ctr == 0) {
    00334       u->breakdown_ctr = 3;
    00335       u->breakdown_delay = 140;
    00336     }
    00337
    00338     v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
    00339     GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00340
    00341     z = v->z_pos;
    00342     if (dist <= TILE_SIZE && z > u->z_pos) z--;
    00343     SetDisasterVehiclePos(v, gp.x, gp.y, z);
    00344
    00345     if (z <= u->z_pos && (u->vehstatus & VS_HIDDEN) == 0) {
    00346       v->age++;
    00347       if (u->u.road.crashed_ctr == 0) {
    00348         u->u.road.crashed_ctr++;
    00349
    00350         AddNewsItem(STR_B001_ROAD_VEHICLE_DESTROYED,
    00351           NS_ACCIDENT_VEHICLE,
    00352           u->index,
    00353           0);
    00354
    00355         AI::NewEvent(u->owner, new AIEventVehicleCrashed(u->index, u->tile, AIEventVehicleCrashed::CRASH_RV_UFO));
    00356
    00357         for (Vehicle *w = u; w != NULL; w = w->Next()) {
    00358           w->vehstatus |= VS_CRASHED;
    00359           MarkSingleVehicleDirty(w);
    00360         }
    00361       }
    00362     }
    00363
    00364     /* Destroy? */
    00365     if (v->age > 50) {
    00366       CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
    00367       SndPlayVehicleFx(SND_12_EXPLOSION, v);
    00368       delete v;
    00369     }
    00370   }
    00371 }
    00372
    00373 static void DestructIndustry(Industry *i)
    00374 {
    00375   TileIndex tile;
    00376
    00377   for (tile = 0; tile != MapSize(); tile++) {
    00378     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == i->index) {
    00379       ResetIndustryConstructionStage(tile);
    00380       MarkTileDirtyByTile(tile);
    00381     }
    00382   }
    00383 }
    00384
    00393 static void DisasterTick_Airplane(Vehicle *v)
    00394 {
    00395   v->tick_counter++;
    00396   v->u.disaster.image_override =
    00397     (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_F_15_FIRING : 0;
    00398
    00399   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00400   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00401
    00402   if (gp.x < (-10 * TILE_SIZE)) {
    00403     delete v;
    00404     return;
    00405   }
    00406
    00407   if (v->current_order.GetDestination() == 2) {
    00408     if (GB(v->tick_counter, 0, 2) == 0) {
    00409       Industry *i = GetIndustry(v->dest_tile);
    00410       int x = TileX(i->xy) * TILE_SIZE;
    00411       int y = TileY(i->xy) * TILE_SIZE;
    00412       uint32 r = Random();
    00413
    00414       CreateEffectVehicleAbove(
    00415         GB(r,  0, 6) + x,
    00416         GB(r,  6, 6) + y,
    00417         GB(r, 12, 4),
    00418         EV_EXPLOSION_SMALL);
    00419
    00420       if (++v->age >= 55) v->current_order.SetDestination(3);
    00421     }
    00422   } else if (v->current_order.GetDestination() == 1) {
    00423     if (++v->age == 112) {
    00424       Industry *i;
    00425
    00426       v->current_order.SetDestination(2);
    00427       v->age = 0;
    00428
    00429       i = GetIndustry(v->dest_tile);
    00430       DestructIndustry(i);
    00431
    00432       SetDParam(0, i->town->index);
    00433       AddNewsItem(STR_B002_OIL_REFINERY_EXPLOSION, NS_ACCIDENT_TILE, i->xy, 0);
    00434       SndPlayTileFx(SND_12_EXPLOSION, i->xy);
    00435     }
    00436   } else if (v->current_order.GetDestination() == 0) {
    00437     int x, y;
    00438     TileIndex tile;
    00439     uint ind;
    00440
    00441     x = v->x_pos - (15 * TILE_SIZE);
    00442     y = v->y_pos;
    00443
    00444     if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
    00445
    00446     tile = TileVirtXY(x, y);
    00447     if (!IsTileType(tile, MP_INDUSTRY)) return;
    00448
    00449     ind = GetIndustryIndex(tile);
    00450     v->dest_tile = ind;
    00451
    00452     if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) {
    00453       v->current_order.SetDestination(1);
    00454       v->age = 0;
    00455     }
    00456   }
    00457 }
    00458
    00466 static void DisasterTick_Helicopter(Vehicle *v)
    00467 {
    00468   v->tick_counter++;
    00469   v->u.disaster.image_override =
    00470     (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_AH_64A_FIRING : 0;
    00471
    00472   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00473   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00474
    00475   if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
    00476     delete v;
    00477     return;
    00478   }
    00479
    00480   if (v->current_order.GetDestination() == 2) {
    00481     if (GB(v->tick_counter, 0, 2) == 0) {
    00482       Industry *i = GetIndustry(v->dest_tile);
    00483       int x = TileX(i->xy) * TILE_SIZE;
    00484       int y = TileY(i->xy) * TILE_SIZE;
    00485       uint32 r = Random();
    00486
    00487       CreateEffectVehicleAbove(
    00488         GB(r,  0, 6) + x,
    00489         GB(r,  6, 6) + y,
    00490         GB(r, 12, 4),
    00491         EV_EXPLOSION_SMALL);
    00492
    00493       if (++v->age >= 55) v->current_order.SetDestination(3);
    00494     }
    00495   } else if (v->current_order.GetDestination() == 1) {
    00496     if (++v->age == 112) {
    00497       Industry *i;
    00498
    00499       v->current_order.SetDestination(2);
    00500       v->age = 0;
    00501
    00502       i = GetIndustry(v->dest_tile);
    00503       DestructIndustry(i);
    00504
    00505       SetDParam(0, i->town->index);
    00506       AddNewsItem(STR_B003_FACTORY_DESTROYED_IN_SUSPICIOUS, NS_ACCIDENT_TILE, i->xy, 0);
    00507       SndPlayTileFx(SND_12_EXPLOSION, i->xy);
    00508     }
    00509   } else if (v->current_order.GetDestination() == 0) {
    00510     int x, y;
    00511     TileIndex tile;
    00512     uint ind;
    00513
    00514     x = v->x_pos + (15 * TILE_SIZE);
    00515     y = v->y_pos;
    00516
    00517     if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
    00518
    00519     tile = TileVirtXY(x, y);
    00520     if (!IsTileType(tile, MP_INDUSTRY)) return;
    00521
    00522     ind = GetIndustryIndex(tile);
    00523     v->dest_tile = ind;
    00524
    00525     if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) {
    00526       v->current_order.SetDestination(1);
    00527       v->age = 0;
    00528     }
    00529   }
    00530 }
    00531
    00533 static void DisasterTick_Helicopter_Rotors(Vehicle *v)
    00534 {
    00535   v->tick_counter++;
    00536   if (HasBit(v->tick_counter, 0)) return;
    00537
    00538   if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
    00539
    00540   VehicleMove(v, true);
    00541 }
    00542
    00549 static void DisasterTick_Big_Ufo(Vehicle *v)
    00550 {
    00551   byte z;
    00552   Town *t;
    00553   TileIndex tile;
    00554   TileIndex tile_org;
    00555
    00556   v->tick_counter++;
    00557
    00558   if (v->current_order.GetDestination() == 1) {
    00559     int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
    00560     int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
    00561     if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
    00562       v->direction = GetDirectionTowards(v, x, y);
    00563
    00564       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00565       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00566       return;
    00567     }
    00568
    00569     if (!IsValidTile(v->dest_tile)) {
    00570       /* Make sure we don't land outside the map. */
    00571       delete v;
    00572       return;
    00573     }
    00574
    00575     z = GetSlopeZ(v->x_pos, v->y_pos);
    00576     if (z < v->z_pos) {
    00577       SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos - 1);
    00578       return;
    00579     }
    00580
    00581     v->current_order.SetDestination(2);
    00582
    00583     Vehicle *u;
    00584     FOR_ALL_VEHICLES(u) {
    00585       if (u->type == VEH_TRAIN || u->type == VEH_ROAD) {
    00586         if (Delta(u->x_pos, v->x_pos) + Delta(u->y_pos, v->y_pos) <= 12 * TILE_SIZE) {
    00587           u->breakdown_ctr = 5;
    00588           u->breakdown_delay = 0xF0;
    00589         }
    00590       }
    00591     }
    00592
    00593     t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
    00594     SetDParam(0, t->index);
    00595     AddNewsItem(STR_B004_UFO_LANDS_NEAR,
    00596       NS_ACCIDENT_TILE,
    00597       v->tile,
    00598       0);
    00599
    00600     if (!Vehicle::CanAllocateItem(2)) {
    00601       delete v;
    00602       return;
    00603     }
    00604     u = new DisasterVehicle();
    00605
    00606     InitializeDisasterVehicle(u, -6 * TILE_SIZE, v->y_pos, 135, DIR_SW, ST_BIG_UFO_DESTROYER);
    00607     u->u.disaster.big_ufo_destroyer_target = v->index;
    00608
    00609     Vehicle *w = new DisasterVehicle();
    00610
    00611     u->SetNext(w);
    00612     InitializeDisasterVehicle(w, -6 * TILE_SIZE, v->y_pos, 0, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
    00613     w->vehstatus |= VS_SHADOW;
    00614   } else if (v->current_order.GetDestination() == 0) {
    00615     int x = TileX(v->dest_tile) * TILE_SIZE;
    00616     int y = TileY(v->dest_tile) * TILE_SIZE;
    00617     if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
    00618       v->direction = GetDirectionTowards(v, x, y);
    00619       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00620       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00621       return;
    00622     }
    00623
    00624     if (++v->age < 6) {
    00625       v->dest_tile = RandomTile();
    00626       return;
    00627     }
    00628     v->current_order.SetDestination(1);
    00629
    00630     tile_org = tile = RandomTile();
    00631     do {
    00632       if (IsTileType(tile, MP_RAILWAY) &&
    00633           IsPlainRailTile(tile) &&
    00634           IsHumanCompany(GetTileOwner(tile))) {
    00635         break;
    00636       }
    00637       tile = TILE_MASK(tile + 1);
    00638     } while (tile != tile_org);
    00639     v->dest_tile = tile;
    00640     v->age = 0;
    00641   } else {
    00642     return;
    00643   }
    00644 }
    00645
    00650 static void DisasterTick_Big_Ufo_Destroyer(Vehicle *v)
    00651 {
    00652   Vehicle *u;
    00653   int i;
    00654
    00655   v->tick_counter++;
    00656
    00657   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00658   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00659
    00660   if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
    00661     delete v;
    00662     return;
    00663   }
    00664
    00665   if (v->current_order.GetDestination() == 0) {
    00666     u = GetVehicle(v->u.disaster.big_ufo_destroyer_target);
    00667     if (Delta(v->x_pos, u->x_pos) > TILE_SIZE) return;
    00668     v->current_order.SetDestination(1);
    00669
    00670     CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
    00671     SndPlayVehicleFx(SND_12_EXPLOSION, u);
    00672
    00673     delete u;
    00674
    00675     for (i = 0; i != 80; i++) {
    00676       uint32 r = Random();
    00677       CreateEffectVehicleAbove(
    00678         GB(r, 0, 6) + v->x_pos - 32,
    00679         GB(r, 5, 6) + v->y_pos - 32,
    00680         0,
    00681         EV_EXPLOSION_SMALL);
    00682     }
    00683
    00684     for (int dy = -3; dy < 3; dy++) {
    00685       for (int dx = -3; dx < 3; dx++) {
    00686         TileIndex tile = TileAddWrap(v->tile, dx, dy);
    00687         if (tile != INVALID_TILE) DisasterClearSquare(tile);
    00688       }
    00689     }
    00690   }
    00691 }
    00692
    00697 static void DisasterTick_Submarine(Vehicle *v)
    00698 {
    00699   TileIndex tile;
    00700
    00701   v->tick_counter++;
    00702
    00703   if (++v->age > 8880) {
    00704     delete v;
    00705     return;
    00706   }
    00707
    00708   if (!HasBit(v->tick_counter, 0)) return;
    00709
    00710   tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
    00711   if (IsValidTile(tile)) {
    00712     TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
    00713     if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
    00714       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00715       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00716       return;
    00717     }
    00718   }
    00719
    00720   v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
    00721 }
    00722
    00723
    00724 static void DisasterTick_NULL(Vehicle *v) {}
    00725 typedef void DisasterVehicleTickProc(Vehicle *v);
    00726
    00727 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
    00728   DisasterTick_Zeppeliner, DisasterTick_NULL,
    00729   DisasterTick_Ufo,        DisasterTick_NULL,
    00730   DisasterTick_Airplane,   DisasterTick_NULL,
    00731   DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
    00732   DisasterTick_Big_Ufo,    DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
    00733   DisasterTick_NULL,
    00734   DisasterTick_Submarine,
    00735   DisasterTick_Submarine,
    00736 };
    00737
    00738
    00739 void DisasterVehicle::Tick()
    00740 {
    00741   _disastervehicle_tick_procs[this->subtype](this);
    00742 }
    00743
    00744 typedef void DisasterInitProc();
    00745
    00746
    00749 static void Disaster_Zeppeliner_Init()
    00750 {
    00751   if (!Vehicle::CanAllocateItem(2)) return;
    00752
    00753   Vehicle *v = new DisasterVehicle();
    00754   Station *st;
    00755
    00756   /* Pick a random place, unless we find a small airport */
    00757   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
    00758
    00759   FOR_ALL_STATIONS(st) {
    00760     if (st->airport_tile != INVALID_TILE && (st->airport_type == AT_SMALL || st->airport_type == AT_LARGE)) {
    00761       x = (TileX(st->airport_tile) + 2) * TILE_SIZE;
    00762       break;
    00763     }
    00764   }
    00765
    00766   InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_ZEPPELINER);
    00767
    00768   /* Allocate shadow */
    00769   Vehicle *u = new DisasterVehicle();
    00770   v->SetNext(u);
    00771   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
    00772   u->vehstatus |= VS_SHADOW;
    00773 }
    00774
    00775
    00778 static void Disaster_Small_Ufo_Init()
    00779 {
    00780   if (!Vehicle::CanAllocateItem(2)) return;
    00781
    00782   Vehicle *v = new DisasterVehicle();
    00783   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
    00784
    00785   InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_SMALL_UFO);
    00786   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
    00787   v->age = 0;
    00788
    00789   /* Allocate shadow */
    00790   Vehicle *u = new DisasterVehicle();
    00791   v->SetNext(u);
    00792   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
    00793   u->vehstatus |= VS_SHADOW;
    00794 }
    00795
    00796
    00797 /* Combat airplane which destroys an oil refinery */
    00798 static void Disaster_Airplane_Init()
    00799 {
    00800   if (!Vehicle::CanAllocateItem(2)) return;
    00801
    00802   Industry *i, *found = NULL;
    00803
    00804   FOR_ALL_INDUSTRIES(i) {
    00805     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
    00806         (found == NULL || Chance16(1, 2))) {
    00807       found = i;
    00808     }
    00809   }
    00810
    00811   if (found == NULL) return;
    00812
    00813   Vehicle *v = new DisasterVehicle();
    00814
    00815   /* Start from the bottom (south side) of the map */
    00816   int x = (MapSizeX() + 9) * TILE_SIZE - 1;
    00817   int y = TileY(found->xy) * TILE_SIZE + 37;
    00818
    00819   InitializeDisasterVehicle(v, x, y, 135, DIR_NE, ST_AIRPLANE);
    00820
    00821   Vehicle *u = new DisasterVehicle();
    00822   v->SetNext(u);
    00823   InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_AIRPLANE_SHADOW);
    00824   u->vehstatus |= VS_SHADOW;
    00825 }
    00826
    00827
    00829 static void Disaster_Helicopter_Init()
    00830 {
    00831   if (!Vehicle::CanAllocateItem(3)) return;
    00832
    00833   Industry *i, *found = NULL;
    00834
    00835   FOR_ALL_INDUSTRIES(i) {
    00836     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
    00837         (found == NULL || Chance16(1, 2))) {
    00838       found = i;
    00839     }
    00840   }
    00841
    00842   if (found == NULL) return;
    00843
    00844   Vehicle *v = new DisasterVehicle();
    00845
    00846   int x = -16 * TILE_SIZE;
    00847   int y = TileY(found->xy) * TILE_SIZE + 37;
    00848
    00849   InitializeDisasterVehicle(v, x, y, 135, DIR_SW, ST_HELICOPTER);
    00850
    00851   Vehicle *u = new DisasterVehicle();
    00852   v->SetNext(u);
    00853   InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_HELICOPTER_SHADOW);
    00854   u->vehstatus |= VS_SHADOW;
    00855
    00856   Vehicle *w = new DisasterVehicle();
    00857   u->SetNext(w);
    00858   InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_HELICOPTER_ROTORS);
    00859 }
    00860
    00861
    00862 /* Big Ufo which lands on a piece of rail and will consequently be shot
    00863  * down by a combat airplane, destroying the surroundings */
    00864 static void Disaster_Big_Ufo_Init()
    00865 {
    00866   if (!Vehicle::CanAllocateItem(2)) return;
    00867
    00868   Vehicle *v = new DisasterVehicle();
    00869   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
    00870   int y = MapMaxX() * TILE_SIZE - 1;
    00871
    00872   InitializeDisasterVehicle(v, x, y, 135, DIR_NW, ST_BIG_UFO);
    00873   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
    00874   v->age = 0;
    00875
    00876   /* Allocate shadow */
    00877   Vehicle *u = new DisasterVehicle();
    00878   v->SetNext(u);
    00879   InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_BIG_UFO_SHADOW);
    00880   u->vehstatus |= VS_SHADOW;
    00881 }
    00882
    00883
    00884 static void Disaster_Submarine_Init(DisasterSubType subtype)
    00885 {
    00886   if (!Vehicle::CanAllocateItem()) return;
    00887
    00888   int y;
    00889   Direction dir;
    00890   uint32 r = Random();
    00891   int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
    00892
    00893   if (HasBit(r, 31)) {
    00894     y = MapMaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
    00895     dir = DIR_NW;
    00896   } else {
    00897     y = TILE_SIZE / 2;
    00898     if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
    00899     dir = DIR_SE;
    00900   }
    00901   if (!IsWaterTile(TileVirtXY(x, y))) return;
    00902
    00903   Vehicle *v = new DisasterVehicle();
    00904   InitializeDisasterVehicle(v, x, y, 0, dir, subtype);
    00905   v->age = 0;
    00906 }
    00907
    00908 /* Curious submarine #1, just floats around */
    00909 static void Disaster_Small_Submarine_Init()
    00910 {
    00911   Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
    00912 }
    00913
    00914
    00915 /* Curious submarine #2, just floats around */
    00916 static void Disaster_Big_Submarine_Init()
    00917 {
    00918   Disaster_Submarine_Init(ST_BIG_SUBMARINE);
    00919 }
    00920
    00921
    00924 static void Disaster_CoalMine_Init()
    00925 {
    00926   int index = GB(Random(), 0, 4);
    00927   uint m;
    00928
    00929   for (m = 0; m < 15; m++) {
    00930     const Industry *i;
    00931
    00932     FOR_ALL_INDUSTRIES(i) {
    00933       if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
    00934         SetDParam(0, i->town->index);
    00935         AddNewsItem(STR_B005_COAL_MINE_SUBSIDENCE_LEAVES,
    00936           NS_ACCIDENT_TILE, i->xy + TileDiffXY(1, 1), 0);
    00937
    00938         {
    00939           TileIndex tile = i->xy;
    00940           TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
    00941
    00942           for (uint n = 0; n < 30; n++) {
    00943             DisasterClearSquare(tile);
    00944             tile += step;
    00945             if (!IsValidTile(tile)) break;
    00946           }
    00947         }
    00948         return;
    00949       }
    00950     }
    00951   }
    00952 }
    00953
    00954 static DisasterInitProc * const _disaster_initprocs[] = {
    00955   Disaster_Zeppeliner_Init,
    00956   Disaster_Small_Ufo_Init,
    00957   Disaster_Airplane_Init,
    00958   Disaster_Helicopter_Init,
    00959   Disaster_Big_Ufo_Init,
    00960   Disaster_Small_Submarine_Init,
    00961   Disaster_Big_Submarine_Init,
    00962   Disaster_CoalMine_Init,
    00963 };
    00964
    00965 static const struct {
    00966   Year min;
    00967   Year max;
    00968 } _dis_years[] = {
    00969   { 1930, 1955 },
    00970   { 1940, 1970 },
    00971   { 1960, 1990 },
    00972   { 1970, 2000 },
    00973   { 2000, 2100 },
    00974   { 1940, 1965 },
    00975   { 1975, 2010 },
    00976   { 1950, 1985 }
    00977 };
    00978
    00979
    00980 static void DoDisaster()
    00981 {
    00982   byte buf[lengthof(_dis_years)];
    00983   uint i;
    00984   uint j;
    00985
    00986   j = 0;
    00987   for (i = 0; i != lengthof(_dis_years); i++) {
    00988     if (_cur_year >= _dis_years[i].min && _cur_year < _dis_years[i].max) buf[j++] = i;
    00989   }
    00990
    00991   if (j == 0) return;
    00992
    00993   _disaster_initprocs[buf[RandomRange(j)]]();
    00994 }
    00995
    00996
    00997 static void ResetDisasterDelay()
    00998 {
    00999   _disaster_delay = GB(Random(), 0, 9) + 730;
    01000 }
    01001
    01002 void DisasterDailyLoop()
    01003 {
    01004   if (--_disaster_delay != 0) return;
    01005
    01006   ResetDisasterDelay();
    01007
    01008   if (_settings_game.difficulty.disasters != 0) DoDisaster();
    01009 }
    01010
    01011 void StartupDisasters()
    01012 {
    01013   ResetDisasterDelay();
    01014 }
    01015
    01020 void ReleaseDisastersTargetingIndustry(IndustryID i)
    01021 {
    01022   Vehicle *v;
    01023   FOR_ALL_VEHICLES(v) {
    01024     /* primary disaster vehicles that have chosen target */
    01025     if (v->type == VEH_DISASTER && (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER)) {
    01026       /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
    01027       if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
    01028     }
    01029   }
    01030 }
    01031
    01032 void DisasterVehicle::UpdateDeltaXY(Direction direction)
    01033 {
    01034   this->x_offs        = -1;
    01035   this->y_offs        = -1;
    01036   this->x_extent      =  2;
    01037   this->y_extent      =  2;
    01038   this->z_extent      =  5;
    01039 }