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 static struct ai_type
*default_ai
= NULL
;
44 /**************************************************************************
45 Return string describing module loading error. Never returns NULL.
46 **************************************************************************/
47 static const char *fc_module_error(void)
49 static char def_err
[] = "Unknown error";
50 const char *errtxt
= lt_dlerror();
59 /**************************************************************************
60 Load ai module from file.
61 **************************************************************************/
62 bool load_ai_module(const char *modname
)
64 struct ai_type
*ai
= ai_type_alloc();
67 bool (*setup_func
)(struct ai_type
*ai
);
68 const char *(*capstr_func
)(void);
79 fc_snprintf(filename
, sizeof(filename
), "fc_ai_%s", modname
);
80 fc_snprintf(buffer
, sizeof(buffer
), "%s", filename
);
81 handle
= lt_dlopenext(buffer
);
83 log_error(_("Cannot open AI module %s (%s)"), filename
, fc_module_error());
87 fc_snprintf(buffer
, sizeof(buffer
), "%s_capstr", filename
);
88 capstr_func
= lt_dlsym(handle
, buffer
);
89 if (capstr_func
== NULL
) {
90 log_error(_("Cannot find capstr function from ai module %s (%s)"),
91 filename
, fc_module_error());
95 capstr
= capstr_func();
96 if (strcmp(FC_AI_MOD_CAPSTR
, capstr
)) {
97 log_error(_("Incompatible ai module %s:"), filename
);
98 log_error(_(" Module options: %s"), capstr
);
99 log_error(_(" Supported options: %s"), FC_AI_MOD_CAPSTR
);
104 fc_snprintf(buffer
, sizeof(buffer
), "%s_setup", filename
);
105 setup_func
= lt_dlsym(handle
, buffer
);
106 if (setup_func
== NULL
) {
107 log_error(_("Cannot find setup function from ai module %s (%s)"),
108 filename
, fc_module_error());
111 setup_success
= setup_func(ai
);
113 if (!setup_success
) {
114 log_error(_("Setup of ai module %s failed."), filename
);
120 #endif /* AI_MODULES */
122 /**************************************************************************
124 **************************************************************************/
127 bool failure
= FALSE
;
128 #if !defined(AI_MODULES) || defined(AI_MOD_STATIC_CLASSIC) || defined(AI_MOD_STATIC_THREADED)
129 /* First !defined(AI_MODULES) case is for default ai support. */
140 /* First search ai modules under directory ai/<module> under
141 current directory. This allows us to run freeciv without
143 const char *moduledirs
[] = { "classic", "threaded", "stub", NULL
};
146 for (i
= 0; moduledirs
[i
] != NULL
; i
++) {
149 fc_snprintf(buf
, sizeof(buf
), "ai/%s", moduledirs
[i
]);
150 lt_dladdsearchdir(buf
);
154 /* Then search ai modules from their installation directory. */
155 lt_dladdsearchdir(AI_MODULEDIR
);
157 #endif /* AI_MODULES */
159 #ifdef AI_MOD_STATIC_CLASSIC
160 ai
= ai_type_alloc();
163 if (!fc_ai_classic_setup(ai
)) {
164 log_error(_("Failed to setup \"%s\" AI module"), "classic");
168 #endif /* AI_MOD_STATIC_CLASSIC */
170 #ifdef AI_MOD_STATIC_THREADED
171 ai
= ai_type_alloc();
174 if (!fc_ai_threaded_setup(ai
)) {
175 log_error(_("Failed to setup \"%s\" AI module"), "threaded");
179 #endif /* AI_MOD_STATIC_THREADED */
181 default_ai
= ai_type_by_name(AI_MOD_DEFAULT
);
183 if (default_ai
== NULL
) {
184 /* Wasn't among statically linked. Try to load dynamic module. */
185 if (!failure
&& !load_ai_module(AI_MOD_DEFAULT
)) {
189 default_ai
= ai_type_by_name(AI_MOD_DEFAULT
);
192 #endif /* AI_MODULES */
193 if (default_ai
== NULL
|| failure
) {
194 log_error(_("Failed to setup default AI module \"%s\", cannot continue."),
200 /**************************************************************************
201 Call incident function of victim.
202 **************************************************************************/
203 void call_incident(enum incident_type type
, struct player
*violator
,
204 struct player
*victim
)
206 CALL_PLR_AI_FUNC(incident
, victim
, type
, violator
, victim
);
209 /****************************************************************************
210 Call ai refresh() callback for all players.
211 ****************************************************************************/
212 void call_ai_refresh(void)
214 players_iterate(pplayer
) {
215 CALL_PLR_AI_FUNC(refresh
, pplayer
, pplayer
);
216 } players_iterate_end
;
219 /**************************************************************************
220 Return name of default ai type.
221 **************************************************************************/
222 const char *default_ai_type_name(void)
224 return default_ai
->name
;