2005-09-07 15:10:11 +00:00
|
|
|
#ifndef AI_H
|
|
|
|
#define AI_H
|
|
|
|
|
|
|
|
#include "../functions.h"
|
2005-11-21 14:28:31 +00:00
|
|
|
#include "../network.h"
|
2005-12-08 21:12:15 +00:00
|
|
|
#include "../player.h"
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* How DoCommands look like for an AI */
|
|
|
|
typedef struct AICommand {
|
|
|
|
uint32 tile;
|
|
|
|
uint32 p1;
|
|
|
|
uint32 p2;
|
|
|
|
uint32 procc;
|
2006-06-04 09:10:24 +00:00
|
|
|
CommandCallback* callback;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
2005-12-10 18:43:49 +00:00
|
|
|
char *text;
|
|
|
|
uint uid;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
struct AICommand *next;
|
|
|
|
} AICommand;
|
|
|
|
|
|
|
|
/* The struct for an AIScript Player */
|
|
|
|
typedef struct AIPlayer {
|
2006-03-09 20:37:51 +00:00
|
|
|
bool active; ///< Is this AI active?
|
|
|
|
AICommand *queue; ///< The commands that he has in his queue
|
|
|
|
AICommand *queue_tail; ///< The tail of this queue
|
2005-09-07 15:10:11 +00:00
|
|
|
} AIPlayer;
|
|
|
|
|
|
|
|
/* The struct to keep some data about the AI in general */
|
|
|
|
typedef struct AIStruct {
|
|
|
|
/* General */
|
2006-03-09 20:37:51 +00:00
|
|
|
bool enabled; ///< Is AI enabled?
|
|
|
|
uint tick; ///< The current tick (something like _frame_counter, only for AIs)
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* For network-clients (a OpenTTD client who acts as an AI connected to a server) */
|
2006-03-09 20:37:51 +00:00
|
|
|
bool network_client; ///< Are we a network_client?
|
|
|
|
uint8 network_playas; ///< The current network player we are connected as
|
2005-09-07 15:10:11 +00:00
|
|
|
} AIStruct;
|
|
|
|
|
|
|
|
VARDEF AIStruct _ai;
|
|
|
|
VARDEF AIPlayer _ai_player[MAX_PLAYERS];
|
|
|
|
|
|
|
|
// ai.c
|
2005-10-24 05:51:23 +00:00
|
|
|
void AI_StartNewAI(PlayerID player);
|
|
|
|
void AI_PlayerDied(PlayerID player);
|
2005-09-07 15:10:11 +00:00
|
|
|
void AI_RunGameLoop(void);
|
|
|
|
void AI_Initialize(void);
|
|
|
|
void AI_Uninitialize(void);
|
2006-05-21 16:18:58 +00:00
|
|
|
int32 AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
|
2006-06-04 09:10:24 +00:00
|
|
|
int32 AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, CommandCallback* callback);
|
2005-09-07 15:10:11 +00:00
|
|
|
|
2005-11-21 14:28:31 +00:00
|
|
|
/** Is it allowed to start a new AI.
|
|
|
|
* This function checks some boundries to see if we should launch a new AI.
|
|
|
|
* @return True if we can start a new AI.
|
|
|
|
*/
|
|
|
|
static inline bool AI_AllowNewAI(void)
|
|
|
|
{
|
|
|
|
/* If disabled, no AI */
|
|
|
|
if (!_ai.enabled)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* If in network, but no server, no AI */
|
|
|
|
if (_networking && !_network_server)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* If in network, and server, possible AI */
|
|
|
|
if (_networking && _network_server) {
|
|
|
|
/* Do we want AIs in multiplayer? */
|
|
|
|
if (!_patches.ai_in_multiplayer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Only the NewAI is allowed... sadly enough the old AI just doesn't support this
|
|
|
|
* system, because all commands are delayed by at least 1 tick, which causes
|
|
|
|
* a big problem, because it uses variables that are only set AFTER the command
|
|
|
|
* is really executed... */
|
(svn r3313) Remove GPMI related changes from trunk
Revisions in detail: 2542, 3226 (partial), 3229, 3231, 3232, 3238, 3242-3245, 3251, 3253, 3260, 3263, 3265, 3266, 3269, 3277, 3278, 3279, 3283 (partial), 3304, 3305, 3306
2005-12-18 12:10:46 +00:00
|
|
|
if (!_patches.ainew_active)
|
2005-11-21 14:28:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
#define AI_CHANCE16(a,b) ((uint16) AI_Random() <= (uint16)((65536 * a) / b))
|
|
|
|
#define AI_CHANCE16R(a,b,r) ((uint16)(r = AI_Random()) <= (uint16)((65536 * a) / b))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The random-function that should be used by ALL AIs.
|
|
|
|
*/
|
|
|
|
static inline uint AI_RandomRange(uint max)
|
|
|
|
{
|
|
|
|
/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
|
|
|
|
* but we pick InteractiveRandomRange if we are a network_server or network-client.
|
|
|
|
*/
|
|
|
|
if (_networking)
|
|
|
|
return InteractiveRandomRange(max);
|
|
|
|
else
|
|
|
|
return RandomRange(max);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The random-function that should be used by ALL AIs.
|
|
|
|
*/
|
|
|
|
static inline uint32 AI_Random(void)
|
|
|
|
{
|
|
|
|
/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
|
|
|
|
* but we pick InteractiveRandomRange if we are a network_server or network-client.
|
|
|
|
*/
|
|
|
|
if (_networking)
|
|
|
|
return InteractiveRandom();
|
|
|
|
else
|
|
|
|
return Random();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* AI_H */
|