2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
9 * @file disaster_vehicle.cpp
11 * All disaster/easter egg vehicles are handled here.
12 * The general flow of control for the disaster vehicles is as follows:
14 * <li>Initialize the disaster in a disaster specific way (eg start position,
15 * possible target, etc.) Disaster_XXX_Init() function
16 * <li>Add a subtype to a disaster, which is an index into the function array
17 * that handles the vehicle's ticks.
18 * <li>Run the disaster vehicles each tick until their target has been reached,
19 * this happens in the DisasterTick_XXX() functions. In here, a vehicle's
20 * state is kept by v->current_order.dest variable. Each achieved sub-target
21 * will increase this value, and the last one will remove the disaster itself
29 #include "disaster_vehicle.h"
31 #include "station_base.h"
32 #include "command_func.h"
33 #include "news_func.h"
35 #include "company_func.h"
36 #include "strings_func.h"
37 #include "date_func.h"
38 #include "viewport_func.h"
39 #include "vehicle_func.h"
40 #include "sound_func.h"
41 #include "effectvehicle_func.h"
44 #include "game/game.hpp"
45 #include "company_base.h"
46 #include "core/random_func.hpp"
47 #include "core/backup_type.hpp"
49 #include "table/strings.h"
51 #include "safeguards.h"
53 /** Delay counter for considering the next disaster. */
54 uint16 _disaster_delay
;
56 static void DisasterClearSquare(TileIndex tile
)
58 if (EnsureNoVehicleOnGround(tile
).Failed()) return;
60 switch (GetTileType(tile
)) {
62 if (Company::IsHumanID(GetTileOwner(tile
)) && !IsRailDepot(tile
)) {
63 Backup
<CompanyID
> cur_company(_current_company
, OWNER_WATER
, FILE_LINE
);
64 DoCommand(tile
, 0, 0, DC_EXEC
, CMD_LANDSCAPE_CLEAR
);
65 cur_company
.Restore();
67 /* update signals in buffer */
68 UpdateSignalsInBuffer();
73 Backup
<CompanyID
> cur_company(_current_company
, OWNER_NONE
, FILE_LINE
);
74 DoCommand(tile
, 0, 0, DC_EXEC
, CMD_LANDSCAPE_CLEAR
);
75 cur_company
.Restore();
89 static const SpriteID _disaster_images_1
[] = {SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
};
90 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
};
91 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
};
92 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
};
93 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
};
94 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
};
95 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
};
96 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
};
97 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
};
99 static const SpriteID
* const _disaster_images
[] = {
100 _disaster_images_1
, _disaster_images_1
, ///< zeppeliner and zeppeliner shadow
101 _disaster_images_2
, _disaster_images_2
, ///< small ufo and small ufo shadow
102 _disaster_images_3
, _disaster_images_3
, ///< combat aircraft and shadow
103 _disaster_images_8
, _disaster_images_8
, _disaster_images_9
, ///< combat helicopter, shadow and rotor
104 _disaster_images_6
, _disaster_images_6
, ///< big ufo and shadow
105 _disaster_images_7
, _disaster_images_7
, ///< skyranger and shadow
106 _disaster_images_4
, _disaster_images_5
, ///< small and big submarine sprites
109 void DisasterVehicle::UpdateImage()
111 SpriteID img
= this->image_override
;
112 if (img
== 0) img
= _disaster_images
[this->subtype
][this->direction
];
113 this->sprite_seq
.Set(img
);
117 * Construct the disaster vehicle.
118 * @param x The X coordinate.
119 * @param y The Y coordinate.
120 * @param direction The direction the vehicle is facing.
121 * @param subtype The sub type of vehicle.
122 * @param big_ufo_destroyer_target The target for the UFO destroyer.
124 DisasterVehicle::DisasterVehicle(int x
, int y
, Direction direction
, DisasterSubType subtype
, VehicleID big_ufo_destroyer_target
) :
125 SpecializedVehicleBase(), big_ufo_destroyer_target(big_ufo_destroyer_target
)
127 this->vehstatus
= VS_UNCLICKABLE
;
137 case ST_BIG_UFO_DESTROYER
:
138 GetAircraftFlightLevelBounds(this, &this->z_pos
, nullptr);
141 case ST_HELICOPTER_ROTORS
:
142 GetAircraftFlightLevelBounds(this, &this->z_pos
, nullptr);
143 this->z_pos
+= ROTOR_Z_OFFSET
;
146 case ST_SMALL_SUBMARINE
:
147 case ST_BIG_SUBMARINE
:
151 case ST_ZEPPELINER_SHADOW
:
152 case ST_SMALL_UFO_SHADOW
:
153 case ST_AIRPLANE_SHADOW
:
154 case ST_HELICOPTER_SHADOW
:
155 case ST_BIG_UFO_SHADOW
:
156 case ST_BIG_UFO_DESTROYER_SHADOW
:
158 this->vehstatus
|= VS_SHADOW
;
162 this->direction
= direction
;
163 this->tile
= TileVirtXY(x
, y
);
164 this->subtype
= subtype
;
165 this->UpdateDeltaXY();
166 this->owner
= OWNER_NONE
;
167 this->image_override
= 0;
168 this->current_order
.Free();
171 this->UpdatePositionAndViewport();
175 * Update the position of the vehicle.
176 * @param x The new X-coordinate.
177 * @param y The new Y-coordinate.
178 * @param z The new Z-coordinate.
180 void DisasterVehicle::UpdatePosition(int x
, int y
, int z
)
185 this->tile
= TileVirtXY(x
, y
);
188 this->UpdatePositionAndViewport();
190 DisasterVehicle
*u
= this->Next();
192 int safe_x
= Clamp(x
, 0, MapMaxX() * TILE_SIZE
);
193 int safe_y
= Clamp(y
- 1, 0, MapMaxY() * TILE_SIZE
);
196 u
->y_pos
= y
- 1 - (max(z
- GetSlopePixelZ(safe_x
, safe_y
), 0) >> 3);
197 safe_y
= Clamp(u
->y_pos
, 0, MapMaxY() * TILE_SIZE
);
198 u
->z_pos
= GetSlopePixelZ(safe_x
, safe_y
);
199 u
->direction
= this->direction
;
202 u
->UpdatePositionAndViewport();
204 if ((u
= u
->Next()) != nullptr) {
207 u
->z_pos
= z
+ ROTOR_Z_OFFSET
;
208 u
->UpdatePositionAndViewport();
214 * Zeppeliner handling, v->current_order.dest states:
215 * 0: Zeppeliner initialization has found a small airport, go there and crash
216 * 1: Create crash and animate falling down for extra dramatic effect
217 * 2: Create more smoke and leave debris on ground
218 * 2: Clear the runway after some time and remove crashed zeppeliner
219 * If not airport was found, only state 0 is reached until zeppeliner leaves map
221 static bool DisasterTick_Zeppeliner(DisasterVehicle
*v
)
225 if (v
->current_order
.GetDestination() < 2) {
226 if (HasBit(v
->tick_counter
, 0)) return true;
228 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
230 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
232 if (v
->current_order
.GetDestination() == 1) {
233 if (++v
->age
== 38) {
234 v
->current_order
.SetDestination(2);
238 if (GB(v
->tick_counter
, 0, 3) == 0) CreateEffectVehicleRel(v
, 0, -17, 2, EV_CRASH_SMOKE
);
240 } else if (v
->current_order
.GetDestination() == 0) {
241 if (IsValidTile(v
->tile
) && IsAirportTile(v
->tile
)) {
242 v
->current_order
.SetDestination(1);
245 SetDParam(0, GetStationIndex(v
->tile
));
246 AddVehicleNewsItem(STR_NEWS_DISASTER_ZEPPELIN
, NT_ACCIDENT
, v
->index
); // Delete the news, when the zeppelin is gone
247 AI::NewEvent(GetTileOwner(v
->tile
), new ScriptEventDisasterZeppelinerCrashed(GetStationIndex(v
->tile
)));
251 if (v
->y_pos
>= (int)((MapSizeY() + 9) * TILE_SIZE
- 1)) {
259 if (v
->current_order
.GetDestination() > 2) {
260 if (++v
->age
<= 13320) return true;
262 if (IsValidTile(v
->tile
) && IsAirportTile(v
->tile
)) {
263 Station
*st
= Station::GetByTile(v
->tile
);
264 CLRBITS(st
->airport
.flags
, RUNWAY_IN_block
);
265 AI::NewEvent(GetTileOwner(v
->tile
), new ScriptEventDisasterZeppelinerCleared(st
->index
));
268 v
->UpdatePosition(v
->x_pos
, v
->y_pos
, GetAircraftFlightLevel(v
));
275 int z
= GetSlopePixelZ(x
, y
);
276 if (z
< v
->z_pos
) z
= v
->z_pos
- 1;
277 v
->UpdatePosition(x
, y
, z
);
280 CreateEffectVehicleRel(v
, 0, 7, 8, EV_EXPLOSION_LARGE
);
281 if (_settings_client
.sound
.disaster
) SndPlayVehicleFx(SND_12_EXPLOSION
, v
);
282 v
->image_override
= SPR_BLIMP_CRASHING
;
283 } else if (v
->age
== 70) {
284 v
->image_override
= SPR_BLIMP_CRASHED
;
285 } else if (v
->age
<= 300) {
286 if (GB(v
->tick_counter
, 0, 3) == 0) {
289 CreateEffectVehicleRel(v
,
295 } else if (v
->age
== 350) {
296 v
->current_order
.SetDestination(3);
300 if (IsValidTile(v
->tile
) && IsAirportTile(v
->tile
)) {
301 SETBITS(Station::GetByTile(v
->tile
)->airport
.flags
, RUNWAY_IN_block
);
308 * (Small) Ufo handling, v->current_order.dest states:
309 * 0: Fly around to the middle of the map, then randomly, after a while target a road vehicle
310 * 1: Home in on a road vehicle and crash it >:)
311 * If not road vehicle was found, only state 0 is used and Ufo disappears after a while
313 static bool DisasterTick_Ufo(DisasterVehicle
*v
)
315 v
->image_override
= (HasBit(++v
->tick_counter
, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER
: SPR_UFO_SMALL_SCOUT
;
317 if (v
->current_order
.GetDestination() == 0) {
318 /* Fly around randomly */
319 int x
= TileX(v
->dest_tile
) * TILE_SIZE
;
320 int y
= TileY(v
->dest_tile
) * TILE_SIZE
;
321 if (Delta(x
, v
->x_pos
) + Delta(y
, v
->y_pos
) >= (int)TILE_SIZE
) {
322 v
->direction
= GetDirectionTowards(v
, x
, y
);
323 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
324 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
328 v
->dest_tile
= RandomTile();
331 v
->current_order
.SetDestination(1);
333 uint n
= 0; // Total number of targetable road vehicles.
334 for (const RoadVehicle
*u
: RoadVehicle::Iterate()) {
335 if (u
->IsFrontEngine()) n
++;
339 /* If there are no targetable road vehicles, destroy the UFO. */
344 n
= RandomRange(n
); // Choose one of them.
345 for (const RoadVehicle
*u
: RoadVehicle::Iterate()) {
346 /* Find (n+1)-th road vehicle. */
347 if (u
->IsFrontEngine() && (n
-- == 0)) {
349 v
->dest_tile
= u
->index
;
357 /* Target a vehicle */
358 RoadVehicle
*u
= RoadVehicle::Get(v
->dest_tile
);
359 assert(u
!= nullptr && u
->type
== VEH_ROAD
&& u
->IsFrontEngine());
361 uint dist
= Delta(v
->x_pos
, u
->x_pos
) + Delta(v
->y_pos
, u
->y_pos
);
363 if (dist
< TILE_SIZE
&& !(u
->vehstatus
& VS_HIDDEN
) && u
->breakdown_ctr
== 0) {
364 u
->breakdown_ctr
= 3;
365 u
->breakdown_delay
= 140;
368 v
->direction
= GetDirectionTowards(v
, u
->x_pos
, u
->y_pos
);
369 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
372 if (dist
<= TILE_SIZE
&& z
> u
->z_pos
) z
--;
373 v
->UpdatePosition(gp
.x
, gp
.y
, z
);
375 if (z
<= u
->z_pos
&& (u
->vehstatus
& VS_HIDDEN
) == 0) {
377 if (u
->crashed_ctr
== 0) {
380 AddVehicleNewsItem(STR_NEWS_DISASTER_SMALL_UFO
, NT_ACCIDENT
, u
->index
); // delete the news, when the roadvehicle is gone
382 AI::NewEvent(u
->owner
, new ScriptEventVehicleCrashed(u
->index
, u
->tile
, ScriptEventVehicleCrashed::CRASH_RV_UFO
));
383 Game::NewEvent(new ScriptEventVehicleCrashed(u
->index
, u
->tile
, ScriptEventVehicleCrashed::CRASH_RV_UFO
));
389 CreateEffectVehicleRel(v
, 0, 7, 8, EV_EXPLOSION_LARGE
);
390 if (_settings_client
.sound
.disaster
) SndPlayVehicleFx(SND_12_EXPLOSION
, v
);
399 static void DestructIndustry(Industry
*i
)
401 for (TileIndex tile
= 0; tile
!= MapSize(); tile
++) {
402 if (i
->TileBelongsToIndustry(tile
)) {
403 ResetIndustryConstructionStage(tile
);
404 MarkTileDirtyByTile(tile
);
410 * Aircraft handling, v->current_order.dest states:
411 * 0: Fly towards the targeted industry
412 * 1: If within 15 tiles, fire away rockets and destroy industry
413 * 2: Industry explosions
414 * 3: Fly out of the map
415 * If the industry was removed in the meantime just fly to the end of the map.
416 * @param v The disaster vehicle.
417 * @param image_override The image at the time the aircraft is firing.
418 * @param leave_at_top True iff the vehicle leaves the map at the north side.
419 * @param news_message The string that's used as news message.
420 * @param industry_flag Only attack industries that have this flag set.
422 static bool DisasterTick_Aircraft(DisasterVehicle
*v
, uint16 image_override
, bool leave_at_top
, StringID news_message
, IndustryBehaviour industry_flag
)
425 v
->image_override
= (v
->current_order
.GetDestination() == 1 && HasBit(v
->tick_counter
, 2)) ? image_override
: 0;
427 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
428 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
430 if ((leave_at_top
&& gp
.x
< (-10 * (int)TILE_SIZE
)) || (!leave_at_top
&& gp
.x
> (int)(MapSizeX() * TILE_SIZE
+ 9 * TILE_SIZE
) - 1)) {
435 if (v
->current_order
.GetDestination() == 2) {
436 if (GB(v
->tick_counter
, 0, 2) == 0) {
437 Industry
*i
= Industry::Get(v
->dest_tile
); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
438 int x
= TileX(i
->location
.tile
) * TILE_SIZE
;
439 int y
= TileY(i
->location
.tile
) * TILE_SIZE
;
442 CreateEffectVehicleAbove(
448 if (++v
->age
>= 55) v
->current_order
.SetDestination(3);
450 } else if (v
->current_order
.GetDestination() == 1) {
451 if (++v
->age
== 112) {
452 v
->current_order
.SetDestination(2);
455 Industry
*i
= Industry::Get(v
->dest_tile
); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
458 SetDParam(0, i
->town
->index
);
459 AddIndustryNewsItem(news_message
, NT_ACCIDENT
, i
->index
); // delete the news, when the industry closes
460 if (_settings_client
.sound
.disaster
) SndPlayTileFx(SND_12_EXPLOSION
, i
->location
.tile
);
462 } else if (v
->current_order
.GetDestination() == 0) {
463 int x
= v
->x_pos
+ ((leave_at_top
? -15 : 15) * TILE_SIZE
);
466 if ((uint
)x
> MapMaxX() * TILE_SIZE
- 1) return true;
468 TileIndex tile
= TileVirtXY(x
, y
);
469 if (!IsTileType(tile
, MP_INDUSTRY
)) return true;
471 IndustryID ind
= GetIndustryIndex(tile
);
474 if (GetIndustrySpec(Industry::Get(ind
)->type
)->behaviour
& industry_flag
) {
475 v
->current_order
.SetDestination(1);
483 /** Airplane handling. */
484 static bool DisasterTick_Airplane(DisasterVehicle
*v
)
486 return DisasterTick_Aircraft(v
, SPR_F_15_FIRING
, true, STR_NEWS_DISASTER_AIRPLANE_OIL_REFINERY
, INDUSTRYBEH_AIRPLANE_ATTACKS
);
489 /** Helicopter handling. */
490 static bool DisasterTick_Helicopter(DisasterVehicle
*v
)
492 return DisasterTick_Aircraft(v
, SPR_AH_64A_FIRING
, false, STR_NEWS_DISASTER_HELICOPTER_FACTORY
, INDUSTRYBEH_CHOPPER_ATTACKS
);
495 /** Helicopter rotor blades; keep these spinning */
496 static bool DisasterTick_Helicopter_Rotors(DisasterVehicle
*v
)
499 if (HasBit(v
->tick_counter
, 0)) return true;
501 SpriteID
&cur_image
= v
->sprite_seq
.seq
[0].sprite
;
502 if (++cur_image
> SPR_ROTOR_MOVING_3
) cur_image
= SPR_ROTOR_MOVING_1
;
504 v
->UpdatePositionAndViewport();
510 * (Big) Ufo handling, v->current_order.dest states:
511 * 0: Fly around to the middle of the map, then randomly for a while and home in on a piece of rail
512 * 1: Land there and breakdown all trains in a radius of 12 tiles; and now we wait...
513 * because as soon as the Ufo lands, a fighter jet, a Skyranger, is called to clear up the mess
515 static bool DisasterTick_Big_Ufo(DisasterVehicle
*v
)
519 if (v
->current_order
.GetDestination() == 1) {
520 int x
= TileX(v
->dest_tile
) * TILE_SIZE
+ TILE_SIZE
/ 2;
521 int y
= TileY(v
->dest_tile
) * TILE_SIZE
+ TILE_SIZE
/ 2;
522 if (Delta(v
->x_pos
, x
) + Delta(v
->y_pos
, y
) >= 8) {
523 v
->direction
= GetDirectionTowards(v
, x
, y
);
525 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
526 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
530 if (!IsValidTile(v
->dest_tile
)) {
531 /* Make sure we don't land outside the map. */
536 int z
= GetSlopePixelZ(v
->x_pos
, v
->y_pos
);
538 v
->UpdatePosition(v
->x_pos
, v
->y_pos
, v
->z_pos
- 1);
542 v
->current_order
.SetDestination(2);
544 for (Vehicle
*target
: Vehicle::Iterate()) {
545 if (target
->IsGroundVehicle()) {
546 if (Delta(target
->x_pos
, v
->x_pos
) + Delta(target
->y_pos
, v
->y_pos
) <= 12 * (int)TILE_SIZE
) {
547 target
->breakdown_ctr
= 5;
548 target
->breakdown_delay
= 0xF0;
553 Town
*t
= ClosestTownFromTile(v
->dest_tile
, UINT_MAX
);
554 SetDParam(0, t
->index
);
555 AddTileNewsItem(STR_NEWS_DISASTER_BIG_UFO
, NT_ACCIDENT
, v
->tile
);
557 if (!Vehicle::CanAllocateItem(2)) {
561 DisasterVehicle
*u
= new DisasterVehicle(-6 * (int)TILE_SIZE
, v
->y_pos
, DIR_SW
, ST_BIG_UFO_DESTROYER
, v
->index
);
562 DisasterVehicle
*w
= new DisasterVehicle(-6 * (int)TILE_SIZE
, v
->y_pos
, DIR_SW
, ST_BIG_UFO_DESTROYER_SHADOW
);
564 } else if (v
->current_order
.GetDestination() == 0) {
565 int x
= TileX(v
->dest_tile
) * TILE_SIZE
;
566 int y
= TileY(v
->dest_tile
) * TILE_SIZE
;
567 if (Delta(x
, v
->x_pos
) + Delta(y
, v
->y_pos
) >= (int)TILE_SIZE
) {
568 v
->direction
= GetDirectionTowards(v
, x
, y
);
569 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
570 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
575 v
->dest_tile
= RandomTile();
578 v
->current_order
.SetDestination(1);
580 TileIndex tile_org
= RandomTile();
581 TileIndex tile
= tile_org
;
583 if (IsPlainRailTile(tile
) &&
584 Company::IsHumanID(GetTileOwner(tile
))) {
587 tile
= TILE_MASK(tile
+ 1);
588 } while (tile
!= tile_org
);
597 * Skyranger destroying (Big) Ufo handling, v->current_order.dest states:
598 * 0: Home in on landed Ufo and shoot it down
600 static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle
*v
)
604 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
605 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
607 if (gp
.x
> (int)(MapSizeX() * TILE_SIZE
+ 9 * TILE_SIZE
) - 1) {
612 if (v
->current_order
.GetDestination() == 0) {
613 Vehicle
*u
= Vehicle::Get(v
->big_ufo_destroyer_target
);
614 if (Delta(v
->x_pos
, u
->x_pos
) > (int)TILE_SIZE
) return true;
615 v
->current_order
.SetDestination(1);
617 CreateEffectVehicleRel(u
, 0, 7, 8, EV_EXPLOSION_LARGE
);
618 if (_settings_client
.sound
.disaster
) SndPlayVehicleFx(SND_12_EXPLOSION
, u
);
622 for (int i
= 0; i
!= 80; i
++) {
624 CreateEffectVehicleAbove(
625 GB(r
, 0, 6) + v
->x_pos
- 32,
626 GB(r
, 5, 6) + v
->y_pos
- 32,
631 for (int dy
= -3; dy
< 3; dy
++) {
632 for (int dx
= -3; dx
< 3; dx
++) {
633 TileIndex tile
= TileAddWrap(v
->tile
, dx
, dy
);
634 if (tile
!= INVALID_TILE
) DisasterClearSquare(tile
);
643 * Submarine, v->current_order.dest states:
644 * Unused, just float around aimlessly and pop up at different places, turning around
646 static bool DisasterTick_Submarine(DisasterVehicle
*v
)
650 if (++v
->age
> 8880) {
655 if (!HasBit(v
->tick_counter
, 0)) return true;
657 TileIndex tile
= v
->tile
+ TileOffsByDiagDir(DirToDiagDir(v
->direction
));
658 if (IsValidTile(tile
)) {
659 TrackBits trackbits
= TrackStatusToTrackBits(GetTileTrackStatus(tile
, TRANSPORT_WATER
, 0));
660 if (trackbits
== TRACK_BIT_ALL
&& !Chance16(1, 90)) {
661 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
662 v
->UpdatePosition(gp
.x
, gp
.y
, v
->z_pos
);
667 v
->direction
= ChangeDir(v
->direction
, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT
: DIRDIFF_90LEFT
);
673 static bool DisasterTick_NULL(DisasterVehicle
*v
)
678 typedef bool DisasterVehicleTickProc(DisasterVehicle
*v
);
680 static DisasterVehicleTickProc
* const _disastervehicle_tick_procs
[] = {
681 DisasterTick_Zeppeliner
, DisasterTick_NULL
,
682 DisasterTick_Ufo
, DisasterTick_NULL
,
683 DisasterTick_Airplane
, DisasterTick_NULL
,
684 DisasterTick_Helicopter
, DisasterTick_NULL
, DisasterTick_Helicopter_Rotors
,
685 DisasterTick_Big_Ufo
, DisasterTick_NULL
, DisasterTick_Big_Ufo_Destroyer
,
687 DisasterTick_Submarine
,
688 DisasterTick_Submarine
,
692 bool DisasterVehicle::Tick()
694 return _disastervehicle_tick_procs
[this->subtype
](this);
697 typedef void DisasterInitProc();
701 * Zeppeliner which crashes on a small airport if one found,
702 * otherwise crashes on a random tile
704 static void Disaster_Zeppeliner_Init()
706 if (!Vehicle::CanAllocateItem(2)) return;
708 /* Pick a random place, unless we find a small airport */
709 int x
= TileX(Random()) * TILE_SIZE
+ TILE_SIZE
/ 2;
711 for (const Station
*st
: Station::Iterate()) {
712 if (st
->airport
.tile
!= INVALID_TILE
&& (st
->airport
.type
== AT_SMALL
|| st
->airport
.type
== AT_LARGE
)) {
713 x
= (TileX(st
->airport
.tile
) + 2) * TILE_SIZE
;
718 DisasterVehicle
*v
= new DisasterVehicle(x
, 0, DIR_SE
, ST_ZEPPELINER
);
719 /* Allocate shadow */
720 DisasterVehicle
*u
= new DisasterVehicle(x
, 0, DIR_SE
, ST_ZEPPELINER_SHADOW
);
726 * Ufo which flies around aimlessly from the middle of the map a bit
727 * until it locates a road vehicle which it targets and then destroys
729 static void Disaster_Small_Ufo_Init()
731 if (!Vehicle::CanAllocateItem(2)) return;
733 int x
= TileX(Random()) * TILE_SIZE
+ TILE_SIZE
/ 2;
734 DisasterVehicle
*v
= new DisasterVehicle(x
, 0, DIR_SE
, ST_SMALL_UFO
);
735 v
->dest_tile
= TileXY(MapSizeX() / 2, MapSizeY() / 2);
737 /* Allocate shadow */
738 DisasterVehicle
*u
= new DisasterVehicle(x
, 0, DIR_SE
, ST_SMALL_UFO_SHADOW
);
743 /* Combat airplane which destroys an oil refinery */
744 static void Disaster_Airplane_Init()
746 if (!Vehicle::CanAllocateItem(2)) return;
748 Industry
*found
= nullptr;
750 for (Industry
*i
: Industry::Iterate()) {
751 if ((GetIndustrySpec(i
->type
)->behaviour
& INDUSTRYBEH_AIRPLANE_ATTACKS
) &&
752 (found
== nullptr || Chance16(1, 2))) {
757 if (found
== nullptr) return;
759 /* Start from the bottom (south side) of the map */
760 int x
= (MapSizeX() + 9) * TILE_SIZE
- 1;
761 int y
= TileY(found
->location
.tile
) * TILE_SIZE
+ 37;
763 DisasterVehicle
*v
= new DisasterVehicle(x
, y
, DIR_NE
, ST_AIRPLANE
);
764 DisasterVehicle
*u
= new DisasterVehicle(x
, y
, DIR_NE
, ST_AIRPLANE_SHADOW
);
769 /** Combat helicopter that destroys a factory */
770 static void Disaster_Helicopter_Init()
772 if (!Vehicle::CanAllocateItem(3)) return;
774 Industry
*found
= nullptr;
776 for (Industry
*i
: Industry::Iterate()) {
777 if ((GetIndustrySpec(i
->type
)->behaviour
& INDUSTRYBEH_CHOPPER_ATTACKS
) &&
778 (found
== nullptr || Chance16(1, 2))) {
783 if (found
== nullptr) return;
785 int x
= -16 * (int)TILE_SIZE
;
786 int y
= TileY(found
->location
.tile
) * TILE_SIZE
+ 37;
788 DisasterVehicle
*v
= new DisasterVehicle(x
, y
, DIR_SW
, ST_HELICOPTER
);
789 DisasterVehicle
*u
= new DisasterVehicle(x
, y
, DIR_SW
, ST_HELICOPTER_SHADOW
);
792 DisasterVehicle
*w
= new DisasterVehicle(x
, y
, DIR_SW
, ST_HELICOPTER_ROTORS
);
797 /* Big Ufo which lands on a piece of rail and will consequently be shot
798 * down by a combat airplane, destroying the surroundings */
799 static void Disaster_Big_Ufo_Init()
801 if (!Vehicle::CanAllocateItem(2)) return;
803 int x
= TileX(Random()) * TILE_SIZE
+ TILE_SIZE
/ 2;
804 int y
= MapMaxX() * TILE_SIZE
- 1;
806 DisasterVehicle
*v
= new DisasterVehicle(x
, y
, DIR_NW
, ST_BIG_UFO
);
807 v
->dest_tile
= TileXY(MapSizeX() / 2, MapSizeY() / 2);
809 /* Allocate shadow */
810 DisasterVehicle
*u
= new DisasterVehicle(x
, y
, DIR_NW
, ST_BIG_UFO_SHADOW
);
815 static void Disaster_Submarine_Init(DisasterSubType subtype
)
817 if (!Vehicle::CanAllocateItem()) return;
822 int x
= TileX(r
) * TILE_SIZE
+ TILE_SIZE
/ 2;
825 y
= MapMaxY() * TILE_SIZE
- TILE_SIZE
/ 2 - 1;
829 if (_settings_game
.construction
.freeform_edges
) y
+= TILE_SIZE
;
832 if (!IsWaterTile(TileVirtXY(x
, y
))) return;
834 new DisasterVehicle(x
, y
, dir
, subtype
);
837 /* Curious submarine #1, just floats around */
838 static void Disaster_Small_Submarine_Init()
840 Disaster_Submarine_Init(ST_SMALL_SUBMARINE
);
844 /* Curious submarine #2, just floats around */
845 static void Disaster_Big_Submarine_Init()
847 Disaster_Submarine_Init(ST_BIG_SUBMARINE
);
852 * Coal mine catastrophe, destroys a stretch of 30 tiles of
853 * land in a certain direction
855 static void Disaster_CoalMine_Init()
857 int index
= GB(Random(), 0, 4);
860 for (m
= 0; m
< 15; m
++) {
861 for (const Industry
*i
: Industry::Iterate()) {
862 if ((GetIndustrySpec(i
->type
)->behaviour
& INDUSTRYBEH_CAN_SUBSIDENCE
) && --index
< 0) {
863 SetDParam(0, i
->town
->index
);
864 AddTileNewsItem(STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE
, NT_ACCIDENT
, i
->location
.tile
+ TileDiffXY(1, 1)); // keep the news, even when the mine closes
867 TileIndex tile
= i
->location
.tile
;
868 TileIndexDiff step
= TileOffsByDiagDir((DiagDirection
)GB(Random(), 0, 2));
870 for (uint n
= 0; n
< 30; n
++) {
871 DisasterClearSquare(tile
);
873 if (!IsValidTile(tile
)) break;
883 DisasterInitProc
*init_proc
; ///< The init function for this disaster.
884 Year min_year
; ///< The first year this disaster will occur.
885 Year max_year
; ///< The last year this disaster will occur.
888 static const Disaster _disasters
[] = {
889 {Disaster_Zeppeliner_Init
, 1930, 1955}, // zeppeliner
890 {Disaster_Small_Ufo_Init
, 1940, 1970}, // ufo (small)
891 {Disaster_Airplane_Init
, 1960, 1990}, // airplane
892 {Disaster_Helicopter_Init
, 1970, 2000}, // helicopter
893 {Disaster_Big_Ufo_Init
, 2000, 2100}, // ufo (big)
894 {Disaster_Small_Submarine_Init
, 1940, 1965}, // submarine (small)
895 {Disaster_Big_Submarine_Init
, 1975, 2010}, // submarine (big)
896 {Disaster_CoalMine_Init
, 1950, 1985}, // coalmine
899 static void DoDisaster()
901 byte buf
[lengthof(_disasters
)];
904 for (size_t i
= 0; i
!= lengthof(_disasters
); i
++) {
905 if (_cur_year
>= _disasters
[i
].min_year
&& _cur_year
< _disasters
[i
].max_year
) buf
[j
++] = (byte
)i
;
910 _disasters
[buf
[RandomRange(j
)]].init_proc();
914 static void ResetDisasterDelay()
916 _disaster_delay
= GB(Random(), 0, 9) + 730;
919 void DisasterDailyLoop()
921 if (--_disaster_delay
!= 0) return;
923 ResetDisasterDelay();
925 if (_settings_game
.difficulty
.disasters
!= 0) DoDisaster();
928 void StartupDisasters()
930 ResetDisasterDelay();
934 * Marks all disasters targeting this industry in such a way
935 * they won't call Industry::Get(v->dest_tile) on invalid industry anymore.
936 * @param i deleted industry
938 void ReleaseDisastersTargetingIndustry(IndustryID i
)
940 for (DisasterVehicle
*v
: DisasterVehicle::Iterate()) {
941 /* primary disaster vehicles that have chosen target */
942 if (v
->subtype
== ST_AIRPLANE
|| v
->subtype
== ST_HELICOPTER
) {
943 /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
944 if (v
->current_order
.GetDestination() > 0 && v
->dest_tile
== i
) v
->current_order
.SetDestination(3);
950 * Notify disasters that we are about to delete a vehicle. So make them head elsewhere.
951 * @param vehicle deleted vehicle
953 void ReleaseDisastersTargetingVehicle(VehicleID vehicle
)
955 for (DisasterVehicle
*v
: DisasterVehicle::Iterate()) {
956 /* primary disaster vehicles that have chosen target */
957 if (v
->subtype
== ST_SMALL_UFO
) {
958 if (v
->current_order
.GetDestination() != 0 && v
->dest_tile
== vehicle
) {
959 /* Revert to target-searching */
960 v
->current_order
.SetDestination(0);
961 v
->dest_tile
= RandomTile();
962 GetAircraftFlightLevelBounds(v
, &v
->z_pos
, nullptr);
969 void DisasterVehicle::UpdateDeltaXY()