|
@@ -67,13 +67,13 @@ CommandCost AiNew_Build_Bridge(Player *p
|
|
|
}
|
|
|
}
|
|
|
// 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);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -141,13 +141,13 @@ CommandCost AiNew_Build_RoutePart(Player
|
|
|
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..
|
|
@@ -197,13 +197,13 @@ CommandCost AiNew_Build_RoutePart(Player
|
|
|
// 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
|
|
@@ -223,13 +223,13 @@ CommandCost AiNew_Build_RoutePart(Player
|
|
|
|
|
|
|
|
|
// 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;
|
|
@@ -241,13 +241,13 @@ EngineID AiNew_PickVehicle(Player *p)
|
|
|
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 */
|
|
@@ -269,49 +269,49 @@ EngineID AiNew_PickVehicle(Player *p)
|
|
|
|
|
|
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);
|