2010-02-22 16:09:26 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-07-22 16:50:58 +00:00
|
|
|
/** @file newgrf_airport.cpp NewGRF handling of airports. */
|
2010-02-22 16:09:26 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
2010-08-05 11:57:55 +00:00
|
|
|
#include "debug.h"
|
2010-02-22 16:09:26 +00:00
|
|
|
#include "date_func.h"
|
2010-08-05 11:57:55 +00:00
|
|
|
#include "newgrf_spritegroup.h"
|
|
|
|
#include "newgrf_text.h"
|
|
|
|
#include "station_base.h"
|
2010-08-07 22:08:20 +00:00
|
|
|
#include "newgrf_class_func.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/** Resolver for the airport scope. */
|
2012-11-10 20:42:50 +00:00
|
|
|
struct AirportScopeResolver : public ScopeResolver {
|
2019-04-10 21:07:06 +00:00
|
|
|
struct Station *st; ///< Station of the airport for which the callback is run, or \c nullptr for build gui.
|
2012-11-10 20:42:50 +00:00
|
|
|
byte airport_id; ///< Type of airport for which the callback is run.
|
|
|
|
byte layout; ///< Layout of the airport to build.
|
|
|
|
TileIndex tile; ///< Tile for the callback, only valid for airporttile callbacks.
|
|
|
|
|
2018-03-11 13:19:41 +00:00
|
|
|
/**
|
|
|
|
* Constructor of the scope resolver for an airport.
|
|
|
|
* @param ro Surrounding resolver.
|
|
|
|
* @param tile %Tile for the callback, only valid for airporttile callbacks.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param st %Station of the airport for which the callback is run, or \c nullptr for build gui.
|
2018-03-11 13:19:41 +00:00
|
|
|
* @param airport_id Type of airport for which the callback is run.
|
|
|
|
* @param layout Layout of the airport to build.
|
|
|
|
*/
|
|
|
|
AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, byte airport_id, byte layout)
|
|
|
|
: ScopeResolver(ro), st(st), airport_id(airport_id), layout(layout), tile(tile)
|
|
|
|
{
|
|
|
|
}
|
2012-11-10 20:42:50 +00:00
|
|
|
|
2019-03-03 22:25:13 +00:00
|
|
|
uint32 GetRandomBits() const override;
|
|
|
|
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override;
|
|
|
|
void StorePSA(uint pos, int32 value) override;
|
2012-11-10 20:42:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Resolver object for airports. */
|
|
|
|
struct AirportResolverObject : public ResolverObject {
|
|
|
|
AirportScopeResolver airport_scope;
|
|
|
|
|
|
|
|
AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
|
|
|
|
CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
|
|
|
|
|
2019-03-03 22:25:13 +00:00
|
|
|
ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
|
2012-11-10 20:42:50 +00:00
|
|
|
{
|
|
|
|
switch (scope) {
|
|
|
|
case VSG_SCOPE_SELF: return &this->airport_scope;
|
2012-11-10 20:45:59 +00:00
|
|
|
default: return ResolverObject::GetScope(scope, relative);
|
2012-11-10 20:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 22:25:13 +00:00
|
|
|
const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
|
2020-01-26 12:45:51 +00:00
|
|
|
|
|
|
|
GrfSpecFeature GetFeature() const override;
|
|
|
|
uint32 GetDebugID() const override;
|
2012-11-10 20:42:50 +00:00
|
|
|
};
|
|
|
|
|
2010-08-07 22:08:20 +00:00
|
|
|
/**
|
|
|
|
* Reset airport classes to their default state.
|
|
|
|
* This includes initialising the defaults classes with an empty
|
|
|
|
* entry, for standard airports.
|
|
|
|
*/
|
|
|
|
template <typename Tspec, typename Tid, Tid Tmax>
|
|
|
|
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
|
|
|
|
{
|
2012-04-22 16:27:55 +00:00
|
|
|
AirportClass::Get(AirportClass::Allocate('SMAL'))->name = STR_AIRPORT_CLASS_SMALL;
|
|
|
|
AirportClass::Get(AirportClass::Allocate('LARG'))->name = STR_AIRPORT_CLASS_LARGE;
|
|
|
|
AirportClass::Get(AirportClass::Allocate('HUB_'))->name = STR_AIRPORT_CLASS_HUB;
|
|
|
|
AirportClass::Get(AirportClass::Allocate('HELI'))->name = STR_AIRPORT_CLASS_HELIPORTS;
|
2010-08-07 22:08:20 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 16:28:27 +00:00
|
|
|
template <typename Tspec, typename Tid, Tid Tmax>
|
|
|
|
bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-07 22:08:20 +00:00
|
|
|
INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX)
|
2010-02-22 16:09:26 +00:00
|
|
|
|
2010-03-05 23:21:35 +00:00
|
|
|
|
2010-03-18 23:12:38 +00:00
|
|
|
AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
AirportSpec AirportSpec::specs[NUM_AIRPORTS]; ///< Airport specifications.
|
2010-03-05 23:21:23 +00:00
|
|
|
|
2010-02-22 16:09:26 +00:00
|
|
|
/**
|
2010-03-05 23:21:30 +00:00
|
|
|
* Retrieve airport spec for the given airport. If an override is available
|
|
|
|
* it is returned.
|
2010-02-22 16:09:26 +00:00
|
|
|
* @param type index of airport
|
|
|
|
* @return A pointer to the corresponding AirportSpec
|
|
|
|
*/
|
|
|
|
/* static */ const AirportSpec *AirportSpec::Get(byte type)
|
|
|
|
{
|
2010-03-05 23:21:23 +00:00
|
|
|
assert(type < lengthof(AirportSpec::specs));
|
2010-03-18 23:12:38 +00:00
|
|
|
const AirportSpec *as = &AirportSpec::specs[type];
|
|
|
|
if (type >= NEW_AIRPORT_OFFSET && !as->enabled) {
|
2019-03-30 21:12:25 +00:00
|
|
|
if (_airport_mngr.GetGRFID(type) == 0) return as;
|
2010-03-18 23:12:38 +00:00
|
|
|
byte subst_id = _airport_mngr.GetSubstituteID(type);
|
|
|
|
if (subst_id == AT_INVALID) return as;
|
|
|
|
as = &AirportSpec::specs[subst_id];
|
|
|
|
}
|
|
|
|
if (as->grf_prop.override != AT_INVALID) return &AirportSpec::specs[as->grf_prop.override];
|
|
|
|
return as;
|
2010-02-22 16:09:26 +00:00
|
|
|
}
|
|
|
|
|
2010-03-05 23:21:30 +00:00
|
|
|
/**
|
|
|
|
* Retrieve airport spec for the given airport. Even if an override is
|
|
|
|
* available the base spec is returned.
|
|
|
|
* @param type index of airport
|
|
|
|
* @return A pointer to the corresponding AirportSpec
|
|
|
|
*/
|
|
|
|
/* static */ AirportSpec *AirportSpec::GetWithoutOverride(byte type)
|
|
|
|
{
|
|
|
|
assert(type < lengthof(AirportSpec::specs));
|
|
|
|
return &AirportSpec::specs[type];
|
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/** Check whether this airport is available to build. */
|
2010-02-22 16:09:26 +00:00
|
|
|
bool AirportSpec::IsAvailable() const
|
|
|
|
{
|
2010-03-05 23:21:51 +00:00
|
|
|
if (!this->enabled) return false;
|
2010-02-22 16:09:26 +00:00
|
|
|
if (_cur_year < this->min_year) return false;
|
|
|
|
if (_settings_game.station.never_expire_airports) return true;
|
|
|
|
return _cur_year <= this->max_year;
|
|
|
|
}
|
2010-03-05 23:21:23 +00:00
|
|
|
|
2019-03-30 22:20:26 +00:00
|
|
|
/**
|
|
|
|
* Check if the airport would be within the map bounds at the given tile.
|
2019-09-29 20:27:32 +00:00
|
|
|
* @param table Selected layout table. This affects airport rotation, and therefore dimensions.
|
2019-03-30 22:20:26 +00:00
|
|
|
* @param tile Top corner of the airport.
|
|
|
|
* @return true iff the airport would be within the map bounds at the given tile.
|
|
|
|
*/
|
|
|
|
bool AirportSpec::IsWithinMapBounds(byte table, TileIndex tile) const
|
|
|
|
{
|
|
|
|
if (table >= this->num_table) return false;
|
|
|
|
|
|
|
|
byte w = this->size_x;
|
|
|
|
byte h = this->size_y;
|
|
|
|
if (this->rotation[table] == DIR_E || this->rotation[table] == DIR_W) Swap(w, h);
|
|
|
|
|
|
|
|
return TileX(tile) + w < MapSizeX() &&
|
|
|
|
TileY(tile) + h < MapSizeY();
|
|
|
|
}
|
|
|
|
|
2010-03-05 23:21:23 +00:00
|
|
|
/**
|
2011-05-14 18:35:40 +00:00
|
|
|
* This function initializes the airportspec array.
|
2010-03-05 23:21:23 +00:00
|
|
|
*/
|
|
|
|
void AirportSpec::ResetAirports()
|
|
|
|
{
|
|
|
|
extern const AirportSpec _origin_airport_specs[];
|
|
|
|
memset(&AirportSpec::specs, 0, sizeof(AirportSpec::specs));
|
2010-03-18 23:10:35 +00:00
|
|
|
memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NEW_AIRPORT_OFFSET);
|
2010-03-18 23:12:38 +00:00
|
|
|
|
|
|
|
_airport_mngr.ResetOverride();
|
2010-03-05 23:21:23 +00:00
|
|
|
}
|
2010-03-05 23:21:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tie all airportspecs to their class.
|
|
|
|
*/
|
|
|
|
void BindAirportSpecs()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < NUM_AIRPORTS; i++) {
|
|
|
|
AirportSpec *as = AirportSpec::GetWithoutOverride(i);
|
2010-08-07 22:08:20 +00:00
|
|
|
if (as->enabled) AirportClass::Assign(as);
|
2010-03-05 23:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-18 23:12:38 +00:00
|
|
|
void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
|
|
|
|
{
|
|
|
|
byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
|
|
|
|
|
|
|
|
if (airport_id == invalid_ID) {
|
|
|
|
grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(AirportSpec::GetWithoutOverride(airport_id), as, sizeof(*as));
|
|
|
|
|
|
|
|
/* Now add the overrides. */
|
|
|
|
for (int i = 0; i < max_offset; i++) {
|
|
|
|
AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
|
|
|
|
|
|
|
|
if (entity_overrides[i] != as->grf_prop.local_id || grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
|
|
|
|
|
|
|
|
overridden_as->grf_prop.override = airport_id;
|
|
|
|
overridden_as->enabled = false;
|
|
|
|
entity_overrides[i] = invalid_ID;
|
|
|
|
grfid_overrides[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:42:50 +00:00
|
|
|
/* virtual */ uint32 AirportScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
|
2010-08-05 11:57:55 +00:00
|
|
|
{
|
|
|
|
switch (variable) {
|
2012-11-10 20:42:50 +00:00
|
|
|
case 0x40: return this->layout;
|
2010-08-05 11:57:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->st == nullptr) {
|
2010-08-05 11:57:55 +00:00
|
|
|
*available = false;
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (variable) {
|
2010-08-05 12:06:13 +00:00
|
|
|
/* Get a variable from the persistent storage */
|
2019-04-10 21:07:06 +00:00
|
|
|
case 0x7C: return (this->st->airport.psa != nullptr) ? this->st->airport.psa->GetValue(parameter) : 0;
|
2010-08-05 12:06:13 +00:00
|
|
|
|
2012-11-10 20:42:50 +00:00
|
|
|
case 0xF0: return this->st->facilities;
|
|
|
|
case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
|
2010-08-05 11:57:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:42:50 +00:00
|
|
|
return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
|
2010-08-05 11:57:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:42:50 +00:00
|
|
|
/* virtual */ const SpriteGroup *AirportResolverObject::ResolveReal(const RealSpriteGroup *group) const
|
2010-08-05 11:57:55 +00:00
|
|
|
{
|
|
|
|
/* Airport action 2s should always have only 1 "loaded" state, but some
|
|
|
|
* times things don't follow the spec... */
|
|
|
|
if (group->num_loaded > 0) return group->loaded[0];
|
|
|
|
if (group->num_loading > 0) return group->loading[0];
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2010-08-05 11:57:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 12:45:51 +00:00
|
|
|
GrfSpecFeature AirportResolverObject::GetFeature() const
|
|
|
|
{
|
|
|
|
return GSF_AIRPORTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 AirportResolverObject::GetDebugID() const
|
|
|
|
{
|
|
|
|
return AirportSpec::Get(this->airport_scope.airport_id)->grf_prop.local_id;
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:42:50 +00:00
|
|
|
/* virtual */ uint32 AirportScopeResolver::GetRandomBits() const
|
2010-08-05 11:57:55 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
return this->st == nullptr ? 0 : this->st->random_bits;
|
2010-08-05 11:57:55 +00:00
|
|
|
}
|
|
|
|
|
2011-06-12 20:38:46 +00:00
|
|
|
/**
|
|
|
|
* Store a value into the object's persistent storage.
|
|
|
|
* @param pos Position in the persistent storage to use.
|
|
|
|
* @param value Value to store.
|
|
|
|
*/
|
2012-11-10 20:42:50 +00:00
|
|
|
/* virtual */ void AirportScopeResolver::StorePSA(uint pos, int32 value)
|
2011-06-12 20:38:46 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->st == nullptr) return;
|
2011-06-12 20:47:45 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->st->airport.psa == nullptr) {
|
2011-06-12 20:47:45 +00:00
|
|
|
/* There is no need to create a storage if the value is zero. */
|
|
|
|
if (value == 0) return;
|
|
|
|
|
|
|
|
/* Create storage on first modification. */
|
2019-04-10 21:07:06 +00:00
|
|
|
uint32 grfid = (this->ro.grffile != nullptr) ? this->ro.grffile->grfid : 0;
|
2011-06-12 20:47:45 +00:00
|
|
|
assert(PersistentStorage::CanAllocateItem());
|
2013-12-23 18:09:29 +00:00
|
|
|
this->st->airport.psa = new PersistentStorage(grfid, GSF_AIRPORTS, this->st->airport.tile);
|
2011-06-12 20:47:45 +00:00
|
|
|
}
|
2012-11-10 20:42:50 +00:00
|
|
|
this->st->airport.psa->StoreValue(pos, value);
|
2011-06-12 20:38:46 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Constructor of the airport resolver.
|
|
|
|
* @param tile %Tile for the callback, only valid for airporttile callbacks.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param st %Station of the airport for which the callback is run, or \c nullptr for build gui.
|
2012-11-10 20:46:39 +00:00
|
|
|
* @param airport_id Type of airport for which the callback is run.
|
|
|
|
* @param layout Layout of the airport to build.
|
|
|
|
* @param callback Callback ID.
|
|
|
|
* @param param1 First parameter (var 10) of the callback.
|
|
|
|
* @param param2 Second parameter (var 18) of the callback.
|
|
|
|
*/
|
2012-11-10 20:42:50 +00:00
|
|
|
AirportResolverObject::AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
|
|
|
|
CallbackID callback, uint32 param1, uint32 param2)
|
2013-11-24 14:41:19 +00:00
|
|
|
: ResolverObject(AirportSpec::Get(airport_id)->grf_prop.grffile, callback, param1, param2), airport_scope(*this, tile, st, airport_id, layout)
|
2010-08-05 11:57:55 +00:00
|
|
|
{
|
2014-03-03 20:02:31 +00:00
|
|
|
this->root_spritegroup = AirportSpec::Get(airport_id)->grf_prop.spritegroup[0];
|
2010-08-05 11:57:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:42:50 +00:00
|
|
|
SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
AirportResolverObject object(INVALID_TILE, nullptr, as->GetIndex(), layout);
|
2014-03-03 20:02:31 +00:00
|
|
|
const SpriteGroup *group = object.Resolve();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (group == nullptr) return as->preview_sprite;
|
2010-08-05 12:03:34 +00:00
|
|
|
|
|
|
|
return group->GetResult();
|
|
|
|
}
|
|
|
|
|
2010-08-05 11:57:55 +00:00
|
|
|
uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
|
|
|
|
{
|
2012-11-10 20:42:50 +00:00
|
|
|
AirportResolverObject object(tile, st, st->airport.type, st->airport.layout, callback, param1, param2);
|
2014-03-03 20:02:31 +00:00
|
|
|
return object.ResolveCallback();
|
2010-08-05 11:57:55 +00:00
|
|
|
}
|
2010-08-05 12:04:33 +00:00
|
|
|
|
2011-05-02 16:14:23 +00:00
|
|
|
/**
|
|
|
|
* Get a custom text for the airport.
|
|
|
|
* @param as The airport type's specification.
|
|
|
|
* @param layout The layout index.
|
|
|
|
* @param callback The callback to call.
|
|
|
|
* @return The custom text.
|
|
|
|
*/
|
2010-08-05 12:04:33 +00:00
|
|
|
StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
AirportResolverObject object(INVALID_TILE, nullptr, as->GetIndex(), layout, (CallbackID)callback);
|
2014-03-03 20:02:31 +00:00
|
|
|
uint16 cb_res = object.ResolveCallback();
|
2011-11-08 17:24:43 +00:00
|
|
|
if (cb_res == CALLBACK_FAILED || cb_res == 0x400) return STR_UNDEFINED;
|
|
|
|
if (cb_res > 0x400) {
|
|
|
|
ErrorUnknownCallbackResult(as->grf_prop.grffile->grfid, callback, cb_res);
|
|
|
|
return STR_UNDEFINED;
|
|
|
|
}
|
2010-08-05 12:04:33 +00:00
|
|
|
|
2011-11-08 17:24:43 +00:00
|
|
|
return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);
|
2010-08-05 12:04:33 +00:00
|
|
|
}
|