# disaster_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: disaster_cmd.cpp 15359 2009-02-05 17:38:47Z 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     BEGIN_TILE_LOOP(tile, 6, 6, v->tile - TileDiffXY(3, 3))
    00686       tile = TILE_MASK(tile);
    00687       DisasterClearSquare(tile);
    00688     END_TILE_LOOP(tile, 6, 6, v->tile - TileDiffXY(3, 3))
    00689   }
    00690 }
    00691
    00696 static void DisasterTick_Submarine(Vehicle *v)
    00697 {
    00698   TileIndex tile;
    00699
    00700   v->tick_counter++;
    00701
    00702   if (++v->age > 8880) {
    00703     VehiclePositionChanged(v);
    00704     MarkSingleVehicleDirty(v);
    00705     delete v;
    00706     return;
    00707   }
    00708
    00709   if (!HasBit(v->tick_counter, 0)) return;
    00710
    00711   tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
    00712   if (IsValidTile(tile)) {
    00713     TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
    00714     if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
    00715       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
    00716       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
    00717       return;
    00718     }
    00719   }
    00720
    00721   v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
    00722 }
    00723
    00724
    00725 static void DisasterTick_NULL(Vehicle *v) {}
    00726 typedef void DisasterVehicleTickProc(Vehicle *v);
    00727
    00728 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
    00729   DisasterTick_Zeppeliner, DisasterTick_NULL,
    00730   DisasterTick_Ufo,        DisasterTick_NULL,
    00731   DisasterTick_Airplane,   DisasterTick_NULL,
    00732   DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
    00733   DisasterTick_Big_Ufo,    DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
    00734   DisasterTick_NULL,
    00735   DisasterTick_Submarine,
    00736   DisasterTick_Submarine,
    00737 };
    00738
    00739
    00740 void DisasterVehicle::Tick()
    00741 {
    00742   _disastervehicle_tick_procs[this->subtype](this);
    00743 }
    00744
    00745 typedef void DisasterInitProc();
    00746
    00747
    00750 static void Disaster_Zeppeliner_Init()
    00751 {
    00752   if (!Vehicle::CanAllocateItem(2)) return;
    00753
    00754   Vehicle *v = new DisasterVehicle();
    00755   Station *st;
    00756
    00757   /* Pick a random place, unless we find a small airport */
    00758   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
    00759
    00760   FOR_ALL_STATIONS(st) {
    00761     if (st->airport_tile != INVALID_TILE && (st->airport_type == AT_SMALL || st->airport_type == AT_LARGE)) {
    00762       x = (TileX(st->airport_tile) + 2) * TILE_SIZE;
    00763       break;
    00764     }
    00765   }
    00766
    00767   InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_Zeppeliner);
    00768
    00769   /* Allocate shadow */
    00770   Vehicle *u = new DisasterVehicle();
    00771   v->SetNext(u);
    00772   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_Zeppeliner_Shadow);
    00773   u->vehstatus |= VS_SHADOW;
    00774 }
    00775
    00776
    00779 static void Disaster_Small_Ufo_Init()
    00780 {
    00781   if (!Vehicle::CanAllocateItem(2)) return;
    00782
    00783   Vehicle *v = new DisasterVehicle();
    00784   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
    00785
    00786   InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_Small_Ufo);
    00787   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
    00788   v->age = 0;
    00789
    00790   /* Allocate shadow */
    00791   Vehicle *u = new DisasterVehicle();
    00792   v->SetNext(u);
    00793   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_Small_Ufo_Shadow);
    00794   u->vehstatus |= VS_SHADOW;
    00795 }
    00796
    00797
    00798 /* Combat airplane which destroys an oil refinery */
    00799 static void Disaster_Airplane_Init()
    00800 {
    00801   if (!Vehicle::CanAllocateItem(2)) return;
    00802
    00803   Industry *i, *found = NULL;
    00804
    00805   FOR_ALL_INDUSTRIES(i) {
    00806     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
    00807         (found == NULL || Chance16(1, 2))) {
    00808       found = i;
    00809     }
    00810   }
    00811
    00812   if (found == NULL) return;
    00813
    00814   Vehicle *v = new DisasterVehicle();
    00815
    00816   /* Start from the bottom (south side) of the map */
    00817   int x = (MapSizeX() + 9) * TILE_SIZE - 1;
    00818   int y = TileY(found->xy) * TILE_SIZE + 37;
    00819
    00820   InitializeDisasterVehicle(v, x, y, 135, DIR_NE, ST_Airplane);
    00821
    00822   Vehicle *u = new DisasterVehicle();
    00823   v->SetNext(u);
    00824   InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_Airplane_Shadow);
    00825   u->vehstatus |= VS_SHADOW;
    00826 }
    00827
    00828
    00830 static void Disaster_Helicopter_Init()
    00831 {
    00832   if (!Vehicle::CanAllocateItem(3)) return;
    00833
    00834   Industry *i, *found = NULL;
    00835
    00836   FOR_ALL_INDUSTRIES(i) {
    00837     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
    00838         (found == NULL || Chance16(1, 2))) {
    00839       found = i;
    00840     }
    00841   }
    00842
    00843   if (found == NULL) return;
    00844
    00845   Vehicle *v = new DisasterVehicle();
    00846
    00847   int x = -16 * TILE_SIZE;
    00848   int y = TileY(found->xy) * TILE_SIZE + 37;
    00849
    00850   InitializeDisasterVehicle(v, x, y, 135, DIR_SW, ST_Helicopter);
    00851
    00852   Vehicle *u = new DisasterVehicle();
    00853   v->SetNext(u);
    00854   InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_Helicopter_Shadow);
    00855   u->vehstatus |= VS_SHADOW;
    00856
    00857   Vehicle *w = new DisasterVehicle();
    00858   u->SetNext(w);
    00859   InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_Helicopter_Rotors);
    00860 }
    00861
    00862
    00863 /* Big Ufo which lands on a piece of rail and will consequently be shot
    00864  * down by a combat airplane, destroying the surroundings */
    00865 static void Disaster_Big_Ufo_Init()
    00866 {
    00867   if (!Vehicle::CanAllocateItem(2)) return;
    00868
    00869   Vehicle *v = new DisasterVehicle();
    00870   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
    00871   int y = MapMaxX() * TILE_SIZE - 1;
    00872
    00873   InitializeDisasterVehicle(v, x, y, 135, DIR_NW, ST_Big_Ufo);
    00874   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
    00875   v->age = 0;
    00876
    00877   /* Allocate shadow */
    00878   Vehicle *u = new DisasterVehicle();
    00879   v->SetNext(u);
    00880   InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_Big_Ufo_Shadow);
    00881   u->vehstatus |= VS_SHADOW;
    00882 }
    00883
    00884
    00885 /* Curious submarine #1, just floats around */
    00886 static void Disaster_Small_Submarine_Init()
    00887 {
    00888   if (!Vehicle::CanAllocateItem()) return;
    00889
    00890   Vehicle *v = new DisasterVehicle();
    00891   int y;
    00892   Direction dir;
    00893   uint32 r = Random();
    00894   int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
    00895
    00896   if (HasBit(r, 31)) {
    00897     y = MapMaxX() * TILE_SIZE - TILE_SIZE / 2 - 1;
    00898     dir = DIR_NW;
    00899   } else {
    00900     y = TILE_SIZE / 2;
    00901     dir = DIR_SE;
    00902   }
    00903   InitializeDisasterVehicle(v, x, y, 0, dir, ST_Small_Submarine);
    00904   v->age = 0;
    00905 }
    00906
    00907
    00908 /* Curious submarine #2, just floats around */
    00909 static void Disaster_Big_Submarine_Init()
    00910 {
    00911   if (!Vehicle::CanAllocateItem()) return;
    00912
    00913   Vehicle *v = new DisasterVehicle();
    00914   int y;
    00915   Direction dir;
    00916   uint32 r = Random();
    00917   int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
    00918
    00919   if (HasBit(r, 31)) {
    00920     y = MapMaxX() * TILE_SIZE - TILE_SIZE / 2 - 1;
    00921     dir = DIR_NW;
    00922   } else {
    00923     y = TILE_SIZE / 2;
    00924     dir = DIR_SE;
    00925   }
    00926   InitializeDisasterVehicle(v, x, y, 0, dir, ST_Big_Submarine);
    00927   v->age = 0;
    00928 }
    00929
    00930
    00933 static void Disaster_CoalMine_Init()
    00934 {
    00935   int index = GB(Random(), 0, 4);
    00936   uint m;
    00937
    00938   for (m = 0; m < 15; m++) {
    00939     const Industry *i;
    00940
    00941     FOR_ALL_INDUSTRIES(i) {
    00942       if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
    00943         SetDParam(0, i->town->index);
    00944         AddNewsItem(STR_B005_COAL_MINE_SUBSIDENCE_LEAVES,
    00945           NS_ACCIDENT_TILE, i->xy + TileDiffXY(1, 1), 0);
    00946
    00947         {
    00948           TileIndex tile = i->xy;
    00949           TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
    00950
    00951           for (uint n = 0; n < 30; n++) {
    00952             DisasterClearSquare(tile);
    00953             tile += step;
    00954             if (!IsValidTile(tile)) break;
    00955           }
    00956         }
    00957         return;
    00958       }
    00959     }
    00960   }
    00961 }
    00962
    00963 static DisasterInitProc * const _disaster_initprocs[] = {
    00964   Disaster_Zeppeliner_Init,
    00965   Disaster_Small_Ufo_Init,
    00966   Disaster_Airplane_Init,
    00967   Disaster_Helicopter_Init,
    00968   Disaster_Big_Ufo_Init,
    00969   Disaster_Small_Submarine_Init,
    00970   Disaster_Big_Submarine_Init,
    00971   Disaster_CoalMine_Init,
    00972 };
    00973
    00974 static const struct {
    00975   Year min;
    00976   Year max;
    00977 } _dis_years[] = {
    00978   { 1930, 1955 },
    00979   { 1940, 1970 },
    00980   { 1960, 1990 },
    00981   { 1970, 2000 },
    00982   { 2000, 2100 },
    00983   { 1940, 1965 },
    00984   { 1975, 2010 },
    00985   { 1950, 1985 }
    00986 };
    00987
    00988
    00989 static void DoDisaster()
    00990 {
    00991   byte buf[lengthof(_dis_years)];
    00992   uint i;
    00993   uint j;
    00994
    00995   j = 0;
    00996   for (i = 0; i != lengthof(_dis_years); i++) {
    00997     if (_cur_year >= _dis_years[i].min && _cur_year < _dis_years[i].max) buf[j++] = i;
    00998   }
    00999
    01000   if (j == 0) return;
    01001
    01002   _disaster_initprocs[buf[RandomRange(j)]]();
    01003 }
    01004
    01005
    01006 static void ResetDisasterDelay()
    01007 {
    01008   _disaster_delay = GB(Random(), 0, 9) + 730;
    01009 }
    01010
    01011 void DisasterDailyLoop()
    01012 {
    01013   if (--_disaster_delay != 0) return;
    01014
    01015   ResetDisasterDelay();
    01016
    01017   if (_settings_game.difficulty.disasters != 0) DoDisaster();
    01018 }
    01019
    01020 void StartupDisasters()
    01021 {
    01022   ResetDisasterDelay();
    01023 }
    01024
    01029 void ReleaseDisastersTargetingIndustry(IndustryID i)
    01030 {
    01031   Vehicle *v;
    01032   FOR_ALL_VEHICLES(v) {
    01033     /* primary disaster vehicles that have chosen target */
    01034     if (v->type == VEH_DISASTER && (v->subtype == ST_Airplane || v->subtype == ST_Helicopter)) {
    01035       /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
    01036       if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
    01037     }
    01038   }
    01039 }
    01040
    01041 void DisasterVehicle::UpdateDeltaXY(Direction direction)
    01042 {
    01043   this->x_offs        = -1;
    01044   this->y_offs        = -1;
    01045   this->x_extent      =  2;
    01046   this->y_extent      =  2;
    01047   this->z_extent      =  5;
    01048 }