1 /* $Id: newgrf_airport.cpp 26388 2014-03-03 20:02:31Z frosch $ */
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/>.
10 /** @file newgrf_airport.cpp NewGRF handling of airports. */
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.
30 * Constructor of the scope resolver for an airport.
31 * @param ro Surrounding resolver.
32 * @param tile %Tile for the callback, only valid for airporttile callbacks.
33 * @param st %Station of the airport for which the callback is run, or \c NULL for build gui.
34 * @param airport_id Type of airport for which the callback is run.
35 * @param layout Layout of the airport to build.
37 AirportScopeResolver(ResolverObject
&ro
, TileIndex tile
, Station
*st
, byte airport_id
, byte layout
)
38 : ScopeResolver(ro
), st(st
), airport_id(airport_id
), layout(layout
), tile(tile
)
42 /* virtual */ uint32
GetRandomBits() const;
43 /* virtual */ uint32
GetVariable(byte variable
, uint32 parameter
, bool *available
) const;
44 /* virtual */ void StorePSA(uint pos
, int32 value
);
47 /** Resolver object for airports. */
48 struct AirportResolverObject
: public ResolverObject
{
49 AirportScopeResolver airport_scope
;
51 AirportResolverObject(TileIndex tile
, Station
*st
, byte airport_id
, byte layout
,
52 CallbackID callback
= CBID_NO_CALLBACK
, uint32 callback_param1
= 0, uint32 callback_param2
= 0);
54 /* virtual */ ScopeResolver
*GetScope(VarSpriteGroupScope scope
= VSG_SCOPE_SELF
, byte relative
= 0)
57 case VSG_SCOPE_SELF
: return &this->airport_scope
;
58 default: return ResolverObject::GetScope(scope
, relative
);
62 /* virtual */ const SpriteGroup
*ResolveReal(const RealSpriteGroup
*group
) const;
66 * Reset airport classes to their default state.
67 * This includes initialising the defaults classes with an empty
68 * entry, for standard airports.
70 template <typename Tspec
, typename Tid
, Tid Tmax
>
71 /* static */ void NewGRFClass
<Tspec
, Tid
, Tmax
>::InsertDefaults()
73 AirportClass::Get(AirportClass::Allocate('SMAL'))->name
= STR_AIRPORT_CLASS_SMALL
;
74 AirportClass::Get(AirportClass::Allocate('LARG'))->name
= STR_AIRPORT_CLASS_LARGE
;
75 AirportClass::Get(AirportClass::Allocate('HUB_'))->name
= STR_AIRPORT_CLASS_HUB
;
76 AirportClass::Get(AirportClass::Allocate('HELI'))->name
= STR_AIRPORT_CLASS_HELIPORTS
;
77 AirportClass::Get(AirportClass::Allocate('SEAP'))->name
= STR_AIRPORT_CLASS_SEA
;
80 template <typename Tspec
, typename Tid
, Tid Tmax
>
81 bool NewGRFClass
<Tspec
, Tid
, Tmax
>::IsUIAvailable(uint index
) const
86 INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass
, AirportSpec
, AirportClassID
, APC_MAX
)
89 AirportOverrideManager
_airport_mngr(NEW_AIRPORT_OFFSET
, NUM_AIRPORTS
, AT_INVALID
);
91 AirportSpec
AirportSpec::specs
[NUM_AIRPORTS
]; ///< Airport specifications.
94 * Retrieve airport spec for the given airport. If an override is available
96 * @param type index of airport
97 * @return A pointer to the corresponding AirportSpec
99 /* static */ const AirportSpec
*AirportSpec::Get(byte type
)
101 assert(type
< lengthof(AirportSpec::specs
));
102 const AirportSpec
*as
= &AirportSpec::specs
[type
];
103 if (type
>= NEW_AIRPORT_OFFSET
&& !as
->enabled
) {
104 byte subst_id
= _airport_mngr
.GetSubstituteID(type
);
105 if (subst_id
== AT_INVALID
) return as
;
106 as
= &AirportSpec::specs
[subst_id
];
108 if (as
->grf_prop
.override
!= AT_INVALID
) return &AirportSpec::specs
[as
->grf_prop
.override
];
113 * Retrieve airport spec for the given airport. Even if an override is
114 * available the base spec is returned.
115 * @param type index of airport
116 * @return A pointer to the corresponding AirportSpec
118 /* static */ AirportSpec
*AirportSpec::GetWithoutOverride(byte type
)
120 assert(type
< lengthof(AirportSpec::specs
));
121 return &AirportSpec::specs
[type
];
124 /** Check whether this airport is available to build. */
125 bool AirportSpec::IsAvailable() const
127 if (!this->enabled
) return false;
128 if (_cur_year
< this->min_year
) return false;
129 if (_settings_game
.station
.never_expire_airports
) return true;
130 return _cur_year
<= this->max_year
;
134 * This function initializes the airportspec array.
136 void AirportSpec::ResetAirports()
138 extern const AirportSpec _origin_airport_specs
[];
139 memset(&AirportSpec::specs
, 0, sizeof(AirportSpec::specs
));
140 memcpy(&AirportSpec::specs
, &_origin_airport_specs
, sizeof(AirportSpec
) * NEW_AIRPORT_OFFSET
);
142 _airport_mngr
.ResetOverride();
146 * Tie all airportspecs to their class.
148 void BindAirportSpecs()
150 for (int i
= 0; i
< NUM_AIRPORTS
; i
++) {
151 AirportSpec
*as
= AirportSpec::GetWithoutOverride(i
);
152 if (as
->enabled
) AirportClass::Assign(as
);
157 void AirportOverrideManager::SetEntitySpec(AirportSpec
*as
)
159 byte airport_id
= this->AddEntityID(as
->grf_prop
.local_id
, as
->grf_prop
.grffile
->grfid
, as
->grf_prop
.subst_id
);
161 if (airport_id
== invalid_ID
) {
162 grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
166 memcpy(AirportSpec::GetWithoutOverride(airport_id
), as
, sizeof(*as
));
168 /* Now add the overrides. */
169 for (int i
= 0; i
< max_offset
; i
++) {
170 AirportSpec
*overridden_as
= AirportSpec::GetWithoutOverride(i
);
172 if (entity_overrides
[i
] != as
->grf_prop
.local_id
|| grfid_overrides
[i
] != as
->grf_prop
.grffile
->grfid
) continue;
174 overridden_as
->grf_prop
.override
= airport_id
;
175 overridden_as
->enabled
= false;
176 entity_overrides
[i
] = invalid_ID
;
177 grfid_overrides
[i
] = 0;
181 /* virtual */ uint32
AirportScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
184 case 0x40: return this->layout
;
187 if (this->st
== NULL
) {
193 /* Get a variable from the persistent storage */
194 case 0x7C: return (this->st
->airport
.psa
!= NULL
) ? this->st
->airport
.psa
->GetValue(parameter
) : 0;
196 case 0xF0: return this->st
->facilities
;
197 case 0xFA: return Clamp(this->st
->build_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 65535);
200 return this->st
->GetNewGRFVariable(this->ro
, variable
, parameter
, available
);
203 /* virtual */ const SpriteGroup
*AirportResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
205 /* Airport action 2s should always have only 1 "loaded" state, but some
206 * times things don't follow the spec... */
207 if (group
->num_loaded
> 0) return group
->loaded
[0];
208 if (group
->num_loading
> 0) return group
->loading
[0];
213 /* virtual */ uint32
AirportScopeResolver::GetRandomBits() const
215 return this->st
== NULL
? 0 : this->st
->random_bits
;
219 * Store a value into the object's persistent storage.
220 * @param object Object that we want to query.
221 * @param pos Position in the persistent storage to use.
222 * @param value Value to store.
224 /* virtual */ void AirportScopeResolver::StorePSA(uint pos
, int32 value
)
226 if (this->st
== NULL
) return;
228 if (this->st
->airport
.psa
== NULL
) {
229 /* There is no need to create a storage if the value is zero. */
230 if (value
== 0) return;
232 /* Create storage on first modification. */
233 uint32 grfid
= (this->ro
.grffile
!= NULL
) ? this->ro
.grffile
->grfid
: 0;
234 assert(PersistentStorage::CanAllocateItem());
235 this->st
->airport
.psa
= new PersistentStorage(grfid
, GSF_AIRPORTS
, this->st
->airport
.tile
);
237 this->st
->airport
.psa
->StoreValue(pos
, value
);
241 * Constructor of the airport resolver.
242 * @param tile %Tile for the callback, only valid for airporttile callbacks.
243 * @param st %Station of the airport for which the callback is run, or \c NULL for build gui.
244 * @param airport_id Type of airport for which the callback is run.
245 * @param layout Layout of the airport to build.
246 * @param callback Callback ID.
247 * @param param1 First parameter (var 10) of the callback.
248 * @param param2 Second parameter (var 18) of the callback.
250 AirportResolverObject::AirportResolverObject(TileIndex tile
, Station
*st
, byte airport_id
, byte layout
,
251 CallbackID callback
, uint32 param1
, uint32 param2
)
252 : ResolverObject(AirportSpec::Get(airport_id
)->grf_prop
.grffile
, callback
, param1
, param2
), airport_scope(*this, tile
, st
, airport_id
, layout
)
254 this->root_spritegroup
= AirportSpec::Get(airport_id
)->grf_prop
.spritegroup
[0];
257 SpriteID
GetCustomAirportSprite(const AirportSpec
*as
, byte layout
)
259 AirportResolverObject
object(INVALID_TILE
, NULL
, as
->GetIndex(), layout
);
260 const SpriteGroup
*group
= object
.Resolve();
261 if (group
== NULL
) return as
->preview_sprite
;
263 return group
->GetResult();
266 uint16
GetAirportCallback(CallbackID callback
, uint32 param1
, uint32 param2
, Station
*st
, TileIndex tile
)
268 AirportResolverObject
object(tile
, st
, st
->airport
.type
, st
->airport
.layout
, callback
, param1
, param2
);
269 return object
.ResolveCallback();
273 * Get a custom text for the airport.
274 * @param as The airport type's specification.
275 * @param layout The layout index.
276 * @param callback The callback to call.
277 * @return The custom text.
279 StringID
GetAirportTextCallback(const AirportSpec
*as
, byte layout
, uint16 callback
)
281 AirportResolverObject
object(INVALID_TILE
, NULL
, as
->GetIndex(), layout
, (CallbackID
)callback
);
282 uint16 cb_res
= object
.ResolveCallback();
283 if (cb_res
== CALLBACK_FAILED
|| cb_res
== 0x400) return STR_UNDEFINED
;
284 if (cb_res
> 0x400) {
285 ErrorUnknownCallbackResult(as
->grf_prop
.grffile
->grfid
, callback
, cb_res
);
286 return STR_UNDEFINED
;
289 return GetGRFStringID(as
->grf_prop
.grffile
->grfid
, 0xD000 + cb_res
);