Split date types into calendar and economy dates

See: 735abfe1
This commit is contained in:
Jonathan G Rennison
2024-02-13 21:34:09 +00:00
parent fad5ee56e7
commit 7ce06e22b8
141 changed files with 1325 additions and 1082 deletions

View File

@@ -30,7 +30,7 @@ inline void LinkGraph::BaseNode::Init(TileIndex xy, StationID st, uint demand)
this->supply = 0;
this->demand = demand;
this->station = st;
this->last_update = INVALID_DATE;
this->last_update = EconTime::INVALID_DATE;
}
/**
@@ -42,13 +42,13 @@ void LinkGraph::ShiftDates(DateDelta interval)
{
for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
BaseNode &source = this->nodes[node1];
if (source.last_update != INVALID_DATE) source.last_update += interval;
if (source.last_update != EconTime::INVALID_DATE) source.last_update += interval;
}
for (auto &it : this->edges) {
BaseEdge &edge = it.second;
if (edge.last_unrestricted_update != INVALID_DATE) edge.last_unrestricted_update += interval;
if (edge.last_restricted_update != INVALID_DATE) edge.last_restricted_update += interval;
if (edge.last_aircraft_update != INVALID_DATE) edge.last_aircraft_update += interval;
if (edge.last_unrestricted_update != EconTime::INVALID_DATE) edge.last_unrestricted_update += interval;
if (edge.last_restricted_update != EconTime::INVALID_DATE) edge.last_restricted_update += interval;
if (edge.last_aircraft_update != EconTime::INVALID_DATE) edge.last_aircraft_update += interval;
}
}
@@ -172,9 +172,9 @@ static void AddEdge(LinkGraph::BaseEdge &edge, uint capacity, uint usage, uint32
edge.capacity = capacity;
edge.usage = usage;
edge.travel_time_sum = static_cast<uint64_t>(travel_time) * capacity;
if (mode & EUM_UNRESTRICTED) edge.last_unrestricted_update = _date;
if (mode & EUM_RESTRICTED) edge.last_restricted_update = _date;
if (mode & EUM_AIRCRAFT) edge.last_aircraft_update = _date;
if (mode & EUM_UNRESTRICTED) edge.last_unrestricted_update = EconTime::CurDate();
if (mode & EUM_RESTRICTED) edge.last_restricted_update = EconTime::CurDate();
if (mode & EUM_AIRCRAFT) edge.last_aircraft_update = EconTime::CurDate();
}
/**
@@ -250,9 +250,9 @@ void LinkGraph::Edge::Update(uint capacity, uint usage, uint32_t travel_time, Ed
}
edge.usage = std::max(edge.usage, usage);
}
if (mode & EUM_UNRESTRICTED) edge.last_unrestricted_update = _date;
if (mode & EUM_RESTRICTED) edge.last_restricted_update = _date;
if (mode & EUM_AIRCRAFT) edge.last_aircraft_update = _date;
if (mode & EUM_UNRESTRICTED) edge.last_unrestricted_update = EconTime::CurDate();
if (mode & EUM_RESTRICTED) edge.last_restricted_update = EconTime::CurDate();
if (mode & EUM_AIRCRAFT) edge.last_aircraft_update = EconTime::CurDate();
}
/**
@@ -279,10 +279,10 @@ void AdjustLinkGraphStateTicksBase(StateTicksDelta delta)
void LinkGraphFixupLastCompressionAfterLoad()
{
/* last_compression was previously a Date, change it to a StateTicks */
for (LinkGraph *lg : LinkGraph::Iterate()) lg->last_compression = DateToStateTicks((Date)lg->last_compression.base());
for (LinkGraph *lg : LinkGraph::Iterate()) lg->last_compression = DateToStateTicks((EconTime::Date)lg->last_compression.base());
for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
LinkGraph *lg = &(const_cast<LinkGraph &>(lgj->Graph()));
lg->last_compression = DateToStateTicks((Date)lg->last_compression.base());
lg->last_compression = DateToStateTicks((EconTime::Date)lg->last_compression.base());
}
}

View File

