Freeciv-3.2
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#include "autosettlers.h"
53
54/* ai */
55#include "handicaps.h"
56
57#include "advdata.h"
58
59static void adv_dipl_new(const struct player *plr1,
60 const struct player *plr2);
61static void adv_dipl_free(const struct player *plr1,
62 const struct player *plr2);
63static struct adv_dipl *adv_dipl_get(const struct player *plr1,
64 const struct player *plr2);
65
66/**********************************************************************/
74static void adv_data_city_impr_calc(struct player *pplayer,
75 struct adv_data *adv)
76{
77 int count[ADV_IMPR_LAST];
78
79 memset(count, 0, sizeof(count));
80
81 improvement_iterate(pimprove) {
82 struct universal source = {
84 .value = {.building = pimprove}
85 };
86
88
89 /* Find largest extension */
91 switch (peffect->type) {
92#if 0
93 /* TODO */
100 case EFT_MAKE_HAPPY:
101#endif
102 case EFT_CAPITAL_CITY:
106 case EFT_OUTPUT_BONUS:
109 case EFT_UPKEEP_FREE:
111 if (VUT_IMPROVEMENT == preq->source.kind
112 && preq->source.value.building == pimprove) {
113 if (adv->impr_calc[improvement_index(pimprove)] != ADV_IMPR_CALCULATE_FULL) {
115 }
116 if (preq->range > adv->impr_range[improvement_index(pimprove)]) {
117 adv->impr_range[improvement_index(pimprove)] = preq->range;
118 }
119 }
121 break;
126 if (VUT_IMPROVEMENT == preq->source.kind
127 && preq->source.value.building == pimprove) {
129 if (preq->range > adv->impr_range[improvement_index(pimprove)]) {
130 adv->impr_range[improvement_index(pimprove)] = preq->range;
131 }
132 }
134 break;
135 default:
136 /* Nothing! */
137 break;
138 }
141}
142
143/**********************************************************************/
149{
150 struct research *presearch, *aresearch;
152
153 if (players_needed == 0) {
154 return FALSE;
155 }
156
157 presearch = research_get(pplayer);
159 int players_having;
160
164 continue;
165 }
166
167 players_having = 0;
168
170 if (aplayer == pplayer) {
171 continue;
172 }
173
176 || aresearch->researching == tech) {
179 return TRUE;
180 }
181 }
184 return FALSE;
185}
186
187/**********************************************************************/
193{
194 struct adv_data *adv = pplayer->server.adv;
195
196 fc_assert_ret(adv != NULL);
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, NULL);
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 }
233 adv->stats.units.teleporters++;
234 }
236 adv->stats.units.airliftable++;
237 }
238 if (can_upgrade_unittype(pplayer, unit_type_get(punit)) >= 0) {
239 adv->stats.units.upgradeable++;
240 }
242}
243
244/**********************************************************************/
248bool is_adv_data_phase_open(struct player *pplayer)
249{
250 struct adv_data *adv = pplayer->server.adv;
251
252 return adv->phase_is_initialized;
253}
254
255/**********************************************************************/
268bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
269{
270 struct adv_data *adv = pplayer->server.adv;
271 bool danger_of_nukes;
273
274 {
275 int i = 0;
276
277 /* Conventional nukes */
280 /* TODO: worry about spy nuking too? */
282 }
283
285
286 if (adv->phase_is_initialized) {
287 return FALSE;
288 }
290
292
294
295 /*** Threats ***/
296
299 adv->continents = fc_calloc(adv->num_continents + 1, sizeof(struct adv_area_info));
300 adv->oceans = fc_calloc(adv->num_oceans + 1, sizeof(struct adv_area_info));
301 adv->threats.invasions = FALSE;
302 adv->threats.nuclear = 0; /* None */
303 adv->threats.igwall = FALSE;
304
305 whole_map_iterate(&(wld.map), ptile) {
306 Continent_id cont = tile_continent(ptile);
307
308 if (cont >= 0) {
309 adv->continents[cont].size++;
310 } else {
311 adv->oceans[-cont].size++;
312 }
314
316 if (!adv_is_player_dangerous(pplayer, aplayer)) {
317 continue;
318 }
319
320 /* The idea is that if there aren't any hostile cities on
321 * our continent, the danger of land attacks is not big
322 * enough to warrant city walls. Concentrate instead on
323 * coastal fortresses and hunting down enemy transports. */
326
327 if (continent >= 0) {
329 }
331
333 const struct unit_class *pclass = unit_class_get(punit);
334
336 adv->threats.igwall = TRUE;
337 }
338
339 if (pclass->adv.sea_move != MOVE_NONE) {
340 /* If the enemy has not started sailing yet, or we have total
341 * control over the seas, don't worry, keep attacking. */
343 /* Enemy represents a cross-continental threat! */
344 adv->threats.invasions = TRUE;
345 } else if (get_transporter_capacity(punit) > 0) {
350 /* Enemy can transport some threatening units! */
351 adv->threats.invasions = TRUE;
352 break;
353 }
355 }
356
357 /* The idea is that while our enemies don't have any offensive
358 * seaborne units, we don't have to worry. Go on the offensive! */
359 if (unit_type_get(punit)->attack_strength > 1) {
362
363 adv->oceans[-continent].threat = TRUE;
364 } else {
366 if (is_ocean_tile(tile2)) {
367 Continent_id continent = tile_continent(tile2);
368
369 adv->oceans[-continent].threat = TRUE;
370 }
372 }
373 }
374 continue;
375 }
376
377 /* If our enemy builds missiles, worry about missile defense. */
379 && unit_type_get(punit)->attack_strength > 1) {
380 adv->threats.suicide_attack = TRUE;
381 }
382
383 /* If they build nukes, worry a lot. */
385 if (unit_can_do_action(punit, act_id)) {
387 }
390
391 /* Check for nuke capability */
393 int i;
395
396 for (i = 0; i < nuke_units; i++) {
397 struct unit_type *nuke =
399
401 adv->threats.nuclear = 1;
402 }
403 }
406
407 /* Increase from fear to terror if opponent actually has nukes */
408 if (danger_of_nukes) {
409 adv->threats.nuclear++; /* sum of both fears */
410 }
411
412 /*** Exploration ***/
413
414 adv->explore.land_done = TRUE;
415 adv->explore.sea_done = TRUE;
416 adv->explore.continent = fc_calloc(adv->num_continents + 1, sizeof(bool));
417 adv->explore.ocean = fc_calloc(adv->num_oceans + 1, sizeof(bool));
418
419 whole_map_iterate(&(wld.map), ptile) {
420 Continent_id continent = tile_continent(ptile);
421
422 if (is_ocean_tile(ptile)) {
423 if (adv->explore.sea_done && has_handicap(pplayer, H_TARGETS)
424 && !map_is_known(ptile, pplayer)) {
425 /* We're not done there. */
426 adv->explore.sea_done = FALSE;
427 adv->explore.ocean[-continent] = TRUE;
428 }
429 /* skip rest, which is land only */
430 continue;
431 }
432 if (adv->explore.continent[tile_continent(ptile)]) {
433 /* we don't need more explaining, we got the point */
434 continue;
435 }
436 if (hut_on_tile(ptile)
437 && (!has_handicap(pplayer, H_HUTS)
438 || map_is_known(ptile, pplayer))) {
439 adv->explore.land_done = FALSE;
440 adv->explore.continent[continent] = TRUE;
441 continue;
442 }
443 if (has_handicap(pplayer, H_TARGETS) && !map_is_known(ptile, pplayer)) {
444 /* this AI must explore */
445 adv->explore.land_done = FALSE;
446 adv->explore.continent[continent] = TRUE;
447 }
449
450 /*** Statistics ***/
451
452 adv->stats.cities = fc_calloc(adv->num_continents + 1, sizeof(int));
453 adv->stats.ocean_cities = fc_calloc(adv->num_oceans + 1, sizeof(int));
454 adv->stats.average_production = 0;
455 city_list_iterate(pplayer->cities, pcity) {
456 Continent_id continent = tile_continent(pcity->tile);
457
458 if (continent >= 0) {
459 adv->stats.cities[continent]++;
460 } else {
461 adv->stats.ocean_cities[-continent]++;
462 }
463 adv->stats.average_production += pcity->surplus[O_SHIELD];
465 adv->stats.average_production /= MAX(1, city_list_size(pplayer->cities));
466
467 /*** Diplomacy ***/
468
470 struct adv_dipl *dip = adv_dipl_get(pplayer, aplayer);
471
472 dip->allied_with_enemy = FALSE;
475 && player_diplstate_get(pplayer, check_pl)->type == DS_WAR) {
476 dip->allied_with_enemy = TRUE;
477 }
480
482
485 if (adv->dipl.production_leader == NULL
486 || adv->dipl.production_leader->score.mfg < aplayer->score.mfg) {
488 }
490
491 adv->dipl.tech_leader = NULL;
493 if (adv->dipl.tech_leader == NULL
494 || adv->dipl.tech_leader->score.techs < aplayer->score.techs) {
495 adv->dipl.tech_leader = aplayer;
496 }
498
499 /*** Priorities ***/
500
501 /* NEVER set these to zero! Weight values are usually multiplied by
502 * these values, so be careful with them. They are used in city
503 * and government calculations, and food and shields should be
504 * slightly bigger because we only look at surpluses there. They
505 * are all WAGs. */
508 if (adv_wants_science(pplayer)) {
509 adv->luxury_priority = 1;
511 } else {
513 adv->science_priority = 1;
514 }
516 adv->happy_priority = 1;
517 adv->unhappy_priority = TRADE_WEIGHTING; /* danger */
518 adv->angry_priority = TRADE_WEIGHTING * 3; /* grave danger */
521
522 /* Research want */
523 if (is_future_tech(research_get(pplayer)->researching)
525 adv->wants_science = FALSE;
526 } else {
527 adv->wants_science = TRUE;
528 }
529
530 /* max num cities
531 * The idea behind this code is that novice players don't understand that
532 * expansion is critical and find it very annoying.
533 * With the following code AI players will try to be only a bit better
534 * than the best human players. This should lead to more exciting games
535 * for the beginners.
536 */
537 if (has_handicap(pplayer, H_EXPANSION)) {
538 bool found_human = FALSE;
539 adv->max_num_cities = 3;
541 if (aplayer == pplayer || is_ai(aplayer)) {
542 continue;
543 }
545 city_list_size(aplayer->cities) + 3);
548 if (!found_human) {
550 }
551 } else {
553 }
554
555 count_my_units(pplayer);
556
558
559 /* Government */
561 adv_best_government(pplayer);
563
564 return TRUE;
565}
566
567/**********************************************************************/
570void adv_data_phase_done(struct player *pplayer)
571{
572 struct adv_data *adv = pplayer->server.adv;
573
574 fc_assert_ret(adv != NULL);
575
576 if (!adv->phase_is_initialized) {
577 return;
578 }
579
580 free(adv->explore.ocean);
581 adv->explore.ocean = NULL;
582
583 free(adv->explore.continent);
584 adv->explore.continent = NULL;
585
586 free(adv->continents);
587 adv->continents = NULL;
588
589 free(adv->oceans);
590 adv->oceans = NULL;
591
592 free(adv->stats.cities);
593 adv->stats.cities = NULL;
594
595 free(adv->stats.ocean_cities);
596 adv->stats.ocean_cities = NULL;
597
598 adv->num_continents = 0;
599 adv->num_oceans = 0;
600
602}
603
604/**********************************************************************/
610struct adv_data *adv_data_get(struct player *pplayer, bool *caller_closes)
611{
612 struct adv_data *adv = pplayer->server.adv;
613
614 fc_assert_ret_val(adv != NULL, NULL);
615
616 /* It's certainly indication of bug causing problems
617 if this adv_data_get() gets called between adv_data_phase_done() and
618 adv_data_phase_init(), since we may end up calling those
619 functions if number of known continents has changed.
620
621 Consider following case:
622 Correct call order would be:
623 a) adv_data_phase_init()
624 b) adv_data_get() -> adv_data_phase_done()
625 c) adv_data_get() -> adv_data_phase_init()
626 d) adv_data_phase_done()
627 e) do something
628 f) adv_data_phase_init()
629
630 In (e) data phase would be closed and data would be
631 correctly initialized at (f), which is probably beginning
632 next turn.
633
634 Buggy version where adv_data_get() (b&c) gets called after (d):
635 a) adv_data_phase_init()
636 d) adv_data_phase_done()
637 b) adv_data_get() -> adv_data_phase_done()
638 c) adv_data_get() -> adv_data_phase_init()
639 e) do something
640 f) adv_data_phase_init()
641
642 Now in (e) data phase would be open. When adv_data_phase_init()
643 then finally gets called and it really should recreate data
644 to match situation of new turn, it detects that data phase
645 is already initialized and does nothing.
646
647 So, this assertion is here for a reason!
648
649 Code below tries to fix the situation best it can if such a bug is
650 encountered. Since we are probably going to trust that to be enough
651 instead of making intrusive fixes for actual bug in stable branch,
652 do not assert for non-debug builds of stable versions. */
653#if defined(FREECIV_DEBUG) || defined(IS_DEVEL_VERSION)
655#endif
656
657 if (caller_closes != NULL) {
659 }
660
662 || adv->num_oceans != wld.map.num_oceans) {
663 /* we discovered more continents, recalculate! */
664
665 if (adv->phase_is_initialized) {
666 /* Only call these in this order if inside data phase.
667 This is blanket "fix" for all cases where adv_data_get() is called
668 at illegal time. This at least minimize bad effects of such calls. */
669 adv_data_phase_done(pplayer);
670 adv_data_phase_init(pplayer, FALSE);
671 } else {
672 /* Call them in "wrong" order so we return recalculated data to caller,
673 but leave data phase closed.
674 This is blanket "fix" for all cases where adv_data_get() is called
675 at illegal time. This at least minimize bad effects of such calls.
676
677 Arguably this is not buggy at all but works just as designed in
678 case of being called in alternate movement mode for players
679 other than currently moving one (for diplomacy between the two,
680 for example) */
681 log_debug("%s advisor data phase closed when adv_data_get() called",
682 player_name(pplayer));
683 adv_data_phase_init(pplayer, FALSE);
684 if (caller_closes != NULL) {
686 } else {
687 adv_data_phase_done(pplayer);
688 }
689 }
690 } else {
691 if (!adv->phase_is_initialized && caller_closes != NULL) {
692 adv_data_phase_init(pplayer, FALSE);
694 }
695 }
696
697 return adv;
698}
699
700/**********************************************************************/
703void adv_data_init(struct player *pplayer)
704{
705 struct adv_data *adv;
706
707 if (pplayer->server.adv == NULL) {
708 pplayer->server.adv = fc_calloc(1, sizeof(*pplayer->server.adv));
709 }
710 adv = pplayer->server.adv;
711
712 adv->government_want = NULL;
713
715 sizeof(*adv->dipl.adv_dipl_slots));
716 player_slots_iterate(pslot) {
717 struct adv_dipl **dip_slot =
719 *dip_slot = NULL;
721
723 adv_dipl_new(pplayer, aplayer);
724 if (aplayer != pplayer) {
725 adv_dipl_new(aplayer, pplayer);
726 }
728
729 adv_data_default(pplayer);
730}
731
732/**********************************************************************/
735void adv_data_default(struct player *pplayer)
736{
737 struct adv_data *adv = pplayer->server.adv;
738
739 fc_assert_ret(adv != NULL);
740
741 adv->govt_reeval = 0;
743 (government_count() + 1)
744 * sizeof(*adv->government_want));
745 memset(adv->government_want, 0,
746 (government_count() + 1) * sizeof(*adv->government_want));
747
748 adv->wonder_city = 0;
749
750 adv->wants_science = TRUE;
751 adv->celebrate = FALSE;
752 adv->max_num_cities = 10000;
753}
754
755/**********************************************************************/
758void adv_data_close(struct player *pplayer)
759{
760 struct adv_data *adv = pplayer->server.adv;
761
762 fc_assert_ret(NULL != adv);
763
764 adv_data_phase_done(pplayer);
765
766 if (adv->government_want != NULL) {
767 free(adv->government_want);
768 }
769
770 if (adv->dipl.adv_dipl_slots != NULL) {
772 adv_dipl_free(pplayer, aplayer);
773 if (aplayer != pplayer) {
774 adv_dipl_free(aplayer, pplayer);
775 }
778 }
779
780 if (adv != NULL) {
781 free(adv);
782 }
783
784 pplayer->server.adv = NULL;
785}
786
787/**********************************************************************/
790static void adv_dipl_new(const struct player *plr1,
791 const struct player *plr2)
792{
793 struct adv_dipl **dip_slot =
795
796 *dip_slot = fc_calloc(1, sizeof(struct adv_dipl));
797}
798
799/**********************************************************************/
802static void adv_dipl_free(const struct player *plr1,
803 const struct player *plr2)
804{
805 struct adv_dipl **dip_slot =
807
808 if (*dip_slot != NULL) {
810 *dip_slot = NULL;
811 }
812}
813
814/**********************************************************************/
817static struct adv_dipl *adv_dipl_get(const struct player *plr1,
818 const struct player *plr2)
819{
820 struct adv_dipl **dip_slot =
822
823 return *dip_slot;
824}
825
826/**********************************************************************/
830{
831 adv_want bonus = 0;
832
833 /* TODO: Individual and well balanced value. */
834 action_iterate(act) {
835 struct action *paction = action_by_number(act);
836
837 if (!action_immune_government(gov, act)) {
838 /* This government doesn't provide immunity against this
839 * action. */
840 continue;
841 }
842
843 switch (paction->result) {
844 case ACTRES_ATTACK:
848 bonus += 4;
849 break;
851 bonus += 2;
852 break;
854 bonus += 1.5;
855 break;
856 case ACTRES_CULTIVATE:
857 case ACTRES_PLANT:
858 bonus += 0.3;
859 break;
861 case ACTRES_PILLAGE:
863 bonus += 0.2;
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, NULL);
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 /* Goodness of the ideal gov is calculated relative to the goodness of the
1094 * best of the available ones. */
1095 adv->goal.govt.val -= best_val;
1096}
1097
1098/**********************************************************************/
1101bool adv_wants_science(struct player *pplayer)
1102{
1103 return adv_data_get(pplayer, NULL)->wants_science;
1104}
1105
1106/**********************************************************************/
1112bool adv_is_player_dangerous(struct player *pplayer,
1113 struct player *aplayer)
1114{
1115 struct adv_dipl *dip;
1116 enum diplstate_type ds;
1118
1119 if (is_ai(pplayer)) {
1120 /* Give AI code possibility to decide itself */
1121 CALL_PLR_AI_FUNC(consider_plr_dangerous, pplayer, pplayer, aplayer, &dang);
1122 }
1123
1124 if (dang == OVERRIDE_FALSE) {
1125 return FALSE;
1126 }
1127
1128 if (dang == OVERRIDE_TRUE) {
1129 return TRUE;
1130 }
1131
1132 if (pplayer == aplayer) {
1133 /* We always trust ourself */
1134 return FALSE;
1135 }
1136
1137 ds = player_diplstate_get(pplayer, aplayer)->type;
1138
1139 if (ds == DS_WAR || ds == DS_CEASEFIRE) {
1140 /* It's already a war or aplayer can declare it soon */
1141 return TRUE;
1142 }
1143
1144 dip = adv_dipl_get(pplayer, aplayer);
1145
1146 if (dip->allied_with_enemy) {
1147 /* Don't trust someone who will declare war on us soon */
1148 return TRUE;
1149 }
1150
1151 if (player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel > 0) {
1152 return TRUE;
1153 }
1154
1155 if (pplayer->ai_common.love[player_index(aplayer)] < MAX_AI_LOVE / 10) {
1156 /* We don't trust players who we don't like. Note that
1157 * aplayer's units inside pplayer's borders decreases AI's love */
1158 return TRUE;
1159 }
1160
1161 return FALSE;
1162}
void action_array_add_all_by_result(action_id *act_array, int *position, enum action_result result)
Definition actions.c:6486
void action_array_end(action_id *act_array, int size)
Definition actions.c:6469
bool action_immune_government(struct government *gov, action_id act)
Definition actions.c:6173
static struct action * action_by_number(action_id act_id)
Definition actions.h:635
#define action_array_iterate(_act_array_, _act_id_)
Definition actions.h:497
#define action_id_get_role(act_id)
Definition actions.h:696
#define action_array_iterate_end
Definition actions.h:509
#define action_iterate_end
Definition actions.h:465
#define MAX_NUM_ACTIONS
Definition actions.h:314
#define action_iterate(_act_)
Definition actions.h:461
#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:74
void adv_data_analyze_rulesets(struct player *pplayer)
Definition advdata.c:192
void adv_data_close(struct player *pplayer)
Definition advdata.c:758
adv_want adv_gov_player_bonus_want(struct player *pplayer)
Definition advdata.c:957
void adv_data_init(struct player *pplayer)
Definition advdata.c:703
static void adv_dipl_free(const struct player *plr1, const struct player *plr2)
Definition advdata.c:802
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:610
bool is_adv_data_phase_open(struct player *pplayer)
Definition advdata.c:248
bool adv_wants_science(struct player *pplayer)
Definition advdata.c:1101
bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
Definition advdata.c:268
void adv_data_phase_done(struct player *pplayer)
Definition advdata.c:570
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
Definition advdata.c:1112
static void adv_dipl_new(const struct player *plr1, const struct player *plr2)
Definition advdata.c:790
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:829
static struct adv_dipl * adv_dipl_get(const struct player *plr1, const struct player *plr2)
Definition advdata.c:817
static bool player_has_really_useful_tech_parasite(struct player *pplayer)
Definition advdata.c:148
void adv_best_government(struct player *pplayer)
Definition advdata.c:985
void adv_data_default(struct player *pplayer)
Definition advdata.c:735
@ 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: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:75
static void nuke_units(QVariant data1, QVariant data2)
Definition dialogs.cpp:2715
static void nuke(QVariant data1, QVariant data2)
Definition dialogs.cpp:3003
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 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:828
#define effect_list_iterate_end
Definition effects.h:406
#define effect_list_iterate(effect_list, peffect)
Definition effects.h:404
bool hut_on_tile(const struct tile *ptile)
Definition extras.c:697
float adv_want
Definition fc_types.h:1354
int action_id
Definition fc_types.h:389
#define ACTRES_NONE
Definition fc_types.h:327
@ O_SHIELD
Definition fc_types.h:101
signed short Continent_id
Definition fc_types.h:372
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:62
struct world wld
Definition game.c:63
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:170
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:191
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_debug(message,...)
Definition log.h:115
#define adjc_iterate_end
Definition map.h:433
#define MAP_INDEX_SIZE
Definition map.h:137
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:428
#define whole_map_iterate(_map, _tile)
Definition map.h:545
#define whole_map_iterate_end
Definition map.h:554
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:894
#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:900
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:1405
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
#define MAX_AI_LOVE
Definition player.h:561
#define player_slots_iterate(_pslot)
Definition player.h:523
#define is_ai(plr)
Definition player.h:230
#define players_iterate_alive_end
Definition player.h:547
#define player_slots_iterate_end
Definition player.h:527
static bool player_is_cpuhog(const struct player *pplayer)
Definition player.h:575
#define players_iterate_alive(_pplayer)
Definition player.h:542
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::@89 threats
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
struct adv_data::@92 dipl
struct adv_data::@91::@94 units
int num_oceans
Definition advdata.h:110
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 government * gov
Definition advdata.h:131
struct adv_data::@93 goal
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::@91 stats
struct adv_data::@93::@95 govt
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
struct adv_data::@90 explore
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 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:81
Continent_id num_oceans
Definition map_types.h:82
bool tech_parasite_allow_holes
int love[MAX_NUM_PLAYER_SLOTS]
Definition player.h:122
enum diplstate_type type
Definition player.h:197
int techs
Definition player.h:90
struct city_list * cities
Definition player.h:279
struct player_ai ai_common
Definition player.h:286
struct government * government
Definition player.h:256
const struct ai_type * ai
Definition player.h:287
struct unit_list * units
Definition player.h:280
bool is_alive
Definition player.h:266
struct player_score score
Definition player.h:281
struct adv_data * adv
Definition player.h:332
struct player::@70::@72 server
struct unit_class::@85 adv
struct unit_type::@87 adv
struct unit_class * uclass
Definition unittype.h:556
bool igwall
Definition unittype.h:574
const struct unit_type * utype
Definition unit.h:139
enum universals_n kind
Definition fc_types.h:902
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:248
#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:244
#define is_ocean_tile(ptile)
Definition terrain.h:303
#define tile_continent(_tile)
Definition tile.h:92
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:367
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:305
#define unit_tile(_pu)
Definition unit.h:397
#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:1968
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2253
int num_role_units(int role)
Definition unittype.c:2203
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1703
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2499
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:190
bool unit_can_take_over(const struct unit *punit)
Definition unittype.c:264
bool utype_can_take_over(const struct unit_type *punittype)
Definition unittype.c:276
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:371
@ MOVE_NONE
Definition unittype.h:144
#define unit_type_iterate(_p)
Definition unittype.h:855
#define uclass_index(_c_)
Definition unittype.h:742
#define unit_type_iterate_end
Definition unittype.h:862