src/ai/trolly/pathfinder.cpp
branchNewGRF_ports
changeset 6872 1c4a4a609f85
parent 6871 5a9dc001e1ad
child 6877 889301acc299
--- a/src/ai/trolly/pathfinder.cpp	Mon Dec 03 23:39:38 2007 +0000
+++ b/src/ai/trolly/pathfinder.cpp	Tue Jan 22 21:00:30 2008 +0000
@@ -4,15 +4,17 @@
 #include "../../openttd.h"
 #include "../../bridge_map.h"
 #include "../../debug.h"
-#include "../../functions.h"
-#include "../../map.h"
-#include "../../tile.h"
-#include "../../command.h"
+#include "../../command_func.h"
 #include "trolly.h"
 #include "../../depot.h"
 #include "../../tunnel_map.h"
 #include "../../bridge.h"
+#include "../../tunnelbridge_map.h"
 #include "../ai.h"
+#include "../../variables.h"
+#include "../../player_base.h"
+#include "../../player_func.h"
+
 
 #define TEST_STATION_NO_DIR 0xFF
 
@@ -27,14 +29,14 @@
 		// TODO: currently we only allow spots that can be access from al 4 directions...
 		//  should be fixed!!!
 		for (dir = 0; dir < 4; dir++) {
-			ret = AiNew_Build_Station(p, p->ainew.tbt, tile, 1, 1, dir, DC_QUERY_COST);
+			ret = AiNew_Build_Station(p, _players_ainew[p->index].tbt, tile, 1, 1, dir, DC_QUERY_COST);
 			if (CmdSucceeded(ret)) return true;
 		}
 		return false;
 	}
 
 	// return true if command succeeded, so the inverse of CmdFailed()
-	return CmdSucceeded(AiNew_Build_Station(p, p->ainew.tbt, tile, 1, 1, dir, DC_QUERY_COST));
+	return CmdSucceeded(AiNew_Build_Station(p, _players_ainew[p->index].tbt, tile, 1, 1, dir, DC_QUERY_COST));
 }
 
 
@@ -43,10 +45,7 @@
 	return
 		// MP_ROAD, but not a road depot?
 		(IsTileType(tile, MP_ROAD) && !IsTileDepotType(tile, TRANSPORT_ROAD)) ||
-		(IsTileType(tile, MP_TUNNELBRIDGE) && (
-			(IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) ||
-			(IsBridge(tile) && GetBridgeTransportType(tile) == TRANSPORT_ROAD)
-		));
+		(IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD);
 }
 
 
@@ -234,11 +233,7 @@
 			// If the next step is a bridge, we have to enter it the right way
 			if (!PathFinderInfo->rail_or_road && IsRoad(atile)) {
 				if (IsTileType(atile, MP_TUNNELBRIDGE)) {
-					if (IsTunnel(atile)) {
-						if (GetTunnelDirection(atile) != i) continue;
-					} else {
-						if (GetBridgeRampDirection(atile) != i) continue;
-					}
+					if (GetTunnelBridgeDirection(atile) != i) continue;
 				}
 			}
 
@@ -364,10 +359,6 @@
 
 extern Foundation GetRailFoundation(Slope tileh, TrackBits bits); // XXX function declaration in .c
 extern Foundation GetRoadFoundation(Slope tileh, RoadBits bits); // XXX function declaration in .c
-extern Foundation GetBridgeFoundation(Slope tileh, Axis); // XXX function declaration in .c
-enum BridgeFoundation {
-	BRIDGE_NO_FOUNDATION = 1 << 0 | 1 << 3 | 1 << 6 | 1 << 9 | 1 << 12,
-};
 
 // The most important function: it calculates the g-value
 static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current, OpenListNode *parent)
@@ -408,10 +399,10 @@
 	if (parent_tileh != SLOPE_FLAT && parent->path.parent != NULL) {
 		// Skip if the tile was from a bridge or tunnel
 		if (parent->path.node.user_data[0] == 0 && current->user_data[0] == 0) {
+			static const uint32 SLOPED_TILEHS = (1 << SLOPE_NW) | (1 << SLOPE_SW) |  (1 << SLOPE_SE) | (1 << SLOPE_NE);
 			if (PathFinderInfo->rail_or_road) {
 				Foundation f = GetRailFoundation(parent_tileh, (TrackBits)(1 << AiNew_GetRailDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile)));
-				// Maybe is BRIDGE_NO_FOUNDATION a bit strange here, but it contains just the right information..
-				if (IsInclinedFoundation(f) || (!IsFoundation(f) && HasBit(BRIDGE_NO_FOUNDATION, parent_tileh))) {
+				if (IsInclinedFoundation(f) || (!IsFoundation(f) && HasBit(SLOPED_TILEHS, parent_tileh))) {
 					res += AI_PATHFINDER_TILE_GOES_UP_PENALTY;
 				} else {
 					res += AI_PATHFINDER_FOUNDATION_PENALTY;
@@ -419,7 +410,7 @@
 			} else {
 				if (!IsRoad(parent->path.node.tile) || !IsTileType(parent->path.node.tile, MP_TUNNELBRIDGE)) {
 					Foundation f = GetRoadFoundation(parent_tileh, (RoadBits)AiNew_GetRoadDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile));
-					if (IsInclinedFoundation(f) || (!IsFoundation(f) && HasBit(BRIDGE_NO_FOUNDATION, parent_tileh))) {
+					if (IsInclinedFoundation(f) || (!IsFoundation(f) && HasBit(SLOPED_TILEHS, parent_tileh))) {
 						res += AI_PATHFINDER_TILE_GOES_UP_PENALTY;
 					} else {
 						res += AI_PATHFINDER_FOUNDATION_PENALTY;
@@ -444,19 +435,9 @@
 		res += AI_PATHFINDER_BRIDGE_PENALTY * GetBridgeLength(current->tile, parent->path.node.tile);
 		// Check if we are going up or down, first for the starting point
 		// In user_data[0] is at the 8th bit the direction
-		if (!HasBit(BRIDGE_NO_FOUNDATION, parent_tileh)) {
-			if (IsLeveledFoundation(GetBridgeFoundation(parent_tileh, (Axis)((current->user_data[0] >> 8) & 1)))) {
-				res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
-			}
-		}
+		if (!HasBridgeFlatRamp(parent_tileh, (Axis)((current->user_data[0] >> 8) & 1))) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
 		// Second for the end point
-		if (!HasBit(BRIDGE_NO_FOUNDATION, tileh)) {
-			if (IsLeveledFoundation(GetBridgeFoundation(tileh, (Axis)((current->user_data[0] >> 8) & 1)))) {
-				res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
-			}
-		}
-		if (parent_tileh == SLOPE_FLAT) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
-		if (tileh == SLOPE_FLAT) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
+		if (!HasBridgeFlatRamp(tileh, (Axis)((current->user_data[0] >> 8) & 1))) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
 	}
 
 	//  To prevent the AI from taking the fastest way in tiles, but not the fastest way