Freeciv-3.2: /fast/freeciv/nightly/S3_2/server/aiiface.c Source File

Freeciv-3.2

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 "player.h"
28
29/* server/advisors */
30#include "autosettlers.h"
31
32/* ai/classic */
33#include "classicai.h"
34
35#include "aiiface.h"
36
37#ifdef AI_MOD_STATIC_THREADED
38bool fc_ai_threaded_setup(struct ai_type *ai);
39#endif
40
41#ifdef AI_MOD_STATIC_TEX
42bool fc_ai_tex_setup(struct ai_type *ai);
43#endif
44
45#ifdef AI_MOD_STATIC_STUB
46bool fc_ai_stub_setup(struct ai_type *ai);
47#endif
48
49 static struct ai_type *default_ai = NULL;
50
51#ifdef AI_MODULES
52/**********************************************************************/
55static const char *fc_module_error(void)
56{
57 static char def_err[] = "Unknown error";
58 const char *errtxt = lt_dlerror();
59
60 if (errtxt == NULL) {
61 return def_err;
62 }
63
64 return errtxt;
65}
66
67/**********************************************************************/
70bool load_ai_module(const char *modname)
71{
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];
80
81 if (ai == NULL) {
82 return FALSE;
83 }
84
85 init_ai(ai);
86
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;
93 }
94
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;
101 }
102
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);
108
109 return FALSE;
110 }
111
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;
118 }
119 setup_success = setup_func(ai);
120
121 if (!setup_success) {
122 log_error(_("Setup of ai module %s failed."), filename);
123 return FALSE;
124 }
125
126 return TRUE;
127}
128#endif /* AI_MODULES */
129
130/**********************************************************************/
133 void ai_init(void)
134{
135 bool failure = FALSE;
136#if !defined(AI_MODULES) || defined(AI_MOD_STATIC_CLASSIC) || defined(AI_MOD_STATIC_THREADED) || defined(AI_MOD_STATIC_TEX) || defined(AI_MOD_STATIC_STUB)
137 /* First !defined(AI_MODULES) case is for default ai support. */
138 struct ai_type *ai;
139#endif
140
141#ifdef AI_MODULES
142 if (lt_dlinit()) {
143 failure = TRUE;
144 }
145 if (!failure) {
146
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", "tex", "stub", NULL };
152 int i;
153
154 for (i = 0; moduledirs[i] != NULL ; i++) {
155 char buf[2048];
156
157 fc_snprintf(buf, sizeof(buf), "ai/%s", moduledirs[i]);
158 lt_dladdsearchdir(buf);
159 }
160#endif /* FREECIV_DEBUG */
161
162 /* Then search ai modules from their installation directory. */
163 lt_dladdsearchdir(AI_MODULEDIR);
164 }
165#endif /* AI_MODULES */
166
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();
174 }
175 }
176#endif /* AI_MOD_STATIC_CLASSIC */
177
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();
185 }
186 }
187#endif /* AI_MOD_STATIC_THREADED */
188
189#ifdef AI_MOD_STATIC_TEX
190 ai = ai_type_alloc();
191 if (ai != NULL) {
192 init_ai(ai);
193 if (!fc_ai_tex_setup(ai)) {
194 log_error(_("Failed to setup \"%s\" AI module"), "tex");
195 ai_type_dealloc();
196 }
197 }
198#endif /* AI_MOD_STATIC_TEX */
199
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();
207 }
208 }
209#endif /* AI_MOD_STATIC_STUB */
210
211 set_default_ai_type_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;
217 }
218 if (!failure) {
219 set_default_ai_type_name(AI_MOD_DEFAULT);
220 }
221 }
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);
227 }
228
229 fc_snprintf(game.server.default_ai_type_name,
230 sizeof(game.server.default_ai_type_name),
231 "%s", default_ai_type_name());
232}
233
234/**********************************************************************/
237 void call_incident(enum incident_type type, enum casus_belli_range scope,
238 const struct action *paction,
239 struct player *violator, struct player *victim)
240{
241 if (scope == CBR_VICTIM_ONLY) {
242 CALL_PLR_AI_FUNC(incident, victim,
243 type, scope, paction, victim, violator, victim);
244 } else {
245 fc_assert(scope == CBR_INTERNATIONAL_OUTRAGE);
246 players_iterate(receiver) {
247 CALL_PLR_AI_FUNC(incident, victim,
248 type, scope, paction, receiver, violator, victim);
249 }
250 } players_iterate_end;
251}
252
253/**********************************************************************/
256 void call_ai_refresh(void)
257{
258 players_iterate(pplayer) {
259 CALL_PLR_AI_FUNC(refresh, pplayer, pplayer);
260 } players_iterate_end;
261}
262
263/**********************************************************************/
266 const char *default_ai_type_name(void)
267{
268 return default_ai->name;
269}
270
271/**********************************************************************/
275 bool set_default_ai_type_name(const char *name)
276{
277 struct ai_type *new_type = ai_type_by_name(name);
278
279 if (new_type != NULL) {
280 default_ai = new_type;
281
282 return TRUE;
283 }
284
285 return FALSE;
286}
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:374
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:237
set_default_ai_type_name
bool set_default_ai_type_name(const char *name)
Definition aiiface.c:275
call_ai_refresh
void call_ai_refresh(void)
Definition aiiface.c:256
ai_init
void ai_init(void)
Definition aiiface.c:133
default_ai
static struct ai_type * default_ai
Definition aiiface.c:49
default_ai_type_name
const char * default_ai_type_name(void)
Definition aiiface.c:266
aiiface.h
load_ai_module
bool load_ai_module(const char *modname)
autosettlers.h
fc_ai_classic_setup
bool fc_ai_classic_setup(struct ai_type *ai)
Definition classicai.c:584
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:62
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:537
players_iterate
#define players_iterate(_pplayer)
Definition player.h:532
action
Definition actions.h:360
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:277
civ_game::server
struct civ_game::@31::@35 server
player
Definition player.h:247
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:974
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:579