Link graph: Store last compression in scaled date ticks

Higher accuracy than using dates at high day legnths
This commit is contained in:
Jonathan G Rennison
2023-07-02 16:44:24 +01:00
parent 2936bf370f
commit e1cce4d9f7
8 changed files with 41 additions and 18 deletions

View File

@@ -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);
}

View File

@@ -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.