Freeciv-3.3: /fast/freeciv/nightly/main/server/aiiface.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
aiiface.c
Go to the documentation of this file.
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.
7
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***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#ifdef AI_MODULES
19#include <ltdl.h>
20#endif
21
22/* utility */
23#include "support.h"
24
25/* common */
26#include "ai.h"
27#include "game.h"
28#include "player.h"
29
30/* ai/classic */
31#include "classicai.h"
32
33#include "aiiface.h"
34
35#ifdef AI_MOD_STATIC_THREADED
36bool fc_ai_threaded_setup(struct ai_type *ai);
37#endif
38
39#ifdef AI_MOD_STATIC_TEX
40bool fc_ai_tex_setup(struct ai_type *ai);
41#endif
42
43#ifdef AI_MOD_STATIC_STUB
44bool fc_ai_stub_setup(struct ai_type *ai);
45#endif
46
47 static struct ai_type *default_ai = NULL;
48
49#ifdef AI_MODULES
50/**********************************************************************/
53static const char *fc_module_error(void)
54{
55 static char def_err[] = "Unknown error";
56 const char *errtxt = lt_dlerror();
57
58 if (errtxt == NULL) {
59 return def_err;
60 }
61
62 return errtxt;
63}
64
65/**********************************************************************/
68bool load_ai_module(const char *modname)
69{
70 struct ai_type *ai = ai_type_alloc();
71 bool setup_success;
72 lt_dlhandle handle;
73 bool (*setup_func)(struct ai_type *ai);
74 const char *(*capstr_func)(void);
75 const char *capstr;
76 char buffer[2048];
77 char filename[1024];
78
79 if (ai == NULL) {
80 return FALSE;
81 }
82
83 init_ai(ai);
84
85 fc_snprintf(filename, sizeof(filename), "fc_ai_%s", modname);
86 fc_snprintf(buffer, sizeof(buffer), "%s", filename);
87 handle = lt_dlopenext(buffer);
88 if (handle == NULL) {
89 log_error(_("Cannot open AI module %s (%s)"), filename, fc_module_error());
90 return FALSE;
91 }
92
93 fc_snprintf(buffer, sizeof(buffer), "%s_capstr", filename);
94 capstr_func = lt_dlsym(handle, buffer);
95 if (capstr_func == NULL) {
96 log_error(_("Cannot find capstr function from ai module %s (%s)"),
97 filename, fc_module_error());
98 return FALSE;
99 }
100
101 capstr = capstr_func();
102 if (strcmp(FC_AI_MOD_CAPSTR, capstr)) {
103 log_error(_("Incompatible ai module %s:"), filename);
104 log_error(_(" Module options: %s"), capstr);
105 log_error(_(" Supported options: %s"), FC_AI_MOD_CAPSTR);
106
107 return FALSE;
108 }
109
110 fc_snprintf(buffer, sizeof(buffer), "%s_setup", filename);
111 setup_func = lt_dlsym(handle, buffer);
112 if (setup_func == NULL) {
113 log_error(_("Cannot find setup function from ai module %s (%s)"),
114 filename, fc_module_error());
115 return FALSE;
116 }
117 setup_success = setup_func(ai);
118
119 if (!setup_success) {
120 log_error(_("Setup of ai module %s failed."), filename);
121 return FALSE;
122 }
123
124 return TRUE;
125}
126#endif /* AI_MODULES */
127
128/**********************************************************************/
131 void ai_init(void)
132{
133 bool failure = FALSE;
134#if !defined(AI_MODULES) || defined(AI_MOD_STATIC_CLASSIC) || defined(AI_MOD_STATIC_THREADED) || defined(AI_MOD_STATIC_TEX) || defined(AI_MOD_STATIC_STUB)
135 /* First !defined(AI_MODULES) case is for default ai support. */
136 struct ai_type *ai;
137#endif
138
139#ifdef AI_MODULES
140 if (lt_dlinit()) {
141 failure = TRUE;
142 }
143 if (!failure) {
144
145#ifdef FREECIV_DEBUG
146 /* First search ai modules under directory ai/<module> under
147 current directory. This allows us to run freeciv without
148 installing it. */
149 const char *moduledirs[] = { "classic", "threaded", "tex", "stub", NULL };
150 int i;
151
152 for (i = 0; moduledirs[i] != NULL ; i++) {
153 char buf[2048];
154
155 fc_snprintf(buf, sizeof(buf), "ai/%s", moduledirs[i]);
156 lt_dladdsearchdir(buf);
157 }
158#endif /* FREECIV_DEBUG */
159
160 /* Then search ai modules from their installation directory. */
161 lt_dladdsearchdir(AI_MODULEDIR);
162 }
163#endif /* AI_MODULES */
164
165#ifdef AI_MOD_STATIC_CLASSIC
166 ai = ai_type_alloc();
167 if (ai != NULL) {
168 init_ai(ai);
169 if (!fc_ai_classic_setup(ai)) {
170 log_error(_("Failed to setup \"%s\" AI module"), "classic");
171 ai_type_dealloc();
172 }
173 }
174#endif /* AI_MOD_STATIC_CLASSIC */
175
176#ifdef AI_MOD_STATIC_THREADED
177 ai = ai_type_alloc();
178 if (ai != NULL) {
179 init_ai(ai);
180 if (!fc_ai_threaded_setup(ai)) {
181 log_error(_("Failed to setup \"%s\" AI module"), "threaded");
182 ai_type_dealloc();
183 }
184 }
185#endif /* AI_MOD_STATIC_THREADED */
186
187#ifdef AI_MOD_STATIC_TEX
188 ai = ai_type_alloc();
189 if (ai != NULL) {
190 init_ai(ai);
191 if (!fc_ai_tex_setup(ai)) {
192 log_error(_("Failed to setup \"%s\" AI module"), "tex");
193 ai_type_dealloc();
194 }
195 }
196#endif /* AI_MOD_STATIC_TEX */
197
198#ifdef AI_MOD_STATIC_STUB
199 ai = ai_type_alloc();
200 if (ai != NULL) {
201 init_ai(ai);
202 if (!fc_ai_stub_setup(ai)) {
203 log_error(_("Failed to setup \"%s\" AI module"), "stub");
204 ai_type_dealloc();
205 }
206 }
207#endif /* AI_MOD_STATIC_STUB */
208
209 set_default_ai_type_name(AI_MOD_DEFAULT);
210#ifdef AI_MODULES
211 if (default_ai == NULL) {
212 /* Wasn't among statically linked. Try to load dynamic module. */
213 if (!failure && !load_ai_module(AI_MOD_DEFAULT)) {
214 failure = TRUE;
215 }
216 if (!failure) {
217 set_default_ai_type_name(AI_MOD_DEFAULT);
218 }
219 }
220#endif /* AI_MODULES */
221 if (default_ai == NULL || failure) {
222 log_error(_("Failed to setup default AI module \"%s\", cannot continue."),
223 AI_MOD_DEFAULT);
224 exit(EXIT_FAILURE);
225 }
226
227 fc_snprintf(game.server.default_ai_type_name,
228 sizeof(game.server.default_ai_type_name),
229 "%s", default_ai_type_name());
230}
231
232/**********************************************************************/
235 void call_incident(enum incident_type type, enum casus_belli_range scope,
236 const struct action *paction,
237 struct player *violator, struct player *victim)
238{
239 if (scope == CBR_VICTIM_ONLY) {
240 CALL_PLR_AI_FUNC(incident, victim,
241 type, scope, paction, victim, violator, victim);
242 } else {
243 fc_assert(scope == CBR_INTERNATIONAL_OUTRAGE);
244 players_iterate(receiver) {
245 CALL_PLR_AI_FUNC(incident, victim,
246 type, scope, paction, receiver, violator, victim);
247 }
248 } players_iterate_end;
249}
250
251/**********************************************************************/
254 void call_ai_refresh(void)
255{
256 players_iterate(pplayer) {
257 CALL_PLR_AI_FUNC(refresh, pplayer, pplayer);
258 } players_iterate_end;
259}
260
261/**********************************************************************/
264 const char *default_ai_type_name(void)
265{
266 return default_ai->name;
267}
268
269/**********************************************************************/
273 bool set_default_ai_type_name(const char *name)
274{
275 struct ai_type *new_type = ai_type_by_name(name);
276
277 if (new_type != NULL) {
278 default_ai = new_type;
279
280 return TRUE;
281 }
282
283 return FALSE;
284}
init_ai
void init_ai(struct ai_type *ai)
Definition ai.c:270
ai_type_alloc
struct ai_type * ai_type_alloc(void)
Definition ai.c:304
ai_type_by_name
struct ai_type * ai_type_by_name(const char *search)
Definition ai.c:290
ai_type_dealloc
void ai_type_dealloc(void)
Definition ai.c:319
ai.h
FC_AI_MOD_CAPSTR
#define FC_AI_MOD_CAPSTR
Definition ai.h:27
incident_type
incident_type
Definition ai.h:45
CALL_PLR_AI_FUNC
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:377
call_incident
void call_incident(enum incident_type type, enum casus_belli_range scope, const struct action *paction, struct player *violator, struct player *victim)
Definition aiiface.c:235
set_default_ai_type_name
bool set_default_ai_type_name(const char *name)
Definition aiiface.c:273
call_ai_refresh
void call_ai_refresh(void)
Definition aiiface.c:254
ai_init
void ai_init(void)
Definition aiiface.c:131
default_ai
static struct ai_type * default_ai
Definition aiiface.c:47
default_ai_type_name
const char * default_ai_type_name(void)
Definition aiiface.c:264
aiiface.h
load_ai_module
bool load_ai_module(const char *modname)
fc_ai_classic_setup
bool fc_ai_classic_setup(struct ai_type *ai)
Definition classicai.c:595
classicai.h
incite_cost
char * incite_cost
Definition comments.c:74
_
#define _(String)
Definition fcintl.h:67
game
struct civ_game game
Definition game.c:61
game.h
type
GType type
Definition repodlgs.c:1313
name
const char * name
Definition inputfile.c:127
fc_assert
#define fc_assert(condition)
Definition log.h:176
log_error
#define log_error(message,...)
Definition log.h:103
player.h
players_iterate_end
#define players_iterate_end
Definition player.h:539
players_iterate
#define players_iterate(_pplayer)
Definition player.h:534
action
Definition actions.h:106
ai_type
Definition ai.h:50
ai_type::refresh
void(* refresh)(struct player *pplayer)
Definition ai.h:302
ai_type::incident
void(* incident)(enum incident_type type, enum casus_belli_range scope, const struct action *paction, struct player *receiver, struct player *violator, struct player *victim)
Definition ai.h:276
ai_type::name
char name[MAX_LEN_NAME]
Definition ai.h:51
civ_game::default_ai_type_name
char default_ai_type_name[256]
Definition game.h:278
civ_game::server
struct civ_game::@31::@35 server
player
Definition player.h:249
fc_ai_stub_setup
bool fc_ai_stub_setup(struct ai_type *ai)
Definition stubai.c:44
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
support.h
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
bool
#define bool
Definition support.h:71
fc_ai_tex_setup
bool fc_ai_tex_setup(struct ai_type *ai)
Definition texai.c:589