summaryrefslogtreecommitdiff
path: root/src/ai
diff options
context:
space:
mode:
Diffstat (limited to 'src/ai')
-rw-r--r--src/ai/ai.cpp5
-rw-r--r--src/ai/default/default.cpp42
-rw-r--r--src/ai/trolly/pathfinder.cpp27
-rw-r--r--src/ai/trolly/trolly.cpp13
4 files changed, 44 insertions, 43 deletions
diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp
index 58b97ad39..5b2e5a8fc 100644
--- a/src/ai/ai.cpp
+++ b/src/ai/ai.cpp
@@ -5,6 +5,7 @@
#include "../variables.h"
#include "../command.h"
#include "../network/network.h"
+#include "../helpers.hpp"
#include "ai.h"
#include "default/default.h"
@@ -50,11 +51,11 @@ static void AI_PutCommandInQueue(PlayerID player, TileIndex tile, uint32 p1, uin
if (_ai_player[player].queue_tail == NULL) {
/* There is no item in the queue yet, create the queue */
- _ai_player[player].queue = malloc(sizeof(AICommand));
+ MallocT(&_ai_player[player].queue, 1);
_ai_player[player].queue_tail = _ai_player[player].queue;
} else {
/* Add an item at the end */
- _ai_player[player].queue_tail->next = malloc(sizeof(AICommand));
+ MallocT(&_ai_player[player].queue_tail->next, 1);
_ai_player[player].queue_tail = _ai_player[player].queue_tail->next;
}
diff --git a/src/ai/default/default.cpp b/src/ai/default/default.cpp
index 143e724b9..5a328e9d0 100644
--- a/src/ai/default/default.cpp
+++ b/src/ai/default/default.cpp
@@ -26,6 +26,7 @@
#include "../../variables.h"
#include "../../bridge.h"
#include "../../date.h"
+#include "../../helpers.hpp"
#include "default.h"
// remove some day perhaps?
@@ -63,10 +64,10 @@ enum {
#include "../../table/ai_rail.h"
-static byte GetRailTrackStatus(TileIndex tile)
+static TrackBits GetRailTrackStatus(TileIndex tile)
{
uint32 r = GetTileTrackStatus(tile, TRANSPORT_RAIL);
- return (byte) (r | r >> 8);
+ return (TrackBits)(byte) (r | r >> 8);
}
@@ -644,8 +645,8 @@ static bool AiCheckIfRouteIsGood(Player *p, FoundRoute *fr, byte bitmask)
}
if (fr->cargo == CT_PASSENGERS || fr->cargo == CT_MAIL) {
- const Town* from = fr->from;
- const Town* to = fr->to;
+ const Town* from = (const Town*)fr->from;
+ const Town* to = (const Town*)fr->to;
if (from->pct_pass_transported > 0x99 ||
to->pct_pass_transported > 0x99) {
@@ -783,7 +784,7 @@ static void AiWantLongIndustryRoute(Player *p)
p->ai.order_list_blocks[2] = 255;
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
- p->ai.state_mode = -1;
+ p->ai.state_mode = UCHAR_MAX;
p->ai.state_counter = 0;
p->ai.timeout_counter = 0;
}
@@ -851,7 +852,7 @@ static void AiWantMediumIndustryRoute(Player *p)
p->ai.order_list_blocks[1] = 1;
p->ai.order_list_blocks[2] = 255;
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
- p->ai.state_mode = -1;
+ p->ai.state_mode = UCHAR_MAX;
p->ai.state_counter = 0;
p->ai.timeout_counter = 0;
}
@@ -919,7 +920,7 @@ static void AiWantShortIndustryRoute(Player *p)
p->ai.order_list_blocks[1] = 1;
p->ai.order_list_blocks[2] = 255;
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
- p->ai.state_mode = -1;
+ p->ai.state_mode = UCHAR_MAX;
p->ai.state_counter = 0;
p->ai.timeout_counter = 0;
}
@@ -1020,7 +1021,7 @@ static void AiWantMailRoute(Player *p)
p->ai.order_list_blocks[1] = 1;
p->ai.order_list_blocks[2] = 255;
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
- p->ai.state_mode = -1;
+ p->ai.state_mode = UCHAR_MAX;
p->ai.state_counter = 0;
p->ai.timeout_counter = 0;
}
@@ -1089,7 +1090,7 @@ static void AiWantPassengerRoute(Player *p)
p->ai.order_list_blocks[1] = 1;
p->ai.order_list_blocks[2] = 255;
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
- p->ai.state_mode = -1;
+ p->ai.state_mode = UCHAR_MAX;
p->ai.state_counter = 0;
p->ai.timeout_counter = 0;
}
@@ -1164,7 +1165,7 @@ static void AiWantLongRoadIndustryRoute(Player *p)
p->ai.order_list_blocks[2] = 255;
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
- p->ai.state_mode = -1;
+ p->ai.state_mode = UCHAR_MAX;
p->ai.state_counter = 0;
p->ai.timeout_counter = 0;
}
@@ -1220,7 +1221,7 @@ static void AiWantMediumRoadIndustryRoute(Player *p)
p->ai.order_list_blocks[2] = 255;
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
- p->ai.state_mode = -1;
+ p->ai.state_mode = UCHAR_MAX;
p->ai.state_counter = 0;
p->ai.timeout_counter = 0;
}
@@ -1278,7 +1279,7 @@ static void AiWantLongRoadPassengerRoute(Player *p)
p->ai.order_list_blocks[2] = 255;
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
- p->ai.state_mode = -1;
+ p->ai.state_mode = UCHAR_MAX;
p->ai.state_counter = 0;
p->ai.timeout_counter = 0;
}
@@ -1334,7 +1335,7 @@ static void AiWantPassengerRouteInsideTown(Player *p)
p->ai.order_list_blocks[2] = 255;
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
- p->ai.state_mode = -1;
+ p->ai.state_mode = UCHAR_MAX;
p->ai.state_counter = 0;
p->ai.timeout_counter = 0;
}
@@ -1852,7 +1853,7 @@ static bool AiDoFollowTrack(const Player* p)
arpfd.tile2 = p->ai.cur_tile_a;
arpfd.flag = false;
arpfd.count = 0;
- FollowTrack(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, p->ai.cur_dir_a^2,
+ FollowTrack(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, (DiagDirection)(p->ai.cur_dir_a^2),
(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
return arpfd.count > 8;
}
@@ -1941,7 +1942,7 @@ static bool AiCheckRailPathBetter(AiRailFinder *arf, const byte *p)
}
}
arf->recursive_mode = 0;
- arf->cur_best_dist = (uint)-1;
+ arf->cur_best_dist = UINT_MAX;
arf->cur_best_depth = 0xff;
return better;
@@ -2185,7 +2186,7 @@ static bool AiRemoveTileAndGoForward(Player *p)
} else {
// Check if the bridge points in the right direction.
// This is not really needed the first place AiRemoveTileAndGoForward is called.
- if (DiagDirToAxis(GetBridgeRampDirection(tile)) != (p->ai.cur_dir_a & 1U)) return false;
+ if (DiagDirToAxis(GetBridgeRampDirection(tile)) != (p->ai.cur_dir_a & 1)) return false;
tile = GetOtherBridgeEnd(tile);
@@ -2820,7 +2821,7 @@ static bool AiCheckRoadFinished(Player *p)
are.best_dist = (uint)-1;
for_each_bit(i, bits) {
- FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, _dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
+ FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, (DiagDirection)_dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
}
if (DistanceManhattan(tile, are.dest) <= are.best_dist) return false;
@@ -3583,7 +3584,7 @@ static void AiStateRemoveStation(Player *p)
p->ai.state = AIS_1;
// Get a list of all stations that are in use by a vehicle
- in_use = malloc(GetMaxStationIndex() + 1);
+ MallocT(&in_use, GetMaxStationIndex() + 1);
memset(in_use, 0, GetMaxStationIndex() + 1);
FOR_ALL_ORDERS(ord) {
if (ord->type == OT_GOTO_STATION) in_use[ord->dest] = 1;
@@ -3705,7 +3706,7 @@ pos_3:
return;
}
- rails = 0;
+ rails = TRACK_BIT_NONE;
switch (GetBridgeRampDirection(tile)) {
default:
@@ -3798,7 +3799,6 @@ static void AiHandleTakeover(Player *p)
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) {
@@ -3830,7 +3830,7 @@ static void AiHandleTakeover(Player *p)
// Too little money for computer to buy it?
if (best_pl->player_money >> 1 >= p->bankrupt_value) {
// Computer wants to buy it.
- old_p = _current_player;
+ PlayerID old_p = _current_player;
_current_player = p->index;
DoCommand(0, old_p, 0, DC_EXEC, CMD_BUY_COMPANY);
_current_player = old_p;
diff --git a/src/ai/trolly/pathfinder.cpp b/src/ai/trolly/pathfinder.cpp
index 6ffdf1fec..5ba50ca7f 100644
--- a/src/ai/trolly/pathfinder.cpp
+++ b/src/ai/trolly/pathfinder.cpp
@@ -57,7 +57,7 @@ static bool IsRoad(TileIndex tile)
// Check if the current tile is in our end-area
static int32 AyStar_AiPathFinder_EndNodeCheck(AyStar *aystar, OpenListNode *current)
{
- const Ai_PathFinderInfo* PathFinderInfo = aystar->user_target;
+ const Ai_PathFinderInfo* PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
// It is not allowed to have a station on the end of a bridge or tunnel ;)
if (current->path.node.user_data[0] != 0) return AYSTAR_DONE;
@@ -82,7 +82,7 @@ static uint AiPathFinder_Hash(uint key1, uint key2)
static void AyStar_AiPathFinder_Free(AyStar *aystar)
{
AyStarMain_Free(aystar);
- free(aystar);
+ delete aystar;
}
@@ -99,7 +99,7 @@ AyStar *new_AyStar_AiPathFinder(int max_tiles_around, Ai_PathFinderInfo *PathFin
uint x;
uint y;
// Create AyStar
- AyStar *result = malloc(sizeof(AyStar));
+ AyStar *result = new AyStar();
init_AyStar(result, AiPathFinder_Hash, 1 << 10);
// Set the function pointers
result->CalculateG = AyStar_AiPathFinder_CalculateG;
@@ -170,7 +170,7 @@ void clean_AyStar_AiPathFinder(AyStar *aystar, Ai_PathFinderInfo *PathFinderInfo
// The h-value, simple calculation
static int32 AyStar_AiPathFinder_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent)
{
- const Ai_PathFinderInfo* PathFinderInfo = aystar->user_target;
+ const Ai_PathFinderInfo* PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
int r, r2;
if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION) {
@@ -214,7 +214,6 @@ static void AyStar_AiPathFinder_FoundEndNode(AyStar *aystar, OpenListNode *curre
// What tiles are around us.
static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *current)
{
- uint i;
int ret;
int dir;
@@ -223,7 +222,7 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
aystar->num_neighbours = 0;
// Go through all surrounding tiles and check if they are within the limits
- for (i = 0; i < 4; i++) {
+ for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) {
TileIndex ctile = current->path.node.tile; // Current tile
TileIndex atile = ctile + TileOffsByDiagDir(i); // Adjacent tile
@@ -238,7 +237,7 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
if (IsTunnel(atile)) {
if (GetTunnelDirection(atile) != i) continue;
} else {
- if ((_m[atile].m5 & 1U) != DiagDirToAxis(i)) continue;
+ if ((_m[atile].m5 & 1) != DiagDirToAxis(i)) continue;
}
}
}
@@ -246,7 +245,7 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
if (!PathFinderInfo->rail_or_road && IsRoad(ctile)) {
if (IsTileType(ctile, MP_TUNNELBRIDGE)) {
// An existing bridge/tunnel... let's test the direction ;)
- if ((_m[ctile].m5 & 1U) != (i & 1)) continue;
+ if ((_m[ctile].m5 & 1) != (i & 1)) continue;
}
}
@@ -254,7 +253,7 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
(AI_PATHFINDER_FLAG_TUNNEL & current->path.node.user_data[0]) != 0) {
// We are a bridge/tunnel, how cool!!
// This means we can only point forward.. get the direction from the user_data
- if (i != (current->path.node.user_data[0] >> 8)) continue;
+ if ((uint)i != (current->path.node.user_data[0] >> 8)) continue;
}
dir = 0;
@@ -371,7 +370,7 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
extern uint GetRailFoundation(Slope tileh, TrackBits bits); // XXX function declaration in .c
-extern uint GetRoadFoundation(Slope tileh, uint bits); // XXX function declaration in .c
+extern uint GetRoadFoundation(Slope tileh, RoadBits bits); // XXX function declaration in .c
extern uint GetBridgeFoundation(Slope tileh, Axis); // XXX function declaration in .c
enum {
BRIDGE_NO_FOUNDATION = 1 << 0 | 1 << 3 | 1 << 6 | 1 << 9 | 1 << 12,
@@ -417,7 +416,7 @@ static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current,
// Skip if the tile was from a bridge or tunnel
if (parent->path.node.user_data[0] == 0 && current->user_data[0] == 0) {
if (PathFinderInfo->rail_or_road) {
- r = GetRailFoundation(parent_tileh, 1 << AiNew_GetRailDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile));
+ r = GetRailFoundation(parent_tileh, (TrackBits)(1 << AiNew_GetRailDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile)));
// Maybe is BRIDGE_NO_FOUNDATION a bit strange here, but it contains just the right information..
if (r >= 15 || (r == 0 && HASBIT(BRIDGE_NO_FOUNDATION, tileh))) {
res += AI_PATHFINDER_TILE_GOES_UP_PENALTY;
@@ -426,7 +425,7 @@ static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current,
}
} else {
if (!IsRoad(parent->path.node.tile) || !IsTileType(parent->path.node.tile, MP_TUNNELBRIDGE)) {
- r = GetRoadFoundation(parent_tileh, AiNew_GetRoadDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile));
+ r = GetRoadFoundation(parent_tileh, (RoadBits)AiNew_GetRoadDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile));
if (r >= 15 || r == 0) {
res += AI_PATHFINDER_TILE_GOES_UP_PENALTY;
} else {
@@ -452,13 +451,13 @@ static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current,
// Check if we are going up or down, first for the starting point
// In user_data[0] is at the 8th bit the direction
if (!HASBIT(BRIDGE_NO_FOUNDATION, parent_tileh)) {
- if (GetBridgeFoundation(parent_tileh, (current->user_data[0] >> 8) & 1) < 15) {
+ if (GetBridgeFoundation(parent_tileh, (Axis)((current->user_data[0] >> 8) & 1)) < 15) {
res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
}
}
// Second for the end point
if (!HASBIT(BRIDGE_NO_FOUNDATION, tileh)) {
- if (GetBridgeFoundation(tileh, (current->user_data[0] >> 8) & 1) < 15) {
+ if (GetBridgeFoundation(tileh, (Axis)((current->user_data[0] >> 8) & 1)) < 15) {
res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
}
}
diff --git a/src/ai/trolly/trolly.cpp b/src/ai/trolly/trolly.cpp
index 35115a880..855f74c8f 100644
--- a/src/ai/trolly/trolly.cpp
+++ b/src/ai/trolly/trolly.cpp
@@ -794,7 +794,7 @@ static void AiNew_State_FindDepot(Player *p)
for (i=2;i<p->ainew.path_info.route_length-2;i++) {
tile = p->ainew.path_info.route[i];
- for (j = 0; j < 4; j++) {
+ for (j = DIAGDIR_BEGIN; j < DIAGDIR_END; j++) {
TileIndex t = tile + TileOffsByDiagDir(j);
if (IsTileType(t, MP_STREET) &&
@@ -825,7 +825,7 @@ static void AiNew_State_FindDepot(Player *p)
tile = p->ainew.path_info.route[i];
- for (j = 0; j < 4; j++) {
+ for (j = DIAGDIR_BEGIN; j < DIAGDIR_END; j++) {
TileIndex t = tile + TileOffsByDiagDir(j);
// It may not be placed on the road/rail itself
@@ -901,7 +901,8 @@ static int AiNew_HowManyVehicles(Player *p)
}
// This is because moving 60% is more than we can dream of!
- max_cargo *= 0.6;
+ max_cargo *= 6;
+ max_cargo /= 10;
// We want all the cargo to be gone in a month.. so, we know the cargo it delivers
// we know what the vehicle takes with him, and we know the time it takes him
// to get back here.. now let's do some math!
@@ -1051,7 +1052,7 @@ static void AiNew_State_BuildPath(Player *p)
dir3 = p->ainew.to_direction;
}
- ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+ ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir((DiagDirection)dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
if (!CmdFailed(ret)) {
dir1 = TileOffsByDiagDir(dir1);
if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) {
@@ -1063,7 +1064,7 @@ static void AiNew_State_BuildPath(Player *p)
}
}
- ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+ ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir((DiagDirection)dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
if (!CmdFailed(ret)) {
dir2 = TileOffsByDiagDir(dir2);
if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) {
@@ -1075,7 +1076,7 @@ static void AiNew_State_BuildPath(Player *p)
}
}
- ret = AI_DoCommand(tile, DiagDirToRoadBits(dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+ ret = AI_DoCommand(tile, DiagDirToRoadBits((DiagDirection)dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
if (!CmdFailed(ret)) {
dir3 = TileOffsByDiagDir(dir3);
if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) {