summaryrefslogtreecommitdiff
path: root/src/station_cmd.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2013-05-19 14:26:14 +0000
committerrubidium <rubidium@openttd.org>2013-05-19 14:26:14 +0000
commitbe4f85766c6c2d6adb0690d77de113b7dc88fc1e (patch)
treeb87c80dab41afdfb2991ce0aaf12ec87a92a6380 /src/station_cmd.cpp
parent39474532777a1f7beadfd940abf29292bc39879a (diff)
downloadopenttd-be4f85766c6c2d6adb0690d77de113b7dc88fc1e.tar.xz
(svn r25260) -Codechange: occasionally clear dead links and compress link graphs (fonsinchen)
Diffstat (limited to 'src/station_cmd.cpp')
-rw-r--r--src/station_cmd.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp
index 10af5519e..03003a518 100644
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -3347,6 +3347,39 @@ static void UpdateStationRating(Station *st)
}
/**
+ * Check all next hops of cargo packets in this station for existance of a
+ * a valid link they may use to travel on. Reroute any cargo not having a valid
+ * link and remove timed out links found like this from the linkgraph. We're
+ * not all links here as that is expensive and useless. A link no one is using
+ * doesn't hurt either.
+ * @param from Station to check.
+ */
+void DeleteStaleLinks(Station *from)
+{
+ for (CargoID c = 0; c < NUM_CARGO; ++c) {
+ GoodsEntry &ge = from->goods[c];
+ LinkGraph *lg = LinkGraph::GetIfValid(ge.link_graph);
+ if (lg == NULL) continue;
+ Node node = (*lg)[ge.node];
+ for (EdgeIterator it(node.Begin()); it != node.End();) {
+ Edge edge = it->second;
+ Station *to = Station::Get((*lg)[it->first].Station());
+ assert(to->goods[c].node == it->first);
+ ++it; // Do that before removing the node. Anything else may crash.
+ assert(_date >= edge.LastUpdate());
+ if ((uint)(_date - edge.LastUpdate()) > LinkGraph::MIN_TIMEOUT_DISTANCE +
+ (DistanceManhattan(from->xy, to->xy) >> 2)) {
+ node.RemoveEdge(to->goods[c].node);
+ }
+ }
+ assert(_date >= lg->LastCompression());
+ if ((uint)(_date - lg->LastCompression()) > LinkGraph::COMPRESSION_INTERVAL) {
+ lg->Compress();
+ }
+ }
+}
+
+/**
* Increase capacity for a link stat given by station cargo and next hop.
* @param st Station to get the link stats from.
* @param cargo Cargo to increase stat for.
@@ -3439,6 +3472,11 @@ void OnTick_Station()
FOR_ALL_BASE_STATIONS(st) {
StationHandleSmallTick(st);
+ /* Clean up the link graph about once a week. */
+ if (Station::IsExpected(st) && (_tick_counter + st->index) % STATION_LINKGRAPH_TICKS == 0) {
+ DeleteStaleLinks(Station::From(st));
+ };
+
/* Run STATION_ACCEPTANCE_TICKS = 250 tick interval trigger for station animation.
* Station index is included so that triggers are not all done
* at the same time. */