Freeciv-3.2
Loading...
Searching...
No Matches
ai.h
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#ifndef FC__AI_H
14#define FC__AI_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20/* common */
21#include "fc_types.h" /* MAX_LEN_NAME */
22
23/* Update this capability string when ever there is changes to ai_type
24 * structure below. When changing mandatory capability part, check that
25 * there's enough reserved_xx pointers in the end of the structure for
26 * taking to use without need to bump mandatory capability again. */
27#define FC_AI_MOD_CAPSTR "+Freeciv-3.2-ai-module-2021.Mar.01"
28
29/* Timers for all AI activities. Define it to get statistics about the AI. */
30#ifdef FREECIV_DEBUG
31# undef DEBUG_AITIMERS
32#endif /* FREECIV_DEBUG */
33
34struct Treaty;
35struct player;
36struct adv_choice;
37struct city;
38struct unit;
39struct tile;
40struct settlermap;
41struct pf_path;
42struct section_file;
43struct adv_data;
44
48
49struct ai_type
50{
52
53 void *private;
54
55 struct {
56 /* Called for every AI type when server quits. */
58
59 /* Called for every AI type when game starts. Game is not necessarily new one,
60 it can also be an old game loaded from a savegame. */
62
63 /* Called for every AI type when game has ended. */
65
66 /* Called for every AI type when map tiles are allocated. */
68
69 /* Called for every AI type when the game map is ready, either generated
70 * or, in case of scenario, loaded. */
72
73 /* Called for every AI type when map tiles are freed. */
75
76 /* Called for every AI type when new player is added to game. */
77 void (*player_alloc)(struct player *pplayer);
78
79 /* Called for every AI type when player is freed from game. */
80 void (*player_free)(struct player *pplayer);
81
82 /* Called for every AI type for each player in game when game saved. */
83 void (*player_save)(struct player *pplayer, struct section_file *file,
84 int plrno);
85
86 /* Called for every AI type for each player in game when game loaded. */
87 void (*player_load)(struct player *pplayer, const struct section_file *file,
88 int plrno);
89
90 /* Called for every AI type for each player in game when game saved,
91 * with each other player as parameter.
92 * In practice it's good to use player_save_relations when you
93 * want to add entries to "player%d.ai%d", but player_iterate() inside
94 * player_save is better otherwise. The difference is in how clean
95 * structure the produced savegame will have. */
96 void (*player_save_relations)(struct player *pplayer, struct player *other,
97 struct section_file *file, int plrno);
98
99 /* Called for every AI type for each player in game when game loaded,
100 * with each other player as parameter. */
101 void (*player_load_relations)(struct player *pplayer, struct player *other,
102 const struct section_file *file, int plrno);
103
104 /* AI console. */
105 void (*player_console)(struct player *pplayer, const char *cmd);
106
107 /* Called for AI type that gains control of player. */
108 void (*gained_control)(struct player *pplayer);
109
110 /* Called for AI type that loses control of player. */
111 void (*lost_control)(struct player *pplayer);
112
113 /* Called for AI type of the player who gets split to two. */
114 void (*split_by_civil_war)(struct player *original, struct player *created);
115
116 /* Called for AI type of the player who got created from the split. */
117 void (*created_by_civil_war)(struct player *original, struct player *created);
118
119 /* Called for player AI type when player phase begins. This is in the
120 * beginning of phase setup. See also first_activities. */
121 void (*phase_begin)(struct player *pplayer, bool new_phase);
122
123 /* Called for player AI type when player phase ends. */
124 void (*phase_finished)(struct player *pplayer);
125
126 /* Called for every AI type when new city is added to game. Called even for
127 * virtual cities. */
128 void (*city_alloc)(struct city *pcity);
129
130 /* Called for every AI type when new city is removed from game. Called even for
131 * virtual cities. */
132 void (*city_free)(struct city *pcity);
133
134 /* Called for every AI type when new city is added to game. Called for real cities
135 * only. */
136 void (*city_created)(struct city *pcity);
137
138 /* Called for every AI type when new city is removed from game. Called for real
139 * cities only. */
140 void (*city_destroyed)(struct city *pcity);
141
142 /* Called for player AI type when player gains control of city. */
143 void (*city_got)(struct player *pplayer, struct city *pcity);
144
145 /* Called for player AI type when player loses control of city. */
146 void (*city_lost)(struct player *pplayer, struct city *pcity);
147
148 /* Called for every AI type for each city in game when game saved. */
149 void (*city_save)(struct section_file *file, const struct city *pcity,
150 const char *citystr);
151
152 /* Called for every AI type for each city in game when game loaded. */
153 void (*city_load)(const struct section_file *file, struct city *pcity,
154 const char *citystr);
155
156 /* Called for player AI type when building advisor has chosen something
157 * to be built in a city. This can override that decision. */
158 void (*choose_building)(struct city *pcity, struct adv_choice *choice);
159
160 /* Called for player AI when building advisor prepares to make decisions. */
161 void (*build_adv_prepare)(struct player *pplayer, struct adv_data *adv);
162
163 /* Called for every AI type when building advisor is first initialized
164 * for the turn. */
165 void (*build_adv_init)(struct player *pplayer);
166
167 /* Called for player AI when building advisor should set wants for buildings.
168 * Without this implemented in AI type building advisor does not adjust wants
169 * at all. */
170 void (*build_adv_adjust_want)(struct player *pplayer, struct city *wonder_city);
171
172 /* Called for player AI when evaluating governments. */
173 void (*gov_value)(struct player *pplayer, struct government *gov,
174 adv_want *val, bool *override);
175
176 /* Called for every AI type when unit ruleset has been loaded. */
178
179 /* Called for every AI type before unit ruleset gets reloaded. */
181
182 /* Called for every AI type when new unit is added to game. Called even for
183 * virtual units. */
185
186 /* Called for every AI type when unit is removed from game. Called even for
187 * virtual units. */
189
190 /* Called for every AI type when new unit is added to game. Called for real
191 * units only. */
193
194 /* Called for every AI type when unit is removed from game. Called for real
195 * units only. */
197
198 /* Called for player AI type when player gains control of unit. */
199 void (*unit_got)(struct unit *punit);
200
201 /* Called for player AI type when unit changes type. */
202 void (*unit_transformed)(struct unit *punit, const struct unit_type *old_type);
203
204 /* Called for player AI type when player loses control of unit. */
206
207 /* Called for unit owner AI type for each unit when turn ends. */
209
210 /* Called for unit owner AI type when advisors goto moves unit. */
211 void (*unit_move)(struct unit *punit, struct tile *ptile,
212 struct pf_path *path, int step);
213
214 /* Called for all AI types when ever unit has moved. */
216
217 /* Called for unit owner AI type when new advisor task is set for unit. */
218 void (*unit_task)(struct unit *punit, enum adv_unit_task task,
219 struct tile *ptile);
220
221 /* Called for every AI type for each unit in game when game saved. */
222 void (*unit_save)(struct section_file *file, const struct unit *punit,
223 const char *unitstr);
224
225 /* Called for every AI type for each unit in game when game loaded. */
226 void (*unit_load)(const struct section_file *file, struct unit *punit,
227 const char *unitstr);
228
229 /* Called for player AI type when autosettlers have been handled for the turn. */
230 void (*settler_reset)(struct player *pplayer);
231
232 /* Called for player AI type when autosettlers should find new work. */
233 void (*settler_run)(struct player *pplayer, struct unit *punit,
234 struct settlermap *state);
235
236 /* Called for player AI type for each autosettler still working.
237 Cancelling current work there will result in settler_run() call. */
238 void (*settler_cont)(struct player *pplayer, struct unit *punit,
239 struct settlermap *state);
240
241 /* Called for player AI type when unit wants to autoexplore towards a tile. */
242 void (*want_to_explore)(struct unit *punit, struct tile *target,
243 enum override_bool *allow);
244
245 /* Called for player AI type in the beginning of player phase.
246 * Unlike with phase_begin, everything is set up for phase already. */
247 void (*first_activities)(struct player *pplayer);
248
249 /* Called for player AI when player phase is already active when AI gains control. */
250 void (*restart_phase)(struct player *pplayer);
251
252 /* Called for player AI type in the beginning of player phase. Not for barbarian
253 * players. */
254 void (*diplomacy_actions)(struct player *pplayer);
255
256 /* Called for player AI type in the end of player phase. */
257 void (*last_activities)(struct player *pplayer);
258
259 /* Called for player AI type when diplomatic treaty requires evaluation. */
260 void (*treaty_evaluate)(struct player *pplayer, struct player *aplayer,
261 struct Treaty *ptreaty);
262
263 /* Called for player AI type when diplomatic treaty has been accepted
264 * by both parties. */
265 void (*treaty_accepted)(struct player *pplayer, struct player *aplayer,
266 struct Treaty *ptreaty);
267
268 /* Called for player AI type when first contact with another player has been
269 * established. Note that when contact is between two AI players, callback
270 * might be already called for the other party, so you can't assume
271 * relations to be all-pristine when this gets called. */
272 void (*first_contact)(struct player *pplayer, struct player *aplayer);
273
274 /* Called for player AI type of the victim when someone does some violation
275 * against them. */
277 const struct action *paction,
278 struct player *receiver,
279 struct player *violator, struct player *victim);
280
281 /* Called for player AI type of city owner when logging requires city debug
282 * information. */
283 void (*log_fragment_city)(char *buffer, int buflength, const struct city *pcity);
284
285 /* Called for player AI type of unit owner when logging requires unit debug
286 * information. */
287 void (*log_fragment_unit)(char *buffer, int buflength, const struct unit *punit);
288
289 /* Called for player AI type to decide if another player is dangerous. */
290 void (*consider_plr_dangerous)(struct player *plr1, struct player *plr2,
291 enum override_bool *result);
292
293 /* Called for player AI type to decide if it's dangerous for unit to enter tile. */
294 void (*consider_tile_dangerous)(struct tile *ptile, struct unit *punit,
295 enum override_bool *result);
296
297 /* Called for player AI to decide if city can be chosen to act as wonder city
298 * for building advisor. */
299 void (*consider_wonder_city)(struct city *pcity, bool *result);
300
301 /* Called for player AI type with short internval */
302 void (*refresh)(struct player *pplayer);
303
304 /* Called for every AI type when tile has changed */
305 void (*tile_info)(struct tile *ptile);
306
307 /* Called for every AI type when certain kind of city change has taken place.
308 * Currently this gets called when:
309 * - city changes owner.
310 */
311 void (*city_info)(struct city *pcity);
312
313 /* Called for every AI type when certain kind of unit change has taken place.
314 * Currently this gets called when:
315 * - unit updates & conversions
316 */
318
319 /* Called for player AI when revolution starts. */
320 void (*revolution_start)(struct player *pplayer);
321
322 /* These are here reserving space for future optional callbacks.
323 * This way we don't need to change the mandatory capability of the AI module
324 * interface when adding such callbacks, but existing modules just have these
325 * set to NULL. Optional capability should be set when taking one of these to use,
326 * so that new modules know if the server is going to call these or is it too old
327 * version to do so.
328 * When mandatory capability then changes again, please add new reservations to
329 * replace those taken to use. */
336};
337
338struct ai_type *ai_type_alloc(void);
339void ai_type_dealloc(void);
340struct ai_type *get_ai_type(int id);
341int ai_type_number(const struct ai_type *ai);
342void init_ai(struct ai_type *ai);
343int ai_type_get_count(void);
344const char *ai_name(const struct ai_type *ai);
345
346struct ai_type *ai_type_by_name(const char *search);
347const char *ai_type_name_or_fallback(const char *orig_name);
348
349#ifdef DEBUG_AITIMERS
350void ai_timer_init(void);
351void ai_timer_free(void);
352void ai_timer_start(const struct ai_type *ai);
353void ai_timer_stop(const struct ai_type *ai);
354void ai_timer_player_start(const struct player *pplayer);
355void ai_timer_player_stop(const struct player *pplayer);
356#else
357#define ai_timer_init(...) (void) 0
358#define ai_timer_free(...) (void) 0
359#define ai_timer_start(...) (void) 0
360#define ai_timer_stop(...) (void) 0
361#define ai_timer_player_start(...) (void) 0
362#define ai_timer_player_stop(...) (void) 0
363#endif /* DEBUG_AITIMERS */
364
365#define ai_type_iterate(NAME_ai) \
366 do { \
367 int _aii_; \
368 int _aitotal_ = ai_type_get_count(); \
369 for (_aii_ = 0; _aii_ < _aitotal_ ; _aii_++) { \
370 struct ai_type *NAME_ai = get_ai_type(_aii_);
371
372#define ai_type_iterate_end \
373 } \
374 } while (FALSE);
375
376/* FIXME: This should also check if player is ai controlled */
377#define CALL_PLR_AI_FUNC(_func, _player, ...) \
378 do { \
379 struct player *_plr_ = _player; /* _player expanded just once */ \
380 if (_plr_ && _plr_->ai && _plr_->ai->funcs._func) { \
381 ai_timer_player_start(_plr_); \
382 _plr_->ai->funcs._func( __VA_ARGS__ ); \
383 ai_timer_player_stop(_plr_); \
384 } \
385 } while (FALSE)
386
387#define CALL_FUNC_EACH_AI(_func, ...) \
388 do { \
389 ai_type_iterate(_ait_) { \
390 if (_ait_->funcs._func) { \
391 ai_timer_start(_ait_); \
392 _ait_->funcs._func( __VA_ARGS__ ); \
393 ai_timer_stop(_ait_); \
394 } \
395 } ai_type_iterate_end; \
396 } while (FALSE)
397
398#ifdef __cplusplus
399}
400#endif /* __cplusplus */
401
402#endif /* FC__AI_H */
#define ai_timer_start(...)
Definition ai.h:359
void init_ai(struct ai_type *ai)
Definition ai.c:270
#define ai_timer_stop(...)
Definition ai.h:360
#define ai_timer_free(...)
Definition ai.h:358
const char * ai_name(const struct ai_type *ai)
Definition ai.c:335
#define ai_timer_player_start(...)
Definition ai.h:361
#define ai_timer_init(...)
Definition ai.h:357
struct ai_type * ai_type_alloc(void)
Definition ai.c:304
const char * ai_type_name_or_fallback(const char *orig_name)
Definition ai.c:346
incident_type
Definition ai.h:45
@ INCIDENT_LAST
Definition ai.h:46
@ INCIDENT_WAR
Definition ai.h:46
@ INCIDENT_ACTION
Definition ai.h:46
struct ai_type * ai_type_by_name(const char *search)
Definition ai.c:290
int ai_type_get_count(void)
Definition ai.c:327
void ai_type_dealloc(void)
Definition ai.c:319
struct ai_type * get_ai_type(int id)
Definition ai.c:260
#define ai_timer_player_stop(...)
Definition ai.h:362
int ai_type_number(const struct ai_type *ai)
Definition ai.c:278
char * incite_cost
Definition comments.c:75
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct Treaty * ptreaty
Definition diplodlg_g.h:28
float adv_want
Definition fc_types.h:1354
adv_unit_task
Definition fc_types.h:370
#define MAX_LEN_NAME
Definition fc_types.h:66
override_bool
Definition fc_types.h:94
GType type
Definition repodlgs.c:1313
int step
Definition specpq.h:92
Definition ai.h:50
void(* revolution_start)(struct player *pplayer)
Definition ai.h:320
void(* log_fragment_unit)(char *buffer, int buflength, const struct unit *punit)
Definition ai.h:287
void(* reserved_02)(void)
Definition ai.h:331
void(* city_alloc)(struct city *pcity)
Definition ai.h:128
void(* created_by_civil_war)(struct player *original, struct player *created)
Definition ai.h:117
void(* consider_tile_dangerous)(struct tile *ptile, struct unit *punit, enum override_bool *result)
Definition ai.h:294
void(* unit_lost)(struct unit *punit)
Definition ai.h:205
void(* treaty_evaluate)(struct player *pplayer, struct player *aplayer, struct Treaty *ptreaty)
Definition ai.h:260
void(* gov_value)(struct player *pplayer, struct government *gov, adv_want *val, bool *override)
Definition ai.h:173
void(* treaty_accepted)(struct player *pplayer, struct player *aplayer, struct Treaty *ptreaty)
Definition ai.h:265
void(* unit_transformed)(struct unit *punit, const struct unit_type *old_type)
Definition ai.h:202
void(* log_fragment_city)(char *buffer, int buflength, const struct city *pcity)
Definition ai.h:283
void(* settler_run)(struct player *pplayer, struct unit *punit, struct settlermap *state)
Definition ai.h:233
void(* unit_load)(const struct section_file *file, struct unit *punit, const char *unitstr)
Definition ai.h:226
void(* unit_move)(struct unit *punit, struct tile *ptile, struct pf_path *path, int step)
Definition ai.h:211
void(* reserved_03)(void)
Definition ai.h:332
void(* unit_free)(struct unit *punit)
Definition ai.h:188
void(* map_alloc)(void)
Definition ai.h:67
void(* phase_finished)(struct player *pplayer)
Definition ai.h:124
void(* tile_info)(struct tile *ptile)
Definition ai.h:305
void(* restart_phase)(struct player *pplayer)
Definition ai.h:250
void(* settler_reset)(struct player *pplayer)
Definition ai.h:230
void(* unit_save)(struct section_file *file, const struct unit *punit, const char *unitstr)
Definition ai.h:222
void(* refresh)(struct player *pplayer)
Definition ai.h:302
void(* game_free)(void)
Definition ai.h:64
void(* unit_created)(struct unit *punit)
Definition ai.h:192
void(* units_ruleset_close)(void)
Definition ai.h:180
void(* city_destroyed)(struct city *pcity)
Definition ai.h:140
void(* diplomacy_actions)(struct player *pplayer)
Definition ai.h:254
void(* player_alloc)(struct player *pplayer)
Definition ai.h:77
void(* build_adv_prepare)(struct player *pplayer, struct adv_data *adv)
Definition ai.h:161
void(* build_adv_adjust_want)(struct player *pplayer, struct city *wonder_city)
Definition ai.h:170
void(* city_info)(struct city *pcity)
Definition ai.h:311
void(* player_load)(struct player *pplayer, const struct section_file *file, int plrno)
Definition ai.h:87
void(* build_adv_init)(struct player *pplayer)
Definition ai.h:165
void(* city_lost)(struct player *pplayer, struct city *pcity)
Definition ai.h:146
void(* unit_task)(struct unit *punit, enum adv_unit_task task, struct tile *ptile)
Definition ai.h:218
void(* reserved_01)(void)
Definition ai.h:330
void(* last_activities)(struct player *pplayer)
Definition ai.h:257
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
void(* settler_cont)(struct player *pplayer, struct unit *punit, struct settlermap *state)
Definition ai.h:238
void(* first_contact)(struct player *pplayer, struct player *aplayer)
Definition ai.h:272
void(* map_free)(void)
Definition ai.h:74
void(* city_created)(struct city *pcity)
Definition ai.h:136
void(* city_save)(struct section_file *file, const struct city *pcity, const char *citystr)
Definition ai.h:149
void(* consider_wonder_city)(struct city *pcity, bool *result)
Definition ai.h:299
void(* city_free)(struct city *pcity)
Definition ai.h:132
void(* choose_building)(struct city *pcity, struct adv_choice *choice)
Definition ai.h:158
struct ai_type::@14 funcs
void(* player_save_relations)(struct player *pplayer, struct player *other, struct section_file *file, int plrno)
Definition ai.h:96
void(* module_close)(void)
Definition ai.h:57
void(* city_load)(const struct section_file *file, struct city *pcity, const char *citystr)
Definition ai.h:153
void(* unit_destroyed)(struct unit *punit)
Definition ai.h:196
void(* player_load_relations)(struct player *pplayer, struct player *other, const struct section_file *file, int plrno)
Definition ai.h:101
void(* phase_begin)(struct player *pplayer, bool new_phase)
Definition ai.h:121
void(* player_free)(struct player *pplayer)
Definition ai.h:80
void(* split_by_civil_war)(struct player *original, struct player *created)
Definition ai.h:114
void(* unit_turn_end)(struct unit *punit)
Definition ai.h:208
void(* city_got)(struct player *pplayer, struct city *pcity)
Definition ai.h:143
void(* unit_alloc)(struct unit *punit)
Definition ai.h:184
void(* gained_control)(struct player *pplayer)
Definition ai.h:108
char name[MAX_LEN_NAME]
Definition ai.h:51
void(* map_ready)(void)
Definition ai.h:71
void(* unit_got)(struct unit *punit)
Definition ai.h:199
void(* player_console)(struct player *pplayer, const char *cmd)
Definition ai.h:105
void(* reserved_04)(void)
Definition ai.h:333
void(* player_save)(struct player *pplayer, struct section_file *file, int plrno)
Definition ai.h:83
void(* first_activities)(struct player *pplayer)
Definition ai.h:247
void(* consider_plr_dangerous)(struct player *plr1, struct player *plr2, enum override_bool *result)
Definition ai.h:290
void(* game_start)(void)
Definition ai.h:61
void(* lost_control)(struct player *pplayer)
Definition ai.h:111
void(* units_ruleset_init)(void)
Definition ai.h:177
void(* unit_move_seen)(struct unit *punit)
Definition ai.h:215
void(* reserved_05)(void)
Definition ai.h:334
void(* want_to_explore)(struct unit *punit, struct tile *target, enum override_bool *allow)
Definition ai.h:242
Definition city.h:320
Definition tile.h:50
Definition unit.h:138