From 2eb1c8a2151deef9155eb73a83873029d3b40f73 Mon Sep 17 00:00:00 2001 From: truelight Date: Mon, 28 Nov 2005 14:02:36 +0000 Subject: (svn r3244) -Fix: [GPMI] Even more GPMI based AI-code cleanup, bug fixes, and you can now control the AI that is going to boot --- ai/ai.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- ai/ai.h | 4 ++++ 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/ai/ai.c b/ai/ai.c index 50a4a39ca..52950be25 100644 --- a/ai/ai.c +++ b/ai/ai.c @@ -198,23 +198,75 @@ void AI_RunGameLoop(void) _current_player = OWNER_NONE; } +#ifdef GPMI + +void (*ottd_GetNextAIData)(char **library, char **param); + +void AI_ShutdownAIControl(bool with_error) +{ + if (_ai.gpmi_mod != NULL) + gpmi_mod_unload(_ai.gpmi_mod); + if (_ai.gpmi_pkg != NULL) + gpmi_pkg_unload(_ai.gpmi_pkg); + + if (with_error) { + DEBUG(ai, 0)("[AI] Failed to load AI Control, switching back to built-in AIs.."); + _ai.gpmi = false; + } +} + +void AI_LoadAIControl(void) +{ + /* Load module */ + _ai.gpmi_mod = gpmi_mod_load("ottd_ai_control_mod", NULL); + if (_ai.gpmi_mod == NULL) { + AI_ShutdownAIControl(true); + return; + } + + /* Load package */ + if (gpmi_pkg_load("ottd_ai_control_pkg", 0, NULL, NULL, &_ai.gpmi_pkg)) { + AI_ShutdownAIControl(true); + return; + } + + /* Now link all the functions */ + { + ottd_GetNextAIData = gpmi_pkg_resolve(_ai.gpmi_pkg, "ottd_GetNextAIData"); + + if (ottd_GetNextAIData == NULL) + AI_ShutdownAIControl(true); + } +} +#endif /* GPMI */ + /** * A new AI sees the day of light. You can do here what ever you think is needed. */ void AI_StartNewAI(PlayerID player) { #ifdef GPMI - char library[80]; - char params[80]; + /* Keep this in a different IF, because the function can turn _ai.gpmi off!! */ + if (_ai.gpmi && _ai.gpmi_mod == NULL) + AI_LoadAIControl(); - /* XXX -- Todo, make a nice assign for library and params from a nice GUI :) */ - snprintf(library, sizeof(library), "php"); - snprintf(params, sizeof(params), "daeb"); + if (_ai.gpmi) { + char *library = NULL; + char *params = NULL; - _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; + ottd_GetNextAIData(&library, ¶ms); + + if (library != NULL) { + _ai_player[player].module = gpmi_mod_load(library, params); + free(library); + } + if (params != NULL) + free(params); + + if (_ai_player[player].module == NULL) { + DEBUG(ai, 0)("[AI] Failed to load AI, aborting.."); + return; + } } #endif /* GPMI */ @@ -234,7 +286,8 @@ void AI_PlayerDied(PlayerID player) _ai_player[player].active = false; #ifdef GPMI - gpmi_mod_unload(_ai_player[player].module); + if (_ai_player[player].module != NULL) + gpmi_mod_unload(_ai_player[player].module); #endif /* GPMI */ } @@ -265,4 +318,8 @@ void AI_Uninitialize(void) FOR_ALL_PLAYERS(p) { if (p->is_active && p->is_ai && _ai_player[p->index].active) AI_PlayerDied(p->index); } + +#ifdef GPMI + AI_ShutdownAIControl(false); +#endif /* GPMI */ } diff --git a/ai/ai.h b/ai/ai.h index 97a70b529..af55f6506 100644 --- a/ai/ai.h +++ b/ai/ai.h @@ -40,6 +40,10 @@ typedef struct AIStruct { uint8 network_playas; //! The current network player we are connected as bool gpmi; //! True if we want GPMI AIs +#ifdef GPMI + 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) +#endif /* GPMI */ } AIStruct; VARDEF AIStruct _ai; -- cgit v1.2.3-70-g09d2