flowmapper.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "flowmapper.h"
00014 
00019 void FlowMapper::Run(LinkGraphJob &job) const
00020 {
00021   /* Time the graph has been running without being compressed. */
00022   uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression();
00023 
00024   for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
00025     Node prev_node = job[node_id];
00026     StationID prev = prev_node.Station();
00027     PathList &paths = prev_node.Paths();
00028     for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
00029       Path *path = *i;
00030       uint flow = path->GetFlow();
00031       if (flow == 0) continue;
00032       /* compress to monthly value */
00033       flow = max(1U, flow * 30 / runtime);
00034       Node node = job[path->GetNode()];
00035       StationID via = node.Station();
00036       StationID origin = job[path->GetOrigin()].Station();
00037       assert(prev != via && via != origin);
00038       /* Mark all of the flow for local consumption at "first". */
00039       node.Flows().AddFlow(origin, via, flow);
00040       if (prev != origin) {
00041         /* Pass some of the flow marked for local consumption at "prev" on
00042          * to this node. */
00043         prev_node.Flows().PassOnFlow(origin, via, flow);
00044       } else {
00045         /* Prev node is origin. Simply add flow. */
00046         prev_node.Flows().AddFlow(origin, via, flow);
00047       }
00048     }
00049   }
00050 
00051   for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
00052     /* Remove local consumption shares marked as invalid. */
00053     Node node = job[node_id];
00054     node.Flows().FinalizeLocalConsumption(node.Station());
00055     /* Clear paths. */
00056     PathList &paths = node.Paths();
00057     for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
00058       delete *i;
00059     }
00060     paths.clear();
00061   }
00062 }