summaryrefslogtreecommitdiff
path: root/ai
diff options
context:
space:
mode:
authortruelight <truelight@openttd.org>2005-11-22 22:32:42 +0000
committertruelight <truelight@openttd.org>2005-11-22 22:32:42 +0000
commit492723691260bd2a79c7aa28d8c487198236333b (patch)
treec108cff1d6d6c569160b15fee5831f21f05596ef /ai
parentf6ddaf6a23c4218f5ab7c61a30288d78d5073484 (diff)
downloadopenttd-492723691260bd2a79c7aa28d8c487198236333b.tar.xz
(svn r3229) -Add: add more GPMI support. Now GPMI-based AIs can be loaded (doesn't change a thing if you didn't enable GPMI)
Diffstat (limited to 'ai')
-rw-r--r--ai/ai.c67
-rw-r--r--ai/ai.h10
2 files changed, 60 insertions, 17 deletions
diff --git a/ai/ai.c b/ai/ai.c
index b7271d2ce..764d06f94 100644
--- a/ai/ai.c
+++ b/ai/ai.c
@@ -5,6 +5,7 @@
#include "../variables.h"
#include "../command.h"
#include "../network.h"
+#include "../debug.h"
#include "ai.h"
#include "default/default.h"
@@ -120,18 +121,28 @@ int32 AI_DoCommand(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
*/
static void AI_RunTick(PlayerID player)
{
- extern void AiNewDoGameLoop(Player *p);
-
- Player *p = GetPlayer(player);
_current_player = player;
- 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;
+#ifdef GPMI
+ if (_ai.gpmi) {
+ gpmi_call_RunTick(_ai_player[player].module, _frame_counter);
+ return;
+ }
+#endif /* GPMI */
+
+ {
+ extern void AiNewDoGameLoop(Player *p);
+
+ Player *p = GetPlayer(player);
+
+ 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;
+ }
}
}
@@ -171,10 +182,7 @@ void AI_RunGameLoop(void)
Player *p;
FOR_ALL_PLAYERS(p) {
- if (p->is_active && p->is_ai) {
- /* This should always be true, else something went wrong... */
- assert(_ai_player[p->index].active);
-
+ if (p->is_active && p->is_ai && _ai_player[p->index].active) {
/* Run the script */
AI_DequeueCommands(p->index);
AI_RunTick(p->index);
@@ -190,6 +198,21 @@ void AI_RunGameLoop(void)
*/
void AI_StartNewAI(PlayerID player)
{
+#ifdef GPMI
+ char library[80];
+ char params[80];
+
+ /* XXX -- Todo, make a nice assign for library and params from a nice GUI :) */
+ snprintf(library, sizeof(library), "php");
+ snprintf(params, sizeof(params), "daeb");
+
+ _ai_player[player].module = gpmi_mod_load(library, params);
+ if (_ai_player[player].module == NULL) {
+ DEBUG(ai, 0)("[AI] Failed to load AI, aborting..");
+ return;
+ }
+#endif /* GPMI */
+
/* Called if a new AI is booted */
_ai_player[player].active = true;
}
@@ -204,6 +227,10 @@ void AI_PlayerDied(PlayerID player)
/* Called if this AI died */
_ai_player[player].active = false;
+
+#ifdef GPMI
+ gpmi_mod_unload(_ai_player[player].module);
+#endif /* GPMI */
}
/**
@@ -211,14 +238,20 @@ void AI_PlayerDied(PlayerID player)
*/
void AI_Initialize(void)
{
- bool ai_network_client = _ai.network_client;
+ bool tmp_ai_network_client = _ai.network_client;
+#ifdef GPMI
+ bool tmp_ai_gpmi = _ai.gpmi;
+#endif /* GPMI */
memset(&_ai, 0, sizeof(_ai));
memset(&_ai_player, 0, sizeof(_ai_player));
- _ai.network_client = ai_network_client;
+ _ai.network_client = tmp_ai_network_client;
_ai.network_playas = OWNER_SPECTATOR;
_ai.enabled = true;
+#ifdef GPMI
+ _ai.gpmi = tmp_ai_gpmi;
+#endif /* GPMI */
}
/**
@@ -229,6 +262,6 @@ void AI_Uninitialize(void)
Player* p;
FOR_ALL_PLAYERS(p) {
- if (p->is_active && p->is_ai) AI_PlayerDied(p->index);
+ if (p->is_active && p->is_ai && _ai_player[p->index].active) AI_PlayerDied(p->index);
}
}
diff --git a/ai/ai.h b/ai/ai.h
index 910e050b1..97747aa7b 100644
--- a/ai/ai.h
+++ b/ai/ai.h
@@ -3,6 +3,9 @@
#include "../functions.h"
#include "../network.h"
+#ifdef GPMI
+#include <gpmi.h>
+#endif /* GPMI */
/* How DoCommands look like for an AI */
typedef struct AICommand {
@@ -21,6 +24,9 @@ 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
+#ifdef GPMI
+ gpmi_module *module; //! The link to the GPMI module
+#endif /* GPMI */
} AIPlayer;
/* The struct to keep some data about the AI in general */
@@ -32,6 +38,10 @@ typedef struct AIStruct {
/* 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
+
+#ifdef GPMI
+ bool gpmi; //! True if we want GPMI AIs
+#endif /* GPMI */
} AIStruct;
VARDEF AIStruct _ai;