2007-12-17 01:35:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
#ifndef AI_H
|
|
|
|
#define AI_H
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "../network/network.h"
|
2005-12-08 21:12:15 +00:00
|
|
|
#include "../player.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "../command_type.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "../core/random_func.hpp"
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* How DoCommands look like for an AI */
|
2007-03-07 12:11:48 +00:00
|
|
|
struct AICommand {
|
2005-09-07 15:10:11 +00:00
|
|
|
uint32 tile;
|
|
|
|
uint32 p1;
|
|
|
|
uint32 p2;
|
|
|
|
uint32 procc;
|
2007-12-21 21:50:46 +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
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
AICommand *next;
|
|
|
|
};
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* The struct for an AIScript Player */
|
2007-03-07 12:11:48 +00:00
|
|
|
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
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* The struct to keep some data about the AI in general */
|
2007-03-07 12:11:48 +00:00
|
|
|
struct AIStruct {
|
2005-09-07 15:10:11 +00:00
|
|
|
/* 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)
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
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);
|
2007-03-07 11:47:46 +00:00
|
|
|
void AI_RunGameLoop();
|
|
|
|
void AI_Initialize();
|
|
|
|
void AI_Uninitialize();
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
|
|
|
|
CommandCost 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.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
static inline bool AI_AllowNewAI()
|
2005-11-21 14:28:31 +00:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2007-04-18 22:10:36 +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))
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
static inline uint32 AI_Random()
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
|
|
|
/* 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 */
|