Changeset - r27786:8a6a45279e7a
[Not reviewed]
master
0 8 0
Patric Stout - 10 months ago 2023-08-12 14:02:38
truebrain@openttd.org
Codechange: use TimerGameCalendar::Date for variables in linkgraph that are dates (#11187)
8 files changed with 16 insertions and 17 deletions:
0 comments (0 inline, 0 general)
src/linkgraph/linkgraph.cpp
Show inline comments
 
@@ -41,25 +41,25 @@ LinkGraph::BaseEdge::BaseEdge(NodeID des
 
	this->usage = 0;
 
	this->travel_time_sum = 0;
 
	this->last_unrestricted_update = INVALID_DATE;
 
	this->last_restricted_update = INVALID_DATE;
 
	this->dest_node = dest_node;
 
}
 

	
 
/**
 
 * Shift all dates by given interval.
 
 * 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) {
 
		BaseNode &source = this->nodes[node1];
 
		if (source.last_update != INVALID_DATE) source.last_update += interval;
 
		for (BaseEdge &edge : this->nodes[node1].edges) {
 
			if (edge.last_unrestricted_update != INVALID_DATE) edge.last_unrestricted_update += interval;
 
			if (edge.last_restricted_update != INVALID_DATE) edge.last_restricted_update += interval;
 
		}
 
	}
 
}
 

	
src/linkgraph/linkgraph.h
Show inline comments
 
@@ -162,52 +162,52 @@ public:
 
		std::vector<BaseEdge>::const_iterator GetEdge(NodeID dest) const
 
		{
 
			return std::lower_bound(this->edges.begin(), this->edges.end(), dest);
 
		}
 
	};
 

	
 
	typedef std::vector<BaseNode> NodeVector;
 

	
 
	/** Minimum effective distance for timeout calculation. */
 
	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
 
	 * target_age. Make sure that the value stays > 0 if it was > 0 before.
 
	 * @param val Value to be scaled.
 
	 * @param target_age Age of the target link graph.
 
	 * @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;
 
	}
 

	
 
	/** Bare constructor, only for save/load. */
 
	LinkGraph() : cargo(INVALID_CARGO), last_compression(0) {}
 
	/**
 
	 * Real constructor.
 
	 * @param cargo Cargo the link graph is about.
 
	 */
 
	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);
 

	
 
	/* Splitting link graphs is intentionally not implemented.
 
	 * The overhead in determining connectedness would probably outweigh the
 
	 * benefit of having to deal with smaller graphs. In real world examples
 
	 * networks generally grow. Only rarely a network is permanently split.
 
	 * Reacting to temporary splits here would obviously create performance
 
	 * problems and detecting the temporary or permanent nature of splits isn't
 
	 * trivial. */
 

	
 
	/**
src/linkgraph/linkgraphjob.h
Show inline comments
 
@@ -214,25 +214,25 @@ public:
 
	inline bool IsScheduledToBeJoined() const { return this->join_date <= TimerGameCalendar::date; }
 

	
 
	/**
 
	 * Get the date when the job should be finished.
 
	 * @return Join date.
 
	 */
 
	inline TimerGameCalendar::Date JoinDate() const { return join_date; }
 

	
 
	/**
 
	 * 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.
 
	 * @return Settings.
 
	 */
 
	inline const LinkGraphSettings &Settings() const { return this->settings; }
 

	
 
	/**
 
	 * Get a node abstraction with the specified id.
 
	 * @param num ID of the node.
 
	 * @return the Requested node.
 
	 */
src/linkgraph/linkgraphschedule.cpp
Show inline comments
 
@@ -123,25 +123,25 @@ void LinkGraphSchedule::SpawnAll()
 
	for (auto &it : instance.running) {
 
		it->AbortJob();
 
	}
 
	instance.running.clear();
 
	instance.schedule.clear();
 
}
 

	
 
/**
 
 * Shift all dates (join dates and edge annotations) of link graphs and link
 
 * 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);
 
}
 

	
 
/**
 
 * Create a link graph schedule and initialize its handlers.
 
 */
 
LinkGraphSchedule::LinkGraphSchedule()
 
