Loading

opf find depot

  1. FindDepotData OPFShipFindNearestDepot(const Ship *v, int max_distance) // todo
  2. {
  3.     TileIndex tile = v->tile;
  4.     Trackdir trackdir = v->GetVehicleTrackdir();
  5.     DiagDirection enterdir = TrackdirToExitdir(trackdir);  
  6.     TrackBits tracks = TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(enterdir));
  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.  
  11.     FindDepotData fdd;
  12.     Depot *depot;
  13.     Track track;
  14.     uint best_dist = UINT_MAX;
  15.  
  16.     FOR_ALL_DEPOTS(depot) {
  17.         TileIndex depottile = depot->xy;
  18.         if (IsShipDepotTile(depottile) && IsTileOwner(depottile, v->owner)) {
  19.  
  20.             /* Let's find out how far it would be if we would reverse first */
  21.             uint fst_dist_rev = UINT_MAX;
  22.             if (tracks_rev != 0) {
  23.                 fst_dist_rev = FindShipTrack(v, tile_rev, ReverseDiagDir(enterdir), tracks_rev, tile, &track, depottile);
  24.                 if (fst_dist_rev != UINT_MAX) fst_dist_rev++; // penalty for reversing
  25.             }
  26.  
  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;
  39.             }
  40.         }
  41.     }
  42.  
  43.     return fdd;
  44. }

Version history

Revision # Author Created at
plwntbwg1 Anonymous 30 Mar 2017, 18:44:54 UTC Diff

Comments