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 /* common */
19 #include "ai.h"
20 #include "city.h"
21 #include "unit.h"
22 
23 /* server */
24 #include "citytools.h"
25 
26 /* ai/default */
27 #include "aidata.h"
28 #include "daimilitary.h"
29 
30 #include "aiplayer.h"
31 
32 /**************************************************************************
33   Initialize player for use with default AI. Note that this is called
34   for all players, not just for those default AI is controlling.
35 **************************************************************************/
dai_player_alloc(struct ai_type * ait,struct player * pplayer)36 void dai_player_alloc(struct ai_type *ait, struct player *pplayer)
37 {
38   struct ai_plr *player_data = fc_calloc(1, sizeof(struct ai_plr));
39 
40   player_set_ai_data(pplayer, ait, player_data);
41 
42   dai_data_init(ait, pplayer);
43 }
44 
45 /**************************************************************************
46   Free player from use with default AI.
47 **************************************************************************/
dai_player_free(struct ai_type * ait,struct player * pplayer)48 void dai_player_free(struct ai_type *ait, struct player *pplayer)
49 {
50   struct ai_plr *player_data = def_ai_player_data(pplayer, ait);
51 
52   dai_data_close(ait, pplayer);
53 
54   if (player_data != NULL) {
55     player_set_ai_data(pplayer, ait, NULL);
56     FC_FREE(player_data);
57   }
58 }
59 
60 /**************************************************************************
61   Store player specific data to savegame
62 **************************************************************************/
dai_player_save(struct ai_type * ait,const char * aitstr,struct player * pplayer,struct section_file * file,int plrno)63 void dai_player_save(struct ai_type *ait, const char *aitstr,
64                      struct player *pplayer, struct section_file *file,
65                      int plrno)
66 {
67   players_iterate(other) {
68     dai_player_save_relations(ait, aitstr, pplayer, other, file, plrno);
69   } players_iterate_end;
70 }
71 
72 /**************************************************************************
73   Store player specific data to savegame
74 **************************************************************************/
dai_player_save_relations(struct ai_type * ait,const char * aitstr,struct player * pplayer,struct player * other,struct section_file * file,int plrno)75 void dai_player_save_relations(struct ai_type *ait, const char *aitstr,
76                                struct player *pplayer, struct player *other,
77                                struct section_file *file, int plrno)
78 {
79   struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, other);
80   char buf[32];
81 
82   fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
83               player_index(other));
84 
85   secfile_insert_int(file, adip->spam,
86                      "%s.spam", buf);
87   secfile_insert_int(file, adip->countdown,
88                      "%s.countdown", buf);
89   secfile_insert_int(file, adip->war_reason,
90                      "%s.war_reason", buf);
91   secfile_insert_int(file, adip->ally_patience,
92                      "%s.patience", buf);
93   secfile_insert_int(file, adip->warned_about_space,
94                      "%s.warn_space", buf);
95   secfile_insert_int(file, adip->asked_about_peace,
96                      "%s.ask_peace", buf);
97   secfile_insert_int(file, adip->asked_about_alliance,
98                      "%s.ask_alliance", buf);
99   secfile_insert_int(file, adip->asked_about_ceasefire,
100                      "%s.ask_ceasefire", buf);
101 }
102 
103 /**************************************************************************
104   Load player specific data from savegame
105 **************************************************************************/
dai_player_load(struct ai_type * ait,const char * aitstr,struct player * pplayer,const struct section_file * file,int plrno)106 void dai_player_load(struct ai_type *ait, const char *aitstr,
107                      struct player *pplayer,
108                      const struct section_file *file, int plrno)
109 {
110   players_iterate(other) {
111     dai_player_load_relations(ait, aitstr, pplayer, other, file, plrno);
112   } players_iterate_end;
113 }
114 
115 /**************************************************************************
116   Load player vs player specific data from savegame
117 **************************************************************************/
dai_player_load_relations(struct ai_type * ait,const char * aitstr,struct player * pplayer,struct player * other,const struct section_file * file,int plrno)118 void dai_player_load_relations(struct ai_type *ait, const char *aitstr,
119                                struct player *pplayer, struct player *other,
120                                const struct section_file *file, int plrno)
121 {
122   struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, other);
123   char buf[32];
124 
125   fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
126               player_index(other));
127 
128   adip->spam
129     = secfile_lookup_int_default(file, 0, "%s.spam", buf);
130   adip->countdown
131     = secfile_lookup_int_default(file, -1, "%s.countdown", buf);
132   adip->war_reason
133     = secfile_lookup_int_default(file, 0, "%s.war_reason", buf);
134   adip->ally_patience
135     = secfile_lookup_int_default(file, 0, "%s.patience", buf);
136   adip->warned_about_space
137     = secfile_lookup_int_default(file, 0, "%s.warn_space", buf);
138   adip->asked_about_peace
139     = secfile_lookup_int_default(file, 0, "%s.ask_peace", buf);
140   adip->asked_about_alliance
141     = secfile_lookup_int_default(file, 0, "%s.ask_alliance", buf);
142   adip->asked_about_ceasefire
143     = secfile_lookup_int_default(file, 0, "%s.ask_ceasefire", buf);
144 }
145 
146 /**************************************************************************
147   Copy default ai data from player to player
148 **************************************************************************/
dai_player_copy(struct ai_type * ait,struct player * original,struct player * created)149 void dai_player_copy(struct ai_type *ait,
150                      struct player *original, struct player *created)
151 {
152   bool close_original;
153   bool close_created;
154   struct ai_plr *orig_data = dai_plr_data_get(ait, original, &close_original);
155   struct ai_plr *created_data = dai_plr_data_get(ait, created, &close_created);
156 
157   advance_index_iterate(A_NONE, i) {
158     created_data->tech_want[i] = orig_data->tech_want[i];
159   } advance_index_iterate_end;
160 
161   if (close_original) {
162     dai_data_phase_finished(ait, original);
163   }
164   if (close_created) {
165     dai_data_phase_finished(ait, created);
166   }
167 }
168 
169 /**************************************************************************
170   Ai got control of the player.
171 **************************************************************************/
dai_gained_control(struct ai_type * ait,struct player * pplayer)172 void dai_gained_control(struct ai_type *ait, struct player *pplayer)
173 {
174   if (pplayer->ai_common.skill_level != AI_LEVEL_AWAY) {
175     multipliers_iterate(pmul) {
176       pplayer->multipliers_target[multiplier_index(pmul)] = pmul->def;
177     } multipliers_iterate_end;
178 
179     /* Clear worker tasks, some AIs (e.g. classic) does not use those */
180     city_list_iterate(pplayer->cities, pcity) {
181       clear_worker_tasks(pcity);
182     } city_list_iterate_end;
183   }
184 
185   dai_assess_danger_player(ait, pplayer);
186 }
187