Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / linkgraph / flowmapper.cpp
blob2e0257fc1bc1ac8703f22a81cab4f3030fe64fc2
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file flowmapper.cpp Definition of flowmapper. */
10 #include "../stdafx.h"
11 #include "flowmapper.h"
13 #include "../safeguards.h"
15 /**
16 * Map the paths generated by the MCF solver into flows associated with nodes.
17 * @param job the link graph component to be used.
19 void FlowMapper::Run(LinkGraphJob &job) const
21 for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
22 Node prev_node = job[node_id];
23 StationID prev = prev_node.Station();
24 PathList &paths = prev_node.Paths();
25 for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
26 Path *path = *i;
27 uint flow = path->GetFlow();
28 if (flow == 0) break;
29 Node node = job[path->GetNode()];
30 StationID via = node.Station();
31 StationID origin = job[path->GetOrigin()].Station();
32 assert(prev != via && via != origin);
33 /* Mark all of the flow for local consumption at "first". */
34 node.Flows().AddFlow(origin, via, flow);
35 if (prev != origin) {
36 /* Pass some of the flow marked for local consumption at "prev" on
37 * to this node. */
38 prev_node.Flows().PassOnFlow(origin, via, flow);
39 } else {
40 /* Prev node is origin. Simply add flow. */
41 prev_node.Flows().AddFlow(origin, via, flow);
46 for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
47 /* Remove local consumption shares marked as invalid. */
48 Node node = job[node_id];
49 FlowStatMap &flows = node.Flows();
50 flows.FinalizeLocalConsumption(node.Station());
51 if (this->scale) {
52 /* Scale by time the graph has been running without being compressed. Add 1 to avoid
53 * division by 0 if spawn date == last compression date. This matches
54 * LinkGraph::Monthly(). */
55 uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression() + 1;
56 for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
57 i->second.ScaleToMonthly(runtime);
60 /* Clear paths. */
61 PathList &paths = node.Paths();
62 for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
63 delete *i;
65 paths.clear();