OpenTTD Source  14.0-beta3
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 <typename Tspec, typename Tid, Tid Tmax>
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 <typename Tspec, typename Tid, Tid Tmax>
37 {
38  return true;
39 }
40 
42 
43 
45 
47 
54 /* static */ const AirportSpec *AirportSpec::Get(byte type)
55 {
56  assert(type < lengthof(AirportSpec::specs));
57  const AirportSpec *as = &AirportSpec::specs[type];
58  if (type >= NEW_AIRPORT_OFFSET && !as->enabled) {
59  if (_airport_mngr.GetGRFID(type) == 0) return as;
60  byte subst_id = _airport_mngr.GetSubstituteID(type);
61  if (subst_id == AT_INVALID) return as;
62  as = &AirportSpec::specs[subst_id];
63  }
65  return as;
66 }
67 
75 {
76  assert(type < lengthof(AirportSpec::specs));
77  return &AirportSpec::specs[type];
78 }
79 
82 {
83  if (!this->enabled) return false;
84  if (TimerGameCalendar::year < this->min_year) return false;
86  return TimerGameCalendar::year <= this->max_year;
87 }
88 
95 bool AirportSpec::IsWithinMapBounds(byte table, TileIndex tile) const
96 {
97  if (table >= this->num_table) return false;
98 
99  byte w = this->size_x;
100  byte h = this->size_y;
101  if (this->rotation[table] == DIR_E || this->rotation[table] == DIR_W) Swap(w, h);
102 
103  return TileX(tile) + w < Map::SizeX() &&
104  TileY(tile) + h < Map::SizeY();
105 }
106 
111 {
112  extern const AirportSpec _origin_airport_specs[NEW_AIRPORT_OFFSET];
113 
114  auto insert = std::copy(std::begin(_origin_airport_specs), std::end(_origin_airport_specs), std::begin(AirportSpec::specs));
115  std::fill(insert, std::end(AirportSpec::specs), AirportSpec{});
116 
117  _airport_mngr.ResetOverride();
118 }
119 
124 {
125  for (int i = 0; i < NUM_AIRPORTS; i++) {
127  if (as->enabled) AirportClass::Assign(as);
128  }
129 }
130 
131 
132 void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
133 {
134  byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
135 
136  if (airport_id == this->invalid_id) {
137  GrfMsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
138  return;
139  }
140 
141  *AirportSpec::GetWithoutOverride(airport_id) = *as;
142 
143  /* Now add the overrides. */
144  for (int i = 0; i < this->max_offset; i++) {
145  AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
146 
147  if (this->entity_overrides[i] != as->grf_prop.local_id || this->grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
148 
149  overridden_as->grf_prop.override = airport_id;
150  overridden_as->enabled = false;
151  this->entity_overrides[i] = this->invalid_id;
152  this->grfid_overrides[i] = 0;
153  }
154 }
155 
156 /* virtual */ uint32_t AirportScopeResolver::GetVariable(byte variable, [[maybe_unused]] uint32_t parameter, bool *available) const
157 {
158  switch (variable) {
159  case 0x40: return this->layout;
160  }
161 
162  if (this->st == nullptr) {
163  *available = false;
164  return UINT_MAX;
165  }
166 
167  switch (variable) {
168  /* Get a variable from the persistent storage */
169  case 0x7C: return (this->st->airport.psa != nullptr) ? this->st->airport.psa->GetValue(parameter) : 0;
170 
171  case 0xF0: return this->st->facilities;
172  case 0xFA: return ClampTo<uint16_t>(this->st->build_date - CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR);
173  }
174 
175  return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
176 }
177 
179 {
180  return GSF_AIRPORTS;
181 }
182 
184 {
185  return AirportSpec::Get(this->airport_scope.airport_id)->grf_prop.local_id;
186 }
187 
188 /* virtual */ uint32_t AirportScopeResolver::GetRandomBits() const
189 {
190  return this->st == nullptr ? 0 : this->st->random_bits;
191 }
192 
198 /* virtual */ void AirportScopeResolver::StorePSA(uint pos, int32_t value)
199 {
200  if (this->st == nullptr) return;
201 
202  if (this->st->airport.psa == nullptr) {
203  /* There is no need to create a storage if the value is zero. */
204  if (value == 0) return;
205 
206  /* Create storage on first modification. */
207  uint32_t grfid = (this->ro.grffile != nullptr) ? this->ro.grffile->grfid : 0;
209  this->st->airport.psa = new PersistentStorage(grfid, GSF_AIRPORTS, this->st->airport.tile);
210  }
211  this->st->airport.psa->StoreValue(pos, value);
212 }
213 
220 {
221  if (!this->town_scope) {
222  Town *t = nullptr;
223  if (this->airport_scope.st != nullptr) {
224  t = this->airport_scope.st->town;
225  } else if (this->airport_scope.tile != INVALID_TILE) {
226  t = ClosestTownFromTile(this->airport_scope.tile, UINT_MAX);
227  }
228  if (t == nullptr) return nullptr;
229  this->town_scope.reset(new TownScopeResolver(*this, t, this->airport_scope.st == nullptr));
230  }
231  return this->town_scope.get();
232 }
233 
244 AirportResolverObject::AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
245  CallbackID callback, uint32_t param1, uint32_t param2)
246  : ResolverObject(AirportSpec::Get(airport_id)->grf_prop.grffile, callback, param1, param2), airport_scope(*this, tile, st, airport_id, layout)
247 {
248  this->root_spritegroup = AirportSpec::Get(airport_id)->grf_prop.spritegroup[0];
249 }
250 
251 SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
252 {
253  AirportResolverObject object(INVALID_TILE, nullptr, as->GetIndex(), layout);
254  const SpriteGroup *group = object.Resolve();
255  if (group == nullptr) return as->preview_sprite;
256 
257  return group->GetResult();
258 }
259 
260 uint16_t GetAirportCallback(CallbackID callback, uint32_t param1, uint32_t param2, Station *st, TileIndex tile)
261 {
262  AirportResolverObject object(tile, st, st->airport.type, st->airport.layout, callback, param1, param2);
263  return object.ResolveCallback();
264 }
265 
273 StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16_t callback)
274 {
275  AirportResolverObject object(INVALID_TILE, nullptr, as->GetIndex(), layout, (CallbackID)callback);
276  uint16_t cb_res = object.ResolveCallback();
277  if (cb_res == CALLBACK_FAILED || cb_res == 0x400) return STR_UNDEFINED;
278  if (cb_res > 0x400) {
279  ErrorUnknownCallbackResult(as->grf_prop.grffile->grfid, callback, cb_res);
280  return STR_UNDEFINED;
281  }
282 
283  return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);
284 }
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:75
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:630
AirportSpec::IsWithinMapBounds
bool IsWithinMapBounds(byte table, TileIndex index) const
Check if the airport would be within the map bounds at the given tile.
Definition: newgrf_airport.cpp:95
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:178
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:3744
BaseStation::town
Town * town
The town this station is associated with.
Definition: base_station_base.h:73
timer_game_calendar.h
AirportSpec::size_y
byte size_y
size of airport in y direction
Definition: newgrf_airport.h:108
Station
Station data structure.
Definition: station_base.h:442
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
INSTANTIATE_NEWGRF_CLASS_METHODS
#define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax)
Force instantiation of the methods so we don't get linker errors.
Definition: newgrf_class_func.h:212
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:307
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:150
ResolverObject::grffile
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
Definition: newgrf_spritegroup.h:335
AirportScopeResolver::airport_id
byte airport_id
Type of airport for which the callback is run.
Definition: newgrf_airport.h:151
AirportScopeResolver::layout
byte layout
Layout of the airport to build.
Definition: newgrf_airport.h:152
Airport::layout
byte layout
Airport layout number.
Definition: station_base.h:295
AirportSpec::min_year
TimerGameCalendar::Year min_year
first year the airport is available
Definition: newgrf_airport.h:111
AirportSpec::max_year
TimerGameCalendar::Year max_year
last year the airport is available
Definition: newgrf_airport.h:112
AirportSpec::ResetAirports
static void ResetAirports()
This function initializes the airportspec array.
Definition: newgrf_airport.cpp:110
town.h
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
newgrf_class_func.h
DIR_W
@ DIR_W
West.
Definition: direction_type.h:32
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition: newgrf_callbacks.h:20
AirportSpec
Defines the data structure for an airport.
Definition: newgrf_airport.h:100
OverrideManagerBase::invalid_id
uint16_t invalid_id
ID used to detected invalid entities.
Definition: newgrf_commons.h:199
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
GRFFileProps::override
uint16_t override
id of the entity been replaced by
Definition: newgrf_commons.h:332
AirportSpec::rotation
const Direction * rotation
the rotation of each tiletable
Definition: newgrf_airport.h:103
BindAirportSpecs
void BindAirportSpecs()
Tie all airportspecs to their class.
Definition: newgrf_airport.cpp:123
AirportSpec::Get
static const AirportSpec * Get(byte type)
Retrieve airport spec for the given airport.
Definition: newgrf_airport.cpp:54
AirportSpec::num_table
byte num_table
number of elements in the table
Definition: newgrf_airport.h:104
DIR_E
@ DIR_E
East.
Definition: direction_type.h:28
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:336
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:199
AirportSpec::GetIndex
byte GetIndex() const
Get the index of this spec.
Definition: newgrf_airport.h:131
AirportResolverObject::AirportResolverObject
AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte 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:244
Station::airport
Airport airport
Tile area the airport covers.
Definition: station_base.h:456
NewGRFClass
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:20
StationSettings::never_expire_airports
bool never_expire_airports
never expire airports
Definition: settings_type.h:594
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
safeguards.h
AT_INVALID
@ AT_INVALID
Invalid airport.
Definition: airport.h:42
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:175
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
AirportSpec::specs
static AirportSpec specs[NUM_AIRPORTS]
Specs of the airports.
Definition: newgrf_airport.h:140
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
NewGRFClass::InsertDefaults
static void InsertDefaults()
Initialise the defaults.
Definition: newgrf_airport.cpp:27
AirportSpec::IsAvailable
bool IsAvailable() const
Check whether this airport is available to build.
Definition: newgrf_airport.cpp:81
GRFFilePropsBase::local_id
uint16_t local_id
id defined by the grf file for this entity
Definition: newgrf_commons.h:318
AirportSpec::size_x
byte size_x
size of airport in x direction
Definition: newgrf_airport.h:107
newgrf_spritegroup.h
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:66
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:420
station_base.h
GRFFilePropsBase::spritegroup
const struct SpriteGroup * spritegroup[Tcnt]
pointer to the different sprites of the entity
Definition: newgrf_commons.h:320
AirportSpec::enabled
bool enabled
Entity still available (by default true). Newgrf can disable it, though.
Definition: newgrf_airport.h:119
AirportSpec::preview_sprite
SpriteID preview_sprite
preview sprite for this airport
Definition: newgrf_airport.h:116
NEW_AIRPORT_OFFSET
@ NEW_AIRPORT_OFFSET
Number of the first newgrf airport.
Definition: airport.h:39
AirportClassID
AirportClassID
List of default airport classes.
Definition: newgrf_airport.h:70
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
AirportSpec::grf_prop
struct GRFFileProps grf_prop
Properties related to the grf file.
Definition: newgrf_airport.h:120
APC_MAX
@ APC_MAX
maximum number of airport classes
Definition: newgrf_airport.h:76
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:305
AirportResolverObject::GetDebugID
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Definition: newgrf_airport.cpp:183
NewGRFClass::name
StringID name
Name of this class.
Definition: newgrf_class.h:39
AirportSpec::GetWithoutOverride
static AirportSpec * GetWithoutOverride(byte type)
Retrieve airport spec for the given airport.
Definition: newgrf_airport.cpp:74
AirportScopeResolver::GetVariable
uint32_t GetVariable(byte variable, [[maybe_unused]] uint32_t parameter, bool *available) const override
Get a variable value.
Definition: newgrf_airport.cpp:156
BaseStation::random_bits
uint16_t random_bits
Random bits assigned to this station.
Definition: base_station_base.h:82
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:499
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:50
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:300
NewGRFClass::Get
static NewGRFClass * Get(Tid cls_id)
Get a particular class.
Definition: newgrf_class_func.h:98
Airport::type
byte type
Type of this airport,.
Definition: station_base.h:294
AirportResolverObject::town_scope
std::unique_ptr< TownScopeResolver > town_scope
The town scope resolver (created on the first call).
Definition: newgrf_airport.h:177
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:289
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition: newgrf_commons.h:319
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
AirportSpec::table
const AirportTileTable *const * table
list of the tiles composing the airport
Definition: newgrf_airport.h:102
AirportResolverObject::GetTown
TownScopeResolver * GetTown()
Get the town scope associated with a station, if it exists.
Definition: newgrf_airport.cpp:219
GetAirportTextCallback
StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16_t callback)
Get a custom text for the airport.
Definition: newgrf_airport.cpp:273
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:198
AirportScopeResolver::GetRandomBits
uint32_t GetRandomBits() const override
Get a few random bits.
Definition: newgrf_airport.cpp:188
PersistentStorageArray::StoreValue
void StoreValue(uint pos, int32_t value)
Stores some value at a given position.
Definition: newgrf_storage.h:80
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:153
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:80