Loading

Revision differences

Old revision #pivfk1xw7New revision #pbdz0s7v8
  1function 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  
1function Road::_Cost(self, path, new_tile, new_direction)  36function Road::_Cost(self, path, new_tile, new_direction)  
2{  37{  
3    /* path == null means this is the first node of a path, so the cost is 0. */  38    /* path == null means this is the first node of a path, so the cost is 0. */  
  
9     * end of the bridge / tunnel or if we just entered the bridge / tunnel. */  44     * end of the bridge / tunnel or if we just entered the bridge / tunnel. */  
10    if (AIBridge.IsBridgeTile(new_tile)) {  45    if (AIBridge.IsBridgeTile(new_tile)) {  
11        if (AIBridge.GetOtherBridgeEnd(new_tile) != prev_tile) {  46        if (AIBridge.GetOtherBridgeEnd(new_tile) != prev_tile) {  
12  12            return path.GetCost() + self._CostHelper(self, prev_tile, new_tile);
13            /* Check for a turn. We do this by substracting the TileID of the current node from    
14             * the TileID of the previous node and comparing that to the difference between the    
15             * previous node and the node before that. */    
16            local cost = self._cost_tile;    
17            if (path.GetParent() != null && (prev_tile - path.GetParent().GetTile()) != (new_tile - prev_tile) &&    
18                    AIMap.DistanceManhattan(path.GetParent().GetTile(), prev_tile) == 1) {    
19                cost += self._cost_turn;    
20            }    
21    
22            /* Check if the last tile was sloped. */    
23            if (path.GetParent() != null && !AIBridge.IsBridgeTile(prev_tile) && !AITunnel.IsTunnelTile(prev_tile) &&    
24                    AIMap.DistanceManhattan(path.GetParent().GetTile(), prev_tile) == 1 && self._IsSlopedRoad(path.GetParent().GetTile(), prev_tile, new_tile)) {    
25                cost += self._cost_slope;    
26            }    
27    
28            if (!AIRoad.AreRoadTilesConnected(prev_tile, new_tile)) {    
29                cost += self._cost_no_existing_road;    
30            }    
31    
32            return path.GetCost() + cost;    
33        }  48        }  
34        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;  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;  
35    }  50    }  
36    if (AITunnel.IsTunnelTile(new_tile)) {  51    if (AITunnel.IsTunnelTile(new_tile)) {  
37        if (AITunnel.GetOtherTunnelEnd(new_tile) != prev_tile) {  52        if (AITunnel.GetOtherTunnelEnd(new_tile) != prev_tile) {  
38  38            return path.GetCost() + self._CostHelper(self, prev_tile, new_tile);
39            /* Check for a turn. We do this by substracting the TileID of the current node from    
40             * the TileID of the previous node and comparing that to the difference between the    
41             * previous node and the node before that. */    
42            local cost = self._cost_tile;    
43            if (path.GetParent() != null && (prev_tile - path.GetParent().GetTile()) != (new_tile - prev_tile) &&    
44                    AIMap.DistanceManhattan(path.GetParent().GetTile(), prev_tile) == 1) {    
45                cost += self._cost_turn;    
46            }    
47    
48            /* Check if the last tile was sloped. */    
49            if (path.GetParent() != null && !AIBridge.IsBridgeTile(prev_tile) && !AITunnel.IsTunnelTile(prev_tile) &&    
50                    AIMap.DistanceManhattan(path.GetParent().GetTile(), prev_tile) == 1 && self._IsSlopedRoad(path.GetParent().GetTile(), prev_tile, new_tile)) {    
51                cost += self._cost_slope;    
52            }    
53    
54            if (!AIRoad.AreRoadTilesConnected(prev_tile, new_tile)) {    
55                cost += self._cost_no_existing_road;    
56            }    
57    
58            return path.GetCost() + cost;    
59        }  54        }  
60        return path.GetCost() + (AIMap.DistanceManhattan(new_tile, prev_tile) + 1) * (self._cost_tile + self._cost_tunnel_per_tile) - self._cost_tile;  55        return path.GetCost() + (AIMap.DistanceManhattan(new_tile, prev_tile) + 1) * (self._cost_tile + self._cost_tunnel_per_tile) - self._cost_tile;  
61    }  56    }  
  
67        if (AITunnel.GetOtherTunnelEnd(new_tile) == prev_tile) {  62        if (AITunnel.GetOtherTunnelEnd(new_tile) == prev_tile) {  
68            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;  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;  
69        } else {  64        } else {  
70            local cost = (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;  70            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);
71            /* Check if the new tile is a coast tile. */    
72            if (AITile.IsCoastTile(new_tile) && AITile.HasTransportType(new_tile, AITile.TRANSPORT_WATER)) {    
73                cost += self._cost_coast;    
74            }    
75            return path.GetCost() + cost;    
76        }  66        }  
77    }  67    }  
78  68  
79    /* Check for a turn. We do this by substracting the TileID of the current node from  79    return path.GetCost() + self._CostHelper(self, prev_tile, new_tile, false);
80     * the TileID of the previous node and comparing that to the difference between the    
81     * previous node and the node before that. */    
82    local cost = self._cost_tile;    
83    if (path.GetParent() != null && (prev_tile - path.GetParent().GetTile()) != (new_tile - prev_tile) &&    
84            AIMap.DistanceManhattan(path.GetParent().GetTile(), prev_tile) == 1) {    
85        cost += self._cost_turn;    
86    }    
87    
88    /* Check if the new tile is a coast tile. */    
89    if (AITile.IsCoastTile(new_tile) && AITile.HasTransportType(new_tile, AITile.TRANSPORT_WATER)) {    
90        cost += self._cost_coast;    
91    }    
92    
93    /* Check if the last tile was sloped. */    
94    if (path.GetParent() != null && !AIBridge.IsBridgeTile(prev_tile) && !AITunnel.IsTunnelTile(prev_tile) &&    
95            AIMap.DistanceManhattan(path.GetParent().GetTile(), prev_tile) == 1 && self._IsSlopedRoad(path.GetParent().GetTile(), prev_tile, new_tile)) {    
96        cost += self._cost_slope;    
97    }    
98    
99    if (!AIRoad.AreRoadTilesConnected(prev_tile, new_tile)) {    
100        cost += self._cost_no_existing_road;    
101    }    
102    
103    return path.GetCost() + cost;    
104} 70}