OpenTTD
flowmapper.cpp
Go to the documentation of this file.
1 /* $Id: flowmapper.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 "flowmapper.h"
14 
15 #include "../safeguards.h"
16 
21 void FlowMapper::Run(LinkGraphJob &job) const
22 {
23  for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
24  Node prev_node = job[node_id];
25  StationID prev = prev_node.Station();
26  PathList &paths = prev_node.Paths();
27  for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
28  Path *path = *i;
29  uint flow = path->GetFlow();
30  if (flow == 0) break;
31  Node node = job[path->GetNode()];
32  StationID via = node.Station();
33  StationID origin = job[path->GetOrigin()].Station();
34  assert(prev != via && via != origin);
35  /* Mark all of the flow for local consumption at "first". */
36  node.Flows().AddFlow(origin, via, flow);
37  if (prev != origin) {
38  /* Pass some of the flow marked for local consumption at "prev" on
39  * to this node. */
40  prev_node.Flows().PassOnFlow(origin, via, flow);
41  } else {
42  /* Prev node is origin. Simply add flow. */
43  prev_node.Flows().AddFlow(origin, via, flow);
44  }
45  }
46  }
47 
48  for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
49  /* Remove local consumption shares marked as invalid. */
50  Node node = job[node_id];
51  FlowStatMap &flows = node.Flows();
52  flows.FinalizeLocalConsumption(node.Station());
53  if (this->scale) {
54  /* Scale by time the graph has been running without being compressed. Add 1 to avoid
55  * division by 0 if spawn date == last compression date. This matches
56  * LinkGraph::Monthly(). */
57  uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression() + 1;
58  for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
59  i->second.ScaleToMonthly(runtime);
60  }
61  }
62  /* Clear paths. */
63  PathList &paths = node.Paths();
64  for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
65  delete *i;
66  }
67  paths.clear();
68  }
69 }