Old revision #plwntbwg1 | New revision #pgbublr1a | ||
---|---|---|---|
1 | FindDepotData OPFShipFindNearestDepot(const Ship *v, int max_distance) // todo | 1 | FindDepotData OPFShipFindNearestDepot(const Ship *v, int max_distance) // todo |
2 | { | 2 | { |
3 | TileIndex tile = v->tile; | ||
3 | Trackdir trackdir = v->GetVehicleTrackdir(); | 4 | Trackdir trackdir = v->GetVehicleTrackdir(); |
4 | DiagDirection enterdir = TrackdirToExitdir(trackdir); | 5 | DiagDirection enterdir = TrackdirToExitdir(trackdir); |
5 | TrackBits tracks = TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(enterdir)); | 6 | TrackBits tracks = TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(enterdir)); |
6 | 7 | ||
8 | TileIndex tile_rev = TILE_ADD(tile, -TileOffsByDiagDir(enterdir)); | ||
9 | TrackBits tracks_rev = TrackStatusToTrackBits(GetTileTrackStatus(tile_rev, TRANSPORT_WATER, 0)) & DiagdirReachesTracks(ReverseDiagDir(enterdir)) & TrackdirBitsToTrackBits(TrackdirToTrackdirBits(trackdir)); | ||
10 | |||
7 | FindDepotData fdd; | 11 | FindDepotData fdd; |
8 | Depot *depot; | 12 | Depot *depot; |
9 | uint best_dist = max_distance == 0 ? UINT_MAX : max_distance; | 13 | Track track; |
14 | uint best_dist = UINT_MAX; | ||
15 | |||
10 | FOR_ALL_DEPOTS(depot) { | 16 | FOR_ALL_DEPOTS(depot) { |
11 | TileIndex depottile = depot->xy; | 17 | TileIndex depottile = depot->xy; |
12 | if (IsShipDepotTile(depottile) && IsTileOwner(depottile, v->owner)) { | 18 | if (IsShipDepotTile(depottile) && IsTileOwner(depottile, v->owner)) { |
13 | uint dist = DistanceManhattan(v->tile, depottile); | 19 | |
14 | if (dist <= best_dist) { | 20 | /* Let's find out how far it would be if we would reverse first */ |
15 | bool path_found; | 21 | uint fst_dist_rev = UINT_MAX; |
16 | Track track = OPFShipChooseTrack(v, v->tile, enterdir, tracks, path_found, depottile); | 22 | if (tracks_rev != 0) { |
17 | if (track != INVALID_TRACK) { | 23 | fst_dist_rev = FindShipTrack(v, tile_rev, ReverseDiagDir(enterdir), tracks_rev, tile, &track, depottile); |
18 | fdd.best_length = dist; | 24 | if (fst_dist_rev != UINT_MAX) fst_dist_rev++; // penalty for reversing |
19 | fdd.tile = depottile; | 25 | } |
20 | break; | 26 | |
21 | } | 27 | /* And if we would not reverse? */ |
28 | uint fst_dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track, depottile); | ||
29 | |||
30 | /* Get the shortest distance */ | ||
31 | uint dist = minu(fst_dist_rev, fst_dist); | ||
32 | |||
33 | if (dist < best_dist) { | ||
34 | best_dist = dist; | ||
35 | |||
36 | uint distdepot = DistanceManhattan(tile, depottile); | ||
37 | fdd.best_length = distdepot; | ||
38 | fdd.tile = depottile; | ||
22 | } | 39 | } |
23 | } | 40 | } |
24 | } | 41 | } |