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);
Base functions for all AIs.
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 const int ROTOR_Z_OFFSET
Z Offset between helicopter- and rotorsprite.
Class for backupping variables and making sure they are restored later.
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
#define CLRBITS(x, y)
Clears several bits in a variable.
#define SETBITS(x, y)
Sets several bits in a variable.
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.
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
An interval timer will fire every interval, and will continue to fire until it is deleted.
static Year year
Current year, starting at 0.
Functions related to commands.
@ DC_EXEC
execute the given command
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
@ OWNER_NONE
The tile has no ownership.
@ OWNER_WATER
The tile/execution is done by "water".
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Direction
Defines the 8 directions on the map.
DiagDirection
Enumeration for diagonal directions.
@ DIRDIFF_90LEFT
Angle of 90 degrees left.
@ DIRDIFF_90RIGHT
Angle of 90 degrees right.
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.
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...
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...
static void Disaster_Helicopter_Init()
Combat helicopter that destroys a factory.
static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
Zeppeliner handling, v->state states: 0: Zeppeliner initialization has found a small airport,...
static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
Helicopter rotor blades; keep these spinning.
static bool DisasterTick_Ufo(DisasterVehicle *v)
(Small) Ufo handling, v->state states: 0: Fly around to the middle of the map, then randomly,...
static bool DisasterTick_Airplane(DisasterVehicle *v)
Airplane handling.
static void Disaster_Zeppeliner_Init()
Zeppeliner which crashes on a small airport if one found, otherwise crashes on a random tile.
uint16_t _disaster_delay
Delay counter for considering the next disaster.
void ReleaseDisasterVehicle(VehicleID vehicle)
Notify disasters that we are about to delete a vehicle.
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,...
static bool DisasterTick_Submarine(DisasterVehicle *v)
Submarine, v->state states: Unused, just float around aimlessly and pop up at different places,...
static bool DisasterTick_Helicopter(DisasterVehicle *v)
Helicopter handling.
void ReleaseDisastersTargetingIndustry(IndustryID i)
Marks all disasters targeting this industry in such a way they won't call Industry::Get(v->dest_tile)...
static void Disaster_CoalMine_Init()
Coal mine catastrophe, destroys a stretch of 30 tiles of land in a certain direction.
DisasterSubType
Different sub types of disaster vehicles.
@ ST_ZEPPELINER_SHADOW
Shadow of the zeppelin.
@ ST_HELICOPTER_SHADOW
Shadow of helicopter.
@ ST_SMALL_UFO_SHADOW
Shadow of small UFO.
@ ST_SMALL_UFO
Small UFO, tries to find a road vehicle to destroy.
@ ST_BIG_UFO
Big UFO, finds a piece of railroad to "park" on.
@ ST_BIG_UFO_SHADOW
Shadow of the big UFO.
@ ST_ZEPPELINER
Zeppelin, crashes at airports.
@ ST_BIG_UFO_DESTROYER_SHADOW
Shadow of the aircraft.
@ ST_BIG_UFO_DESTROYER
Aircraft the will bomb the big UFO.
@ ST_BIG_SUBMARINE
Big submarine, pops up in the oceans but doesn't do anything.
@ ST_SMALL_SUBMARINE
Small submarine, pops up in the oceans but doesn't do anything.
@ ST_HELICOPTER
Helicopter destroying a factory.
@ ST_AIRPLANE
Airplane destroying an oil refinery.
@ ST_HELICOPTER_ROTORS
Rotors of helicopter.
@ ST_AIRPLANE_SHADOW
Shadow of airplane.
EffectVehicle * CreateEffectVehicleAbove(int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular location.
EffectVehicle * CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular vehicle.
Functions related to effect vehicles.
@ EV_CRASH_SMOKE
Smoke of disasters.
@ EV_EXPLOSION_SMALL
Various explosions.
@ EV_EXPLOSION_LARGE
Various explosions.
Base functions for all Games.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
void ResetIndustryConstructionStage(Tile tile)
Reset the construction stage counter of the industry, as well as the completion bit.
IndustryID GetIndustryIndex(Tile t)
Get the industry ID of the given tile.
IndustryBehaviour
Various industry behaviours mostly to represent original TTD specialities.
@ INDUSTRYBEH_CAN_SUBSIDENCE
can cause a subsidence (coal mine, shaft that collapses)
@ INDUSTRYBEH_AIRPLANE_ATTACKS
can be exploded by a military airplane (oil refinery)
@ INDUSTRYBEH_CHOPPER_ATTACKS
can be exploded by a military helicopter (factory)
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
Command definitions related to landscape (slopes etc.).
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.
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
#define RandomTile()
Get a valid random tile.
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
static debug_inline TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
int32_t TileIndexDiff
An offset value between two tiles.
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Functions related to news.
@ NT_ACCIDENT
An accident or disaster has occurred.
static debug_inline bool IsRailDepot(Tile t)
Is this rail tile a rail depot?
static debug_inline bool IsPlainRailTile(Tile t)
Checks whether the tile is a rail tile or rail tile with signals.
Pseudo random number generator.
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.
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.
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
ClientSettings _settings_client
The current settings for this game.
static SigSegState UpdateSignalsInBuffer(Owner owner)
Updates blocks in _globset buffer.
Functions related to sound.
@ SND_12_EXPLOSION
16 == 0x10 Destruction, crashes, disasters, ...
Base classes/functions for stations.
bool IsAirportTile(Tile t)
Is this tile a station tile and an airport tile?
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition of base types and functions in a cross-platform compatible way.
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
uint64_t flags
stores which blocks on the airport are taken. was 16 bit earlier on, then 32
Class to backup a specific variable and restore it later.
void Restore()
Restore the variable.
VehicleType type
Type of vehicle.
SoundSettings sound
sound effect settings
static bool IsHumanID(size_t index)
Is this company a company not controlled by a NoAI program?
bool freeform_edges
allow terraforming the tiles at the map edges
bool disasters
are disasters enabled
Disasters, like submarines, skyrangers and their shadows, belong to this class.
void UpdateDeltaXY() override
Updates the x and y offsets and the size of the sprite used for this vehicle.
SpriteID image_override
Override for the default disaster vehicle sprite.
void UpdatePosition(int x, int y, int z)
Update the position of the vehicle.
DisasterVehicle()
For use by saveload.
VehicleID big_ufo_destroyer_target
The big UFO that this destroyer is supposed to bomb.
bool Tick() override
Calls the tick handler of the vehicle.
uint16_t state
Action stage of the disaster vehicle.
DisasterInitProc * init_proc
The init function for this disaster.
TimerGameCalendar::Year min_year
The first year this disaster will occur.
TimerGameCalendar::Year max_year
The last year this disaster will occur.
ConstructionSettings construction
construction of things in-game
DifficultySettings difficulty
settings related to the difficulty
Position information of a vehicle after it moved.
int y
x and y position of the vehicle after moving
IndustryBehaviour behaviour
How this industry will behave, and how others entities can use it.
Defines the internal data of a functional industry.
IndustryType type
type of industry.
TileArea location
Location of the industry.
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
static uint SizeY()
Get the size of the map along the Y.
static debug_inline uint SizeX()
Get the size of the map along the X.
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
static debug_inline uint Size()
Get the size of the map.
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
VehicleSpriteSeq sprite_seq
Vehicle appearance.
TileIndex tile
The base tile of the area.
SpriteID sprite
The 'real' sprite.
Tindex index
Index of this pool item.
static Titem * Get(size_t index)
Returns Titem with given index.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Buses, trucks and trams belong to this class.
uint Crash(bool flooded=false) override
Crash the (whole) vehicle chain.
uint16_t crashed_ctr
Animation counter when the vehicle has crashed.
VehicleID disaster_vehicle
NOSAVE: Disaster vehicle targetting this vehicle.
bool disaster
Play disaster and accident sounds.
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
static Station * GetByTile(TileIndex tile)
Get the station belonging to a specific tile.
Class defining several overloaded accessors so we don't have to cast vehicle types that often.
DisasterVehicle * Next() const
Get next vehicle in the chain.
static Pool::IterateWrapper< T > Iterate(size_t from=0)
Returns an iterable ensemble of all valid vehicles of type T.
static DisasterVehicle * GetIfValid(size_t index)
Returns vehicle if the index is a valid index for this vehicle type.
static T * Get(size_t index)
Gets vehicle with given index.
Airport airport
Tile area the airport covers.
'Train' is either a loco or a wagon.
void Set(SpriteID sprite)
Assign a single sprite to the sequence.
int32_t z_pos
z coordinate.
Direction direction
facing
uint8_t x_extent
x-extent of vehicle bounding box
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
uint8_t breakdown_ctr
Counter for managing breakdown events.
uint8_t breakdown_delay
Counter for managing breakdown length.
uint8_t z_extent
z-extent of vehicle bounding box
int8_t y_offs
y offset for vehicle sprite
debug_inline bool IsFrontEngine() const
Check if the vehicle is a front engine.
int8_t x_offs
x offset for vehicle sprite
int32_t y_pos
y coordinate.
int32_t x_pos
x coordinate.
uint8_t y_extent
y-extent of vehicle bounding box
TimerGameCalendar::Date age
Age in calendar days.
void SetNext(Vehicle *next)
Set the next vehicle of this vehicle.
MutableSpriteCache sprite_cache
Cache of sprites and values related to recalculating them, see MutableSpriteCache.
void UpdatePositionAndViewport()
Update the position of the vehicle, and update the viewport.
uint8_t tick_counter
Increased by one for each tick.
TileIndex tile
Current tile index.
TileIndex dest_tile
Heading for this tile.
void UpdatePosition()
Update the position of the vehicle.
Owner owner
Which company owns the vehicle?
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
bool IsValidTile(Tile tile)
Checks if a tile is valid.
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
static const uint TILE_SIZE
Tile size in world coordinates.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
@ MP_TREES
Tile got trees.
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
@ MP_HOUSE
A house by a town.
@ MP_INDUSTRY
Part of an industry.
Definition of Interval and OneShot timers.
Definition of the game-economy-timer.
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
TrackBits
Allow incrementing of Track variables.
@ TRACK_BIT_ALL
All possible tracks.
Base for the train class.
@ TRANSPORT_WATER
Transport over water.
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
Get position information of a vehicle when moving one pixel in the direction it is facing.
@ VS_UNCLICKABLE
Vehicle is not clickable by the user (shadow vehicles).
@ VS_SHADOW
Vehicle is a shadow vehicle.
@ VS_HIDDEN
Vehicle is not visible.
@ VS_CRASHED
Vehicle is crashed.
Functions related to vehicles.
@ VEH_ROAD
Road vehicle type.
uint32_t VehicleID
The type all our vehicle IDs have.
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
Functions related to (drawing on) viewports.
bool IsWaterTile(Tile t)
Is it a water tile with plain water?