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