From 607e518ab80aa3a8a29c4e7d94a93a1817615ee5 Mon Sep 17 00:00:00 2001 From: truelight Date: Wed, 14 Dec 2005 14:38:23 +0000 Subject: (svn r3304) -Add: allow AI-events to see the UID of the command -Fix: improved the logic of the UID code for AIs --- ai/ai.c | 28 +++++++++++++++++++++++----- ai/ai.h | 5 ++++- ai/ai_event.h | 2 +- 3 files changed, 28 insertions(+), 7 deletions(-) (limited to 'ai') diff --git a/ai/ai.c b/ai/ai.c index 15d08bb54..a66a42123 100644 --- a/ai/ai.c +++ b/ai/ai.c @@ -156,7 +156,7 @@ 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) { AICommand *new; - uint unique_id = uids[_current_player]; + uint unique_id = uids[_current_player] + 1; // + 1, because 0 is reserved int32 res; res = DoCommandByTile(tile, p1, p2, flags & ~DC_EXEC, procc); @@ -192,13 +192,16 @@ int32 AI_DoCommandChecked(uint tile, uint32 p1, uint32 p2, uint32 flags, uint pr } /** - * A command is executed for real, and is giving us his result (failed yes/no). Inform the AI with it via - * an event.Z + * Find the right UID for this command. */ -void AI_CommandResult(uint32 cmd, uint32 p1, uint32 p2, TileIndex tile, bool succeeded) +void AI_GetCommandUID(uint32 cmd, uint32 p1, uint32 p2, TileIndex tile) { AICommand *command = command_uid[_current_player]; + /* Reset to 0, meaning no UID. Then we start detecting if we have an UID for this command */ + _ai_current_uid = 0; + _ai_current_tile = INVALID_TILE; + if (command == NULL) return; @@ -217,14 +220,29 @@ void AI_CommandResult(uint32 cmd, uint32 p1, uint32 p2, TileIndex tile, bool suc return; } + /* Remove this command from the list */ command_uid[_current_player] = command_uid[_current_player]->next; if (command_uid[_current_player] == NULL) command_uid_tail[_current_player] = NULL; - ai_event(_current_player, succeeded ? ottd_Event_CommandSucceeded : ottd_Event_CommandFailed, tile, command->uid); + /* Broadcast our current UID and tile */ + _ai_current_uid = command->uid; + _ai_current_tile = tile; free(command); } +/** + * A command is executed for real, and is giving us his result (failed yes/no). Inform the AI with it via + * an event. + */ +void AI_CommandResult(bool succeeded) +{ + if (_ai_current_uid == 0) + return; + + ai_event(_current_player, succeeded ? ottd_Event_CommandSucceeded : ottd_Event_CommandFailed, _ai_current_tile); +} + /** * Run 1 tick of the AI. Don't overdo it, keep it realistic. */ diff --git a/ai/ai.h b/ai/ai.h index a2361af1b..325885410 100644 --- a/ai/ai.h +++ b/ai/ai.h @@ -51,6 +51,8 @@ typedef struct AIStruct { VARDEF AIStruct _ai; VARDEF AIPlayer _ai_player[MAX_PLAYERS]; +VARDEF uint _ai_current_uid; //! Keeps track of the current UID, if any (0 means none) +VARDEF TileIndex _ai_current_tile; //! Keeps track of the current Tile. // ai.c void AI_StartNewAI(PlayerID player); @@ -60,7 +62,8 @@ 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); +void AI_GetCommandUID(uint32 cmd, uint32 p1, uint32 p2, TileIndex tile); +void AI_CommandResult(bool failed); /** Is it allowed to start a new AI. * This function checks some boundries to see if we should launch a new AI. diff --git a/ai/ai_event.h b/ai/ai_event.h index d1daf7804..81aa9e89a 100644 --- a/ai/ai_event.h +++ b/ai/ai_event.h @@ -13,7 +13,7 @@ * tell us ASAP! */ # define ai_event(player, event, ...) \ if ((player) < MAX_PLAYERS && _ai_player[(player)].module != NULL) \ - gpmi_event(_ai_player[(player)].module, (event), ##__VA_ARGS__) + gpmi_event(_ai_player[(player)].module, (event), _ai_current_uid, ##__VA_ARGS__) #else /* GPMI */ -- cgit v1.2.3-70-g09d2