src/ai/ai.cpp
author truelight
Fri, 13 Jul 2007 10:37:01 +0000
branchnoai
changeset 9643 413e30aed44e
parent 9621 7654501cf02d
child 9682 d031eb183733
permissions -rw-r--r--
(svn r10535) [NoAI] -Fix: set _current_player also for dying AI (tnx Rubidium for the help!!)
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"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
     8
#include "../command.h"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
     9
#include "../network/network.h"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    10
#include "../helpers.hpp"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    11
#include "../debug.h"
9621
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
    12
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
    13
#ifndef NO_THREADS
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    14
#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
    15
#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
    16
#include "ai_factory.hpp"
9429
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    17
#include "api/ai_base.hpp"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    18
#include "api/ai_controller.hpp"
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    19
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents: 9360
diff changeset
    20
static AIController *_ai_player[MAX_PLAYERS];
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    21
static uint _ai_frame_counter;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    22
static bool _ai_enabled;
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    23
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
    24
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    25
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    26
 * The gameloop for AIs.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    27
 *  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
    28
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5860
diff changeset
    29
void AI_RunGameLoop()
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
	/* 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
    32
	if (!_ai_enabled) return;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    33
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
    34
	/* 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
    35
	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
    36
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    37
	/* New tick */
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    38
	_ai_frame_counter++;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    39
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    40
	/* Make sure the AI follows the difficulty rule.. */
2446
df659fbe7e4f (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
    41
	assert(_opt.diff.competitor_speed <= 4);
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    42
	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
    43
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    44
	/* 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
    45
	if (!_networking || _network_server) {
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    46
		/* Check if we want to run AIs (server or SP only) */
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3950
diff changeset
    47
		const Player* p;
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
		FOR_ALL_PLAYERS(p) {
2767
3282c77ffc27 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
    50
			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
    51
				/* 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
    52
				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
    53
				_current_player = p->index;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    54
				AI_RunTick(p->index);
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
		}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    57
	}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    58
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    59
	_current_player = OWNER_NONE;
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
 * 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
    64
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
    65
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
    66
{
4850
b4e9be22945f (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4848
diff changeset
    67
	assert(IsValidPlayer(player));
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    68
	if (!_ai_enabled) return;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    69
	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
    70
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    71
	if (_forced_ai_name == NULL) {
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    72
		_ai_player[player] = AIFactoryBase::SelectRandomAI();
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    73
	} else {
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    74
		_ai_player[player] = AIFactoryBase::SelectAI(_forced_ai_name);
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    75
	}
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents: 9383
diff changeset
    76
	if (_ai_player[player] == NULL) {
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    77
		if (_forced_ai_name == NULL) {
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    78
			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
    79
		} else {
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
    80
			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
    81
		}
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents: 9383
diff changeset
    82
		/* 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
    83
		return;
9383
817b07079052 (svn r9173) [NoAI] -Codechange: start both AIs when the game starts more than one.
rubidium
parents: 9365
diff changeset
    84
	}
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
    85
	_current_player = player;
9454
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9444
diff changeset
    86
	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
    87
	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
    88
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    89
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    90
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    91
 * 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
    92
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
    93
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
    94
{
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    95
	if (!_ai_enabled) return;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
    96
9643
413e30aed44e (svn r10535) [NoAI] -Fix: set _current_player also for dying AI (tnx Rubidium for the help!!)
truelight
parents: 9621
diff changeset
    97
	_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
    98
	AI_StopPlayer(player);
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    99
	/* 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
   100
	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
   101
	_ai_player[player] = NULL;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   102
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   103
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   104
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   105
 * Initialize some AI-related stuff.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   106
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5860
diff changeset
   107
void AI_Initialize()
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   108
{
2706
d31bd0aa0096 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   109
	/* 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
   110
	AI_Uninitialize();
d31bd0aa0096 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   111
2767
3282c77ffc27 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   112
	memset(&_ai_player, 0, sizeof(_ai_player));
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   113
	_ai_frame_counter = 0;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   114
	_ai_enabled = true;
9400
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9390
diff changeset
   115
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9390
diff changeset
   116
	AIFactoryBase::RunInitializers();
2395
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
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   119
/**
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   120
 * 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
   121
 * 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
   122
 * @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
   123
 */
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   124
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
   125
{
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   126
	free(_forced_ai_name);
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   127
	_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
   128
}
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   129
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   130
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9454
diff changeset
   131
/**
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   132
 * Deinitializer for AI-related stuff.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   133
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5860
diff changeset
   134
void AI_Uninitialize()
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   135
{
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
   136
	const Player* p;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   137
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   138
	FOR_ALL_PLAYERS(p) {
2767
3282c77ffc27 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   139
		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
   140
	}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   141
}
9360
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   142
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   143
/**
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   144
 * 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
   145
 * 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
   146
 * @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
   147
 */
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   148
bool AI_AllowNewAI()
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   149
{
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   150
	/* If disabled, no AI */
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   151
	if (!_ai_enabled) return false;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   152
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   153
	/* 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
   154
	if (_networking && !_network_server) return false;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   155
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   156
	/* If in network, and server, possible AI */
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   157
	if (_networking && _network_server) {
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   158
		/* Do we want AIs in multiplayer? */
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   159
		if (!_patches.ai_in_multiplayer) return false;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   160
	}
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   161
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   162
	return true;
c20d0a9e0a5c (svn r9142) [NoAI] -Add: added initial code for AI-layer system
truelight
parents: 9359
diff changeset
   163
}
9621
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   164
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   165
#else /* NO_THREADS */
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   166
/* Stub functions for when we do not have threads. */
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   167
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
   168
void AI_PlayerDied(PlayerID player) {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   169
void AI_RunGameLoop() {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   170
void AI_Initialize() {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   171
void AI_Uninitialize() {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   172
bool AI_AllowNewAI() { return false; }
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   173
void AI_ForceAI(const char *forced_ai) {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   174
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2) {}
7654501cf02d (svn r9821) [NoAI] -Fix: support compilation without threads.
rubidium
parents: 9467
diff changeset
   175
#endif /* NO_THREADS */