Freeciv-3.4
Loading...
Searching...
No Matches
advdata.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2002 - The Freeciv Project
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/* utility */
19#include "log.h"
20#include "mem.h"
21
22/* common */
23#include "actions.h"
24#include "ai.h"
25#include "city.h"
26#include "effects.h"
27#include "game.h"
28#include "government.h"
29#include "map.h"
30#include "movement.h"
31#include "research.h"
32#include "unit.h"
33#include "unitlist.h"
34
35/* common/aicore */
36#include "aisupport.h"
37#include "path_finding.h"
38#include "pf_tools.h"
39
40/* server */
41#include "cityturn.h"
42#include "diplhand.h"
43#include "maphand.h"
44#include "plrhand.h"
45#include "srv_log.h"
46#include "unittools.h"
47
48/* server/advisors */
49#include "advbuilding.h"
50#include "advcity.h"
51#include "advtools.h"
52
53/* ai */
54#include "handicaps.h"
55
56#include "advdata.h"
57
58static void adv_dipl_new(const struct player *plr1,
59 const struct player *plr2);
60static void adv_dipl_free(const struct player *plr1,
61 const struct player *plr2);
62static struct adv_dipl *adv_dipl_get(const struct player *plr1,
63 const struct player *plr2);
64
65/**********************************************************************/
73static void adv_data_city_impr_calc(struct player *pplayer,
74 struct adv_data *adv)
75{
76 int count[ADV_IMPR_LAST];
77
78 memset(count, 0, sizeof(count));
79
80 improvement_iterate(pimprove) {
81 struct universal source = {
83 .value = {.building = pimprove}
84 };
85
87
88 /* Find largest extension */
90 switch (peffect->type) {
91#if 0
92 /* TODO */
99 case EFT_MAKE_HAPPY:
100#endif /* 0 */
101 case EFT_CAPITAL_CITY:
105 case EFT_OUTPUT_BONUS:
108 case EFT_UPKEEP_FREE:
110 if (VUT_IMPROVEMENT == preq->source.kind
111 && preq->source.value.building == pimprove) {
112 if (adv->impr_calc[improvement_index(pimprove)] != ADV_IMPR_CALCULATE_FULL) {
114 }
115 if (preq->range > adv->impr_range[improvement_index(pimprove)]) {
116 adv->impr_range[improvement_index(pimprove)] = preq->range;
117 }
118 }
120 break;
125 if (VUT_IMPROVEMENT == preq->source.kind
126 && preq->source.value.building == pimprove) {
128 if (preq->range > adv->impr_range[improvement_index(pimprove)]) {
129 adv->impr_range[improvement_index(pimprove)] = preq->range;
130 }
131 }
133 break;
134 default:
135 /* Nothing! */
136 break;
137 }
140}
141
142/**********************************************************************/
148{
149 struct research *presearch, *aresearch;
151
152 if (players_needed == 0) {
153 return FALSE;
154 }
155
156 presearch = research_get(pplayer);
158 int players_having;
159
163 continue;
164 }
165
166 players_having = 0;
167
169 if (aplayer == pplayer) {
170 continue;
171 }
172
175 || aresearch->researching == tech) {
178 return TRUE;
179 }
180 }
183
184 return FALSE;
185}
186
187/**********************************************************************/
193{
194 struct adv_data *adv = pplayer->server.adv;
195
196 fc_assert_ret(adv != nullptr);
197
198 adv_data_city_impr_calc(pplayer, adv);
199}
200
201/**********************************************************************/
204static void count_my_units(struct player *pplayer)
205{
206 struct adv_data *adv = adv_data_get(pplayer, nullptr);
207
208 memset(&adv->stats.units, 0, sizeof(adv->stats.units));
209
210 unit_list_iterate(pplayer->units, punit) {
212
213 adv->stats.units.byclass[uclass_index(pclass)]++;
214
216 adv->stats.units.coast_strict++;
217 }
219 adv->stats.units.suicide_attackers++;
220 }
235 adv->stats.units.teleporters++;
236 }
238 adv->stats.units.airliftable++;
239 }
240 if (can_upgrade_unittype(pplayer, unit_type_get(punit)) >= 0) {
241 adv->stats.units.upgradeable++;
242 }
244}
245
246/**********************************************************************/
250bool is_adv_data_phase_open(struct player *pplayer)
251{
252 struct adv_data *adv = pplayer->server.adv;
253
254 return adv->phase_is_initialized;
255}
256
257/**********************************************************************/
270bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
271{
272 struct adv_data *adv = pplayer->server.adv;
273 bool danger_of_nukes;
275
276 {
277 int i = 0;
278
279 /* Conventional nukes */
282 /* TODO: worry about spy nuking too? */
284 }
285
286 fc_assert_ret_val(adv != nullptr, FALSE);
287
288 if (adv->phase_is_initialized) {
289 return FALSE;
290 }
292
294
296
297 /*** Threats ***/
298
301 adv->continents = fc_calloc(adv->num_continents + 1, sizeof(struct adv_area_info));
302 adv->oceans = fc_calloc(adv->num_oceans + 1, sizeof(struct adv_area_info));
303 adv->threats.invasions = FALSE;
304 adv->threats.nuclear = 0; /* None */
305 adv->threats.igwall = FALSE;
306
307 whole_map_iterate(&(wld.map), ptile) {
308 Continent_id cont = tile_continent(ptile);
309
310 if (cont >= 0) {
311 adv->continents[cont].size++;
312 } else {
313 adv->oceans[-cont].size++;
314 }
316
318 if (!adv_is_player_dangerous(pplayer, aplayer)) {
319 continue;
320 }
321
322 /* The idea is that if there aren't any hostile cities on
323 * our continent, the danger of land attacks is not big
324 * enough to warrant city walls. Concentrate instead on
325 * coastal fortresses and hunting down enemy transports. */
328
329 if (continent >= 0) {
331 }
333
335 const struct unit_class *pclass = unit_class_get(punit);
336
338 adv->threats.igwall = TRUE;
339 }
340
341 if (pclass->adv.sea_move != MOVE_NONE) {
342 /* If the enemy has not started sailing yet, or we have total
343 * control over the seas, don't worry, keep attacking. */
345 /* Enemy represents a cross-continental threat! */
346 adv->threats.invasions = TRUE;
347 } else if (get_transporter_capacity(punit) > 0) {
352 /* Enemy can transport some threatening units! */
353 adv->threats.invasions = TRUE;
354 break;
355 }
357 }
358
359 /* The idea is that while our enemies don't have any offensive
360 * seaborne units, we don't have to worry. Go on the offensive! */
361 if (unit_type_get(punit)->attack_strength > 1) {
364
365 adv->oceans[-continent].threat = TRUE;
366 } else {
368 if (is_ocean_tile(tile2)) {
369 Continent_id continent = tile_continent(tile2);
370
371 adv->oceans[-continent].threat = TRUE;
372 }
374 }
375 }
376 continue;
377 }
378
379 /* If our enemy builds missiles, worry about missile defense. */
381 && unit_type_get(punit)->attack_strength > 1) {
382 adv->threats.suicide_attack = TRUE;
383 }
384
385 /* If they build nukes, worry a lot. */
387 if (unit_can_do_action(punit, act_id)) {
389 }
392
393 /* Check for nuke capability */
395 int i;
397
398 for (i = 0; i < nuke_units; i++) {
399 struct unit_type *nuke
401
403 RPT_CERTAIN, FALSE)) {
404 adv->threats.nuclear = 1;
405 }
406 }
409
410 /* Increase from fear to terror if opponent actually has nukes */
411 if (danger_of_nukes) {
412 adv->threats.nuclear++; /* Sum of both fears */
413 }
414
415 /*** Exploration ***/
416
417 adv->explore.land_done = TRUE;
418 adv->explore.sea_done = TRUE;
419 adv->explore.continent = fc_calloc(adv->num_continents + 1, sizeof(bool));
420 adv->explore.ocean = fc_calloc(adv->num_oceans + 1, sizeof(bool));
421
422 whole_map_iterate(&(wld.map), ptile) {
423 Continent_id continent = tile_continent(ptile);
424
425 if (is_ocean_tile(ptile)) {
426 if (adv->explore.sea_done && has_handicap(pplayer, H_TARGETS)
427 && !map_is_known(ptile, pplayer)) {
428 /* We're not done there. */
429 adv->explore.sea_done = FALSE;
430 adv->explore.ocean[-continent] = TRUE;
431 }
432 /* Skip rest, which is land only */
433 continue;
434 }
435 if (adv->explore.continent[tile_continent(ptile)]) {
436 /* We don't need more explaining, we got the point */
437 continue;
438 }
439 if (hut_on_tile(ptile)
440 && (!has_handicap(pplayer, H_HUTS)
441 || map_is_known(ptile, pplayer))) {
442 adv->explore.land_done = FALSE;
443 adv->explore.continent[continent] = TRUE;
444 continue;
445 }
446 if (has_handicap(pplayer, H_TARGETS) && !map_is_known(ptile, pplayer)) {
447 /* This AI must explore */
448 adv->explore.land_done = FALSE;
449 adv->explore.continent[continent] = TRUE;
450 }
452
453 /*** Statistics ***/
454
455 adv->stats.cities = fc_calloc(adv->num_continents + 1, sizeof(int));
456 adv->stats.ocean_cities = fc_calloc(adv->num_oceans + 1, sizeof(int));
457 adv->stats.average_production = 0;
458 city_list_iterate(pplayer->cities, pcity) {
460
461 if (continent >= 0) {
462 adv->stats.cities[continent]++;
463 } else {
464 adv->stats.ocean_cities[-continent]++;
465 }
466 adv->stats.average_production += pcity->surplus[O_SHIELD];
468 adv->stats.average_production /= MAX(1, city_list_size(pplayer->cities));
469
470 /*** Diplomacy ***/
471
473 struct adv_dipl *dip = adv_dipl_get(pplayer, aplayer);
474
475 dip->allied_with_enemy = FALSE;
478 && player_diplstate_get(pplayer, check_pl)->type == DS_WAR) {
479 dip->allied_with_enemy = TRUE;
480 }
483
485
486 adv->dipl.production_leader = nullptr;
488 if (adv->dipl.production_leader == nullptr
489 || adv->dipl.production_leader->score.mfg < aplayer->score.mfg) {
491 }
493
494 adv->dipl.tech_leader = nullptr;
496 if (adv->dipl.tech_leader == nullptr
497 || adv->dipl.tech_leader->score.techs < aplayer->score.techs) {
498 adv->dipl.tech_leader = aplayer;
499 }
501
502 /*** Priorities ***/
503
504 /* NEVER set these to zero! Weight values are usually multiplied by
505 * these values, so be careful with them. They are used in city
506 * and government calculations, and food and shields should be
507 * slightly bigger because we only look at surpluses there. They
508 * are all WAGs. */
511 if (adv_wants_science(pplayer)) {
512 adv->luxury_priority = 1;
514 } else {
516 adv->science_priority = 1;
517 }
519 adv->happy_priority = 1;
520 adv->unhappy_priority = TRADE_WEIGHTING; /* Danger */
521 adv->angry_priority = TRADE_WEIGHTING * 3; /* Grave danger */
524
525 /* Research want */
526 if (is_future_tech(research_get(pplayer)->researching)
528 adv->wants_science = FALSE;
529 } else {
530 adv->wants_science = TRUE;
531 }
532
533 /* Max num cities
534 * The idea behind this code is that novice players don't understand that
535 * expansion is critical and find it very annoying.
536 * With the following code AI players will try to be only a bit better
537 * than the best human players. This should lead to more exciting games
538 * for the beginners.
539 */
540 if (has_handicap(pplayer, H_EXPANSION)) {
541 bool found_human = FALSE;
542
543 adv->max_num_cities = 3;
545 if (aplayer == pplayer || is_ai(aplayer)) {
546 continue;
547 }
549 city_list_size(aplayer->cities) + 3);
552 if (!found_human) {
554 }
555 } else {
557 }
558
559 count_my_units(pplayer);
560
562
563 /* Government */
565 adv_best_government(pplayer);
567
568 return TRUE;
569}
570
571/**********************************************************************/
574void adv_data_phase_done(struct player *pplayer)
575{
576 struct adv_data *adv = pplayer->server.adv;
577
578 fc_assert_ret(adv != nullptr);
579
580 if (!adv->phase_is_initialized) {
581 return;
582 }
583
584 free(adv->explore.ocean);
585 adv->explore.ocean = nullptr;
586
587 free(adv->explore.continent);
588 adv->explore.continent = nullptr;
589
590 free(adv->continents);
591 adv->continents = nullptr;
592
593 free(adv->oceans);
594 adv->oceans = nullptr;
595
596 free(adv->stats.cities);
597 adv->stats.cities = nullptr;
598
599 free(adv->stats.ocean_cities);
600 adv->stats.ocean_cities = nullptr;
601
602 adv->num_continents = 0;
603 adv->num_oceans = 0;
604
606}
607
608/**********************************************************************/
614struct adv_data *adv_data_get(struct player *pplayer, bool *caller_closes)
615{
616 struct adv_data *adv = pplayer->server.adv;
617
618 fc_assert_ret_val(adv != nullptr, nullptr);
619
620 /* It's certainly an indication of a bug causing problems
621 if this adv_data_get() gets called between adv_data_phase_done() and
622 adv_data_phase_init(), since we may end up calling those
623 functions if number of known continents has changed.
624
625 Consider following case:
626 Correct call order would be:
627 a) adv_data_phase_init()
628 b) adv_data_get() -> adv_data_phase_done()
629 c) adv_data_get() -> adv_data_phase_init()
630 d) adv_data_phase_done()
631 e) do something
632 f) adv_data_phase_init()
633
634 In (e) data phase would be closed and data would be
635 correctly initialized at (f), which is probably beginning
636 next turn.
637
638 Buggy version where adv_data_get() (b&c) gets called after (d):
639 a) adv_data_phase_init()
640 d) adv_data_phase_done()
641 b) adv_data_get() -> adv_data_phase_done()
642 c) adv_data_get() -> adv_data_phase_init()
643 e) do something
644 f) adv_data_phase_init()
645
646 Now in (e) data phase would be open. When adv_data_phase_init()
647 then finally gets called and it really should recreate data
648 to match situation of new turn, it detects that data phase
649 is already initialized and does nothing.
650
651 So, this assertion is here for a reason!
652
653 Code below tries to fix the situation best it can if such a bug is
654 encountered. Since we are probably going to trust that to be enough
655 instead of making intrusive fixes for actual bug in stable branch,
656 do not assert for non-debug builds of stable versions. */
657#if defined(FREECIV_DEBUG) || defined(IS_DEVEL_VERSION)
658 fc_assert(caller_closes != nullptr || adv->phase_is_initialized);
659#endif
660
661 if (caller_closes != nullptr) {
663 }
664
666 || adv->num_oceans != wld.map.num_oceans) {
667 /* We discovered more continents, recalculate! */
668
669 if (adv->phase_is_initialized) {
670 /* Only call these in this order if inside data phase.
671 This is blanket "fix" for all cases where adv_data_get() is called
672 at illegal time. This at least minimize bad effects of such calls. */
673 adv_data_phase_done(pplayer);
674 adv_data_phase_init(pplayer, FALSE);
675 } else {
676 /* Call them in "wrong" order so we return recalculated data to caller,
677 but leave data phase closed.
678 This is blanket "fix" for all cases where adv_data_get() is called
679 at illegal time. This at least minimize bad effects of such calls.
680
681 Arguably this is not buggy at all but works just as designed in
682 case of being called in alternate movement mode for players
683 other than currently moving one (for diplomacy between the two,
684 for example) */
685 log_debug("%s advisor data phase closed when adv_data_get() called",
686 player_name(pplayer));
687 adv_data_phase_init(pplayer, FALSE);
688 if (caller_closes != nullptr) {
690 } else {
691 adv_data_phase_done(pplayer);
692 }
693 }
694 } else {
695 if (!adv->phase_is_initialized && caller_closes != nullptr) {
696 adv_data_phase_init(pplayer, FALSE);
698 }
699 }
700
701 return adv;
702}
703
704/**********************************************************************/
707void adv_data_init(struct player *pplayer)
708{
709 struct adv_data *adv;
710
711 if (pplayer->server.adv == nullptr) {
712 pplayer->server.adv = fc_calloc(1, sizeof(*pplayer->server.adv));
713 }
714 adv = pplayer->server.adv;
715
716 adv->government_want = nullptr;
717
719 sizeof(*adv->dipl.adv_dipl_slots));
720 player_slots_iterate(pslot) {
721 struct adv_dipl **dip_slot
722 = adv->dipl.adv_dipl_slots + player_slot_index(pslot);
723
724 *dip_slot = nullptr;
726
728 adv_dipl_new(pplayer, aplayer);
729 if (aplayer != pplayer) {
730 adv_dipl_new(aplayer, pplayer);
731 }
733
734 adv_data_default(pplayer);
735}
736
737/**********************************************************************/
740void adv_data_default(struct player *pplayer)
741{
742 struct adv_data *adv = pplayer->server.adv;
743
744 fc_assert_ret(adv != nullptr);
745
746 adv->govt_reeval = 0;
748 (government_count() + 1)
749 * sizeof(*adv->government_want));
750 memset(adv->government_want, 0,
751 (government_count() + 1) * sizeof(*adv->government_want));
752
753 adv->wonder_city = 0;
754
755 adv->wants_science = TRUE;
756 adv->celebrate = FALSE;
757 adv->max_num_cities = 10000;
758}
759
760/**********************************************************************/
763void adv_data_close(struct player *pplayer)
764{
765 struct adv_data *adv = pplayer->server.adv;
766
767 fc_assert_ret(adv != nullptr);
768
769 adv_data_phase_done(pplayer);
770
771 if (adv->government_want != nullptr) {
772 free(adv->government_want);
773 }
774
775 if (adv->dipl.adv_dipl_slots != nullptr) {
777 adv_dipl_free(pplayer, aplayer);
778 if (aplayer != pplayer) {
779 adv_dipl_free(aplayer, pplayer);
780 }
783 }
784
785 if (adv != nullptr) {
786 free(adv);
787 }
788
789 pplayer->server.adv = nullptr;
790}
791
792/**********************************************************************/
795static void adv_dipl_new(const struct player *plr1,
796 const struct player *plr2)
797{
798 struct adv_dipl **dip_slot =
800
801 *dip_slot = fc_calloc(1, sizeof(struct adv_dipl));
802}
803
804/**********************************************************************/
807static void adv_dipl_free(const struct player *plr1,
808 const struct player *plr2)
809{
810 struct adv_dipl **dip_slot
811 = plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
812
813 if (*dip_slot != nullptr) {
815 *dip_slot = nullptr;
816 }
817}
818
819/**********************************************************************/
822static struct adv_dipl *adv_dipl_get(const struct player *plr1,
823 const struct player *plr2)
824{
825 struct adv_dipl **dip_slot
826 = plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
827
828 return *dip_slot;
829}
830
831/**********************************************************************/
835{
836 adv_want bonus = 0;
837
838 /* TODO: Individual and well balanced value. */
839 action_iterate(act) {
840 struct action *paction = action_by_number(act);
841
842 if (!action_immune_government(gov, act)) {
843 /* This government doesn't provide immunity against this
844 * action. */
845 continue;
846 }
847
848 switch (paction->result) {
849 case ACTRES_ATTACK:
853 bonus += 4;
854 break;
856 bonus += 2;
857 break;
859 bonus += 1.5;
860 break;
861 case ACTRES_CULTIVATE:
862 case ACTRES_PLANT:
863 bonus += 0.3;
864 break;
866 case ACTRES_PILLAGE:
867 bonus += 0.2;
868 break;
871 bonus += 0.3;
872 break;
873 case ACTRES_HUT_ENTER:
875 /* It is mine. My own. My precious. */
876 bonus += 0.1;
877 break;
890 case ACTRES_BOMBARD:
891 case ACTRES_SPY_NUKE:
892 case ACTRES_NUKE:
899 /* Being a target of this is usually undesirable */
900 /* TODO: Individual and well balanced values. */
901 bonus += 0.1;
902 break;
903
904 case ACTRES_UNIT_MOVE:
905 case ACTRES_TELEPORT:
911 case ACTRES_PARADROP:
913 case ACTRES_FORTIFY:
915 /* Wants the ability to do this to it self. Don't want others
916 * to target it. Do nothing since action_immune_government()
917 * doesn't separate based on who the actor is. */
918 break;
919
920 case ACTRES_NONE:
921 /* Ruleset defined */
922 break;
923
926 case ACTRES_JOIN_CITY:
929 case ACTRES_HOME_CITY:
930 case ACTRES_HOMELESS:
932 case ACTRES_AIRLIFT:
933 case ACTRES_HEAL_UNIT:
934 case ACTRES_ROAD:
935 case ACTRES_CONVERT:
936 case ACTRES_BASE:
937 case ACTRES_MINE:
938 case ACTRES_IRRIGATE:
939 case ACTRES_CLEAN:
946 /* Could be good. An embassy gives permanent contact. A trade
947 * route gives gold per turn. Join city gives population.
948 * Help wonder gives shields. */
949 /* TODO: Individual and well balanced values. */
950 break;
951
953 }
955
956 return bonus;
957}
958
959/**********************************************************************/
966{
967 adv_want bonus = 0;
968
969 /* Bonuses for non-economic abilities. We increase val by
970 * a very small amount here to choose govt in cases where
971 * we have no cities yet. */
972 bonus += get_player_bonus(pplayer, EFT_VETERAN_BUILD) > 0 ? 3 : 0;
973 bonus += get_player_bonus(pplayer, EFT_INSPIRE_PARTISANS) > 0 ? 3 : 0;
974 bonus += get_player_bonus(pplayer, EFT_RAPTURE_GROW) > 0 ? 2 : 0;
975 bonus += get_player_bonus(pplayer, EFT_FANATICS) > 0 ? 3 : 0;
976 bonus += get_player_bonus(pplayer, EFT_OUTPUT_INC_TILE) * 8;
977
978 return bonus;
979}
980
981/**********************************************************************/
993void adv_best_government(struct player *pplayer)
994{
995 struct adv_data *adv = adv_data_get(pplayer, nullptr);
996 int best_val = 0;
998
999 adv->goal.govt.gov = current_gov;
1000 adv->goal.govt.val = 0;
1001 adv->goal.govt.req = A_UNSET;
1003
1004 if (has_handicap(pplayer, H_AWAY) || !pplayer->is_alive) {
1005 return;
1006 }
1007
1008 if (adv->govt_reeval == 0) {
1009 const struct research *presearch = research_get(pplayer);
1010
1011 governments_iterate(gov) {
1012 adv_want val = 0;
1013 bool override = FALSE;
1014
1016 continue; /* Pointless */
1017 }
1018 if (gov->ai.better
1019 && can_change_to_government(pplayer, gov->ai.better)) {
1020 continue; /* We have better governments available */
1021 }
1022
1023 CALL_PLR_AI_FUNC(gov_value, pplayer, pplayer, gov, &val, &override);
1024
1025 if (!override) {
1026 int dist;
1027 adv_want bonus = 0; /* In percentage */
1028 int revolution_turns;
1029
1030 pplayer->government = gov;
1031 /* Ideally we should change tax rates here, but since
1032 * this is a rather big CPU operation, we'd rather not. */
1033 check_player_max_rates(pplayer);
1034 city_list_iterate(pplayer->cities, acity) {
1037 city_list_iterate(pplayer->cities, pcity) {
1038 val += adv_eval_calc_city(pcity, adv);
1040
1041 bonus += adv_gov_action_immunity_want(gov);
1042 bonus += adv_gov_player_bonus_want(pplayer);
1043
1045 if (revolution_turns > 0) {
1046 bonus -= 6 / revolution_turns;
1047 }
1048
1049 val += (val * bonus) / 100;
1050
1051 /* FIXME: handle reqs other than technologies. */
1052 dist = 0;
1053 requirement_vector_iterate(&gov->reqs, preq) {
1054 if (VUT_ADVANCE == preq->source.kind) {
1056 advance_number(preq->source.value.advance));
1057
1058 dist += MAX(1, gut);
1059 }
1061 val = amortize(val, dist);
1062 }
1063
1064 adv->government_want[government_index(gov)] = val; /* Save want */
1066 /* Now reset our gov to it's real state. */
1067 pplayer->government = current_gov;
1068 city_list_iterate(pplayer->cities, acity) {
1071 if (player_is_cpuhog(pplayer)) {
1072 adv->govt_reeval = 1;
1073 } else {
1074 adv->govt_reeval = CLIP(5, city_list_size(pplayer->cities), 20);
1075 }
1076 }
1077 adv->govt_reeval--;
1078
1079 /* Figure out which government is the best for us this turn. */
1080 governments_iterate(gov) {
1081 int gi = government_index(gov);
1082 if (adv->government_want[gi] > best_val
1083 && can_change_to_government(pplayer, gov)) {
1084 best_val = adv->government_want[gi];
1085 adv->goal.revolution = gov;
1086 }
1087 if (adv->government_want[gi] > adv->goal.govt.val) {
1088 adv->goal.govt.gov = gov;
1089 adv->goal.govt.val = adv->government_want[gi];
1090
1091 /* FIXME: Handle reqs other than technologies. */
1092 adv->goal.govt.req = A_NONE;
1093 requirement_vector_iterate(&gov->reqs, preq) {
1094 if (VUT_ADVANCE == preq->source.kind) {
1095 adv->goal.govt.req = advance_number(preq->source.value.advance);
1096 break;
1097 }
1099 }
1101
1102 /* Goodness of the ideal gov is calculated relative to the goodness of the
1103 * best of the available ones. */
1104 adv->goal.govt.val -= best_val;
1105}
1106
1107/**********************************************************************/
1110bool adv_wants_science(struct player *pplayer)
1111{
1112 return adv_data_get(pplayer, nullptr)->wants_science;
1113}
1114
1115/**********************************************************************/
1121bool adv_is_player_dangerous(struct player *pplayer,
1122 struct player *aplayer)
1123{
1124 struct adv_dipl *dip;
1125 enum diplstate_type ds;
1127
1128 if (is_ai(pplayer)) {
1129 /* Give AI code possibility to decide itself */
1130 CALL_PLR_AI_FUNC(consider_plr_dangerous, pplayer, pplayer, aplayer, &dang);
1131 }
1132
1133 if (dang == OVERRIDE_FALSE) {
1134 return FALSE;
1135 }
1136
1137 if (dang == OVERRIDE_TRUE) {
1138 return TRUE;
1139 }
1140
1141 if (pplayer == aplayer) {
1142 /* We always trust ourself */
1143 return FALSE;
1144 }
1145
1146 ds = player_diplstate_get(pplayer, aplayer)->type;
1147
1148 if (ds == DS_WAR || ds == DS_CEASEFIRE) {
1149 /* It's already a war or aplayer can declare it soon */
1150 return TRUE;
1151 }
1152
1153 dip = adv_dipl_get(pplayer, aplayer);
1154
1155 if (dip->allied_with_enemy) {
1156 /* Don't trust someone who will declare war on us soon */
1157 return TRUE;
1158 }
1159
1160 if (player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel > 0) {
1161 return TRUE;
1162 }
1163
1164 if (pplayer->ai_common.love[player_index(aplayer)] < MAX_AI_LOVE / 10) {
1165 /* We don't trust players who we don't like. Note that
1166 * aplayer's units inside pplayer's borders decreases AI's love */
1167 return TRUE;
1168 }
1169
1170 return FALSE;
1171}
void action_array_add_all_by_result(action_id *act_array, int *position, enum action_result result)
Definition actions.c:6021
void action_array_end(action_id *act_array, int size)
Definition actions.c:6004
bool action_immune_government(struct government *gov, action_id act)
Definition actions.c:5673
static struct action * action_by_number(action_id act_id)
Definition actions.h:400
#define action_array_iterate(_act_array_, _act_id_)
Definition actions.h:261
#define action_id_get_role(act_id)
Definition actions.h:461
#define action_array_iterate_end
Definition actions.h:273
#define action_iterate_end
Definition actions.h:218
#define MAX_NUM_ACTIONS
Definition actions.h:62
#define action_iterate(_act_)
Definition actions.h:214
#define ASSERT_UNUSED_ACTRES_CASES
Definition actres.h:37
#define TRADE_WEIGHTING
Definition advbuilding.h:21
#define INFRA_WEIGHTING
Definition advbuilding.h:29
#define FOOD_WEIGHTING
Definition advbuilding.h:19
#define SHIELD_WEIGHTING
Definition advbuilding.h:20
#define POLLUTION_WEIGHTING
Definition advbuilding.h:28
int adv_eval_calc_city(struct city *pcity, struct adv_data *adv)
Definition advcity.c:32
static void adv_data_city_impr_calc(struct player *pplayer, struct adv_data *adv)
Definition advdata.c:73
void adv_data_analyze_rulesets(struct player *pplayer)
Definition advdata.c:192
void adv_data_close(struct player *pplayer)
Definition advdata.c:763
adv_want adv_gov_player_bonus_want(struct player *pplayer)
Definition advdata.c:965
void adv_data_init(struct player *pplayer)
Definition advdata.c:707
static void adv_dipl_free(const struct player *plr1, const struct player *plr2)
Definition advdata.c:807
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:614
bool is_adv_data_phase_open(struct player *pplayer)
Definition advdata.c:250
bool adv_wants_science(struct player *pplayer)
Definition advdata.c:1110
bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
Definition advdata.c:270
void adv_data_phase_done(struct player *pplayer)
Definition advdata.c:574
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
Definition advdata.c:1121
static void adv_dipl_new(const struct player *plr1, const struct player *plr2)
Definition advdata.c:795
static void count_my_units(struct player *pplayer)
Definition advdata.c:204
adv_want adv_gov_action_immunity_want(struct government *gov)
Definition advdata.c:834
static struct adv_dipl * adv_dipl_get(const struct player *plr1, const struct player *plr2)
Definition advdata.c:822
static bool player_has_really_useful_tech_parasite(struct player *pplayer)
Definition advdata.c:147
void adv_best_government(struct player *pplayer)
Definition advdata.c:993
void adv_data_default(struct player *pplayer)
Definition advdata.c:740
@ ADV_IMPR_CALCULATE_FULL
Definition advdata.h:35
@ ADV_IMPR_LAST
Definition advdata.h:37
@ ADV_IMPR_ESTIMATE
Definition advdata.h:36
@ ADV_IMPR_CALCULATE
Definition advdata.h:34
adv_want amortize(adv_want benefit, int delay)
Definition advtools.c:29
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:380
struct player * player_leading_spacerace(void)
Definition aisupport.c:38
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_list_iterate_end
Definition city.h:510
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:367
char * incite_cost
Definition comments.c:77
static void nuke_units(QVariant data1, QVariant data2)
Definition dialogs.cpp:2752
static void nuke(QVariant data1, QVariant data2)
Definition dialogs.cpp:3040
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 unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
struct effect_list * get_req_source_effects(const struct universal *psource)
Definition effects.c:153
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:824
#define effect_list_iterate_end
Definition effects.h:81
#define effect_list_iterate(effect_list, peffect)
Definition effects.h:79
bool hut_on_tile(const struct tile *ptile)
Definition extras.c:705
float adv_want
Definition fc_types.h:1050
@ RPT_CERTAIN
Definition fc_types.h:516
int action_id
Definition fc_types.h:250
#define ACTRES_NONE
Definition fc_types.h:188
@ O_SHIELD
Definition fc_types.h:103
signed short Continent_id
Definition fc_types.h:233
override_bool
Definition fc_types.h:96
@ OVERRIDE_TRUE
Definition fc_types.h:96
@ NO_OVERRIDE
Definition fc_types.h:96
@ OVERRIDE_FALSE
Definition fc_types.h:96
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
Government_type_id government_count(void)
Definition government.c:73
struct government * government_of_player(const struct player *pplayer)
Definition government.c:119
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:178
Government_type_id government_index(const struct government *pgovern)
Definition government.c:84
#define governments_iterate(NAME_pgov)
Definition government.h:127
#define governments_iterate_end
Definition government.h:130
static GtkWidget * source
Definition gotodlg.c:58
GType type
Definition repodlgs.c:1313
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
Definition handicaps.c:66
@ H_TARGETS
Definition handicaps.h:24
@ H_AWAY
Definition handicaps.h:19
@ H_HUTS
Definition handicaps.h:25
@ H_EXPANSION
Definition handicaps.h:31
Impr_type_id improvement_index(const struct impr_type *pimprove)
#define improvement_iterate_end
#define improvement_iterate(_p)
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_debug(message,...)
Definition log.h:116
#define adjc_iterate_end
Definition map.h:439
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:434
#define whole_map_iterate(_map, _tile)
Definition map.h:582
#define whole_map_iterate_end
Definition map.h:591
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:899
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_realloc(ptr, sz)
Definition mem.h:36
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:919
int player_slot_count(void)
Definition player.c:415
const char * player_name(const struct player *pplayer)
Definition player.c:885
int player_slot_index(const struct player_slot *pslot)
Definition player.c:423
int player_index(const struct player *pplayer)
Definition player.c:818
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:325
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1397
#define players_iterate_end
Definition player.h:552
#define players_iterate(_pplayer)
Definition player.h:547
#define MAX_AI_LOVE
Definition player.h:576
#define player_slots_iterate(_pslot)
Definition player.h:538
#define is_ai(plr)
Definition player.h:232
#define players_iterate_alive_end
Definition player.h:562
#define player_slots_iterate_end
Definition player.h:542
static bool player_is_cpuhog(const struct player *pplayer)
Definition player.h:590
#define players_iterate_alive(_pplayer)
Definition player.h:557
void check_player_max_rates(struct player *pplayer)
Definition plrhand.c:776
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
int research_goal_unknown_techs(const struct research *presearch, Tech_type_id goal)
Definition research.c:753
struct research * research_get(const struct player *pplayer)
Definition research.c:130
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:622
bool research_invention_gettable(const struct research *presearch, const Tech_type_id tech, bool allow_holes)
Definition research.c:696
#define CLIP(lower, current, upper)
Definition shared.h:57
#define MAX(x, y)
Definition shared.h:54
@ AIT_GOVERNMENT
Definition srv_log.h:47
@ AIT_AIDATA
Definition srv_log.h:46
@ TIMER_STOP
Definition srv_log.h:76
@ TIMER_START
Definition srv_log.h:76
#define TIMING_LOG(timer, activity)
Definition srv_log.h:125
bool threat
Definition advdata.h:47
bool wants_science
Definition advdata.h:139
int req
Definition advdata.h:133
short govt_reeval
Definition advdata.h:126
adv_want val
Definition advdata.h:132
int pollution_priority
Definition advdata.h:121
int shield_priority
Definition advdata.h:113
bool * ocean
Definition advdata.h:74
struct adv_data::@95 dipl
struct adv_data::@96::@98 govt
int * cities
Definition advdata.h:96
int * ocean_cities
Definition advdata.h:97
bool invasions
Definition advdata.h:66
struct government * revolution
Definition advdata.h:135
int infra_priority
Definition advdata.h:122
struct adv_dipl ** adv_dipl_slots
Definition advdata.h:102
bool igwall
Definition advdata.h:69
bool celebrate
Definition advdata.h:142
int num_oceans
Definition advdata.h:110
struct adv_data::@96 goal
int unhappy_priority
Definition advdata.h:119
int luxury_priority
Definition advdata.h:115
int nuclear
Definition advdata.h:68
int angry_priority
Definition advdata.h:120
int food_priority
Definition advdata.h:114
int max_num_cities
Definition advdata.h:145
int num_continents
Definition advdata.h:109
adv_want * government_want
Definition advdata.h:125
struct adv_area_info * oceans
Definition advdata.h:62
struct player * production_leader
Definition advdata.h:106
struct adv_data::@92 threats
struct government * gov
Definition advdata.h:131
struct adv_area_info * continents
Definition advdata.h:61
int happy_priority
Definition advdata.h:118
bool * continent
Definition advdata.h:75
struct adv_data::@93 explore
struct adv_data::@94::@97 units
enum adv_improvement_status impr_calc[B_LAST]
Definition advdata.h:58
bool phase_is_initialized
Definition advdata.h:52
int wonder_city
Definition advdata.h:55
enum req_range impr_range[B_LAST]
Definition advdata.h:59
int science_priority
Definition advdata.h:117
int gold_priority
Definition advdata.h:116
struct player * tech_leader
Definition advdata.h:105
struct player * spacerace_leader
Definition advdata.h:104
struct adv_data::@94 stats
struct packet_game_info info
Definition game.h:89
struct government * government_during_revolution
Definition game.h:94
Continent_id num_continents
Definition map_types.h:83
Continent_id num_oceans
Definition map_types.h:84
bool tech_parasite_allow_holes
int love[MAX_NUM_PLAYER_SLOTS]
Definition player.h:124
enum diplstate_type type
Definition player.h:199
int techs
Definition player.h:92
struct city_list * cities
Definition player.h:281
struct player_ai ai_common
Definition player.h:288
struct government * government
Definition player.h:258
const struct ai_type * ai
Definition player.h:289
struct unit_list * units
Definition player.h:282
bool is_alive
Definition player.h:268
struct player::@73::@75 server
struct player_score score
Definition player.h:283
struct adv_data * adv
Definition player.h:334
struct unit_class::@88 adv
struct unit_class * uclass
Definition unittype.h:563
struct unit_type::@90 adv
bool igwall
Definition unittype.h:581
struct tile * tile
Definition unit.h:142
const struct unit_type * utype
Definition unit.h:141
enum universals_n kind
Definition fc_types.h:595
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
bool is_future_tech(Tech_type_id tech)
Definition tech.c:286
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:100
#define advance_index_iterate_end
Definition tech.h:246
#define A_FIRST
Definition tech.h:44
#define A_NONE
Definition tech.h:43
#define A_UNSET
Definition tech.h:48
#define advance_index_iterate(_start, _index)
Definition tech.h:242
#define is_ocean_tile(ptile)
Definition terrain.h:197
#define tile_continent(_tile)
Definition tile.h:93
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:402
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:316
#define unit_tile(_pu)
Definition unit.h:408
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:126
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2284
int num_role_units(int role)
Definition unittype.c:2234
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1729
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2535
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:215
bool unit_can_take_over(const struct unit *punit)
Definition unittype.c:289
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype, const enum req_problem_type prob_type, bool consider_reg_impr_req)
Definition unittype.c:1995
bool utype_can_take_over(const struct unit_type *punittype)
Definition unittype.c:301
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:396
@ MOVE_NONE
Definition unittype.h:144
#define unit_type_iterate(_p)
Definition unittype.h:865
#define uclass_index(_c_)
Definition unittype.h:749
#define unit_type_iterate_end
Definition unittype.h:872
#define MAP_INDEX_SIZE