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