@@ -57,7 +57,7 @@ public:
uint demand; ///< Acceptance at the station.
StationID station; ///< Station ID.
TileIndex xy; ///< Location of the station referred to by the node.
Date last_update; ///< When the supply was last updated.
EconTime::Date last_update; ///< When the supply was last updated.
void Init(TileIndex xy = INVALID_TILE, StationID st = INVALID_STATION, uint demand = 0);
};
@@ -71,18 +71,18 @@ public:
uint capacity; ///< Capacity of the link.
uint usage; ///< Usage of the link.
uint64_t travel_time_sum; ///< Sum of the travel times of the link, in ticks.
Date last_unrestricted_update; ///< When the unrestricted part of the link was last updated.
Date last_restricted_update; ///< When the restricted part of the link was last updated.
Date last_aircraft_update; ///< When aircraft capacity of the link was last updated.
EconTime::Date last_unrestricted_update; ///< When the unrestricted part of the link was last updated.
EconTime::Date last_restricted_update; ///< When the restricted part of the link was last updated.
EconTime::Date last_aircraft_update; ///< When aircraft capacity of the link was last updated.
void Init()
{
this->capacity = 0;
this->usage = 0;
this->travel_time_sum = 0;
this->last_unrestricted_update = INVALID_DATE;
this->last_restricted_update = INVALID_DATE;
this->last_aircraft_update = INVALID_DATE;
this->last_unrestricted_update = EconTime::INVALID_DATE;
this->last_restricted_update = EconTime::INVALID_DATE;
this->last_aircraft_update = EconTime::INVALID_DATE;
}
BaseEdge() { this->Init(); }
@@ -130,25 +130,25 @@ public:
* Get the date of the last update to the edge's unrestricted capacity.
* @return Last update.
*/
Date LastUnrestrictedUpdate() const { return this->edge->last_unrestricted_update; }
EconTime::Date LastUnrestrictedUpdate() const { return this->edge->last_unrestricted_update; }
/**
* Get the date of the last update to the edge's restricted capacity.
* @return Last update.
*/
Date LastRestrictedUpdate() const { return this->edge->last_restricted_update; }
EconTime::Date LastRestrictedUpdate() const { return this->edge->last_restricted_update; }
/**
* Get the date of the last update to the edge's aircraft capacity.
* @return Last update.
*/
Date LastAircraftUpdate() const { return this->edge->last_aircraft_update; }
EconTime::Date LastAircraftUpdate() const { return this->edge->last_aircraft_update; }
/**
* Get the date of the last update to any part of the edge's capacity.
* @return Last update.
*/
Date LastUpdate() const { return std::max(this->edge->last_unrestricted_update, this->edge->last_restricted_update); }
EconTime::Date LastUpdate() const { return std::max(this->edge->last_unrestricted_update, this->edge->last_restricted_update); }
};
/**
@@ -192,7 +192,7 @@ public:
* Get node's last update.
* @return Last update.
*/
Date LastUpdate() const { return this->node.last_update; }
EconTime::Date LastUpdate() const { return this->node.last_update; }
/**
* Get the location of the station associated with the node.
@@ -219,9 +219,9 @@ public:
*/
Edge(BaseEdge &edge) : EdgeWrapper<BaseEdge>(edge) {}
void Update(uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode);
void Restrict() { this->edge->last_unrestricted_update = INVALID_DATE; }
void Release() { this->edge->last_restricted_update = INVALID_DATE; }
void ClearAircraft() { this->edge->last_aircraft_update = INVALID_DATE; }
void Restrict() { this->edge->last_unrestricted_update = EconTime::INVALID_DATE; }
void Release() { this->edge->last_restricted_update = EconTime::INVALID_DATE; }
void ClearAircraft() { this->edge->last_aircraft_update = EconTime::INVALID_DATE; }
};
/**
@@ -261,7 +261,7 @@ public:
void UpdateSupply(uint supply)
{
this->node.supply += supply;
this->node.last_update = _date;
this->node.last_update = EconTime::CurDate();
}
/**

View File

@@ -26,10 +26,10 @@ INSTANTIATE_POOL_METHODS(LinkGraphJob)
*/
/* static */ Path *Path::invalid_path = new Path(INVALID_NODE, true);
static DateTicks GetLinkGraphJobJoinDateTicks(uint duration_multiplier)
static EconTime::DateTicks GetLinkGraphJobJoinDateTicks(uint duration_multiplier)
{
DateTicksDelta ticks = (_settings_game.linkgraph.recalc_time * DAY_TICKS * duration_multiplier) / (SECONDS_PER_DAY * _settings_game.economy.day_length_factor);
return ticks + NowDateTicks();
return ticks + EconTime::CurDateTicks();
}
/**
@@ -44,7 +44,7 @@ LinkGraphJob::LinkGraphJob(const LinkGraph &orig, uint duration_multiplier) :
link_graph(orig),
settings(_settings_game.linkgraph),
join_date_ticks(GetLinkGraphJobJoinDateTicks(duration_multiplier)),
start_date_ticks(NowDateTicks()),
start_date_ticks(EconTime::CurDateTicks()),
job_completed(false),
job_aborted(false)
{
@@ -130,14 +130,14 @@ void LinkGraphJob::FinaliseJob()
LinkGraph::ConstEdge lg_edge = lg->GetConstEdge(edge.From(), edge.To());
if (st2 == nullptr || st2->goods[this->Cargo()].link_graph != this->link_graph.index ||
st2->goods[this->Cargo()].node != edge.To() ||
lg_edge.LastUpdate() == INVALID_DATE) {
lg_edge.LastUpdate() == EconTime::INVALID_DATE) {
/* Edge has been removed. Delete flows. */
StationIDStack erased = flows.DeleteFlows(to);
/* Delete old flows for source stations which have been deleted
* from the new flows. This avoids flow cycles between old and
* new flows. */
while (!erased.IsEmpty()) geflows.erase(erased.Pop());
} else if (lg_edge.LastUnrestrictedUpdate() == INVALID_DATE) {
} else if (lg_edge.LastUnrestrictedUpdate() == EconTime::INVALID_DATE) {
/* Edge is fully restricted. */
flows.RestrictFlows(to);
}
@@ -242,7 +242,7 @@ void LinkGraphJob::Init()
distance_anno = calculate_distance();
}
if (edge.LastAircraftUpdate() != INVALID_DATE && aircraft_link_scale > 100) {
if (edge.LastAircraftUpdate() != EconTime::INVALID_DATE && aircraft_link_scale > 100) {
distance_anno *= aircraft_link_scale;
distance_anno /= 100;
}

View File

@@ -139,8 +139,8 @@ protected:
std::shared_ptr<LinkGraphJobGroup> group; ///< Job group thread the job is running in or nullptr if it's running in the main thread.
const LinkGraphSettings settings; ///< Copy of _settings_game.linkgraph at spawn time.
DateTicks join_date_ticks; ///< Date when the job is to be joined.
DateTicks start_date_ticks; ///< Date when the job was started.
EconTime::DateTicks join_date_ticks; ///< Date when the job is to be joined.
EconTime::DateTicks start_date_ticks; ///< Date when the job was started.
NodeAnnotationVector nodes; ///< Extra node data necessary for link graph calculation.
EdgeAnnotationVector edges; ///< Edge data necessary for link graph calculation.
std::atomic<bool> job_completed; ///< Is the job still running. This is accessed by multiple threads and reads may be stale.
@@ -263,7 +263,7 @@ public:
* settings have to be brutally const-casted in order to populate them.
*/
LinkGraphJob() : settings(_settings_game.linkgraph),
join_date_ticks(INVALID_DATE_TICKS), start_date_ticks(INVALID_DATE_TICKS), job_completed(false), job_aborted(false) {}
join_date_ticks(EconTime::INVALID_DATE_TICKS), start_date_ticks(EconTime::INVALID_DATE_TICKS), job_completed(false), job_aborted(false) {}
LinkGraphJob(const LinkGraph &orig, uint duration_multiplier);
~LinkGraphJob();
@@ -298,19 +298,19 @@ public:
* @param tick_offset Optional number of ticks to add to the current date
* @return True if job should be finished by now, false if not.
*/
inline bool IsScheduledToBeJoined(int tick_offset = 0) const { return this->join_date_ticks <= NowDateTicks() + tick_offset; }
inline bool IsScheduledToBeJoined(int tick_offset = 0) const { return this->join_date_ticks <= EconTime::CurDateTicks() + tick_offset; }
/**
* Get the date when the job should be finished.
* @return Join date.
*/
inline DateTicks JoinDateTicks() const { return join_date_ticks; }
inline EconTime::DateTicks JoinDateTicks() const { return join_date_ticks; }
/**
* Get the date when the job was started.
* @return Start date.
*/
inline DateTicks StartDateTicks() const { return start_date_ticks; }
inline EconTime::DateTicks StartDateTicks() const { return start_date_ticks; }
/**
* Change the start and join dates on date cheating.

View File

@@ -87,7 +87,7 @@ void LinkGraphSchedule::SpawnNext()
lg->index, lg->Size(), cost, duration_multiplier);
} else {
// find right place to insert
auto iter = std::upper_bound(this->running.begin(), this->running.end(), job->JoinDateTicks(), [](DateTicks a, const std::unique_ptr<LinkGraphJob> &b) {
auto iter = std::upper_bound(this->running.begin(), this->running.end(), job->JoinDateTicks(), [](EconTime::DateTicks a, const std::unique_ptr<LinkGraphJob> &b) {
return a < b->JoinDateTicks();
});
this->running.insert(iter, std::move(job));
@@ -281,11 +281,11 @@ void LinkGraphJobGroup::JoinThread()
std::vector<LinkGraphJob *> bucket;
uint bucket_cost = 0;
DateTicks bucket_join_date = 0;
EconTime::DateTicks bucket_join_date = 0;
auto flush_bucket = [&]() {
if (!bucket_cost) return;
DEBUG(linkgraph, 2, "LinkGraphJobGroup::ExecuteJobSet: Creating Job Group: jobs: " PRINTF_SIZE ", cost: %u, join after: " OTTD_PRINTF64,
bucket.size(), bucket_cost, (bucket_join_date - NowDateTicks()).base());
bucket.size(), bucket_cost, (bucket_join_date - EconTime::CurDateTicks()).base());
auto group = std::make_shared<LinkGraphJobGroup>(constructor_token(), std::move(bucket));
group->SpawnThread();
bucket_cost = 0;
@@ -318,12 +318,12 @@ void StateGameLoop_LinkGraphPauseControl()
if (!LinkGraphSchedule::instance.IsJoinWithUnfinishedJobDue()) {
DoCommandP(0, PM_PAUSED_LINK_GRAPH, 0, CMD_PAUSE);
}
} else if (_pause_mode == PM_UNPAUSED && _tick_skip_counter == 0) {
} else if (_pause_mode == PM_UNPAUSED && TickSkipCounter() == 0) {
if (DayLengthFactor() == 1) {
if (_date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK - 2) return;
if (_date.base() % _settings_game.linkgraph.recalc_interval != (_settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY) / 2) return;
if (EconTime::CurDateFract() != LinkGraphSchedule::SPAWN_JOIN_TICK - 2) return;
if (EconTime::CurDate().base() % _settings_game.linkgraph.recalc_interval != (_settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY) / 2) return;
} else {
int date_ticks = (NowDateTicks() - (LinkGraphSchedule::SPAWN_JOIN_TICK - 2)).base();
int date_ticks = (EconTime::CurDateTicks() - (LinkGraphSchedule::SPAWN_JOIN_TICK - 2)).base();
int interval = std::max<int>(2, (_settings_game.linkgraph.recalc_interval * DAY_TICKS / (SECONDS_PER_DAY * DayLengthFactor())));
if (date_ticks % interval != interval / 2) return;
}
@@ -356,12 +356,12 @@ void OnTick_LinkGraph()
int offset;
int interval;
if (DayLengthFactor() == 1) {
if (_date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK) return;
if (EconTime::CurDateFract() != LinkGraphSchedule::SPAWN_JOIN_TICK) return;
interval = _settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY;
offset = _date.base() % interval;
offset = EconTime::CurDate().base() % interval;
} else {
interval = std::max<int>(2, (_settings_game.linkgraph.recalc_interval * DAY_TICKS / (SECONDS_PER_DAY * DayLengthFactor())));
offset = (NowDateTicks() - LinkGraphSchedule::SPAWN_JOIN_TICK).base() % interval;
offset = (EconTime::CurDateTicks() - LinkGraphSchedule::SPAWN_JOIN_TICK).base() % interval;
}
if (offset == 0) {
LinkGraphSchedule::instance.SpawnNext();