ai/ai.c
author tron
Mon, 14 Nov 2005 19:48:04 +0000
changeset 2639 eeaefdabfdfd
parent 2551 00e02ecf5835
child 2682 7fa4b202b9f0
permissions -rw-r--r--
(svn r3181) -Bracing
-Indentation
-Whitespace
-DeMorgan's Law
-Test with NULL or 0 for non-booleans
-'\0' instead of 0 for chars
-Remove redundantly redundant comments (like DoFoo(); // Do foo)
-Join multiple short lines with a single statement
-Split single lines with multiple statements
-Avoid assignments in if
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"
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    10
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
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    14
void AI_DequeueCommands(byte player)
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 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    34
		memcpy(_decode_parameters, com->dp, sizeof(com->dp));
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;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    39
		free(com);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    40
	}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    41
}
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
 * 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
    45
 *  will make infinite loops (AIScript).
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    46
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    47
void AI_PutCommandInQueue(byte player, uint tile, uint32 p1, uint32 p2, uint procc)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    48
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    49
	AICommand *com;
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
	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
    52
		/* 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
    53
		_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
    54
		_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
    55
	} else {
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    56
		/* 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
    57
		_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
    58
		_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
    59
	}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    60
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    61
	/* This is our new item */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    62
	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
    63
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    64
	/* Assign the info */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    65
	com->tile  = tile;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    66
	com->p1    = p1;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    67
	com->p2    = p2;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    68
	com->procc = procc;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    69
	com->next  = NULL;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    70
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    71
	/* Copy the decode_parameters */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    72
	memcpy(com->dp, _decode_parameters, sizeof(com->dp));
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    73
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    74
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    75
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    76
 * 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
    77
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    78
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
    79
{
2551
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
    80
	PlayerID old_lp;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    81
	int32 res = 0;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    82
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    83
	/* 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
    84
	 *   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
    85
	 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    86
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    87
	/* First, do a test-run to see if we can do this */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    88
	res = DoCommandByTile(tile, p1, p2, flags & ~DC_EXEC, procc);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    89
	/* The command failed, or you didn't want to execute, or you are quering, return */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    90
	if ((res & CMD_ERROR) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST))
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    91
		return res;
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
	/* 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
    94
	    over the network */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    95
	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
    96
	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
    97
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
    98
	/* 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
    99
	    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
   100
	old_lp = _local_player;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   101
	_local_player = _current_player;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   102
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   103
	/* Send the command */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   104
	if (_networking)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   105
		/* 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
   106
		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
   107
	else
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   108
		/* 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
   109
		 *  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
   110
		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
   111
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   112
	/* Set _local_player back */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   113
	_local_player = old_lp;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   114
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   115
	return res;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   116
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   117
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   118
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   119
 * 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
   120
 */
2551
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   121
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
   122
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   123
	extern void AiNewDoGameLoop(Player *p);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   124
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   125
	Player *p = GetPlayer(player);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   126
	_current_player = player;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   127
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   128
	if (_patches.ainew_active) {
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   129
		AiNewDoGameLoop(p);
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   130
	} else {
2422
897a01f7c624 (svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents: 2395
diff changeset
   131
		/* Enable all kind of cheats the old AI needs in order to operate correctly... */
897a01f7c624 (svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents: 2395
diff changeset
   132
		_is_old_ai_player = true;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   133
		AiDoGameLoop(p);
2422
897a01f7c624 (svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents: 2395
diff changeset
   134
		_is_old_ai_player = false;
897a01f7c624 (svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents: 2395
diff changeset
   135
	}
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   136
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   137
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   138
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   139
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   140
 * The gameloop for AIs.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   141
 *  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
   142
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   143
void AI_RunGameLoop(void)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   144
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   145
	/* Don't do anything if ai is disabled */
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   146
	if (!_ai.enabled) return;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   147
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   148
	/* New tick */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   149
	_ai.tick++;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   150
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   151
	/* Make sure the AI follows the difficulty rule.. */
2446
92efeca5839c (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   152
	assert(_opt.diff.competitor_speed <= 4);
92efeca5839c (svn r2972) Fix the speed of the AI
tron
parents: 2422
diff changeset
   153
	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
   154
		return;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   155
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   156
	/* Check for AI-client (so joining a network with an AI) */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   157
	if (_ai.network_client) {
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   158
		/* Run the script */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   159
		AI_DequeueCommands(_ai.network_player);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   160
		AI_RunTick(_ai.network_player);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   161
	} else if (!_networking || _network_server) {
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   162
		/* 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
   163
		Player *p;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   164
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   165
		FOR_ALL_PLAYERS(p) {
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   166
			if (p->is_active && p->is_ai) {
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   167
				/* This should always be true, else something went wrong... */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   168
				assert(_ai_player[p->index].active);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   169
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   170
				/* Run the script */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   171
				AI_DequeueCommands(p->index);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   172
				AI_RunTick(p->index);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   173
			}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   174
		}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   175
	}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   176
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   177
	_current_player = OWNER_NONE;
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   178
}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   179
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   180
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   181
 * 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
   182
 */
2551
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   183
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
   184
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   185
	/* 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
   186
	_ai_player[player].active = true;
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
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   189
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   190
 * 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
   191
 */
2551
00e02ecf5835 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2548
diff changeset
   192
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
   193
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   194
	/* Called if this AI died */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   195
	_ai_player[player].active = false;
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
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   198
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   199
 * Initialize some AI-related stuff.
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   200
 */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   201
void AI_Initialize(void)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   202
{
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   203
	memset(&_ai, 0, sizeof(_ai));
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   204
	memset(&_ai_player, 0, sizeof(_ai_player));
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
	_ai.enabled = true;
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
/**
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   210
 * Deinitializer for AI-related stuff.
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
void AI_Uninitialize(void)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   213
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   214
	Player* p;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   215
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   216
	FOR_ALL_PLAYERS(p) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2551
diff changeset
   217
		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
   218
	}
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents:
diff changeset
   219
}