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 assert(u->type == VEH_ROAD && IsRoadVehFront(u));
00327
00328 dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
00329
00330 if (dist < TILE_SIZE && !(u->vehstatus & VS_HIDDEN) && u->breakdown_ctr == 0) {
00331 u->breakdown_ctr = 3;
00332 u->breakdown_delay = 140;
00333 }
00334
00335 v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
00336 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00337
00338 z = v->z_pos;
00339 if (dist <= TILE_SIZE && z > u->z_pos) z--;
00340 SetDisasterVehiclePos(v, gp.x, gp.y, z);
00341
00342 if (z <= u->z_pos && (u->vehstatus & VS_HIDDEN) == 0) {
00343 v->age++;
00344 if (u->u.road.crashed_ctr == 0) {
00345 u->u.road.crashed_ctr++;
00346
00347 AddNewsItem(STR_B001_ROAD_VEHICLE_DESTROYED,
00348 NS_ACCIDENT_VEHICLE,
00349 u->index,
00350 0);
00351
00352 AI::NewEvent(u->owner, new AIEventVehicleCrashed(u->index, u->tile, AIEventVehicleCrashed::CRASH_RV_UFO));
00353
00354 for (Vehicle *w = u; w != NULL; w = w->Next()) {
00355 w->vehstatus |= VS_CRASHED;
00356 MarkSingleVehicleDirty(w);
00357 }
00358 }
00359 }
00360
00361
00362 if (v->age > 50) {
00363 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00364 SndPlayVehicleFx(SND_12_EXPLOSION, v);
00365 delete v;
00366 }
00367 }
00368 }
00369
00370 static void DestructIndustry(Industry *i)
00371 {
00372 TileIndex tile;
00373
00374 for (tile = 0; tile != MapSize(); tile++) {
00375 if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == i->index) {
00376 ResetIndustryConstructionStage(tile);
00377 MarkTileDirtyByTile(tile);
00378 }
00379 }
00380 }
00381
00390 static void DisasterTick_Airplane(Vehicle *v)
00391 {
00392 v->tick_counter++;
00393 v->u.disaster.image_override =
00394 (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_F_15_FIRING : 0;
00395
00396 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00397 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00398
00399 if (gp.x < (-10 * TILE_SIZE)) {
00400 delete v;
00401 return;
00402 }
00403
00404 if (v->current_order.GetDestination() == 2) {
00405 if (GB(v->tick_counter, 0, 2) == 0) {
00406 Industry *i = GetIndustry(v->dest_tile);
00407 int x = TileX(i->xy) * TILE_SIZE;
00408 int y = TileY(i->xy) * TILE_SIZE;
00409 uint32 r = Random();
00410
00411 CreateEffectVehicleAbove(
00412 GB(r, 0, 6) + x,
00413 GB(r, 6, 6) + y,
00414 GB(r, 12, 4),
00415 EV_EXPLOSION_SMALL);
00416
00417 if (++v->age >= 55) v->current_order.SetDestination(3);
00418 }
00419 } else if (v->current_order.GetDestination() == 1) {
00420 if (++v->age == 112) {
00421 Industry *i;
00422
00423 v->current_order.SetDestination(2);
00424 v->age = 0;
00425
00426 i = GetIndustry(v->dest_tile);
00427 DestructIndustry(i);
00428
00429 SetDParam(0, i->town->index);
00430 AddNewsItem(STR_B002_OIL_REFINERY_EXPLOSION, NS_ACCIDENT_TILE, i->xy, 0);
00431 SndPlayTileFx(SND_12_EXPLOSION, i->xy);
00432 }
00433 } else if (v->current_order.GetDestination() == 0) {
00434 int x, y;
00435 TileIndex tile;
00436 uint ind;
00437
00438 x = v->x_pos - (15 * TILE_SIZE);
00439 y = v->y_pos;
00440
00441 if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
00442
00443 tile = TileVirtXY(x, y);
00444 if (!IsTileType(tile, MP_INDUSTRY)) return;
00445
00446 ind = GetIndustryIndex(tile);
00447 v->dest_tile = ind;
00448
00449 if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) {
00450 v->current_order.SetDestination(1);
00451 v->age = 0;
00452 }
00453 }
00454 }
00455
00463 static void DisasterTick_Helicopter(Vehicle *v)
00464 {
00465 v->tick_counter++;
00466 v->u.disaster.image_override =
00467 (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? SPR_AH_64A_FIRING : 0;
00468
00469 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00470 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00471
00472 if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
00473 delete v;
00474 return;
00475 }
00476
00477 if (v->current_order.GetDestination() == 2) {
00478 if (GB(v->tick_counter, 0, 2) == 0) {
00479 Industry *i = GetIndustry(v->dest_tile);
00480 int x = TileX(i->xy) * TILE_SIZE;
00481 int y = TileY(i->xy) * TILE_SIZE;
00482 uint32 r = Random();
00483
00484 CreateEffectVehicleAbove(
00485 GB(r, 0, 6) + x,
00486 GB(r, 6, 6) + y,
00487 GB(r, 12, 4),
00488 EV_EXPLOSION_SMALL);
00489
00490 if (++v->age >= 55) v->current_order.SetDestination(3);
00491 }
00492 } else if (v->current_order.GetDestination() == 1) {
00493 if (++v->age == 112) {
00494 Industry *i;
00495
00496 v->current_order.SetDestination(2);
00497 v->age = 0;
00498
00499 i = GetIndustry(v->dest_tile);
00500 DestructIndustry(i);
00501
00502 SetDParam(0, i->town->index);
00503 AddNewsItem(STR_B003_FACTORY_DESTROYED_IN_SUSPICIOUS, NS_ACCIDENT_TILE, i->xy, 0);
00504 SndPlayTileFx(SND_12_EXPLOSION, i->xy);
00505 }
00506 } else if (v->current_order.GetDestination() == 0) {
00507 int x, y;
00508 TileIndex tile;
00509 uint ind;
00510
00511 x = v->x_pos + (15 * TILE_SIZE);
00512 y = v->y_pos;
00513
00514 if ( (uint)x > MapMaxX() * TILE_SIZE - 1) return;
00515
00516 tile = TileVirtXY(x, y);
00517 if (!IsTileType(tile, MP_INDUSTRY)) return;
00518
00519 ind = GetIndustryIndex(tile);
00520 v->dest_tile = ind;
00521
00522 if (GetIndustrySpec(GetIndustry(ind)->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) {
00523 v->current_order.SetDestination(1);
00524 v->age = 0;
00525 }
00526 }
00527 }
00528
00530 static void DisasterTick_Helicopter_Rotors(Vehicle *v)
00531 {
00532 v->tick_counter++;
00533 if (HasBit(v->tick_counter, 0)) return;
00534
00535 if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
00536
00537 VehicleMove(v, true);
00538 }
00539
00546 static void DisasterTick_Big_Ufo(Vehicle *v)
00547 {
00548 byte z;
00549 Town *t;
00550 TileIndex tile;
00551 TileIndex tile_org;
00552
00553 v->tick_counter++;
00554
00555 if (v->current_order.GetDestination() == 1) {
00556 int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00557 int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00558 if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
00559 v->direction = GetDirectionTowards(v, x, y);
00560
00561 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00562 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00563 return;
00564 }
00565
00566 if (!IsValidTile(v->dest_tile)) {
00567
00568 delete v;
00569 return;
00570 }
00571
00572 z = GetSlopeZ(v->x_pos, v->y_pos);
00573 if (z < v->z_pos) {
00574 SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos - 1);
00575 return;
00576 }
00577
00578 v->current_order.SetDestination(2);
00579
00580 Vehicle *u;
00581 FOR_ALL_VEHICLES(u) {
00582 if (u->type == VEH_TRAIN || u->type == VEH_ROAD) {
00583 if (Delta(u->x_pos, v->x_pos) + Delta(u->y_pos, v->y_pos) <= 12 * TILE_SIZE) {
00584 u->breakdown_ctr = 5;
00585 u->breakdown_delay = 0xF0;
00586 }
00587 }
00588 }
00589
00590 t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
00591 SetDParam(0, t->index);
00592 AddNewsItem(STR_B004_UFO_LANDS_NEAR,
00593 NS_ACCIDENT_TILE,
00594 v->tile,
00595 0);
00596
00597 if (!Vehicle::CanAllocateItem(2)) {
00598 delete v;
00599 return;
00600 }
00601 u = new DisasterVehicle();
00602
00603 InitializeDisasterVehicle(u, -6 * TILE_SIZE, v->y_pos, 135, DIR_SW, ST_BIG_UFO_DESTROYER);
00604 u->u.disaster.big_ufo_destroyer_target = v->index;
00605
00606 Vehicle *w = new DisasterVehicle();
00607
00608 u->SetNext(w);
00609 InitializeDisasterVehicle(w, -6 * TILE_SIZE, v->y_pos, 0, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
00610 w->vehstatus |= VS_SHADOW;
00611 } else if (v->current_order.GetDestination() == 0) {
00612 int x = TileX(v->dest_tile) * TILE_SIZE;
00613 int y = TileY(v->dest_tile) * TILE_SIZE;
00614 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
00615 v->direction = GetDirectionTowards(v, x, y);
00616 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00617 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00618 return;
00619 }
00620
00621 if (++v->age < 6) {
00622 v->dest_tile = RandomTile();
00623 return;
00624 }
00625 v->current_order.SetDestination(1);
00626
00627 tile_org = tile = RandomTile();
00628 do {
00629 if (IsPlainRailTile(tile) &&
00630 IsHumanCompany(GetTileOwner(tile))) {
00631 break;
00632 }
00633 tile = TILE_MASK(tile + 1);
00634 } while (tile != tile_org);
00635 v->dest_tile = tile;
00636 v->age = 0;
00637 } else {
00638 return;
00639 }
00640 }
00641
00646 static void DisasterTick_Big_Ufo_Destroyer(Vehicle *v)
00647 {
00648 Vehicle *u;
00649 int i;
00650
00651 v->tick_counter++;
00652
00653 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00654 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00655
00656 if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
00657 delete v;
00658 return;
00659 }
00660
00661 if (v->current_order.GetDestination() == 0) {
00662 u = GetVehicle(v->u.disaster.big_ufo_destroyer_target);
00663 if (Delta(v->x_pos, u->x_pos) > TILE_SIZE) return;
00664 v->current_order.SetDestination(1);
00665
00666 CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
00667 SndPlayVehicleFx(SND_12_EXPLOSION, u);
00668
00669 delete u;
00670
00671 for (i = 0; i != 80; i++) {
00672 uint32 r = Random();
00673 CreateEffectVehicleAbove(
00674 GB(r, 0, 6) + v->x_pos - 32,
00675 GB(r, 5, 6) + v->y_pos - 32,
00676 0,
00677 EV_EXPLOSION_SMALL);
00678 }
00679
00680 for (int dy = -3; dy < 3; dy++) {
00681 for (int dx = -3; dx < 3; dx++) {
00682 TileIndex tile = TileAddWrap(v->tile, dx, dy);
00683 if (tile != INVALID_TILE) DisasterClearSquare(tile);
00684 }
00685 }
00686 }
00687 }
00688
00693 static void DisasterTick_Submarine(Vehicle *v)
00694 {
00695 TileIndex tile;
00696
00697 v->tick_counter++;
00698
00699 if (++v->age > 8880) {
00700 delete v;
00701 return;
00702 }
00703
00704 if (!HasBit(v->tick_counter, 0)) return;
00705
00706 tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
00707 if (IsValidTile(tile)) {
00708 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00709 if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
00710 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00711 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00712 return;
00713 }
00714 }
00715
00716 v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
00717 }
00718
00719
00720 static void DisasterTick_NULL(Vehicle *v) {}
00721 typedef void DisasterVehicleTickProc(Vehicle *v);
00722
00723 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
00724 DisasterTick_Zeppeliner, DisasterTick_NULL,
00725 DisasterTick_Ufo, DisasterTick_NULL,
00726 DisasterTick_Airplane, DisasterTick_NULL,
00727 DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
00728 DisasterTick_Big_Ufo, DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
00729 DisasterTick_NULL,
00730 DisasterTick_Submarine,
00731 DisasterTick_Submarine,
00732 };
00733
00734
00735 void DisasterVehicle::Tick()
00736 {
00737 _disastervehicle_tick_procs[this->subtype](this);
00738 }
00739
00740 typedef void DisasterInitProc();
00741
00742
00745 static void Disaster_Zeppeliner_Init()
00746 {
00747 if (!Vehicle::CanAllocateItem(2)) return;
00748
00749 Vehicle *v = new DisasterVehicle();
00750 Station *st;
00751
00752
00753 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00754
00755 FOR_ALL_STATIONS(st) {
00756 if (st->airport_tile != INVALID_TILE && (st->airport_type == AT_SMALL || st->airport_type == AT_LARGE)) {
00757 x = (TileX(st->airport_tile) + 2) * TILE_SIZE;
00758 break;
00759 }
00760 }
00761
00762 InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_ZEPPELINER);
00763
00764
00765 Vehicle *u = new DisasterVehicle();
00766 v->SetNext(u);
00767 InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
00768 u->vehstatus |= VS_SHADOW;
00769 }
00770
00771
00774 static void Disaster_Small_Ufo_Init()
00775 {
00776 if (!Vehicle::CanAllocateItem(2)) return;
00777
00778 Vehicle *v = new DisasterVehicle();
00779 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00780
00781 InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_SMALL_UFO);
00782 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00783 v->age = 0;
00784
00785
00786 Vehicle *u = new DisasterVehicle();
00787 v->SetNext(u);
00788 InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
00789 u->vehstatus |= VS_SHADOW;
00790 }
00791
00792
00793
00794 static void Disaster_Airplane_Init()
00795 {
00796 if (!Vehicle::CanAllocateItem(2)) return;
00797
00798 Industry *i, *found = NULL;
00799
00800 FOR_ALL_INDUSTRIES(i) {
00801 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
00802 (found == NULL || Chance16(1, 2))) {
00803 found = i;
00804 }
00805 }
00806
00807 if (found == NULL) return;
00808
00809 Vehicle *v = new DisasterVehicle();
00810
00811
00812 int x = (MapSizeX() + 9) * TILE_SIZE - 1;
00813 int y = TileY(found->xy) * TILE_SIZE + 37;
00814
00815 InitializeDisasterVehicle(v, x, y, 135, DIR_NE, ST_AIRPLANE);
00816
00817 Vehicle *u = new DisasterVehicle();
00818 v->SetNext(u);
00819 InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_AIRPLANE_SHADOW);
00820 u->vehstatus |= VS_SHADOW;
00821 }
00822
00823
00825 static void Disaster_Helicopter_Init()
00826 {
00827 if (!Vehicle::CanAllocateItem(3)) return;
00828
00829 Industry *i, *found = NULL;
00830
00831 FOR_ALL_INDUSTRIES(i) {
00832 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
00833 (found == NULL || Chance16(1, 2))) {
00834 found = i;
00835 }
00836 }
00837
00838 if (found == NULL) return;
00839
00840 Vehicle *v = new DisasterVehicle();
00841
00842 int x = -16 * TILE_SIZE;
00843 int y = TileY(found->xy) * TILE_SIZE + 37;
00844
00845 InitializeDisasterVehicle(v, x, y, 135, DIR_SW, ST_HELICOPTER);
00846
00847 Vehicle *u = new DisasterVehicle();
00848 v->SetNext(u);
00849 InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_HELICOPTER_SHADOW);
00850 u->vehstatus |= VS_SHADOW;
00851
00852 Vehicle *w = new DisasterVehicle();
00853 u->SetNext(w);
00854 InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_HELICOPTER_ROTORS);
00855 }
00856
00857
00858
00859
00860 static void Disaster_Big_Ufo_Init()
00861 {
00862 if (!Vehicle::CanAllocateItem(2)) return;
00863
00864 Vehicle *v = new DisasterVehicle();
00865 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00866 int y = MapMaxX() * TILE_SIZE - 1;
00867
00868 InitializeDisasterVehicle(v, x, y, 135, DIR_NW, ST_BIG_UFO);
00869 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00870 v->age = 0;
00871
00872
00873 Vehicle *u = new DisasterVehicle();
00874 v->SetNext(u);
00875 InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_BIG_UFO_SHADOW);
00876 u->vehstatus |= VS_SHADOW;
00877 }
00878
00879
00880 static void Disaster_Submarine_Init(DisasterSubType subtype)
00881 {
00882 if (!Vehicle::CanAllocateItem()) return;
00883
00884 int y;
00885 Direction dir;
00886 uint32 r = Random();
00887 int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
00888
00889 if (HasBit(r, 31)) {
00890 y = MapMaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
00891 dir = DIR_NW;
00892 } else {
00893 y = TILE_SIZE / 2;
00894 if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
00895 dir = DIR_SE;
00896 }
00897 if (!IsWaterTile(TileVirtXY(x, y))) return;
00898
00899 Vehicle *v = new DisasterVehicle();
00900 InitializeDisasterVehicle(v, x, y, 0, dir, subtype);
00901 v->age = 0;
00902 }
00903
00904
00905 static void Disaster_Small_Submarine_Init()
00906 {
00907 Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
00908 }
00909
00910
00911
00912 static void Disaster_Big_Submarine_Init()
00913 {
00914 Disaster_Submarine_Init(ST_BIG_SUBMARINE);
00915 }
00916
00917
00920 static void Disaster_CoalMine_Init()
00921 {
00922 int index = GB(Random(), 0, 4);
00923 uint m;
00924
00925 for (m = 0; m < 15; m++) {
00926 const Industry *i;
00927
00928 FOR_ALL_INDUSTRIES(i) {
00929 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
00930 SetDParam(0, i->town->index);
00931 AddNewsItem(STR_B005_COAL_MINE_SUBSIDENCE_LEAVES,
00932 NS_ACCIDENT_TILE, i->xy + TileDiffXY(1, 1), 0);
00933
00934 {
00935 TileIndex tile = i->xy;
00936 TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
00937
00938 for (uint n = 0; n < 30; n++) {
00939 DisasterClearSquare(tile);
00940 tile += step;
00941 if (!IsValidTile(tile)) break;
00942 }
00943 }
00944 return;
00945 }
00946 }
00947 }
00948 }
00949
00950 static DisasterInitProc * const _disaster_initprocs[] = {
00951 Disaster_Zeppeliner_Init,
00952 Disaster_Small_Ufo_Init,
00953 Disaster_Airplane_Init,
00954 Disaster_Helicopter_Init,
00955 Disaster_Big_Ufo_Init,
00956 Disaster_Small_Submarine_Init,
00957 Disaster_Big_Submarine_Init,
00958 Disaster_CoalMine_Init,
00959 };
00960
00961 static const struct {
00962 Year min;
00963 Year max;
00964 } _dis_years[] = {
00965 { 1930, 1955 },
00966 { 1940, 1970 },
00967 { 1960, 1990 },
00968 { 1970, 2000 },
00969 { 2000, 2100 },
00970 { 1940, 1965 },
00971 { 1975, 2010 },
00972 { 1950, 1985 }
00973 };
00974
00975
00976 static void DoDisaster()
00977 {
00978 byte buf[lengthof(_dis_years)];
00979 uint i;
00980 uint j;
00981
00982 j = 0;
00983 for (i = 0; i != lengthof(_dis_years); i++) {
00984 if (_cur_year >= _dis_years[i].min && _cur_year < _dis_years[i].max) buf[j++] = i;
00985 }
00986
00987 if (j == 0) return;
00988
00989 _disaster_initprocs[buf[RandomRange(j)]]();
00990 }
00991
00992
00993 static void ResetDisasterDelay()
00994 {
00995 _disaster_delay = GB(Random(), 0, 9) + 730;
00996 }
00997
00998 void DisasterDailyLoop()
00999 {
01000 if (--_disaster_delay != 0) return;
01001
01002 ResetDisasterDelay();
01003
01004 if (_settings_game.difficulty.disasters != 0) DoDisaster();
01005 }
01006
01007 void StartupDisasters()
01008 {
01009 ResetDisasterDelay();
01010 }
01011
01016 void ReleaseDisastersTargetingIndustry(IndustryID i)
01017 {
01018 Vehicle *v;
01019 FOR_ALL_VEHICLES(v) {
01020
01021 if (v->type == VEH_DISASTER && (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER)) {
01022
01023 if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
01024 }
01025 }
01026 }
01027
01031 void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
01032 {
01033 Vehicle *v;
01034 FOR_ALL_VEHICLES(v) {
01035
01036 if (v->type == VEH_DISASTER && v->subtype == ST_SMALL_UFO) {
01037 if (v->current_order.GetDestination() != 0 && v->dest_tile == vehicle) {
01038
01039 v->current_order.SetDestination(0);
01040 v->dest_tile = RandomTile();
01041 v->z_pos = 135;
01042 v->age = 0;
01043 }
01044 }
01045 }
01046 }
01047
01048 void DisasterVehicle::UpdateDeltaXY(Direction direction)
01049 {
01050 this->x_offs = -1;
01051 this->y_offs = -1;
01052 this->x_extent = 2;
01053 this->y_extent = 2;
01054 this->z_extent = 5;
01055 }