ai_build.c
changeset 2366 346cbe2de606
parent 2186 db48cf29b983
--- a/ai_build.c	Fri Aug 26 20:26:34 2005 +0000
+++ b/ai_build.c	Fri Aug 26 20:56:48 2005 +0000
@@ -23,6 +23,7 @@
 	return true;
 }
 
+
 // Build station
 //  Params:
 //    type : AI_TRAIN/AI_BUS/AI_TRUCK : indicates the type of station
@@ -42,6 +43,7 @@
 	return DoCommandByTile(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
 }
 
+
 // Builds a brdige. The second best out of the ones available for this player
 //  Params:
 //   tile_a : starting point
@@ -59,8 +61,7 @@
 			type2 = type;
 			type = bridge_type;
 			// We found two bridges, exit
-			if (type2 != 0)
-				break;
+			if (type2 != 0) break;
 		}
 	}
 	// There is only one bridge that can be build..
@@ -84,8 +85,9 @@
 //   part : Which part we need to build
 //
 // TODO: skip already builded road-pieces (e.g.: cityroad)
-int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag) {
-    int part = PathFinderInfo->position;
+int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag)
+{
+	int part = PathFinderInfo->position;
 	byte *route_extra = PathFinderInfo->route_extra;
 	TileIndex *route = PathFinderInfo->route;
 	int dir;
@@ -101,144 +103,147 @@
 
 	if (PathFinderInfo->rail_or_road) {
 		// Tunnel code
-     	if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
-     		cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
-     		PathFinderInfo->position++;
-     		// TODO: problems!
-     		if (CmdFailed(cost)) {
-     			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
+		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
+			cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
+			PathFinderInfo->position++;
+			// TODO: problems!
+			if (CmdFailed(cost)) {
+				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
 				return 0;
-     		}
-     		return cost;
-     	}
-     	// Bridge code
-     	if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
-     		cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag);
-     		PathFinderInfo->position++;
-     		// TODO: problems!
-     		if (CmdFailed(cost)) {
-     			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
+			}
+			return cost;
+		}
+		// Bridge code
+		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
+			cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag);
+			PathFinderInfo->position++;
+			// TODO: problems!
+			if (CmdFailed(cost)) {
+				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
 				return 0;
-     		}
-     		return cost;
-     	}
+			}
+			return cost;
+		}
 
-     	// Build normal rail
-     	// Keep it doing till we go an other way
-     	if (route_extra[part-1] == 0 && route_extra[part] == 0) {
-     		while (route_extra[part] == 0) {
-	     		// Get the current direction
-	     		dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]);
-	     		// Is it the same as the last one?
-	     		if (old_dir != -1 && old_dir != dir) break;
-	     		old_dir = dir;
-	     		// Build the tile
-	     		res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
-	     		if (CmdFailed(res)) {
-	     			// Problem.. let's just abort it all!
-	     			p->ainew.state = AI_STATE_NOTHING;
-	     			return 0;
-	     		}
-	     		cost += res;
-	     		// Go to the next tile
-	     		part++;
-	     		// Check if it is still in range..
-	     		if (part >= PathFinderInfo->route_length - 1) break;
-	     	}
-	     	part--;
-	    }
-     	// We want to return the last position, so we go back one
-     	PathFinderInfo->position = part;
-    } else {
+		// Build normal rail
+		// Keep it doing till we go an other way
+		if (route_extra[part-1] == 0 && route_extra[part] == 0) {
+			while (route_extra[part] == 0) {
+				// Get the current direction
+				dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]);
+				// Is it the same as the last one?
+				if (old_dir != -1 && old_dir != dir) break;
+				old_dir = dir;
+				// Build the tile
+				res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
+				if (CmdFailed(res)) {
+					// Problem.. let's just abort it all!
+					p->ainew.state = AI_STATE_NOTHING;
+					return 0;
+				}
+				cost += res;
+				// Go to the next tile
+				part++;
+				// Check if it is still in range..
+				if (part >= PathFinderInfo->route_length - 1) break;
+			}
+			part--;
+		}
+		// We want to return the last position, so we go back one
+		PathFinderInfo->position = part;
+	} else {
 		// Tunnel code
-     	if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
-     		cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
-     		PathFinderInfo->position++;
-     		// TODO: problems!
-     		if (CmdFailed(cost)) {
-     			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
+		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
+			cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
+			PathFinderInfo->position++;
+			// TODO: problems!
+			if (CmdFailed(cost)) {
+				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
 				return 0;
-     		}
-     		return cost;
-     	}
-     	// Bridge code
-     	if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
-     		cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag);
-     		PathFinderInfo->position++;
-     		// TODO: problems!
-     		if (CmdFailed(cost)) {
-     			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
+			}
+			return cost;
+		}
+		// Bridge code
+		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
+			cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag);
+			PathFinderInfo->position++;
+			// TODO: problems!
+			if (CmdFailed(cost)) {
+				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
 				return 0;
-     		}
-     		return cost;
-     	}
+			}
+			return cost;
+		}
 
