src/ai/ai.cpp
author truebrain
Mon, 05 May 2008 12:35:38 +0000
branchnoai
changeset 10412 ef44f62cb8b9
parent 9850 d3228c6a1376
child 10455 22c441f5adf9
permissions -rw-r--r--
(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
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
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
     3
/** @file ai.cpp handles the communication between the AI layer and the OpenTTD core */
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
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    17
#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
    18
#include "ai_threads.h"
9433
d439c1c469f4 (svn r9238) [NoAI] -Codechange: removed ai/api/ai_factory.h, as it isn't an api header
truelight
parents: 9429
diff changeset
    19
#include "ai_factory.hpp"
9429
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    20
#include "api/ai_base.hpp"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    21
#include "api/ai_controller.hpp"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    22
#include "../signal_func.h"
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    23
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents: 9360
diff changeset
    24
static AIController *_ai_player[MAX_PLAYERS];
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
    25
static AIFactoryBase *_ai_factory[MAX_PLAYERS];
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    26
static uint _ai_frame_counter;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    27
static bool _ai_enabled;
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    28
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
    29
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    30
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    31
 * The gameloop for AIs.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    32
 *  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
    33
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5860
diff changeset
    34
void AI_RunGameLoop()
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    35
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    36
	/* 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
    37
	if (!_ai_enabled) return;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    38
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
    39
	/* 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
    40
	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
    41
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    42
	/* New tick */
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    43
	_ai_frame_counter++;
2395
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
	/* Make sure the AI follows the difficulty rule.. */
2446
df659fbe7e4f (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
    46
	assert(_opt.diff.competitor_speed <= 4);
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    47
	if ((_ai_frame_counter & ((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
    48
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    49
	/* 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
    50
	if (!_networking || _network_server) {
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    51
		/* 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
    52
		const Player *p;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    53
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    54
		FOR_ALL_PLAYERS(p) {
2767
3282c77ffc27 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
    55
			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
    56
				/* 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
    57
				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
    58
				_current_player = p->index;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    59
				AI_RunTick(p->index);
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
	}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    63
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    64
	_current_player = OWNER_NONE;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    65
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    66
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
    67
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
    68
{
9822
e2f83940bc52 (svn r12430) [NoAI] -Fix: safeguard, don't allow AI_Event calls to players that are not AIs
truebrain
parents: 9770
diff changeset
    69
	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
    70
	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
    71
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
    72
	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
    73
	_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
    74
	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
    75
	_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
    76
}
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
    77
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    78
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    79
 * 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
    80
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
    81
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
    82
{
4850
b4e9be22945f (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4848
diff changeset
    83
	assert(IsValidPlayer(player));
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    84
	if (!_ai_enabled) return;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    85
	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
    86
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
    87
	AIFactoryBase *factory;
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    88
	if (_forced_ai_name == NULL) {
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
    89
		factory = AIFactoryBase::SelectRandomAI();
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    90
	} else {
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
    91
		factory = AIFactoryBase::SelectAI(_forced_ai_name);
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    92
	}
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
    93
	if (factory == NULL) {
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    94
		if (_forced_ai_name == NULL) {
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    95
			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
    96
		} else {
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    97
			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
    98
		}
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents: 9383
diff changeset
    99
		/* 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
   100
		return;
9383
817b07079052 (svn r9173) [NoAI] -Codechange: start both AIs when the game starts more than one.
rubidium
parents: 9365
diff changeset
   101
	}
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
   102
9850
d3228c6a1376 (svn r12518) [NoAI] -Fix: minor change of order avoids silly one-in-a-milion errors :)
truebrain
parents: 9822
diff changeset
   103
	_ai_factory[player] = factory;
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
   104
	_ai_player[player] = factory->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
   105
	_current_player = player;
9454
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9444
diff changeset
   106
	AIObject::ResetInternalPlayerData();
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
	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
   108
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   109
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   110
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   111
 * 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
   112
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   113
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
   114
{
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   115
	if (!_ai_enabled) return;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   116
9643
413e30aed44e (svn r10535) [NoAI] -Fix: set _current_player also for dying AI (tnx Rubidium for the help!!)
truelight
parents: 9621
diff changeset
   117
	_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
   118
	AI_StopPlayer(player);
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   119
	/* 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
   120
	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
   121
	_ai_player[player] = NULL;
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
   122
	_ai_factory[player] = NULL;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   123
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   124
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   125
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   126
 * Initialize some AI-related stuff.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   127
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5860
diff changeset
   128
void AI_Initialize()
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   129
{
2706
d31bd0aa0096 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   130
	/* 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
   131
	AI_Uninitialize();
d31bd0aa0096 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   132
2767
3282c77ffc27 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   133
	memset(&_ai_player, 0, sizeof(_ai_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
   134
	memset(&_ai_factory, 0, sizeof(_ai_factory));
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   135
	_ai_frame_counter = 0;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   136
	_ai_enabled = true;
9400
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9390
diff changeset
   137
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9390
diff changeset
   138
	AIFactoryBase::RunInitializers();
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   139
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   140
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   141
/**
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   142
 * 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
   143
 * 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
   144
 * @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
   145
 */
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   146
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
   147
{
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   148
	free(_forced_ai_name);
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   149
	_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
   150
}
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   151
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
   152
AIFactoryBase *AI_GetPlayerFactory(PlayerID player)
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
   153
{
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
   154
	return _ai_factory[player];
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
   155
}
9467
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
/**
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   158
 * Deinitializer for AI-related stuff.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   159
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5860
diff changeset
   160
void AI_Uninitialize()
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   161
{
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
   162
	const Player* p;
2395
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
	FOR_ALL_PLAYERS(p) {
2767
3282c77ffc27 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   165
		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
   166
	}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   167
}
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   168
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   169
/**
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   170
 * 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
   171
 * 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
   172
 * @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
   173
 */
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   174
bool AI_AllowNewAI()
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   175
{
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   176
	/* If disabled, no AI */
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   177
	if (!_ai_enabled) return false;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   178
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   179
	/* 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
   180
	if (_networking && !_network_server) return false;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   181
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   182
	/* If in network, and server, possible AI */
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   183
	if (_networking && _network_server) {
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   184
		/* Do we want AIs in multiplayer? */
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   185
		if (!_patches.ai_in_multiplayer) return false;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   186
	}
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   187
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   188
	return true;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   189
}
9621
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   190
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   191
#else /* NO_THREADS */
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   192
/* Stub functions for when we do not have threads. */
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   193
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
   194
void AI_PlayerDied(PlayerID player) {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   195
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
   196
void AI_Event(PlayerID player, AIEvent *event) {}
9621
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   197
void AI_Initialize() {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   198
void AI_Uninitialize() {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   199
bool AI_AllowNewAI() { return false; }
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   200
void AI_ForceAI(const char *forced_ai) {}
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
   201
AIFactoryBase *AI_GetPlayerFactory(PlayerID player) { return NULL; }
9621
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   202
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2) {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   203
#endif /* NO_THREADS */