@@ -69,101 +69,103 @@ void AI_PutCommandInQueue(byte player, u
} 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)
#ifndef AI_H
#define AI_H
#include "../functions.h"
#include "../network.h"
#include "../player.h"
#ifdef GPMI
#include <gpmi.h>
#endif /* GPMI */
/* 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
Status change: