|
@@ -278,22 +278,23 @@ static void AiRestoreVehicleOrders(Vehic
|
|
|
|
|
|
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;
|
|
@@ -307,21 +308,22 @@ static void AiHandleReplaceTrain(Player
|
|
|
|
|
|
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;
|
|
@@ -335,21 +337,22 @@ static void AiHandleReplaceRoadVeh(Playe
|
|
|
|
|
|
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;
|
|
@@ -1529,17 +1532,16 @@ static void AiStateWantNewRoute(Player *
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
@@ -1830,13 +1832,13 @@ 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;
|
|
@@ -2172,13 +2174,13 @@ static void AiBuildRailConstruct(Player
|
|
|
|
|
|
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.
|
|
@@ -2491,13 +2493,14 @@ static bool AiCheckRoadResources(TileInd
|
|
|
}
|
|
|
|
|
|
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;
|
|
@@ -2762,19 +2765,19 @@ static bool AiCheckRoadPathBetter(AiRoad
|
|
|
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;
|
|
|
}
|
|
@@ -2790,13 +2793,13 @@ static const uint16 _ai_road_table_and[4
|
|
|
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));
|
|
@@ -2821,13 +2824,13 @@ static bool AiCheckRoadFinished(Player *
|
|
|
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,
|
|
@@ -2959,13 +2962,13 @@ 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;
|
|
|
}
|
|
@@ -3161,13 +3164,14 @@ static int AiGetStationIdFromRoadBlock(T
|
|
|
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));
|
|
|
|
|
@@ -3233,13 +3237,14 @@ static void AiStateDeleteRoadBlocks(Play
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
|
|
@@ -3345,23 +3350,23 @@ static int32 AiDoBuildDefaultAirportBloc
|
|
|
}
|
|
|
|
|
|
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 {
|
|
@@ -3478,13 +3483,13 @@ static int AiGetStationIdFromAircraftBlo
|
|
|
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++) {}
|