OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
52 #include "table/strings.h"
59 static void DisasterClearSquare(
TileIndex tile)
92 static const SpriteID _disaster_images_1[] = {SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP};
93 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};
94 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};
95 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};
96 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};
97 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};
98 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};
99 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};
100 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};
102 static const SpriteID *
const _disaster_images[] = {
103 _disaster_images_1, _disaster_images_1,
104 _disaster_images_2, _disaster_images_2,
105 _disaster_images_3, _disaster_images_3,
106 _disaster_images_8, _disaster_images_8, _disaster_images_9,
107 _disaster_images_6, _disaster_images_6,
108 _disaster_images_7, _disaster_images_7,
109 _disaster_images_4, _disaster_images_5,
112 void DisasterVehicle::UpdateImage()
207 if ((u = u->
Next()) !=
nullptr) {
236 if (++v->
age == 38) {
243 }
else if (v->
state == 0) {
263 if (++v->
age <= 13320)
return true;
279 if (z < v->z_pos) z = v->
z_pos - 1;
286 }
else if (v->
age == 70) {
288 }
else if (v->
age <= 300) {
290 uint32_t r = Random();
298 }
else if (v->
age == 350) {
325 v->
direction = GetDirectionTowards(v, x, y);
338 n += c->group_all[
VEH_ROAD].num_vehicle;
350 if (u->IsFrontEngine() && (n-- == 0)) {
359 u->disaster_vehicle = v->
index;
381 if (dist <= TILE_SIZE && z > u->
z_pos) z--;
387 uint victims = u->
Crash();
393 Game::NewEvent(
new ScriptEventVehicleCrashed(u->
index, u->
tile, ScriptEventVehicleCrashed::CRASH_RV_UFO, victims));
409 static void DestructIndustry(
Industry *i)
450 uint32_t r = Random();
460 }
else if (v->
state == 1) {
461 if (++v->
age == 112) {
472 }
else if (v->
state == 0) {
512 if (++cur_image > SPR_ROTOR_MOVING_3) cur_image = SPR_ROTOR_MOVING_1;
533 v->
direction = GetDirectionTowards(v, x, y);
555 if (target->IsGroundVehicle()) {
557 target->breakdown_ctr = 5;
558 target->breakdown_delay = 0xF0;
574 }
else if (v->
state == 0) {
578 v->
direction = GetDirectionTowards(v, x, y);
590 const auto is_valid_target = [](
const Train *t) {
591 return t->IsFrontEngine()
599 if (is_valid_target(t)) n++;
611 if (is_valid_target(t) && (n-- == 0)) {
649 for (
int i = 0; i != 80; i++) {
650 uint32_t r = Random();
658 for (
int dy = -3; dy < 3; dy++) {
659 for (
int dx = -3; dx < 3; dx++) {
677 if (++v->
age > 8880) {
707 static DisasterVehicleTickProc *
const _disastervehicle_tick_procs[] = {
721 return _disastervehicle_tick_procs[this->
subtype](
this);
724 typedef void DisasterInitProc();
771 static void Disaster_Airplane_Init()
779 (found ==
nullptr ||
Chance16(1, 2))) {
784 if (found ==
nullptr)
return;
805 (found ==
nullptr ||
Chance16(1, 2))) {
810 if (found ==
nullptr)
return;
826 static void Disaster_Big_Ufo_Init()
848 uint32_t r = Random();
865 static void Disaster_Small_Submarine_Init()
872 static void Disaster_Big_Submarine_Init()
884 int index =
GB(Random(), 0, 4);
887 for (m = 0; m < 15; m++) {
897 for (uint n = 0; n < 30; n++) {
898 DisasterClearSquare(tile);
915 static const Disaster _disasters[] = {
918 {Disaster_Airplane_Init, 1960, 1990},
920 {Disaster_Big_Ufo_Init, 2000, 2100},
921 {Disaster_Small_Submarine_Init, 1940, 1965},
922 {Disaster_Big_Submarine_Init, 1975, 2010},
926 static void DoDisaster()
928 std::vector<DisasterInitProc *> available_disasters;
930 for (
auto &disaster : _disasters) {
932 available_disasters.push_back(disaster.init_proc);
936 if (available_disasters.empty())
return;
938 available_disasters[
RandomRange(
static_cast<uint32_t
>(available_disasters.size()))]();
942 static void ResetDisasterDelay()
951 ResetDisasterDelay();
956 void StartupDisasters()
958 ResetDisasterDelay();
984 if (v ==
nullptr)
return;
988 assert(v->
state != 0);
SpriteID image_override
Override for the default disaster vehicle sprite.
Buses, trucks and trams belong to this class.
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
debug_inline bool IsFrontEngine() const
Check if the vehicle is a front engine.
@ MP_HOUSE
A house by a town.
static bool DisasterTick_Airplane(DisasterVehicle *v)
Airplane handling.
static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
Zeppeliner handling, v->state states: 0: Zeppeliner initialization has found a small airport,...
uint64_t flags
stores which blocks on the airport are taken. was 16 bit earlier on, then 32
static bool DisasterTick_Submarine(DisasterVehicle *v)
Submarine, v->state states: Unused, just float around aimlessly and pop up at different places,...
static Titem * Get(size_t index)
Returns Titem with given index.
VehicleSpriteSeq sprite_seq
Vehicle appearance.
static SigSegState UpdateSignalsInBuffer(Owner owner)
Updates blocks in _globset buffer.
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
Class to backup a specific variable and restore it later.
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
bool Tick() override
Calls the tick handler of the vehicle.
DisasterVehicle * Next() const
Get next vehicle in the chain.
VehicleID big_ufo_destroyer_target
The big UFO that this destroyer is supposed to bomb.
@ TRANSPORT_WATER
Transport over water.
@ NT_ACCIDENT
An accident or disaster has occurred.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
void Set(SpriteID sprite)
Assign a single sprite to the sequence.
uint8_t y_extent
y-extent of vehicle bounding box
@ INDUSTRYBEH_CAN_SUBSIDENCE
can cause a subsidence (coal mine, shaft that collapses)
An interval timer will fire every interval, and will continue to fire until it is deleted.
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Tindex index
Index of this pool item.
void SetNext(Vehicle *next)
Set the next vehicle of this vehicle.
SpriteID sprite
The 'real' sprite.
@ ST_SMALL_UFO_SHADOW
Shadow of small UFO.
DifficultySettings difficulty
settings related to the difficulty
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
@ SND_12_EXPLOSION
16 == 0x10 Destruction, crashes, disasters, ...
@ VEH_ROAD
Road vehicle type.
static const uint TILE_SIZE
Tile size in world coordinates.
DiagDirection
Enumeration for diagonal directions.
void UpdatePosition(int x, int y, int z)
Update the position of the vehicle.
ClientSettings _settings_client
The current settings for this game.
@ MP_INDUSTRY
Part of an industry.
static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle *v)
Skyranger destroying (Big) Ufo handling, v->state states: 0: Home in on landed Ufo and shoot it down.
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
@ EV_EXPLOSION_SMALL
Various explosions.
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
DisasterSubType
Different sub types of disaster vehicles.
Defines the internal data of a functional industry.
Owner owner
Which company owns the vehicle?
@ EV_EXPLOSION_LARGE
Various explosions.
@ DC_EXEC
execute the given command
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
@ ST_BIG_SUBMARINE
Big submarine, pops up in the oceans but doesn't do anything.
bool Chance16(const uint32_t a, const uint32_t b, const std::source_location location=std::source_location::current())
Flips a coin with given probability.
static void Disaster_Zeppeliner_Init()
Zeppeliner which crashes on a small airport if one found, otherwise crashes on a random tile.
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
#define SETBITS(x, y)
Sets several bits in a variable.
int32_t x_pos
x coordinate.
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
static void Disaster_CoalMine_Init()
Coal mine catastrophe, destroys a stretch of 30 tiles of land in a certain direction.
bool disasters
are disasters enabled
@ INDUSTRYBEH_AIRPLANE_ATTACKS
can be exploded by a military airplane (oil refinery)
@ VS_HIDDEN
Vehicle is not visible.
static debug_inline bool IsRailDepot(Tile t)
Is this rail tile a rail depot?
@ ST_AIRPLANE
Airplane destroying an oil refinery.
void UpdateDeltaXY() override
Updates the x and y offsets and the size of the sprite used for this vehicle.
TileIndex dest_tile
Heading for this tile.
VehicleID disaster_vehicle
NOSAVE: Disaster vehicle targetting this vehicle.
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
This function checks if we add addx/addy to tile, if we do wrap around the edges.
TileArea location
Location of the industry.
@ ST_ZEPPELINER
Zeppelin, crashes at airports.
SoundSettings sound
sound effect settings
@ OWNER_NONE
The tile has no ownership.
@ DIRDIFF_90LEFT
Angle of 90 degrees left.
TrackBits
Allow incrementing of Track variables.
IndustryType type
type of industry.
TileIndex tile
Current tile index.
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
@ VS_CRASHED
Vehicle is crashed.
@ ST_BIG_UFO_SHADOW
Shadow of the big UFO.
static void Disaster_Helicopter_Init()
Combat helicopter that destroys a factory.
Class defining several overloaded accessors so we don't have to cast vehicle types that often.
Airport airport
Tile area the airport covers.
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static void Disaster_Small_Ufo_Init()
Ufo which flies around aimlessly from the middle of the map a bit until it locates a road vehicle whi...
void GetAircraftFlightLevelBounds(const Vehicle *v, int *min, int *max)
Get the 'flight level' bounds, in pixels from 'z_pos' 0 for a particular vehicle for normal flight si...
static RoadVehicle * Get(size_t index)
Gets vehicle with given index.
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
uint Crash(bool flooded=false) override
Crash the (whole) vehicle chain.
TimerGameCalendar::Year min_year
The first year this disaster will occur.
IndustryID GetIndustryIndex(Tile t)
Get the industry ID of the given tile.
@ EV_CRASH_SMOKE
Smoke of disasters.
'Train' is either a loco or a wagon.
bool freeform_edges
allow terraforming the tiles at the map edges
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
#define RandomTile()
Get a valid random tile.
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
uint32_t VehicleID
The type all our vehicle IDs have.
bool disaster
Play disaster and accident sounds.
int32_t TileIndexDiff
An offset value between two tiles.
EffectVehicle * CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular vehicle.
TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
static bool IsHumanID(size_t index)
Is this company a company not controlled by a NoAI program?
void ResetIndustryConstructionStage(Tile tile)
Reset the construction stage counter of the industry, as well as the completion bit.
MutableSpriteCache sprite_cache
Cache of sprites and values related to recalculating them, see MutableSpriteCache.
void ReleaseDisastersTargetingIndustry(IndustryID i)
Marks all disasters targeting this industry in such a way they won't call Industry::Get(v->dest_tile)...
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Disasters, like submarines, skyrangers and their shadows, belong to this class.
static debug_inline bool IsPlainRailTile(Tile t)
Checks whether the tile is a rail tile or rail tile with signals.
int32_t z_pos
z coordinate.
@ TRACK_BIT_ALL
All possible tracks.
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Direction direction
facing
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
@ MP_TREES
Tile got trees.
static debug_inline uint SizeX()
Get the size of the map along the X.
uint8_t tick_counter
Increased by one for each tick.
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
@ ST_HELICOPTER_SHADOW
Shadow of helicopter.
@ ST_SMALL_SUBMARINE
Small submarine, pops up in the oceans but doesn't do anything.
static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
(Big) Ufo handling, v->state states: 0: Fly around to the middle of the map, then randomly for a whil...
@ ST_BIG_UFO_DESTROYER_SHADOW
Shadow of the aircraft.
CompanyID _current_company
Company currently doing an action.
uint8_t breakdown_delay
Counter for managing breakdown length.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
Helicopter rotor blades; keep these spinning.
@ ST_AIRPLANE_SHADOW
Shadow of airplane.
static bool DisasterTick_Ufo(DisasterVehicle *v)
(Small) Ufo handling, v->state states: 0: Fly around to the middle of the map, then randomly,...
void Restore()
Restore the variable.
int8_t x_offs
x offset for vehicle sprite
static debug_inline uint Size()
Get the size of the map.
@ ST_HELICOPTER
Helicopter destroying a factory.
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
StationID GetStationIndex(Tile t)
Get StationID from a tile.
TileIndex tile
The base tile of the area.
@ OWNER_WATER
The tile/execution is done by "water".
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Position information of a vehicle after it moved.
TimerGameCalendar::Date age
Age in calendar days.
static Station * GetByTile(TileIndex tile)
Get the station belonging to a specific tile.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Direction
Defines the 8 directions on the map.
@ DIRDIFF_90RIGHT
Angle of 90 degrees right.
static DisasterVehicle * GetIfValid(size_t index)
Returns vehicle if the index is a valid index for this vehicle type.
EffectVehicle * CreateEffectVehicleAbove(int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular location.
static Pool::IterateWrapper< RoadVehicle > Iterate(size_t from=0)
Returns an iterable ensemble of all valid vehicles of type T.
int32_t y_pos
y coordinate.
uint32_t RandomRange(uint32_t limit, const std::source_location location=std::source_location::current())
Pick a random number between 0 and limit - 1, inclusive.
@ VS_UNCLICKABLE
Vehicle is not clickable by the user (shadow vehicles).
IndustryBehaviour behaviour
How this industry will behave, and how others entities can use it.
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
@ ST_HELICOPTER_ROTORS
Rotors of helicopter.
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
uint8_t x_extent
x-extent of vehicle bounding box
static debug_inline TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
@ INDUSTRYBEH_CHOPPER_ATTACKS
can be exploded by a military helicopter (factory)
uint16_t crashed_ctr
Animation counter when the vehicle has crashed.
ConstructionSettings construction
construction of things in-game
bool IsAirportTile(Tile t)
Is this tile a station tile and an airport tile?
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
@ ST_BIG_UFO_DESTROYER
Aircraft the will bomb the big UFO.
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
IndustryBehaviour
Various industry behaviours mostly to represent original TTD specialities.
VehicleType type
Type of vehicle.
@ ST_BIG_UFO
Big UFO, finds a piece of railroad to "park" on.
void ReleaseDisasterVehicle(VehicleID vehicle)
Notify disasters that we are about to delete a vehicle.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
static bool DisasterTick_Aircraft(DisasterVehicle *v, uint16_t image_override, bool leave_at_top, StringID news_message, IndustryBehaviour industry_flag)
Aircraft handling, v->state states: 0: Fly towards the targeted industry 1: If within 15 tiles,...
void UpdatePositionAndViewport()
Update the position of the vehicle, and update the viewport.
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
void UpdatePosition()
Update the position of the vehicle.
int y
x and y position of the vehicle after moving
@ ST_ZEPPELINER_SHADOW
Shadow of the zeppelin.
uint16_t _disaster_delay
Delay counter for considering the next disaster.
uint16_t state
Action stage of the disaster vehicle.
DisasterVehicle()
For use by saveload.
static bool DisasterTick_Helicopter(DisasterVehicle *v)
Helicopter handling.
#define CLRBITS(x, y)
Clears several bits in a variable.
DisasterInitProc * init_proc
The init function for this disaster.
@ VS_SHADOW
Vehicle is a shadow vehicle.
@ ST_SMALL_UFO
Small UFO, tries to find a road vehicle to destroy.
int8_t y_offs
y offset for vehicle sprite
static uint SizeY()
Get the size of the map along the Y.
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
Get position information of a vehicle when moving one pixel in the direction it is facing.
uint8_t z_extent
z-extent of vehicle bounding box
TimerGameCalendar::Year max_year
The last year this disaster will occur.
static const int ROTOR_Z_OFFSET
Z Offset between helicopter- and rotorsprite.
static Year year
Current year, starting at 0.
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
uint8_t breakdown_ctr
Counter for managing breakdown events.
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.