2005-09-07 15:10:11 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../openttd.h"
|
|
|
|
#include "../variables.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "../command_func.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "../network/network.h"
|
2007-12-25 09:48:53 +00:00
|
|
|
#include "../core/alloc_func.hpp"
|
2008-01-12 14:10:35 +00:00
|
|
|
#include "../player_func.h"
|
|
|
|
#include "../player_base.h"
|
2005-09-07 15:10:11 +00:00
|
|
|
#include "ai.h"
|
2005-09-23 06:38:36 +00:00
|
|
|
#include "default/default.h"
|
2008-01-16 01:18:15 +00:00
|
|
|
#include "../signal_func.h"
|
2005-12-05 12:27:58 +00:00
|
|
|
|
2008-01-13 13:36:01 +00:00
|
|
|
AIStruct _ai;
|
|
|
|
AIPlayer _ai_player[MAX_PLAYERS];
|
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
/**
|
|
|
|
* Dequeues commands put in the queue via AI_PutCommandInQueue.
|
|
|
|
*/
|
2006-04-29 14:31:16 +00:00
|
|
|
static void AI_DequeueCommands(PlayerID player)
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
|
|
|
AICommand *com, *entry_com;
|
|
|
|
|
|
|
|
entry_com = _ai_player[player].queue;
|
|
|
|
|
|
|
|
/* It happens that DoCommandP issues a new DoCommandAI which adds a new command
|
|
|
|
* to this very same queue (don't argue about this, if it currently doesn't
|
|
|
|
* happen I can tell you it will happen with AIScript -- TrueLight). If we
|
|
|
|
* do not make the queue NULL, that commands will be dequeued immediatly.
|
|
|
|
* Therefor we safe the entry-point to entry_com, and make the queue NULL, so
|
|
|
|
* the new queue can be safely built up. */
|
|
|
|
_ai_player[player].queue = NULL;
|
|
|
|
_ai_player[player].queue_tail = NULL;
|
|
|
|
|
|
|
|
/* Dequeue all commands */
|
|
|
|
while ((com = entry_com) != NULL) {
|
|
|
|
_current_player = player;
|
|
|
|
|
2005-12-10 18:43:49 +00:00
|
|
|
_cmd_text = com->text;
|
2006-06-04 09:10:24 +00:00
|
|
|
DoCommandP(com->tile, com->p1, com->p2, com->callback, com->procc);
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* Free item */
|
|
|
|
entry_com = com->next;
|
2006-05-21 15:27:10 +00:00
|
|
|
free(com->text);
|
2005-09-07 15:10:11 +00:00
|
|
|
free(com);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Needed for SP; we need to delay DoCommand with 1 tick, because else events
|
|
|
|
* will make infinite loops (AIScript).
|
|
|
|
*/
|
2007-12-21 21:50:46 +00:00
|
|
|
static void AI_PutCommandInQueue(PlayerID player, TileIndex tile, uint32 p1, uint32 p2, uint32 procc, CommandCallback* callback)
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
|
|
|
AICommand *com;
|
|
|
|
|
|
|
|
if (_ai_player[player].queue_tail == NULL) {
|
|
|
|
/* There is no item in the queue yet, create the queue */
|
2007-01-11 17:29:39 +00:00
|
|
|
_ai_player[player].queue = MallocT<AICommand>(1);
|
2005-09-07 15:10:11 +00:00
|
|
|
_ai_player[player].queue_tail = _ai_player[player].queue;
|
|
|
|
} else {
|
|
|
|
/* Add an item at the end */
|
2007-01-11 17:29:39 +00:00
|
|
|
_ai_player[player].queue_tail->next = MallocT<AICommand>(1);
|
2005-09-07 15:10:11 +00:00
|
|
|
_ai_player[player].queue_tail = _ai_player[player].queue_tail->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is our new item */
|
|
|
|
com = _ai_player[player].queue_tail;
|
|
|
|
|
|
|
|
/* Assign the info */
|
|
|
|
com->tile = tile;
|
|
|
|
com->p1 = p1;
|
|
|
|
com->p2 = p2;
|
|
|
|
com->procc = procc;
|
2006-06-04 09:10:24 +00:00
|
|
|
com->callback = callback;
|
2005-09-07 15:10:11 +00:00
|
|
|
com->next = NULL;
|
2005-12-10 18:43:49 +00:00
|
|
|
com->text = NULL;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
2005-12-10 18:43:49 +00:00
|
|
|
/* Copy the cmd_text, if needed */
|
|
|
|
if (_cmd_text != NULL) {
|
|
|
|
com->text = strdup(_cmd_text);
|
|
|
|
_cmd_text = NULL;
|
|
|
|
}
|
2005-09-07 15:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a raw DoCommand for the AI.
|
|
|
|
*/
|
2007-12-21 21:50:46 +00:00
|
|
|
CommandCost AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint32 procc, CommandCallback* callback)
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
2005-10-24 05:51:23 +00:00
|
|
|
PlayerID old_lp;
|
2007-06-18 19:53:50 +00:00
|
|
|
CommandCost res;
|
2006-06-04 13:03:48 +00:00
|
|
|
const char* tmp_cmdtext;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* If you enable DC_EXEC with DC_QUERY_COST you are a really strange
|
|
|
|
* person.. should we check for those funny jokes?
|
|
|
|
*/
|
|
|
|
|
2006-06-04 13:03:48 +00:00
|
|
|
/* The test already resets _cmd_text, so backup the pointer */
|
|
|
|
tmp_cmdtext = _cmd_text;
|
2005-12-10 18:43:49 +00:00
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
/* First, do a test-run to see if we can do this */
|
2006-04-10 07:15:58 +00:00
|
|
|
res = DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc);
|
2005-09-07 15:10:11 +00:00
|
|
|
/* The command failed, or you didn't want to execute, or you are quering, return */
|
2006-06-10 08:37:41 +00:00
|
|
|
if (CmdFailed(res) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) {
|
2005-09-07 15:10:11 +00:00
|
|
|
return res;
|
2005-12-10 18:43:49 +00:00
|
|
|
}
|
|
|
|
|
2006-06-04 13:03:48 +00:00
|
|
|
/* Restore _cmd_text */
|
|
|
|
_cmd_text = tmp_cmdtext;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* NetworkSend_Command needs _local_player to be set correctly, so
|
2006-06-10 08:37:41 +00:00
|
|
|
* adjust it, and put it back right after the function */
|
2005-09-07 15:10:11 +00:00
|
|
|
old_lp = _local_player;
|
|
|
|
_local_player = _current_player;
|
|
|
|
|
2005-12-08 21:12:15 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2005-09-07 15:10:11 +00:00
|
|
|
/* Send the command */
|
2006-06-10 08:37:41 +00:00
|
|
|
if (_networking) {
|
2005-09-07 15:10:11 +00:00
|
|
|
/* Network is easy, send it to his handler */
|
2006-06-04 09:10:24 +00:00
|
|
|
NetworkSend_Command(tile, p1, p2, procc, callback);
|
2006-06-10 08:37:41 +00:00
|
|
|
} else {
|
|
|
|
#else
|
|
|
|
{
|
2005-12-08 21:12:15 +00:00
|
|
|
#endif
|
2005-09-07 15:10:11 +00:00
|
|
|
/* If we execute BuildCommands directly in SP, we have a big problem with events
|
|
|
|
* so we need to delay is for 1 tick */
|
2006-06-04 09:10:24 +00:00
|
|
|
AI_PutCommandInQueue(_current_player, tile, p1, p2, procc, callback);
|
2006-06-10 08:37:41 +00:00
|
|
|
}
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* Set _local_player back */
|
|
|
|
_local_player = old_lp;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-06-04 09:10:24 +00:00
|
|
|
|
2007-12-21 21:50:46 +00:00
|
|
|
CommandCost AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint32 procc)
|
2006-06-04 09:10:24 +00:00
|
|
|
{
|
|
|
|
return AI_DoCommandCc(tile, p1, p2, flags, procc, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
/**
|
|
|
|
* Run 1 tick of the AI. Don't overdo it, keep it realistic.
|
|
|
|
*/
|
2005-10-24 05:51:23 +00:00
|
|
|
static void AI_RunTick(PlayerID player)
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
(svn r3313) Remove GPMI related changes from trunk
Revisions in detail: 2542, 3226 (partial), 3229, 3231, 3232, 3238, 3242-3245, 3251, 3253, 3260, 3263, 3265, 3266, 3269, 3277, 3278, 3279, 3283 (partial), 3304, 3305, 3306
2005-12-18 12:10:46 +00:00
|
|
|
extern void AiNewDoGameLoop(Player *p);
|
2005-11-22 22:32:42 +00:00
|
|
|
|
(svn r3313) Remove GPMI related changes from trunk
Revisions in detail: 2542, 3226 (partial), 3229, 3231, 3232, 3238, 3242-3245, 3251, 3253, 3260, 3263, 3265, 3266, 3269, 3277, 3278, 3279, 3283 (partial), 3304, 3305, 3306
2005-12-18 12:10:46 +00:00
|
|
|
Player *p = GetPlayer(player);
|
|
|
|
_current_player = player;
|
2005-11-22 22:32:42 +00:00
|
|
|
|
(svn r3313) Remove GPMI related changes from trunk
Revisions in detail: 2542, 3226 (partial), 3229, 3231, 3232, 3238, 3242-3245, 3251, 3253, 3260, 3263, 3265, 3266, 3269, 3277, 3278, 3279, 3283 (partial), 3304, 3305, 3306
2005-12-18 12:10:46 +00:00
|
|
|
if (_patches.ainew_active) {
|
|
|
|
AiNewDoGameLoop(p);
|
|
|
|
} else {
|
|
|
|
/* Enable all kind of cheats the old AI needs in order to operate correctly... */
|
|
|
|
_is_old_ai_player = true;
|
|
|
|
AiDoGameLoop(p);
|
|
|
|
_is_old_ai_player = false;
|
2005-09-13 12:19:27 +00:00
|
|
|
}
|
2008-01-16 01:18:15 +00:00
|
|
|
|
|
|
|
/* AI could change some track, so update signals */
|
|
|
|
UpdateSignalsInBuffer();
|
2005-09-07 15:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The gameloop for AIs.
|
|
|
|
* Handles one tick for all the AIs.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void AI_RunGameLoop()
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
|
|
|
/* Don't do anything if ai is disabled */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!_ai.enabled) return;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
2006-12-13 22:38:45 +00:00
|
|
|
/* Don't do anything if we are a network-client, or the AI has been disabled */
|
|
|
|
if (_networking && (!_network_server || !_patches.ai_in_multiplayer)) return;
|
2005-11-21 14:28:31 +00:00
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
/* New tick */
|
|
|
|
_ai.tick++;
|
|
|
|
|
|
|
|
/* Make sure the AI follows the difficulty rule.. */
|
2005-09-23 06:34:06 +00:00
|
|
|
assert(_opt.diff.competitor_speed <= 4);
|
2006-06-10 08:37:41 +00:00
|
|
|
if ((_ai.tick & ((1 << (4 - _opt.diff.competitor_speed)) - 1)) != 0) return;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* Check for AI-client (so joining a network with an AI) */
|
2006-10-15 17:01:19 +00:00
|
|
|
if (!_networking || _network_server) {
|
2005-09-07 15:10:11 +00:00
|
|
|
/* Check if we want to run AIs (server or SP only) */
|
2006-06-10 08:37:41 +00:00
|
|
|
const Player* p;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
(svn r3313) Remove GPMI related changes from trunk
Revisions in detail: 2542, 3226 (partial), 3229, 3231, 3232, 3238, 3242-3245, 3251, 3253, 3260, 3263, 3265, 3266, 3269, 3277, 3278, 3279, 3283 (partial), 3304, 3305, 3306
2005-12-18 12:10:46 +00:00
|
|
|
if (p->is_active && p->is_ai) {
|
|
|
|
/* This should always be true, else something went wrong... */
|
|
|
|
assert(_ai_player[p->index].active);
|
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
/* Run the script */
|
|
|
|
AI_DequeueCommands(p->index);
|
|
|
|
AI_RunTick(p->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_current_player = OWNER_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A new AI sees the day of light. You can do here what ever you think is needed.
|
|
|
|
*/
|
2005-10-24 05:51:23 +00:00
|
|
|
void AI_StartNewAI(PlayerID player)
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
2006-10-14 22:31:18 +00:00
|
|
|
assert(IsValidPlayer(player));
|
2005-11-29 16:06:06 +00:00
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
/* Called if a new AI is booted */
|
|
|
|
_ai_player[player].active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This AI player died. Give it some chance to make a final puf.
|
|
|
|
*/
|
2005-10-24 05:51:23 +00:00
|
|
|
void AI_PlayerDied(PlayerID player)
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
|
|
|
/* Called if this AI died */
|
|
|
|
_ai_player[player].active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize some AI-related stuff.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void AI_Initialize()
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
2005-11-30 16:10:19 +00:00
|
|
|
/* First, make sure all AIs are DEAD! */
|
|
|
|
AI_Uninitialize();
|
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
memset(&_ai, 0, sizeof(_ai));
|
(svn r3313) Remove GPMI related changes from trunk
Revisions in detail: 2542, 3226 (partial), 3229, 3231, 3232, 3238, 3242-3245, 3251, 3253, 3260, 3263, 3265, 3266, 3269, 3277, 3278, 3279, 3283 (partial), 3304, 3305, 3306
2005-12-18 12:10:46 +00:00
|
|
|
memset(&_ai_player, 0, sizeof(_ai_player));
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
_ai.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deinitializer for AI-related stuff.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void AI_Uninitialize()
|
2005-09-07 15:10:11 +00:00
|
|
|
{
|
2006-06-10 08:37:41 +00:00
|
|
|
const Player* p;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
(svn r3313) Remove GPMI related changes from trunk
Revisions in detail: 2542, 3226 (partial), 3229, 3231, 3232, 3238, 3242-3245, 3251, 3253, 3260, 3263, 3265, 3266, 3269, 3277, 3278, 3279, 3283 (partial), 3304, 3305, 3306
2005-12-18 12:10:46 +00:00
|
|
|
if (p->is_active && p->is_ai) AI_PlayerDied(p->index);
|
2005-09-07 15:10:11 +00:00
|
|
|
}
|
|
|
|
}
|