disaster_cmd.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00027 #include "stdafx.h"
00028 
00029 #include "industry.h"
00030 #include "station_base.h"
00031 #include "command_func.h"
00032 #include "news_func.h"
00033 #include "town.h"
00034 #include "company_func.h"
00035 #include "strings_func.h"
00036 #include "date_func.h"
00037 #include "viewport_func.h"
00038 #include "vehicle_func.h"
00039 #include "sound_func.h"
00040 #include "effectvehicle_func.h"
00041 #include "roadveh.h"
00042 #include "ai/ai.hpp"
00043 #include "game/game.hpp"
00044 #include "company_base.h"
00045 #include "core/random_func.hpp"
00046 #include "core/backup_type.hpp"
00047 
00048 #include "table/strings.h"
00049 
00051 uint16 _disaster_delay;
00052 
00053 enum DisasterSubType {
00054   ST_ZEPPELINER,
00055   ST_ZEPPELINER_SHADOW,
00056   ST_SMALL_UFO,
00057   ST_SMALL_UFO_SHADOW,
00058   ST_AIRPLANE,
00059   ST_AIRPLANE_SHADOW,
00060   ST_HELICOPTER,
00061   ST_HELICOPTER_SHADOW,
00062   ST_HELICOPTER_ROTORS,
00063   ST_BIG_UFO,
00064   ST_BIG_UFO_SHADOW,
00065   ST_BIG_UFO_DESTROYER,
00066   ST_BIG_UFO_DESTROYER_SHADOW,
00067   ST_SMALL_SUBMARINE,
00068   ST_BIG_SUBMARINE,
00069 };
00070 
00071 static const uint INITIAL_DISASTER_VEHICLE_ZPOS = 135; 
00072 
00073 static void DisasterClearSquare(TileIndex tile)
00074 {
00075   if (EnsureNoVehicleOnGround(tile).Failed()) return;
00076 
00077   switch (GetTileType(tile)) {
00078     case MP_RAILWAY:
00079       if (Company::IsHumanID(GetTileOwner(tile))) {
00080         Backup<CompanyByte> cur_company(_current_company, OWNER_WATER, FILE_LINE);
00081         DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00082         cur_company.Restore();
00083 
00084         /* update signals in buffer */
00085         UpdateSignalsInBuffer();
00086       }
00087       break;
00088 
00089     case MP_HOUSE: {
00090       Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
00091       DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00092       cur_company.Restore();
00093       break;
00094     }
00095 
00096     case MP_TREES:
00097     case MP_CLEAR:
00098       DoClearSquare(tile);
00099       break;
00100 
00101     default:
00102       break;
00103   }
00104 }
00105 
00106 static const SpriteID _disaster_images_1[] = {SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP};
00107 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};
00108 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};
00109 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};
00110 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};
00111 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};
00112 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};
00113 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};
00114 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};
00115 
00116 static const SpriteID * const _disaster_images[] = {
00117   _disaster_images_1, _disaster_images_1,                     
00118   _disaster_images_2, _disaster_images_2,                     
00119   _disaster_images_3, _disaster_images_3,                     
00120   _disaster_images_8, _disaster_images_8, _disaster_images_9, 
00121   _disaster_images_6, _disaster_images_6,                     
00122   _disaster_images_7, _disaster_images_7,                     
00123   _disaster_images_4, _disaster_images_5,                     
00124 };
00125 
00126 static void DisasterVehicleUpdateImage(DisasterVehicle *v)
00127 {
00128   SpriteID img = v->image_override;
00129   if (img == 0) img = _disaster_images[v->subtype][v->direction];
00130   v->cur_image = img;
00131 }
00132 
00137 static void InitializeDisasterVehicle(DisasterVehicle *v, int x, int y, int z, Direction direction, byte subtype)
00138 {
00139   v->x_pos = x;
00140   v->y_pos = y;
00141   v->z_pos = z;
00142   v->tile = TileVirtXY(x, y);
00143   v->direction = direction;
00144   v->subtype = subtype;
00145   v->UpdateDeltaXY(INVALID_DIR);
00146   v->owner = OWNER_NONE;
00147   v->vehstatus = VS_UNCLICKABLE;
00148   v->image_override = 0;
00149   v->current_order.Free();
00150 
00151   DisasterVehicleUpdateImage(v);
00152   VehicleUpdatePositionAndViewport(v);
00153 }
00154 
00155 static void SetDisasterVehiclePos(DisasterVehicle *v, int x, int y, int z)
00156 {
00157   v->x_pos = x;
00158   v->y_pos = y;
00159   v->z_pos = z;
00160   v->tile = TileVirtXY(x, y);
00161 
00162   DisasterVehicleUpdateImage(v);
00163   VehicleUpdatePositionAndViewport(v);
00164 
00165   DisasterVehicle *u = v->Next();
00166   if (u != NULL) {
00167     int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
00168     int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
00169 
00170     u->x_pos = x;
00171     u->y_pos = y - 1 - (max(z - GetSlopePixelZ(safe_x, safe_y), 0) >> 3);
00172     safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
00173     u->z_pos = GetSlopePixelZ(safe_x, safe_y);
00174     u->direction = v->direction;
00175 
00176     DisasterVehicleUpdateImage(u);
00177     VehicleUpdatePositionAndViewport(u);
00178 
00179     if ((u = u->Next()) != NULL) {
00180       u->x_pos = x;
00181       u->y_pos = y;
00182       u->z_pos = z + 5;
00183       VehicleUpdatePositionAndViewport(u);
00184     }
00185   }
00186 }
00187 
00196 static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
00197 {
00198   v->tick_counter++;
00199 
00200   if (v->current_order.GetDestination() < 2) {
00201     if (HasBit(v->tick_counter, 0)) return true;
00202 
00203     GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00204 
00205     SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00206 
00207     if (v->current_order.GetDestination() == 1) {
00208       if (++v->age == 38) {
00209         v->current_order.SetDestination(2);
00210         v->age = 0;
00211       }
00212 
00213       if (GB(v->tick_counter, 0, 3) == 0) CreateEffectVehicleRel(v, 0, -17, 2, EV_CRASH_SMOKE);
00214 
00215     } else if (v->current_order.GetDestination() == 0) {
00216       if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
00217         v->current_order.SetDestination(1);
00218         v->age = 0;
00219 
00220         SetDParam(0, GetStationIndex(v->tile));
00221         AddVehicleNewsItem(STR_NEWS_DISASTER_ZEPPELIN,
00222           NS_ACCIDENT,
00223           v->index); // Delete the news, when the zeppelin is gone
00224         AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCrashed(GetStationIndex(v->tile)));
00225       }
00226     }
00227 
00228     if (v->y_pos >= (int)((MapSizeY() + 9) * TILE_SIZE - 1)) {
00229       delete v;
00230       return false;
00231     }
00232 
00233     return true;
00234   }
00235 
00236   if (v->current_order.GetDestination() > 2) {
00237     if (++v->age <= 13320) return true;
00238 
00239     if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
00240       Station *st = Station::GetByTile(v->tile);
00241       CLRBITS(st->airport.flags, RUNWAY_IN_block);
00242       AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCleared(st->index));
00243     }
00244 
00245     SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos);
00246     delete v;
00247     return false;
00248   }
00249 
00250   int x = v->x_pos;
00251   int y = v->y_pos;
00252   int z = GetSlopePixelZ(x, y);
00253   if (z < v->z_pos) z = v->z_pos - 1;
00254   SetDisasterVehiclePos(v, x, y, z);
00255 
00256   if (++v->age == 1) {
00257     CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00258     SndPlayVehicleFx(SND_12_EXPLOSION, v);
00259     v->image_override = SPR_BLIMP_CRASHING;
00260   } else if (v->age == 70) {
00261     v->image_override = SPR_BLIMP_CRASHED;
00262   } else if (v->age <= 300) {
00263     if (GB(v->tick_counter, 0, 3) == 0) {
00264       uint32 r = Random();
00265 
00266       CreateEffectVehicleRel(v,
00267         GB(r, 0, 4) - 7,
00268         GB(r, 4, 4) - 7,
00269         GB(r, 8, 3) + 5,
00270         EV_EXPLOSION_SMALL);
00271     }
00272   } else if (v->age == 350) {
00273     v->current_order.SetDestination(3);
00274     v->age = 0;
00275   }
00276 
00277   if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
00278     SETBITS(Station::GetByTile(v->tile)->airport.flags, RUNWAY_IN_block);
00279   }
00280 
00281   return true;
00282 }
00283 
00290 static bool DisasterTick_Ufo(DisasterVehicle *v)
00291 {
00292   v->image_override = (HasBit(++v->tick_counter, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER : SPR_UFO_SMALL_SCOUT;
00293 
00294   if (v->current_order.GetDestination() == 0) {
00295     /* Fly around randomly */
00296     int x = TileX(v->dest_tile) * TILE_SIZE;
00297     int y = TileY(v->dest_tile) * TILE_SIZE;
00298     if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
00299       v->direction = GetDirectionTowards(v, x, y);
00300       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00301       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00302       return true;
00303     }
00304     if (++v->age < 6) {
00305       v->dest_tile = RandomTile();
00306       return true;
00307     }
00308     v->current_order.SetDestination(1);
00309 
00310     uint n = 0; // Total number of targetable road vehicles.
00311     RoadVehicle *u;
00312     FOR_ALL_ROADVEHICLES(u) {
00313       if (u->IsFrontEngine()) n++;
00314     }
00315 
00316     if (n == 0) {
00317       /* If there are no targetable road vehicles, destroy the UFO. */
00318       delete v;
00319       return false;
00320     }
00321 
00322     n = RandomRange(n); // Choose one of them.
00323     FOR_ALL_ROADVEHICLES(u) {
00324       /* Find (n+1)-th road vehicle. */
00325       if (u->IsFrontEngine() && (n-- == 0)) break;
00326     }
00327 
00328     /* Target it. */
00329     v->dest_tile = u->index;
00330     v->age = 0;
00331     return true;
00332   } else {
00333     /* Target a vehicle */
00334     RoadVehicle *u = RoadVehicle::Get(v->dest_tile);
00335     assert(u != NULL && u->type == VEH_ROAD && u->IsFrontEngine());
00336 
00337     uint 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     int 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->crashed_ctr == 0) {
00354         u->Crash();
00355 
00356         AddVehicleNewsItem(STR_NEWS_DISASTER_SMALL_UFO,
00357           NS_ACCIDENT,
00358           u->index); // delete the news, when the roadvehicle is gone
00359 
00360         AI::NewEvent(u->owner, new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
00361         Game::NewEvent(new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
00362       }
00363     }
00364 
00365     /* Destroy? */
00366     if (v->age > 50) {
00367       CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00368       SndPlayVehicleFx(SND_12_EXPLOSION, v);
00369       delete v;
00370       return false;
00371     }
00372   }
00373 
00374   return true;
00375 }
00376 
00377 static void DestructIndustry(Industry *i)
00378 {
00379   for (TileIndex tile = 0; tile != MapSize(); tile++) {
00380     if (i->TileBelongsToIndustry(tile)) {
00381       ResetIndustryConstructionStage(tile);
00382       MarkTileDirtyByTile(tile);
00383     }
00384   }
00385 }
00386 
00400 static bool DisasterTick_Aircraft(DisasterVehicle *v, uint16 image_override, bool leave_at_top, StringID news_message, IndustryBehaviour industry_flag)
00401 {
00402   v->tick_counter++;
00403   v->image_override = (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? image_override : 0;
00404 
00405   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00406   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00407 
00408   if ((leave_at_top && gp.x < (-10 * (int)TILE_SIZE)) || (!leave_at_top && gp.x > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1)) {
00409     delete v;
00410     return false;
00411   }
00412 
00413   if (v->current_order.GetDestination() == 2) {
00414     if (GB(v->tick_counter, 0, 2) == 0) {
00415       Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
00416       int x = TileX(i->location.tile) * TILE_SIZE;
00417       int y = TileY(i->location.tile) * 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       v->current_order.SetDestination(2);
00431       v->age = 0;
00432 
00433       Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
00434       DestructIndustry(i);
00435 
00436       SetDParam(0, i->town->index);
00437       AddIndustryNewsItem(news_message, NS_ACCIDENT, i->index); // delete the news, when the industry closes
00438       SndPlayTileFx(SND_12_EXPLOSION, i->location.tile);
00439     }
00440   } else if (v->current_order.GetDestination() == 0) {
00441     int x = v->x_pos + ((leave_at_top ? -15 : 15) * TILE_SIZE);
00442     int y = v->y_pos;
00443 
00444     if ((uint)x > MapMaxX() * TILE_SIZE - 1) return true;
00445 
00446     TileIndex tile = TileVirtXY(x, y);
00447     if (!IsTileType(tile, MP_INDUSTRY)) return true;
00448 
00449     IndustryID ind = GetIndustryIndex(tile);
00450     v->dest_tile = ind;
00451 
00452     if (GetIndustrySpec(Industry::Get(ind)->type)->behaviour & industry_flag) {
00453       v->current_order.SetDestination(1);
00454       v->age = 0;
00455     }
00456   }
00457 
00458   return true;
00459 }
00460 
00462 static bool DisasterTick_Airplane(DisasterVehicle *v)
00463 {
00464   return DisasterTick_Aircraft(v, SPR_F_15_FIRING, true, STR_NEWS_DISASTER_AIRPLANE_OIL_REFINERY, INDUSTRYBEH_AIRPLANE_ATTACKS);
00465 }
00466 
00468 static bool DisasterTick_Helicopter(DisasterVehicle *v)
00469 {
00470   return DisasterTick_Aircraft(v, SPR_AH_64A_FIRING, false, STR_NEWS_DISASTER_HELICOPTER_FACTORY, INDUSTRYBEH_CHOPPER_ATTACKS);
00471 }
00472 
00474 static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
00475 {
00476   v->tick_counter++;
00477   if (HasBit(v->tick_counter, 0)) return true;
00478 
00479   if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
00480 
00481   VehicleUpdatePositionAndViewport(v);
00482 
00483   return true;
00484 }
00485 
00492 static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
00493 {
00494   v->tick_counter++;
00495 
00496   if (v->current_order.GetDestination() == 1) {
00497     int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00498     int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00499     if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
00500       v->direction = GetDirectionTowards(v, x, y);
00501 
00502       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00503       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00504       return true;
00505     }
00506 
00507     if (!IsValidTile(v->dest_tile)) {
00508       /* Make sure we don't land outside the map. */
00509       delete v;
00510       return false;
00511     }
00512 
00513     int z = GetSlopePixelZ(v->x_pos, v->y_pos);
00514     if (z < v->z_pos) {
00515       SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos - 1);
00516       return true;
00517     }
00518 
00519     v->current_order.SetDestination(2);
00520 
00521     Vehicle *target;
00522     FOR_ALL_VEHICLES(target) {
00523       if (target->IsGroundVehicle()) {
00524         if (Delta(target->x_pos, v->x_pos) + Delta(target->y_pos, v->y_pos) <= 12 * (int)TILE_SIZE) {
00525           target->breakdown_ctr = 5;
00526           target->breakdown_delay = 0xF0;
00527         }
00528       }
00529     }
00530 
00531     Town *t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
00532     SetDParam(0, t->index);
00533     AddNewsItem(STR_NEWS_DISASTER_BIG_UFO,
00534       NS_ACCIDENT,
00535       NR_TILE,
00536       v->tile);
00537 
00538     if (!Vehicle::CanAllocateItem(2)) {
00539       delete v;
00540       return false;
00541     }
00542     DisasterVehicle *u = new DisasterVehicle();
00543 
00544     InitializeDisasterVehicle(u, -6 * (int)TILE_SIZE, v->y_pos, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SW, ST_BIG_UFO_DESTROYER);
00545     u->big_ufo_destroyer_target = v->index;
00546 
00547     DisasterVehicle *w = new DisasterVehicle();
00548 
00549     u->SetNext(w);
00550     InitializeDisasterVehicle(w, -6 * (int)TILE_SIZE, v->y_pos, 0, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
00551     w->vehstatus |= VS_SHADOW;
00552   } else if (v->current_order.GetDestination() == 0) {
00553     int x = TileX(v->dest_tile) * TILE_SIZE;
00554     int y = TileY(v->dest_tile) * TILE_SIZE;
00555     if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
00556       v->direction = GetDirectionTowards(v, x, y);
00557       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00558       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00559       return true;
00560     }
00561 
00562     if (++v->age < 6) {
00563       v->dest_tile = RandomTile();
00564       return true;
00565     }
00566     v->current_order.SetDestination(1);
00567 
00568     TileIndex tile_org = RandomTile();
00569     TileIndex tile = tile_org;
00570     do {
00571       if (IsPlainRailTile(tile) &&
00572           Company::IsHumanID(GetTileOwner(tile))) {
00573         break;
00574       }
00575       tile = TILE_MASK(tile + 1);
00576     } while (tile != tile_org);
00577     v->dest_tile = tile;
00578     v->age = 0;
00579   }
00580 
00581   return true;
00582 }
00583 
00588 static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle *v)
00589 {
00590   v->tick_counter++;
00591 
00592   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00593   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00594 
00595   if (gp.x > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1) {
00596     delete v;
00597     return false;
00598   }
00599 
00600   if (v->current_order.GetDestination() == 0) {
00601     Vehicle *u = Vehicle::Get(v->big_ufo_destroyer_target);
00602     if (Delta(v->x_pos, u->x_pos) > (int)TILE_SIZE) return true;
00603     v->current_order.SetDestination(1);
00604 
00605     CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
00606     SndPlayVehicleFx(SND_12_EXPLOSION, u);
00607 
00608     delete u;
00609 
00610     for (int i = 0; i != 80; i++) {
00611       uint32 r = Random();
00612       CreateEffectVehicleAbove(
00613         GB(r, 0, 6) + v->x_pos - 32,
00614         GB(r, 5, 6) + v->y_pos - 32,
00615         0,
00616         EV_EXPLOSION_SMALL);
00617     }
00618 
00619     for (int dy = -3; dy < 3; dy++) {
00620       for (int dx = -3; dx < 3; dx++) {
00621         TileIndex tile = TileAddWrap(v->tile, dx, dy);
00622         if (tile != INVALID_TILE) DisasterClearSquare(tile);
00623       }
00624     }
00625   }
00626 
00627   return true;
00628 }
00629 
00634 static bool DisasterTick_Submarine(DisasterVehicle *v)
00635 {
00636   v->tick_counter++;
00637 
00638   if (++v->age > 8880) {
00639     delete v;
00640     return false;
00641   }
00642 
00643   if (!HasBit(v->tick_counter, 0)) return true;
00644 
00645   TileIndex tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
00646   if (IsValidTile(tile)) {
00647     TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00648     if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
00649       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00650       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00651       return true;
00652     }
00653   }
00654 
00655   v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
00656 
00657   return true;
00658 }
00659 
00660 
00661 static bool DisasterTick_NULL(DisasterVehicle *v)
00662 {
00663   return true;
00664 }
00665 
00666 typedef bool DisasterVehicleTickProc(DisasterVehicle *v);
00667 
00668 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
00669   DisasterTick_Zeppeliner, DisasterTick_NULL,
00670   DisasterTick_Ufo,        DisasterTick_NULL,
00671   DisasterTick_Airplane,   DisasterTick_NULL,
00672   DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
00673   DisasterTick_Big_Ufo,    DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
00674   DisasterTick_NULL,
00675   DisasterTick_Submarine,
00676   DisasterTick_Submarine,
00677 };
00678 
00679 
00680 bool DisasterVehicle::Tick()
00681 {
00682   return _disastervehicle_tick_procs[this->subtype](this);
00683 }
00684 
00685 typedef void DisasterInitProc();
00686 
00687 
00692 static void Disaster_Zeppeliner_Init()
00693 {
00694   if (!Vehicle::CanAllocateItem(2)) return;
00695 
00696   /* Pick a random place, unless we find a small airport */
00697   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00698 
00699   Station *st;
00700   FOR_ALL_STATIONS(st) {
00701     if (st->airport.tile != INVALID_TILE && (st->airport.type == AT_SMALL || st->airport.type == AT_LARGE)) {
00702       x = (TileX(st->airport.tile) + 2) * TILE_SIZE;
00703       break;
00704     }
00705   }
00706 
00707   DisasterVehicle *v = new DisasterVehicle();
00708   InitializeDisasterVehicle(v, x, 0, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SE, ST_ZEPPELINER);
00709 
00710   /* Allocate shadow */
00711   DisasterVehicle *u = new DisasterVehicle();
00712   v->SetNext(u);
00713   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
00714   u->vehstatus |= VS_SHADOW;
00715 }
00716 
00717 
00722 static void Disaster_Small_Ufo_Init()
00723 {
00724   if (!Vehicle::CanAllocateItem(2)) return;
00725 
00726   DisasterVehicle *v = new DisasterVehicle();
00727   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00728 
00729   InitializeDisasterVehicle(v, x, 0, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SE, ST_SMALL_UFO);
00730   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00731   v->age = 0;
00732 
00733   /* Allocate shadow */
00734   DisasterVehicle *u = new DisasterVehicle();
00735   v->SetNext(u);
00736   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
00737   u->vehstatus |= VS_SHADOW;
00738 }
00739 
00740 
00741 /* Combat airplane which destroys an oil refinery */
00742 static void Disaster_Airplane_Init()
00743 {
00744   if (!Vehicle::CanAllocateItem(2)) return;
00745 
00746   Industry *i, *found = NULL;
00747 
00748   FOR_ALL_INDUSTRIES(i) {
00749     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
00750         (found == NULL || Chance16(1, 2))) {
00751       found = i;
00752     }
00753   }
00754 
00755   if (found == NULL) return;
00756 
00757   DisasterVehicle *v = new DisasterVehicle();
00758 
00759   /* Start from the bottom (south side) of the map */
00760   int x = (MapSizeX() + 9) * TILE_SIZE - 1;
00761   int y = TileY(found->location.tile) * TILE_SIZE + 37;
00762 
00763   InitializeDisasterVehicle(v, x, y, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_NE, ST_AIRPLANE);
00764 
00765   DisasterVehicle *u = new DisasterVehicle();
00766   v->SetNext(u);
00767   InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_AIRPLANE_SHADOW);
00768   u->vehstatus |= VS_SHADOW;
00769 }
00770 
00771 
00773 static void Disaster_Helicopter_Init()
00774 {
00775   if (!Vehicle::CanAllocateItem(3)) return;
00776 
00777   Industry *i, *found = NULL;
00778 
00779   FOR_ALL_INDUSTRIES(i) {
00780     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
00781         (found == NULL || Chance16(1, 2))) {
00782       found = i;
00783     }
00784   }
00785 
00786   if (found == NULL) return;
00787 
00788   DisasterVehicle *v = new DisasterVehicle();
00789 
00790   int x = -16 * (int)TILE_SIZE;
00791   int y = TileY(found->location.tile) * TILE_SIZE + 37;
00792 
00793   InitializeDisasterVehicle(v, x, y, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SW, ST_HELICOPTER);
00794 
00795   DisasterVehicle *u = new DisasterVehicle();
00796   v->SetNext(u);
00797   InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_HELICOPTER_SHADOW);
00798   u->vehstatus |= VS_SHADOW;
00799 
00800   DisasterVehicle *w = new DisasterVehicle();
00801   u->SetNext(w);
00802   InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_HELICOPTER_ROTORS);
00803 }
00804 
00805 
00806 /* Big Ufo which lands on a piece of rail and will consequently be shot
00807  * down by a combat airplane, destroying the surroundings */
00808 static void Disaster_Big_Ufo_Init()
00809 {
00810   if (!Vehicle::CanAllocateItem(2)) return;
00811 
00812   DisasterVehicle *v = new DisasterVehicle();
00813   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00814   int y = MapMaxX() * TILE_SIZE - 1;
00815 
00816   InitializeDisasterVehicle(v, x, y, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_NW, ST_BIG_UFO);
00817   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00818   v->age = 0;
00819 
00820   /* Allocate shadow */
00821   DisasterVehicle *u = new DisasterVehicle();
00822   v->SetNext(u);
00823   InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_BIG_UFO_SHADOW);
00824   u->vehstatus |= VS_SHADOW;
00825 }
00826 
00827 
00828 static void Disaster_Submarine_Init(DisasterSubType subtype)
00829 {
00830   if (!Vehicle::CanAllocateItem()) return;
00831 
00832   int y;
00833   Direction dir;
00834   uint32 r = Random();
00835   int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
00836 
00837   if (HasBit(r, 31)) {
00838     y = MapMaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
00839     dir = DIR_NW;
00840   } else {
00841     y = TILE_SIZE / 2;
00842     if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
00843     dir = DIR_SE;
00844   }
00845   if (!IsWaterTile(TileVirtXY(x, y))) return;
00846 
00847   DisasterVehicle *v = new DisasterVehicle();
00848   InitializeDisasterVehicle(v, x, y, 0, dir, subtype);
00849   v->age = 0;
00850 }
00851 
00852 /* Curious submarine #1, just floats around */
00853 static void Disaster_Small_Submarine_Init()
00854 {
00855   Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
00856 }
00857 
00858 
00859 /* Curious submarine #2, just floats around */
00860 static void Disaster_Big_Submarine_Init()
00861 {
00862   Disaster_Submarine_Init(ST_BIG_SUBMARINE);
00863 }
00864 
00865 
00870 static void Disaster_CoalMine_Init()
00871 {
00872   int index = GB(Random(), 0, 4);
00873   uint m;
00874 
00875   for (m = 0; m < 15; m++) {
00876     const Industry *i;
00877 
00878     FOR_ALL_INDUSTRIES(i) {
00879       if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
00880         SetDParam(0, i->town->index);
00881         AddNewsItem(STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE,
00882           NS_ACCIDENT, NR_TILE, i->location.tile + TileDiffXY(1, 1)); // keep the news, even when the mine closes
00883 
00884         {
00885           TileIndex tile = i->location.tile;
00886           TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
00887 
00888           for (uint n = 0; n < 30; n++) {
00889             DisasterClearSquare(tile);
00890             tile += step;
00891             if (!IsValidTile(tile)) break;
00892           }
00893         }
00894         return;
00895       }
00896     }
00897   }
00898 }
00899 
00900 struct Disaster {
00901   DisasterInitProc *init_proc; 
00902   Year min_year;               
00903   Year max_year;               
00904 };
00905 
00906 static const Disaster _disasters[] = {
00907   {Disaster_Zeppeliner_Init,      1930, 1955}, // zeppeliner
00908   {Disaster_Small_Ufo_Init,       1940, 1970}, // ufo (small)
00909   {Disaster_Airplane_Init,        1960, 1990}, // airplane
00910   {Disaster_Helicopter_Init,      1970, 2000}, // helicopter
00911   {Disaster_Big_Ufo_Init,         2000, 2100}, // ufo (big)
00912   {Disaster_Small_Submarine_Init, 1940, 1965}, // submarine (small)
00913   {Disaster_Big_Submarine_Init,   1975, 2010}, // submarine (big)
00914   {Disaster_CoalMine_Init,        1950, 1985}, // coalmine
00915 };
00916 
00917 static void DoDisaster()
00918 {
00919   byte buf[lengthof(_disasters)];
00920 
00921   byte j = 0;
00922   for (size_t i = 0; i != lengthof(_disasters); i++) {
00923     if (_cur_year >= _disasters[i].min_year && _cur_year < _disasters[i].max_year) buf[j++] = (byte)i;
00924   }
00925 
00926   if (j == 0) return;
00927 
00928   _disasters[buf[RandomRange(j)]].init_proc();
00929 }
00930 
00931 
00932 static void ResetDisasterDelay()
00933 {
00934   _disaster_delay = GB(Random(), 0, 9) + 730;
00935 }
00936 
00937 void DisasterDailyLoop()
00938 {
00939   if (--_disaster_delay != 0) return;
00940 
00941   ResetDisasterDelay();
00942 
00943   if (_settings_game.difficulty.disasters != 0) DoDisaster();
00944 }
00945 
00946 void StartupDisasters()
00947 {
00948   ResetDisasterDelay();
00949 }
00950 
00956 void ReleaseDisastersTargetingIndustry(IndustryID i)
00957 {
00958   DisasterVehicle *v;
00959   FOR_ALL_DISASTERVEHICLES(v) {
00960     /* primary disaster vehicles that have chosen target */
00961     if (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER) {
00962       /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
00963       if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
00964     }
00965   }
00966 }
00967 
00972 void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
00973 {
00974   DisasterVehicle *v;
00975   FOR_ALL_DISASTERVEHICLES(v) {
00976     /* primary disaster vehicles that have chosen target */
00977     if (v->subtype == ST_SMALL_UFO) {
00978       if (v->current_order.GetDestination() != 0 && v->dest_tile == vehicle) {
00979         /* Revert to target-searching */
00980         v->current_order.SetDestination(0);
00981         v->dest_tile = RandomTile();
00982         v->z_pos = INITIAL_DISASTER_VEHICLE_ZPOS;
00983         v->age = 0;
00984       }
00985     }
00986   }
00987 }
00988 
00989 void DisasterVehicle::UpdateDeltaXY(Direction direction)
00990 {
00991   this->x_offs        = -1;
00992   this->y_offs        = -1;
00993   this->x_extent      =  2;
00994   this->y_extent      =  2;
00995   this->z_extent      =  5;
00996 }