1
0
Fork 0

Add: NewGRF persistent storage for vehicles.

pull/10670/head
Peter Nelson 2023-04-17 08:14:51 +01:00
parent 3038e2975b
commit c4931c3a6e
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
8 changed files with 41 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#include "newgrf_railtype.h" #include "newgrf_railtype.h"
#include "newgrf_roadtype.h" #include "newgrf_roadtype.h"
#include "ship.h" #include "ship.h"
#include "newgrf_debug.h"
#include "safeguards.h" #include "safeguards.h"
@ -315,6 +316,23 @@ static uint8_t MapAircraftMovementAction(const Aircraft *v)
return this->v == nullptr ? 0 : this->v->waiting_triggers; return this->v == nullptr ? 0 : this->v->waiting_triggers;
} }
/* virtual */ void VehicleScopeResolver::StorePSA(uint pos, int32_t value)
{
if (this->v == nullptr) return;
/* const_cast because we're in too deep to change Vehicle to be non-const. */
Vehicle *v = const_cast<Vehicle *>(this->v);
if (v->psa == nullptr) {
/* There is no need to create a storage if the value is zero. */
if (value == 0) return;
uint32_t grfid = (this->ro.grffile != nullptr) ? this->ro.grffile->grfid : 0;
assert(PersistentStorage::CanAllocateItem());
v->psa = new PersistentStorage(grfid, GetGrfSpecFeature(this->v->type), INVALID_TILE);
}
v->psa->StoreValue(pos, value);
}
/* virtual */ ScopeResolver *VehicleResolverObject::GetScope(VarSpriteGroupScope scope, uint8_t relative) /* virtual */ ScopeResolver *VehicleResolverObject::GetScope(VarSpriteGroupScope scope, uint8_t relative)
{ {
@ -693,6 +711,8 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec
default: return 0x00; default: return 0x00;
} }
case 0x7C: return (v->psa != nullptr) ? v->psa->GetValue(parameter) : 0;
case 0xFE: case 0xFE:
case 0xFF: { case 0xFF: {
uint16_t modflags = 0; uint16_t modflags = 0;

View File

@ -41,6 +41,7 @@ struct VehicleScopeResolver : public ScopeResolver {
uint32_t GetRandomBits() const override; uint32_t GetRandomBits() const override;
uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override; uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
uint32_t GetTriggers() const override; uint32_t GetTriggers() const override;
void StorePSA(uint pos, int32_t value) override;
}; };
/** Resolver for a vehicle (chain) */ /** Resolver for a vehicle (chain) */

View File

@ -48,6 +48,7 @@
#include "../subsidy_base.h" #include "../subsidy_base.h"
#include "../subsidy_func.h" #include "../subsidy_func.h"
#include "../newgrf.h" #include "../newgrf.h"
#include "../newgrf_debug.h"
#include "../newgrf_station.h" #include "../newgrf_station.h"
#include "../engine_func.h" #include "../engine_func.h"
#include "../rail_gui.h" #include "../rail_gui.h"
@ -290,6 +291,11 @@ static void InitializeWindowsAndCaches()
it->tile = t->xy; it->tile = t->xy;
} }
} }
for (Vehicle *v : Vehicle::Iterate()) {
if (v->psa != nullptr) {
v->psa->feature = GetGrfSpecFeature(v->type);
}
}
for (RoadVehicle *rv : RoadVehicle::Iterate()) { for (RoadVehicle *rv : RoadVehicle::Iterate()) {
if (rv->IsFrontEngine()) { if (rv->IsFrontEngine()) {
rv->CargoChanged(); rv->CargoChanged();

View File

@ -397,6 +397,8 @@ enum SaveLoadVersion : uint16_t {
SLV_INCREASE_HOUSE_LIMIT, ///< 348 PR#12288 Increase house limit to 4096. SLV_INCREASE_HOUSE_LIMIT, ///< 348 PR#12288 Increase house limit to 4096.
SLV_VARIABLE_PERSISTENT_STORAGE, ///< 349 PR#10670 NewGRF persistent storage moved from fixed array to dynamic. SLV_VARIABLE_PERSISTENT_STORAGE, ///< 349 PR#10670 NewGRF persistent storage moved from fixed array to dynamic.
SLV_VEHICLE_STORAGE, ///< 350 PR#10670 Addition of persistent storage for vehicles.
SL_MAX_VERSION, ///< Highest possible saveload version SL_MAX_VERSION, ///< Highest possible saveload version
}; };

View File

@ -767,6 +767,7 @@ public:
SLE_CONDVAR(Vehicle, depot_unbunching_last_departure, SLE_UINT64, SLV_DEPOT_UNBUNCHING, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, depot_unbunching_last_departure, SLE_UINT64, SLV_DEPOT_UNBUNCHING, SL_MAX_VERSION),
SLE_CONDVAR(Vehicle, depot_unbunching_next_departure, SLE_UINT64, SLV_DEPOT_UNBUNCHING, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, depot_unbunching_next_departure, SLE_UINT64, SLV_DEPOT_UNBUNCHING, SL_MAX_VERSION),
SLE_CONDVAR(Vehicle, round_trip_time, SLE_INT32, SLV_DEPOT_UNBUNCHING, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, round_trip_time, SLE_INT32, SLV_DEPOT_UNBUNCHING, SL_MAX_VERSION),
SLE_CONDREF(Vehicle, psa, REF_STORAGE, SLV_VEHICLE_STORAGE, SL_MAX_VERSION),
}; };
inline const static SaveLoadCompatTable compat_description = _vehicle_common_sl_compat; inline const static SaveLoadCompatTable compat_description = _vehicle_common_sl_compat;

View File

@ -84,6 +84,13 @@ class NIHVehicle : public NIHelper {
VehicleResolverObject ro(v->engine_type, v, VehicleResolverObject::WO_CACHED); VehicleResolverObject ro(v->engine_type, v, VehicleResolverObject::WO_CACHED);
return ro.GetScope(VSG_SCOPE_SELF)->GetVariable(var, param, avail); return ro.GetScope(VSG_SCOPE_SELF)->GetVariable(var, param, avail);
} }
std::span<const int32_t> GetPSA(uint index, uint32_t) const override
{
const Vehicle *v = Vehicle::Get(index);
if (v->psa == nullptr) return {};
return v->psa->storage;
}
}; };
static const NIFeature _nif_vehicle = { static const NIFeature _nif_vehicle = {

View File

@ -901,6 +901,8 @@ Vehicle::~Vehicle()
* it may happen that vehicle chain is deleted when visible */ * it may happen that vehicle chain is deleted when visible */
if (!(this->vehstatus & VS_HIDDEN)) this->MarkAllViewportsDirty(); if (!(this->vehstatus & VS_HIDDEN)) this->MarkAllViewportsDirty();
delete this->psa;
Vehicle *v = this->Next(); Vehicle *v = this->Next();
this->SetNext(nullptr); this->SetNext(nullptr);

View File

@ -367,6 +367,8 @@ public:
mutable MutableSpriteCache sprite_cache; ///< Cache of sprites and values related to recalculating them, see #MutableSpriteCache mutable MutableSpriteCache sprite_cache; ///< Cache of sprites and values related to recalculating them, see #MutableSpriteCache
struct PersistentStorage *psa; ///< Persistent storage
/** /**
* Calculates the weight value that this vehicle will have when fully loaded with its current cargo. * Calculates the weight value that this vehicle will have when fully loaded with its current cargo.
* @return Weight value in tonnes. * @return Weight value in tonnes.