OpenTTD Source 15.0-beta2
newgrf_airporttiles.cpp
Go to the documentation of this file.
1/*
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/>.
6 */
7
10#include "stdafx.h"
11#include "debug.h"
12#include "newgrf_airporttiles.h"
13#include "newgrf_badge.h"
14#include "newgrf_spritegroup.h"
15#include "newgrf_sound.h"
16#include "station_base.h"
17#include "water.h"
18#include "landscape.h"
19#include "company_base.h"
20#include "town.h"
22
23#include "table/strings.h"
24#include "table/airporttiles.h"
25
26#include "safeguards.h"
27
28extern uint32_t GetRelativePosition(TileIndex tile, TileIndex ind_tile);
29
30AirportTileSpec AirportTileSpec::tiles[NUM_AIRPORTTILES];
31
33
40{
41 /* should be assert(gfx < lengthof(tiles)), but that gives compiler warnings
42 * since it's always true if the following holds: */
43 static_assert(std::numeric_limits<StationGfx>::max() + 1 == lengthof(tiles));
44 return &AirportTileSpec::tiles[gfx];
45}
46
56
61{
62 auto insert = std::copy(std::begin(_origin_airporttile_specs), std::end(_origin_airporttile_specs), std::begin(AirportTileSpec::tiles));
63 std::fill(insert, std::end(AirportTileSpec::tiles), AirportTileSpec{});
64
65 /* Reset any overrides that have been set. */
66 _airporttile_mngr.ResetOverride();
67}
68
69void AirportTileOverrideManager::SetEntitySpec(const AirportTileSpec *airpts)
70{
71 StationGfx airpt_id = this->AddEntityID(airpts->grf_prop.local_id, airpts->grf_prop.grfid, airpts->grf_prop.subst_id);
72
73 if (airpt_id == this->invalid_id) {
74 GrfMsg(1, "AirportTile.SetEntitySpec: Too many airport tiles allocated. Ignoring.");
75 return;
76 }
77
78 AirportTileSpec::tiles[airpt_id] = *airpts;
79
80 /* Now add the overrides. */
81 for (int i = 0; i < this->max_offset; i++) {
82 AirportTileSpec *overridden_airpts = &AirportTileSpec::tiles[i];
83
84 if (this->entity_overrides[i] != airpts->grf_prop.local_id || this->grfid_overrides[i] != airpts->grf_prop.grfid) continue;
85
86 overridden_airpts->grf_prop.override_id = airpt_id;
87 overridden_airpts->enabled = false;
88 this->entity_overrides[i] = this->invalid_id;
89 this->grfid_overrides[i] = 0;
90 }
91}
92
103
112static uint32_t GetNearbyAirportTileInformation(uint8_t parameter, TileIndex tile, StationID index, bool grf_version8)
113{
114 if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
115 bool is_same_airport = (IsTileType(tile, MP_STATION) && IsAirport(tile) && GetStationIndex(tile) == index);
116
117 return GetNearbyTileInformation(tile, grf_version8) | (is_same_airport ? 1 : 0) << 8;
118}
119
120
129static uint32_t GetAirportTileIDAtOffset(TileIndex tile, const Station *st, uint32_t cur_grfid)
130{
131 if (!st->TileBelongsToAirport(tile)) {
132 return 0xFFFF;
133 }
134
135 StationGfx gfx = GetAirportGfx(tile);
136 const AirportTileSpec *ats = AirportTileSpec::Get(gfx);
137
138 if (gfx < NEW_AIRPORTTILE_OFFSET) { // Does it belongs to an old type?
139 /* It is an old tile. We have to see if it's been overridden */
140 if (ats->grf_prop.override_id == INVALID_AIRPORTTILE) { // has it been overridden?
141 return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
142 }
143 /* Overridden */
145
146 if (tile_ovr->grf_prop.grfid == cur_grfid) {
147 return tile_ovr->grf_prop.local_id; // same grf file
148 } else {
149 return 0xFFFE; // not the same grf file
150 }
151 }
152 /* Not an 'old type' tile */
153 if (ats->grf_prop.GetSpriteGroup() != nullptr) { // tile has a spritegroup ?
154 if (ats->grf_prop.grfid == cur_grfid) { // same airport, same grf ?
155 return ats->grf_prop.local_id;
156 } else {
157 return 0xFFFE; // Defined in another grf file
158 }
159 }
160 /* The tile has no spritegroup */
161 return 0xFF << 8 | ats->grf_prop.subst_id; // so just give it the substitute
162}
163
164/* virtual */ uint32_t AirportTileScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
165{
166 assert(this->st != nullptr);
167
168 switch (variable) {
169 /* Terrain type */
170 case 0x41: return GetTerrainType(this->tile);
171
172 /* Current town zone of the tile in the nearest town */
173 case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile, UINT_MAX), this->tile);
174
175 /* Position relative to most northern airport tile. */
176 case 0x43: return GetRelativePosition(this->tile, this->st->airport.tile);
177
178 /* Animation frame of tile */
179 case 0x44: return GetAnimationFrame(this->tile);
180
181 /* Land info of nearby tiles */
182 case 0x60: return GetNearbyAirportTileInformation(parameter, this->tile, this->st->index, this->ro.grffile->grf_version >= 8);
183
184 /* Animation stage of nearby tiles */
185 case 0x61: {
186 TileIndex tile = GetNearbyTile(parameter, this->tile);
187 if (this->st->TileBelongsToAirport(tile)) {
188 return GetAnimationFrame(tile);
189 }
190 return UINT_MAX;
191 }
192
193 /* Get airport tile ID at offset */
194 case 0x62: return GetAirportTileIDAtOffset(GetNearbyTile(parameter, this->tile), this->st, this->ro.grffile->grfid);
195
196 case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, this->ats->badges, parameter);
197 }
198
199 Debug(grf, 1, "Unhandled airport tile variable 0x{:X}", variable);
200
201 available = false;
202 return UINT_MAX;
203}
204
205/* virtual */ uint32_t AirportTileScopeResolver::GetRandomBits() const
206{
207 return (this->st == nullptr ? 0 : this->st->random_bits) | (this->tile == INVALID_TILE ? 0 : GetStationTileRandomBits(this->tile) << 16);
208}
209
220 CallbackID callback, uint32_t callback_param1, uint32_t callback_param2)
221 : ResolverObject(ats->grf_prop.grffile, callback, callback_param1, callback_param2),
222 tiles_scope(*this, ats, tile, st),
223 airport_scope(*this, tile, st, st != nullptr ? AirportSpec::Get(st->airport.type) : nullptr, st != nullptr ? st->airport.layout : 0)
224{
226}
227
229{
230 return GSF_AIRPORTTILES;
231}
232
234{
235 return this->tiles_scope.ats->grf_prop.local_id;
236}
237
238uint16_t GetAirportTileCallback(CallbackID callback, uint32_t param1, uint32_t param2, const AirportTileSpec *ats, Station *st, TileIndex tile, [[maybe_unused]] int extra_data = 0)
239{
240 AirportTileResolverObject object(ats, tile, st, callback, param1, param2);
241 return object.ResolveCallback();
242}
243
244static void AirportDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, uint8_t colour)
245{
246 const DrawTileSprites *dts = group->ProcessRegisters(nullptr);
247
248 SpriteID image = dts->ground.sprite;
249 SpriteID pal = dts->ground.pal;
250
251 if (GB(image, 0, SPRITE_WIDTH) != 0) {
252 if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
253 DrawWaterClassGround(ti);
254 } else {
255 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(colour)));
256 }
257 }
258
259 DrawNewGRFTileSeq(ti, dts, TO_BUILDINGS, 0, GENERAL_SPRITE_COLOUR(colour));
260}
261
262bool DrawNewAirportTile(TileInfo *ti, Station *st, const AirportTileSpec *airts)
263{
264 if (ti->tileh != SLOPE_FLAT) {
265 bool draw_old_one = true;
267 /* Called to determine the type (if any) of foundation to draw */
268 uint32_t callback_res = GetAirportTileCallback(CBID_AIRPTILE_DRAW_FOUNDATIONS, 0, 0, airts, st, ti->tile);
269 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(airts->grf_prop.grffile, CBID_AIRPTILE_DRAW_FOUNDATIONS, callback_res);
270 }
271
272 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
273 }
274
275 AirportTileResolverObject object(airts, ti->tile, st);
276 const SpriteGroup *group = object.Resolve();
277 if (group == nullptr || group->type != SGT_TILELAYOUT) {
278 return false;
279 }
280
281 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
282 AirportDrawTileLayout(ti, tlgroup, Company::Get(st->owner)->colour);
283 return true;
284}
285
287struct AirportTileAnimationBase : public AnimationBase<AirportTileAnimationBase, AirportTileSpec, Station, int, GetAirportTileCallback, TileAnimationFrameAnimationHelper<Station> > {
288 static constexpr CallbackID cb_animation_speed = CBID_AIRPTILE_ANIMATION_SPEED;
289 static constexpr CallbackID cb_animation_next_frame = CBID_AIRPTILE_ANIM_NEXT_FRAME;
290
291 static constexpr AirportTileCallbackMask cbm_animation_speed = AirportTileCallbackMask::AnimationSpeed;
292 static constexpr AirportTileCallbackMask cbm_animation_next_frame = AirportTileCallbackMask::AnimationNextFrame;
293};
294
295void AnimateAirportTile(TileIndex tile)
296{
298 if (ats == nullptr) return;
299
301}
302
303void AirportTileAnimationTrigger(Station *st, TileIndex tile, AirpAnimationTrigger trigger, CargoType cargo_type)
304{
306 if (!HasBit(ats->animation.triggers, trigger)) return;
307
308 AirportTileAnimationBase::ChangeAnimationFrame(CBID_AIRPTILE_ANIM_START_STOP, ats, st, tile, Random(), (uint8_t)trigger | (cargo_type << 8));
309}
310
311void AirportAnimationTrigger(Station *st, AirpAnimationTrigger trigger, CargoType cargo_type)
312{
313 if (st->airport.tile == INVALID_TILE) return;
314
315 for (TileIndex tile : st->airport) {
316 if (st->TileBelongsToAirport(tile)) AirportTileAnimationTrigger(st, tile, trigger, cargo_type);
317 }
318}
319
static const uint INVALID_AIRPORTTILE
id for an invalid airport tile
Definition airport.h:25
static const uint NEW_AIRPORTTILE_OFFSET
offset of first newgrf airport tile
Definition airport.h:24
static const uint NUM_AIRPORTTILES
Total number of airport tiles.
Definition airport.h:23
Tables with airporttile defaults.
static const AirportTileSpec _origin_airporttile_specs[]
All default airport tiles.
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
uint16_t invalid_id
ID used to detected invalid entities.
void ResetOverride()
Resets the override, which is used while initializing game.
uint16_t max_offset
what is the length of the original entity's array of specs
virtual uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id)
Reserves a place in the mapping array for an entity to be installed.
Definition of stuff that is very close to a company, like the company struct itself.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
void DrawFoundation(TileInfo *ti, Foundation f)
Draw foundation f at tile ti.
Functions related to OTTD's landscape.
@ Random
Randomise borders.
GrfSpecFeature
Definition newgrf.h:70
uint8_t StationGfx
Copy from station_map.h.
StationGfx GetTranslatedAirportTileID(StationGfx gfx)
Do airporttile gfx ID translation for NewGRFs.
uint32_t GetRelativePosition(TileIndex tile, TileIndex ind_tile)
This is the position of the tile relative to the northernmost tile of the industry.
static uint32_t GetAirportTileIDAtOffset(TileIndex tile, const Station *st, uint32_t cur_grfid)
Make an analysis of a tile and check whether it belongs to the same airport, and/or the same grf file...
static uint32_t GetNearbyAirportTileInformation(uint8_t parameter, TileIndex tile, StationID index, bool grf_version8)
Based on newhouses/newindustries equivalent, but adapted for airports.
NewGRF handling of airport tiles.
Function implementations related to NewGRF animation.
AirpAnimationTrigger
Animation triggers for airport tiles.
uint32_t GetBadgeVariableResult(const GRFFile &grffile, std::span< const BadgeID > badges, uint32_t parameter)
Test for a matching badge in a list of badges, returning the number of matching bits.
Functions related to NewGRF badges.
AirportTileCallbackMask
Callback masks for airport tiles.
@ DrawFoundations
decides if default foundations need to be drawn
@ AnimationNextFrame
decides next animation frame
@ AnimationSpeed
decides animation speed
CallbackID
List of implemented NewGRF callbacks.
@ CBID_AIRPTILE_DRAW_FOUNDATIONS
Called to determine the type (if any) of foundation to draw for an airport tile.
@ CBID_AIRPTILE_ANIM_START_STOP
Called for periodically starting or stopping the animation.
@ CBID_AIRPTILE_ANIMATION_SPEED
Called to indicate how long the current animation frame should last.
@ CBID_AIRPTILE_ANIM_NEXT_FRAME
Called to determine airport tile next animation frame.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
uint32_t GetNearbyTileInformation(TileIndex tile, bool grf_version8)
Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62.
bool ConvertBooleanCallback(const GRFFile *grffile, uint16_t cbid, uint16_t cb_res)
Converts a callback result into a boolean.
uint32_t GetTerrainType(TileIndex tile, TileContext context)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
TileIndex GetNearbyTile(uint8_t parameter, TileIndex tile, bool signed_offsets, Axis axis)
Get the tile at the given offset.
Functions related to NewGRF provided sounds.
Action 2 handling.
A number of safeguards to prevent using unsafe methods.
@ SLOPE_FLAT
a flat tile
Definition slope_type.h:49
@ FOUNDATION_LEVELED
The tile is leveled up to a flat slope.
Definition slope_type.h:95
void DrawNewGRFTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, uint32_t stage, PaletteID default_palette)
Draw NewGRF industrytile or house sprite layout.
Definition sprite.h:130
PaletteID GroundSpritePaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
Applies PALETTE_MODIFIER_COLOUR to a palette entry of a ground sprite.
Definition sprite.h:174
static constexpr uint8_t SPRITE_WIDTH
number of bits for the sprite number
Definition sprites.h:1539
Base classes/functions for stations.
bool IsAirport(Tile t)
Is this station tile an airport?
uint8_t GetStationTileRandomBits(Tile t)
Get the random bits of a station tile.
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
StationGfx GetAirportGfx(Tile t)
Get the station graphics of this airport tile.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
Defines the data structure for an airport.
Helper class for animation control.
Resolver for tiles of an airport.
AirportTileScopeResolver tiles_scope
Scope resolver for the tiles.
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
AirportTileResolverObject(const AirportTileSpec *ats, TileIndex tile, Station *st, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Constructor of the resolver for airport tiles.
struct Station * st
Station of the airport for which the callback is run, or nullptr for build gui.
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
TileIndex tile
Tile for the callback, only valid for airporttile callbacks.
uint32_t GetRandomBits() const override
Get a few random bits.
Defines the data structure of each individual tile of an airport.
bool enabled
entity still available (by default true). newgrf can disable it, though
static const AirportTileSpec * Get(StationGfx gfx)
Retrieve airport tile spec for the given airport tile.
AnimationInfo animation
Information about the animation.
static const AirportTileSpec * GetByTile(TileIndex tile)
Retrieve airport tile spec for the given airport tile.
GRFFileProps grf_prop
properties related the the grf file
static void ResetAirportTiles()
This function initializes the tile array of AirportTileSpec.
uint8_t animation_special_flags
Extra flags to influence the animation.
AirportTileCallbackMasks callback_mask
Bitmask telling which grf callback is set.
Helper class for a unified approach to NewGRF animation.
static void AnimateTile(const AirportTileSpec *spec, Station *obj, TileIndex tile, bool random_animation, int extra_data=0)
Animate a single tile.
static void ChangeAnimationFrame(CallbackID cb, const AirportTileSpec *spec, Station *obj, TileIndex tile, uint32_t random_bits, uint32_t trigger, int extra_data=0)
Check a callback to determine what the next animation step is and execute that step.
uint16_t triggers
The triggers that trigger animation.
Owner owner
The owner of this station.
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
uint16_t random_bits
Random bits assigned to this station.
Ground palette sprite of a tile, together with its sprite layout.
Definition sprite.h:46
PalSpriteID ground
Palette and sprite for the ground.
Definition sprite.h:47
const struct SpriteGroup * GetSpriteGroup(size_t index=0) const
Get the SpriteGroup at the specified index.
const struct GRFFile * grffile
grf file that introduced this entity
uint16_t local_id
id defined by the grf file for this entity
uint32_t grfid
grfid that introduced this entity.
uint16_t override_id
id of the entity been replaced by
TileIndex tile
The base tile of the area.
SpriteID sprite
The 'real' sprite.
Definition gfx_type.h:23
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition gfx_type.h:24
static Titem * Get(auto index)
Returns Titem with given index.
Tindex index
Index of this pool item.
Interface for SpriteGroup-s to access the gamestate.
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
ResolverObject & ro
Surrounding resolver object.
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Station data structure.
Airport airport
Tile area the airport covers.
Tile information, used while rendering the tile.
Definition tile_cmd.h:43
Slope tileh
Slope of the tile.
Definition tile_cmd.h:46
TileIndex tile
Tile index.
Definition tile_cmd.h:47
Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
const DrawTileSprites * ProcessRegisters(uint8_t *stage) const
Process registers and the construction stage into the sprite layout.
uint8_t GetAnimationFrame(Tile t)
Get the current animation frame.
Definition tile_map.h:250
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
Base of the town class.
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
@ TO_BUILDINGS
company buildings - depots, stations, HQ, ...
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite for the current tile.
Definition viewport.cpp:579
Functions related to water (management)
bool IsTileOnWater(Tile t)
Tests if the tile was built on water.
Definition water_map.h:136