civ2civ3: improve README.civ2civ3 text.
[freeciv.git] / server / aiiface.c
blobd030667c0ad3506e7463f6b36191e9aee28cd1b5
1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #ifdef AI_MODULES
19 #include <ltdl.h>
20 #endif
22 /* utility */
23 #include "support.h"
25 /* common */
26 #include "ai.h"
27 #include "player.h"
29 /* server/advisors */
30 #include "autosettlers.h"
32 /* ai/classic */
33 #include "classicai.h"
35 #include "aiiface.h"
37 #ifdef AI_MOD_STATIC_THREADED
38 bool fc_ai_threaded_setup(struct ai_type *ai);
39 #endif
41 #ifdef AI_MOD_STATIC_THREXPR
42 bool fc_ai_threxpr_setup(struct ai_type *ai);
43 #endif
45 #ifdef AI_MOD_STATIC_STUB
46 bool fc_ai_stub_setup(struct ai_type *ai);
47 #endif
49 static struct ai_type *default_ai = NULL;
51 #ifdef AI_MODULES
52 /**************************************************************************
53 Return string describing module loading error. Never returns NULL.
54 **************************************************************************/
55 static const char *fc_module_error(void)
57 static char def_err[] = "Unknown error";
58 const char *errtxt = lt_dlerror();
60 if (errtxt == NULL) {
61 return def_err;
64 return errtxt;
67 /**************************************************************************
68 Load ai module from file.
69 **************************************************************************/
70 bool load_ai_module(const char *modname)
72 struct ai_type *ai = ai_type_alloc();
73 bool setup_success;
74 lt_dlhandle handle;
75 bool (*setup_func)(struct ai_type *ai);
76 const char *(*capstr_func)(void);
77 const char *capstr;
78 char buffer[2048];
79 char filename[1024];
81 if (ai == NULL) {
82 return FALSE;
85 init_ai(ai);
87 fc_snprintf(filename, sizeof(filename), "fc_ai_%s", modname);
88 fc_snprintf(buffer, sizeof(buffer), "%s", filename);
89 handle = lt_dlopenext(buffer);
90 if (handle == NULL) {
91 log_error(_("Cannot open AI module %s (%s)"), filename, fc_module_error());
92 return FALSE;
95 fc_snprintf(buffer, sizeof(buffer), "%s_capstr", filename);
96 capstr_func = lt_dlsym(handle, buffer);
97 if (capstr_func == NULL) {
98 log_error(_("Cannot find capstr function from ai module %s (%s)"),
99 filename, fc_module_error());
100 return FALSE;
103 capstr = capstr_func();
104 if (strcmp(FC_AI_MOD_CAPSTR, capstr)) {
105 log_error(_("Incompatible ai module %s:"), filename);
106 log_error(_(" Module options: %s"), capstr);
107 log_error(_(" Supported options: %s"), FC_AI_MOD_CAPSTR);
109 return FALSE;
112 fc_snprintf(buffer, sizeof(buffer), "%s_setup", filename);
113 setup_func = lt_dlsym(handle, buffer);
114 if (setup_func == NULL) {
115 log_error(_("Cannot find setup function from ai module %s (%s)"),
116 filename, fc_module_error());
117 return FALSE;
119 setup_success = setup_func(ai);
121 if (!setup_success) {
122 log_error(_("Setup of ai module %s failed."), filename);
123 return FALSE;
126 return TRUE;
128 #endif /* AI_MODULES */
130 /**************************************************************************
131 Initialize ai stuff
132 **************************************************************************/
133 void ai_init(void)
135 bool failure = FALSE;
136 #if !defined(AI_MODULES) || defined(AI_MOD_STATIC_CLASSIC) || defined(AI_MOD_STATIC_THREADED) || defined(AI_MOD_STATIC_THREXPR) || defined(AI_MOD_STATIC_STUB)
137 /* First !defined(AI_MODULES) case is for default ai support. */
138 struct ai_type *ai;
139 #endif
141 #ifdef AI_MODULES
142 if (lt_dlinit()) {
143 failure = TRUE;
145 if (!failure) {
147 #ifdef FREECIV_DEBUG
148 /* First search ai modules under directory ai/<module> under
149 current directory. This allows us to run freeciv without
150 installing it. */
151 const char *moduledirs[] = { "classic", "threaded", "threxpr", "stub", NULL };
152 int i;
154 for (i = 0; moduledirs[i] != NULL ; i++) {
155 char buf[2048];
157 fc_snprintf(buf, sizeof(buf), "ai/%s", moduledirs[i]);
158 lt_dladdsearchdir(buf);
160 #endif /* FREECIV_DEBUG */
162 /* Then search ai modules from their installation directory. */
163 lt_dladdsearchdir(AI_MODULEDIR);
165 #endif /* AI_MODULES */
167 #ifdef AI_MOD_STATIC_CLASSIC
168 ai = ai_type_alloc();
169 if (ai != NULL) {
170 init_ai(ai);
171 if (!fc_ai_classic_setup(ai)) {
172 log_error(_("Failed to setup \"%s\" AI module"), "classic");
173 ai_type_dealloc();
176 #endif /* AI_MOD_STATIC_CLASSIC */
178 #ifdef AI_MOD_STATIC_THREADED
179 ai = ai_type_alloc();
180 if (ai != NULL) {
181 init_ai(ai);
182 if (!fc_ai_threaded_setup(ai)) {
183 log_error(_("Failed to setup \"%s\" AI module"), "threaded");
184 ai_type_dealloc();
187 #endif /* AI_MOD_STATIC_THREADED */
189 #ifdef AI_MOD_STATIC_THREXPR
190 ai = ai_type_alloc();
191 if (ai != NULL) {
192 init_ai(ai);
193 if (!fc_ai_threxpr_setup(ai)) {
194 log_error(_("Failed to setup \"%s\" AI module"), "threxpr");
195 ai_type_dealloc();
198 #endif /* AI_MOD_STATIC_THREXPR */
200 #ifdef AI_MOD_STATIC_STUB
201 ai = ai_type_alloc();
202 if (ai != NULL) {
203 init_ai(ai);
204 if (!fc_ai_stub_setup(ai)) {
205 log_error(_("Failed to setup \"%s\" AI module"), "stub");
206 ai_type_dealloc();
209 #endif /* AI_MOD_STATIC_STUB */
211 default_ai = ai_type_by_name(AI_MOD_DEFAULT);
212 #ifdef AI_MODULES
213 if (default_ai == NULL) {
214 /* Wasn't among statically linked. Try to load dynamic module. */
215 if (!failure && !load_ai_module(AI_MOD_DEFAULT)) {
216 failure = TRUE;
218 if (!failure) {
219 default_ai = ai_type_by_name(AI_MOD_DEFAULT);
222 #endif /* AI_MODULES */
223 if (default_ai == NULL || failure) {
224 log_error(_("Failed to setup default AI module \"%s\", cannot continue."),
225 AI_MOD_DEFAULT);
226 exit(EXIT_FAILURE);
230 /**************************************************************************
231 Call incident function of victim.
232 **************************************************************************/
233 void call_incident(enum incident_type type, struct player *violator,
234 struct player *victim)
236 CALL_PLR_AI_FUNC(incident, victim, type, violator, victim);
239 /****************************************************************************
240 Call ai refresh() callback for all players.
241 ****************************************************************************/
242 void call_ai_refresh(void)
244 players_iterate(pplayer) {
245 CALL_PLR_AI_FUNC(refresh, pplayer, pplayer);
246 } players_iterate_end;
249 /**************************************************************************
250 Return name of default ai type.
251 **************************************************************************/
252 const char *default_ai_type_name(void)
254 return default_ai->name;