2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2005-01-29 12:19:05 +00:00
|
|
|
#include "tile.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "player.h"
|
|
|
|
#include "vehicle.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "town.h"
|
|
|
|
#include "industry.h"
|
|
|
|
#include "station.h"
|
|
|
|
#include "pathfind.h"
|
|
|
|
#include "economy.h"
|
|
|
|
#include "airport.h"
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "depot.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// remove some day perhaps?
|
|
|
|
static Player *_cur_ai_player;
|
2004-09-10 18:54:23 +00:00
|
|
|
static uint _ai_service_interval;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
typedef void AiStateAction(Player *p);
|
|
|
|
|
|
|
|
enum {
|
|
|
|
AIS_0 = 0,
|
|
|
|
AIS_1 = 1,
|
|
|
|
AIS_VEH_LOOP = 2,
|
|
|
|
AIS_VEH_CHECK_REPLACE_VEHICLE = 3,
|
|
|
|
AIS_VEH_DO_REPLACE_VEHICLE = 4,
|
|
|
|
AIS_WANT_NEW_ROUTE = 5,
|
|
|
|
AIS_BUILD_DEFAULT_RAIL_BLOCKS = 6,
|
|
|
|
AIS_BUILD_RAIL = 7,
|
|
|
|
AIS_BUILD_RAIL_VEH = 8,
|
|
|
|
AIS_DELETE_RAIL_BLOCKS = 9,
|
|
|
|
AIS_BUILD_DEFAULT_ROAD_BLOCKS = 10,
|
|
|
|
AIS_BUILD_ROAD = 11,
|
|
|
|
AIS_BUILD_ROAD_VEHICLES = 12,
|
|
|
|
AIS_DELETE_ROAD_BLOCKS = 13,
|
|
|
|
AIS_AIRPORT_STUFF = 14,
|
|
|
|
AIS_BUILD_DEFAULT_AIRPORT_BLOCKS = 15,
|
|
|
|
AIS_BUILD_AIRCRAFT_VEHICLES = 16,
|
|
|
|
AIS_CHECK_SHIP_STUFF = 17,
|
|
|
|
AIS_BUILD_DEFAULT_SHIP_BLOCKS = 18,
|
|
|
|
AIS_DO_SHIP_STUFF = 19,
|
|
|
|
AIS_SELL_VEHICLE = 20,
|
|
|
|
AIS_REMOVE_STATION = 21,
|
|
|
|
AIS_REMOVE_TRACK = 22,
|
|
|
|
AIS_REMOVE_SINGLE_RAIL_TILE = 23
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#include "table/ai_rail.h"
|
|
|
|
|
|
|
|
static byte GetRailTrackStatus(TileIndex tile) {
|
2004-09-05 16:15:22 +00:00
|
|
|
uint32 r = GetTileTrackStatus(tile, TRANSPORT_RAIL);
|
2004-08-09 17:04:08 +00:00
|
|
|
return (byte) (r | r >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void AiCase0(Player *p)
|
|
|
|
{
|
|
|
|
p->ai.state = AIS_REMOVE_TRACK;
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiCase1(Player *p)
|
|
|
|
{
|
|
|
|
p->ai.cur_veh = NULL;
|
|
|
|
p->ai.state = AIS_VEH_LOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateVehLoop(Player *p)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
2005-01-06 22:31:58 +00:00
|
|
|
uint index;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
index = (p->ai.cur_veh == NULL) ? 0 : p->ai.cur_veh->index + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
FOR_ALL_VEHICLES_FROM(v, index) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->type == 0 || v->owner != _current_player)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((v->type == VEH_Train && v->subtype==0) ||
|
|
|
|
v->type == VEH_Road ||
|
|
|
|
(v->type == VEH_Aircraft && v->subtype <= 2) ||
|
|
|
|
v->type == VEH_Ship) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* replace engine? */
|
|
|
|
if (v->type == VEH_Train && v->engine_type < 3 &&
|
|
|
|
(_price.build_railvehicle >> 3) < p->player_money) {
|
|
|
|
p->ai.state = AIS_VEH_CHECK_REPLACE_VEHICLE;
|
|
|
|
p->ai.cur_veh = v;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not profitable? */
|
|
|
|
if (v->age >= 730 &&
|
|
|
|
v->profit_last_year < _price.station_value*5 &&
|
|
|
|
v->profit_this_year < _price.station_value*5) {
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
p->ai.state = AIS_SELL_VEHICLE;
|
|
|
|
p->ai.cur_veh = v;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not reliable? */
|
|
|
|
if ((v->age != 0 &&
|
2005-06-07 18:13:49 +00:00
|
|
|
GetEngine(v->engine_type)->reliability < 35389) ||
|
2004-08-09 17:04:08 +00:00
|
|
|
v->age >= v->max_age) {
|
|
|
|
p->ai.state = AIS_VEH_CHECK_REPLACE_VEHICLE;
|
|
|
|
p->ai.cur_veh = v;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p->ai.state = AIS_WANT_NEW_ROUTE;
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
}
|
|
|
|
|
2005-01-30 13:03:32 +00:00
|
|
|
static int AiChooseTrainToBuild(byte railtype, int32 money, byte flag, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int best_veh_index = -1;
|
|
|
|
byte best_veh_score = 0;
|
2005-04-20 22:30:40 +00:00
|
|
|
int32 ret;
|
2004-11-19 19:13:32 +00:00
|
|
|
int i;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-19 19:13:32 +00:00
|
|
|
for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
|
2004-12-04 00:34:20 +00:00
|
|
|
const RailVehicleInfo *rvi = RailVehInfo(i);
|
2005-06-07 18:13:49 +00:00
|
|
|
const Engine* e = GetEngine(i);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-11-19 19:13:32 +00:00
|
|
|
if (e->railtype != railtype || rvi->flags & RVI_WAGON
|
|
|
|
|| !HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D)
|
2004-08-09 17:04:08 +00:00
|
|
|
continue;
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE);
|
|
|
|
if (!CmdFailed(ret) && (!(_cmd_build_rail_veh_var1&1) || !(flag&1)) && ret <= money &&
|
2004-08-09 17:04:08 +00:00
|
|
|
_cmd_build_rail_veh_score >= best_veh_score) {
|
|
|
|
best_veh_score = _cmd_build_rail_veh_score;
|
|
|
|
best_veh_index = i;
|
|
|
|
}
|
2004-11-19 19:13:32 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return best_veh_index;
|
|
|
|
}
|
|
|
|
|
2005-01-30 13:42:11 +00:00
|
|
|
static int AiChooseRoadVehToBuild(byte cargo, int32 money, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int best_veh_index = -1;
|
|
|
|
int32 best_veh_cost = 0;
|
2005-04-20 22:30:40 +00:00
|
|
|
int32 ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
int i = _cargoc.ai_roadveh_start[cargo];
|
|
|
|
int end = i + _cargoc.ai_roadveh_count[cargo];
|
2005-06-07 18:13:49 +00:00
|
|
|
const Engine* e = GetEngine(i);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
|
|
|
if (!HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D)
|
|
|
|
continue;
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(tile, i, 0, 0, CMD_BUILD_ROAD_VEH);
|
|
|
|
if (!CmdFailed(ret) && ret <= money && ret >= best_veh_cost) {
|
|
|
|
best_veh_cost = ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
best_veh_index = i;
|
|
|
|
}
|
|
|
|
} while (++e, ++i != end);
|
|
|
|
|
|
|
|
return best_veh_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int AiChooseAircraftToBuild(int32 money, byte flag)
|
|
|
|
{
|
|
|
|
int best_veh_index = -1;
|
|
|
|
int32 best_veh_cost = 0;
|
2005-04-20 22:30:40 +00:00
|
|
|
int32 ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
int i = AIRCRAFT_ENGINES_INDEX;
|
|
|
|
int end = i + NUM_AIRCRAFT_ENGINES;
|
2005-06-07 18:13:49 +00:00
|
|
|
const Engine* e = GetEngine(i);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
|
|
|
if (!HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (flag&1) {
|
|
|
|
if (i<253) continue;
|
|
|
|
} else {
|
|
|
|
if (i>=253) continue;
|
|
|
|
}
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(0, i, 0, 0, CMD_BUILD_AIRCRAFT);
|
|
|
|
if (!CmdFailed(ret) && ret <= money && ret >= best_veh_cost) {
|
|
|
|
best_veh_cost = ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
best_veh_index = i;
|
|
|
|
}
|
|
|
|
} while (++e, ++i != end);
|
|
|
|
|
|
|
|
return best_veh_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32 AiGetBasePrice(Player *p)
|
|
|
|
{
|
|
|
|
int32 base = _price.station_value;
|
|
|
|
|
|
|
|
// adjust base price when more expensive vehicles are available
|
|
|
|
if (p->ai.railtype_to_use == 1) base = (base * 3) >> 1;
|
|
|
|
else if (p->ai.railtype_to_use == 2) base *= 2;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int AiChooseShipToBuild(byte cargo, int32 money)
|
|
|
|
{
|
|
|
|
// XXX: not done
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int AiChooseRoadVehToReplaceWith(Player *p, Vehicle *v)
|
|
|
|
{
|
|
|
|
int32 avail_money = p->player_money + v->value;
|
2005-01-30 13:42:11 +00:00
|
|
|
return AiChooseRoadVehToBuild(v->cargo_type, avail_money, v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int AiChooseAircraftToReplaceWith(Player *p, Vehicle *v)
|
|
|
|
{
|
|
|
|
int32 avail_money = p->player_money + v->value;
|
|
|
|
return AiChooseAircraftToBuild(avail_money, v->engine_type>=253?1:0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int AiChooseTrainToReplaceWith(Player *p, Vehicle *v)
|
|
|
|
{
|
|
|
|
int32 avail_money = p->player_money + v->value;
|
|
|
|
int num=0;
|
|
|
|
Vehicle *u = v;
|
|
|
|
|
|
|
|
while (++num, u->next != NULL) {
|
|
|
|
u = u->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: check if a wagon
|
2005-01-30 13:03:32 +00:00
|
|
|
return AiChooseTrainToBuild(v->u.rail.railtype, avail_money, 0, v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2004-12-05 12:43:04 +00:00
|
|
|
if (p->ai.cur_veh->current_order.type != OT_GOTO_DEPOT)
|
2004-08-09 17:04:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
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;
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, p->ai.cur_veh->index);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak)
|
|
|
|
{
|
2005-01-24 20:20:30 +00:00
|
|
|
int i;
|
2004-12-05 12:43:04 +00:00
|
|
|
|
2005-01-24 20:20:30 +00:00
|
|
|
for (i = 0; bak->order[i].type != OT_NOTHING; i++)
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)))
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiHandleReplaceTrain(Player *p)
|
|
|
|
{
|
|
|
|
Vehicle *v = p->ai.cur_veh;
|
|
|
|
BackuppedOrders orderbak[1];
|
|
|
|
int veh;
|
|
|
|
|
|
|
|
// wait until the vehicle reaches the depot.
|
2005-02-06 22:36:08 +00:00
|
|
|
if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
AiHandleGotoDepot(p, CMD_TRAIN_GOTO_DEPOT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
veh = AiChooseTrainToReplaceWith(p, v);
|
|
|
|
if (veh != -1) {
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
BackupVehicleOrders(v, orderbak);
|
|
|
|
tile = v->tile;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(DoCommandByTile(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) &&
|
|
|
|
!CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE)) ) {
|
2004-08-09 17:04:08 +00:00
|
|
|
veh = _new_train_id;
|
2005-01-06 22:31:58 +00:00
|
|
|
AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
|
2004-08-09 17:04:08 +00:00
|
|
|
DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN);
|
2004-09-10 18:54:23 +00:00
|
|
|
|
|
|
|
DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiHandleReplaceRoadVeh(Player *p)
|
|
|
|
{
|
|
|
|
Vehicle *v = p->ai.cur_veh;
|
|
|
|
BackuppedOrders orderbak[1];
|
|
|
|
int veh;
|
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
AiHandleGotoDepot(p, CMD_SEND_ROADVEH_TO_DEPOT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
veh = AiChooseRoadVehToReplaceWith(p, v);
|
|
|
|
if (veh != -1) {
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
BackupVehicleOrders(v, orderbak);
|
|
|
|
tile = v->tile;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) &&
|
|
|
|
!CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH)) ) {
|
2004-08-09 17:04:08 +00:00
|
|
|
veh = _new_roadveh_id;
|
2005-01-06 22:31:58 +00:00
|
|
|
AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
|
2004-08-09 17:04:08 +00:00
|
|
|
DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
|
2004-09-10 18:54:23 +00:00
|
|
|
|
|
|
|
DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiHandleReplaceAircraft(Player *p)
|
|
|
|
{
|
|
|
|
Vehicle *v = p->ai.cur_veh;
|
|
|
|
int veh;
|
|
|
|
BackuppedOrders orderbak[1];
|
|
|
|
|
|
|
|
if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) {
|
|
|
|
AiHandleGotoDepot(p, CMD_SEND_AIRCRAFT_TO_HANGAR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
veh = AiChooseAircraftToReplaceWith(p, v);
|
|
|
|
if (veh != -1) {
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
BackupVehicleOrders(v, orderbak);
|
|
|
|
tile = v->tile;
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT)) &&
|
|
|
|
!CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT)) ) {
|
2004-08-09 17:04:08 +00:00
|
|
|
veh = _new_aircraft_id;
|
2005-01-06 22:31:58 +00:00
|
|
|
AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
|
2004-08-09 17:04:08 +00:00
|
|
|
DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
|
2004-09-10 18:54:23 +00:00
|
|
|
|
|
|
|
DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateDoReplaceVehicle(Player *p)
|
|
|
|
{
|
|
|
|
Vehicle *v = p->ai.cur_veh;
|
|
|
|
p->ai.state = AIS_VEH_LOOP;
|
|
|
|
// vehicle is not owned by the player anymore, something went very wrong.
|
|
|
|
if (v->type == 0 || v->owner != _current_player)
|
|
|
|
return;
|
|
|
|
_veh_do_replace_proc[v->type - VEH_Train](p);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct FoundRoute {
|
|
|
|
int distance;
|
|
|
|
byte cargo;
|
|
|
|
void *from;
|
|
|
|
void *to;
|
|
|
|
} FoundRoute;
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static Town *AiFindRandomTown(void)
|
|
|
|
{
|
2005-01-06 22:31:58 +00:00
|
|
|
Town *t = GetTown(RandomRange(_total_towns));
|
2004-08-09 17:04:08 +00:00
|
|
|
return (t->xy != 0) ? t : NULL;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static Industry *AiFindRandomIndustry(void)
|
|
|
|
{
|
2005-01-06 22:31:58 +00:00
|
|
|
Industry *i = GetIndustry(RandomRange(_total_industries));
|
2004-08-09 17:04:08 +00:00
|
|
|
return (i->xy != 0) ? i : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiFindSubsidyIndustryRoute(FoundRoute *fr)
|
|
|
|
{
|
2005-01-09 21:25:44 +00:00
|
|
|
uint i;
|
2004-08-09 17:04:08 +00:00
|
|
|
byte cargo;
|
|
|
|
Subsidy *s;
|
|
|
|
Industry *from, *to_ind;
|
|
|
|
Town *to_tow;
|
|
|
|
TileIndex to_xy;
|
|
|
|
|
|
|
|
// initially error
|
|
|
|
fr->distance = -1;
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
// Randomize subsidy index..
|
2004-08-09 17:04:08 +00:00
|
|
|
i = RandomRange(lengthof(_subsidies) * 3);
|
|
|
|
if (i >= lengthof(_subsidies))
|
|
|
|
return;
|
|
|
|
|
|
|
|
s = &_subsidies[i];
|
|
|
|
|
|
|
|
// Don't want passengers or mail
|
|
|
|
cargo = s->cargo_type;
|
|
|
|
if (cargo == 0xFF || cargo == CT_PASSENGERS || cargo == CT_MAIL || s->age > 7)
|
|
|
|
return;
|
|
|
|
fr->cargo = cargo;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
fr->from = from = GetIndustry(s->from);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (cargo == CT_GOODS || cargo == CT_FOOD) {
|
2005-01-06 22:31:58 +00:00
|
|
|
to_tow = GetTown(s->to);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (to_tow->population < (uint32)(cargo == CT_FOOD ? 200 : 900))
|
|
|
|
return; // error
|
|
|
|
fr->to = to_tow;
|
|
|
|
to_xy = to_tow->xy;
|
|
|
|
} else {
|
2005-01-06 22:31:58 +00:00
|
|
|
to_ind = GetIndustry(s->to);
|
2004-08-09 17:04:08 +00:00
|
|
|
fr->to = to_ind;
|
|
|
|
to_xy = to_ind->xy;
|
|
|
|
}
|
|
|
|
|
2005-01-31 07:23:15 +00:00
|
|
|
fr->distance = DistanceManhattan(from->xy, to_xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AiFindSubsidyPassengerRoute(FoundRoute *fr)
|
|
|
|
{
|
|
|
|
uint i;
|
|
|
|
Subsidy *s;
|
|
|
|
Town *from,*to;
|
|
|
|
|
|
|
|
// initially error
|
|
|
|
fr->distance = -1;
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
// Randomize subsidy index..
|
2004-08-09 17:04:08 +00:00
|
|
|
i = RandomRange(lengthof(_subsidies) * 3);
|
|
|
|
if (i >= lengthof(_subsidies))
|
|
|
|
return;
|
|
|
|
|
|
|
|
s = &_subsidies[i];
|
|
|
|
|
|
|
|
// Only want passengers
|
|
|
|
if (s->cargo_type != CT_PASSENGERS || s->age > 7)
|
|
|
|
return;
|
|
|
|
fr->cargo = s->cargo_type;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
fr->from = from = GetTown(s->from);
|
|
|
|
fr->to = to = GetTown(s->to);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// They must be big enough
|
|
|
|
if (from->population < 400 || to->population < 400)
|
|
|
|
return;
|
|
|
|
|
2005-01-31 07:23:15 +00:00
|
|
|
fr->distance = DistanceManhattan(from->xy, to->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AiFindRandomIndustryRoute(FoundRoute *fr)
|
|
|
|
{
|
|
|
|
Industry *i,*i2;
|
|
|
|
Town *t;
|
|
|
|
uint32 r;
|
|
|
|
byte cargo;
|
|
|
|
|
|
|
|
// initially error
|
|
|
|
fr->distance = -1;
|
|
|
|
|
|
|
|
r = Random();
|
|
|
|
|
|
|
|
// pick a source
|
|
|
|
fr->from = i = AiFindRandomIndustry();
|
2004-09-10 19:02:27 +00:00
|
|
|
if (i == NULL)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// pick a random produced cargo
|
|
|
|
cargo = i->produced_cargo[0];
|
|
|
|
if (r&1 && i->produced_cargo[1] != 0xFF)
|
|
|
|
cargo = i->produced_cargo[1];
|
|
|
|
|
|
|
|
fr->cargo = cargo;
|
|
|
|
|
|
|
|
// don't allow passengers
|
|
|
|
if (cargo == 0xFF || cargo == CT_PASSENGERS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cargo != CT_GOODS && cargo != CT_FOOD) {
|
|
|
|
// pick a dest, and see if it can receive
|
|
|
|
i2 = AiFindRandomIndustry();
|
|
|
|
if (i2 == NULL || i == i2 || !(i2->accepts_cargo[0] == cargo || i2->accepts_cargo[1] == cargo || i2->accepts_cargo[2] == cargo))
|
|
|
|
return;
|
|
|
|
|
|
|
|
fr->to = i2;
|
2005-01-31 07:23:15 +00:00
|
|
|
fr->distance = DistanceManhattan(i->xy, i2->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
// pick a dest town, and see if it's big enough
|
|
|
|
t = AiFindRandomTown();
|
|
|
|
if (t == NULL || t->population < (uint32)(cargo == CT_FOOD ? 200 : 900))
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
fr->to = t;
|
2005-01-31 07:23:15 +00:00
|
|
|
fr->distance = DistanceManhattan(i->xy, t->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiFindRandomPassengerRoute(FoundRoute *fr)
|
|
|
|
{
|
|
|
|
uint32 r;
|
|
|
|
Town *source, *dest;
|
|
|
|
|
|
|
|
// initially error
|
|
|
|
fr->distance = -1;
|
|
|
|
|
|
|
|
r = Random();
|
|
|
|
|
|
|
|
fr->from = source = AiFindRandomTown();
|
|
|
|
if (source == NULL || source->population < 400)
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
fr->to = dest = AiFindRandomTown();
|
|
|
|
if (dest == NULL || source == dest || dest->population < 400)
|
|
|
|
return;
|
|
|
|
|
2005-01-31 07:23:15 +00:00
|
|
|
fr->distance = DistanceManhattan(source->xy, dest->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warn: depends on 'xy' being the first element in both Town and Industry
|
|
|
|
#define GET_TOWN_OR_INDUSTRY_TILE(p) (((Town*)(p))->xy)
|
|
|
|
|
|
|
|
static bool AiCheckIfRouteIsGood(Player *p, FoundRoute *fr, byte bitmask)
|
|
|
|
{
|
|
|
|
TileIndex from_tile, to_tile;
|
|
|
|
Station *st;
|
|
|
|
int dist, cur;
|
|
|
|
uint same_station = 0;
|
|
|
|
|
|
|
|
// Make sure distance to closest station is < 37 pixels.
|
|
|
|
from_tile = GET_TOWN_OR_INDUSTRY_TILE(fr->from);
|
|
|
|
to_tile = GET_TOWN_OR_INDUSTRY_TILE(fr->to);
|
|
|
|
|
|
|
|
dist = 0xFFFF;
|
|
|
|
FOR_ALL_STATIONS(st) if (st->xy != 0 && st->owner == _current_player) {
|
2005-01-31 07:23:15 +00:00
|
|
|
cur = DistanceMax(from_tile, st->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (cur < dist) dist = cur;
|
2005-01-31 07:23:15 +00:00
|
|
|
cur = DistanceMax(to_tile, st->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (cur < dist) dist = cur;
|
|
|
|
if (to_tile == from_tile && st->xy == to_tile)
|
|
|
|
same_station++;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// To prevent the AI from building ten busstations in the same town, do some calculations
|
|
|
|
// For each road or airport station, we want 350 of population!
|
|
|
|
if ((bitmask == 2 || bitmask == 4) && same_station > 2 && ((Town *)(fr->from))->population < same_station * 350)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (dist != 0xFFFF && dist > 37)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (p->ai.route_type_mask != 0 && !(p->ai.route_type_mask&bitmask) && !CHANCE16(1,5))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (fr->cargo == CT_PASSENGERS || fr->cargo == CT_MAIL) {
|
|
|
|
if (((Town*)fr->from)->pct_pass_transported > 0x99 ||
|
|
|
|
((Town*)fr->to)->pct_pass_transported > 0x99)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make sure it has a reasonably good rating
|
|
|
|
if ( ((Town*)fr->from)->ratings[_current_player] < -100 ||
|
|
|
|
((Town*)fr->to)->ratings[_current_player] < -100)
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
Industry *i = (Industry*)fr->from;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
|
|
|
if (i->pct_transported[fr->cargo != i->produced_cargo[0]] > 0x99 ||
|
2004-08-09 17:04:08 +00:00
|
|
|
i->total_production[fr->cargo != i->produced_cargo[0]] == 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->ai.route_type_mask |= bitmask;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static byte AiGetDirectionBetweenTiles(TileIndex a, TileIndex b)
|
|
|
|
{
|
2005-01-07 17:02:43 +00:00
|
|
|
byte i = (TileX(a) < TileX(b)) ? 1 : 0;
|
|
|
|
if (TileY(a) >= TileY(b)) i ^= 3;
|
2004-08-09 17:04:08 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TileIndex AiGetPctTileBetween(TileIndex a, TileIndex b, byte pct)
|
|
|
|
{
|
2005-06-25 16:44:57 +00:00
|
|
|
return TileXY(
|
2005-01-07 17:02:43 +00:00
|
|
|
TileX(a) + ((TileX(b) - TileX(a)) * pct >> 8),
|
|
|
|
TileY(a) + ((TileY(b) - TileY(a)) * pct >> 8)
|
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantLongIndustryRoute(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
|
|
|
// look for one from the subsidy list
|
|
|
|
AiFindSubsidyIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 60, 90+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// try a random one
|
|
|
|
AiFindRandomIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 60, 90+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to);
|
|
|
|
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from);
|
|
|
|
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 9;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.unk6 = 1;
|
|
|
|
p->ai.src.unk7 = 0;
|
|
|
|
p->ai.src.buildcmd_a = 0x24;
|
|
|
|
p->ai.src.buildcmd_b = 0xFF;
|
|
|
|
p->ai.src.direction = AiGetDirectionBetweenTiles(
|
|
|
|
p->ai.src.spec_tile,
|
|
|
|
p->ai.dst.spec_tile
|
|
|
|
);
|
|
|
|
p->ai.src.cargo = fr.cargo | 0x80;
|
|
|
|
|
|
|
|
// Fill the dest field
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 9;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.unk6 = 1;
|
|
|
|
p->ai.dst.unk7 = 0;
|
|
|
|
p->ai.dst.buildcmd_a = 0x34;
|
|
|
|
p->ai.dst.buildcmd_b = 0xFF;
|
|
|
|
p->ai.dst.direction = AiGetDirectionBetweenTiles(
|
|
|
|
p->ai.dst.spec_tile,
|
|
|
|
p->ai.src.spec_tile
|
|
|
|
);
|
|
|
|
p->ai.dst.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill middle field 1
|
|
|
|
p->ai.mid1.spec_tile = AiGetPctTileBetween(
|
|
|
|
p->ai.src.spec_tile,
|
|
|
|
p->ai.dst.spec_tile,
|
|
|
|
0x55
|
|
|
|
);
|
|
|
|
p->ai.mid1.use_tile = 0;
|
|
|
|
p->ai.mid1.rand_rng = 6;
|
|
|
|
p->ai.mid1.cur_building_rule = 0xFF;
|
|
|
|
p->ai.mid1.unk6 = 2;
|
|
|
|
p->ai.mid1.unk7 = 1;
|
|
|
|
p->ai.mid1.buildcmd_a = 0x30;
|
|
|
|
p->ai.mid1.buildcmd_b = 0xFF;
|
|
|
|
p->ai.mid1.direction = p->ai.src.direction;
|
|
|
|
p->ai.mid1.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill middle field 2
|
|
|
|
p->ai.mid2.spec_tile = AiGetPctTileBetween(
|
|
|
|
p->ai.src.spec_tile,
|
|
|
|
p->ai.dst.spec_tile,
|
|
|
|
0xAA
|
|
|
|
);
|
|
|
|
p->ai.mid2.use_tile = 0;
|
|
|
|
p->ai.mid2.rand_rng = 6;
|
|
|
|
p->ai.mid2.cur_building_rule = 0xFF;
|
|
|
|
p->ai.mid2.unk6 = 2;
|
|
|
|
p->ai.mid2.unk7 = 1;
|
|
|
|
p->ai.mid2.buildcmd_a = 0xFF;
|
|
|
|
p->ai.mid2.buildcmd_b = 0xFF;
|
|
|
|
p->ai.mid2.direction = p->ai.dst.direction;
|
|
|
|
p->ai.mid2.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = fr.cargo;
|
|
|
|
p->ai.num_wagons = 3;
|
|
|
|
p->ai.build_kind = 2;
|
|
|
|
p->ai.num_build_rec = 4;
|
|
|
|
p->ai.num_loco_to_build = 2;
|
|
|
|
p->ai.num_want_fullload = 2;
|
|
|
|
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_counter = 0;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantMediumIndustryRoute(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// look for one from the subsidy list
|
|
|
|
AiFindSubsidyIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 40, 60+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// try a random one
|
|
|
|
AiFindRandomIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 40, 60+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from);
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 9;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.unk6 = 1;
|
|
|
|
p->ai.src.unk7 = 0;
|
|
|
|
p->ai.src.buildcmd_a = 0x10;
|
|
|
|
p->ai.src.buildcmd_b = 0xFF;
|
|
|
|
p->ai.src.direction = AiGetDirectionBetweenTiles(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to)
|
|
|
|
);
|
|
|
|
p->ai.src.cargo = fr.cargo | 0x80;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to);
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 9;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.unk6 = 1;
|
|
|
|
p->ai.dst.unk7 = 0;
|
|
|
|
p->ai.dst.buildcmd_a = 0xFF;
|
|
|
|
p->ai.dst.buildcmd_b = 0xFF;
|
|
|
|
p->ai.dst.direction = AiGetDirectionBetweenTiles(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from)
|
|
|
|
);
|
|
|
|
p->ai.dst.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = fr.cargo;
|
|
|
|
p->ai.num_wagons = 3;
|
|
|
|
p->ai.build_kind = 1;
|
|
|
|
p->ai.num_build_rec = 2;
|
|
|
|
p->ai.num_loco_to_build = 1;
|
|
|
|
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_counter = 0;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantShortIndustryRoute(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// look for one from the subsidy list
|
|
|
|
AiFindSubsidyIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 15, 40+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// try a random one
|
|
|
|
AiFindRandomIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 15, 40+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from);
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 9;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.unk6 = 1;
|
|
|
|
p->ai.src.unk7 = 0;
|
|
|
|
p->ai.src.buildcmd_a = 0x10;
|
|
|
|
p->ai.src.buildcmd_b = 0xFF;
|
|
|
|
p->ai.src.direction = AiGetDirectionBetweenTiles(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to)
|
|
|
|
);
|
|
|
|
p->ai.src.cargo = fr.cargo | 0x80;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to);
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 9;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.unk6 = 1;
|
|
|
|
p->ai.dst.unk7 = 0;
|
|
|
|
p->ai.dst.buildcmd_a = 0xFF;
|
|
|
|
p->ai.dst.buildcmd_b = 0xFF;
|
|
|
|
p->ai.dst.direction = AiGetDirectionBetweenTiles(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from)
|
|
|
|
);
|
|
|
|
p->ai.dst.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = fr.cargo;
|
|
|
|
p->ai.num_wagons = 2;
|
|
|
|
p->ai.build_kind = 1;
|
|
|
|
p->ai.num_build_rec = 2;
|
|
|
|
p->ai.num_loco_to_build = 1;
|
|
|
|
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_counter = 0;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantMailRoute(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// look for one from the subsidy list
|
|
|
|
AiFindSubsidyPassengerRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 60, 110+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// try a random one
|
|
|
|
AiFindRandomPassengerRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 60, 110+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fr.cargo = CT_MAIL;
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from);
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 7;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.unk6 = 1;
|
|
|
|
p->ai.src.unk7 = 0;
|
|
|
|
p->ai.src.buildcmd_a = 0x24;
|
|
|
|
p->ai.src.buildcmd_b = 0xFF;
|
|
|
|
p->ai.src.direction = AiGetDirectionBetweenTiles(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to)
|
|
|
|
);
|
|
|
|
p->ai.src.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to);
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 7;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.unk6 = 1;
|
|
|
|
p->ai.dst.unk7 = 0;
|
|
|
|
p->ai.dst.buildcmd_a = 0x34;
|
|
|
|
p->ai.dst.buildcmd_b = 0xFF;
|
|
|
|
p->ai.dst.direction = AiGetDirectionBetweenTiles(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from)
|
|
|
|
);
|
|
|
|
p->ai.dst.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill middle field 1
|
|
|
|
p->ai.mid1.spec_tile = AiGetPctTileBetween(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to),
|
|
|
|
0x55
|
|
|
|
);
|
|
|
|
p->ai.mid1.use_tile = 0;
|
|
|
|
p->ai.mid1.rand_rng = 6;
|
|
|
|
p->ai.mid1.cur_building_rule = 0xFF;
|
|
|
|
p->ai.mid1.unk6 = 2;
|
|
|
|
p->ai.mid1.unk7 = 1;
|
|
|
|
p->ai.mid1.buildcmd_a = 0x30;
|
|
|
|
p->ai.mid1.buildcmd_b = 0xFF;
|
|
|
|
p->ai.mid1.direction = p->ai.src.direction;
|
|
|
|
p->ai.mid1.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill middle field 2
|
|
|
|
p->ai.mid2.spec_tile = AiGetPctTileBetween(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to),
|
|
|
|
0xAA
|
|
|
|
);
|
|
|
|
p->ai.mid2.use_tile = 0;
|
|
|
|
p->ai.mid2.rand_rng = 6;
|
|
|
|
p->ai.mid2.cur_building_rule = 0xFF;
|
|
|
|
p->ai.mid2.unk6 = 2;
|
|
|
|
p->ai.mid2.unk7 = 1;
|
|
|
|
p->ai.mid2.buildcmd_a = 0xFF;
|
|
|
|
p->ai.mid2.buildcmd_b = 0xFF;
|
|
|
|
p->ai.mid2.direction = p->ai.dst.direction;
|
|
|
|
p->ai.mid2.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = fr.cargo;
|
|
|
|
p->ai.num_wagons = 3;
|
|
|
|
p->ai.build_kind = 2;
|
|
|
|
p->ai.num_build_rec = 4;
|
|
|
|
p->ai.num_loco_to_build = 2;
|
|
|
|
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_counter = 0;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantPassengerRoute(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// look for one from the subsidy list
|
|
|
|
AiFindSubsidyPassengerRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 0, 55+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// try a random one
|
|
|
|
AiFindRandomPassengerRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 0, 55+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fr.cargo = CT_PASSENGERS;
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from);
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 7;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.unk6 = 1;
|
|
|
|
p->ai.src.unk7 = 0;
|
|
|
|
p->ai.src.buildcmd_a = 0x10;
|
|
|
|
p->ai.src.buildcmd_b = 0xFF;
|
|
|
|
p->ai.src.direction = AiGetDirectionBetweenTiles(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to)
|
|
|
|
);
|
|
|
|
p->ai.src.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to);
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 7;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.unk6 = 1;
|
|
|
|
p->ai.dst.unk7 = 0;
|
|
|
|
p->ai.dst.buildcmd_a = 0xFF;
|
|
|
|
p->ai.dst.buildcmd_b = 0xFF;
|
|
|
|
p->ai.dst.direction = AiGetDirectionBetweenTiles(
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.to),
|
|
|
|
GET_TOWN_OR_INDUSTRY_TILE(fr.from)
|
|
|
|
);
|
|
|
|
p->ai.dst.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = fr.cargo;
|
|
|
|
p->ai.num_wagons = 2;
|
|
|
|
p->ai.build_kind = 1;
|
|
|
|
p->ai.num_build_rec = 2;
|
|
|
|
p->ai.num_loco_to_build = 1;
|
|
|
|
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_counter = 0;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantTrainRoute(Player *p)
|
|
|
|
{
|
|
|
|
uint16 r;
|
|
|
|
p->ai.railtype_to_use = p->max_railtype - 1;
|
|
|
|
r = (uint16)Random();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (r > 0xD000) {
|
|
|
|
AiWantLongIndustryRoute(p);
|
|
|
|
} else if (r > 0x6000) {
|
|
|
|
AiWantMediumIndustryRoute(p);
|
|
|
|
} else if (r > 0x1000) {
|
|
|
|
AiWantShortIndustryRoute(p);
|
|
|
|
} else if (r > 0x800) {
|
|
|
|
AiWantPassengerRoute(p);
|
|
|
|
} else {
|
|
|
|
AiWantMailRoute(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantLongRoadIndustryRoute(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// look for one from the subsidy list
|
|
|
|
AiFindSubsidyIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 35, 55+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// try a random one
|
|
|
|
AiFindRandomIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 35, 55+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 2))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from);
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 9;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.buildcmd_a = 1;
|
|
|
|
p->ai.src.direction = 0;
|
|
|
|
p->ai.src.cargo = fr.cargo | 0x80;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to);
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 9;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.buildcmd_a = 0xFF;
|
|
|
|
p->ai.dst.direction = 0;
|
|
|
|
p->ai.dst.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = fr.cargo;
|
|
|
|
p->ai.num_build_rec = 2;
|
|
|
|
p->ai.num_loco_to_build = 5;
|
|
|
|
p->ai.num_want_fullload = 5;
|
|
|
|
|
|
|
|
// 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_counter = 0;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantMediumRoadIndustryRoute(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// look for one from the subsidy list
|
|
|
|
AiFindSubsidyIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 15, 40+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// try a random one
|
|
|
|
AiFindRandomIndustryRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 15, 40+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 2))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from);
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 9;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.buildcmd_a = 1;
|
|
|
|
p->ai.src.direction = 0;
|
|
|
|
p->ai.src.cargo = fr.cargo | 0x80;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to);
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 9;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.buildcmd_a = 0xFF;
|
|
|
|
p->ai.dst.direction = 0;
|
|
|
|
p->ai.dst.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = fr.cargo;
|
|
|
|
p->ai.num_build_rec = 2;
|
|
|
|
p->ai.num_loco_to_build = 3;
|
|
|
|
p->ai.num_want_fullload = 3;
|
|
|
|
|
|
|
|
// 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_counter = 0;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantLongRoadPassengerRoute(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// look for one from the subsidy list
|
|
|
|
AiFindSubsidyPassengerRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 55, 180+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// try a random one
|
|
|
|
AiFindRandomPassengerRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance, 55, 180+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fr.cargo = CT_PASSENGERS;
|
|
|
|
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 2))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to);
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 10;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.buildcmd_a = 1;
|
|
|
|
p->ai.src.direction = 0;
|
|
|
|
p->ai.src.cargo = CT_PASSENGERS;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from);
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 10;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.buildcmd_a = 0xFF;
|
|
|
|
p->ai.dst.direction = 0;
|
|
|
|
p->ai.dst.cargo = CT_PASSENGERS;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = CT_PASSENGERS;
|
|
|
|
p->ai.num_build_rec = 2;
|
|
|
|
p->ai.num_loco_to_build = 4;
|
|
|
|
p->ai.num_want_fullload = 0;
|
|
|
|
|
|
|
|
// 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_counter = 0;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantPassengerRouteInsideTown(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
Town *t;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
|
|
|
// Find a town big enough
|
|
|
|
t = AiFindRandomTown();
|
|
|
|
if (t != NULL && t->population >= 700)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fr.cargo = CT_PASSENGERS;
|
|
|
|
fr.from = fr.to = t;
|
|
|
|
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 2))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = t->xy;
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 10;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.buildcmd_a = 1;
|
|
|
|
p->ai.src.direction = 0;
|
|
|
|
p->ai.src.cargo = CT_PASSENGERS;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = t->xy;
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 10;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.buildcmd_a = 0xFF;
|
|
|
|
p->ai.dst.direction = 0;
|
|
|
|
p->ai.dst.cargo = CT_PASSENGERS;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = CT_PASSENGERS;
|
|
|
|
p->ai.num_build_rec = 2;
|
|
|
|
p->ai.num_loco_to_build = 2;
|
|
|
|
p->ai.num_want_fullload = 0;
|
|
|
|
|
|
|
|
// 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_counter = 0;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantRoadRoute(Player *p)
|
|
|
|
{
|
|
|
|
uint16 r = (uint16)Random();
|
|
|
|
|
|
|
|
if (r > 0x4000) {
|
|
|
|
AiWantLongRoadIndustryRoute(p);
|
|
|
|
} else if (r > 0x2000) {
|
|
|
|
AiWantMediumRoadIndustryRoute(p);
|
|
|
|
} else if (r > 0x1000) {
|
|
|
|
AiWantLongRoadPassengerRoute(p);
|
|
|
|
} else {
|
|
|
|
AiWantPassengerRouteInsideTown(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantPassengerAircraftRoute(Player *p)
|
|
|
|
{
|
|
|
|
FoundRoute fr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// look for one from the subsidy list
|
|
|
|
AiFindSubsidyPassengerRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance,0,95+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// try a random one
|
|
|
|
AiFindRandomPassengerRoute(&fr);
|
|
|
|
if (IS_INT_INSIDE(fr.distance,0,95+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fr.cargo = CT_PASSENGERS;
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 4))
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to);
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 12;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from);
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 12;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.cargo = fr.cargo;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = fr.cargo;
|
|
|
|
p->ai.build_kind = 0;
|
|
|
|
p->ai.num_build_rec = 2;
|
|
|
|
p->ai.num_loco_to_build = 1;
|
|
|
|
p->ai.num_want_fullload = 1;
|
|
|
|
// 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;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.state = AIS_AIRPORT_STUFF;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantOilRigAircraftRoute(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FoundRoute fr;
|
|
|
|
Town *t;
|
|
|
|
Industry *in;
|
|
|
|
|
|
|
|
i = 60;
|
|
|
|
for(;;) {
|
|
|
|
// Find a town
|
|
|
|
t = AiFindRandomTown();
|
|
|
|
if (t != NULL) {
|
|
|
|
// Find a random oil rig industry
|
2005-01-06 22:31:58 +00:00
|
|
|
in = GetIndustry(RandomRange(_total_industries));
|
2004-08-09 17:04:08 +00:00
|
|
|
if (in != NULL && in->type == IT_OIL_RIG) {
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceManhattan(t->xy, in->xy) < 60)
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// only test 60 times
|
|
|
|
if (--i == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fr.cargo = CT_PASSENGERS;
|
|
|
|
fr.from = fr.to = t;
|
|
|
|
|
|
|
|
if (!AiCheckIfRouteIsGood(p, &fr, 4))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fill the source field
|
|
|
|
p->ai.src.spec_tile = t->xy;
|
|
|
|
p->ai.src.use_tile = 0;
|
|
|
|
p->ai.src.rand_rng = 12;
|
|
|
|
p->ai.src.cur_building_rule = 0xFF;
|
|
|
|
p->ai.src.cargo = CT_PASSENGERS;
|
|
|
|
|
|
|
|
// Fill the dest field
|
|
|
|
p->ai.dst.spec_tile = in->xy;
|
|
|
|
p->ai.dst.use_tile = 0;
|
|
|
|
p->ai.dst.rand_rng = 5;
|
|
|
|
p->ai.dst.cur_building_rule = 0xFF;
|
|
|
|
p->ai.dst.cargo = CT_PASSENGERS;
|
|
|
|
|
|
|
|
// Fill common fields
|
|
|
|
p->ai.cargo_type = CT_PASSENGERS;
|
|
|
|
p->ai.build_kind = 1;
|
|
|
|
p->ai.num_build_rec = 2;
|
|
|
|
p->ai.num_loco_to_build = 1;
|
|
|
|
p->ai.num_want_fullload = 0;
|
|
|
|
// 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_AIRPORT_STUFF;
|
|
|
|
p->ai.timeout_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantAircraftRoute(Player *p)
|
|
|
|
{
|
|
|
|
uint16 r = (uint16)Random();
|
|
|
|
|
|
|
|
if (r >= 0x2AAA || _date < 0x3912) {
|
|
|
|
AiWantPassengerAircraftRoute(p);
|
|
|
|
} else {
|
|
|
|
AiWantOilRigAircraftRoute(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiWantShipRoute(Player *p)
|
|
|
|
{
|
|
|
|
// XXX
|
|
|
|
// error("AiWaitShipRoute");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-17 16:20:04 +00:00
|
|
|
if (_patches.ai_disable_veh_train && _patches.ai_disable_veh_roadveh &&
|
|
|
|
_patches.ai_disable_veh_aircraft && _patches.ai_disable_veh_ship)
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (r < 0x7626) {
|
2004-08-11 21:35:52 +00:00
|
|
|
if (_patches.ai_disable_veh_train) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
AiWantTrainRoute(p);
|
|
|
|
} else if (r < 0xC4EA) {
|
2004-08-11 21:35:52 +00:00
|
|
|
if (_patches.ai_disable_veh_roadveh) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
AiWantRoadRoute(p);
|
|
|
|
} else if (r < 0xD89B) {
|
2004-08-11 21:35:52 +00:00
|
|
|
if (_patches.ai_disable_veh_aircraft) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
AiWantAircraftRoute(p);
|
|
|
|
} else {
|
2004-08-11 21:35:52 +00:00
|
|
|
if (_patches.ai_disable_veh_ship) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
AiWantShipRoute(p);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// 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;
|
2004-12-08 15:46:13 +00:00
|
|
|
int rad;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
for(;p->mode != 4;p++) if (p->mode == 1) {
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
w = ((p->attr>>1) & 7);
|
|
|
|
h = ((p->attr>>4) & 7);
|
|
|
|
if (p->attr&1) intswap(w, h);
|
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
|
|
|
|
if (_patches.modified_catchment) {
|
|
|
|
rad = CA_TRAIN;
|
|
|
|
} else {
|
|
|
|
rad = 4;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (cargo & 0x80) {
|
2004-12-08 15:46:13 +00:00
|
|
|
GetProductionAroundTiles(values, tile2, w, h, rad);
|
2004-08-09 17:04:08 +00:00
|
|
|
return values[cargo & 0x7F] != 0;
|
|
|
|
} else {
|
2004-12-08 15:46:13 +00:00
|
|
|
GetAcceptanceAroundTiles(values, tile2, w, h, rad);
|
2004-08-09 17:04:08 +00:00
|
|
|
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)
|
|
|
|
{
|
2005-04-20 22:30:40 +00:00
|
|
|
int32 ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
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.
|
2005-01-06 11:39:00 +00:00
|
|
|
uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_cleared_town = NULL;
|
|
|
|
|
|
|
|
if (p->mode < 2) {
|
|
|
|
if (p->mode == 0) {
|
|
|
|
// Depot
|
2005-04-20 22:30:40 +00:00
|
|
|
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);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
// Station
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(c, (p->attr&1) | (p->attr>>4)<<8 | (p->attr>>1&7)<<16, _cur_ai_player->ai.railtype_to_use, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_RAILROAD_STATION);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(ret)) return CMD_ERROR;
|
|
|
|
total_cost += ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
clear_town_stuff:;
|
|
|
|
if (_cleared_town != NULL) {
|
|
|
|
if (t != NULL && t != _cleared_town)
|
|
|
|
return CMD_ERROR;
|
|
|
|
t = _cleared_town;
|
|
|
|
rating += _cleared_town_rating;
|
|
|
|
}
|
|
|
|
} else if (p->mode == 2) {
|
|
|
|
// Rail
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(c, MP_RAILWAY))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
j = p->attr;
|
|
|
|
k = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Build the rail
|
|
|
|
for(i=0; i!=6; i++,j>>=1) {
|
|
|
|
if (j&1) {
|
|
|
|
k = i;
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(c, _cur_ai_player->ai.railtype_to_use, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
|
|
|
|
if (CmdFailed(ret)) return CMD_ERROR;
|
|
|
|
total_cost += ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* signals too? */
|
|
|
|
if (j&3) {
|
|
|
|
// Can't build signals on a road.
|
2005-04-20 22:30:40 +00:00
|
|
|
if (IsTileType(c, MP_STREET)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flag & DC_EXEC) {
|
|
|
|
j = 4 - j;
|
|
|
|
do {
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(c, k, 0, flag, CMD_BUILD_SIGNALS);
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (--j);
|
|
|
|
} else {
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = _price.build_signals;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(ret)) return CMD_ERROR;
|
|
|
|
total_cost += ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else if (p->mode == 3) {
|
|
|
|
//Clear stuff and then build single rail.
|
|
|
|
if (GetTileSlope(c,NULL) != 0)
|
|
|
|
return CMD_ERROR;
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR);
|
|
|
|
if (CmdFailed(ret)) return CMD_ERROR;
|
|
|
|
total_cost += ret + _price.build_rail;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flag & DC_EXEC) {
|
|
|
|
DoCommandByTile(c, _cur_ai_player->ai.railtype_to_use, p->attr&1, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_SINGLE_RAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto clear_town_stuff;
|
|
|
|
} else {
|
|
|
|
// Unk
|
|
|
|
break;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flag & DC_EXEC)) {
|
|
|
|
if (t != NULL && rating > t->ratings[_current_player]) {
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns rule and cost
|
|
|
|
static int AiBuildDefaultRailTrack(TileIndex tile, byte p0, byte p1, byte p2, byte p3, byte dir, byte cargo, int32 *cost)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const AiDefaultRailBlock *p;
|
|
|
|
|
|
|
|
for(i=0; (p = _default_rail_track_data[i]) != NULL; i++) {
|
2004-09-10 19:02:27 +00:00
|
|
|
if (p->p0 == p0 && p->p1 == p1 && p->p2 == p2 && p->p3 == p3 &&
|
2004-08-09 17:04:08 +00:00
|
|
|
(p->dir == 0xFF || p->dir == dir || ((p->dir-1)&3) == dir)) {
|
|
|
|
*cost = AiDoBuildDefaultRailTrack(tile, p->data, DC_NO_TOWN_RATING);
|
|
|
|
if (*cost != CMD_ERROR && AiCheckTrackResources(tile, p->data, cargo))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const byte _terraform_up_flags[] = {
|
|
|
|
14, 13, 12, 11,
|
|
|
|
10, 9, 8, 7,
|
|
|
|
6, 5, 4, 3,
|
|
|
|
2, 1, 0, 1,
|
|
|
|
2, 1, 4, 1,
|
|
|
|
2, 1, 8, 1,
|
|
|
|
2, 1, 4, 2,
|
|
|
|
2, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte _terraform_down_flags[] = {
|
|
|
|
1, 2, 3, 4,
|
|
|
|
5, 6, 1, 8,
|
|
|
|
9, 10, 8, 12,
|
|
|
|
4, 2, 0, 0,
|
|
|
|
1, 2, 3, 4,
|
|
|
|
5, 6, 2, 8,
|
|
|
|
9, 10, 1, 12,
|
|
|
|
8, 4
|
|
|
|
};
|
|
|
|
|
|
|
|
static void AiDoTerraformLand(TileIndex tile, int dir, int unk, int mode)
|
|
|
|
{
|
|
|
|
byte old_player;
|
|
|
|
uint32 r;
|
|
|
|
uint slope;
|
2005-02-07 10:41:45 +00:00
|
|
|
uint h;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
old_player = _current_player;
|
|
|
|
_current_player = OWNER_NONE;
|
|
|
|
|
|
|
|
r = Random();
|
|
|
|
|
|
|
|
unk &= (int)r;
|
|
|
|
|
|
|
|
do {
|
2005-01-05 13:32:03 +00:00
|
|
|
tile = TILE_MASK(tile + TileOffsByDir(dir));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
r >>= 2;
|
|
|
|
if (r&2) {
|
|
|
|
dir++;
|
|
|
|
if (r&1)
|
|
|
|
dir -= 2;
|
|
|
|
}
|
|
|
|
dir &= 3;
|
|
|
|
} while (--unk >= 0);
|
|
|
|
|
|
|
|
slope = GetTileSlope(tile, &h);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (slope != 0) {
|
|
|
|
if (mode > 0 || (mode == 0 && !(r&0xC))) {
|
|
|
|
// Terraform up
|
|
|
|
DoCommandByTile(tile, _terraform_up_flags[slope-1], 1,
|
|
|
|
DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
|
|
|
|
} else if (h != 0) {
|
|
|
|
// Terraform down
|
2004-09-10 19:02:27 +00:00
|
|
|
DoCommandByTile(tile, _terraform_down_flags[slope-1], 0,
|
2004-08-09 17:04:08 +00:00
|
|
|
DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_current_player = old_player;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateBuildDefaultRailBlocks(Player *p)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
AiBuildRec *aib;
|
|
|
|
int rule;
|
|
|
|
int32 cost;
|
|
|
|
|
|
|
|
// time out?
|
|
|
|
if (++p->ai.timeout_counter == 1388) {
|
|
|
|
p->ai.state = AIS_DELETE_RAIL_BLOCKS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do the following 8 times
|
|
|
|
i = 8;
|
|
|
|
do {
|
|
|
|
// check if we can build the default track
|
|
|
|
aib = &p->ai.src;
|
|
|
|
j = p->ai.num_build_rec;
|
|
|
|
do {
|
|
|
|
// this item has already been built?
|
|
|
|
if (aib->cur_building_rule != 255)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// adjust the coordinate randomly,
|
|
|
|
// to make sure that we find a position.
|
|
|
|
aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng);
|
|
|
|
|
|
|
|
// check if the track can be build there.
|
|
|
|
rule = AiBuildDefaultRailTrack(aib->use_tile,
|
|
|
|
p->ai.build_kind, p->ai.num_wagons,
|
|
|
|
aib->unk6, aib->unk7,
|
|
|
|
aib->direction, aib->cargo, &cost);
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
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++;
|
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
return tile + ToTileIndexDiff(p->tileoffs) - TileOffsByDir(*dir = p->attr);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct AiRailPathFindData {
|
|
|
|
TileIndex tile;
|
|
|
|
TileIndex tile2;
|
|
|
|
int count;
|
|
|
|
bool flag;
|
|
|
|
} AiRailPathFindData;
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool AiEnumFollowTrack(TileIndex tile, AiRailPathFindData *a, int track, uint length, byte *state)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (a->flag)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (length > 20 || tile == a->tile) {
|
|
|
|
a->flag = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceMax(tile, a->tile2) < 4)
|
2004-08-09 17:04:08 +00:00
|
|
|
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;
|
2005-01-05 13:32:03 +00:00
|
|
|
FollowTrack(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, p->ai.cur_dir_a^2,
|
2004-08-09 17:04:08 +00:00
|
|
|
(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},
|
|
|
|
{0, 2, 2, 3, 5, 1, 128+2, 66},
|
|
|
|
{1, 3, 5, 0, 3, 2, 128+3, 67}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte _dir_table_1[] = {3, 9, 12, 6};
|
|
|
|
static const byte _dir_table_2[] = {12, 6, 3, 9};
|
|
|
|
|
|
|
|
|
|
|
|
static bool AiIsTileBanned(Player *p, TileIndex tile, byte val) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i!=p->ai.banned_tile_count; i++)
|
2004-09-10 19:02:27 +00:00
|
|
|
if (p->ai.banned_tiles[i] == tile &&
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.banned_val[i] == val)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiBanTile(Player *p, TileIndex tile, byte val) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=lengthof(p->ai.banned_tiles)-1; i!=0; i--) {
|
|
|
|
p->ai.banned_tiles[i] = p->ai.banned_tiles[i-1];
|
|
|
|
p->ai.banned_val[i] = p->ai.banned_val[i-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
p->ai.banned_tiles[0] = tile;
|
|
|
|
p->ai.banned_val[0] = val;
|
|
|
|
|
|
|
|
if (p->ai.banned_tile_count != lengthof(p->ai.banned_tiles))
|
|
|
|
p->ai.banned_tile_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, int dir);
|
|
|
|
|
|
|
|
static bool AiCheckRailPathBetter(AiRailFinder *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->best_dir = arf->cur_best_dir;
|
|
|
|
arf->best_dist = arf->cur_best_dist;
|
|
|
|
arf->best_ptr = p;
|
|
|
|
arf->best_tile = arf->cur_best_tile;
|
|
|
|
better = true;
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2004-12-03 07:43:00 +00:00
|
|
|
static inline void AiCheckBuildRailBridgeHere(AiRailFinder *arf, TileIndex tile, const byte *p)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
TileIndex tile_new;
|
|
|
|
bool flag;
|
|
|
|
|
|
|
|
int dir2 = p[0] & 3;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
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(;;) {
|
2005-01-29 18:22:07 +00:00
|
|
|
if ((TileIndexDiff)tile_new < -TileOffsByDir(dir2)) return; // Wraping around map, no bridge possible!
|
2005-01-05 13:32:03 +00:00
|
|
|
tile_new = TILE_MASK(tile_new + TileOffsByDir(dir2));
|
2004-08-09 17:04:08 +00:00
|
|
|
FindLandscapeHeightByTile(&arf->ti, tile_new);
|
|
|
|
if (arf->ti.tileh != 0 || arf->ti.type == MP_CLEAR || arf->ti.type == MP_TREES) {
|
|
|
|
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)?
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(DoCommandByTile(tile_new, tile, 0 | arf->player->ai.railtype_to_use << 8,
|
|
|
|
DC_AUTO, CMD_BUILD_BRIDGE)) )
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
AiBuildRailRecursive(arf, tile_new, dir2);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// At the bottom depth, check if the new path is better than the old one.
|
|
|
|
if (arf->depth == 1) {
|
|
|
|
if (AiCheckRailPathBetter(arf, p))
|
|
|
|
arf->bridge_end_tile = tile_new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-03 07:43:00 +00:00
|
|
|
static inline void AiCheckBuildRailTunnelHere(AiRailFinder *arf, TileIndex tile, const byte *p)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
FindLandscapeHeightByTile(&arf->ti, tile);
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
if (arf->ti.tileh == _dir_table_2[p[0]&3] && arf->ti.z!=0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 cost = DoCommandByTile(tile, arf->player->ai.railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL);
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(cost) && cost <= (arf->player->player_money>>4)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
AiBuildRailRecursive(arf, _build_tunnel_endtile, p[0]&3);
|
|
|
|
if (arf->depth == 1) {
|
|
|
|
AiCheckRailPathBetter(arf, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, int dir)
|
|
|
|
{
|
|
|
|
const byte *p;
|
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
tile = TILE_MASK(tile + TileOffsByDir(dir));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Reached destination?
|
|
|
|
if (tile == arf->final_tile) {
|
|
|
|
if (arf->final_dir != (dir^2)) {
|
|
|
|
if (arf->recursive_mode != 2)
|
|
|
|
arf->recursive_mode = 1;
|
|
|
|
} else if (arf->recursive_mode != 2) {
|
|
|
|
arf->recursive_mode = 2;
|
|
|
|
arf->cur_best_depth = arf->depth;
|
|
|
|
} else {
|
|
|
|
if (arf->depth < arf->cur_best_depth)
|
|
|
|
arf->cur_best_depth = arf->depth;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Depth too deep?
|
|
|
|
if (arf->depth >= 4) {
|
2005-01-31 07:23:15 +00:00
|
|
|
uint dist = DistanceMaxPlusManhattan(tile, arf->final_tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (dist < arf->cur_best_dist) {
|
|
|
|
// Store the tile that is closest to the final position.
|
|
|
|
arf->cur_best_depth = arf->depth;
|
|
|
|
arf->cur_best_dist = dist;
|
|
|
|
arf->cur_best_tile = tile;
|
|
|
|
arf->cur_best_dir = dir;
|
|
|
|
}
|
|
|
|
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 the tile is not in the list of banned tiles and that a rail can be built here.
|
|
|
|
if (!AiIsTileBanned(arf->player, tile, p[0]) &&
|
2005-04-20 22:30:40 +00:00
|
|
|
!CmdFailed(DoCommandByTile(tile, arf->player->ai.railtype_to_use, p[0], DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP, CMD_BUILD_SINGLE_RAIL))) {
|
2004-08-09 17:04:08 +00:00
|
|
|
AiBuildRailRecursive(arf, tile, p[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// At the bottom depth?
|
|
|
|
if (arf->depth == 1) {
|
|
|
|
AiCheckRailPathBetter(arf, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
p += 2;
|
|
|
|
} while (!(p[0]&0x80));
|
|
|
|
}
|
|
|
|
|
|
|
|
AiCheckBuildRailBridgeHere(arf, tile, p);
|
|
|
|
AiCheckBuildRailTunnelHere(arf, tile, p+1);
|
|
|
|
|
|
|
|
arf->depth--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
static const byte _dir_table_3[]= {0x25, 0x2A, 0x19, 0x16};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static void AiBuildRailConstruct(Player *p)
|
|
|
|
{
|
|
|
|
AiRailFinder arf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// Check too much lookahead?
|
|
|
|
if (AiDoFollowTrack(p)) {
|
|
|
|
p->ai.state_counter = (Random()&0xE)+6; // Destruct this amount of blocks
|
|
|
|
p->ai.state_mode = 1; // Start destruct
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Ban this tile and don't reach it for a while.
|
|
|
|
AiBanTile(p, p->ai.cur_tile_a, FindFirstBit(GetRailTrackStatus(p->ai.cur_tile_a)));
|
|
|
|
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;
|
2004-09-10 19:02:27 +00:00
|
|
|
AiBuildRailRecursive(&arf, p->ai.cur_tile_a, p->ai.cur_dir_a);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
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_counter = 40;
|
|
|
|
p->ai.state_mode = 1;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Ban this tile
|
|
|
|
AiBanTile(p, p->ai.cur_tile_a, FindFirstBit(GetRailTrackStatus(p->ai.cur_tile_a)));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
p->ai.cur_tile_a += TileOffsByDir(p->ai.cur_dir_a);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (arf.best_ptr[0]&0x80) {
|
|
|
|
int i;
|
|
|
|
int32 bridge_len = GetBridgeLength(arf.bridge_end_tile, p->ai.cur_tile_a);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* 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
|
|
|
|
*/
|
2005-03-25 17:30:54 +00:00
|
|
|
for (i = MAX_BRIDGES - 1; i != 0; i--) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (CheckBridge_Stuff(i, bridge_len)) {
|
2005-03-25 17:30:54 +00:00
|
|
|
int32 cost = DoCommandByTile(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO, CMD_BUILD_BRIDGE);
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(cost) && cost < (p->player_money >> 5))
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build it
|
2005-03-25 17:30:54 +00:00
|
|
|
DoCommandByTile(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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];
|
2004-09-10 19:02:27 +00:00
|
|
|
DoCommandByTile(p->ai.cur_tile_a, p->ai.railtype_to_use, arf.best_ptr[0],
|
2004-08-09 17:04:08 +00:00
|
|
|
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;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile = p->ai.cur_tile_a;
|
2004-08-09 17:04:08 +00:00
|
|
|
int offs;
|
|
|
|
TileIndex tilenew;
|
|
|
|
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2005-07-13 18:04:01 +00:00
|
|
|
if (!(_m[tile].m5 & 0x80)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// Clear the tunnel and continue at the other side of it.
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
2005-01-05 13:32:03 +00:00
|
|
|
p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDir(p->ai.cur_dir_a));
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
if (!(_m[tile].m5 & 0x40)) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Check if the bridge points in the right direction.
|
|
|
|
// This is not really needed the first place AiRemoveTileAndGoForward is called.
|
2005-07-13 18:04:01 +00:00
|
|
|
if ((_m[tile].m5&1) != (p->ai.cur_dir_a&1))
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find other side of bridge.
|
2005-01-05 13:32:03 +00:00
|
|
|
offs = TileOffsByDir(p->ai.cur_dir_a);
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
|
|
|
tile = TILE_MASK(tile - offs);
|
2005-07-13 18:04:01 +00:00
|
|
|
} while (_m[tile].m5 & 0x40);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
tilenew = TILE_MASK(tile - offs);
|
|
|
|
// And clear the bridge.
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
|
2004-08-09 17:04:08 +00:00
|
|
|
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.
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile, MP_RAILWAY) &&
|
2005-07-13 18:04:01 +00:00
|
|
|
(_m[tile].m5&0xC0) == 0x40) {
|
2004-08-09 17:04:08 +00:00
|
|
|
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_REMOVE_SIGNALS);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// And also remove the rail.
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(DoCommandByTile(tile, 0, bit, DC_EXEC, CMD_REMOVE_SINGLE_RAIL)) )
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find the direction at the other edge of the rail.
|
|
|
|
ptr = _ai_table_15[p->ai.cur_dir_a^2];
|
|
|
|
while (ptr[0] != bit) ptr+=2;
|
|
|
|
p->ai.cur_dir_a = ptr[1] ^ 2;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// And then also switch tile.
|
2005-01-05 13:32:03 +00:00
|
|
|
p->ai.cur_tile_a = TILE_MASK(p->ai.cur_tile_a - TileOffsByDir(p->ai.cur_dir_a));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void AiBuildRailDestruct(Player *p)
|
|
|
|
{
|
|
|
|
// Decrease timeout.
|
|
|
|
if (!--p->ai.state_counter) {
|
|
|
|
p->ai.state_mode = 2;
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't do anything if the destination is already reached.
|
|
|
|
if (p->ai.cur_tile_a == p->ai.start_tile_a)
|
|
|
|
return;
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
AiRemoveTileAndGoForward(p);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void AiBuildRail(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (p->ai.state_mode < 1) {
|
|
|
|
// Construct mode, build new rail.
|
2004-09-10 19:02:27 +00:00
|
|
|
AiBuildRailConstruct(p);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (p->ai.state_mode == 1) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Destruct mode, destroy the rail currently built.
|
|
|
|
AiBuildRailDestruct(p);
|
|
|
|
} else if (p->ai.state_mode == 2) {
|
|
|
|
|
|
|
|
// Terraform some and then try building again.
|
|
|
|
for(i=0; i!=4; i++)
|
|
|
|
AiDoTerraformLand(p->ai.cur_tile_a, p->ai.cur_dir_a, 3, 0);
|
|
|
|
|
|
|
|
if (++p->ai.state_counter == 4) {
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
p->ai.state_mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateBuildRail(Player *p)
|
|
|
|
{
|
|
|
|
int num;
|
|
|
|
AiBuildRec *aib;
|
|
|
|
byte cmd;
|
|
|
|
TileIndex tile;
|
|
|
|
int dir;
|
|
|
|
|
|
|
|
// time out?
|
|
|
|
if (++p->ai.timeout_counter == 1388) {
|
|
|
|
p->ai.state = AIS_DELETE_RAIL_BLOCKS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently building a rail between two points?
|
|
|
|
if (p->ai.state_mode != 255) {
|
|
|
|
AiBuildRail(p);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Alternate between edges
|
|
|
|
swap_tile(&p->ai.start_tile_a, &p->ai.start_tile_b);
|
|
|
|
swap_tile(&p->ai.cur_tile_a, &p->ai.cur_tile_b);
|
|
|
|
swap_byte(&p->ai.start_dir_a, &p->ai.start_dir_b);
|
|
|
|
swap_byte(&p->ai.cur_dir_a, &p->ai.cur_dir_b);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
cmd = aib->buildcmd_b;
|
|
|
|
aib->buildcmd_b = 255;
|
|
|
|
if (cmd != 255) break;
|
|
|
|
|
|
|
|
aib++;
|
|
|
|
if (--num == 0) {
|
|
|
|
p->ai.state = AIS_BUILD_RAIL_VEH;
|
|
|
|
p->ai.state_counter = 0; // timeout
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find first edge to build from.
|
|
|
|
tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, cmd&3, &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;
|
2005-01-05 13:32:03 +00:00
|
|
|
DoCommandByTile(TILE_MASK(tile + TileOffsByDir(dir)), 0, (dir&1)?1:0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
assert(TILE_MASK(tile) != 0xFF00);
|
|
|
|
|
|
|
|
// Find second edge to build to
|
|
|
|
aib = (&p->ai.src) + ((cmd >> 4)&0xF);
|
|
|
|
tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, (cmd>>2)&3, &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;
|
2005-01-05 13:32:03 +00:00
|
|
|
DoCommandByTile(TILE_MASK(tile + TileOffsByDir(dir)), 0, (dir&1)?1:0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
assert(TILE_MASK(tile) != 0xFF00);
|
|
|
|
|
|
|
|
// And setup state.
|
|
|
|
p->ai.state_mode = 2;
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
p->ai.banned_tile_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int AiGetStationIdByDef(TileIndex tile, int id)
|
|
|
|
{
|
|
|
|
const AiDefaultBlockData *p = _default_rail_track_data[id]->data;
|
|
|
|
while (p->mode != 1) p++;
|
2005-07-13 18:04:01 +00:00
|
|
|
return _m[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))].m2;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateBuildRailVeh(Player *p)
|
|
|
|
{
|
|
|
|
const AiDefaultBlockData *ptr;
|
|
|
|
TileIndex tile;
|
|
|
|
int i, veh;
|
|
|
|
int cargo;
|
|
|
|
int32 cost;
|
|
|
|
Vehicle *v;
|
|
|
|
uint loco_id;
|
|
|
|
|
|
|
|
ptr = _default_rail_track_data[p->ai.src.cur_building_rule]->data;
|
|
|
|
while (ptr->mode != 0) { ptr++; }
|
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
cargo = p->ai.cargo_type;
|
|
|
|
for(i=0;;) {
|
|
|
|
if (p->ai.wagon_list[i] == INVALID_VEHICLE) {
|
|
|
|
veh = _cargoc.ai_railwagon[p->ai.railtype_to_use][cargo];
|
|
|
|
cost = DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE);
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(cost)) goto handle_nocash;
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.wagon_list[i] = _new_wagon_id;
|
|
|
|
p->ai.wagon_list[i+1] = INVALID_VEHICLE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cargo == CT_MAIL)
|
|
|
|
cargo = CT_PASSENGERS;
|
|
|
|
if (++i == p->ai.num_wagons * 2 - 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Which locomotive to build?
|
2005-01-30 13:03:32 +00:00
|
|
|
veh = AiChooseTrainToBuild(p->ai.railtype_to_use, p->player_money, (cargo!=CT_PASSENGERS)?1:0, tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (veh == -1) {
|
|
|
|
handle_nocash:
|
|
|
|
// after a while, if AI still doesn't have cash, get out of this block by selling the wagons.
|
|
|
|
if (++p->ai.state_counter == 1000) {
|
|
|
|
for(i=0; p->ai.wagon_list[i] != INVALID_VEHICLE; i++) {
|
|
|
|
cost = DoCommandByTile(tile, p->ai.wagon_list[i], 0, DC_EXEC, CMD_SELL_RAIL_WAGON);
|
2005-04-20 22:30:40 +00:00
|
|
|
assert(!CmdFailed(cost));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
p->ai.state = AIS_0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to build the locomotive
|
|
|
|
cost = DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE);
|
2005-04-20 22:30:40 +00:00
|
|
|
assert(!CmdFailed(cost));
|
2004-08-09 17:04:08 +00:00
|
|
|
loco_id = _new_train_id;
|
|
|
|
|
|
|
|
// Sell a vehicle if the train is double headed.
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(loco_id);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->next != NULL) {
|
|
|
|
i = p->ai.wagon_list[p->ai.num_wagons*2-2];
|
|
|
|
p->ai.wagon_list[p->ai.num_wagons*2-2] = INVALID_VEHICLE;
|
|
|
|
DoCommandByTile(tile, i, 0, DC_EXEC, CMD_SELL_RAIL_WAGON);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the wagons onto the train
|
|
|
|
for(i=0; p->ai.wagon_list[i] != INVALID_VEHICLE; i++) {
|
|
|
|
DoCommandByTile(tile, p->ai.wagon_list[i] | (loco_id << 16), 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2004-12-05 21:30:58 +00:00
|
|
|
Order order;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-05 21:30:58 +00:00
|
|
|
order.type = OT_GOTO_STATION;
|
|
|
|
order.flags = 0;
|
|
|
|
order.station = AiGetStationIdByDef(aib->use_tile, aib->cur_building_rule);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 21:30:58 +00:00
|
|
|
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);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DoCommandByTile(0, loco_id, 0, DC_EXEC, CMD_START_STOP_TRAIN);
|
2004-09-10 18:54:23 +00:00
|
|
|
|
|
|
|
DoCommandByTile(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
num = p->ai.num_build_rec;
|
2004-08-09 17:04:08 +00:00
|
|
|
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) {
|
2005-01-06 11:39:00 +00:00
|
|
|
DoCommandByTile(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
b++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (++aib,--num);
|
|
|
|
|
|
|
|
p->ai.state = AIS_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool AiCheckRoadResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
|
|
|
|
{
|
|
|
|
uint values[NUM_CARGO];
|
2004-12-08 15:46:13 +00:00
|
|
|
int rad;
|
2004-12-30 10:03:35 +00:00
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
if (_patches.modified_catchment) {
|
2004-12-30 10:03:35 +00:00
|
|
|
rad = CA_TRUCK; //Same as CA_BUS at the moment?
|
2004-12-08 15:46:13 +00:00
|
|
|
} else { //change that at some point?
|
|
|
|
rad = 4;
|
|
|
|
}
|
2004-12-30 10:03:35 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
for(;;p++) {
|
|
|
|
if (p->mode == 4) {
|
|
|
|
return true;
|
|
|
|
} else if (p->mode == 1) {
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (cargo & 0x80) {
|
2004-12-08 15:46:13 +00:00
|
|
|
GetProductionAroundTiles(values, tile2, 1, 1, rad);
|
2004-08-09 17:04:08 +00:00
|
|
|
return values[cargo & 0x7F] != 0;
|
|
|
|
} else {
|
2004-12-08 15:46:13 +00:00
|
|
|
GetAcceptanceAroundTiles(values, tile2, 1, 1, rad);
|
2004-08-09 17:04:08 +00:00
|
|
|
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)
|
|
|
|
{
|
2005-04-20 22:30:40 +00:00
|
|
|
int32 ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 total_cost = 0;
|
|
|
|
Town *t = NULL;
|
|
|
|
int rating = 0;
|
|
|
|
int roadflag = 0;
|
|
|
|
|
|
|
|
for(;p->mode != 4;p++) {
|
2005-01-06 11:39:00 +00:00
|
|
|
uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_cleared_town = NULL;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (p->mode == 2) {
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(c, MP_STREET) &&
|
2005-07-13 18:04:01 +00:00
|
|
|
(_m[c].m5&0xF0)==0 &&
|
|
|
|
(_m[c].m5&p->attr)!=0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
roadflag |= 2;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// all bits are already built?
|
2005-07-13 18:04:01 +00:00
|
|
|
if ((_m[c].m5&p->attr)==p->attr)
|
2004-08-09 17:04:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
|
|
|
|
if (CmdFailed(ret)) return CMD_ERROR;
|
|
|
|
total_cost += ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->mode == 0) {
|
|
|
|
// Depot
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_DEPOT);
|
2004-08-09 17:04:08 +00:00
|
|
|
goto clear_town_stuff;
|
|
|
|
} else if (p->mode == 1) {
|
|
|
|
if (_want_road_truck_station) {
|
|
|
|
// Truck station
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(c, p->attr, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
// Bus station
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(c, p->attr, RS_BUS, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
clear_town_stuff:;
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(ret)) return CMD_ERROR;
|
|
|
|
total_cost += ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (_cleared_town != NULL) {
|
|
|
|
if (t != NULL && t != _cleared_town)
|
|
|
|
return CMD_ERROR;
|
|
|
|
t = _cleared_town;
|
|
|
|
rating += _cleared_town_rating;
|
|
|
|
}
|
|
|
|
} else if (p->mode == 3) {
|
|
|
|
if (flag & DC_EXEC)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (GetTileSlope(c, NULL) != 0)
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
if (!(IsTileType(c, MP_STREET) && (_m[c].m5 & 0xF0) == 0)) {
|
2005-04-20 22:30:40 +00:00
|
|
|
ret = DoCommandByTile(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR);
|
|
|
|
if (CmdFailed(ret)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_want_road_truck_station && !(roadflag&2))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (!(flag & DC_EXEC)) {
|
|
|
|
if (t != NULL && rating > t->ratings[_current_player]) {
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return total_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the blocks are not too close to each other
|
|
|
|
static bool AiCheckBlockDistances(Player *p, TileIndex tile)
|
|
|
|
{
|
|
|
|
AiBuildRec *aib;
|
|
|
|
int num;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
num = p->ai.num_build_rec;
|
2004-09-10 19:02:27 +00:00
|
|
|
aib = &p->ai.src;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (aib->cur_building_rule != 255) {
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceManhattan(aib->use_tile, tile) < 9)
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (++aib, --num);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void AiStateBuildDefaultRoadBlocks(Player *p)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
AiBuildRec *aib;
|
|
|
|
int rule;
|
|
|
|
int32 cost;
|
|
|
|
|
|
|
|
// time out?
|
|
|
|
if (++p->ai.timeout_counter == 1388) {
|
|
|
|
p->ai.state = AIS_DELETE_RAIL_BLOCKS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do the following 8 times
|
|
|
|
i = 8;
|
|
|
|
do {
|
|
|
|
// check if we can build the default track
|
|
|
|
aib = &p->ai.src;
|
|
|
|
j = p->ai.num_build_rec;
|
|
|
|
do {
|
|
|
|
// this item has already been built?
|
|
|
|
if (aib->cur_building_rule != 255)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// adjust the coordinate randomly,
|
|
|
|
// to make sure that we find a position.
|
|
|
|
aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng);
|
|
|
|
|
|
|
|
// check if the road can be built there.
|
|
|
|
rule = AiFindBestDefaultRoadBlock(aib->use_tile,
|
|
|
|
aib->direction, aib->cargo, &cost);
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
int32 r;
|
|
|
|
|
|
|
|
// player has money, build it.
|
|
|
|
aib->cur_building_rule = rule;
|
|
|
|
|
|
|
|
r = AiDoBuildDefaultRoadBlock(
|
|
|
|
aib->use_tile,
|
|
|
|
_road_default_block_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_ROAD;
|
|
|
|
p->ai.state_mode = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
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;
|
|
|
|
} AiRoadFinder;
|
|
|
|
|
|
|
|
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.
|
2004-09-10 19:02:27 +00:00
|
|
|
if (arf->cur_best_dist < arf->best_dist ||
|
2004-08-09 17:04:08 +00:00
|
|
|
(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;
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool AiEnumFollowRoad(TileIndex tile, AiRoadEnum *a, int track, uint length, byte *state)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-31 07:23:15 +00:00
|
|
|
uint dist = DistanceManhattan(tile, a->dest);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (dist <= a->best_dist) {
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
|
|
|
|
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile2, MP_STREET) &&
|
2005-07-13 18:04:01 +00:00
|
|
|
(_m[tile2].m5&0xF0) == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static bool AiCheckRoadFinished(Player *p)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
AiRoadEnum are;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
int dir = p->ai.cur_dir_a;
|
|
|
|
uint32 bits;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
are.dest = p->ai.cur_tile_b;
|
2005-01-05 13:32:03 +00:00
|
|
|
tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(dir));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-05 16:15:22 +00:00
|
|
|
bits = GetTileTrackStatus(tile, TRANSPORT_ROAD) & _ai_road_table_and[dir];
|
2004-08-09 17:04:08 +00:00
|
|
|
if (bits == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
are.best_dist = (uint)-1;
|
|
|
|
|
|
|
|
for_each_bit(i, bits) {
|
2004-09-05 16:15:22 +00:00
|
|
|
FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, _dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceManhattan(tile, are.dest) <= are.best_dist)
|
2004-08-09 17:04:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool AiBuildRoadHelper(TileIndex tile, int flags, int type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
static const byte _road_bits[] = {
|
|
|
|
8+2,
|
|
|
|
1+4,
|
|
|
|
1+8,
|
|
|
|
4+2,
|
|
|
|
1+2,
|
|
|
|
8+4,
|
|
|
|
};
|
2005-04-20 22:30:40 +00:00
|
|
|
return !CmdFailed(DoCommandByTile(tile, _road_bits[type], 0, flags, CMD_BUILD_ROAD));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-03 07:43:00 +00:00
|
|
|
static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
TileIndex tile_new;
|
|
|
|
bool flag;
|
|
|
|
|
|
|
|
int dir2 = p[0] & 3;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
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(;;) {
|
2005-01-29 18:22:07 +00:00
|
|
|
if ((TileIndexDiff)tile_new < -TileOffsByDir(dir2)) return; // Wraping around map, no bridge possible!
|
2005-01-05 13:32:03 +00:00
|
|
|
tile_new = TILE_MASK(tile_new + TileOffsByDir(dir2));
|
2004-08-09 17:04:08 +00:00
|
|
|
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)?
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(DoCommandByTile(tile_new, tile, 0x8000, DC_AUTO, CMD_BUILD_BRIDGE)))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
AiBuildRoadRecursive(arf, tile_new, dir2);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-03 07:43:00 +00:00
|
|
|
static inline void AiCheckBuildRoadTunnelHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
FindLandscapeHeightByTile(&arf->ti, tile);
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
if (arf->ti.tileh == _dir_table_2[p[0]&3] && arf->ti.z!=0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 cost = DoCommandByTile(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL);
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(cost) && cost <= (arf->player->player_money>>4)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
AiBuildRoadRecursive(arf, _build_tunnel_endtile, p[0]&3);
|
|
|
|
if (arf->depth == 1) {
|
|
|
|
AiCheckRoadPathBetter(arf, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void AiBuildRoadRecursive(AiRoadFinder *arf, TileIndex tile, int dir)
|
|
|
|
{
|
|
|
|
const byte *p;
|
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
tile = TILE_MASK(tile + TileOffsByDir(dir));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Reached destination?
|
|
|
|
if (tile == arf->final_tile) {
|
|
|
|
if ((arf->final_dir^2) == dir) {
|
|
|
|
arf->recursive_mode = 2;
|
|
|
|
arf->cur_best_depth = arf->depth;
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Depth too deep?
|
|
|
|
if (arf->depth >= 4) {
|
2005-01-31 07:23:15 +00:00
|
|
|
uint dist = DistanceMaxPlusManhattan(tile, arf->final_tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (dist < arf->cur_best_dist) {
|
|
|
|
// 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;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// 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;
|
2004-09-10 19:02:27 +00:00
|
|
|
AiBuildRoadRecursive(&arf, p->ai.cur_tile_a, p->ai.cur_dir_a);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
p->ai.cur_tile_a = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a));
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.cur_dir_a ^= 2;
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (arf.best_ptr[0]&0x80) {
|
|
|
|
int i;
|
|
|
|
int32 bridge_len;
|
|
|
|
p->ai.cur_tile_a = arf.bridge_end_tile;
|
|
|
|
bridge_len = GetBridgeLength(tile, p->ai.cur_tile_a); // tile
|
|
|
|
|
|
|
|
/* Figure out what (road)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
|
|
|
|
*/
|
2004-09-10 19:02:27 +00:00
|
|
|
for(i = 10; i != 0; i--) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (CheckBridge_Stuff(i, bridge_len)) {
|
|
|
|
int32 cost = DoCommandByTile(tile, p->ai.cur_tile_a, i + (0x80 << 8), DC_AUTO, CMD_BUILD_BRIDGE);
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(cost) && cost < (p->player_money >> 5))
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build it
|
|
|
|
DoCommandByTile(tile, p->ai.cur_tile_a, i + (0x80 << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
|
|
|
|
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
} else if (arf.best_ptr[0]&0x40) {
|
|
|
|
// tunnel
|
|
|
|
DoCommandByTile(tile, 0x200, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL);
|
|
|
|
p->ai.cur_tile_a = _build_tunnel_endtile;
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
} else {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// road
|
|
|
|
if (!AiBuildRoadHelper(tile, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, arf.best_ptr[0]))
|
|
|
|
goto do_some_terraform;
|
|
|
|
|
|
|
|
p->ai.cur_dir_a = arf.best_ptr[1];
|
|
|
|
p->ai.cur_tile_a = tile;
|
|
|
|
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 void AiBuildRoad(Player *p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (p->ai.state_mode < 1) {
|
|
|
|
// Construct mode, build new road.
|
2004-09-10 19:02:27 +00:00
|
|
|
AiBuildRoadConstruct(p);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (p->ai.state_mode == 1) {
|
|
|
|
// Destruct mode, not implemented for roads.
|
|
|
|
p->ai.state_mode = 2;
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
} else if (p->ai.state_mode == 2) {
|
|
|
|
|
|
|
|
// Terraform some and then try building again.
|
|
|
|
for(i=0; i!=4; i++)
|
|
|
|
AiDoTerraformLand(p->ai.cur_tile_a, p->ai.cur_dir_a, 3, 0);
|
|
|
|
|
|
|
|
if (++p->ai.state_counter == 4) {
|
|
|
|
p->ai.state_counter = 0;
|
|
|
|
p->ai.state_mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static TileIndex AiGetRoadBlockEdge(byte rule, TileIndex tile, int *dir)
|
|
|
|
{
|
|
|
|
const AiDefaultBlockData *p = _road_default_block_data[rule]->data;
|
|
|
|
while (p->mode != 1) p++;
|
|
|
|
*dir = p->attr;
|
2005-01-06 11:39:00 +00:00
|
|
|
return TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void AiStateBuildRoad(Player *p)
|
|
|
|
{
|
|
|
|
int num;
|
|
|
|
AiBuildRec *aib;
|
|
|
|
byte cmd;
|
|
|
|
TileIndex tile;
|
|
|
|
int dir;
|
|
|
|
|
|
|
|
// time out?
|
|
|
|
if (++p->ai.timeout_counter == 1388) {
|
|
|
|
p->ai.state = AIS_DELETE_ROAD_BLOCKS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently building a road between two points?
|
|
|
|
if (p->ai.state_mode != 255) {
|
|
|
|
AiBuildRoad(p);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Alternate between edges
|
|
|
|
swap_tile(&p->ai.start_tile_a, &p->ai.start_tile_b);
|
|
|
|
swap_tile(&p->ai.cur_tile_a, &p->ai.cur_tile_b);
|
|
|
|
swap_byte(&p->ai.start_dir_a, &p->ai.start_dir_b);
|
|
|
|
swap_byte(&p->ai.cur_dir_a, &p->ai.cur_dir_b);
|
|
|
|
|
|
|
|
sw ^= 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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++;
|
2005-07-13 18:04:01 +00:00
|
|
|
return _m[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))].m2;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateBuildRoadVehicles(Player *p)
|
|
|
|
{
|
|
|
|
const AiDefaultBlockData *ptr;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
|
|
|
uint loco_id;
|
2004-08-09 17:04:08 +00:00
|
|
|
int veh, i;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
ptr = _road_default_block_data[p->ai.src.cur_building_rule]->data;
|
|
|
|
for(;ptr->mode != 0;ptr++) {}
|
2005-01-06 11:39:00 +00:00
|
|
|
tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 13:42:11 +00:00
|
|
|
veh = AiChooseRoadVehToBuild(p->ai.cargo_type, p->player_money, tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (veh == -1) {
|
|
|
|
p->ai.state = AIS_0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH))) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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));
|
2004-12-05 21:30:58 +00:00
|
|
|
Order order;
|
|
|
|
|
|
|
|
order.type = OT_GOTO_STATION;
|
|
|
|
order.flags = 0;
|
|
|
|
order.station = AiGetStationIdFromRoadBlock(aib->use_tile, aib->cur_building_rule);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 21:30:58 +00:00
|
|
|
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;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 21:30:58 +00:00
|
|
|
DoCommandByTile(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DoCommandByTile(0, loco_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
|
2004-09-10 18:54:23 +00:00
|
|
|
|
|
|
|
DoCommandByTile(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
num = p->ai.num_build_rec;
|
2004-08-09 17:04:08 +00:00
|
|
|
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) {
|
2005-01-06 11:39:00 +00:00
|
|
|
DoCommandByTile(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (++aib,--num);
|
|
|
|
|
|
|
|
p->ai.state = AIS_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool AiCheckIfHangar(Station *st)
|
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile = st->airport_tile;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// HANGAR of airports
|
|
|
|
// 0x20 - hangar large airport (32)
|
|
|
|
// 0x41 - hangar small airport (65)
|
2005-07-13 18:04:01 +00:00
|
|
|
return (_m[tile].m5 == 32 || _m[tile].m5 == 65);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Dismiss airports too far away.
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceMax(st->airport_tile, aib->spec_tile) > aib->rand_rng)
|
2004-08-09 17:04:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// It's ideal airport, let's take it!
|
|
|
|
|
|
|
|
/* XXX: This part is utterly broken - rule should
|
|
|
|
* contain number of the rule appropriate for the
|
|
|
|
* airport type (country, town, ...), see
|
|
|
|
* _airport_default_block_data (rule is just an index
|
|
|
|
* in this array). But the only difference between the
|
|
|
|
* currently existing two rules (rule 0 - town and rule
|
|
|
|
* 1 - country) is the attr field which is used only
|
|
|
|
* 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)
|
|
|
|
{
|
2005-04-20 22:30:40 +00:00
|
|
|
int32 total_cost = 0, ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
for(;p->mode == 0;p++) {
|
|
|
|
if (!HASBIT(_avail_aircraft, p->attr))
|
|
|
|
return CMD_ERROR;
|
2005-04-20 22:30:40 +00:00
|
|
|
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;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return total_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
|
|
|
|
{
|
|
|
|
uint values[NUM_CARGO];
|
|
|
|
int w,h;
|
2004-12-08 15:46:13 +00:00
|
|
|
int rad;
|
2004-12-30 10:03:35 +00:00
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
if (_patches.modified_catchment) {
|
2004-12-30 10:03:35 +00:00
|
|
|
rad = CA_AIR_LARGE; //I Have NFI what airport the
|
2004-12-08 15:46:13 +00:00
|
|
|
} else { //AI is going to build here
|
|
|
|
rad = 4;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
for(;p->mode==0;p++) {
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
w = _airport_size_x[p->attr];
|
|
|
|
h = _airport_size_y[p->attr];
|
|
|
|
if (cargo & 0x80) {
|
2004-12-08 15:46:13 +00:00
|
|
|
GetProductionAroundTiles(values, tile2, w, h, rad);
|
2004-08-09 17:04:08 +00:00
|
|
|
return values[cargo & 0x7F] != 0;
|
|
|
|
} else {
|
2004-12-08 15:46:13 +00:00
|
|
|
GetAcceptanceAroundTiles(values, tile2, w, h, rad);
|
2004-08-09 17:04:08 +00:00
|
|
|
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 {
|
2004-09-10 19:02:27 +00:00
|
|
|
// check if we can build the default
|
2004-08-09 17:04:08 +00:00
|
|
|
aib = &p->ai.src;
|
|
|
|
j = p->ai.num_build_rec;
|
|
|
|
do {
|
|
|
|
// this item has already been built?
|
|
|
|
if (aib->cur_building_rule != 255)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// adjust the coordinate randomly,
|
|
|
|
// to make sure that we find a position.
|
|
|
|
aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng);
|
|
|
|
|
|
|
|
// check if the aircraft stuff can be built there.
|
|
|
|
rule = AiFindBestDefaultAirportBlock(aib->use_tile, aib->cargo, p->ai.build_kind, &cost);
|
|
|
|
|
|
|
|
#if 0
|
2005-01-16 11:24:58 +00:00
|
|
|
if (!IsTileType(aib->use_tile, MP_STREET) &&
|
|
|
|
!IsTileType(aib->use_tile, MP_RAILWAY) &&
|
|
|
|
!IsTileType(aib->use_tile, MP_STATION)
|
2004-08-09 17:04:08 +00:00
|
|
|
) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
_m[aib->use_tile].type_height = 0xa1;
|
|
|
|
_m[aib->use_tile].m5 = 0x80;
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkTileDirtyByTile(aib->use_tile);
|
|
|
|
}
|
|
|
|
#endif
|
2005-05-26 19:43:56 +00:00
|
|
|
// SetRedErrorSquare(aib->use_tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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++;
|
2005-07-13 18:04:01 +00:00
|
|
|
return _m[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))].m2;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateBuildAircraftVehicles(Player *p)
|
|
|
|
{
|
|
|
|
const AiDefaultBlockData *ptr;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
int veh;
|
|
|
|
int i;
|
|
|
|
uint loco_id;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
ptr = _airport_default_block_data[p->ai.src.cur_building_rule];
|
|
|
|
for(;ptr->mode!=0;ptr++) {}
|
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
veh = AiChooseAircraftToBuild(p->player_money, p->ai.build_kind!=0 ? 1 : 0);
|
|
|
|
if (veh == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-04-20 22:30:40 +00:00
|
|
|
if (CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT))) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
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);
|
2004-12-05 21:30:58 +00:00
|
|
|
Order order;
|
|
|
|
|
|
|
|
order.type = OT_GOTO_STATION;
|
|
|
|
order.flags = 0;
|
|
|
|
order.station = AiGetStationIdFromAircraftBlock(aib->use_tile, aib->cur_building_rule);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 21:30:58 +00:00
|
|
|
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;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 21:30:58 +00:00
|
|
|
DoCommandByTile(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DoCommandByTile(0, loco_id, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
|
2004-09-10 18:54:23 +00:00
|
|
|
|
|
|
|
DoCommandByTile(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
error("!AiStateCheckShipStuff");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateBuildDefaultShipBlocks(Player *p)
|
|
|
|
{
|
|
|
|
// XXX
|
|
|
|
error("!AiStateBuildDefaultShipBlocks");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateDoShipStuff(Player *p)
|
|
|
|
{
|
|
|
|
// XXX
|
|
|
|
error("!AiStateDoShipStuff");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateSellVeh(Player *p)
|
|
|
|
{
|
|
|
|
Vehicle *v = p->ai.cur_veh;
|
|
|
|
|
|
|
|
if (v->owner == _current_player) {
|
|
|
|
if (v->type == VEH_Train) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_DEPOT)
|
2004-08-09 17:04:08 +00:00
|
|
|
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_TRAIN_GOTO_DEPOT);
|
|
|
|
goto going_to_depot;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sell whole train
|
|
|
|
DoCommandByTile(v->tile, v->index, 1, DC_EXEC, CMD_SELL_RAIL_WAGON);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (v->type == VEH_Road) {
|
2005-02-06 22:36:08 +00:00
|
|
|
if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) {
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_DEPOT)
|
2004-08-09 17:04:08 +00:00
|
|
|
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
|
|
|
|
goto going_to_depot;
|
|
|
|
}
|
|
|
|
|
|
|
|
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
|
|
|
|
} else if (v->type == VEH_Aircraft) {
|
|
|
|
if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) {
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_DEPOT)
|
2004-08-09 17:04:08 +00:00
|
|
|
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR);
|
|
|
|
goto going_to_depot;
|
|
|
|
}
|
|
|
|
|
|
|
|
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT);
|
|
|
|
} else if (v->type == VEH_Ship) {
|
|
|
|
// XXX: not implemented
|
|
|
|
error("!v->type == VEH_Ship");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goto return_to_loop;
|
|
|
|
going_to_depot:;
|
|
|
|
if (++p->ai.state_counter <= 832)
|
|
|
|
return;
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
}
|
|
|
|
return_to_loop:;
|
|
|
|
p->ai.state = AIS_VEH_LOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateRemoveStation(Player *p)
|
|
|
|
{
|
|
|
|
// Remove stations that aren't in use by any vehicle
|
2005-04-03 13:57:39 +00:00
|
|
|
byte *in_use;
|
|
|
|
const byte *used;
|
2005-01-16 14:42:53 +00:00
|
|
|
const Order *ord;
|
2005-04-03 13:57:39 +00:00
|
|
|
const Station *st;
|
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// 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
|
2005-04-03 13:57:39 +00:00
|
|
|
in_use = malloc(GetStationPoolSize());
|
|
|
|
memset(in_use, 0, GetStationPoolSize());
|
2005-01-15 19:06:22 +00:00
|
|
|
FOR_ALL_ORDERS(ord) {
|
2004-12-05 12:43:04 +00:00
|
|
|
if (ord->type == OT_GOTO_STATION)
|
|
|
|
in_use[ord->station] = 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go through all stations and delete those that aren't in use
|
2005-04-03 13:57:39 +00:00
|
|
|
used = in_use;
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy != 0 && st->owner == _current_player && !*used &&
|
2005-01-29 19:41:44 +00:00
|
|
|
( (st->bus_stops != NULL && (tile = st->bus_stops->xy) != 0) ||
|
|
|
|
(st->truck_stops != NULL && (tile = st->truck_stops->xy)) != 0 ||
|
2004-08-09 17:04:08 +00:00
|
|
|
(tile = st->train_tile) != 0 ||
|
|
|
|
(tile = st->dock_tile) != 0 ||
|
|
|
|
(tile = st->airport_tile) != 0)) {
|
|
|
|
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
|
|
}
|
|
|
|
used++;
|
|
|
|
}
|
|
|
|
|
2005-04-03 13:57:39 +00:00
|
|
|
free(in_use);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-27 00:28:14 +00:00
|
|
|
static void AiRemovePlayerRailOrRoad(Player *p, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte m5;
|
|
|
|
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile, MP_RAILWAY)) {
|
2005-06-04 11:56:32 +00:00
|
|
|
if (!IsTileOwner(tile, _current_player)) return;
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
m5 = _m[tile].m5;
|
2004-08-09 17:04:08 +00:00
|
|
|
if ((m5&~0x3) != 0xC0) {
|
|
|
|
is_rail_crossing:;
|
|
|
|
m5 = GetRailTrackStatus(tile);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (m5 == 0xC || m5 == 0x30)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m5&0x25) {
|
|
|
|
pos_0:
|
2005-06-25 16:44:57 +00:00
|
|
|
if (!(GetRailTrackStatus(TILE_MASK(tile - TileDiffXY(1, 0))) & 0x19)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.cur_dir_a = 0;
|
|
|
|
p->ai.cur_tile_a = tile;
|
|
|
|
p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m5&0x2A) {
|
|
|
|
pos_1:
|
2005-06-25 16:44:57 +00:00
|
|
|
if (!(GetRailTrackStatus(TILE_MASK(tile + TileDiffXY(0, 1))) & 0x16)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.cur_dir_a = 1;
|
|
|
|
p->ai.cur_tile_a = tile;
|
|
|
|
p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m5&0x19) {
|
|
|
|
pos_2:
|
2005-06-25 16:44:57 +00:00
|
|
|
if (!(GetRailTrackStatus(TILE_MASK(tile + TileDiffXY(1, 0))) & 0x25)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.cur_dir_a = 2;
|
|
|
|
p->ai.cur_tile_a = tile;
|
|
|
|
p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m5&0x16) {
|
|
|
|
pos_3:
|
2005-06-25 16:44:57 +00:00
|
|
|
if (!(GetRailTrackStatus(TILE_MASK(tile - TileDiffXY(0, 1))) & 0x2A)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.cur_dir_a = 3;
|
|
|
|
p->ai.cur_tile_a = tile;
|
|
|
|
p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
static const byte _depot_bits[] = {0x19,0x16,0x25,0x2A};
|
|
|
|
|
|
|
|
m5 &= 3;
|
2005-01-05 13:32:03 +00:00
|
|
|
if (GetRailTrackStatus(tile + TileOffsByDir(m5)) & _depot_bits[m5])
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
|
|
}
|
2005-01-16 11:24:58 +00:00
|
|
|
} else if (IsTileType(tile, MP_STREET)) {
|
2005-06-04 11:56:32 +00:00
|
|
|
if (!IsTileOwner(tile, _current_player)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-07 21:37:00 +00:00
|
|
|
if (IsLevelCrossing(tile))
|
2004-08-09 17:04:08 +00:00
|
|
|
goto is_rail_crossing;
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
if ( (_m[tile].m5&0xF0) == 0x20) {
|
2004-08-09 17:04:08 +00:00
|
|
|
int dir;
|
|
|
|
|
|
|
|
// Check if there are any stations around.
|
2005-06-25 16:44:57 +00:00
|
|
|
if (IsTileType(tile + TileDiffXY(-1, 0), MP_STATION) &&
|
|
|
|
IsTileOwner(tile + TileDiffXY(-1, 0), _current_player))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-06-25 16:44:57 +00:00
|
|
|
if (IsTileType(tile + TileDiffXY(1, 0), MP_STATION) &&
|
|
|
|
IsTileOwner(tile + TileDiffXY(1, 0), _current_player))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-06-25 16:44:57 +00:00
|
|
|
if (IsTileType(tile + TileDiffXY(0, -1), MP_STATION) &&
|
|
|
|
IsTileOwner(tile + TileDiffXY(0, -1), _current_player))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-06-25 16:44:57 +00:00
|
|
|
if (IsTileType(tile + TileDiffXY(0, 1), MP_STATION) &&
|
|
|
|
IsTileOwner(tile + TileDiffXY(0, 1), _current_player))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
dir = _m[tile].m5 & 3;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
|
|
DoCommandByTile(
|
2005-01-05 13:32:03 +00:00
|
|
|
TILE_MASK(tile + TileOffsByDir(dir)),
|
2004-08-09 17:04:08 +00:00
|
|
|
8 >> (dir ^ 2),
|
|
|
|
0,
|
|
|
|
DC_EXEC,
|
|
|
|
CMD_REMOVE_ROAD);
|
|
|
|
}
|
2005-01-16 11:24:58 +00:00
|
|
|
} else if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
byte b;
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
if (!IsTileOwner(tile, _current_player) || (_m[tile].m5 & 0xC6) != 0x80)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
m5 = 0;
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
b = _m[tile].m5 & 0x21;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (b == 0) goto pos_0;
|
|
|
|
if (b == 1) goto pos_3;
|
|
|
|
if (b == 0x20) goto pos_2;
|
|
|
|
goto pos_1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateRemoveTrack(Player *p)
|
|
|
|
{
|
2005-03-27 00:28:14 +00:00
|
|
|
/* Was 1000 for standard 8x8 maps. */
|
|
|
|
int num = MapSizeX() * 4;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2005-03-27 00:28:14 +00:00
|
|
|
TileIndex tile = ++p->ai.state_counter;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Iterated all tiles?
|
2005-01-25 21:43:57 +00:00
|
|
|
if (tile >= MapSize()) {
|
2004-08-09 17:04:08 +00:00
|
|
|
p->ai.state = AIS_REMOVE_STATION;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove player stuff in that tile
|
|
|
|
AiRemovePlayerRailOrRoad(p, tile);
|
|
|
|
if (p->ai.state != AIS_REMOVE_TRACK)
|
|
|
|
return;
|
|
|
|
} while (--num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiStateRemoveSingleRailTile(Player *p)
|
|
|
|
{
|
|
|
|
// Remove until we can't remove more.
|
|
|
|
if (!AiRemoveTileAndGoForward(p)) {
|
|
|
|
p->ai.state = AIS_REMOVE_TRACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static AiStateAction * const _ai_actions[] = {
|
|
|
|
AiCase0,
|
|
|
|
AiCase1,
|
|
|
|
AiStateVehLoop,
|
|
|
|
AiStateCheckReplaceVehicle,
|
|
|
|
AiStateDoReplaceVehicle,
|
|
|
|
AiStateWantNewRoute,
|
|
|
|
|
|
|
|
AiStateBuildDefaultRailBlocks,
|
|
|
|
AiStateBuildRail,
|
|
|
|
AiStateBuildRailVeh,
|
|
|
|
AiStateDeleteRailBlocks,
|
|
|
|
|
|
|
|
AiStateBuildDefaultRoadBlocks,
|
|
|
|
AiStateBuildRoad,
|
|
|
|
AiStateBuildRoadVehicles,
|
|
|
|
AiStateDeleteRoadBlocks,
|
|
|
|
|
|
|
|
AiStateAirportStuff,
|
|
|
|
AiStateBuildDefaultAirportBlocks,
|
|
|
|
AiStateBuildAircraftVehicles,
|
|
|
|
|
|
|
|
AiStateCheckShipStuff,
|
|
|
|
AiStateBuildDefaultShipBlocks,
|
|
|
|
AiStateDoShipStuff,
|
|
|
|
|
|
|
|
AiStateSellVeh,
|
|
|
|
AiStateRemoveStation,
|
|
|
|
AiStateRemoveTrack,
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
AiStateRemoveSingleRailTile
|
|
|
|
};
|
|
|
|
|
|
|
|
extern void ShowBuyCompanyDialog(uint player);
|
|
|
|
|
|
|
|
static void AiHandleTakeover(Player *p)
|
|
|
|
{
|
|
|
|
if (p->bankrupt_timeout != 0) {
|
|
|
|
if ((p->bankrupt_timeout-=8) > 0)
|
|
|
|
return;
|
|
|
|
p->bankrupt_timeout = 0;
|
|
|
|
DeleteWindowById(WC_BUY_COMPANY, _current_player);
|
|
|
|
if (_current_player == _local_player) {
|
|
|
|
AskExitToGameMenu();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (IS_HUMAN_PLAYER(_current_player))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 &&
|
|
|
|
best_val < pp->old_economy[1].performance_history) {
|
|
|
|
best_val = pp->old_economy[1].performance_history;
|
|
|
|
best_pl = pp;
|
|
|
|
}
|
|
|
|
asked>>=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Asked all players?
|
|
|
|
if (best_val == -1) {
|
|
|
|
p->bankrupt_asked = 255;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SETBIT(p->bankrupt_asked, best_pl->index);
|
|
|
|
|
|
|
|
if (best_pl->index == _local_player) {
|
|
|
|
p->bankrupt_timeout = 4440;
|
|
|
|
ShowBuyCompanyDialog(_current_player);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (IS_HUMAN_PLAYER(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;
|
|
|
|
_current_player = p->index;
|
|
|
|
DoCommandByTile(0, old_p, 0, DC_EXEC, CMD_BUY_COMPANY);
|
|
|
|
_current_player = old_p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiAdjustLoan(Player *p)
|
|
|
|
{
|
|
|
|
int32 base = AiGetBasePrice(p);
|
|
|
|
|
|
|
|
if (p->player_money > base * 1400) {
|
|
|
|
// Decrease loan
|
|
|
|
if (p->current_loan != 0) {
|
2005-05-06 06:56:30 +00:00
|
|
|
DoCommandByTile(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else if (p->player_money < base * 500) {
|
|
|
|
// Increase loan
|
|
|
|
if (p->current_loan < _economy.max_loan &&
|
|
|
|
p->num_valid_stat_ent >= 2 &&
|
|
|
|
-(p->old_economy[0].expenses+p->old_economy[1].expenses) < base * 60) {
|
2005-05-06 06:56:30 +00:00
|
|
|
DoCommandByTile(0, 0, 0, DC_EXEC, CMD_INCREASE_LOAN);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AiBuildCompanyHQ(Player *p)
|
|
|
|
{
|
|
|
|
TileIndex tile;
|
|
|
|
|
|
|
|
if (p->location_of_house == 0 &&
|
|
|
|
p->last_build_coordinate != 0) {
|
|
|
|
tile = AdjustTileCoordRandomly(p->last_build_coordinate, 8);
|
|
|
|
DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
void AiDoGameLoop(Player *p)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
_cur_ai_player = p;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (p->bankrupt_asked != 0) {
|
|
|
|
AiHandleTakeover(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-09-10 18:54:23 +00:00
|
|
|
// Ugly hack to make sure the service interval of the AI is good, not looking
|
|
|
|
// to the patch-setting
|
|
|
|
// Also, it takes into account the setting if the service-interval is in days
|
|
|
|
// or in %
|
|
|
|
_ai_service_interval = _patches.servint_ispercent?80:180;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (IS_HUMAN_PLAYER(_current_player))
|
|
|
|
return;
|
|
|
|
|
|
|
|
AiAdjustLoan(p);
|
|
|
|
AiBuildCompanyHQ(p);
|
|
|
|
|
|
|
|
if (_opt.diff.competitor_speed == 4) {
|
|
|
|
/* ultraspeed */
|
|
|
|
_ai_actions[p->ai.state](p);
|
|
|
|
if (p->bankrupt_asked != 0)
|
|
|
|
return;
|
|
|
|
} else if (_opt.diff.competitor_speed != 3) {
|
|
|
|
p->ai.tick++;
|
|
|
|
if (!(p->ai.tick&1))
|
|
|
|
return;
|
|
|
|
if (_opt.diff.competitor_speed != 2) {
|
|
|
|
if (!(p->ai.tick&2))
|
|
|
|
return;
|
|
|
|
if (_opt.diff.competitor_speed == 0) {
|
|
|
|
if (!(p->ai.tick&4))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
static byte old_state = 99;
|
|
|
|
static bool hasdots = false;
|
|
|
|
char *_ai_state_names[]={
|
|
|
|
"AiCase0",
|
|
|
|
"AiCase1",
|
|
|
|
"AiStateVehLoop",
|
|
|
|
"AiStateCheckReplaceVehicle",
|
|
|
|
"AiStateDoReplaceVehicle",
|
|
|
|
"AiStateWantNewRoute",
|
|
|
|
"AiStateBuildDefaultRailBlocks",
|
|
|
|
"AiStateBuildRail",
|
|
|
|
"AiStateBuildRailVeh",
|
|
|
|
"AiStateDeleteRailBlocks",
|
|
|
|
"AiStateBuildDefaultRoadBlocks",
|
|
|
|
"AiStateBuildRoad",
|
|
|
|
"AiStateBuildRoadVehicles",
|
|
|
|
"AiStateDeleteRoadBlocks",
|
|
|
|
"AiStateAirportStuff",
|
|
|
|
"AiStateBuildDefaultAirportBlocks",
|
|
|
|
"AiStateBuildAircraftVehicles",
|
|
|
|
"AiStateCheckShipStuff",
|
|
|
|
"AiStateBuildDefaultShipBlocks",
|
|
|
|
"AiStateDoShipStuff",
|
|
|
|
"AiStateSellVeh",
|
|
|
|
"AiStateRemoveStation",
|
|
|
|
"AiStateRemoveTrack",
|
|
|
|
"AiStateRemoveSingleRailTile"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (p->ai.state != old_state) {
|
|
|
|
if (hasdots)
|
|
|
|
printf("\n");
|
|
|
|
hasdots=false;
|
|
|
|
printf("AiState: %s\n", _ai_state_names[old_state=p->ai.state]);
|
|
|
|
} else {
|
|
|
|
printf(".");
|
|
|
|
hasdots=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
_ai_actions[p->ai.state](p);
|
|
|
|
}
|