Freeciv-3.1
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 = {
83 .kind = VUT_IMPROVEMENT,
84 .value = {.building = pimprove}
85 };
86
88
89 /* Find largest extension */
91 switch (peffect->type) {
92#if 0
93 /* TODO */
94 case EFT_FORCE_CONTENT:
95 case EFT_FORCE_CONTENT_PCT:
96 case EFT_MAKE_CONTENT:
97 case EFT_MAKE_CONTENT_MIL:
98 case EFT_MAKE_CONTENT_MIL_PER:
99 case EFT_MAKE_CONTENT_PCT:
100 case EFT_MAKE_HAPPY:
101#endif
102 case EFT_CAPITAL_CITY:
103 case EFT_POLLU_POP_PCT:
104 case EFT_POLLU_POP_PCT_2:
105 case EFT_POLLU_PROD_PCT:
106 case EFT_OUTPUT_BONUS:
107 case EFT_OUTPUT_BONUS_2:
108 case EFT_OUTPUT_WASTE_PCT:
109 case EFT_UPKEEP_FREE:
110 requirement_vector_iterate(&peffect->reqs, preq) {
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;
122 case EFT_OUTPUT_ADD_TILE:
123 case EFT_OUTPUT_PER_TILE:
124 case EFT_OUTPUT_INC_TILE:
125 requirement_vector_iterate(&peffect->reqs, preq) {
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;
151 int players_needed = get_player_bonus(pplayer, EFT_TECH_PARASITE);
152
153 if (players_needed == 0) {
154 return FALSE;
155 }
156
157 presearch = research_get(pplayer);
159 int players_having;
160
161 if (!research_invention_gettable(presearch, tech,
163 || TECH_KNOWN == research_invention_state(presearch, tech)) {
164 continue;
165 }
166
167 players_having = 0;
168
169 players_iterate_alive(aplayer) {
170 if (aplayer == pplayer) {
171 continue;
172 }
173
174 aresearch = research_get(aplayer);
175 if (TECH_KNOWN == research_invention_state(aresearch, tech)
176 || aresearch->researching == tech) {
177 players_having++;
178 if (players_having >= players_needed) {
179 return TRUE;
180 }
181 }
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) {
211 struct unit_class *pclass = unit_class_get(punit);
212
213 adv->stats.units.byclass[uclass_index(pclass)]++;
214
215 if (unit_has_type_flag(punit, UTYF_COAST_STRICT)) {
216 adv->stats.units.coast_strict++;
217 }
218 if (utype_can_do_action(unit_type_get(punit), ACTION_SUICIDE_ATTACK)) {
219 adv->stats.units.suicide_attackers++;
220 }
221 if (unit_can_do_action(punit, ACTION_PARADROP)
222 || unit_can_do_action(punit, ACTION_PARADROP_CONQUER)
223 || unit_can_do_action(punit, ACTION_PARADROP_FRIGHTEN)
224 || unit_can_do_action(punit, ACTION_PARADROP_FRIGHTEN_CONQUER)
225 || unit_can_do_action(punit, ACTION_PARADROP_ENTER)
226 || unit_can_do_action(punit, ACTION_PARADROP_ENTER_CONQUER)) {
227 adv->stats.units.paratroopers++;
228 }
229 if (utype_can_do_action(punit->utype, ACTION_AIRLIFT)) {
230 adv->stats.units.airliftable++;
231 }
232 if (can_upgrade_unittype(pplayer, unit_type_get(punit)) >= 0) {
233 adv->stats.units.upgradeable++;
234 }
236}
237
238/**********************************************************************/
242bool is_adv_data_phase_open(struct player *pplayer)
243{
244 struct adv_data *adv = pplayer->server.adv;
245
246 return adv->phase_is_initialized;
247}
248
249/**********************************************************************/
262bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
263{
264 struct adv_data *adv = pplayer->server.adv;
265 bool danger_of_nukes;
266 action_id nuke_actions[MAX_NUM_ACTIONS];
267
268 {
269 int i = 0;
270
271 /* Conventional nukes */
272 action_list_add_all_by_result(nuke_actions, &i, ACTRES_NUKE);
273 action_list_add_all_by_result(nuke_actions, &i, ACTRES_NUKE_UNITS);
274 /* TODO: worry about spy nuking too? */
275 action_list_end(nuke_actions, i);
276 }
277
278 fc_assert_ret_val(adv != NULL, FALSE);
279
280 if (adv->phase_is_initialized) {
281 return FALSE;
282 }
284
286
287 danger_of_nukes = FALSE;
288
289 /*** Threats ***/
290
293 adv->threats.continent = fc_calloc(adv->num_continents + 1, sizeof(bool));
294 adv->threats.invasions = FALSE;
295 adv->threats.nuclear = 0; /* none */
296 adv->threats.ocean = fc_calloc(adv->num_oceans + 1, sizeof(bool));
297 adv->threats.igwall = FALSE;
298
299 players_iterate(aplayer) {
300 if (!adv_is_player_dangerous(pplayer, aplayer)) {
301 continue;
302 }
303
304 /* The idea is that if there aren't any hostile cities on
305 * our continent, the danger of land attacks is not big
306 * enough to warrant city walls. Concentrate instead on
307 * coastal fortresses and hunting down enemy transports. */
308 city_list_iterate(aplayer->cities, acity) {
310 if (continent >= 0) {
312 }
314
315 unit_list_iterate(aplayer->units, punit) {
316 const struct unit_class *pclass = unit_class_get(punit);
317
319 adv->threats.igwall = TRUE;
320 }
321
322 if (pclass->adv.sea_move != MOVE_NONE) {
323 /* If the enemy has not started sailing yet, or we have total
324 * control over the seas, don't worry, keep attacking. */
326 /* Enemy represents a cross-continental threat! */
327 adv->threats.invasions = TRUE;
328 } else if (get_transporter_capacity(punit) > 0) {
329 unit_type_iterate(cargotype) {
331 cargotype->uclass)
332 && utype_can_take_over(cargotype)) {
333 /* Enemy can transport some threatening units! */
334 adv->threats.invasions = TRUE;
335 break;
336 }
338 }
339
340 /* The idea is that while our enemies don't have any offensive
341 * seaborne units, we don't have to worry. Go on the offensive! */
342 if (unit_type_get(punit)->attack_strength > 1) {
345
346 adv->threats.ocean[-continent] = TRUE;
347 } else {
348 adjc_iterate(&(wld.map), unit_tile(punit), tile2) {
349 if (is_ocean_tile(tile2)) {
350 Continent_id continent = tile_continent(tile2);
351
352 adv->threats.ocean[-continent] = TRUE;
353 }
355 }
356 }
357 continue;
358 }
359
360 /* If our enemy builds missiles, worry about missile defense. */
361 if (utype_can_do_action(unit_type_get(punit), ACTION_SUICIDE_ATTACK)
362 && unit_type_get(punit)->attack_strength > 1) {
363 adv->threats.suicide_attack = TRUE;
364 }
365
366 /* If they build nukes, worry a lot. */
367 action_array_iterate(nuke_actions, act_id) {
368 if (unit_can_do_action(punit, act_id)) {
369 danger_of_nukes = TRUE;
370 }
373
374 /* Check for nuke capability */
375 action_array_iterate(nuke_actions, act_id) {
376 int i;
378
379 for (i = 0; i < nuke_units; i++) {
380 struct unit_type *nuke =
382
383 if (can_player_build_unit_direct(aplayer, nuke)) {
384 adv->threats.nuclear = 1;
385 }
386 }
389
390 /* Increase from fear to terror if opponent actually has nukes */
391 if (danger_of_nukes) {
392 adv->threats.nuclear++; /* sum of both fears */
393 }
394
395 /*** Exploration ***/
396
397 adv->explore.land_done = TRUE;
398 adv->explore.sea_done = TRUE;
399 adv->explore.continent = fc_calloc(adv->num_continents + 1, sizeof(bool));
400 adv->explore.ocean = fc_calloc(adv->num_oceans + 1, sizeof(bool));
401
402 whole_map_iterate(&(wld.map), ptile) {
403 Continent_id continent = tile_continent(ptile);
404
405 if (is_ocean_tile(ptile)) {
406 if (adv->explore.sea_done && has_handicap(pplayer, H_TARGETS)
407 && !map_is_known(ptile, pplayer)) {
408 /* We're not done there. */
409 adv->explore.sea_done = FALSE;
410 adv->explore.ocean[-continent] = TRUE;
411 }
412 /* skip rest, which is land only */
413 continue;
414 }
415 if (adv->explore.continent[tile_continent(ptile)]) {
416 /* we don't need more explaining, we got the point */
417 continue;
418 }
419 if (hut_on_tile(ptile)
420 && (!has_handicap(pplayer, H_HUTS)
421 || map_is_known(ptile, pplayer))) {
422 adv->explore.land_done = FALSE;
423 adv->explore.continent[continent] = TRUE;
424 continue;
425 }
426 if (has_handicap(pplayer, H_TARGETS) && !map_is_known(ptile, pplayer)) {
427 /* this AI must explore */
428 adv->explore.land_done = FALSE;
429 adv->explore.continent[continent] = TRUE;
430 }
432
433 /*** Statistics ***/
434
435 adv->stats.cities = fc_calloc(adv->num_continents + 1, sizeof(int));
436 adv->stats.ocean_cities = fc_calloc(adv->num_oceans + 1, sizeof(int));
437 adv->stats.average_production = 0;
438 city_list_iterate(pplayer->cities, pcity) {
439 Continent_id continent = tile_continent(pcity->tile);
440
441 if (continent >= 0) {
442 adv->stats.cities[continent]++;
443 } else {
444 adv->stats.ocean_cities[-continent]++;
445 }
446 adv->stats.average_production += pcity->surplus[O_SHIELD];
448 adv->stats.average_production /= MAX(1, city_list_size(pplayer->cities));
449
450 /*** Diplomacy ***/
451
452 players_iterate(aplayer) {
453 struct adv_dipl *dip = adv_dipl_get(pplayer, aplayer);
454
456 players_iterate(check_pl) {
457 if (pplayers_allied(aplayer, check_pl)
458 && player_diplstate_get(pplayer, check_pl)->type == DS_WAR) {
459 dip->allied_with_enemy = TRUE;
460 }
463
465
466 adv->dipl.production_leader = NULL;
467 players_iterate(aplayer) {
468 if (adv->dipl.production_leader == NULL
469 || adv->dipl.production_leader->score.mfg < aplayer->score.mfg) {
470 adv->dipl.production_leader = aplayer;
471 }
473
474 adv->dipl.tech_leader = NULL;
475 players_iterate(aplayer) {
476 if (adv->dipl.tech_leader == NULL
477 || adv->dipl.tech_leader->score.techs < aplayer->score.techs) {
478 adv->dipl.tech_leader = aplayer;
479 }
481
482 /*** Priorities ***/
483
484 /* NEVER set these to zero! Weight values are usually multiplied by
485 * these values, so be careful with them. They are used in city
486 * and government calculations, and food and shields should be
487 * slightly bigger because we only look at surpluses there. They
488 * are all WAGs. */
491 if (adv_wants_science(pplayer)) {
492 adv->luxury_priority = 1;
494 } else {
496 adv->science_priority = 1;
497 }
499 adv->happy_priority = 1;
500 adv->unhappy_priority = TRADE_WEIGHTING; /* danger */
501 adv->angry_priority = TRADE_WEIGHTING * 3; /* grave danger */
504
505 /* Research want */
506 if (is_future_tech(research_get(pplayer)->researching)
508 adv->wants_science = FALSE;
509 } else {
510 adv->wants_science = TRUE;
511 }
512
513 /* max num cities
514 * The idea behind this code is that novice players don't understand that
515 * expansion is critical and find it very annoying.
516 * With the following code AI players will try to be only a bit better
517 * than the best human players. This should lead to more exciting games
518 * for the beginners.
519 */
520 if (has_handicap(pplayer, H_EXPANSION)) {
521 bool found_human = FALSE;
522 adv->max_num_cities = 3;
523 players_iterate_alive(aplayer) {
524 if (aplayer == pplayer || is_ai(aplayer)) {
525 continue;
526 }
528 city_list_size(aplayer->cities) + 3);
529 found_human = TRUE;
531 if (!found_human) {
533 }
534 } else {
536 }
537
538 count_my_units(pplayer);
539
541
542 /* Government */
544 adv_best_government(pplayer);
546
547 return TRUE;
548}
549
550/**********************************************************************/
553void adv_data_phase_done(struct player *pplayer)
554{
555 struct adv_data *adv = pplayer->server.adv;
556
557 fc_assert_ret(adv != NULL);
558
559 if (!adv->phase_is_initialized) {
560 return;
561 }
562
563 free(adv->explore.ocean);
564 adv->explore.ocean = NULL;
565
566 free(adv->explore.continent);
567 adv->explore.continent = NULL;
568
569 free(adv->threats.continent);
570 adv->threats.continent = NULL;
571
572 free(adv->threats.ocean);
573 adv->threats.ocean = NULL;
574
575 free(adv->stats.cities);
576 adv->stats.cities = NULL;
577
578 free(adv->stats.ocean_cities);
579 adv->stats.ocean_cities = NULL;
580
581 adv->num_continents = 0;
582 adv->num_oceans = 0;
583
585}
586
587/**********************************************************************/
593struct adv_data *adv_data_get(struct player *pplayer, bool *caller_closes)
594{
595 struct adv_data *adv = pplayer->server.adv;
596
597 fc_assert_ret_val(adv != NULL, NULL);
598
599 /* It's certainly indication of bug causing problems
600 if this adv_data_get() gets called between adv_data_phase_done() and
601 adv_data_phase_init(), since we may end up calling those
602 functions if number of known continents has changed.
603
604 Consider following case:
605 Correct call order would be:
606 a) adv_data_phase_init()
607 b) adv_data_get() -> adv_data_phase_done()
608 c) adv_data_get() -> adv_data_phase_init()
609 d) adv_data_phase_done()
610 e) do something
611 f) adv_data_phase_init()
612
613 In (e) data phase would be closed and data would be
614 correctly initialized at (f), which is probably beginning
615 next turn.
616
617 Buggy version where adv_data_get() (b&c) gets called after (d):
618 a) adv_data_phase_init()
619 d) adv_data_phase_done()
620 b) adv_data_get() -> adv_data_phase_done()
621 c) adv_data_get() -> adv_data_phase_init()
622 e) do something
623 f) adv_data_phase_init()
624
625 Now in (e) data phase would be open. When adv_data_phase_init()
626 then finally gets called and it really should recreate data
627 to match situation of new turn, it detects that data phase
628 is already initialized and does nothing.
629
630 So, this assertion is here for a reason!
631
632 Code below tries to fix the situation best it can if such a bug is
633 encountered. Since we are probably going to trust that to be enough
634 instead of making intrusive fixes for actual bug in stable branch,
635 do not assert for non-debug builds of stable versions. */
636#if defined(FREECIV_DEBUG) || defined(IS_DEVEL_VERSION)
637 fc_assert(caller_closes != NULL || adv->phase_is_initialized);
638#endif
639
640 if (caller_closes != NULL) {
641 *caller_closes = FALSE;
642 }
643
645 || adv->num_oceans != wld.map.num_oceans) {
646 /* we discovered more continents, recalculate! */
647
648 if (adv->phase_is_initialized) {
649 /* Only call these in this order if inside data phase.
650 This is blanket "fix" for all cases where adv_data_get() is called
651 at illegal time. This at least minimize bad effects of such calls. */
652 adv_data_phase_done(pplayer);
653 adv_data_phase_init(pplayer, FALSE);
654 } else {
655 /* Call them in "wrong" order so we return recalculated data to caller,
656 but leave data phase closed.
657 This is blanket "fix" for all cases where adv_data_get() is called
658 at illegal time. This at least minimize bad effects of such calls.
659
660 Arguably this is not buggy at all but works just as designed in
661 case of being called in alternate movement mode for players
662 other than currently moving one (for diplomacy between the two,
663 for example) */
664 log_debug("%s advisor data phase closed when adv_data_get() called",
665 player_name(pplayer));
666 adv_data_phase_init(pplayer, FALSE);
667 if (caller_closes != NULL) {
668 *caller_closes = TRUE;
669 } else {
670 adv_data_phase_done(pplayer);
671 }
672 }
673 } else {
674 if (!adv->phase_is_initialized && caller_closes != NULL) {
675 adv_data_phase_init(pplayer, FALSE);
676 *caller_closes = TRUE;
677 }
678 }
679
680 return adv;
681}
682
683/**********************************************************************/
686void adv_data_init(struct player *pplayer)
687{
688 struct adv_data *adv;
689
690 if (pplayer->server.adv == NULL) {
691 pplayer->server.adv = fc_calloc(1, sizeof(*pplayer->server.adv));
692 }
693 adv = pplayer->server.adv;
694
695 adv->government_want = NULL;
696
698 sizeof(*adv->dipl.adv_dipl_slots));
699 player_slots_iterate(pslot) {
700 struct adv_dipl **dip_slot =
702 *dip_slot = NULL;
704
705 players_iterate(aplayer) {
706 adv_dipl_new(pplayer, aplayer);
707 if (aplayer != pplayer) {
708 adv_dipl_new(aplayer, pplayer);
709 }
711
712 adv_data_default(pplayer);
713}
714
715/**********************************************************************/
718void adv_data_default(struct player *pplayer)
719{
720 struct adv_data *adv = pplayer->server.adv;
721
722 fc_assert_ret(adv != NULL);
723
724 adv->govt_reeval = 0;
726 (government_count() + 1)
727 * sizeof(*adv->government_want));
728 memset(adv->government_want, 0,
729 (government_count() + 1) * sizeof(*adv->government_want));
730
731 adv->wonder_city = 0;
732
733 adv->wants_science = TRUE;
734 adv->celebrate = FALSE;
735 adv->max_num_cities = 10000;
736}
737
738/**********************************************************************/
741void adv_data_close(struct player *pplayer)
742{
743 struct adv_data *adv = pplayer->server.adv;
744
745 fc_assert_ret(NULL != adv);
746
747 adv_data_phase_done(pplayer);
748
749 if (adv->government_want != NULL) {
750 free(adv->government_want);
751 }
752
753 if (adv->dipl.adv_dipl_slots != NULL) {
754 players_iterate(aplayer) {
755 adv_dipl_free(pplayer, aplayer);
756 if (aplayer != pplayer) {
757 adv_dipl_free(aplayer, pplayer);
758 }
761 }
762
763 if (adv != NULL) {
764 free(adv);
765 }
766
767 pplayer->server.adv = NULL;
768}
769
770/**********************************************************************/
773static void adv_dipl_new(const struct player *plr1,
774 const struct player *plr2)
775{
776 struct adv_dipl **dip_slot =
778
779 *dip_slot = fc_calloc(1, sizeof(struct adv_dipl));
780}
781
782/**********************************************************************/
785static void adv_dipl_free(const struct player *plr1,
786 const struct player *plr2)
787{
788 struct adv_dipl **dip_slot =
790
791 if (*dip_slot != NULL) {
792 FC_FREE(*dip_slot);
793 *dip_slot = NULL;
794 }
795}
796
797/**********************************************************************/
800static struct adv_dipl *adv_dipl_get(const struct player *plr1,
801 const struct player *plr2)
802{
803 struct adv_dipl **dip_slot =
805
806 return *dip_slot;
807}
808
809/**********************************************************************/
813{
814 adv_want bonus = 0;
815
816 /* TODO: Individual and well balanced value. */
817 action_iterate(act) {
818 struct action *paction = action_by_number(act);
819
820 if (!action_immune_government(gov, act)) {
821 /* This government doesn't provide immunity against this
822 * action. */
823 continue;
824 }
825
826 switch (paction->result) {
827 case ACTRES_ATTACK:
828 case ACTRES_SPY_INCITE_CITY:
829 case ACTRES_CONQUER_CITY:
830 bonus += 4;
831 break;
832 case ACTRES_SPY_BRIBE_UNIT:
833 bonus += 2;
834 break;
835 case ACTRES_TRANSFORM_TERRAIN:
836 bonus += 1.5;
837 break;
838 case ACTRES_CULTIVATE:
839 case ACTRES_PLANT:
840 bonus += 0.3;
841 break;
842 case ACTRES_CONQUER_EXTRAS:
843 case ACTRES_PILLAGE:
844 bonus += 0.2;
845 break;
846 case ACTRES_HUT_ENTER:
847 case ACTRES_HUT_FRIGHTEN:
848 /* It is mine. My own. My precious. */
849 bonus += 0.1;
850 break;
851 case ACTRES_SPY_INVESTIGATE_CITY:
852 case ACTRES_SPY_POISON:
853 case ACTRES_SPY_SPREAD_PLAGUE:
854 case ACTRES_SPY_STEAL_GOLD:
855 case ACTRES_SPY_SABOTAGE_CITY:
856 case ACTRES_SPY_TARGETED_SABOTAGE_CITY:
857 case ACTRES_SPY_SABOTAGE_CITY_PRODUCTION:
858 case ACTRES_SPY_STEAL_TECH:
859 case ACTRES_SPY_TARGETED_STEAL_TECH:
860 case ACTRES_SPY_SABOTAGE_UNIT:
861 case ACTRES_CAPTURE_UNITS:
862 case ACTRES_STEAL_MAPS:
863 case ACTRES_BOMBARD:
864 case ACTRES_SPY_NUKE:
865 case ACTRES_NUKE:
866 case ACTRES_NUKE_UNITS:
867 case ACTRES_DESTROY_CITY:
868 case ACTRES_EXPEL_UNIT:
869 case ACTRES_STRIKE_BUILDING:
870 case ACTRES_STRIKE_PRODUCTION:
871 case ACTRES_SPY_ATTACK:
872 /* Being a target of this is usually undesirable */
873 /* TODO: Individual and well balanced values. */
874 bonus += 0.1;
875 break;
876
877 case ACTRES_UNIT_MOVE:
878 case ACTRES_MARKETPLACE:
879 case ACTRES_FOUND_CITY:
880 case ACTRES_DISBAND_UNIT:
881 case ACTRES_PARADROP:
882 case ACTRES_PARADROP_CONQUER:
883 case ACTRES_FORTIFY:
884 /* Wants the ability to do this to it self. Don't want others
885 * to target it. Do nothing since action_immune_government()
886 * doesn't separate based on who the actor is. */
887 break;
888
889 case ACTRES_NONE:
890 /* Ruleset defined */
891 break;
892
893 case ACTRES_ESTABLISH_EMBASSY:
894 case ACTRES_TRADE_ROUTE:
895 case ACTRES_JOIN_CITY:
896 case ACTRES_HELP_WONDER:
897 case ACTRES_DISBAND_UNIT_RECOVER:
898 case ACTRES_HOME_CITY:
899 case ACTRES_HOMELESS:
900 case ACTRES_UPGRADE_UNIT:
901 case ACTRES_AIRLIFT:
902 case ACTRES_HEAL_UNIT:
903 case ACTRES_ROAD:
904 case ACTRES_CONVERT:
905 case ACTRES_BASE:
906 case ACTRES_MINE:
907 case ACTRES_IRRIGATE:
908 case ACTRES_CLEAN_POLLUTION:
909 case ACTRES_CLEAN_FALLOUT:
910 case ACTRES_TRANSPORT_ALIGHT:
911 case ACTRES_TRANSPORT_UNLOAD:
912 case ACTRES_TRANSPORT_DISEMBARK:
913 case ACTRES_TRANSPORT_BOARD:
914 case ACTRES_TRANSPORT_EMBARK:
915 /* Could be good. An embassy gives permanent contact. A trade
916 * route gives gold per turn. Join city gives population. Help
917 * wonder gives shields. */
918 /* TODO: Individual and well balanced values. */
919 break;
920 }
922
923 return bonus;
924}
925
926/**********************************************************************/
933{
934 adv_want bonus = 0;
935
936 /* Bonuses for non-economic abilities. We increase val by
937 * a very small amount here to choose govt in cases where
938 * we have no cities yet. */
939 bonus += get_player_bonus(pplayer, EFT_VETERAN_BUILD) > 0 ? 3 : 0;
940 bonus += get_player_bonus(pplayer, EFT_INSPIRE_PARTISANS) > 0 ? 3 : 0;
941 bonus += get_player_bonus(pplayer, EFT_RAPTURE_GROW) > 0 ? 2 : 0;
942 bonus += get_player_bonus(pplayer, EFT_FANATICS) > 0 ? 3 : 0;
943 bonus += get_player_bonus(pplayer, EFT_OUTPUT_INC_TILE) * 8;
944
945 return bonus;
946}
947
948/**********************************************************************/
960void adv_best_government(struct player *pplayer)
961{
962 struct adv_data *adv = adv_data_get(pplayer, NULL);
963 int best_val = 0;
964 struct government *current_gov = government_of_player(pplayer);
965
966 adv->goal.govt.gov = current_gov;
967 adv->goal.govt.val = 0;
968 adv->goal.govt.req = A_UNSET;
969 adv->goal.revolution = current_gov;
970
971 if (has_handicap(pplayer, H_AWAY) || !pplayer->is_alive) {
972 return;
973 }
974
975 if (adv->govt_reeval == 0) {
976 const struct research *presearch = research_get(pplayer);
977
979 adv_want val = 0;
980 bool override = FALSE;
981
983 continue; /* pointless */
984 }
985 if (gov->ai.better
986 && can_change_to_government(pplayer, gov->ai.better)) {
987 continue; /* we have better governments available */
988 }
989
990 CALL_PLR_AI_FUNC(gov_value, pplayer, pplayer, gov, &val, &override);
991
992 if (!override) {
993 int dist;
994 adv_want bonus = 0; /* in percentage */
995 int revolution_turns;
996
997 pplayer->government = gov;
998 /* Ideally we should change tax rates here, but since
999 * this is a rather big CPU operation, we'd rather not. */
1000 check_player_max_rates(pplayer);
1001 city_list_iterate(pplayer->cities, acity) {
1002 auto_arrange_workers(acity);
1004 city_list_iterate(pplayer->cities, pcity) {
1005 val += adv_eval_calc_city(pcity, adv);
1007
1008 bonus += adv_gov_action_immunity_want(gov);
1009 bonus += adv_gov_player_bonus_want(pplayer);
1010
1011 revolution_turns = get_player_bonus(pplayer, EFT_REVOLUTION_UNHAPPINESS);
1012 if (revolution_turns > 0) {
1013 bonus -= 6 / revolution_turns;
1014 }
1015
1016 val += (val * bonus) / 100;
1017
1018 /* FIXME: handle reqs other than technologies. */
1019 dist = 0;
1020 requirement_vector_iterate(&gov->reqs, preq) {
1021 if (VUT_ADVANCE == preq->source.kind) {
1022 int gut = research_goal_unknown_techs(presearch,
1023 advance_number(preq->source.value.advance));
1024
1025 dist += MAX(1, gut);
1026 }
1028 val = amortize(val, dist);
1029 }
1030
1031 adv->government_want[government_index(gov)] = val; /* Save want */
1033 /* Now reset our gov to it's real state. */
1034 pplayer->government = current_gov;
1035 city_list_iterate(pplayer->cities, acity) {
1036 auto_arrange_workers(acity);
1038 if (player_is_cpuhog(pplayer)) {
1039 adv->govt_reeval = 1;
1040 } else {
1041 adv->govt_reeval = CLIP(5, city_list_size(pplayer->cities), 20);
1042 }
1043 }
1044 adv->govt_reeval--;
1045
1046 /* Figure out which government is the best for us this turn. */
1047 governments_iterate(gov) {
1048 int gi = government_index(gov);
1049 if (adv->government_want[gi] > best_val
1050 && can_change_to_government(pplayer, gov)) {
1051 best_val = adv->government_want[gi];
1052 adv->goal.revolution = gov;
1053 }
1054 if (adv->government_want[gi] > adv->goal.govt.val) {
1055 adv->goal.govt.gov = gov;
1056 adv->goal.govt.val = adv->government_want[gi];
1057
1058 /* FIXME: handle reqs other than technologies. */
1059 adv->goal.govt.req = A_NONE;
1060 requirement_vector_iterate(&gov->reqs, preq) {
1061 if (VUT_ADVANCE == preq->source.kind) {
1062 adv->goal.govt.req = advance_number(preq->source.value.advance);
1063 break;
1064 }
1066 }
1068 /* Goodness of the ideal gov is calculated relative to the goodness of the
1069 * best of the available ones. */
1070 adv->goal.govt.val -= best_val;
1071}
1072
1073/**********************************************************************/
1076bool adv_wants_science(struct player *pplayer)
1077{
1078 return adv_data_get(pplayer, NULL)->wants_science;
1079}
1080
1081/**********************************************************************/
1087bool adv_is_player_dangerous(struct player *pplayer,
1088 struct player *aplayer)
1089{
1090 struct adv_dipl *dip;
1091 enum diplstate_type ds;
1092 enum override_bool dang = NO_OVERRIDE;
1093
1094 if (is_ai(pplayer)) {
1095 /* Give AI code possibility to decide itself */
1096 CALL_PLR_AI_FUNC(consider_plr_dangerous, pplayer, pplayer, aplayer, &dang);
1097 }
1098
1099 if (dang == OVERRIDE_FALSE) {
1100 return FALSE;
1101 }
1102
1103 if (dang == OVERRIDE_TRUE) {
1104 return TRUE;
1105 }
1106
1107 if (pplayer == aplayer) {
1108 /* We always trust ourself */
1109 return FALSE;
1110 }
1111
1112 ds = player_diplstate_get(pplayer, aplayer)->type;
1113
1114 if (ds == DS_WAR || ds == DS_CEASEFIRE) {
1115 /* It's already a war or aplayer can declare it soon */
1116 return TRUE;
1117 }
1118
1119 dip = adv_dipl_get(pplayer, aplayer);
1120
1121 if (dip->allied_with_enemy) {
1122 /* Don't trust someone who will declare war on us soon */
1123 return TRUE;
1124 }
1125
1126 if (player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel > 0) {
1127 return TRUE;
1128 }
1129
1130 if (pplayer->ai_common.love[player_index(aplayer)] < MAX_AI_LOVE / 10) {
1131 /* We don't trust players who we don't like. Note that
1132 * aplayer's units inside pplayer's borders decreases AI's love */
1133 return TRUE;
1134 }
1135
1136 return FALSE;
1137}
void action_list_end(action_id *act_list, int size)
Definition actions.c:7397
bool action_immune_government(struct government *gov, action_id act)
Definition actions.c:7101
void action_list_add_all_by_result(action_id *act_list, int *position, enum action_result result)
Definition actions.c:7414
static struct action * action_by_number(action_id act_id)
Definition actions.h:638
#define action_id_get_role(act_id)
Definition actions.h:708
#define action_array_iterate(_act_list_, _act_id_)
Definition actions.h:504
#define action_array_iterate_end
Definition actions.h:516
#define action_iterate_end
Definition actions.h:472
#define MAX_NUM_ACTIONS
Definition actions.h:296
#define action_iterate(_act_)
Definition actions.h:467
#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:741
adv_want adv_gov_player_bonus_want(struct player *pplayer)
Definition advdata.c:932
void adv_data_init(struct player *pplayer)
Definition advdata.c:686
static void adv_dipl_free(const struct player *plr1, const struct player *plr2)
Definition advdata.c:785
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:593
bool is_adv_data_phase_open(struct player *pplayer)
Definition advdata.c:242
bool adv_wants_science(struct player *pplayer)
Definition advdata.c:1076
bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
Definition advdata.c:262
void adv_data_phase_done(struct player *pplayer)
Definition advdata.c:553
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
Definition advdata.c:1087
static void adv_dipl_new(const struct player *plr1, const struct player *plr2)
Definition advdata.c:773
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:812
static struct adv_dipl * adv_dipl_get(const struct player *plr1, const struct player *plr2)
Definition advdata.c:800
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:960
void adv_data_default(struct player *pplayer)
Definition advdata.c:718
@ 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:374
struct player * player_leading_spacerace(void)
Definition aisupport.c:38
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_list_iterate_end
Definition city.h:490
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:369
static void nuke_units(QVariant data1, QVariant data2)
Definition dialogs.cpp:2631
static void nuke(QVariant data1, QVariant data2)
Definition dialogs.cpp:2903
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:73
struct effect_list * get_req_source_effects(struct universal *psource)
Definition effects.c:146
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:771
#define effect_list_iterate_end
Definition effects.h:375
#define effect_list_iterate(effect_list, peffect)
Definition effects.h:373
bool hut_on_tile(const struct tile *ptile)
Definition extras.c:672
float adv_want
Definition fc_types.h:1206
int action_id
Definition fc_types.h:359
@ O_SHIELD
Definition fc_types.h:91
signed short Continent_id
Definition fc_types.h:342
override_bool
Definition fc_types.h:84
@ OVERRIDE_TRUE
Definition fc_types.h:84
@ NO_OVERRIDE
Definition fc_types.h:84
@ OVERRIDE_FALSE
Definition fc_types.h:84
struct civ_game game
Definition game.c:57
struct world wld
Definition game.c:58
Government_type_id government_count(void)
Definition government.c:70
struct government * government_of_player(const struct player *pplayer)
Definition government.c:113
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:165
Government_type_id government_index(const struct government *pgovern)
Definition government.c:81
#define governments_iterate(NAME_pgov)
Definition government.h:121
#define governments_iterate_end
Definition government.h:124
static GtkWidget * source
Definition gotodlg.c:58
GType type
Definition repodlgs.c:1312
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:427
#define MAP_INDEX_SIZE
Definition map.h:131
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:422
#define whole_map_iterate(_map, _tile)
Definition map.h:539
#define whole_map_iterate_end
Definition map.h:548
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:886
#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:826
int player_slot_count(void)
Definition player.c:411
const char * player_name(const struct player *pplayer)
Definition player.c:886
int player_slot_index(const struct player_slot *pslot)
Definition player.c:419
int player_index(const struct player *pplayer)
Definition player.c:820
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:317
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1381
#define players_iterate_end
Definition player.h:535
#define players_iterate(_pplayer)
Definition player.h:530
#define MAX_AI_LOVE
Definition player.h:559
#define player_slots_iterate(_pslot)
Definition player.h:521
#define is_ai(plr)
Definition player.h:234
#define players_iterate_alive_end
Definition player.h:545
#define player_slots_iterate_end
Definition player.h:525
static bool player_is_cpuhog(const struct player *pplayer)
Definition player.h:573
#define players_iterate_alive(_pplayer)
Definition player.h:540
void check_player_max_rates(struct player *pplayer)
Definition plrhand.c:657
#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:747
struct research * research_get(const struct player *pplayer)
Definition research.c:126
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:616
bool research_invention_gettable(const struct research *presearch, const Tech_type_id tech, bool allow_holes)
Definition research.c:690
#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
enum action_result result
Definition actions.h:382
bool wants_science
Definition advdata.h:133
struct adv_data::@90 threats
int req
Definition advdata.h:127
struct adv_data::@91 explore
short govt_reeval
Definition advdata.h:120
adv_want val
Definition advdata.h:126
int pollution_priority
Definition advdata.h:115
struct adv_data::@92 stats
int shield_priority
Definition advdata.h:107
bool * ocean
Definition advdata.h:60
struct adv_data::@92::@95 units
int * cities
Definition advdata.h:90
int * ocean_cities
Definition advdata.h:91
bool invasions
Definition advdata.h:58
struct government * revolution
Definition advdata.h:129
int infra_priority
Definition advdata.h:116
struct adv_dipl ** adv_dipl_slots
Definition advdata.h:96
bool igwall
Definition advdata.h:63
bool celebrate
Definition advdata.h:136
int num_oceans
Definition advdata.h:104
int unhappy_priority
Definition advdata.h:113
int luxury_priority
Definition advdata.h:109
int nuclear
Definition advdata.h:62
int angry_priority
Definition advdata.h:114
struct adv_data::@94::@96 govt
int food_priority
Definition advdata.h:108
int max_num_cities
Definition advdata.h:139
int num_continents
Definition advdata.h:103
adv_want * government_want
Definition advdata.h:119
struct player * production_leader
Definition advdata.h:100
struct adv_data::@93 dipl
struct government * gov
Definition advdata.h:125
int happy_priority
Definition advdata.h:112
bool * continent
Definition advdata.h:59
enum adv_improvement_status impr_calc[B_LAST]
Definition advdata.h:53
bool phase_is_initialized
Definition advdata.h:47
int wonder_city
Definition advdata.h:50
enum req_range impr_range[B_LAST]
Definition advdata.h:54
int science_priority
Definition advdata.h:111
struct adv_data::@94 goal
int gold_priority
Definition advdata.h:110
struct player * tech_leader
Definition advdata.h:99
struct player * spacerace_leader
Definition advdata.h:98
bool allied_with_enemy
Definition advdata.h:42
struct packet_game_info info
Definition game.h:89
struct government * government_during_revolution
Definition game.h:94
int num_continents
Definition map_types.h:78
int num_oceans
Definition map_types.h:79
bool tech_parasite_allow_holes
int love[MAX_NUM_PLAYER_SLOTS]
Definition player.h:130
enum diplstate_type type
Definition player.h:201
int techs
Definition player.h:99
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_score score
Definition player.h:283
struct adv_data * adv
Definition player.h:334
struct player::@69::@71 server
Tech_type_id researching
Definition research.h:52
enum move_level sea_move
Definition unittype.h:150
struct unit_class::@84 adv
struct unit_type::@87 adv
struct unit_class * uclass
Definition unittype.h:537
bool igwall
Definition unittype.h:555
const struct unit_type * utype
Definition unit.h:139
enum universals_n kind
Definition fc_types.h:758
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:289
#define tile_continent(_tile)
Definition tile.h:91
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:328
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:299
#define unit_tile(_pu)
Definition unit.h:395
#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
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2301
int num_role_units(int role)
Definition unittype.c:2251
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1755
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype)
Definition unittype.c:2015
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2547
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:184
bool unit_can_take_over(const struct unit *punit)
Definition unittype.c:258
bool utype_can_take_over(const struct unit_type *punittype)
Definition unittype.c:270
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:443
@ MOVE_NONE
Definition unittype.h:131
#define unit_type_iterate(_p)
Definition unittype.h:841
#define uclass_index(_c_)
Definition unittype.h:729
#define unit_type_iterate_end
Definition unittype.h:848