author | rubidium |
Wed, 07 Mar 2007 11:47:46 +0000 | |
changeset 6573 | 7624f942237f |
parent 5860 | 7fdc9b423ba1 |
child 6720 | 35756db7e577 |
child 7439 | 0c0e2945c890 |
child 9359 | e915dd81a279 |
permissions | -rw-r--r-- |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
1 |
/* $Id$ */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
2 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
3 |
#include "../stdafx.h" |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
4 |
#include "../openttd.h" |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
5 |
#include "../variables.h" |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
6 |
#include "../command.h" |
5720
cc0ceeafaa55
(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:
5332
diff
changeset
|
7 |
#include "../network/network.h" |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
8 |
#include "../helpers.hpp" |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
9 |
#include "ai.h" |
2447
47f2b670fb22
(svn r2973) Move a function declaration somewhere where it belongs
tron
parents:
2446
diff
changeset
|
10 |
#include "default/default.h" |
2715
0ad451d9264b
(svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents:
2707
diff
changeset
|
11 |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
12 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
13 |
* Dequeues commands put in the queue via AI_PutCommandInQueue. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
14 |
*/ |
3692 | 15 |
static void AI_DequeueCommands(PlayerID player) |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
16 |
{ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
17 |
AICommand *com, *entry_com; |
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 |
entry_com = _ai_player[player].queue; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
20 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
21 |
/* It happens that DoCommandP issues a new DoCommandAI which adds a new command |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
22 |
* to this very same queue (don't argue about this, if it currently doesn't |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
23 |
* happen I can tell you it will happen with AIScript -- TrueLight). If we |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
24 |
* do not make the queue NULL, that commands will be dequeued immediatly. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
25 |
* Therefor we safe the entry-point to entry_com, and make the queue NULL, so |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
26 |
* the new queue can be safely built up. */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
27 |
_ai_player[player].queue = NULL; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
28 |
_ai_player[player].queue_tail = NULL; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
29 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
30 |
/* Dequeue all commands */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
31 |
while ((com = entry_com) != NULL) { |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
32 |
_current_player = player; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
33 |
|
2738
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
34 |
_cmd_text = com->text; |
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
35 |
DoCommandP(com->tile, com->p1, com->p2, com->callback, com->procc); |
2395
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 |
/* Free item */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
38 |
entry_com = com->next; |
3886 | 39 |
free(com->text); |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
40 |
free(com); |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
41 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
42 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
43 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
44 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
45 |
* Needed for SP; we need to delay DoCommand with 1 tick, because else events |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
46 |
* will make infinite loops (AIScript). |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
47 |
*/ |
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
48 |
static void AI_PutCommandInQueue(PlayerID player, TileIndex tile, uint32 p1, uint32 p2, uint procc, CommandCallback* callback) |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
49 |
{ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
50 |
AICommand *com; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
51 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
52 |
if (_ai_player[player].queue_tail == NULL) { |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
53 |
/* There is no item in the queue yet, create the queue */ |
5860
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
54 |
_ai_player[player].queue = MallocT<AICommand>(1); |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
55 |
_ai_player[player].queue_tail = _ai_player[player].queue; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
56 |
} else { |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
57 |
/* Add an item at the end */ |
5860
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
58 |
_ai_player[player].queue_tail->next = MallocT<AICommand>(1); |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
59 |
_ai_player[player].queue_tail = _ai_player[player].queue_tail->next; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
60 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
61 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
62 |
/* This is our new item */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
63 |
com = _ai_player[player].queue_tail; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
64 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
65 |
/* Assign the info */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
66 |
com->tile = tile; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
67 |
com->p1 = p1; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
68 |
com->p2 = p2; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
69 |
com->procc = procc; |
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
70 |
com->callback = callback; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
71 |
com->next = NULL; |
2738
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
72 |
com->text = NULL; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
73 |
|
2738
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
74 |
/* Copy the cmd_text, if needed */ |
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
75 |
if (_cmd_text != NULL) { |
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
76 |
com->text = strdup(_cmd_text); |
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
77 |
_cmd_text = NULL; |
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
78 |
} |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
79 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
80 |
|
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 |
* Executes a raw DoCommand for the AI. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
83 |
*/ |
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
84 |
int32 AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, CommandCallback* callback) |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
85 |
{ |
2551
436aaaa22ba5
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
86 |
PlayerID old_lp; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
87 |
int32 res = 0; |
3950 | 88 |
const char* tmp_cmdtext; |
2395
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 you enable DC_EXEC with DC_QUERY_COST you are a really strange |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
91 |
* person.. should we check for those funny jokes? |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
92 |
*/ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
93 |
|
3950 | 94 |
/* The test already resets _cmd_text, so backup the pointer */ |
95 |
tmp_cmdtext = _cmd_text; |
|
2738
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
96 |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
97 |
/* First, do a test-run to see if we can do this */ |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
2817
diff
changeset
|
98 |
res = DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc); |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
99 |
/* The command failed, or you didn't want to execute, or you are quering, return */ |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
100 |
if (CmdFailed(res) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) { |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
101 |
return res; |
2738
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
102 |
} |
d8de2d4bee17
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
103 |
|
3950 | 104 |
/* Restore _cmd_text */ |
105 |
_cmd_text = tmp_cmdtext; |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
106 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
107 |
/* If we did a DC_EXEC, and the command did not return an error, execute it |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
108 |
* over the network */ |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
109 |
if (flags & DC_AUTO) procc |= CMD_AUTO; |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
110 |
if (flags & DC_NO_WATER) procc |= CMD_NO_WATER; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
111 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
112 |
/* NetworkSend_Command needs _local_player to be set correctly, so |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
113 |
* adjust it, and put it back right after the function */ |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
114 |
old_lp = _local_player; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
115 |
_local_player = _current_player; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
116 |
|
2727
6c726ecbc31c
(svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents:
2724
diff
changeset
|
117 |
#ifdef ENABLE_NETWORK |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
118 |
/* Send the command */ |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
119 |
if (_networking) { |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
120 |
/* Network is easy, send it to his handler */ |
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
121 |
NetworkSend_Command(tile, p1, p2, procc, callback); |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
122 |
} else { |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
123 |
#else |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
124 |
{ |
2727
6c726ecbc31c
(svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents:
2724
diff
changeset
|
125 |
#endif |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
126 |
/* If we execute BuildCommands directly in SP, we have a big problem with events |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
127 |
* so we need to delay is for 1 tick */ |
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
128 |
AI_PutCommandInQueue(_current_player, tile, p1, p2, procc, callback); |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
129 |
} |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
130 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
131 |
/* Set _local_player back */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
132 |
_local_player = old_lp; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
133 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
134 |
return res; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
135 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
136 |
|
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
137 |
|
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
138 |
int32 AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc) |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
139 |
{ |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
140 |
return AI_DoCommandCc(tile, p1, p2, flags, procc, NULL); |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
141 |
} |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
142 |
|
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
143 |
|
2759
c2a6fd12b41f
(svn r3304) -Add: allow AI-events to see the UID of the command
truelight
parents:
2752
diff
changeset
|
144 |
/** |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
145 |
* Run 1 tick of the AI. Don't overdo it, keep it realistic. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
146 |
*/ |
2551
436aaaa22ba5
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
147 |
static void AI_RunTick(PlayerID player) |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
148 |
{ |
2767 | 149 |
extern void AiNewDoGameLoop(Player *p); |
150 |
||
151 |
Player *p = GetPlayer(player); |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
152 |
_current_player = player; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
153 |
|
2767 | 154 |
if (_patches.ainew_active) { |
155 |
AiNewDoGameLoop(p); |
|
156 |
} else { |
|
157 |
/* Enable all kind of cheats the old AI needs in order to operate correctly... */ |
|
158 |
_is_old_ai_player = true; |
|
159 |
AiDoGameLoop(p); |
|
160 |
_is_old_ai_player = false; |
|
2422
914a12dee832
(svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents:
2395
diff
changeset
|
161 |
} |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
162 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
163 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
164 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
165 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
166 |
* The gameloop for AIs. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
167 |
* Handles one tick for all the AIs. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
168 |
*/ |
6573 | 169 |
void AI_RunGameLoop() |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
170 |
{ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
171 |
/* Don't do anything if ai is disabled */ |
2639 | 172 |
if (!_ai.enabled) return; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
173 |
|
5332
ffb2e98b961e
(svn r7494) -Fix: Really disable AI's in Multiplayer if you tell it so. In loaded games with
Darkvater
parents:
4854
diff
changeset
|
174 |
/* Don't do anything if we are a network-client, or the AI has been disabled */ |
ffb2e98b961e
(svn r7494) -Fix: Really disable AI's in Multiplayer if you tell it so. In loaded games with
Darkvater
parents:
4854
diff
changeset
|
175 |
if (_networking && (!_network_server || !_patches.ai_in_multiplayer)) return; |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
176 |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
177 |
/* New tick */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
178 |
_ai.tick++; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
179 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
180 |
/* Make sure the AI follows the difficulty rule.. */ |
2446 | 181 |
assert(_opt.diff.competitor_speed <= 4); |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
182 |
if ((_ai.tick & ((1 << (4 - _opt.diff.competitor_speed)) - 1)) != 0) return; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
183 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
184 |
/* Check for AI-client (so joining a network with an AI) */ |
4854
383ef523793f
(svn r6780) -Codechange: Remove GPMI leftovers (-b impersonisation of AI in MP).
Darkvater
parents:
4850
diff
changeset
|
185 |
if (!_networking || _network_server) { |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
186 |
/* Check if we want to run AIs (server or SP only) */ |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
187 |
const Player* p; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
188 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
189 |
FOR_ALL_PLAYERS(p) { |
2767 | 190 |
if (p->is_active && p->is_ai) { |
191 |
/* This should always be true, else something went wrong... */ |
|
192 |
assert(_ai_player[p->index].active); |
|
193 |
||
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
194 |
/* Run the script */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
195 |
AI_DequeueCommands(p->index); |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
196 |
AI_RunTick(p->index); |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
197 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
198 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
199 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
200 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
201 |
_current_player = OWNER_NONE; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
202 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
203 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
204 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
205 |
* A new AI sees the day of light. You can do here what ever you think is needed. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
206 |
*/ |
2551
436aaaa22ba5
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
207 |
void AI_StartNewAI(PlayerID player) |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
208 |
{ |
4850
b4e9be22945f
(svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents:
4848
diff
changeset
|
209 |
assert(IsValidPlayer(player)); |
2702
e4663e88c530
(svn r3246) -Fix: small glitch in ai_network_client code (network_client.c)
truelight
parents:
2701
diff
changeset
|
210 |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
211 |
/* Called if a new AI is booted */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
212 |
_ai_player[player].active = true; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
213 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
214 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
215 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
216 |
* This AI player died. Give it some chance to make a final puf. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
217 |
*/ |
2551
436aaaa22ba5
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
218 |
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
|
219 |
{ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
220 |
/* Called if this AI died */ |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
221 |
_ai_player[player].active = false; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
222 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
223 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
224 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
225 |
* Initialize some AI-related stuff. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
226 |
*/ |
6573 | 227 |
void AI_Initialize() |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
228 |
{ |
2706
d31bd0aa0096
(svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents:
2702
diff
changeset
|
229 |
/* First, make sure all AIs are DEAD! */ |
d31bd0aa0096
(svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents:
2702
diff
changeset
|
230 |
AI_Uninitialize(); |
d31bd0aa0096
(svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents:
2702
diff
changeset
|
231 |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
232 |
memset(&_ai, 0, sizeof(_ai)); |
2767 | 233 |
memset(&_ai_player, 0, sizeof(_ai_player)); |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
234 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
235 |
_ai.enabled = true; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
236 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
237 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
238 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
239 |
* Deinitializer for AI-related stuff. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
240 |
*/ |
6573 | 241 |
void AI_Uninitialize() |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
242 |
{ |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3950
diff
changeset
|
243 |
const Player* p; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
244 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
245 |
FOR_ALL_PLAYERS(p) { |
2767 | 246 |
if (p->is_active && p->is_ai) AI_PlayerDied(p->index); |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
247 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
248 |
} |