From a9ed590ca71cc133607762dac2071e30496d2b09 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sat, 12 Aug 2023 16:02:38 +0200 Subject: [PATCH] Codechange: use TimerGameCalendar::Date for variables in linkgraph that are dates (#11187) --- src/linkgraph/linkgraph.cpp | 2 +- src/linkgraph/linkgraph.h | 8 ++++---- src/linkgraph/linkgraphjob.h | 2 +- src/linkgraph/linkgraphschedule.cpp | 2 +- src/linkgraph/linkgraphschedule.h | 2 +- src/station_cmd.cpp | 13 ++++++------- src/vehicle.cpp | 2 +- src/vehicle_base.h | 2 +- 8 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/linkgraph/linkgraph.cpp b/src/linkgraph/linkgraph.cpp index 015e167955..f0e0597189 100644 --- a/src/linkgraph/linkgraph.cpp +++ b/src/linkgraph/linkgraph.cpp @@ -50,7 +50,7 @@ LinkGraph::BaseEdge::BaseEdge(NodeID dest_node) * 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(TimerGameCalendar::Date interval) { this->last_compression += interval; for (NodeID node1 = 0; node1 < this->Size(); ++node1) { diff --git a/src/linkgraph/linkgraph.h b/src/linkgraph/linkgraph.h index 106af79ab1..59bbcb758e 100644 --- a/src/linkgraph/linkgraph.h +++ b/src/linkgraph/linkgraph.h @@ -171,10 +171,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 TimerGameCalendar::Date STALE_LINK_DEPOT_TIMEOUT = 1024; /** Minimum number of days between subsequent compressions of a LG. */ - static const uint COMPRESSION_INTERVAL = 256; + static constexpr TimerGameCalendar::Date COMPRESSION_INTERVAL = 256; /** * Scale a value from a link graph of age orig_age for usage in one of age @@ -184,7 +184,7 @@ public: * @param orig_age Age of the original link graph. * @return scaled value. */ - inline static uint Scale(uint val, uint target_age, uint orig_age) + inline static uint Scale(uint val, TimerGameCalendar::Date target_age, TimerGameCalendar::Date orig_age) { return val > 0 ? std::max(1U, val * target_age / orig_age) : 0; } @@ -198,7 +198,7 @@ public: LinkGraph(CargoID cargo) : cargo(cargo), last_compression(TimerGameCalendar::date) {} void Init(uint size); - void ShiftDates(int interval); + void ShiftDates(TimerGameCalendar::Date interval); void Compress(); void Merge(LinkGraph *other); diff --git a/src/linkgraph/linkgraphjob.h b/src/linkgraph/linkgraphjob.h index 2c2e9a86cc..6ea6c70307 100644 --- a/src/linkgraph/linkgraphjob.h +++ b/src/linkgraph/linkgraphjob.h @@ -223,7 +223,7 @@ public: * Change the join date on date cheating. * @param interval Number of days to add. */ - inline void ShiftJoinDate(int interval) { this->join_date += interval; } + inline void ShiftJoinDate(TimerGameCalendar::Date interval) { this->join_date += interval; } /** * Get the link graph settings for this component. diff --git a/src/linkgraph/linkgraphschedule.cpp b/src/linkgraph/linkgraphschedule.cpp index 3c3a12f4fe..ffdd54e7f3 100644 --- a/src/linkgraph/linkgraphschedule.cpp +++ b/src/linkgraph/linkgraphschedule.cpp @@ -132,7 +132,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(TimerGameCalendar::Date interval) { for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(interval); for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) lgj->ShiftJoinDate(interval); diff --git a/src/linkgraph/linkgraphschedule.h b/src/linkgraph/linkgraphschedule.h index 1e76cda324..572db030a9 100644 --- a/src/linkgraph/linkgraphschedule.h +++ b/src/linkgraph/linkgraphschedule.h @@ -58,7 +58,7 @@ public: bool IsJoinWithUnfinishedJobDue() const; void JoinNext(); void SpawnAll(); - void ShiftDates(int interval); + void ShiftDates(TimerGameCalendar::Date interval); /** * Queue a link graph for execution. diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 10ab53f652..7aa176e49e 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -3806,8 +3806,8 @@ void DeleteStaleLinks(Station *from) Station *to = Station::Get((*lg)[edge.dest_node].station); assert(to->goods[c].node == edge.dest_node); assert(TimerGameCalendar::date >= edge.LastUpdate()); - uint timeout = LinkGraph::MIN_TIMEOUT_DISTANCE + (DistanceManhattan(from->xy, to->xy) >> 3); - if ((uint)(TimerGameCalendar::date - edge.LastUpdate()) > timeout) { + auto timeout = TimerGameCalendar::Date(LinkGraph::MIN_TIMEOUT_DISTANCE + (DistanceManhattan(from->xy, to->xy) >> 3)); + if (TimerGameCalendar::date - edge.LastUpdate() > timeout) { bool updated = false; if (auto_distributed) { @@ -3835,8 +3835,7 @@ void DeleteStaleLinks(Station *from) while (iter != vehicles.end()) { Vehicle *v = *iter; /* Do not refresh links of vehicles that have been stopped in depot for a long time. */ - if (!v->IsStoppedInDepot() || static_cast(TimerGameCalendar::date - v->date_of_last_service) <= - LinkGraph::STALE_LINK_DEPOT_TIMEOUT) { + if (!v->IsStoppedInDepot() || TimerGameCalendar::date - v->date_of_last_service <= LinkGraph::STALE_LINK_DEPOT_TIMEOUT) { LinkRefresher::Run(v, false); // Don't allow merging. Otherwise lg might get deleted. } if (edge.LastUpdate() == TimerGameCalendar::date) { @@ -3862,11 +3861,11 @@ void DeleteStaleLinks(Station *from) ge.flows.DeleteFlows(to->index); RerouteCargo(from, c, to->index, from->index); } - } else if (edge.last_unrestricted_update != INVALID_DATE && (uint)(TimerGameCalendar::date - edge.last_unrestricted_update) > timeout) { + } else if (edge.last_unrestricted_update != INVALID_DATE && TimerGameCalendar::date - edge.last_unrestricted_update > timeout) { edge.Restrict(); ge.flows.RestrictFlows(to->index); RerouteCargo(from, c, to->index, from->index); - } else if (edge.last_restricted_update != INVALID_DATE && (uint)(TimerGameCalendar::date - edge.last_restricted_update) > timeout) { + } else if (edge.last_restricted_update != INVALID_DATE && TimerGameCalendar::date - edge.last_restricted_update > timeout) { edge.Release(); } } @@ -3874,7 +3873,7 @@ void DeleteStaleLinks(Station *from) for (NodeID r : to_remove) (*lg)[ge.node].RemoveEdge(r); assert(TimerGameCalendar::date >= lg->LastCompression()); - if ((uint)(TimerGameCalendar::date - lg->LastCompression()) > LinkGraph::COMPRESSION_INTERVAL) { + if (TimerGameCalendar::date - lg->LastCompression() > LinkGraph::COMPRESSION_INTERVAL) { lg->Compress(); } } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index f6e5219c39..a21e4ff052 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -759,7 +759,7 @@ uint32_t Vehicle::GetGRFID() const * This is useful if the date has been modified with the cheat menu. * @param interval Number of days to be added or substracted. */ -void Vehicle::ShiftDates(int interval) +void Vehicle::ShiftDates(TimerGameCalendar::Date interval) { this->date_of_last_service = std::max(this->date_of_last_service + interval, TimerGameCalendar::Date(0)); /* date_of_last_service_newgrf is not updated here as it must stay stable diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 5ceb992690..a9a32deb4d 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -575,7 +575,7 @@ public: */ virtual void OnNewDay() {}; - void ShiftDates(int interval); + void ShiftDates(TimerGameCalendar::Date interval); /** * Crash the (whole) vehicle chain.