author | glx |
Mon, 26 May 2008 17:40:33 +0000 | |
branch | noai |
changeset 10718 | 7e9d9e40e16f |
parent 10715 | 6bdf79ffb022 |
child 10739 | 0d70706cf183 |
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 |
|
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10412
diff
changeset
|
3 |
/** @file ai.cpp Handles the communication between the AI layer and the OpenTTD core */ |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
4 |
|
9429
25b7d020a3a9
(svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents:
9427
diff
changeset
|
5 |
#include "../stdafx.h" |
25b7d020a3a9
(svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents:
9427
diff
changeset
|
6 |
#include "../openttd.h" |
25b7d020a3a9
(svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents:
9427
diff
changeset
|
7 |
#include "../variables.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9682
diff
changeset
|
8 |
#include "../command_func.h" |
9429
25b7d020a3a9
(svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents:
9427
diff
changeset
|
9 |
#include "../network/network.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9682
diff
changeset
|
10 |
#include "../core/alloc_func.hpp" |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
11 |
#include "../settings_type.h" |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
12 |
#include "../player_base.h" |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
13 |
#include "../player_func.h" |
9429
25b7d020a3a9
(svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents:
9427
diff
changeset
|
14 |
#include "../debug.h" |
9621
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
15 |
|
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
16 |
#ifndef NO_THREADS |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
17 |
#include <squirrel.h> |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
18 |
#include "../squirrel.hpp" |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
19 |
#include "../squirrel_helper.hpp" |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
20 |
#include "../squirrel_class.hpp" |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
21 |
#include "../squirrel_std.hpp" |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
22 |
#include "api/ai_controller.hpp" |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
23 |
#include "ai_squirrel.hpp" |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
24 |
#include "ai_info.hpp" |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
25 |
#include "ai.h" |
9444
fd27df7ca2a0
(svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents:
9441
diff
changeset
|
26 |
#include "ai_threads.h" |
9429
25b7d020a3a9
(svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents:
9427
diff
changeset
|
27 |
#include "api/ai_base.hpp" |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
28 |
#include "../signal_func.h" |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
29 |
|
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
9360
diff
changeset
|
30 |
static AIController *_ai_player[MAX_PLAYERS]; |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
31 |
static AIInfo *_ai_info[MAX_PLAYERS]; |
10649
9034b80fdbdb
(svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents:
10643
diff
changeset
|
32 |
static AISquirrel *_ai_squirrel = NULL; |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
33 |
static uint _ai_frame_counter; |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
34 |
static bool _ai_enabled; |
9467
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
35 |
static char *_forced_ai_name = NULL; |
2715
0ad451d9264b
(svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents:
2707
diff
changeset
|
36 |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
37 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
38 |
* The gameloop for AIs. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
39 |
* 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
|
40 |
*/ |
6573 | 41 |
void AI_RunGameLoop() |
2395
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 |
/* Don't do anything if ai is disabled */ |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
44 |
if (!_ai_enabled) return; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
45 |
|
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
|
46 |
/* Don't do anything if we are a network-client, or the AI has been disabled */ |
10715
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10650
diff
changeset
|
47 |
if (_networking && (!_network_server || !_settings.ai.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
|
48 |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
49 |
/* New tick */ |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
50 |
_ai_frame_counter++; |
2395
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 |
/* Make sure the AI follows the difficulty rule.. */ |
10715
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10650
diff
changeset
|
53 |
assert(_settings.difficulty.competitor_speed <= 4); |
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10650
diff
changeset
|
54 |
if ((_ai_frame_counter & ((1 << (4 - _settings.difficulty.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
|
55 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
56 |
/* 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
|
57 |
if (!_networking || _network_server) { |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
58 |
/* Check if we want to run AIs (server or SP only) */ |
9822
e2f83940bc52
(svn r12430) [NoAI] -Fix: safeguard, don't allow AI_Event calls to players that are not AIs
truebrain
parents:
9770
diff
changeset
|
59 |
const Player *p; |
2395
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 |
FOR_ALL_PLAYERS(p) { |
2767 | 62 |
if (p->is_active && p->is_ai) { |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
63 |
/* Run the script */ |
9444
fd27df7ca2a0
(svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents:
9441
diff
changeset
|
64 |
if (_ai_player[p->index] == NULL) continue; |
fd27df7ca2a0
(svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents:
9441
diff
changeset
|
65 |
_current_player = p->index; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
66 |
AI_RunTick(p->index); |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
67 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
68 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
69 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
70 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
71 |
_current_player = OWNER_NONE; |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
72 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
73 |
|
9682
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
74 |
void AI_Event(PlayerID player, AIEvent *event) |
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
75 |
{ |
9822
e2f83940bc52
(svn r12430) [NoAI] -Fix: safeguard, don't allow AI_Event calls to players that are not AIs
truebrain
parents:
9770
diff
changeset
|
76 |
if (player >= MAX_PLAYERS || !GetPlayer(player)->is_active || !GetPlayer(player)->is_ai) return; |
10412
ef44f62cb8b9
(svn r12954) [NoAI] -Fix: move the DEBUG level of non-essential debug statements up a few levels, so it doesn't show up at least before lvl 5
truebrain
parents:
9850
diff
changeset
|
77 |
DEBUG(ai, 5, "Event (%d) for player %d\n", event->GetEventType(), player); |
9682
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
78 |
|
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
79 |
PlayerID old_player = _current_player; |
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
80 |
_current_player = player; |
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
81 |
AIEventController::InsertEvent(event); |
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
82 |
_current_player = old_player; |
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
83 |
} |
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
84 |
|
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
85 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
86 |
* 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
|
87 |
*/ |
2551
436aaaa22ba5
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
88 |
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
|
89 |
{ |
4850
b4e9be22945f
(svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents:
4848
diff
changeset
|
90 |
assert(IsValidPlayer(player)); |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
91 |
if (!_ai_enabled) return; |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
92 |
if (_networking && !_network_server) return; |
2702
e4663e88c530
(svn r3246) -Fix: small glitch in ai_network_client code (network_client.c)
truelight
parents:
2701
diff
changeset
|
93 |
|
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
94 |
AIInfo *info; |
9467
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
95 |
if (_forced_ai_name == NULL) { |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
96 |
info = _ai_squirrel->SelectRandomAI(); |
9467
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
97 |
} else { |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
98 |
info = _ai_squirrel->SelectAI(_forced_ai_name); |
9467
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
99 |
} |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
100 |
if (info == NULL) { |
9467
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
101 |
if (_forced_ai_name == NULL) { |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
102 |
DEBUG(ai, 0, "Couldn't find a suitable AI to start for company '%d'", player); |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
103 |
} else { |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
104 |
DEBUG(ai, 0, "The AI named \"%s\" is unknown or not suitable", _forced_ai_name); |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
105 |
} |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
9383
diff
changeset
|
106 |
/* XXX -- We have no clean way to handle this yet! */ |
9441
03da911c8d5f
(svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
9433
diff
changeset
|
107 |
return; |
9383
817b07079052
(svn r9173) [NoAI] -Codechange: start both AIs when the game starts more than one.
rubidium
parents:
9365
diff
changeset
|
108 |
} |
9770
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9724
diff
changeset
|
109 |
|
10650
30fc5395b1b8
(svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents:
10649
diff
changeset
|
110 |
_current_player = player; |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
111 |
_ai_info[player] = info; |
10650
30fc5395b1b8
(svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents:
10649
diff
changeset
|
112 |
AIObject::ResetInternalPlayerData(); |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
113 |
_ai_player[player] = info->CreateInstance(); |
9441
03da911c8d5f
(svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
9433
diff
changeset
|
114 |
AI_StartPlayer(player, _ai_player[player]); |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
115 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
116 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
117 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
118 |
* 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
|
119 |
*/ |
2551
436aaaa22ba5
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2548
diff
changeset
|
120 |
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
|
121 |
{ |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
122 |
if (!_ai_enabled) return; |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
123 |
|
9643
413e30aed44e
(svn r10535) [NoAI] -Fix: set _current_player also for dying AI (tnx Rubidium for the help!!)
truelight
parents:
9621
diff
changeset
|
124 |
_current_player = player; |
9444
fd27df7ca2a0
(svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents:
9441
diff
changeset
|
125 |
AI_StopPlayer(player); |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
126 |
/* Called if this AI died */ |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
9360
diff
changeset
|
127 |
delete _ai_player[player]; |
9444
fd27df7ca2a0
(svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents:
9441
diff
changeset
|
128 |
_ai_player[player] = NULL; |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
129 |
_ai_info[player] = NULL; |
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 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
132 |
/** |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
133 |
* Initialize some AI-related stuff. |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
134 |
*/ |
6573 | 135 |
void AI_Initialize() |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
136 |
{ |
2706
d31bd0aa0096
(svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents:
2702
diff
changeset
|
137 |
/* 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
|
138 |
AI_Uninitialize(); |
d31bd0aa0096
(svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents:
2702
diff
changeset
|
139 |
|
2767 | 140 |
memset(&_ai_player, 0, sizeof(_ai_player)); |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
141 |
memset(&_ai_info, 0, sizeof(_ai_info)); |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
142 |
_ai_frame_counter = 0; |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
143 |
_ai_enabled = true; |
9400
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9390
diff
changeset
|
144 |
|
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
145 |
/* Create the Squirrel collector, which scans for all AIs available */ |
10649
9034b80fdbdb
(svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents:
10643
diff
changeset
|
146 |
assert(_ai_squirrel == NULL); |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
147 |
_ai_squirrel = new AISquirrel(); |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
148 |
} |
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
149 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
150 |
/** |
9467
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
151 |
* Forces the given AI to be used for all players. |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
152 |
* Pass NULL to use a random AI. |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
153 |
* @param forced_ai the AI to force. |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
154 |
*/ |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
155 |
void AI_ForceAI(const char *forced_ai) |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
156 |
{ |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
157 |
free(_forced_ai_name); |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
158 |
_forced_ai_name = (forced_ai == NULL) ? NULL : strdup(forced_ai); |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
159 |
} |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
160 |
|
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
161 |
char *AI_GetConsoleList(char *p, const char *last) |
9770
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9724
diff
changeset
|
162 |
{ |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
163 |
return _ai_squirrel->GetAIConsoleList(p, last); |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
164 |
} |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
165 |
|
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
166 |
AIInfo *AI_GetPlayerInfo(PlayerID player) |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
167 |
{ |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
168 |
return _ai_info[player]; |
9770
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9724
diff
changeset
|
169 |
} |
9467
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
170 |
|
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9454
diff
changeset
|
171 |
/** |
10718 | 172 |
* Kill all AIs. |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
173 |
*/ |
10718 | 174 |
void AI_KillAll() |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
175 |
{ |
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
|
176 |
const Player* p; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
177 |
|
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
178 |
FOR_ALL_PLAYERS(p) { |
2767 | 179 |
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
|
180 |
} |
10718 | 181 |
} |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
182 |
|
10718 | 183 |
/** |
184 |
* Deinitializer for AI-related stuff. |
|
185 |
*/ |
|
186 |
void AI_Uninitialize() |
|
187 |
{ |
|
188 |
AI_KillAll(); |
|
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
189 |
delete _ai_squirrel; |
10649
9034b80fdbdb
(svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents:
10643
diff
changeset
|
190 |
_ai_squirrel = NULL; |
2395
19b4da30806b
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff
changeset
|
191 |
} |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
192 |
|
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
193 |
/** |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
194 |
* Is it allowed to start a new AI. |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
195 |
* This function checks some boundries to see if we should launch a new AI. |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
196 |
* @return True if we can start a new AI. |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
197 |
*/ |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
198 |
bool AI_AllowNewAI() |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
199 |
{ |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
200 |
/* If disabled, no AI */ |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
201 |
if (!_ai_enabled) return false; |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
202 |
|
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
203 |
/* If in network, but no server, no AI */ |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
204 |
if (_networking && !_network_server) return false; |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
205 |
|
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
206 |
/* If in network, and server, possible AI */ |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
207 |
if (_networking && _network_server) { |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
208 |
/* Do we want AIs in multiplayer? */ |
10715
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10650
diff
changeset
|
209 |
if (!_settings.ai.ai_in_multiplayer) return false; |
9360
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
210 |
} |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
211 |
|
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
212 |
return true; |
c20d0a9e0a5c
(svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents:
9359
diff
changeset
|
213 |
} |
9621
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
214 |
|
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
215 |
#else /* NO_THREADS */ |
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
216 |
/* Stub functions for when we do not have threads. */ |
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
217 |
void AI_StartNewAI(PlayerID player) { DEBUG(ai, 0, "Threading is disabled and therefor the AI too."); } |
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
218 |
void AI_PlayerDied(PlayerID player) {} |
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
219 |
void AI_RunGameLoop() {} |
9682
d031eb183733
(svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents:
9643
diff
changeset
|
220 |
void AI_Event(PlayerID player, AIEvent *event) {} |
9621
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
221 |
void AI_Initialize() {} |
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
222 |
void AI_Uninitialize() {} |
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
223 |
bool AI_AllowNewAI() { return false; } |
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
224 |
void AI_ForceAI(const char *forced_ai) {} |
10643
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
225 |
char *AI_GetConsoleList(char *p, const char *last) { return p; } |
970417eef395
(svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents:
10455
diff
changeset
|
226 |
AIInfo *AI_GetPlayerInfo(PlayerID player) { return NULL; } |
9621
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
227 |
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2) {} |
7654501cf02d
(svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents:
9467
diff
changeset
|
228 |
#endif /* NO_THREADS */ |