Freeciv-3.3
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 }
227 /* TODO: Cover also teleporting */
228 adv->stats.units.paratroopers++;
229 }
231 adv->stats.units.airliftable++;
232 }
233 if (can_upgrade_unittype(pplayer, unit_type_get(punit)) >= 0) {
234 adv->stats.units.upgradeable++;
235 }
237}
238
239/**********************************************************************/
243bool is_adv_data_phase_open(struct player *pplayer)
244{
245 struct adv_data *adv = pplayer->server.adv;
246
247 return adv->phase_is_initialized;
248}
249
250/**********************************************************************/
263bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
264{
265 struct adv_data *adv = pplayer->server.adv;
266 bool danger_of_nukes;
268
269 {
270 int i = 0;
271
272 /* Conventional nukes */
275 /* TODO: worry about spy nuking too? */
277 }
278
279 fc_assert_ret_val(adv != nullptr, FALSE);
280
281 if (adv->phase_is_initialized) {
282 return FALSE;
283 }
285
287
289
290 /*** Threats ***/
291
294 adv->continents = fc_calloc(adv->num_continents + 1, sizeof(struct adv_area_info));
295 adv->oceans = fc_calloc(adv->num_oceans + 1, sizeof(struct adv_area_info));
296 adv->threats.invasions = FALSE;
297 adv->threats.nuclear = 0; /* None */
298 adv->threats.igwall = FALSE;
299
300 whole_map_iterate(&(wld.map), ptile) {
301 Continent_id cont = tile_continent(ptile);
302
303 if (cont >= 0) {
304 adv->continents[cont].size++;
305 } else {
306 adv->oceans[-cont].size++;
307 }
309
311 if (!adv_is_player_dangerous(pplayer, aplayer)) {
312 continue;
313 }
314
315 /* The idea is that if there aren't any hostile cities on
316 * our continent, the danger of land attacks is not big
317 * enough to warrant city walls. Concentrate instead on
318 * coastal fortresses and hunting down enemy transports. */
321
322 if (continent >= 0) {
324 }
326
328 const struct unit_class *pclass = unit_class_get(punit);
329
331 adv->threats.igwall = TRUE;
332 }
333
334 if (pclass->adv.sea_move != MOVE_NONE) {
335 /* If the enemy has not started sailing yet, or we have total
336 * control over the seas, don't worry, keep attacking. */
338 /* Enemy represents a cross-continental threat! */
339 adv->threats.invasions = TRUE;
340 } else if (get_transporter_capacity(punit) > 0) {
345 /* Enemy can transport some threatening units! */
346 adv->threats.invasions = TRUE;
347 break;
348 }
350 }
351
352 /* The idea is that while our enemies don't have any offensive
353 * seaborne units, we don't have to worry. Go on the offensive! */
354 if (unit_type_get(punit)->attack_strength > 1) {
357
358 adv->oceans[-continent].threat = TRUE;
359 } else {
361 if (is_ocean_tile(tile2)) {
362 Continent_id continent = tile_continent(tile2);
363
364 adv->oceans[-continent].threat = TRUE;
365 }
367 }
368 }
369 continue;
370 }
371
372 /* If our enemy builds missiles, worry about missile defense. */
374 && unit_type_get(punit)->attack_strength > 1) {
375 adv->threats.suicide_attack = TRUE;
376 }
377
378 /* If they build nukes, worry a lot. */
380 if (unit_can_do_action(punit, act_id)) {
382 }
385
386 /* Check for nuke capability */
388 int i;
390
391 for (i = 0; i < nuke_units; i++) {
392 struct unit_type *nuke
394
396 adv->threats.nuclear = 1;
397 }
398 }
401
402 /* Increase from fear to terror if opponent actually has nukes */
403 if (danger_of_nukes) {
404 adv->threats.nuclear++; /* Sum of both fears */
405 }
406
407 /*** Exploration ***/
408
409 adv->explore.land_done = TRUE;
410 adv->explore.sea_done = TRUE;
411 adv->explore.continent = fc_calloc(adv->num_continents + 1, sizeof(bool));
412 adv->explore.ocean = fc_calloc(adv->num_oceans + 1, sizeof(bool));
413
414 whole_map_iterate(&(wld.map), ptile) {
415 Continent_id continent = tile_continent(ptile);
416
417 if (is_ocean_tile(ptile)) {
418 if (adv->explore.sea_done && has_handicap(pplayer, H_TARGETS)
419 && !map_is_known(ptile, pplayer)) {
420 /* We're not done there. */
421 adv->explore.sea_done = FALSE;
422 adv->explore.ocean[-continent] = TRUE;
423 }
424 /* Skip rest, which is land only */
425 continue;
426 }
427 if (adv->explore.continent[tile_continent(ptile)]) {
428 /* We don't need more explaining, we got the point */
429 continue;
430 }
431 if (hut_on_tile(ptile)
432 && (!has_handicap(pplayer, H_HUTS)
433 || map_is_known(ptile, pplayer))) {
434 adv->explore.land_done = FALSE;
435 adv->explore.continent[continent] = TRUE;
436 continue;
437 }
438 if (has_handicap(pplayer, H_TARGETS) && !map_is_known(ptile, pplayer)) {
439 /* This AI must explore */
440 adv->explore.land_done = FALSE;
441 adv->explore.continent[continent] = TRUE;
442 }
444
445 /*** Statistics ***/
446
447 adv->stats.cities = fc_calloc(adv->num_continents + 1, sizeof(int));
448 adv->stats.ocean_cities = fc_calloc(adv->num_oceans + 1, sizeof(int));
449 adv->stats.average_production = 0;
450 city_list_iterate(pplayer->cities, pcity) {
452
453 if (continent >= 0) {
454 adv->stats.cities[continent]++;
455 } else {
456 adv->stats.ocean_cities[-continent]++;
457 }
458 adv->stats.average_production += pcity->surplus[O_SHIELD];
460 adv->stats.average_production /= MAX(1, city_list_size(pplayer->cities));
461
462 /*** Diplomacy ***/
463
465 struct adv_dipl *dip = adv_dipl_get(pplayer, aplayer);
466
467 dip->allied_with_enemy = FALSE;
470 && player_diplstate_get(pplayer, check_pl)->type == DS_WAR) {
471 dip->allied_with_enemy = TRUE;
472 }
475
477
478 adv->dipl.production_leader = nullptr;
480 if (adv->dipl.production_leader == nullptr
481 || adv->dipl.production_leader->score.mfg < aplayer->score.mfg) {
483 }
485
486 adv->dipl.tech_leader = nullptr;
488 if (adv->dipl.tech_leader == nullptr
489 || adv->dipl.tech_leader->score.techs < aplayer->score.techs) {
490 adv->dipl.tech_leader = aplayer;
491 }
493
494 /*** Priorities ***/
495
496 /* NEVER set these to zero! Weight values are usually multiplied by
497 * these values, so be careful with them. They are used in city
498 * and government calculations, and food and shields should be
499 * slightly bigger because we only look at surpluses there. They
500 * are all WAGs. */
503 if (adv_wants_science(pplayer)) {
504 adv->luxury_priority = 1;
506 } else {
508 adv->science_priority = 1;
509 }
511 adv->happy_priority = 1;
512 adv->unhappy_priority = TRADE_WEIGHTING; /* Danger */
513 adv->angry_priority = TRADE_WEIGHTING * 3; /* Grave danger */
516
517 /* Research want */
518 if (is_future_tech(research_get(pplayer)->researching)
520 adv->wants_science = FALSE;
521 } else {
522 adv->wants_science = TRUE;
523 }
524
525 /* Max num cities
526 * The idea behind this code is that novice players don't understand that
527 * expansion is critical and find it very annoying.
528 * With the following code AI players will try to be only a bit better
529 * than the best human players. This should lead to more exciting games
530 * for the beginners.
531 */
532 if (has_handicap(pplayer, H_EXPANSION)) {
533 bool found_human = FALSE;
534
535 adv->max_num_cities = 3;
537 if (aplayer == pplayer || is_ai(aplayer)) {
538 continue;
539 }
541 city_list_size(aplayer->cities) + 3);
544 if (!found_human) {
546 }
547 } else {
549 }
550
551 count_my_units(pplayer);
552
554
555 /* Government */
557 adv_best_government(pplayer);
559
560 return TRUE;
561}
562
563/**********************************************************************/
566void adv_data_phase_done(struct player *pplayer)
567{
568 struct adv_data *adv = pplayer->server.adv;
569
570 fc_assert_ret(adv != nullptr);
571
572 if (!adv->phase_is_initialized) {
573 return;
574 }
575
576 free(adv->explore.ocean);
577 adv->explore.ocean = nullptr;
578
579 free(adv->explore.continent);
580 adv->explore.continent = nullptr;
581
582 free(adv->continents);
583 adv->continents = nullptr;
584
585 free(adv->oceans);
586 adv->oceans = nullptr;
587
588 free(adv->stats.cities);
589 adv->stats.cities = nullptr;
590
591 free(adv->stats.ocean_cities);
592 adv->stats.ocean_cities = nullptr;
593
594 adv->num_continents = 0;
595 adv->num_oceans = 0;
596
598}
599
600/**********************************************************************/
606struct adv_data *adv_data_get(struct player *pplayer, bool *caller_closes)
607{
608 struct adv_data *adv = pplayer->server.adv;
609
610 fc_assert_ret_val(adv != nullptr, nullptr);
611
612 /* It's certainly an indication of a bug causing problems
613 if this adv_data_get() gets called between adv_data_phase_done() and
614 adv_data_phase_init(), since we may end up calling those
615 functions if number of known continents has changed.
616
617 Consider following case:
618 Correct call order would be:
619 a) adv_data_phase_init()
620 b) adv_data_get() -> adv_data_phase_done()
621 c) adv_data_get() -> adv_data_phase_init()
622 d) adv_data_phase_done()
623 e) do something
624 f) adv_data_phase_init()
625
626 In (e) data phase would be closed and data would be
627 correctly initialized at (f), which is probably beginning
628 next turn.
629
630 Buggy version where adv_data_get() (b&c) gets called after (d):
631 a) adv_data_phase_init()
632 d) adv_data_phase_done()
633 b) adv_data_get() -> adv_data_phase_done()
634 c) adv_data_get() -> adv_data_phase_init()
635 e) do something
636 f) adv_data_phase_init()
637
638 Now in (e) data phase would be open. When adv_data_phase_init()
639 then finally gets called and it really should recreate data
640 to match situation of new turn, it detects that data phase
641 is already initialized and does nothing.
642
643 So, this assertion is here for a reason!
644
645 Code below tries to fix the situation best it can if such a bug is
646 encountered. Since we are probably going to trust that to be enough
647 instead of making intrusive fixes for actual bug in stable branch,
648 do not assert for non-debug builds of stable versions. */
649#if defined(FREECIV_DEBUG) || defined(IS_DEVEL_VERSION)
650 fc_assert(caller_closes != nullptr || adv->phase_is_initialized);
651#endif
652
653 if (caller_closes != nullptr) {
655 }
656
658 || adv->num_oceans != wld.map.num_oceans) {
659 /* We discovered more continents, recalculate! */
660
661 if (adv->phase_is_initialized) {
662 /* Only call these in this order if inside data phase.
663 This is blanket "fix" for all cases where adv_data_get() is called
664 at illegal time. This at least minimize bad effects of such calls. */
665 adv_data_phase_done(pplayer);
666 adv_data_phase_init(pplayer, FALSE);
667 } else {
668 /* Call them in "wrong" order so we return recalculated data to caller,
669 but leave data phase closed.
670 This is blanket "fix" for all cases where adv_data_get() is called
671 at illegal time. This at least minimize bad effects of such calls.
672
673 Arguably this is not buggy at all but works just as designed in
674 case of being called in alternate movement mode for players
675 other than currently moving one (for diplomacy between the two,
676 for example) */
677 log_debug("%s advisor data phase closed when adv_data_get() called",
678 player_name(pplayer));
679 adv_data_phase_init(pplayer, FALSE);
680 if (caller_closes != nullptr) {
682 } else {
683 adv_data_phase_done(pplayer);
684 }
685 }
686 } else {
687 if (!adv->phase_is_initialized && caller_closes != nullptr) {
688 adv_data_phase_init(pplayer, FALSE);
690 }
691 }
692
693 return adv;
694}
695
696/**********************************************************************/
699void adv_data_init(struct player *pplayer)
700{
701 struct adv_data *adv;
702
703 if (pplayer->server.adv == nullptr) {
704 pplayer->server.adv = fc_calloc(1, sizeof(*pplayer->server.adv));
705 }
706 adv = pplayer->server.adv;
707
708 adv->government_want = nullptr;
709
711 sizeof(*adv->dipl.adv_dipl_slots));
712 player_slots_iterate(pslot) {
713 struct adv_dipl **dip_slot
714 = adv->dipl.adv_dipl_slots + player_slot_index(pslot);
715
716 *dip_slot = nullptr;
718
720 adv_dipl_new(pplayer, aplayer);
721 if (aplayer != pplayer) {
722 adv_dipl_new(aplayer, pplayer);
723 }
725
726 adv_data_default(pplayer);
727}
728
729/**********************************************************************/
732void adv_data_default(struct player *pplayer)
733{
734 struct adv_data *adv = pplayer->server.adv;
735
736 fc_assert_ret(adv != nullptr);
737
738 adv->govt_reeval = 0;
740 (government_count() + 1)
741 * sizeof(*adv->government_want));
742 memset(adv->government_want, 0,
743 (government_count() + 1) * sizeof(*adv->government_want));
744
745 adv->wonder_city = 0;
746
747 adv->wants_science = TRUE;
748 adv->celebrate = FALSE;
749 adv->max_num_cities = 10000;
750}
751
752/**********************************************************************/
755void adv_data_close(struct player *pplayer)
756{
757 struct adv_data *adv = pplayer->server.adv;
758
759 fc_assert_ret(adv != nullptr);
760
761 adv_data_phase_done(pplayer);
762
763 if (adv->government_want != nullptr) {
764 free(adv->government_want);
765 }
766
767 if (adv->dipl.adv_dipl_slots != nullptr) {
769 adv_dipl_free(pplayer, aplayer);
770 if (aplayer != pplayer) {
771 adv_dipl_free(aplayer, pplayer);
772 }
775 }
776
777 if (adv != nullptr) {
778 free(adv);
779 }
780
781 pplayer->server.adv = nullptr;
782}
783
784/**********************************************************************/
787static void adv_dipl_new(const struct player *plr1,
788 const struct player *plr2)
789{
790 struct adv_dipl **dip_slot =
792
793 *dip_slot = fc_calloc(1, sizeof(struct adv_dipl));
794}
795
796/**********************************************************************/
799static void adv_dipl_free(const struct player *plr1,
800 const struct player *plr2)
801{
802 struct adv_dipl **dip_slot
803 = plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
804
805 if (*dip_slot != nullptr) {
807 *dip_slot = nullptr;
808 }
809}
810
811/**********************************************************************/
814static struct adv_dipl *adv_dipl_get(const struct player *plr1,
815 const struct player *plr2)
816{
817 struct adv_dipl **dip_slot
818 = plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
819
820 return *dip_slot;
821}
822
823/**********************************************************************/
827{
828 adv_want bonus = 0;
829
830 /* TODO: Individual and well balanced value. */
831 action_iterate(act) {
832 struct action *paction = action_by_number(act);
833
834 if (!action_immune_government(gov, act)) {
835 /* This government doesn't provide immunity against this
836 * action. */
837 continue;
838 }
839
840 switch (paction->result) {
841 case ACTRES_ATTACK:
845 bonus += 4;
846 break;
848 bonus += 2;
849 break;
851 bonus += 1.5;
852 break;
853 case ACTRES_CULTIVATE:
854 case ACTRES_PLANT:
855 bonus += 0.3;
856 break;
858 case ACTRES_PILLAGE:
859 bonus += 0.2;
860 break;
863 bonus += 0.3;
864 break;
865 case ACTRES_HUT_ENTER:
867 /* It is mine. My own. My precious. */
868 bonus += 0.1;
869 break;
882 case ACTRES_BOMBARD:
883 case ACTRES_SPY_NUKE:
884 case ACTRES_NUKE:
891 /* Being a target of this is usually undesirable */
892 /* TODO: Individual and well balanced values. */
893 bonus += 0.1;
894 break;
895
896 case ACTRES_UNIT_MOVE:
897 case ACTRES_TELEPORT:
903 case ACTRES_PARADROP:
905 case ACTRES_FORTIFY:
907 /* Wants the ability to do this to it self. Don't want others
908 * to target it. Do nothing since action_immune_government()
909 * doesn't separate based on who the actor is. */
910 break;
911
912 case ACTRES_NONE:
913 /* Ruleset defined */
914 break;
915
918 case ACTRES_JOIN_CITY:
921 case ACTRES_HOME_CITY:
922 case ACTRES_HOMELESS:
924 case ACTRES_AIRLIFT:
925 case ACTRES_HEAL_UNIT:
926 case ACTRES_ROAD:
927 case ACTRES_CONVERT:
928 case ACTRES_BASE:
929 case ACTRES_MINE:
930 case ACTRES_IRRIGATE:
931 case ACTRES_CLEAN:
938 /* Could be good. An embassy gives permanent contact. A trade
939 * route gives gold per turn. Join city gives population.
940 * Help wonder gives shields. */
941 /* TODO: Individual and well balanced values. */
942 break;
943
945 }
947
948 return bonus;
949}
950
951/**********************************************************************/
958{
959 adv_want bonus = 0;
960
961 /* Bonuses for non-economic abilities. We increase val by
962 * a very small amount here to choose govt in cases where
963 * we have no cities yet. */
964 bonus += get_player_bonus(pplayer, EFT_VETERAN_BUILD) > 0 ? 3 : 0;
965 bonus += get_player_bonus(pplayer, EFT_INSPIRE_PARTISANS) > 0 ? 3 : 0;
966 bonus += get_player_bonus(pplayer, EFT_RAPTURE_GROW) > 0 ? 2 : 0;
967 bonus += get_player_bonus(pplayer, EFT_FANATICS) > 0 ? 3 : 0;
968 bonus += get_player_bonus(pplayer, EFT_OUTPUT_INC_TILE) * 8;
969
970 return bonus;
971}
972
973/**********************************************************************/
985void adv_best_government(struct player *pplayer)
986{
987 struct adv_data *adv = adv_data_get(pplayer, nullptr);
988 int best_val = 0;
990
991 adv->goal.govt.gov = current_gov;
992 adv->goal.govt.val = 0;
993 adv->goal.govt.req = A_UNSET;
995
996 if (has_handicap(pplayer, H_AWAY) || !pplayer->is_alive) {
997 return;
998 }
999
1000 if (adv->govt_reeval == 0) {
1001 const struct research *presearch = research_get(pplayer);
1002
1003 governments_iterate(gov) {
1004 adv_want val = 0;
1005 bool override = FALSE;
1006
1008 continue; /* Pointless */
1009 }
1010 if (gov->ai.better
1011 && can_change_to_government(pplayer, gov->ai.better)) {
1012 continue; /* We have better governments available */
1013 }
1014
1015 CALL_PLR_AI_FUNC(gov_value, pplayer, pplayer, gov, &val, &override);
1016
1017 if (!override) {
1018 int dist;
1019 adv_want bonus = 0; /* In percentage */
1020 int revolution_turns;
1021
1022 pplayer->government = gov;
1023 /* Ideally we should change tax rates here, but since
1024 * this is a rather big CPU operation, we'd rather not. */
1025 check_player_max_rates(pplayer);
1026 city_list_iterate(pplayer->cities, acity) {
1029 city_list_iterate(pplayer->cities, pcity) {
1030 val += adv_eval_calc_city(pcity, adv);
1032
1033 bonus += adv_gov_action_immunity_want(gov);
1034 bonus += adv_gov_player_bonus_want(pplayer);
1035
1037 if (revolution_turns > 0) {
1038 bonus -= 6 / revolution_turns;
1039 }
1040
1041 val += (val * bonus) / 100;
1042
1043 /* FIXME: handle reqs other than technologies. */
1044 dist = 0;
1045 requirement_vector_iterate(&gov->reqs, preq) {
1046 if (VUT_ADVANCE == preq->source.kind) {
1048 advance_number(preq->source.value.advance));
1049
1050 dist += MAX(1, gut);
1051 }
1053 val = amortize(val, dist);
1054 }
1055
1056 adv->government_want[government_index(gov)] = val; /* Save want */
1058 /* Now reset our gov to it's real state. */
1059 pplayer->government = current_gov;
1060 city_list_iterate(pplayer->cities, acity) {
1063 if (player_is_cpuhog(pplayer)) {
1064 adv->govt_reeval = 1;
1065 } else {
1066 adv->govt_reeval = CLIP(5, city_list_size(pplayer->cities), 20);
1067 }
1068 }
1069 adv->govt_reeval--;
1070
1071 /* Figure out which government is the best for us this turn. */
1072 governments_iterate(gov) {
1073 int gi = government_index(gov);
1074 if (adv->government_want[gi] > best_val
1075 && can_change_to_government(pplayer, gov)) {
1076 best_val = adv->government_want[gi];
1077 adv->goal.revolution = gov;
1078 }
1079 if (adv->government_want[gi] > adv->goal.govt.val) {
1080 adv->goal.govt.gov = gov;
1081 adv->goal.govt.val = adv->government_want[gi];
1082
1083 /* FIXME: Handle reqs other than technologies. */
1084 adv->goal.govt.req = A_NONE;
1085 requirement_vector_iterate(&gov->reqs, preq) {
1086 if (VUT_ADVANCE == preq->source.kind) {
1087 adv->goal.govt.req = advance_number(preq->source.value.advance);
1088 break;
1089 }
1091 }
1093
1094 /* Goodness of the ideal gov is calculated relative to the goodness of the
1095 * best of the available ones. */
1096 adv->goal.govt.val -= best_val;
1097}
1098
1099/**********************************************************************/
1102bool adv_wants_science(struct player *pplayer)
1103{
1104 return adv_data_get(pplayer, nullptr)->wants_science;
1105}
1106
1107/**********************************************************************/
1113bool adv_is_player_dangerous(struct player *pplayer,
1114 struct player *aplayer)
1115{
1116 struct adv_dipl *dip;
1117 enum diplstate_type ds;
1119
1120 if (is_ai(pplayer)) {
1121 /* Give AI code possibility to decide itself */
1122 CALL_PLR_AI_FUNC(consider_plr_dangerous, pplayer, pplayer, aplayer, &dang);
1123 }
1124
1125 if (dang == OVERRIDE_FALSE) {
1126 return FALSE;
1127 }
1128
1129 if (dang == OVERRIDE_TRUE) {
1130 return TRUE;
1131 }
1132
1133 if (pplayer == aplayer) {
1134 /* We always trust ourself */
1135 return FALSE;
1136 }
1137
1138 ds = player_diplstate_get(pplayer, aplayer)->type;
1139
1140 if (ds == DS_WAR || ds == DS_CEASEFIRE) {
1141 /* It's already a war or aplayer can declare it soon */
1142 return TRUE;
1143 }
1144
1145 dip = adv_dipl_get(pplayer, aplayer);
1146
1147 if (dip->allied_with_enemy) {
1148 /* Don't trust someone who will declare war on us soon */
1149 return TRUE;
1150 }
1151
1152 if (player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel > 0) {
1153 return TRUE;
1154 }
1155
1156 if (pplayer->ai_common.love[player_index(aplayer)] < MAX_AI_LOVE / 10) {
1157 /* We don't trust players who we don't like. Note that
1158 * aplayer's units inside pplayer's borders decreases AI's love */
1159 return TRUE;
1160 }
1161
1162 return FALSE;
1163}
void action_array_add_all_by_result(action_id *act_array, int *position, enum action_result result)
Definition actions.c:5787
void action_array_end(action_id *act_array, int size)
Definition actions.c:5770
bool action_immune_government(struct government *gov, action_id act)
Definition actions.c:5447
static struct action * action_by_number(action_id act_id)
Definition actions.h:396
#define action_array_iterate(_act_array_, _act_id_)
Definition actions.h:257
#define action_id_get_role(act_id)
Definition actions.h:457
#define action_array_iterate_end
Definition actions.h:269
#define action_iterate_end
Definition actions.h:214
#define MAX_NUM_ACTIONS
Definition actions.h:58
#define action_iterate(_act_)
Definition actions.h:210
#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:755
adv_want adv_gov_player_bonus_want(struct player *pplayer)
Definition advdata.c:957
void adv_data_init(struct player *pplayer)
Definition advdata.c:699
static void adv_dipl_free(const struct player *plr1, const struct player *plr2)
Definition advdata.c:799
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:606
bool is_adv_data_phase_open(struct player *pplayer)
Definition advdata.c:243
bool adv_wants_science(struct player *pplayer)
Definition advdata.c:1102
bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
Definition advdata.c:263
void adv_data_phase_done(struct player *pplayer)
Definition advdata.c:566
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
Definition advdata.c:1113
static void adv_dipl_new(const struct player *plr1, const struct player *plr2)
Definition advdata.c:787
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:826
static struct adv_dipl * adv_dipl_get(const struct player *plr1, const struct player *plr2)
Definition advdata.c:814
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:985
void adv_data_default(struct player *pplayer)
Definition advdata.c:732
@ 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:377
struct player * player_leading_spacerace(void)
Definition aisupport.c:38
#define city_list_iterate(citylist, pcity)
Definition city.h:505
#define city_list_iterate_end
Definition city.h:507
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:366
char * incite_cost
Definition comments.c:76
static void nuke_units(QVariant data1, QVariant data2)
Definition dialogs.cpp:2743
static void nuke(QVariant data1, QVariant data2)
Definition dialogs.cpp:3019
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:1063
int action_id
Definition fc_types.h:248
#define ACTRES_NONE
Definition fc_types.h:186
@ O_SHIELD
Definition fc_types.h:101
signed short Continent_id
Definition fc_types.h:231
override_bool
Definition fc_types.h:94
@ OVERRIDE_TRUE
Definition fc_types.h:94
@ NO_OVERRIDE
Definition fc_types.h:94
@ OVERRIDE_FALSE
Definition fc_types.h:94
struct civ_game game
Definition game.c:61
struct world wld
Definition game.c:62
Government_type_id government_count(void)
Definition government.c:71
struct government * government_of_player(const struct player *pplayer)
Definition government.c:114
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:166
Government_type_id government_index(const struct government *pgovern)
Definition government.c:82
#define governments_iterate(NAME_pgov)
Definition government.h:124
#define governments_iterate_end
Definition government.h:127
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:430
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:425
#define whole_map_iterate(_map, _tile)
Definition map.h:573
#define whole_map_iterate_end
Definition map.h:582
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:869
int player_slot_count(void)
Definition player.c:418
const char * player_name(const struct player *pplayer)
Definition player.c:895
int player_slot_index(const struct player_slot *pslot)
Definition player.c:426
int player_index(const struct player *pplayer)
Definition player.c:829
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:324
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1409
#define players_iterate_end
Definition player.h:542
#define players_iterate(_pplayer)
Definition player.h:537
#define MAX_AI_LOVE
Definition player.h:566
#define player_slots_iterate(_pslot)
Definition player.h:528
#define is_ai(plr)
Definition player.h:232
#define players_iterate_alive_end
Definition player.h:552
#define player_slots_iterate_end
Definition player.h:532
static bool player_is_cpuhog(const struct player *pplayer)
Definition player.h:580
#define players_iterate_alive(_pplayer)
Definition player.h:547
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:750
struct research * research_get(const struct player *pplayer)
Definition research.c:128
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:619
bool research_invention_gettable(const struct research *presearch, const Tech_type_id tech, bool allow_holes)
Definition research.c:693
#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:608
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:281
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:98
#define advance_index_iterate_end
Definition tech.h:244
#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:240
#define is_ocean_tile(ptile)
Definition terrain.h:196
#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:383
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:297
#define unit_tile(_pu)
Definition unit.h:404
#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:123
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype, bool consider_reg_impr_req)
Definition unittype.c:1974
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2259
int num_role_units(int role)
Definition unittype.c:2209
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1709
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2505
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:196
bool unit_can_take_over(const struct unit *punit)
Definition unittype.c:270
bool utype_can_take_over(const struct unit_type *punittype)
Definition unittype.c:282
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:377
@ MOVE_NONE
Definition unittype.h:144
#define unit_type_iterate(_p)
Definition unittype.h:862
#define uclass_index(_c_)
Definition unittype.h:749
#define unit_type_iterate_end
Definition unittype.h:869
#define MAP_INDEX_SIZE