disaster_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: disaster_cmd.cpp 21949 2011-02-02 23:06:38Z smatz $ */
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 "functions.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 "company_base.h"
00044 #include "core/random_func.hpp"
00045 #include "core/backup_type.hpp"
00046 
00047 #include "table/strings.h"
00048 
00050 uint16 _disaster_delay;
00051 
00052 enum DisasterSubType {
00053   ST_ZEPPELINER,
00054   ST_ZEPPELINER_SHADOW,
00055   ST_SMALL_UFO,
00056   ST_SMALL_UFO_SHADOW,
00057   ST_AIRPLANE,
00058   ST_AIRPLANE_SHADOW,
00059   ST_HELICOPTER,
00060   ST_HELICOPTER_SHADOW,
00061   ST_HELICOPTER_ROTORS,
00062   ST_BIG_UFO,
00063   ST_BIG_UFO_SHADOW,
00064   ST_BIG_UFO_DESTROYER,
00065   ST_BIG_UFO_DESTROYER_SHADOW,
00066   ST_SMALL_SUBMARINE,
00067   ST_BIG_SUBMARINE,
00068 };
00069 
00070 static const uint INITIAL_DISASTER_VEHICLE_ZPOS = 135; 
00071 
00072 static void DisasterClearSquare(TileIndex tile)
00073 {
00074   if (EnsureNoVehicleOnGround(tile).Failed()) return;
00075 
00076   switch (GetTileType(tile)) {
00077     case MP_RAILWAY:
00078       if (Company::IsHumanID(GetTileOwner(tile))) {
00079         Backup<CompanyByte> cur_company(_current_company, OWNER_WATER, FILE_LINE);
00080         DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00081         cur_company.Restore();
00082 
00083         /* update signals in buffer */
00084         UpdateSignalsInBuffer();
00085       }
00086       break;
00087 
00088     case MP_HOUSE: {
00089       Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
00090       DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00091       cur_company.Restore();
00092       break;
00093     }
00094 
00095     case MP_TREES:
00096     case MP_CLEAR:
00097       DoClearSquare(tile);
00098       break;
00099 
00100     default:
00101       break;
00102   }
00103 }
00104 
00105 static const SpriteID _disaster_images_1[] = {SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP};
00106 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};
00107 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};
00108 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};
00109 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};
00110 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};
00111 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};
00112 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};
00113 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};
00114 
00115 static const SpriteID * const _disaster_images[] = {
00116   _disaster_images_1, _disaster_images_1,                     
00117   _disaster_images_2, _disaster_images_2,                     
00118   _disaster_images_3, _disaster_images_3,                     
00119   _disaster_images_8, _disaster_images_8, _disaster_images_9, 
00120   _disaster_images_6, _disaster_images_6,                     
00121   _disaster_images_7, _disaster_images_7,                     
00122   _disaster_images_4, _disaster_images_5,                     
00123 };
00124 
00125 static void DisasterVehicleUpdateImage(DisasterVehicle *v)
00126 {
00127   SpriteID img = v->image_override;
00128   if (img == 0) img = _disaster_images[v->subtype][v->direction];
00129   v->cur_image = img;
00130 }
00131 
00136 static void InitializeDisasterVehicle(DisasterVehicle *v, int x, int y, byte z, Direction direction, byte subtype)
00137 {
00138   v->x_pos = x;
00139   v->y_pos = y;
00140   v->z_pos = z;
00141   v->tile = TileVirtXY(x, y);
00142   v->direction = direction;
00143   v->subtype = subtype;
00144   v->UpdateDeltaXY(INVALID_DIR);
00145   v->owner = OWNER_NONE;
00146   v->vehstatus = VS_UNCLICKABLE;
00147   v->image_override = 0;
00148   v->current_order.Free();
00149 
00150   DisasterVehicleUpdateImage(v);
00151   VehicleMove(v, false);
00152   MarkSingleVehicleDirty(v);
00153 }
00154 
00155 static void SetDisasterVehiclePos(DisasterVehicle *v, int x, int y, byte 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   VehicleMove(v, true);
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 - GetSlopeZ(safe_x, safe_y), 0U) >> 3);
00172     safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
00173     u->z_pos = GetSlopeZ(safe_x, safe_y);
00174     u->direction = v->direction;
00175 
00176     DisasterVehicleUpdateImage(u);
00177     VehicleMove(u, true);
00178 
00179     if ((u = u->Next()) != NULL) {
00180       u->x_pos = x;
00181       u->y_pos = y;
00182       u->z_pos = z + 5;
00183       VehicleMove(u, true);
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_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 AIEventDisasterZeppelinerCrashed(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 AIEventDisasterZeppelinerCleared(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   byte z = GetSlopeZ(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     byte 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 AIEventVehicleCrashed(u->index, u->tile, AIEventVehicleCrashed::CRASH_RV_UFO));
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       return false;
00370     }
00371   }
00372 
00373   return true;
00374 }
00375 
00376 static void DestructIndustry(Industry *i)
00377 {
00378   for (TileIndex tile = 0; tile != MapSize(); tile++) {
00379     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == i->index) {
00380       ResetIndustryConstructionStage(tile);
00381       MarkTileDirtyByTile(tile);
00382     }
00383   }
00384 }
00385 
00399 static bool DisasterTick_Aircraft(DisasterVehicle *v, uint16 image_override, bool leave_at_top, StringID news_message, IndustryBehaviour industry_flag)
00400 {
00401   v->tick_counter++;
00402   v->image_override = (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? image_override : 0;
00403 
00404   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00405   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00406 
00407   if ((leave_at_top && gp.x < (-10 * (int)TILE_SIZE)) || (!leave_at_top && gp.x > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1)) {
00408     delete v;
00409     return false;
00410   }
00411 
00412   if (v->current_order.GetDestination() == 2) {
00413     if (GB(v->tick_counter, 0, 2) == 0) {
00414       Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
00415       int x = TileX(i->location.tile) * TILE_SIZE;
00416       int y = TileY(i->location.tile) * TILE_SIZE;
00417       uint32 r = Random();
00418 
00419       CreateEffectVehicleAbove(
00420         GB(r,  0, 6) + x,
00421         GB(r,  6, 6) + y,
00422         GB(r, 12, 4),
00423         EV_EXPLOSION_SMALL);
00424 
00425       if (++v->age >= 55) v->current_order.SetDestination(3);
00426     }
00427   } else if (v->current_order.GetDestination() == 1) {
00428     if (++v->age == 112) {
00429       v->current_order.SetDestination(2);
00430       v->age = 0;
00431 
00432       Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
00433       DestructIndustry(i);
00434 
00435       SetDParam(0, i->town->index);
00436       AddIndustryNewsItem(news_message, NS_ACCIDENT, i->index); // delete the news, when the industry closes
00437       SndPlayTileFx(SND_12_EXPLOSION, i->location.tile);
00438     }
00439   } else if (v->current_order.GetDestination() == 0) {
00440     int x = v->x_pos + ((leave_at_top ? -15 : 15) * TILE_SIZE);
00441     int y = v->y_pos;
00442 
00443     if ((uint)x > MapMaxX() * TILE_SIZE - 1) return true;
00444 
00445     TileIndex tile = TileVirtXY(x, y);
00446     if (!IsTileType(tile, MP_INDUSTRY)) return true;
00447 
00448     IndustryID ind = GetIndustryIndex(tile);
00449     v->dest_tile = ind;
00450 
00451     if (GetIndustrySpec(Industry::Get(ind)->type)->behaviour & industry_flag) {
00452       v->current_order.SetDestination(1);
00453       v->age = 0;
00454     }
00455   }
00456 
00457   return true;
00458 }
00459 
00461 static bool DisasterTick_Airplane(DisasterVehicle *v)
00462 {
00463   return DisasterTick_Aircraft(v, SPR_F_15_FIRING, true, STR_NEWS_DISASTER_AIRPLANE_OIL_REFINERY, INDUSTRYBEH_AIRPLANE_ATTACKS);
00464 }
00465 
00467 static bool DisasterTick_Helicopter(DisasterVehicle *v)
00468 {
00469   return DisasterTick_Aircraft(v, SPR_AH_64A_FIRING, false, STR_NEWS_DISASTER_HELICOPTER_FACTORY, INDUSTRYBEH_CHOPPER_ATTACKS);
00470 }
00471 
00473 static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
00474 {
00475   v->tick_counter++;
00476   if (HasBit(v->tick_counter, 0)) return true;
00477 
00478   if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
00479 
00480   VehicleMove(v, true);
00481 
00482   return true;
00483 }
00484 
00491 static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
00492 {
00493   v->tick_counter++;
00494 
00495   if (v->current_order.GetDestination() == 1) {
00496     int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00497     int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00498     if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
00499       v->direction = GetDirectionTowards(v, x, y);
00500 
00501       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00502       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00503       return true;
00504     }
00505 
00506     if (!IsValidTile(v->dest_tile)) {
00507       /* Make sure we don't land outside the map. */
00508       delete v;
00509       return false;
00510     }
00511 
00512     byte z = GetSlopeZ(v->x_pos, v->y_pos);
00513     if (z < v->z_pos) {
00514       SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos - 1);
00515       return true;
00516     }
00517 
00518     v->current_order.SetDestination(2);
00519 
00520     Vehicle *target;
00521     FOR_ALL_VEHICLES(target) {
00522       if (target->IsGroundVehicle()) {
00523         if (Delta(target->x_pos, v->x_pos) + Delta(target->y_pos, v->y_pos) <= 12 * (int)TILE_SIZE) {
00524           target->breakdown_ctr = 5;
00525           target->breakdown_delay = 0xF0;
00526         }
00527       }
00528     }
00529 
00530     Town *t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
00531     SetDParam(0, t->index);
00532     AddNewsItem(STR_NEWS_DISASTER_BIG_UFO,
00533       NS_ACCIDENT,
00534       NR_TILE,
00535       v->tile);
00536 
00537     if (!Vehicle::CanAllocateItem(2)) {
00538       delete v;
00539       return false;
00540     }
00541     DisasterVehicle *u = new DisasterVehicle();
00542 
00543     InitializeDisasterVehicle(u, -6 * (int)TILE_SIZE, v->y_pos, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SW, ST_BIG_UFO_DESTROYER);
00544     u->big_ufo_destroyer_target = v->index;
00545 
00546     DisasterVehicle *w = new DisasterVehicle();
00547 
00548     u->SetNext(w);
00549     InitializeDisasterVehicle(w, -6 * (int)TILE_SIZE, v->y_pos, 0, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
00550     w->vehstatus |= VS_SHADOW;
00551   } else if (v->current_order.GetDestination() == 0) {
00552     int x = TileX(v->dest_tile) * TILE_SIZE;
00553     int y = TileY(v->dest_tile) * TILE_SIZE;
00554     if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
00555       v->direction = GetDirectionTowards(v, x, y);
00556       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00557       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00558       return true;
00559     }
00560 
00561     if (++v->age < 6) {
00562       v->dest_tile = RandomTile();
00563       return true;
00564     }
00565     v->current_order.SetDestination(1);
00566 
00567     TileIndex tile_org = RandomTile();
00568     TileIndex tile = tile_org;
00569     do {
00570       if (IsPlainRailTile(tile) &&
00571           Company::IsHumanID(GetTileOwner(tile))) {
00572         break;
00573       }
00574       tile = TILE_MASK(tile + 1);
00575     } while (tile != tile_org);
00576     v->dest_tile = tile;
00577     v->age = 0;
00578   }
00579 
00580   return true;
00581 }
00582 
00587 static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle *v)
00588 {
00589   v->tick_counter++;
00590 
00591   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00592   SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00593 
00594   if (gp.x > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1) {
00595     delete v;
00596     return false;
00597   }
00598 
00599   if (v->current_order.GetDestination() == 0) {
00600     Vehicle *u = Vehicle::Get(v->big_ufo_destroyer_target);
00601     if (Delta(v->x_pos, u->x_pos) > (int)TILE_SIZE) return true;
00602     v->current_order.SetDestination(1);
00603 
00604     CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
00605     SndPlayVehicleFx(SND_12_EXPLOSION, u);
00606 
00607     delete u;
00608 
00609     for (int i = 0; i != 80; i++) {
00610       uint32 r = Random();
00611       CreateEffectVehicleAbove(
00612         GB(r, 0, 6) + v->x_pos - 32,
00613         GB(r, 5, 6) + v->y_pos - 32,
00614         0,
00615         EV_EXPLOSION_SMALL);
00616     }
00617 
00618     for (int dy = -3; dy < 3; dy++) {
00619       for (int dx = -3; dx < 3; dx++) {
00620         TileIndex tile = TileAddWrap(v->tile, dx, dy);
00621         if (tile != INVALID_TILE) DisasterClearSquare(tile);
00622       }
00623     }
00624   }
00625 
00626   return true;
00627 }
00628 
00633 static bool DisasterTick_Submarine(DisasterVehicle *v)
00634 {
00635   v->tick_counter++;
00636 
00637   if (++v->age > 8880) {
00638     delete v;
00639     return false;
00640   }
00641 
00642   if (!HasBit(v->tick_counter, 0)) return true;
00643 
00644   TileIndex tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
00645   if (IsValidTile(tile)) {
00646     TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00647     if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
00648       GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00649       SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00650       return true;
00651     }
00652   }
00653 
00654   v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
00655 
00656   return true;
00657 }
00658 
00659 
00660 static bool DisasterTick_NULL(DisasterVehicle *v)
00661 {
00662   return true;
00663 }
00664 
00665 typedef bool DisasterVehicleTickProc(DisasterVehicle *v);
00666 
00667 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
00668   DisasterTick_Zeppeliner, DisasterTick_NULL,
00669   DisasterTick_Ufo,        DisasterTick_NULL,
00670   DisasterTick_Airplane,   DisasterTick_NULL,
00671   DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
00672   DisasterTick_Big_Ufo,    DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
00673   DisasterTick_NULL,
00674   DisasterTick_Submarine,
00675   DisasterTick_Submarine,
00676 };
00677 
00678 
00679 bool DisasterVehicle::Tick()
00680 {
00681   return _disastervehicle_tick_procs[this->subtype](this);
00682 }
00683 
00684 typedef void DisasterInitProc();
00685 
00686 
00691 static void Disaster_Zeppeliner_Init()
00692 {
00693   if (!Vehicle::CanAllocateItem(2)) return;
00694 
00695   /* Pick a random place, unless we find a small airport */
00696   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00697 
00698   Station *st;
00699   FOR_ALL_STATIONS(st) {
00700     if (st->airport.tile != INVALID_TILE && (st->airport.type == AT_SMALL || st->airport.type == AT_LARGE)) {
00701       x = (TileX(st->airport.tile) + 2) * TILE_SIZE;
00702       break;
00703     }
00704   }
00705 
00706   DisasterVehicle *v = new DisasterVehicle();
00707   InitializeDisasterVehicle(v, x, 0, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SE, ST_ZEPPELINER);
00708 
00709   /* Allocate shadow */
00710   DisasterVehicle *u = new DisasterVehicle();
00711   v->SetNext(u);
00712   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
00713   u->vehstatus |= VS_SHADOW;
00714 }
00715 
00716 
00721 static void Disaster_Small_Ufo_Init()
00722 {
00723   if (!Vehicle::CanAllocateItem(2)) return;
00724 
00725   DisasterVehicle *v = new DisasterVehicle();
00726   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00727 
00728   InitializeDisasterVehicle(v, x, 0, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SE, ST_SMALL_UFO);
00729   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00730   v->age = 0;
00731 
00732   /* Allocate shadow */
00733   DisasterVehicle *u = new DisasterVehicle();
00734   v->SetNext(u);
00735   InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
00736   u->vehstatus |= VS_SHADOW;
00737 }
00738 
00739 
00740 /* Combat airplane which destroys an oil refinery */
00741 static void Disaster_Airplane_Init()
00742 {
00743   if (!Vehicle::CanAllocateItem(2)) return;
00744 
00745   Industry *i, *found = NULL;
00746 
00747   FOR_ALL_INDUSTRIES(i) {
00748     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
00749         (found == NULL || Chance16(1, 2))) {
00750       found = i;
00751     }
00752   }
00753 
00754   if (found == NULL) return;
00755 
00756   DisasterVehicle *v = new DisasterVehicle();
00757 
00758   /* Start from the bottom (south side) of the map */
00759   int x = (MapSizeX() + 9) * TILE_SIZE - 1;
00760   int y = TileY(found->location.tile) * TILE_SIZE + 37;
00761 
00762   InitializeDisasterVehicle(v, x, y, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_NE, ST_AIRPLANE);
00763 
00764   DisasterVehicle *u = new DisasterVehicle();
00765   v->SetNext(u);
00766   InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_AIRPLANE_SHADOW);
00767   u->vehstatus |= VS_SHADOW;
00768 }
00769 
00770 
00772 static void Disaster_Helicopter_Init()
00773 {
00774   if (!Vehicle::CanAllocateItem(3)) return;
00775 
00776   Industry *i, *found = NULL;
00777 
00778   FOR_ALL_INDUSTRIES(i) {
00779     if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
00780         (found == NULL || Chance16(1, 2))) {
00781       found = i;
00782     }
00783   }
00784 
00785   if (found == NULL) return;
00786 
00787   DisasterVehicle *v = new DisasterVehicle();
00788 
00789   int x = -16 * (int)TILE_SIZE;
00790   int y = TileY(found->location.tile) * TILE_SIZE + 37;
00791 
00792   InitializeDisasterVehicle(v, x, y, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SW, ST_HELICOPTER);
00793 
00794   DisasterVehicle *u = new DisasterVehicle();
00795   v->SetNext(u);
00796   InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_HELICOPTER_SHADOW);
00797   u->vehstatus |= VS_SHADOW;
00798 
00799   DisasterVehicle *w = new DisasterVehicle();
00800   u->SetNext(w);
00801   InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_HELICOPTER_ROTORS);
00802 }
00803 
00804 
00805 /* Big Ufo which lands on a piece of rail and will consequently be shot
00806  * down by a combat airplane, destroying the surroundings */
00807 static void Disaster_Big_Ufo_Init()
00808 {
00809   if (!Vehicle::CanAllocateItem(2)) return;
00810 
00811   DisasterVehicle *v = new DisasterVehicle();
00812   int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00813   int y = MapMaxX() * TILE_SIZE - 1;
00814 
00815   InitializeDisasterVehicle(v, x, y, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_NW, ST_BIG_UFO);
00816   v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00817   v->age = 0;
00818 
00819   /* Allocate shadow */
00820   DisasterVehicle *u = new DisasterVehicle();
00821   v->SetNext(u);
00822   InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_BIG_UFO_SHADOW);
00823   u->vehstatus |= VS_SHADOW;
00824 }
00825 
00826 
00827 static void Disaster_Submarine_Init(DisasterSubType subtype)
00828 {
00829   if (!Vehicle::CanAllocateItem()) return;
00830 
00831   int y;
00832   Direction dir;
00833   uint32 r = Random();
00834   int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
00835 
00836   if (HasBit(r, 31)) {
00837     y = MapMaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
00838     dir = DIR_NW;
00839   } else {
00840     y = TILE_SIZE / 2;
00841     if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
00842     dir = DIR_SE;
00843   }
00844   if (!IsWaterTile(TileVirtXY(x, y))) return;
00845 
00846   DisasterVehicle *v = new DisasterVehicle();
00847   InitializeDisasterVehicle(v, x, y, 0, dir, subtype);
00848   v->age = 0;
00849 }
00850 
00851 /* Curious submarine #1, just floats around */
00852 static void Disaster_Small_Submarine_Init()
00853 {
00854   Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
00855 }
00856 
00857 
00858 /* Curious submarine #2, just floats around */
00859 static void Disaster_Big_Submarine_Init()
00860 {
00861   Disaster_Submarine_Init(ST_BIG_SUBMARINE);
00862 }
00863 
00864 
00869 static void Disaster_CoalMine_Init()
00870 {
00871   int index = GB(Random(), 0, 4);
00872   uint m;
00873 
00874   for (m = 0; m < 15; m++) {
00875     const Industry *i;
00876 
00877     FOR_ALL_INDUSTRIES(i) {
00878       if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
00879         SetDParam(0, i->town->index);
00880         AddNewsItem(STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE,
00881           NS_ACCIDENT, NR_TILE, i->location.tile + TileDiffXY(1, 1)); // keep the news, even when the mine closes
00882 
00883         {
00884           TileIndex tile = i->location.tile;
00885           TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
00886 
00887           for (uint n = 0; n < 30; n++) {
00888             DisasterClearSquare(tile);
00889             tile += step;
00890             if (!IsValidTile(tile)) break;
00891           }
00892         }
00893         return;
00894       }
00895     }
00896   }
00897 }
00898 
00899 struct Disaster {
00900   DisasterInitProc *init_proc; 
00901   Year min_year;               
00902   Year max_year;               
00903 };
00904 
00905 static const Disaster _disasters[] = {
00906   {Disaster_Zeppeliner_Init,      1930, 1955}, // zeppeliner
00907   {Disaster_Small_Ufo_Init,       1940, 1970}, // ufo (small)
00908   {Disaster_Airplane_Init,        1960, 1990}, // airplane
00909   {Disaster_Helicopter_Init,      1970, 2000}, // helicopter
00910   {Disaster_Big_Ufo_Init,         2000, 2100}, // ufo (big)
00911   {Disaster_Small_Submarine_Init, 1940, 1965}, // submarine (small)
00912   {Disaster_Big_Submarine_Init,   1975, 2010}, // submarine (big)
00913   {Disaster_CoalMine_Init,        1950, 1985}, // coalmine
00914 };
00915 
00916 static void DoDisaster()
00917 {
00918   byte buf[lengthof(_disasters)];
00919 
00920   byte j = 0;
00921   for (size_t i = 0; i != lengthof(_disasters); i++) {
00922     if (_cur_year >= _disasters[i].min_year && _cur_year < _disasters[i].max_year) buf[j++] = (byte)i;
00923   }
00924 
00925   if (j == 0) return;
00926 
00927   _disasters[buf[RandomRange(j)]].init_proc();
00928 }
00929 
00930 
00931 static void ResetDisasterDelay()
00932 {
00933   _disaster_delay = GB(Random(), 0, 9) + 730;
00934 }
00935 
00936 void DisasterDailyLoop()
00937 {
00938   if (--_disaster_delay != 0) return;
00939 
00940   ResetDisasterDelay();
00941 
00942   if (_settings_game.difficulty.disasters != 0) DoDisaster();
00943 }
00944 
00945 void StartupDisasters()
00946 {
00947   ResetDisasterDelay();
00948 }
00949 
00955 void ReleaseDisastersTargetingIndustry(IndustryID i)
00956 {
00957   DisasterVehicle *v;
00958   FOR_ALL_DISASTERVEHICLES(v) {
00959     /* primary disaster vehicles that have chosen target */
00960     if (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER) {
00961       /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
00962       if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
00963     }
00964   }
00965 }
00966 
00971 void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
00972 {
00973   DisasterVehicle *v;
00974   FOR_ALL_DISASTERVEHICLES(v) {
00975     /* primary disaster vehicles that have chosen target */
00976     if (v->subtype == ST_SMALL_UFO) {
00977       if (v->current_order.GetDestination() != 0 && v->dest_tile == vehicle) {
00978         /* Revert to target-searching */
00979         v->current_order.SetDestination(0);
00980         v->dest_tile = RandomTile();
00981         v->z_pos = INITIAL_DISASTER_VEHICLE_ZPOS;
00982         v->age = 0;
00983       }
00984     }
00985   }
00986 }
00987 
00988 void DisasterVehicle::UpdateDeltaXY(Direction direction)
00989 {
00990   this->x_offs        = -1;
00991   this->y_offs        = -1;
00992   this->x_extent      =  2;
00993   this->y_extent      =  2;
00994   this->z_extent      =  5;
00995 }

Generated on Fri Feb 4 20:53:39 2011 for OpenTTD by  doxygen 1.6.1