|
@@ -272,90 +272,93 @@ static void AiRestoreVehicleOrders(Vehic
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
@@ -1523,29 +1526,28 @@ static void AiStateWantNewRoute(Player *
|
|
|
// 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;
|
|
|
}
|
|
|
|
|
@@ -1824,25 +1826,25 @@ static TileIndex AiGetEdgeOfDefaultRailB
|
|
|
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++;
|
|
|
|
|
@@ -2166,25 +2168,25 @@ static void AiBuildRailConstruct(Player
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
@@ -2485,25 +2487,26 @@ static bool AiCheckRoadResources(TileInd
|
|
|
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;
|
|
@@ -2756,53 +2759,53 @@ static bool AiCheckRoadPathBetter(AiRoad
|
|
|
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;
|
|
|
}
|
|
|
|
|
@@ -2815,25 +2818,25 @@ static bool AiCheckRoadFinished(Player *
|
|
|
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));
|
|
|
}
|
|
|
|
|
@@ -2953,25 +2956,25 @@ static void AiBuildRoadRecursive(AiRoadF
|
|
|
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;
|
|
@@ -3155,25 +3158,26 @@ static void AiStateBuildRoad(Player *p)
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
@@ -3227,25 +3231,26 @@ static void AiStateDeleteRoadBlocks(Play
|
|
|
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;
|
|
@@ -3339,35 +3344,35 @@ static int32 AiDoBuildDefaultAirportBloc
|
|
|
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;
|
|
|
}
|
|
@@ -3472,25 +3477,25 @@ static void AiStateBuildDefaultAirportBl
|
|
|
}
|
|
|
|
|
|
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;
|