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