ai/trolly/build.c
changeset 2682 7fa4b202b9f0
parent 2639 eeaefdabfdfd
child 3033 9cba043eb38f
equal deleted inserted replaced
2681:d7376b436f9c 2682:7fa4b202b9f0
     9 #include "../../command.h"
     9 #include "../../command.h"
    10 #include "trolly.h"
    10 #include "trolly.h"
    11 #include "../../engine.h"
    11 #include "../../engine.h"
    12 #include "../../station.h"
    12 #include "../../station.h"
    13 #include "../../variables.h"
    13 #include "../../variables.h"
       
    14 #include "../ai.h"
    14 
    15 
    15 // Build HQ
    16 // Build HQ
    16 //  Params:
    17 //  Params:
    17 //    tile : tile where HQ is going to be build
    18 //    tile : tile where HQ is going to be build
    18 bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile)
    19 bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile)
    19 {
    20 {
    20 	if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ)))
    21 	if (CmdFailed(AI_DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ)))
    21 		return false;
    22 		return false;
    22 	DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
    23 	AI_DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
    23 	return true;
    24 	return true;
    24 }
    25 }
    25 
    26 
    26 
    27 
    27 // Build station
    28 // Build station
    33 //    direction : the direction of the station
    34 //    direction : the direction of the station
    34 //    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
    35 //    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
    35 int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
    36 int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
    36 {
    37 {
    37 	if (type == AI_TRAIN)
    38 	if (type == AI_TRAIN)
    38 		return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
    39 		return AI_DoCommand(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
    39 
    40 
    40 	if (type == AI_BUS)
    41 	if (type == AI_BUS)
    41 		return DoCommandByTile(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
    42 		return AI_DoCommand(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
    42 
    43 
    43 	return DoCommandByTile(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
    44 	return AI_DoCommand(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
    44 }
    45 }
    45 
    46 
    46 
    47 
    47 // Builds a brdige. The second best out of the ones available for this player
    48 // Builds a brdige. The second best out of the ones available for this player
    48 //  Params:
    49 //  Params:
    67 	// There is only one bridge that can be build..
    68 	// There is only one bridge that can be build..
    68 	if (type2 == 0 && type != 0) type2 = type;
    69 	if (type2 == 0 && type != 0) type2 = type;
    69 
    70 
    70 	// Now, simply, build the bridge!
    71 	// Now, simply, build the bridge!
    71 	if (p->ainew.tbt == AI_TRAIN)
    72 	if (p->ainew.tbt == AI_TRAIN)
    72 		return DoCommandByTile(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    73 		return AI_DoCommand(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    73 
    74 
    74 	return DoCommandByTile(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    75 	return AI_DoCommand(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    75 }
    76 }
    76 
    77 
    77 
    78 
    78 // Build the route part by part
    79 // Build the route part by part
    79 // Basicly what this function do, is build that amount of parts of the route
    80 // Basicly what this function do, is build that amount of parts of the route
   102 
   103 
   103 
   104 
   104 	if (PathFinderInfo->rail_or_road) {
   105 	if (PathFinderInfo->rail_or_road) {
   105 		// Tunnel code
   106 		// Tunnel code
   106 		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
   107 		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
   107 			cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
   108 			cost += AI_DoCommand(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
   108 			PathFinderInfo->position++;
   109 			PathFinderInfo->position++;
   109 			// TODO: problems!
   110 			// TODO: problems!
   110 			if (CmdFailed(cost)) {
   111 			if (CmdFailed(cost)) {
   111 				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
   112 				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
   112 				return 0;
   113 				return 0;
   133 				dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]);
   134 				dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]);
   134 				// Is it the same as the last one?
   135 				// Is it the same as the last one?
   135 				if (old_dir != -1 && old_dir != dir) break;
   136 				if (old_dir != -1 && old_dir != dir) break;
   136 				old_dir = dir;
   137 				old_dir = dir;
   137 				// Build the tile
   138 				// Build the tile
   138 				res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
   139 				res = AI_DoCommand(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
   139 				if (CmdFailed(res)) {
   140 				if (CmdFailed(res)) {
   140 					// Problem.. let's just abort it all!
   141 					// Problem.. let's just abort it all!
   141 					p->ainew.state = AI_STATE_NOTHING;
   142 					p->ainew.state = AI_STATE_NOTHING;
   142 					return 0;
   143 					return 0;
   143 				}
   144 				}
   152 		// We want to return the last position, so we go back one
   153 		// We want to return the last position, so we go back one
   153 		PathFinderInfo->position = part;
   154 		PathFinderInfo->position = part;
   154 	} else {
   155 	} else {
   155 		// Tunnel code
   156 		// Tunnel code
   156 		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
   157 		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
   157 			cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
   158 			cost += AI_DoCommand(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
   158 			PathFinderInfo->position++;
   159 			PathFinderInfo->position++;
   159 			// TODO: problems!
   160 			// TODO: problems!
   160 			if (CmdFailed(cost)) {
   161 			if (CmdFailed(cost)) {
   161 				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
   162 				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
   162 				return 0;
   163 				return 0;
   187 				if (old_dir != -1 && old_dir != dir) break;
   188 				if (old_dir != -1 && old_dir != dir) break;
   188 				old_dir = dir;
   189 				old_dir = dir;
   189 				// There is already some road, and it is a bridge.. don't build!!!
   190 				// There is already some road, and it is a bridge.. don't build!!!
   190 				if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
   191 				if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
   191 					// Build the tile
   192 					// Build the tile
   192 					res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
   193 					res = AI_DoCommand(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
   193 					// Currently, we ignore CMD_ERRORs!
   194 					// Currently, we ignore CMD_ERRORs!
   194 					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) {
   195 					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) {
   195 						// Problem.. let's just abort it all!
   196 						// Problem.. let's just abort it all!
   196 						DEBUG(ai,0)("Darn, the route could not be builded.. aborting!");
   197 						DEBUG(ai,0)("Darn, the route could not be builded.. aborting!");
   197 						p->ainew.state = AI_STATE_NOTHING;
   198 						p->ainew.state = AI_STATE_NOTHING;
   232 		for (i = start + count - 1; i >= start; i--) {
   233 		for (i = start + count - 1; i >= start; i--) {
   233 			// Is it availiable?
   234 			// Is it availiable?
   234 			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
   235 			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
   235 			if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
   236 			if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
   236 			// Can we build it?
   237 			// Can we build it?
   237 			ret = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
   238 			ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
   238 			if (!CmdFailed(ret)) break;
   239 			if (!CmdFailed(ret)) break;
   239 		}
   240 		}
   240 		// We did not find a vehicle :(
   241 		// We did not find a vehicle :(
   241 		if (CmdFailed(ret)) return -1;
   242 		if (CmdFailed(ret)) return -1;
   242 		return i;
   243 		return i;
   250 	int i = AiNew_PickVehicle(p);
   251 	int i = AiNew_PickVehicle(p);
   251 	if (i == -1) return CMD_ERROR;
   252 	if (i == -1) return CMD_ERROR;
   252 
   253 
   253 	if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
   254 	if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
   254 
   255 
   255 	return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
   256 	return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
   256 }
   257 }
   257 
   258 
   258 int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag)
   259 int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag)
   259 {
   260 {
   260 	static const byte _roadbits_by_dir[4] = {2,1,8,4};
   261 	static const byte _roadbits_by_dir[4] = {2,1,8,4};
   261 	int ret, ret2;
   262 	int ret, ret2;
   262 	if (p->ainew.tbt == AI_TRAIN)
   263 	if (p->ainew.tbt == AI_TRAIN)
   263 		return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
   264 		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
   264 
   265 
   265 	ret = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
   266 	ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
   266 	if (CmdFailed(ret)) return ret;
   267 	if (CmdFailed(ret)) return ret;
   267 	// Try to build the road from the depot
   268 	// Try to build the road from the depot
   268 	ret2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
   269 	ret2 = AI_DoCommand(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
   269 	// If it fails, ignore it..
   270 	// If it fails, ignore it..
   270 	if (CmdFailed(ret2)) return ret;
   271 	if (CmdFailed(ret2)) return ret;
   271 	return ret + ret2;
   272 	return ret + ret2;
   272 }
   273 }