|
@@ -61,25 +61,25 @@ CommandCost AiNew_Build_Bridge(Player *p
|
|
|
for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) {
|
|
|
if (CheckBridge_Stuff(bridge_type, bridge_len)) {
|
|
|
type2 = type;
|
|
|
type = bridge_type;
|
|
|
// We found two bridges, exit
|
|
|
if (type2 != 0) break;
|
|
|
}
|
|
|
}
|
|
|
// There is only one bridge that can be built
|
|
|
if (type2 == 0 && type != 0) type2 = type;
|
|
|
|
|
|
// Now, simply, build the bridge!
|
|
|
if (p->ainew.tbt == AI_TRAIN) {
|
|
|
if (_players_ainew[p->index].tbt == AI_TRAIN) {
|
|
|
return AI_DoCommand(tile_a, tile_b, (0x00 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
|
|
|
} else {
|
|
|
return AI_DoCommand(tile_a, tile_b, ((0x80 | ROADTYPES_ROAD) << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// Build the route part by part
|
|
|
// Basicly what this function do, is build that amount of parts of the route
|
|
|
// that go in the same direction. It sets 'part' to the last part of the route builded.
|
|
|
// The return value is the cost for the builded parts
|
|
|
//
|
|
@@ -135,25 +135,25 @@ CommandCost AiNew_Build_RoutePart(Player
|
|
|
// 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 = AI_DoCommand(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;
|
|
|
_players_ainew[p->index].state = AI_STATE_NOTHING;
|
|
|
return CommandCost();
|
|
|
}
|
|
|
cost.AddCost(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;
|
|
@@ -191,25 +191,25 @@ CommandCost AiNew_Build_RoutePart(Player
|
|
|
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 = AI_DoCommand(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_ROAD) && !EnsureNoVehicleOnGround(route[part])) {
|
|
|
// Problem.. let's just abort it all!
|
|
|
DEBUG(ai, 0, "[BuidPath] route building failed at tile 0x%X, aborting", route[part]);
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
_players_ainew[p->index].state = AI_STATE_NOTHING;
|
|
|
return CommandCost();
|
|
|
}
|
|
|
|
|
|
if (CmdSucceeded(res)) cost.AddCost(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
|
|
@@ -217,43 +217,43 @@ CommandCost AiNew_Build_RoutePart(Player
|
|
|
if (!EnsureNoVehicleOnGround(route[part]) && flag == DC_EXEC) part--;
|
|
|
PathFinderInfo->position = part;
|
|
|
}
|
|
|
|
|
|
return cost;
|
|
|
}
|
|
|
|
|
|
|
|
|
// This functions tries to find the best vehicle for this type of cargo
|
|
|
// It returns INVALID_ENGINE if not suitable engine is found
|
|
|
EngineID AiNew_PickVehicle(Player *p)
|
|
|
{
|
|
|
if (p->ainew.tbt == AI_TRAIN) {
|
|
|
if (_players_ainew[p->index].tbt == AI_TRAIN) {
|
|
|
// Not supported yet
|
|
|
return INVALID_ENGINE;
|
|
|
} else {
|
|
|
EngineID best_veh_index = INVALID_ENGINE;
|
|
|
int32 best_veh_rating = 0;
|
|
|
EngineID start = ROAD_ENGINES_INDEX;
|
|
|
EngineID end = ROAD_ENGINES_INDEX + NUM_ROAD_ENGINES;
|
|
|
EngineID i;
|
|
|
|
|
|
/* Loop through all road vehicles */
|
|
|
for (i = start; i != end; i++) {
|
|
|
const RoadVehicleInfo *rvi = RoadVehInfo(i);
|
|
|
const Engine* e = GetEngine(i);
|
|
|
int32 rating;
|
|
|
CommandCost ret;
|
|
|
|
|
|
/* Skip vehicles which can't take our cargo type */
|
|
|
if (rvi->cargo_type != p->ainew.cargo && !CanRefitTo(i, p->ainew.cargo)) continue;
|
|
|
if (rvi->cargo_type != _players_ainew[p->index].cargo && !CanRefitTo(i, _players_ainew[p->index].cargo)) continue;
|
|
|
|
|
|
// Is it availiable?
|
|
|
// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
|
|
|
if (!HasBit(e->player_avail, _current_player) || e->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
|
|
|
|
|
|
/* Rate and compare the engine by speed & capacity */
|
|
|
rating = rvi->max_speed * rvi->capacity;
|
|
|
if (rating <= best_veh_rating) continue;
|
|
|
|
|
|
// Can we build it?
|
|
|
ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
|
|
|
if (CmdFailed(ret)) continue;
|
|
@@ -263,61 +263,61 @@ EngineID AiNew_PickVehicle(Player *p)
|
|
|
}
|
|
|
|
|
|
return best_veh_index;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2)
|
|
|
{
|
|
|
Player* p = GetPlayer(_current_player);
|
|
|
|
|
|
if (success) {
|
|
|
p->ainew.state = AI_STATE_GIVE_ORDERS;
|
|
|
p->ainew.veh_id = _new_vehicle_id;
|
|
|
_players_ainew[p->index].state = AI_STATE_GIVE_ORDERS;
|
|
|
_players_ainew[p->index].veh_id = _new_vehicle_id;
|
|
|
|
|
|
if (GetVehicle(p->ainew.veh_id)->cargo_type != p->ainew.cargo) {
|
|
|
if (GetVehicle(_players_ainew[p->index].veh_id)->cargo_type != _players_ainew[p->index].cargo) {
|
|
|
/* Cargo type doesn't match, so refit it */
|
|
|
if (CmdFailed(DoCommand(tile, p->ainew.veh_id, p->ainew.cargo, DC_EXEC, CMD_REFIT_ROAD_VEH))) {
|
|
|
if (CmdFailed(DoCommand(tile, _players_ainew[p->index].veh_id, _players_ainew[p->index].cargo, DC_EXEC, CMD_REFIT_ROAD_VEH))) {
|
|
|
/* Refit failed, so sell the vehicle */
|
|
|
DoCommand(tile, p->ainew.veh_id, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
DoCommand(tile, _players_ainew[p->index].veh_id, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
|
|
|
_players_ainew[p->index].state = AI_STATE_NOTHING;
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
/* XXX this should be handled more gracefully */
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
_players_ainew[p->index].state = AI_STATE_NOTHING;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// Builds the best vehicle possible
|
|
|
CommandCost AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
|
|
|
{
|
|
|
EngineID i = AiNew_PickVehicle(p);
|
|
|
|
|
|
if (i == INVALID_ENGINE) return CMD_ERROR;
|
|
|
if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
|
|
|
if (_players_ainew[p->index].tbt == AI_TRAIN) return CMD_ERROR;
|
|
|
|
|
|
if (flag & DC_EXEC) {
|
|
|
return AI_DoCommandCc(tile, i, 0, flag, CMD_BUILD_ROAD_VEH, CcAI);
|
|
|
} else {
|
|
|
return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
CommandCost AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag)
|
|
|
{
|
|
|
CommandCost ret, ret2;
|
|
|
if (p->ainew.tbt == AI_TRAIN) {
|
|
|
if (_players_ainew[p->index].tbt == AI_TRAIN) {
|
|
|
return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
|
|
|
} else {
|
|
|
ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
|
|
|
if (CmdFailed(ret2)) return ret;
|
|
|
// Try to build the road from the depot
|
|
|
ret2 = AI_DoCommand(tile + TileOffsByDiagDir(direction), DiagDirToRoadBits(ReverseDiagDir(direction)), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
|
|
|
// If it fails, ignore it..
|
|
|
if (CmdFailed(ret2)) return ret;
|
|
|
ret.AddCost(ret2);
|
|
|
return ret;
|
|
|
}
|
|
|
}
|