|
@@ -23,12 +23,13 @@
|
|
|
#include "../../economy.h"
|
|
|
#include "../../airport.h"
|
|
|
#include "../../depot.h"
|
|
|
#include "../../variables.h"
|
|
|
#include "../../bridge.h"
|
|
|
#include "../../date.h"
|
|
|
#include "../../helpers.hpp"
|
|
|
#include "default.h"
|
|
|
|
|
|
// remove some day perhaps?
|
|
|
static uint _ai_service_interval;
|
|
|
|
|
|
typedef void AiStateAction(Player *p);
|
|
@@ -60,16 +61,16 @@ enum {
|
|
|
AIS_REMOVE_SINGLE_RAIL_TILE = 23
|
|
|
};
|
|
|
|
|
|
|
|
|
#include "../../table/ai_rail.h"
|
|
|
|
|
|
static byte GetRailTrackStatus(TileIndex tile)
|
|
|
static TrackBits GetRailTrackStatus(TileIndex tile)
|
|
|
{
|
|
|
uint32 r = GetTileTrackStatus(tile, TRANSPORT_RAIL);
|
|
|
return (byte) (r | r >> 8);
|
|
|
return (TrackBits)(byte) (r | r >> 8);
|
|
|
}
|
|
|
|
|
|
|
|
|
static void AiCase0(Player *p)
|
|
|
{
|
|
|
p->ai.state = AIS_REMOVE_TRACK;
|
|
@@ -641,14 +642,14 @@ static bool AiCheckIfRouteIsGood(Player
|
|
|
!(p->ai.route_type_mask & bitmask) &&
|
|
|
!CHANCE16(1, 5)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (fr->cargo == CT_PASSENGERS || fr->cargo == CT_MAIL) {
|
|
|
const Town* from = fr->from;
|
|
|
const Town* to = fr->to;
|
|
|
const Town* from = (const Town*)fr->from;
|
|
|
const Town* to = (const Town*)fr->to;
|
|
|
|
|
|
if (from->pct_pass_transported > 0x99 ||
|
|
|
to->pct_pass_transported > 0x99) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -780,13 +781,13 @@ static void AiWantLongIndustryRoute(Play
|
|
|
p->ai.wagon_list[0] = INVALID_VEHICLE;
|
|
|
p->ai.order_list_blocks[0] = 0;
|
|
|
p->ai.order_list_blocks[1] = 1;
|
|
|
p->ai.order_list_blocks[2] = 255;
|
|
|
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
|
|
|
p->ai.state_mode = -1;
|
|
|
p->ai.state_mode = UCHAR_MAX;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.timeout_counter = 0;
|
|
|
}
|
|
|
|
|
|
static void AiWantMediumIndustryRoute(Player *p)
|
|
|
{
|
|
@@ -848,13 +849,13 @@ static void AiWantMediumIndustryRoute(Pl
|
|
|
p->ai.num_want_fullload = 1;
|
|
|
p->ai.wagon_list[0] = INVALID_VEHICLE;
|
|
|
p->ai.order_list_blocks[0] = 0;
|
|
|
p->ai.order_list_blocks[1] = 1;
|
|
|
p->ai.order_list_blocks[2] = 255;
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
|
|
|
p->ai.state_mode = -1;
|
|
|
p->ai.state_mode = UCHAR_MAX;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.timeout_counter = 0;
|
|
|
}
|
|
|
|
|
|
static void AiWantShortIndustryRoute(Player *p)
|
|
|
{
|
|
@@ -916,13 +917,13 @@ static void AiWantShortIndustryRoute(Pla
|
|
|
p->ai.num_want_fullload = 1;
|
|
|
p->ai.wagon_list[0] = INVALID_VEHICLE;
|
|
|
p->ai.order_list_blocks[0] = 0;
|
|
|
p->ai.order_list_blocks[1] = 1;
|
|
|
p->ai.order_list_blocks[2] = 255;
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
|
|
|
p->ai.state_mode = -1;
|
|
|
p->ai.state_mode = UCHAR_MAX;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.timeout_counter = 0;
|
|
|
}
|
|
|
|
|
|
static void AiWantMailRoute(Player *p)
|
|
|
{
|
|
@@ -1017,13 +1018,13 @@ static void AiWantMailRoute(Player *p)
|
|
|
p->ai.num_want_fullload = 0;
|
|
|
p->ai.wagon_list[0] = INVALID_VEHICLE;
|
|
|
p->ai.order_list_blocks[0] = 0;
|
|
|
p->ai.order_list_blocks[1] = 1;
|
|
|
p->ai.order_list_blocks[2] = 255;
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
|
|
|
p->ai.state_mode = -1;
|
|
|
p->ai.state_mode = UCHAR_MAX;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.timeout_counter = 0;
|
|
|
}
|
|
|
|
|
|
static void AiWantPassengerRoute(Player *p)
|
|
|
{
|
|
@@ -1086,13 +1087,13 @@ static void AiWantPassengerRoute(Player
|
|
|
p->ai.num_want_fullload = 0;
|
|
|
p->ai.wagon_list[0] = INVALID_VEHICLE;
|
|
|
p->ai.order_list_blocks[0] = 0;
|
|
|
p->ai.order_list_blocks[1] = 1;
|
|
|
p->ai.order_list_blocks[2] = 255;
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
|
|
|
p->ai.state_mode = -1;
|
|
|
p->ai.state_mode = UCHAR_MAX;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.timeout_counter = 0;
|
|
|
}
|
|
|
|
|
|
static void AiWantTrainRoute(Player *p)
|
|
|
{
|
|
@@ -1161,13 +1162,13 @@ static void AiWantLongRoadIndustryRoute(
|
|
|
// p->ai.loco_id = INVALID_VEHICLE;
|
|
|
p->ai.order_list_blocks[0] = 0;
|
|
|
p->ai.order_list_blocks[1] = 1;
|
|
|
p->ai.order_list_blocks[2] = 255;
|
|
|
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
|
|
|
p->ai.state_mode = -1;
|
|
|
p->ai.state_mode = UCHAR_MAX;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.timeout_counter = 0;
|
|
|
}
|
|
|
|
|
|
static void AiWantMediumRoadIndustryRoute(Player *p)
|
|
|
{
|
|
@@ -1217,13 +1218,13 @@ static void AiWantMediumRoadIndustryRout
|
|
|
// p->ai.loco_id = INVALID_VEHICLE;
|
|
|
p->ai.order_list_blocks[0] = 0;
|
|
|
p->ai.order_list_blocks[1] = 1;
|
|
|
p->ai.order_list_blocks[2] = 255;
|
|
|
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
|
|
|
p->ai.state_mode = -1;
|
|
|
p->ai.state_mode = UCHAR_MAX;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.timeout_counter = 0;
|
|
|
}
|
|
|
|
|
|
static void AiWantLongRoadPassengerRoute(Player *p)
|
|
|
{
|
|
@@ -1275,13 +1276,13 @@ static void AiWantLongRoadPassengerRoute
|
|
|
// p->ai.loco_id = INVALID_VEHICLE;
|
|
|
p->ai.order_list_blocks[0] = 0;
|
|
|
p->ai.order_list_blocks[1] = 1;
|
|
|
p->ai.order_list_blocks[2] = 255;
|
|
|
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
|
|
|
p->ai.state_mode = -1;
|
|
|
p->ai.state_mode = UCHAR_MAX;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.timeout_counter = 0;
|
|
|
}
|
|
|
|
|
|
static void AiWantPassengerRouteInsideTown(Player *p)
|
|
|
{
|
|
@@ -1331,13 +1332,13 @@ static void AiWantPassengerRouteInsideTo
|
|
|
// p->ai.loco_id = INVALID_VEHICLE;
|
|
|
p->ai.order_list_blocks[0] = 0;
|
|
|
p->ai.order_list_blocks[1] = 1;
|
|
|
p->ai.order_list_blocks[2] = 255;
|
|
|
|
|
|
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
|
|
|
p->ai.state_mode = -1;
|
|
|
p->ai.state_mode = UCHAR_MAX;
|
|
|
p->ai.state_counter = 0;
|
|
|
p->ai.timeout_counter = 0;
|
|
|
}
|
|
|
|
|
|
static void AiWantRoadRoute(Player *p)
|
|
|
{
|
|
@@ -1849,13 +1850,13 @@ static bool AiDoFollowTrack(const Player
|
|
|
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 + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, p->ai.cur_dir_a^2,
|
|
|
FollowTrack(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, (DiagDirection)(p->ai.cur_dir_a^2),
|
|
|
(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
|
|
|
return arpfd.count > 8;
|
|
|
}
|
|
|
|
|
|
typedef struct AiRailFinder {
|
|
|
TileIndex final_tile;
|
|
@@ -1938,13 +1939,13 @@ static bool AiCheckRailPathBetter(AiRail
|
|
|
arf->best_ptr = p;
|
|
|
arf->best_tile = 0;
|
|
|
better = true;
|
|
|
}
|
|
|
}
|
|
|
arf->recursive_mode = 0;
|
|
|
arf->cur_best_dist = (uint)-1;
|
|
|
arf->cur_best_dist = UINT_MAX;
|
|
|
arf->cur_best_depth = 0xff;
|
|
|
|
|
|
return better;
|
|
|
}
|
|
|
|
|
|
static inline void AiCheckBuildRailBridgeHere(AiRailFinder *arf, TileIndex tile, const byte *p)
|
|
@@ -2182,13 +2183,13 @@ static bool AiRemoveTileAndGoForward(Pla
|
|
|
return false;
|
|
|
p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDiagDir(p->ai.cur_dir_a));
|
|
|
return true;
|
|
|
} else {
|
|
|
// Check if the bridge points in the right direction.
|
|
|
// This is not really needed the first place AiRemoveTileAndGoForward is called.
|
|
|
if (DiagDirToAxis(GetBridgeRampDirection(tile)) != (p->ai.cur_dir_a & 1U)) return false;
|
|
|
if (DiagDirToAxis(GetBridgeRampDirection(tile)) != (p->ai.cur_dir_a & 1)) return false;
|
|
|
|
|
|
tile = GetOtherBridgeEnd(tile);
|
|
|
|
|
|
tilenew = TILE_MASK(tile - TileOffsByDiagDir(p->ai.cur_dir_a));
|
|
|
// And clear the bridge.
|
|
|
if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)))
|
|
@@ -2817,13 +2818,13 @@ static bool AiCheckRoadFinished(Player *
|
|
|
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);
|
|
|
FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, (DiagDirection)_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;
|
|
|
|
|
@@ -3580,13 +3581,13 @@ static void AiStateRemoveStation(Player
|
|
|
TileIndex tile;
|
|
|
|
|
|
// Go to this state when we're done.
|
|
|
p->ai.state = AIS_1;
|
|
|
|
|
|
// Get a list of all stations that are in use by a vehicle
|
|
|
in_use = malloc(GetMaxStationIndex() + 1);
|
|
|
MallocT(&in_use, GetMaxStationIndex() + 1);
|
|
|
memset(in_use, 0, GetMaxStationIndex() + 1);
|
|
|
FOR_ALL_ORDERS(ord) {
|
|
|
if (ord->type == OT_GOTO_STATION) in_use[ord->dest] = 1;
|
|
|
}
|
|
|
|
|
|
// Go through all stations and delete those that aren't in use
|
|
@@ -3702,13 +3703,13 @@ pos_3:
|
|
|
if (!IsTileOwner(tile, _current_player) ||
|
|
|
!IsBridge(tile) ||
|
|
|
GetBridgeTransportType(tile) != TRANSPORT_RAIL) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
rails = 0;
|
|
|
rails = TRACK_BIT_NONE;
|
|
|
|
|
|
switch (GetBridgeRampDirection(tile)) {
|
|
|
default:
|
|
|
case DIAGDIR_NE: goto pos_2;
|
|
|
case DIAGDIR_SE: goto pos_3;
|
|
|
case DIAGDIR_SW: goto pos_0;
|
|
@@ -3795,13 +3796,12 @@ static void AiHandleTakeover(Player *p)
|
|
|
if (p->bankrupt_asked == 255) return;
|
|
|
|
|
|
{
|
|
|
uint asked = p->bankrupt_asked;
|
|
|
Player *pp, *best_pl = NULL;
|
|
|
int32 best_val = -1;
|
|
|
uint old_p;
|
|
|
|
|
|
// Ask the guy with the highest performance hist.
|
|
|
FOR_ALL_PLAYERS(pp) {
|
|
|
if (pp->is_active &&
|
|
|
!(asked&1) &&
|
|
|
pp->bankrupt_asked == 0 &&
|
|
@@ -3827,13 +3827,13 @@ static void AiHandleTakeover(Player *p)
|
|
|
}
|
|
|
if (IsHumanPlayer(best_pl->index)) return;
|
|
|
|
|
|
// Too little money for computer to buy it?
|
|
|
if (best_pl->player_money >> 1 >= p->bankrupt_value) {
|
|
|
// Computer wants to buy it.
|
|
|
old_p = _current_player;
|
|
|
PlayerID old_p = _current_player;
|
|
|
_current_player = p->index;
|
|
|
DoCommand(0, old_p, 0, DC_EXEC, CMD_BUY_COMPANY);
|
|
|
_current_player = old_p;
|
|
|
}
|
|
|
}
|
|
|
}
|