Loading

Revision differences

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