ai_build.c
changeset 1713 659ca3025cc5
parent 1299 39c06aba09aa
child 1891 862800791170
equal deleted inserted replaced
1712:2d7148a0479f 1713:659ca3025cc5
    10 
    10 
    11 // Build HQ
    11 // Build HQ
    12 //  Params:
    12 //  Params:
    13 //    tile : tile where HQ is going to be build
    13 //    tile : tile where HQ is going to be build
    14 bool AiNew_Build_CompanyHQ(Player *p, uint tile) {
    14 bool AiNew_Build_CompanyHQ(Player *p, uint tile) {
    15 	if (DoCommandByTile(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ) == CMD_ERROR)
    15 	if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ)))
    16 		return false;
    16 		return false;
    17 	DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
    17 	DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
    18 	return true;
    18 	return true;
    19 }
    19 }
    20 
    20 
    27 //    direction : the direction of the station
    27 //    direction : the direction of the station
    28 //    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
    28 //    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
    29 int AiNew_Build_Station(Player *p, byte type, uint tile, byte length, byte numtracks, byte direction, byte flag) {
    29 int AiNew_Build_Station(Player *p, byte type, uint tile, byte length, byte numtracks, byte direction, byte flag) {
    30 	if (type == AI_TRAIN)
    30 	if (type == AI_TRAIN)
    31 		return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
    31 		return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
    32 	else if (type == AI_BUS)
    32 
       
    33 	if (type == AI_BUS)
    33 		return DoCommandByTile(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
    34 		return DoCommandByTile(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
    34 	else
    35 
    35 		return DoCommandByTile(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
    36 	return DoCommandByTile(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
    36 }
    37 }
    37 
    38 
    38 // Builds a brdige. The second best out of the ones available for this player
    39 // Builds a brdige. The second best out of the ones available for this player
    39 //  Params:
    40 //  Params:
    40 //   tile_a : starting point
    41 //   tile_a : starting point
    59 	if (type2 == 0 && type != 0) type2 = type;
    60 	if (type2 == 0 && type != 0) type2 = type;
    60 
    61 
    61 	// Now, simply, build the bridge!
    62 	// Now, simply, build the bridge!
    62 	if (p->ainew.tbt == AI_TRAIN)
    63 	if (p->ainew.tbt == AI_TRAIN)
    63 		return DoCommandByTile(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    64 		return DoCommandByTile(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    64 	else
    65 
    65 		return DoCommandByTile(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    66 	return DoCommandByTile(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    66 }
    67 }
    67 
    68 
    68 
    69 
    69 // Build the route part by part
    70 // Build the route part by part
    70 // Basicly what this function do, is build that amount of parts of the route
    71 // Basicly what this function do, is build that amount of parts of the route
    95 		// Tunnel code
    96 		// Tunnel code
    96      	if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
    97      	if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
    97      		cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
    98      		cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
    98      		PathFinderInfo->position++;
    99      		PathFinderInfo->position++;
    99      		// TODO: problems!
   100      		// TODO: problems!
   100      		if (cost == CMD_ERROR) {
   101      		if (CmdFailed(cost)) {
   101      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
   102      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
   102 				return 0;
   103 				return 0;
   103      		}
   104      		}
   104      		return cost;
   105      		return cost;
   105      	}
   106      	}
   106      	// Bridge code
   107      	// Bridge code
   107      	if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
   108      	if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
   108      		cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag);
   109      		cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag);
   109      		PathFinderInfo->position++;
   110      		PathFinderInfo->position++;
   110      		// TODO: problems!
   111      		// TODO: problems!
   111      		if (cost == CMD_ERROR) {
   112      		if (CmdFailed(cost)) {
   112      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
   113      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
   113 				return 0;
   114 				return 0;
   114      		}
   115      		}
   115      		return cost;
   116      		return cost;
   116      	}
   117      	}
   124 	     		// Is it the same as the last one?
   125 	     		// Is it the same as the last one?
   125 	     		if (old_dir != -1 && old_dir != dir) break;
   126 	     		if (old_dir != -1 && old_dir != dir) break;
   126 	     		old_dir = dir;
   127 	     		old_dir = dir;
   127 	     		// Build the tile
   128 	     		// Build the tile
   128 	     		res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
   129 	     		res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
   129 	     		if (res == CMD_ERROR) {
   130 	     		if (CmdFailed(res)) {
   130 	     			// Problem.. let's just abort it all!
   131 	     			// Problem.. let's just abort it all!
   131 	     			p->ainew.state = AI_STATE_NOTHING;
   132 	     			p->ainew.state = AI_STATE_NOTHING;
   132 	     			return 0;
   133 	     			return 0;
   133 	     		}
   134 	     		}
   134 	     		cost += res;
   135 	     		cost += res;
   145 		// Tunnel code
   146 		// Tunnel code
   146      	if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
   147      	if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
   147      		cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
   148      		cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
   148      		PathFinderInfo->position++;
   149      		PathFinderInfo->position++;
   149      		// TODO: problems!
   150      		// TODO: problems!
   150      		if (cost == CMD_ERROR) {
   151      		if (CmdFailed(cost)) {
   151      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
   152      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
   152 				return 0;
   153 				return 0;
   153      		}
   154      		}
   154      		return cost;
   155      		return cost;
   155      	}
   156      	}
   156      	// Bridge code
   157      	// Bridge code
   157      	if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
   158      	if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
   158      		cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag);
   159      		cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag);
   159      		PathFinderInfo->position++;
   160      		PathFinderInfo->position++;
   160      		// TODO: problems!
   161      		// TODO: problems!
   161      		if (cost == CMD_ERROR) {
   162      		if (CmdFailed(cost)) {
   162      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
   163      			DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
   163 				return 0;
   164 				return 0;
   164      		}
   165      		}
   165      		return cost;
   166      		return cost;
   166      	}
   167      	}
   179 	     		// There is already some road, and it is a bridge.. don't build!!!
   180 	     		// There is already some road, and it is a bridge.. don't build!!!
   180 	     		if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
   181 	     		if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
   181 	     			// Build the tile
   182 	     			// Build the tile
   182 	     			res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
   183 	     			res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
   183 	     			// Currently, we ignore CMD_ERRORs!
   184 	     			// Currently, we ignore CMD_ERRORs!
   184 	     			if (res == CMD_ERROR && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) {
   185 						if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) {
   185      					// Problem.. let's just abort it all!
   186 							// Problem.. let's just abort it all!
   186      					DEBUG(ai,0)("Darn, the route could not be builded.. aborting!");
   187 							DEBUG(ai,0)("Darn, the route could not be builded.. aborting!");
   187     	     			p->ainew.state = AI_STATE_NOTHING;
   188 							p->ainew.state = AI_STATE_NOTHING;
   188     	     			return 0;
   189 							return 0;
   189     	     		} else {
   190 						}
   190     	     			if (res != CMD_ERROR)
   191 
   191     	     				cost += res;
   192     	     	if (!CmdFailed(res)) cost += res;
   192     	     		}
       
   193 		     	}
   193 		     	}
   194 	     		// Go to the next tile
   194 	     		// Go to the next tile
   195 	     		part++;
   195 	     		part++;
   196 	     		// Check if it is still in range..
   196 	     		// Check if it is still in range..
   197 	     		if (part >= PathFinderInfo->route_length - 1) break;
   197 	     		if (part >= PathFinderInfo->route_length - 1) break;
   211 int AiNew_PickVehicle(Player *p) {
   211 int AiNew_PickVehicle(Player *p) {
   212     if (p->ainew.tbt == AI_TRAIN) {
   212     if (p->ainew.tbt == AI_TRAIN) {
   213         // Not supported yet
   213         // Not supported yet
   214         return -1;
   214         return -1;
   215     } else {
   215     } else {
   216         int start, count, i, r = CMD_ERROR;
   216         int start, count, i, ret = CMD_ERROR;
   217         start = _cargoc.ai_roadveh_start[p->ainew.cargo];
   217         start = _cargoc.ai_roadveh_start[p->ainew.cargo];
   218         count = _cargoc.ai_roadveh_count[p->ainew.cargo];
   218         count = _cargoc.ai_roadveh_count[p->ainew.cargo];
   219 
   219 
   220         // Let's check it backwards.. we simply want to best engine available..
   220         // Let's check it backwards.. we simply want to best engine available..
   221         for (i=start+count-1;i>=start;i--) {
   221         for (i=start+count-1;i>=start;i--) {
   222         	// Is it availiable?
   222         	// Is it availiable?
   223         	// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
   223         	// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
   224         	if (!HASBIT(_engines[i].player_avail, _current_player) || _engines[i].reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
   224         	if (!HASBIT(_engines[i].player_avail, _current_player) || _engines[i].reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
   225         	// Can we build it?
   225         	// Can we build it?
   226         	r = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
   226         	ret = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
   227         	if (r != CMD_ERROR) break;
   227         	if (!CmdFailed(ret)) break;
   228        	}
   228        	}
   229        	// We did not find a vehicle :(
   229        	// We did not find a vehicle :(
   230        	if (r == CMD_ERROR) { return -1; }
   230        	if (CmdFailed(ret)) { return -1; }
   231        	return i;
   231        	return i;
   232     }
   232     }
   233 }
   233 }
   234 
   234 
   235 // Builds the best vehicle possible
   235 // Builds the best vehicle possible
   236 int AiNew_Build_Vehicle(Player *p, uint tile, byte flag) {
   236 int AiNew_Build_Vehicle(Player *p, uint tile, byte flag) {
   237 	int i = AiNew_PickVehicle(p);
   237 	int i = AiNew_PickVehicle(p);
   238 	if (i == -1) return CMD_ERROR;
   238 	if (i == -1) return CMD_ERROR;
   239 
   239 
   240 	if (p->ainew.tbt == AI_TRAIN) {
   240 	if (p->ainew.tbt == AI_TRAIN)
   241 		return CMD_ERROR;
   241 		return CMD_ERROR;
   242 	} else {
   242 
   243 		return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
   243 	return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
   244 	}
   244 }
   245 }
   245 
   246 
   246 int AiNew_Build_Depot(Player *p, uint tile, byte direction, byte flag)
   247 int AiNew_Build_Depot(Player *p, uint tile, byte direction, byte flag) {
   247 {
   248 	static const byte _roadbits_by_dir[4] = {2,1,8,4};
   248 	static const byte _roadbits_by_dir[4] = {2,1,8,4};
   249 	int r, r2;
   249 	int ret, ret2;
   250     if (p->ainew.tbt == AI_TRAIN) {
   250     if (p->ainew.tbt == AI_TRAIN)
   251     	return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
   251     	return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
   252     } else {
   252 
   253     	r = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
   253 		ret = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
   254     	if (r == CMD_ERROR) return r;
   254 		if (CmdFailed(ret)) return ret;
   255     	// Try to build the road from the depot
   255 		// Try to build the road from the depot
   256     	r2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
   256 		ret2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
   257     	// If it fails, ignore it..
   257 		// If it fails, ignore it..
   258     	if (r2 == CMD_ERROR) return r;
   258 		if (CmdFailed(ret2)) return ret;
   259     	return r + r2;
   259 		return ret + ret2;
   260     }
   260 }
   261 }