# flowmapper.cpp

Go to the documentation of this file.

    00001 /* $Id: flowmapper.cpp 26461 2014-04-13 10:52:19Z frosch $ */
    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   for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
    00022     Node prev_node = job[node_id];
    00023     StationID prev = prev_node.Station();
    00024     PathList &paths = prev_node.Paths();
    00025     for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
    00026       Path *path = *i;
    00027       uint flow = path->GetFlow();
    00028       if (flow == 0) break;
    00029       Node node = job[path->GetNode()];
    00030       StationID via = node.Station();
    00031       StationID origin = job[path->GetOrigin()].Station();
    00032       assert(prev != via && via != origin);
    00033       /* Mark all of the flow for local consumption at "first". */
    00034       node.Flows().AddFlow(origin, via, flow);
    00035       if (prev != origin) {
    00036         /* Pass some of the flow marked for local consumption at "prev" on
    00037          * to this node. */
    00038         prev_node.Flows().PassOnFlow(origin, via, flow);
    00039       } else {
    00040         /* Prev node is origin. Simply add flow. */
    00041         prev_node.Flows().AddFlow(origin, via, flow);
    00042       }
    00043     }
    00044   }
    00045
    00046   for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
    00047     /* Remove local consumption shares marked as invalid. */
    00048     Node node = job[node_id];
    00049     FlowStatMap &flows = node.Flows();
    00050     flows.FinalizeLocalConsumption(node.Station());
    00051     if (this->scale) {
    00052       /* Scale by time the graph has been running without being compressed. Add 1 to avoid
    00053        * division by 0 if spawn date == last compression date. This matches
    00054        * LinkGraph::Monthly(). */
    00055       uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression() + 1;
    00056       for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
    00057         i->second.ScaleToMonthly(runtime);
    00058       }
    00059     }
    00060     /* Clear paths. */
    00061     PathList &paths = node.Paths();
    00062     for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
    00063       delete *i;
    00064     }
    00065     paths.clear();
    00066   }
    00067 }