author | peter1138 |
Sun, 03 Aug 2008 17:35:08 +0000 | |
changeset 9834 | 7474149531c1 |
parent 9701 | da907123be89 |
child 10207 | c291a21b304e |
permissions | -rw-r--r-- |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
1 |
/* $Id$ */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
2 |
|
9111
48ce04029fe4
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
8306
diff
changeset
|
3 |
/** @file ai.cpp Base for all AIs. */ |
48ce04029fe4
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
8306
diff
changeset
|
4 |
|
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
5 |
#include "../stdafx.h" |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
6 |
#include "../openttd.h" |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
7 |
#include "../variables.h" |
8116
8da76dcb3287
(svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents:
7521
diff
changeset
|
8 |
#include "../command_func.h" |
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:
5332
diff
changeset
|
9 |
#include "../network/network.h" |
8130
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8116
diff
changeset
|
10 |
#include "../core/alloc_func.hpp" |
8254
1496654ca5e7
(svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents:
8229
diff
changeset
|
11 |
#include "../player_func.h" |
1496654ca5e7
(svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents:
8229
diff
changeset
|
12 |
#include "../player_base.h" |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
13 |
#include "ai.h" |
2447
071e4afe707c
(svn r2973) Move a function declaration somewhere where it belongs
tron
parents:
2446
diff
changeset
|
14 |
#include "default/default.h" |
9701
da907123be89
(svn r13809) -Fix: memory leak each time a "new ai" got (re)started.
rubidium
parents:
9659
diff
changeset
|
15 |
#include "trolly/trolly.h" |
8306
22e1344c5457
(svn r11871) -Fix [FS#1074]: do not update signals after each tile when building/removing a large block of track/signals/station
smatz
parents:
8268
diff
changeset
|
16 |
#include "../signal_func.h" |
2715
d406c6ed777e
(svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents:
2707
diff
changeset
|
17 |
|
8268
5027ad5e70a0
(svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents:
8254
diff
changeset
|
18 |
AIStruct _ai; |
5027ad5e70a0
(svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents:
8254
diff
changeset
|
19 |
AIPlayer _ai_player[MAX_PLAYERS]; |
5027ad5e70a0
(svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents:
8254
diff
changeset
|
20 |
|
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
21 |
/** |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
22 |
* Dequeues commands put in the queue via AI_PutCommandInQueue. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
23 |
*/ |
3692 | 24 |
static void AI_DequeueCommands(PlayerID player) |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
25 |
{ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
26 |
AICommand *com, *entry_com; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
27 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
28 |
entry_com = _ai_player[player].queue; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
29 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
30 |
/* It happens that DoCommandP issues a new DoCommandAI which adds a new command |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
31 |
* to this very same queue (don't argue about this, if it currently doesn't |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
32 |
* happen I can tell you it will happen with AIScript -- TrueLight). If we |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
33 |
* do not make the queue NULL, that commands will be dequeued immediatly. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
34 |
* Therefor we safe the entry-point to entry_com, and make the queue NULL, so |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
35 |
* the new queue can be safely built up. */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
36 |
_ai_player[player].queue = NULL; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
37 |
_ai_player[player].queue_tail = NULL; |
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 |
/* Dequeue all commands */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
40 |
while ((com = entry_com) != NULL) { |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
41 |
_current_player = player; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
42 |
|
2738
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
43 |
_cmd_text = com->text; |
3946
3c8c78208dbb
(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
|
44 |
DoCommandP(com->tile, com->p1, com->p2, com->callback, com->procc); |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
45 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
46 |
/* Free item */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
47 |
entry_com = com->next; |
3886 | 48 |
free(com->text); |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
49 |
free(com); |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
50 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
51 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
52 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
53 |
/** |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
54 |
* Needed for SP; we need to delay DoCommand with 1 tick, because else events |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
55 |
* will make infinite loops (AIScript). |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
56 |
*/ |
8116
8da76dcb3287
(svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents:
7521
diff
changeset
|
57 |
static void AI_PutCommandInQueue(PlayerID player, TileIndex tile, uint32 p1, uint32 p2, uint32 procc, CommandCallback* callback) |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
58 |
{ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
59 |
AICommand *com; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
60 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
61 |
if (_ai_player[player].queue_tail == NULL) { |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
62 |
/* There is no item in the queue yet, create the queue */ |
5609
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
63 |
_ai_player[player].queue = MallocT<AICommand>(1); |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
64 |
_ai_player[player].queue_tail = _ai_player[player].queue; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
65 |
} else { |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
66 |
/* Add an item at the end */ |
5609
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
67 |
_ai_player[player].queue_tail->next = MallocT<AICommand>(1); |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
68 |
_ai_player[player].queue_tail = _ai_player[player].queue_tail->next; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
69 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
70 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
71 |
/* This is our new item */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
72 |
com = _ai_player[player].queue_tail; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
73 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
74 |
/* Assign the info */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
75 |
com->tile = tile; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
76 |
com->p1 = p1; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
77 |
com->p2 = p2; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
78 |
com->procc = procc; |
3946
3c8c78208dbb
(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
|
79 |
com->callback = callback; |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
80 |
com->next = NULL; |
2738
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
81 |
com->text = NULL; |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
82 |
|
2738
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
83 |
/* Copy the cmd_text, if needed */ |
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
84 |
if (_cmd_text != NULL) { |
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
85 |
com->text = strdup(_cmd_text); |
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
86 |
_cmd_text = NULL; |
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
87 |
} |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
88 |
} |
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 |
/** |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
91 |
* Executes a raw DoCommand for the AI. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
92 |
*/ |
8116
8da76dcb3287
(svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents:
7521
diff
changeset
|
93 |
CommandCost AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint32 procc, CommandCallback* callback) |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
94 |
{ |
2551
00e02ecf5835
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
95 |
PlayerID old_lp; |
6950
14ecb0acdfb4
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
6943
diff
changeset
|
96 |
CommandCost res; |
3950 | 97 |
const char* tmp_cmdtext; |
2395
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 |
/* If you enable DC_EXEC with DC_QUERY_COST you are a really strange |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
100 |
* person.. should we check for those funny jokes? |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
101 |
*/ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
102 |
|
3950 | 103 |
/* The test already resets _cmd_text, so backup the pointer */ |
104 |
tmp_cmdtext = _cmd_text; |
|
2738
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
105 |
|
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
106 |
/* First, do a test-run to see if we can do this */ |
3491
35d747bb5e82
(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
|
107 |
res = DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc); |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
108 |
/* The command failed, or you didn't want to execute, or you are quering, return */ |
4000
4009d092b306
(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 (CmdFailed(res) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) { |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
110 |
return res; |
2738
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
111 |
} |
01590c341f54
(svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents:
2734
diff
changeset
|
112 |
|
3950 | 113 |
/* Restore _cmd_text */ |
114 |
_cmd_text = tmp_cmdtext; |
|
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
115 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
116 |
/* NetworkSend_Command needs _local_player to be set correctly, so |
4000
4009d092b306
(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
|
117 |
* adjust it, and put it back right after the function */ |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
118 |
old_lp = _local_player; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
119 |
_local_player = _current_player; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
120 |
|
2727
90beb642e8d4
(svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents:
2724
diff
changeset
|
121 |
#ifdef ENABLE_NETWORK |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
122 |
/* Send the command */ |
4000
4009d092b306
(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 |
if (_networking) { |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
124 |
/* Network is easy, send it to his handler */ |
3946
3c8c78208dbb
(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
|
125 |
NetworkSend_Command(tile, p1, p2, procc, callback); |
4000
4009d092b306
(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
|
126 |
} else { |
4009d092b306
(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
|
127 |
#else |
4009d092b306
(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
|
128 |
{ |
2727
90beb642e8d4
(svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents:
2724
diff
changeset
|
129 |
#endif |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
130 |
/* If we execute BuildCommands directly in SP, we have a big problem with events |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
131 |
* so we need to delay is for 1 tick */ |
3946
3c8c78208dbb
(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
|
132 |
AI_PutCommandInQueue(_current_player, tile, p1, p2, procc, callback); |
4000
4009d092b306
(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
|
133 |
} |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
134 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
135 |
/* Set _local_player back */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
136 |
_local_player = old_lp; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
137 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
138 |
return res; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
139 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
140 |
|
3946
3c8c78208dbb
(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 |
|
8116
8da76dcb3287
(svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents:
7521
diff
changeset
|
142 |
CommandCost AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint32 procc) |
3946
3c8c78208dbb
(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 |
{ |
3c8c78208dbb
(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
|
144 |
return AI_DoCommandCc(tile, p1, p2, flags, procc, NULL); |
3c8c78208dbb
(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
|
145 |
} |
3c8c78208dbb
(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
|
146 |
|
3c8c78208dbb
(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
|
147 |
|
2759
9a5079782c67
(svn r3304) -Add: allow AI-events to see the UID of the command
truelight
parents:
2752
diff
changeset
|
148 |
/** |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
149 |
* Run 1 tick of the AI. Don't overdo it, keep it realistic. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
150 |
*/ |
2551
00e02ecf5835
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
151 |
static void AI_RunTick(PlayerID player) |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
152 |
{ |
2767 | 153 |
extern void AiNewDoGameLoop(Player *p); |
154 |
||
155 |
Player *p = GetPlayer(player); |
|
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
156 |
_current_player = player; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
157 |
|
9413
7042a8ec3fa8
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
9358
diff
changeset
|
158 |
if (_settings_game.ai.ainew_active) { |
2767 | 159 |
AiNewDoGameLoop(p); |
160 |
} else { |
|
161 |
/* Enable all kind of cheats the old AI needs in order to operate correctly... */ |
|
162 |
_is_old_ai_player = true; |
|
163 |
AiDoGameLoop(p); |
|
164 |
_is_old_ai_player = false; |
|
2422
897a01f7c624
(svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents:
2395
diff
changeset
|
165 |
} |
8306
22e1344c5457
(svn r11871) -Fix [FS#1074]: do not update signals after each tile when building/removing a large block of track/signals/station
smatz
parents:
8268
diff
changeset
|
166 |
|
22e1344c5457
(svn r11871) -Fix [FS#1074]: do not update signals after each tile when building/removing a large block of track/signals/station
smatz
parents:
8268
diff
changeset
|
167 |
/* AI could change some track, so update signals */ |
22e1344c5457
(svn r11871) -Fix [FS#1074]: do not update signals after each tile when building/removing a large block of track/signals/station
smatz
parents:
8268
diff
changeset
|
168 |
UpdateSignalsInBuffer(); |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
169 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
170 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
171 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
172 |
/** |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
173 |
* The gameloop for AIs. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
174 |
* Handles one tick for all the AIs. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
175 |
*/ |
6247 | 176 |
void AI_RunGameLoop() |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
177 |
{ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
178 |
/* Don't do anything if ai is disabled */ |
2639 | 179 |
if (!_ai.enabled) return; |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
180 |
|
5332
f54c1fed42df
(svn r7494) -Fix: Really disable AI's in Multiplayer if you tell it so. In loaded games with
Darkvater
parents:
4854
diff
changeset
|
181 |
/* Don't do anything if we are a network-client, or the AI has been disabled */ |
9413
7042a8ec3fa8
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
9358
diff
changeset
|
182 |
if (_networking && (!_network_server || !_settings_game.ai.ai_in_multiplayer)) return; |
2682
7fa4b202b9f0
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
183 |
|
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
184 |
/* New tick */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
185 |
_ai.tick++; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
186 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
187 |
/* Make sure the AI follows the difficulty rule.. */ |
9413
7042a8ec3fa8
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
9358
diff
changeset
|
188 |
assert(_settings_game.difficulty.competitor_speed <= 4); |
7042a8ec3fa8
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
9358
diff
changeset
|
189 |
if ((_ai.tick & ((1 << (4 - _settings_game.difficulty.competitor_speed)) - 1)) != 0) return; |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
190 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
191 |
/* Check for AI-client (so joining a network with an AI) */ |
4854
151cdb683187
(svn r6780) -Codechange: Remove GPMI leftovers (-b impersonisation of AI in MP).
Darkvater
parents:
4850
diff
changeset
|
192 |
if (!_networking || _network_server) { |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
193 |
/* Check if we want to run AIs (server or SP only) */ |
4000
4009d092b306
(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
|
194 |
const Player* p; |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
195 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
196 |
FOR_ALL_PLAYERS(p) { |
9659
187142ff9b6c
(svn r13731) -Codechange: make a pool of the array of players.
rubidium
parents:
9652
diff
changeset
|
197 |
if (p->is_ai) { |
2767 | 198 |
/* This should always be true, else something went wrong... */ |
199 |
assert(_ai_player[p->index].active); |
|
200 |
||
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
201 |
/* Run the script */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
202 |
AI_DequeueCommands(p->index); |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
203 |
AI_RunTick(p->index); |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
204 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
205 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
206 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
207 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
208 |
_current_player = OWNER_NONE; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
209 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
210 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
211 |
/** |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
212 |
* A new AI sees the day of light. You can do here what ever you think is needed. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
213 |
*/ |
2551
00e02ecf5835
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
214 |
void AI_StartNewAI(PlayerID player) |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
215 |
{ |
9652
0405e98d8e96
(svn r13719) -Codechange: rename IsValidPlayer to IsValidPlayerID in line with all other structs/classes that are in a pool.
rubidium
parents:
9413
diff
changeset
|
216 |
assert(IsValidPlayerID(player)); |
2702
9172e3030a35
(svn r3246) -Fix: small glitch in ai_network_client code (network_client.c)
truelight
parents:
2701
diff
changeset
|
217 |
|
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
218 |
/* Called if a new AI is booted */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
219 |
_ai_player[player].active = true; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
220 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
221 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
222 |
/** |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
223 |
* This AI player died. Give it some chance to make a final puf. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
224 |
*/ |
2551
00e02ecf5835
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
225 |
void AI_PlayerDied(PlayerID player) |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
226 |
{ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
227 |
/* Called if this AI died */ |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
228 |
_ai_player[player].active = false; |
9701
da907123be89
(svn r13809) -Fix: memory leak each time a "new ai" got (re)started.
rubidium
parents:
9659
diff
changeset
|
229 |
|
da907123be89
(svn r13809) -Fix: memory leak each time a "new ai" got (re)started.
rubidium
parents:
9659
diff
changeset
|
230 |
if (_players_ainew[player].pathfinder == NULL) return; |
da907123be89
(svn r13809) -Fix: memory leak each time a "new ai" got (re)started.
rubidium
parents:
9659
diff
changeset
|
231 |
|
da907123be89
(svn r13809) -Fix: memory leak each time a "new ai" got (re)started.
rubidium
parents:
9659
diff
changeset
|
232 |
AyStarMain_Free(_players_ainew[player].pathfinder); |
da907123be89
(svn r13809) -Fix: memory leak each time a "new ai" got (re)started.
rubidium
parents:
9659
diff
changeset
|
233 |
delete _players_ainew[player].pathfinder; |
da907123be89
(svn r13809) -Fix: memory leak each time a "new ai" got (re)started.
rubidium
parents:
9659
diff
changeset
|
234 |
_players_ainew[player].pathfinder = NULL; |
da907123be89
(svn r13809) -Fix: memory leak each time a "new ai" got (re)started.
rubidium
parents:
9659
diff
changeset
|
235 |
|
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
236 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
237 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
238 |
/** |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
239 |
* Initialize some AI-related stuff. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
240 |
*/ |
6247 | 241 |
void AI_Initialize() |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
242 |
{ |
2706
fc872b9b17a7
(svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents:
2702
diff
changeset
|
243 |
/* First, make sure all AIs are DEAD! */ |
fc872b9b17a7
(svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents:
2702
diff
changeset
|
244 |
AI_Uninitialize(); |
fc872b9b17a7
(svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents:
2702
diff
changeset
|
245 |
|
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
246 |
memset(&_ai, 0, sizeof(_ai)); |
2767 | 247 |
memset(&_ai_player, 0, sizeof(_ai_player)); |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
248 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
249 |
_ai.enabled = true; |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
250 |
} |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
251 |
|
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
252 |
/** |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
253 |
* Deinitializer for AI-related stuff. |
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
254 |
*/ |
6247 | 255 |
void AI_Uninitialize() |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
256 |
{ |
9701
da907123be89
(svn r13809) -Fix: memory leak each time a "new ai" got (re)started.
rubidium
parents:
9659
diff
changeset
|
257 |
for (PlayerID p = PLAYER_FIRST; p < MAX_PLAYERS; p++) AI_PlayerDied(p); |
2395
d1629f64d157
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
258 |
} |