Loading

opf find depot

  1. FindDepotData OPFShipFindNearestDepot(const Ship *v, int max_distance)
  2. {
  3.     FindDepotData fdd;
  4.  
  5.     Trackdir trackdir = v->GetVehicleTrackdir();
  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));
  10.     TrackBits tracks = TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(enterdir));
  11.  
  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.        
  22.     Depot *depot;
  23.     FOR_ALL_DEPOTS(depot) {
  24.         TileIndex depottile = depot->xy;
  25.         if (IsShipDepotTile(depottile) && IsTileOwner(depottile, v->owner)) {
  26.             uint distdepot = DistanceManhattan(v->tile, depottile);
  27.             if (distdepot <= (uint)max_distance && distdepot < best_length) {
  28.  
  29.                 /* Let's find the length it would be if we would reverse first */
  30.                 uint length_rev = UINT_MAX;
  31.                 if (tracks_rev != 0) {
  32.                     length_rev = FindShipTrack(v, tile_rev, enterdir_rev, tracks_rev, tile, &track, depottile); //
  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);
  48.                 }
  49.             }
  50.         }
  51.     }
  52.  
  53.     return fdd;
  54. }

Version history

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

Comments