@@ -21,197 +21,199 @@
static AICommand *command_uid[MAX_PLAYERS];
static AICommand *command_uid_tail[MAX_PLAYERS];
static uint uids[MAX_PLAYERS];
/**
* Dequeues commands put in the queue via AI_PutCommandInQueue.
*/
void AI_DequeueCommands(byte player)
{
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;
/* Copy the DP back in place */
memcpy(_decode_parameters, com->dp, sizeof(com->dp));
DoCommandP(com->tile, com->p1, com->p2, NULL, com->procc);
/* Free item */
entry_com = com->next;
free(com);
}
* Needed for SP; we need to delay DoCommand with 1 tick, because else events
* will make infinite loops (AIScript).
void AI_PutCommandInQueue(byte player, uint tile, uint32 p1, uint32 p2, uint procc)
AICommand *com;
if (_ai_player[player].queue_tail == NULL) {
/* There is no item in the queue yet, create the queue */
_ai_player[player].queue = malloc(sizeof(AICommand));
_ai_player[player].queue_tail = _ai_player[player].queue;
} else {
/* Add an item at the end */
_ai_player[player].queue_tail->next = malloc(sizeof(AICommand));
_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;
com->next = NULL;
/* Copy the decode_parameters */
memcpy(com->dp, _decode_parameters, sizeof(com->dp));
* Executes a raw DoCommand for the AI.
int32 AI_DoCommand(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
PlayerID old_lp;
int32 res = 0;
/* If you enable DC_EXEC with DC_QUERY_COST you are a really strange
* person.. should we check for those funny jokes?
/* First, do a test-run to see if we can do this */
res = DoCommandByTile(tile, p1, p2, flags & ~DC_EXEC, procc);
/* The command failed, or you didn't want to execute, or you are quering, return */
if ((CmdFailed(res)) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST))
return res;
/* If we did a DC_EXEC, and the command did not return an error, execute it
over the network */
if (flags & DC_AUTO) procc |= CMD_AUTO;
if (flags & DC_NO_WATER) procc |= CMD_NO_WATER;
/* NetworkSend_Command needs _local_player to be set correctly, so
adjust it, and put it back right after the function */
old_lp = _local_player;
_local_player = _current_player;
#ifdef ENABLE_NETWORK
/* Send the command */
if (_networking)
/* Network is easy, send it to his handler */
NetworkSend_Command(tile, p1, p2, procc, NULL);
else
#endif
/* If we execute BuildCommands directly in SP, we have a big problem with events
* so we need to delay is for 1 tick */
AI_PutCommandInQueue(_current_player, tile, p1, p2, procc);
/* Set _local_player back */
_local_player = old_lp;
int32 AI_DoCommandChecked(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
AICommand *new;
uint unique_id = uids[_current_player]++;
int32 res;
res = AI_DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc);
if (CmdFailed(res))
return CMD_ERROR;
/* Save the command and his things, together with the unique_id */
new = malloc(sizeof(AICommand));
new->tile = tile;
new->p1 = p1;
new->p2 = p2;
new->procc = procc;
new->next = NULL;
new->dp[0] = unique_id;
/* Add it to the back of the list */
if (command_uid_tail[_current_player] == NULL)
command_uid_tail[_current_player] = new;
command_uid_tail[_current_player]->next = new;
if (command_uid[_current_player] == NULL)
command_uid[_current_player] = new;
AI_DoCommand(tile, p1, p2, flags, procc);
return unique_id;
* A command is executed for real, and is giving us his result (failed yes/no). Inform the AI with it via
* an event.Z
void AI_CommandResult(uint32 cmd, uint32 p1, uint32 p2, TileIndex tile, bool succeeded)
AICommand *command = command_uid[_current_player];
if (command == NULL)
return;
if (command->procc != (cmd & 0xFF) || command->p1 != p1 || command->p2 != p2 || command->tile != tile) {
/* If we come here, we see a command that isn't at top in our list. This is okay, if the command isn't
* anywhere else in our list, else we have a big problem.. so check for that. If it isn't in our list,
* it is okay, else, drop the game.
* Why do we have a big problem in the case it is in our list? Simply, we have a command sooner in
* our list that wasn't triggered to be failed or succeeded, so it is sitting there without an
* event, so the script will never know if it succeeded yes/no, so it can hang.. this is VERY bad
* and should never ever happen. */
while (command != NULL && (command->procc != (cmd & 0xFF) || command->p1 != p1 || command->p2 != p2 || command->tile != tile)) {
command = command->next;
assert(command == NULL);
command_uid[_current_player] = command_uid[_current_player]->next;
command_uid_tail[_current_player] = NULL;
ai_event(_current_player, succeeded ? ottd_Event_CommandSucceeded : ottd_Event_CommandFailed, tile, command->dp[0]);
free(command);
* Run 1 tick of the AI. Don't overdo it, keep it realistic.
static void AI_RunTick(PlayerID player)
#ifdef GPMI
if (_ai.gpmi) {
gpmi_call_RunTick(_ai_player[player].module, _frame_counter);
#endif /* GPMI */
extern void AiNewDoGameLoop(Player *p);
Player *p = GetPlayer(player);
if (_patches.ainew_active) {
#ifndef AI_H
#define AI_H
#include "../functions.h"
#include "../network.h"
#include "../player.h"
#include <gpmi.h>
/* How DoCommands look like for an AI */
typedef struct AICommand {
uint32 tile;
uint32 p1;
uint32 p2;
uint32 procc;
uint32 dp[20];
struct AICommand *next;
} AICommand;
/* The struct for an AIScript Player */
typedef struct AIPlayer {
bool active; //! Is this AI active?
AICommand *queue; //! The commands that he has in his queue
AICommand *queue_tail; //! The tail of this queue
gpmi_module *module; //! The link to the GPMI module
} AIPlayer;
/* The struct to keep some data about the AI in general */
typedef struct AIStruct {
/* General */
bool enabled; //! Is AI enabled?
uint tick; //! The current tick (something like _frame_counter, only for AIs)
/* For network-clients (a OpenTTD client who acts as an AI connected to a server) */
bool network_client; //! Are we a network_client?
uint8 network_playas; //! The current network player we are connected as
bool gpmi; //! True if we want GPMI AIs
gpmi_module *gpmi_mod; //! The module controller for GPMI based AIs (Event-handling)
gpmi_package *gpmi_pkg; //! The package controller for GPMI based AIs (Functions)
char gpmi_param[128]; //! The params given to the gpmi_mod
} AIStruct;
VARDEF AIStruct _ai;
VARDEF AIPlayer _ai_player[MAX_PLAYERS];
// ai.c
void AI_StartNewAI(PlayerID player);
void AI_PlayerDied(PlayerID player);
void AI_RunGameLoop(void);
void AI_Initialize(void);
void AI_Uninitialize(void);
int32 AI_DoCommand(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
int32 AI_DoCommandChecked(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
void AI_CommandResult(uint32 cmd, uint32 p1, uint32 p2, TileIndex tile, bool failed);
/** Is it allowed to start a new AI.
* This function checks some boundries to see if we should launch a new AI.
* @return True if we can start a new AI.
static inline bool AI_AllowNewAI(void)
/* If disabled, no AI */
if (!_ai.enabled)
return false;
/* If in network, but no server, no AI */
if (_networking && !_network_server)
/* If in network, and server, possible AI */
if (_networking && _network_server) {
/* Do we want AIs in multiplayer? */
if (!_patches.ai_in_multiplayer)
/* Only the NewAI is allowed... sadly enough the old AI just doesn't support this
* system, because all commands are delayed by at least 1 tick, which causes
* a big problem, because it uses variables that are only set AFTER the command
* is really executed... */
if (!_patches.ainew_active && !_ai.gpmi)
return true;
#define AI_CHANCE16(a,b) ((uint16) AI_Random() <= (uint16)((65536 * a) / b))
#define AI_CHANCE16R(a,b,r) ((uint16)(r = AI_Random()) <= (uint16)((65536 * a) / b))
* The random-function that should be used by ALL AIs.
static inline uint AI_RandomRange(uint max)
Status change: