ai/ai.c
author truelight
Sat, 10 Dec 2005 18:50:07 +0000
changeset 2740 073f2c37934d
parent 2739 73bbb2b02554
child 2752 b5fe5a7e6282
permissions -rw-r--r--
(svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
prefixed it with 'tmp', so now we know it is a temp char* :)
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     1
/* $Id$ */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     2
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     3
#include "../stdafx.h"
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     4
#include "../openttd.h"
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     5
#include "../variables.h"
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     6
#include "../command.h"
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     7
#include "../network.h"
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
     8
#include "../debug.h"
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     9
#include "ai.h"
2447
47f2b670fb22 (svn r2973) Move a function declaration somewhere where it belongs
tron
parents: 2446
diff changeset
    10
#include "default/default.h"
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
    11
#include "../string.h"
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    12
2690
2f810b43cb7f (svn r3232) -Add: implemented the event-system for AIs
truelight
parents: 2687
diff changeset
    13
/* Here we define the events */
2f810b43cb7f (svn r3232) -Add: implemented the event-system for AIs
truelight
parents: 2687
diff changeset
    14
#define DEF_EVENTS
2f810b43cb7f (svn r3232) -Add: implemented the event-system for AIs
truelight
parents: 2687
diff changeset
    15
#include "ai_event.h"
2f810b43cb7f (svn r3232) -Add: implemented the event-system for AIs
truelight
parents: 2687
diff changeset
    16
#undef DEF_EVENTS
2f810b43cb7f (svn r3232) -Add: implemented the event-system for AIs
truelight
parents: 2687
diff changeset
    17
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
    18
/* Here we keep track of all commands that are called via AI_DoCommandChecked,
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
    19
 *  in special we save the unique_id here. Now this id is given back
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
    20
 *  when the command fails or succeeds and is detected as added in this storage. */
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
    21
static AICommand *command_uid[MAX_PLAYERS];
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
    22
static AICommand *command_uid_tail[MAX_PLAYERS];
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
    23
