Remove enum PylonSpriteOffset
[openttd/fttd.git] / src / newgrf_airport.cpp
blobb0a0bfb0096cd6052a46ec5a37706278c9777d9b
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_airport.cpp NewGRF handling of airports. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "date_func.h"
15 #include "newgrf_spritegroup.h"
16 #include "newgrf_text.h"
17 #include "station_base.h"
18 #include "newgrf_class_func.h"
20 /** Resolver for the airport scope. */
21 struct AirportScopeResolver : public ScopeResolver {
22 const GRFFile *const grffile; ///< GRFFile the resolved SpriteGroup belongs to.
23 struct Station *st; ///< Station of the airport for which the callback is run, or \c NULL for build gui.
24 byte layout; ///< Layout of the airport to build.
25 TileIndex tile; ///< Tile for the callback, only valid for airporttile callbacks.
27 /**
28 * Constructor of the scope resolver for an airport.
29 * @param grffile GRFFile the resolved SpriteGroup belongs to.
30 * @param tile %Tile for the callback, only valid for airporttile callbacks.
31 * @param st %Station of the airport for which the callback is run, or \c NULL for build gui.
32 * @param layout Layout of the airport to build.
34 AirportScopeResolver (const GRFFile *grffile, TileIndex tile,
35 Station *st, byte layout)
36 : ScopeResolver(), grffile(grffile),
37 st(st), layout(layout), tile(tile)
41 /* virtual */ uint32 GetRandomBits() const;
42 /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
43 /* virtual */ void StorePSA(uint pos, int32 value);
46 /** Resolver object for airports. */
47 struct AirportResolverObject : public ResolverObject {
48 AirportScopeResolver airport_scope;
50 /**
51 * Constructor of the airport resolver.
52 * @param tile %Tile for the callback, only valid for airporttile callbacks.
53 * @param st %Station of the airport for which the callback is run, or \c NULL for build gui.
54 * @param as AirportSpec for which the callback is run.
55 * @param layout Layout of the airport to build.
56 * @param callback Callback ID.
57 * @param param1 First parameter (var 10) of the callback.
58 * @param param2 Second parameter (var 18) of the callback.
60 AirportResolverObject (TileIndex tile, Station *st, const AirportSpec *as, byte layout,
61 CallbackID callback = CBID_NO_CALLBACK, uint32 param1 = 0, uint32 param2 = 0)
62 : ResolverObject (as->grf_prop.grffile, callback, param1, param2),
63 airport_scope (this->grffile, tile, st, layout)
67 /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
69 switch (scope) {
70 case VSG_SCOPE_SELF: return &this->airport_scope;
71 default: return ResolverObject::GetScope(scope, relative);
75 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
78 /**
79 * Reset airport classes to their default state.
80 * This includes initialising the defaults classes with an empty
81 * entry, for standard airports.
83 template <>
84 /* static */ void NewGRFClass <AirportSpec, AirportClassID, APC_MAX>::InsertDefaults()
86 AirportClass::Get(AirportClass::Allocate('SMAL'))->name = STR_AIRPORT_CLASS_SMALL;
87 AirportClass::Get(AirportClass::Allocate('LARG'))->name = STR_AIRPORT_CLASS_LARGE;
88 AirportClass::Get(AirportClass::Allocate('HUB_'))->name = STR_AIRPORT_CLASS_HUB;
89 AirportClass::Get(AirportClass::Allocate('HELI'))->name = STR_AIRPORT_CLASS_HELIPORTS;
92 template <>
93 bool NewGRFClass <AirportSpec, AirportClassID, APC_MAX>::IsUIAvailable (uint index) const
95 return true;
98 INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass)
101 AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
103 AirportSpec AirportSpec::specs[NUM_AIRPORTS]; ///< Airport specifications.
106 * Retrieve airport spec for the given airport. If an override is available
107 * it is returned.
108 * @param type index of airport
109 * @return A pointer to the corresponding AirportSpec
111 /* static */ const AirportSpec *AirportSpec::Get(byte type)
113 assert(type < lengthof(AirportSpec::specs));
114 const AirportSpec *as = &AirportSpec::specs[type];
115 if (type >= NEW_AIRPORT_OFFSET && !as->enabled) {
116 byte subst_id = _airport_mngr.GetSubstituteID(type);
117 if (subst_id == AT_INVALID) return as;
118 as = &AirportSpec::specs[subst_id];
120 if (as->grf_prop.override != AT_INVALID) return &AirportSpec::specs[as->grf_prop.override];
121 return as;
125 * Retrieve airport spec for the given airport. Even if an override is
126 * available the base spec is returned.
127 * @param type index of airport
128 * @return A pointer to the corresponding AirportSpec
130 /* static */ AirportSpec *AirportSpec::GetWithoutOverride(byte type)
132 assert(type < lengthof(AirportSpec::specs));
133 return &AirportSpec::specs[type];
136 /** Check whether this airport is available to build. */
137 bool AirportSpec::IsAvailable() const
139 if (!this->enabled) return false;
140 if (_cur_year < this->min_year) return false;
141 if (_settings_game.station.never_expire_airports) return true;
142 return _cur_year <= this->max_year;
146 * This function initializes the airportspec array.
148 void AirportSpec::ResetAirports()
150 extern const AirportSpec _origin_airport_specs[];
151 memset(&AirportSpec::specs, 0, sizeof(AirportSpec::specs));
152 memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NEW_AIRPORT_OFFSET);
154 _airport_mngr.ResetOverride();
158 * Tie all airportspecs to their class.
160 void BindAirportSpecs()
162 for (int i = 0; i < NUM_AIRPORTS; i++) {
163 AirportSpec *as = AirportSpec::GetWithoutOverride(i);
164 if (as->enabled) AirportClass::Assign(as);
169 void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
171 byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
173 if (airport_id == invalid_ID) {
174 grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
175 return;
178 memcpy(AirportSpec::GetWithoutOverride(airport_id), as, sizeof(*as));
180 /* Now add the overrides. */
181 for (int i = 0; i < max_offset; i++) {
182 AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
184 if (entity_overrides[i] != as->grf_prop.local_id || grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
186 overridden_as->grf_prop.override = airport_id;
187 overridden_as->enabled = false;
188 entity_overrides[i] = invalid_ID;
189 grfid_overrides[i] = 0;
193 /* virtual */ uint32 AirportScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
195 switch (variable) {
196 case 0x40: return this->layout;
199 if (this->st == NULL) {
200 *available = false;
201 return UINT_MAX;
204 switch (variable) {
205 /* Get a variable from the persistent storage */
206 case 0x7C: return (this->st->airport.psa != NULL) ? this->st->airport.psa->GetValue(parameter) : 0;
208 case 0xF0: return this->st->facilities;
209 case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
212 return this->st->GetNewGRFVariable (this->grffile, variable, parameter, available);
215 /* virtual */ const SpriteGroup *AirportResolverObject::ResolveReal(const RealSpriteGroup *group) const
217 /* Airport action 2s should always have only 1 "loaded" state, but some
218 * times things don't follow the spec... */
219 if (group->num_loaded > 0) return group->loaded[0];
220 if (group->num_loading > 0) return group->loading[0];
222 return NULL;
225 /* virtual */ uint32 AirportScopeResolver::GetRandomBits() const
227 return this->st == NULL ? 0 : this->st->random_bits;
231 * Store a value into the object's persistent storage.
232 * @param object Object that we want to query.
233 * @param pos Position in the persistent storage to use.
234 * @param value Value to store.
236 /* virtual */ void AirportScopeResolver::StorePSA(uint pos, int32 value)
238 if (this->st == NULL) return;
240 if (this->st->airport.psa == NULL) {
241 /* There is no need to create a storage if the value is zero. */
242 if (value == 0) return;
244 /* Create storage on first modification. */
245 uint32 grfid = (this->grffile != NULL) ? this->grffile->grfid : 0;
246 assert(PersistentStorage::CanAllocateItem());
247 this->st->airport.psa = new PersistentStorage(grfid, GSF_AIRPORTS, this->st->airport.tile);
249 this->st->airport.psa->StoreValue(pos, value);
252 static const SpriteGroup *AirportResolve (TileIndex tile, Station *st, const AirportSpec *as, byte layout,
253 CallbackID callback = CBID_NO_CALLBACK, uint32 param1 = 0, uint32 param2 = 0)
255 AirportResolverObject object (tile, st, as, layout, callback, param1, param2);
256 return SpriteGroup::Resolve (as->grf_prop.spritegroup[0], object);
259 SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
261 const SpriteGroup *group = AirportResolve (INVALID_TILE, NULL, as, layout);
262 if (group == NULL) return as->preview_sprite;
264 return group->GetResult();
267 uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
269 return SpriteGroup::CallbackResult (AirportResolve (tile, st,
270 AirportSpec::Get (st->airport.type), st->airport.layout,
271 callback, param1, param2));
275 * Get a custom text for the airport.
276 * @param as The airport type's specification.
277 * @param layout The layout index.
278 * @param callback The callback to call.
279 * @return The custom text.
281 StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
283 uint16 cb_res = SpriteGroup::CallbackResult (AirportResolve (INVALID_TILE,
284 NULL, as, layout, (CallbackID)callback));
285 if (cb_res == CALLBACK_FAILED || cb_res == 0x400) return STR_UNDEFINED;
286 if (cb_res > 0x400) {
287 ErrorUnknownCallbackResult(as->grf_prop.grffile->grfid, callback, cb_res);
288 return STR_UNDEFINED;
291 return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);