Strong typedef: Use strong typedefs for date, date tick, minutes types

Add delta types
Adjust/add type conversion functions
Add various utility methods on types
Remove the various minute macros
Fix some minute conversion inconsistencies
This commit is contained in:
Jonathan G Rennison
2023-12-19 01:03:18 +00:00
parent 1e7b56e13a
commit 03e0ec8276
80 changed files with 538 additions and 431 deletions

View File

@@ -53,7 +53,7 @@ void FlowMapper::Run(LinkGraphJob &job) const
/* Scale by time the graph has been running without being compressed. Add 1 to avoid
* division by 0 if spawn date == last compression date. This matches
* LinkGraph::Monthly(). */
uint runtime = (uint)Clamp<DateTicksScaled>(DateTicksToScaledDateTicks(job.StartDateTicks()) - job.LastCompression() + 1, 1, UINT32_MAX);
uint runtime = (uint)Clamp<DateTicksScaledDelta>(DateTicksToScaledDateTicks(job.StartDateTicks()) - job.LastCompression() + 1, 1, UINT32_MAX).base();
for (auto &it : flows) {
it.ScaleToMonthly(runtime);
}

View File

@@ -38,7 +38,7 @@ inline void LinkGraph::BaseNode::Init(TileIndex xy, StationID st, uint demand)
* This is useful if the date has been modified with the cheat menu.
* @param interval Number of days to be added or subtracted.
*/
void LinkGraph::ShiftDates(int interval)
void LinkGraph::ShiftDates(DateDelta interval)
{
for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
BaseNode &source = this->nodes[node1];
@@ -54,7 +54,7 @@ void LinkGraph::ShiftDates(int interval)
void LinkGraph::Compress()
{
this->last_compression = (_scaled_date_ticks + this->last_compression) / 2;
this->last_compression = (_scaled_date_ticks.base() + this->last_compression.base()) / 2;
for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
this->nodes[node1].supply /= 2;
}
@@ -79,8 +79,8 @@ void LinkGraph::Compress()
*/
void LinkGraph::Merge(LinkGraph *other)
{
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));
uint32 age = ClampTo<uint32>(CeilDivT<int64>(_scaled_date_ticks.base() - this->last_compression.base() + 1, DAY_TICKS));
uint32 other_age = ClampTo<uint32>(CeilDivT<int64>(_scaled_date_ticks.base() - other->last_compression.base() + 1, DAY_TICKS));
NodeID first = this->Size();
this->nodes.reserve(first + other->Size());
for (NodeID node1 = 0; node1 < other->Size(); ++node1) {
@@ -266,7 +266,7 @@ void LinkGraph::Init(uint size)
this->nodes.resize(size);
}
void AdjustLinkGraphScaledTickBase(int64 delta)
void AdjustLinkGraphScaledTickBase(DateTicksScaledDelta delta)
{
for (LinkGraph *lg : LinkGraph::Iterate()) lg->last_compression += delta;
@@ -279,10 +279,10 @@ void AdjustLinkGraphScaledTickBase(int64 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);
for (LinkGraph *lg : LinkGraph::Iterate()) lg->last_compression = DateToScaledDateTicks((Date)lg->last_compression.base());
for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
LinkGraph *lg = &(const_cast<LinkGraph &>(lgj->Graph()));
lg->last_compression = DateToScaledDateTicks((Date)lg->last_compression);
lg->last_compression = DateToScaledDateTicks((Date)lg->last_compression.base());
}
}

View File

@@ -287,10 +287,10 @@ public:
static const uint MIN_TIMEOUT_DISTANCE = 32;
/** Number of days before deleting links served only by vehicles stopped in depot. */
static const uint STALE_LINK_DEPOT_TIMEOUT = 1024;
static constexpr DateDelta STALE_LINK_DEPOT_TIMEOUT = 1024;
/** Minimum number of days between subsequent compressions of a LG. */
static const uint COMPRESSION_INTERVAL = 256 * DAY_TICKS;
/** Minimum number of ticks between subsequent compressions of a LG. */
static constexpr DateTicksScaledDelta COMPRESSION_INTERVAL = 256 * DAY_TICKS;
/**
* Scale a value from a link graph of age orig_age for usage in one of age
@@ -314,7 +314,7 @@ public:
LinkGraph(CargoID cargo) : cargo(cargo), last_compression(_scaled_date_ticks) {}
void Init(uint size);
void ShiftDates(int interval);
void ShiftDates(DateDelta interval);
void Compress();
void Merge(LinkGraph *other);
@@ -365,7 +365,7 @@ public:
*/
inline uint Monthly(uint base) const
{
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);
return (static_cast<uint64>(base) * 30 * DAY_TICKS * _settings_game.economy.day_length_factor) / std::max<uint64>((_scaled_date_ticks - this->last_compression).base(), DAY_TICKS);
}
NodeID AddNode(const Station *st);
@@ -392,7 +392,7 @@ protected:
friend upstream_sl::SlLinkgraphNode;
friend upstream_sl::SlLinkgraphEdge;
friend void AdjustLinkGraphScaledTickBase(int64 delta);
friend void AdjustLinkGraphScaledTickBase(DateTicksScaledDelta delta);
friend void LinkGraphFixupLastCompressionAfterLoad();
CargoID cargo; ///< Cargo of this component's link graph.

View File

