Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / newgrf_airporttiles.cpp
blob5204d7b55b8e9bc7ffcf88260d0fa0da75f47546
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 */
8 /** @file newgrf_airporttiles.cpp NewGRF handling of airport tiles. */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "newgrf_airporttiles.h"
13 #include "newgrf_spritegroup.h"
14 #include "newgrf_sound.h"
15 #include "station_base.h"
16 #include "water.h"
17 #include "landscape.h"
18 #include "company_base.h"
19 #include "town.h"
20 #include "table/strings.h"
21 #include "table/airporttiles.h"
22 #include "newgrf_animation_base.h"
24 #include "safeguards.h"
26 extern uint32_t GetRelativePosition(TileIndex tile, TileIndex ind_tile);
28 AirportTileSpec AirportTileSpec::tiles[NUM_AIRPORTTILES];
30 AirportTileOverrideManager _airporttile_mngr(NEW_AIRPORTTILE_OFFSET, NUM_AIRPORTTILES, INVALID_AIRPORTTILE);
32 /**
33 * Retrieve airport tile spec for the given airport tile
34 * @param gfx index of airport tile
35 * @return A pointer to the corresponding AirportTileSpec
37 /* static */ const AirportTileSpec *AirportTileSpec::Get(StationGfx gfx)
39 /* should be assert(gfx < lengthof(tiles)), but that gives compiler warnings
40 * since it's always true if the following holds: */
41 static_assert(MAX_UVALUE(StationGfx) + 1 == lengthof(tiles));
42 return &AirportTileSpec::tiles[gfx];
45 /**
46 * Retrieve airport tile spec for the given airport tile.
47 * @param tile The airport tile.
48 * @return A pointer to the corresponding AirportTileSpec.
50 /* static */ const AirportTileSpec *AirportTileSpec::GetByTile(TileIndex tile)
52 return AirportTileSpec::Get(GetAirportGfx(tile));
55 /**
56 * This function initializes the tile array of AirportTileSpec
58 void AirportTileSpec::ResetAirportTiles()
60 auto insert = std::copy(std::begin(_origin_airporttile_specs), std::end(_origin_airporttile_specs), std::begin(AirportTileSpec::tiles));
61 std::fill(insert, std::end(AirportTileSpec::tiles), AirportTileSpec{});
63 /* Reset any overrides that have been set. */
64 _airporttile_mngr.ResetOverride();
67 void AirportTileOverrideManager::SetEntitySpec(const AirportTileSpec *airpts)
69 StationGfx airpt_id = this->AddEntityID(airpts->grf_prop.local_id, airpts->grf_prop.grffile->grfid, airpts->grf_prop.subst_id);
71 if (airpt_id == this->invalid_id) {
72 GrfMsg(1, "AirportTile.SetEntitySpec: Too many airport tiles allocated. Ignoring.");
73 return;
76 AirportTileSpec::tiles[airpt_id] = *airpts;
78 /* Now add the overrides. */
79 for (int i = 0; i < this->max_offset; i++) {
80 AirportTileSpec *overridden_airpts = &AirportTileSpec::tiles[i];
82 if (this->entity_overrides[i] != airpts->grf_prop.local_id || this->grfid_overrides[i] != airpts->grf_prop.grffile->grfid) continue;
84 overridden_airpts->grf_prop.override = airpt_id;
85 overridden_airpts->enabled = false;
86 this->entity_overrides[i] = this->invalid_id;
87 this->grfid_overrides[i] = 0;
91 /**
92 * Do airporttile gfx ID translation for NewGRFs.
93 * @param gfx the type to get the override for.
94 * @return the gfx to actually work with.
96 StationGfx GetTranslatedAirportTileID(StationGfx gfx)
98 const AirportTileSpec *it = AirportTileSpec::Get(gfx);
99 return it->grf_prop.override == INVALID_AIRPORTTILE ? gfx : it->grf_prop.override;
103 * Based on newhouses/newindustries equivalent, but adapted for airports.
104 * @param parameter from callback. It's in fact a pair of coordinates
105 * @param tile TileIndex from which the callback was initiated
106 * @param index of the industry been queried for
107 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
108 * @return a construction of bits obeying the newgrf format
110 static uint32_t GetNearbyAirportTileInformation(byte parameter, TileIndex tile, StationID index, bool grf_version8)
112 if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
113 bool is_same_airport = (IsTileType(tile, MP_STATION) && IsAirport(tile) && GetStationIndex(tile) == index);
115 return GetNearbyTileInformation(tile, grf_version8) | (is_same_airport ? 1 : 0) << 8;
120 * Make an analysis of a tile and check whether it belongs to the same
121 * airport, and/or the same grf file
122 * @param tile TileIndex of the tile to query
123 * @param st Station to which to compare the tile to
124 * @param cur_grfid GRFID of the current callback
125 * @return value encoded as per NFO specs
127 static uint32_t GetAirportTileIDAtOffset(TileIndex tile, const Station *st, uint32_t cur_grfid)
129 if (!st->TileBelongsToAirport(tile)) {
130 return 0xFFFF;
133 StationGfx gfx = GetAirportGfx(tile);
134 const AirportTileSpec *ats = AirportTileSpec::Get(gfx);
136 if (gfx < NEW_AIRPORTTILE_OFFSET) { // Does it belongs to an old type?
137 /* It is an old tile. We have to see if it's been overridden */
138 if (ats->grf_prop.override == INVALID_AIRPORTTILE) { // has it been overridden?
139 return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
141 /* Overridden */
142 const AirportTileSpec *tile_ovr = AirportTileSpec::Get(ats->grf_prop.override);
144 if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
145 return tile_ovr->grf_prop.local_id; // same grf file
146 } else {
147 return 0xFFFE; // not the same grf file
150 /* Not an 'old type' tile */
151 if (ats->grf_prop.spritegroup[0] != nullptr) { // tile has a spritegroup ?
152 if (ats->grf_prop.grffile->grfid == cur_grfid) { // same airport, same grf ?
153 return ats->grf_prop.local_id;
154 } else {
155 return 0xFFFE; // Defined in another grf file
158 /* The tile has no spritegroup */
159 return 0xFF << 8 | ats->grf_prop.subst_id; // so just give it the substitute
162 /* virtual */ uint32_t AirportTileScopeResolver::GetVariable(byte variable, [[maybe_unused]] uint32_t parameter, bool *available) const
164 assert(this->st != nullptr);
166 switch (variable) {
167 /* Terrain type */
168 case 0x41: return GetTerrainType(this->tile);
170 /* Current town zone of the tile in the nearest town */
171 case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile, UINT_MAX), this->tile);
173 /* Position relative to most northern airport tile. */
174 case 0x43: return GetRelativePosition(this->tile, this->st->airport.tile);
176 /* Animation frame of tile */
177 case 0x44: return GetAnimationFrame(this->tile);
179 /* Land info of nearby tiles */
180 case 0x60: return GetNearbyAirportTileInformation(parameter, this->tile, this->st->index, this->ro.grffile->grf_version >= 8);
182 /* Animation stage of nearby tiles */
183 case 0x61: {
184 TileIndex tile = GetNearbyTile(parameter, this->tile);
185 if (this->st->TileBelongsToAirport(tile)) {
186 return GetAnimationFrame(tile);
188 return UINT_MAX;
191 /* Get airport tile ID at offset */
192 case 0x62: return GetAirportTileIDAtOffset(GetNearbyTile(parameter, this->tile), this->st, this->ro.grffile->grfid);
195 Debug(grf, 1, "Unhandled airport tile variable 0x{:X}", variable);
197 *available = false;
198 return UINT_MAX;
201 /* virtual */ uint32_t AirportTileScopeResolver::GetRandomBits() const
203 return (this->st == nullptr ? 0 : this->st->random_bits) | (this->tile == INVALID_TILE ? 0 : GetStationTileRandomBits(this->tile) << 16);
207 * Constructor of the resolver for airport tiles.
208 * @param ats Specification of the airport tiles.
209 * @param tile %Tile for the callback, only valid for airporttile callbacks.
210 * @param st Station of the airport for which the callback is run, or \c nullptr for build gui.
211 * @param callback Callback ID.
212 * @param callback_param1 First parameter (var 10) of the callback.
213 * @param callback_param2 Second parameter (var 18) of the callback.
215 AirportTileResolverObject::AirportTileResolverObject(const AirportTileSpec *ats, TileIndex tile, Station *st,
216 CallbackID callback, uint32_t callback_param1, uint32_t callback_param2)
217 : ResolverObject(ats->grf_prop.grffile, callback, callback_param1, callback_param2),
218 tiles_scope(*this, ats, tile, st),
219 airport_scope(*this, tile, st, st != nullptr ? st->airport.type : (byte)AT_DUMMY, st != nullptr ? st->airport.layout : 0)
221 this->root_spritegroup = ats->grf_prop.spritegroup[0];
224 GrfSpecFeature AirportTileResolverObject::GetFeature() const
226 return GSF_AIRPORTTILES;
229 uint32_t AirportTileResolverObject::GetDebugID() const
231 return this->tiles_scope.ats->grf_prop.local_id;
234 uint16_t GetAirportTileCallback(CallbackID callback, uint32_t param1, uint32_t param2, const AirportTileSpec *ats, Station *st, TileIndex tile, [[maybe_unused]] int extra_data = 0)
236 AirportTileResolverObject object(ats, tile, st, callback, param1, param2);
237 return object.ResolveCallback();
240 static void AirportDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte colour)
242 const DrawTileSprites *dts = group->ProcessRegisters(nullptr);
244 SpriteID image = dts->ground.sprite;
245 SpriteID pal = dts->ground.pal;
247 if (GB(image, 0, SPRITE_WIDTH) != 0) {
248 if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
249 DrawWaterClassGround(ti);
250 } else {
251 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(colour)));
255 DrawNewGRFTileSeq(ti, dts, TO_BUILDINGS, 0, GENERAL_SPRITE_COLOUR(colour));
258 bool DrawNewAirportTile(TileInfo *ti, Station *st, const AirportTileSpec *airts)
260 if (ti->tileh != SLOPE_FLAT) {
261 bool draw_old_one = true;
262 if (HasBit(airts->callback_mask, CBM_AIRT_DRAW_FOUNDATIONS)) {
263 /* Called to determine the type (if any) of foundation to draw */
264 uint32_t callback_res = GetAirportTileCallback(CBID_AIRPTILE_DRAW_FOUNDATIONS, 0, 0, airts, st, ti->tile);
265 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(airts->grf_prop.grffile, CBID_AIRPTILE_DRAW_FOUNDATIONS, callback_res);
268 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
271 AirportTileResolverObject object(airts, ti->tile, st);
272 const SpriteGroup *group = object.Resolve();
273 if (group == nullptr || group->type != SGT_TILELAYOUT) {
274 return false;
277 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
278 AirportDrawTileLayout(ti, tlgroup, Company::Get(st->owner)->colour);
279 return true;
282 /** Helper class for animation control. */
283 struct AirportTileAnimationBase : public AnimationBase<AirportTileAnimationBase, AirportTileSpec, Station, int, GetAirportTileCallback, TileAnimationFrameAnimationHelper<Station> > {
284 static const CallbackID cb_animation_speed = CBID_AIRPTILE_ANIMATION_SPEED;
285 static const CallbackID cb_animation_next_frame = CBID_AIRPTILE_ANIM_NEXT_FRAME;
287 static const AirportTileCallbackMask cbm_animation_speed = CBM_AIRT_ANIM_SPEED;
288 static const AirportTileCallbackMask cbm_animation_next_frame = CBM_AIRT_ANIM_NEXT_FRAME;
291 void AnimateAirportTile(TileIndex tile)
293 const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
294 if (ats == nullptr) return;
296 AirportTileAnimationBase::AnimateTile(ats, Station::GetByTile(tile), tile, HasBit(ats->animation_special_flags, 0));
299 void AirportTileAnimationTrigger(Station *st, TileIndex tile, AirpAnimationTrigger trigger, CargoID cargo_type)
301 const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
302 if (!HasBit(ats->animation.triggers, trigger)) return;
304 AirportTileAnimationBase::ChangeAnimationFrame(CBID_AIRPTILE_ANIM_START_STOP, ats, st, tile, Random(), (uint8_t)trigger | (cargo_type << 8));
307 void AirportAnimationTrigger(Station *st, AirpAnimationTrigger trigger, CargoID cargo_type)
309 if (st->airport.tile == INVALID_TILE) return;
311 for (TileIndex tile : st->airport) {
312 if (st->TileBelongsToAirport(tile)) AirportTileAnimationTrigger(st, tile, trigger, cargo_type);