Freeciv21
Develop your civilization from humble roots to a global empire
aiplayer.cpp
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 
14 // common
15 #include "ai.h"
16 #include "city.h"
17 #include "game.h"
18 #include "multipliers.h"
19 #include "registry_ini.h"
20 
21 // server
22 #include "citytools.h"
23 
24 /* ai/default */
25 #include "aidata.h"
26 #include "daimilitary.h"
27 
28 #include "aiplayer.h"
29 
34 void dai_player_alloc(struct ai_type *ait, struct player *pplayer)
35 {
36  struct ai_plr *player_data = new ai_plr{};
37 
38  player_set_ai_data(pplayer, ait, player_data);
39 
40  dai_data_init(ait, pplayer);
41 }
42 
46 void dai_player_free(struct ai_type *ait, struct player *pplayer)
47 {
48  struct ai_plr *player_data = def_ai_player_data(pplayer, ait);
49 
50  dai_data_close(ait, pplayer);
51 
52  if (player_data != nullptr) {
53  player_set_ai_data(pplayer, ait, nullptr);
54  delete player_data;
55  player_data = nullptr;
56  }
57 }
58 
62 void dai_player_save_relations(struct ai_type *ait, const char *aitstr,
63  struct player *pplayer, struct player *other,
64  struct section_file *file, int plrno)
65 {
66  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, other);
67  char buf[32];
68 
69  fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
70  player_index(other));
71 
72  secfile_insert_int(file, adip->spam, "%s.spam", buf);
73  secfile_insert_int(file, adip->countdown, "%s.countdown", buf);
74  secfile_insert_int(file, adip->war_reason, "%s.war_reason", buf);
75  secfile_insert_int(file, adip->ally_patience, "%s.patience", buf);
76  secfile_insert_int(file, adip->warned_about_space, "%s.warn_space", buf);
77  secfile_insert_int(file, adip->asked_about_peace, "%s.ask_peace", buf);
78  secfile_insert_int(file, adip->asked_about_alliance, "%s.ask_alliance",
79  buf);
80  secfile_insert_int(file, adip->asked_about_ceasefire, "%s.ask_ceasefire",
81  buf);
82 }
83 
87 void dai_player_load_relations(struct ai_type *ait, const char *aitstr,
88  struct player *pplayer, struct player *other,
89  const struct section_file *file, int plrno)
90 {
91  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, other);
92  char buf[32];
93 
94  fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
95  player_index(other));
96 
97  adip->spam = secfile_lookup_int_default(file, 0, "%s.spam", buf);
98  adip->countdown =
99  secfile_lookup_int_default(file, -1, "%s.countdown", buf);
100  adip->war_reason = static_cast<war_reason>(
101  secfile_lookup_int_default(file, 0, "%s.war_reason", buf));
102  adip->ally_patience =
103  secfile_lookup_int_default(file, 0, "%s.patience", buf);
104  adip->warned_about_space =
105  secfile_lookup_int_default(file, 0, "%s.warn_space", buf);
106  adip->asked_about_peace =
107  secfile_lookup_int_default(file, 0, "%s.ask_peace", buf);
108  adip->asked_about_alliance =
109  secfile_lookup_int_default(file, 0, "%s.ask_alliance", buf);
110  adip->asked_about_ceasefire =
111  secfile_lookup_int_default(file, 0, "%s.ask_ceasefire", buf);
112 }
113 
117 void dai_player_copy(struct ai_type *ait, struct player *original,
118  struct player *created)
119 {
120  bool close_original;
121  bool close_created;
122  struct ai_plr *orig_data =
123  dai_plr_data_get(ait, original, &close_original);
124  struct ai_plr *created_data =
125  dai_plr_data_get(ait, created, &close_created);
126 
128  {
129  created_data->tech_want[i] = orig_data->tech_want[i];
130  }
132 
133  if (close_original) {
134  dai_data_phase_finished(ait, original);
135  }
136  if (close_created) {
137  dai_data_phase_finished(ait, created);
138  }
139 }
140 
144 void dai_gained_control(struct ai_type *ait, struct player *pplayer)
145 {
146  if (pplayer->ai_common.skill_level != AI_LEVEL_AWAY) {
147  multipliers_iterate(pmul)
148  {
149  pplayer->multipliers_target[multiplier_index(pmul)] = pmul->def;
150  }
152 
153  // Clear worker tasks, some AIs (e.g. classic) does not use those
154  city_list_iterate(pplayer->cities, pcity) { clear_worker_tasks(pcity); }
156  }
157 
158  dai_assess_danger_player(ait, pplayer, &(wld.map));
159 }
struct ai_dip_intel * dai_diplomacy_get(struct ai_type *ait, const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: aidata.cpp:397
void dai_data_init(struct ai_type *ait, struct player *pplayer)
Initialize ai data structure.
Definition: aidata.cpp:50
void dai_data_phase_finished(struct ai_type *ait, struct player *pplayer)
Clean up ai data after phase finished.
Definition: aidata.cpp:281
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Get current default ai data related to player.
Definition: aidata.cpp:304
void dai_data_close(struct ai_type *ait, struct player *pplayer)
Deinitialize ai data structure.
Definition: aidata.cpp:98
void dai_player_alloc(struct ai_type *ait, struct player *pplayer)
Initialize player for use with default AI.
Definition: aiplayer.cpp:34
void dai_player_copy(struct ai_type *ait, struct player *original, struct player *created)
Copy default ai data from player to player.
Definition: aiplayer.cpp:117
void dai_player_save_relations(struct ai_type *ait, const char *aitstr, struct player *pplayer, struct player *other, struct section_file *file, int plrno)
Store player specific data to savegame.
Definition: aiplayer.cpp:62
void dai_player_free(struct ai_type *ait, struct player *pplayer)
Free player from use with default AI.
Definition: aiplayer.cpp:46
void dai_gained_control(struct ai_type *ait, struct player *pplayer)
Ai got control of the player.
Definition: aiplayer.cpp:144
void dai_player_load_relations(struct ai_type *ait, const char *aitstr, struct player *pplayer, struct player *other, const struct section_file *file, int plrno)
Load player vs player specific data from savegame.
Definition: aiplayer.cpp:87
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition: aiplayer.h:47
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define city_list_iterate_end
Definition: city.h:484
void clear_worker_tasks(struct city *pcity)
Clear all worker tasks from the city and inform owner.
Definition: citytools.cpp:3423
void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer, const struct civ_map *dmap)
Call assess_danger() for all cities owned by pplayer.
struct world wld
Definition: game.cpp:48
Multiplier_type_id multiplier_index(const struct multiplier *pmul)
Returns multiplier index.
Definition: multipliers.cpp:77
#define multipliers_iterate(_mul_)
Definition: multipliers.h:45
#define multipliers_iterate_end
Definition: multipliers.h:51
void player_set_ai_data(struct player *pplayer, const struct ai_type *ai, void *data)
Attach ai data to player.
Definition: player.cpp:1846
int player_index(const struct player *pplayer)
Return the player index.
Definition: player.cpp:748
int secfile_lookup_int_default(const struct section_file *secfile, int def, const char *path,...)
Lookup a integer value in the secfile.
#define secfile_insert_int(secfile, value, path,...)
Definition: registry_ini.h:116
signed char asked_about_alliance
Definition: aidata.h:58
enum war_reason war_reason
Definition: aidata.h:55
signed char spam
Definition: aidata.h:52
signed char asked_about_peace
Definition: aidata.h:57
signed char warned_about_space
Definition: aidata.h:60
int countdown
Definition: aidata.h:54
signed char ally_patience
Definition: aidata.h:56
signed char asked_about_ceasefire
Definition: aidata.h:59
Definition: aidata.h:63
adv_want tech_want[A_LAST+1]
Definition: aidata.h:95
Definition: ai.h:42
enum ai_level skill_level
Definition: player.h:109
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
struct player_ai ai_common
Definition: player.h:270
int multipliers_target[MAX_NUM_MULTIPLIERS]
Definition: player.h:298
struct civ_map map
Definition: world_object.h:21
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
Definition: support.cpp:537
#define advance_index_iterate_end
Definition: tech.h:226
#define A_NONE
Definition: tech.h:36
#define advance_index_iterate(_start, _index)
Definition: tech.h:221