00001
00002
00019 #include "stdafx.h"
00020 #include "landscape.h"
00021
00022 #include "industry_map.h"
00023 #include "station_map.h"
00024 #include "command_func.h"
00025 #include "news_func.h"
00026 #include "town.h"
00027 #include "company_func.h"
00028 #include "variables.h"
00029 #include "strings_func.h"
00030 #include "date_func.h"
00031 #include "functions.h"
00032 #include "vehicle_func.h"
00033 #include "sound_func.h"
00034 #include "effectvehicle_func.h"
00035 #include "roadveh.h"
00036 #include "ai/ai.hpp"
00037
00038 #include "table/strings.h"
00039 #include "table/sprites.h"
00040
00041 enum DisasterSubType {
00042 ST_ZEPPELINER,
00043 ST_ZEPPELINER_SHADOW,
00044 ST_SMALL_UFO,
00045 ST_SMALL_UFO_SHADOW,
00046 ST_AIRPLANE,
00047 ST_AIRPLANE_SHADOW,
00048 ST_HELICOPTER,
00049 ST_HELICOPTER_SHADOW,
00050 ST_HELICOPTER_ROTORS,
00051 ST_BIG_UFO,
00052 ST_BIG_UFO_SHADOW,
00053 ST_BIG_UFO_DESTROYER,
00054 ST_BIG_UFO_DESTROYER_SHADOW,
00055 ST_SMALL_SUBMARINE,
00056 ST_BIG_SUBMARINE,
00057 };
00058
00059 static void DisasterClearSquare(TileIndex tile)
00060 {
00061 if (!EnsureNoVehicleOnGround(tile)) return;
00062
00063 switch (GetTileType(tile)) {
00064 case MP_RAILWAY:
00065 if (IsHumanCompany(GetTileOwner(tile)) && !IsRailWaypoint(tile)) {
00066 CompanyID old_company = _current_company;
00067 _current_company = OWNER_WATER;
00068 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00069 _current_company = old_company;
00070
00071
00072 UpdateSignalsInBuffer();
00073 }
00074 break;
00075
00076 case MP_HOUSE: {
00077 CompanyID old_company = _current_company;
00078 _current_company = OWNER_NONE;
00079 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00080 _current_company = old_company;
00081 break;
00082 }
00083
00084 case MP_TREES:
00085 case MP_CLEAR:
00086 DoClearSquare(tile);
00087 break;
00088
00089 default:
00090 break;
00091 }
00092 }
00093
00094 static const SpriteID _disaster_images_1[] = {SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP};
00095 static const SpriteID _disaster_images_2[] = {SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT};
00096 static const SpriteID _disaster_images_3[] = {SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15};
00097 static const SpriteID _disaster_images_4[] = {SPR_SUB_SMALL_NE, SPR_SUB_SMALL_NE, SPR_SUB_SMALL_SE, SPR_SUB_SMALL_SE, SPR_SUB_SMALL_SW, SPR_SUB_SMALL_SW, SPR_SUB_SMALL_NW, SPR_SUB_SMALL_NW};
00098 static const SpriteID _disaster_images_5[] = {SPR_SUB_LARGE_NE, SPR_SUB_LARGE_NE, SPR_SUB_LARGE_SE, SPR_SUB_LARGE_SE, SPR_SUB_LARGE_SW, SPR_SUB_LARGE_SW, SPR_SUB_LARGE_NW, SPR_SUB_LARGE_NW};
00099 static const SpriteID _disaster_images_6[] = {SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER};
00100 static const SpriteID _disaster_images_7[] = {SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER};
00101 static const SpriteID _disaster_images_8[] = {SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A};
00102 static const SpriteID _disaster_images_9[] = {SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1};
00103
00104 static const SpriteID * const _disaster_images[] = {
00105 _disaster_images_1, _disaster_images_1,
00106 _disaster_images_2, _disaster_images_2,
00107 _disaster_images_3, _disaster_images_3,
00108 _disaster_images_8, _disaster_images_8, _disaster_images_9,
00109 _disaster_images_6, _disaster_images_6,
00110 _disaster_images_7, _disaster_images_7,
00111 _disaster_images_4, _disaster_images_5,
00112 };
00113
00114 static void DisasterVehicleUpdateImage(Vehicle *v)
00115 {
00116 SpriteID img = v->u.disaster.image_override;
00117 if (img == 0) img = _disaster_images[v->subtype][v->direction];
00118 v->cur_image = img;
00119 }
00120
00123 static void InitializeDisasterVehicle(Vehicle *v, int x, int y, byte z, Direction direction, byte subtype)
00124 {
00125 v->x_pos = x;
00126 v->y_pos = y;
00127 v->z_pos = z;
00128 v->tile = TileVirtXY(x, y);
00129 v->direction = direction;
00130 v->subtype = subtype;
00131 v->UpdateDeltaXY(INVALID_DIR);
00132 v->owner = OWNER_NONE;
00133 v->vehstatus = VS_UNCLICKABLE;
00134 v->u.disaster.image_override = 0;
00135 v->current_order.Free();
00136
00137 DisasterVehicleUpdateImage(v);
00138 VehicleMove(v, false);
00139 MarkSingleVehicleDirty(v);
00140 }
00141
00142 static void SetDisasterVehiclePos(Vehicle *v, int x, int y, byte z)
00143 {
00144 Vehicle *u;
00145
00146 v->x_pos = x;
00147 v->y_pos = y;
00148 v->z_pos = z;
00149 v->tile = TileVirtXY(x, y);
00150
00151 DisasterVehicleUpdateImage(v);
00152 VehicleMove(v, true);
00153
00154 if ((u = v->Next()) != NULL) {
00155 int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
00156 int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
00157
00158 u->x_pos = x;
00159 u->y_pos = y - 1 - (max(z - GetSlopeZ(safe_x, safe_y), 0U) >> 3);
00160 safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
00161 u->z_pos = GetSlopeZ(safe_x, safe_y);
00162 u->direction = v->direction;
00163
00164 DisasterVehicleUpdateImage(u);
00165 VehicleMove(u, true);
00166
00167 if ((u = u->Next()) != NULL) {
00168 u->x_pos = x;
00169 u->y_pos = y;
00170 u->z_pos = z + 5;
00171 VehicleMove(u, true);
00172 }
00173 }
00174 }
00175
00184 static void DisasterTick_Zeppeliner(Vehicle *v)
00185 {
00186 Station *st;
00187 int x, y;
00188 byte z;
00189 TileIndex tile;
00190
00191 v->tick_counter++;
00192
00193 if (v->current_order.GetDestination() < 2) {
00194 if (HasBit(v->tick_counter, 0)) return;
00195
00196 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00197
00198 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00199
00200 if (v->current_order.GetDestination() == 1) {
00201 if (++v->age == 38) {
00202 v->current_order.SetDestination(2);
00203 v->age = 0;
00204 }
00205
00206 if (GB(v->tick_counter, 0, 3) == 0) CreateEffectVehicleRel(v, 0, -17, 2, EV_SMOKE);
00207
00208 } else if (v->current_order.GetDestination() == 0) {
00209 tile = v->tile;
00210
00211 if (IsValidTile(tile) &&
00212 IsTileType(tile, MP_STATION) &&
00213 IsAirport(tile)) {
00214 v->current_order.SetDestination(1);
00215 v->age = 0;
00216
00217 SetDParam(0, GetStationIndex(tile));
00218 AddNewsItem(STR_B000_ZEPPELIN_DISASTER_AT,
00219 NS_ACCIDENT_VEHICLE,
00220 v->index,
00221 0);
00222 AI::NewEvent(GetTileOwner(tile), new AIEventDisasterZeppelinerCrashed(GetStationIndex(tile)));
00223 }
00224 }
00225
00226 if (v->y_pos >= ((int)MapSizeY() + 9) * TILE_SIZE - 1) delete v;
00227 return;
00228 }
00229
00230 if (v->current_order.GetDestination() > 2) {
00231 if (++v->age <= 13320) return;
00232
00233 tile = v->tile;
00234
00235 if (IsValidTile(tile) &&
00236 IsTileType(tile, MP_STATION) &&
00237 IsAirport(tile)) {
00238 st = GetStationByTile(tile);
00239 CLRBITS(st->airport_flags, RUNWAY_IN_block);
00240 AI::NewEvent(GetTileOwner(tile), new AIEventDisasterZeppelinerCleared(st->index));
00241 }
00242
00243 SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos);
00244 delete v;
00245 return;
00246 }
00247
00248 x = v->x_pos;
00249 y = v->y_pos;
00250 z = GetSlopeZ(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 SndPlayVehicleFx(SND_12_EXPLOSION, v);
00257 v->u.disaster.image_override = SPR_BLIMP_CRASHING;
00258 } else if (v->age == 70) {
00259 v->u.disaster.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 tile = v->tile;
00276 if (IsValidTile(tile) &&
00277 IsTileType(tile, MP_STATION) &&
00278 IsAirport(tile)) {
00279 st = GetStationByTile(tile);
00280 SETBITS(st->airport_flags, RUNWAY_IN_block);
00281 }
00282 }
00283
00290 static void DisasterTick_Ufo(Vehicle *v)
00291 {
00292 Vehicle *u;
00293 uint dist;
00294 byte z;
00295
00296 v->u.disaster.image_override = (HasBit(++v->tick_counter, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER : SPR_UFO_SMALL_SCOUT;
00297
00298 if (v->current_order.GetDestination() == 0) {
00299
00300 int x = TileX(v->dest_tile) * TILE_SIZE;
00301 int y = TileY(v->dest_tile) * TILE_SIZE;
00302 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
00303 v->direction = GetDirectionTowards(v, x, y);
00304 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00305 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00306 return;
00307 }
00308 if (++v->age < 6) {
00309 v->dest_tile = RandomTile();
00310 return;
00311 }
00312 v->current_order.SetDestination(1);
00313
00314 FOR_ALL_VEHICLES(u) {
00315 if (u->type == VEH_ROAD && IsRoadVehFront(u)) {
00316 v->dest_tile = u->index;
00317 v->age = 0;
00318 return;
00319 }
00320 }
00321
00322 delete v;
00323 } else {
00324
00325 u = GetVehicle(v->dest_tile);
00326 if (u->type != VEH_ROAD || !IsRoadVehFront(u)) {
00327 delete v;
00328 return;
00329 }
00330
00331 dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
00332
00333 if (dist < TILE_SIZE && !(u->vehstatus & VS_HIDDEN) && u->breakdown_ctr == 0) {
00334 u->breakdown_ctr = 3;
00335 u->breakdown_delay = 140;
00336 }
00337
00338 v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
00339 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00340
00341 z = v->z_pos;
00342 if (dist <= TILE_SIZE && z > u->z_pos) z--;
00343 SetDisasterVehiclePos(v, gp.x, gp.y, z);
00344
00345 if (z <= u->z_pos && (u->vehstatus & VS_HIDDEN) == 0) {
00346 v->age++;
00347 if (u->u.road.crashed_ctr == 0) {
00348 u->u.road.crashed_ctr++;
00349
00350 AddNewsItem(STR_B001_ROAD_VEHICLE_DESTROYED,
00351 NS_ACCIDENT_VEHICLE,
00352 u->index,
00353 0);
00354
00355 AI::NewEvent(u->owner, new AIEventVehicleCrashed(u->index, u->tile, AIEventVehicleCrashed::CRASH_RV_UFO));
00356
00357 for (Vehicle *w = u; w != NULL; w = w->Next()) {
00358 w->vehstatus |= VS_CRASHED;
00359 MarkSingleVehicleDirty(w);
00360 }
00361 }
00362 }
00363
00364
00365 if (v->age > 50) {
00366 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00367 SndPlayVehicleFx(SND_12_EXPLOSION, v);
00368 delete v;
00369 }
00370 }
00371 }
00372
00373 static void DestructIndustry(Industry *i)
00374 {
00375 TileIndex tile;
00376
00377 for (tile = 0; tile != MapSize(); tile++) {
00378 if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == i->index) {
00379 ResetIndustryConstructionStage(tile);
00380 MarkTileDirtyByTile(tile);
00381 }
00382 }
00383 }
00384
00393 static void DisasterTick_Airplane(Vehicle *v)
00394 {
00395 v->tick_counter++;
00396 v->u.disaster.image_override =
00397 (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_F_15_FIRING : 0;
00398
00399 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00400 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00401
00402 if (gp.x < (-10 * TILE_SIZE)) {
00403 delete v;
00404 return;
00405 }
00406
00407 if (v->current_order.GetDestination() == 2) {
00408 if (GB(v->tick_counter, 0, 2) == 0) {
00409 Industry *i = GetIndustry(v->dest_tile);
00410 int x = TileX(i->xy) * TILE_SIZE;
00411 int y = TileY(i->xy) * TILE_SIZE;
00412 uint32 r = Random();
00413
00414 CreateEffectVehicleAbove(
00415 GB(r, 0, 6) + x,
00416 GB(r, 6, 6) + y,
00417 GB(r, 12, 4),
00418 EV_EXPLOSION_SMALL);
00419
00420 if (++v->age >= 55) v->current_order.SetDestination(3);
00421 }
00422 } else if (v->current_order.GetDestination() == 1) {
00423 if (++v->age == 112) {
00424 Industry *i;
00425
00426 v->current_order.SetDestination(2);
00427 v->age = 0;
00428
00429 i = GetIndustry(v->dest_tile);
00430 DestructIndustry(i);
00431
00432 SetDParam(0, i->town->index);
00433 AddNewsItem(STR_B002_OIL_REFINERY_EXPLOSION, NS_ACCIDENT_TILE, i->xy, 0);
00434 SndPlayTileFx(SND_12_EXPLOSION, i->xy);
00435 }
00436 } else if (v->current_order.GetDestination() == 0) {
00437 int x, y;
00438 TileIndex tile;
00439 uint ind;
00440
00441 x = v->x_pos - (15 * TILE_SIZE);
00442 y = v->y_pos;
00443
00444 if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
00445
00446 tile = TileVirtXY(x, y);
00447 if (!IsTileType(tile, MP_INDUSTRY)) return;
00448
00449 ind = GetIndustryIndex(tile);
00450 v->dest_tile = ind;
00451
00452 if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) {
00453 v->current_order.SetDestination(1);
00454 v->age = 0;
00455 }
00456 }
00457 }
00458
00466 static void DisasterTick_Helicopter(Vehicle *v)
00467 {
00468 v->tick_counter++;
00469 v->u.disaster.image_override =
00470 (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_AH_64A_FIRING : 0;
00471
00472 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00473 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00474
00475 if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
00476 delete v;
00477 return;
00478 }
00479
00480 if (v->current_order.GetDestination() == 2) {
00481 if (GB(v->tick_counter, 0, 2) == 0) {
00482 Industry *i = GetIndustry(v->dest_tile);
00483 int x = TileX(i->xy) * TILE_SIZE;
00484 int y = TileY(i->xy) * TILE_SIZE;
00485 uint32 r = Random();
00486
00487 CreateEffectVehicleAbove(
00488 GB(r, 0, 6) + x,
00489 GB(r, 6, 6) + y,
00490 GB(r, 12, 4),
00491 EV_EXPLOSION_SMALL);
00492
00493 if (++v->age >= 55) v->current_order.SetDestination(3);
00494 }
00495 } else if (v->current_order.GetDestination() == 1) {
00496 if (++v->age == 112) {
00497 Industry *i;
00498
00499 v->current_order.SetDestination(2);
00500 v->age = 0;
00501
00502 i = GetIndustry(v->dest_tile);
00503 DestructIndustry(i);
00504
00505 SetDParam(0, i->town->index);
00506 AddNewsItem(STR_B003_FACTORY_DESTROYED_IN_SUSPICIOUS, NS_ACCIDENT_TILE, i->xy, 0);
00507 SndPlayTileFx(SND_12_EXPLOSION, i->xy);
00508 }
00509 } else if (v->current_order.GetDestination() == 0) {
00510 int x, y;
00511 TileIndex tile;
00512 uint ind;
00513
00514 x = v->x_pos + (15 * TILE_SIZE);
00515 y = v->y_pos;
00516
00517 if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
00518
00519 tile = TileVirtXY(x, y);
00520 if (!IsTileType(tile, MP_INDUSTRY)) return;
00521
00522 ind = GetIndustryIndex(tile);
00523 v->dest_tile = ind;
00524
00525 if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) {
00526 v->current_order.SetDestination(1);
00527 v->age = 0;
00528 }
00529 }
00530 }
00531
00533 static void DisasterTick_Helicopter_Rotors(Vehicle *v)
00534 {
00535 v->tick_counter++;
00536 if (HasBit(v->tick_counter, 0)) return;
00537
00538 if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
00539
00540 VehicleMove(v, true);
00541 }
00542
00549 static void DisasterTick_Big_Ufo(Vehicle *v)
00550 {
00551 byte z;
00552 Town *t;
00553 TileIndex tile;
00554 TileIndex tile_org;
00555
00556 v->tick_counter++;
00557
00558 if (v->current_order.GetDestination() == 1) {
00559 int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00560 int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00561 if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
00562 v->direction = GetDirectionTowards(v, x, y);
00563
00564 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00565 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00566 return;
00567 }
00568
00569 z = GetSlopeZ(v->x_pos, v->y_pos);
00570 if (z < v->z_pos) {
00571 SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos - 1);
00572 return;
00573 }
00574
00575 v->current_order.SetDestination(2);
00576
00577 Vehicle *u;
00578 FOR_ALL_VEHICLES(u) {
00579 if (u->type == VEH_TRAIN || u->type == VEH_ROAD) {
00580 if (Delta(u->x_pos, v->x_pos) + Delta(u->y_pos, v->y_pos) <= 12 * TILE_SIZE) {
00581 u->breakdown_ctr = 5;
00582 u->breakdown_delay = 0xF0;
00583 }
00584 }
00585 }
00586
00587 t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
00588 SetDParam(0, t->index);
00589 AddNewsItem(STR_B004_UFO_LANDS_NEAR,
00590 NS_ACCIDENT_TILE,
00591 v->tile,
00592 0);
00593
00594 if (!Vehicle::CanAllocateItem(2)) {
00595 delete v;
00596 return;
00597 }
00598 u = new DisasterVehicle();
00599
00600 InitializeDisasterVehicle(u, -6 * TILE_SIZE, v->y_pos, 135, DIR_SW, ST_BIG_UFO_DESTROYER);
00601 u->u.disaster.big_ufo_destroyer_target = v->index;
00602
00603 Vehicle *w = new DisasterVehicle();
00604
00605 u->SetNext(w);
00606 InitializeDisasterVehicle(w, -6 * TILE_SIZE, v->y_pos, 0, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
00607 w->vehstatus |= VS_SHADOW;
00608 } else if (v->current_order.GetDestination() == 0) {
00609 int x = TileX(v->dest_tile) * TILE_SIZE;
00610 int y = TileY(v->dest_tile) * TILE_SIZE;
00611 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
00612 v->direction = GetDirectionTowards(v, x, y);
00613 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00614 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00615 return;
00616 }
00617
00618 if (++v->age < 6) {
00619 v->dest_tile = RandomTile();
00620 return;
00621 }
00622 v->current_order.SetDestination(1);
00623
00624 tile_org = tile = RandomTile();
00625 do {
00626 if (IsTileType(tile, MP_RAILWAY) &&
00627 IsPlainRailTile(tile) &&
00628 IsHumanCompany(GetTileOwner(tile))) {
00629 break;
00630 }
00631 tile = TILE_MASK(tile + 1);
00632 } while (tile != tile_org);
00633 v->dest_tile = tile;
00634 v->age = 0;
00635 } else {
00636 return;
00637 }
00638 }
00639
00644 static void DisasterTick_Big_Ufo_Destroyer(Vehicle *v)
00645 {
00646 Vehicle *u;
00647 int i;
00648
00649 v->tick_counter++;
00650
00651 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00652 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00653
00654 if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
00655 delete v;
00656 return;
00657 }
00658
00659 if (v->current_order.GetDestination() == 0) {
00660 u = GetVehicle(v->u.disaster.big_ufo_destroyer_target);
00661 if (Delta(v->x_pos, u->x_pos) > TILE_SIZE) return;
00662 v->current_order.SetDestination(1);
00663
00664 CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
00665 SndPlayVehicleFx(SND_12_EXPLOSION, u);
00666
00667 delete u;
00668
00669 for (i = 0; i != 80; i++) {
00670 uint32 r = Random();
00671 CreateEffectVehicleAbove(
00672 GB(r, 0, 6) + v->x_pos - 32,
00673 GB(r, 5, 6) + v->y_pos - 32,
00674 0,
00675 EV_EXPLOSION_SMALL);
00676 }
00677
00678 for (int dy = -3; dy < 3; dy++) {
00679 for (int dx = -3; dx < 3; dx++) {
00680 TileIndex tile = TileAddWrap(v->tile, dx, dy);
00681 if (tile != INVALID_TILE) DisasterClearSquare(tile);
00682 }
00683 }
00684 }
00685 }
00686
00691 static void DisasterTick_Submarine(Vehicle *v)
00692 {
00693 TileIndex tile;
00694
00695 v->tick_counter++;
00696
00697 if (++v->age > 8880) {
00698 delete v;
00699 return;
00700 }
00701
00702 if (!HasBit(v->tick_counter, 0)) return;
00703
00704 tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
00705 if (IsValidTile(tile)) {
00706 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00707 if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
00708 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00709 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00710 return;
00711 }
00712 }
00713
00714 v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
00715 }
00716
00717
00718 static void DisasterTick_NULL(Vehicle *v) {}
00719 typedef void DisasterVehicleTickProc(Vehicle *v);
00720
00721 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
00722 DisasterTick_Zeppeliner, DisasterTick_NULL,
00723 DisasterTick_Ufo, DisasterTick_NULL,
00724 DisasterTick_Airplane, DisasterTick_NULL,
00725 DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
00726 DisasterTick_Big_Ufo, DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
00727 DisasterTick_NULL,
00728 DisasterTick_Submarine,
00729 DisasterTick_Submarine,
00730 };
00731
00732
00733 void DisasterVehicle::Tick()
00734 {
00735 _disastervehicle_tick_procs[this->subtype](this);
00736 }
00737
00738 typedef void DisasterInitProc();
00739
00740
00743 static void Disaster_Zeppeliner_Init()
00744 {
00745 if (!Vehicle::CanAllocateItem(2)) return;
00746
00747 Vehicle *v = new DisasterVehicle();
00748 Station *st;
00749
00750
00751 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00752
00753 FOR_ALL_STATIONS(st) {
00754 if (st->airport_tile != INVALID_TILE && (st->airport_type == AT_SMALL || st->airport_type == AT_LARGE)) {
00755 x = (TileX(st->airport_tile) + 2) * TILE_SIZE;
00756 break;
00757 }
00758 }
00759
00760 InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_ZEPPELINER);
00761
00762
00763 Vehicle *u = new DisasterVehicle();
00764 v->SetNext(u);
00765 InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
00766 u->vehstatus |= VS_SHADOW;
00767 }
00768
00769
00772 static void Disaster_Small_Ufo_Init()
00773 {
00774 if (!Vehicle::CanAllocateItem(2)) return;
00775
00776 Vehicle *v = new DisasterVehicle();
00777 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00778
00779 InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_SMALL_UFO);
00780 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00781 v->age = 0;
00782
00783
00784 Vehicle *u = new DisasterVehicle();
00785 v->SetNext(u);
00786 InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
00787 u->vehstatus |= VS_SHADOW;
00788 }
00789
00790
00791
00792 static void Disaster_Airplane_Init()
00793 {
00794 if (!Vehicle::CanAllocateItem(2)) return;
00795
00796 Industry *i, *found = NULL;
00797
00798 FOR_ALL_INDUSTRIES(i) {
00799 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
00800 (found == NULL || Chance16(1, 2))) {
00801 found = i;
00802 }
00803 }
00804
00805 if (found == NULL) return;
00806
00807 Vehicle *v = new DisasterVehicle();
00808
00809
00810 int x = (MapSizeX() + 9) * TILE_SIZE - 1;
00811 int y = TileY(found->xy) * TILE_SIZE + 37;
00812
00813 InitializeDisasterVehicle(v, x, y, 135, DIR_NE, ST_AIRPLANE);
00814
00815 Vehicle *u = new DisasterVehicle();
00816 v->SetNext(u);
00817 InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_AIRPLANE_SHADOW);
00818 u->vehstatus |= VS_SHADOW;
00819 }
00820
00821
00823 static void Disaster_Helicopter_Init()
00824 {
00825 if (!Vehicle::CanAllocateItem(3)) return;
00826
00827 Industry *i, *found = NULL;
00828
00829 FOR_ALL_INDUSTRIES(i) {
00830 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
00831 (found == NULL || Chance16(1, 2))) {
00832 found = i;
00833 }
00834 }
00835
00836 if (found == NULL) return;
00837
00838 Vehicle *v = new DisasterVehicle();
00839
00840 int x = -16 * TILE_SIZE;
00841 int y = TileY(found->xy) * TILE_SIZE + 37;
00842
00843 InitializeDisasterVehicle(v, x, y, 135, DIR_SW, ST_HELICOPTER);
00844
00845 Vehicle *u = new DisasterVehicle();
00846 v->SetNext(u);
00847 InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_HELICOPTER_SHADOW);
00848 u->vehstatus |= VS_SHADOW;
00849
00850 Vehicle *w = new DisasterVehicle();
00851 u->SetNext(w);
00852 InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_HELICOPTER_ROTORS);
00853 }
00854
00855
00856
00857
00858 static void Disaster_Big_Ufo_Init()
00859 {
00860 if (!Vehicle::CanAllocateItem(2)) return;
00861
00862 Vehicle *v = new DisasterVehicle();
00863 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00864 int y = MapMaxX() * TILE_SIZE - 1;
00865
00866 InitializeDisasterVehicle(v, x, y, 135, DIR_NW, ST_BIG_UFO);
00867 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00868 v->age = 0;
00869
00870
00871 Vehicle *u = new DisasterVehicle();
00872 v->SetNext(u);
00873 InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_BIG_UFO_SHADOW);
00874 u->vehstatus |= VS_SHADOW;
00875 }
00876
00877
00878 static void Disaster_Submarine_Init(DisasterSubType subtype)
00879 {
00880 if (!Vehicle::CanAllocateItem()) return;
00881
00882 int y;
00883 Direction dir;
00884 uint32 r = Random();
00885 int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
00886
00887 if (HasBit(r, 31)) {
00888 y = MapMaxX() * TILE_SIZE - TILE_SIZE / 2 - 1;
00889 dir = DIR_NW;
00890 } else {
00891 y = TILE_SIZE / 2;
00892 if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
00893 dir = DIR_SE;
00894 }
00895 if (!IsWaterTile(TileVirtXY(x, y))) return;
00896
00897 Vehicle *v = new DisasterVehicle();
00898 InitializeDisasterVehicle(v, x, y, 0, dir, subtype);
00899 v->age = 0;
00900 }
00901
00902
00903 static void Disaster_Small_Submarine_Init()
00904 {
00905 Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
00906 }
00907
00908
00909
00910 static void Disaster_Big_Submarine_Init()
00911 {
00912 Disaster_Submarine_Init(ST_BIG_SUBMARINE);
00913 }
00914
00915
00918 static void Disaster_CoalMine_Init()
00919 {
00920 int index = GB(Random(), 0, 4);
00921 uint m;
00922
00923 for (m = 0; m < 15; m++) {
00924 const Industry *i;
00925
00926 FOR_ALL_INDUSTRIES(i) {
00927 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
00928 SetDParam(0, i->town->index);
00929 AddNewsItem(STR_B005_COAL_MINE_SUBSIDENCE_LEAVES,
00930 NS_ACCIDENT_TILE, i->xy + TileDiffXY(1, 1), 0);
00931
00932 {
00933 TileIndex tile = i->xy;
00934 TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
00935
00936 for (uint n = 0; n < 30; n++) {
00937 DisasterClearSquare(tile);
00938 tile += step;
00939 if (!IsValidTile(tile)) break;
00940 }
00941 }
00942 return;
00943 }
00944 }
00945 }
00946 }
00947
00948 static DisasterInitProc * const _disaster_initprocs[] = {
00949 Disaster_Zeppeliner_Init,
00950 Disaster_Small_Ufo_Init,
00951 Disaster_Airplane_Init,
00952 Disaster_Helicopter_Init,
00953 Disaster_Big_Ufo_Init,
00954 Disaster_Small_Submarine_Init,
00955 Disaster_Big_Submarine_Init,
00956 Disaster_CoalMine_Init,
00957 };
00958
00959 static const struct {
00960 Year min;
00961 Year max;
00962 } _dis_years[] = {
00963 { 1930, 1955 },
00964 { 1940, 1970 },
00965 { 1960, 1990 },
00966 { 1970, 2000 },
00967 { 2000, 2100 },
00968 { 1940, 1965 },
00969 { 1975, 2010 },
00970 { 1950, 1985 }
00971 };
00972
00973
00974 static void DoDisaster()
00975 {
00976 byte buf[lengthof(_dis_years)];
00977 uint i;
00978 uint j;
00979
00980 j = 0;
00981 for (i = 0; i != lengthof(_dis_years); i++) {
00982 if (_cur_year >= _dis_years[i].min && _cur_year < _dis_years[i].max) buf[j++] = i;
00983 }
00984
00985 if (j == 0) return;
00986
00987 _disaster_initprocs[buf[RandomRange(j)]]();
00988 }
00989
00990
00991 static void ResetDisasterDelay()
00992 {
00993 _disaster_delay = GB(Random(), 0, 9) + 730;
00994 }
00995
00996 void DisasterDailyLoop()
00997 {
00998 if (--_disaster_delay != 0) return;
00999
01000 ResetDisasterDelay();
01001
01002 if (_settings_game.difficulty.disasters != 0) DoDisaster();
01003 }
01004
01005 void StartupDisasters()
01006 {
01007 ResetDisasterDelay();
01008 }
01009
01014 void ReleaseDisastersTargetingIndustry(IndustryID i)
01015 {
01016 Vehicle *v;
01017 FOR_ALL_VEHICLES(v) {
01018
01019 if (v->type == VEH_DISASTER && (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER)) {
01020
01021 if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
01022 }
01023 }
01024 }
01025
01026 void DisasterVehicle::UpdateDeltaXY(Direction direction)
01027 {
01028 this->x_offs = -1;
01029 this->y_offs = -1;
01030 this->x_extent = 2;
01031 this->y_extent = 2;
01032 this->z_extent = 5;
01033 }