00001
00002
00003
00004
00005
00006
00007
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
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 VehicleMove(v, false);
00153 MarkSingleVehicleDirty(v);
00154 }
00155
00156 static void SetDisasterVehiclePos(DisasterVehicle *v, int x, int y, int z)
00157 {
00158 v->x_pos = x;
00159 v->y_pos = y;
00160 v->z_pos = z;
00161 v->tile = TileVirtXY(x, y);
00162
00163 DisasterVehicleUpdateImage(v);
00164 VehicleMove(v, true);
00165
00166 DisasterVehicle *u = v->Next();
00167 if (u != NULL) {
00168 int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
00169 int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
00170
00171 u->x_pos = x;
00172 u->y_pos = y - 1 - (max(z - GetSlopePixelZ(safe_x, safe_y), 0) >> 3);
00173 safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
00174 u->z_pos = GetSlopePixelZ(safe_x, safe_y);
00175 u->direction = v->direction;
00176
00177 DisasterVehicleUpdateImage(u);
00178 VehicleMove(u, true);
00179
00180 if ((u = u->Next()) != NULL) {
00181 u->x_pos = x;
00182 u->y_pos = y;
00183 u->z_pos = z + 5;
00184 VehicleMove(u, true);
00185 }
00186 }
00187 }
00188
00197 static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
00198 {
00199 v->tick_counter++;
00200
00201 if (v->current_order.GetDestination() < 2) {
00202 if (HasBit(v->tick_counter, 0)) return true;
00203
00204 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00205
00206 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00207
00208 if (v->current_order.GetDestination() == 1) {
00209 if (++v->age == 38) {
00210 v->current_order.SetDestination(2);
00211 v->age = 0;
00212 }
00213
00214 if (GB(v->tick_counter, 0, 3) == 0) CreateEffectVehicleRel(v, 0, -17, 2, EV_CRASH_SMOKE);
00215
00216 } else if (v->current_order.GetDestination() == 0) {
00217 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
00218 v->current_order.SetDestination(1);
00219 v->age = 0;
00220
00221 SetDParam(0, GetStationIndex(v->tile));
00222 AddVehicleNewsItem(STR_NEWS_DISASTER_ZEPPELIN,
00223 NS_ACCIDENT,
00224 v->index);
00225 AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCrashed(GetStationIndex(v->tile)));
00226 }
00227 }
00228
00229 if (v->y_pos >= (int)((MapSizeY() + 9) * TILE_SIZE - 1)) {
00230 delete v;
00231 return false;
00232 }
00233
00234 return true;
00235 }
00236
00237 if (v->current_order.GetDestination() > 2) {
00238 if (++v->age <= 13320) return true;
00239
00240 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
00241 Station *st = Station::GetByTile(v->tile);
00242 CLRBITS(st->airport.flags, RUNWAY_IN_block);
00243 AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCleared(st->index));
00244 }
00245
00246 SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos);
00247 delete v;
00248 return false;
00249 }
00250
00251 int x = v->x_pos;
00252 int y = v->y_pos;
00253 int z = GetSlopePixelZ(x, y);
00254 if (z < v->z_pos) z = v->z_pos - 1;
00255 SetDisasterVehiclePos(v, x, y, z);
00256
00257 if (++v->age == 1) {
00258 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00259 SndPlayVehicleFx(SND_12_EXPLOSION, v);
00260 v->image_override = SPR_BLIMP_CRASHING;
00261 } else if (v->age == 70) {
00262 v->image_override = SPR_BLIMP_CRASHED;
00263 } else if (v->age <= 300) {
00264 if (GB(v->tick_counter, 0, 3) == 0) {
00265 uint32 r = Random();
00266
00267 CreateEffectVehicleRel(v,
00268 GB(r, 0, 4) - 7,
00269 GB(r, 4, 4) - 7,
00270 GB(r, 8, 3) + 5,
00271 EV_EXPLOSION_SMALL);
00272 }
00273 } else if (v->age == 350) {
00274 v->current_order.SetDestination(3);
00275 v->age = 0;
00276 }
00277
00278 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
00279 SETBITS(Station::GetByTile(v->tile)->airport.flags, RUNWAY_IN_block);
00280 }
00281
00282 return true;
00283 }
00284
00291 static bool DisasterTick_Ufo(DisasterVehicle *v)
00292 {
00293 v->image_override = (HasBit(++v->tick_counter, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER : SPR_UFO_SMALL_SCOUT;
00294
00295 if (v->current_order.GetDestination() == 0) {
00296
00297 int x = TileX(v->dest_tile) * TILE_SIZE;
00298 int y = TileY(v->dest_tile) * TILE_SIZE;
00299 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
00300 v->direction = GetDirectionTowards(v, x, y);
00301 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00302 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00303 return true;
00304 }
00305 if (++v->age < 6) {
00306 v->dest_tile = RandomTile();
00307 return true;
00308 }
00309 v->current_order.SetDestination(1);
00310
00311 uint n = 0;
00312 RoadVehicle *u;
00313 FOR_ALL_ROADVEHICLES(u) {
00314 if (u->IsFrontEngine()) n++;
00315 }
00316
00317 if (n == 0) {
00318
00319 delete v;
00320 return false;
00321 }
00322
00323 n = RandomRange(n);
00324 FOR_ALL_ROADVEHICLES(u) {
00325
00326 if (u->IsFrontEngine() && (n-- == 0)) break;
00327 }
00328
00329
00330 v->dest_tile = u->index;
00331 v->age = 0;
00332 return true;
00333 } else {
00334
00335 RoadVehicle *u = RoadVehicle::Get(v->dest_tile);
00336 assert(u != NULL && u->type == VEH_ROAD && u->IsFrontEngine());
00337
00338 uint dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
00339
00340 if (dist < TILE_SIZE && !(u->vehstatus & VS_HIDDEN) && u->breakdown_ctr == 0) {
00341 u->breakdown_ctr = 3;
00342 u->breakdown_delay = 140;
00343 }
00344
00345 v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
00346 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00347
00348 int z = v->z_pos;
00349 if (dist <= TILE_SIZE && z > u->z_pos) z--;
00350 SetDisasterVehiclePos(v, gp.x, gp.y, z);
00351
00352 if (z <= u->z_pos && (u->vehstatus & VS_HIDDEN) == 0) {
00353 v->age++;
00354 if (u->crashed_ctr == 0) {
00355 u->Crash();
00356
00357 AddVehicleNewsItem(STR_NEWS_DISASTER_SMALL_UFO,
00358 NS_ACCIDENT,
00359 u->index);
00360
00361 AI::NewEvent(u->owner, new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
00362 Game::NewEvent(new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
00363 }
00364 }
00365
00366
00367 if (v->age > 50) {
00368 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00369 SndPlayVehicleFx(SND_12_EXPLOSION, v);
00370 delete v;
00371 return false;
00372 }
00373 }
00374
00375 return true;
00376 }
00377
00378 static void DestructIndustry(Industry *i)
00379 {
00380 for (TileIndex tile = 0; tile != MapSize(); tile++) {
00381 if (i->TileBelongsToIndustry(tile)) {
00382 ResetIndustryConstructionStage(tile);
00383 MarkTileDirtyByTile(tile);
00384 }
00385 }
00386 }
00387
00401 static bool DisasterTick_Aircraft(DisasterVehicle *v, uint16 image_override, bool leave_at_top, StringID news_message, IndustryBehaviour industry_flag)
00402 {
00403 v->tick_counter++;
00404 v->image_override = (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? image_override : 0;
00405
00406 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00407 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00408
00409 if ((leave_at_top && gp.x < (-10 * (int)TILE_SIZE)) || (!leave_at_top && gp.x > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1)) {
00410 delete v;
00411 return false;
00412 }
00413
00414 if (v->current_order.GetDestination() == 2) {
00415 if (GB(v->tick_counter, 0, 2) == 0) {
00416 Industry *i = Industry::Get(v->dest_tile);
00417 int x = TileX(i->location.tile) * TILE_SIZE;
00418 int y = TileY(i->location.tile) * TILE_SIZE;
00419 uint32 r = Random();
00420
00421 CreateEffectVehicleAbove(
00422 GB(r, 0, 6) + x,
00423 GB(r, 6, 6) + y,
00424 GB(r, 12, 4),
00425 EV_EXPLOSION_SMALL);
00426
00427 if (++v->age >= 55) v->current_order.SetDestination(3);
00428 }
00429 } else if (v->current_order.GetDestination() == 1) {
00430 if (++v->age == 112) {
00431 v->current_order.SetDestination(2);
00432 v->age = 0;
00433
00434 Industry *i = Industry::Get(v->dest_tile);
00435 DestructIndustry(i);
00436
00437 SetDParam(0, i->town->index);
00438 AddIndustryNewsItem(news_message, NS_ACCIDENT, i->index);
00439 SndPlayTileFx(SND_12_EXPLOSION, i->location.tile);
00440 }
00441 } else if (v->current_order.GetDestination() == 0) {
00442 int x = v->x_pos + ((leave_at_top ? -15 : 15) * TILE_SIZE);
00443 int y = v->y_pos;
00444
00445 if ((uint)x > MapMaxX() * TILE_SIZE - 1) return true;
00446
00447 TileIndex tile = TileVirtXY(x, y);
00448 if (!IsTileType(tile, MP_INDUSTRY)) return true;
00449
00450 IndustryID ind = GetIndustryIndex(tile);
00451 v->dest_tile = ind;
00452
00453 if (GetIndustrySpec(Industry::Get(ind)->type)->behaviour & industry_flag) {
00454 v->current_order.SetDestination(1);
00455 v->age = 0;
00456 }
00457 }
00458
00459 return true;
00460 }
00461
00463 static bool DisasterTick_Airplane(DisasterVehicle *v)
00464 {
00465 return DisasterTick_Aircraft(v, SPR_F_15_FIRING, true, STR_NEWS_DISASTER_AIRPLANE_OIL_REFINERY, INDUSTRYBEH_AIRPLANE_ATTACKS);
00466 }
00467
00469 static bool DisasterTick_Helicopter(DisasterVehicle *v)
00470 {
00471 return DisasterTick_Aircraft(v, SPR_AH_64A_FIRING, false, STR_NEWS_DISASTER_HELICOPTER_FACTORY, INDUSTRYBEH_CHOPPER_ATTACKS);
00472 }
00473
00475 static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
00476 {
00477 v->tick_counter++;
00478 if (HasBit(v->tick_counter, 0)) return true;
00479
00480 if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
00481
00482 VehicleMove(v, true);
00483
00484 return true;
00485 }
00486
00493 static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
00494 {
00495 v->tick_counter++;
00496
00497 if (v->current_order.GetDestination() == 1) {
00498 int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00499 int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00500 if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
00501 v->direction = GetDirectionTowards(v, x, y);
00502
00503 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00504 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00505 return true;
00506 }
00507
00508 if (!IsValidTile(v->dest_tile)) {
00509
00510 delete v;
00511 return false;
00512 }
00513
00514 int z = GetSlopePixelZ(v->x_pos, v->y_pos);
00515 if (z < v->z_pos) {
00516 SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos - 1);
00517 return true;
00518 }
00519
00520 v->current_order.SetDestination(2);
00521
00522 Vehicle *target;
00523 FOR_ALL_VEHICLES(target) {
00524 if (target->IsGroundVehicle()) {
00525 if (Delta(target->x_pos, v->x_pos) + Delta(target->y_pos, v->y_pos) <= 12 * (int)TILE_SIZE) {
00526 target->breakdown_ctr = 5;
00527 target->breakdown_delay = 0xF0;
00528 }
00529 }
00530 }
00531
00532 Town *t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
00533 SetDParam(0, t->index);
00534 AddNewsItem(STR_NEWS_DISASTER_BIG_UFO,
00535 NS_ACCIDENT,
00536 NR_TILE,
00537 v->tile);
00538
00539 if (!Vehicle::CanAllocateItem(2)) {
00540 delete v;
00541 return false;
00542 }
00543 DisasterVehicle *u = new DisasterVehicle();
00544
00545 InitializeDisasterVehicle(u, -6 * (int)TILE_SIZE, v->y_pos, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SW, ST_BIG_UFO_DESTROYER);
00546 u->big_ufo_destroyer_target = v->index;
00547
00548 DisasterVehicle *w = new DisasterVehicle();
00549
00550 u->SetNext(w);
00551 InitializeDisasterVehicle(w, -6 * (int)TILE_SIZE, v->y_pos, 0, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
00552 w->vehstatus |= VS_SHADOW;
00553 } else if (v->current_order.GetDestination() == 0) {
00554 int x = TileX(v->dest_tile) * TILE_SIZE;
00555 int y = TileY(v->dest_tile) * TILE_SIZE;
00556 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
00557 v->direction = GetDirectionTowards(v, x, y);
00558 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00559 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00560 return true;
00561 }
00562
00563 if (++v->age < 6) {
00564 v->dest_tile = RandomTile();
00565 return true;
00566 }
00567 v->current_order.SetDestination(1);
00568
00569 TileIndex tile_org = RandomTile();
00570 TileIndex tile = tile_org;
00571 do {
00572 if (IsPlainRailTile(tile) &&
00573 Company::IsHumanID(GetTileOwner(tile))) {
00574 break;
00575 }
00576 tile = TILE_MASK(tile + 1);
00577 } while (tile != tile_org);
00578 v->dest_tile = tile;
00579 v->age = 0;
00580 }
00581
00582 return true;
00583 }
00584
00589 static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle *v)
00590 {
00591 v->tick_counter++;
00592
00593 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00594 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00595
00596 if (gp.x > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1) {
00597 delete v;
00598 return false;
00599 }
00600
00601 if (v->current_order.GetDestination() == 0) {
00602 Vehicle *u = Vehicle::Get(v->big_ufo_destroyer_target);
00603 if (Delta(v->x_pos, u->x_pos) > (int)TILE_SIZE) return true;
00604 v->current_order.SetDestination(1);
00605
00606 CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
00607 SndPlayVehicleFx(SND_12_EXPLOSION, u);
00608
00609 delete u;
00610
00611 for (int i = 0; i != 80; i++) {
00612 uint32 r = Random();
00613 CreateEffectVehicleAbove(
00614 GB(r, 0, 6) + v->x_pos - 32,
00615 GB(r, 5, 6) + v->y_pos - 32,
00616 0,
00617 EV_EXPLOSION_SMALL);
00618 }
00619
00620 for (int dy = -3; dy < 3; dy++) {
00621 for (int dx = -3; dx < 3; dx++) {
00622 TileIndex tile = TileAddWrap(v->tile, dx, dy);
00623 if (tile != INVALID_TILE) DisasterClearSquare(tile);
00624 }
00625 }
00626 }
00627
00628 return true;
00629 }
00630
00635 static bool DisasterTick_Submarine(DisasterVehicle *v)
00636 {
00637 v->tick_counter++;
00638
00639 if (++v->age > 8880) {
00640 delete v;
00641 return false;
00642 }
00643
00644 if (!HasBit(v->tick_counter, 0)) return true;
00645
00646 TileIndex tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
00647 if (IsValidTile(tile)) {
00648 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00649 if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
00650 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00651 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00652 return true;
00653 }
00654 }
00655
00656 v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
00657
00658 return true;
00659 }
00660
00661
00662 static bool DisasterTick_NULL(DisasterVehicle *v)
00663 {
00664 return true;
00665 }
00666
00667 typedef bool DisasterVehicleTickProc(DisasterVehicle *v);
00668
00669 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
00670 DisasterTick_Zeppeliner, DisasterTick_NULL,
00671 DisasterTick_Ufo, DisasterTick_NULL,
00672 DisasterTick_Airplane, DisasterTick_NULL,
00673 DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
00674 DisasterTick_Big_Ufo, DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
00675 DisasterTick_NULL,
00676 DisasterTick_Submarine,
00677 DisasterTick_Submarine,
00678 };
00679
00680
00681 bool DisasterVehicle::Tick()
00682 {
00683 return _disastervehicle_tick_procs[this->subtype](this);
00684 }
00685
00686 typedef void DisasterInitProc();
00687
00688
00693 static void Disaster_Zeppeliner_Init()
00694 {
00695 if (!Vehicle::CanAllocateItem(2)) return;
00696
00697
00698 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00699
00700 Station *st;
00701 FOR_ALL_STATIONS(st) {
00702 if (st->airport.tile != INVALID_TILE && (st->airport.type == AT_SMALL || st->airport.type == AT_LARGE)) {
00703 x = (TileX(st->airport.tile) + 2) * TILE_SIZE;
00704 break;
00705 }
00706 }
00707
00708 DisasterVehicle *v = new DisasterVehicle();
00709 InitializeDisasterVehicle(v, x, 0, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SE, ST_ZEPPELINER);
00710
00711
00712 DisasterVehicle *u = new DisasterVehicle();
00713 v->SetNext(u);
00714 InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
00715 u->vehstatus |= VS_SHADOW;
00716 }
00717
00718
00723 static void Disaster_Small_Ufo_Init()
00724 {
00725 if (!Vehicle::CanAllocateItem(2)) return;
00726
00727 DisasterVehicle *v = new DisasterVehicle();
00728 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00729
00730 InitializeDisasterVehicle(v, x, 0, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SE, ST_SMALL_UFO);
00731 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00732 v->age = 0;
00733
00734
00735 DisasterVehicle *u = new DisasterVehicle();
00736 v->SetNext(u);
00737 InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
00738 u->vehstatus |= VS_SHADOW;
00739 }
00740
00741
00742
00743 static void Disaster_Airplane_Init()
00744 {
00745 if (!Vehicle::CanAllocateItem(2)) return;
00746
00747 Industry *i, *found = NULL;
00748
00749 FOR_ALL_INDUSTRIES(i) {
00750 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
00751 (found == NULL || Chance16(1, 2))) {
00752 found = i;
00753 }
00754 }
00755
00756 if (found == NULL) return;
00757
00758 DisasterVehicle *v = new DisasterVehicle();
00759
00760
00761 int x = (MapSizeX() + 9) * TILE_SIZE - 1;
00762 int y = TileY(found->location.tile) * TILE_SIZE + 37;
00763
00764 InitializeDisasterVehicle(v, x, y, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_NE, ST_AIRPLANE);
00765
00766 DisasterVehicle *u = new DisasterVehicle();
00767 v->SetNext(u);
00768 InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_AIRPLANE_SHADOW);
00769 u->vehstatus |= VS_SHADOW;
00770 }
00771
00772
00774 static void Disaster_Helicopter_Init()
00775 {
00776 if (!Vehicle::CanAllocateItem(3)) return;
00777
00778 Industry *i, *found = NULL;
00779
00780 FOR_ALL_INDUSTRIES(i) {
00781 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
00782 (found == NULL || Chance16(1, 2))) {
00783 found = i;
00784 }
00785 }
00786
00787 if (found == NULL) return;
00788
00789 DisasterVehicle *v = new DisasterVehicle();
00790
00791 int x = -16 * (int)TILE_SIZE;
00792 int y = TileY(found->location.tile) * TILE_SIZE + 37;
00793
00794 InitializeDisasterVehicle(v, x, y, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_SW, ST_HELICOPTER);
00795
00796 DisasterVehicle *u = new DisasterVehicle();
00797 v->SetNext(u);
00798 InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_HELICOPTER_SHADOW);
00799 u->vehstatus |= VS_SHADOW;
00800
00801 DisasterVehicle *w = new DisasterVehicle();
00802 u->SetNext(w);
00803 InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_HELICOPTER_ROTORS);
00804 }
00805
00806
00807
00808
00809 static void Disaster_Big_Ufo_Init()
00810 {
00811 if (!Vehicle::CanAllocateItem(2)) return;
00812
00813 DisasterVehicle *v = new DisasterVehicle();
00814 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00815 int y = MapMaxX() * TILE_SIZE - 1;
00816
00817 InitializeDisasterVehicle(v, x, y, INITIAL_DISASTER_VEHICLE_ZPOS, DIR_NW, ST_BIG_UFO);
00818 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00819 v->age = 0;
00820
00821
00822 DisasterVehicle *u = new DisasterVehicle();
00823 v->SetNext(u);
00824 InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_BIG_UFO_SHADOW);
00825 u->vehstatus |= VS_SHADOW;
00826 }
00827
00828
00829 static void Disaster_Submarine_Init(DisasterSubType subtype)
00830 {
00831 if (!Vehicle::CanAllocateItem()) return;
00832
00833 int y;
00834 Direction dir;
00835 uint32 r = Random();
00836 int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
00837
00838 if (HasBit(r, 31)) {
00839 y = MapMaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
00840 dir = DIR_NW;
00841 } else {
00842 y = TILE_SIZE / 2;
00843 if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
00844 dir = DIR_SE;
00845 }
00846 if (!IsWaterTile(TileVirtXY(x, y))) return;
00847
00848 DisasterVehicle *v = new DisasterVehicle();
00849 InitializeDisasterVehicle(v, x, y, 0, dir, subtype);
00850 v->age = 0;
00851 }
00852
00853
00854 static void Disaster_Small_Submarine_Init()
00855 {
00856 Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
00857 }
00858
00859
00860
00861 static void Disaster_Big_Submarine_Init()
00862 {
00863 Disaster_Submarine_Init(ST_BIG_SUBMARINE);
00864 }
00865
00866
00871 static void Disaster_CoalMine_Init()
00872 {
00873 int index = GB(Random(), 0, 4);
00874 uint m;
00875
00876 for (m = 0; m < 15; m++) {
00877 const Industry *i;
00878
00879 FOR_ALL_INDUSTRIES(i) {
00880 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
00881 SetDParam(0, i->town->index);
00882 AddNewsItem(STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE,
00883 NS_ACCIDENT, NR_TILE, i->location.tile + TileDiffXY(1, 1));
00884
00885 {
00886 TileIndex tile = i->location.tile;
00887 TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
00888
00889 for (uint n = 0; n < 30; n++) {
00890 DisasterClearSquare(tile);
00891 tile += step;
00892 if (!IsValidTile(tile)) break;
00893 }
00894 }
00895 return;
00896 }
00897 }
00898 }
00899 }
00900
00901 struct Disaster {
00902 DisasterInitProc *init_proc;
00903 Year min_year;
00904 Year max_year;
00905 };
00906
00907 static const Disaster _disasters[] = {
00908 {Disaster_Zeppeliner_Init, 1930, 1955},
00909 {Disaster_Small_Ufo_Init, 1940, 1970},
00910 {Disaster_Airplane_Init, 1960, 1990},
00911 {Disaster_Helicopter_Init, 1970, 2000},
00912 {Disaster_Big_Ufo_Init, 2000, 2100},
00913 {Disaster_Small_Submarine_Init, 1940, 1965},
00914 {Disaster_Big_Submarine_Init, 1975, 2010},
00915 {Disaster_CoalMine_Init, 1950, 1985},
00916 };
00917
00918 static void DoDisaster()
00919 {
00920 byte buf[lengthof(_disasters)];
00921
00922 byte j = 0;
00923 for (size_t i = 0; i != lengthof(_disasters); i++) {
00924 if (_cur_year >= _disasters[i].min_year && _cur_year < _disasters[i].max_year) buf[j++] = (byte)i;
00925 }
00926
00927 if (j == 0) return;
00928
00929 _disasters[buf[RandomRange(j)]].init_proc();
00930 }
00931
00932
00933 static void ResetDisasterDelay()
00934 {
00935 _disaster_delay = GB(Random(), 0, 9) + 730;
00936 }
00937
00938 void DisasterDailyLoop()
00939 {
00940 if (--_disaster_delay != 0) return;
00941
00942 ResetDisasterDelay();
00943
00944 if (_settings_game.difficulty.disasters != 0) DoDisaster();
00945 }
00946
00947 void StartupDisasters()
00948 {
00949 ResetDisasterDelay();
00950 }
00951
00957 void ReleaseDisastersTargetingIndustry(IndustryID i)
00958 {
00959 DisasterVehicle *v;
00960 FOR_ALL_DISASTERVEHICLES(v) {
00961
00962 if (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER) {
00963
00964 if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
00965 }
00966 }
00967 }
00968
00973 void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
00974 {
00975 DisasterVehicle *v;
00976 FOR_ALL_DISASTERVEHICLES(v) {
00977
00978 if (v->subtype == ST_SMALL_UFO) {
00979 if (v->current_order.GetDestination() != 0 && v->dest_tile == vehicle) {
00980
00981 v->current_order.SetDestination(0);
00982 v->dest_tile = RandomTile();
00983 v->z_pos = INITIAL_DISASTER_VEHICLE_ZPOS;
00984 v->age = 0;
00985 }
00986 }
00987 }
00988 }
00989
00990 void DisasterVehicle::UpdateDeltaXY(Direction direction)
00991 {
00992 this->x_offs = -1;
00993 this->y_offs = -1;
00994 this->x_extent = 2;
00995 this->y_extent = 2;
00996 this->z_extent = 5;
00997 }