ai/ai.c
author truelight
Fri, 09 Dec 2005 17:42:56 +0000
changeset 2732 ceabbda7e642
parent 2727 6c726ecbc31c
child 2733 d8f39e8819a3
permissions -rw-r--r--
(svn r3277) -Fix: lets use the right DoCommand for testing a build (tnx to Igor2Code)
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 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    48
		memcpy(_decode_parameters, com->dp, sizeof(com->dp));
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;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    53
		free(com);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    54
	}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    55
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    56
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    57
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    58
 * 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
    59
 *  will make infinite loops (AIScript).
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    60
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    61
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
    62
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    63
	AICommand *com;
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
	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
    66
		/* 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
    67
		_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
    68
		_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
    69
	} else {
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    70
		/* 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
    71
		_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
    72
		_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
    73
	}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    74
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    75
	/* This is our new item */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    76
	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
    77
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    78
	/* Assign the info */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    79
	com->tile  = tile;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    80
	com->p1    = p1;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    81
	com->p2    = p2;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    82
	com->procc = procc;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    83
	com->next  = NULL;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    84
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    85
	/* Copy the decode_parameters */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    86
	memcpy(com->dp, _decode_parameters, sizeof(com->dp));
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    87
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    88
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    89
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    90
 * 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
    91
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    92
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
    93
{
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
    94
	PlayerID old_lp;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    95
	int32 res = 0;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    96
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    97
	/* 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
    98
	 *   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
    99
	 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   100
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   101
	/* 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
   102
	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
   103
	/* The command failed, or you didn't want to execute, or you are quering, return */
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   104
	if ((CmdFailed(res)) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST))
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   105
		return res;
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
	/* 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
   108
	    over the network */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   109
	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
   110
	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
   111
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   112
	/* 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
   113
	    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
   114
	old_lp = _local_player;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   115
	_local_player = _current_player;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   116
2727
6c726ecbc31c (svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents: 2724
diff changeset
   117
#ifdef ENABLE_NETWORK
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   118
	/* Send the command */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   119
	if (_networking)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   120
		/* 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
   121
		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
   122
	else
2727
6c726ecbc31c (svn r3272) -Fix: [AI] fixed issue in AI that prevented compilation without network support
bjarni
parents: 2724
diff changeset
   123
#endif
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   124
		/* 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
   125
		 *  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
   126
		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
   127
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   128
	/* Set _local_player back */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   129
	_local_player = old_lp;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   130
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   131
	return res;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   132
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   133
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   134
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
   135
{
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   136
	AICommand *new;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   137
	uint unique_id = uids[_current_player]++;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   138
	int32 res;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   139
2732
ceabbda7e642 (svn r3277) -Fix: lets use the right DoCommand for testing a build (tnx to Igor2Code)
truelight
parents: 2727
diff changeset
   140
	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
   141
	if (CmdFailed(res))
2724
1b97ad37691e (svn r3269) -Fix: return CMD_ERROR instead of -1 if AI_DoCommandChecked fails
truelight
parents: 2715
diff changeset
   142
		return CMD_ERROR;
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   143
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   144
	/* 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
   145
	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
   146
	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
   147
	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
   148
	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
   149
	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
   150
	new->next  = NULL;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   151
	new->dp[0] = unique_id;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   152
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   153
	/* 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
   154
	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
   155
		command_uid_tail[_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
   156
	else
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   157
		command_uid_tail[_current_player]->next = new;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   158
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   159
	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
   160
		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
   161
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   162
	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
   163
	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
   164
}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   165
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   166
/**
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   167
 * 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
   168
 *  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
   169
 */
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   170
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
   171
{
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   172
	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
   173
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   174
	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
   175
		return;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   176
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   177
	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
   178
		/* 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
   179
		 *  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
   180
		 *  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
   181
		 * 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
   182
		 *  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
   183
		 *  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
   184
		 *  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
   185
		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
   186
			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
   187
		}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   188
		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
   189
		return;
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   190
	}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   191
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   192
	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
   193
	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
   194
		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
   195
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   196
	ai_event(_current_player, succeeded ? ottd_Event_CommandSucceeded : ottd_Event_CommandFailed, tile, command->dp[0]);
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   197
	free(command);
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   198
}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   199
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   200
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   201
 * 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
   202
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   203
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
   204
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   205
	_current_player = player;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   206
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
   207
#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
   208
	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
   209
		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
   210
		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
   211
	}
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
   212
#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
   213
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
   214
	{
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
   215
		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
   216
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
   217
		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
   218
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
   219
		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
   220
			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
   221
		} 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
   222
			/* 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
   223
			_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
   224
			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
   225
			_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
   226
		}
2422
914a12dee832 (svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents: 2395
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
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   230
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   231
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   232
 * The gameloop for AIs.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   233
 *  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
   234
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   235
void AI_RunGameLoop(void)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   236
{
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   237
	/* Don't do anything if ai is disabled */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   238
	if (!_ai.enabled) return;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   239
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   240
	/* 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
   241
	 *  (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
   242
	 *   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
   243
	 *   the AIs to continue ;))
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   244
	 */
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   245
	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
   246
		return;
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   247
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   248
	/* New tick */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   249
	_ai.tick++;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   250
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   251
	/* Make sure the AI follows the difficulty rule.. */
2446
df659fbe7e4f (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   252
	assert(_opt.diff.competitor_speed <= 4);
df659fbe7e4f (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   253
	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
   254
		return;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   255
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   256
	/* 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
   257
	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
   258
		/* Run the script */
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   259
		AI_DequeueCommands(_ai.network_playas);
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   260
		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
   261
	} else if (!_networking || _network_server) {
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   262
		/* 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
   263
		Player *p;
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
		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
   266
			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
   267
				/* Run the script */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   268
				AI_DequeueCommands(p->index);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   269
				AI_RunTick(p->index);
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   270
			}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   271
		}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   272
	}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   273
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   274
	_current_player = OWNER_NONE;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   275
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   276
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
   277
#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
   278
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
   279
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
   280
{
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
   281
	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
   282
		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
   283
	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
   284
		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
   285
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
   286
	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
   287
		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
   288
		_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
   289
	}
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
   290
}
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
   291
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   292
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
   293
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
   294
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
   295
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
   296
{
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
   297
	/* 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
   298
	_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
   299
	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
   300
		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
   301
		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
   302
	}
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
   303
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
   304
	/* 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
   305
	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
   306
		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
   307
		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
   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
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
	/* 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
   311
	{
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
		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
   313
		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
   314
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   315
		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
   316
			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
   317
	}
2701
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   318
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   319
	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
   320
}
2707
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   321
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   322
/**
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   323
 * 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
   324
 */
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   325
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
   326
{
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   327
	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
   328
}
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   329
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
   330
#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
   331
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   332
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   333
 * 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
   334
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   335
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
   336
{
2702
e4663e88c530 (svn r3246) -Fix: small glitch in ai_network_client code (network_client.c)
truelight
parents: 2701
diff changeset
   337
	assert(player < MAX_PLAYERS);
e4663e88c530 (svn r3246) -Fix: small glitch in ai_network_client code (network_client.c)
truelight
parents: 2701
diff changeset
   338
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
   339
#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
   340
	/* 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
   341
	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
   342
		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
   343
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
	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
   345
		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
   346
		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
   347
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
		ottd_GetNextAIData(&library, &params);
2707
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   349
		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
   350
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
   351
		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
   352
			_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
   353
			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
   354
		}
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
   355
		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
   356
			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
   357
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
		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
   359
			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
   360
			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
   361
			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
   362
		}
2707
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   363
		gpmi_error_stack_enable = 0;
4f3a8819eb6e (svn r3251) -Fix: report errors from GPMI in a more detailed way (Igor2Code)
truelight
parents: 2706
diff changeset
   364
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
   365
	}
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
   366
#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
   367
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   368
	/* 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
   369
	_ai_player[player].active = true;
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   370
}
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   371
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   372
/**
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   373
 * 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
   374
 */
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   375
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
   376
{
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   377
	if (_ai.network_client && _ai.network_playas == player)
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   378
		_ai.network_playas = OWNER_SPECTATOR;
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   379
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   380
	/* Called if this AI died */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   381
	_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
   382
2715
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   383
	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
   384
		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
   385
			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
   386
			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
   387
			free(command);
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   388
		}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   389
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   390
		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
   391
		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
   392
	}
0ad451d9264b (svn r3260) -Add: add events for AIs to check if a command execution failed or succeeded
truelight
parents: 2707
diff changeset
   393
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
   394
#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
   395
	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
   396
		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
   397
#endif /* GPMI */
2395
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
 * Initialize some AI-related stuff.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   402
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   403
void AI_Initialize(void)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   404
{
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
   405
	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
   406
	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
   407
#ifdef GPMI
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   408
	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
   409
#endif /* GPMI */
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   410
2706
d31bd0aa0096 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   411
	/* 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
   412
	AI_Uninitialize();
d31bd0aa0096 (svn r3250) -Fix: AIs weren't uninitialized when a new game was loaded
truelight
parents: 2702
diff changeset
   413
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   414
	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
   415
	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
   416
	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
   417
	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
   418
	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
   419
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
   420
	_ai.network_client = tmp_ai_network_client;
2684
8aba54c245cc (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2682
diff changeset
   421
	_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
   422
	_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
   423
	_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
   424
#ifdef GPMI
a2b9a934044e (svn r3245) -Add: allow OpenTTD to give his GPMI-params to the GPMI modules
truelight
parents: 2700
diff changeset
   425
	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
   426
	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
   427
#endif /* GPMI */
2395
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
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
 * Deinitializer for AI-related stuff.
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   432
 */
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   433
void AI_Uninitialize(void)
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   434
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   435
	Player* p;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   436
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   437
	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
   438
		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
   439
	}
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
   440
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
   441
#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
   442
	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
   443
#endif /* GPMI */
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   444
}