(svn r27950) -Merge: Documentation updates from 1.7 branch
[openttd.git] / src / newgrf_airport.cpp
blob6db12f7373274f362927a3767b438792ff65f147
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 #include "safeguards.h"
22 /** Resolver for the airport scope. */
23 struct AirportScopeResolver : public ScopeResolver {
24 struct Station *st; ///< Station of the airport for which the callback is run, or \c NULL for build gui.
25 byte airport_id; ///< Type of airport for which the callback is run.
26 byte layout; ///< Layout of the airport to build.
27 TileIndex tile; ///< Tile for the callback, only valid for airporttile callbacks.
29 AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, byte airport_id, byte layout);
31 /* virtual */ uint32 GetRandomBits() const;
32 /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
33 /* virtual */ void StorePSA(uint pos, int32 value);
36 /** Resolver object for airports. */
37 struct AirportResolverObject : public ResolverObject {
38 AirportScopeResolver airport_scope;
40 AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
41 CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
43 /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
45 switch (scope) {
46 case VSG_SCOPE_SELF: return &this->airport_scope;
47 default: return ResolverObject::GetScope(scope, relative);
51 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
54 /**
55 * Reset airport classes to their default state.
56 * This includes initialising the defaults classes with an empty
57 * entry, for standard airports.
59 template <typename Tspec, typename Tid, Tid Tmax>
60 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
62 AirportClass::Get(AirportClass::Allocate('SMAL'))->name = STR_AIRPORT_CLASS_SMALL;
63 AirportClass::Get(AirportClass::Allocate('LARG'))->name = STR_AIRPORT_CLASS_LARGE;
64 AirportClass::Get(AirportClass::Allocate('HUB_'))->name = STR_AIRPORT_CLASS_HUB;
65 AirportClass::Get(AirportClass::Allocate('HELI'))->name = STR_AIRPORT_CLASS_HELIPORTS;
68 template <typename Tspec, typename Tid, Tid Tmax>
69 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
71 return true;
74 INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX)
77 AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
79 AirportSpec AirportSpec::specs[NUM_AIRPORTS]; ///< Airport specifications.
81 /**
82 * Retrieve airport spec for the given airport. If an override is available
83 * it is returned.
84 * @param type index of airport
85 * @return A pointer to the corresponding AirportSpec
87 /* static */ const AirportSpec *AirportSpec::Get(byte type)
89 assert(type < lengthof(AirportSpec::specs));
90 const AirportSpec *as = &AirportSpec::specs[type];
91 if (type >= NEW_AIRPORT_OFFSET && !as->enabled) {
92 byte subst_id = _airport_mngr.GetSubstituteID(type);
93 if (subst_id == AT_INVALID) return as;
94 as = &AirportSpec::specs[subst_id];
96 if (as->grf_prop.override != AT_INVALID) return &AirportSpec::specs[as->grf_prop.override];
97 return as;
101 * Retrieve airport spec for the given airport. Even if an override is
102 * available the base spec is returned.
103 * @param type index of airport
104 * @return A pointer to the corresponding AirportSpec
106 /* static */ AirportSpec *AirportSpec::GetWithoutOverride(byte type)
108 assert(type < lengthof(AirportSpec::specs));
109 return &AirportSpec::specs[type];
112 /** Check whether this airport is available to build. */
113 bool AirportSpec::IsAvailable() const
115 if (!this->enabled) return false;
116 if (_cur_year < this->min_year) return false;
117 if (_settings_game.station.never_expire_airports) return true;
118 return _cur_year <= this->max_year;
122 * This function initializes the airportspec array.
124 void AirportSpec::ResetAirports()
126 extern const AirportSpec _origin_airport_specs[];
127 memset(&AirportSpec::specs, 0, sizeof(AirportSpec::specs));
128 memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NEW_AIRPORT_OFFSET);
130 _airport_mngr.ResetOverride();
134 * Tie all airportspecs to their class.
136 void BindAirportSpecs()
138 for (int i = 0; i < NUM_AIRPORTS; i++) {
139 AirportSpec *as = AirportSpec::GetWithoutOverride(i);
140 if (as->enabled) AirportClass::Assign(as);
145 void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
147 byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
149 if (airport_id == invalid_ID) {
150 grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
151 return;
154 memcpy(AirportSpec::GetWithoutOverride(airport_id), as, sizeof(*as));
156 /* Now add the overrides. */
157 for (int i = 0; i < max_offset; i++) {
158 AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
160 if (entity_overrides[i] != as->grf_prop.local_id || grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
162 overridden_as->grf_prop.override = airport_id;
163 overridden_as->enabled = false;
164 entity_overrides[i] = invalid_ID;
165 grfid_overrides[i] = 0;
169 /* virtual */ uint32 AirportScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
171 switch (variable) {
172 case 0x40: return this->layout;
175 if (this->st == NULL) {
176 *available = false;
177 return UINT_MAX;
180 switch (variable) {
181 /* Get a variable from the persistent storage */
182 case 0x7C: return (this->st->airport.psa != NULL) ? this->st->airport.psa->GetValue(parameter) : 0;
184 case 0xF0: return this->st->facilities;
185 case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
188 return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
191 /* virtual */ const SpriteGroup *AirportResolverObject::ResolveReal(const RealSpriteGroup *group) const
193 /* Airport action 2s should always have only 1 "loaded" state, but some
194 * times things don't follow the spec... */
195 if (group->num_loaded > 0) return group->loaded[0];
196 if (group->num_loading > 0) return group->loading[0];
198 return NULL;
201 /* virtual */ uint32 AirportScopeResolver::GetRandomBits() const
203 return this->st == NULL ? 0 : this->st->random_bits;
207 * Store a value into the object's persistent storage.
208 * @param object Object that we want to query.
209 * @param pos Position in the persistent storage to use.
210 * @param value Value to store.
212 /* virtual */ void AirportScopeResolver::StorePSA(uint pos, int32 value)
214 if (this->st == NULL) return;
216 if (this->st->airport.psa == NULL) {
217 /* There is no need to create a storage if the value is zero. */
218 if (value == 0) return;
220 /* Create storage on first modification. */
221 uint32 grfid = (this->ro.grffile != NULL) ? this->ro.grffile->grfid : 0;
222 assert(PersistentStorage::CanAllocateItem());
223 this->st->airport.psa = new PersistentStorage(grfid, GSF_AIRPORTS, this->st->airport.tile);
225 this->st->airport.psa->StoreValue(pos, value);
229 * Constructor of the airport resolver.
230 * @param tile %Tile for the callback, only valid for airporttile callbacks.
231 * @param st %Station of the airport for which the callback is run, or \c NULL for build gui.
232 * @param airport_id Type of airport for which the callback is run.
233 * @param layout Layout of the airport to build.
234 * @param callback Callback ID.
235 * @param param1 First parameter (var 10) of the callback.
236 * @param param2 Second parameter (var 18) of the callback.
238 AirportResolverObject::AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
239 CallbackID callback, uint32 param1, uint32 param2)
240 : ResolverObject(AirportSpec::Get(airport_id)->grf_prop.grffile, callback, param1, param2), airport_scope(*this, tile, st, airport_id, layout)
242 this->root_spritegroup = AirportSpec::Get(airport_id)->grf_prop.spritegroup[0];
246 * Constructor of the scope resolver for an airport.
247 * @param ro Surrounding resolver.
248 * @param tile %Tile for the callback, only valid for airporttile callbacks.
249 * @param st %Station of the airport for which the callback is run, or \c NULL for build gui.
250 * @param airport_id Type of airport for which the callback is run.
251 * @param layout Layout of the airport to build.
253 AirportScopeResolver::AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, byte airport_id, byte layout) : ScopeResolver(ro)
255 this->st = st;
256 this->airport_id = airport_id;
257 this->layout = layout;
258 this->tile = tile;
261 SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
263 AirportResolverObject object(INVALID_TILE, NULL, as->GetIndex(), layout);
264 const SpriteGroup *group = object.Resolve();
265 if (group == NULL) return as->preview_sprite;
267 return group->GetResult();
270 uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
272 AirportResolverObject object(tile, st, st->airport.type, st->airport.layout, callback, param1, param2);
273 return object.ResolveCallback();
277 * Get a custom text for the airport.
278 * @param as The airport type's specification.
279 * @param layout The layout index.
280 * @param callback The callback to call.
281 * @return The custom text.
283 StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
285 AirportResolverObject object(INVALID_TILE, NULL, as->GetIndex(), layout, (CallbackID)callback);
286 uint16 cb_res = object.ResolveCallback();
287 if (cb_res == CALLBACK_FAILED || cb_res == 0x400) return STR_UNDEFINED;
288 if (cb_res > 0x400) {
289 ErrorUnknownCallbackResult(as->grf_prop.grffile->grfid, callback, cb_res);
290 return STR_UNDEFINED;
293 return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);