ai_build.c
changeset 84 1e0721c29bad
child 110 a22a6b07904b
equal deleted inserted replaced
83:bd21afea96d8 84:1e0721c29bad
       
     1 #include "stdafx.h"
       
     2 #include "ttd.h"
       
     3 #include "command.h"
       
     4 #include "ai.h"
       
     5 #include "engine.h"
       
     6 
       
     7 // Build HQ
       
     8 //  Params:
       
     9 //    tile : tile where HQ is going to be build
       
    10 bool AiNew_Build_CompanyHQ(Player *p, uint tile) {
       
    11 	if (DoCommandByTile(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ) == CMD_ERROR)
       
    12 		return false;
       
    13 	DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
       
    14 	return true;
       
    15 }
       
    16 
       
    17 // Build station
       
    18 //  Params:
       
    19 //    type : AI_TRAIN/AI_BUS/AI_TRUCK : indicates the type of station
       
    20 //    tile : tile where station is going to be build
       
    21 //    length : in case of AI_TRAIN: length of station
       
    22 //    numtracks : in case of AI_TRAIN: tracks of station
       
    23 //    direction : the direction of the station
       
    24 //    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
       
    25 int AiNew_Build_Station(Player *p, byte type, uint tile, byte length, byte numtracks, byte direction, byte flag) {
       
    26 	if (type == AI_TRAIN)
       
    27 		return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
       
    28 	else if (type == AI_BUS)
       
    29 		return DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_BUS_STATION);
       
    30 	else
       
    31 		return DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRUCK_STATION);
       
    32 }
       
    33 
       
    34 // Builds a brdige. The second best out of the ones available for this player
       
    35 //  Params:
       
    36 //   tile_a : starting point
       
    37 //   tile_b : end point
       
    38 //   flag : flag passed to DoCommand
       
    39 int AiNew_Build_Bridge(Player *p, uint tile_a, uint tile_b, byte flag) {
       
    40 	int bridge_type, bridge_len, type, type2;
       
    41 
       
    42 	// Find a good bridgetype (the best money can buy)
       
    43 	bridge_len = GetBridgeLength(tile_a, tile_b);
       
    44 	type = type2 = 0;
       
    45 	for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) {
       
    46 		if (CheckBridge_Stuff(bridge_type, bridge_len)) {
       
    47 			type2 = type;
       
    48 			type = bridge_type;
       
    49 			// We found two bridges, exit
       
    50 			if (type2 != 0)
       
    51 				break;
       
    52 		}
       
    53 	}
       
    54 	// There is only one bridge that can be build..
       
    55 	if (type2 == 0 && type != 0) type2 = type;
       
    56 
       
    57 	// Now, simply, build the bridge!
       
    58 	if (p->ainew.tbt == AI_TRAIN)
       
    59 		return DoCommandByTile(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
       
    60 	else
       
    61 		return DoCommandByTile(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
       
    62 }
       
    63 
       
    64 
       
    65 // Build the route part by part
       
    66 // Basicly what this function do, is build that amount of parts of the route
       
    67 //  that go in the same direction. It sets 'part' to the last part of the route builded.
       
    68 //  The return value is the cost for the builded parts
       
    69 //
       
    70 //  Params:
       
    71 //   PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder
       
    72 //   part : Which part we need to build
       
    73 //
       
    74 // TODO: skip already builded road-pieces (e.g.: cityroad)
       
    75 int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag) {
       
    76     int part = PathFinderInfo->position;
       
    77 	byte *route_extra = PathFinderInfo->route_extra;
       
    78 	TileIndex *route = PathFinderInfo->route;
       
    79 	int dir;
       
    80 	int old_dir = -1;
       
    81 	int cost = 0;
       
    82 	int res;
       
    83 	// We need to calculate the direction with the parent of the parent.. so we skip
       
    84 	//  the first pieces and the last piece
       
    85 	if (part < 1) part = 1;
       
    86 	// When we are done, stop it
       
    87 	if (part >= PathFinderInfo->route_length - 1) { PathFinderInfo->position = -2; return 0; }
       
    88 	
       
    89 	
       
    90 	if (PathFinderInfo->rail_or_road) {
       
    91 		// Tunnel code
       
    92      	if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
       
    93      		cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
       
    94      		PathFinderInfo->position++;
       
    95      		// TODO: problems!
       
    96      		if (cost == CMD_ERROR) {
       
    97      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
       
    98 				return 0;
       
    99      		}
       
   100      		return cost;
       
   101      	}
       
   102      	// Bridge code
       
   103      	if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
       
   104      		cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag);
       
   105      		PathFinderInfo->position++;
       
   106      		// TODO: problems!
       
   107      		if (cost == CMD_ERROR) {
       
   108      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
       
   109 				return 0;
       
   110      		}
       
   111      		return cost;
       
   112      	}
       
   113 
       
   114      	// Build normal rail
       
   115      	// Keep it doing till we go an other way
       
   116      	if (route_extra[part-1] == 0 && route_extra[part] == 0) {
       
   117      		while (route_extra[part] == 0) {
       
   118 	     		// Get the current direction
       
   119 	     		dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]);
       
   120 	     		// Is it the same as the last one?
       
   121 	     		if (old_dir != -1 && old_dir != dir) break;
       
   122 	     		old_dir = dir;
       
   123 	     		// Build the tile
       
   124 	     		res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
       
   125 	     		if (res == CMD_ERROR) {
       
   126 	     			// Problem.. let's just abort it all!
       
   127 	     			p->ainew.state = AI_STATE_NOTHING;
       
   128 	     			return 0;
       
   129 	     		}
       
   130 	     		cost += res;
       
   131 	     		// Go to the next tile
       
   132 	     		part++;
       
   133 	     		// Check if it is still in range..
       
   134 	     		if (part >= PathFinderInfo->route_length - 1) break;
       
   135 	     	}
       
   136 	     	part--;
       
   137 	    }
       
   138      	// We want to return the last position, so we go back one
       
   139      	PathFinderInfo->position = part;
       
   140     } else {
       
   141 		// Tunnel code
       
   142      	if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
       
   143      		cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
       
   144      		PathFinderInfo->position++;
       
   145      		// TODO: problems!
       
   146      		if (cost == CMD_ERROR) {
       
   147      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
       
   148 				return 0;
       
   149      		}
       
   150      		return cost;
       
   151      	}
       
   152      	// Bridge code
       
   153      	if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
       
   154      		cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag);
       
   155      		PathFinderInfo->position++;
       
   156      		// TODO: problems!
       
   157      		if (cost == CMD_ERROR) {
       
   158      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
       
   159 				return 0;
       
   160      		}
       
   161      		return cost;
       
   162      	}
       
   163 
       
   164      	// Build normal road
       
   165      	// Keep it doing till we go an other way
       
   166      	// EnsureNoVehicle makes sure we don't build on a tile where a vehicle is. This way
       
   167      	//  it will wait till the vehicle is gone..
       
   168      	if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
       
   169      		while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
       
   170 	     		// Get the current direction
       
   171 	     		dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]);
       
   172 	     		// Is it the same as the last one?
       
   173 	     		if (old_dir != -1 && old_dir != dir) break;
       
   174 	     		old_dir = dir;
       
   175 	     		// There is already some road, and it is a bridge.. don't build!!!
       
   176 	     		if (!IS_TILETYPE(route[part], MP_TUNNELBRIDGE)) {
       
   177 	     			// Build the tile
       
   178 	     			res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
       
   179 	     			// Currently, we ignore CMD_ERRORs!
       
   180 	     			if (res == CMD_ERROR && !IS_TILETYPE(route[part], MP_STREET) && (flag == DC_EXEC && !EnsureNoVehicle(route[part]))) {
       
   181      					// Problem.. let's just abort it all!
       
   182      					DEBUG(ai,0)("Darn, the route could not be builded.. aborting!");
       
   183     	     			p->ainew.state = AI_STATE_NOTHING;
       
   184     	     			return 0;
       
   185     	     		} else {
       
   186     	     			if (res != CMD_ERROR)
       
   187     	     				cost += res;
       
   188     	     		}
       
   189 		     	}
       
   190 	     		// Go to the next tile
       
   191 	     		part++;
       
   192 	     		// Check if it is still in range..
       
   193 	     		if (part >= PathFinderInfo->route_length - 1) break;
       
   194 	     	}
       
   195 	     	part--;
       
   196 	     	// We want to return the last position, so we go back one
       
   197 	    }
       
   198 	    if (!EnsureNoVehicle(route[part]) && flag == DC_EXEC) part--;
       
   199      	PathFinderInfo->position = part;
       
   200     }
       
   201     
       
   202     return cost;
       
   203 }
       
   204 
       
   205 // This functions tries to find the best vehicle for this type of cargo
       
   206 // It returns vehicle_id or -1 if not found
       
   207 int AiNew_PickVehicle(Player *p) {
       
   208     if (p->ainew.tbt == AI_TRAIN) {
       
   209         // Not supported yet
       
   210         return -1;
       
   211     } else {
       
   212         int start, count, i, r = CMD_ERROR;
       
   213         start = _cargoc.ai_roadveh_start[p->ainew.cargo];
       
   214         count = _cargoc.ai_roadveh_count[p->ainew.cargo];
       
   215 
       
   216         // Let's check it backwards.. we simply want to best engine available..
       
   217         for (i=start+count-1;i>=start;i--) {
       
   218         	// Is it availiable?
       
   219         	// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
       
   220         	if (!HASBIT(_engines[i].player_avail, _current_player) || _engines[i].reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
       
   221         	// Can we build it?
       
   222         	r = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
       
   223         	if (r != CMD_ERROR) break;
       
   224        	}
       
   225        	// We did not find a vehicle :(
       
   226        	if (r == CMD_ERROR) { return -1; }
       
   227        	return i;
       
   228     }
       
   229 }
       
   230 
       
   231 // Builds the best vehicle possible
       
   232 int AiNew_Build_Vehicle(Player *p, uint tile, byte flag) {
       
   233 	int i = AiNew_PickVehicle(p);
       
   234 	if (i == -1) return CMD_ERROR;
       
   235 	
       
   236 	if (p->ainew.tbt == AI_TRAIN) {
       
   237 		return CMD_ERROR;
       
   238 	} else {
       
   239 		return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
       
   240 	}
       
   241 }
       
   242 
       
   243 int AiNew_Build_Depot(Player *p, uint tile, byte direction, byte flag) {
       
   244 	static const byte _roadbits_by_dir[4] = {2,1,8,4};
       
   245 	int r, r2;
       
   246     if (p->ainew.tbt == AI_TRAIN) {
       
   247     	return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
       
   248     } else {
       
   249     	r = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
       
   250     	if (r == CMD_ERROR) return r;
       
   251     	// Try to build the road from the depot
       
   252     	r2 = DoCommandByTile(tile + _tileoffs_by_dir[direction], _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
       
   253     	// If it fails, ignore it..
       
   254     	if (r2 == CMD_ERROR) return r;
       
   255     	return r + r2;
       
   256     }
       
   257 }