{
 
	this->handlers[0] = new InitHandler;
 
	this->handlers[1] = new DemandHandler;
src/linkgraph/linkgraphschedule.h
Show inline comments
 
@@ -49,25 +49,25 @@ protected:
 
public:
 
	/* This is a tick where not much else is happening, so a small lag might go unnoticed. */
 
	static const uint SPAWN_JOIN_TICK = 21; ///< Tick when jobs are spawned or joined every day.
 
	static LinkGraphSchedule instance;
 

	
 
	static void Run(LinkGraphJob *job);
 
	static void Clear();
 

	
 
	void SpawnNext();
 
	bool IsJoinWithUnfinishedJobDue() const;
 
	void JoinNext();
 
	void SpawnAll();
 
	void ShiftDates(int interval);
 
	void ShiftDates(TimerGameCalendar::Date interval);
 

	
 
	/**
 
	 * Queue a link graph for execution.
 
	 * @param lg Link graph to be queued.
 
	 */
 
	void Queue(LinkGraph *lg)
 
	{
 
		assert(LinkGraph::Get(lg->index) == lg);
 
		this->schedule.push_back(lg);
 
	}
 

	
 
	/**
src/station_cmd.cpp
Show inline comments
 
@@ -3797,26 +3797,26 @@ void RerouteCargo(Station *st, CargoID c
 
void DeleteStaleLinks(Station *from)
 
{
 
	for (CargoID c = 0; c < NUM_CARGO; ++c) {
 
		const bool auto_distributed = (_settings_game.linkgraph.GetDistributionType(c) != DT_MANUAL);
 
		GoodsEntry &ge = from->goods[c];
 
		LinkGraph *lg = LinkGraph::GetIfValid(ge.link_graph);
 
		if (lg == nullptr) continue;
 
		std::vector<NodeID> to_remove{};
 
		for (Edge &edge : (*lg)[ge.node].edges) {
 
			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) {
 
					/* Have all vehicles refresh their next hops before deciding to
 
					 * remove the node. */
 
					std::vector<Vehicle *> vehicles;
 
					for (OrderList *l : OrderList::Iterate()) {
 
						bool found_from = false;
 
						bool found_to = false;
 
						for (Order *order = l->GetFirstOrder(); order != nullptr; order = order->next) {
 
							if (!order->IsType(OT_GOTO_STATION) && !order->IsType(OT_IMPLICIT)) continue;
 
							if (order->GetDestination() == from->index) {
 
@@ -3826,26 +3826,25 @@ void DeleteStaleLinks(Station *from)
 
								found_to = true;
 
								if (found_from) break;
 
							}
 
						}
 
						if (!found_to || !found_from) continue;
 
						vehicles.push_back(l->GetFirstSharedVehicle());
 
					}
 

	
 
					auto iter = vehicles.begin();
 
					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<uint>(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) {
 
							updated = true;
 
							break;
 
						}
 

	
 
						Vehicle *next_shared = v->NextShared();
 
						if (next_shared) {
 
							*iter = next_shared;
 
							++iter;
 
						} else {
 
@@ -3853,37 +3852,37 @@ void DeleteStaleLinks(Station *from)
 
						}
 

	
 
						if (iter == vehicles.end()) iter = vehicles.begin();
 
					}
 
				}
 

	
 
				if (!updated) {
 
					/* If it's still considered dead remove it. */
 
					to_remove.emplace_back(to->goods[c].node);
 
					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();
 
			}
 
		}
 
		/* Remove dead edges. */
 
		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();
 
		}
 
	}
 
}
 

	
 
/**
 
 * Increase capacity for a link stat given by station cargo and next hop.
 
 * @param st Station to get the link stats from.
 
 * @param cargo Cargo to increase stat for.
 
 * @param next_station_id Station the consist will be travelling to next.
 
 * @param capacity Capacity to add to link stat.
 
 * @param usage Usage to add to link stat.
src/vehicle.cpp
Show inline comments
 
@@ -750,25 +750,25 @@ const GRFFile *Vehicle::GetGRF() const
 
 * @return GRF ID of the associated NewGRF.
 
 */
 
uint32_t Vehicle::GetGRFID() const
 
{
 
	return this->GetEngine()->GetGRFID();
 
}
 

	
 
/**
 
 * Shift all dates by given interval.
 
 * 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
 
	 * for vehicles outside of a depot. */
 
}
 

	
 
/**
 
 * Handle the pathfinding result, especially the lost status.
 
 * If the vehicle is now lost and wasn't previously fire an
 
 * event to the AIs and a news message to the user. If the
 
 * vehicle is not lost anymore remove the news message.
 
 * @param path_found Whether the vehicle has a path to its destination.
src/vehicle_base.h
Show inline comments
 
@@ -566,25 +566,25 @@ public:
 

	
 
	/**
 
	 * Calls the tick handler of the vehicle
 
	 * @return is this vehicle still valid?
 
	 */
 
	virtual bool Tick() { return true; };
 

	
 
	/**
 
	 * Calls the new day handler of the vehicle
 
	 */
 
	virtual void OnNewDay() {};
 

	
 
	void ShiftDates(int interval);
 
	void ShiftDates(TimerGameCalendar::Date interval);
 

	
 
	/**
 
	 * Crash the (whole) vehicle chain.
 
	 * @param flooded whether the cause of the crash is flooding or not.
 
	 * @return the number of lost souls.
 
	 */
 
	virtual uint Crash(bool flooded = false);
 

	
 
	/**
 
	 * Returns the Trackdir on which the vehicle is currently located.
 
	 * Works for trains and ships.
 
	 * Currently works only sortof for road vehicles, since they have a fuzzy
0 comments (0 inline, 0 general)