OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_airport.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_airport.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
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 */
9
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"
19
20 #include "safeguards.h"
21
23 struct AirportScopeResolver : public ScopeResolver {
24 struct Station *st;
25 byte airport_id;
26 byte layout;
27 TileIndex tile;
28
29 AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, byte airport_id, byte layout);
30
31 /* virtual */ uint32 GetRandomBits() const;
32 /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
33 /* virtual */ void StorePSA(uint pos, int32 value);
34 };
35
37 struct AirportResolverObject : public ResolverObject {
38 AirportScopeResolver airport_scope;
39
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);
42
43 /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
44 {
45 switch (scope) {
46 case VSG_SCOPE_SELF: return &this->airport_scope;
47 default: return ResolverObject::GetScope(scope, relative);
48 }
49 }
50
51 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
52 };
53
59 template <typename Tspec, typename Tid, Tid Tmax>
60 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
61 {
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;
66 }
67
68 template <typename Tspec, typename Tid, Tid Tmax>
69 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
70 {
71 return true;
72 }
73
74 INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX)
75
76
77 AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
78
79 AirportSpec AirportSpec::specs[NUM_AIRPORTS];
80
87 /* static */ const AirportSpec *AirportSpec::Get(byte type)
88 {
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];
95 }
96 if (as->grf_prop.override != AT_INVALID) return &AirportSpec::specs[as->grf_prop.override];
97 return as;
98 }
99
106 /* static */ AirportSpec *AirportSpec::GetWithoutOverride(byte type)
107 {
108 assert(type < lengthof(AirportSpec::specs));
109 return &AirportSpec::specs[type];
110 }
111
113 bool AirportSpec::IsAvailable() const
114 {
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;
119 }
120
124 void AirportSpec::ResetAirports()
125 {
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);
129
130 _airport_mngr.ResetOverride();
131 }
132
136 void BindAirportSpecs()
137 {
138 for (int i = 0; i < NUM_AIRPORTS; i++) {
139 AirportSpec *as = AirportSpec::GetWithoutOverride(i);
140 if (as->enabled) AirportClass::Assign(as);
141 }
142 }
143
144
145 void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
146 {
147 byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
148
149 if (airport_id == invalid_ID) {
150 grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
151 return;
152 }
153
154 memcpy(AirportSpec::GetWithoutOverride(airport_id), as, sizeof(*as));
155
156 /* Now add the overrides. */
157 for (int i = 0; i < max_offset; i++) {
158 AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
159
160 if (entity_overrides[i] != as->grf_prop.local_id || grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
161
162 overridden_as->grf_prop.override = airport_id;
163 overridden_as->enabled = false;
164 entity_overrides[i] = invalid_ID;
165 grfid_overrides[i] = 0;
166 }
167 }
168
169 /* virtual */ uint32 AirportScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
170 {
171 switch (variable) {
172 case 0x40: return this->layout;
173 }
174
175 if (this->st == NULL) {
176 *available = false;
177 return UINT_MAX;
178 }
179
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;
183
184 case 0xF0: return this->st->facilities;
185 case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
186 }
187
188 return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
189 }
190
191 /* virtual */ const SpriteGroup *AirportResolverObject::ResolveReal(const RealSpriteGroup *group) const
192 {
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];
197
198 return NULL;
199 }
200
201 /* virtual */ uint32 AirportScopeResolver::GetRandomBits() const
202 {
203 return this->st == NULL ? 0 : this->st->random_bits;
204 }
205
212 /* virtual */ void AirportScopeResolver::StorePSA(uint pos, int32 value)
213 {
214 if (this->st == NULL) return;
215
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;
219
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);
224 }
225 this->st->airport.psa->StoreValue(pos, value);
226 }
227
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)
241 {
242 this->root_spritegroup = AirportSpec::Get(airport_id)->grf_prop.spritegroup[0];
243 }
244
253 AirportScopeResolver::AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, byte airport_id, byte layout) : ScopeResolver(ro)
254 {
255 this->st = st;
256 this->airport_id = airport_id;
257 this->layout = layout;
258 this->tile = tile;
259 }
260
261 SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
262 {
263 AirportResolverObject object(INVALID_TILE, NULL, as->GetIndex(), layout);
264 const SpriteGroup *group = object.Resolve();
265 if (group == NULL) return as->preview_sprite;
266
267 return group->GetResult();
268 }
269
270 uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
271 {
272 AirportResolverObject object(tile, st, st->airport.type, st->airport.layout, callback, param1, param2);
273 return object.ResolveCallback();
274 }
275
283 StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
284 {
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;
291 }
292
293 return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);
294 }