-     	// Build normal road
-     	// Keep it doing till we go an other way
-     	// EnsureNoVehicle makes sure we don't build on a tile where a vehicle is. This way
-     	//  it will wait till the vehicle is gone..
-     	if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
-     		while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
-	     		// Get the current direction
-	     		dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]);
-	     		// Is it the same as the last one?
-	     		if (old_dir != -1 && old_dir != dir) break;
-	     		old_dir = dir;
-	     		// There is already some road, and it is a bridge.. don't build!!!
-	     		if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
-	     			// Build the tile
-	     			res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
-	     			// Currently, we ignore CMD_ERRORs!
-						if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) {
-							// Problem.. let's just abort it all!
-							DEBUG(ai,0)("Darn, the route could not be builded.. aborting!");
-							p->ainew.state = AI_STATE_NOTHING;
-							return 0;
-						}
+		// Build normal road
+		// Keep it doing till we go an other way
+		// EnsureNoVehicle makes sure we don't build on a tile where a vehicle is. This way
+		//  it will wait till the vehicle is gone..
+		if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
+			while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
+				// Get the current direction
+				dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]);
+				// Is it the same as the last one?
+				if (old_dir != -1 && old_dir != dir) break;
+				old_dir = dir;
+				// There is already some road, and it is a bridge.. don't build!!!
+				if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
+					// Build the tile
+					res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
+					// Currently, we ignore CMD_ERRORs!
+					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) {
+						// Problem.. let's just abort it all!
+						DEBUG(ai,0)("Darn, the route could not be builded.. aborting!");
+						p->ainew.state = AI_STATE_NOTHING;
+						return 0;
+					}
 
-    	     	if (!CmdFailed(res)) cost += res;
-		     	}
-	     		// Go to the next tile
-	     		part++;
-	     		// Check if it is still in range..
-	     		if (part >= PathFinderInfo->route_length - 1) break;
-	     	}
-	     	part--;
-	     	// We want to return the last position, so we go back one
-	    }
-	    if (!EnsureNoVehicle(route[part]) && flag == DC_EXEC) part--;
-     	PathFinderInfo->position = part;
-    }
+					if (!CmdFailed(res)) cost += res;
+				}
+				// Go to the next tile
+				part++;
+				// Check if it is still in range..
+				if (part >= PathFinderInfo->route_length - 1) break;
+			}
+			part--;
+			// We want to return the last position, so we go back one
+		}
+		if (!EnsureNoVehicle(route[part]) && flag == DC_EXEC) part--;
+		PathFinderInfo->position = part;
+	}
 
-    return cost;
+	return cost;
 }
 
+
 // This functions tries to find the best vehicle for this type of cargo
 // It returns vehicle_id or -1 if not found
-int AiNew_PickVehicle(Player *p) {
-    if (p->ainew.tbt == AI_TRAIN) {
-        // Not supported yet
-        return -1;
-    } else {
-        int start, count, i, ret = CMD_ERROR;
-        start = _cargoc.ai_roadveh_start[p->ainew.cargo];
-        count = _cargoc.ai_roadveh_count[p->ainew.cargo];
+int AiNew_PickVehicle(Player *p)
+{
+	if (p->ainew.tbt == AI_TRAIN) {
+		// Not supported yet
+		return -1;
+	} else {
+		int start, count, i, ret = CMD_ERROR;
+		start = _cargoc.ai_roadveh_start[p->ainew.cargo];
+		count = _cargoc.ai_roadveh_count[p->ainew.cargo];
 
-        // Let's check it backwards.. we simply want to best engine available..
-        for (i=start+count-1;i>=start;i--) {
-        	// Is it availiable?
-        	// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
-        	if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
-        	// Can we build it?
-        	ret = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
-        	if (!CmdFailed(ret)) break;
-       	}
-       	// We did not find a vehicle :(
-       	if (CmdFailed(ret)) { return -1; }
-       	return i;
-    }
+		// Let's check it backwards.. we simply want to best engine available..
+		for (i=start+count-1;i>=start;i--) {
+			// Is it availiable?
+			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
+			if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
+			// Can we build it?
+			ret = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
+			if (!CmdFailed(ret)) break;
+		}
+		// We did not find a vehicle :(
+		if (CmdFailed(ret)) { return -1; }
+		return i;
+	}
 }
 
+
 // Builds the best vehicle possible
 int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
 {
@@ -246,7 +251,7 @@
 	if (i == -1) return CMD_ERROR;
 
 	if (p->ainew.tbt == AI_TRAIN)
-		return CMD_ERROR;
+	return CMD_ERROR;
 
 	return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
 }
@@ -255,14 +260,14 @@
 {
 	static const byte _roadbits_by_dir[4] = {2,1,8,4};
 	int ret, ret2;
-    if (p->ainew.tbt == AI_TRAIN)
-    	return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
+	if (p->ainew.tbt == AI_TRAIN)
+		return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
 
-		ret = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
-		if (CmdFailed(ret)) return ret;
-		// Try to build the road from the depot
-		ret2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
-		// If it fails, ignore it..
-		if (CmdFailed(ret2)) return ret;
-		return ret + ret2;
+	ret = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
+	if (CmdFailed(ret)) return ret;
+	// Try to build the road from the depot
+	ret2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
+	// If it fails, ignore it..
+	if (CmdFailed(ret2)) return ret;
+	return ret + ret2;
 }