|
@@ -236,162 +236,165 @@ static int AiChooseTrainToReplaceWith(Pl
|
|
|
|
|
|
while (++num, u->next != NULL) {
|
|
|
u = u->next;
|
|
|
}
|
|
|
|
|
|
// XXX: check if a wagon
|
|
|
return AiChooseTrainToBuild(v->u.rail.railtype, avail_money, 0, v->tile);
|
|
|
}
|
|
|
|
|
|
static int AiChooseShipToReplaceWith(Player *p, Vehicle *v)
|
|
|
{
|
|
|
error("!AiChooseShipToReplaceWith");
|
|
|
|
|
|
/* maybe useless, but avoids compiler warning this way */
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
static void AiHandleGotoDepot(Player *p, int cmd)
|
|
|
{
|
|
|
if (p->ai.cur_veh->current_order.type != OT_GOTO_DEPOT)
|
|
|
DoCommandByTile(0, p->ai.cur_veh->index, 0, DC_EXEC, cmd);
|
|
|
|
|
|
if (++p->ai.state_counter <= 1387) {
|
|
|
p->ai.state = AIS_VEH_DO_REPLACE_VEHICLE;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (p->ai.cur_veh->current_order.type == OT_GOTO_DEPOT) {
|
|
|
p->ai.cur_veh->current_order.type = OT_DUMMY;
|
|
|
p->ai.cur_veh->current_order.flags = 0;
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, p->ai.cur_veh->index);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak)
|
|
|
{
|
|
|
int i;
|
|
|
|
|
|
for (i = 0; bak->order[i].type != OT_NOTHING; i++)
|
|
|
if (CmdFailed(DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)))
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
static void AiHandleReplaceTrain(Player *p)
|
|
|
{
|
|
|
Vehicle *v = p->ai.cur_veh;
|
|
|
BackuppedOrders orderbak[1];
|
|
|
int veh;
|
|
|
uint tile;
|
|
|
|
|
|
// wait until the vehicle reaches the depot.
|
|
|
if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
|
|
|
AiHandleGotoDepot(p, CMD_TRAIN_GOTO_DEPOT);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
veh = AiChooseTrainToReplaceWith(p, v);
|
|
|
if (veh != -1) {
|
|
|
TileIndex tile;
|
|
|
|
|
|
BackupVehicleOrders(v, orderbak);
|
|
|
tile = v->tile;
|
|
|
|
|
|
if (!CmdFailed(DoCommandByTile(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) &&
|
|
|
!CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE)) ) {
|
|
|
veh = _new_train_id;
|
|
|
AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
|
|
|
DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN);
|
|
|
|
|
|
DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static void AiHandleReplaceRoadVeh(Player *p)
|
|
|
{
|
|
|
Vehicle *v = p->ai.cur_veh;
|
|
|
BackuppedOrders orderbak[1];
|
|
|
int veh;
|
|
|
uint tile;
|
|
|
|
|
|
if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) {
|
|
|
AiHandleGotoDepot(p, CMD_SEND_ROADVEH_TO_DEPOT);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
veh = AiChooseRoadVehToReplaceWith(p, v);
|
|
|
if (veh != -1) {
|
|
|
TileIndex tile;
|
|
|
|
|
|
BackupVehicleOrders(v, orderbak);
|
|
|
tile = v->tile;
|
|
|
|
|
|
if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) &&
|
|
|
!CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH)) ) {
|
|
|
veh = _new_roadveh_id;
|
|
|
AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
|
|
|
DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
|
|
|
|
|
|
DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static void AiHandleReplaceAircraft(Player *p)
|
|
|
{
|
|
|
Vehicle *v = p->ai.cur_veh;
|
|
|
int veh;
|
|
|
BackuppedOrders orderbak[1];
|
|
|
uint tile;
|
|
|
|
|
|
if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) {
|
|
|
AiHandleGotoDepot(p, CMD_SEND_AIRCRAFT_TO_HANGAR);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
veh = AiChooseAircraftToReplaceWith(p, v);
|
|
|
if (veh != -1) {
|
|
|
TileIndex tile;
|
|
|
|
|
|
BackupVehicleOrders(v, orderbak);
|
|
|
tile = v->tile;
|
|
|
|
|
|
if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT)) &&
|
|
|
!CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT)) ) {
|
|
|
veh = _new_aircraft_id;
|
|
|
AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
|
|
|
DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
|
|
|
|
|
|
DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static void AiHandleReplaceShip(Player *p)
|
|
|
{
|
|
|
error("!AiHandleReplaceShip");
|
|
|
}
|
|
|
|
|
|
typedef int CheckReplaceProc(Player *p, Vehicle *v);
|
|
|
|
|
|
static CheckReplaceProc * const _veh_check_replace_proc[] = {
|
|
|
AiChooseTrainToReplaceWith,
|
|
|
AiChooseRoadVehToReplaceWith,
|
|
|
AiChooseShipToReplaceWith,
|
|
|
AiChooseAircraftToReplaceWith,
|
|
|
};
|
|
|
|
|
|
typedef void DoReplaceProc(Player *p);
|
|
|
static DoReplaceProc * const _veh_do_replace_proc[] = {
|
|
|
AiHandleReplaceTrain,
|
|
|
AiHandleReplaceRoadVeh,
|
|
|
AiHandleReplaceShip,
|
|
|
AiHandleReplaceAircraft
|
|
|
};
|
|
|
|
|
|
static void AiStateCheckReplaceVehicle(Player *p)
|
|
|
{
|
|
|
Vehicle *v = p->ai.cur_veh;
|
|
|
|
|
|
if (v->type == 0 || v->owner != _current_player || v->type > VEH_Ship || _veh_check_replace_proc[v->type - VEH_Train](p, v) == -1) {
|
|
|
p->ai.state = AIS_VEH_LOOP;
|
|
|
} else {
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.state = AIS_VEH_DO_REPLACE_VEHICLE;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1487,101 +1490,100 @@ static void AiWantShipRoute(Player *p)
|
|
|
static void AiStateWantNewRoute(Player *p)
|
|
|
{
|
|
|
uint16 r;
|
|
|
int i;
|
|
|
|
|
|
if (p->player_money < AiGetBasePrice(p) * 500) {
|
|
|
p->ai.state = AIS_0;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
i = 200;
|
|
|
for(;;) {
|
|
|
r = (uint16)Random();
|
|
|
|
|
|
if (_patches.ai_disable_veh_train && _patches.ai_disable_veh_roadveh &&
|
|
|
_patches.ai_disable_veh_aircraft && _patches.ai_disable_veh_ship)
|
|
|
return;
|
|
|
|
|
|
if (r < 0x7626) {
|
|
|
if (_patches.ai_disable_veh_train) continue;
|
|
|
AiWantTrainRoute(p);
|
|
|
} else if (r < 0xC4EA) {
|
|
|
if (_patches.ai_disable_veh_roadveh) continue;
|
|
|
AiWantRoadRoute(p);
|
|
|
} else if (r < 0xD89B) {
|
|
|
if (_patches.ai_disable_veh_aircraft) continue;
|
|
|
AiWantAircraftRoute(p);
|
|
|
} else {
|
|
|
if (_patches.ai_disable_veh_ship) continue;
|
|
|
AiWantShipRoute(p);
|
|
|
}
|
|
|
|
|
|
// got a route?
|
|
|
if (p->ai.state != AIS_WANT_NEW_ROUTE)
|
|
|
break;
|
|
|
|
|
|
// time out?
|
|
|
if (--i == 0) {
|
|
|
if (++p->ai.state_counter == 556) p->ai.state = AIS_0;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
|
|
|
{
|
|
|
uint values[NUM_CARGO];
|
|
|
int w,h;
|
|
|
uint tile2;
|
|
|
int rad;
|
|
|
|
|
|
for(;p->mode != 4;p++) if (p->mode == 1) {
|
|
|
tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
|
|
TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
|
|
|
|
|
w = ((p->attr>>1) & 7);
|
|
|
h = ((p->attr>>4) & 7);
|
|
|
if (p->attr&1) intswap(w, h);
|
|
|
|
|
|
|
|
|
if (_patches.modified_catchment) {
|
|
|
rad = CA_TRAIN;
|
|
|
} else {
|
|
|
rad = 4;
|
|
|
}
|
|
|
|
|
|
if (cargo & 0x80) {
|
|
|
GetProductionAroundTiles(values, tile2, w, h, rad);
|
|
|
return values[cargo & 0x7F] != 0;
|
|
|
} else {
|
|
|
GetAcceptanceAroundTiles(values, tile2, w, h, rad);
|
|
|
if (!(values[cargo] & ~7))
|
|
|
return false;
|
|
|
if (cargo != CT_MAIL)
|
|
|
return true;
|
|
|
return !!((values[cargo]>>1) & ~7);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
static int32 AiDoBuildDefaultRailTrack(TileIndex tile, const AiDefaultBlockData *p, byte flag)
|
|
|
{
|
|
|
int32 ret;
|
|
|
int32 total_cost = 0;
|
|
|
Town *t = NULL;
|
|
|
int rating = 0;
|
|
|
int i,j,k;
|
|
|
|
|
|
for(;;) {
|
|
|
// This will seldomly overflow for valid reasons. Mask it to be on the safe side.
|
|
|
uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs));
|
|
|
|
|
|
_cleared_town = NULL;
|
|
|
|
|
|
if (p->mode < 2) {
|
|
|
if (p->mode == 0) {
|
|
|
// Depot
|
|
|
ret = DoCommandByTile(c, _cur_ai_player->ai.railtype_to_use, p->attr, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_TRAIN_DEPOT);
|
|
|
} else {
|
|
|
// Station
|
|
@@ -1788,97 +1790,97 @@ static void AiStateBuildDefaultRailBlock
|
|
|
if (++p->ai.state_counter >= 1000) {
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.state_mode = -p->ai.state_mode;
|
|
|
}
|
|
|
} else if (CheckPlayerHasMoney(cost)) {
|
|
|
int32 r;
|
|
|
// player has money, build it.
|
|
|
aib->cur_building_rule = rule;
|
|
|
|
|
|
r = AiDoBuildDefaultRailTrack(
|
|
|
aib->use_tile,
|
|
|
_default_rail_track_data[rule]->data,
|
|
|
DC_EXEC | DC_NO_TOWN_RATING
|
|
|
);
|
|
|
assert(r != CMD_ERROR);
|
|
|
}
|
|
|
} while (++aib,--j);
|
|
|
} while (--i);
|
|
|
|
|
|
// check if we're done with all of them
|
|
|
aib = &p->ai.src;
|
|
|
j = p->ai.num_build_rec;
|
|
|
do {
|
|
|
if (aib->cur_building_rule == 255)
|
|
|
return;
|
|
|
} while (++aib,--j);
|
|
|
|
|
|
// yep, all are done. switch state to the rail building state.
|
|
|
p->ai.state = AIS_BUILD_RAIL;
|
|
|
p->ai.state_mode = 255;
|
|
|
}
|
|
|
|
|
|
static TileIndex AiGetEdgeOfDefaultRailBlock(byte rule, TileIndex tile, byte cmd, int *dir)
|
|
|
{
|
|
|
const AiDefaultBlockData *p = _default_rail_track_data[rule]->data;
|
|
|
|
|
|
while (p->mode != 3 || !((--cmd) & 0x80)) p++;
|
|
|
|
|
|
return tile + ToTileIndexDiff(p->tileoffs) - TileOffsByDir(*dir = p->attr);
|
|
|
}
|
|
|
|
|
|
typedef struct AiRailPathFindData {
|
|
|
TileIndex tile;
|
|
|
TileIndex tile2;
|
|
|
int count;
|
|
|
bool flag;
|
|
|
} AiRailPathFindData;
|
|
|
|
|
|
static bool AiEnumFollowTrack(uint tile, AiRailPathFindData *a, int track, uint length, byte *state)
|
|
|
static bool AiEnumFollowTrack(TileIndex tile, AiRailPathFindData *a, int track, uint length, byte *state)
|
|
|
{
|
|
|
if (a->flag)
|
|
|
return true;
|
|
|
|
|
|
if (length > 20 || tile == a->tile) {
|
|
|
a->flag = true;
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if (DistanceMax(tile, a->tile2) < 4)
|
|
|
a->count++;
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
static bool AiDoFollowTrack(Player *p)
|
|
|
{
|
|
|
AiRailPathFindData arpfd;
|
|
|
arpfd.tile = p->ai.start_tile_a;
|
|
|
arpfd.tile2 = p->ai.cur_tile_a;
|
|
|
arpfd.flag = false;
|
|
|
arpfd.count = 0;
|
|
|
FollowTrack(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, p->ai.cur_dir_a^2,
|
|
|
(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
|
|
|
return arpfd.count > 8;
|
|
|
}
|
|
|
|
|
|
typedef struct AiRailFinder {
|
|
|
TileIndex final_tile;
|
|
|
byte final_dir;
|
|
|
byte depth;
|
|
|
byte recursive_mode;
|
|
|
byte cur_best_dir;
|
|
|
byte best_dir;
|
|
|
byte cur_best_depth;
|
|
|
byte best_depth;
|
|
|
uint cur_best_dist;
|
|
|
const byte *best_ptr;
|
|
|
uint best_dist;
|
|
|
TileIndex cur_best_tile, best_tile;
|
|
|
TileIndex bridge_end_tile;
|
|
|
Player *player;
|
|
|
TileInfo ti;
|
|
|
} AiRailFinder;
|
|
|
|
|
|
static const byte _ai_table_15[4][8] = {
|
|
|
{0, 0, 4, 3, 3, 1, 128+0, 64},
|
|
|
{1, 1, 2, 0, 4, 2, 128+1, 65},
|
|
@@ -2130,97 +2132,97 @@ static void AiBuildRailConstruct(Player
|
|
|
p->ai.cur_tile_a += TileOffsByDir(p->ai.cur_dir_a);
|
|
|
|
|
|
if (arf.best_ptr[0]&0x80) {
|
|
|
int i;
|
|
|
int32 bridge_len = GetBridgeLength(arf.bridge_end_tile, p->ai.cur_tile_a);
|
|
|
|
|
|
/* Figure out what (rail)bridge type to build
|
|
|
start with best bridge, then go down to worse and worse bridges
|
|
|
unnecessary to check for worse bridge (i=0), since AI will always build that.
|
|
|
AI is so fucked up that fixing this small thing will probably not solve a thing
|
|
|
*/
|
|
|
for (i = MAX_BRIDGES - 1; i != 0; i--) {
|
|
|
if (CheckBridge_Stuff(i, bridge_len)) {
|
|
|
int32 cost = DoCommandByTile(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO, CMD_BUILD_BRIDGE);
|
|
|
if (!CmdFailed(cost) && cost < (p->player_money >> 5))
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Build it
|
|
|
DoCommandByTile(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
|
|
|
|
|
|
p->ai.cur_tile_a = arf.bridge_end_tile;
|
|
|
p->ai.state_counter = 0;
|
|
|
} else if (arf.best_ptr[0]&0x40) {
|
|
|
// tunnel
|
|
|
DoCommandByTile(p->ai.cur_tile_a, p->ai.railtype_to_use, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL);
|
|
|
p->ai.cur_tile_a = _build_tunnel_endtile;
|
|
|
p->ai.state_counter = 0;
|
|
|
} else {
|
|
|
// rail
|
|
|
p->ai.cur_dir_a = arf.best_ptr[1];
|
|
|
DoCommandByTile(p->ai.cur_tile_a, p->ai.railtype_to_use, arf.best_ptr[0],
|
|
|
DC_EXEC | DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP, CMD_BUILD_SINGLE_RAIL);
|
|
|
p->ai.state_counter = 0;
|
|
|
}
|
|
|
|
|
|
if (arf.best_tile != 0) {
|
|
|
for(i=0; i!=2; i++)
|
|
|
AiDoTerraformLand(arf.best_tile, arf.best_dir, 3, 0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static bool AiRemoveTileAndGoForward(Player *p)
|
|
|
{
|
|
|
byte b;
|
|
|
int bit;
|
|
|
const byte *ptr;
|
|
|
uint tile = p->ai.cur_tile_a;
|
|
|
TileIndex tile = p->ai.cur_tile_a;
|
|
|
int offs;
|
|
|
TileIndex tilenew;
|
|
|
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
|
|
if (!(_map5[tile] & 0x80)) {
|
|
|
// Clear the tunnel and continue at the other side of it.
|
|
|
if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
|
|
|
return false;
|
|
|
p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDir(p->ai.cur_dir_a));
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if (!(_map5[tile] & 0x40)) {
|
|
|
|
|
|
// Check if the bridge points in the right direction.
|
|
|
// This is not really needed the first place AiRemoveTileAndGoForward is called.
|
|
|
if ((_map5[tile]&1) != (p->ai.cur_dir_a&1))
|
|
|
return false;
|
|
|
|
|
|
// Find other side of bridge.
|
|
|
offs = TileOffsByDir(p->ai.cur_dir_a);
|
|
|
do {
|
|
|
tile = TILE_MASK(tile - offs);
|
|
|
} while (_map5[tile] & 0x40);
|
|
|
|
|
|
tilenew = TILE_MASK(tile - offs);
|
|
|
// And clear the bridge.
|
|
|
if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
|
|
|
return false;
|
|
|
p->ai.cur_tile_a = tilenew;
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Find the railtype at the position. Quit if no rail there.
|
|
|
b = GetRailTrackStatus(tile) & _dir_table_3[p->ai.cur_dir_a];
|
|
|
if (b == 0)
|
|
|
return false;
|
|
|
|
|
|
// Convert into a bit position that CMD_REMOVE_SINGLE_RAIL expects.
|
|
|
bit = FindFirstBit(b);
|
|
|
|
|
|
// Then remove and signals if there are any.
|
|
|
if (IsTileType(tile, MP_RAILWAY) &&
|
|
|
(_map5[tile]&0xC0) == 0x40) {
|
|
|
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_REMOVE_SIGNALS);
|
|
|
}
|
|
|
|
|
@@ -2449,97 +2451,98 @@ handle_nocash:
|
|
|
|
|
|
if (p->ai.num_want_fullload != 0)
|
|
|
p->ai.num_want_fullload--;
|
|
|
|
|
|
if (--p->ai.num_loco_to_build != 0) {
|
|
|
// p->ai.loco_id = INVALID_VEHICLE;
|
|
|
p->ai.wagon_list[0] = INVALID_VEHICLE;
|
|
|
} else {
|
|
|
p->ai.state = AIS_0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static void AiStateDeleteRailBlocks(Player *p)
|
|
|
{
|
|
|
int num;
|
|
|
AiBuildRec *aib;
|
|
|
const AiDefaultBlockData *b;
|
|
|
|
|
|
num = p->ai.num_build_rec;
|
|
|
aib = &p->ai.src;
|
|
|
do {
|
|
|
if (aib->cur_building_rule != 255) {
|
|
|
b = _default_rail_track_data[aib->cur_building_rule]->data;
|
|
|
while (b->mode != 4) {
|
|
|
DoCommandByTile(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
|
b++;
|
|
|
}
|
|
|
}
|
|
|
} while (++aib,--num);
|
|
|
|
|
|
p->ai.state = AIS_0;
|
|
|
}
|
|
|
|
|
|
static bool AiCheckRoadResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
|
|
|
{
|
|
|
uint values[NUM_CARGO];
|
|
|
int rad;
|
|
|
|
|
|
if (_patches.modified_catchment) {
|
|
|
rad = CA_TRUCK; //Same as CA_BUS at the moment?
|
|
|
} else { //change that at some point?
|
|
|
rad = 4;
|
|
|
}
|
|
|
|
|
|
for(;;p++) {
|
|
|
if (p->mode == 4) {
|
|
|
return true;
|
|
|
} else if (p->mode == 1) {
|
|
|
uint tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
|
|
TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
|
|
|
|
|
if (cargo & 0x80) {
|
|
|
GetProductionAroundTiles(values, tile2, 1, 1, rad);
|
|
|
return values[cargo & 0x7F] != 0;
|
|
|
} else {
|
|
|
GetAcceptanceAroundTiles(values, tile2, 1, 1, rad);
|
|
|
return (values[cargo]&~7) != 0;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static bool _want_road_truck_station;
|
|
|
static int32 AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag);
|
|
|
|
|
|
// Returns rule and cost
|
|
|
static int AiFindBestDefaultRoadBlock(TileIndex tile, byte direction, byte cargo, int32 *cost)
|
|
|
{
|
|
|
int i;
|
|
|
const AiDefaultRoadBlock *p;
|
|
|
|
|
|
_want_road_truck_station = (cargo & 0x7F) != CT_PASSENGERS;
|
|
|
|
|
|
for(i=0; (p = _road_default_block_data[i]) != NULL; i++) {
|
|
|
if (p->dir == direction) {
|
|
|
*cost = AiDoBuildDefaultRoadBlock(tile, p->data, 0);
|
|
|
if (*cost != CMD_ERROR && AiCheckRoadResources(tile, p->data, cargo))
|
|
|
return i;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
static int32 AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
|
|
|
{
|
|
|
int32 ret;
|
|
|
int32 total_cost = 0;
|
|
|
Town *t = NULL;
|
|
|
int rating = 0;
|
|
|
int roadflag = 0;
|
|
|
|
|
|
for(;p->mode != 4;p++) {
|
|
|
uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs));
|
|
|
|
|
|
_cleared_town = NULL;
|
|
|
|
|
|
if (p->mode == 2) {
|
|
|
if (IsTileType(c, MP_STREET) &&
|
|
@@ -2720,156 +2723,156 @@ typedef struct {
|
|
|
typedef struct AiRoadEnum {
|
|
|
TileIndex dest;
|
|
|
TileIndex best_tile;
|
|
|
int best_track;
|
|
|
uint best_dist;
|
|
|
} AiRoadEnum;
|
|
|
|
|
|
static const byte _dir_by_track[] = {
|
|
|
0,1,0,1,2,1, 0,0,
|
|
|
2,3,3,2,3,0,
|
|
|
};
|
|
|
|
|
|
static void AiBuildRoadRecursive(AiRoadFinder *arf, TileIndex tile, int dir);
|
|
|
|
|
|
static bool AiCheckRoadPathBetter(AiRoadFinder *arf, const byte *p)
|
|
|
{
|
|
|
bool better = false;
|
|
|
|
|
|
if (arf->recursive_mode < 1) {
|
|
|
// Mode is 0. This means destination has not been found yet.
|
|
|
// If the found path is shorter than the current one, remember it.
|
|
|
if (arf->cur_best_dist < arf->best_dist ||
|
|
|
(arf->cur_best_dist == arf->best_dist && arf->cur_best_depth < arf->best_depth)) {
|
|
|
arf->best_depth = arf->cur_best_depth;
|
|
|
arf->best_dist = arf->cur_best_dist;
|
|
|
arf->best_dir = arf->cur_best_dir;
|
|
|
arf->best_ptr = p;
|
|
|
arf->best_tile = arf->cur_best_tile;
|
|
|
better = true;
|
|
|
}
|
|
|
} else if (arf->recursive_mode > 1) {
|
|
|
// Mode is 2.
|
|
|
if (arf->best_dist != 0 || arf->cur_best_depth < arf->best_depth) {
|
|
|
arf->best_depth = arf->cur_best_depth;
|
|
|
arf->best_dist = 0;
|
|
|
arf->best_ptr = p;
|
|
|
arf->best_tile = 0;
|
|
|
better = true;
|
|
|
}
|
|
|
}
|
|
|
arf->recursive_mode = 0;
|
|
|
arf->cur_best_dist = (uint)-1;
|
|
|
arf->cur_best_depth = 0xff;
|
|
|
|
|
|
return better;
|
|
|
}
|
|
|
|
|
|
|
|
|
static bool AiEnumFollowRoad(uint tile, AiRoadEnum *a, int track, uint length, byte *state)
|
|
|
static bool AiEnumFollowRoad(TileIndex tile, AiRoadEnum *a, int track, uint length, byte *state)
|
|
|
{
|
|
|
uint dist = DistanceManhattan(tile, a->dest);
|
|
|
uint tile2;
|
|
|
|
|
|
if (dist <= a->best_dist) {
|
|
|
tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
|
|
|
TileIndex tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
|
|
|
|
|
|
if (IsTileType(tile2, MP_STREET) &&
|
|
|
(_map5[tile2]&0xF0) == 0) {
|
|
|
a->best_dist = dist;
|
|
|
a->best_tile = tile;
|
|
|
a->best_track = track;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
static const uint16 _ai_road_table_and[4] = {
|
|
|
0x1009,
|
|
|
0x16,
|
|
|
0x520,
|
|
|
0x2A00,
|
|
|
};
|
|
|
|
|
|
static bool AiCheckRoadFinished(Player *p)
|
|
|
{
|
|
|
AiRoadEnum are;
|
|
|
uint tile;
|
|
|
TileIndex tile;
|
|
|
int dir = p->ai.cur_dir_a;
|
|
|
uint32 bits;
|
|
|
int i;
|
|
|
|
|
|
are.dest = p->ai.cur_tile_b;
|
|
|
tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(dir));
|
|
|
|
|
|
bits = GetTileTrackStatus(tile, TRANSPORT_ROAD) & _ai_road_table_and[dir];
|
|
|
if (bits == 0) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
are.best_dist = (uint)-1;
|
|
|
|
|
|
for_each_bit(i, bits) {
|
|
|
FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, _dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
|
|
|
}
|
|
|
|
|
|
if (DistanceManhattan(tile, are.dest) <= are.best_dist)
|
|
|
return false;
|
|
|
|
|
|
if (are.best_dist == 0)
|
|
|
return true;
|
|
|
|
|
|
p->ai.cur_tile_a = are.best_tile;
|
|
|
p->ai.cur_dir_a = _dir_by_track[are.best_track];
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
static bool AiBuildRoadHelper(uint tile, int flags, int type)
|
|
|
static bool AiBuildRoadHelper(TileIndex tile, int flags, int type)
|
|
|
{
|
|
|
static const byte _road_bits[] = {
|
|
|
8+2,
|
|
|
1+4,
|
|
|
1+8,
|
|
|
4+2,
|
|
|
1+2,
|
|
|
8+4,
|
|
|
};
|
|
|
return !CmdFailed(DoCommandByTile(tile, _road_bits[type], 0, flags, CMD_BUILD_ROAD));
|
|
|
}
|
|
|
|
|
|
static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
|
|
|
{
|
|
|
TileIndex tile_new;
|
|
|
bool flag;
|
|
|
|
|
|
int dir2 = p[0] & 3;
|
|
|
|
|
|
FindLandscapeHeightByTile(&arf->ti, tile);
|
|
|
if (arf->ti.tileh == _dir_table_1[dir2] || (arf->ti.tileh==0 && arf->ti.z!=0)) {
|
|
|
tile_new = tile;
|
|
|
// Allow bridges directly over bottom tiles
|
|
|
flag = arf->ti.z == 0;
|
|
|
for(;;) {
|
|
|
if ((TileIndexDiff)tile_new < -TileOffsByDir(dir2)) return; // Wraping around map, no bridge possible!
|
|
|
tile_new = TILE_MASK(tile_new + TileOffsByDir(dir2));
|
|
|
FindLandscapeHeightByTile(&arf->ti, tile_new);
|
|
|
if (arf->ti.tileh != 0 || arf->ti.type == MP_CLEAR || arf->ti.type == MP_TREES) {
|
|
|
// Allow a bridge if either we have a tile that's water, rail or street,
|
|
|
// or if we found an up tile.
|
|
|
if (!flag) return;
|
|
|
break;
|
|
|
}
|
|
|
if (arf->ti.type != MP_WATER && arf->ti.type != MP_RAILWAY && arf->ti.type != MP_STREET)
|
|
|
return;
|
|
|
flag = true;
|
|
|
}
|
|
|
|
|
|
// Is building a (rail)bridge possible at this place (type doesn't matter)?
|
|
|
if (CmdFailed(DoCommandByTile(tile_new, tile, 0x8000, DC_AUTO, CMD_BUILD_BRIDGE)))
|
|
|
return;
|
|
|
AiBuildRoadRecursive(arf, tile_new, dir2);
|
|
|
|
|
|
// At the bottom depth, check if the new path is better than the old one.
|
|
|
if (arf->depth == 1) {
|
|
|
if (AiCheckRoadPathBetter(arf, p))
|
|
|
arf->bridge_end_tile = tile_new;
|
|
@@ -2917,97 +2920,97 @@ static void AiBuildRoadRecursive(AiRoadF
|
|
|
// Store the tile that is closest to the final position.
|
|
|
arf->cur_best_dist = dist;
|
|
|
arf->cur_best_tile = tile;
|
|
|
arf->cur_best_dir = dir;
|
|
|
arf->cur_best_depth = arf->depth;
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// Increase recursion depth
|
|
|
arf->depth++;
|
|
|
|
|
|
// Grab pointer to list of stuff that is possible to build
|
|
|
p = _ai_table_15[dir];
|
|
|
|
|
|
// Try to build a single rail in all directions.
|
|
|
FindLandscapeHeightByTile(&arf->ti, tile);
|
|
|
if (arf->ti.z == 0) {
|
|
|
p += 6;
|
|
|
} else {
|
|
|
do {
|
|
|
// Make sure that a road can be built here.
|
|
|
if (AiBuildRoadHelper(tile, DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, p[0])) {
|
|
|
AiBuildRoadRecursive(arf, tile, p[1]);
|
|
|
}
|
|
|
|
|
|
// At the bottom depth?
|
|
|
if (arf->depth == 1) {
|
|
|
AiCheckRoadPathBetter(arf, p);
|
|
|
}
|
|
|
|
|
|
p += 2;
|
|
|
} while (!(p[0]&0x80));
|
|
|
}
|
|
|
|
|
|
AiCheckBuildRoadBridgeHere(arf, tile, p);
|
|
|
AiCheckBuildRoadTunnelHere(arf, tile, p+1);
|
|
|
|
|
|
arf->depth--;
|
|
|
}
|
|
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
|
static void AiBuildRoadConstruct(Player *p)
|
|
|
{
|
|
|
AiRoadFinder arf;
|
|
|
int i;
|
|
|
uint tile;
|
|
|
TileIndex tile;
|
|
|
|
|
|
// Reached destination?
|
|
|
if (AiCheckRoadFinished(p)) {
|
|
|
p->ai.state_mode = 255;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// Setup recursive finder and call it.
|
|
|
arf.player = p;
|
|
|
arf.final_tile = p->ai.cur_tile_b;
|
|
|
arf.final_dir = p->ai.cur_dir_b;
|
|
|
arf.depth = 0;
|
|
|
arf.recursive_mode = 0;
|
|
|
arf.best_ptr = NULL;
|
|
|
arf.cur_best_dist = (uint)-1;
|
|
|
arf.cur_best_depth = 0xff;
|
|
|
arf.best_dist = (uint)-1;
|
|
|
arf.best_depth = 0xff;
|
|
|
arf.cur_best_tile = 0;
|
|
|
arf.best_tile = 0;
|
|
|
AiBuildRoadRecursive(&arf, p->ai.cur_tile_a, p->ai.cur_dir_a);
|
|
|
|
|
|
// Reached destination?
|
|
|
if (arf.recursive_mode == 2 && arf.cur_best_depth == 0) {
|
|
|
p->ai.state_mode = 255;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// Didn't find anything to build?
|
|
|
if (arf.best_ptr == NULL) {
|
|
|
// Terraform some
|
|
|
do_some_terraform:
|
|
|
for(i=0; i!=5; i++)
|
|
|
AiDoTerraformLand(p->ai.cur_tile_a, p->ai.cur_dir_a, 3, 0);
|
|
|
|
|
|
if (++p->ai.state_counter == 21) {
|
|
|
p->ai.state_mode = 1;
|
|
|
|
|
|
p->ai.cur_tile_a = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a));
|
|
|
p->ai.cur_dir_a ^= 2;
|
|
|
p->ai.state_counter = 0;
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a));
|
|
|
|
|
|
if (arf.best_ptr[0]&0x80) {
|
|
@@ -3119,169 +3122,171 @@ static void AiStateBuildRoad(Player *p)
|
|
|
|
|
|
// Now, find two new points to build between
|
|
|
num = p->ai.num_build_rec;
|
|
|
aib = &p->ai.src;
|
|
|
|
|
|
for(;;) {
|
|
|
cmd = aib->buildcmd_a;
|
|
|
aib->buildcmd_a = 255;
|
|
|
if (cmd != 255) break;
|
|
|
|
|
|
aib++;
|
|
|
if (--num == 0) {
|
|
|
p->ai.state = AIS_BUILD_ROAD_VEHICLES;
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Find first edge to build from.
|
|
|
tile = AiGetRoadBlockEdge(aib->cur_building_rule, aib->use_tile, &dir);
|
|
|
p->ai.start_tile_a = tile;
|
|
|
p->ai.cur_tile_a = tile;
|
|
|
p->ai.start_dir_a = dir;
|
|
|
p->ai.cur_dir_a = dir;
|
|
|
|
|
|
// Find second edge to build to
|
|
|
aib = (&p->ai.src) + (cmd&0xF);
|
|
|
tile = AiGetRoadBlockEdge(aib->cur_building_rule, aib->use_tile, &dir);
|
|
|
p->ai.start_tile_b = tile;
|
|
|
p->ai.cur_tile_b = tile;
|
|
|
p->ai.start_dir_b = dir;
|
|
|
p->ai.cur_dir_b = dir;
|
|
|
|
|
|
// And setup state.
|
|
|
p->ai.state_mode = 2;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.banned_tile_count = 0;
|
|
|
}
|
|
|
|
|
|
static int AiGetStationIdFromRoadBlock(TileIndex tile, int id)
|
|
|
{
|
|
|
const AiDefaultBlockData *p = _road_default_block_data[id]->data;
|
|
|
while (p->mode != 1) p++;
|
|
|
return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))];
|
|
|
}
|
|
|
|
|
|
static void AiStateBuildRoadVehicles(Player *p)
|
|
|
{
|
|
|
const AiDefaultBlockData *ptr;
|
|
|
uint tile,loco_id;
|
|
|
TileIndex tile;
|
|
|
uint loco_id;
|
|
|
int veh, i;
|
|
|
|
|
|
ptr = _road_default_block_data[p->ai.src.cur_building_rule]->data;
|
|
|
for(;ptr->mode != 0;ptr++) {}
|
|
|
tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
|
|
|
|
|
|
veh = AiChooseRoadVehToBuild(p->ai.cargo_type, p->player_money, tile);
|
|
|
if (veh == -1) {
|
|
|
p->ai.state = AIS_0;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH))) return;
|
|
|
|
|
|
loco_id = _new_roadveh_id;
|
|
|
|
|
|
for(i=0; p->ai.order_list_blocks[i] != 0xFF; i++) {
|
|
|
AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i];
|
|
|
bool is_pass = (p->ai.cargo_type == CT_PASSENGERS ||
|
|
|
p->ai.cargo_type == CT_MAIL ||
|
|
|
(_opt.landscape==LT_NORMAL && p->ai.cargo_type == CT_VALUABLES));
|
|
|
Order order;
|
|
|
|
|
|
order.type = OT_GOTO_STATION;
|
|
|
order.flags = 0;
|
|
|
order.station = AiGetStationIdFromRoadBlock(aib->use_tile, aib->cur_building_rule);
|
|
|
|
|
|
if (!is_pass && i == 1) order.flags |= OF_UNLOAD;
|
|
|
if (p->ai.num_want_fullload != 0 && (is_pass || i == 0))
|
|
|
order.flags |= OF_FULL_LOAD;
|
|
|
|
|
|
DoCommandByTile(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
|
|
|
}
|
|
|
|
|
|
DoCommandByTile(0, loco_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
|
|
|
|
|
|
DoCommandByTile(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
|
|
|
|
|
if (p->ai.num_want_fullload != 0)
|
|
|
p->ai.num_want_fullload--;
|
|
|
|
|
|
if (--p->ai.num_loco_to_build == 0) {
|
|
|
p->ai.state = AIS_0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static void AiStateDeleteRoadBlocks(Player *p)
|
|
|
{
|
|
|
int num;
|
|
|
AiBuildRec *aib;
|
|
|
const AiDefaultBlockData *b;
|
|
|
|
|
|
num = p->ai.num_build_rec;
|
|
|
aib = &p->ai.src;
|
|
|
do {
|
|
|
if (aib->cur_building_rule != 255) {
|
|
|
b = _road_default_block_data[aib->cur_building_rule]->data;
|
|
|
while (b->mode != 4) {
|
|
|
if (b->mode <= 1) {
|
|
|
DoCommandByTile(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
|
}
|
|
|
b++;
|
|
|
}
|
|
|
}
|
|
|
} while (++aib,--num);
|
|
|
|
|
|
p->ai.state = AIS_0;
|
|
|
}
|
|
|
|
|
|
static bool AiCheckIfHangar(Station *st)
|
|
|
{
|
|
|
uint tile = st->airport_tile;
|
|
|
TileIndex tile = st->airport_tile;
|
|
|
|
|
|
// HANGAR of airports
|
|
|
// 0x20 - hangar large airport (32)
|
|
|
// 0x41 - hangar small airport (65)
|
|
|
return (_map5[tile] == 32 || _map5[tile] == 65);
|
|
|
}
|
|
|
|
|
|
static void AiStateAirportStuff(Player *p)
|
|
|
{
|
|
|
Station *st;
|
|
|
byte acc_planes;
|
|
|
int i;
|
|
|
AiBuildRec *aib;
|
|
|
byte rule;
|
|
|
|
|
|
// Here we look for an airport we could use instead of building a new
|
|
|
// one. If we find such an aiport for any waypoint,
|
|
|
// AiStateBuildDefaultAirportBlocks() will kindly skip that one when
|
|
|
// building the waypoints.
|
|
|
|
|
|
i = 0;
|
|
|
do {
|
|
|
// We do this all twice - once for the source (town in the case
|
|
|
// of oilrig route) and then for the destination (oilrig in the
|
|
|
// case of oilrig route).
|
|
|
aib = &p->ai.src + i;
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
// Dismiss ghost stations.
|
|
|
if (st->xy == 0)
|
|
|
continue;
|
|
|
|
|
|
// Is this an airport?
|
|
|
if (!(st->facilities & FACIL_AIRPORT))
|
|
|
continue;
|
|
|
|
|
|
// Do we own the airport? (Oilrigs aren't owned, though.)
|
|
|
if (st->owner != OWNER_NONE && st->owner != _current_player)
|
|
|
continue;
|
|
|
|
|
|
acc_planes = GetAirport(st->airport_type)->acc_planes;
|
|
|
|
|
|
// Dismiss heliports, unless we are checking an oilrig.
|
|
|
if (acc_planes == HELICOPTERS_ONLY && !(p->ai.build_kind == 1 && i == 1))
|
|
|
continue;
|
|
|
|
|
|
// Dismiss country airports if we are doing the other
|
|
|
// endpoint of an oilrig route.
|
|
|
if (acc_planes == AIRCRAFT_ONLY && (p->ai.build_kind == 1 && i == 0))
|
|
@@ -3303,107 +3308,107 @@ static void AiStateAirportStuff(Player *
|
|
|
* when building new airports - and that's irrelevant
|
|
|
* for us. So using just about any rule will suffice
|
|
|
* here for now (some of the new airport types would be
|
|
|
* broken because they will probably need different
|
|
|
* tileoff values etc), no matter that
|
|
|
* AiCheckIfHangar() makes no sense. --pasky */
|
|
|
if (acc_planes == HELICOPTERS_ONLY) {
|
|
|
/* Heliports should have maybe own rulesets but
|
|
|
* OTOH we don't want AI to pick them up when
|
|
|
* looking for a suitable airport type to build.
|
|
|
* So any of rules 0 or 1 would do for now. The
|
|
|
* original rule number was 2 but that's a bug
|
|
|
* because we have no such rule. */
|
|
|
rule = 1;
|
|
|
} else {
|
|
|
rule = AiCheckIfHangar(st);
|
|
|
}
|
|
|
|
|
|
aib->cur_building_rule = rule;
|
|
|
aib->use_tile = st->airport_tile;
|
|
|
break;
|
|
|
}
|
|
|
} while (++i != p->ai.num_build_rec);
|
|
|
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_AIRPORT_BLOCKS;
|
|
|
p->ai.state_mode = 255;
|
|
|
p->ai.state_counter = 0;
|
|
|
}
|
|
|
|
|
|
static int32 AiDoBuildDefaultAirportBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
|
|
|
{
|
|
|
int32 total_cost = 0, ret;
|
|
|
|
|
|
for(;p->mode == 0;p++) {
|
|
|
if (!HASBIT(_avail_aircraft, p->attr))
|
|
|
return CMD_ERROR;
|
|
|
ret = DoCommandByTile(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr,0,flag | DC_AUTO | DC_NO_WATER,CMD_BUILD_AIRPORT);
|
|
|
if (CmdFailed(ret)) return CMD_ERROR;
|
|
|
total_cost += ret;
|
|
|
}
|
|
|
|
|
|
return total_cost;
|
|
|
}
|
|
|
|
|
|
static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
|
|
|
{
|
|
|
uint values[NUM_CARGO];
|
|
|
int w,h;
|
|
|
uint tile2;
|
|
|
int rad;
|
|
|
|
|
|
if (_patches.modified_catchment) {
|
|
|
rad = CA_AIR_LARGE; //I Have NFI what airport the
|
|
|
} else { //AI is going to build here
|
|
|
rad = 4;
|
|
|
}
|
|
|
|
|
|
for(;p->mode==0;p++) {
|
|
|
tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
|
|
TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
|
|
|
|
|
w = _airport_size_x[p->attr];
|
|
|
h = _airport_size_y[p->attr];
|
|
|
if (cargo & 0x80) {
|
|
|
GetProductionAroundTiles(values, tile2, w, h, rad);
|
|
|
return values[cargo & 0x7F] != 0;
|
|
|
} else {
|
|
|
GetAcceptanceAroundTiles(values, tile2, w, h, rad);
|
|
|
return values[cargo] >= 8;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
static int AiFindBestDefaultAirportBlock(TileIndex tile, byte cargo, byte heli, int32 *cost)
|
|
|
{
|
|
|
int i;
|
|
|
const AiDefaultBlockData *p;
|
|
|
for(i=0; (p = _airport_default_block_data[i]) != NULL; i++) {
|
|
|
// If we are doing a helicopter service, avoid building
|
|
|
// airports where they can't land.
|
|
|
if (heli && GetAirport(p->attr)->acc_planes == AIRCRAFT_ONLY)
|
|
|
continue;
|
|
|
|
|
|
*cost = AiDoBuildDefaultAirportBlock(tile, p, 0);
|
|
|
if (*cost != CMD_ERROR && AiCheckAirportResources(tile, p, cargo))
|
|
|
return i;
|
|
|
}
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
static void AiStateBuildDefaultAirportBlocks(Player *p)
|
|
|
{
|
|
|
int i, j;
|
|
|
AiBuildRec *aib;
|
|
|
int rule;
|
|
|
int32 cost;
|
|
|
|
|
|
// time out?
|
|
|
if (++p->ai.timeout_counter == 1388) {
|
|
|
p->ai.state = AIS_0;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// do the following 8 times
|
|
|
i = 8;
|
|
|
do {
|
|
|
// check if we can build the default
|
|
|
aib = &p->ai.src;
|
|
@@ -3436,97 +3441,97 @@ static void AiStateBuildDefaultAirportBl
|
|
|
if (rule == -1) {
|
|
|
// cannot build, terraform after a while
|
|
|
if (p->ai.state_counter >= 600) {
|
|
|
AiDoTerraformLand(aib->use_tile, Random()&3, 3, (int8)p->ai.state_mode);
|
|
|
}
|
|
|
// also try the other terraform direction
|
|
|
if (++p->ai.state_counter >= 1000) {
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.state_mode = -p->ai.state_mode;
|
|
|
}
|
|
|
} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p,aib->use_tile)) {
|
|
|
// player has money, build it.
|
|
|
int32 r;
|
|
|
|
|
|
aib->cur_building_rule = rule;
|
|
|
|
|
|
r = AiDoBuildDefaultAirportBlock(
|
|
|
aib->use_tile,
|
|
|
_airport_default_block_data[rule],
|
|
|
DC_EXEC | DC_NO_TOWN_RATING
|
|
|
);
|
|
|
assert(r != CMD_ERROR);
|
|
|
}
|
|
|
} while (++aib,--j);
|
|
|
} while (--i);
|
|
|
|
|
|
// check if we're done with all of them
|
|
|
aib = &p->ai.src;
|
|
|
j = p->ai.num_build_rec;
|
|
|
do {
|
|
|
if (aib->cur_building_rule == 255)
|
|
|
return;
|
|
|
} while (++aib,--j);
|
|
|
|
|
|
// yep, all are done. switch state.
|
|
|
p->ai.state = AIS_BUILD_AIRCRAFT_VEHICLES;
|
|
|
}
|
|
|
|
|
|
static int AiGetStationIdFromAircraftBlock(TileIndex tile, int id)
|
|
|
{
|
|
|
const AiDefaultBlockData *p = _airport_default_block_data[id];
|
|
|
while (p->mode != 1) p++;
|
|
|
return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))];
|
|
|
}
|
|
|
|
|
|
static void AiStateBuildAircraftVehicles(Player *p)
|
|
|
{
|
|
|
const AiDefaultBlockData *ptr;
|
|
|
uint tile;
|
|
|
TileIndex tile;
|
|
|
int veh;
|
|
|
int i;
|
|
|
uint loco_id;
|
|
|
|
|
|
ptr = _airport_default_block_data[p->ai.src.cur_building_rule];
|
|
|
for(;ptr->mode!=0;ptr++) {}
|
|
|
|
|
|
tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
|
|
|
|
|
|
veh = AiChooseAircraftToBuild(p->player_money, p->ai.build_kind!=0 ? 1 : 0);
|
|
|
if (veh == -1) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT))) return;
|
|
|
loco_id = _new_aircraft_id;
|
|
|
|
|
|
for(i=0; p->ai.order_list_blocks[i] != 0xFF; i++) {
|
|
|
AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i];
|
|
|
bool is_pass = (p->ai.cargo_type == CT_PASSENGERS || p->ai.cargo_type == CT_MAIL);
|
|
|
Order order;
|
|
|
|
|
|
order.type = OT_GOTO_STATION;
|
|
|
order.flags = 0;
|
|
|
order.station = AiGetStationIdFromAircraftBlock(aib->use_tile, aib->cur_building_rule);
|
|
|
|
|
|
if (!is_pass && i == 1) order.flags |= OF_UNLOAD;
|
|
|
if (p->ai.num_want_fullload != 0 && (is_pass || i == 0))
|
|
|
order.flags |= OF_FULL_LOAD;
|
|
|
|
|
|
DoCommandByTile(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
|
|
|
}
|
|
|
|
|
|
DoCommandByTile(0, loco_id, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
|
|
|
|
|
|
DoCommandByTile(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
|
|
|
|
|
if (p->ai.num_want_fullload != 0)
|
|
|
p->ai.num_want_fullload--;
|
|
|
|
|
|
if (--p->ai.num_loco_to_build == 0) {
|
|
|
p->ai.state = AIS_0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static void AiStateCheckShipStuff(Player *p)
|
|
|
{
|
|
|
// XXX
|