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_TEX
38bool fc_ai_tex_setup(struct ai_type *ai);
39#endif
40
41#ifdef AI_MOD_STATIC_STUB
42bool fc_ai_stub_setup(struct ai_type *ai);
43#endif
44
45static struct ai_type *default_ai = NULL;
46
47#ifdef AI_MODULES
48/**********************************************************************/
51static const char *fc_module_error(void)
52{
53 static char def_err[] = "Unknown error";
54 const char *errtxt = lt_dlerror();
55
56 if (errtxt == NULL) {
57 return def_err;
58 }
59
60 return errtxt;
61}
62
63/**********************************************************************/
66bool load_ai_module(const char *modname)
67{
68 struct ai_type *ai = ai_type_alloc();
69 bool setup_success;
71 bool (*setup_func)(struct ai_type *ai);
72 const char *(*capstr_func)(void);
73 const char *capstr;
74 char buffer[2048];
75 char filename[1024];
76
77 if (ai == NULL) {
78 return FALSE;
79 }
80
81 init_ai(ai);
82
83 fc_snprintf(filename, sizeof(filename), "fc_ai_%s", modname);
84 fc_snprintf(buffer, sizeof(buffer), "%s", filename);
85 handle = lt_dlopenext(buffer);
86 if (handle == NULL) {
87 log_error(_("Cannot open AI module %s (%s)"), filename, fc_module_error());
88 return FALSE;
89 }
90
91 fc_snprintf(buffer, sizeof(buffer), "%s_capstr", filename);
92 capstr_func = lt_dlsym(handle, buffer);
93 if (capstr_func == NULL) {
94 log_error(_("Cannot find capstr function from ai module %s (%s)"),
95 filename, fc_module_error());
96 return FALSE;
97 }
98
101 log_error(_("Incompatible ai module %s:"), filename);
102 log_error(_(" Module options: %s"), capstr);
103 log_error(_(" Supported options: %s"), FC_AI_MOD_CAPSTR);
104
105 return FALSE;
106 }
107
108 fc_snprintf(buffer, sizeof(buffer), "%s_setup", filename);
109 setup_func = lt_dlsym(handle, buffer);
110 if (setup_func == NULL) {
111 log_error(_("Cannot find setup function from ai module %s (%s)"),
112 filename, fc_module_error());
113 return FALSE;
114 }
116
117 if (!setup_success) {
118 log_error(_("Setup of ai module %s failed."), filename);
119 return FALSE;
120 }
121
122 return TRUE;
123}
124#endif /* AI_MODULES */
125
126/**********************************************************************/
129void ai_init(void)
130{
131 bool failure = FALSE;
132#if !defined(AI_MODULES) || defined(AI_MOD_STATIC_CLASSIC) || defined(AI_MOD_STATIC_TEX) || defined(AI_MOD_STATIC_STUB)
133 /* First !defined(AI_MODULES) case is for default ai support. */
134 struct ai_type *ai;
135#endif
136
137#ifdef AI_MODULES
138 if (lt_dlinit()) {
139 failure = TRUE;
140 }
141 if (!failure) {
142
143#ifdef FREECIV_DEBUG
144 /* First search ai modules under directory ai/<module> under
145 current directory. This allows us to run freeciv without
146 installing it. */
147 const char *moduledirs[] = { "classic", "threaded", "tex", "stub", NULL };
148 int i;
149
150 for (i = 0; moduledirs[i] != NULL ; i++) {
151 char buf[2048];
152
153 fc_snprintf(buf, sizeof(buf), "ai/%s", moduledirs[i]);
155 }
156#endif /* FREECIV_DEBUG */
157
158 /* Then search ai modules from their installation directory. */
160 }
161#endif /* AI_MODULES */
162
163#ifdef AI_MOD_STATIC_CLASSIC
164 ai = ai_type_alloc();
165 if (ai != NULL) {
166 init_ai(ai);
167 if (!fc_ai_classic_setup(ai)) {
168 log_error(_("Failed to setup \"%s\" AI module"), "classic");
170 }
171 }
172#endif /* AI_MOD_STATIC_CLASSIC */
173
174#ifdef AI_MOD_STATIC_TEX
175 ai = ai_type_alloc();
176 if (ai != NULL) {
177 init_ai(ai);
178 if (!fc_ai_tex_setup(ai)) {
179 log_error(_("Failed to setup \"%s\" AI module"), "tex");
181 }
182 }
183#endif /* AI_MOD_STATIC_TEX */
184
185#ifdef AI_MOD_STATIC_STUB
186 ai = ai_type_alloc();
187 if (ai != NULL) {
188 init_ai(ai);
189 if (!fc_ai_stub_setup(ai)) {
190 log_error(_("Failed to setup \"%s\" AI module"), "stub");
192 }
193 }
194#endif /* AI_MOD_STATIC_STUB */
195
197#ifdef AI_MODULES
198 if (default_ai == NULL) {
199 /* Wasn't among statically linked. Try to load dynamic module. */
201 failure = TRUE;
202 }
203 if (!failure) {
205 }
206 }
207#endif /* AI_MODULES */
208 if (default_ai == NULL || failure) {
209 log_error(_("Failed to setup default AI module \"%s\", cannot continue."),
212 }
213
216 "%s", default_ai_type_name());
217}
218
219/**********************************************************************/
223 const struct action *paction,
224 struct player *violator, struct player *victim)
225{
226 if (scope == CBR_VICTIM_ONLY) {
229 } else {
231 players_iterate(receiver) {
233 type, scope, paction, receiver, violator, victim);
235 }
236}
237
238/**********************************************************************/
242{
243 players_iterate(pplayer) {
244 CALL_PLR_AI_FUNC(refresh, pplayer, pplayer);
246}
247
248/**********************************************************************/
251const char *default_ai_type_name(void)
252{
253 return default_ai->name;
254}
255
256/**********************************************************************/
261{
263
264 if (new_type != NULL) {
266
267 return TRUE;
268 }
269
270 return FALSE;
271}
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:222
bool set_default_ai_type_name(const char *name)
Definition aiiface.c:260
void call_ai_refresh(void)
Definition aiiface.c:241
void ai_init(void)
Definition aiiface.c:129
static struct ai_type * default_ai
Definition aiiface.c:45
const char * default_ai_type_name(void)
Definition aiiface.c:251
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