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 adv->threats.nuclear = 1;
404 }
405 }
408
409 /* Increase from fear to terror if opponent actually has nukes */
410 if (danger_of_nukes) {
411 adv->threats.nuclear++; /* Sum of both fears */
412 }
413
414 /*** Exploration ***/
415
416 adv->explore.land_done = TRUE;
417 adv->explore.sea_done = TRUE;
418 adv->explore.continent = fc_calloc(adv->num_continents + 1, sizeof(bool));
419 adv->explore.ocean = fc_calloc(adv->num_oceans + 1, sizeof(bool));
420
421 whole_map_iterate(&(wld.map), ptile) {
422 Continent_id continent = tile_continent(ptile);
423
424 if (is_ocean_tile(ptile)) {
425 if (adv->explore.sea_done && has_handicap(pplayer, H_TARGETS)
426 && !map_is_known(ptile, pplayer)) {
427 /* We're not done there. */
428 adv->explore.sea_done = FALSE;
429 adv->explore.ocean[-continent] = TRUE;
430 }
431 /* Skip rest, which is land only */
432 continue;
433 }
434 if (adv->explore.continent[tile_continent(ptile)]) {
435 /* We don't need more explaining, we got the point */
436 continue;
437 }
438 if (hut_on_tile(ptile)
439 && (!has_handicap(pplayer, H_HUTS)
440 || map_is_known(ptile, pplayer))) {
441 adv->explore.land_done = FALSE;
442 adv->explore.continent[continent] = TRUE;
443 continue;
444 }
445 if (has_handicap(pplayer, H_TARGETS) && !map_is_known(ptile, pplayer)) {
446 /* This AI must explore */
447 adv->explore.land_done = FALSE;
448 adv->explore.continent[continent] = TRUE;
449 }
451
452 /*** Statistics ***/
453
454 adv->stats.cities = fc_calloc(adv->num_continents + 1, sizeof(int));
455 adv->stats.ocean_cities = fc_calloc(adv->num_oceans + 1, sizeof(int));
456 adv->stats.average_production = 0;
457 city_list_iterate(pplayer->cities, pcity) {
459
460 if (continent >= 0) {
461 adv->stats.cities[continent]++;
462 } else {
463 adv->stats.ocean_cities[-continent]++;
464 }
465 adv->stats.average_production += pcity->surplus[O_SHIELD];
467 adv->stats.average_production /= MAX(1, city_list_size(pplayer->cities));
468
469 /*** Diplomacy ***/
470
472 struct adv_dipl *dip = adv_dipl_get(pplayer, aplayer);
473
474 dip->allied_with_enemy = FALSE;
477 && player_diplstate_get(pplayer, check_pl)->type == DS_WAR) {
478 dip->allied_with_enemy = TRUE;
479 }
482
484
485 adv->dipl.production_leader = nullptr;
487 if (adv->dipl.production_leader == nullptr
488 || adv->dipl.production_leader->score.mfg < aplayer->score.mfg) {
490 }
492
493 adv->dipl.tech_leader = nullptr;
495 if (adv->dipl.tech_leader == nullptr
496 || adv->dipl.tech_leader->score.techs < aplayer->score.techs) {
497 adv->dipl.tech_leader = aplayer;
498 }
500
501 /*** Priorities ***/
502
503 /* NEVER set these to zero! Weight values are usually multiplied by
504 * these values, so be careful with them. They are used in city
505 * and government calculations, and food and shields should be
506 * slightly bigger because we only look at surpluses there. They
507 * are all WAGs. */
510 if (adv_wants_science(pplayer)) {
511 adv->luxury_priority = 1;
513 } else {
515 adv->science_priority = 1;
516 }
518 adv->happy_priority = 1;
519 adv->unhappy_priority = TRADE_WEIGHTING; /* Danger */
520 adv->angry_priority = TRADE_WEIGHTING * 3; /* Grave danger */
523
524 /* Research want */
525 if (is_future_tech(research_get(pplayer)->researching)
527 adv->wants_science = FALSE;
528 } else {
529 adv->wants_science = TRUE;
530 }
531
532 /* Max num cities
533 * The idea behind this code is that novice players don't understand that
534 * expansion is critical and find it very annoying.
535 * With the following code AI players will try to be only a bit better
536 * than the best human players. This should lead to more exciting games
537 * for the beginners.
538 */
539 if (has_handicap(pplayer, H_EXPANSION)) {
540 bool found_human = FALSE;
541
542 adv->max_num_cities = 3;
544 if (aplayer == pplayer || is_ai(aplayer)) {
545 continue;
546 }
548 city_list_size(aplayer->cities) + 3);
551 if (!found_human) {
553 }
554 } else {
556 }
557
558 count_my_units(pplayer);
559
561
562 /* Government */
564 adv_best_government(pplayer);
566
567 return TRUE;
568}
569
570/**********************************************************************/
573void adv_data_phase_done(struct player *pplayer)
574{
575 struct adv_data *adv = pplayer->server.adv;
576
577 fc_assert_ret(adv != nullptr);
578
579 if (!adv->phase_is_initialized) {
580 return;
581 }
582
583 free(adv->explore.ocean);
584 adv->explore.ocean = nullptr;
585
586 free(adv->explore.continent);
587 adv->explore.continent = nullptr;
588
589 free(adv->continents);
590 adv->continents = nullptr;
591
592 free(adv->oceans);
593 adv->oceans = nullptr;
594
595 free(adv->stats.cities);
596 adv->stats.cities = nullptr;
597
598 free(adv->stats.ocean_cities);
599 adv->stats.ocean_cities = nullptr;
600
601 adv->num_continents = 0;
602 adv->num_oceans = 0;
603
605}
606
607/**********************************************************************/
613struct adv_data *adv_data_get(struct player *pplayer, bool *caller_closes)
614{
615 struct adv_data *adv = pplayer->server.adv;
616
617 fc_assert_ret_val(adv != nullptr, nullptr);
618
619 /* It's certainly an indication of a bug causing problems
620 if this adv_data_get() gets called between adv_data_phase_done() and
621 adv_data_phase_init(), since we may end up calling those
622 functions if number of known continents has changed.
623
624 Consider following case:
625 Correct call order would be:
626 a) adv_data_phase_init()
627 b) adv_data_get() -> adv_data_phase_done()
628 c) adv_data_get() -> adv_data_phase_init()
629 d) adv_data_phase_done()
630 e) do something
631 f) adv_data_phase_init()
632
633 In (e) data phase would be closed and data would be
634 correctly initialized at (f), which is probably beginning
635 next turn.
636
637 Buggy version where adv_data_get() (b&c) gets called after (d):
638 a) adv_data_phase_init()
639 d) adv_data_phase_done()
640 b) adv_data_get() -> adv_data_phase_done()
641 c) adv_data_get() -> adv_data_phase_init()
642 e) do something
643 f) adv_data_phase_init()
644
645 Now in (e) data phase would be open. When adv_data_phase_init()
646 then finally gets called and it really should recreate data
647 to match situation of new turn, it detects that data phase
648 is already initialized and does nothing.
649
650 So, this assertion is here for a reason!
651
652 Code below tries to fix the situation best it can if such a bug is
653 encountered. Since we are probably going to trust that to be enough
654 instead of making intrusive fixes for actual bug in stable branch,
655 do not assert for non-debug builds of stable versions. */
656#if defined(FREECIV_DEBUG) || defined(IS_DEVEL_VERSION)
657 fc_assert(caller_closes != nullptr || adv->phase_is_initialized);
658#endif
659
660 if (caller_closes != nullptr) {
662 }
663
665 || adv->num_oceans != wld.map.num_oceans) {
666 /* We discovered more continents, recalculate! */
667
668 if (adv->phase_is_initialized) {
669 /* Only call these in this order if inside data phase.
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 adv_data_phase_done(pplayer);
673 adv_data_phase_init(pplayer, FALSE);
674 } else {
675 /* Call them in "wrong" order so we return recalculated data to caller,
676 but leave data phase closed.
677 This is blanket "fix" for all cases where adv_data_get() is called
678 at illegal time. This at least minimize bad effects of such calls.
679
680 Arguably this is not buggy at all but works just as designed in
681 case of being called in alternate movement mode for players
682 other than currently moving one (for diplomacy between the two,
683 for example) */
684 log_debug("%s advisor data phase closed when adv_data_get() called",
685 player_name(pplayer));
686 adv_data_phase_init(pplayer, FALSE);
687 if (caller_closes != nullptr) {
689 } else {
690 adv_data_phase_done(pplayer);
691 }
692 }
693 } else {
694 if (!adv->phase_is_initialized && caller_closes != nullptr) {
695 adv_data_phase_init(pplayer, FALSE);
697 }
698 }
699
700 return adv;
701}
702
703/**********************************************************************/
706void adv_data_init(struct player *pplayer)
707{
708 struct adv_data *adv;
709
710 if (pplayer->server.adv == nullptr) {
711 pplayer->server.adv = fc_calloc(1, sizeof(*pplayer->server.adv));
712 }
713 adv = pplayer->server.adv;
714
715 adv->government_want = nullptr;
716
718 sizeof(*adv->dipl.adv_dipl_slots));
719 player_slots_iterate(pslot) {
720 struct adv_dipl **dip_slot
721 = adv->dipl.adv_dipl_slots + player_slot_index(pslot);
722
723 *dip_slot = nullptr;
725
727 adv_dipl_new(pplayer, aplayer);
728 if (aplayer != pplayer) {
729 adv_dipl_new(aplayer, pplayer);
730 }
732
733 adv_data_default(pplayer);
734}
735
736/**********************************************************************/
739void adv_data_default(struct player *pplayer)
740{
741 struct adv_data *adv = pplayer->server.adv;
742
743 fc_assert_ret(adv != nullptr);
744
745 adv->govt_reeval = 0;
747 (government_count() + 1)
748 * sizeof(*adv->government_want));
749 memset(adv->government_want, 0,
750 (government_count() + 1) * sizeof(*adv->government_want));
751
752 adv->wonder_city = 0;
753
754 adv->wants_science = TRUE;
755 adv->celebrate = FALSE;
756 adv->max_num_cities = 10000;
757}
758
759/**********************************************************************/
762void adv_data_close(struct player *pplayer)
763{
764 struct adv_data *adv = pplayer->server.adv;
765
766 fc_assert_ret(adv != nullptr);
767
768 adv_data_phase_done(pplayer);
769
770 if (adv->government_want != nullptr) {
771 free(adv->government_want);
772 }
773
774 if (adv->dipl.adv_dipl_slots != nullptr) {
776 adv_dipl_free(pplayer, aplayer);
777 if (aplayer != pplayer) {
778 adv_dipl_free(aplayer, pplayer);
779 }
782 }
783
784 if (adv != nullptr) {
785 free(adv);
786 }
787
788 pplayer->server.adv = nullptr;
789}
790
791/**********************************************************************/
794static void adv_dipl_new(const struct player *plr1,
795 const struct player *plr2)
796{
797 struct adv_dipl **dip_slot =
799
800 *dip_slot = fc_calloc(1, sizeof(struct adv_dipl));
801}
802
803/**********************************************************************/
806static void adv_dipl_free(const struct player *plr1,
807 const struct player *plr2)
808{
809 struct adv_dipl **dip_slot
810 = plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
811
812 if (*dip_slot != nullptr) {
814 *dip_slot = nullptr;
815 }
816}
817
818/**********************************************************************/
821static struct adv_dipl *adv_dipl_get(const struct player *plr1,
822 const struct player *plr2)
823{
824 struct adv_dipl **dip_slot
825 = plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
826
827 return *dip_slot;
828}
829
830/**********************************************************************/
834{
835 adv_want bonus = 0;
836
837 /* TODO: Individual and well balanced value. */
838 action_iterate(act) {
839 struct action *paction = action_by_number(act);
840
841 if (!action_immune_government(gov, act)) {
842 /* This government doesn't provide immunity against this
843 * action. */
844 continue;
845 }
846
847 switch (paction->result) {
848 case ACTRES_ATTACK:
852 bonus += 4;
853 break;
855 bonus += 2;
856 break;
858 bonus += 1.5;
859 break;
860 case ACTRES_CULTIVATE:
861 case ACTRES_PLANT:
862 bonus += 0.3;
863 break;
865 case ACTRES_PILLAGE:
866 bonus += 0.2;
867 break;
870 bonus += 0.3;
871 break;
872 case ACTRES_HUT_ENTER:
874 /* It is mine. My own. My precious. */
875 bonus += 0.1;
876 break;
889 case ACTRES_BOMBARD:
890 case ACTRES_SPY_NUKE:
891 case ACTRES_NUKE:
898 /* Being a target of this is usually undesirable */
899 /* TODO: Individual and well balanced values. */
900 bonus += 0.1;
901 break;
902
903 case ACTRES_UNIT_MOVE:
904 case ACTRES_TELEPORT:
910 case ACTRES_PARADROP:
912 case ACTRES_FORTIFY:
914 /* Wants the ability to do this to it self. Don't want others
915 * to target it. Do nothing since action_immune_government()
916 * doesn't separate based on who the actor is. */
917 break;
918
919 case ACTRES_NONE:
920 /* Ruleset defined */
921 break;
922
925 case ACTRES_JOIN_CITY:
928 case ACTRES_HOME_CITY:
929 case ACTRES_HOMELESS:
931 case ACTRES_AIRLIFT:
932 case ACTRES_HEAL_UNIT:
933 case ACTRES_ROAD:
934 case ACTRES_CONVERT:
935 case ACTRES_BASE:
936 case ACTRES_MINE:
937 case ACTRES_IRRIGATE:
938 case ACTRES_CLEAN:
945 /* Could be good. An embassy gives permanent contact. A trade
946 * route gives gold per turn. Join city gives population.
947 * Help wonder gives shields. */
948 /* TODO: Individual and well balanced values. */
949 break;
950
952 }
954
955 return bonus;
956}
957
958/**********************************************************************/
965{
966 adv_want bonus = 0;
967
968 /* Bonuses for non-economic abilities. We increase val by
969 * a very small amount here to choose govt in cases where
970 * we have no cities yet. */
971 bonus += get_player_bonus(pplayer, EFT_VETERAN_BUILD) > 0 ? 3 : 0;
972 bonus += get_player_bonus(pplayer, EFT_INSPIRE_PARTISANS) > 0 ? 3 : 0;
973 bonus += get_player_bonus(pplayer, EFT_RAPTURE_GROW) > 0 ? 2 : 0;
974 bonus += get_player_bonus(pplayer, EFT_FANATICS) > 0 ? 3 : 0;
975 bonus += get_player_bonus(pplayer, EFT_OUTPUT_INC_TILE) * 8;
976
977 return bonus;
978}
979
980/**********************************************************************/
992void adv_best_government(struct player *pplayer)
993{
994 struct adv_data *adv = adv_data_get(pplayer, nullptr);
995 int best_val = 0;
997
998 adv->goal.govt.gov = current_gov;
999 adv->goal.govt.val = 0;
1000 adv->goal.govt.req = A_UNSET;
1002
1003 if (has_handicap(pplayer, H_AWAY) || !pplayer->is_alive) {
1004 return;
1005 }
1006
1007 if (adv->govt_reeval == 0) {
1008 const struct research *presearch = research_get(pplayer);
1009
1010 governments_iterate(gov) {
1011 adv_want val = 0;
1012 bool override = FALSE;
1013
1015 continue; /* Pointless */
1016 }
1017 if (gov->ai.better
1018 && can_change_to_government(pplayer, gov->ai.better)) {
1019 continue; /* We have better governments available */
1020 }
1021
1022 CALL_PLR_AI_FUNC(gov_value, pplayer, pplayer, gov, &val, &override);
1023
1024 if (!override) {
1025 int dist;
1026 adv_want bonus = 0; /* In percentage */
1027 int revolution_turns;
1028
1029 pplayer->government = gov;
1030 /* Ideally we should change tax rates here, but since
1031 * this is a rather big CPU operation, we'd rather not. */
1032 check_player_max_rates(pplayer);
1033 city_list_iterate(pplayer->cities, acity) {
1036 city_list_iterate(pplayer->cities, pcity) {
1037 val += adv_eval_calc_city(pcity, adv);
1039
1040 bonus += adv_gov_action_immunity_want(gov);
1041 bonus += adv_gov_player_bonus_want(pplayer);
1042
1044 if (revolution_turns > 0) {
1045 bonus -= 6 / revolution_turns;
1046 }
1047
1048 val += (val * bonus) / 100;
1049
1050 /* FIXME: handle reqs other than technologies. */
1051 dist = 0;
1052 requirement_vector_iterate(&gov->reqs, preq) {
1053 if (VUT_ADVANCE == preq->source.kind) {
1055 advance_number(preq->source.value.advance));
1056
1057 dist += MAX(1, gut);
1058 }
1060 val = amortize(val, dist);
1061 }
1062
1063 adv->government_want[government_index(gov)] = val; /* Save want */
1065 /* Now reset our gov to it's real state. */
1066 pplayer->government = current_gov;
1067 city_list_iterate(pplayer->cities, acity) {
1070 if (player_is_cpuhog(pplayer)) {
1071 adv->govt_reeval = 1;
1072 } else {
1073 adv->govt_reeval = CLIP(5, city_list_size(pplayer->cities), 20);
1074 }
1075 }
1076 adv->govt_reeval--;
1077
1078 /* Figure out which government is the best for us this turn. */
1079 governments_iterate(gov) {
1080 int gi = government_index(gov);
1081 if (adv->government_want[gi] > best_val
1082 && can_change_to_government(pplayer, gov)) {
1083 best_val = adv->government_want[gi];
1084 adv->goal.revolution = gov;
1085 }
1086 if (adv->government_want[gi] > adv->goal.govt.val) {
1087 adv->goal.govt.gov = gov;
1088 adv->goal.govt.val = adv->government_want[gi];
1089
1090 /* FIXME: Handle reqs other than technologies. */
1091 adv->goal.govt.req = A_NONE;
1092 requirement_vector_iterate(&gov->reqs, preq) {
1093 if (VUT_ADVANCE == preq->source.kind) {
1094 adv->goal.govt.req = advance_number(preq->source.value.advance);
1095 break;
1096 }
1098 }
1100
1101 /* Goodness of the ideal gov is calculated relative to the goodness of the
1102 * best of the available ones. */
1103 adv->goal.govt.val -= best_val;
1104}
1105
1106/**********************************************************************/
1109bool adv_wants_science(struct player *pplayer)
1110{
1111 return adv_data_get(pplayer, nullptr)->wants_science;
1112}
1113
1114/**********************************************************************/
1120bool adv_is_player_dangerous(struct player *pplayer,
1121 struct player *aplayer)
1122{
1123 struct adv_dipl *dip;
1124 enum diplstate_type ds;
1126
1127 if (is_ai(pplayer)) {
1128 /* Give AI code possibility to decide itself */
1129 CALL_PLR_AI_FUNC(consider_plr_dangerous, pplayer, pplayer, aplayer, &dang);
1130 }
1131
1132 if (dang == OVERRIDE_FALSE) {
1133 return FALSE;
1134 }
1135
1136 if (dang == OVERRIDE_TRUE) {
1137 return TRUE;
1138 }
1139
1140 if (pplayer == aplayer) {
1141 /* We always trust ourself */
1142 return FALSE;
1143 }
1144
1145 ds = player_diplstate_get(pplayer, aplayer)->type;
1146
1147 if (ds == DS_WAR || ds == DS_CEASEFIRE) {
1148 /* It's already a war or aplayer can declare it soon */
1149 return TRUE;
1150 }
1151
1152 dip = adv_dipl_get(pplayer, aplayer);
1153
1154 if (dip->allied_with_enemy) {
1155 /* Don't trust someone who will declare war on us soon */
1156 return TRUE;
1157 }
1158
1159 if (player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel > 0) {
1160 return TRUE;
1161 }
1162
1163 if (pplayer->ai_common.love[player_index(aplayer)] < MAX_AI_LOVE / 10) {
1164 /* We don't trust players who we don't like. Note that
1165 * aplayer's units inside pplayer's borders decreases AI's love */
1166 return TRUE;
1167 }
1168
1169 return FALSE;
1170}
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:762
adv_want adv_gov_player_bonus_want(struct player *pplayer)
Definition advdata.c:964
void adv_data_init(struct player *pplayer)
Definition advdata.c:706
static void adv_dipl_free(const struct player *plr1, const struct player *plr2)
Definition advdata.c:806
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:613
bool is_adv_data_phase_open(struct player *pplayer)
Definition advdata.c:250
bool adv_wants_science(struct player *pplayer)
Definition advdata.c:1109
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:573
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
Definition advdata.c:1120
static void adv_dipl_new(const struct player *plr1, const struct player *plr2)
Definition advdata.c:794
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:833
static struct adv_dipl * adv_dipl_get(const struct player *plr1, const struct player *plr2)
Definition advdata.c:821
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:992
void adv_data_default(struct player *pplayer)
Definition advdata.c:739
@ 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
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:116
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:172
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:407
#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
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype, bool consider_reg_impr_req)
Definition unittype.c:1994
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2279
int num_role_units(int role)
Definition unittype.c:2229
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:2530
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 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:863
#define uclass_index(_c_)
Definition unittype.h:749
#define unit_type_iterate_end
Definition unittype.h:870
#define MAP_INDEX_SIZE