OpenTTD

newgrf_airport.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_airport.cpp 27984 2018-03-11 13:19:41Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
9
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "date_func.h"
15 #include "newgrf_spritegroup.h"
16 #include "newgrf_text.h"
17 #include "station_base.h"
18 #include "newgrf_class_func.h"
19
20 #include "safeguards.h"
21
23 struct AirportScopeResolver : public ScopeResolver {
24 struct Station *st;
25 byte airport_id;
26 byte layout;
27 TileIndex tile;
28
37 AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, byte airport_id, byte layout)
38 : ScopeResolver(ro), st(st), airport_id(airport_id), layout(layout), tile(tile)
39 {
40 }
41
42 /* virtual */ uint32 GetRandomBits() const;
43 /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
44 /* virtual */ void StorePSA(uint pos, int32 value);
45 };
46
48 struct AirportResolverObject : public ResolverObject {
49 AirportScopeResolver airport_scope;
50
51 AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
52 CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
53
54 /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
55 {
56 switch (scope) {
57 case VSG_SCOPE_SELF: return &this->airport_scope;
58 default: return ResolverObject::GetScope(scope, relative);
59 }
60 }
61
62 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
63 };
64
70 template <typename Tspec, typename Tid, Tid Tmax>
71 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
72 {
73 AirportClass::Get(AirportClass::Allocate('SMAL'))->name = STR_AIRPORT_CLASS_SMALL;
74 AirportClass::Get(AirportClass::Allocate('LARG'))->name = STR_AIRPORT_CLASS_LARGE;
75 AirportClass::Get(AirportClass::Allocate('HUB_'))->name = STR_AIRPORT_CLASS_HUB;
76 AirportClass::Get(AirportClass::Allocate('HELI'))->name = STR_AIRPORT_CLASS_HELIPORTS;
77 }
78
79 template <typename Tspec, typename Tid, Tid Tmax>
80 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
81 {
82 return true;
83 }
84
85 INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX)
86
87
88 AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
89
90 AirportSpec AirportSpec::specs[NUM_AIRPORTS];
91
98 /* static */ const AirportSpec *AirportSpec::Get(byte type)
99 {
100 assert(type < lengthof(AirportSpec::specs));
101 const AirportSpec *as = &AirportSpec::specs[type];
102 if (type >= NEW_AIRPORT_OFFSET && !as->enabled) {
103 byte subst_id = _airport_mngr.GetSubstituteID(type);
104 if (subst_id == AT_INVALID) return as;
105 as = &AirportSpec::specs[subst_id];
106 }
107 if (as->grf_prop.override != AT_INVALID) return &AirportSpec::specs[as->grf_prop.override];
108 return as;
109 }
110
117 /* static */ AirportSpec *AirportSpec::GetWithoutOverride(byte type)
118 {
119 assert(type < lengthof(AirportSpec::specs));
120 return &AirportSpec::specs[type];
121 }
122
124 bool AirportSpec::IsAvailable() const
125 {
126 if (!this->enabled) return false;
127 if (_cur_year < this->min_year) return false;
128 if (_settings_game.station.never_expire_airports) return true;
129 return _cur_year <= this->max_year;
130 }
131
135 void AirportSpec::ResetAirports()
136 {
137 extern const AirportSpec _origin_airport_specs[];
138 memset(&AirportSpec::specs, 0, sizeof(AirportSpec::specs));
139 memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NEW_AIRPORT_OFFSET);
140
141 _airport_mngr.ResetOverride();
142 }
143
147 void BindAirportSpecs()
148 {
149 for (int i = 0; i < NUM_AIRPORTS; i++) {
150 AirportSpec *as = AirportSpec::GetWithoutOverride(i);
151 if (as->enabled) AirportClass::Assign(as);
152 }
153 }
154
155
156 void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
157 {
158 byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
159
160 if (airport_id == invalid_ID) {
161 grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
162 return;
163 }
164
165 memcpy(AirportSpec::GetWithoutOverride(airport_id), as, sizeof(*as));
166
167 /* Now add the overrides. */
168 for (int i = 0; i < max_offset; i++) {
169 AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
170
171 if (entity_overrides[i] != as->grf_prop.local_id || grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
172
173 overridden_as->grf_prop.override = airport_id;
174 overridden_as->enabled = false;
175 entity_overrides[i] = invalid_ID;
176 grfid_overrides[i] = 0;
177 }
178 }
179
180 /* virtual */ uint32 AirportScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
181 {
182 switch (variable) {
183 case 0x40: return this->layout;
184 }
185
186 if (this->st == NULL) {
187 *available = false;
188 return UINT_MAX;
189 }
190
191 switch (variable) {
192 /* Get a variable from the persistent storage */
193 case 0x7C: return (this->st->airport.psa != NULL) ? this->st->airport.psa->GetValue(parameter) : 0;
194
195 case 0xF0: return this->st->facilities;
196 case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
197 }
198
199 return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
200 }
201
202 /* virtual */ const SpriteGroup *AirportResolverObject::ResolveReal(const RealSpriteGroup *group) const
203 {
204 /* Airport action 2s should always have only 1 "loaded" state, but some
205 * times things don't follow the spec... */
206 if (group->num_loaded > 0) return group->loaded[0];
207 if (group->num_loading > 0) return group->loading[0];
208
209 return NULL;
210 }
211
212 /* virtual */ uint32 AirportScopeResolver::GetRandomBits() const
213 {
214 return this->st == NULL ? 0 : this->st->random_bits;
215 }
216
223 /* virtual */ void AirportScopeResolver::StorePSA(uint pos, int32 value)
224 {
225 if (this->st == NULL) return;
226
227 if (this->st->airport.psa == NULL) {
228 /* There is no need to create a storage if the value is zero. */
229 if (value == 0) return;
230
231 /* Create storage on first modification. */
232 uint32 grfid = (this->ro.grffile != NULL) ? this->ro.grffile->grfid : 0;
233 assert(PersistentStorage::CanAllocateItem());
234 this->st->airport.psa = new PersistentStorage(grfid, GSF_AIRPORTS, this->st->airport.tile);
235 }
236 this->st->airport.psa->StoreValue(pos, value);
237 }
238
249 AirportResolverObject::AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
250 CallbackID callback, uint32 param1, uint32 param2)
251 : ResolverObject(AirportSpec::Get(airport_id)->grf_prop.grffile, callback, param1, param2), airport_scope(*this, tile, st, airport_id, layout)
252 {
253 this->root_spritegroup = AirportSpec::Get(airport_id)->grf_prop.spritegroup[0];
254 }
255
256 SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
257 {
258 AirportResolverObject object(INVALID_TILE, NULL, as->GetIndex(), layout);
259 const SpriteGroup *group = object.Resolve();
260 if (group == NULL) return as->preview_sprite;
261
262 return group->GetResult();
263 }
264
265 uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
266 {
267 AirportResolverObject object(tile, st, st->airport.type, st->airport.layout, callback, param1, param2);
268 return object.ResolveCallback();
269 }
270
278 StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
279 {
280 AirportResolverObject object(INVALID_TILE, NULL, as->GetIndex(), layout, (CallbackID)callback);
281 uint16 cb_res = object.ResolveCallback();
282 if (cb_res == CALLBACK_FAILED || cb_res == 0x400) return STR_UNDEFINED;
283 if (cb_res > 0x400) {
284 ErrorUnknownCallbackResult(as->grf_prop.grffile->grfid, callback, cb_res);
285 return STR_UNDEFINED;
286 }
287
288 return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);
289 }
newgrf_class_func.h
Implementation of the NewGRF class&#39; functions.
Airport::type
byte type
Type of this airport,.
Definition: station_base.h:309
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope)...
Definition: newgrf_spritegroup.h:291
AirportScopeResolver::tile
TileIndex tile
Tile for the callback, only valid for airporttile callbacks.
Definition: newgrf_airport.cpp:27
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
AirportSpec::IsAvailable
bool IsAvailable() const
Check whether this airport is available to build.
Definition: newgrf_airport.cpp:124
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:406
AirportClassID
AirportClassID
List of default airport classes.
Definition: newgrf_airport.h:70
AirportSpec::GetWithoutOverride
static AirportSpec * GetWithoutOverride(byte type)
Retrieve airport spec for the given airport.
Definition: newgrf_airport.cpp:117
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition: newgrf_spritegroup.h:292
DAYS_TILL_ORIGINAL_BASE_YEAR
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the &#39;_date == 0&#39; till &#39;ConvertYMDToDate(ORIGINAL_BASE_YEAR, 0, 1)&#39;.
Definition: date_type.h:82
VarSpriteGroupScope
VarSpriteGroupScope
Definition: newgrf_spritegroup.h:100
date_func.h
Functions related to dates.
debug.h
Functions related to debugging.
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:310
AirportSpec::GetIndex
byte GetIndex() const
Get the index of this spec.
Definition: newgrf_airport.h:130
StationSettings::never_expire_airports
bool never_expire_airports
never expire airports
Definition: settings_type.h:519
CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition: newgrf_callbacks.h:24
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:249
INSTANTIATE_NEWGRF_CLASS_METHODS
#define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax)
Force instantiation of the methods so we don&#39;t get linker errors.
Definition: newgrf_class_func.h:218
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:37
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0)
Get a resolver for the scope.
Definition: newgrf_spritegroup.cpp:142
GetGRFStringID
StringID GetGRFStringID(uint32 grfid, StringID stringid)
Returns the index for this stringid associated with its grfID.
Definition: newgrf_text.cpp:702
RealSpriteGroup::num_loaded
byte num_loaded
Number of loaded groups.
Definition: newgrf_spritegroup.h:90
AirportResolverObject::ResolveReal
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const
Get the real sprites of the grf.
Definition: newgrf_airport.cpp:202
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:80
_cur_year
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
AirportScopeResolver::GetRandomBits
uint32 GetRandomBits() const
Get a few random bits.
Definition: newgrf_airport.cpp:212
GameSettings::station
StationSettings station
settings related to station management
Definition: settings_type.h:555
RealSpriteGroup
Definition: newgrf_spritegroup.h:79
ResolverObject::root_spritegroup
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition: newgrf_spritegroup.h:339
AT_INVALID
Invalid airport.
Definition: airport.h:44
newgrf_text.h
Header of Action 04 "universal holder" structure and functions.
NUM_AIRPORTS
Maximal number of airports in total.
Definition: airport.h:43
NewGRFClass
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:21
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:62
newgrf_spritegroup.h
Action 2 handling.
AirportScopeResolver::GetVariable
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
Definition: newgrf_airport.cpp:180
PersistentStorageArray::GetValue
TYPE GetValue(uint pos) const
Gets the value from a given position.
Definition: newgrf_storage.h:126
RealSpriteGroup::loaded
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:92
NEW_AIRPORT_OFFSET
Number of the first newgrf airport.
Definition: airport.h:41
PersistentStorageArray::StoreValue
void StoreValue(uint pos, int32 value)
Stores some value at a given position.
Definition: newgrf_storage.h:97
APC_MAX
maximum number of airport classes
Definition: newgrf_airport.h:76
AirportSpec::ResetAirports
static void ResetAirports()
This function initializes the airportspec array.
Definition: newgrf_airport.cpp:135
Airport::layout
byte layout
Airport layout number.
Definition: station_base.h:310
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
safeguards.h
A number of safeguards to prevent using unsafe methods.
SpriteGroup
Definition: newgrf_spritegroup.h:58
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:223
AirportSpec::Get
static const AirportSpec * Get(byte type)
Retrieve airport spec for the given airport.
Definition: newgrf_airport.cpp:98
Airport::psa
PersistentStorage * psa
Persistent storage for NewGRF airports.
Definition: station_base.h:313
RealSpriteGroup::loading
const SpriteGroup ** loading
List of loading groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:93
BaseStation::facilities
StationFacilityByte facilities
The facilities that this station has.
Definition: base_station_base.h:64
AirportScopeResolver
Resolver for the airport scope.
Definition: newgrf_airport.cpp:23
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
AirportSpec::specs
static AirportSpec specs[NUM_AIRPORTS]
Specs of the airports.
Definition: newgrf_airport.h:139
VSG_SCOPE_SELF
Resolved object itself.
Definition: newgrf_spritegroup.h:103
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
GRFFilePropsBase::spritegroup
const struct SpriteGroup * spritegroup[Tcnt]
pointer to the different sprites of the entity
Definition: newgrf_commons.h:321
AirportScopeResolver::st
struct Station * st
Station of the airport for which the callback is run, or NULL for build gui.
Definition: newgrf_airport.cpp:24
AirportScopeResolver::layout
byte layout
Layout of the airport to build.
Definition: newgrf_airport.cpp:26
AirportScopeResolver::StorePSA
void StorePSA(uint pos, int32 value)
Store a value into the object&#39;s persistent storage.
Definition: newgrf_airport.cpp:223
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
GRFFileProps::override
uint16 override
id of the entity been replaced by
Definition: newgrf_commons.h:333
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
BindAirportSpecs
void BindAirportSpecs()
Tie all airportspecs to their class.
Definition: newgrf_airport.cpp:147
RealSpriteGroup::num_loading
byte num_loading
Number of loading groups.
Definition: newgrf_spritegroup.h:91
ResolverObject::grffile
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
Definition: newgrf_spritegroup.h:338
ErrorUnknownCallbackResult
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
Definition: newgrf_commons.cpp:518
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
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:375
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition: newgrf_spritegroup.h:328
NewGRFClass::Get
static NewGRFClass * Get(Tid cls_id)
Get a particular class.
Definition: newgrf_class_func.h:105
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:216
AirportScopeResolver::airport_id
byte airport_id
Type of airport for which the callback is run.
Definition: newgrf_airport.cpp:25
AirportOverrideManager
Definition: newgrf_commons.h:259
GRFFilePropsBase::local_id
uint16 local_id
id defined by the grf file for this entity
Definition: newgrf_commons.h:319
AirportResolverObject
Resolver object for airports.
Definition: newgrf_airport.cpp:48
NewGRFClass::InsertDefaults
static void InsertDefaults()
Initialise the defaults.
Definition: newgrf_airport.cpp:71
BaseStation::random_bits
uint16 random_bits
Random bits assigned to this station.
Definition: base_station_base.h:71
Station::airport
Airport airport
Tile area the airport covers.
Definition: station_base.h:460
OverrideManagerBase::ResetOverride
void ResetOverride()
Resets the override, which is used while initializing game.
Definition: newgrf_commons.cpp:90
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this 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
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition: newgrf_callbacks.h:22
AirportSpec::preview_sprite
SpriteID preview_sprite
preview sprite for this airport
Definition: newgrf_airport.h:116
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
AirportResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0)
Get a resolver for the scope.
Definition: newgrf_airport.cpp:54
AirportSpec
Defines the data structure for an airport.
Definition: newgrf_airport.h:100
station_base.h
Base classes/functions for stations.
GetAirportTextCallback
StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
Get a custom text for the airport.
Definition: newgrf_airport.cpp:278
Station
Station data structure.
Definition: station_base.h:446
AirportSpec::grf_prop
struct GRFFileProps grf_prop
Properties related to the grf file.
Definition: newgrf_airport.h:120
BaseStation::build_date
Date build_date
Date of construction.
Definition: base_station_base.h:69
OverrideManagerBase::GetSubstituteID
uint16 GetSubstituteID(uint16 entity_id) const
Gives the substitute of the entity, as specified by the grf file.
Definition: newgrf_commons.cpp:168
NewGRFClass::name
StringID name
Name of this class.
Definition: newgrf_class.h:41