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