(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
authortruelight
Mon, 28 Nov 2005 14:02:36 +0000
changeset 2700 555506b02195
parent 2699 ae1379383e0f
child 2701 a2b9a934044e
(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
ai/ai.h
--- a/ai/ai.c	Mon Nov 28 11:30:12 2005 +0000
+++ b/ai/ai.c	Mon Nov 28 14:02:36 2005 +0000
@@ -198,23 +198,75 @@
 	_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, &params);
+
+		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 @@
 	_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 @@
 	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 */
 }
--- a/ai/ai.h	Mon Nov 28 11:30:12 2005 +0000
+++ b/ai/ai.h	Mon Nov 28 14:02:36 2005 +0000
@@ -40,6 +40,10 @@
 	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;