OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

linkgraphjob.cpp
Go to the documentation of this file.
1 /* $Id: linkgraphjob.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../core/pool_func.hpp"
14 #include "../window_func.h"
15 #include "linkgraphjob.h"
16 #include "linkgraphschedule.h"
17
18 #include "../safeguards.h"
19
20 /* Initialize the link-graph-job-pool */
21 LinkGraphJobPool _link_graph_job_pool("LinkGraphJob");
22 INSTANTIATE_POOL_METHODS(LinkGraphJob)
23
24
30 LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
31 /* Copying the link graph here also copies its index member.
32 * This is on purpose. */
33 link_graph(orig),
34 settings(_settings_game.linkgraph),
35 thread(NULL),
36 join_date(_date + _settings_game.linkgraph.recalc_time)
37 {
38 }
39
44 void LinkGraphJob::EraseFlows(NodeID from)
45 {
46 for (NodeID node_id = 0; node_id < this->Size(); ++node_id) {
47 (*this)[node_id].Flows().erase(from);
48 }
49 }
50
55 void LinkGraphJob::SpawnThread()
56 {
57 if (!ThreadObject::New(&(LinkGraphSchedule::Run), this, &this->thread)) {
58 this->thread = NULL;
59 /* Of course this will hang a bit.
60 * On the other hand, if you want to play games which make this hang noticably
61 * on a platform without threads then you'll probably get other problems first.
62 * OK:
63 * If someone comes and tells me that this hangs for him/her, I'll implement a
64 * smaller grained "Step" method for all handlers and add some more ticks where
65 * "Step" is called. No problem in principle. */
66 LinkGraphSchedule::Run(this);
67 }
68 }
69
73 void LinkGraphJob::JoinThread()
74 {
75 if (this->thread != NULL) {
76 this->thread->Join();
77 delete this->thread;
78 this->thread = NULL;
79 }
80 }
81
85 LinkGraphJob::~LinkGraphJob()
86 {
87 this->JoinThread();
88
89 /* Don't update stuff from other pools, when everything is being removed.
90 * Accessing other pools may be invalid. */
91 if (CleaningPool()) return;
92
93 /* Link graph has been merged into another one. */
94 if (!LinkGraph::IsValidID(this->link_graph.index)) return;
95
96 uint size = this->Size();
97 for (NodeID node_id = 0; node_id < size; ++node_id) {
98 Node from = (*this)[node_id];
99
100 /* The station can have been deleted. Remove all flows originating from it then. */
101 Station *st = Station::GetIfValid(from.Station());
102 if (st == NULL) {
103 this->EraseFlows(node_id);
104 continue;
105 }
106
107 /* Link graph merging and station deletion may change around IDs. Make
108 * sure that everything is still consistent or ignore it otherwise. */
109 GoodsEntry &ge = st->goods[this->Cargo()];
110 if (ge.link_graph != this->link_graph.index || ge.node != node_id) {
111 this->EraseFlows(node_id);
112 continue;
113 }
114
115 LinkGraph *lg = LinkGraph::Get(ge.link_graph);
116 FlowStatMap &flows = from.Flows();
117
118 for (EdgeIterator it(from.Begin()); it != from.End(); ++it) {
119 if (from[it->first].Flow() == 0) continue;
120 StationID to = (*this)[it->first].Station();
121 Station *st2 = Station::GetIfValid(to);
122 if (st2 == NULL || st2->goods[this->Cargo()].link_graph != this->link_graph.index ||
123 st2->goods[this->Cargo()].node != it->first ||
124 (*lg)[node_id][it->first].LastUpdate() == INVALID_DATE) {
125 /* Edge has been removed. Delete flows. */
126 StationIDStack erased = flows.DeleteFlows(to);
127 /* Delete old flows for source stations which have been deleted
128 * from the new flows. This avoids flow cycles between old and
129 * new flows. */
130 while (!erased.IsEmpty()) ge.flows.erase(erased.Pop());
131 } else if ((*lg)[node_id][it->first].LastUnrestrictedUpdate() == INVALID_DATE) {
132 /* Edge is fully restricted. */
133 flows.RestrictFlows(to);
134 }
135 }
136
137 /* Swap shares and invalidate ones that are completely deleted. Don't
138 * really delete them as we could then end up with unroutable cargo
139 * somewhere. Do delete them and also reroute relevant cargo if
140 * automatic distribution has been turned off for that cargo. */
141 for (FlowStatMap::iterator it(ge.flows.begin()); it != ge.flows.end();) {
142 FlowStatMap::iterator new_it = flows.find(it->first);
143 if (new_it == flows.end()) {
144 if (_settings_game.linkgraph.GetDistributionType(this->Cargo()) != DT_MANUAL) {
145 it->second.Invalidate();
146 ++it;
147 } else {
148 FlowStat shares(INVALID_STATION, 1);
149 it->second.SwapShares(shares);
150 ge.flows.erase(it++);
151 for (FlowStat::SharesMap::const_iterator shares_it(shares.GetShares()->begin());
152 shares_it != shares.GetShares()->end(); ++shares_it) {
153 RerouteCargo(st, this->Cargo(), shares_it->second, st->index);
154 }
155 }
156 } else {
157 it->second.SwapShares(new_it->second);
158 flows.erase(new_it);
159 ++it;
160 }
161 }
162 ge.flows.insert(flows.begin(), flows.end());
163 InvalidateWindowData(WC_STATION_VIEW, st->index, this->Cargo());
164 }
165 }
166
172 void LinkGraphJob::Init()
173 {
174 uint size = this->Size();
175 this->nodes.Resize(size);
176 this->edges.Resize(size, size);
177 for (uint i = 0; i < size; ++i) {
178 this->nodes[i].Init(this->link_graph[i].Supply());
179 EdgeAnnotation *node_edges = this->edges[i];
180 for (uint j = 0; j < size; ++j) {
181 node_edges[j].Init();
182 }
183 }
184 }
185
189 void LinkGraphJob::EdgeAnnotation::Init()
190 {
191 this->demand = 0;
192 this->flow = 0;
193 this->unsatisfied_demand = 0;
194 }
195
201 void LinkGraphJob::NodeAnnotation::Init(uint supply)
202 {
203 this->undelivered_supply = supply;
204 new (&this->flows) FlowStatMap;
205 new (&this->paths) PathList;
206 }
207
216 void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
217 {
218 this->capacity = min(base->capacity, cap);
219 this->free_capacity = min(base->free_capacity, free_cap);
220 this->distance = base->distance + dist;
221 assert(this->distance > 0);
222 if (this->parent != base) {
223 this->Detach();
224 this->parent = base;
225 this->parent->num_children++;
226 }
227 this->origin = base->origin;
228 }
229
238 uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
239 {
240 if (this->parent != NULL) {
241 LinkGraphJob::Edge edge = job[this->parent->node][this->node];
242 if (max_saturation != UINT_MAX) {
243 uint usable_cap = edge.Capacity() * max_saturation / 100;
244 if (usable_cap > edge.Flow()) {
245 new_flow = min(new_flow, usable_cap - edge.Flow());
246 } else {
247 return 0;
248 }
249 }
250 new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
251 if (this->flow == 0 && new_flow > 0) {
252 job[this->parent->node].Paths().push_front(this);
253 }
254 edge.AddFlow(new_flow);
255 }
256 this->flow += new_flow;
257 return new_flow;
258 }
259
265 Path::Path(NodeID n, bool source) :
266 distance(source ? 0 : UINT_MAX),
267 capacity(source ? UINT_MAX : 0),
268 free_capacity(source ? INT_MAX : INT_MIN),
269 flow(0), node(n), origin(source ? n : INVALID_NODE),
270 num_children(0), parent(NULL)
271 {}
272