OpenTTD Source  12.0-beta2
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"
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"
17 
18 #include "safeguards.h"
19 
22  struct Station *st;
23  byte airport_id;
24  byte layout;
26 
37  {
38  }
39 
40  uint32 GetRandomBits() const override;
41  uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override;
42  void StorePSA(uint pos, int32 value) override;
43 };
44 
47  AirportScopeResolver airport_scope;
48 
49  AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
51 
52  ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
53  {
54  switch (scope) {
55  case VSG_SCOPE_SELF: return &this->airport_scope;
56  default: return ResolverObject::GetScope(scope, relative);
57  }
58  }
59 
60  GrfSpecFeature GetFeature() const override;
61  uint32 GetDebugID() const override;
62 };
63 
69 template <typename Tspec, typename Tid, Tid Tmax>
71 {
72  AirportClass::Get(AirportClass::Allocate('SMAL'))->name = STR_AIRPORT_CLASS_SMALL;
73  AirportClass::Get(AirportClass::Allocate('LARG'))->name = STR_AIRPORT_CLASS_LARGE;
74  AirportClass::Get(AirportClass::Allocate('HUB_'))->name = STR_AIRPORT_CLASS_HUB;
75  AirportClass::Get(AirportClass::Allocate('HELI'))->name = STR_AIRPORT_CLASS_HELIPORTS;
76 }
77 
78 template <typename Tspec, typename Tid, Tid Tmax>
80 {
81  return true;
82 }
83 
85 
86 
88 
90 
97 /* static */ const AirportSpec *AirportSpec::Get(byte type)
98 {
99  assert(type < lengthof(AirportSpec::specs));
100  const AirportSpec *as = &AirportSpec::specs[type];
101  if (type >= NEW_AIRPORT_OFFSET && !as->enabled) {
102  if (_airport_mngr.GetGRFID(type) == 0) return as;
103  byte subst_id = _airport_mngr.GetSubstituteID(type);
104  if (subst_id == AT_INVALID) return as;
105  as = &AirportSpec::specs[subst_id];
106  }
108  return as;
109 }
110 
118 {
119  assert(type < lengthof(AirportSpec::specs));
120  return &AirportSpec::specs[type];
121 }
122 
125 {
126  if (!this->enabled) return false;
127  if (_cur_year < this->min_year) return false;
129  return _cur_year <= this->max_year;
130 }
131 
138 bool AirportSpec::IsWithinMapBounds(byte table, TileIndex tile) const
139 {
140  if (table >= this->num_table) return false;
141 
142  byte w = this->size_x;
143  byte h = this->size_y;
144  if (this->rotation[table] == DIR_E || this->rotation[table] == DIR_W) Swap(w, h);
145 
146  return TileX(tile) + w < MapSizeX() &&
147  TileY(tile) + h < MapSizeY();
148 }
149 
154 {
155  extern const AirportSpec _origin_airport_specs[];
156  memset(&AirportSpec::specs, 0, sizeof(AirportSpec::specs));
157  memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NEW_AIRPORT_OFFSET);
158 
159  _airport_mngr.ResetOverride();
160 }
161 
166 {
167  for (int i = 0; i < NUM_AIRPORTS; i++) {
169  if (as->enabled) AirportClass::Assign(as);
170  }
171 }
172 
173 
174 void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
175 {
176  byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
177 
178  if (airport_id == invalid_ID) {
179  grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
180  return;
181  }
182 
183  memcpy(AirportSpec::GetWithoutOverride(airport_id), as, sizeof(*as));
184 
185  /* Now add the overrides. */
186  for (int i = 0; i < max_offset; i++) {
187  AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
188 
189  if (entity_overrides[i] != as->grf_prop.local_id || grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
190 
191  overridden_as->grf_prop.override = airport_id;
192  overridden_as->enabled = false;
193  entity_overrides[i] = invalid_ID;
194  grfid_overrides[i] = 0;
195  }
196 }
197 
198 /* virtual */ uint32 AirportScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
199 {
200  switch (variable) {
201  case 0x40: return this->layout;
202  }
203 
204  if (this->st == nullptr) {
205  *available = false;
206  return UINT_MAX;
207  }
208 
209  switch (variable) {
210  /* Get a variable from the persistent storage */
211  case 0x7C: return (this->st->airport.psa != nullptr) ? this->st->airport.psa->GetValue(parameter) : 0;
212 
213  case 0xF0: return this->st->facilities;
214  case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
215  }
216 
217  return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
218 }
219 
221 {
222  return GSF_AIRPORTS;
223 }
224 
226 {
227  return AirportSpec::Get(this->airport_scope.airport_id)->grf_prop.local_id;
228 }
229 
230 /* virtual */ uint32 AirportScopeResolver::GetRandomBits() const
231 {
232  return this->st == nullptr ? 0 : this->st->random_bits;
233 }
234 
240 /* virtual */ void AirportScopeResolver::StorePSA(uint pos, int32 value)
241 {
242  if (this->st == nullptr) return;
243 
244  if (this->st->airport.psa == nullptr) {
245  /* There is no need to create a storage if the value is zero. */
246  if (value == 0) return;
247 
248  /* Create storage on first modification. */
249  uint32 grfid = (this->ro.grffile != nullptr) ? this->ro.grffile->grfid : 0;
251  this->st->airport.psa = new PersistentStorage(grfid, GSF_AIRPORTS, this->st->airport.tile);
252  }
253  this->st->airport.psa->StoreValue(pos, value);
254 }
255 
266 AirportResolverObject::AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
267  CallbackID callback, uint32 param1, uint32 param2)
268  : ResolverObject(AirportSpec::Get(airport_id)->grf_prop.grffile, callback, param1, param2), airport_scope(*this, tile, st, airport_id, layout)
269 {
270  this->root_spritegroup = AirportSpec::Get(airport_id)->grf_prop.spritegroup[0];
271 }
272 
273 SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
274 {
275  AirportResolverObject object(INVALID_TILE, nullptr, as->GetIndex(), layout);
276  const SpriteGroup *group = object.Resolve();
277  if (group == nullptr) return as->preview_sprite;
278 
279  return group->GetResult();
280 }
281 
282 uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
283 {
284  AirportResolverObject object(tile, st, st->airport.type, st->airport.layout, callback, param1, param2);
285  return object.ResolveCallback();
286 }
287 
295 StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
296 {
297  AirportResolverObject object(INVALID_TILE, nullptr, as->GetIndex(), layout, (CallbackID)callback);
298  uint16 cb_res = object.ResolveCallback();
299  if (cb_res == CALLBACK_FAILED || cb_res == 0x400) return STR_UNDEFINED;
300  if (cb_res > 0x400) {
301  ErrorUnknownCallbackResult(as->grf_prop.grffile->grfid, callback, cb_res);
302  return STR_UNDEFINED;
303  }
304 
305  return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);
306 }
AirportSpec::min_year
Year min_year
first year the airport is available
Definition: newgrf_airport.h:109
BaseStation::facilities
StationFacility facilities
The facilities that this station has.
Definition: base_station_base.h:63
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
VarSpriteGroupScope
VarSpriteGroupScope
Definition: newgrf_spritegroup.h:97
ResolverObject::callback_param1
uint32 callback_param1
First parameter (var 10) of the callback.
Definition: newgrf_spritegroup.h:326
GRFFileProps::override
uint16 override
id of the entity been replaced by
Definition: newgrf_commons.h:333
GameSettings::station
StationSettings station
settings related to station management
Definition: settings_type.h:587
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:138
AirportResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition: newgrf_airport.cpp:220
GetAirportTextCallback
StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
Get a custom text for the airport.
Definition: newgrf_airport.cpp:295
grfmsg
void CDECL grfmsg(int severity, const char *str,...)
Debug() function dedicated to newGRF debugging messages Function is essentially the same as Debug(grf...
Definition: newgrf.cpp:391
_cur_year
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
AirportSpec::size_y
byte size_y
size of airport in y direction
Definition: newgrf_airport.h:106
Station
Station data structure.
Definition: station_base.h:447
AirportSpec::max_year
Year max_year
last year the airport is available
Definition: newgrf_airport.h:110
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:216
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:307
AirportScopeResolver::st
struct Station * st
Station of the airport for which the callback is run, or nullptr for build gui.
Definition: newgrf_airport.cpp:22
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.cpp:23
AirportScopeResolver::layout
byte layout
Layout of the airport to build.
Definition: newgrf_airport.cpp:24
Airport::layout
byte layout
Airport layout number.
Definition: station_base.h:307
AirportSpec::ResetAirports
static void ResetAirports()
This function initializes the airportspec array.
Definition: newgrf_airport.cpp:153
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
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
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
Definition: newgrf_spritegroup.h:288
AirportSpec
Defines the data structure for an airport.
Definition: newgrf_airport.h:98
VSG_SCOPE_SELF
@ VSG_SCOPE_SELF
Resolved object itself.
Definition: newgrf_spritegroup.h:100
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
MapSizeX
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:72
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:79
AirportResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0) override
Get a resolver for the scope.
Definition: newgrf_airport.cpp:52
ResolverObject::callback_param2
uint32 callback_param2
Second parameter (var 18) of the callback.
Definition: newgrf_spritegroup.h:327
AirportSpec::rotation
const Direction * rotation
the rotation of each tiletable
Definition: newgrf_airport.h:101
BindAirportSpecs
void BindAirportSpecs()
Tie all airportspecs to their class.
Definition: newgrf_airport.cpp:165
AirportSpec::Get
static const AirportSpec * Get(byte type)
Retrieve airport spec for the given airport.
Definition: newgrf_airport.cpp:97
AirportScopeResolver::AirportScopeResolver
AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, byte airport_id, byte layout)
Constructor of the scope resolver for an airport.
Definition: newgrf_airport.cpp:35
AirportSpec::num_table
byte num_table
number of elements in the table
Definition: newgrf_airport.h:102
DIR_E
@ DIR_E
East.
Definition: direction_type.h:28
BaseStation::random_bits
uint16 random_bits
Random bits assigned to this station.
Definition: base_station_base.h:70
AirportScopeResolver::GetRandomBits
uint32 GetRandomBits() const override
Get a few random bits.
Definition: newgrf_airport.cpp:230
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition: newgrf_callbacks.h:22
PersistentStorageArray::GetValue
TYPE GetValue(uint pos) const
Gets the value from a given position.
Definition: newgrf_storage.h:124
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:221
AirportSpec::GetIndex
byte GetIndex() const
Get the index of this spec.
Definition: newgrf_airport.h:129
Station::airport
Airport airport
Tile area the airport covers.
Definition: station_base.h:461
NewGRFClass
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:19
StationSettings::never_expire_airports
bool never_expire_airports
never expire airports
Definition: settings_type.h:551
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:53
PersistentStorageArray::StoreValue
void StoreValue(uint pos, int32 value)
Stores some value at a given position.
Definition: newgrf_storage.h:95
safeguards.h
AT_INVALID
@ AT_INVALID
Invalid airport.
Definition: airport.h:42
GetGRFStringID
StringID GetGRFStringID(uint32 grfid, StringID stringid)
Returns the index for this stringid associated with its grfID.
Definition: newgrf_text.cpp:601
NUM_AIRPORTS
@ NUM_AIRPORTS
Maximal number of airports in total.
Definition: airport.h:41
AirportResolverObject
Resolver object for airports.
Definition: newgrf_airport.cpp:46
newgrf_text.h
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0)
Get a resolver for the scope.
Definition: newgrf_spritegroup.cpp:139
MapSizeY
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:82
date_func.h
stdafx.h
AirportSpec::specs
static AirportSpec specs[NUM_AIRPORTS]
Specs of the airports.
Definition: newgrf_airport.h:138
NewGRFClass::InsertDefaults
static void InsertDefaults()
Initialise the defaults.
Definition: newgrf_airport.cpp:70
AirportScopeResolver::GetVariable
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override
Get a variable value.
Definition: newgrf_airport.cpp:198
AirportSpec::IsAvailable
bool IsAvailable() const
Check whether this airport is available to build.
Definition: newgrf_airport.cpp:124
AirportSpec::size_x
byte size_x
size of airport in x direction
Definition: newgrf_airport.h:105
newgrf_spritegroup.h
AirportScopeResolver::StorePSA
void StorePSA(uint pos, int32 value) override
Store a value into the object's persistent storage.
Definition: newgrf_airport.cpp:240
GrfSpecFeature
GrfSpecFeature
Definition: newgrf.h:66
AirportScopeResolver
Resolver for the airport scope.
Definition: newgrf_airport.cpp:21
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:61
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:404
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
station_base.h
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
GRFFilePropsBase::spritegroup
const struct SpriteGroup * spritegroup[Tcnt]
pointer to the different sprites of the entity
Definition: newgrf_commons.h:321
AirportSpec::enabled
bool enabled
Entity still available (by default true). Newgrf can disable it, though.
Definition: newgrf_airport.h:117
AirportSpec::preview_sprite
SpriteID preview_sprite
preview sprite for this airport
Definition: newgrf_airport.h:114
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:68
AirportResolverObject::AirportResolverObject
AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout, CallbackID callback=CBID_NO_CALLBACK, uint32 callback_param1=0, uint32 callback_param2=0)
Constructor of the airport resolver.
Definition: newgrf_airport.cpp:266
OverrideManagerBase::GetSubstituteID
uint16 GetSubstituteID(uint16 entity_id) const
Gives the substitute of the entity, as specified by the grf file.
Definition: newgrf_commons.cpp:166
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:118
APC_MAX
@ APC_MAX
maximum number of airport classes
Definition: newgrf_airport.h:74
OverrideManagerBase::ResetOverride
void ResetOverride()
Resets the override, which is used while initializing game.
Definition: newgrf_commons.cpp:88
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:307
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition: newgrf_spritegroup.h:325
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:117
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
NewGRFClass::Get
static NewGRFClass * Get(Tid cls_id)
Get a particular class.
Definition: newgrf_class_func.h:103
Airport::type
byte type
Type of this airport,.
Definition: station_base.h:306
OverrideManagerBase::max_offset
uint16 max_offset
what is the length of the original entity's array of specs
Definition: newgrf_commons.h:197
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:88
OverrideManagerBase::AddEntityID
virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
Reserves a place in the mapping array for an entity to be installed.
Definition: newgrf_commons.cpp:123
OverrideManagerBase::GetGRFID
uint32 GetGRFID(uint16 entity_id) const
Gives the GRFID of the file the entity belongs to.
Definition: newgrf_commons.cpp:156
GRFFilePropsBase::local_id
uint16 local_id
id defined by the grf file for this entity
Definition: newgrf_commons.h:319
ErrorUnknownCallbackResult
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
Definition: newgrf_commons.cpp:516
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:320
Swap
static void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:215
AirportResolverObject::GetDebugID
uint32 GetDebugID() const override
Get an identifier for the item being resolved.
Definition: newgrf_airport.cpp:225
Airport::psa
PersistentStorage * psa
Persistent storage for NewGRF airports.
Definition: station_base.h:310
AirportSpec::table
const AirportTileTable *const * table
list of the tiles composing the airport
Definition: newgrf_airport.h:100
BaseStation::build_date
Date build_date
Date of construction.
Definition: base_station_base.h:68
SpriteGroup
Definition: newgrf_spritegroup.h:57
AirportOverrideManager
Definition: newgrf_commons.h:259
OverrideManagerBase::invalid_ID
uint16 invalid_ID
ID used to detected invalid entities;.
Definition: newgrf_commons.h:200
debug.h
DAYS_TILL_ORIGINAL_BASE_YEAR
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the '_date == 0' till 'ConvertYMDToDate(ORIGINAL_BASE_YEAR,...
Definition: date_type.h:81
AirportScopeResolver::tile
TileIndex tile
Tile for the callback, only valid for airporttile callbacks.
Definition: newgrf_airport.cpp:25