OpenTTD Source  20240917-master-g9ab0a47812
newgrf_airport.cpp
Go to the documentation of this file.
1 /*
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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "debug.h"
13 #include "newgrf_spritegroup.h"
14 #include "newgrf_text.h"
15 #include "station_base.h"
16 #include "newgrf_class_func.h"
17 #include "town.h"
18 
19 #include "safeguards.h"
20 
26 template <>
27 /* static */ void AirportClass::InsertDefaults()
28 {
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;
33 }
34 
35 template <>
37 {
38  return true;
39 }
40 
41 /* Instantiate AirportClass. */
43 
44 
46 
48 
55 /* static */ const AirportSpec *AirportSpec::Get(uint8_t type)
56 {
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];
64  }
66  return as;
67 }
68 
75 /* static */ AirportSpec *AirportSpec::GetWithoutOverride(uint8_t type)
76 {
77  assert(type < lengthof(AirportSpec::specs));
78  return &AirportSpec::specs[type];
79 }
80 
83 {
84  if (!this->enabled) return false;
85  if (TimerGameCalendar::year < this->min_year) return false;
87  return TimerGameCalendar::year <= this->max_year;
88 }
89 
96 bool AirportSpec::IsWithinMapBounds(uint8_t table, TileIndex tile) const
97 {
98  if (table >= this->layouts.size()) return false;
99 
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);
103 
104  return TileX(tile) + w < Map::SizeX() &&
105  TileY(tile) + h < Map::SizeY();
106 }
107 
112 {
113  extern const AirportSpec _origin_airport_specs[NEW_AIRPORT_OFFSET];
114 
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{});
117 
118  _airport_mngr.ResetOverride();
119 }
120 
125 {
126  for (int i = 0; i < NUM_AIRPORTS; i++) {
128  if (as->enabled) AirportClass::Assign(as);
129  }
130 }
131 
132 
133 void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
134 {
135  uint8_t airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
136 
137  if (airport_id == this->invalid_id) {
138  GrfMsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
139  return;
140  }
141 
142  *AirportSpec::GetWithoutOverride(airport_id) = *as;
143 
144  /* Now add the overrides. */
145  for (int i = 0; i < this->max_offset; i++) {
146  AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
147 
148  if (this->entity_overrides[i] != as->grf_prop.local_id || this->grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
149 
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;
154  }
155 }
156 
157 /* virtual */ uint32_t AirportScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
158 {
159  switch (variable) {
160  case 0x40: return this->layout;
161  }
162 
163  if (this->st == nullptr) {
164  available = false;
165  return UINT_MAX;
166  }
167 
168  switch (variable) {
169  /* Get a variable from the persistent storage */
170  case 0x7C: return (this->st->airport.psa != nullptr) ? this->st->airport.psa->GetValue(parameter) : 0;
171 
172  case 0xF0: return this->st->facilities;
173  case 0xFA: return ClampTo<uint16_t>(this->st->build_date - CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR);
174  }
175 
176  return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
177 }
178 
180 {
181  return GSF_AIRPORTS;
182 }
183 
185 {
186  return this->airport_scope.spec->grf_prop.local_id;
187 }
188 
189 /* virtual */ uint32_t AirportScopeResolver::GetRandomBits() const
190 {
191  return this->st == nullptr ? 0 : this->st->random_bits;
192 }
193 
199 /* virtual */ void AirportScopeResolver::StorePSA(uint pos, int32_t value)
200 {
201  if (this->st == nullptr) return;
202 
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;
206 
207  /* Create storage on first modification. */
208  uint32_t grfid = (this->ro.grffile != nullptr) ? this->ro.grffile->grfid : 0;
210  this->st->airport.psa = new PersistentStorage(grfid, GSF_AIRPORTS, this->st->airport.tile);
211  }
212  this->st->airport.psa->StoreValue(pos, value);
213 }
214 
221 {
222  if (!this->town_scope.has_value()) {
223  Town *t = nullptr;
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);
228  }
229  if (t == nullptr) return nullptr;
230  this->town_scope.emplace(*this, t, this->airport_scope.st == nullptr);
231  }
232  return &*this->town_scope;
233 }
234 
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)
248 {
249  this->root_spritegroup = spec->grf_prop.spritegroup[0];
250 }
251 
252 SpriteID GetCustomAirportSprite(const AirportSpec *as, uint8_t layout)
253 {
254  AirportResolverObject object(INVALID_TILE, nullptr, as, layout);
255  const SpriteGroup *group = object.Resolve();
256  if (group == nullptr) return as->preview_sprite;
257 
258  return group->GetResult();
259 }
260 
261 uint16_t GetAirportCallback(CallbackID callback, uint32_t param1, uint32_t param2, Station *st, TileIndex tile)
262 {
263  AirportResolverObject object(tile, st, AirportSpec::Get(st->airport.type), st->airport.layout, callback, param1, param2);
264  return object.ResolveCallback();
265 }
266 
274 StringID GetAirportTextCallback(const AirportSpec *as, uint8_t layout, uint16_t callback)
275 {
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;
282  }
283 
284  return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);
285 }
TileY
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:437
BaseStation::facilities
StationFacility facilities
The facilities that this station has.
Definition: base_station_base.h:70
OverrideManagerBase::GetGRFID
uint32_t GetGRFID(uint16_t entity_id) const
Gives the GRFID of the file the entity belongs to.
Definition: newgrf_commons.cpp:139
GameSettings::station
StationSettings station
settings related to station management
Definition: settings_type.h:605
DIR_E
@ DIR_E
East.
Definition: direction_type.h:28
TimerGameConst< struct Calendar >::DAYS_TILL_ORIGINAL_BASE_YEAR
static constexpr TimerGame< struct Calendar >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
The date of the first day of the original base year.
Definition: timer_game_common.h:184
AirportResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition: newgrf_airport.cpp:179
Swap
constexpr void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:283
ClosestTownFromTile
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
Definition: town_cmd.cpp:3862
AirportScopeResolver::GetVariable
uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override
Get a variable value.
Definition: newgrf_airport.cpp:157
BaseStation::town
Town * town
The town this station is associated with.
Definition: base_station_base.h:68
timer_game_calendar.h
Station
Station data structure.
Definition: station_base.h:439
AirportSpec::layouts
std::vector< AirportTileLayout > layouts
List of layouts composing the airport.
Definition: newgrf_airport.h:107
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
NewGRFClass::Assign
static void Assign(Tspec *spec)
Assign a spec to one of the classes.
Definition: newgrf_class_func.h:70
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:308
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
AirportScopeResolver::st
struct Station * st
Station of the airport for which the callback is run, or nullptr for build gui.
Definition: newgrf_airport.h:151
ResolverObject::grffile
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
Definition: newgrf_spritegroup.h:336
AirportSpec::min_year
TimerGameCalendar::Year min_year
first year the airport is available
Definition: newgrf_airport.h:113
AirportSpec::max_year
TimerGameCalendar::Year max_year
last year the airport is available
Definition: newgrf_airport.h:114
AirportSpec::ResetAirports
static void ResetAirports()
This function initializes the airportspec array.
Definition: newgrf_airport.cpp:111
town.h
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
newgrf_class_func.h
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition: newgrf_callbacks.h:20
NewGRFClass::InsertDefaults
static void InsertDefaults()
Initialise the defaults.
Definition: newgrf_airport.cpp:27
NewGRFClass::IsUIAvailable
bool IsUIAvailable(uint index) const
Check whether the spec will be available to the user at some point in time.
Definition: newgrf_airport.cpp:36
AirportSpec
Defines the data structure for an airport.
Definition: newgrf_airport.h:105
NewGRFClass::name
StringID name
Name of this class.
Definition: newgrf_class.h:49
AirportScopeResolver::layout
uint8_t layout
Layout of the airport to build.
Definition: newgrf_airport.h:153
OverrideManagerBase::invalid_id
uint16_t invalid_id
ID used to detected invalid entities.
Definition: newgrf_commons.h:199
GRFFileProps::override
uint16_t override
id of the entity been replaced by
Definition: newgrf_commons.h:322
DIR_W
@ DIR_W
West.
Definition: direction_type.h:32
AirportResolverObject::AirportResolverObject
AirportResolverObject(TileIndex tile, Station *st, const AirportSpec *spec, uint8_t layout, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Constructor of the airport resolver.
Definition: newgrf_airport.cpp:245
BindAirportSpecs
void BindAirportSpecs()
Tie all airportspecs to their class.
Definition: newgrf_airport.cpp:124
PersistentStorageArray::GetValue
TYPE GetValue(uint pos) const
Gets the value from a given position.
Definition: newgrf_storage.h:108
ResolverObject::root_spritegroup
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition: newgrf_spritegroup.h:337
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:199
Station::airport
Airport airport
Tile area the airport covers.
Definition: station_base.h:453
StationSettings::never_expire_airports
bool never_expire_airports
never expire airports
Definition: settings_type.h:569
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
safeguards.h
AT_INVALID
@ AT_INVALID
Invalid airport.
Definition: airport.h:42
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:280
NUM_AIRPORTS
@ NUM_AIRPORTS
Maximal number of airports in total.
Definition: airport.h:41
OverrideManagerBase::GetSubstituteID
uint16_t GetSubstituteID(uint16_t entity_id) const
Gives the substitute of the entity, as specified by the grf file.
Definition: newgrf_commons.cpp:149
AirportResolverObject
Resolver object for airports.
Definition: newgrf_airport.h:176
newgrf_text.h
TownScopeResolver
Scope resolver for a town.
Definition: newgrf_town.h:22
OverrideManagerBase::AddEntityID
virtual uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id)
Reserves a place in the mapping array for an entity to be installed.
Definition: newgrf_commons.cpp:109
GetGRFStringID
StringID GetGRFStringID(uint32_t grfid, StringID stringid)
Returns the index for this stringid associated with its grfID.
Definition: newgrf_text.cpp:587
stdafx.h
GetAirportTextCallback
StringID GetAirportTextCallback(const AirportSpec *as, uint8_t layout, uint16_t callback)
Get a custom text for the airport.
Definition: newgrf_airport.cpp:274
AirportSpec::specs
static AirportSpec specs[NUM_AIRPORTS]
Specs of the airports.
Definition: newgrf_airport.h:141
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:18
AirportSpec::IsAvailable
bool IsAvailable() const
Check whether this airport is available to build.
Definition: newgrf_airport.cpp:82
GRFFilePropsBase::local_id
uint16_t local_id
id defined by the grf file for this entity
Definition: newgrf_commons.h:311
AirportSpec::size_x
uint8_t size_x
size of airport in x direction
Definition: newgrf_airport.h:109
newgrf_spritegroup.h
AirportResolverObject::town_scope
std::optional< TownScopeResolver > town_scope
The town scope resolver (created on the first call).
Definition: newgrf_airport.h:178
Map::SizeX
static debug_inline uint SizeX()
Get the size of the map along the X.
Definition: map_func.h:270
GrfSpecFeature
GrfSpecFeature
Definition: newgrf.h:67
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:420
station_base.h
AirportScopeResolver::spec
const AirportSpec * spec
AirportSpec for which the callback is run.
Definition: newgrf_airport.h:152
AirportSpec::enabled
bool enabled
Entity still available (by default true). Newgrf can disable it, though.
Definition: newgrf_airport.h:120
AirportSpec::preview_sprite
SpriteID preview_sprite
preview sprite for this airport
Definition: newgrf_airport.h:117
NEW_AIRPORT_OFFSET
@ NEW_AIRPORT_OFFSET
Number of the first newgrf airport.
Definition: airport.h:39
AirportSpec::size_y
uint8_t size_y
size of airport in y direction
Definition: newgrf_airport.h:110
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
NewGRFClass::Allocate
static Tindex Allocate(uint32_t global_id)
Allocate a class with a given global class ID.
Definition: newgrf_class_func.h:32
AirportSpec::grf_prop
struct GRFFileProps grf_prop
Properties related to the grf file.
Definition: newgrf_airport.h:121
OverrideManagerBase::ResetOverride
void ResetOverride()
Resets the override, which is used while initializing game.
Definition: newgrf_commons.cpp:78
Pool::PoolItem<&_persistent_storage_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:309
AirportResolverObject::GetDebugID
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Definition: newgrf_airport.cpp:184
NewGRFClass
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:26
BaseStation::random_bits
uint16_t random_bits
Random bits assigned to this station.
Definition: base_station_base.h:77
ErrorUnknownCallbackResult
void ErrorUnknownCallbackResult(uint32_t grfid, uint16_t cbid, uint16_t cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
Definition: newgrf_commons.cpp:505
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve([[maybe_unused]] ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:61
Town
Town data structure.
Definition: town.h:54
AirportSpec::GetWithoutOverride
static AirportSpec * GetWithoutOverride(uint8_t type)
Retrieve airport spec for the given airport.
Definition: newgrf_airport.cpp:75
AirportSpec::IsWithinMapBounds
bool IsWithinMapBounds(uint8_t table, TileIndex index) const
Check if the airport would be within the map bounds at the given tile.
Definition: newgrf_airport.cpp:96
OverrideManagerBase::max_offset
uint16_t max_offset
what is the length of the original entity's array of specs
Definition: newgrf_commons.h:196
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition: newgrf_spritegroup.h:290
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition: newgrf_commons.h:312
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:427
Airport::psa
PersistentStorage * psa
Persistent storage for NewGRF airports.
Definition: station_base.h:298
NewGRFClass::Get
static NewGRFClass * Get(Tindex class_index)
Get a particular class.
Definition: newgrf_class_func.h:82
AirportResolverObject::GetTown
TownScopeResolver * GetTown()
Get the town scope associated with a station, if it exists.
Definition: newgrf_airport.cpp:220
Airport::type
uint8_t type
Type of this airport,.
Definition: station_base.h:294
Airport::layout
uint8_t layout
Airport layout number.
Definition: station_base.h:295
SpriteGroup
Definition: newgrf_spritegroup.h:57
AirportScopeResolver::StorePSA
void StorePSA(uint pos, int32_t value) override
Store a value into the object's persistent storage.
Definition: newgrf_airport.cpp:199
AirportScopeResolver::GetRandomBits
uint32_t GetRandomBits() const override
Get a few random bits.
Definition: newgrf_airport.cpp:189
PersistentStorageArray::StoreValue
void StoreValue(uint pos, int32_t value)
Stores some value at a given position.
Definition: newgrf_storage.h:80
GRFFilePropsBase::spritegroup
std::array< const struct SpriteGroup *, Tcnt > spritegroup
pointers to the different sprites of the entity
Definition: newgrf_commons.h:313
AirportSpec::Get
static const AirportSpec * Get(uint8_t type)
Retrieve airport spec for the given airport.
Definition: newgrf_airport.cpp:55
AirportOverrideManager
Definition: newgrf_commons.h:258
Map::SizeY
static uint SizeY()
Get the size of the map along the Y.
Definition: map_func.h:279
debug.h
AirportScopeResolver::tile
TileIndex tile
Tile for the callback, only valid for airporttile callbacks.
Definition: newgrf_airport.h:154
TimerGameCalendar::year
static Year year
Current year, starting at 0.
Definition: timer_game_calendar.h:32
BaseStation::build_date
TimerGameCalendar::Date build_date
Date of construction.
Definition: base_station_base.h:75