2013-06-09 12:57:41 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file linkgraphjob.cpp Definition of link graph job classes used for cargo distribution. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../core/pool_func.hpp"
|
|
|
|
#include "../window_func.h"
|
|
|
|
#include "linkgraphjob.h"
|
2014-02-16 18:42:59 +00:00
|
|
|
#include "linkgraphschedule.h"
|
2013-06-09 12:57:41 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2013-06-09 12:57:41 +00:00
|
|
|
/* Initialize the link-graph-job-pool */
|
|
|
|
LinkGraphJobPool _link_graph_job_pool("LinkGraphJob");
|
|
|
|
INSTANTIATE_POOL_METHODS(LinkGraphJob)
|
|
|
|
|
2015-03-07 18:27:01 +00:00
|
|
|
/**
|
|
|
|
* Static instance of an invalid path.
|
|
|
|
* Note: This instance is created on task start.
|
|
|
|
* Lazy creation on first usage results in a data race between the CDist threads.
|
|
|
|
*/
|
|
|
|
/* static */ Path *Path::invalid_path = new Path(INVALID_NODE, true);
|
|
|
|
|
2013-06-09 12:57:41 +00:00
|
|
|
/**
|
|
|
|
* Create a link graph job from a link graph. The link graph will be copied so
|
|
|
|
* that the calculations don't interfer with the normal operations on the
|
|
|
|
* original. The job is immediately started.
|
|
|
|
* @param orig Original LinkGraph to be copied.
|
|
|
|
*/
|
|
|
|
LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
|
|
|
|
/* Copying the link graph here also copies its index member.
|
|
|
|
* This is on purpose. */
|
|
|
|
link_graph(orig),
|
|
|
|
settings(_settings_game.linkgraph),
|
2019-01-22 18:31:08 +00:00
|
|
|
join_date(_date + _settings_game.linkgraph.recalc_time),
|
2020-12-24 23:36:36 +00:00
|
|
|
job_completed(false),
|
|
|
|
job_aborted(false)
|
2013-06-09 12:57:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-29 19:55:29 +00:00
|
|
|
/**
|
|
|
|
* Erase all flows originating at a specific node.
|
|
|
|
* @param from Node to erase flows for.
|
|
|
|
*/
|
|
|
|
void LinkGraphJob::EraseFlows(NodeID from)
|
|
|
|
{
|
|
|
|
for (NodeID node_id = 0; node_id < this->Size(); ++node_id) {
|
|
|
|
(*this)[node_id].Flows().erase(from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 18:42:59 +00:00
|
|
|
/**
|
|
|
|
* Spawn a thread if possible and run the link graph job in the thread. If
|
|
|
|
* that's not possible run the job right now in the current thread.
|
|
|
|
*/
|
|
|
|
void LinkGraphJob::SpawnThread()
|
|
|
|
{
|
2019-03-17 00:59:46 +00:00
|
|
|
if (!StartNewThread(&this->thread, "ottd:linkgraph", &(LinkGraphSchedule::Run), this)) {
|
2014-02-16 18:42:59 +00:00
|
|
|
/* Of course this will hang a bit.
|
2019-09-29 20:27:32 +00:00
|
|
|
* On the other hand, if you want to play games which make this hang noticeably
|
2014-02-16 18:42:59 +00:00
|
|
|
* on a platform without threads then you'll probably get other problems first.
|
|
|
|
* OK:
|
2021-05-09 17:02:17 +00:00
|
|
|
* If someone comes and tells me that this hangs for them, I'll implement a
|
2014-02-16 18:42:59 +00:00
|
|
|
* smaller grained "Step" method for all handlers and add some more ticks where
|
|
|
|
* "Step" is called. No problem in principle. */
|
|
|
|
LinkGraphSchedule::Run(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join the calling thread with this job's thread if threading is enabled.
|
|
|
|
*/
|
|
|
|
void LinkGraphJob::JoinThread()
|
|
|
|
{
|
2019-03-17 00:59:46 +00:00
|
|
|
if (this->thread.joinable()) {
|
|
|
|
this->thread.join();
|
2014-02-16 18:42:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-09 12:57:41 +00:00
|
|
|
/**
|
|
|
|
* Join the link graph job and destroy it.
|
|
|
|
*/
|
|
|
|
LinkGraphJob::~LinkGraphJob()
|
|
|
|
{
|
2014-02-16 18:42:59 +00:00
|
|
|
this->JoinThread();
|
2013-06-09 12:57:41 +00:00
|
|
|
|
2013-10-31 20:31:10 +00:00
|
|
|
/* Don't update stuff from other pools, when everything is being removed.
|
|
|
|
* Accessing other pools may be invalid. */
|
|
|
|
if (CleaningPool()) return;
|
|
|
|
|
2020-12-24 23:36:36 +00:00
|
|
|
/* If the job has been aborted, the job state is invalid.
|
|
|
|
* This should never be reached, as once the job has been marked as aborted
|
|
|
|
* the only valid job operation is to clear the LinkGraphJob pool. */
|
|
|
|
assert(!this->IsJobAborted());
|
|
|
|
|
2013-06-09 12:57:41 +00:00
|
|
|
/* Link graph has been merged into another one. */
|
|
|
|
if (!LinkGraph::IsValidID(this->link_graph.index)) return;
|
|
|
|
|
2021-05-10 21:43:52 +00:00
|
|
|
uint16 size = this->Size();
|
2013-06-09 12:57:41 +00:00
|
|
|
for (NodeID node_id = 0; node_id < size; ++node_id) {
|
|
|
|
Node from = (*this)[node_id];
|
|
|
|
|
2014-01-29 19:55:29 +00:00
|
|
|
/* The station can have been deleted. Remove all flows originating from it then. */
|
2013-06-09 12:57:41 +00:00
|
|
|
Station *st = Station::GetIfValid(from.Station());
|
2019-04-10 21:07:06 +00:00
|
|
|
if (st == nullptr) {
|
2014-01-29 19:55:29 +00:00
|
|
|
this->EraseFlows(node_id);
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-09 12:57:41 +00:00
|
|
|
|
|
|
|
/* Link graph merging and station deletion may change around IDs. Make
|
|
|
|
* sure that everything is still consistent or ignore it otherwise. */
|
|
|
|
GoodsEntry &ge = st->goods[this->Cargo()];
|
2014-01-29 19:55:29 +00:00
|
|
|
if (ge.link_graph != this->link_graph.index || ge.node != node_id) {
|
|
|
|
this->EraseFlows(node_id);
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-09 12:57:41 +00:00
|
|
|
|
|
|
|
LinkGraph *lg = LinkGraph::Get(ge.link_graph);
|
|
|
|
FlowStatMap &flows = from.Flows();
|
|
|
|
|
|
|
|
for (EdgeIterator it(from.Begin()); it != from.End(); ++it) {
|
2023-01-02 20:46:49 +00:00
|
|
|
if (it->second.Flow() == 0) continue;
|
2013-06-09 12:57:41 +00:00
|
|
|
StationID to = (*this)[it->first].Station();
|
|
|
|
Station *st2 = Station::GetIfValid(to);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (st2 == nullptr || st2->goods[this->Cargo()].link_graph != this->link_graph.index ||
|
2013-06-09 12:57:41 +00:00
|
|
|
st2->goods[this->Cargo()].node != it->first ||
|
2023-01-02 20:46:49 +00:00
|
|
|
!(*lg)[node_id].HasEdgeTo(it->first) ||
|
2013-06-09 12:57:41 +00:00
|
|
|
(*lg)[node_id][it->first].LastUpdate() == INVALID_DATE) {
|
|
|
|
/* Edge has been removed. Delete flows. */
|
2013-11-10 15:18:49 +00:00
|
|
|
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()) ge.flows.erase(erased.Pop());
|
2013-10-22 16:13:54 +00:00
|
|
|
} else if ((*lg)[node_id][it->first].LastUnrestrictedUpdate() == INVALID_DATE) {
|
|
|
|
/* Edge is fully restricted. */
|
|
|
|
flows.RestrictFlows(to);
|
2013-06-09 12:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-17 20:38:11 +00:00
|
|
|
/* Swap shares and invalidate ones that are completely deleted. Don't
|
|
|
|
* really delete them as we could then end up with unroutable cargo
|
2014-02-16 11:17:17 +00:00
|
|
|
* somewhere. Do delete them and also reroute relevant cargo if
|
|
|
|
* automatic distribution has been turned off for that cargo. */
|
2013-07-30 19:03:56 +00:00
|
|
|
for (FlowStatMap::iterator it(ge.flows.begin()); it != ge.flows.end();) {
|
2013-06-17 20:38:11 +00:00
|
|
|
FlowStatMap::iterator new_it = flows.find(it->first);
|
|
|
|
if (new_it == flows.end()) {
|
2013-07-30 19:03:56 +00:00
|
|
|
if (_settings_game.linkgraph.GetDistributionType(this->Cargo()) != DT_MANUAL) {
|
|
|
|
it->second.Invalidate();
|
|
|
|
++it;
|
|
|
|
} else {
|
2014-02-16 11:17:17 +00:00
|
|
|
FlowStat shares(INVALID_STATION, 1);
|
|
|
|
it->second.SwapShares(shares);
|
2013-07-30 19:03:56 +00:00
|
|
|
ge.flows.erase(it++);
|
2014-02-16 11:17:17 +00:00
|
|
|
for (FlowStat::SharesMap::const_iterator shares_it(shares.GetShares()->begin());
|
|
|
|
shares_it != shares.GetShares()->end(); ++shares_it) {
|
|
|
|
RerouteCargo(st, this->Cargo(), shares_it->second, st->index);
|
|
|
|
}
|
2013-07-30 19:03:56 +00:00
|
|
|
}
|
2013-06-17 20:38:11 +00:00
|
|
|
} else {
|
|
|
|
it->second.SwapShares(new_it->second);
|
|
|
|
flows.erase(new_it);
|
2013-07-30 19:03:56 +00:00
|
|
|
++it;
|
2013-06-17 20:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ge.flows.insert(flows.begin(), flows.end());
|
2013-06-09 12:57:41 +00:00
|
|
|
InvalidateWindowData(WC_STATION_VIEW, st->index, this->Cargo());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the link graph job: Resize nodes and edges and populate them.
|
|
|
|
* This is done after the constructor so that we can do it in the calculation
|
|
|
|
* thread without delaying the main game.
|
|
|
|
*/
|
|
|
|
void LinkGraphJob::Init()
|
|
|
|
{
|
|
|
|
uint size = this->Size();
|
2023-01-01 16:12:56 +00:00
|
|
|
this->nodes.reserve(size);
|
2013-06-09 12:57:41 +00:00
|
|
|
for (uint i = 0; i < size; ++i) {
|
2023-01-02 00:06:31 +00:00
|
|
|
this->nodes.emplace_back(this->link_graph.nodes[i], this->link_graph.Size());
|
2013-06-09 12:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add this path as a new child to the given base path, thus making this path
|
|
|
|
* a "fork" of the base path.
|
|
|
|
* @param base Path to fork from.
|
|
|
|
* @param cap Maximum capacity of the new leg.
|
|
|
|
* @param free_cap Remaining free capacity of the new leg.
|
|
|
|
* @param dist Distance of the new leg.
|
|
|
|
*/
|
|
|
|
void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
|
|
|
|
{
|
2021-01-08 10:16:18 +00:00
|
|
|
this->capacity = std::min(base->capacity, cap);
|
|
|
|
this->free_capacity = std::min(base->free_capacity, free_cap);
|
2013-06-09 12:57:41 +00:00
|
|
|
this->distance = base->distance + dist;
|
|
|
|
assert(this->distance > 0);
|
|
|
|
if (this->parent != base) {
|
|
|
|
this->Detach();
|
|
|
|
this->parent = base;
|
|
|
|
this->parent->num_children++;
|
|
|
|
}
|
|
|
|
this->origin = base->origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push some flow along a path and register the path in the nodes it passes if
|
|
|
|
* successful.
|
|
|
|
* @param new_flow Amount of flow to push.
|
|
|
|
* @param job Link graph job this node belongs to.
|
|
|
|
* @param max_saturation Maximum saturation of edges.
|
|
|
|
* @return Amount of flow actually pushed.
|
|
|
|
*/
|
|
|
|
uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->parent != nullptr) {
|
2013-06-09 12:57:41 +00:00
|
|
|
LinkGraphJob::Edge edge = job[this->parent->node][this->node];
|
|
|
|
if (max_saturation != UINT_MAX) {
|
|
|
|
uint usable_cap = edge.Capacity() * max_saturation / 100;
|
|
|
|
if (usable_cap > edge.Flow()) {
|
2021-01-08 10:16:18 +00:00
|
|
|
new_flow = std::min(new_flow, usable_cap - edge.Flow());
|
2013-06-09 12:57:41 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
|
|
|
|
if (this->flow == 0 && new_flow > 0) {
|
2013-10-19 17:15:19 +00:00
|
|
|
job[this->parent->node].Paths().push_front(this);
|
2013-06-09 12:57:41 +00:00
|
|
|
}
|
|
|
|
edge.AddFlow(new_flow);
|
|
|
|
}
|
|
|
|
this->flow += new_flow;
|
|
|
|
return new_flow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a leg of a path in the link graph.
|
|
|
|
* @param n Id of the link graph node this path passes.
|
|
|
|
* @param source If true, this is the first leg of the path.
|
|
|
|
*/
|
|
|
|
Path::Path(NodeID n, bool source) :
|
|
|
|
distance(source ? 0 : UINT_MAX),
|
2013-06-17 20:37:07 +00:00
|
|
|
capacity(source ? UINT_MAX : 0),
|
2013-06-09 12:57:41 +00:00
|
|
|
free_capacity(source ? INT_MAX : INT_MIN),
|
|
|
|
flow(0), node(n), origin(source ? n : INVALID_NODE),
|
2019-04-10 21:07:06 +00:00
|
|
|
num_children(0), parent(nullptr)
|
2013-06-09 12:57:41 +00:00
|
|
|
{}
|
|
|
|
|