1 /* $Id: disaster_cmd.cpp 24900 2013-01-08 22:46:42Z planetmaker $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
11 * @file disaster_vehicle.cpp
13 * All disaster/easter egg vehicles are handled here.
14 * The general flow of control for the disaster vehicles is as follows:
16 * <li>Initialize the disaster in a disaster specific way (eg start position,
17 * possible target, etc.) Disaster_XXX_Init() function
18 * <li>Add a subtype to a disaster, which is an index into the function array
19 * that handles the vehicle's ticks.
20 * <li>Run the disaster vehicles each tick until their target has been reached,
21 * this happens in the DisasterTick_XXX() functions. In here, a vehicle's
22 * state is kept by v->current_order.dest variable. Each achieved sub-target
23 * will increase this value, and the last one will remove the disaster itself
31 #include "disaster_vehicle.h"
33 #include "station_base.h"
34 #include "command_func.h"
35 #include "news_func.h"
37 #include "company_func.h"
38 #include "strings_func.h"
39 #include "date_func.h"
40 #include "viewport_func.h"
41 #include "vehicle_func.h"
42 #include "sound_func.h"
43 #include "effectvehicle_func.h"
46 #include "game/game.hpp"
47 #include "company_base.h"
48 #include "core/random_func.hpp"
49 #include "core/backup_type.hpp"
51 #include "table/strings.h"
53 #include "safeguards.h"
55 /** Delay counter for considering the next disaster. */
56 uint16 _disaster_delay
;
58 static void DisasterClearSquare(TileIndex tile
)
60 if (EnsureNoVehicleOnGround(tile
).Failed()) return;
62 switch (GetTileType(tile
)) {
64 if (Company::IsHumanID(GetTileOwner(tile
)) && !IsRailDepot(tile
)) {
65 Backup
<CompanyByte
> cur_company(_current_company
, OWNER_WATER
, FILE_LINE
);
66 DoCommand(tile
, 0, 0, DC_EXEC
, CMD_LANDSCAPE_CLEAR
);
67 cur_company
.Restore();
69 /* update signals in buffer */
70 UpdateSignalsInBuffer();
75 Backup
<CompanyByte
> cur_company(_current_company
, OWNER_NONE
, FILE_LINE
);
76 DoCommand(tile
, 0, 0, DC_EXEC
, CMD_LANDSCAPE_CLEAR
);
77 cur_company
.Restore();
91 static const SpriteID _disaster_images_1
[] = {SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
, SPR_BLIMP
};
92 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
};
93 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
};
94 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
};
95 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
};
96 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
};
97 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
};
98 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
};
99 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
};
101 static const SpriteID
* const _disaster_images
[] = {
102 _disaster_images_1
, _disaster_images_1
, ///< zeppeliner and zeppeliner shadow
103 _disaster_images_2
, _disaster_images_2
, ///< small ufo and small ufo shadow
104 _disaster_images_3
, _disaster_images_3
, ///< combat aircraft and shadow
105 _disaster_images_8
, _disaster_images_8
, _disaster_images_9
, ///< combat helicopter, shadow and rotor
106 _disaster_images_6
, _disaster_images_6
, ///< big ufo and shadow
107 _disaster_images_7
, _disaster_images_7
, ///< skyranger and shadow
108 _disaster_images_4
, _disaster_images_5
, ///< small and big submarine sprites
111 void DisasterVehicle::UpdateImage()
113 SpriteID img
= this->image_override
;
114 if (img
== 0) img
= _disaster_images
[this->subtype
][this->direction
];
115 this->sprite_seq
.Set(img
);
119 * Construct the disaster vehicle.
120 * @param x The X coordinate.
121 * @param y The Y coordinate.
122 * @param direction The direction the vehicle is facing.
123 * @param subtype The sub type of vehicle.
124 * @param big_ufo_destroyer_target The target for the UFO destroyer.
126 DisasterVehicle::DisasterVehicle(int x
, int y
, Direction direction
, DisasterSubType subtype
, VehicleID big_ufo_destroyer_target
) :
127 SpecializedVehicleBase(), big_ufo_destroyer_target(big_ufo_destroyer_target
)
129 this->vehstatus
= VS_UNCLICKABLE
;
139 case ST_BIG_UFO_DESTROYER
:
140 GetAircraftFlightLevelBounds(this, &this->z_pos
, nullptr);
143 case ST_HELICOPTER_ROTORS
:
144 GetAircraftFlightLevelBounds(this, &this->z_pos
, nullptr);
145 this->z_pos
+= ROTOR_Z_OFFSET
;
148 case ST_SMALL_SUBMARINE
:
149 case ST_BIG_SUBMARINE
:
153 case ST_ZEPPELINER_SHADOW
:
154 case ST_SMALL_UFO_SHADOW
:
155 case ST_AIRPLANE_SHADOW
:
156 case ST_HELICOPTER_SHADOW
:
157 case ST_BIG_UFO_SHADOW
:
158 case ST_BIG_UFO_DESTROYER_SHADOW
:
160 this->vehstatus
|= VS_SHADOW
;
164 this->direction
= direction
;
165 this->tile
= TileVirtXY(x
, y
);
166 this->subtype
= subtype
;
167 this->UpdateDeltaXY(INVALID_DIR
);
168 this->owner
= OWNER_NONE
;
169 this->image_override
= 0;
170 this->current_order
.Free();
173 this->UpdatePositionAndViewport();
177 * Update the position of the vehicle.
178 * @param x The new X-coordinate.
179 * @param y The new Y-coordinate.
180 * @param z The new Z-coordinate.
182 void DisasterVehicle::UpdatePosition(int x
, int y
, int z
)
187 this->tile
= TileVirtXY(x
, y
);
190 this->UpdatePositionAndViewport();
192 DisasterVehicle
*u
= this->Next();
194 int safe_x
= Clamp(x
, 0, MapMaxX() * TILE_SIZE
);
195 int safe_y
= Clamp(y
- 1, 0, MapMaxY() * TILE_SIZE
);
198 u
->y_pos
= y
- 1 - (max(z
- GetSlopePixelZ(safe_x
, safe_y
), 0) >> 3);
199 safe_y
= Clamp(u
->y_pos
, 0, MapMaxY() * TILE_SIZE
);
200 u
->z_pos
= GetSlopePixelZ(safe_x
, safe_y
);
201 u
->direction
= this->direction
;
204 u
->UpdatePositionAndViewport();
206 if ((u
= u
->Next()) != nullptr) {
209 u
->z_pos
= z
+ ROTOR_Z_OFFSET
;
210 u
->UpdatePositionAndViewport();
216 * Zeppeliner handling, v->current_order.dest states:
217 * 0: Zeppeliner initialization has found a small airport, go there and crash
218 * 1: Create crash and animate falling down for extra dramatic effect
219 * 2: Create more smoke and leave debris on ground
220 * 2: Clear the runway after some time and remove crashed zeppeliner
221 * If not airport was found, only state 0 is reached until zeppeliner leaves map
223 static bool DisasterTick_Zeppeliner(DisasterVehicle
*v
)
227 if (v
->current_order
.GetDestination() < 2) {
228 if (HasBit(v
->tick_counter
, 0)) return true;
230 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
232 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
234 if (v
->current_order
.GetDestination() == 1) {
235 if (++v
->age
== 38) {
236 v
->current_order
.SetDestination(2);
240 if (GB(v
->tick_counter
, 0, 3) == 0) CreateEffectVehicleRel(v
, 0, -17, 2, EV_CRASH_SMOKE
);
242 } else if (v
->current_order
.GetDestination() == 0) {
243 if (IsValidTile(v
->tile
) && IsAirportTile(v
->tile
)) {
244 v
->current_order
.SetDestination(1);
247 SetDParam(0, GetStationIndex(v
->tile
));
248 AddVehicleNewsItem(STR_NEWS_DISASTER_ZEPPELIN
, NT_ACCIDENT
, v
->index
); // Delete the news, when the zeppelin is gone
249 AI::NewEvent(GetTileOwner(v
->tile
), new ScriptEventDisasterZeppelinerCrashed(GetStationIndex(v
->tile
)));
253 if (v
->y_pos
>= (int)((MapSizeY() + 9) * TILE_SIZE
- 1)) {
261 if (v
->current_order
.GetDestination() > 2) {
262 if (++v
->age
<= 13320) return true;
264 if (IsValidTile(v
->tile
) && IsAirportTile(v
->tile
)) {
265 Station
*st
= Station::GetByTile(v
->tile
);
266 CLRBITS(st
->airport
.flags
, RUNWAY_IN_block
);
267 AI::NewEvent(GetTileOwner(v
->tile
), new ScriptEventDisasterZeppelinerCleared(st
->index
));
270 v
->UpdatePosition(v
->x_pos
, v
->y_pos
, GetAircraftFlightLevel(v
));
277 int z
= GetSlopePixelZ(x
, y
);
278 if (z
< v
->z_pos
) z
= v
->z_pos
- 1;
279 v
->UpdatePosition(x
, y
, z
);
282 CreateEffectVehicleRel(v
, 0, 7, 8, EV_EXPLOSION_LARGE
);
283 if (_settings_client
.sound
.disaster
) SndPlayVehicleFx(SND_12_EXPLOSION
, v
);
284 v
->image_override
= SPR_BLIMP_CRASHING
;
285 } else if (v
->age
== 70) {
286 v
->image_override
= SPR_BLIMP_CRASHED
;
287 } else if (v
->age
<= 300) {
288 if (GB(v
->tick_counter
, 0, 3) == 0) {
291 CreateEffectVehicleRel(v
,
297 } else if (v
->age
== 350) {
298 v
->current_order
.SetDestination(3);
302 if (IsValidTile(v
->tile
) && IsAirportTile(v
->tile
)) {
303 SETBITS(Station::GetByTile(v
->tile
)->airport
.flags
, RUNWAY_IN_block
);
310 * (Small) Ufo handling, v->current_order.dest states:
311 * 0: Fly around to the middle of the map, then randomly, after a while target a road vehicle
312 * 1: Home in on a road vehicle and crash it >:)
313 * If not road vehicle was found, only state 0 is used and Ufo disappears after a while
315 static bool DisasterTick_Ufo(DisasterVehicle
*v
)
317 v
->image_override
= (HasBit(++v
->tick_counter
, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER
: SPR_UFO_SMALL_SCOUT
;
319 if (v
->current_order
.GetDestination() == 0) {
320 /* Fly around randomly */
321 int x
= TileX(v
->dest_tile
) * TILE_SIZE
;
322 int y
= TileY(v
->dest_tile
) * TILE_SIZE
;
323 if (Delta(x
, v
->x_pos
) + Delta(y
, v
->y_pos
) >= (int)TILE_SIZE
) {
324 v
->direction
= GetDirectionTowards(v
, x
, y
);
325 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
326 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
330 v
->dest_tile
= RandomTile();
333 v
->current_order
.SetDestination(1);
335 uint n
= 0; // Total number of targetable road vehicles.
337 FOR_ALL_ROADVEHICLES(u
) {
338 if (u
->IsFrontEngine()) n
++;
342 /* If there are no targetable road vehicles, destroy the UFO. */
347 n
= RandomRange(n
); // Choose one of them.
348 FOR_ALL_ROADVEHICLES(u
) {
349 /* Find (n+1)-th road vehicle. */
350 if (u
->IsFrontEngine() && (n
-- == 0)) break;
354 v
->dest_tile
= u
->index
;
358 /* Target a vehicle */
359 RoadVehicle
*u
= RoadVehicle::Get(v
->dest_tile
);
360 assert(u
!= nullptr && u
->type
== VEH_ROAD
&& u
->IsFrontEngine());
362 uint dist
= Delta(v
->x_pos
, u
->x_pos
) + Delta(v
->y_pos
, u
->y_pos
);
364 if (dist
< TILE_SIZE
&& !(u
->vehstatus
& VS_HIDDEN
) && u
->breakdown_ctr
== 0) {
365 u
->breakdown_ctr
= 3;
366 u
->breakdown_delay
= 140;
369 v
->direction
= GetDirectionTowards(v
, u
->x_pos
, u
->y_pos
);
370 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
373 if (dist
<= TILE_SIZE
&& z
> u
->z_pos
) z
--;
374 v
->UpdatePosition(gp
.x
, gp
.y
, z
);
376 if (z
<= u
->z_pos
&& (u
->vehstatus
& VS_HIDDEN
) == 0) {
378 if (u
->crashed_ctr
== 0) {
381 AddVehicleNewsItem(STR_NEWS_DISASTER_SMALL_UFO
, NT_ACCIDENT
, u
->index
); // delete the news, when the roadvehicle is gone
383 AI::NewEvent(u
->owner
, new ScriptEventVehicleCrashed(u
->index
, u
->tile
, ScriptEventVehicleCrashed::CRASH_RV_UFO
));
384 Game::NewEvent(new ScriptEventVehicleCrashed(u
->index
, u
->tile
, ScriptEventVehicleCrashed::CRASH_RV_UFO
));
390 CreateEffectVehicleRel(v
, 0, 7, 8, EV_EXPLOSION_LARGE
);
391 if (_settings_client
.sound
.disaster
) SndPlayVehicleFx(SND_12_EXPLOSION
, v
);
400 static void DestructIndustry(Industry
*i
)
402 for (TileIndex tile
= 0; tile
!= MapSize(); tile
++) {
403 if (i
->TileBelongsToIndustry(tile
)) {
404 ResetIndustryConstructionStage(tile
);
405 MarkTileDirtyByTile(tile
);
411 * Aircraft handling, v->current_order.dest states:
412 * 0: Fly towards the targeted industry
413 * 1: If within 15 tiles, fire away rockets and destroy industry
414 * 2: Industry explosions
415 * 3: Fly out of the map
416 * If the industry was removed in the meantime just fly to the end of the map.
417 * @param v The disaster vehicle.
418 * @param image_override The image at the time the aircraft is firing.
419 * @param leave_at_top True iff the vehicle leaves the map at the north side.
420 * @param news_message The string that's used as news message.
421 * @param industry_flag Only attack industries that have this flag set.
423 static bool DisasterTick_Aircraft(DisasterVehicle
*v
, uint16 image_override
, bool leave_at_top
, StringID news_message
, IndustryBehaviour industry_flag
)
426 v
->image_override
= (v
->current_order
.GetDestination() == 1 && HasBit(v
->tick_counter
, 2)) ? image_override
: 0;
428 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
429 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
431 if ((leave_at_top
&& gp
.x
< (-10 * (int)TILE_SIZE
)) || (!leave_at_top
&& gp
.x
> (int)(MapSizeX() * TILE_SIZE
+ 9 * TILE_SIZE
) - 1)) {
436 if (v
->current_order
.GetDestination() == 2) {
437 if (GB(v
->tick_counter
, 0, 2) == 0) {
438 Industry
*i
= Industry::Get(v
->dest_tile
); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
439 int x
= TileX(i
->location
.tile
) * TILE_SIZE
;
440 int y
= TileY(i
->location
.tile
) * TILE_SIZE
;
443 CreateEffectVehicleAbove(
449 if (++v
->age
>= 55) v
->current_order
.SetDestination(3);
451 } else if (v
->current_order
.GetDestination() == 1) {
452 if (++v
->age
== 112) {
453 v
->current_order
.SetDestination(2);
456 Industry
*i
= Industry::Get(v
->dest_tile
); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
459 SetDParam(0, i
->town
->index
);
460 AddIndustryNewsItem(news_message
, NT_ACCIDENT
, i
->index
); // delete the news, when the industry closes
461 if (_settings_client
.sound
.disaster
) SndPlayTileFx(SND_12_EXPLOSION
, i
->location
.tile
);
463 } else if (v
->current_order
.GetDestination() == 0) {
464 int x
= v
->x_pos
+ ((leave_at_top
? -15 : 15) * TILE_SIZE
);
467 if ((uint
)x
> MapMaxX() * TILE_SIZE
- 1) return true;
469 TileIndex tile
= TileVirtXY(x
, y
);
470 if (!IsTileType(tile
, MP_INDUSTRY
)) return true;
472 IndustryID ind
= GetIndustryIndex(tile
);
475 if (GetIndustrySpec(Industry::Get(ind
)->type
)->behaviour
& industry_flag
) {
476 v
->current_order
.SetDestination(1);
484 /** Airplane handling. */
485 static bool DisasterTick_Airplane(DisasterVehicle
*v
)
487 return DisasterTick_Aircraft(v
, SPR_F_15_FIRING
, true, STR_NEWS_DISASTER_AIRPLANE_OIL_REFINERY
, INDUSTRYBEH_AIRPLANE_ATTACKS
);
490 /** Helicopter handling. */
491 static bool DisasterTick_Helicopter(DisasterVehicle
*v
)
493 return DisasterTick_Aircraft(v
, SPR_AH_64A_FIRING
, false, STR_NEWS_DISASTER_HELICOPTER_FACTORY
, INDUSTRYBEH_CHOPPER_ATTACKS
);
496 /** Helicopter rotor blades; keep these spinning */
497 static bool DisasterTick_Helicopter_Rotors(DisasterVehicle
*v
)
500 if (HasBit(v
->tick_counter
, 0)) return true;
502 SpriteID
&cur_image
= v
->sprite_seq
.seq
[0].sprite
;
503 if (++cur_image
> SPR_ROTOR_MOVING_3
) cur_image
= SPR_ROTOR_MOVING_1
;
505 v
->UpdatePositionAndViewport();
511 * (Big) Ufo handling, v->current_order.dest states:
512 * 0: Fly around to the middle of the map, then randomly for a while and home in on a piece of rail
513 * 1: Land there and breakdown all trains in a radius of 12 tiles; and now we wait...
514 * because as soon as the Ufo lands, a fighter jet, a Skyranger, is called to clear up the mess
516 static bool DisasterTick_Big_Ufo(DisasterVehicle
*v
)
520 if (v
->current_order
.GetDestination() == 1) {
521 int x
= TileX(v
->dest_tile
) * TILE_SIZE
+ TILE_SIZE
/ 2;
522 int y
= TileY(v
->dest_tile
) * TILE_SIZE
+ TILE_SIZE
/ 2;
523 if (Delta(v
->x_pos
, x
) + Delta(v
->y_pos
, y
) >= 8) {
524 v
->direction
= GetDirectionTowards(v
, x
, y
);
526 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
527 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
531 if (!IsValidTile(v
->dest_tile
)) {
532 /* Make sure we don't land outside the map. */
537 int z
= GetSlopePixelZ(v
->x_pos
, v
->y_pos
);
539 v
->UpdatePosition(v
->x_pos
, v
->y_pos
, v
->z_pos
- 1);
543 v
->current_order
.SetDestination(2);
546 FOR_ALL_VEHICLES(target
) {
547 if (target
->IsGroundVehicle()) {
548 if (Delta(target
->x_pos
, v
->x_pos
) + Delta(target
->y_pos
, v
->y_pos
) <= 12 * (int)TILE_SIZE
) {
549 target
->breakdown_ctr
= 5;
550 target
->breakdown_delay
= 0xF0;
555 Town
*t
= ClosestTownFromTile(v
->dest_tile
, UINT_MAX
);
556 SetDParam(0, t
->index
);
557 AddTileNewsItem(STR_NEWS_DISASTER_BIG_UFO
, NT_ACCIDENT
, v
->tile
);
559 if (!Vehicle::CanAllocateItem(2)) {
563 DisasterVehicle
*u
= new DisasterVehicle(-6 * (int)TILE_SIZE
, v
->y_pos
, DIR_SW
, ST_BIG_UFO_DESTROYER
, v
->index
);
564 DisasterVehicle
*w
= new DisasterVehicle(-6 * (int)TILE_SIZE
, v
->y_pos
, DIR_SW
, ST_BIG_UFO_DESTROYER_SHADOW
);
566 } else if (v
->current_order
.GetDestination() == 0) {
567 int x
= TileX(v
->dest_tile
) * TILE_SIZE
;
568 int y
= TileY(v
->dest_tile
) * TILE_SIZE
;
569 if (Delta(x
, v
->x_pos
) + Delta(y
, v
->y_pos
) >= (int)TILE_SIZE
) {
570 v
->direction
= GetDirectionTowards(v
, x
, y
);
571 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
572 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
577 v
->dest_tile
= RandomTile();
580 v
->current_order
.SetDestination(1);
582 TileIndex tile_org
= RandomTile();
583 TileIndex tile
= tile_org
;
585 if (IsPlainRailTile(tile
) &&
586 Company::IsHumanID(GetTileOwner(tile
))) {
589 tile
= TILE_MASK(tile
+ 1);
590 } while (tile
!= tile_org
);
599 * Skyranger destroying (Big) Ufo handling, v->current_order.dest states:
600 * 0: Home in on landed Ufo and shoot it down
602 static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle
*v
)
606 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
607 v
->UpdatePosition(gp
.x
, gp
.y
, GetAircraftFlightLevel(v
));
609 if (gp
.x
> (int)(MapSizeX() * TILE_SIZE
+ 9 * TILE_SIZE
) - 1) {
614 if (v
->current_order
.GetDestination() == 0) {
615 Vehicle
*u
= Vehicle::Get(v
->big_ufo_destroyer_target
);
616 if (Delta(v
->x_pos
, u
->x_pos
) > (int)TILE_SIZE
) return true;
617 v
->current_order
.SetDestination(1);
619 CreateEffectVehicleRel(u
, 0, 7, 8, EV_EXPLOSION_LARGE
);
620 if (_settings_client
.sound
.disaster
) SndPlayVehicleFx(SND_12_EXPLOSION
, u
);
624 for (int i
= 0; i
!= 80; i
++) {
626 CreateEffectVehicleAbove(
627 GB(r
, 0, 6) + v
->x_pos
- 32,
628 GB(r
, 5, 6) + v
->y_pos
- 32,
633 for (int dy
= -3; dy
< 3; dy
++) {
634 for (int dx
= -3; dx
< 3; dx
++) {
635 TileIndex tile
= TileAddWrap(v
->tile
, dx
, dy
);
636 if (tile
!= INVALID_TILE
) DisasterClearSquare(tile
);
645 * Submarine, v->current_order.dest states:
646 * Unused, just float around aimlessly and pop up at different places, turning around
648 static bool DisasterTick_Submarine(DisasterVehicle
*v
)
652 if (++v
->age
> 8880) {
657 if (!HasBit(v
->tick_counter
, 0)) return true;
659 TileIndex tile
= v
->tile
+ TileOffsByDiagDir(DirToDiagDir(v
->direction
));
660 if (IsValidTile(tile
)) {
661 TrackBits trackbits
= TrackStatusToTrackBits(GetTileTrackStatus(tile
, TRANSPORT_WATER
, 0));
662 if (trackbits
== TRACK_BIT_ALL
&& !Chance16(1, 90)) {
663 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
664 v
->UpdatePosition(gp
.x
, gp
.y
, v
->z_pos
);
669 v
->direction
= ChangeDir(v
->direction
, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT
: DIRDIFF_90LEFT
);
675 static bool DisasterTick_NULL(DisasterVehicle
*v
)
680 typedef bool DisasterVehicleTickProc(DisasterVehicle
*v
);
682 static DisasterVehicleTickProc
* const _disastervehicle_tick_procs
[] = {
683 DisasterTick_Zeppeliner
, DisasterTick_NULL
,
684 DisasterTick_Ufo
, DisasterTick_NULL
,
685 DisasterTick_Airplane
, DisasterTick_NULL
,
686 DisasterTick_Helicopter
, DisasterTick_NULL
, DisasterTick_Helicopter_Rotors
,
687 DisasterTick_Big_Ufo
, DisasterTick_NULL
, DisasterTick_Big_Ufo_Destroyer
,
689 DisasterTick_Submarine
,
690 DisasterTick_Submarine
,
694 bool DisasterVehicle::Tick()
696 return _disastervehicle_tick_procs
[this->subtype
](this);
699 typedef void DisasterInitProc();
703 * Zeppeliner which crashes on a small airport if one found,
704 * otherwise crashes on a random tile
706 static void Disaster_Zeppeliner_Init()
708 if (!Vehicle::CanAllocateItem(2)) return;
710 /* Pick a random place, unless we find a small airport */
711 int x
= TileX(Random()) * TILE_SIZE
+ TILE_SIZE
/ 2;
714 FOR_ALL_STATIONS(st
) {
715 if (st
->airport
.tile
!= INVALID_TILE
&& (st
->airport
.type
== AT_SMALL
|| st
->airport
.type
== AT_LARGE
)) {
716 x
= (TileX(st
->airport
.tile
) + 2) * TILE_SIZE
;
721 DisasterVehicle
*v
= new DisasterVehicle(x
, 0, DIR_SE
, ST_ZEPPELINER
);
722 /* Allocate shadow */
723 DisasterVehicle
*u
= new DisasterVehicle(x
, 0, DIR_SE
, ST_ZEPPELINER_SHADOW
);
729 * Ufo which flies around aimlessly from the middle of the map a bit
730 * until it locates a road vehicle which it targets and then destroys
732 static void Disaster_Small_Ufo_Init()
734 if (!Vehicle::CanAllocateItem(2)) return;
736 int x
= TileX(Random()) * TILE_SIZE
+ TILE_SIZE
/ 2;
737 DisasterVehicle
*v
= new DisasterVehicle(x
, 0, DIR_SE
, ST_SMALL_UFO
);
738 v
->dest_tile
= TileXY(MapSizeX() / 2, MapSizeY() / 2);
740 /* Allocate shadow */
741 DisasterVehicle
*u
= new DisasterVehicle(x
, 0, DIR_SE
, ST_SMALL_UFO_SHADOW
);
746 /* Combat airplane which destroys an oil refinery */
747 static void Disaster_Airplane_Init()
749 if (!Vehicle::CanAllocateItem(2)) return;
751 Industry
*i
, *found
= nullptr;
753 FOR_ALL_INDUSTRIES(i
) {
754 if ((GetIndustrySpec(i
->type
)->behaviour
& INDUSTRYBEH_AIRPLANE_ATTACKS
) &&
755 (found
== nullptr || Chance16(1, 2))) {
760 if (found
== nullptr) return;
762 /* Start from the bottom (south side) of the map */
763 int x
= (MapSizeX() + 9) * TILE_SIZE
- 1;
764 int y
= TileY(found
->location
.tile
) * TILE_SIZE
+ 37;
766 DisasterVehicle
*v
= new DisasterVehicle(x
, y
, DIR_NE
, ST_AIRPLANE
);
767 DisasterVehicle
*u
= new DisasterVehicle(x
, y
, DIR_NE
, ST_AIRPLANE_SHADOW
);
772 /** Combat helicopter that destroys a factory */
773 static void Disaster_Helicopter_Init()
775 if (!Vehicle::CanAllocateItem(3)) return;
777 Industry
*i
, *found
= nullptr;
779 FOR_ALL_INDUSTRIES(i
) {
780 if ((GetIndustrySpec(i
->type
)->behaviour
& INDUSTRYBEH_CHOPPER_ATTACKS
) &&
781 (found
== nullptr || Chance16(1, 2))) {
786 if (found
== nullptr) return;
788 int x
= -16 * (int)TILE_SIZE
;
789 int y
= TileY(found
->location
.tile
) * TILE_SIZE
+ 37;
791 DisasterVehicle
*v
= new DisasterVehicle(x
, y
, DIR_SW
, ST_HELICOPTER
);
792 DisasterVehicle
*u
= new DisasterVehicle(x
, y
, DIR_SW
, ST_HELICOPTER_SHADOW
);
795 DisasterVehicle
*w
= new DisasterVehicle(x
, y
, DIR_SW
, ST_HELICOPTER_ROTORS
);
800 /* Big Ufo which lands on a piece of rail and will consequently be shot
801 * down by a combat airplane, destroying the surroundings */
802 static void Disaster_Big_Ufo_Init()
804 if (!Vehicle::CanAllocateItem(2)) return;
806 int x
= TileX(Random()) * TILE_SIZE
+ TILE_SIZE
/ 2;
807 int y
= MapMaxX() * TILE_SIZE
- 1;
809 DisasterVehicle
*v
= new DisasterVehicle(x
, y
, DIR_NW
, ST_BIG_UFO
);
810 v
->dest_tile
= TileXY(MapSizeX() / 2, MapSizeY() / 2);
812 /* Allocate shadow */
813 DisasterVehicle
*u
= new DisasterVehicle(x
, y
, DIR_NW
, ST_BIG_UFO_SHADOW
);
818 static void Disaster_Submarine_Init(DisasterSubType subtype
)
820 if (!Vehicle::CanAllocateItem()) return;
825 int x
= TileX(r
) * TILE_SIZE
+ TILE_SIZE
/ 2;
828 y
= MapMaxY() * TILE_SIZE
- TILE_SIZE
/ 2 - 1;
832 if (_settings_game
.construction
.freeform_edges
) y
+= TILE_SIZE
;
835 if (!IsWaterTile(TileVirtXY(x
, y
))) return;
837 new DisasterVehicle(x
, y
, dir
, subtype
);
840 /* Curious submarine #1, just floats around */
841 static void Disaster_Small_Submarine_Init()
843 Disaster_Submarine_Init(ST_SMALL_SUBMARINE
);
847 /* Curious submarine #2, just floats around */
848 static void Disaster_Big_Submarine_Init()
850 Disaster_Submarine_Init(ST_BIG_SUBMARINE
);
855 * Coal mine catastrophe, destroys a stretch of 30 tiles of
856 * land in a certain direction
858 static void Disaster_CoalMine_Init()
860 int index
= GB(Random(), 0, 4);
863 for (m
= 0; m
< 15; m
++) {
866 FOR_ALL_INDUSTRIES(i
) {
867 if ((GetIndustrySpec(i
->type
)->behaviour
& INDUSTRYBEH_CAN_SUBSIDENCE
) && --index
< 0) {
868 SetDParam(0, i
->town
->index
);
869 AddTileNewsItem(STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE
, NT_ACCIDENT
, i
->location
.tile
+ TileDiffXY(1, 1)); // keep the news, even when the mine closes
872 TileIndex tile
= i
->location
.tile
;
873 TileIndexDiff step
= TileOffsByDiagDir((DiagDirection
)GB(Random(), 0, 2));
875 for (uint n
= 0; n
< 30; n
++) {
876 DisasterClearSquare(tile
);
878 if (!IsValidTile(tile
)) break;
888 DisasterInitProc
*init_proc
; ///< The init function for this disaster.
889 Year min_year
; ///< The first year this disaster will occur.
890 Year max_year
; ///< The last year this disaster will occur.
893 static const Disaster _disasters
[] = {
894 {Disaster_Zeppeliner_Init
, 1930, 1955}, // zeppeliner
895 {Disaster_Small_Ufo_Init
, 1940, 1970}, // ufo (small)
896 {Disaster_Airplane_Init
, 1960, 1990}, // airplane
897 {Disaster_Helicopter_Init
, 1970, 2000}, // helicopter
898 {Disaster_Big_Ufo_Init
, 2000, 2100}, // ufo (big)
899 {Disaster_Small_Submarine_Init
, 1940, 1965}, // submarine (small)
900 {Disaster_Big_Submarine_Init
, 1975, 2010}, // submarine (big)
901 {Disaster_CoalMine_Init
, 1950, 1985}, // coalmine
904 static void DoDisaster()
906 byte buf
[lengthof(_disasters
)];
909 for (size_t i
= 0; i
!= lengthof(_disasters
); i
++) {
910 if (_cur_year
>= _disasters
[i
].min_year
&& _cur_year
< _disasters
[i
].max_year
) buf
[j
++] = (byte
)i
;
915 _disasters
[buf
[RandomRange(j
)]].init_proc();
919 static void ResetDisasterDelay()
921 _disaster_delay
= GB(Random(), 0, 9) + 730;
924 void DisasterDailyLoop()
926 if (--_disaster_delay
!= 0) return;
928 ResetDisasterDelay();
930 if (_settings_game
.difficulty
.disasters
!= 0) DoDisaster();
933 void StartupDisasters()
935 ResetDisasterDelay();
939 * Marks all disasters targeting this industry in such a way
940 * they won't call Industry::Get(v->dest_tile) on invalid industry anymore.
941 * @param i deleted industry
943 void ReleaseDisastersTargetingIndustry(IndustryID i
)
946 FOR_ALL_DISASTERVEHICLES(v
) {
947 /* primary disaster vehicles that have chosen target */
948 if (v
->subtype
== ST_AIRPLANE
|| v
->subtype
== ST_HELICOPTER
) {
949 /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
950 if (v
->current_order
.GetDestination() > 0 && v
->dest_tile
== i
) v
->current_order
.SetDestination(3);
956 * Notify disasters that we are about to delete a vehicle. So make them head elsewhere.
957 * @param vehicle deleted vehicle
959 void ReleaseDisastersTargetingVehicle(VehicleID vehicle
)
962 FOR_ALL_DISASTERVEHICLES(v
) {
963 /* primary disaster vehicles that have chosen target */
964 if (v
->subtype
== ST_SMALL_UFO
) {
965 if (v
->current_order
.GetDestination() != 0 && v
->dest_tile
== vehicle
) {
966 /* Revert to target-searching */
967 v
->current_order
.SetDestination(0);
968 v
->dest_tile
= RandomTile();
969 GetAircraftFlightLevelBounds(v
, &v
->z_pos
, nullptr);
976 void DisasterVehicle::UpdateDeltaXY(Direction direction
)