Freeciv-3.4
Loading...
Searching...
No Matches
daicity.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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#include <string.h>
19#include <math.h> /* pow */
20
21/* utility */
22#include "rand.h"
23#include "registry.h"
24
25/* common */
26#include "actions.h"
27#include "game.h"
28#include "government.h"
29#include "map.h"
30#include "research.h"
31#include "specialist.h"
32
33/* server */
34#include "cityhand.h"
35#include "citytools.h"
36#include "cityturn.h"
37#include "notify.h"
38#include "plrhand.h"
39#include "srv_log.h"
40#include "unithand.h"
41#include "unittools.h"
42
43/* server/advisors */
44#include "advdata.h"
45#include "advtools.h"
46#include "autoworkers.h"
47#include "advbuilding.h"
48#include "infracache.h"
49
50/* ai */
51#include "aitraits.h"
52#include "difficulty.h"
53#include "handicaps.h"
54
55/* ai/default */
56#include "daidata.h"
57#include "daidiplomacy.h"
58#include "daidomestic.h"
59#include "daihand.h"
60#include "daimilitary.h"
61#include "daieffects.h"
62#include "daiplayer.h"
63#include "daisettler.h"
64#include "daitools.h"
65
66#include "daicity.h"
67
68#define LOG_BUY LOG_DEBUG
69#define LOG_EMERGENCY LOG_VERBOSE
70#define LOG_WANT LOG_VERBOSE
71
72/* TODO: AI_CITY_RECALC_SPEED should be configurable to ai difficulty.
73 -kauf */
74#define AI_CITY_RECALC_SPEED 5
75
76#define AI_BA_RECALC_SPEED 5
77
78#define SPECVEC_TAG tech
79#define SPECVEC_TYPE struct advance *
80#include "specvec.h"
81
82#define SPECVEC_TAG impr
83#define SPECVEC_TYPE const struct impr_type *
84#include "specvec.h"
85
86/* Iterate over cities within a certain range around a given city
87 * (city_here) that exist within a given city list. */
88#define city_range_iterate(city_here, list, range, city) \
89{ \
90 city_list_iterate(list, city) { \
91 if (range == REQ_RANGE_PLAYER \
92 || range == REQ_RANGE_TEAM \
93 || range == REQ_RANGE_ALLIANCE \
94 || (range == REQ_RANGE_TRADE_ROUTE \
95 && (city == city_here \
96 || have_cities_trade_route(city, city_here))) \
97 || ((range == REQ_RANGE_CITY || range == REQ_RANGE_LOCAL) \
98 && city == city_here) \
99 || (range == REQ_RANGE_CONTINENT \
100 && tile_continent(city->tile) == \
101 tile_continent(city_here->tile))) {
102
103#define city_range_iterate_end \
104 } \
105 } city_list_iterate_end; \
106}
107
108#define CITY_EMERGENCY(pcity) \
109 (pcity->surplus[O_SHIELD] < 0 || city_unhappy(pcity) \
110 || pcity->food_stock + pcity->surplus[O_FOOD] < 0)
111
112static void dai_city_sell_noncritical(struct city *pcity,
113 bool redundant_only);
114static void resolve_city_emergency(struct ai_type *ait,
115 struct player *pplayer,
116 struct city *pcity);
117
118/**********************************************************************/
131 struct player *pplayer,
132 const struct city *pcity,
133 const struct impr_type *pimprove,
134 const struct advance *tech,
135 adv_want building_want)
136{
137 /* The conversion factor was determined by experiment,
138 * and might need adjustment. See also dai_tech_effect_values()
139 */
140 const adv_want tech_want
141 = building_want * def_ai_city_data(pcity, ait)->building_wait * 14 / 8;
142#if 0
143 /* This logging is relatively expensive,
144 * so activate it only while necessary. */
145 TECH_LOG(LOG_DEBUG, pplayer, tech,
146 "wanted by %s for building: %d -> %d",
148 building_want, tech_want);
149#endif /* 0 */
150 if (tech) {
151 def_ai_player_data(pplayer, ait)->tech_want[advance_index(tech)]
152 += tech_want;
153 }
154}
155
156/**********************************************************************/
161 struct player *pplayer,
162 const struct city *pcity,
163 const struct impr_type *pimprove,
165 adv_want building_want)
166{
167 int t;
169
170 for (t = 0; t < n_needed_techs; t++) {
171 want_tech_for_improvement_effect(ait, pplayer, pcity, pimprove,
173 building_want);
174 }
175}
176
177/**********************************************************************/
182 struct player *pplayer,
183 const struct city *pcity,
184 const struct impr_type *pimprove,
185 adv_want building_want)
186{
188 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
189 want_tech_for_improvement_effect(ait, pplayer, pcity, pimprove,
190 pobs->source.value.advance,
191 -building_want);
192 }
194}
195
196/**********************************************************************/
204static void dai_barbarian_choose_build(struct player *pplayer,
205 struct city *pcity,
206 struct adv_choice *choice)
207{
208 struct unit_type *bestunit = nullptr;
209 int i, bestattack = 0;
210 const struct civ_map *nmap = &(wld.map);
211
212 /* Choose the best unit among the basic ones */
213 for (i = 0; i < num_role_units(L_BARBARIAN_BUILD); i++) {
215
216 if (iunit->attack_strength > bestattack
218 RPT_CERTAIN)) {
219 bestunit = iunit;
220 bestattack = iunit->attack_strength;
221 }
222 }
223
224 /* Choose among those made available through other civ's research */
225 for (i = 0; i < num_role_units(L_BARBARIAN_BUILD_TECH); i++) {
227
228 if (iunit->attack_strength > bestattack
230 RPT_CERTAIN)) {
231 bestunit = iunit;
232 bestattack = iunit->attack_strength;
233 }
234 }
235
236 /* If found anything, put it into the choice */
237 if (bestunit) {
238 choice->value.utype = bestunit;
239 /* "Overriding military emergency" indicator */
241 choice->type = CT_ATTACKER;
242 adv_choice_set_use(choice, "barbarian");
243 } else {
244 log_base(LOG_WANT, "Barbarians don't know what to build!");
245 }
246}
247
248/**********************************************************************/
256static void dai_city_choose_build(struct ai_type *ait,
257 struct player *pplayer,
258 struct city *pcity)
259{
260 struct adv_choice *newchoice;
261 struct adv_data *adv = adv_data_get(pplayer, nullptr);
262 struct ai_city *city_data = def_ai_city_data(pcity, ait);
263 const struct civ_map *nmap = &(wld.map);
264
265 if (has_handicap(pplayer, H_AWAY)
267 && city_data->urgency == 0) {
268 /* Don't change existing productions unless we have to. */
269 return;
270 }
271
272 if (is_barbarian(pplayer)) {
273 dai_barbarian_choose_build(pplayer, pcity, &(city_data->choice));
274 } else {
275 if ((city_data->choice.want < DAI_WANT_MILITARY_EMERGENCY
276 || city_data->urgency == 0)
277 && !(dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0
278 && pcity->id != adv->wonder_city)) {
280 adv_choice_copy(&(city_data->choice),
283 }
284 }
285
286 /* Fallbacks */
287 if (city_data->choice.want == 0) {
288 /* Fallbacks do happen with techlevel 0, which is now default. -- Per */
289 CITY_LOG(LOG_WANT, pcity, "Falling back - didn't want to build soldiers,"
290 " workers, caravans, settlers, or buildings!");
291 city_data->choice.want = 1;
293 city_data->choice.value.utype
295 city_data->choice.type = CT_CIVILIAN;
296 adv_choice_set_use(&(city_data->choice), "fallback trade route");
297 } else {
298 unsigned int our_def = assess_defense_quadratic(ait, pcity);
299
300 if (our_def == 0
301 && dai_process_defender_want(ait, nmap, pplayer, pcity, 1,
302 &(city_data->choice), 0)) {
303 adv_choice_set_use(&(city_data->choice), "fallback defender");
304 CITY_LOG(LOG_DEBUG, pcity, "Building fallback defender");
305 } else if (best_role_unit(pcity, UTYF_WORKERS)) {
306 city_data->choice.value.utype
308 city_data->choice.type = CT_CIVILIAN;
309 adv_choice_set_use(&(city_data->choice), "fallback worker");
310 } else {
311 CITY_LOG(LOG_ERROR, pcity, "Cannot even build a fallback "
312 "(caravan/coinage/settlers). Fix the ruleset!");
313 city_data->choice.want = 0;
314 }
315 }
316 }
317
318 if (city_data->choice.want != 0) {
319 struct universal build_new;
320
322
323#ifdef ADV_CHOICE_TRACK
324 if (city_data->choice.log_if_chosen) {
325 log_normal("%s wants %s for %s with desire " ADV_WANT_PRINTF ".",
328 city_data->choice.use,
329 city_data->choice.want);
330 }
331#endif /* ADV_CHOICE_TRACK */
332
333 CITY_LOG(LOG_DEBUG, pcity, "wants %s with desire " ADV_WANT_PRINTF ".",
335 city_data->choice.want);
336
337 switch (city_data->choice.type) {
338 case CT_CIVILIAN:
339 case CT_ATTACKER:
340 case CT_DEFENDER:
341 build_new.kind = VUT_UTYPE;
342 build_new.value.utype = city_data->choice.value.utype;
343 break;
344 case CT_BUILDING:
346 build_new.value.building = city_data->choice.value.building;
347 break;
348 case CT_NONE:
349 build_new.kind = VUT_NONE;
350 break;
351 case CT_LAST:
353 break;
354 };
355
358 }
359}
360
361/**********************************************************************/
364static void try_to_sell_stuff(struct player *pplayer, struct city *pcity)
365{
366 improvement_iterate(pimprove) {
367 if (can_city_sell_building(pcity, pimprove)
368 && !building_has_effect(pimprove, EFT_DEFEND_BONUS)) {
369 /* Selling walls to buy defenders is counterproductive -- Syela */
370 really_handle_city_sell(pplayer, pcity, pimprove);
371 break;
372 }
374}
375
376/**********************************************************************/
380static void increase_maxbuycost(struct player *pplayer, int new_value)
381{
383}
384
385/**********************************************************************/
390static void dai_upgrade_units(struct city *pcity, int limit, bool military)
391{
393 struct player *pplayer = city_owner(pcity);
394 int expenses;
395
396 {
397 /* Find upgrade unit actions */
398 int i = 0;
401 }
402
403 dai_calc_data(pplayer, nullptr, &expenses, nullptr);
404
406 if (pcity->owner == punit->owner) {
407 /* Only upgrade units you own, not allied ones */
408
409 const struct unit_type *old_type = unit_type_get(punit);
410 const struct unit_type *punittype
411 = can_upgrade_unittype(pplayer, old_type);
412
413 if (military && !IS_ATTACKER(old_type)) {
414 /* Only upgrade military units this round */
415 continue;
416 } else if (!military && IS_ATTACKER(old_type)) {
417 /* Only civilians or transports this round */
418 continue;
419 }
420
421 if (punittype == nullptr) {
422 continue;
423 }
424
426 if (action_ever_possible(act_id)) {
427 const struct action *paction = action_by_number(act_id);
428 int cost = unit_upgrade_price(pplayer, old_type, punittype);
429 int real_limit = limit;
430
431 /* Sinking Triremes are DANGEROUS!!
432 * We'll do anything to upgrade 'em. */
433 /* FIXME: This assumes rules to be quite close to civ/2.
434 * Of the supplied rulesets those are the only ones with
435 * UTYF_COAST unit, but... */
438 }
439 if (pplayer->economic.gold - cost > real_limit) {
440 CITY_LOG(LOG_BUY, pcity, "Upgraded %s to %s for %d (%s)",
443 cost,
444 military ? "military" : "civilian");
446 pcity->id, 0, "",
447 paction->id);
448 } else {
449 increase_maxbuycost(pplayer, cost);
450 }
451 }
453 }
455}
456
457/**********************************************************************/
463static void unit_do_disband_trad(struct player *owner, struct unit *punit,
464 const enum action_requester requester)
465{
466 const int punit_id_stored = punit->id;
467 const struct civ_map *nmap = &(wld.map);
468
470
471 /* Help Wonder gives 100% of the shields used to produce the unit to the
472 * city where it is located. */
474 struct city *tgt_city;
475
476 /* Only a city at the same tile as the unit can benefit. */
478
479 if (tgt_city
481 punit, tgt_city)) {
483 0, nullptr, ACTION_HELP_WONDER, requester)) {
484 /* No shields wasted. The unit did Help Wonder. */
485 return;
486 }
487
489 /* The unit is gone. Maybe it was killed in Lua? */
490 return;
491 }
492 }
493 }
494
495 /* Disbanding a unit inside a city gives it 50% of the shields used to
496 * produce the unit. */
498 struct city *tgt_city;
499
500 /* Only a city at the same tile as the unit can benefit. */
502
503 if (tgt_city
505 punit, tgt_city)) {
507 0, nullptr, ACTION_DISBAND_UNIT_RECOVER,
508 requester)) {
509 /* The unit did Disband Unit Recover. 50% of the shields wasted. */
510 return;
511 }
512
514 /* The unit is gone. Maybe it was killed in Lua? */
515 return;
516 }
517 }
518 }
519
520 /* Try to disband even if all shields will be wasted. */
524 0, nullptr, ACTION_DISBAND_UNIT, requester)) {
525 /* All shields wasted. The unit did Disband Unit. */
526 return;
527 }
528 }
529 }
530}
531
532/**********************************************************************/
535static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
536{
537 struct adv_choice bestchoice;
538 int cached_limit = dai_gold_reserve(pplayer);
539 int expenses;
540 bool war_footing = dai_on_war_footing(ait, pplayer);
541
542 /* Disband explorers that are at home but don't serve a purpose.
543 * FIXME: This is a hack, and should be removed once we
544 * learn how to ferry explorers to new land. */
545 city_list_iterate(pplayer->cities, pcity) {
546 struct tile *ptile = pcity->tile;
549 && pcity->id == punit->homecity
550 && def_ai_city_data(pcity, ait)->urgency == 0) {
551 CITY_LOG(LOG_BUY, pcity, "disbanding %s to increase production",
554 }
557
558 dai_calc_data(pplayer, nullptr, &expenses, nullptr);
559
560 do {
561 bool expensive; /* Don't buy when it costs x2 unless we must */
562 int buycost;
563 int limit = cached_limit; /* cached_limit is our gold reserve */
564 struct city *pcity = nullptr;
565 struct ai_city *city_data;
566
567 /* Find highest wanted item on the buy list */
569 city_list_iterate(pplayer->cities, acity) {
571
572 if (acity_data->choice.want
573 > bestchoice.want && ai_fuzzy(pplayer, TRUE)) {
575 pcity = acity;
576 }
578
579 /* We found nothing, so we're done */
580 if (pcity == nullptr) {
581 break;
582 }
583
585
586 /* Not dealing with this city a second time */
587 city_data->choice.want = -1;
588
590
591 /* Try upgrade units at danger location (high want is usually danger) */
592 if (city_data->urgency > 1) {
593 if (bestchoice.type == CT_BUILDING
594 && is_wonder(bestchoice.value.building)) {
595 CITY_LOG(LOG_BUY, pcity, "Wonder being built in dangerous position!");
596 } else {
597 /* If we have urgent want, spend more */
598 int upgrade_limit = limit;
599
600 if (city_data->urgency > 1) {
602 }
603 /* Upgrade only military units now */
605 }
606 }
607
608 if (pcity->anarchy != 0 && bestchoice.type != CT_BUILDING) {
609 continue; /* Nothing we can do */
610 }
611
612 /* Cost to complete production */
614
615 if (buycost <= 0) {
616 continue; /* Already completed */
617 }
618
620 && utype_is_cityfounder(bestchoice.value.utype)) {
622 && bestchoice.value.utype->pop_cost > 0
623 && city_size_get(pcity) <= bestchoice.value.utype->pop_cost) {
624 /* Don't buy settlers in cities that cannot afford
625 * the population cost. */
626 /* This used to check also if city is about to grow to required size
627 * next turn and allow buying of settlers in that case, but current
628 * order of end/start turn activities is such that settler building
629 * fails already before city grows. */
630 continue;
631 } else if (city_list_size(pplayer->cities) > 6) {
632 /* Don't waste precious money buying settlers late game
633 * since this raises taxes, and we want science. Adjust this
634 * again when our tax algorithm is smarter. */
635 continue;
636 } else if (war_footing) {
637 continue;
638 }
639 } else {
640 /* We are not a settler. Therefore we increase the cash need we
641 * balance our buy desire with to keep cash at hand for emergencies
642 * and for upgrades */
643 limit *= 2;
644 }
645
646 /* It costs x2 to buy something with no shields contributed */
647 expensive = (pcity->shield_stock == 0)
648 || (pplayer->economic.gold - buycost < limit);
649
650 if (bestchoice.type == CT_ATTACKER
651 && buycost
653 bestchoice.value.utype) * 2
654 && !war_footing) {
655 /* Too expensive for an offensive unit */
656 continue;
657 }
658
659 /* FIXME: Here Syela wanted some code to check if
660 * pcity was doomed, and we should therefore attempt
661 * to sell everything in it of non-military value */
662
663 if (pplayer->economic.gold - expenses >= buycost
664 && (!expensive
665 || (city_data->grave_danger != 0
666 && assess_defense(ait, pcity) == 0)
667 || (bestchoice.want > 200 && city_data->urgency > 1))) {
668 /* Buy stuff */
670 "Crash buy of %s for %d (want " ADV_WANT_PRINTF ")",
672 buycost,
673 bestchoice.want);
675 } else if (city_data->grave_danger != 0
676 && bestchoice.type == CT_DEFENDER
677 && assess_defense(ait, pcity) == 0) {
678 /* We have no gold but MUST have a defender */
679 CITY_LOG(LOG_BUY, pcity, "must have %s but can't afford it (%d < %d)!",
681 pplayer->economic.gold,
682 buycost);
683 try_to_sell_stuff(pplayer, pcity);
684 if (pplayer->economic.gold - expenses >= buycost) {
685 CITY_LOG(LOG_BUY, pcity, "now we can afford it (sold something)");
687 }
689 }
690 } while (TRUE);
691
692 if (!war_footing) {
693 /* Civilian upgrades now */
694 city_list_iterate(pplayer->cities, pcity) {
697 }
698
699 log_base(LOG_BUY, "%s wants to keep %d in reserve (tax factor %d)",
700 player_name(pplayer), cached_limit,
701 pplayer->ai_common.maxbuycost);
702}
703
704/**********************************************************************/
707static int unit_food_upkeep(struct unit *punit)
708{
709 int upkeep = unit_upkeep_cost(punit, O_FOOD);
710
711 if (punit->id != 0 && punit->homecity == 0) {
712 upkeep = 0; /* Thanks, Peter */
713 }
714
715 return upkeep;
716}
717
718/**********************************************************************/
726static int unit_foodbox_cost(struct unit *punit)
727{
728 int pop_cost = unit_type_get(punit)->pop_cost;
729
730 if (pop_cost <= 0) {
731 return 0;
732 }
733
734 if (punit->id == 0) {
735 /* It is a virtual unit, so must start in a city... */
736 struct city *pcity = tile_city(unit_tile(punit));
737 int size = city_size_get(pcity);
738 int cost = 0;
739 int i;
740
741 /* The default is to lose 100%. The granary bonus reduces this. */
743
744 foodloss_pct = CLIP(0, foodloss_pct, 100);
745 fc_assert_ret_val(pcity != nullptr, -1);
746 fc_assert(size >= pop_cost);
747
748 for (i = pop_cost; i > 0 ; i--) {
750 }
751 cost = cost * foodloss_pct / 100;
752
753 return cost;
754 }
755
756 return 30;
757}
758
759/**********************************************************************/
766 struct city *pcity)
767{
768 struct unit *virtualunit;
769 int want;
771 struct extra_type *best_target;
772 struct tile *best_tile = nullptr; /* May be accessed by log_*() calls. */
773 struct tile *pcenter = city_tile(pcity);
774 struct player *pplayer = city_owner(pcity);
775 struct adv_data *adv = adv_data_get(pplayer, nullptr);
776 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
777 struct unit_type *utype;
779 struct ai_city *city_data = def_ai_city_data(pcity, ait);
780 struct dai_private_data *private = (struct dai_private_data *)ait->private;
781 const struct civ_map *nmap = &(wld.map);
782
783 if (!private->contemplace_workers) {
784 /* AI type uses custom method to set worker want and type. */
785 return;
786 }
787
788 city_data->worker_want = 0; /* Make sure old want does not stay if
789 * we don't want now */
790
792
793 if (utype == nullptr) {
794 log_debug("No UTYF_WORKERS role unit available");
795 return;
796 }
797
798 /* Create a localized "virtual" unit to do operations with. */
799 virtualunit = unit_virtual_create(pplayer, pcity, utype, 0);
800 /* Advisors data space not allocated as it's not needed in the
801 lifetime of the virtualunit. */
805 &best_tile,
806 nullptr, nullptr);
807 if (unit_type_get(virtualunit)->pop_cost >= city_size_get(pcity)) {
808 /* We don't like disbanding the city as a side effect */
810
811 return;
812 }
813 /* We consider unit_food_upkeep with only half FOOD_WEIGHTING to
814 * balance the fact that unit can improve many tiles during its
815 * lifetime, and want is calculated for just one of them.
816 * Having full FOOD_WEIGHT here would mean that tile improvement of
817 * +1 food would give just zero want for settler. Other weights
818 * are lower, so +1 shield - unit food upkeep would be negative. */
819 want = (want - unit_food_upkeep(virtualunit) * FOOD_WEIGHTING / 2) * 100
822
823 /* Massage our desire based on available statistics to prevent
824 * overflooding with worker type units if they come cheap in
825 * the ruleset */
826 if (place >= 0) {
827 want /= MAX(1, ai->stats.workers[place] / (adv->stats.cities[place] + 1));
828 want -= ai->stats.workers[place];
829 } else {
830 want /= MAX(1, ai->stats.ocean_workers[-place]
831 / (adv->stats.ocean_cities[-place] + 1));
832 want -= ai->stats.ocean_workers[-place];
833 }
834 want = MAX(want, 0);
835
836 if (place >= 0) {
837 CITY_LOG(LOG_DEBUG, pcity, "wants %s with want %d to do %s at (%d,%d), "
838 "we have %d workers and %d cities on the continent",
839 utype_rule_name(utype),
840 want,
843 ai->stats.workers[place],
844 adv->stats.cities[place]);
845 } else {
846 CITY_LOG(LOG_DEBUG, pcity, "wants %s with want %d to do %s at (%d,%d), "
847 "we have %d workers and %d cities on the ocean",
848 utype_rule_name(utype),
849 want,
853 adv->stats.ocean_cities[-place]);
854 }
855
856 fc_assert(want >= 0);
857
858 city_data->worker_want = want;
859 city_data->worker_type
861 place >= 0 ? TC_LAND : TC_OCEAN);
862}
863
864/**********************************************************************/
870void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
871{
872 pplayer->ai_common.maxbuycost = 0;
873
875 city_list_iterate(pplayer->cities, pcity) {
877 || city_granary_size(city_size_get(pcity)) == pcity->food_stock) {
878 /* Having a full granary isn't an emergency, but we want to rearrange */
879 auto_arrange_workers(pcity); /* This usually helps */
880 }
881 if (CITY_EMERGENCY(pcity)) {
882 /* Fix critical shortages or unhappiness */
883 resolve_city_emergency(ait, pplayer, pcity);
884 }
886 sync_cities();
888 if (pplayer->economic.tax >= 30 /* Otherwise expect it to increase tax */
889 && player_get_expected_income(pplayer) < -(pplayer->economic.gold)) {
890 int count = city_list_size(pplayer->cities);
891 struct city *sellers[count + 1];
892 int i;
893
894 /* Randomized order */
895 count = 0;
896 city_list_iterate(pplayer->cities, pcity) {
897 sellers[count++] = pcity;
899
900 for (i = 0; i < count; i++) {
901 int replace = fc_rand(count);
902 struct city *tmp;
903
904 tmp = sellers[i];
905 sellers[i] = sellers[replace];
906 sellers[replace] = tmp;
907 }
908
909 i = 0;
910 while (player_get_expected_income(pplayer) < -(pplayer->economic.gold)
911 && i < count) {
913 }
914 }
916
918 building_advisor(pplayer);
920
921 /* Initialize the infrastructure cache, which is used shortly. */
923 city_list_iterate(pplayer->cities, pcity) {
924 struct ai_city *city_data = def_ai_city_data(pcity, ait);
925 struct adv_choice *choice;
926
927 if (city_data->choice.want <= 0) {
928 /* Note that this function mungs the seamap, but we don't care */
930 choice = military_advisor_choose_build(ait, &(wld.map), pplayer, pcity,
931 nullptr);
932 adv_choice_copy(&(city_data->choice), choice);
933 adv_free_choice(choice);
935 }
936 if (dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0) {
937 city_data->worker_want = 0;
938 city_data->founder_want = 0;
939 city_data->founder_turn = game.info.turn; /* Do not consider zero we set
940 * here a valid value,
941 * if real want is needed.
942 * Recalculate immediately
943 * in such situation. */
944 continue; /* Go, soldiers! */
945 }
946 /* Will record its findings in pcity->worker_want */
950
952 if (city_data->founder_turn <= game.info.turn) {
953 /* Will record its findings in pcity->founder_want */
955 /* Avoid recalculating all the time.. */
956 /* This means AI is not very opportunistic if there happens to open up
957 * spot for a new city. */
958 city_data->founder_turn
960 } else if (pcity->server.debug) {
961 /* Recalculate every turn */
963 }
967 /* Reset auto settler state for the next run. */
968 dai_auto_settler_reset(ait, pplayer);
969
970 city_list_iterate(pplayer->cities, pcity) {
971 dai_city_choose_build(ait, pplayer, pcity);
972
973 /* Initialize for next turn */
974 def_ai_city_data(pcity, ait)->choice.want = -1;
976
977 dai_spend_gold(ait, pplayer);
978}
979
980/**********************************************************************/
987static bool building_crucial(const struct player *plr,
988 struct impr_type *pimprove,
989 const struct city *pcity)
990{
991#if 0 /* This check will become more complicated now. */
992 if (ai_wants_no_science(plr)
994 return FALSE;
995 }
996#endif /* 0 */
997
999 /* selling city walls is really, really dumb -- Syela */
1000 || is_improvement_productive(pcity, pimprove)) {
1001 return TRUE;
1002 }
1003
1004 return FALSE;
1005}
1006
1007/**********************************************************************/
1011 bool redundant_only)
1012{
1013 struct player *pplayer = city_owner(pcity);
1014
1015 city_built_iterate(pcity, pimprove) {
1016 if (can_city_sell_building(pcity, pimprove)
1017 && !building_crucial(pplayer, pimprove, pcity)
1018 && (!redundant_only || is_improvement_redundant(pcity, pimprove))) {
1019 int gain = impr_sell_gold(pimprove);
1020
1022 PL_("%s is selling %s for %d.",
1023 "%s is selling %s for %d.", gain),
1026 gain);
1027 do_sell_building(pplayer, pcity, pimprove, "sold");
1028
1029 return; /* Max 1 building each turn */
1030 }
1032}
1033
1034/**********************************************************************/
1053static void resolve_city_emergency(struct ai_type *ait,
1054 struct player *pplayer,
1055 struct city *pcity)
1056{
1057 struct tile *pcenter = city_tile(pcity);
1058 const struct civ_map *nmap = &(wld.map);
1059
1061 "Emergency in %s (%s, angry%d, unhap%d food%d, prod%d)",
1063 city_unhappy(pcity) ? "unhappy" : "content",
1066 pcity->surplus[O_FOOD],
1067 pcity->surplus[O_SHIELD]);
1068
1070 struct city *acity = tile_worked(atile);
1071
1072 if (acity && acity != pcity && city_owner(acity) == city_owner(pcity)) {
1073 log_base(LOG_EMERGENCY, "%s taking over %s square in (%d, %d)",
1075
1076#ifndef FREECIV_NDEBUG
1077 int ax, ay;
1078#endif
1080 continue);
1081
1082 if (is_free_worked(acity, atile)) {
1083 /* Can't remove a worker here. */
1084 continue;
1085 }
1086
1088 acity->specialists[DEFAULT_SPECIALIST]++;
1090 }
1092
1094
1095 if (!CITY_EMERGENCY(pcity)) {
1096 log_base(LOG_EMERGENCY, "Emergency in %s resolved", city_name_get(pcity));
1097 goto cleanup;
1098 }
1099
1100 unit_list_iterate_safe(pcity->units_supported, punit) {
1101 if (city_unhappy(pcity)
1102 && (utype_happy_cost(unit_type_get(punit), pplayer) > 0
1104 && def_ai_unit_data(punit, ait)->passenger == 0) {
1105 UNIT_LOG(LOG_EMERGENCY, punit, "is causing unrest, disbanded");
1106 /* TODO: If Help Wonder stops blocking Disband Unit there may be
1107 * cases where Disband Unit should be selected. Example: Field unit
1108 * in allied city that is building a wonder that makes the ally win
1109 * without sharing the victory. */
1110 /* TODO: Should the unit try to find legal targets at adjacent tiles?
1111 * Should it consider other self eliminating actions than the
1112 * components of the traditional disband? */
1115 }
1117
1118 if (CITY_EMERGENCY(pcity)) {
1119 log_base(LOG_EMERGENCY, "Emergency in %s remains unresolved",
1121 } else {
1123 "Emergency in %s resolved by disbanding unit(s)",
1125 }
1126
1127 cleanup:
1129 sync_cities();
1130}
1131
1132/**********************************************************************/
1135void dai_city_alloc(struct ai_type *ait, struct city *pcity)
1136{
1137 struct ai_city *city_data = fc_calloc(1, sizeof(struct ai_city));
1138
1139 city_data->building_wait = BUILDING_WAIT_MINIMUM;
1140 adv_init_choice(&(city_data->choice));
1141
1143}
1144
1145/**********************************************************************/
1148void dai_city_free(struct ai_type *ait, struct city *pcity)
1149{
1150 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1151
1152 if (city_data != nullptr) {
1153 adv_deinit_choice(&(city_data->choice));
1154 city_set_ai_data(pcity, ait, nullptr);
1156 }
1157}
1158
1159/**********************************************************************/
1162void dai_city_save(struct ai_type *ait, const char *aitstr,
1163 struct section_file *file, const struct city *pcity,
1164 const char *citystr)
1165{
1166 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1167
1168 /* FIXME: Remove this when the urgency is properly recalculated. */
1169 secfile_insert_int(file, city_data->urgency, "%s.%s.urgency",
1170 citystr, aitstr);
1171
1172 /* Avoid fc_rand() recalculations on subsequent reload. */
1173 secfile_insert_int(file, city_data->building_turn, "%s.%s.building_turn",
1174 citystr, aitstr);
1175 secfile_insert_int(file, city_data->building_wait, "%s.%s.building_wait",
1176 citystr, aitstr);
1177
1178 /* Avoid fc_rand() and expensive recalculations on subsequent reload. */
1179 secfile_insert_int(file, city_data->founder_turn, "%s.%s.founder_turn",
1180 citystr, aitstr);
1181 secfile_insert_int(file, city_data->founder_want, "%s.%s.founder_want",
1182 citystr, aitstr);
1183 secfile_insert_bool(file, city_data->founder_boat, "%s.%s.founder_boat",
1184 citystr, aitstr);
1185}
1186
1187/**********************************************************************/
1190void dai_city_load(struct ai_type *ait, const char *aitstr,
1191 const struct section_file *file,
1192 struct city *pcity, const char *citystr)
1193{
1194 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1195
1196 /* FIXME: Remove this when the urgency is properly recalculated. */
1197 city_data->urgency
1198 = secfile_lookup_int_default(file, 0, "%s.%s.urgency", citystr, aitstr);
1199
1200 /* Avoid fc_rand() recalculations on subsequent reload. */
1201 city_data->building_turn
1202 = secfile_lookup_int_default(file, 0, "%s.%s.building_turn", citystr,
1203 aitstr);
1204 city_data->building_wait
1206 "%s.%s.building_wait", citystr, aitstr);
1207
1208 /* Avoid fc_rand() and expensive recalculations on subsequent reload. */
1209 city_data->founder_turn
1210 = secfile_lookup_int_default(file, 0, "%s.%s.founder_turn", citystr,
1211 aitstr);
1212 city_data->founder_want
1213 = secfile_lookup_int_default(file, 0, "%s.%s.founder_want", citystr,
1214 aitstr);
1215 city_data->founder_boat
1216 = secfile_lookup_bool_default(file, (city_data->founder_want < 0),
1217 "%s.%s.founder_boat", citystr, aitstr);
1218}
1219
1220/**********************************************************************/
1233 const struct city *pcity)
1234{
1235 struct action *paction = action_by_number(act_id);
1236
1238 "Action %d don't exist.", act_id);
1239
1241 /* Not city targeted in this ruleset. */
1242 return 0;
1243 }
1244
1245 switch (paction->result) {
1247 /* Copied from the evaluation of the No_Incite effect */
1248 return MAX((game.server.diplchance * 2
1251 - game.server.incite_unit_factor * 5, 0);
1252
1253 /* Really bad for the city owner. */
1254 case ACTRES_SPY_NUKE:
1256 /* The ai will never destroy their own city to keep it out of enemy
1257 * hands. If it starts supporting it this value should change. */
1259 return 20;
1260
1261 /* Bad for the city owner. */
1262 case ACTRES_SPY_POISON:
1270 case ACTRES_NUKE:
1271 /* TODO: Individual and well balanced values */
1272 return 10;
1273
1274 /* Good for an enemy */
1277 case ACTRES_STEAL_MAPS:
1278 /* TODO: Individual and well balanced values */
1279 return 8;
1280
1281 /* Could be worse */
1284 case ACTRES_MARKETPLACE:
1285 case ACTRES_SPY_ESCAPE:
1286 /* TODO: Individual and well balanced values */
1287 return 1;
1288
1289 /* Good for the city owner in most cases. */
1290 case ACTRES_TRADE_ROUTE:
1291 case ACTRES_HELP_WONDER:
1292 case ACTRES_JOIN_CITY:
1294 case ACTRES_HOME_CITY:
1296 case ACTRES_AIRLIFT:
1297 /* TODO: Individual and well balanced values */
1298 return -1;
1299
1300 /* Ruleset defined actions. We have no idea what they do. */
1301 case ACTRES_NONE:
1302 return 0;
1303
1304 /* Shouldn't happen. */
1306 /* Against the tile so potential city effects are overlooked for now. */
1310 case ACTRES_SPY_ATTACK:
1311 case ACTRES_EXPEL_UNIT:
1314 case ACTRES_BOMBARD:
1315 case ACTRES_FOUND_CITY:
1316 case ACTRES_NUKE_UNITS:
1317 case ACTRES_PARADROP:
1318 case ACTRES_ATTACK:
1319 case ACTRES_WIPE_UNITS:
1321 case ACTRES_HEAL_UNIT:
1323 case ACTRES_CULTIVATE:
1324 case ACTRES_PLANT:
1325 case ACTRES_PILLAGE:
1326 case ACTRES_CLEAN:
1327 case ACTRES_FORTIFY:
1328 case ACTRES_ROAD:
1329 case ACTRES_CONVERT:
1330 case ACTRES_BASE:
1331 case ACTRES_MINE:
1332 case ACTRES_IRRIGATE:
1340 case ACTRES_HUT_ENTER:
1342 case ACTRES_UNIT_MOVE:
1343 case ACTRES_TELEPORT:
1345 case ACTRES_HOMELESS:
1348 "Action not aimed at cities");
1349 break;
1350 }
1351
1352 /* Wrong action. Ignore it. */
1353 return 0;
1354}
1355
1356/**********************************************************************/
1365static bool adjust_wants_for_reqs(struct ai_type *ait,
1366 struct player *pplayer,
1367 struct city *pcity,
1368 const struct impr_type *pimprove,
1369 const adv_want v)
1370{
1371 bool all_met = TRUE;
1372 int n_needed_techs = 0;
1373 int n_needed_improvements = 0;
1376
1377 const struct req_context context = {
1378 .player = pplayer,
1379 .city = pcity,
1380 .tile = city_tile(pcity),
1381 .building = pimprove,
1382 };
1383
1386
1387 requirement_vector_iterate(&pimprove->reqs, preq) {
1388 const bool active = is_req_active(&context, nullptr, preq, RPT_POSSIBLE);
1389
1390 if (VUT_ADVANCE == preq->source.kind && preq->present && !active) {
1391 /* Found a missing technology requirement for this improvement. */
1392 tech_vector_append(&needed_techs, preq->source.value.advance);
1393 } else if (VUT_IMPROVEMENT == preq->source.kind && preq->present
1394 && !active) {
1395 /* Found a missing improvement requirement for this improvement.
1396 * For example, in the default ruleset a city must have a Library
1397 * before it can have a University. */
1398 impr_vector_append(&needed_improvements, preq->source.value.building);
1399 }
1400 all_met = all_met && active;
1402
1403 /* If v is negative, the improvement is not worth building,
1404 * but there is no need to punish research of the technologies
1405 * that would make it available.
1406 */
1408 if (0 < v && 0 < n_needed_techs) {
1409 /* Tell AI module how much we want this improvement and what techs are
1410 * required to get it. */
1411 const adv_want dv = v / n_needed_techs;
1412
1413 want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1414 &needed_techs, dv);
1415 }
1416
1417 /* If v is negative, the improvement is not worth building,
1418 * but there is no need to punish building the improvements
1419 * that would make it available.
1420 */
1422 if (0 < v && 0 < n_needed_improvements) {
1423 /* Because we want this improvement,
1424 * we want the improvements that will make it possible */
1425 const adv_want dv = v / (n_needed_improvements * 4); /* WAG */
1426 int i;
1427
1428 for (i = 0; i < n_needed_improvements; i++) {
1429 const struct impr_type *needed_impr
1431 /* TODO: increase the want for the needed_impr,
1432 * if we can build it now */
1433 /* Recurse */
1434 (void) adjust_wants_for_reqs(ait, pplayer, pcity, needed_impr, dv);
1435 }
1436 }
1437
1438 /* TODO: use a similar method to increase wants for governments
1439 * that will make this improvement possible? */
1440
1443
1444 return all_met;
1445}
1446
1447/**********************************************************************/
1452adv_want dai_city_want(struct player *pplayer, struct city *acity,
1453 struct adv_data *adv, struct impr_type *pimprove)
1454{
1455 adv_want want = 0;
1456 int prod[O_LAST], bonus[O_LAST], waste[O_LAST];
1457 const struct civ_map *nmap = &(wld.map);
1458
1459 memset(prod, 0, O_LAST * sizeof(*prod));
1460 if (pimprove != nullptr
1461 && adv->impr_calc[improvement_index(pimprove)]
1463 struct tile *acenter = city_tile(acity);
1465
1466 /* The below calculation mostly duplicates get_worked_tile_output().
1467 * We do this only for buildings that we know may change tile
1468 * outputs. */
1470 if (tile_worked(ptile) == acity) {
1472 prod[o] += city_tile_output(acity, ptile, celebrating, o);
1474 }
1476
1478 } else {
1479 fc_assert(sizeof(*prod) == sizeof(*acity->citizen_base));
1480 memcpy(prod, acity->citizen_base, O_LAST * sizeof(*prod));
1481 }
1482
1484 prod[O_TRADE] += proute->value;
1489 waste[o] = city_waste(acity, o, prod[o] * bonus[o] / 100, nullptr);
1491 add_tax_income(pplayer,
1492 prod[O_TRADE] * bonus[O_TRADE] / 100 - waste[O_TRADE],
1493 prod);
1495 prod[o] = prod[o] * bonus[o] / 100 - waste[o];
1497
1501 /* Unit upkeep isn't handled here. Unless we do a full city_refresh it
1502 * won't be changed anyway. */
1503
1504 want += prod[O_FOOD] * adv->food_priority;
1505 if (prod[O_SHIELD] != 0) {
1506 want += prod[O_SHIELD] * adv->shield_priority;
1507 want -= city_pollution(acity, prod[O_SHIELD]) * adv->pollution_priority;
1508 }
1509 want += prod[O_LUXURY] * adv->luxury_priority;
1510 want += prod[O_SCIENCE] * adv->science_priority;
1511 if (pplayer->economic.tax > 50) {
1512 /* Increased tax rate indicates that we've had gold shortage which
1513 * we are trying to fill with taxes. Consider gold more critical
1514 * than usually.
1515 * Smallest tax rate we can have here is
1516 * 60% -> factor (60 - 40) / 14.0 = 1.43 */
1517 want += prod[O_GOLD] * adv->gold_priority
1518 * (pplayer->economic.tax - 40) / 14.0;
1519 } else {
1520 want += prod[O_GOLD] * adv->gold_priority;
1521 }
1522
1523 return want;
1524}
1525
1526/**********************************************************************/
1530static adv_want base_want(struct ai_type *ait, struct player *pplayer,
1531 struct city *pcity, struct impr_type *pimprove)
1532{
1533 struct adv_data *adv = adv_data_get(pplayer, nullptr);
1534 adv_want final_want = 0;
1537
1538 if (adv->impr_calc[improvement_index(pimprove)] == ADV_IMPR_ESTIMATE) {
1539 return 0; /* Nothing to calculate here. */
1540 }
1541
1543 || (is_small_wonder(pimprove)
1544 && city_from_small_wonder(pplayer, pimprove) != nullptr)) {
1545 return 0;
1546 }
1547
1548 if (is_wonder(pimprove)) {
1549 if (is_great_wonder(pimprove)) {
1552 }
1553 wonder_city_id = pplayer->wonders[improvement_index(pimprove)];
1554 }
1555 /* Add the improvement */
1556 city_add_improvement(pcity, pimprove);
1557
1558 /* Stir, then compare notes */
1560 adv->impr_range[improvement_index(pimprove)], acity) {
1561 final_want += dai_city_want(pplayer, acity, adv, pimprove)
1562 - def_ai_city_data(acity, ait)->worth;
1564
1565 /* Restore */
1566 city_remove_improvement(pcity, pimprove);
1567 if (is_wonder(pimprove)) {
1568 if (is_great_wonder(pimprove)) {
1571 }
1572
1573 pplayer->wonders[improvement_index(pimprove)] = wonder_city_id;
1574 }
1575
1576 return final_want;
1577}
1578
1579/**********************************************************************/
1609 struct player *pplayer,
1610 struct city *pcity,
1611 struct impr_type *pimprove,
1612 const bool already)
1613{
1614 adv_want v = 0;
1616 int nplayers = normal_player_count();
1617 struct adv_data *ai = adv_data_get(pplayer, nullptr);
1618 bool capital = is_capital(pcity);
1619 bool can_build = TRUE;
1620 struct universal source = {
1622 .value = {.building = pimprove}
1623 };
1624 const bool is_convert = is_convert_improvement(pimprove);
1625 int turns = 9999;
1627
1628 /* FIXME: Do we really need the effects check to be made *without*
1629 * passing the city tile? */
1630 const struct req_context effect_ctxt = {
1631 .player = pplayer,
1632 .city = pcity,
1633 .building = pimprove,
1634 };
1635
1636 /* Do NOT pass building here, as the action might be about
1637 * targeting some completely different building, AND
1638 * the check to see if the action is possible before
1639 * the building is there is also ignoring the buildings.
1640 * We don't want those two results to differ for
1641 * an unrelated reason to what we are evaluating. */
1642 const struct req_context actenabler_ctxt = {
1643 .player = pplayer,
1644 .city = pcity,
1645 .tile = city_tile(pcity),
1646 };
1647
1648 /* Remove team members from the equation */
1650 if (aplayer->team
1651 && aplayer->team == pplayer->team
1652 && aplayer != pplayer) {
1653 nplayers--;
1654 }
1656
1657 if (is_convert) {
1658 /* Since coinage-like improvements contains some entirely spurious
1659 * ruleset values, we need to hard-code a sensible want.
1660 * We must otherwise handle IG_CONVERT improvements like the others,
1661 * so the AI will research techs that make it available,
1662 * for rulesets that do not provide it from the start.
1663 */
1664 if (improvement_has_flag(pimprove, IF_GOLD)) {
1665 v += TRADE_WEIGHTING / 10;
1666 } else if (improvement_has_flag(pimprove, IF_INFRA)) {
1667 v += INFRA_WEIGHTING / 10;
1668 }
1669 /* Without relevant flags, base want remains 0. */
1670 } else {
1671 /* Base want is calculated above using a more direct approach. */
1672 v += base_want(ait, pplayer, pcity, pimprove);
1673 if (v != 0) {
1675 "%s base_want is " ADV_WANT_PRINTF " (range=%d)",
1676 improvement_rule_name(pimprove),
1677 v,
1678 ai->impr_range[improvement_index(pimprove)]);
1679 }
1680 }
1681
1682 if (!is_convert) {
1683 /* Adjust by building cost */
1684 /* FIXME: ought to reduce by upkeep cost and amortise by building cost */
1685 v -= (impr_build_shield_cost(pcity, pimprove)
1686 / (pcity->surplus[O_SHIELD] * 10 + 1));
1687 }
1688
1689 /* Find number of cities per range. */
1691 /* kludge -- Number of *our* cities in these ranges. */
1694
1695 if (place < 0) {
1697 } else {
1699 }
1700
1701 /* All the trade partners and the city being considered. */
1703
1705
1706 /* Invalid building range */
1708 = cities[REQ_RANGE_TILE] = 0;
1709
1711 int potential = (aplayer->server.bulbs_last_turn
1712 + city_list_size(aplayer->cities) + 1);
1713
1714 if (potential > 0) {
1716 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1717 turns = MIN(turns,
1719 advance_number(pobs->source.value.advance))
1720 / (potential + 1));
1721 }
1723 }
1725
1728 bool active = TRUE;
1729 int n_needed_techs = 0;
1731 bool present = TRUE;
1732 bool impossible_to_get = FALSE;
1733
1735
1737 /* Check if all the requirements for the currently evaluated effect
1738 * are met, except for having the building that we are evaluating. */
1740 if (preq->range < range) {
1741 /* More limited range */
1742 range = preq->range;
1743 }
1744 present = preq->present;
1745 continue;
1746 }
1747
1748 if (!is_req_active(&effect_ctxt, nullptr, preq, RPT_POSSIBLE)) {
1749 active = FALSE;
1750 if (VUT_ADVANCE == preq->source.kind && preq->present) {
1751 /* This missing requirement is a missing tech requirement.
1752 * This will be for some additional effect
1753 * (For example, in the default ruleset, Mysticism increases
1754 * the effect of Temples). */
1755 tech_vector_append(&needed_techs, preq->source.value.advance);
1756 } else if (!dai_can_requirement_be_met_in_city(preq, pplayer, pcity)) {
1758 }
1759 }
1761
1763 if ((active || n_needed_techs) && !impossible_to_get) {
1764 adv_want v1 = dai_effect_value(pplayer, ai, pcity, capital,
1765 turns, peffect, range, nplayers);
1766 /* v1 could be negative (the effect could be undesirable),
1767 * although it is usually positive.
1768 * For example, in the default ruleset, Communism decreases the
1769 * effectiveness of a Cathedral. */
1770
1771 if (!present) {
1772 /* Building removes the effect */
1773 /* Currently v1 is (v + delta). Make it (v - delta) instead */
1774 v1 = -v1;
1775 }
1776
1777 if (active) {
1778 v += v1;
1779 } else if (v1 > 0) {
1780 /* If value of the effect is negative, do not hold it against
1781 * the tech - having the tech won't force one to build the
1782 * building. */
1783
1784 /* We might want the technology that will enable this
1785 * (additional) effect.
1786 * The better the effect, the more we want the technology.
1787 * We are more interested in (additional) effects that enhance
1788 * buildings we already have.
1789 */
1790 const int a = already? 5: 4; /* WAG */
1791 const adv_want dv = v1 * a / (4 * n_needed_techs);
1792
1793 want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1794 &needed_techs, dv);
1795 }
1796 }
1797
1800
1801 /* Can the city be the target of an action? */
1802 action_iterate (act_id) {
1803 bool is_possible;
1804 bool will_be_possible = FALSE;
1805 enum req_range max_range;
1806 int act_neg_util;
1807
1808 /* Is the action relevant? */
1809 if (action_id_get_target_kind(act_id) != ATK_CITY) {
1810 continue;
1811 }
1812
1813 /* No range found yet. Local is the most narrow range. */
1815
1816 /* Is it possible to do the action to the city right now?
1817 *
1818 * (DiplRel requirements are ignored since actor_player is nullptr) */
1819 is_possible = is_action_possible_on_city(act_id, nullptr, pcity);
1820
1821 /* Will it be possible to do the action to the city if the building is
1822 * built? */
1824 enabler) {
1825 bool active = TRUE;
1827
1828 requirement_vector_iterate(&(enabler->target_reqs), preq) {
1830 /* Pretend the building is there */
1831 if (preq->present) {
1832 range = preq->range; /* Assumption: Max one pr vector */
1833 continue;
1834 } else {
1835 active = FALSE;
1836 break;
1837 }
1838 } else if (!is_req_active(&actenabler_ctxt, nullptr,
1839 preq, RPT_POSSIBLE)) {
1840 active = FALSE;
1841 break;
1842 }
1844
1845 if (active) {
1847
1848 /* Store the widest range that enables the action. */
1849 if (max_range < range) {
1850 max_range = range;
1851 }
1852
1853 /* Don't break the iteration even if the action is enabled. There
1854 * could be a wider range in an active action enabler not yet seen.
1855 */
1856 }
1858
1859 /* Will the building significantly change the ability to target
1860 * the city? */
1862 continue;
1863 }
1864
1865 /* How undesirable is it that the city may be a target? */
1867
1868 /* Multiply the desire by number of cities in range.
1869 * Note: This is a simplification. If the action can be done or not
1870 * _may_ be uncanged or changed in the opposite direction in the other
1871 * cities in the range. */
1873
1874 /* Consider the utility of being a potential target.
1875 * Remember: act_util is the negative utility of being a target. */
1876 if (will_be_possible) {
1877 v -= act_neg_util;
1878 } else {
1879 v += act_neg_util;
1880 }
1882
1883 if (already) {
1884 /* Discourage research of the technology that would make this building
1885 * obsolete. The bigger the desire for this building, the more
1886 * we want to discourage the technology. */
1887 dont_want_tech_obsoleting_impr(ait, pplayer, pcity, pimprove, v);
1888 } else {
1889 /* Increase the want for technologies that will enable
1890 * construction of this improvement, if necessary.
1891 */
1892 const bool all_met
1893 = adjust_wants_for_reqs(ait, pplayer, pcity, pimprove, v);
1894
1896 }
1897
1898 if (is_convert && can_build) {
1899 /* Could have a negative want for coinage-like improvements,
1900 * if we have some stock in a building already. */
1901 pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1902 } else if (!already && can_build) {
1903 const struct research *presearch = research_get(pplayer);
1904
1905 /* Convert the base 'want' into a building want
1906 * by applying various adjustments */
1907
1908 /* Would it mean losing shields? */
1909 if ((VUT_UTYPE == pcity->production.kind
1910 || (is_wonder(pcity->production.value.building)
1911 && !is_wonder(pimprove))
1912 || (!is_wonder(pcity->production.value.building)
1913 && is_wonder(pimprove)))
1914 && pcity->turn_last_built != game.info.turn) {
1915 if (has_handicap(pplayer, H_PRODCHGPEN)) {
1916 v -= pcity->shield_stock * SHIELD_WEIGHTING / 4;
1917 } else {
1918 v -= pcity->shield_stock * SHIELD_WEIGHTING / 15;
1919 }
1920 }
1921
1922 /* Reduce want if building gets obsoleted soon */
1924 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1926 advance_number(pobs->source.value.advance));
1927
1928 v -= v / MAX(1, num_tech);
1929 }
1931
1932 /* Are we wonder city? Try to avoid building non-wonders very much. */
1933 if (pcity->id == ai->wonder_city && !is_wonder(pimprove)) {
1934 v /= 5;
1935 }
1936
1937 /* Set */
1938 pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1939 }
1940 /* Else we either have the improvement already,
1941 * or we can not build it (yet) */
1942}
1943
1944/**********************************************************************/
1953static bool should_force_recalc(struct city *pcity)
1954{
1956 || (VUT_IMPROVEMENT == pcity->production.kind
1957 && !is_convert_improvement(pcity->production.value.building)
1959 (pcity, pcity->production.value.building));
1960}
1961
1962/**********************************************************************/
1966void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
1967{
1968 struct adv_data *ai = adv_data_get(pplayer, nullptr);
1969
1970 /* Find current worth of cities and cache this. */
1971 city_list_iterate(pplayer->cities, pcity) {
1972 def_ai_city_data(pcity, ait)->worth = dai_city_want(pplayer, pcity, ai,
1973 nullptr);
1975}
1976
1977/**********************************************************************/
1983void dai_build_adv_adjust(struct ai_type *ait, struct player *pplayer,
1984 struct city *wonder_city)
1985{
1986 /* Clear old building wants.
1987 * Do this separately from the iteration over improvement types
1988 * because each iteration could actually update more than one improvement,
1989 * if improvements have improvements as requirements.
1990 */
1991 city_list_iterate(pplayer->cities, pcity) {
1992 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1993
1994 if (city_data->building_turn <= game.info.turn) {
1995 /* Do a scheduled recalculation this turn */
1996 improvement_iterate(pimprove) {
1997 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1999 } else if (should_force_recalc(pcity)) {
2000 /* Do an emergency recalculation this turn. */
2001 city_data->building_wait = city_data->building_turn
2002 - game.info.turn;
2003 city_data->building_turn = game.info.turn;
2004
2005 improvement_iterate(pimprove) {
2006 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
2008 }
2010
2011 improvement_iterate(pimprove) {
2012 if (can_player_build_improvement_later(pplayer, pimprove)) {
2013 city_list_iterate(pplayer->cities, pcity) {
2014 struct ai_city *city_data = def_ai_city_data(pcity, ait);
2015
2016 if (pcity != wonder_city && is_wonder(pimprove)) {
2017 /* Only wonder city should build wonders! */
2018 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
2019 } else if (city_data->building_turn <= game.info.turn) {
2020 /* Building wants vary relatively slowly, so not worthwhile
2021 * recalculating them every turn.
2022 * We DO want to calculate (tech) wants because of buildings
2023 * we already have. */
2024 const bool already = city_has_building(pcity, pimprove);
2025 int idx = improvement_index(pimprove);
2026
2028 pimprove, already);
2029
2031 && 0 < pcity->server.adv->building_want[idx]));
2032
2033 if (is_great_wonder(pimprove)) {
2034 /* Not only would we get the wonder, but we would also prevent
2035 * opponents from getting it. */
2036 pcity->server.adv->building_want[idx] *= 1.5;
2037
2038 if (pcity->production.kind == VUT_IMPROVEMENT
2039 && is_great_wonder(pcity->production.value.building)) {
2040 /* If we already are building a great wonder, prefer continuing
2041 * to do so over stopping it */
2042 pcity->server.adv->building_want[idx] *= 1.25;
2043 }
2044 }
2045
2046 /* If I am not an expansionist, I want buildings more than units */
2047 if (pcity->server.adv->building_want[idx] > 0) {
2048 pcity->server.adv->building_want[idx]
2049 = pcity->server.adv->building_want[idx]
2052 }
2053 }
2054 /* else wait until a later turn */
2056 } else {
2057 /* An impossible improvement */
2058 city_list_iterate(pplayer->cities, pcity) {
2059 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
2061 }
2063
2064#ifdef FREECIV_DEBUG
2065 /* This logging is relatively expensive, so activate only if necessary */
2066 city_list_iterate(pplayer->cities, pcity) {
2067 improvement_iterate(pimprove) {
2068 if (pcity->server.adv->building_want[improvement_index(pimprove)] != 0) {
2069 CITY_LOG(LOG_DEBUG, pcity, "want to build %s with " ADV_WANT_PRINTF,
2070 improvement_rule_name(pimprove),
2071 pcity->server.adv->building_want[improvement_index(pimprove)]);
2072 }
2075#endif /* FREECIV_DEBUG */
2076
2077 /* Reset recalc counter */
2078 city_list_iterate(pplayer->cities, pcity) {
2079 struct ai_city *city_data = def_ai_city_data(pcity, ait);
2080
2081 if (city_data->building_turn <= game.info.turn) {
2082 /* This will spread recalcs out so that no one turn end is
2083 * much longer than others */
2084 city_data->building_wait
2086 city_data->building_turn
2087 = game.info.turn + city_data->building_wait;
2088 }
2090}
2091
2092/**********************************************************************/
2095void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity,
2096 bool *result)
2097{
2098 if (def_ai_city_data(pcity, ait)->grave_danger > 0) {
2099 *result = FALSE;
2100 } else {
2101 *result = TRUE;
2102 }
2103}
2104
2105/**********************************************************************/
2109static bool dai_cant_help_req(const struct req_context *context,
2110 const struct requirement *req)
2111{
2112 switch (req->source.kind) {
2113 /* Unskilled in channel digging and merchantry */
2114 case VUT_TERRAIN:
2115 case VUT_EXTRA:
2116 case VUT_TILEDEF:
2117 case VUT_GOOD:
2118 case VUT_TERRAINCLASS:
2119 case VUT_TERRFLAG:
2120 case VUT_TERRAINALTER:
2121 case VUT_CITYTILE:
2122 return !is_req_active(context, nullptr, req, RPT_POSSIBLE);
2123 default:
2124 return is_req_preventing(context, nullptr, req, RPT_POSSIBLE) > REQUCH_NO;
2125 }
2126}
2127
2128/**********************************************************************/
2132 const struct impr_type *pimprove)
2133{
2134 const struct req_context city_ctxt = {
2136 .city = pcity,
2137 .tile = city_tile(pcity),
2138 };
2139
2140 /* FIXME: AI may be too stupid to sell obsoleting improvements
2141 * from the city that are _not_ checked here. */
2143 pimprove)) {
2144 return FALSE;
2145 }
2146 /* Check for requirements that aren't met and that are unchanging
2147 * (so they can never be met). */
2148 requirement_vector_iterate(&pimprove->reqs, preq) {
2150 return FALSE;
2151 }
2153
2154 return TRUE;
2155}
2156
2157/**********************************************************************/
2160bool
2162 const struct impr_type *pimprove)
2163{
2164 const struct req_context context = { .player = p };
2165
2166 if (!valid_improvement(pimprove)) {
2167 return FALSE;
2168 }
2169 if (improvement_obsolete(p, pimprove, nullptr)) {
2170 return FALSE;
2171 }
2172 if (is_great_wonder(pimprove) && !great_wonder_is_available(pimprove)) {
2173 /* Can't build wonder if already built */
2174 return FALSE;
2175 }
2176
2177 /* Check for requirements that aren't met and that are unchanging
2178 * (so they can never be met). */
2179 requirement_vector_iterate(&pimprove->reqs, preq) {
2180 if (preq->range >= REQ_RANGE_PLAYER
2182 return FALSE;
2183 }
2185 /* FIXME: should check some "unchanging" reqs here - like if there's
2186 * a nation requirement, we can go ahead and check it now. */
2187
2188 return TRUE;
2189}
2190
2191/**********************************************************************/
2198 enum effect_type effect_type,
2199 const struct unit_type *utype)
2200{
2201 int greatest_value = 0;
2202 const struct impr_type *best_building = nullptr;
2203 const struct req_context context = {
2205 .city = pcity,
2206 .tile = city_tile(pcity),
2207 .unittype = utype,
2208 };
2209
2210 effect_list_iterate(get_effects(effect_type), peffect) {
2211 if (peffect->value > greatest_value) {
2212 const struct impr_type *building = nullptr;
2213 bool wrong_unit = FALSE;
2214
2216 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2217 building = preq->source.value.building;
2218
2220 || !is_improvement(building)) {
2221 building = nullptr;
2222 break;
2223 }
2224 } else if (VUT_IMPR_FLAG == preq->source.kind && preq->present) {
2225 /* TODO: Ruleset cache for buildings with specific flag */
2226 improvement_iterate(impr) {
2227 if (improvement_has_flag(impr, preq->source.value.impr_flag)) {
2229 && is_improvement(impr)) {
2230 if (building == nullptr) {
2231 building = impr;
2232 break;
2233 }
2234 }
2235 }
2237
2238 if (building == nullptr) {
2239 break;
2240 }
2241 } else if (utype != nullptr
2242 && !is_req_active(&context, nullptr, preq, RPT_POSSIBLE)) {
2243 /* Effect requires other kind of unit than what we are
2244 * interested about */
2245 wrong_unit = TRUE;
2246 break;
2247 }
2249 if (!wrong_unit && building != nullptr) {
2250 best_building = building;
2251 greatest_value = peffect->value;
2252 }
2253 }
2255
2256 if (best_building) {
2258 }
2259
2260 return B_LAST;
2261}
bool is_action_possible_on_city(action_id act_id, const struct player *actor_player, const struct city *target_city)
Definition actions.c:5717
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_ever_possible(action_id action)
Definition actions.c:7500
bool is_action_enabled_unit_on_city(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit, const struct city *target_city)
Definition actions.c:3103
bool action_id_exists(const action_id act_id)
Definition actions.c:1089
enum action_target_kind action_get_target_kind(const struct action *paction)
Definition actions.c:1129
bool is_action_enabled_unit_on_self(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit)
Definition actions.c:3493
struct action_enabler_list * action_enablers_for_action(action_id action)
Definition actions.c:1580
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_enabler_list_iterate_end
Definition actions.h:194
#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_enabler_list_iterate(action_enabler_list, aenabler)
Definition actions.h:192
#define action_iterate(_act_)
Definition actions.h:214
#define action_id_get_target_kind(act_id)
Definition actions.h:417
void building_advisor(struct player *pplayer)
#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
void adv_deinit_choice(struct adv_choice *choice)
Definition advchoice.c:46
void adv_init_choice(struct adv_choice *choice)
Definition advchoice.c:31
void adv_free_choice(struct adv_choice *choice)
Definition advchoice.c:71
struct adv_choice * adv_better_choice(struct adv_choice *first, struct adv_choice *second)
Definition advchoice.c:85
bool is_unit_choice_type(enum choice_type type)
Definition advchoice.c:115
const char * adv_choice_rule_name(const struct adv_choice *choice)
Definition advchoice.c:124
#define adv_choice_set_use(_choice, _use)
Definition advchoice.h:85
#define ADV_CHOICE_ASSERT(c)
Definition advchoice.h:100
static void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
Definition advchoice.h:79
@ CT_CIVILIAN
Definition advchoice.h:39
@ CT_LAST
Definition advchoice.h:42
@ CT_DEFENDER
Definition advchoice.h:41
@ CT_ATTACKER
Definition advchoice.h:40
@ CT_NONE
Definition advchoice.h:37
@ CT_BUILDING
Definition advchoice.h:38
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:614
@ ADV_IMPR_CALCULATE_FULL
Definition advdata.h:35
@ ADV_IMPR_ESTIMATE
Definition advdata.h:36
int ai_trait_get_value(enum trait tr, struct player *pplayer)
Definition aitraits.c:68
adv_want worker_evaluate_improvements(const struct civ_map *nmap, struct unit *punit, enum unit_activity *best_act, struct extra_type **best_target, struct tile **best_tile, struct pf_path **ppath, struct workermap *state)
bool base_city_celebrating(const struct city *pcity)
Definition city.c:1692
bool is_free_worked(const struct city *pcity, const struct tile *ptile)
Definition city.c:3699
int city_granary_size(int city_size)
Definition city.c:2187
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Definition city.c:3715
bool city_built_last_turn(const struct city *pcity)
Definition city.c:2334
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown)
Definition city.c:3309
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1294
bool is_capital(const struct city *pcity)
Definition city.c:1634
const char * city_name_get(const struct city *pcity)
Definition city.c:1155
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3467
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1310
void add_tax_income(const struct player *pplayer, int trade, int *output)
Definition city.c:2300
int get_city_tithes_bonus(const struct city *pcity)
Definition city.c:2282
bool city_unhappy(const struct city *pcity)
Definition city.c:1681
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove, const enum req_problem_type prob_type)
Definition city.c:860
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Definition city.c:2267
int city_pollution(const struct city *pcity, int shield_total)
Definition city.c:2876
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3453
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:137
bool city_base_to_city_map(int *city_map_x, int *city_map_y, const struct city *const pcity, const struct tile *map_tile)
Definition city.c:281
void add_specialist_output(const struct city *pcity, int *output)
Definition city.c:2419
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype, const enum req_problem_type prob_type)
Definition city.c:957
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1338
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:565
static citizens city_size_get(const struct city *pcity)
Definition city.h:570
@ CITIZEN_ANGRY
Definition city.h:269
@ CITIZEN_UNHAPPY
Definition city.h:268
#define output_type_iterate(output)
Definition city.h:851
#define city_owner(_pcity_)
Definition city.h:564
#define city_list_iterate_end
Definition city.h:510
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:228
@ FEELING_FINAL
Definition city.h:282
#define city_tile_iterate_end
Definition city.h:236
#define city_built_iterate(_pcity, _p)
Definition city.h:840
#define city_built_iterate_end
Definition city.h:846
#define output_type_iterate_end
Definition city.h:857
void really_handle_city_buy(struct player *pplayer, struct city *pcity)
Definition cityhand.c:294
void really_handle_city_sell(struct player *pplayer, struct city *pcity, struct impr_type *pimprove)
Definition cityhand.c:241
void city_map_update_empty(struct city *pcity, struct tile *ptile)
Definition citytools.c:3280
void city_freeze_workers_queue(struct city *pcity)
Definition citytools.c:160
int city_production_buy_gold_cost(const struct city *pcity)
Definition citytools.c:3651
void sync_cities(void)
Definition citytools.c:3354
void change_build_target(struct player *pplayer, struct city *pcity, struct universal *target, enum event_type event)
Definition citytools.c:3191
void do_sell_building(struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const char *reason)
Definition citytools.c:3010
void city_thaw_workers_queue(void)
Definition citytools.c:197
int city_shrink_granary_savings(const struct city *pcity)
Definition cityturn.c:894
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:367
bool city_refresh(struct city *pcity)
Definition cityturn.c:159
char * incite_cost
Definition comments.c:77
static void try_to_sell_stuff(struct player *pplayer, struct city *pcity)
Definition daicity.c:364
#define AI_BA_RECALC_SPEED
Definition daicity.c:76
#define AI_CITY_RECALC_SPEED
Definition daicity.c:74
static bool adjust_wants_for_reqs(struct ai_type *ait, struct player *pplayer, struct city *pcity, const struct impr_type *pimprove, const adv_want v)
Definition daicity.c:1365
static void unit_do_disband_trad(struct player *owner, struct unit *punit, const enum action_requester requester)
Definition daicity.c:463
static bool dai_cant_help_req(const struct req_context *context, const struct requirement *req)
Definition daicity.c:2109
void want_techs_for_improvement_effect(struct ai_type *ait, struct player *pplayer, const struct city *pcity, const struct impr_type *pimprove, struct tech_vector *needed_techs, adv_want building_want)
Definition daicity.c:160
static void increase_maxbuycost(struct player *pplayer, int new_value)
Definition daicity.c:380
static void dai_barbarian_choose_build(struct player *pplayer, struct city *pcity, struct adv_choice *choice)
Definition daicity.c:204
void dai_city_save(struct ai_type *ait, const char *aitstr, struct section_file *file, const struct city *pcity, const char *citystr)
Definition daicity.c:1162
void dai_city_load(struct ai_type *ait, const char *aitstr, const struct section_file *file, struct city *pcity, const char *citystr)
Definition daicity.c:1190
#define LOG_BUY
Definition daicity.c:68
static void dai_city_sell_noncritical(struct city *pcity, bool redundant_only)
Definition daicity.c:1010
#define city_range_iterate_end
Definition daicity.c:103
static int unit_food_upkeep(struct unit *punit)
Definition daicity.c:707
void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:870
void dai_city_alloc(struct ai_type *ait, struct city *pcity)
Definition daicity.c:1135
#define LOG_EMERGENCY
Definition daicity.c:69
void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity, bool *result)
Definition daicity.c:2095
static bool should_force_recalc(struct city *pcity)
Definition daicity.c:1953
bool dai_can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
Definition daicity.c:2161
static int action_target_neg_util(action_id act_id, const struct city *pcity)
Definition daicity.c:1232
#define city_range_iterate(city_here, list, range, city)
Definition daicity.c:88
static void resolve_city_emergency(struct ai_type *ait, struct player *pplayer, struct city *pcity)
Definition daicity.c:1053
void dont_want_tech_obsoleting_impr(struct ai_type *ait, struct player *pplayer, const struct city *pcity, const struct impr_type *pimprove, adv_want building_want)
Definition daicity.c:181
void dai_build_adv_adjust(struct ai_type *ait, struct player *pplayer, struct city *wonder_city)
Definition daicity.c:1983
static void contemplate_terrain_improvements(struct ai_type *ait, struct city *pcity)
Definition daicity.c:765
static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:535
#define LOG_WANT
Definition daicity.c:70
static void adjust_improvement_wants_by_effects(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const bool already)
Definition daicity.c:1608
void dai_city_free(struct ai_type *ait, struct city *pcity)
Definition daicity.c:1148
static void want_tech_for_improvement_effect(struct ai_type *ait, struct player *pplayer, const struct city *pcity, const struct impr_type *pimprove, const struct advance *tech, adv_want building_want)
Definition daicity.c:130
Impr_type_id dai_find_source_building(struct city *pcity, enum effect_type effect_type, const struct unit_type *utype)
Definition daicity.c:2197
void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:1966
static void dai_upgrade_units(struct city *pcity, int limit, bool military)
Definition daicity.c:390
#define CITY_EMERGENCY(pcity)
Definition daicity.c:108
bool dai_can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Definition daicity.c:2131
adv_want dai_city_want(struct player *pplayer, struct city *acity, struct adv_data *adv, struct impr_type *pimprove)
Definition daicity.c:1452
static adv_want base_want(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove)
Definition daicity.c:1530
static int unit_foodbox_cost(struct unit *punit)
Definition daicity.c:726
static bool building_crucial(const struct player *plr, struct impr_type *pimprove, const struct city *pcity)
Definition daicity.c:987
static void dai_city_choose_build(struct ai_type *ait, struct player *pplayer, struct city *pcity)
Definition daicity.c:256
#define BUILDING_WAIT_MINIMUM
Definition daicity.h:39
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Definition daidata.c:308
bool dai_on_war_footing(struct ai_type *ait, struct player *pplayer)
struct adv_choice * domestic_advisor_choose_build(struct ai_type *ait, struct player *pplayer, struct city *pcity)
bool dai_can_requirement_be_met_in_city(const struct requirement *preq, const struct player *pplayer, const struct city *pcity)
Definition daieffects.c:821
adv_want dai_effect_value(struct player *pplayer, const struct adv_data *adv, const struct city *pcity, const bool capital, int turns, const struct effect *peffect, const int c, const int nplayers)
Definition daieffects.c:138
void dai_calc_data(const struct player *pplayer, int *trade, int *expenses, int *income)
Definition daihand.c:117
#define TECH_LOG(ait, loglevel, pplayer, padvance, msg,...)
Definition dailog.h:36
int assess_defense(struct ai_type *ait, struct city *pcity)
struct adv_choice * military_advisor_choose_build(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct city *pcity, player_unit_list_getter ul_cb)
bool dai_process_defender_want(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct city *pcity, unsigned int danger, struct adv_choice *choice, adv_want extra_want)
int assess_defense_quadratic(struct ai_type *ait, struct city *pcity)
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition daiplayer.h:56
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition daiplayer.h:44
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition daiplayer.h:50
void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
void contemplate_new_city(struct ai_type *ait, struct city *pcity)
int dai_gold_reserve(struct player *pplayer)
Definition daitools.c:1354
#define DAI_WANT_MILITARY_EMERGENCY
Definition daitools.h:32
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Definition daiunit.c:3493
#define IS_ATTACKER(ptype)
Definition daiunit.h:70
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 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 cost
Definition dialogs_g.h:74
bool ai_fuzzy(const struct player *pplayer, bool normal_decision)
Definition difficulty.c:339
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:842
struct effect_list * get_effects(enum effect_type effect_type)
Definition effects.c:143
struct effect_list * get_req_source_effects(const struct universal *psource)
Definition effects.c:153
bool building_has_effect(const struct impr_type *pimprove, enum effect_type effect_type)
Definition effects.c:639
#define effect_list_iterate_end
Definition effects.h:81
#define effect_list_iterate(effect_list, peffect)
Definition effects.h:79
float adv_want
Definition fc_types.h:1050
int Impr_type_id
Definition fc_types.h:237
@ RPT_CERTAIN
Definition fc_types.h:516
@ RPT_POSSIBLE
Definition fc_types.h:515
int action_id
Definition fc_types.h:250
#define ACTRES_NONE
Definition fc_types.h:188
#define ADV_WANT_PRINTF
Definition fc_types.h:1051
@ O_SHIELD
Definition fc_types.h:103
@ O_FOOD
Definition fc_types.h:103
@ O_TRADE
Definition fc_types.h:103
@ O_SCIENCE
Definition fc_types.h:103
@ O_LUXURY
Definition fc_types.h:103
@ O_GOLD
Definition fc_types.h:103
@ O_LAST
Definition fc_types.h:103
signed short Continent_id
Definition fc_types.h:233
#define PL_(String1, String2, n)
Definition fcintl.h:71
const struct ft_color ftc_server
const char * city_link(const struct city *pcity)
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
struct city * owner
Definition citydlg.c:226
static GtkWidget * source
Definition gotodlg.c:58
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
Definition handicaps.c:66
@ H_AWAY
Definition handicaps.h:19
@ H_PRODCHGPEN
Definition handicaps.h:35
const struct impr_type * valid_improvement(const struct impr_type *pimprove)
int impr_sell_gold(const struct impr_type *pimprove)
bool can_city_sell_building(const struct city *pcity, const struct impr_type *pimprove)
bool is_improvement_redundant(const struct city *pcity, const struct impr_type *pimprove)
bool can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
bool is_improvement(const struct impr_type *pimprove)
Impr_type_id improvement_number(const struct impr_type *pimprove)
const char * improvement_rule_name(const struct impr_type *pimprove)
Impr_type_id improvement_index(const struct impr_type *pimprove)
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
bool is_wonder(const struct impr_type *pimprove)
bool is_great_wonder(const struct impr_type *pimprove)
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
bool improvement_has_flag(const struct impr_type *pimprove, enum impr_flag_id flag)
struct city * city_from_small_wonder(const struct player *pplayer, const struct impr_type *pimprove)
bool is_convert_improvement(const struct impr_type *pimprove)
const char * improvement_name_translation(const struct impr_type *pimprove)
bool is_small_wonder(const struct impr_type *pimprove)
bool great_wonder_is_available(const struct impr_type *pimprove)
bool is_improvement_productive(const struct city *pcity, const struct impr_type *pimprove)
#define WONDER_NOT_OWNED
#define WONDER_NOT_BUILT
#define improvement_iterate_end
#define improvement_iterate(_p)
#define B_LAST
Definition improvement.h:42
void initialize_infrastructure_cache(struct player *pplayer)
Definition infracache.c:250
#define fc_assert_msg(condition, message,...)
Definition log.h:182
#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 fc_assert_action(condition, action)
Definition log.h:188
#define log_debug(message,...)
Definition log.h:116
#define log_normal(message,...)
Definition log.h:108
#define log_base(level, message,...)
Definition log.h:95
@ LOG_ERROR
Definition log.h:31
@ LOG_DEBUG
Definition log.h:35
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
void notify_player(const struct player *pplayer, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:291
struct city_list * cities
Definition packhand.c:120
int player_get_expected_income(const struct player *pplayer)
Definition player.c:1274
const char * player_name(const struct player *pplayer)
Definition player.c:885
#define players_iterate_end
Definition player.h:552
#define players_iterate(_pplayer)
Definition player.h:547
static bool is_barbarian(const struct player *pplayer)
Definition player.h:499
int normal_player_count(void)
Definition plrhand.c:3212
#define fc_rand(_size)
Definition rand.h:56
bool secfile_lookup_bool_default(const struct section_file *secfile, bool def, const char *path,...)
int secfile_lookup_int_default(const struct section_file *secfile, int def, const char *path,...)
#define secfile_insert_int(secfile, value, path,...)
#define secfile_insert_bool(secfile, value, path,...)
bool is_req_active(const struct req_context *context, const struct req_context *other_context, const struct requirement *req, const enum req_problem_type prob_type)
enum req_unchanging_status is_req_preventing(const struct req_context *context, const struct req_context *other_context, const struct requirement *req, enum req_problem_type prob_type)
enum req_item_found universal_fulfills_requirement(const struct requirement *preq, const struct universal *source)
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
@ ITF_YES
#define REQ_RANGE_MAX
@ REQUCH_NO
int research_goal_unknown_techs(const struct research *presearch, Tech_type_id goal)
Definition research.c:753
int research_goal_bulbs_required(const struct research *presearch, Tech_type_id goal)
Definition research.c:775
struct research * research_get(const struct player *pplayer)
Definition research.c:130
#define CLIP(lower, current, upper)
Definition shared.h:57
#define MIN(x, y)
Definition shared.h:55
#define MAX(x, y)
Definition shared.h:54
#define DEFAULT_SPECIALIST
Definition specialist.h:43
size_t size
Definition specvec.h:72
#define CITY_LOG(loglevel, pcity, msg,...)
Definition srv_log.h:83
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
@ AIT_CITY_MILITARY
Definition srv_log.h:63
@ AIT_BUILDINGS
Definition srv_log.h:51
@ AIT_CITY_TERRAIN
Definition srv_log.h:64
@ AIT_CITY_SETTLERS
Definition srv_log.h:65
@ AIT_EMERGENCY
Definition srv_log.h:62
@ 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 choice_type type
Definition advchoice.h:46
adv_want want
Definition advchoice.h:48
universals_u value
Definition advchoice.h:47
int pollution_priority
Definition advdata.h:121
int shield_priority
Definition advdata.h:113
int * cities
Definition advdata.h:96
int * ocean_cities
Definition advdata.h:97
int luxury_priority
Definition advdata.h:115
int food_priority
Definition advdata.h:114
enum adv_improvement_status impr_calc[B_LAST]
Definition advdata.h:58
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 adv_data::@94 stats
struct adv_choice choice
Definition daicity.h:41
int building_wait
Definition daicity.h:38
adv_want worth
Definition daicity.h:35
unsigned int grave_danger
Definition daicity.h:48
int * ocean_workers
Definition daidata.h:83
int * workers
Definition daidata.h:82
adv_want tech_want[A_LAST+1]
Definition daidata.h:104
struct ai_plr::@279 stats
Definition ai.h:50
void * private
Definition ai.h:53
Definition city.h:318
int incite_total_factor
Definition game.h:157
int diplchance
Definition game.h:145
struct packet_game_info info
Definition game.h:89
int incite_improvement_factor
Definition game.h:156
int incite_unit_factor
Definition game.h:158
struct civ_game::@32::@36 server
struct requirement_vector obsolete_by
Definition improvement.h:59
struct requirement_vector reqs
Definition improvement.h:58
int great_wonder_owners[B_LAST]
int maxbuycost
Definition player.h:114
struct city_list * cities
Definition player.h:281
struct player_ai ai_common
Definition player.h:288
int wonders[B_LAST]
Definition player.h:305
struct team * team
Definition player.h:261
struct player_economic economic
Definition player.h:284
const struct player * player
struct universal source
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
int pop_cost
Definition unittype.h:518
Definition unit.h:140
int id
Definition unit.h:147
bool debug
Definition unit.h:237
struct unit::@84::@87 server
struct tile * tile
Definition unit.h:142
struct unit_adv * adv
Definition unit.h:239
int homecity
Definition unit.h:148
const struct unit_type * utype
Definition unit.h:141
struct player * owner
Definition unit.h:145
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
Tech_type_id advance_index(const struct advance *padvance)
Definition tech.c:90
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:100
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_worked(_tile)
Definition tile.h:119
#define TILE_XY(ptile)
Definition tile.h:43
#define tile_continent(_tile)
Definition tile.h:93
int city_num_trade_routes(const struct city *pcity)
#define trade_routes_iterate_end
#define trade_routes_iterate(c, proute)
#define TRAIT_DEFAULT_VALUE
Definition traits.h:32
const struct unit_type * utype
Definition fc_types.h:536
bool unit_is_alive(int id)
Definition unit.c:2306
int unit_upkeep_cost(const struct unit *punit, Output_type_id otype)
Definition unit.c:2972
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1576
bool is_field_unit(const struct unit *punit)
Definition unit.c:457
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:402
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1682
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:655
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1786
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1308
#define unit_tile(_pu)
Definition unit.h:408
#define unit_owner(_pu)
Definition unit.h:407
bool unit_perform_action(struct player *pplayer, const int actor_id, const int target_id, const int sub_tgt_id_incoming, const char *name, const action_id action_type, const enum action_requester requester)
Definition unithand.c:3370
void unit_do_action(struct player *pplayer, const int actor_id, const int target_id, const int sub_tgt_id, const char *name, const action_id action_type)
Definition unithand.c:3350
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_safe(unitlist, _unit)
Definition unitlist.h:39
#define unit_list_iterate_end
Definition unitlist.h:33
#define unit_list_iterate_safe_end
Definition unitlist.h:61
struct unit_type * best_role_unit(const struct city *pcity, int role)
Definition unittype.c:2303
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:126
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:3002
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2284
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1613
int num_role_units(int role)
Definition unittype.c:2234
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1729
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1604
bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
Definition unittype.c:233
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:215
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1463
int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Definition unittype.c:206
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1758