ai/ai.h
author truelight
Tue, 22 Nov 2005 15:55:38 +0000
changeset 2684 8aba54c245cc
parent 2682 94ca0b4dc53f
child 2687 eb2443a14a74
permissions -rw-r--r--
(svn r3226) -Fix: GPMI implementation had minor glitches
-Fix: the AI speed control is done by the AI-core, individual AIs don't have to do it (so, AIs were delayed twice ;)
-Add: Support for AI-network-clients (an AI, connecting to a remote server)
-Fix: minor AI-core problems
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     1
#ifndef AI_H
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     2
#define AI_H
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     3
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     4
#include "../functions.h"
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
     5
#include "../network.h"
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     6
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     7
/* How DoCommands look like for an AI */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     8
typedef struct AICommand {
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     9
	uint32 tile;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    10
	uint32 p1;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    11
	uint32 p2;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    12
	uint32 procc;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    13
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    14
	uint32 dp[20];
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    15
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    16
	struct AICommand *next;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    17
} AICommand;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    18
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    19
/* The struct for an AIScript Player */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    20
typedef struct AIPlayer {
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    21
	bool active;            //! Is this AI active?
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    22
	AICommand *queue;       //! The commands that he has in his queue
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    23
	AICommand *queue_tail;  //! The tail of this queue
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    24
} AIPlayer;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    25
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    26
/* The struct to keep some data about the AI in general */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    27
typedef struct AIStruct {
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    28
	/* General */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    29
	bool enabled;           //! Is AI enabled?
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    30
	uint tick;              //! The current tick (something like _frame_counter, only for AIs)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    31
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    32
	/* For network-clients (a OpenTTD client who acts as an AI connected to a server) */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    33
	bool network_client;    //! Are we a network_client?
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
    34
	uint8 network_playas;   //! The current network player we are connected as
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    35
} AIStruct;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    36
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    37
VARDEF AIStruct _ai;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    38
VARDEF AIPlayer _ai_player[MAX_PLAYERS];
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    39
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    40
// ai.c
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2395
diff changeset
    41
void AI_StartNewAI(PlayerID player);
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2395
diff changeset
    42
void AI_PlayerDied(PlayerID player);
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    43
void AI_RunGameLoop(void);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    44
void AI_Initialize(void);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    45
void AI_Uninitialize(void);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    46
int32 AI_DoCommand(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    47
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    48
/** Is it allowed to start a new AI.
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    49
 * This function checks some boundries to see if we should launch a new AI.
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    50
 * @return True if we can start a new AI.
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    51
 */
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    52
static inline bool AI_AllowNewAI(void)
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    53
{
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    54
	/* If disabled, no AI */
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    55
	if (!_ai.enabled)
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    56
		return false;
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    57
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    58
	/* If in network, but no server, no AI */
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    59
	if (_networking && !_network_server)
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    60
		return false;
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    61
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    62
	/* If in network, and server, possible AI */
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    63
	if (_networking && _network_server) {
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    64
		/* Do we want AIs in multiplayer? */
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    65
		if (!_patches.ai_in_multiplayer)
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    66
			return false;
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    67
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    68
		/* Only the NewAI is allowed... sadly enough the old AI just doesn't support this
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    69
		 *  system, because all commands are delayed by at least 1 tick, which causes
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    70
		 *  a big problem, because it uses variables that are only set AFTER the command
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    71
		 *  is really executed... */
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    72
		if (!_patches.ainew_active)
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    73
			return false;
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    74
	}
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    75
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    76
	return true;
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    77
}
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    78
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    79
#define AI_CHANCE16(a,b)    ((uint16)     AI_Random()  <= (uint16)((65536 * a) / b))
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    80
#define AI_CHANCE16R(a,b,r) ((uint16)(r = AI_Random()) <= (uint16)((65536 * a) / b))
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    81
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    82
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    83
 * The random-function that should be used by ALL AIs.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    84
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    85
static inline uint AI_RandomRange(uint max)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    86
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    87
	/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    88
	 *   but we pick InteractiveRandomRange if we are a network_server or network-client.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    89
	 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    90
	if (_networking)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    91
		return InteractiveRandomRange(max);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    92
	else
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    93
		return RandomRange(max);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    94
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    95
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    96
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    97
 * The random-function that should be used by ALL AIs.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    98
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    99
static inline uint32 AI_Random(void)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   100
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   101
/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   102
	 *   but we pick InteractiveRandomRange if we are a network_server or network-client.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   103
	 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   104
	if (_networking)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   105
		return InteractiveRandom();
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   106
	else
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   107
		return Random();
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   108
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   109
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   110
#endif /* AI_H */