2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file newgrf_airport.cpp NewGRF handling of airports. */
12 #include "date_func.h"
13 #include "newgrf_spritegroup.h"
14 #include "newgrf_text.h"
15 #include "station_base.h"
16 #include "newgrf_class_func.h"
18 #include "safeguards.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 nullptr 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.
28 * Constructor of the scope resolver for an airport.
29 * @param ro Surrounding resolver.
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 nullptr for build gui.
32 * @param airport_id Type of airport for which the callback is run.
33 * @param layout Layout of the airport to build.
35 AirportScopeResolver(ResolverObject
&ro
, TileIndex tile
, Station
*st
, byte airport_id
, byte layout
)
36 : ScopeResolver(ro
), st(st
), airport_id(airport_id
), layout(layout
), tile(tile
)
40 uint32
GetRandomBits() const override
;
41 uint32
GetVariable(byte variable
, uint32 parameter
, bool *available
) const override
;
42 void StorePSA(uint pos
, int32 value
) override
;
45 /** Resolver object for airports. */
46 struct AirportResolverObject
: public ResolverObject
{
47 AirportScopeResolver airport_scope
;
49 AirportResolverObject(TileIndex tile
, Station
*st
, byte airport_id
, byte layout
,
50 CallbackID callback
= CBID_NO_CALLBACK
, uint32 callback_param1
= 0, uint32 callback_param2
= 0);
52 ScopeResolver
*GetScope(VarSpriteGroupScope scope
= VSG_SCOPE_SELF
, byte relative
= 0) override
55 case VSG_SCOPE_SELF
: return &this->airport_scope
;
56 default: return ResolverObject::GetScope(scope
, relative
);
60 const SpriteGroup
*ResolveReal(const RealSpriteGroup
*group
) const override
;
62 GrfSpecFeature
GetFeature() const override
;
63 uint32
GetDebugID() const override
;
67 * Reset airport classes to their default state.
68 * This includes initialising the defaults classes with an empty
69 * entry, for standard airports.
71 template <typename Tspec
, typename Tid
, Tid Tmax
>
72 /* static */ void NewGRFClass
<Tspec
, Tid
, Tmax
>::InsertDefaults()
74 AirportClass::Get(AirportClass::Allocate('SMAL'))->name
= STR_AIRPORT_CLASS_SMALL
;
75 AirportClass::Get(AirportClass::Allocate('LARG'))->name
= STR_AIRPORT_CLASS_LARGE
;
76 AirportClass::Get(AirportClass::Allocate('HUB_'))->name
= STR_AIRPORT_CLASS_HUB
;
77 AirportClass::Get(AirportClass::Allocate('HELI'))->name
= STR_AIRPORT_CLASS_HELIPORTS
;
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 if (_airport_mngr
.GetGRFID(type
) == 0) return as
;
105 byte subst_id
= _airport_mngr
.GetSubstituteID(type
);
106 if (subst_id
== AT_INVALID
) return as
;
107 as
= &AirportSpec::specs
[subst_id
];
109 if (as
->grf_prop
.override
!= AT_INVALID
) return &AirportSpec::specs
[as
->grf_prop
.override
];
114 * Retrieve airport spec for the given airport. Even if an override is
115 * available the base spec is returned.
116 * @param type index of airport
117 * @return A pointer to the corresponding AirportSpec
119 /* static */ AirportSpec
*AirportSpec::GetWithoutOverride(byte type
)
121 assert(type
< lengthof(AirportSpec::specs
));
122 return &AirportSpec::specs
[type
];
125 /** Check whether this airport is available to build. */
126 bool AirportSpec::IsAvailable() const
128 if (!this->enabled
) return false;
129 if (_cur_year
< this->min_year
) return false;
130 if (_settings_game
.station
.never_expire_airports
) return true;
131 return _cur_year
<= this->max_year
;
135 * Check if the airport would be within the map bounds at the given tile.
136 * @param table Selected layout table. This affects airport rotation, and therefore dimensions.
137 * @param tile Top corner of the airport.
138 * @return true iff the airport would be within the map bounds at the given tile.
140 bool AirportSpec::IsWithinMapBounds(byte table
, TileIndex tile
) const
142 if (table
>= this->num_table
) return false;
144 byte w
= this->size_x
;
145 byte h
= this->size_y
;
146 if (this->rotation
[table
] == DIR_E
|| this->rotation
[table
] == DIR_W
) Swap(w
, h
);
148 return TileX(tile
) + w
< MapSizeX() &&
149 TileY(tile
) + h
< MapSizeY();
153 * This function initializes the airportspec array.
155 void AirportSpec::ResetAirports()
157 extern const AirportSpec _origin_airport_specs
[];
158 memset(&AirportSpec::specs
, 0, sizeof(AirportSpec::specs
));
159 memcpy(&AirportSpec::specs
, &_origin_airport_specs
, sizeof(AirportSpec
) * NEW_AIRPORT_OFFSET
);
161 _airport_mngr
.ResetOverride();
165 * Tie all airportspecs to their class.
167 void BindAirportSpecs()
169 for (int i
= 0; i
< NUM_AIRPORTS
; i
++) {
170 AirportSpec
*as
= AirportSpec::GetWithoutOverride(i
);
171 if (as
->enabled
) AirportClass::Assign(as
);
176 void AirportOverrideManager::SetEntitySpec(AirportSpec
*as
)
178 byte airport_id
= this->AddEntityID(as
->grf_prop
.local_id
, as
->grf_prop
.grffile
->grfid
, as
->grf_prop
.subst_id
);
180 if (airport_id
== invalid_ID
) {
181 grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
185 memcpy(AirportSpec::GetWithoutOverride(airport_id
), as
, sizeof(*as
));
187 /* Now add the overrides. */
188 for (int i
= 0; i
< max_offset
; i
++) {
189 AirportSpec
*overridden_as
= AirportSpec::GetWithoutOverride(i
);
191 if (entity_overrides
[i
] != as
->grf_prop
.local_id
|| grfid_overrides
[i
] != as
->grf_prop
.grffile
->grfid
) continue;
193 overridden_as
->grf_prop
.override
= airport_id
;
194 overridden_as
->enabled
= false;
195 entity_overrides
[i
] = invalid_ID
;
196 grfid_overrides
[i
] = 0;
200 /* virtual */ uint32
AirportScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
203 case 0x40: return this->layout
;
206 if (this->st
== nullptr) {
212 /* Get a variable from the persistent storage */
213 case 0x7C: return (this->st
->airport
.psa
!= nullptr) ? this->st
->airport
.psa
->GetValue(parameter
) : 0;
215 case 0xF0: return this->st
->facilities
;
216 case 0xFA: return Clamp(this->st
->build_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 65535);
219 return this->st
->GetNewGRFVariable(this->ro
, variable
, parameter
, available
);
222 /* virtual */ const SpriteGroup
*AirportResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
224 /* Airport action 2s should always have only 1 "loaded" state, but some
225 * times things don't follow the spec... */
226 if (group
->num_loaded
> 0) return group
->loaded
[0];
227 if (group
->num_loading
> 0) return group
->loading
[0];
232 GrfSpecFeature
AirportResolverObject::GetFeature() const
237 uint32
AirportResolverObject::GetDebugID() const
239 return AirportSpec::Get(this->airport_scope
.airport_id
)->grf_prop
.local_id
;
242 /* virtual */ uint32
AirportScopeResolver::GetRandomBits() const
244 return this->st
== nullptr ? 0 : this->st
->random_bits
;
248 * Store a value into the object's persistent storage.
249 * @param pos Position in the persistent storage to use.
250 * @param value Value to store.
252 /* virtual */ void AirportScopeResolver::StorePSA(uint pos
, int32 value
)
254 if (this->st
== nullptr) return;
256 if (this->st
->airport
.psa
== nullptr) {
257 /* There is no need to create a storage if the value is zero. */
258 if (value
== 0) return;
260 /* Create storage on first modification. */
261 uint32 grfid
= (this->ro
.grffile
!= nullptr) ? this->ro
.grffile
->grfid
: 0;
262 assert(PersistentStorage::CanAllocateItem());
263 this->st
->airport
.psa
= new PersistentStorage(grfid
, GSF_AIRPORTS
, this->st
->airport
.tile
);
265 this->st
->airport
.psa
->StoreValue(pos
, value
);
269 * Constructor of the airport resolver.
270 * @param tile %Tile for the callback, only valid for airporttile callbacks.
271 * @param st %Station of the airport for which the callback is run, or \c nullptr for build gui.
272 * @param airport_id Type of airport for which the callback is run.
273 * @param layout Layout of the airport to build.
274 * @param callback Callback ID.
275 * @param param1 First parameter (var 10) of the callback.
276 * @param param2 Second parameter (var 18) of the callback.
278 AirportResolverObject::AirportResolverObject(TileIndex tile
, Station
*st
, byte airport_id
, byte layout
,
279 CallbackID callback
, uint32 param1
, uint32 param2
)
280 : ResolverObject(AirportSpec::Get(airport_id
)->grf_prop
.grffile
, callback
, param1
, param2
), airport_scope(*this, tile
, st
, airport_id
, layout
)
282 this->root_spritegroup
= AirportSpec::Get(airport_id
)->grf_prop
.spritegroup
[0];
285 SpriteID
GetCustomAirportSprite(const AirportSpec
*as
, byte layout
)
287 AirportResolverObject
object(INVALID_TILE
, nullptr, as
->GetIndex(), layout
);
288 const SpriteGroup
*group
= object
.Resolve();
289 if (group
== nullptr) return as
->preview_sprite
;
291 return group
->GetResult();
294 uint16
GetAirportCallback(CallbackID callback
, uint32 param1
, uint32 param2
, Station
*st
, TileIndex tile
)
296 AirportResolverObject
object(tile
, st
, st
->airport
.type
, st
->airport
.layout
, callback
, param1
, param2
);
297 return object
.ResolveCallback();
301 * Get a custom text for the airport.
302 * @param as The airport type's specification.
303 * @param layout The layout index.
304 * @param callback The callback to call.
305 * @return The custom text.
307 StringID
GetAirportTextCallback(const AirportSpec
*as
, byte layout
, uint16 callback
)
309 AirportResolverObject
object(INVALID_TILE
, nullptr, as
->GetIndex(), layout
, (CallbackID
)callback
);
310 uint16 cb_res
= object
.ResolveCallback();
311 if (cb_res
== CALLBACK_FAILED
|| cb_res
== 0x400) return STR_UNDEFINED
;
312 if (cb_res
> 0x400) {
313 ErrorUnknownCallbackResult(as
->grf_prop
.grffile
->grfid
, callback
, cb_res
);
314 return STR_UNDEFINED
;
317 return GetGRFStringID(as
->grf_prop
.grffile
->grfid
, 0xD000 + cb_res
);