Freeciv21
Develop your civilization from humble roots to a global empire
advdata.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can redistribute it
4  and/or modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation, either version 3 of the
6  License, or (at your option) any later version. You should have received
7  a copy of the GNU General Public License along with Freeciv21. If not,
8  see https://www.gnu.org/licenses/.
9  */
10 
11 // utility
12 #include "log.h"
13 
14 // common
15 #include "actions.h"
16 #include "ai.h"
17 #include "city.h"
18 #include "effects.h"
19 #include "game.h"
20 #include "government.h"
21 #include "map.h"
22 #include "movement.h"
23 #include "research.h"
24 #include "unit.h"
25 #include "unitlist.h"
26 
27 /* common/aicore */
28 #include "aisupport.h"
29 #include "path_finding.h"
30 
31 // server
32 #include "cityturn.h"
33 #include "maphand.h"
34 #include "plrhand.h"
35 #include "srv_log.h"
36 #include "unittools.h"
37 
38 /* server/advisors */
39 #include "advbuilding.h"
40 #include "advcity.h"
41 #include "advtools.h"
42 
43 // ai
44 #include "handicaps.h"
45 
46 #include "advdata.h"
47 
48 static void adv_dipl_new(const struct player *plr1,
49  const struct player *plr2);
50 static void adv_dipl_free(const struct player *plr1,
51  const struct player *plr2);
52 static struct adv_dipl *adv_dipl_get(const struct player *plr1,
53  const struct player *plr2);
54 
63 static void adv_data_city_impr_calc(struct player *pplayer,
64  struct adv_data *adv)
65 {
66  int count[ADV_IMPR_LAST];
67 
68  memset(count, 0, sizeof(count));
69 
70  improvement_iterate(pimprove)
71  {
72  struct universal source = {.value = {.building = pimprove},
73  .kind = VUT_IMPROVEMENT};
74 
76 
77  // Find largest extension
79  {
80  switch (peffect->type) {
81 #if 0
82  // TODO
83  case EFT_FORCE_CONTENT:
84  case EFT_FORCE_CONTENT_PCT:
85  case EFT_MAKE_CONTENT:
86  case EFT_MAKE_CONTENT_MIL:
87  case EFT_MAKE_CONTENT_MIL_PER:
88  case EFT_MAKE_CONTENT_PCT:
89  case EFT_MAKE_HAPPY:
90 #endif
91  case EFT_CAPITAL_CITY:
92  case EFT_POLLU_POP_PCT:
93  case EFT_POLLU_POP_PCT_2:
94  case EFT_POLLU_PROD_PCT:
95  case EFT_OUTPUT_BONUS:
96  case EFT_OUTPUT_BONUS_2:
97  case EFT_OUTPUT_WASTE_PCT:
98  case EFT_UPKEEP_FREE:
99  requirement_vector_iterate(&peffect->reqs, preq)
100  {
101  if (VUT_IMPROVEMENT == preq->source.kind
102  && preq->source.value.building == pimprove) {
103  if (adv->impr_calc[improvement_index(pimprove)]
105  adv->impr_calc[improvement_index(pimprove)] =
107  }
108  if (preq->range > adv->impr_range[improvement_index(pimprove)]) {
109  adv->impr_range[improvement_index(pimprove)] = preq->range;
110  }
111  }
112  }
114  break;
115  case EFT_OUTPUT_ADD_TILE:
116  case EFT_OUTPUT_PER_TILE:
117  case EFT_OUTPUT_INC_TILE:
118  requirement_vector_iterate(&peffect->reqs, preq)
119  {
120  if (VUT_IMPROVEMENT == preq->source.kind
121  && preq->source.value.building == pimprove) {
122  adv->impr_calc[improvement_index(pimprove)] =
124  if (preq->range > adv->impr_range[improvement_index(pimprove)]) {
125  adv->impr_range[improvement_index(pimprove)] = preq->range;
126  }
127  }
128  }
130  break;
131  default:
132  // Nothing!
133  break;
134  }
135  }
137  }
139 }
140 
146 static bool player_has_really_useful_tech_parasite(struct player *pplayer)
147 {
148  struct research *presearch, *aresearch;
149  int players_needed = get_player_bonus(pplayer, EFT_TECH_PARASITE);
150 
151  if (players_needed == 0) {
152  return false;
153  }
154 
155  presearch = research_get(pplayer);
157  {
158  int players_having;
159 
160  if (!research_invention_gettable(presearch, tech,
161  game.info.tech_parasite_allow_holes)
162  || TECH_KNOWN == research_invention_state(presearch, tech)) {
163  continue;
164  }
165 
166  players_having = 0;
167 
168  players_iterate_alive(aplayer)
169  {
170  if (aplayer == pplayer) {
171  continue;
172  }
173 
174  aresearch = research_get(aplayer);
175  if (TECH_KNOWN == research_invention_state(aresearch, tech)
176  || aresearch->researching == tech) {
177  players_having++;
178  if (players_having >= players_needed) {
179  return true;
180  }
181  }
182  }
184  }
186  return false;
187 }
188 
194 void adv_data_analyze_rulesets(struct player *pplayer)
195 {
196  struct adv_data *adv = pplayer->server.adv;
197 
198  fc_assert_ret(adv != nullptr);
199 
200  adv_data_city_impr_calc(pplayer, adv);
201 }
202 
206 static void count_my_units(struct player *pplayer)
207 {
208  struct adv_data *adv = adv_data_get(pplayer, nullptr);
209 
210  memset(&adv->stats.units, 0, sizeof(adv->stats.units));
211 
212  unit_list_iterate(pplayer->units, punit)
213  {
214  struct unit_class *pclass = unit_class_get(punit);
215 
216  adv->stats.units.byclass[uclass_index(pclass)]++;
217 
218  if (unit_has_type_flag(punit, UTYF_COAST_STRICT)) {
219  adv->stats.units.coast_strict++;
220  }
221  if (utype_can_do_action(unit_type_get(punit), ACTION_SUICIDE_ATTACK)) {
222  adv->stats.units.suicide_attackers++;
223  }
224  if (unit_can_do_action(punit, ACTION_PARADROP)) {
225  adv->stats.units.paratroopers++;
226  }
227  if (utype_can_do_action(punit->utype, ACTION_AIRLIFT)) {
228  adv->stats.units.airliftable++;
229  }
230  if (can_upgrade_unittype(pplayer, unit_type_get(punit))) {
231  adv->stats.units.upgradeable++;
232  }
233  }
235 }
236 
250 bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
251 {
252  struct adv_data *adv = pplayer->server.adv;
253  bool danger_of_nukes;
254  action_id nuke_actions[MAX_NUM_ACTIONS];
255 
256  {
257  int i = 0;
258 
259  // Conventional nukes
260  action_list_add_all_by_result(nuke_actions, &i, ACTRES_NUKE);
261  action_list_add_all_by_result(nuke_actions, &i, ACTRES_NUKE_CITY);
262  action_list_add_all_by_result(nuke_actions, &i, ACTRES_NUKE_UNITS);
263  // TODO: worry about spy nuking too?
264  action_list_end(nuke_actions, i);
265  }
266 
267  fc_assert_ret_val(adv != nullptr, false);
268 
269  if (adv->phase_is_initialized) {
270  return false;
271  }
272  adv->phase_is_initialized = true;
273 
275 
276  danger_of_nukes = false;
277 
278  /*** Threats ***/
279 
281  adv->num_oceans = wld.map.num_oceans;
282  adv->threats.continent = new bool[adv->num_continents + 1]();
283  adv->threats.invasions = false;
284  adv->threats.nuclear = 0; // none
285  adv->threats.ocean = new bool[adv->num_oceans + 1]();
286  adv->threats.igwall = false;
287 
288  players_iterate(aplayer)
289  {
290  if (!adv_is_player_dangerous(pplayer, aplayer)) {
291  continue;
292  }
293 
294  /* The idea is that if there aren't any hostile cities on
295  * our continent, the danger of land attacks is not big
296  * enough to warrant city walls. Concentrate instead on
297  * coastal fortresses and hunting down enemy transports. */
298  city_list_iterate(aplayer->cities, acity)
299  {
300  Continent_id continent = tile_continent(acity->tile);
301  if (continent >= 0) {
302  adv->threats.continent[continent] = true;
303  }
304  }
306 
307  unit_list_iterate(aplayer->units, punit)
308  {
309  const struct unit_class *pclass = unit_class_get(punit);
310 
311  if (unit_type_get(punit)->adv.igwall) {
312  adv->threats.igwall = true;
313  }
314 
315  if (pclass->adv.sea_move != MOVE_NONE) {
316  /* If the enemy has not started sailing yet, or we have total
317  * control over the seas, don't worry, keep attacking. */
318  if (uclass_has_flag(pclass, UCF_CAN_OCCUPY_CITY)) {
319  // Enemy represents a cross-continental threat!
320  adv->threats.invasions = true;
321  } else if (get_transporter_capacity(punit) > 0) {
322  unit_class_iterate(cargoclass)
323  {
324  if (uclass_has_flag(cargoclass, UCF_CAN_OCCUPY_CITY)
326  cargoclass)) {
327  // Enemy can transport some threatening units!
328  adv->threats.invasions = true;
329  break;
330  }
331  }
333  }
334 
335  /* The idea is that while our enemies don't have any offensive
336  * seaborne units, we don't have to worry. Go on the offensive! */
337  if (unit_type_get(punit)->attack_strength > 1) {
338  if (is_ocean_tile(unit_tile(punit))) {
339  Continent_id continent = tile_continent(unit_tile(punit));
340 
341  adv->threats.ocean[-continent] = true;
342  } else {
343  adjc_iterate(&(wld.map), unit_tile(punit), tile2)
344  {
345  if (is_ocean_tile(tile2)) {
346  Continent_id continent = tile_continent(tile2);
347 
348  adv->threats.ocean[-continent] = true;
349  }
350  }
352  }
353  }
354  continue;
355  }
356 
357  // If our enemy builds missiles, worry about missile defence.
358  if (utype_can_do_action(unit_type_get(punit), ACTION_SUICIDE_ATTACK)
359  && unit_type_get(punit)->attack_strength > 1) {
360  adv->threats.suicide_attack = true;
361  }
362 
363  // If he builds nukes, worry a lot.
364  action_list_iterate(nuke_actions, act_id)
365  {
366  if (unit_can_do_action(punit, act_id)) {
367  danger_of_nukes = true;
368  }
369  }
371  }
373 
374  // Check for nuke capability
375  action_list_iterate(nuke_actions, act_id)
376  {
377  int i;
379 
380  for (i = 0; i < nuke_units; i++) {
381  struct unit_type *nuke =
382  get_role_unit(action_id_get_role(act_id), i);
383 
384  if (can_player_build_unit_direct(aplayer, nuke)) {
385  adv->threats.nuclear = 1;
386  }
387  }
388  }
390  }
392 
393  // Increase from fear to terror if opponent actually has nukes
394  if (danger_of_nukes) {
395  adv->threats.nuclear++; // sum of both fears
396  }
397 
398  /*** Exploration ***/
399 
400  adv->explore.land_done = true;
401  adv->explore.sea_done = true;
402  adv->explore.continent = new bool[adv->num_continents + 1]();
403  adv->explore.ocean = new bool[adv->num_oceans + 1]();
404 
405  whole_map_iterate(&(wld.map), ptile)
406  {
407  Continent_id continent = tile_continent(ptile);
408 
409  if (is_ocean_tile(ptile)) {
410  if (adv->explore.sea_done && has_handicap(pplayer, H_TARGETS)
411  && !map_is_known(ptile, pplayer)) {
412  // We're not done there.
413  adv->explore.sea_done = false;
414  adv->explore.ocean[-continent] = true;
415  }
416  // skip rest, which is land only
417  continue;
418  }
419  if (adv->explore.continent[tile_continent(ptile)]) {
420  // we don't need more explaining, we got the point
421  continue;
422  }
423  if (hut_on_tile(ptile)
424  && (!has_handicap(pplayer, H_HUTS)
425  || map_is_known(ptile, pplayer))) {
426  adv->explore.land_done = false;
427  adv->explore.continent[continent] = true;
428  continue;
429  }
430  if (has_handicap(pplayer, H_TARGETS) && !map_is_known(ptile, pplayer)) {
431  // this AI must explore
432  adv->explore.land_done = false;
433  adv->explore.continent[continent] = true;
434  }
435  }
437 
438  /*** Statistics ***/
439 
440  adv->stats.cities = new int[adv->num_continents + 1]();
441  adv->stats.ocean_cities = new int[adv->num_oceans + 1]();
442  adv->stats.average_production = 0;
443  city_list_iterate(pplayer->cities, pcity)
444  {
445  Continent_id continent = tile_continent(pcity->tile);
446 
447  if (continent >= 0) {
448  adv->stats.cities[continent]++;
449  } else {
450  adv->stats.ocean_cities[-continent]++;
451  }
452  adv->stats.average_production += pcity->surplus[O_SHIELD];
453  }
455  adv->stats.average_production /= MAX(1, city_list_size(pplayer->cities));
456 
457  /*** Diplomacy ***/
458 
459  players_iterate(aplayer)
460  {
461  struct adv_dipl *dip = adv_dipl_get(pplayer, aplayer);
462 
463  dip->allied_with_enemy = false;
464  players_iterate(check_pl)
465  {
466  if (pplayers_allied(aplayer, check_pl)
467  && player_diplstate_get(pplayer, check_pl)->type == DS_WAR) {
468  dip->allied_with_enemy = true;
469  }
470  }
472  }
474 
476 
477  adv->dipl.production_leader = nullptr;
478  players_iterate(aplayer)
479  {
480  if (adv->dipl.production_leader == nullptr
481  || adv->dipl.production_leader->score.mfg < aplayer->score.mfg) {
482  adv->dipl.production_leader = aplayer;
483  }
484  }
486 
487  /*** Priorities ***/
488 
489  /* NEVER set these to zero! Weight values are usually multiplied by
490  * these values, so be careful with them. They are used in city
491  * and government calculations, and food and shields should be
492  * slightly bigger because we only look at surpluses there. They
493  * are all WAGs. */
496  if (adv_wants_science(pplayer)) {
497  adv->luxury_priority = 1;
498  adv->science_priority = TRADE_WEIGHTING * 1.2;
499  } else {
501  adv->science_priority = 1;
502  }
504  adv->happy_priority = 1;
505  adv->unhappy_priority = TRADE_WEIGHTING; // danger
506  adv->angry_priority = TRADE_WEIGHTING * 3; // grave danger
509 
510  // Research want
511  adv->wants_science = !(is_future_tech(research_get(pplayer)->researching)
513 
514  /* max num cities
515  * The idea behind this code is that novice players don't understand that
516  * expansion is critical and find it very annoying.
517  * With the following code AI players will try to be only a bit better
518  * than the best human players. This should lead to more exciting games
519  * for the beginners.
520  */
521  if (has_handicap(pplayer, H_EXPANSION)) {
522  bool found_human = false;
523  adv->max_num_cities = 3;
524  players_iterate_alive(aplayer)
525  {
526  if (aplayer == pplayer || is_ai(aplayer)) {
527  continue;
528  }
529  adv->max_num_cities =
530  MAX(adv->max_num_cities, city_list_size(aplayer->cities) + 3);
531  found_human = true;
532  }
534  if (!found_human) {
536  }
537  } else {
539  }
540 
541  count_my_units(pplayer);
542 
544 
545  // Government
547  adv_best_government(pplayer);
549 
550  return true;
551 }
552 
556 void adv_data_phase_done(struct player *pplayer)
557 {
558  struct adv_data *adv = pplayer->server.adv;
559 
560  fc_assert_ret(adv != nullptr);
561 
562  if (!adv->phase_is_initialized) {
563  return;
564  }
565 
566  delete[] adv->explore.ocean;
567  delete[] adv->explore.continent;
568  delete[] adv->threats.continent;
569  delete[] adv->threats.ocean;
570  delete[] adv->stats.cities;
571  delete[] adv->stats.ocean_cities;
572 
573  adv->explore.ocean = nullptr;
574  adv->explore.continent = nullptr;
575  adv->threats.continent = nullptr;
576  adv->threats.ocean = nullptr;
577  adv->stats.cities = nullptr;
578  adv->stats.ocean_cities = nullptr;
579  adv->num_continents = 0;
580  adv->num_oceans = 0;
581 
582  adv->phase_is_initialized = false;
583 }
584 
591 struct adv_data *adv_data_get(struct player *pplayer, bool *caller_closes)
592 {
593  struct adv_data *adv = pplayer->server.adv;
594 
595  fc_assert_ret_val(adv != nullptr, nullptr);
596 
597  /* It's certainly indication of bug causing problems
598  if this adv_data_get() gets called between adv_data_phase_done() and
599  adv_data_phase_init(), since we may end up calling those
600  functions if number of known continents has changed.
601 
602  Consider following case:
603  Correct call order would be:
604  a) adv_data_phase_init()
605  b) adv_data_get() -> adv_data_phase_done()
606  c) adv_data_get() -> adv_data_phase_init()
607  d) adv_data_phase_done()
608  e) do something
609  f) adv_data_phase_init()
610 
611  In (e) data phase would be closed and data would be
612  correctly initialized at (f), which is probably beginning
613  next turn.
614 
615  Buggy version where adv_data_get() (b&c) gets called after (d):
616  a) adv_data_phase_init()
617  d) adv_data_phase_done()
618  b) adv_data_get() -> adv_data_phase_done()
619  c) adv_data_get() -> adv_data_phase_init()
620  e) do something
621  f) adv_data_phase_init()
622 
623  Now in (e) data phase would be open. When adv_data_phase_init()
624  then finally gets called and it really should recreate data
625  to match situation of new turn, it detects that data phase
626  is already initialized and does nothing.
627 
628  So, this assertion is here for a reason!
629 
630  Code below tries to fix the situation best it can if such a bug is
631  encountered. Since we are probably going to trust that to be enough
632  instead of making intrusive fixes for actual bug in stable branch,
633  do not assert for non-debug builds of stable versions. */
634 #if defined(FREECIV_DEBUG) || defined(IS_DEVEL_VERSION)
635  fc_assert(caller_closes != nullptr || adv->phase_is_initialized);
636 #endif
637 
638  if (caller_closes != nullptr) {
639  *caller_closes = false;
640  }
641 
643  || adv->num_oceans != wld.map.num_oceans) {
644  // we discovered more continents, recalculate!
645 
646  if (adv->phase_is_initialized) {
647  /* Only call these in this order if inside data phase.
648  This is blanket "fix" for all cases where adv_data_get() is called
649  at illegal time. This at least minimize bad effects of such calls.
650  */
651  adv_data_phase_done(pplayer);
652  adv_data_phase_init(pplayer, false);
653  } else {
654  /* Call them in "wrong" order so we return recalculated data to caller,
655  but leave data phase closed.
656  This is blanket "fix" for all cases where adv_data_get() is called
657  at illegal time. This at least minimize bad effects of such calls.
658 
659  Arguably this is not buggy at all but works just as designed in
660  case of being called in alternate movement mode for players
661  other than currently moving one (for diplomacy between the two,
662  for example) */
663  log_debug("%s advisor data phase closed when adv_data_get() called",
664  player_name(pplayer));
665  adv_data_phase_init(pplayer, false);
666  if (caller_closes != nullptr) {
667  *caller_closes = true;
668  } else {
669  adv_data_phase_done(pplayer);
670  }
671  }
672  } else {
673  if (!adv->phase_is_initialized && caller_closes != nullptr) {
674  adv_data_phase_init(pplayer, false);
675  *caller_closes = true;
676  }
677  }
678 
679  return adv;
680 }
681 
685 void adv_data_init(struct player *pplayer)
686 {
687  struct adv_data *adv;
688 
689  if (pplayer->server.adv == nullptr) {
690  pplayer->server.adv = new adv_data();
691  }
692  adv = pplayer->server.adv;
693 
694  adv->government_want.clear();
695 
697  player_slots_iterate(pslot)
698  {
699  struct adv_dipl **dip_slot =
700  adv->dipl.adv_dipl_slots + player_slot_index(pslot);
701  *dip_slot = nullptr;
702  }
704 
705  players_iterate(aplayer)
706  {
707  adv_dipl_new(pplayer, aplayer);
708  if (aplayer != pplayer) {
709  adv_dipl_new(aplayer, pplayer);
710  }
711  }
713 
714  adv_data_default(pplayer);
715 }
716 
720 void adv_data_default(struct player *pplayer)
721 {
722  struct adv_data *adv = pplayer->server.adv;
723 
724  fc_assert_ret(adv != nullptr);
725 
726  adv->govt_reeval = 0;
727  adv->government_want.resize(government_count());
728 
729  adv->wonder_city = 0;
730 
731  adv->wants_science = true;
732  adv->celebrate = false;
733  adv->max_num_cities = 10000;
734 }
735 
739 void adv_data_close(struct player *pplayer)
740 {
741  struct adv_data *adv = pplayer->server.adv;
742 
743  fc_assert_ret(nullptr != adv);
744 
745  adv_data_phase_done(pplayer);
746 
747  adv->government_want.clear();
748  if (adv->dipl.adv_dipl_slots != nullptr) {
749  players_iterate(aplayer)
750  {
751  adv_dipl_free(pplayer, aplayer);
752  if (aplayer != pplayer) {
753  adv_dipl_free(aplayer, pplayer);
754  }
755  }
757  delete[] adv->dipl.adv_dipl_slots;
758  }
759 
760  delete adv;
761 
762  pplayer->server.adv = nullptr;
763 }
764 
768 static void adv_dipl_new(const struct player *plr1,
769  const struct player *plr2)
770 {
771  struct adv_dipl **dip_slot =
772  plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
773 
774  *dip_slot = new adv_dipl();
775 }
776 
780 static void adv_dipl_free(const struct player *plr1,
781  const struct player *plr2)
782 {
783  struct adv_dipl **dip_slot =
784  plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
785 
786  if (*dip_slot != nullptr) {
787  delete *dip_slot;
788  *dip_slot = nullptr;
789  }
790 }
791 
795 static struct adv_dipl *adv_dipl_get(const struct player *plr1,
796  const struct player *plr2)
797 {
798  struct adv_dipl **dip_slot =
799  plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
800 
801  return *dip_slot;
802 }
803 
816 void adv_best_government(struct player *pplayer)
817 {
818  struct adv_data *adv = adv_data_get(pplayer, nullptr);
819  int best_val = 0;
820  struct government *current_gov = government_of_player(pplayer);
821 
822  adv->goal.govt.gov = current_gov;
823  adv->goal.govt.val = 0;
824  adv->goal.govt.req = A_UNSET;
825  adv->goal.revolution = current_gov;
826 
827  if (has_handicap(pplayer, H_AWAY) || !pplayer->is_alive) {
828  return;
829  }
830 
831  if (adv->govt_reeval == 0) {
832  const struct research *presearch = research_get(pplayer);
833 
834  for (auto &gov : governments) {
835  adv_want val = 0;
836  bool override = false;
837 
838  if (&gov == game.government_during_revolution) {
839  continue; // pointless
840  }
841  if (gov.ai.better
842  && can_change_to_government(pplayer, gov.ai.better)) {
843  continue; // we have better governments available
844  }
845 
846  CALL_PLR_AI_FUNC(gov_value, pplayer, pplayer, &gov, &val, &override);
847 
848  if (!override) {
849  int dist;
850  adv_want bonus = 0; // in percentage
851  int revolution_turns;
852 
853  pplayer->government = &gov;
854  /* Ideally we should change national budget here, but since
855  * this is a rather big CPU operation, we'd rather not. */
856  check_player_max_rates(pplayer);
857  city_list_iterate(pplayer->cities, acity)
858  {
859  auto_arrange_workers(acity);
860  }
862  city_list_iterate(pplayer->cities, pcity)
863  {
864  val += adv_eval_calc_city(pcity, adv);
865  }
867 
868  /* Bonuses for non-economic abilities. We increase val by
869  * a very small amount here to choose govt in cases where
870  * we have no cities yet. */
871  bonus += get_player_bonus(pplayer, EFT_VETERAN_BUILD) > 0 ? 3 : 0;
872 
873  // TODO: Individual and well balanced value.
874  action_iterate(act)
875  {
876  struct action *paction = action_by_number(act);
877 
878  if (!action_immune_government(&gov, act)) {
879  /* This government doesn't provide immunity againt this
880  * action. */
881  continue;
882  }
883 
884  switch (paction->result) {
885  case ACTRES_ATTACK:
886  case ACTRES_SPY_INCITE_CITY:
887  case ACTRES_CONQUER_CITY:
888  bonus += 4;
889  break;
890  case ACTRES_SPY_BRIBE_UNIT:
891  bonus += 2;
892  break;
893  case ACTRES_TRANSFORM_TERRAIN:
894  bonus += 1.5;
895  break;
896  case ACTRES_CULTIVATE:
897  case ACTRES_PLANT:
898  bonus += 0.3;
899  break;
900  case ACTRES_PILLAGE:
901  bonus += 0.2;
902  break;
903  case ACTRES_SPY_INVESTIGATE_CITY:
904  case ACTRES_SPY_POISON:
905  case ACTRES_SPY_SPREAD_PLAGUE:
906  case ACTRES_SPY_STEAL_GOLD:
907  case ACTRES_SPY_SABOTAGE_CITY:
908  case ACTRES_SPY_TARGETED_SABOTAGE_CITY:
909  case ACTRES_SPY_SABOTAGE_CITY_PRODUCTION:
910  case ACTRES_SPY_STEAL_TECH:
911  case ACTRES_SPY_TARGETED_STEAL_TECH:
912  case ACTRES_SPY_SABOTAGE_UNIT:
913  case ACTRES_CAPTURE_UNITS:
914  case ACTRES_STEAL_MAPS:
915  case ACTRES_BOMBARD:
916  case ACTRES_SPY_NUKE:
917  case ACTRES_NUKE:
918  case ACTRES_NUKE_CITY:
919  case ACTRES_NUKE_UNITS:
920  case ACTRES_DESTROY_CITY:
921  case ACTRES_EXPEL_UNIT:
922  case ACTRES_STRIKE_BUILDING:
923  case ACTRES_STRIKE_PRODUCTION:
924  case ACTRES_SPY_ATTACK:
925  // Being a target of this is usually undesireable
926  // TODO: Individual and well balanced values.
927  bonus += 0.1;
928  break;
929 
930  case ACTRES_MARKETPLACE:
931  case ACTRES_FOUND_CITY:
932  case ACTRES_DISBAND_UNIT:
933  case ACTRES_PARADROP:
934  case ACTRES_FORTIFY:
935  /* Wants the ability to do this to it self. Don't want others
936  * to target it. Do nothing since action_immune_government()
937  * doesn't separate based on who the actor is. */
938  break;
939 
940  case ACTRES_NONE:
941  // Ruleset defined
942  break;
943 
944  case ACTRES_ESTABLISH_EMBASSY:
945  case ACTRES_TRADE_ROUTE:
946  case ACTRES_JOIN_CITY:
947  case ACTRES_HELP_WONDER:
948  case ACTRES_RECYCLE_UNIT:
949  case ACTRES_HOME_CITY:
950  case ACTRES_UPGRADE_UNIT:
951  case ACTRES_AIRLIFT:
952  case ACTRES_HEAL_UNIT:
953  case ACTRES_ROAD:
954  case ACTRES_CONVERT:
955  case ACTRES_BASE:
956  case ACTRES_MINE:
957  case ACTRES_IRRIGATE:
958  case ACTRES_CLEAN_POLLUTION:
959  case ACTRES_CLEAN_FALLOUT:
960  case ACTRES_TRANSPORT_ALIGHT:
961  case ACTRES_TRANSPORT_UNLOAD:
962  case ACTRES_TRANSPORT_DISEMBARK:
963  case ACTRES_TRANSPORT_BOARD:
964  case ACTRES_TRANSPORT_EMBARK:
965  /* Could be good. An embassy gives permanent contact. A trade
966  * route gives gold per turn. Join city gives population. Help
967  * wonder gives shields. */
968  // TODO: Individual and well balanced values.
969  break;
970  }
971  }
973 
974  bonus +=
975  get_player_bonus(pplayer, EFT_INSPIRE_PARTISANS) > 0 ? 3 : 0;
976  bonus += get_player_bonus(pplayer, EFT_RAPTURE_GROW) > 0 ? 2 : 0;
977  bonus += get_player_bonus(pplayer, EFT_FANATICS) > 0 ? 3 : 0;
978  bonus += get_player_bonus(pplayer, EFT_OUTPUT_INC_TILE) * 8;
979 
980  revolution_turns =
981  get_player_bonus(pplayer, EFT_REVOLUTION_UNHAPPINESS);
982  if (revolution_turns > 0) {
983  bonus -= int(6 / revolution_turns);
984  }
985 
986  val += (val * bonus) / 100;
987 
988  // FIXME: handle reqs other than technologies.
989  dist = 0;
990  requirement_vector_iterate(&gov.reqs, preq)
991  {
992  if (VUT_ADVANCE == preq->source.kind) {
993  dist += MAX(1, research_goal_unknown_techs(
994  presearch,
995  advance_number(preq->source.value.advance)));
996  }
997  }
999  val = amortize(val, dist);
1000  }
1001 
1002  adv->government_want[government_index(&gov)] = val; // Save want
1003  }
1004  // Now reset our gov to it's real state.
1005  pplayer->government = current_gov;
1006  city_list_iterate(pplayer->cities, acity)
1007  {
1008  auto_arrange_workers(acity);
1009  }
1011  if (player_is_cpuhog(pplayer)) {
1012  adv->govt_reeval = 1;
1013  } else {
1014  adv->govt_reeval = CLIP(5, city_list_size(pplayer->cities), 20);
1015  }
1016  }
1017  adv->govt_reeval--;
1018 
1019  // Figure out which government is the best for us this turn.
1020  for (auto &gov : governments) {
1021  int gi = government_index(&gov);
1022  if (adv->government_want[gi] > best_val
1023  && can_change_to_government(pplayer, &gov)) {
1024  best_val = adv->government_want[gi];
1025  adv->goal.revolution = &gov;
1026  }
1027  if (adv->government_want[gi] > adv->goal.govt.val) {
1028  adv->goal.govt.gov = &gov;
1029  adv->goal.govt.val = adv->government_want[gi];
1030 
1031  // FIXME: handle reqs other than technologies.
1032  adv->goal.govt.req = A_NONE;
1033  requirement_vector_iterate(&gov.reqs, preq)
1034  {
1035  if (VUT_ADVANCE == preq->source.kind) {
1036  adv->goal.govt.req = advance_number(preq->source.value.advance);
1037  break;
1038  }
1039  }
1041  }
1042  };
1043  /* Goodness of the ideal gov is calculated relative to the goodness of the
1044  * best of the available ones. */
1045  adv->goal.govt.val -= best_val;
1046 }
1047 
1051 bool adv_wants_science(struct player *pplayer)
1052 {
1053  return adv_data_get(pplayer, nullptr)->wants_science;
1054 }
1055 
1062 bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
1063 {
1064  struct adv_dipl *dip;
1065  enum diplstate_type ds;
1066  enum override_bool dang = NO_OVERRIDE;
1067 
1068  if (is_ai(pplayer)) {
1069  // Give AI code possibility to decide itself
1070  CALL_PLR_AI_FUNC(consider_plr_dangerous, pplayer, pplayer, aplayer,
1071  &dang);
1072  }
1073 
1074  if (dang == OVERRIDE_FALSE) {
1075  return false;
1076  }
1077 
1078  if (dang == OVERRIDE_TRUE) {
1079  return true;
1080  }
1081 
1082  if (pplayer == aplayer) {
1083  // We always trust ourself
1084  return false;
1085  }
1086 
1087  ds = player_diplstate_get(pplayer, aplayer)->type;
1088 
1089  if (ds == DS_WAR || ds == DS_CEASEFIRE) {
1090  // It's already a war or aplayer can declare it soon
1091  return true;
1092  }
1093 
1094  dip = adv_dipl_get(pplayer, aplayer);
1095 
1096  if (dip->allied_with_enemy) {
1097  // Don't trust someone who will declare war on us soon
1098  return true;
1099  }
1100 
1101  if (player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel > 0) {
1102  return true;
1103  }
1104 
1105  if (pplayer->ai_common.love[player_index(aplayer)] < MAX_AI_LOVE / 10) {
1106  /* We don't trust players who we don't like. Note that
1107  * aplayer's units inside pplayer's borders decreases AI's love */
1108  return true;
1109  }
1110 
1111  return false;
1112 }
void action_list_end(action_id *act_list, int size)
Terminate an action list of the specified size.
Definition: actions.cpp:5881
bool action_immune_government(struct government *gov, action_id act)
Will a player with the government gov be immune to the action act?
Definition: actions.cpp:5687
void action_list_add_all_by_result(action_id *act_list, int *position, enum action_result result)
Add all actions with the specified result to the specified action list starting at the specified posi...
Definition: actions.cpp:5898
struct action * action_by_number(action_id act_id)
Return the action with the given id.
Definition: actions.cpp:1149
#define action_list_iterate_end
Definition: actions.h:413
#define action_list_iterate(_act_list_, _act_id_)
Definition: actions.h:401
#define action_id_get_role(act_id)
Definition: actions.h:580
#define action_iterate_end
Definition: actions.h:383
#define MAX_NUM_ACTIONS
Definition: actions.h:223
#define action_iterate(_act_)
Definition: actions.h:378
#define TRADE_WEIGHTING
Definition: advbuilding.h:18
#define INFRA_WEIGHTING
Definition: advbuilding.h:26
#define FOOD_WEIGHTING
Definition: advbuilding.h:16
#define SHIELD_WEIGHTING
Definition: advbuilding.h:17
#define POLLUTION_WEIGHTING
Definition: advbuilding.h:25
int adv_eval_calc_city(struct city *pcity, struct adv_data *adv)
This calculates the usefulness of pcity to us.
Definition: advcity.cpp:26
static void adv_data_city_impr_calc(struct player *pplayer, struct adv_data *adv)
Precalculates some important data about the improvements in the game that we use later in ai/aicity....
Definition: advdata.cpp:63
void adv_data_analyze_rulesets(struct player *pplayer)
Analyze rulesets.
Definition: advdata.cpp:194
void adv_data_close(struct player *pplayer)
Free memory for advisor data.
Definition: advdata.cpp:739
void adv_data_init(struct player *pplayer)
Allocate memory for advisor data.
Definition: advdata.cpp:685
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Return a pointer to our data.
Definition: advdata.cpp:591
static void adv_dipl_free(const struct player *plr1, const struct player *plr2)
Free resources allocated for diplomacy information between two players.
Definition: advdata.cpp:780
bool adv_wants_science(struct player *pplayer)
Return whether science would help us at all.
Definition: advdata.cpp:1051
bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
Make and cache lots of calculations needed for other functions.
Definition: advdata.cpp:250
void adv_data_phase_done(struct player *pplayer)
Clean up our mess.
Definition: advdata.cpp:556
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
There are some signs that a player might be dangerous: We are at war with him, he has done lots of ig...
Definition: advdata.cpp:1062
static void adv_dipl_new(const struct player *plr1, const struct player *plr2)
Allocate new advisor diplomacy slot.
Definition: advdata.cpp:768
static void count_my_units(struct player *pplayer)
This function is called each turn to initialize pplayer->ai.stats.units.
Definition: advdata.cpp:206
static struct adv_dipl * adv_dipl_get(const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: advdata.cpp:795
static bool player_has_really_useful_tech_parasite(struct player *pplayer)
Check if the player still takes advantage of EFT_TECH_PARASITE.
Definition: advdata.cpp:146
void adv_best_government(struct player *pplayer)
Find best government to aim for.
Definition: advdata.cpp:816
void adv_data_default(struct player *pplayer)
Initialize with sane values.
Definition: advdata.cpp:720
@ ADV_IMPR_CALCULATE_FULL
Definition: advdata.h:31
@ ADV_IMPR_LAST
Definition: advdata.h:33
@ ADV_IMPR_ESTIMATE
Definition: advdata.h:32
@ ADV_IMPR_CALCULATE
Definition: advdata.h:30
adv_want amortize(adv_want benefit, int delay)
Amortize means gradually paying off a cost or debt over time.
Definition: advtools.cpp:25
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition: ai.h:383
struct player * player_leading_spacerace()
Find who is leading the space race.
Definition: aisupport.cpp:31
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define city_list_iterate_end
Definition: city.h:484
void auto_arrange_workers(struct city *pcity)
Call sync_cities() to send the affected cities to the clients.
Definition: cityturn.cpp:359
static void nuke_units(QVariant data1, QVariant data2)
Action "Nuke Units" for choice dialog.
Definition: dialogs.cpp:2220
static void nuke(QVariant data1, QVariant data2)
Action "Explode Nuclear" for choice dialog.
Definition: dialogs.cpp:2450
struct effect_list * get_req_source_effects(struct universal *psource)
Get a list of effects with this requirement source.
Definition: effects.cpp:132
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Returns the effect bonus for a player.
Definition: effects.cpp:673
#define effect_list_iterate_end
Definition: effects.h:349
#define effect_list_iterate(effect_list, peffect)
Definition: effects.h:347
bool hut_on_tile(const struct tile *ptile)
Returns TRUE iff an extra on the tile is a hut (removed by entering).
Definition: extras.cpp:626
float adv_want
Definition: fc_types.h:1144
int action_id
Definition: fc_types.h:306
#define MAX_NUM_PLAYER_SLOTS
Definition: fc_types.h:24
@ O_SHIELD
Definition: fc_types.h:86
signed short Continent_id
Definition: fc_types.h:289
override_bool
Definition: fc_types.h:78
@ OVERRIDE_TRUE
Definition: fc_types.h:78
@ NO_OVERRIDE
Definition: fc_types.h:78
@ OVERRIDE_FALSE
Definition: fc_types.h:78
struct civ_game game
Definition: game.cpp:47
struct world wld
Definition: game.cpp:48
struct government * government_of_player(const struct player *pplayer)
Return the government of a player.
Definition: government.cpp:107
Government_type_id government_count()
Return the number of governments.
Definition: government.cpp:64
std::vector< government > governments
Definition: government.cpp:28
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Can change to government if appropriate tech exists, and one of:
Definition: government.cpp:159
Government_type_id government_index(const struct government *pgovern)
Return the government index.
Definition: government.cpp:75
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
AI players may have handicaps - allowing them to cheat or preventing them from using certain algorith...
Definition: handicaps.cpp:62
@ H_TARGETS
Definition: handicaps.h:23
@ H_AWAY
Definition: handicaps.h:18
@ H_HUTS
Definition: handicaps.h:24
@ H_EXPANSION
Definition: handicaps.h:30
Impr_type_id improvement_index(const struct impr_type *pimprove)
Return the improvement index.
#define improvement_iterate_end
Definition: improvement.h:199
#define improvement_iterate(_p)
Definition: improvement.h:193
#define fc_assert_ret(condition)
Definition: log.h:112
#define fc_assert(condition)
Definition: log.h:89
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
#define log_debug(message,...)
Definition: log.h:65
#define adjc_iterate_end
Definition: map.h:358
#define MAP_INDEX_SIZE
Definition: map.h:91
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition: map.h:351
#define whole_map_iterate(_map, _tile)
Definition: map.h:473
#define whole_map_iterate_end
Definition: map.h:480
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Return whether the player knows the tile.
Definition: maphand.cpp:884
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Return TRUE iff transporter type has ability to transport transported class.
Definition: movement.cpp:698
int player_slot_index(const struct player_slot *pslot)
Returns the index of the player slot.
Definition: player.cpp:373
int player_index(const struct player *pplayer)
Return the player index.
Definition: player.cpp:748
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
Definition: player.cpp:816
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied.
Definition: player.cpp:1334
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: player.cpp:288
#define players_iterate_end
Definition: player.h:520
#define players_iterate(_pplayer)
Definition: player.h:514
#define MAX_AI_LOVE
Definition: player.h:546
#define player_slots_iterate(_pslot)
Definition: player.h:505
#define is_ai(plr)
Definition: player.h:227
#define players_iterate_alive_end
Definition: player.h:532
#define player_slots_iterate_end
Definition: player.h:509
static bool player_is_cpuhog(const struct player *pplayer)
Definition: player.h:558
#define players_iterate_alive(_pplayer)
Definition: player.h:526
void check_player_max_rates(struct player *pplayer)
The following checks that government rates are acceptable for the present form of government.
Definition: plrhand.cpp:637
#define requirement_vector_iterate_end
Definition: requirements.h:80
#define requirement_vector_iterate(req_vec, preq)
Definition: requirements.h:78
int research_goal_unknown_techs(const struct research *presearch, Tech_type_id goal)
Returns the number of technologies the player need to research to get the goal technology.
Definition: research.cpp:745
struct research * research_get(const struct player *pplayer)
Returns the research structure associated with the player.
Definition: research.cpp:110
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Returns state of the tech for current research.
Definition: research.cpp:609
bool research_invention_gettable(const struct research *presearch, const Tech_type_id tech, bool allow_holes)
Returns TRUE iff the given tech can be given to the players sharing the research immediately.
Definition: research.cpp:686
#define CLIP(lower, current, upper)
Definition: shared.h:51
#define MAX(x, y)
Definition: shared.h:48
@ AIT_GOVERNMENT
Definition: srv_log.h:49
@ AIT_AIDATA
Definition: srv_log.h:48
@ TIMER_STOP
Definition: srv_log.h:77
@ TIMER_START
Definition: srv_log.h:77
#define TIMING_LOG(timer, activity)
Definition: srv_log.h:121
enum action_result result
Definition: actions.h:308
bool wants_science
Definition: advdata.h:128
short govt_reeval
Definition: advdata.h:115
int pollution_priority
Definition: advdata.h:110
int shield_priority
Definition: advdata.h:102
bool * ocean
Definition: advdata.h:56
struct adv_data::@85 threats
int * cities
Definition: advdata.h:86
int * ocean_cities
Definition: advdata.h:87
bool invasions
Definition: advdata.h:54
struct government * revolution
Definition: advdata.h:124
int infra_priority
Definition: advdata.h:111
struct adv_dipl ** adv_dipl_slots
Definition: advdata.h:92
bool igwall
Definition: advdata.h:59
struct adv_data::@87 stats
bool celebrate
Definition: advdata.h:131
int num_oceans
Definition: advdata.h:99
int unhappy_priority
Definition: advdata.h:108
int luxury_priority
Definition: advdata.h:104
int nuclear
Definition: advdata.h:58
int angry_priority
Definition: advdata.h:109
int food_priority
Definition: advdata.h:103
int max_num_cities
Definition: advdata.h:134
int num_continents
Definition: advdata.h:98
struct player * production_leader
Definition: advdata.h:95
struct adv_data::@89 goal
struct adv_data::@88 dipl
struct adv_data::@89::@91 govt
int happy_priority
Definition: advdata.h:107
struct adv_data::@87::@90 units
bool * continent
Definition: advdata.h:55
enum adv_improvement_status impr_calc[B_LAST]
Definition: advdata.h:49
bool phase_is_initialized
Definition: advdata.h:43
int wonder_city
Definition: advdata.h:46
std::vector< adv_want > government_want
Definition: advdata.h:114
enum req_range impr_range[B_LAST]
Definition: advdata.h:50
int science_priority
Definition: advdata.h:106
int gold_priority
Definition: advdata.h:105
struct adv_data::@86 explore
struct player * spacerace_leader
Definition: advdata.h:94
bool allied_with_enemy
Definition: advdata.h:38
struct packet_game_info info
Definition: game.h:80
struct government * government_during_revolution
Definition: game.h:85
int num_continents
Definition: map_types.h:74
int num_oceans
Definition: map_types.h:75
int love[MAX_NUM_PLAYER_SLOTS]
Definition: player.h:117
enum diplstate_type type
Definition: player.h:193
int mfg
Definition: player.h:95
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
struct player_ai ai_common
Definition: player.h:270
struct player::@65::@67 server
struct government * government
Definition: player.h:240
const struct ai_type * ai
Definition: player.h:271
struct unit_list * units
Definition: player.h:264
bool is_alive
Definition: player.h:250
struct player_score score
Definition: player.h:265
Tech_type_id researching
Definition: research.h:45
struct unit_class::@80 adv
enum move_level sea_move
Definition: unittype.h:136
struct unit_type::@82 adv
universals_u value
Definition: fc_types.h:739
struct civ_map map
Definition: world_object.h:21
bool is_future_tech(Tech_type_id tech)
Is the given tech a future tech.
Definition: tech.cpp:268
Tech_type_id advance_number(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:85
#define advance_index_iterate_end
Definition: tech.h:226
#define A_FIRST
Definition: tech.h:37
#define A_NONE
Definition: tech.h:36
#define A_UNSET
Definition: tech.h:41
#define advance_index_iterate(_start, _index)
Definition: tech.h:221
#define is_ocean_tile(ptile)
Definition: terrain.h:279
#define tile_continent(_tile)
Definition: tile.h:74
const struct impr_type * building
Definition: fc_types.h:579
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Return TRUE iff this unit can do the specified generalized (ruleset defined) action enabler controlle...
Definition: unit.cpp:309
int get_transporter_capacity(const struct unit *punit)
Return the number of units the transporter can hold (or 0).
Definition: unit.cpp:280
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Return whether this player can upgrade this unit type (to any other unit type).
Definition: unittype.cpp:1379
int num_role_units(int role)
How many unit types have specified role/flag.
Definition: unittype.cpp:1866
struct unit_class * unit_class_get(const struct unit *punit)
Returns unit class pointer for a unit.
Definition: unittype.cpp:2151
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype)
Whether player can build given unit somewhere, ignoring whether unit is obsolete and assuming the pla...
Definition: unittype.cpp:1628
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
Definition: unittype.cpp:176
struct unit_type * get_role_unit(int role, int role_index)
Return index-th unit with specified role/flag.
Definition: unittype.cpp:1900
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Return TRUE iff units of the given type can do the specified generalized (ruleset defined) action ena...
Definition: unittype.cpp:386
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition: unittype.h:704
#define unit_class_iterate(_p)
Definition: unittype.h:823
@ MOVE_NONE
Definition: unittype.h:115
#define uclass_index(_c_)
Definition: unittype.h:684
#define unit_class_iterate_end
Definition: unittype.h:829