Loading

Paste #pbdz0s7v8

  1. function Road::_CostHelper(self, prev_tile, new_tile, coast_cost_only = null)
  2. {
  3.     local cost = 0;
  4.  
  5.     if (coast_cost_only != true) {
  6.         /* Check for a turn. We do this by substracting the TileID of the current node from
  7.         * the TileID of the previous node and comparing that to the difference between the
  8.         * previous node and the node before that. */
  9.         cost += self._cost_tile;
  10.         if (path.GetParent() != null && (prev_tile - path.GetParent().GetTile()) != (new_tile - prev_tile) &&
  11.                 AIMap.DistanceManhattan(path.GetParent().GetTile(), prev_tile) == 1) {
  12.             cost += self._cost_turn;
  13.         }
  14.  
  15.         /* Check if the last tile was sloped. */
  16.         if (path.GetParent() != null && !AIBridge.IsBridgeTile(prev_tile) && !AITunnel.IsTunnelTile(prev_tile) &&
  17.                 (AIMap.DistanceManhattan(path.GetParent().GetTile(), prev_tile) == 1) && self._IsSlopedRoad(path.GetParent().GetTile(), prev_tile, new_tile)) {
  18.             cost += self._cost_slope;
  19.         }
  20.  
  21.         if (!AIRoad.AreRoadTilesConnected(prev_tile, new_tile)) {
  22.             cost += self._cost_no_existing_road;
  23.         }
  24.     }
  25.  
  26.     if (coast_cost_only != null) {
  27.         /* Check if the new tile is a coast tile. */
  28.         if (AITile.IsCoastTile(new_tile) && AITile.HasTransportType(new_tile, AITile.TRANSPORT_WATER)) {
  29.             cost += self._cost_coast;
  30.         }
  31.     }
  32.  
  33.     return cost;
  34. }
  35.  
  36. function Road::_Cost(self, path, new_tile, new_direction)
  37. {
  38.     /* path == null means this is the first node of a path, so the cost is 0. */
  39.     if (path == null) return 0;
  40.  
  41.     local prev_tile = path.GetTile();
  42.  
  43.     /* If the new tile is a bridge / tunnel tile, check whether we came from the other
  44.      * end of the bridge / tunnel or if we just entered the bridge / tunnel. */
  45.     if (AIBridge.IsBridgeTile(new_tile)) {
  46.         if (AIBridge.GetOtherBridgeEnd(new_tile) != prev_tile) {
  47.             return path.GetCost() + self._CostHelper(self, prev_tile, new_tile);
  48.         }
  49.         return path.GetCost() + (AIMap.DistanceManhattan(new_tile, prev_tile) + 1) * (self._cost_tile + self._cost_bridge_per_tile) - self._cost_tile + self._GetBridgeNumSlopes(new_tile, prev_tile) * self._cost_slope;
  50.     }
  51.     if (AITunnel.IsTunnelTile(new_tile)) {
  52.         if (AITunnel.GetOtherTunnelEnd(new_tile) != prev_tile) {
  53.             return path.GetCost() + self._CostHelper(self, prev_tile, new_tile);
  54.         }
  55.         return path.GetCost() + (AIMap.DistanceManhattan(new_tile, prev_tile) + 1) * (self._cost_tile + self._cost_tunnel_per_tile) - self._cost_tile;
  56.     }
  57.  
  58.     /* If the two tiles are more than 1 tile apart, the pathfinder wants a bridge or tunnel
  59.      * to be built. It isn't an existing bridge / tunnel, as that case is already handled. */
  60.     if (AIMap.DistanceManhattan(new_tile, prev_tile) > 1) {
  61.         /* Check if we should build a bridge or a tunnel. */
  62.         if (AITunnel.GetOtherTunnelEnd(new_tile) == prev_tile) {
  63.             return path.GetCost() + (AIMap.DistanceManhattan(new_tile, prev_tile) + 1) * (self._cost_tile + self._cost_tunnel_per_tile + self._cost_no_existing_road) - self._cost_tile;
  64.         } else {
  65.             return path.GetCost() + (AIMap.DistanceManhattan(new_tile, prev_tile) + 1) * (self._cost_tile + self._cost_bridge_per_tile + self._cost_no_existing_road) - self._cost_tile + self._GetBridgeNumSlopes(new_tile, prev_tile) * self._cost_slope + self._CostHelper(self, prev_tile, new_tile, true);
  66.         }
  67.     }
  68.  
  69.     return path.GetCost() + self._CostHelper(self, prev_tile, new_tile, false);
  70. }

Version history

Revision # Author Created at
pivfk1xw7 Anonymous 23 Sep 2018, 20:31:59 UTC Diff

Comments