Files
@ r28549:6bc33bc6ee38
Branch filter:
Location: cpp/openttd-patchpack/source/src/linkgraph/linkgraphjob.cpp - annotation
r28549:6bc33bc6ee38
8.7 KiB
text/x-c
Codechange: Split dates and timers into Economy and Calendar time (#10700)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r21259:acd95ff9be40 r20340:dd61643506ed r21383:942c32fb8b0e r21383:942c32fb8b0e r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r22021:daba722f3935 r22021:daba722f3935 r22021:daba722f3935 r22021:daba722f3935 r22021:daba722f3935 r22021:daba722f3935 r22021:daba722f3935 r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r28549:6bc33bc6ee38 r24489:93c2688c28bb r24489:93c2688c28bb r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r21199:0d94a08beeae r21199:0d94a08beeae r21199:0d94a08beeae r21199:0d94a08beeae r21199:0d94a08beeae r21199:0d94a08beeae r26921:1ce3943c7e3a r21199:0d94a08beeae r21199:0d94a08beeae r21199:0d94a08beeae r21199:0d94a08beeae r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r23587:821735a25dec r21259:acd95ff9be40 r23838:bfeaabaa7b1d r21259:acd95ff9be40 r21259:acd95ff9be40 r25484:c5af4712cb7f r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r23587:821735a25dec r23587:821735a25dec r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r21259:acd95ff9be40 r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r21259:acd95ff9be40 r20340:dd61643506ed r20890:ee633efd4240 r20890:ee633efd4240 r20890:ee633efd4240 r20890:ee633efd4240 r24489:93c2688c28bb r24489:93c2688c28bb r24489:93c2688c28bb r24489:93c2688c28bb r24489:93c2688c28bb r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r27737:728d55b97775 r20340:dd61643506ed r26921:1ce3943c7e3a r20340:dd61643506ed r21199:0d94a08beeae r26921:1ce3943c7e3a r23607:36c15679007d r21199:0d94a08beeae r21199:0d94a08beeae r21199:0d94a08beeae r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r21199:0d94a08beeae r21199:0d94a08beeae r21199:0d94a08beeae r21199:0d94a08beeae r20340:dd61643506ed r20340:dd61643506ed r26921:1ce3943c7e3a r20340:dd61643506ed r26921:1ce3943c7e3a r26921:1ce3943c7e3a r26921:1ce3943c7e3a r26921:1ce3943c7e3a r20340:dd61643506ed r23607:36c15679007d r26921:1ce3943c7e3a r26921:1ce3943c7e3a r28549:6bc33bc6ee38 r20340:dd61643506ed r20916:7557e3ac4ccb r20916:7557e3ac4ccb r20916:7557e3ac4ccb r20916:7557e3ac4ccb r20916:7557e3ac4ccb r28549:6bc33bc6ee38 r20855:9927a23da845 r20855:9927a23da845 r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20409:7af3404f1664 r20409:7af3404f1664 r21253:721e56378bc5 r21253:721e56378bc5 r20599:d47fae254933 r20409:7af3404f1664 r20409:7af3404f1664 r20599:d47fae254933 r20599:d47fae254933 r20599:d47fae254933 r20599:d47fae254933 r21253:721e56378bc5 r21253:721e56378bc5 r20599:d47fae254933 r21253:721e56378bc5 r21253:721e56378bc5 r21253:721e56378bc5 r21253:721e56378bc5 r20599:d47fae254933 r20409:7af3404f1664 r20409:7af3404f1664 r20409:7af3404f1664 r20599:d47fae254933 r20409:7af3404f1664 r20409:7af3404f1664 r20409:7af3404f1664 r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r26916:2753c2f94e2d r20340:dd61643506ed r26918:4d6a6b456ce8 r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r24597:afde5721a3b6 r24597:afde5721a3b6 r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r23607:36c15679007d r28178:d924f9b08845 r20340:dd61643506ed r26921:1ce3943c7e3a r20340:dd61643506ed r24597:afde5721a3b6 r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r26921:1ce3943c7e3a r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20340:dd61643506ed r20407:1af842eaf6e3 r20340:dd61643506ed r20340:dd61643506ed r23607:36c15679007d r20340:dd61643506ed r20340:dd61643506ed | /*
* 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"
#include "linkgraphschedule.h"
#include "../safeguards.h"
/* Initialize the link-graph-job-pool */
LinkGraphJobPool _link_graph_job_pool("LinkGraphJob");
INSTANTIATE_POOL_METHODS(LinkGraphJob)
/**
* 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);
/**
* 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),
join_date(TimerGameEconomy::date + (_settings_game.linkgraph.recalc_time / EconomyTime::SECONDS_PER_DAY)),
job_completed(false),
job_aborted(false)
{
}
/**
* 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);
}
}
/**
* 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()
{
if (!StartNewThread(&this->thread, "ottd:linkgraph", &(LinkGraphSchedule::Run), this)) {
/* Of course this will hang a bit.
* On the other hand, if you want to play games which make this hang noticeably
* on a platform without threads then you'll probably get other problems first.
* OK:
* If someone comes and tells me that this hangs for them, I'll implement a
* 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()
{
if (this->thread.joinable()) {
this->thread.join();
}
}
/**
* Join the link graph job and destroy it.
*/
LinkGraphJob::~LinkGraphJob()
{
this->JoinThread();
/* Don't update stuff from other pools, when everything is being removed.
* Accessing other pools may be invalid. */
if (CleaningPool()) return;
/* 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());
/* Link graph has been merged into another one. */
if (!LinkGraph::IsValidID(this->link_graph.index)) return;
uint16_t size = this->Size();
for (NodeID node_id = 0; node_id < size; ++node_id) {
NodeAnnotation &from = this->nodes[node_id];
/* The station can have been deleted. Remove all flows originating from it then. */
Station *st = Station::GetIfValid(from.base.station);
if (st == nullptr) {
this->EraseFlows(node_id);
continue;
}
/* 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()];
if (ge.link_graph != this->link_graph.index || ge.node != node_id) {
this->EraseFlows(node_id);
continue;
}
LinkGraph *lg = LinkGraph::Get(ge.link_graph);
FlowStatMap &flows = from.flows;
for (const auto &edge : from.edges) {
if (edge.Flow() == 0) continue;
NodeID dest_id = edge.base.dest_node;
StationID to = this->nodes[dest_id].base.station;
Station *st2 = Station::GetIfValid(to);
if (st2 == nullptr || st2->goods[this->Cargo()].link_graph != this->link_graph.index ||
st2->goods[this->Cargo()].node != dest_id ||
!(*lg)[node_id].HasEdgeTo(dest_id) ||
(*lg)[node_id][dest_id].LastUpdate() == EconomyTime::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()) ge.flows.erase(erased.Pop());
} else if ((*lg)[node_id][dest_id].last_unrestricted_update == EconomyTime::INVALID_DATE) {
/* Edge is fully restricted. */
flows.RestrictFlows(to);
}
}
/* Swap shares and invalidate ones that are completely deleted. Don't
* really delete them as we could then end up with unroutable cargo
* somewhere. Do delete them and also reroute relevant cargo if
* automatic distribution has been turned off for that cargo. */
for (FlowStatMap::iterator it(ge.flows.begin()); it != ge.flows.end();) {
FlowStatMap::iterator new_it = flows.find(it->first);
if (new_it == flows.end()) {
if (_settings_game.linkgraph.GetDistributionType(this->Cargo()) != DT_MANUAL) {
it->second.Invalidate();
++it;
} else {
FlowStat shares(INVALID_STATION, 1);
it->second.SwapShares(shares);
ge.flows.erase(it++);
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);
}
}
} else {
it->second.SwapShares(new_it->second);
flows.erase(new_it);
++it;
}
}
ge.flows.insert(flows.begin(), flows.end());
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();
this->nodes.reserve(size);
for (uint i = 0; i < size; ++i) {
this->nodes.emplace_back(this->link_graph.nodes[i], this->link_graph.Size());
}
}
/**
* 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)
{
this->capacity = std::min(base->capacity, cap);
this->free_capacity = std::min(base->free_capacity, free_cap);
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)
{
if (this->parent != nullptr) {
LinkGraphJob::EdgeAnnotation &edge = job[this->parent->node][this->node];
if (max_saturation != UINT_MAX) {
uint usable_cap = edge.base.capacity * max_saturation / 100;
if (usable_cap > edge.Flow()) {
new_flow = std::min(new_flow, usable_cap - edge.Flow());
} else {
return 0;
}
}
new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
if (this->flow == 0 && new_flow > 0) {
job[this->parent->node].paths.push_front(this);
}
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),
capacity(source ? UINT_MAX : 0),
free_capacity(source ? INT_MAX : INT_MIN),
flow(0), node(n), origin(source ? n : INVALID_NODE),
num_children(0), parent(nullptr)
{}
|