Loading

Revision differences

Old revision #pgbublr1aNew revision #pxwa9eode
1FindDepotData OPFShipFindNearestDepot(const Ship *v, int max_distance) // todo  1FindDepotData OPFShipFindNearestDepot(const Ship *v, int max_distance)
2{  2{  
3    TileIndex tile = v->tile;  3    FindDepotData fdd;
   4
4    Trackdir trackdir = v->GetVehicleTrackdir();  5    Trackdir trackdir = v->GetVehicleTrackdir();  
5    DiagDirection enterdir = TrackdirToExitdir(trackdir);     6
   7    /* Argument values for FindShipTrack below, for the current ship direction */
   8    DiagDirection enterdir = TrackdirToExitdir(trackdir);
   9    TileIndex tile = TILE_ADD(v->tile, TileOffsByDiagDir(enterdir));
6    TrackBits tracks = TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(enterdir));  10    TrackBits tracks = TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(enterdir));  
7  11  
8    TileIndex tile_rev = TILE_ADD(tile, -TileOffsByDiagDir(enterdir));  12    /* Argument values for FindShipTrack below, for the reversed ship direction */
9    TrackBits tracks_rev = TrackStatusToTrackBits(GetTileTrackStatus(tile_rev, TRANSPORT_WATER, 0)) & DiagdirReachesTracks(ReverseDiagDir(enterdir)) & TrackdirBitsToTrackBits(TrackdirToTrackdirBits(trackdir));  13    DiagDirection enterdir_rev = ReverseDiagDir(enterdir);
   14    TileIndex tile_rev = v->tile;
   15    TrackBits tracks_rev = TrackStatusToTrackBits(GetTileTrackStatus(tile_rev, TRANSPORT_WATER, 0)) & DiagdirReachesTracks(enterdir_rev) & TrackdirBitsToTrackBits(TrackdirToTrackdirBits(trackdir));
10  16  
11    FindDepotData fdd;  17    Track track; // initialized, but unused
   18
   19    if (max_distance == 0) max_distance = INT_MAX;
   20    uint best_length = UINT_MAX;
   21       
12    Depot *depot;  22    Depot *depot;  
13    Track track;    
14    uint best_dist = UINT_MAX;    
15    
16    FOR_ALL_DEPOTS(depot) {  23    FOR_ALL_DEPOTS(depot) {  
17        TileIndex depottile = depot->xy;  24        TileIndex depottile = depot->xy;  
18        if (IsShipDepotTile(depottile) && IsTileOwner(depottile, v->owner)) {  25        if (IsShipDepotTile(depottile) && IsTileOwner(depottile, v->owner)) {  
  26            uint distdepot = DistanceManhattan(v->tile, depottile);  
  27            if (distdepot <= (uint)max_distance && distdepot < best_length) {  
19  28  
20            /* Let's find out how far it would be if we would reverse first */  20                /* Let's find the length it would be if we would reverse first */
21            uint fst_dist_rev = UINT_MAX;  21                uint length_rev = UINT_MAX;
22            if (tracks_rev != 0) {  22                if (tracks_rev != 0) {
23                fst_dist_rev = FindShipTrack(v, tile_rev, ReverseDiagDir(enterdir), tracks_rev, tile, &track, depottile);  23                    length_rev = FindShipTrack(v, tile_rev, enterdir_rev, tracks_rev, tile, &track, depottile); //
24                if (fst_dist_rev != UINT_MAX) fst_dist_rev++; // penalty for reversing  24                    if (length_rev != UINT_MAX) length_rev++; // penalty for reversing
25            }  25                }
26  35  
27            /* And if we would not reverse? */  27                /* And if we would not reverse? */
28            uint fst_dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track, depottile);  28                uint length = FindShipTrack(v, tile, enterdir, tracks, 0, &track, depottile);
29  38  
30            /* Get the shortest distance */  30                /* Get the shortest length */
31            uint dist = minu(fst_dist_rev, fst_dist);  31                uint min_length = minu(length_rev, length);
32  41  
33            if (dist < best_dist) {  33                if (min_length < best_length) {
34                best_dist = dist;  34                    best_length = min_length;
35  44  
36                uint distdepot = DistanceManhattan(tile, depottile);  45                    fdd.tile = depottile; // tile location of ship depot
37                fdd.best_length = distdepot;  46                    fdd.best_length = distdepot; // distance manhattan from ship to depot
38                fdd.tile = depottile;  47                    fdd.reverse = !(length <= length_rev);
   48                }
39            }  49            }  
40        }  50        }  
41    }  51    }