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 /** Resolver for the airport scope. */
21 struct AirportScopeResolver
: public ScopeResolver
{
22 struct Station
*st
; ///< Station of the airport for which the callback is run, or \c NULL for build gui.
23 byte airport_id
; ///< Type of airport for which the callback is run.
24 byte layout
; ///< Layout of the airport to build.
25 TileIndex tile
; ///< Tile for the callback, only valid for airporttile callbacks.
27 AirportScopeResolver(ResolverObject
&ro
, TileIndex tile
, Station
*st
, byte airport_id
, byte layout
);
29 /* virtual */ uint32
GetRandomBits() const;
30 /* virtual */ uint32
GetVariable(byte variable
, uint32 parameter
, bool *available
) const;
31 /* virtual */ void StorePSA(uint pos
, int32 value
);
34 /** Resolver object for airports. */
35 struct AirportResolverObject
: public ResolverObject
{
36 AirportScopeResolver airport_scope
;
38 AirportResolverObject(TileIndex tile
, Station
*st
, byte airport_id
, byte layout
,
39 CallbackID callback
= CBID_NO_CALLBACK
, uint32 callback_param1
= 0, uint32 callback_param2
= 0);
41 /* virtual */ ScopeResolver
*GetScope(VarSpriteGroupScope scope
= VSG_SCOPE_SELF
, byte relative
= 0)
44 case VSG_SCOPE_SELF
: return &this->airport_scope
;
45 default: return ResolverObject::GetScope(scope
, relative
);
49 /* virtual */ const SpriteGroup
*ResolveReal(const RealSpriteGroup
*group
) const;
53 * Reset airport classes to their default state.
54 * This includes initialising the defaults classes with an empty
55 * entry, for standard airports.
57 template <typename Tspec
, typename Tid
, Tid Tmax
>
58 /* static */ void NewGRFClass
<Tspec
, Tid
, Tmax
>::InsertDefaults()
60 AirportClass::Get(AirportClass::Allocate('SMAL'))->name
= STR_AIRPORT_CLASS_SMALL
;
61 AirportClass::Get(AirportClass::Allocate('LARG'))->name
= STR_AIRPORT_CLASS_LARGE
;
62 AirportClass::Get(AirportClass::Allocate('HUB_'))->name
= STR_AIRPORT_CLASS_HUB
;
63 AirportClass::Get(AirportClass::Allocate('HELI'))->name
= STR_AIRPORT_CLASS_HELIPORTS
;
66 template <typename Tspec
, typename Tid
, Tid Tmax
>
67 bool NewGRFClass
<Tspec
, Tid
, Tmax
>::IsUIAvailable(uint index
) const
72 INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass
, AirportSpec
, AirportClassID
, APC_MAX
)
75 AirportOverrideManager
_airport_mngr(NEW_AIRPORT_OFFSET
, NUM_AIRPORTS
, AT_INVALID
);
77 AirportSpec
AirportSpec::specs
[NUM_AIRPORTS
]; ///< Airport specifications.
80 * Retrieve airport spec for the given airport. If an override is available
82 * @param type index of airport
83 * @return A pointer to the corresponding AirportSpec
85 /* static */ const AirportSpec
*AirportSpec::Get(byte type
)
87 assert(type
< lengthof(AirportSpec::specs
));
88 const AirportSpec
*as
= &AirportSpec::specs
[type
];
89 if (type
>= NEW_AIRPORT_OFFSET
&& !as
->enabled
) {
90 byte subst_id
= _airport_mngr
.GetSubstituteID(type
);
91 if (subst_id
== AT_INVALID
) return as
;
92 as
= &AirportSpec::specs
[subst_id
];
94 if (as
->grf_prop
.override
!= AT_INVALID
) return &AirportSpec::specs
[as
->grf_prop
.override
];
99 * Retrieve airport spec for the given airport. Even if an override is
100 * available the base spec is returned.
101 * @param type index of airport
102 * @return A pointer to the corresponding AirportSpec
104 /* static */ AirportSpec
*AirportSpec::GetWithoutOverride(byte type
)
106 assert(type
< lengthof(AirportSpec::specs
));
107 return &AirportSpec::specs
[type
];
110 /** Check whether this airport is available to build. */
111 bool AirportSpec::IsAvailable() const
113 if (!this->enabled
) return false;
114 if (_cur_year
< this->min_year
) return false;
115 if (_settings_game
.station
.never_expire_airports
) return true;
116 return _cur_year
<= this->max_year
;
120 * This function initializes the airportspec array.
122 void AirportSpec::ResetAirports()
124 extern const AirportSpec _origin_airport_specs
[];
125 memset(&AirportSpec::specs
, 0, sizeof(AirportSpec::specs
));
126 memcpy(&AirportSpec::specs
, &_origin_airport_specs
, sizeof(AirportSpec
) * NEW_AIRPORT_OFFSET
);
128 _airport_mngr
.ResetOverride();
132 * Tie all airportspecs to their class.
134 void BindAirportSpecs()
136 for (int i
= 0; i
< NUM_AIRPORTS
; i
++) {
137 AirportSpec
*as
= AirportSpec::GetWithoutOverride(i
);
138 if (as
->enabled
) AirportClass::Assign(as
);
143 void AirportOverrideManager::SetEntitySpec(AirportSpec
*as
)
145 byte airport_id
= this->AddEntityID(as
->grf_prop
.local_id
, as
->grf_prop
.grffile
->grfid
, as
->grf_prop
.subst_id
);
147 if (airport_id
== invalid_ID
) {
148 grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
152 memcpy(AirportSpec::GetWithoutOverride(airport_id
), as
, sizeof(*as
));
154 /* Now add the overrides. */
155 for (int i
= 0; i
< max_offset
; i
++) {
156 AirportSpec
*overridden_as
= AirportSpec::GetWithoutOverride(i
);
158 if (entity_overrides
[i
] != as
->grf_prop
.local_id
|| grfid_overrides
[i
] != as
->grf_prop
.grffile
->grfid
) continue;
160 overridden_as
->grf_prop
.override
= airport_id
;
161 overridden_as
->enabled
= false;
162 entity_overrides
[i
] = invalid_ID
;
163 grfid_overrides
[i
] = 0;
167 /* virtual */ uint32
AirportScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
170 case 0x40: return this->layout
;
173 if (this->st
== NULL
) {
179 /* Get a variable from the persistent storage */
180 case 0x7C: return (this->st
->airport
.psa
!= NULL
) ? this->st
->airport
.psa
->GetValue(parameter
) : 0;
182 case 0xF0: return this->st
->facilities
;
183 case 0xFA: return Clamp(this->st
->build_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 65535);
186 return this->st
->GetNewGRFVariable(this->ro
, variable
, parameter
, available
);
189 /* virtual */ const SpriteGroup
*AirportResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
191 /* Airport action 2s should always have only 1 "loaded" state, but some
192 * times things don't follow the spec... */
193 if (group
->num_loaded
> 0) return group
->loaded
[0];
194 if (group
->num_loading
> 0) return group
->loading
[0];
199 /* virtual */ uint32
AirportScopeResolver::GetRandomBits() const
201 return this->st
== NULL
? 0 : this->st
->random_bits
;
205 * Store a value into the object's persistent storage.
206 * @param object Object that we want to query.
207 * @param pos Position in the persistent storage to use.
208 * @param value Value to store.
210 /* virtual */ void AirportScopeResolver::StorePSA(uint pos
, int32 value
)
212 if (this->st
== NULL
) return;
214 if (this->st
->airport
.psa
== NULL
) {
215 /* There is no need to create a storage if the value is zero. */
216 if (value
== 0) return;
218 /* Create storage on first modification. */
219 uint32 grfid
= (this->ro
.grffile
!= NULL
) ? this->ro
.grffile
->grfid
: 0;
220 assert(PersistentStorage::CanAllocateItem());
221 this->st
->airport
.psa
= new PersistentStorage(grfid
, GSF_AIRPORTS
, this->st
->airport
.tile
);
223 this->st
->airport
.psa
->StoreValue(pos
, value
);
227 * Constructor of the airport resolver.
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.
230 * @param airport_id Type of airport for which the callback is run.
231 * @param layout Layout of the airport to build.
232 * @param callback Callback ID.
233 * @param param1 First parameter (var 10) of the callback.
234 * @param param2 Second parameter (var 18) of the callback.
236 AirportResolverObject::AirportResolverObject(TileIndex tile
, Station
*st
, byte airport_id
, byte layout
,
237 CallbackID callback
, uint32 param1
, uint32 param2
)
238 : ResolverObject(AirportSpec::Get(airport_id
)->grf_prop
.grffile
, callback
, param1
, param2
), airport_scope(*this, tile
, st
, airport_id
, layout
)
243 * Constructor of the scope resolver for an airport.
244 * @param ro Surrounding resolver.
245 * @param tile %Tile for the callback, only valid for airporttile callbacks.
246 * @param st %Station of the airport for which the callback is run, or \c NULL for build gui.
247 * @param airport_id Type of airport for which the callback is run.
248 * @param layout Layout of the airport to build.
250 AirportScopeResolver::AirportScopeResolver(ResolverObject
&ro
, TileIndex tile
, Station
*st
, byte airport_id
, byte layout
) : ScopeResolver(ro
)
253 this->airport_id
= airport_id
;
254 this->layout
= layout
;
258 SpriteID
GetCustomAirportSprite(const AirportSpec
*as
, byte layout
)
260 AirportResolverObject
object(INVALID_TILE
, NULL
, as
->GetIndex(), layout
);
261 const SpriteGroup
*group
= SpriteGroup::Resolve(as
->grf_prop
.spritegroup
[0], object
);
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 AirportResolverObject
object(tile
, st
, st
->airport
.type
, st
->airport
.layout
, callback
, param1
, param2
);
270 const SpriteGroup
*group
= SpriteGroup::Resolve(st
->airport
.GetSpec()->grf_prop
.spritegroup
[0], object
);
271 if (group
== NULL
) return CALLBACK_FAILED
;
273 return group
->GetCallbackResult();
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 const SpriteGroup
*group
= SpriteGroup::Resolve(as
->grf_prop
.spritegroup
[0], object
);
287 uint16 cb_res
= (group
!= NULL
) ? group
->GetCallbackResult() : CALLBACK_FAILED
;
288 if (cb_res
== CALLBACK_FAILED
|| cb_res
== 0x400) return STR_UNDEFINED
;
289 if (cb_res
> 0x400) {
290 ErrorUnknownCallbackResult(as
->grf_prop
.grffile
->grfid
, callback
, cb_res
);
291 return STR_UNDEFINED
;
294 return GetGRFStringID(as
->grf_prop
.grffile
->grfid
, 0xD000 + cb_res
);