Fix old map array tunnel head conversion
[openttd/fttd.git] / src / newgrf_airporttiles.cpp
blobc6b43d45ee382cd414e37fadf8adc11ed2a13110
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file newgrf_airporttiles.cpp NewGRF handling of airport tiles. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "newgrf_airporttiles.h"
15 #include "newgrf_spritegroup.h"
16 #include "newgrf_sound.h"
17 #include "station_base.h"
18 #include "water.h"
19 #include "landscape.h"
20 #include "company_base.h"
21 #include "town.h"
22 #include "table/strings.h"
23 #include "table/airporttiles.h"
24 #include "newgrf_animation_base.h"
27 AirportTileSpec AirportTileSpec::tiles[NUM_AIRPORTTILES];
29 AirportTileOverrideManager _airporttile_mngr(NEW_AIRPORTTILE_OFFSET, NUM_AIRPORTTILES, INVALID_AIRPORTTILE);
31 /**
32 * Retrieve airport tile spec for the given airport tile
33 * @param gfx index of airport tile
34 * @return A pointer to the corresponding AirportTileSpec
36 /* static */ const AirportTileSpec *AirportTileSpec::Get(StationGfx gfx)
38 /* should be assert(gfx < lengthof(tiles)), but that gives compiler warnings
39 * since it's always true if the following holds: */
40 assert_compile(MAX_UVALUE(StationGfx) + 1 == lengthof(tiles));
41 return &AirportTileSpec::tiles[gfx];
44 /**
45 * Retrieve airport tile spec for the given airport tile.
46 * @param tile The airport tile.
47 * @return A pointer to the corresponding AirportTileSpec.
49 /* static */ const AirportTileSpec *AirportTileSpec::GetByTile(TileIndex tile)
51 return AirportTileSpec::Get(GetAirportGfx(tile));
54 /**
55 * This function initializes the tile array of AirportTileSpec
57 void AirportTileSpec::ResetAirportTiles()
59 memset(&AirportTileSpec::tiles, 0, sizeof(AirportTileSpec::tiles));
60 memcpy(&AirportTileSpec::tiles, &_origin_airporttile_specs, sizeof(_origin_airporttile_specs));
62 /* Reset any overrides that have been set. */
63 _airporttile_mngr.ResetOverride();
66 void AirportTileOverrideManager::SetEntitySpec(const AirportTileSpec *airpts)
68 StationGfx airpt_id = this->AddEntityID(airpts->grf_prop.local_id, airpts->grf_prop.grffile->grfid, airpts->grf_prop.subst_id);
70 if (airpt_id == invalid_ID) {
71 grfmsg(1, "AirportTile.SetEntitySpec: Too many airport tiles allocated. Ignoring.");
72 return;
75 memcpy(&AirportTileSpec::tiles[airpt_id], airpts, sizeof(*airpts));
77 /* Now add the overrides. */
78 for (int i = 0; i < max_offset; i++) {
79 AirportTileSpec *overridden_airpts = &AirportTileSpec::tiles[i];
81 if (entity_overrides[i] != airpts->grf_prop.local_id || grfid_overrides[i] != airpts->grf_prop.grffile->grfid) continue;
83 overridden_airpts->grf_prop.override = airpt_id;
84 overridden_airpts->enabled = false;
85 entity_overrides[i] = invalid_ID;
86 grfid_overrides[i] = 0;
90 /**
91 * Do airporttile gfx ID translation for NewGRFs.
92 * @param gfx the type to get the override for.
93 * @return the gfx to actually work with.
95 StationGfx GetTranslatedAirportTileID(StationGfx gfx)
97 const AirportTileSpec *it = AirportTileSpec::Get(gfx);
98 return it->grf_prop.override == INVALID_AIRPORTTILE ? gfx : it->grf_prop.override;
102 * Based on newhouses/newindustries equivalent, but adapted for airports.
103 * @param parameter from callback. It's in fact a pair of coordinates
104 * @param tile TileIndex from which the callback was initiated
105 * @param index of the industry been queried for
106 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
107 * @return a construction of bits obeying the newgrf format
109 static uint32 GetNearbyAirportTileInformation(byte parameter, TileIndex tile, StationID index, bool grf_version8)
111 if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
112 bool is_same_airport = (IsStationTile(tile) && IsAirport(tile) && GetStationIndex(tile) == index);
114 return GetNearbyTileInformation(tile, grf_version8) | (is_same_airport ? 1 : 0) << 8;
119 * Make an analysis of a tile and check whether it belongs to the same
120 * airport, and/or the same grf file
121 * @param tile TileIndex of the tile to query
122 * @param st Station to which to compare the tile to
123 * @param cur_grfid GRFID of the current callback
124 * @return value encoded as per NFO specs
126 static uint32 GetAirportTileIDAtOffset(TileIndex tile, const Station *st, uint32 cur_grfid)
128 if (!st->TileBelongsToAirport(tile)) {
129 return 0xFFFF;
132 StationGfx gfx = GetAirportGfx(tile);
133 const AirportTileSpec *ats = AirportTileSpec::Get(gfx);
135 if (gfx < NEW_AIRPORTTILE_OFFSET) { // Does it belongs to an old type?
136 /* It is an old tile. We have to see if it's been overridden */
137 if (ats->grf_prop.override == INVALID_AIRPORTTILE) { // has it been overridden?
138 return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
140 /* Overridden */
141 const AirportTileSpec *tile_ovr = AirportTileSpec::Get(ats->grf_prop.override);
143 if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
144 return tile_ovr->grf_prop.local_id; // same grf file
145 } else {
146 return 0xFFFE; // not the same grf file
149 /* Not an 'old type' tile */
150 if (ats->grf_prop.spritegroup != NULL) { // tile has a spritegroup ?
151 if (ats->grf_prop.grffile->grfid == cur_grfid) { // same airport, same grf ?
152 return ats->grf_prop.local_id;
153 } else {
154 return 0xFFFE; // Defined in another grf file
157 /* The tile has no spritegroup */
158 return 0xFF << 8 | ats->grf_prop.subst_id; // so just give him the substitute
161 /* virtual */ uint32 AirportTileScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
163 assert(this->st != NULL);
165 extern uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile);
167 switch (variable) {
168 /* Terrain type */
169 case 0x41: return GetTerrainType(this->tile);
171 /* Current town zone of the tile in the nearest town */
172 case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile), this->tile);
174 /* Position relative to most northern airport tile. */
175 case 0x43: return GetRelativePosition(this->tile, this->st->airport.tile);
177 /* Animation frame of tile */
178 case 0x44: return GetAnimationFrame(this->tile);
180 /* Land info of nearby tiles */
181 case 0x60: return GetNearbyAirportTileInformation (parameter, this->tile, this->st->index, this->grffile->grf_version >= 8);
183 /* Animation stage of nearby tiles */
184 case 0x61: {
185 TileIndex tile = GetNearbyTile(parameter, this->tile);
186 if (this->st->TileBelongsToAirport(tile)) {
187 return GetAnimationFrame(tile);
189 return UINT_MAX;
192 /* Get airport tile ID at offset */
193 case 0x62: return GetAirportTileIDAtOffset (GetNearbyTile (parameter, this->tile), this->st, this->grffile->grfid);
196 DEBUG(grf, 1, "Unhandled airport tile variable 0x%X", variable);
198 *available = false;
199 return UINT_MAX;
202 /* virtual */ uint32 AirportTileScopeResolver::GetRandomBits() const
204 return (this->st == NULL ? 0 : this->st->random_bits) | (this->tile == INVALID_TILE ? 0 : GetStationTileRandomBits(this->tile) << 16);
208 * Constructor of the resolver for airport tiles.
209 * @param ats Specification of the airport tiles.
210 * @param tile %Tile for the callback, only valid for airporttile callbacks.
211 * @param st Station of the airport for which the callback is run, or \c NULL for build gui.
212 * @param callback Callback ID.
213 * @param callback_param1 First parameter (var 10) of the callback.
214 * @param callback_param2 Second parameter (var 18) of the callback.
216 AirportTileResolverObject::AirportTileResolverObject(const AirportTileSpec *ats, TileIndex tile, Station *st,
217 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
218 : ResolverObject (ats->grf_prop.grffile, callback, callback_param1, callback_param2),
219 tiles_scope (this->grffile, ats, tile, st)
224 * Constructor of the scope resolver specific for airport tiles.
225 * @param grffile GRFFile the resolved SpriteGroup belongs to.
226 * @param ats Specification of the airport tiles.
227 * @param tile %Tile for the callback, only valid for airporttile callbacks.
228 * @param st Station of the airport for which the callback is run, or \c NULL for build gui.
230 AirportTileScopeResolver::AirportTileScopeResolver (const GRFFile *grffile, const AirportTileSpec *ats, TileIndex tile, Station *st)
231 : ScopeResolver(), grffile(grffile)
233 assert(st != NULL);
235 this->st = st;
236 this->tile = tile;
239 static inline const SpriteGroup *AirportTileResolve (const AirportTileSpec *ats,
240 TileIndex tile, Station *st, CallbackID callback = CBID_NO_CALLBACK,
241 uint32 param1 = 0, uint32 param2 = 0)
243 AirportTileResolverObject object (ats, tile, st, callback, param1, param2);
244 return SpriteGroup::Resolve (ats->grf_prop.spritegroup, object);
247 uint16 GetAirportTileCallback (CallbackID callback, uint32 param1, uint32 param2, const AirportTileSpec *ats, Station *st, TileIndex tile)
249 return SpriteGroup::CallbackResult (AirportTileResolve (ats, tile, st, callback, param1, param2));
252 static void AirportDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte colour, StationGfx gfx)
254 TileLayoutSpriteGroup::Result result (group);
256 SpriteID image = result.ground.sprite;
257 PaletteID pal = result.ground.pal;
259 if (GB(image, 0, SPRITE_WIDTH) != 0) {
260 if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
261 DrawWaterClassGround(ti);
262 } else {
263 DrawGroundSprite (ti, image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(colour)));
267 DrawNewGRFTileSeq (ti, result.seq, TO_BUILDINGS, 0, GENERAL_SPRITE_COLOUR(colour));
270 bool DrawNewAirportTile(TileInfo *ti, Station *st, StationGfx gfx, const AirportTileSpec *airts)
272 if (ti->tileh != SLOPE_FLAT) {
273 bool draw_old_one = true;
274 if (HasBit(airts->callback_mask, CBM_AIRT_DRAW_FOUNDATIONS)) {
275 /* Called to determine the type (if any) of foundation to draw */
276 uint32 callback_res = GetAirportTileCallback(CBID_AIRPTILE_DRAW_FOUNDATIONS, 0, 0, airts, st, ti->tile);
277 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(airts->grf_prop.grffile, CBID_AIRPTILE_DRAW_FOUNDATIONS, callback_res);
280 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
283 const SpriteGroup *group = AirportTileResolve (airts, ti->tile, st);
284 if (group == NULL || !group->IsType (SGT_TILELAYOUT)) {
285 return false;
288 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
289 AirportDrawTileLayout(ti, tlgroup, Company::Get(st->owner)->colour, gfx);
290 return true;
293 /** Helper class for animation control. */
294 struct AirportTileAnimationBase {
295 static const CallbackID cb_animation_speed = CBID_AIRPTILE_ANIMATION_SPEED;
296 static const CallbackID cb_animation_next_frame = CBID_AIRPTILE_ANIM_NEXT_FRAME;
298 static const AirportTileCallbackMask cbm_animation_speed = CBM_AIRT_ANIM_SPEED;
299 static const AirportTileCallbackMask cbm_animation_next_frame = CBM_AIRT_ANIM_NEXT_FRAME;
301 /** Callback wrapper for animation control. */
302 static uint16 get_callback (CallbackID callback, uint32 param1, uint32 param2, const AirportTileSpec *ats, Station *st, TileIndex tile)
304 return GetAirportTileCallback (callback, param1, param2, ats, st, tile);
308 void AnimateAirportTile(TileIndex tile)
310 const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
311 if (ats == NULL) return;
313 AnimationBase::AnimateTile <AirportTileAnimationBase> (ats, Station::GetByTile(tile), tile, HasBit(ats->animation_special_flags, 0));
316 void AirportTileAnimationTrigger(Station *st, TileIndex tile, AirpAnimationTrigger trigger, CargoID cargo_type)
318 const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
319 if (!HasBit(ats->animation.triggers, trigger)) return;
321 uint16 callback = GetAirportTileCallback (CBID_AIRPTILE_ANIM_START_STOP,
322 Random(), (uint8)trigger | (cargo_type << 8),
323 ats, st, tile);
324 AnimationBase::ChangeAnimationFrame (ats, tile, callback);
327 void AirportAnimationTrigger(Station *st, AirpAnimationTrigger trigger, CargoID cargo_type)
329 if (st->airport.tile == INVALID_TILE) return;
331 TILE_AREA_LOOP(tile, st->airport) {
332 if (st->TileBelongsToAirport(tile)) AirportTileAnimationTrigger(st, tile, trigger, cargo_type);