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
47static 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;
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
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 }
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/**********************************************************************/
131void 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]);
157 }
158#endif /* FREECIV_DEBUG */
159
160 /* Then search ai modules from their installation directory. */
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");
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");
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");
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");
205 }
206 }
207#endif /* AI_MOD_STATIC_STUB */
208
210#ifdef AI_MODULES
211 if (default_ai == NULL) {
212 /* Wasn't among statically linked. Try to load dynamic module. */
214 failure = TRUE;
215 }
216 if (!failure) {
218 }
219 }
220#endif /* AI_MODULES */
221 if (default_ai == NULL || failure) {
222 log_error(_("Failed to setup default AI module \"%s\", cannot continue."),
225 }
226
229 "%s", default_ai_type_name());
230}
231
232/**********************************************************************/
236 const struct action *paction,
237 struct player *violator, struct player *victim)
238{
239 if (scope == CBR_VICTIM_ONLY) {
242 } else {
244 players_iterate(receiver) {
246 type, scope, paction, receiver, violator, victim);
247 }
249}
250
251/**********************************************************************/
255{
256 players_iterate(pplayer) {
257 CALL_PLR_AI_FUNC(refresh, pplayer, pplayer);
259}
260
261/**********************************************************************/
264const char *default_ai_type_name(void)
265{
266 return default_ai->name;
267}
268
269/**********************************************************************/
274{
276
277 if (new_type != NULL) {
279
280 return TRUE;
281 }
282
283 return FALSE;
284}
void init_ai(struct ai_type *ai)
Definition ai.c:270
struct ai_type * ai_type_alloc(void)
Definition ai.c:304
struct ai_type * ai_type_by_name(const char *search)
Definition ai.c:290
void ai_type_dealloc(void)
Definition ai.c:319
#define FC_AI_MOD_CAPSTR
Definition ai.h:27
incident_type
Definition ai.h:45
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:377
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
bool set_default_ai_type_name(const char *name)
Definition aiiface.c:273
void call_ai_refresh(void)
Definition aiiface.c:254
void ai_init(void)
Definition aiiface.c:131
static struct ai_type * default_ai
Definition aiiface.c:47
const char * default_ai_type_name(void)
Definition aiiface.c:264
bool load_ai_module(const char *modname)
bool fc_ai_classic_setup(struct ai_type *ai)
Definition classicai.c:595
char * incite_cost
Definition comments.c:76
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:61
GType type
Definition repodlgs.c:1313
const char * name
Definition inputfile.c:127
#define fc_assert(condition)
Definition log.h:177
#define log_error(message,...)
Definition log.h:104
#define players_iterate_end
Definition player.h:542
#define players_iterate(_pplayer)
Definition player.h:537
Definition ai.h:50
void(* refresh)(struct player *pplayer)
Definition ai.h:302
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
char name[MAX_LEN_NAME]
Definition ai.h:51
char default_ai_type_name[256]
Definition game.h:285
struct civ_game::@32::@36 server
bool fc_ai_stub_setup(struct ai_type *ai)
Definition stubai.c:44
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define bool
Definition support.h:71
bool fc_ai_tex_setup(struct ai_type *ai)
Definition texai.c:589