summaryrefslogtreecommitdiff
path: root/src/ship_cmd.cpp
diff options
context:
space:
mode:
authorPeter Nelson <peter1138@openttd.org>2019-01-14 23:33:42 +0000
committerPeterN <peter@fuzzle.org>2019-01-19 23:11:17 +0000
commit81330b8d6edee68c38717462737fbfca6420701d (patch)
treeed4e0e34930a5e6f28b5d275c27f6ccb50a2ba63 /src/ship_cmd.cpp
parent1c725fce47d60907bbf2224c7bdc28607fcf6017 (diff)
downloadopenttd-81330b8d6edee68c38717462737fbfca6420701d.tar.xz
Change: Add path cache for ships.
Diffstat (limited to 'src/ship_cmd.cpp')
-rw-r--r--src/ship_cmd.cpp26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp
index ab1f59f11..7727aa608 100644
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -193,7 +193,7 @@ static void CheckIfShipNeedsService(Vehicle *v)
}
v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
- v->dest_tile = depot->xy;
+ v->SetDestTile(depot->xy);
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
}
@@ -473,10 +473,24 @@ static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, Tr
}
path_found = false;
} else {
+ /* Attempt to follow cached path. */
+ if (!v->path.empty()) {
+ track = TrackdirToTrack(v->path.front());
+
+ if (HasBit(tracks, track)) {
+ v->path.pop_front();
+ /* HandlePathfindResult() is not called here because this is not a new pathfinder result. */
+ return track;
+ }
+
+ /* Cached path is invalid so continue with pathfinder. */
+ }
+
+ v->path.clear();
switch (_settings_game.pf.pathfinder_for_ships) {
case VPF_OPF: track = OPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
case VPF_NPF: track = NPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
- case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
+ case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found, v->path); break;
default: NOT_REACHED();
}
}
@@ -665,6 +679,7 @@ getout:
reverse_direction:
dir = ReverseDir(v->direction);
v->direction = dir;
+ v->path.clear();
goto getout;
}
@@ -679,6 +694,13 @@ bool Ship::Tick()
return true;
}
+void Ship::SetDestTile(TileIndex tile)
+{
+ if (tile == this->dest_tile) return;
+ this->path.clear();
+ this->dest_tile = tile;
+}
+
/**
* Build a ship.
* @param tile tile of the depot where ship is built.