2013-06-09 13:01:23 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file flowmapper.cpp Definition of flowmapper. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "flowmapper.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2013-06-09 13:01:23 +00:00
|
|
|
/**
|
|
|
|
* Map the paths generated by the MCF solver into flows associated with nodes.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param job the link graph component to be used.
|
2013-06-09 13:01:23 +00:00
|
|
|
*/
|
|
|
|
void FlowMapper::Run(LinkGraphJob &job) const
|
|
|
|
{
|
|
|
|
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
|
|
|
|
Node prev_node = job[node_id];
|
|
|
|
StationID prev = prev_node.Station();
|
|
|
|
PathList &paths = prev_node.Paths();
|
2017-02-08 20:38:05 +00:00
|
|
|
for (PathList::reverse_iterator i = paths.rbegin(); i != paths.rend(); ++i) {
|
2013-06-09 13:01:23 +00:00
|
|
|
Path *path = *i;
|
2017-02-08 20:38:05 +00:00
|
|
|
if (!path) continue;
|
2013-06-09 13:01:23 +00:00
|
|
|
uint flow = path->GetFlow();
|
2013-10-19 17:15:19 +00:00
|
|
|
if (flow == 0) break;
|
2013-06-09 13:01:23 +00:00
|
|
|
Node node = job[path->GetNode()];
|
|
|
|
StationID via = node.Station();
|
|
|
|
StationID origin = job[path->GetOrigin()].Station();
|
|
|
|
assert(prev != via && via != origin);
|
|
|
|
/* Mark all of the flow for local consumption at "first". */
|
|
|
|
node.Flows().AddFlow(origin, via, flow);
|
|
|
|
if (prev != origin) {
|
|
|
|
/* Pass some of the flow marked for local consumption at "prev" on
|
|
|
|
* to this node. */
|
|
|
|
prev_node.Flows().PassOnFlow(origin, via, flow);
|
|
|
|
} else {
|
|
|
|
/* Prev node is origin. Simply add flow. */
|
|
|
|
prev_node.Flows().AddFlow(origin, via, flow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
|
|
|
|
/* Remove local consumption shares marked as invalid. */
|
|
|
|
Node node = job[node_id];
|
2013-12-20 14:57:44 +00:00
|
|
|
FlowStatMap &flows = node.Flows();
|
|
|
|
flows.FinalizeLocalConsumption(node.Station());
|
|
|
|
if (this->scale) {
|
2014-04-08 19:28:14 +00:00
|
|
|
/* Scale by time the graph has been running without being compressed. Add 1 to avoid
|
|
|
|
* division by 0 if spawn date == last compression date. This matches
|
|
|
|
* LinkGraph::Monthly(). */
|
2023-07-30 10:04:42 +00:00
|
|
|
uint runtime = (uint)Clamp<DateTicksScaled>(DateTicksToScaledDateTicks(job.StartDateTicks()) - job.LastCompression() + 1, 1, UINT32_MAX);
|
2023-05-07 11:09:54 +00:00
|
|
|
for (auto &it : flows) {
|
2023-06-07 20:28:46 +00:00
|
|
|
it.ScaleToMonthly(runtime);
|
2013-12-20 14:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-09 13:01:23 +00:00
|
|
|
/* Clear paths. */
|
2017-02-08 21:46:32 +00:00
|
|
|
node.Paths().clear();
|
2013-06-09 13:01:23 +00:00
|
|
|
}
|
2017-02-08 21:46:32 +00:00
|
|
|
job.path_allocator.ResetArena();
|
2013-06-09 13:01:23 +00:00
|
|
|
}
|