@@ -28,8 +28,8 @@ INSTANTIATE_POOL_METHODS(LinkGraphJob)
static DateTicks GetLinkGraphJobJoinDateTicks(uint duration_multiplier)
{
DateTicks ticks = (_settings_game.linkgraph.recalc_time * DAY_TICKS * duration_multiplier) / (SECONDS_PER_DAY * _settings_game.economy.day_length_factor);
return ticks + (_date * DAY_TICKS) + _date_fract;
DateTicksDelta ticks = (_settings_game.linkgraph.recalc_time * DAY_TICKS * duration_multiplier) / (SECONDS_PER_DAY * _settings_game.economy.day_length_factor);
return ticks + NowDateTicks();
}
/**
@@ -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((_date * DAY_TICKS) + _date_fract),
start_date_ticks(NowDateTicks()),
job_completed(false),
job_aborted(false)
{

View File

@@ -262,7 +262,7 @@ public:
* settings have to be brutally const-casted in order to populate them.
*/
LinkGraphJob() : settings(_settings_game.linkgraph),
join_date_ticks(INVALID_DATE), start_date_ticks(INVALID_DATE), job_completed(false), job_aborted(false) {}
join_date_ticks(INVALID_DATE_TICKS), start_date_ticks(INVALID_DATE_TICKS), job_completed(false), job_aborted(false) {}
LinkGraphJob(const LinkGraph &orig, uint duration_multiplier);
~LinkGraphJob();
@@ -297,7 +297,7 @@ 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 <= (_date * DAY_TICKS) + _date_fract + tick_offset; }
inline bool IsScheduledToBeJoined(int tick_offset = 0) const { return this->join_date_ticks <= NowDateTicks() + tick_offset; }
/**
* Get the date when the job should be finished.
@@ -315,10 +315,10 @@ public:
* Change the start and join dates on date cheating.
* @param interval Number of days to add.
*/
inline void ShiftJoinDate(int interval)
inline void ShiftJoinDate(DateDelta interval)
{
this->join_date_ticks += interval * DAY_TICKS;
this->start_date_ticks += interval * DAY_TICKS;
this->join_date_ticks += DateDeltaToDateTicksDelta(interval);
this->start_date_ticks += DateDeltaToDateTicksDelta(interval);
}
/**

View File

@@ -200,7 +200,7 @@ void LinkGraphSchedule::SpawnAll()
* graph jobs by the number of days given.
* @param interval Number of days to be added or subtracted.
*/
void LinkGraphSchedule::ShiftDates(int interval)
void LinkGraphSchedule::ShiftDates(DateDelta interval)
{
for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(interval);
for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) lgj->ShiftJoinDate(interval);
@@ -285,7 +285,7 @@ void LinkGraphJobGroup::JoinThread()
auto flush_bucket = [&]() {
if (!bucket_cost) return;
DEBUG(linkgraph, 2, "LinkGraphJobGroup::ExecuteJobSet: Creating Job Group: jobs: " PRINTF_SIZE ", cost: %u, join after: %d",
bucket.size(), bucket_cost, bucket_join_date - ((_date * DAY_TICKS) + _date_fract));
bucket.size(), bucket_cost, (bucket_join_date - NowDateTicks()).base());
auto group = std::make_shared<LinkGraphJobGroup>(constructor_token(), std::move(bucket));
group->SpawnThread();
bucket_cost = 0;
@@ -321,9 +321,9 @@ void StateGameLoop_LinkGraphPauseControl()
} else if (_pause_mode == PM_UNPAUSED && _tick_skip_counter == 0) {
if (_settings_game.economy.day_length_factor == 1) {
if (_date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK - 2) return;
if (_date % _settings_game.linkgraph.recalc_interval != (_settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY) / 2) return;
if (_date.base() % _settings_game.linkgraph.recalc_interval != (_settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY) / 2) return;
} else {
int date_ticks = ((_date * DAY_TICKS) + _date_fract - (LinkGraphSchedule::SPAWN_JOIN_TICK - 2));
int date_ticks = (NowDateTicks() - (LinkGraphSchedule::SPAWN_JOIN_TICK - 2)).base();
int interval = std::max<int>(2, (_settings_game.linkgraph.recalc_interval * DAY_TICKS / (SECONDS_PER_DAY * _settings_game.economy.day_length_factor)));
if (date_ticks % interval != interval / 2) return;
}
@@ -358,10 +358,10 @@ void OnTick_LinkGraph()
if (_settings_game.economy.day_length_factor == 1) {
if (_date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK) return;
interval = _settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY;
offset = _date % interval;
offset = _date.base() % interval;
} else {
interval = std::max<int>(2, (_settings_game.linkgraph.recalc_interval * DAY_TICKS / (SECONDS_PER_DAY * _settings_game.economy.day_length_factor)));
offset = ((_date * DAY_TICKS) + _date_fract - LinkGraphSchedule::SPAWN_JOIN_TICK) % interval;
offset = (NowDateTicks() - LinkGraphSchedule::SPAWN_JOIN_TICK).base() % interval;
}
if (offset == 0) {
LinkGraphSchedule::instance.SpawnNext();

View File

@@ -66,7 +66,7 @@ public:
bool IsJoinWithUnfinishedJobDue() const;
void JoinNext();
void SpawnAll();
void ShiftDates(int interval);
void ShiftDates(DateDelta interval);
/**
* Queue a link graph for execution.