Link graph: Store last compression in scaled date ticks

Higher accuracy than using dates at high day legnths
pull/564/head
Jonathan G Rennison 12 months ago
parent 2936bf370f
commit e1cce4d9f7

@ -65,6 +65,9 @@ void CheckScaledDateTicksWrap()
extern void AdjustVehicleScaledTickBase(int64 delta);
AdjustVehicleScaledTickBase(-tick_adjust);
extern void AdjustLinkGraphScaledTickBase(int64 delta);
AdjustLinkGraphScaledTickBase(-tick_adjust);
}
void RebaseScaledDateTicksBase()

@ -39,7 +39,6 @@ inline void LinkGraph::BaseNode::Init(TileIndex xy, StationID st, uint demand)
*/
void LinkGraph::ShiftDates(int interval)
{
this->last_compression += interval;
for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
BaseNode &source = this->nodes[node1];
if (source.last_update != INVALID_DATE) source.last_update += interval;
@ -54,7 +53,7 @@ void LinkGraph::ShiftDates(int interval)
void LinkGraph::Compress()
{
this->last_compression = (_date + this->last_compression) / 2;
this->last_compression = (_scaled_date_ticks + this->last_compression) / 2;
for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
this->nodes[node1].supply /= 2;
}
@ -79,8 +78,8 @@ void LinkGraph::Compress()
*/
void LinkGraph::Merge(LinkGraph *other)
{
Date age = _date - this->last_compression + 1;
Date other_age = _date - other->last_compression + 1;
uint32 age = ClampTo<uint32>(CeilDivT<DateTicksScaled>(_scaled_date_ticks - this->last_compression + 1, DAY_TICKS));
uint32 other_age = ClampTo<uint32>(CeilDivT<DateTicksScaled>(_scaled_date_ticks - other->last_compression + 1, DAY_TICKS));
NodeID first = this->Size();
this->nodes.reserve(first + other->Size());
for (NodeID node1 = 0; node1 < other->Size(); ++node1) {
@ -265,3 +264,14 @@ void LinkGraph::Init(uint size)
assert(this->Size() == 0);
this->nodes.resize(size);
}
void AdjustLinkGraphScaledTickBase(int64 delta)
{
for (LinkGraph *lg : LinkGraph::Iterate()) lg->last_compression += delta;
}
void LinkGraphFixupLastCompressionAfterLoad()
{
/* last_compression was previously a Date, change it to a DateTicksScaled */
for (LinkGraph *lg : LinkGraph::Iterate()) lg->last_compression = DateToScaledDateTicks((Date)lg->last_compression);
}

@ -289,7 +289,7 @@ public:
static const uint STALE_LINK_DEPOT_TIMEOUT = 1024;
/** Minimum number of days between subsequent compressions of a LG. */
static const uint COMPRESSION_INTERVAL = 256;
static const uint COMPRESSION_INTERVAL = 256 * DAY_TICKS;
/**
* Scale a value from a link graph of age orig_age for usage in one of age
@ -310,7 +310,7 @@ public:
* Real constructor.
* @param cargo Cargo the link graph is about.
*/
LinkGraph(CargoID cargo) : cargo(cargo), last_compression(_date) {}
LinkGraph(CargoID cargo) : cargo(cargo), last_compression(_scaled_date_ticks) {}
void Init(uint size);
void ShiftDates(int interval);
@ -349,7 +349,7 @@ public:
* Get date of last compression.
* @return Date of last compression.
*/
inline Date LastCompression() const { return this->last_compression; }
inline DateTicksScaled LastCompression() const { return this->last_compression; }
/**
* Get the cargo ID this component's link graph refers to.
@ -364,7 +364,7 @@ public:
*/
inline uint Monthly(uint base) const
{
return base * 30 / (_date - this->last_compression + 1);
return (static_cast<uint64>(base) * 30 * DAY_TICKS * _settings_game.economy.day_length_factor) / std::max<uint64>(_scaled_date_ticks - this->last_compression, DAY_TICKS);
}
NodeID AddNode(const Station *st);
@ -391,8 +391,11 @@ protected:
friend upstream_sl::SlLinkgraphNode;
friend upstream_sl::SlLinkgraphEdge;
friend void AdjustLinkGraphScaledTickBase(int64 delta);
friend void LinkGraphFixupLastCompressionAfterLoad();
CargoID cargo; ///< Cargo of this component's link graph.
Date last_compression; ///< Last time the capacities and supplies were compressed.
DateTicksScaled last_compression; ///< Last time the capacities and supplies were compressed.
NodeVector nodes; ///< Nodes in the component.
EdgeMatrix edges; ///< Edges in the component.

@ -913,6 +913,12 @@ bool AfterLoadGame()
_settings_game.linkgraph.recalc_time *= SECONDS_PER_DAY;
}
/* Convert link graph last compression from date to scaled ticks. */
if (SlXvIsFeatureMissing(XSLFI_LINKGRAPH_DAY_SCALE, 4)) {
extern void LinkGraphFixupLastCompressionAfterLoad();
LinkGraphFixupLastCompressionAfterLoad();
}
/* Load the sprites */
GfxLoadSprites();
LoadStringWidthTable();

@ -125,7 +125,7 @@ public:
SaveLoadTable GetLinkGraphDesc()
{
static const SaveLoad link_graph_desc[] = {
SLE_VAR(LinkGraph, last_compression, SLE_INT32),
SLE_VAR(LinkGraph, last_compression, SLE_VAR_I64 | SLE_FILE_I32),
SLEG_CONDVAR("num_nodes", _num_nodes, SLE_UINT16, SL_MIN_VERSION, SLV_SAVELOAD_LIST_LENGTH),
SLE_VAR(LinkGraph, cargo, SLE_UINT8),
SLEG_STRUCTLIST("nodes", SlLinkgraphNode),

@ -107,7 +107,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = {
{ XSLFI_EXTRA_LARGE_MAP, XSCF_NULL, 0, 1, "extra_large_map", nullptr, nullptr, nullptr },
{ XSLFI_REVERSE_AT_WAYPOINT, XSCF_NULL, 1, 1, "reverse_at_waypoint", nullptr, nullptr, nullptr },
{ XSLFI_VEH_LIFETIME_PROFIT, XSCF_NULL, 1, 1, "veh_lifetime_profit", nullptr, nullptr, nullptr },
{ XSLFI_LINKGRAPH_DAY_SCALE, XSCF_NULL, 3, 3, "linkgraph_day_scale", nullptr, nullptr, nullptr },
{ XSLFI_LINKGRAPH_DAY_SCALE, XSCF_NULL, 4, 4, "linkgraph_day_scale", nullptr, nullptr, nullptr },
{ XSLFI_TEMPLATE_REPLACEMENT, XSCF_NULL, 9, 9, "template_replacement", nullptr, nullptr, "TRPL,TMPL" },
{ XSLFI_MORE_RAIL_TYPES, XSCF_NULL, 0, 1, "more_rail_types", nullptr, nullptr, nullptr },
{ XSLFI_CARGO_TYPE_ORDERS, XSCF_NULL, 3, 3, "cargo_type_orders", nullptr, nullptr, "ORDX,VEOX" },

@ -31,7 +31,8 @@ static uint16 _num_nodes;
SaveLoadTable GetLinkGraphDesc()
{
static const SaveLoad link_graph_desc[] = {
SLE_VAR(LinkGraph, last_compression, SLE_INT32),
SLE_CONDVAR_X(LinkGraph, last_compression, SLE_VAR_I64 | SLE_FILE_I32, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_LINKGRAPH_DAY_SCALE, 0, 3)),
SLE_CONDVAR_X(LinkGraph, last_compression, SLE_INT64, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_LINKGRAPH_DAY_SCALE, 4)),
SLEG_VAR(_num_nodes, SLE_UINT16),
SLE_VAR(LinkGraph, cargo, SLE_UINT8),
};
@ -225,7 +226,7 @@ static void DoSave_LGRJ(LinkGraphJob *lgj)
{
SlObjectSaveFiltered(lgj, _filtered_job_desc);
_num_nodes = lgj->Size();
SlObjectSaveFiltered(const_cast<LinkGraph *>(&lgj->Graph()), GetLinkGraphDesc()); // GetLinkGraphDesc has no conditionals
SlObject(const_cast<LinkGraph *>(&lgj->Graph()), GetLinkGraphDesc());
Save_LinkGraph(const_cast<LinkGraph &>(lgj->Graph()));
}
@ -236,7 +237,7 @@ static void DoSave_LGRJ(LinkGraphJob *lgj)
static void DoSave_LGRP(LinkGraph *lg)
{
_num_nodes = lg->Size();
SlObjectSaveFiltered(lg, GetLinkGraphDesc()); // GetLinkGraphDesc has no conditionals
SlObject(lg, GetLinkGraphDesc());
Save_LinkGraph(*lg);
}
@ -253,7 +254,7 @@ static void Load_LGRP()
NOT_REACHED();
}
LinkGraph *lg = new (index) LinkGraph();
SlObjectLoadFiltered(lg, GetLinkGraphDesc()); // GetLinkGraphDesc has no conditionals
SlObject(lg, GetLinkGraphDesc());
lg->Init(_num_nodes);
Load_LinkGraph(*lg);
}
@ -278,7 +279,7 @@ static void Load_LGRJ()
GetLinkGraphJobDayLengthScaleAfterLoad(lgj);
}
LinkGraph &lg = const_cast<LinkGraph &>(lgj->Graph());
SlObjectLoadFiltered(&lg, GetLinkGraphDesc()); // GetLinkGraphDesc has no conditionals
SlObject(&lg, GetLinkGraphDesc());
lg.Init(_num_nodes);
Load_LinkGraph(lg);
}

@ -4531,8 +4531,8 @@ void DeleteStaleLinks(Station *from)
return result;
});
assert(_date >= lg->LastCompression());
if ((uint)(_date - lg->LastCompression()) > std::max<uint>(LinkGraph::COMPRESSION_INTERVAL / _settings_game.economy.day_length_factor, 1)) {
assert(_scaled_date_ticks >= lg->LastCompression());
if ((_scaled_date_ticks - lg->LastCompression()) > LinkGraph::COMPRESSION_INTERVAL) {
lg->Compress();
}
}

Loading…
Cancel
Save