(svn r27950) -Merge: Documentation updates from 1.7 branch
[openttd.git] / src / newgrf_airporttiles.cpp
blob5eb24f670f5a40aa8d457b256cb5fb0c4b94049e
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"
26 #include "safeguards.h"
29 AirportTileSpec AirportTileSpec::tiles[NUM_AIRPORTTILES];
31 AirportTileOverrideManager _airporttile_mngr(NEW_AIRPORTTILE_OFFSET, NUM_AIRPORTTILES, INVALID_AIRPORTTILE);
33 /**
34 * Retrieve airport tile spec for the given airport tile
35 * @param gfx index of airport tile
36 * @return A pointer to the corresponding AirportTileSpec
38 /* static */ const AirportTileSpec *AirportTileSpec::Get(StationGfx gfx)
40 /* should be assert(gfx < lengthof(tiles)), but that gives compiler warnings
41 * since it's always true if the following holds: */
42 assert_compile(MAX_UVALUE(StationGfx) + 1 == lengthof(tiles));
43 return &AirportTileSpec::tiles[gfx];
46 /**
47 * Retrieve airport tile spec for the given airport tile.
48 * @param tile The airport tile.
49 * @return A pointer to the corresponding AirportTileSpec.
51 /* static */ const AirportTileSpec *AirportTileSpec::GetByTile(TileIndex tile)
53 return AirportTileSpec::Get(GetAirportGfx(tile));
56 /**
57 * This function initializes the tile array of AirportTileSpec
59 void AirportTileSpec::ResetAirportTiles()
61 memset(&AirportTileSpec::tiles, 0, sizeof(AirportTileSpec::tiles));
62 memcpy(&AirportTileSpec::tiles, &_origin_airporttile_specs, sizeof(_origin_airporttile_specs));
64 /* Reset any overrides that have been set. */
65 _airporttile_mngr.ResetOverride();
68 void AirportTileOverrideManager::SetEntitySpec(const AirportTileSpec *airpts)
70 StationGfx airpt_id = this->AddEntityID(airpts->grf_prop.local_id, airpts->grf_prop.grffile->grfid, airpts->grf_prop.subst_id);
72 if (airpt_id == invalid_ID) {
73 grfmsg(1, "AirportTile.SetEntitySpec: Too many airport tiles allocated. Ignoring.");
74 return;
77 memcpy(&AirportTileSpec::tiles[airpt_id], airpts, sizeof(*airpts));
79 /* Now add the overrides. */
80 for (int i = 0; i < max_offset; i++) {
81 AirportTileSpec *overridden_airpts = &AirportTileSpec::tiles[i];
83 if (entity_overrides[i] != airpts->grf_prop.local_id || grfid_overrides[i] != airpts->grf_prop.grffile->grfid) continue;
85 overridden_airpts->grf_prop.override = airpt_id;
86 overridden_airpts->enabled = false;
87 entity_overrides[i] = invalid_ID;
88 grfid_overrides[i] = 0;
92 /**
93 * Do airporttile gfx ID translation for NewGRFs.
94 * @param gfx the type to get the override for.
95 * @return the gfx to actually work with.
97 StationGfx GetTranslatedAirportTileID(StationGfx gfx)
99 const AirportTileSpec *it = AirportTileSpec::Get(gfx);
100 return it->grf_prop.override == INVALID_AIRPORTTILE ? gfx : it->grf_prop.override;
104 * Based on newhouses/newindustries equivalent, but adapted for airports.
105 * @param parameter from callback. It's in fact a pair of coordinates
106 * @param tile TileIndex from which the callback was initiated
107 * @param index of the industry been queried for
108 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
109 * @return a construction of bits obeying the newgrf format
111 static uint32 GetNearbyAirportTileInformation(byte parameter, TileIndex tile, StationID index, bool grf_version8)
113 if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
114 bool is_same_airport = (IsTileType(tile, MP_STATION) && IsAirport(tile) && GetStationIndex(tile) == index);
116 return GetNearbyTileInformation(tile, grf_version8) | (is_same_airport ? 1 : 0) << 8;
121 * Make an analysis of a tile and check whether it belongs to the same
122 * airport, and/or the same grf file
123 * @param tile TileIndex of the tile to query
124 * @param st Station to which to compare the tile to
125 * @param cur_grfid GRFID of the current callback
126 * @return value encoded as per NFO specs
128 static uint32 GetAirportTileIDAtOffset(TileIndex tile, const Station *st, uint32 cur_grfid)
130 if (!st->TileBelongsToAirport(tile)) {
131 return 0xFFFF;
134 StationGfx gfx = GetAirportGfx(tile);
135 const AirportTileSpec *ats = AirportTileSpec::Get(gfx);
137 if (gfx < NEW_AIRPORTTILE_OFFSET) { // Does it belongs to an old type?
138 /* It is an old tile. We have to see if it's been overridden */
139 if (ats->grf_prop.override == INVALID_AIRPORTTILE) { // has it been overridden?
140 return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
142 /* Overridden */
143 const AirportTileSpec *tile_ovr = AirportTileSpec::Get(ats->grf_prop.override);
145 if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
146 return tile_ovr->grf_prop.local_id; // same grf file
147 } else {
148 return 0xFFFE; // not the same grf file
151 /* Not an 'old type' tile */
152 if (ats->grf_prop.spritegroup[0] != NULL) { // tile has a spritegroup ?
153 if (ats->grf_prop.grffile->grfid == cur_grfid) { // same airport, same grf ?
154 return ats->grf_prop.local_id;
155 } else {
156 return 0xFFFE; // Defined in another grf file
159 /* The tile has no spritegroup */
160 return 0xFF << 8 | ats->grf_prop.subst_id; // so just give him the substitute
163 /* virtual */ uint32 AirportTileScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
165 assert(this->st != NULL);
167 extern uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile);
169 switch (variable) {
170 /* Terrain type */
171 case 0x41: return GetTerrainType(this->tile);
173 /* Current town zone of the tile in the nearest town */
174 case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile, UINT_MAX), this->tile);
176 /* Position relative to most northern airport tile. */
177 case 0x43: return GetRelativePosition(this->tile, this->st->airport.tile);
179 /* Animation frame of tile */
180 case 0x44: return GetAnimationFrame(this->tile);
182 /* Land info of nearby tiles */
183 case 0x60: return GetNearbyAirportTileInformation(parameter, this->tile, this->st->index, this->ro.grffile->grf_version >= 8);
185 /* Animation stage of nearby tiles */
186 case 0x61: {
187 TileIndex tile = GetNearbyTile(parameter, this->tile);
188 if (this->st->TileBelongsToAirport(tile)) {
189 return GetAnimationFrame(tile);
191 return UINT_MAX;
194 /* Get airport tile ID at offset */
195 case 0x62: return GetAirportTileIDAtOffset(GetNearbyTile(parameter, this->tile), this->st, this->ro.grffile->grfid);
198 DEBUG(grf, 1, "Unhandled airport tile variable 0x%X", variable);
200 *available = false;
201 return UINT_MAX;
204 /* virtual */ uint32 AirportTileScopeResolver::GetRandomBits() const
206 return (this->st == NULL ? 0 : this->st->random_bits) | (this->tile == INVALID_TILE ? 0 : GetStationTileRandomBits(this->tile) << 16);
210 * Constructor of the resolver for airport tiles.
211 * @param ats Specification of the airport tiles.
212 * @param tile %Tile for the callback, only valid for airporttile callbacks.
213 * @param st Station of the airport for which the callback is run, or \c NULL for build gui.
214 * @param callback Callback ID.
215 * @param callback_param1 First parameter (var 10) of the callback.
216 * @param callback_param2 Second parameter (var 18) of the callback.
218 AirportTileResolverObject::AirportTileResolverObject(const AirportTileSpec *ats, TileIndex tile, Station *st,
219 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
220 : ResolverObject(ats->grf_prop.grffile, callback, callback_param1, callback_param2), tiles_scope(*this, ats, tile, st)
222 this->root_spritegroup = ats->grf_prop.spritegroup[0];
226 * Constructor of the scope resolver specific for airport tiles.
227 * @param ats Specification of the airport tiles.
228 * @param tile %Tile for the callback, only valid for airporttile callbacks.
229 * @param st Station of the airport for which the callback is run, or \c NULL for build gui.
231 AirportTileScopeResolver::AirportTileScopeResolver(ResolverObject &ro, const AirportTileSpec *ats, TileIndex tile, Station *st) : ScopeResolver(ro)
233 assert(st != NULL);
235 this->st = st;
236 this->airport_id = st->airport.type;
237 this->tile = tile;
240 uint16 GetAirportTileCallback(CallbackID callback, uint32 param1, uint32 param2, const AirportTileSpec *ats, Station *st, TileIndex tile, int extra_data = 0)
242 AirportTileResolverObject object(ats, tile, st, callback, param1, param2);
243 return object.ResolveCallback();
246 static void AirportDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte colour, StationGfx gfx)
248 const DrawTileSprites *dts = group->ProcessRegisters(NULL);
250 SpriteID image = dts->ground.sprite;
251 SpriteID pal = dts->ground.pal;
253 if (GB(image, 0, SPRITE_WIDTH) != 0) {
254 if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
255 DrawWaterClassGround(ti);
256 } else {
257 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(colour)));
261 DrawNewGRFTileSeq(ti, dts, TO_BUILDINGS, 0, GENERAL_SPRITE_COLOUR(colour));
264 bool DrawNewAirportTile(TileInfo *ti, Station *st, StationGfx gfx, const AirportTileSpec *airts)
266 if (ti->tileh != SLOPE_FLAT) {
267 bool draw_old_one = true;
268 if (HasBit(airts->callback_mask, CBM_AIRT_DRAW_FOUNDATIONS)) {
269 /* Called to determine the type (if any) of foundation to draw */
270 uint32 callback_res = GetAirportTileCallback(CBID_AIRPTILE_DRAW_FOUNDATIONS, 0, 0, airts, st, ti->tile);
271 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(airts->grf_prop.grffile, CBID_AIRPTILE_DRAW_FOUNDATIONS, callback_res);
274 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
277 AirportTileResolverObject object(airts, ti->tile, st);
278 const SpriteGroup *group = object.Resolve();
279 if (group == NULL || group->type != SGT_TILELAYOUT) {
280 return false;
283 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
284 AirportDrawTileLayout(ti, tlgroup, Company::Get(st->owner)->colour, gfx);
285 return true;
288 /** Helper class for animation control. */
289 struct AirportTileAnimationBase : public AnimationBase<AirportTileAnimationBase, AirportTileSpec, Station, int, GetAirportTileCallback> {
290 static const CallbackID cb_animation_speed = CBID_AIRPTILE_ANIMATION_SPEED;
291 static const CallbackID cb_animation_next_frame = CBID_AIRPTILE_ANIM_NEXT_FRAME;
293 static const AirportTileCallbackMask cbm_animation_speed = CBM_AIRT_ANIM_SPEED;
294 static const AirportTileCallbackMask cbm_animation_next_frame = CBM_AIRT_ANIM_NEXT_FRAME;
297 void AnimateAirportTile(TileIndex tile)
299 const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
300 if (ats == NULL) return;
302 AirportTileAnimationBase::AnimateTile(ats, Station::GetByTile(tile), tile, HasBit(ats->animation_special_flags, 0));
305 void AirportTileAnimationTrigger(Station *st, TileIndex tile, AirpAnimationTrigger trigger, CargoID cargo_type)
307 const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
308 if (!HasBit(ats->animation.triggers, trigger)) return;
310 AirportTileAnimationBase::ChangeAnimationFrame(CBID_AIRPTILE_ANIM_START_STOP, ats, st, tile, Random(), (uint8)trigger | (cargo_type << 8));
313 void AirportAnimationTrigger(Station *st, AirpAnimationTrigger trigger, CargoID cargo_type)
315 if (st->airport.tile == INVALID_TILE) return;
317 TILE_AREA_LOOP(tile, st->airport) {
318 if (st->TileBelongsToAirport(tile)) AirportTileAnimationTrigger(st, tile, trigger, cargo_type);