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)
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 ***********************************************************************/
15 #include <fc_config.h>
30 #include "autosettlers.h"
33 #include "classicai.h"
37 #ifdef AI_MOD_STATIC_THREADED
38 bool fc_ai_threaded_setup(struct ai_type
*ai
);
41 #ifdef AI_MOD_STATIC_THREXPR
42 bool fc_ai_threxpr_setup(struct ai_type
*ai
);
45 #ifdef AI_MOD_STATIC_STUB
46 bool fc_ai_stub_setup(struct ai_type
*ai
);
49 static struct ai_type
*default_ai
= NULL
;
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();
67 /**************************************************************************
68 Load ai module from file.
69 **************************************************************************/
70 bool load_ai_module(const char *modname
)
72 struct ai_type
*ai
= ai_type_alloc();
75 bool (*setup_func
)(struct ai_type
*ai
);
76 const char *(*capstr_func
)(void);
87 fc_snprintf(filename
, sizeof(filename
), "fc_ai_%s", modname
);
88 fc_snprintf(buffer
, sizeof(buffer
), "%s", filename
);
89 handle
= lt_dlopenext(buffer
);
91 log_error(_("Cannot open AI module %s (%s)"), filename
, fc_module_error());
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());
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
);
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());
119 setup_success
= setup_func(ai
);
121 if (!setup_success
) {
122 log_error(_("Setup of ai module %s failed."), filename
);
128 #endif /* AI_MODULES */
130 /**************************************************************************
132 **************************************************************************/
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. */
148 /* First search ai modules under directory ai/<module> under
149 current directory. This allows us to run freeciv without
151 const char *moduledirs
[] = { "classic", "threaded", "threxpr", "stub", NULL
};
154 for (i
= 0; moduledirs
[i
] != NULL
; i
++) {
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();
171 if (!fc_ai_classic_setup(ai
)) {
172 log_error(_("Failed to setup \"%s\" AI module"), "classic");
176 #endif /* AI_MOD_STATIC_CLASSIC */
178 #ifdef AI_MOD_STATIC_THREADED
179 ai
= ai_type_alloc();
182 if (!fc_ai_threaded_setup(ai
)) {
183 log_error(_("Failed to setup \"%s\" AI module"), "threaded");
187 #endif /* AI_MOD_STATIC_THREADED */
189 #ifdef AI_MOD_STATIC_THREXPR
190 ai
= ai_type_alloc();
193 if (!fc_ai_threxpr_setup(ai
)) {
194 log_error(_("Failed to setup \"%s\" AI module"), "threxpr");
198 #endif /* AI_MOD_STATIC_THREXPR */
200 #ifdef AI_MOD_STATIC_STUB
201 ai
= ai_type_alloc();
204 if (!fc_ai_stub_setup(ai
)) {
205 log_error(_("Failed to setup \"%s\" AI module"), "stub");
209 #endif /* AI_MOD_STATIC_STUB */
211 default_ai
= ai_type_by_name(AI_MOD_DEFAULT
);
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
)) {
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."),
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
;