src/ai/ai.h
author rubidium
Tue, 25 Dec 2007 11:26:07 +0000
changeset 8131 160939e24ed3
parent 8116 8da76dcb3287
child 8211 29a8510dfd62
permissions -rw-r--r--
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
8091
674be8638d74 (svn r11652) -Codechange: add the svn $ header for several files
smatz
parents: 6943
diff changeset
     1
/* $Id$ */
674be8638d74 (svn r11652) -Codechange: add the svn $ header for several files
smatz
parents: 6943
diff changeset
     2
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     3
#ifndef AI_H
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     4
#define AI_H
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     5
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 4854
diff changeset
     6
#include "../network/network.h"
2727
90beb642e8d4 (svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents: 2715
diff changeset
     7
#include "../player.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8091
diff changeset
     8
#include "../command_type.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8116
diff changeset
     9
#include "../core/random_func.hpp"
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    10
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    11
/* How DoCommands look like for an AI */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    12
struct AICommand {
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    13
	uint32 tile;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    14
	uint32 p1;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    15
	uint32 p2;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    16
	uint32 procc;
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8091
diff changeset
    17
	CommandCallback *callback;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    18
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2727
diff changeset
    19
	char *text;
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2727
diff changeset
    20
	uint uid;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    21
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    22
	AICommand *next;
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    23
};
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    24
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    25
/* The struct for an AIScript Player */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    26
struct AIPlayer {
3173
f56ca618721b (svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents: 2767
diff changeset
    27
	bool active;            ///< Is this AI active?
f56ca618721b (svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents: 2767
diff changeset
    28
	AICommand *queue;       ///< The commands that he has in his queue
f56ca618721b (svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents: 2767
diff changeset
    29
	AICommand *queue_tail;  ///< The tail of this queue
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    30
};
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    31
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    32
/* The struct to keep some data about the AI in general */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    33
struct AIStruct {
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    34
	/* General */
3173
f56ca618721b (svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents: 2767
diff changeset
    35
	bool enabled;           ///< Is AI enabled?
f56ca618721b (svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents: 2767
diff changeset
    36
	uint tick;              ///< The current tick (something like _frame_counter, only for AIs)
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    37
};
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    38
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    39
VARDEF AIStruct _ai;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    40
VARDEF AIPlayer _ai_player[MAX_PLAYERS];
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    41
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    42
// ai.c
2551
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2395
diff changeset
    43
void AI_StartNewAI(PlayerID player);
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2395
diff changeset
    44
void AI_PlayerDied(PlayerID player);
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5475
diff changeset
    45
void AI_RunGameLoop();
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5475
diff changeset
    46
void AI_Initialize();
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5475
diff changeset
    47
void AI_Uninitialize();
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6491
diff changeset
    48
CommandCost AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6491
diff changeset
    49
CommandCost AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, CommandCallback* callback);
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    50
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    51
/** Is it allowed to start a new AI.
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    52
 * This function checks some boundries to see if we should launch a new AI.
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    53
 * @return True if we can start a new AI.
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    54
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5475
diff changeset
    55
static inline bool AI_AllowNewAI()
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    56
{
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    57
	/* If disabled, no AI */
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    58
	if (!_ai.enabled)
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    59
		return false;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    60
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    61
	/* If in network, but no server, no AI */
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    62
	if (_networking && !_network_server)
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    63
		return false;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    64
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    65
	/* If in network, and server, possible AI */
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    66
	if (_networking && _network_server) {
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    67
		/* Do we want AIs in multiplayer? */
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    68
		if (!_patches.ai_in_multiplayer)
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    69
			return false;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    70
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    71
		/* Only the NewAI is allowed... sadly enough the old AI just doesn't support this
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    72
		 *  system, because all commands are delayed by at least 1 tick, which causes
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    73
		 *  a big problem, because it uses variables that are only set AFTER the command
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    74
		 *  is really executed... */
2767
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2759
diff changeset
    75
		if (!_patches.ainew_active)
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    76
			return false;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    77
	}
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    78
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    79
	return true;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    80
}
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2551
diff changeset
    81
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6248
diff changeset
    82
#define AI_CHANCE16(a, b)    ((uint16)     AI_Random()  <= (uint16)((65536 * a) / b))
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6248
diff changeset
    83
#define AI_CHANCE16R(a, b, r) ((uint16)(r = AI_Random()) <= (uint16)((65536 * a) / b))
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    84
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    85
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    86
 * The random-function that should be used by ALL AIs.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    87
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    88
static inline uint AI_RandomRange(uint max)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    89
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    90
	/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    91
	 *   but we pick InteractiveRandomRange if we are a network_server or network-client.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    92
	 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    93
	if (_networking)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    94
		return InteractiveRandomRange(max);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    95
	else
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    96
		return RandomRange(max);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    97
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    98
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    99
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   100
 * The random-function that should be used by ALL AIs.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   101
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5475
diff changeset
   102
static inline uint32 AI_Random()
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   103
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   104
/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   105
	 *   but we pick InteractiveRandomRange if we are a network_server or network-client.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   106
	 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   107
	if (_networking)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   108
		return InteractiveRandom();
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   109
	else
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   110
		return Random();
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   111
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   112
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   113
#endif /* AI_H */