ai/ai.c
author tron
Mon, 10 Apr 2006 07:15:58 +0000
changeset 3491 35d747bb5e82
parent 2817 cdf488223c23
child 3692 1d8f5041d437
permissions -rw-r--r--
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
Remove DoCommandByTile(), because now it does the same as DoCommand()
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     1
/* $Id$ */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     2
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     3
#include "../stdafx.h"
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     4
#include "../openttd.h"
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     5
#include "../variables.h"
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     6
#include "../command.h"
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     7
#include "../network.h"
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
     8
#include "ai.h"
2447
071e4afe707c (svn r2973) Move a function declaration somewhere where it belongs
tron
parents: 2446
diff changeset
     9
#include "default/default.h"
2715
d406c6ed777e (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
    10
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    11
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    12
 * Dequeues commands put in the queue via AI_PutCommandInQueue.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    13
 */
2752
55e04dee346d (svn r3297) Staticise
tron
parents: 2740
diff changeset
    14
static void AI_DequeueCommands(byte player)
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    15
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    16
	AICommand *com, *entry_com;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    17
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    18
	entry_com = _ai_player[player].queue;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    19
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    20
	/* It happens that DoCommandP issues a new DoCommandAI which adds a new command
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    21
	 *  to this very same queue (don't argue about this, if it currently doesn't
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    22
	 *  happen I can tell you it will happen with AIScript -- TrueLight). If we
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    23
	 *  do not make the queue NULL, that commands will be dequeued immediatly.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    24
	 *  Therefor we safe the entry-point to entry_com, and make the queue NULL, so
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    25
	 *  the new queue can be safely built up. */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    26
	_ai_player[player].queue = NULL;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    27
	_ai_player[player].queue_tail = NULL;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    28
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    29
	/* Dequeue all commands */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    30
	while ((com = entry_com) != NULL) {
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    31
		_current_player = player;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    32
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    33
		/* Copy the DP back in place */
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    34
		_cmd_text = com->text;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    35
		DoCommandP(com->tile, com->p1, com->p2, NULL, com->procc);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    36
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    37
		/* Free item */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    38
		entry_com = com->next;
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    39
		if (com->text != NULL)
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    40
			free(com->text);
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    41
		free(com);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    42
	}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    43
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    44
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    45
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    46
 * Needed for SP; we need to delay DoCommand with 1 tick, because else events
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    47
 *  will make infinite loops (AIScript).
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    48
 */
2817
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2767
diff changeset
    49
static void AI_PutCommandInQueue(byte player, uint tile, uint32 p1, uint32 p2, uint procc)
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    50
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    51
	AICommand *com;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    52
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    53
	if (_ai_player[player].queue_tail == NULL) {
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    54
		/* There is no item in the queue yet, create the queue */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    55
		_ai_player[player].queue = malloc(sizeof(AICommand));
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    56
		_ai_player[player].queue_tail = _ai_player[player].queue;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    57
	} else {
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    58
		/* Add an item at the end */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    59
		_ai_player[player].queue_tail->next = malloc(sizeof(AICommand));
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    60
		_ai_player[player].queue_tail = _ai_player[player].queue_tail->next;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    61
	}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    62
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    63
	/* This is our new item */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    64
	com = _ai_player[player].queue_tail;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    65
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    66
	/* Assign the info */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    67
	com->tile  = tile;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    68
	com->p1    = p1;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    69
	com->p2    = p2;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    70
	com->procc = procc;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    71
	com->next  = NULL;
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    72
	com->text  = NULL;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    73
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    74
	/* Copy the cmd_text, if needed */
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    75
	if (_cmd_text != NULL) {
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    76
		com->text = strdup(_cmd_text);
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    77
		_cmd_text = NULL;
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    78
	}
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    79
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    80
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    81
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    82
 * Executes a raw DoCommand for the AI.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    83
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    84
int32 AI_DoCommand(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    85
{
2551
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
    86
	PlayerID old_lp;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    87
	int32 res = 0;
2740
4f48b7ad85ba (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
    88
	char *tmp_cmdtext = NULL;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    89
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    90
	/* If you enable DC_EXEC with DC_QUERY_COST you are a really strange
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    91
	 *   person.. should we check for those funny jokes?
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    92
	 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    93
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    94
	/* The test already free _cmd_text in most cases, so let's backup the string, else we have a problem ;) */
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    95
	if (_cmd_text != NULL)
2740
4f48b7ad85ba (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
    96
		tmp_cmdtext = strdup(_cmd_text);
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
    97
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    98
	/* First, do a test-run to see if we can do this */
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 2817
diff changeset
    99
	res = DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc);
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   100
	/* The command failed, or you didn't want to execute, or you are quering, return */
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   101
	if ((CmdFailed(res)) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) {
2740
4f48b7ad85ba (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   102
		if (tmp_cmdtext != NULL)
4f48b7ad85ba (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   103
			free(tmp_cmdtext);
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   104
		return res;
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   105
	}
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   106
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   107
	/* Recover _cmd_text */
2740
4f48b7ad85ba (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   108
	if (tmp_cmdtext != NULL)
4f48b7ad85ba (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   109
		_cmd_text = tmp_cmdtext;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   110
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   111
	/* If we did a DC_EXEC, and the command did not return an error, execute it
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   112
	    over the network */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   113
	if (flags & DC_AUTO)                  procc |= CMD_AUTO;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   114
	if (flags & DC_NO_WATER)              procc |= CMD_NO_WATER;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   115
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   116
	/* NetworkSend_Command needs _local_player to be set correctly, so
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   117
	    adjust it, and put it back right after the function */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   118
	old_lp = _local_player;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   119
	_local_player = _current_player;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   120
2727
90beb642e8d4 (svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents: 2724
diff changeset
   121
#ifdef ENABLE_NETWORK
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   122
	/* Send the command */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   123
	if (_networking)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   124
		/* Network is easy, send it to his handler */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   125
		NetworkSend_Command(tile, p1, p2, procc, NULL);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   126
	else
2727
90beb642e8d4 (svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents: 2724
diff changeset
   127
#endif
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   128
		/* If we execute BuildCommands directly in SP, we have a big problem with events
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   129
		 *  so we need to delay is for 1 tick */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   130
		AI_PutCommandInQueue(_current_player, tile, p1, p2, procc);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   131
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   132
	/* Set _local_player back */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   133
	_local_player = old_lp;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   134
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   135
	/* Free the temp _cmd_text var */
2740
4f48b7ad85ba (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   136
	if (tmp_cmdtext != NULL)
4f48b7ad85ba (svn r3285) -Codechange: Tron was confused by a name, so let's make him happy,
truelight
parents: 2739
diff changeset
   137
		free(tmp_cmdtext);
2738
01590c341f54 (svn r3283) -Fix: decode_parameters was still used, while _cmd_text is the way
truelight
parents: 2734
diff changeset
   138
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   139
	return res;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   140
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   141
2759
9a5079782c67 (svn r3304) -Add: allow AI-events to see the UID of the command
truelight
parents: 2752
diff changeset
   142
/**
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   143
 * Run 1 tick of the AI. Don't overdo it, keep it realistic.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   144
 */
2551
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   145
static void AI_RunTick(PlayerID player)
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   146
{
2767
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   147
	extern void AiNewDoGameLoop(Player *p);
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   148
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   149
	Player *p = GetPlayer(player);
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   150
	_current_player = player;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   151
2767
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   152
	if (_patches.ainew_active) {
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   153
		AiNewDoGameLoop(p);
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   154
	} else {
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   155
		/* Enable all kind of cheats the old AI needs in order to operate correctly... */
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   156
		_is_old_ai_player = true;
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   157
		AiDoGameLoop(p);
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   158
		_is_old_ai_player = false;
2422
897a01f7c624 (svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents: 2395
diff changeset
   159
	}
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   160
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   161
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   162
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   163
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   164
 * The gameloop for AIs.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   165
 *  Handles one tick for all the AIs.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   166
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   167
void AI_RunGameLoop(void)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   168
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   169
	/* Don't do anything if ai is disabled */
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   170
	if (!_ai.enabled) return;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   171
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   172
	/* Don't do anything if we are a network-client
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   173
	 *  (too bad when a client joins, he thinks the AIs are real, so it wants to control
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   174
	 *   them.. this avoids that, while loading a network game in singleplayer, does make
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   175
	 *   the AIs to continue ;))
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   176
	 */
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   177
	if (_networking && !_network_server && !_ai.network_client)
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   178
		return;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   179
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   180
	/* New tick */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   181
	_ai.tick++;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   182
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   183
	/* Make sure the AI follows the difficulty rule.. */
2446
92efeca5839c (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   184
	assert(_opt.diff.competitor_speed <= 4);
92efeca5839c (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   185
	if ((_ai.tick & ((1 << (4 - _opt.diff.competitor_speed)) - 1)) != 0)
92efeca5839c (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   186
		return;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   187
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   188
	/* Check for AI-client (so joining a network with an AI) */
2684
4df784e4be66 (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   189
	if (_ai.network_client && _ai_player[_ai.network_playas].active) {
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   190
		/* Run the script */
2684
4df784e4be66 (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   191
		AI_DequeueCommands(_ai.network_playas);
4df784e4be66 (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   192
		AI_RunTick(_ai.network_playas);
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   193
	} else if (!_networking || _network_server) {
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   194
		/* Check if we want to run AIs (server or SP only) */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   195
		Player *p;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   196
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   197
		FOR_ALL_PLAYERS(p) {
2767
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   198
			if (p->is_active && p->is_ai) {
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   199
				/* This should always be true, else something went wrong... */
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   200
				assert(_ai_player[p->index].active);
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   201
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   202
				/* Run the script */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   203
				AI_DequeueCommands(p->index);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   204
				AI_RunTick(p->index);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   205
			}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   206
		}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   207
	}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   208
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   209
	_current_player = OWNER_NONE;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   210
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   211
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   212
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   213
 * A new AI sees the day of light. You can do here what ever you think is needed.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   214
 */
2551
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   215
void AI_StartNewAI(PlayerID player)
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   216
{
2702
9172e3030a35 (svn r3246) -Fix: small glitch in ai_network_client code (network_client.c)
truelight
parents: 2701
diff changeset
   217
	assert(player < MAX_PLAYERS);
9172e3030a35 (svn r3246) -Fix: small glitch in ai_network_client code (network_client.c)
truelight
parents: 2701
diff changeset
   218
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   219
	/* Called if a new AI is booted */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   220
	_ai_player[player].active = true;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   221
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   222
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   223
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   224
 * This AI player died. Give it some chance to make a final puf.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   225
 */
2551
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   226
void AI_PlayerDied(PlayerID player)
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   227
{
2684
4df784e4be66 (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   228
	if (_ai.network_client && _ai.network_playas == player)
4df784e4be66 (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   229
		_ai.network_playas = OWNER_SPECTATOR;
4df784e4be66 (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   230
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   231
	/* Called if this AI died */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   232
	_ai_player[player].active = false;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   233
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   234
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   235
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   236
 * Initialize some AI-related stuff.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   237
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   238
void AI_Initialize(void)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   239
{
2767
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   240
	bool ai_network_client = _ai.network_client;
2684
4df784e4be66 (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   241
2706
fc872b9b17a7 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   242
	/* First, make sure all AIs are DEAD! */
fc872b9b17a7 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   243
	AI_Uninitialize();
fc872b9b17a7 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   244
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   245
	memset(&_ai, 0, sizeof(_ai));
2767
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   246
	memset(&_ai_player, 0, sizeof(_ai_player));
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   247
2767
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   248
	_ai.network_client = ai_network_client;
2684
4df784e4be66 (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   249
	_ai.network_playas = OWNER_SPECTATOR;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   250
	_ai.enabled = true;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   251
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   252
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   253
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   254
 * Deinitializer for AI-related stuff.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   255
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   256
void AI_Uninitialize(void)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   257
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   258
	Player* p;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   259
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   260
	FOR_ALL_PLAYERS(p) {
2767
25fef06bab87 (svn r3313) Remove GPMI related changes from trunk
tron
parents: 2761
diff changeset
   261
		if (p->is_active && p->is_ai) AI_PlayerDied(p->index);
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   262
	}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   263
}