static uint uids[MAX_PLAYERS];
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
 * Dequeues commands put in the queue via AI_PutCommandInQueue.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    27
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    28
void AI_DequeueCommands(byte player)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    29
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    30
	AICommand *com, *entry_com;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    31
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    32
	entry_com = _ai_player[player].queue;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    33
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    34
	/* It happens that DoCommandP issues a new DoCommandAI which adds a new command
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    35
	 *  to this very same queue (don't argue about this, if it currently doesn't
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    36
	 *  happen I can tell you it will happen with AIScript -- TrueLight). If we
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    37
	 *  do not make the queue NULL, that commands will be dequeued immediatly.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    38
	 *  Therefor we safe the entry-point to entry_com, and make the queue NULL, so
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    39
	 *  the new queue can be safely built up. */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    40
	_ai_player[player].queue = NULL;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    41
	_ai_player[player].queue_tail = NULL;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    42
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    43
	/* Dequeue all commands */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    44
	while ((com = entry_com) != NULL) {
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    45
		_current_player = player;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    46
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    47
		/* Copy the DP back in place */
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    48
		_cmd_text = com->text;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    49
		DoCommandP(com->tile, com->p1, com->p2, NULL, com->procc);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    50
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    51
		/* Free item */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    52
		entry_com = com->next;
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    53
		if (com->text != NULL)
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    54
			free(com->text);
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    55
		free(com);
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
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    60
 * Needed for SP; we need to delay DoCommand with 1 tick, because else events
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    61
 *  will make infinite loops (AIScript).
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
void AI_PutCommandInQueue(byte player, uint tile, uint32 p1, uint32 p2, uint procc)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    64
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    65
	AICommand *com;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    66
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    67
	if (_ai_player[player].queue_tail == NULL) {
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    68
		/* There is no item in the queue yet, create the queue */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    69
		_ai_player[player].queue = malloc(sizeof(AICommand));
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    70
		_ai_player[player].queue_tail = _ai_player[player].queue;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    71
	} else {
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    72
		/* Add an item at the end */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    73
		_ai_player[player].queue_tail->next = malloc(sizeof(AICommand));
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    74
		_ai_player[player].queue_tail = _ai_player[player].queue_tail->next;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    75
	}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    76
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    77
	/* This is our new item */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    78
	com = _ai_player[player].queue_tail;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    79
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    80
	/* Assign the info */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    81
	com->tile  = tile;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    82
	com->p1    = p1;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    83
	com->p2    = p2;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    84
	com->procc = procc;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    85
	com->next  = NULL;
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    86
	com->text  = NULL;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    87
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    88
	/* Copy the cmd_text, if needed */
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    89
	if (_cmd_text != NULL) {
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    90
		com->text = strdup(_cmd_text);
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    91
		_cmd_text = NULL;
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    92
	}
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    93
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    94
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    95
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    96
 * Executes a raw DoCommand for the AI.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    97
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    98
int32 AI_DoCommand(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    99
{
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   100
	PlayerID old_lp;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   101
	int32 res = 0;
2740
073f2c37934d (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   102
	char *tmp_cmdtext = NULL;
2395
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
	/* If you enable DC_EXEC with DC_QUERY_COST you are a really strange
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   105
	 *   person.. should we check for those funny jokes?
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   106
	 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   107
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   108
	/* The test already free _cmd_text in most cases, so let's backup the string, else we have a problem ;) */
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   109
	if (_cmd_text != NULL)
2740
073f2c37934d (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   110
		tmp_cmdtext = strdup(_cmd_text);
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   111
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   112
	/* First, do a test-run to see if we can do this */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   113
	res = DoCommandByTile(tile, p1, p2, flags & ~DC_EXEC, procc);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   114
	/* The command failed, or you didn't want to execute, or you are quering, return */
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   115
	if ((CmdFailed(res)) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) {
2740
073f2c37934d (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   116
		if (tmp_cmdtext != NULL)
073f2c37934d (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   117
			free(tmp_cmdtext);
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   118
		return res;
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   119
	}
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   120
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   121
	/* Recover _cmd_text */
2740
073f2c37934d (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   122
	if (tmp_cmdtext != NULL)
073f2c37934d (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   123
		_cmd_text = tmp_cmdtext;
2395
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
	/* If we did a DC_EXEC, and the command did not return an error, execute it
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   126
	    over the network */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   127
	if (flags & DC_AUTO)                  procc |= CMD_AUTO;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   128
	if (flags & DC_NO_WATER)              procc |= CMD_NO_WATER;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   129
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   130
	/* NetworkSend_Command needs _local_player to be set correctly, so
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   131
	    adjust it, and put it back right after the function */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   132
	old_lp = _local_player;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   133
	_local_player = _current_player;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   134
2727
6c726ecbc31c (svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents: 2724
diff changeset
   135
#ifdef ENABLE_NETWORK
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   136
	/* Send the command */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   137
	if (_networking)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   138
		/* Network is easy, send it to his handler */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   139
		NetworkSend_Command(tile, p1, p2, procc, NULL);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   140
	else
2727
6c726ecbc31c (svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents: 2724
diff changeset
   141
#endif
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   142
		/* If we execute BuildCommands directly in SP, we have a big problem with events
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   143
		 *  so we need to delay is for 1 tick */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   144
		AI_PutCommandInQueue(_current_player, tile, p1, p2, procc);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   145
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   146
	/* Set _local_player back */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   147
	_local_player = old_lp;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   148
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   149
	/* Free the temp _cmd_text var */
2740
073f2c37934d (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   150
	if (tmp_cmdtext != NULL)
073f2c37934d (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   151
		free(tmp_cmdtext);
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   152
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   153
	return res;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   154
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   155
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   156
int32 AI_DoCommandChecked(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   157
{
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   158
	AICommand *new;
2734
a2b52101be1c (svn r3279) -Fix: do not try to execute something that is not set to execute
truelight
parents: 2733
diff changeset
   159
	uint unique_id = uids[_current_player];
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   160
	int32 res;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   161
2732
ceabbda7e642 (svn r3277) -Fix: lets use the right DoCommand for testing a build (tnx to Igor2Code)
truelight
parents: 2727
diff changeset
   162
	res = DoCommandByTile(tile, p1, p2, flags & ~DC_EXEC, procc);
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   163
	if (CmdFailed(res))
2724
1b97ad37691e (svn r3269) -Fix: return CMD_ERROR instead of -1 if AI_DoCommandChecked fails
truelight
parents: 2715
diff changeset
   164
		return CMD_ERROR;
2734
a2b52101be1c (svn r3279) -Fix: do not try to execute something that is not set to execute
truelight
parents: 2733
diff changeset
   165
	if (!(flags & DC_EXEC))
a2b52101be1c (svn r3279) -Fix: do not try to execute something that is not set to execute
truelight
parents: 2733
diff changeset
   166
		return res;
a2b52101be1c (svn r3279) -Fix: do not try to execute something that is not set to execute
truelight
parents: 2733
diff changeset
   167
a2b52101be1c (svn r3279) -Fix: do not try to execute something that is not set to execute
truelight
parents: 2733
diff changeset
   168
	uids[_current_player]++;
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   169
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   170
	/* Save the command and his things, together with the unique_id */
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   171
	new = malloc(sizeof(AICommand));
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   172
	new->tile  = tile;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   173
	new->p1    = p1;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   174
	new->p2    = p2;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   175
	new->procc = procc;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   176
	new->next  = NULL;
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   177
	new->uid   = unique_id;
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   178
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   179
	/* Add it to the back of the list */
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   180
	if (command_uid_tail[_current_player] == NULL)
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   181
		command_uid_tail[_current_player] = new;
2733
d8f39e8819a3 (svn r3278) -Fix: lets update the tail-pointer if we add a new item, else the linkedlist never grows ;)
truelight
parents: 2732
diff changeset
   182
	else {
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   183
		command_uid_tail[_current_player]->next = new;
2733
d8f39e8819a3 (svn r3278) -Fix: lets update the tail-pointer if we add a new item, else the linkedlist never grows ;)
truelight
parents: 2732
diff changeset
   184
		command_uid_tail[_current_player] = new;
d8f39e8819a3 (svn r3278) -Fix: lets update the tail-pointer if we add a new item, else the linkedlist never grows ;)
truelight
parents: 2732
diff changeset
   185
	}
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   186
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   187
	if (command_uid[_current_player] == NULL)
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   188
		command_uid[_current_player] = new;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   189
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   190
	AI_DoCommand(tile, p1, p2, flags, procc);
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   191
	return unique_id;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   192
}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   193
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   194
/**
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   195
 * A command is executed for real, and is giving us his result (failed yes/no). Inform the AI with it via
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   196
 *  an event.Z
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   197
 */
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   198
void AI_CommandResult(uint32 cmd, uint32 p1, uint32 p2, TileIndex tile, bool succeeded)
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   199
{
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   200
	AICommand *command = command_uid[_current_player];
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   201
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   202
	if (command == NULL)
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   203
		return;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   204
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   205
	if (command->procc != (cmd & 0xFF) || command->p1 != p1 || command->p2 != p2 || command->tile != tile) {
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   206
		/* If we come here, we see a command that isn't at top in our list. This is okay, if the command isn't
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   207
		 *  anywhere else in our list, else we have a big problem.. so check for that. If it isn't in our list,
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   208
		 *  it is okay, else, drop the game.
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   209
		 * Why do we have a big problem in the case it is in our list? Simply, we have a command sooner in
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   210
		 *  our list that wasn't triggered to be failed or succeeded, so it is sitting there without an
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   211
		 *  event, so the script will never know if it succeeded yes/no, so it can hang.. this is VERY bad
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   212
		 *  and should never ever happen. */
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   213
		while (command != NULL && (command->procc != (cmd & 0xFF) || command->p1 != p1 || command->p2 != p2 || command->tile != tile)) {
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   214
			command = command->next;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   215
		}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   216
		assert(command == NULL);
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   217
		return;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   218
	}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   219
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   220
	command_uid[_current_player] = command_uid[_current_player]->next;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   221
	if (command_uid[_current_player] == NULL)
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   222
		command_uid_tail[_current_player] = NULL;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   223
2738
d8de2d4bee17 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   224
	ai_event(_current_player, succeeded ? ottd_Event_CommandSucceeded : ottd_Event_CommandFailed, tile, command->uid);
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   225
	free(command);
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   226
}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   227
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   228
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   229
 * Run 1 tick of the AI. Don't overdo it, keep it realistic.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   230
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   231
static void AI_RunTick(PlayerID player)
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   232
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   233
	_current_player = player;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   234
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   235
#ifdef GPMI
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   236
	if (_ai.gpmi) {
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   237
		gpmi_call_RunTick(_ai_player[player].module, _frame_counter);
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   238
		return;
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   239
	}
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   240
#endif /* GPMI */
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   241
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   242
	{
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   243
		extern void AiNewDoGameLoop(Player *p);
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   244
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   245
		Player *p = GetPlayer(player);
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   246
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   247
		if (_patches.ainew_active) {
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   248
			AiNewDoGameLoop(p);
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   249
		} else {
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   250
			/* Enable all kind of cheats the old AI needs in order to operate correctly... */
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   251
			_is_old_ai_player = true;
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   252
			AiDoGameLoop(p);
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   253
			_is_old_ai_player = false;
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   254
		}
2422
914a12dee832 (svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents: 2395
diff changeset
   255
	}
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   256
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   257
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   258
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   259
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   260
 * The gameloop for AIs.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   261
 *  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
   262
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   263
void AI_RunGameLoop(void)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   264
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   265
	/* Don't do anything if ai is disabled */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   266
	if (!_ai.enabled) return;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   267
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   268
	/* Don't do anything if we are a network-client
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   269
	 *  (too bad when a client joins, he thinks the AIs are real, so it wants to control
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   270
	 *   them.. this avoids that, while loading a network game in singleplayer, does make
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   271
	 *   the AIs to continue ;))
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   272
	 */
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   273
	if (_networking && !_network_server && !_ai.network_client)
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   274
		return;
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   275
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   276
	/* New tick */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   277
	_ai.tick++;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   278
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   279
	/* Make sure the AI follows the difficulty rule.. */
2446
df659fbe7e4f (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   280
	assert(_opt.diff.competitor_speed <= 4);
df659fbe7e4f (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   281
	if ((_ai.tick & ((1 << (4 - _opt.diff.competitor_speed)) - 1)) != 0)
df659fbe7e4f (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   282
		return;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   283
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   284
	/* Check for AI-client (so joining a network with an AI) */
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   285
	if (_ai.network_client && _ai_player[_ai.network_playas].active) {
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   286
		/* Run the script */
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   287
		AI_DequeueCommands(_ai.network_playas);
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   288
		AI_RunTick(_ai.network_playas);
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   289
	} else if (!_networking || _network_server) {
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   290
		/* Check if we want to run AIs (server or SP only) */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   291
		Player *p;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   292
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   293
		FOR_ALL_PLAYERS(p) {
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   294
			if (p->is_active && p->is_ai && _ai_player[p->index].active) {
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   295
				/* Run the script */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   296
				AI_DequeueCommands(p->index);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   297
				AI_RunTick(p->index);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   298
			}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   299
		}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   300
	}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   301
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   302
	_current_player = OWNER_NONE;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   303
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   304
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   305
#ifdef GPMI
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   306
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   307
void AI_ShutdownAIControl(bool with_error)
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   308
{
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   309
	if (_ai.gpmi_mod != NULL)
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   310
		gpmi_mod_unload(_ai.gpmi_mod);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   311
	if (_ai.gpmi_pkg != NULL)
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   312
		gpmi_pkg_unload(_ai.gpmi_pkg);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   313
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   314
	if (with_error) {
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   315
		DEBUG(ai, 0)("[AI] Failed to load AI Control, switching back to built-in AIs..");
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   316
		_ai.gpmi = false;
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   317
	}
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   318
}
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   319
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   320
void (*ottd_GetNextAIData)(char **library, char **param);
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   321
void (*ottd_SetAIParam)(char *param);
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   322
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   323
void AI_LoadAIControl(void)
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   324
{
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   325
	/* Load module */
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   326
	_ai.gpmi_mod = gpmi_mod_load("ottd_ai_control_mod", NULL);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   327
	if (_ai.gpmi_mod == NULL) {
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   328
		AI_ShutdownAIControl(true);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   329
		return;
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   330
	}
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   331
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   332
	/* Load package */
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   333
	if (gpmi_pkg_load("ottd_ai_control_pkg", 0, NULL, NULL, &_ai.gpmi_pkg)) {
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   334
		AI_ShutdownAIControl(true);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   335
		return;
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   336
	}
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   337
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   338
	/* Now link all the functions */
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   339
	{
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   340
		ottd_GetNextAIData = gpmi_pkg_resolve(_ai.gpmi_pkg, "ottd_GetNextAIData");
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   341
		ottd_SetAIParam = gpmi_pkg_resolve(_ai.gpmi_pkg, "ottd_SetAIParam");
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   342
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   343
		if (ottd_GetNextAIData == NULL || ottd_SetAIParam == NULL)
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   344
			AI_ShutdownAIControl(true);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   345
	}
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   346
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   347
	ottd_SetAIParam(_ai.gpmi_param);
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   348
}
2707
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   349
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   350
/**
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   351
 * Dump an entry of the GPMI error stack (callback routine). This helps the user to trace errors back to their roots.
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   352
 */
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   353
void AI_PrintErrorStack(gpmi_err_stack_t *entry, char *string)
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   354
{
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   355
	DEBUG(ai, 0)("%s", string);
2707
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   356
}
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   357
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   358
#endif /* GPMI */
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   359
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   360
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   361
 * 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
   362
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   363
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
   364
{
2702
e4663e88c530 (svn r3246) -Fix: small glitch in ai_network_client code (network_client.c)
truelight
parents: 2701
diff changeset
   365
	assert(player < MAX_PLAYERS);
e4663e88c530 (svn r3246) -Fix: small glitch in ai_network_client code (network_client.c)
truelight
parents: 2701
diff changeset
   366
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   367
#ifdef GPMI
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   368
	/* Keep this in a different IF, because the function can turn _ai.gpmi off!! */
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   369
	if (_ai.gpmi && _ai.gpmi_mod == NULL)
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   370
		AI_LoadAIControl();
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   371
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   372
	if (_ai.gpmi) {
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   373
		char *library = NULL;
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   374
		char *params = NULL;
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   375
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   376
		ottd_GetNextAIData(&library, &params);
2707
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   377
		gpmi_error_stack_enable = 1;
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   378
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   379
		if (library != NULL) {
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   380
			_ai_player[player].module = gpmi_mod_load(library, params);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   381
			free(library);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   382
		}
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   383
		if (params != NULL)
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   384
			free(params);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   385
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   386
		if (_ai_player[player].module == NULL) {
2707
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   387
			DEBUG(ai, 0)("[AI] Failed to load AI, aborting. GPMI error stack:");
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   388
			gpmi_err_stack_process_str(AI_PrintErrorStack);
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   389
			return;
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   390
		}
2707
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   391
		gpmi_error_stack_enable = 0;
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   392
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   393
	}
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   394
#endif /* GPMI */
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   395
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   396
	/* Called if a new AI is booted */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   397
	_ai_player[player].active = true;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   398
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   399
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   400
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   401
 * 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
   402
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   403
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
   404
{
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   405
	if (_ai.network_client && _ai.network_playas == player)
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   406
		_ai.network_playas = OWNER_SPECTATOR;
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   407
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   408
	/* Called if this AI died */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   409
	_ai_player[player].active = false;
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   410
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   411
	if (command_uid[player] != NULL) {
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   412
		while (command_uid[player] != NULL) {
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   413
			AICommand *command = command_uid[player];
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   414
			command_uid[player] = command->next;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   415
			free(command);
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   416
		}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   417
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   418
		command_uid[player] = NULL;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   419
		command_uid_tail[player] = NULL;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   420
	}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   421
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   422
#ifdef GPMI
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   423
	if (_ai_player[player].module != NULL)
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   424
		gpmi_mod_unload(_ai_player[player].module);
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   425
#endif /* GPMI */
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   426
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   427
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   428
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   429
 * Initialize some AI-related stuff.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   430
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   431
void AI_Initialize(void)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   432
{
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   433
	bool tmp_ai_network_client = _ai.network_client;
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   434
	bool tmp_ai_gpmi = _ai.gpmi;
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   435
#ifdef GPMI
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   436
	char *tmp_ai_gpmi_param = strdup(_ai.gpmi_param);
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   437
#endif /* GPMI */
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   438
2706
d31bd0aa0096 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   439
	/* 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
   440
	AI_Uninitialize();
d31bd0aa0096 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   441
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   442
	memset(&_ai, 0, sizeof(_ai));
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   443
	memset(_ai_player, 0, sizeof(_ai_player));
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   444
	memset(uids, 0, sizeof(uids));
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   445
	memset(command_uid, 0, sizeof(command_uid));
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   446
	memset(command_uid_tail, 0, sizeof(command_uid_tail));
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   447
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   448
	_ai.network_client = tmp_ai_network_client;
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   449
	_ai.network_playas = OWNER_SPECTATOR;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   450
	_ai.enabled = true;
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   451
	_ai.gpmi = tmp_ai_gpmi;
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   452
#ifdef GPMI
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   453
	ttd_strlcpy(_ai.gpmi_param, tmp_ai_gpmi_param, sizeof(_ai.gpmi_param));
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   454
	free(tmp_ai_gpmi_param);
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   455
#endif /* GPMI */
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   456
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   457
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   458
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   459
 * Deinitializer for AI-related stuff.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   460
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   461
void AI_Uninitialize(void)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   462
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   463
	Player* p;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   464
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   465
	FOR_ALL_PLAYERS(p) {
2687
eb2443a14a74 (svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
truelight
parents: 2684
diff changeset
   466
		if (p->is_active && p->is_ai && _ai_player[p->index].active) AI_PlayerDied(p->index);
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   467
	}
2700
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   468
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   469
#ifdef GPMI
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   470
	AI_ShutdownAIControl(false);
555506b02195 (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot
truelight
parents: 2696
diff changeset
   471
#endif /* GPMI */
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   472
}