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
49static 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;
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
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 }
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/**********************************************************************/
133void 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]);
159 }
160#endif /* FREECIV_DEBUG */
161
162 /* Then search ai modules from their installation directory. */
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");
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");
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");
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");
207 }
208 }
209#endif /* AI_MOD_STATIC_STUB */
210
212#ifdef AI_MODULES
213 if (default_ai == NULL) {
214 /* Wasn't among statically linked. Try to load dynamic module. */
216 failure = TRUE;
217 }
218 if (!failure) {
220 }
221 }
222#endif /* AI_MODULES */
223 if (default_ai == NULL || failure) {
224 log_error(_("Failed to setup default AI module \"%s\", cannot continue."),
227 }
228
231 "%s", default_ai_type_name());
232}
233
234/**********************************************************************/
238 const struct action *paction,
239 struct player *violator, struct player *victim)
240{
241 if (scope == CBR_VICTIM_ONLY) {
244 } else {
246 players_iterate(receiver) {
248 type, scope, paction, receiver, violator, victim);
249 }
251}
252
253/**********************************************************************/
257{
258 players_iterate(pplayer) {
259 CALL_PLR_AI_FUNC(refresh, pplayer, pplayer);
261}
262
263/**********************************************************************/
266const char *default_ai_type_name(void)
267{
268 return default_ai->name;
269}
270
271/**********************************************************************/
276{
278
279 if (new_type != NULL) {
281
282 return TRUE;
283 }
284
285 return FALSE;
286}
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:237
bool set_default_ai_type_name(const char *name)
Definition aiiface.c:275
void call_ai_refresh(void)
Definition aiiface.c:256
void ai_init(void)
Definition aiiface.c:133
static struct ai_type * default_ai
Definition aiiface.c:49
const char * default_ai_type_name(void)
Definition aiiface.c:266
bool load_ai_module(const char *modname)
bool fc_ai_classic_setup(struct ai_type *ai)
Definition classicai.c:594
char * incite_cost
Definition comments.c:75
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:62
GType type
Definition repodlgs.c:1313
const char * name
Definition inputfile.c:127
#define fc_assert(condition)
Definition log.h:176
#define log_error(message,...)
Definition log.h:103
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
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:281
struct civ_game::@31::@35 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:974
#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:588