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 bestunit = iunit;
219 bestattack = iunit->attack_strength;
220 }
221 }
222
223 /* Choose among those made available through other civ's research */
224 for (i = 0; i < num_role_units(L_BARBARIAN_BUILD_TECH); i++) {
226
227 if (iunit->attack_strength > bestattack
229 bestunit = iunit;
230 bestattack = iunit->attack_strength;
231 }
232 }
233
234 /* If found anything, put it into the choice */
235 if (bestunit) {
236 choice->value.utype = bestunit;
237 /* "Overriding military emergency" indicator */
239 choice->type = CT_ATTACKER;
240 adv_choice_set_use(choice, "barbarian");
241 } else {
242 log_base(LOG_WANT, "Barbarians don't know what to build!");
243 }
244}
245
246/**********************************************************************/
254static void dai_city_choose_build(struct ai_type *ait,
255 struct player *pplayer,
256 struct city *pcity)
257{
258 struct adv_choice *newchoice;
259 struct adv_data *adv = adv_data_get(pplayer, nullptr);
260 struct ai_city *city_data = def_ai_city_data(pcity, ait);
261 const struct civ_map *nmap = &(wld.map);
262
263 if (has_handicap(pplayer, H_AWAY)
265 && city_data->urgency == 0) {
266 /* Don't change existing productions unless we have to. */
267 return;
268 }
269
270 if (is_barbarian(pplayer)) {
271 dai_barbarian_choose_build(pplayer, pcity, &(city_data->choice));
272 } else {
273 if ((city_data->choice.want < DAI_WANT_MILITARY_EMERGENCY
274 || city_data->urgency == 0)
275 && !(dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0
276 && pcity->id != adv->wonder_city)) {
278 adv_choice_copy(&(city_data->choice),
281 }
282 }
283
284 /* Fallbacks */
285 if (city_data->choice.want == 0) {
286 /* Fallbacks do happen with techlevel 0, which is now default. -- Per */
287 CITY_LOG(LOG_WANT, pcity, "Falling back - didn't want to build soldiers,"
288 " workers, caravans, settlers, or buildings!");
289 city_data->choice.want = 1;
291 city_data->choice.value.utype
293 city_data->choice.type = CT_CIVILIAN;
294 adv_choice_set_use(&(city_data->choice), "fallback trade route");
295 } else {
296 unsigned int our_def = assess_defense_quadratic(ait, pcity);
297
298 if (our_def == 0
299 && dai_process_defender_want(ait, nmap, pplayer, pcity, 1,
300 &(city_data->choice), 0)) {
301 adv_choice_set_use(&(city_data->choice), "fallback defender");
302 CITY_LOG(LOG_DEBUG, pcity, "Building fallback defender");
303 } else if (best_role_unit(pcity, UTYF_WORKERS)) {
304 city_data->choice.value.utype
306 city_data->choice.type = CT_CIVILIAN;
307 adv_choice_set_use(&(city_data->choice), "fallback worker");
308 } else {
309 CITY_LOG(LOG_ERROR, pcity, "Cannot even build a fallback "
310 "(caravan/coinage/settlers). Fix the ruleset!");
311 city_data->choice.want = 0;
312 }
313 }
314 }
315
316 if (city_data->choice.want != 0) {
317 struct universal build_new;
318
320
321#ifdef ADV_CHOICE_TRACK
322 if (city_data->choice.log_if_chosen) {
323 log_normal("%s wants %s for %s with desire " ADV_WANT_PRINTF ".",
326 city_data->choice.use,
327 city_data->choice.want);
328 }
329#endif /* ADV_CHOICE_TRACK */
330
331 CITY_LOG(LOG_DEBUG, pcity, "wants %s with desire " ADV_WANT_PRINTF ".",
333 city_data->choice.want);
334
335 switch (city_data->choice.type) {
336 case CT_CIVILIAN:
337 case CT_ATTACKER:
338 case CT_DEFENDER:
339 build_new.kind = VUT_UTYPE;
340 build_new.value.utype = city_data->choice.value.utype;
341 break;
342 case CT_BUILDING:
344 build_new.value.building = city_data->choice.value.building;
345 break;
346 case CT_NONE:
347 build_new.kind = VUT_NONE;
348 break;
349 case CT_LAST:
351 break;
352 };
353
356 }
357}
358
359/**********************************************************************/
362static void try_to_sell_stuff(struct player *pplayer, struct city *pcity)
363{
364 improvement_iterate(pimprove) {
365 if (can_city_sell_building(pcity, pimprove)
366 && !building_has_effect(pimprove, EFT_DEFEND_BONUS)) {
367 /* Selling walls to buy defenders is counterproductive -- Syela */
368 really_handle_city_sell(pplayer, pcity, pimprove);
369 break;
370 }
372}
373
374/**********************************************************************/
378static void increase_maxbuycost(struct player *pplayer, int new_value)
379{
381}
382
383/**********************************************************************/
388static void dai_upgrade_units(struct city *pcity, int limit, bool military)
389{
391 struct player *pplayer = city_owner(pcity);
392 int expenses;
393
394 {
395 /* Find upgrade unit actions */
396 int i = 0;
399 }
400
401 dai_calc_data(pplayer, nullptr, &expenses, nullptr);
402
404 if (pcity->owner == punit->owner) {
405 /* Only upgrade units you own, not allied ones */
406
407 const struct unit_type *old_type = unit_type_get(punit);
408 const struct unit_type *punittype
409 = can_upgrade_unittype(pplayer, old_type);
410
411 if (military && !IS_ATTACKER(old_type)) {
412 /* Only upgrade military units this round */
413 continue;
414 } else if (!military && IS_ATTACKER(old_type)) {
415 /* Only civilians or transports this round */
416 continue;
417 }
418
419 if (punittype == nullptr) {
420 continue;
421 }
422
424 if (action_ever_possible(act_id)) {
425 const struct action *paction = action_by_number(act_id);
426 int cost = unit_upgrade_price(pplayer, old_type, punittype);
427 int real_limit = limit;
428
429 /* Sinking Triremes are DANGEROUS!!
430 * We'll do anything to upgrade 'em. */
431 /* FIXME: This assumes rules to be quite close to civ/2.
432 * Of the supplied rulesets those are the only ones with
433 * UTYF_COAST unit, but... */
436 }
437 if (pplayer->economic.gold - cost > real_limit) {
438 CITY_LOG(LOG_BUY, pcity, "Upgraded %s to %s for %d (%s)",
441 cost,
442 military ? "military" : "civilian");
444 pcity->id, 0, "",
445 paction->id);
446 } else {
447 increase_maxbuycost(pplayer, cost);
448 }
449 }
451 }
453}
454
455/**********************************************************************/
461static void unit_do_disband_trad(struct player *owner, struct unit *punit,
462 const enum action_requester requester)
463{
464 const int punit_id_stored = punit->id;
465 const struct civ_map *nmap = &(wld.map);
466
468
469 /* Help Wonder gives 100% of the shields used to produce the unit to the
470 * city where it is located. */
472 struct city *tgt_city;
473
474 /* Only a city at the same tile as the unit can benefit. */
476
477 if (tgt_city
479 punit, tgt_city)) {
481 0, nullptr, ACTION_HELP_WONDER, requester)) {
482 /* No shields wasted. The unit did Help Wonder. */
483 return;
484 }
485
487 /* The unit is gone. Maybe it was killed in Lua? */
488 return;
489 }
490 }
491 }
492
493 /* Disbanding a unit inside a city gives it 50% of the shields used to
494 * produce the unit. */
496 struct city *tgt_city;
497
498 /* Only a city at the same tile as the unit can benefit. */
500
501 if (tgt_city
503 punit, tgt_city)) {
505 0, nullptr, ACTION_DISBAND_UNIT_RECOVER,
506 requester)) {
507 /* The unit did Disband Unit Recover. 50% of the shields wasted. */
508 return;
509 }
510
512 /* The unit is gone. Maybe it was killed in Lua? */
513 return;
514 }
515 }
516 }
517
518 /* Try to disband even if all shields will be wasted. */
522 0, nullptr, ACTION_DISBAND_UNIT, requester)) {
523 /* All shields wasted. The unit did Disband Unit. */
524 return;
525 }
526 }
527 }
528}
529
530/**********************************************************************/
533static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
534{
535 struct adv_choice bestchoice;
536 int cached_limit = dai_gold_reserve(pplayer);
537 int expenses;
538 bool war_footing = dai_on_war_footing(ait, pplayer);
539
540 /* Disband explorers that are at home but don't serve a purpose.
541 * FIXME: This is a hack, and should be removed once we
542 * learn how to ferry explorers to new land. */
543 city_list_iterate(pplayer->cities, pcity) {
544 struct tile *ptile = pcity->tile;
547 && pcity->id == punit->homecity
548 && def_ai_city_data(pcity, ait)->urgency == 0) {
549 CITY_LOG(LOG_BUY, pcity, "disbanding %s to increase production",
552 }
555
556 dai_calc_data(pplayer, nullptr, &expenses, nullptr);
557
558 do {
559 bool expensive; /* Don't buy when it costs x2 unless we must */
560 int buycost;
561 int limit = cached_limit; /* cached_limit is our gold reserve */
562 struct city *pcity = nullptr;
563 struct ai_city *city_data;
564
565 /* Find highest wanted item on the buy list */
567 city_list_iterate(pplayer->cities, acity) {
569
570 if (acity_data->choice.want
571 > bestchoice.want && ai_fuzzy(pplayer, TRUE)) {
573 pcity = acity;
574 }
576
577 /* We found nothing, so we're done */
578 if (pcity == nullptr) {
579 break;
580 }
581
583
584 /* Not dealing with this city a second time */
585 city_data->choice.want = -1;
586
588
589 /* Try upgrade units at danger location (high want is usually danger) */
590 if (city_data->urgency > 1) {
591 if (bestchoice.type == CT_BUILDING
592 && is_wonder(bestchoice.value.building)) {
593 CITY_LOG(LOG_BUY, pcity, "Wonder being built in dangerous position!");
594 } else {
595 /* If we have urgent want, spend more */
596 int upgrade_limit = limit;
597
598 if (city_data->urgency > 1) {
600 }
601 /* Upgrade only military units now */
603 }
604 }
605
606 if (pcity->anarchy != 0 && bestchoice.type != CT_BUILDING) {
607 continue; /* Nothing we can do */
608 }
609
610 /* Cost to complete production */
612
613 if (buycost <= 0) {
614 continue; /* Already completed */
615 }
616
618 && utype_is_cityfounder(bestchoice.value.utype)) {
620 && bestchoice.value.utype->pop_cost > 0
621 && city_size_get(pcity) <= bestchoice.value.utype->pop_cost) {
622 /* Don't buy settlers in cities that cannot afford
623 * the population cost. */
624 /* This used to check also if city is about to grow to required size
625 * next turn and allow buying of settlers in that case, but current
626 * order of end/start turn activities is such that settler building
627 * fails already before city grows. */
628 continue;
629 } else if (city_list_size(pplayer->cities) > 6) {
630 /* Don't waste precious money buying settlers late game
631 * since this raises taxes, and we want science. Adjust this
632 * again when our tax algorithm is smarter. */
633 continue;
634 } else if (war_footing) {
635 continue;
636 }
637 } else {
638 /* We are not a settler. Therefore we increase the cash need we
639 * balance our buy desire with to keep cash at hand for emergencies
640 * and for upgrades */
641 limit *= 2;
642 }
643
644 /* It costs x2 to buy something with no shields contributed */
645 expensive = (pcity->shield_stock == 0)
646 || (pplayer->economic.gold - buycost < limit);
647
648 if (bestchoice.type == CT_ATTACKER
649 && buycost
651 bestchoice.value.utype) * 2
652 && !war_footing) {
653 /* Too expensive for an offensive unit */
654 continue;
655 }
656
657 /* FIXME: Here Syela wanted some code to check if
658 * pcity was doomed, and we should therefore attempt
659 * to sell everything in it of non-military value */
660
661 if (pplayer->economic.gold - expenses >= buycost
662 && (!expensive
663 || (city_data->grave_danger != 0
664 && assess_defense(ait, pcity) == 0)
665 || (bestchoice.want > 200 && city_data->urgency > 1))) {
666 /* Buy stuff */
668 "Crash buy of %s for %d (want " ADV_WANT_PRINTF ")",
670 buycost,
671 bestchoice.want);
673 } else if (city_data->grave_danger != 0
674 && bestchoice.type == CT_DEFENDER
675 && assess_defense(ait, pcity) == 0) {
676 /* We have no gold but MUST have a defender */
677 CITY_LOG(LOG_BUY, pcity, "must have %s but can't afford it (%d < %d)!",
679 pplayer->economic.gold,
680 buycost);
681 try_to_sell_stuff(pplayer, pcity);
682 if (pplayer->economic.gold - expenses >= buycost) {
683 CITY_LOG(LOG_BUY, pcity, "now we can afford it (sold something)");
685 }
687 }
688 } while (TRUE);
689
690 if (!war_footing) {
691 /* Civilian upgrades now */
692 city_list_iterate(pplayer->cities, pcity) {
695 }
696
697 log_base(LOG_BUY, "%s wants to keep %d in reserve (tax factor %d)",
698 player_name(pplayer), cached_limit,
699 pplayer->ai_common.maxbuycost);
700}
701
702/**********************************************************************/
705static int unit_food_upkeep(struct unit *punit)
706{
707 int upkeep = unit_upkeep_cost(punit, O_FOOD);
708
709 if (punit->id != 0 && punit->homecity == 0) {
710 upkeep = 0; /* Thanks, Peter */
711 }
712
713 return upkeep;
714}
715
716/**********************************************************************/
724static int unit_foodbox_cost(struct unit *punit)
725{
726 int pop_cost = unit_type_get(punit)->pop_cost;
727
728 if (pop_cost <= 0) {
729 return 0;
730 }
731
732 if (punit->id == 0) {
733 /* It is a virtual unit, so must start in a city... */
734 struct city *pcity = tile_city(unit_tile(punit));
735 int size = city_size_get(pcity);
736 int cost = 0;
737 int i;
738
739 /* The default is to lose 100%. The granary bonus reduces this. */
741
742 foodloss_pct = CLIP(0, foodloss_pct, 100);
743 fc_assert_ret_val(pcity != nullptr, -1);
744 fc_assert(size >= pop_cost);
745
746 for (i = pop_cost; i > 0 ; i--) {
748 }
749 cost = cost * foodloss_pct / 100;
750
751 return cost;
752 }
753
754 return 30;
755}
756
757/**********************************************************************/
764 struct city *pcity)
765{
766 struct unit *virtualunit;
767 int want;
769 struct extra_type *best_target;
770 struct tile *best_tile = nullptr; /* May be accessed by log_*() calls. */
771 struct tile *pcenter = city_tile(pcity);
772 struct player *pplayer = city_owner(pcity);
773 struct adv_data *adv = adv_data_get(pplayer, nullptr);
774 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
775 struct unit_type *utype;
777 struct ai_city *city_data = def_ai_city_data(pcity, ait);
778 struct dai_private_data *private = (struct dai_private_data *)ait->private;
779 const struct civ_map *nmap = &(wld.map);
780
781 if (!private->contemplace_workers) {
782 /* AI type uses custom method to set worker want and type. */
783 return;
784 }
785
786 city_data->worker_want = 0; /* Make sure old want does not stay if
787 * we don't want now */
788
790
791 if (utype == nullptr) {
792 log_debug("No UTYF_WORKERS role unit available");
793 return;
794 }
795
796 /* Create a localized "virtual" unit to do operations with. */
797 virtualunit = unit_virtual_create(pplayer, pcity, utype, 0);
798 /* Advisors data space not allocated as it's not needed in the
799 lifetime of the virtualunit. */
803 &best_tile,
804 nullptr, nullptr);
805 if (unit_type_get(virtualunit)->pop_cost >= city_size_get(pcity)) {
806 /* We don't like disbanding the city as a side effect */
808
809 return;
810 }
811 /* We consider unit_food_upkeep with only half FOOD_WEIGHTING to
812 * balance the fact that unit can improve many tiles during its
813 * lifetime, and want is calculated for just one of them.
814 * Having full FOOD_WEIGHT here would mean that tile improvement of
815 * +1 food would give just zero want for settler. Other weights
816 * are lower, so +1 shield - unit food upkeep would be negative. */
817 want = (want - unit_food_upkeep(virtualunit) * FOOD_WEIGHTING / 2) * 100
820
821 /* Massage our desire based on available statistics to prevent
822 * overflooding with worker type units if they come cheap in
823 * the ruleset */
824 if (place >= 0) {
825 want /= MAX(1, ai->stats.workers[place] / (adv->stats.cities[place] + 1));
826 want -= ai->stats.workers[place];
827 } else {
828 want /= MAX(1, ai->stats.ocean_workers[-place]
829 / (adv->stats.ocean_cities[-place] + 1));
830 want -= ai->stats.ocean_workers[-place];
831 }
832 want = MAX(want, 0);
833
834 if (place >= 0) {
835 CITY_LOG(LOG_DEBUG, pcity, "wants %s with want %d to do %s at (%d,%d), "
836 "we have %d workers and %d cities on the continent",
837 utype_rule_name(utype),
838 want,
841 ai->stats.workers[place],
842 adv->stats.cities[place]);
843 } else {
844 CITY_LOG(LOG_DEBUG, pcity, "wants %s with want %d to do %s at (%d,%d), "
845 "we have %d workers and %d cities on the ocean",
846 utype_rule_name(utype),
847 want,
851 adv->stats.ocean_cities[-place]);
852 }
853
854 fc_assert(want >= 0);
855
856 city_data->worker_want = want;
857 city_data->worker_type
859 place >= 0 ? TC_LAND : TC_OCEAN);
860}
861
862/**********************************************************************/
868void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
869{
870 pplayer->ai_common.maxbuycost = 0;
871
873 city_list_iterate(pplayer->cities, pcity) {
875 || city_granary_size(city_size_get(pcity)) == pcity->food_stock) {
876 /* Having a full granary isn't an emergency, but we want to rearrange */
877 auto_arrange_workers(pcity); /* This usually helps */
878 }
879 if (CITY_EMERGENCY(pcity)) {
880 /* Fix critical shortages or unhappiness */
881 resolve_city_emergency(ait, pplayer, pcity);
882 }
884 sync_cities();
886 if (pplayer->economic.tax >= 30 /* Otherwise expect it to increase tax */
887 && player_get_expected_income(pplayer) < -(pplayer->economic.gold)) {
888 int count = city_list_size(pplayer->cities);
889 struct city *sellers[count + 1];
890 int i;
891
892 /* Randomized order */
893 count = 0;
894 city_list_iterate(pplayer->cities, pcity) {
895 sellers[count++] = pcity;
897
898 for (i = 0; i < count; i++) {
899 int replace = fc_rand(count);
900 struct city *tmp;
901
902 tmp = sellers[i];
903 sellers[i] = sellers[replace];
904 sellers[replace] = tmp;
905 }
906
907 i = 0;
908 while (player_get_expected_income(pplayer) < -(pplayer->economic.gold)
909 && i < count) {
911 }
912 }
914
916 building_advisor(pplayer);
918
919 /* Initialize the infrastructure cache, which is used shortly. */
921 city_list_iterate(pplayer->cities, pcity) {
922 struct ai_city *city_data = def_ai_city_data(pcity, ait);
923 struct adv_choice *choice;
924
925 if (city_data->choice.want <= 0) {
926 /* Note that this function mungs the seamap, but we don't care */
928 choice = military_advisor_choose_build(ait, &(wld.map), pplayer, pcity,
929 nullptr);
930 adv_choice_copy(&(city_data->choice), choice);
931 adv_free_choice(choice);
933 }
934 if (dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0) {
935 city_data->worker_want = 0;
936 city_data->founder_want = 0;
937 city_data->founder_turn = game.info.turn; /* Do not consider zero we set
938 * here a valid value,
939 * if real want is needed.
940 * Recalculate immediately
941 * in such situation. */
942 continue; /* Go, soldiers! */
943 }
944 /* Will record its findings in pcity->worker_want */
948
950 if (city_data->founder_turn <= game.info.turn) {
951 /* Will record its findings in pcity->founder_want */
953 /* Avoid recalculating all the time.. */
954 /* This means AI is not very opportunistic if there happens to open up
955 * spot for a new city. */
956 city_data->founder_turn
958 } else if (pcity->server.debug) {
959 /* Recalculate every turn */
961 }
965 /* Reset auto settler state for the next run. */
966 dai_auto_settler_reset(ait, pplayer);
967
968 city_list_iterate(pplayer->cities, pcity) {
969 dai_city_choose_build(ait, pplayer, pcity);
970
971 /* Initialize for next turn */
972 def_ai_city_data(pcity, ait)->choice.want = -1;
974
975 dai_spend_gold(ait, pplayer);
976}
977
978/**********************************************************************/
985static bool building_crucial(const struct player *plr,
986 struct impr_type *pimprove,
987 const struct city *pcity)
988{
989#if 0 /* This check will become more complicated now. */
990 if (ai_wants_no_science(plr)
992 return FALSE;
993 }
994#endif /* 0 */
995
997 /* selling city walls is really, really dumb -- Syela */
998 || is_improvement_productive(pcity, pimprove)) {
999 return TRUE;
1000 }
1001
1002 return FALSE;
1003}
1004
1005/**********************************************************************/
1009 bool redundant_only)
1010{
1011 struct player *pplayer = city_owner(pcity);
1012
1013 city_built_iterate(pcity, pimprove) {
1014 if (can_city_sell_building(pcity, pimprove)
1015 && !building_crucial(pplayer, pimprove, pcity)
1016 && (!redundant_only || is_improvement_redundant(pcity, pimprove))) {
1017 int gain = impr_sell_gold(pimprove);
1018
1020 PL_("%s is selling %s for %d.",
1021 "%s is selling %s for %d.", gain),
1024 gain);
1025 do_sell_building(pplayer, pcity, pimprove, "sold");
1026
1027 return; /* Max 1 building each turn */
1028 }
1030}
1031
1032/**********************************************************************/
1051static void resolve_city_emergency(struct ai_type *ait,
1052 struct player *pplayer,
1053 struct city *pcity)
1054{
1055 struct tile *pcenter = city_tile(pcity);
1056 const struct civ_map *nmap = &(wld.map);
1057
1059 "Emergency in %s (%s, angry%d, unhap%d food%d, prod%d)",
1061 city_unhappy(pcity) ? "unhappy" : "content",
1064 pcity->surplus[O_FOOD],
1065 pcity->surplus[O_SHIELD]);
1066
1068 struct city *acity = tile_worked(atile);
1069
1070 if (acity && acity != pcity && city_owner(acity) == city_owner(pcity)) {
1071 log_base(LOG_EMERGENCY, "%s taking over %s square in (%d, %d)",
1073
1074#ifndef FREECIV_NDEBUG
1075 int ax, ay;
1076#endif
1078 continue);
1079
1080 if (is_free_worked(acity, atile)) {
1081 /* Can't remove a worker here. */
1082 continue;
1083 }
1084
1086 acity->specialists[DEFAULT_SPECIALIST]++;
1088 }
1090
1092
1093 if (!CITY_EMERGENCY(pcity)) {
1094 log_base(LOG_EMERGENCY, "Emergency in %s resolved", city_name_get(pcity));
1095 goto cleanup;
1096 }
1097
1098 unit_list_iterate_safe(pcity->units_supported, punit) {
1099 if (city_unhappy(pcity)
1100 && (utype_happy_cost(unit_type_get(punit), pplayer) > 0
1102 && def_ai_unit_data(punit, ait)->passenger == 0) {
1103 UNIT_LOG(LOG_EMERGENCY, punit, "is causing unrest, disbanded");
1104 /* TODO: If Help Wonder stops blocking Disband Unit there may be
1105 * cases where Disband Unit should be selected. Example: Field unit
1106 * in allied city that is building a wonder that makes the ally win
1107 * without sharing the victory. */
1108 /* TODO: Should the unit try to find legal targets at adjacent tiles?
1109 * Should it consider other self eliminating actions than the
1110 * components of the traditional disband? */
1113 }
1115
1116 if (CITY_EMERGENCY(pcity)) {
1117 log_base(LOG_EMERGENCY, "Emergency in %s remains unresolved",
1119 } else {
1121 "Emergency in %s resolved by disbanding unit(s)",
1123 }
1124
1125 cleanup:
1127 sync_cities();
1128}
1129
1130/**********************************************************************/
1133void dai_city_alloc(struct ai_type *ait, struct city *pcity)
1134{
1135 struct ai_city *city_data = fc_calloc(1, sizeof(struct ai_city));
1136
1137 city_data->building_wait = BUILDING_WAIT_MINIMUM;
1138 adv_init_choice(&(city_data->choice));
1139
1141}
1142
1143/**********************************************************************/
1146void dai_city_free(struct ai_type *ait, struct city *pcity)
1147{
1148 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1149
1150 if (city_data != nullptr) {
1151 adv_deinit_choice(&(city_data->choice));
1152 city_set_ai_data(pcity, ait, nullptr);
1154 }
1155}
1156
1157/**********************************************************************/
1160void dai_city_save(struct ai_type *ait, const char *aitstr,
1161 struct section_file *file, const struct city *pcity,
1162 const char *citystr)
1163{
1164 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1165
1166 /* FIXME: Remove this when the urgency is properly recalculated. */
1167 secfile_insert_int(file, city_data->urgency, "%s.%s.urgency",
1168 citystr, aitstr);
1169
1170 /* Avoid fc_rand() recalculations on subsequent reload. */
1171 secfile_insert_int(file, city_data->building_turn, "%s.%s.building_turn",
1172 citystr, aitstr);
1173 secfile_insert_int(file, city_data->building_wait, "%s.%s.building_wait",
1174 citystr, aitstr);
1175
1176 /* Avoid fc_rand() and expensive recalculations on subsequent reload. */
1177 secfile_insert_int(file, city_data->founder_turn, "%s.%s.founder_turn",
1178 citystr, aitstr);
1179 secfile_insert_int(file, city_data->founder_want, "%s.%s.founder_want",
1180 citystr, aitstr);
1181 secfile_insert_bool(file, city_data->founder_boat, "%s.%s.founder_boat",
1182 citystr, aitstr);
1183}
1184
1185/**********************************************************************/
1188void dai_city_load(struct ai_type *ait, const char *aitstr,
1189 const struct section_file *file,
1190 struct city *pcity, const char *citystr)
1191{
1192 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1193
1194 /* FIXME: Remove this when the urgency is properly recalculated. */
1195 city_data->urgency
1196 = secfile_lookup_int_default(file, 0, "%s.%s.urgency", citystr, aitstr);
1197
1198 /* Avoid fc_rand() recalculations on subsequent reload. */
1199 city_data->building_turn
1200 = secfile_lookup_int_default(file, 0, "%s.%s.building_turn", citystr,
1201 aitstr);
1202 city_data->building_wait
1204 "%s.%s.building_wait", citystr, aitstr);
1205
1206 /* Avoid fc_rand() and expensive recalculations on subsequent reload. */
1207 city_data->founder_turn
1208 = secfile_lookup_int_default(file, 0, "%s.%s.founder_turn", citystr,
1209 aitstr);
1210 city_data->founder_want
1211 = secfile_lookup_int_default(file, 0, "%s.%s.founder_want", citystr,
1212 aitstr);
1213 city_data->founder_boat
1214 = secfile_lookup_bool_default(file, (city_data->founder_want < 0),
1215 "%s.%s.founder_boat", citystr, aitstr);
1216}
1217
1218/**********************************************************************/
1231 const struct city *pcity)
1232{
1233 struct action *paction = action_by_number(act_id);
1234
1236 "Action %d don't exist.", act_id);
1237
1239 /* Not city targeted in this ruleset. */
1240 return 0;
1241 }
1242
1243 switch (paction->result) {
1245 /* Copied from the evaluation of the No_Incite effect */
1246 return MAX((game.server.diplchance * 2
1249 - game.server.incite_unit_factor * 5, 0);
1250
1251 /* Really bad for the city owner. */
1252 case ACTRES_SPY_NUKE:
1254 /* The ai will never destroy their own city to keep it out of enemy
1255 * hands. If it starts supporting it this value should change. */
1257 return 20;
1258
1259 /* Bad for the city owner. */
1260 case ACTRES_SPY_POISON:
1268 case ACTRES_NUKE:
1269 /* TODO: Individual and well balanced values */
1270 return 10;
1271
1272 /* Good for an enemy */
1275 case ACTRES_STEAL_MAPS:
1276 /* TODO: Individual and well balanced values */
1277 return 8;
1278
1279 /* Could be worse */
1282 case ACTRES_MARKETPLACE:
1283 case ACTRES_SPY_ESCAPE:
1284 /* TODO: Individual and well balanced values */
1285 return 1;
1286
1287 /* Good for the city owner in most cases. */
1288 case ACTRES_TRADE_ROUTE:
1289 case ACTRES_HELP_WONDER:
1290 case ACTRES_JOIN_CITY:
1292 case ACTRES_HOME_CITY:
1294 case ACTRES_AIRLIFT:
1295 /* TODO: Individual and well balanced values */
1296 return -1;
1297
1298 /* Ruleset defined actions. We have no idea what they do. */
1299 case ACTRES_NONE:
1300 return 0;
1301
1302 /* Shouldn't happen. */
1304 /* Against the tile so potential city effects are overlooked for now. */
1308 case ACTRES_SPY_ATTACK:
1309 case ACTRES_EXPEL_UNIT:
1312 case ACTRES_BOMBARD:
1313 case ACTRES_FOUND_CITY:
1314 case ACTRES_NUKE_UNITS:
1315 case ACTRES_PARADROP:
1316 case ACTRES_ATTACK:
1317 case ACTRES_WIPE_UNITS:
1319 case ACTRES_HEAL_UNIT:
1321 case ACTRES_CULTIVATE:
1322 case ACTRES_PLANT:
1323 case ACTRES_PILLAGE:
1324 case ACTRES_CLEAN:
1325 case ACTRES_FORTIFY:
1326 case ACTRES_ROAD:
1327 case ACTRES_CONVERT:
1328 case ACTRES_BASE:
1329 case ACTRES_MINE:
1330 case ACTRES_IRRIGATE:
1338 case ACTRES_HUT_ENTER:
1340 case ACTRES_UNIT_MOVE:
1341 case ACTRES_TELEPORT:
1343 case ACTRES_HOMELESS:
1346 "Action not aimed at cities");
1347 break;
1348 }
1349
1350 /* Wrong action. Ignore it. */
1351 return 0;
1352}
1353
1354/**********************************************************************/
1363static bool adjust_wants_for_reqs(struct ai_type *ait,
1364 struct player *pplayer,
1365 struct city *pcity,
1366 const struct impr_type *pimprove,
1367 const adv_want v)
1368{
1369 bool all_met = TRUE;
1370 int n_needed_techs = 0;
1371 int n_needed_improvements = 0;
1374
1375 const struct req_context context = {
1376 .player = pplayer,
1377 .city = pcity,
1378 .tile = city_tile(pcity),
1379 .building = pimprove,
1380 };
1381
1384
1385 requirement_vector_iterate(&pimprove->reqs, preq) {
1386 const bool active = is_req_active(&context, nullptr, preq, RPT_POSSIBLE);
1387
1388 if (VUT_ADVANCE == preq->source.kind && preq->present && !active) {
1389 /* Found a missing technology requirement for this improvement. */
1390 tech_vector_append(&needed_techs, preq->source.value.advance);
1391 } else if (VUT_IMPROVEMENT == preq->source.kind && preq->present
1392 && !active) {
1393 /* Found a missing improvement requirement for this improvement.
1394 * For example, in the default ruleset a city must have a Library
1395 * before it can have a University. */
1396 impr_vector_append(&needed_improvements, preq->source.value.building);
1397 }
1398 all_met = all_met && active;
1400
1401 /* If v is negative, the improvement is not worth building,
1402 * but there is no need to punish research of the technologies
1403 * that would make it available.
1404 */
1406 if (0 < v && 0 < n_needed_techs) {
1407 /* Tell AI module how much we want this improvement and what techs are
1408 * required to get it. */
1409 const adv_want dv = v / n_needed_techs;
1410
1411 want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1412 &needed_techs, dv);
1413 }
1414
1415 /* If v is negative, the improvement is not worth building,
1416 * but there is no need to punish building the improvements
1417 * that would make it available.
1418 */
1420 if (0 < v && 0 < n_needed_improvements) {
1421 /* Because we want this improvement,
1422 * we want the improvements that will make it possible */
1423 const adv_want dv = v / (n_needed_improvements * 4); /* WAG */
1424 int i;
1425
1426 for (i = 0; i < n_needed_improvements; i++) {
1427 const struct impr_type *needed_impr
1429 /* TODO: increase the want for the needed_impr,
1430 * if we can build it now */
1431 /* Recurse */
1432 (void) adjust_wants_for_reqs(ait, pplayer, pcity, needed_impr, dv);
1433 }
1434 }
1435
1436 /* TODO: use a similar method to increase wants for governments
1437 * that will make this improvement possible? */
1438
1441
1442 return all_met;
1443}
1444
1445/**********************************************************************/
1450adv_want dai_city_want(struct player *pplayer, struct city *acity,
1451 struct adv_data *adv, struct impr_type *pimprove)
1452{
1453 adv_want want = 0;
1454 int prod[O_LAST], bonus[O_LAST], waste[O_LAST];
1455 const struct civ_map *nmap = &(wld.map);
1456
1457 memset(prod, 0, O_LAST * sizeof(*prod));
1458 if (pimprove != nullptr
1459 && adv->impr_calc[improvement_index(pimprove)]
1461 struct tile *acenter = city_tile(acity);
1463
1464 /* The below calculation mostly duplicates get_worked_tile_output().
1465 * We do this only for buildings that we know may change tile
1466 * outputs. */
1468 if (tile_worked(ptile) == acity) {
1470 prod[o] += city_tile_output(acity, ptile, celebrating, o);
1472 }
1474
1476 } else {
1477 fc_assert(sizeof(*prod) == sizeof(*acity->citizen_base));
1478 memcpy(prod, acity->citizen_base, O_LAST * sizeof(*prod));
1479 }
1480
1482 prod[O_TRADE] += proute->value;
1487 waste[o] = city_waste(acity, o, prod[o] * bonus[o] / 100, nullptr);
1489 add_tax_income(pplayer,
1490 prod[O_TRADE] * bonus[O_TRADE] / 100 - waste[O_TRADE],
1491 prod);
1493 prod[o] = prod[o] * bonus[o] / 100 - waste[o];
1495
1499 /* Unit upkeep isn't handled here. Unless we do a full city_refresh it
1500 * won't be changed anyway. */
1501
1502 want += prod[O_FOOD] * adv->food_priority;
1503 if (prod[O_SHIELD] != 0) {
1504 want += prod[O_SHIELD] * adv->shield_priority;
1505 want -= city_pollution(acity, prod[O_SHIELD]) * adv->pollution_priority;
1506 }
1507 want += prod[O_LUXURY] * adv->luxury_priority;
1508 want += prod[O_SCIENCE] * adv->science_priority;
1509 if (pplayer->economic.tax > 50) {
1510 /* Increased tax rate indicates that we've had gold shortage which
1511 * we are trying to fill with taxes. Consider gold more critical
1512 * than usually.
1513 * Smallest tax rate we can have here is
1514 * 60% -> factor (60 - 40) / 14.0 = 1.43 */
1515 want += prod[O_GOLD] * adv->gold_priority
1516 * (pplayer->economic.tax - 40) / 14.0;
1517 } else {
1518 want += prod[O_GOLD] * adv->gold_priority;
1519 }
1520
1521 return want;
1522}
1523
1524/**********************************************************************/
1528static adv_want base_want(struct ai_type *ait, struct player *pplayer,
1529 struct city *pcity, struct impr_type *pimprove)
1530{
1531 struct adv_data *adv = adv_data_get(pplayer, nullptr);
1532 adv_want final_want = 0;
1535
1536 if (adv->impr_calc[improvement_index(pimprove)] == ADV_IMPR_ESTIMATE) {
1537 return 0; /* Nothing to calculate here. */
1538 }
1539
1541 || (is_small_wonder(pimprove)
1542 && city_from_small_wonder(pplayer, pimprove) != nullptr)) {
1543 return 0;
1544 }
1545
1546 if (is_wonder(pimprove)) {
1547 if (is_great_wonder(pimprove)) {
1550 }
1551 wonder_city_id = pplayer->wonders[improvement_index(pimprove)];
1552 }
1553 /* Add the improvement */
1554 city_add_improvement(pcity, pimprove);
1555
1556 /* Stir, then compare notes */
1558 adv->impr_range[improvement_index(pimprove)], acity) {
1559 final_want += dai_city_want(pplayer, acity, adv, pimprove)
1560 - def_ai_city_data(acity, ait)->worth;
1562
1563 /* Restore */
1564 city_remove_improvement(pcity, pimprove);
1565 if (is_wonder(pimprove)) {
1566 if (is_great_wonder(pimprove)) {
1569 }
1570
1571 pplayer->wonders[improvement_index(pimprove)] = wonder_city_id;
1572 }
1573
1574 return final_want;
1575}
1576
1577/**********************************************************************/
1607 struct player *pplayer,
1608 struct city *pcity,
1609 struct impr_type *pimprove,
1610 const bool already)
1611{
1612 adv_want v = 0;
1614 int nplayers = normal_player_count();
1615 struct adv_data *ai = adv_data_get(pplayer, nullptr);
1616 bool capital = is_capital(pcity);
1617 bool can_build = TRUE;
1618 struct universal source = {
1620 .value = {.building = pimprove}
1621 };
1622 const bool is_convert = is_convert_improvement(pimprove);
1623 int turns = 9999;
1625
1626 /* FIXME: Do we really need the effects check to be made *without*
1627 * passing the city tile? */
1628 const struct req_context effect_ctxt = {
1629 .player = pplayer,
1630 .city = pcity,
1631 .building = pimprove,
1632 };
1633
1634 /* Do NOT pass building here, as the action might be about
1635 * targeting some completely different building, AND
1636 * the check to see if the action is possible before
1637 * the building is there is also ignoring the buildings.
1638 * We don't want those two results to differ for
1639 * an unrelated reason to what we are evaluating. */
1640 const struct req_context actenabler_ctxt = {
1641 .player = pplayer,
1642 .city = pcity,
1643 .tile = city_tile(pcity),
1644 };
1645
1646 /* Remove team members from the equation */
1648 if (aplayer->team
1649 && aplayer->team == pplayer->team
1650 && aplayer != pplayer) {
1651 nplayers--;
1652 }
1654
1655 if (is_convert) {
1656 /* Since coinage-like improvements contains some entirely spurious
1657 * ruleset values, we need to hard-code a sensible want.
1658 * We must otherwise handle IG_CONVERT improvements like the others,
1659 * so the AI will research techs that make it available,
1660 * for rulesets that do not provide it from the start.
1661 */
1662 if (improvement_has_flag(pimprove, IF_GOLD)) {
1663 v += TRADE_WEIGHTING / 10;
1664 } else if (improvement_has_flag(pimprove, IF_INFRA)) {
1665 v += INFRA_WEIGHTING / 10;
1666 }
1667 /* Without relevant flags, base want remains 0. */
1668 } else {
1669 /* Base want is calculated above using a more direct approach. */
1670 v += base_want(ait, pplayer, pcity, pimprove);
1671 if (v != 0) {
1673 "%s base_want is " ADV_WANT_PRINTF " (range=%d)",
1674 improvement_rule_name(pimprove),
1675 v,
1676 ai->impr_range[improvement_index(pimprove)]);
1677 }
1678 }
1679
1680 if (!is_convert) {
1681 /* Adjust by building cost */
1682 /* FIXME: ought to reduce by upkeep cost and amortise by building cost */
1683 v -= (impr_build_shield_cost(pcity, pimprove)
1684 / (pcity->surplus[O_SHIELD] * 10 + 1));
1685 }
1686
1687 /* Find number of cities per range. */
1689 /* kludge -- Number of *our* cities in these ranges. */
1692
1693 if (place < 0) {
1695 } else {
1697 }
1698
1699 /* All the trade partners and the city being considered. */
1701
1703
1704 /* Invalid building range */
1706 = cities[REQ_RANGE_TILE] = 0;
1707
1709 int potential = (aplayer->server.bulbs_last_turn
1710 + city_list_size(aplayer->cities) + 1);
1711
1712 if (potential > 0) {
1714 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1715 turns = MIN(turns,
1717 advance_number(pobs->source.value.advance))
1718 / (potential + 1));
1719 }
1721 }
1723
1726 bool active = TRUE;
1727 int n_needed_techs = 0;
1729 bool present = TRUE;
1730 bool impossible_to_get = FALSE;
1731
1733
1735 /* Check if all the requirements for the currently evaluated effect
1736 * are met, except for having the building that we are evaluating. */
1738 if (preq->range < range) {
1739 /* More limited range */
1740 range = preq->range;
1741 }
1742 present = preq->present;
1743 continue;
1744 }
1745
1746 if (!is_req_active(&effect_ctxt, nullptr, preq, RPT_POSSIBLE)) {
1747 active = FALSE;
1748 if (VUT_ADVANCE == preq->source.kind && preq->present) {
1749 /* This missing requirement is a missing tech requirement.
1750 * This will be for some additional effect
1751 * (For example, in the default ruleset, Mysticism increases
1752 * the effect of Temples). */
1753 tech_vector_append(&needed_techs, preq->source.value.advance);
1754 } else if (!dai_can_requirement_be_met_in_city(preq, pplayer, pcity)) {
1756 }
1757 }
1759
1761 if ((active || n_needed_techs) && !impossible_to_get) {
1762 adv_want v1 = dai_effect_value(pplayer, ai, pcity, capital,
1763 turns, peffect, range, nplayers);
1764 /* v1 could be negative (the effect could be undesirable),
1765 * although it is usually positive.
1766 * For example, in the default ruleset, Communism decreases the
1767 * effectiveness of a Cathedral. */
1768
1769 if (!present) {
1770 /* Building removes the effect */
1771 /* Currently v1 is (v + delta). Make it (v - delta) instead */
1772 v1 = -v1;
1773 }
1774
1775 if (active) {
1776 v += v1;
1777 } else if (v1 > 0) {
1778 /* If value of the effect is negative, do not hold it against
1779 * the tech - having the tech won't force one to build the
1780 * building. */
1781
1782 /* We might want the technology that will enable this
1783 * (additional) effect.
1784 * The better the effect, the more we want the technology.
1785 * We are more interested in (additional) effects that enhance
1786 * buildings we already have.
1787 */
1788 const int a = already? 5: 4; /* WAG */
1789 const adv_want dv = v1 * a / (4 * n_needed_techs);
1790
1791 want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1792 &needed_techs, dv);
1793 }
1794 }
1795
1798
1799 /* Can the city be the target of an action? */
1800 action_iterate (act_id) {
1801 bool is_possible;
1802 bool will_be_possible = FALSE;
1803 enum req_range max_range;
1804 int act_neg_util;
1805
1806 /* Is the action relevant? */
1807 if (action_id_get_target_kind(act_id) != ATK_CITY) {
1808 continue;
1809 }
1810
1811 /* No range found yet. Local is the most narrow range. */
1813
1814 /* Is it possible to do the action to the city right now?
1815 *
1816 * (DiplRel requirements are ignored since actor_player is nullptr) */
1817 is_possible = is_action_possible_on_city(act_id, nullptr, pcity);
1818
1819 /* Will it be possible to do the action to the city if the building is
1820 * built? */
1822 enabler) {
1823 bool active = TRUE;
1825
1826 requirement_vector_iterate(&(enabler->target_reqs), preq) {
1828 /* Pretend the building is there */
1829 if (preq->present) {
1830 range = preq->range; /* Assumption: Max one pr vector */
1831 continue;
1832 } else {
1833 active = FALSE;
1834 break;
1835 }
1836 } else if (!is_req_active(&actenabler_ctxt, nullptr,
1837 preq, RPT_POSSIBLE)) {
1838 active = FALSE;
1839 break;
1840 }
1842
1843 if (active) {
1845
1846 /* Store the widest range that enables the action. */
1847 if (max_range < range) {
1848 max_range = range;
1849 }
1850
1851 /* Don't break the iteration even if the action is enabled. There
1852 * could be a wider range in an active action enabler not yet seen.
1853 */
1854 }
1856
1857 /* Will the building significantly change the ability to target
1858 * the city? */
1860 continue;
1861 }
1862
1863 /* How undesirable is it that the city may be a target? */
1865
1866 /* Multiply the desire by number of cities in range.
1867 * Note: This is a simplification. If the action can be done or not
1868 * _may_ be uncanged or changed in the opposite direction in the other
1869 * cities in the range. */
1871
1872 /* Consider the utility of being a potential target.
1873 * Remember: act_util is the negative utility of being a target. */
1874 if (will_be_possible) {
1875 v -= act_neg_util;
1876 } else {
1877 v += act_neg_util;
1878 }
1880
1881 if (already) {
1882 /* Discourage research of the technology that would make this building
1883 * obsolete. The bigger the desire for this building, the more
1884 * we want to discourage the technology. */
1885 dont_want_tech_obsoleting_impr(ait, pplayer, pcity, pimprove, v);
1886 } else {
1887 /* Increase the want for technologies that will enable
1888 * construction of this improvement, if necessary.
1889 */
1890 const bool all_met
1891 = adjust_wants_for_reqs(ait, pplayer, pcity, pimprove, v);
1892
1894 }
1895
1896 if (is_convert && can_build) {
1897 /* Could have a negative want for coinage-like improvements,
1898 * if we have some stock in a building already. */
1899 pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1900 } else if (!already && can_build) {
1901 const struct research *presearch = research_get(pplayer);
1902
1903 /* Convert the base 'want' into a building want
1904 * by applying various adjustments */
1905
1906 /* Would it mean losing shields? */
1907 if ((VUT_UTYPE == pcity->production.kind
1908 || (is_wonder(pcity->production.value.building)
1909 && !is_wonder(pimprove))
1910 || (!is_wonder(pcity->production.value.building)
1911 && is_wonder(pimprove)))
1912 && pcity->turn_last_built != game.info.turn) {
1913 if (has_handicap(pplayer, H_PRODCHGPEN)) {
1914 v -= pcity->shield_stock * SHIELD_WEIGHTING / 4;
1915 } else {
1916 v -= pcity->shield_stock * SHIELD_WEIGHTING / 15;
1917 }
1918 }
1919
1920 /* Reduce want if building gets obsoleted soon */
1922 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1924 advance_number(pobs->source.value.advance));
1925
1926 v -= v / MAX(1, num_tech);
1927 }
1929
1930 /* Are we wonder city? Try to avoid building non-wonders very much. */
1931 if (pcity->id == ai->wonder_city && !is_wonder(pimprove)) {
1932 v /= 5;
1933 }
1934
1935 /* Set */
1936 pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1937 }
1938 /* Else we either have the improvement already,
1939 * or we can not build it (yet) */
1940}
1941
1942/**********************************************************************/
1951static bool should_force_recalc(struct city *pcity)
1952{
1954 || (VUT_IMPROVEMENT == pcity->production.kind
1955 && !is_convert_improvement(pcity->production.value.building)
1957 (pcity, pcity->production.value.building));
1958}
1959
1960/**********************************************************************/
1964void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
1965{
1966 struct adv_data *ai = adv_data_get(pplayer, nullptr);
1967
1968 /* Find current worth of cities and cache this. */
1969 city_list_iterate(pplayer->cities, pcity) {
1970 def_ai_city_data(pcity, ait)->worth = dai_city_want(pplayer, pcity, ai,
1971 nullptr);
1973}
1974
1975/**********************************************************************/
1981void dai_build_adv_adjust(struct ai_type *ait, struct player *pplayer,
1982 struct city *wonder_city)
1983{
1984 /* Clear old building wants.
1985 * Do this separately from the iteration over improvement types
1986 * because each iteration could actually update more than one improvement,
1987 * if improvements have improvements as requirements.
1988 */
1989 city_list_iterate(pplayer->cities, pcity) {
1990 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1991
1992 if (city_data->building_turn <= game.info.turn) {
1993 /* Do a scheduled recalculation this turn */
1994 improvement_iterate(pimprove) {
1995 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1997 } else if (should_force_recalc(pcity)) {
1998 /* Do an emergency recalculation this turn. */
1999 city_data->building_wait = city_data->building_turn
2000 - game.info.turn;
2001 city_data->building_turn = game.info.turn;
2002
2003 improvement_iterate(pimprove) {
2004 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
2006 }
2008
2009 improvement_iterate(pimprove) {
2010 if (can_player_build_improvement_later(pplayer, pimprove)) {
2011 city_list_iterate(pplayer->cities, pcity) {
2012 struct ai_city *city_data = def_ai_city_data(pcity, ait);
2013
2014 if (pcity != wonder_city && is_wonder(pimprove)) {
2015 /* Only wonder city should build wonders! */
2016 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
2017 } else if (city_data->building_turn <= game.info.turn) {
2018 /* Building wants vary relatively slowly, so not worthwhile
2019 * recalculating them every turn.
2020 * We DO want to calculate (tech) wants because of buildings
2021 * we already have. */
2022 const bool already = city_has_building(pcity, pimprove);
2023 int idx = improvement_index(pimprove);
2024
2026 pimprove, already);
2027
2029 && 0 < pcity->server.adv->building_want[idx]));
2030
2031 if (is_great_wonder(pimprove)) {
2032 /* Not only would we get the wonder, but we would also prevent
2033 * opponents from getting it. */
2034 pcity->server.adv->building_want[idx] *= 1.5;
2035
2036 if (pcity->production.kind == VUT_IMPROVEMENT
2037 && is_great_wonder(pcity->production.value.building)) {
2038 /* If we already are building a great wonder, prefer continuing
2039 * to do so over stopping it */
2040 pcity->server.adv->building_want[idx] *= 1.25;
2041 }
2042 }
2043
2044 /* If I am not an expansionist, I want buildings more than units */
2045 if (pcity->server.adv->building_want[idx] > 0) {
2046 pcity->server.adv->building_want[idx]
2047 = pcity->server.adv->building_want[idx]
2050 }
2051 }
2052 /* else wait until a later turn */
2054 } else {
2055 /* An impossible improvement */
2056 city_list_iterate(pplayer->cities, pcity) {
2057 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
2059 }
2061
2062#ifdef FREECIV_DEBUG
2063 /* This logging is relatively expensive, so activate only if necessary */
2064 city_list_iterate(pplayer->cities, pcity) {
2065 improvement_iterate(pimprove) {
2066 if (pcity->server.adv->building_want[improvement_index(pimprove)] != 0) {
2067 CITY_LOG(LOG_DEBUG, pcity, "want to build %s with " ADV_WANT_PRINTF,
2068 improvement_rule_name(pimprove),
2069 pcity->server.adv->building_want[improvement_index(pimprove)]);
2070 }
2073#endif /* FREECIV_DEBUG */
2074
2075 /* Reset recalc counter */
2076 city_list_iterate(pplayer->cities, pcity) {
2077 struct ai_city *city_data = def_ai_city_data(pcity, ait);
2078
2079 if (city_data->building_turn <= game.info.turn) {
2080 /* This will spread recalcs out so that no one turn end is
2081 * much longer than others */
2082 city_data->building_wait
2084 city_data->building_turn
2085 = game.info.turn + city_data->building_wait;
2086 }
2088}
2089
2090/**********************************************************************/
2093void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity,
2094 bool *result)
2095{
2096 if (def_ai_city_data(pcity, ait)->grave_danger > 0) {
2097 *result = FALSE;
2098 } else {
2099 *result = TRUE;
2100 }
2101}
2102
2103/**********************************************************************/
2107static bool dai_cant_help_req(const struct req_context *context,
2108 const struct requirement *req)
2109{
2110 switch (req->source.kind) {
2111 /* Unskilled in channel digging and merchantry */
2112 case VUT_TERRAIN:
2113 case VUT_EXTRA:
2114 case VUT_GOOD:
2115 case VUT_TERRAINCLASS:
2116 case VUT_TERRFLAG:
2117 case VUT_TERRAINALTER:
2118 case VUT_CITYTILE:
2119 return !is_req_active(context, nullptr, req, RPT_POSSIBLE);
2120 default:
2121 return is_req_preventing(context, nullptr, req, RPT_POSSIBLE) > REQUCH_NO;
2122 }
2123}
2124
2125/**********************************************************************/
2129 const struct impr_type *pimprove)
2130{
2131 const struct req_context city_ctxt = {
2133 .city = pcity,
2134 .tile = city_tile(pcity),
2135 };
2136
2137 /* FIXME: AI may be too stupid to sell obsoleting improvements
2138 * from the city that are _not_ checked here. */
2140 pimprove)) {
2141 return FALSE;
2142 }
2143 /* Check for requirements that aren't met and that are unchanging
2144 * (so they can never be met). */
2145 requirement_vector_iterate(&pimprove->reqs, preq) {
2147 return FALSE;
2148 }
2150
2151 return TRUE;
2152}
2153
2154/**********************************************************************/
2157bool
2159 const struct impr_type *pimprove)
2160{
2161 const struct req_context context = { .player = p };
2162
2163 if (!valid_improvement(pimprove)) {
2164 return FALSE;
2165 }
2166 if (improvement_obsolete(p, pimprove, nullptr)) {
2167 return FALSE;
2168 }
2169 if (is_great_wonder(pimprove) && !great_wonder_is_available(pimprove)) {
2170 /* Can't build wonder if already built */
2171 return FALSE;
2172 }
2173
2174 /* Check for requirements that aren't met and that are unchanging
2175 * (so they can never be met). */
2176 requirement_vector_iterate(&pimprove->reqs, preq) {
2177 if (preq->range >= REQ_RANGE_PLAYER
2179 return FALSE;
2180 }
2182 /* FIXME: should check some "unchanging" reqs here - like if there's
2183 * a nation requirement, we can go ahead and check it now. */
2184
2185 return TRUE;
2186}
2187
2188/**********************************************************************/
2195 enum effect_type effect_type,
2196 const struct unit_type *utype)
2197{
2198 int greatest_value = 0;
2199 const struct impr_type *best_building = nullptr;
2200 const struct req_context context = {
2202 .city = pcity,
2203 .tile = city_tile(pcity),
2204 .unittype = utype,
2205 };
2206
2207 effect_list_iterate(get_effects(effect_type), peffect) {
2208 if (peffect->value > greatest_value) {
2209 const struct impr_type *building = nullptr;
2210 bool wrong_unit = FALSE;
2211
2213 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2214 building = preq->source.value.building;
2215
2217 || !is_improvement(building)) {
2218 building = nullptr;
2219 break;
2220 }
2221 } else if (VUT_IMPR_FLAG == preq->source.kind && preq->present) {
2222 /* TODO: Ruleset cache for buildings with specific flag */
2223 improvement_iterate(impr) {
2224 if (improvement_has_flag(impr, preq->source.value.impr_flag)) {
2226 && is_improvement(impr)) {
2227 if (building == nullptr) {
2228 building = impr;
2229 break;
2230 }
2231 }
2232 }
2234
2235 if (building == nullptr) {
2236 break;
2237 }
2238 } else if (utype != nullptr
2239 && !is_req_active(&context, nullptr, preq, RPT_POSSIBLE)) {
2240 /* Effect requires other kind of unit than what we are
2241 * interested about */
2242 wrong_unit = TRUE;
2243 break;
2244 }
2246 if (!wrong_unit && building != nullptr) {
2247 best_building = building;
2248 greatest_value = peffect->value;
2249 }
2250 }
2252
2253 if (best_building) {
2255 }
2256
2257 return B_LAST;
2258}
bool is_action_possible_on_city(action_id act_id, const struct player *actor_player, const struct city *target_city)
Definition actions.c:5706
void action_array_add_all_by_result(action_id *act_array, int *position, enum action_result result)
Definition actions.c:6010
void action_array_end(action_id *act_array, int size)
Definition actions.c:5993
bool action_ever_possible(action_id action)
Definition actions.c:7489
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:3092
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:3482
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:606
@ 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 **path, struct workermap *state)
bool base_city_celebrating(const struct city *pcity)
Definition city.c:1637
bool is_free_worked(const struct city *pcity, const struct tile *ptile)
Definition city.c:3641
int city_granary_size(int city_size)
Definition city.c:2132
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Definition city.c:3657
bool city_built_last_turn(const struct city *pcity)
Definition city.c:2279
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown)
Definition city.c:3253
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1240
bool is_capital(const struct city *pcity)
Definition city.c:1579
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3411
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1255
void add_tax_income(const struct player *pplayer, int trade, int *output)
Definition city.c:2245
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:854
int get_city_tithes_bonus(const struct city *pcity)
Definition city.c:2227
bool city_unhappy(const struct city *pcity)
Definition city.c:1626
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Definition city.c:2212
int city_pollution(const struct city *pcity, int shield_total)
Definition city.c:2821
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3397
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:2364
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1283
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:947
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
@ CITIZEN_ANGRY
Definition city.h:269
@ CITIZEN_UNHAPPY
Definition city.h:268
#define output_type_iterate(output)
Definition city.h:846
#define city_owner(_pcity_)
Definition city.h:563
#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:835
#define city_built_iterate_end
Definition city.h:841
#define output_type_iterate_end
Definition city.h:852
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:892
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:366
bool city_refresh(struct city *pcity)
Definition cityturn.c:158
char * incite_cost
Definition comments.c:76
static void try_to_sell_stuff(struct player *pplayer, struct city *pcity)
Definition daicity.c:362
#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:1363
static void unit_do_disband_trad(struct player *owner, struct unit *punit, const enum action_requester requester)
Definition daicity.c:461
static bool dai_cant_help_req(const struct req_context *context, const struct requirement *req)
Definition daicity.c:2107
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:378
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:1160
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:1188
#define LOG_BUY
Definition daicity.c:68
static void dai_city_sell_noncritical(struct city *pcity, bool redundant_only)
Definition daicity.c:1008
#define city_range_iterate_end
Definition daicity.c:103
static int unit_food_upkeep(struct unit *punit)
Definition daicity.c:705
void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:868
void dai_city_alloc(struct ai_type *ait, struct city *pcity)
Definition daicity.c:1133
#define LOG_EMERGENCY
Definition daicity.c:69
void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity, bool *result)
Definition daicity.c:2093
static bool should_force_recalc(struct city *pcity)
Definition daicity.c:1951
bool dai_can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
Definition daicity.c:2158
static int action_target_neg_util(action_id act_id, const struct city *pcity)
Definition daicity.c:1230
#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:1051
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:1981
static void contemplate_terrain_improvements(struct ai_type *ait, struct city *pcity)
Definition daicity.c:763
static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:533
#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:1606
void dai_city_free(struct ai_type *ait, struct city *pcity)
Definition daicity.c:1146
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:2194
void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:1964
static void dai_upgrade_units(struct city *pcity, int limit, bool military)
Definition daicity.c:388
#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:2128
adv_want dai_city_want(struct player *pplayer, struct city *acity, struct adv_data *adv, struct impr_type *pimprove)
Definition daicity.c:1450
static adv_want base_want(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove)
Definition daicity.c:1528
static int unit_foodbox_cost(struct unit *punit)
Definition daicity.c:724
static bool building_crucial(const struct player *plr, struct impr_type *pimprove, const struct city *pcity)
Definition daicity.c:985
static void dai_city_choose_build(struct ai_type *ait, struct player *pplayer, struct city *pcity)
Definition daicity.c:254
#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:54
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition daiplayer.h:42
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition daiplayer.h:48
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:1329
#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:3464
#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:1048
int Impr_type_id
Definition fc_types.h:236
@ RPT_POSSIBLE
Definition fc_types.h:514
int action_id
Definition fc_types.h:249
#define ACTRES_NONE
Definition fc_types.h:187
#define ADV_WANT_PRINTF
Definition fc_types.h:1049
@ O_SHIELD
Definition fc_types.h:102
@ O_FOOD
Definition fc_types.h:102
@ O_TRADE
Definition fc_types.h:102
@ O_SCIENCE
Definition fc_types.h:102
@ O_LUXURY
Definition fc_types.h:102
@ O_GOLD
Definition fc_types.h:102
@ O_LAST
Definition fc_types.h:102
signed short Continent_id
Definition fc_types.h:232
#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:1294
const char * player_name(const struct player *pplayer)
Definition player.c:900
#define players_iterate_end
Definition player.h:542
#define players_iterate(_pplayer)
Definition player.h:537
static bool is_barbarian(const struct player *pplayer)
Definition player.h:491
int normal_player_count(void)
Definition plrhand.c:3209
#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
struct ai_plr::@282 stats
adv_want tech_want[A_LAST+1]
Definition daidata.h:104
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:593
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:89
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:98
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:535
bool unit_is_alive(int id)
Definition unit.c:2317
int unit_upkeep_cost(const struct unit *punit, Output_type_id otype)
Definition unit.c:2994
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1582
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:1688
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:657
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1793
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1311
#define unit_tile(_pu)
Definition unit.h:407
#define unit_owner(_pu)
Definition unit.h:406
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:3369
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:3349
#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:2298
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:2997
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2279
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1613
int num_role_units(int role)
Definition unittype.c:2229
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1729
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:1757