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 "timer/timer_game_calendar.h"
13 #include "newgrf_spritegroup.h"
14 #include "newgrf_text.h"
15 #include "station_base.h"
16 #include "newgrf_class_func.h"
19 #include "safeguards.h"
22 * Reset airport classes to their default state.
23 * This includes initialising the defaults classes with an empty
24 * entry, for standard airports.
27 /* static */ void AirportClass::InsertDefaults()
29 AirportClass::Get(AirportClass::Allocate('SMAL'))->name
= STR_AIRPORT_CLASS_SMALL
;
30 AirportClass::Get(AirportClass::Allocate('LARG'))->name
= STR_AIRPORT_CLASS_LARGE
;
31 AirportClass::Get(AirportClass::Allocate('HUB_'))->name
= STR_AIRPORT_CLASS_HUB
;
32 AirportClass::Get(AirportClass::Allocate('HELI'))->name
= STR_AIRPORT_CLASS_HELIPORTS
;
36 bool AirportClass::IsUIAvailable(uint
) const
41 /* Instantiate AirportClass. */
42 template class NewGRFClass
<AirportSpec
, AirportClassID
, APC_MAX
>;
45 AirportOverrideManager
_airport_mngr(NEW_AIRPORT_OFFSET
, NUM_AIRPORTS
, AT_INVALID
);
47 AirportSpec
AirportSpec::specs
[NUM_AIRPORTS
]; ///< Airport specifications.
50 * Retrieve airport spec for the given airport. If an override is available
52 * @param type index of airport
53 * @return A pointer to the corresponding AirportSpec
55 /* static */ const AirportSpec
*AirportSpec::Get(uint8_t type
)
57 assert(type
< lengthof(AirportSpec::specs
));
58 const AirportSpec
*as
= &AirportSpec::specs
[type
];
59 if (type
>= NEW_AIRPORT_OFFSET
&& !as
->enabled
) {
60 if (_airport_mngr
.GetGRFID(type
) == 0) return as
;
61 uint8_t subst_id
= _airport_mngr
.GetSubstituteID(type
);
62 if (subst_id
== AT_INVALID
) return as
;
63 as
= &AirportSpec::specs
[subst_id
];
65 if (as
->grf_prop
.override
!= AT_INVALID
) return &AirportSpec::specs
[as
->grf_prop
.override
];
70 * Retrieve airport spec for the given airport. Even if an override is
71 * available the base spec is returned.
72 * @param type index of airport
73 * @return A pointer to the corresponding AirportSpec
75 /* static */ AirportSpec
*AirportSpec::GetWithoutOverride(uint8_t type
)
77 assert(type
< lengthof(AirportSpec::specs
));
78 return &AirportSpec::specs
[type
];
81 /** Check whether this airport is available to build. */
82 bool AirportSpec::IsAvailable() const
84 if (!this->enabled
) return false;
85 if (TimerGameCalendar::year
< this->min_year
) return false;
86 if (_settings_game
.station
.never_expire_airports
) return true;
87 return TimerGameCalendar::year
<= this->max_year
;
91 * Check if the airport would be within the map bounds at the given tile.
92 * @param table Selected layout table. This affects airport rotation, and therefore dimensions.
93 * @param tile Top corner of the airport.
94 * @return true iff the airport would be within the map bounds at the given tile.
96 bool AirportSpec::IsWithinMapBounds(uint8_t table
, TileIndex tile
) const
98 if (table
>= this->layouts
.size()) return false;
100 uint8_t w
= this->size_x
;
101 uint8_t h
= this->size_y
;
102 if (this->layouts
[table
].rotation
== DIR_E
|| this->layouts
[table
].rotation
== DIR_W
) Swap(w
, h
);
104 return TileX(tile
) + w
< Map::SizeX() &&
105 TileY(tile
) + h
< Map::SizeY();
109 * This function initializes the airportspec array.
111 void AirportSpec::ResetAirports()
113 extern const AirportSpec _origin_airport_specs
[NEW_AIRPORT_OFFSET
];
115 auto insert
= std::copy(std::begin(_origin_airport_specs
), std::end(_origin_airport_specs
), std::begin(AirportSpec::specs
));
116 std::fill(insert
, std::end(AirportSpec::specs
), AirportSpec
{});
118 _airport_mngr
.ResetOverride();
122 * Tie all airportspecs to their class.
124 void BindAirportSpecs()
126 for (int i
= 0; i
< NUM_AIRPORTS
; i
++) {
127 AirportSpec
*as
= AirportSpec::GetWithoutOverride(i
);
128 if (as
->enabled
) AirportClass::Assign(as
);
133 void AirportOverrideManager::SetEntitySpec(AirportSpec
*as
)
135 uint8_t airport_id
= this->AddEntityID(as
->grf_prop
.local_id
, as
->grf_prop
.grffile
->grfid
, as
->grf_prop
.subst_id
);
137 if (airport_id
== this->invalid_id
) {
138 GrfMsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
142 *AirportSpec::GetWithoutOverride(airport_id
) = *as
;
144 /* Now add the overrides. */
145 for (int i
= 0; i
< this->max_offset
; i
++) {
146 AirportSpec
*overridden_as
= AirportSpec::GetWithoutOverride(i
);
148 if (this->entity_overrides
[i
] != as
->grf_prop
.local_id
|| this->grfid_overrides
[i
] != as
->grf_prop
.grffile
->grfid
) continue;
150 overridden_as
->grf_prop
.override
= airport_id
;
151 overridden_as
->enabled
= false;
152 this->entity_overrides
[i
] = this->invalid_id
;
153 this->grfid_overrides
[i
] = 0;
157 /* virtual */ uint32_t AirportScopeResolver::GetVariable(uint8_t variable
, [[maybe_unused
]] uint32_t parameter
, bool &available
) const
160 case 0x40: return this->layout
;
163 if (this->st
== nullptr) {
169 /* Get a variable from the persistent storage */
170 case 0x7C: return (this->st
->airport
.psa
!= nullptr) ? this->st
->airport
.psa
->GetValue(parameter
) : 0;
172 case 0xF0: return this->st
->facilities
;
173 case 0xFA: return ClampTo
<uint16_t>(this->st
->build_date
- CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR
);
176 return this->st
->GetNewGRFVariable(this->ro
, variable
, parameter
, available
);
179 GrfSpecFeature
AirportResolverObject::GetFeature() const
184 uint32_t AirportResolverObject::GetDebugID() const
186 return this->airport_scope
.spec
->grf_prop
.local_id
;
189 /* virtual */ uint32_t AirportScopeResolver::GetRandomBits() const
191 return this->st
== nullptr ? 0 : this->st
->random_bits
;
195 * Store a value into the object's persistent storage.
196 * @param pos Position in the persistent storage to use.
197 * @param value Value to store.
199 /* virtual */ void AirportScopeResolver::StorePSA(uint pos
, int32_t value
)
201 if (this->st
== nullptr) return;
203 if (this->st
->airport
.psa
== nullptr) {
204 /* There is no need to create a storage if the value is zero. */
205 if (value
== 0) return;
207 /* Create storage on first modification. */
208 uint32_t grfid
= (this->ro
.grffile
!= nullptr) ? this->ro
.grffile
->grfid
: 0;
209 assert(PersistentStorage::CanAllocateItem());
210 this->st
->airport
.psa
= new PersistentStorage(grfid
, GSF_AIRPORTS
, this->st
->airport
.tile
);
212 this->st
->airport
.psa
->StoreValue(pos
, value
);
216 * Get the town scope associated with a station, if it exists.
217 * On the first call, the town scope is created (if possible).
218 * @return Town scope, if available.
220 TownScopeResolver
*AirportResolverObject::GetTown()
222 if (!this->town_scope
.has_value()) {
224 if (this->airport_scope
.st
!= nullptr) {
225 t
= this->airport_scope
.st
->town
;
226 } else if (this->airport_scope
.tile
!= INVALID_TILE
) {
227 t
= ClosestTownFromTile(this->airport_scope
.tile
, UINT_MAX
);
229 if (t
== nullptr) return nullptr;
230 this->town_scope
.emplace(*this, t
, this->airport_scope
.st
== nullptr);
232 return &*this->town_scope
;
236 * Constructor of the airport resolver.
237 * @param tile %Tile for the callback, only valid for airporttile callbacks.
238 * @param st %Station of the airport for which the callback is run, or \c nullptr for build gui.
239 * @param spec AirportSpec for which the callback is run.
240 * @param layout Layout of the airport to build.
241 * @param callback Callback ID.
242 * @param param1 First parameter (var 10) of the callback.
243 * @param param2 Second parameter (var 18) of the callback.
245 AirportResolverObject::AirportResolverObject(TileIndex tile
, Station
*st
, const AirportSpec
*spec
, uint8_t layout
,
246 CallbackID callback
, uint32_t param1
, uint32_t param2
)
247 : ResolverObject(spec
->grf_prop
.grffile
, callback
, param1
, param2
), airport_scope(*this, tile
, st
, spec
, layout
)
249 this->root_spritegroup
= spec
->grf_prop
.spritegroup
[0];
252 SpriteID
GetCustomAirportSprite(const AirportSpec
*as
, uint8_t layout
)
254 AirportResolverObject
object(INVALID_TILE
, nullptr, as
, layout
);
255 const SpriteGroup
*group
= object
.Resolve();
256 if (group
== nullptr) return as
->preview_sprite
;
258 return group
->GetResult();
261 uint16_t GetAirportCallback(CallbackID callback
, uint32_t param1
, uint32_t param2
, Station
*st
, TileIndex tile
)
263 AirportResolverObject
object(tile
, st
, AirportSpec::Get(st
->airport
.type
), st
->airport
.layout
, callback
, param1
, param2
);
264 return object
.ResolveCallback();
268 * Get a custom text for the airport.
269 * @param as The airport type's specification.
270 * @param layout The layout index.
271 * @param callback The callback to call.
272 * @return The custom text.
274 StringID
GetAirportTextCallback(const AirportSpec
*as
, uint8_t layout
, uint16_t callback
)
276 AirportResolverObject
object(INVALID_TILE
, nullptr, as
, layout
, (CallbackID
)callback
);
277 uint16_t cb_res
= object
.ResolveCallback();
278 if (cb_res
== CALLBACK_FAILED
|| cb_res
== 0x400) return STR_UNDEFINED
;
279 if (cb_res
> 0x400) {
280 ErrorUnknownCallbackResult(as
->grf_prop
.grffile
->grfid
, callback
, cb_res
);
281 return STR_UNDEFINED
;
284 return GetGRFStringID(as
->grf_prop
.grffile
->grfid
, 0xD000 + cb_res
);