Freeciv-3.3
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 "aihand.h"
57#include "daidata.h"
58#include "daidiplomacy.h"
59#include "daidomestic.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, bool redundant_only);
113static void resolve_city_emergency(struct ai_type *ait, struct player *pplayer,
114 struct city *pcity);
115
116/**********************************************************************/
129 struct player *pplayer,
130 const struct city *pcity,
131 const struct impr_type *pimprove,
132 const struct advance *tech,
133 adv_want building_want)
134{
135 /* The conversion factor was determined by experiment,
136 * and might need adjustment. See also dai_tech_effect_values()
137 */
138 const adv_want tech_want = building_want * def_ai_city_data(pcity, ait)->building_wait
139 * 14 / 8;
140#if 0
141 /* This logging is relatively expensive,
142 * so activate it only while necessary. */
143 TECH_LOG(LOG_DEBUG, pplayer, tech,
144 "wanted by %s for building: %d -> %d",
146 building_want, tech_want);
147#endif /* 0 */
148 if (tech) {
149 def_ai_player_data(pplayer, ait)->tech_want[advance_index(tech)] += tech_want;
150 }
151}
152
153/**********************************************************************/
158 struct player *pplayer,
159 const struct city *pcity,
160 const struct impr_type *pimprove,
162 adv_want building_want)
163{
164 int t;
166
167 for (t = 0; t < n_needed_techs; t++) {
168 want_tech_for_improvement_effect(ait, pplayer, pcity, pimprove,
170 building_want);
171 }
172}
173
174/**********************************************************************/
179 struct player *pplayer,
180 const struct city *pcity,
181 const struct impr_type *pimprove,
182 adv_want building_want)
183{
185 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
186 want_tech_for_improvement_effect(ait, pplayer, pcity, pimprove,
187 pobs->source.value.advance,
188 -building_want);
189 }
191}
192
193/**********************************************************************/
201static void dai_barbarian_choose_build(struct player *pplayer,
202 struct city *pcity,
203 struct adv_choice *choice)
204{
205 struct unit_type *bestunit = NULL;
206 int i, bestattack = 0;
207 const struct civ_map *nmap = &(wld.map);
208
209 /* Choose the best unit among the basic ones */
210 for (i = 0; i < num_role_units(L_BARBARIAN_BUILD); i++) {
212
213 if (iunit->attack_strength > bestattack
215 bestunit = iunit;
216 bestattack = iunit->attack_strength;
217 }
218 }
219
220 /* Choose among those made available through other civ's research */
221 for (i = 0; i < num_role_units(L_BARBARIAN_BUILD_TECH); i++) {
223
224 if (iunit->attack_strength > bestattack
226 bestunit = iunit;
227 bestattack = iunit->attack_strength;
228 }
229 }
230
231 /* If found anything, put it into the choice */
232 if (bestunit) {
233 choice->value.utype = bestunit;
234 /* "Overriding military emergency" indicator */
236 choice->type = CT_ATTACKER;
237 adv_choice_set_use(choice, "barbarian");
238 } else {
239 log_base(LOG_WANT, "Barbarians don't know what to build!");
240 }
241}
242
243/**********************************************************************/
251static void dai_city_choose_build(struct ai_type *ait, struct player *pplayer,
252 struct city *pcity)
253{
254 struct adv_choice *newchoice;
255 struct adv_data *adv = adv_data_get(pplayer, NULL);
256 struct ai_city *city_data = def_ai_city_data(pcity, ait);
257 const struct civ_map *nmap = &(wld.map);
258
259 if (has_handicap(pplayer, H_AWAY)
261 && city_data->urgency == 0) {
262 /* Don't change existing productions unless we have to. */
263 return;
264 }
265
266 if (is_barbarian(pplayer)) {
267 dai_barbarian_choose_build(pplayer, pcity, &(city_data->choice));
268 } else {
269 if ((city_data->choice.want < DAI_WANT_MILITARY_EMERGENCY
270 || city_data->urgency == 0)
271 && !(dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0
272 && pcity->id != adv->wonder_city)) {
276 }
277 }
278
279 /* Fallbacks */
280 if (city_data->choice.want == 0) {
281 /* Fallbacks do happen with techlevel 0, which is now default. -- Per */
282 CITY_LOG(LOG_WANT, pcity, "Falling back - didn't want to build soldiers,"
283 " workers, caravans, settlers, or buildings!");
284 city_data->choice.want = 1;
286 city_data->choice.value.utype
288 city_data->choice.type = CT_CIVILIAN;
289 adv_choice_set_use(&(city_data->choice), "fallback trade route");
290 } else {
291 unsigned int our_def = assess_defense_quadratic(ait, pcity);
292
293 if (our_def == 0
294 && dai_process_defender_want(ait, nmap, pplayer, pcity, 1,
295 &(city_data->choice), 0)) {
296 adv_choice_set_use(&(city_data->choice), "fallback defender");
297 CITY_LOG(LOG_DEBUG, pcity, "Building fallback defender");
298 } else if (best_role_unit(pcity, UTYF_WORKERS)) {
299 city_data->choice.value.utype
301 city_data->choice.type = CT_CIVILIAN;
302 adv_choice_set_use(&(city_data->choice), "fallback worker");
303 } else {
304 CITY_LOG(LOG_ERROR, pcity, "Cannot even build a fallback "
305 "(caravan/coinage/settlers). Fix the ruleset!");
306 city_data->choice.want = 0;
307 }
308 }
309 }
310
311 if (city_data->choice.want != 0) {
312 struct universal build_new;
313
315
316#ifdef ADV_CHOICE_TRACK
317 if (city_data->choice.log_if_chosen) {
318 log_normal("%s wants %s for %s with desire " ADV_WANT_PRINTF ".",
321 city_data->choice.use,
322 city_data->choice.want);
323 }
324#endif /* ADV_CHOICE_TRACK */
325
326 CITY_LOG(LOG_DEBUG, pcity, "wants %s with desire " ADV_WANT_PRINTF ".",
328 city_data->choice.want);
329
330 switch (city_data->choice.type) {
331 case CT_CIVILIAN:
332 case CT_ATTACKER:
333 case CT_DEFENDER:
334 build_new.kind = VUT_UTYPE;
335 build_new.value.utype = city_data->choice.value.utype;
336 break;
337 case CT_BUILDING:
339 build_new.value.building = city_data->choice.value.building;
340 break;
341 case CT_NONE:
342 build_new.kind = VUT_NONE;
343 break;
344 case CT_LAST:
346 break;
347 };
348
350 }
351}
352
353/**********************************************************************/
356static void try_to_sell_stuff(struct player *pplayer, struct city *pcity)
357{
358 improvement_iterate(pimprove) {
359 if (can_city_sell_building(pcity, pimprove)
360 && !building_has_effect(pimprove, EFT_DEFEND_BONUS)) {
361 /* selling walls to buy defenders is counterproductive -- Syela */
362 really_handle_city_sell(pplayer, pcity, pimprove);
363 break;
364 }
366}
367
368/**********************************************************************/
372static void increase_maxbuycost(struct player *pplayer, int new_value)
373{
375}
376
377/**********************************************************************/
382static void dai_upgrade_units(struct city *pcity, int limit, bool military)
383{
385 struct player *pplayer = city_owner(pcity);
386 int expenses;
387
388 {
389 /* Find upgrade unit actions */
390 int i = 0;
393 }
394
395 dai_calc_data(pplayer, NULL, &expenses, NULL);
396
398 if (pcity->owner == punit->owner) {
399 /* Only upgrade units you own, not allied ones */
400
401 const struct unit_type *old_type = unit_type_get(punit);
402 const struct unit_type *punittype = can_upgrade_unittype(pplayer, old_type);
403
404 if (military && !IS_ATTACKER(old_type)) {
405 /* Only upgrade military units this round */
406 continue;
407 } else if (!military && IS_ATTACKER(old_type)) {
408 /* Only civilians or transports this round */
409 continue;
410 }
411
412 if (punittype == NULL) {
413 continue;
414 }
415
417 if (action_ever_possible(act_id)) {
418 const struct action *paction = action_by_number(act_id);
419 int cost = unit_upgrade_price(pplayer, old_type, punittype);
420 int real_limit = limit;
421
422 /* Sinking Triremes are DANGEROUS!! We'll do anything to upgrade 'em. */
423 /* FIXME: This assumes rules to be quite close to civ/2.
424 * Of the supplied rulesets those are the only ones with
425 * UTYF_COAST unit, but... */
428 }
429 if (pplayer->economic.gold - cost > real_limit) {
430 CITY_LOG(LOG_BUY, pcity, "Upgraded %s to %s for %d (%s)",
433 cost,
434 military ? "military" : "civilian");
436 pcity->id, 0, "",
437 paction->id);
438 } else {
439 increase_maxbuycost(pplayer, cost);
440 }
441 }
443 }
445}
446
447/**********************************************************************/
453static void unit_do_disband_trad(struct player *owner, struct unit *punit,
454 const enum action_requester requester)
455{
456 const int punit_id_stored = punit->id;
457 const struct civ_map *nmap = &(wld.map);
458
460
461 /* Help Wonder gives 100% of the shields used to produce the unit to the
462 * city where it is located. */
464 struct city *tgt_city;
465
466 /* Only a city at the same tile as the unit can benefit. */
468
469 if (tgt_city
471 punit, tgt_city)) {
474 /* No shields wasted. The unit did Help Wonder. */
475 return;
476 }
477
479 /* The unit is gone. Maybe it was killed in Lua? */
480 return;
481 }
482 }
483 }
484
485 /* Disbanding a unit inside a city gives it 50% of the shields used to
486 * produce the unit. */
488 struct city *tgt_city;
489
490 /* Only a city at the same tile as the unit can benefit. */
492
493 if (tgt_city
495 punit, tgt_city)) {
498 /* The unit did Disband Unit Recover. 50% of the shields wasted. */
499 return;
500 }
501
503 /* The unit is gone. Maybe it was killed in Lua? */
504 return;
505 }
506 }
507 }
508
509 /* Try to disband even if all shields will be wasted. */
514 /* All shields wasted. The unit did Disband Unit. */
515 return;
516 }
517 }
518 }
519}
520
521/**********************************************************************/
524static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
525{
526 struct adv_choice bestchoice;
527 int cached_limit = dai_gold_reserve(pplayer);
528 int expenses;
529 bool war_footing = dai_on_war_footing(ait, pplayer);
530
531 /* Disband explorers that are at home but don't serve a purpose.
532 * FIXME: This is a hack, and should be removed once we
533 * learn how to ferry explorers to new land. */
534 city_list_iterate(pplayer->cities, pcity) {
535 struct tile *ptile = pcity->tile;
538 && pcity->id == punit->homecity
539 && def_ai_city_data(pcity, ait)->urgency == 0) {
540 CITY_LOG(LOG_BUY, pcity, "disbanding %s to increase production",
543 }
546
547 dai_calc_data(pplayer, NULL, &expenses, NULL);
548
549 do {
550 bool expensive; /* don't buy when it costs x2 unless we must */
551 int buycost;
552 int limit = cached_limit; /* cached_limit is our gold reserve */
553 struct city *pcity = NULL;
554 struct ai_city *city_data;
555
556 /* Find highest wanted item on the buy list */
558 city_list_iterate(pplayer->cities, acity) {
560
561 if (acity_data->choice.want
562 > bestchoice.want && ai_fuzzy(pplayer, TRUE)) {
564 pcity = acity;
565 }
567
568 /* We found nothing, so we're done */
569 if (pcity == NULL) {
570 break;
571 }
572
574
575 /* Not dealing with this city a second time */
576 city_data->choice.want = -1;
577
579
580 /* Try upgrade units at danger location (high want is usually danger) */
581 if (city_data->urgency > 1) {
582 if (bestchoice.type == CT_BUILDING
583 && is_wonder(bestchoice.value.building)) {
584 CITY_LOG(LOG_BUY, pcity, "Wonder being built in dangerous position!");
585 } else {
586 /* If we have urgent want, spend more */
587 int upgrade_limit = limit;
588
589 if (city_data->urgency > 1) {
591 }
592 /* Upgrade only military units now */
594 }
595 }
596
597 if (pcity->anarchy != 0 && bestchoice.type != CT_BUILDING) {
598 continue; /* Nothing we can do */
599 }
600
601 /* Cost to complete production */
603
604 if (buycost <= 0) {
605 continue; /* Already completed */
606 }
607
609 && utype_is_cityfounder(bestchoice.value.utype)) {
611 && bestchoice.value.utype->pop_cost > 0
612 && city_size_get(pcity) <= bestchoice.value.utype->pop_cost) {
613 /* Don't buy settlers in cities that cannot afford the population cost. */
614 /* This used to check also if city is about to grow to required size
615 * next turn and allow buying of settlers in that case, but current
616 * order of end/start turn activities is such that settler building
617 * fails already before city grows. */
618 continue;
619 } else if (city_list_size(pplayer->cities) > 6) {
620 /* Don't waste precious money buying settlers late game
621 * since this raises taxes, and we want science. Adjust this
622 * again when our tax algorithm is smarter. */
623 continue;
624 } else if (war_footing) {
625 continue;
626 }
627 } else {
628 /* We are not a settler. Therefore we increase the cash need we
629 * balance our buy desire with to keep cash at hand for emergencies
630 * and for upgrades */
631 limit *= 2;
632 }
633
634 /* It costs x2 to buy something with no shields contributed */
635 expensive = (pcity->shield_stock == 0)
636 || (pplayer->economic.gold - buycost < limit);
637
638 if (bestchoice.type == CT_ATTACKER
639 && buycost
641 && !war_footing) {
642 /* Too expensive for an offensive unit */
643 continue;
644 }
645
646 /* FIXME: Here Syela wanted some code to check if
647 * pcity was doomed, and we should therefore attempt
648 * to sell everything in it of non-military value */
649
650 if (pplayer->economic.gold - expenses >= buycost
651 && (!expensive
652 || (city_data->grave_danger != 0
653 && assess_defense(ait, pcity) == 0)
654 || (bestchoice.want > 200 && city_data->urgency > 1))) {
655 /* Buy stuff */
656 CITY_LOG(LOG_BUY, pcity, "Crash buy of %s for %d (want " ADV_WANT_PRINTF ")",
658 buycost,
659 bestchoice.want);
661 } else if (city_data->grave_danger != 0
662 && bestchoice.type == CT_DEFENDER
663 && assess_defense(ait, pcity) == 0) {
664 /* We have no gold but MUST have a defender */
665 CITY_LOG(LOG_BUY, pcity, "must have %s but can't afford it (%d < %d)!",
667 pplayer->economic.gold,
668 buycost);
669 try_to_sell_stuff(pplayer, pcity);
670 if (pplayer->economic.gold - expenses >= buycost) {
671 CITY_LOG(LOG_BUY, pcity, "now we can afford it (sold something)");
673 }
675 }
676 } while (TRUE);
677
678 if (!war_footing) {
679 /* Civilian upgrades now */
680 city_list_iterate(pplayer->cities, pcity) {
683 }
684
685 log_base(LOG_BUY, "%s wants to keep %d in reserve (tax factor %d)",
686 player_name(pplayer), cached_limit, pplayer->ai_common.maxbuycost);
687}
688
689/**********************************************************************/
692static int unit_food_upkeep(struct unit *punit)
693{
694 struct player *pplayer = unit_owner(punit);
695 int upkeep = utype_upkeep_cost(unit_type_get(punit), pplayer, O_FOOD);
696
697 if (punit->id != 0 && punit->homecity == 0)
698 upkeep = 0; /* thanks, Peter */
699
700 return upkeep;
701}
702
703/**********************************************************************/
711static int unit_foodbox_cost(struct unit *punit)
712{
713 int pop_cost = unit_type_get(punit)->pop_cost;
714
715 if (pop_cost <= 0) {
716 return 0;
717 }
718
719 if (punit->id == 0) {
720 /* It is a virtual unit, so must start in a city... */
721 struct city *pcity = tile_city(unit_tile(punit));
722 int size = city_size_get(pcity);
723 int cost = 0;
724 int i;
725
726 /* The default is to lose 100%. The granary bonus reduces this. */
728
729 foodloss_pct = CLIP(0, foodloss_pct, 100);
731 fc_assert(size >= pop_cost);
732
733 for (i = pop_cost; i > 0 ; i--) {
735 }
736 cost = cost * foodloss_pct / 100;
737
738 return cost;
739 }
740
741 return 30;
742}
743
744/**********************************************************************/
751 struct city *pcity)
752{
753 struct unit *virtualunit;
754 int want;
756 struct extra_type *best_target;
757 struct tile *best_tile = NULL; /* May be accessed by log_*() calls. */
758 struct tile *pcenter = city_tile(pcity);
759 struct player *pplayer = city_owner(pcity);
760 struct adv_data *adv = adv_data_get(pplayer, NULL);
761 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, NULL);
762 struct unit_type *utype;
764 struct ai_city *city_data = def_ai_city_data(pcity, ait);
765 struct dai_private_data *private = (struct dai_private_data *)ait->private;
766 const struct civ_map *nmap = &(wld.map);
767
768 if (!private->contemplace_workers) {
769 /* AI type uses custom method to set worker want and type. */
770 return;
771 }
772
773 city_data->worker_want = 0; /* Make sure old want does not stay if we don't want now */
774
776
777 if (utype == NULL) {
778 log_debug("No UTYF_WORKERS role unit available");
779 return;
780 }
781
782 /* Create a localized "virtual" unit to do operations with. */
783 virtualunit = unit_virtual_create(pplayer, pcity, utype, 0);
784 /* Advisors data space not allocated as it's not needed in the
785 lifetime of the virtualunit. */
789 &best_tile,
790 NULL, NULL);
791 if (unit_type_get(virtualunit)->pop_cost >= city_size_get(pcity)) {
792 /* We don't like disbanding the city as a side effect */
794
795 return;
796 }
797 /* We consider unit_food_upkeep with only half FOOD_WEIGHTING to
798 * balance the fact that unit can improve many tiles during its
799 * lifetime, and want is calculated for just one of them.
800 * Having full FOOD_WEIGHT here would mean that tile improvement of
801 * +1 food would give just zero want for settler. Other weights
802 * are lower, so +1 shield - unit food upkeep would be negative. */
803 want = (want - unit_food_upkeep(virtualunit) * FOOD_WEIGHTING / 2) * 100
806
807 /* Massage our desire based on available statistics to prevent
808 * overflooding with worker type units if they come cheap in
809 * the ruleset */
810 if (place >= 0) {
811 want /= MAX(1, ai->stats.workers[place] / (adv->stats.cities[place] + 1));
812 want -= ai->stats.workers[place];
813 } else {
814 want /= MAX(1, ai->stats.ocean_workers[-place] / (adv->stats.ocean_cities[-place] + 1));
815 want -= ai->stats.ocean_workers[-place];
816 }
817 want = MAX(want, 0);
818
819 if (place >= 0) {
820 CITY_LOG(LOG_DEBUG, pcity, "wants %s with want %d to do %s at (%d,%d), "
821 "we have %d workers and %d cities on the continent",
822 utype_rule_name(utype),
823 want,
826 ai->stats.workers[place],
827 adv->stats.cities[place]);
828 } else {
829 CITY_LOG(LOG_DEBUG, pcity, "wants %s with want %d to do %s at (%d,%d), "
830 "we have %d workers and %d cities on the ocean",
831 utype_rule_name(utype),
832 want,
836 adv->stats.ocean_cities[-place]);
837 }
838
839 fc_assert(want >= 0);
840
841 city_data->worker_want = want;
843 place >= 0 ? TC_LAND : TC_OCEAN);
844}
845
846/**********************************************************************/
852void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
853{
854 pplayer->ai_common.maxbuycost = 0;
855
857 city_list_iterate(pplayer->cities, pcity) {
859 || city_granary_size(city_size_get(pcity)) == pcity->food_stock) {
860 /* Having a full granary isn't an emergency, but we want to rearrange */
861 auto_arrange_workers(pcity); /* this usually helps */
862 }
863 if (CITY_EMERGENCY(pcity)) {
864 /* Fix critical shortages or unhappiness */
865 resolve_city_emergency(ait, pplayer, pcity);
866 }
868 sync_cities();
870 if (pplayer->economic.tax >= 30 /* Otherwise expect it to increase tax */
871 && player_get_expected_income(pplayer) < -(pplayer->economic.gold)) {
872 int count = city_list_size(pplayer->cities);
873 struct city *sellers[count + 1];
874 int i;
875
876 /* Randomized order */
877 count = 0;
878 city_list_iterate(pplayer->cities, pcity) {
879 sellers[count++] = pcity;
881
882 for (i = 0; i < count; i++) {
883 int replace = fc_rand(count);
884 struct city *tmp;
885
886 tmp = sellers[i];
887 sellers[i] = sellers[replace];
888 sellers[replace] = tmp;
889 }
890
891 i = 0;
892 while (player_get_expected_income(pplayer) < -(pplayer->economic.gold)
893 && i < count) {
895 }
896 }
898
900 building_advisor(pplayer);
902
903 /* Initialize the infrastructure cache, which is used shortly. */
905 city_list_iterate(pplayer->cities, pcity) {
906 struct ai_city *city_data = def_ai_city_data(pcity, ait);
907 struct adv_choice *choice;
908
909 if (city_data->choice.want <= 0) {
910 /* Note that this function mungs the seamap, but we don't care */
912 choice = military_advisor_choose_build(ait, &(wld.map), pplayer, pcity, NULL);
913 adv_choice_copy(&(city_data->choice), choice);
914 adv_free_choice(choice);
916 }
917 if (dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0) {
918 city_data->worker_want = 0;
919 city_data->founder_want = 0;
920 city_data->founder_turn = game.info.turn; /* Do not consider zero we set here
921 * valid value, if real want is needed.
922 * Recalculate immediately in such situation. */
923 continue; /* Go, soldiers! */
924 }
925 /* Will record its findings in pcity->worker_want */
929
931 if (city_data->founder_turn <= game.info.turn) {
932 /* Will record its findings in pcity->founder_want */
934 /* Avoid recalculating all the time.. */
935 /* This means AI is not very opportunistic if there happens to open up spot for
936 * a new city. */
937 city_data->founder_turn =
939 } else if (pcity->server.debug) {
940 /* recalculate every turn */
942 }
946 /* Reset auto settler state for the next run. */
947 dai_auto_settler_reset(ait, pplayer);
948
949 city_list_iterate(pplayer->cities, pcity) {
950 dai_city_choose_build(ait, pplayer, pcity);
951
952 /* Initialize for next turn */
953 def_ai_city_data(pcity, ait)->choice.want = -1;
955
956 dai_spend_gold(ait, pplayer);
957}
958
959/**********************************************************************/
966static bool building_crucial(const struct player *plr,
967 struct impr_type *pimprove,
968 const struct city *pcity)
969{
970#if 0 /* This check will become more complicated now. */
971 if (ai_wants_no_science(plr)
973 return FALSE;
974 }
975#endif
977 /* selling city walls is really, really dumb -- Syela */
978 || is_improvement_productive(pcity, pimprove)) {
979 return TRUE;
980 }
981
982 return FALSE;
983}
984
985/**********************************************************************/
989 bool redundant_only)
990{
991 struct player *pplayer = city_owner(pcity);
992
993 city_built_iterate(pcity, pimprove) {
994 if (can_city_sell_building(pcity, pimprove)
995 && !building_crucial(pplayer, pimprove, pcity)
996 && (!redundant_only || is_improvement_redundant(pcity, pimprove))) {
997 int gain = impr_sell_gold(pimprove);
998
1000 PL_("%s is selling %s for %d.",
1001 "%s is selling %s for %d.", gain),
1004 gain);
1005 do_sell_building(pplayer, pcity, pimprove, "sold");
1006
1007 return; /* max 1 building each turn */
1008 }
1010}
1011
1012/**********************************************************************/
1031static void resolve_city_emergency(struct ai_type *ait, struct player *pplayer,
1032 struct city *pcity)
1033{
1034 struct tile *pcenter = city_tile(pcity);
1035 const struct civ_map *nmap = &(wld.map);
1036
1038 "Emergency in %s (%s, angry%d, unhap%d food%d, prod%d)",
1040 city_unhappy(pcity) ? "unhappy" : "content",
1043 pcity->surplus[O_FOOD],
1044 pcity->surplus[O_SHIELD]);
1045
1047 struct city *acity = tile_worked(atile);
1048
1049 if (acity && acity != pcity && city_owner(acity) == city_owner(pcity)) {
1050 log_base(LOG_EMERGENCY, "%s taking over %s square in (%d, %d)",
1052
1053#ifndef FREECIV_NDEBUG
1054 int ax, ay;
1055#endif
1057 continue);
1058
1059 if (is_free_worked(acity, atile)) {
1060 /* Can't remove a worker here. */
1061 continue;
1062 }
1063
1065 acity->specialists[DEFAULT_SPECIALIST]++;
1067 }
1069
1071
1072 if (!CITY_EMERGENCY(pcity)) {
1073 log_base(LOG_EMERGENCY, "Emergency in %s resolved", city_name_get(pcity));
1074 goto cleanup;
1075 }
1076
1077 unit_list_iterate_safe(pcity->units_supported, punit) {
1078 if (city_unhappy(pcity)
1079 && (utype_happy_cost(unit_type_get(punit), pplayer) > 0
1081 && def_ai_unit_data(punit, ait)->passenger == 0) {
1082 UNIT_LOG(LOG_EMERGENCY, punit, "is causing unrest, disbanded");
1083 /* TODO: If Help Wonder stops blocking Disband Unit there may be
1084 * cases where Disband Unit should be selected. Example: Field unit
1085 * in allied city that is building a wonder that makes the ally win
1086 * without sharing the victory. */
1087 /* TODO: Should the unit try to find legal targets at adjacent tiles?
1088 * Should it consider other self eliminating actions than the
1089 * components of the traditional disband? */
1092 }
1094
1095 if (CITY_EMERGENCY(pcity)) {
1096 log_base(LOG_EMERGENCY, "Emergency in %s remains unresolved",
1098 } else {
1100 "Emergency in %s resolved by disbanding unit(s)",
1102 }
1103
1104 cleanup:
1106 sync_cities();
1107}
1108
1109/**********************************************************************/
1112void dai_city_alloc(struct ai_type *ait, struct city *pcity)
1113{
1114 struct ai_city *city_data = fc_calloc(1, sizeof(struct ai_city));
1115
1116 city_data->building_wait = BUILDING_WAIT_MINIMUM;
1117 adv_init_choice(&(city_data->choice));
1118
1120}
1121
1122/**********************************************************************/
1125void dai_city_free(struct ai_type *ait, struct city *pcity)
1126{
1127 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1128
1129 if (city_data != NULL) {
1130 adv_deinit_choice(&(city_data->choice));
1133 }
1134}
1135
1136/**********************************************************************/
1139void dai_city_save(struct ai_type *ait, const char *aitstr,
1140 struct section_file *file, const struct city *pcity,
1141 const char *citystr)
1142{
1143 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1144
1145 /* FIXME: remove this when the urgency is properly recalculated. */
1146 secfile_insert_int(file, city_data->urgency, "%s.%s.urgency", citystr, aitstr);
1147
1148 /* avoid fc_rand recalculations on subsequent reload. */
1149 secfile_insert_int(file, city_data->building_turn, "%s.%s.building_turn",
1150 citystr, aitstr);
1151 secfile_insert_int(file, city_data->building_wait, "%s.%s.building_wait",
1152 citystr, aitstr);
1153
1154 /* avoid fc_rand and expensive recalculations on subsequent reload. */
1155 secfile_insert_int(file, city_data->founder_turn, "%s.%s.founder_turn",
1156 citystr, aitstr);
1157 secfile_insert_int(file, city_data->founder_want, "%s.%s.founder_want",
1158 citystr, aitstr);
1159 secfile_insert_bool(file, city_data->founder_boat, "%s.%s.founder_boat",
1160 citystr, aitstr);
1161}
1162
1163/**********************************************************************/
1166void dai_city_load(struct ai_type *ait, const char *aitstr,
1167 const struct section_file *file,
1168 struct city *pcity, const char *citystr)
1169{
1170 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1171
1172 /* FIXME: remove this when the urgency is properly recalculated. */
1173 city_data->urgency
1174 = secfile_lookup_int_default(file, 0, "%s.%s.urgency", citystr, aitstr);
1175
1176 /* avoid fc_rand recalculations on subsequent reload. */
1177 city_data->building_turn
1178 = secfile_lookup_int_default(file, 0, "%s.%s.building_turn", citystr,
1179 aitstr);
1180 city_data->building_wait
1182 "%s.%s.building_wait", citystr, aitstr);
1183
1184 /* avoid fc_rand and expensive recalculations on subsequent reload. */
1185 city_data->founder_turn
1186 = secfile_lookup_int_default(file, 0, "%s.%s.founder_turn", citystr,
1187 aitstr);
1188 city_data->founder_want
1189 = secfile_lookup_int_default(file, 0, "%s.%s.founder_want", citystr,
1190 aitstr);
1191 city_data->founder_boat
1192 = secfile_lookup_bool_default(file, (city_data->founder_want < 0),
1193 "%s.%s.founder_boat", citystr, aitstr);
1194}
1195
1196/**********************************************************************/
1209 const struct city *pcity)
1210{
1211 struct action *paction = action_by_number(act_id);
1212
1214 "Action %d don't exist.", act_id);
1215
1217 /* Not city targeted in this ruleset. */
1218 return 0;
1219 }
1220
1221 switch (paction->result) {
1223 /* Copied from the evaluation of the No_Incite effect */
1224 return MAX((game.server.diplchance * 2
1227 - game.server.incite_unit_factor * 5, 0);
1228
1229 /* Really bad for the city owner. */
1230 case ACTRES_SPY_NUKE:
1232 /* The ai will never destroy their own city to keep it out of enemy
1233 * hands. If it starts supporting it this value should change. */
1235 return 20;
1236
1237 /* Bad for the city owner. */
1238 case ACTRES_SPY_POISON:
1246 case ACTRES_NUKE:
1247 /* TODO: Individual and well balanced values */
1248 return 10;
1249
1250 /* Good for an enemy */
1253 case ACTRES_STEAL_MAPS:
1254 /* TODO: Individual and well balanced values */
1255 return 8;
1256
1257 /* Could be worse */
1260 case ACTRES_MARKETPLACE:
1261 case ACTRES_SPY_ESCAPE:
1262 /* TODO: Individual and well balanced values */
1263 return 1;
1264
1265 /* Good for the city owner in most cases. */
1266 case ACTRES_TRADE_ROUTE:
1267 case ACTRES_HELP_WONDER:
1268 case ACTRES_JOIN_CITY:
1270 case ACTRES_HOME_CITY:
1272 case ACTRES_AIRLIFT:
1273 /* TODO: Individual and well balanced values */
1274 return -1;
1275
1276 /* Ruleset defined actions. We have no idea what they do. */
1277 case ACTRES_NONE:
1278 return 0;
1279
1280 /* Shouldn't happen. */
1282 /* Against the tile so potential city effects are overlooked for now. */
1286 case ACTRES_SPY_ATTACK:
1287 case ACTRES_EXPEL_UNIT:
1290 case ACTRES_BOMBARD:
1291 case ACTRES_FOUND_CITY:
1292 case ACTRES_NUKE_UNITS:
1293 case ACTRES_PARADROP:
1294 case ACTRES_ATTACK:
1295 case ACTRES_WIPE_UNITS:
1297 case ACTRES_HEAL_UNIT:
1299 case ACTRES_CULTIVATE:
1300 case ACTRES_PLANT:
1301 case ACTRES_PILLAGE:
1302 case ACTRES_CLEAN:
1303 case ACTRES_FORTIFY:
1304 case ACTRES_ROAD:
1305 case ACTRES_CONVERT:
1306 case ACTRES_BASE:
1307 case ACTRES_MINE:
1308 case ACTRES_IRRIGATE:
1316 case ACTRES_HUT_ENTER:
1318 case ACTRES_UNIT_MOVE:
1319 case ACTRES_TELEPORT:
1321 case ACTRES_HOMELESS:
1324 "Action not aimed at cities");
1325 break;
1326 }
1327
1328 /* Wrong action. Ignore it. */
1329 return 0;
1330}
1331
1332/**********************************************************************/
1341static bool adjust_wants_for_reqs(struct ai_type *ait,
1342 struct player *pplayer,
1343 struct city *pcity,
1344 const struct impr_type *pimprove,
1345 const adv_want v)
1346{
1347 bool all_met = TRUE;
1348 int n_needed_techs = 0;
1349 int n_needed_improvements = 0;
1352
1353 const struct req_context context = {
1354 .player = pplayer,
1355 .city = pcity,
1356 .tile = city_tile(pcity),
1357 .building = pimprove,
1358 };
1359
1362
1363 requirement_vector_iterate(&pimprove->reqs, preq) {
1364 const bool active = is_req_active(&context, NULL, preq, RPT_POSSIBLE);
1365
1366 if (VUT_ADVANCE == preq->source.kind && preq->present && !active) {
1367 /* Found a missing technology requirement for this improvement. */
1368 tech_vector_append(&needed_techs, preq->source.value.advance);
1369 } else if (VUT_IMPROVEMENT == preq->source.kind && preq->present && !active) {
1370 /* Found a missing improvement requirement for this improvement.
1371 * For example, in the default ruleset a city must have a Library
1372 * before it can have a University. */
1373 impr_vector_append(&needed_improvements, preq->source.value.building);
1374 }
1375 all_met = all_met && active;
1377
1378 /* If v is negative, the improvement is not worth building,
1379 * but there is no need to punish research of the technologies
1380 * that would make it available.
1381 */
1383 if (0 < v && 0 < n_needed_techs) {
1384 /* Tell AI module how much we want this improvement and what techs are
1385 * required to get it. */
1386 const adv_want dv = v / n_needed_techs;
1387
1388 want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1389 &needed_techs, dv);
1390 }
1391
1392 /* If v is negative, the improvement is not worth building,
1393 * but there is no need to punish building the improvements
1394 * that would make it available.
1395 */
1397 if (0 < v && 0 < n_needed_improvements) {
1398 /* Because we want this improvement,
1399 * we want the improvements that will make it possible */
1400 const adv_want dv = v / (n_needed_improvements * 4); /* WAG */
1401 int i;
1402
1403 for (i = 0; i < n_needed_improvements; i++) {
1405 /* TODO: increase the want for the needed_impr,
1406 * if we can build it now */
1407 /* Recurse */
1408 (void) adjust_wants_for_reqs(ait, pplayer, pcity, needed_impr, dv);
1409 }
1410 }
1411
1412 /* TODO: use a similar method to increase wants for governments
1413 * that will make this improvement possible? */
1414
1417
1418 return all_met;
1419}
1420
1421/**********************************************************************/
1426adv_want dai_city_want(struct player *pplayer, struct city *acity,
1427 struct adv_data *adv, struct impr_type *pimprove)
1428{
1429 adv_want want = 0;
1430 int prod[O_LAST], bonus[O_LAST], waste[O_LAST];
1431 const struct civ_map *nmap = &(wld.map);
1432
1433 memset(prod, 0, O_LAST * sizeof(*prod));
1434 if (NULL != pimprove
1435 && adv->impr_calc[improvement_index(pimprove)] == ADV_IMPR_CALCULATE_FULL) {
1436 struct tile *acenter = city_tile(acity);
1438
1439 /* The below calculation mostly duplicates get_worked_tile_output().
1440 * We do this only for buildings that we know may change tile
1441 * outputs. */
1443 if (tile_worked(ptile) == acity) {
1445 prod[o] += city_tile_output(acity, ptile, celebrating, o);
1447 }
1449
1451 } else {
1452 fc_assert(sizeof(*prod) == sizeof(*acity->citizen_base));
1453 memcpy(prod, acity->citizen_base, O_LAST * sizeof(*prod));
1454 }
1455
1457 prod[O_TRADE] += proute->value;
1462 waste[o] = city_waste(acity, o, prod[o] * bonus[o] / 100, NULL);
1464 add_tax_income(pplayer,
1465 prod[O_TRADE] * bonus[O_TRADE] / 100 - waste[O_TRADE],
1466 prod);
1468 prod[o] = prod[o] * bonus[o] / 100 - waste[o];
1470
1474 /* Unit upkeep isn't handled here. Unless we do a full city_refresh it
1475 * won't be changed anyway. */
1476
1477 want += prod[O_FOOD] * adv->food_priority;
1478 if (prod[O_SHIELD] != 0) {
1479 want += prod[O_SHIELD] * adv->shield_priority;
1480 want -= city_pollution(acity, prod[O_SHIELD]) * adv->pollution_priority;
1481 }
1482 want += prod[O_LUXURY] * adv->luxury_priority;
1483 want += prod[O_SCIENCE] * adv->science_priority;
1484 if (pplayer->economic.tax > 50) {
1485 /* Increased tax rate indicates that we've had gold shortage which
1486 * we are trying to fill with taxes. Consider gold more critical
1487 * than usually.
1488 * Smallest tax rate we can have here is 60% -> factor (60 - 40) / 14.0 = 1.43 */
1489 want += prod[O_GOLD] * adv->gold_priority * (pplayer->economic.tax - 40) / 14.0;
1490 } else {
1491 want += prod[O_GOLD] * adv->gold_priority;
1492 }
1493
1494 return want;
1495}
1496
1497/**********************************************************************/
1501static adv_want base_want(struct ai_type *ait, struct player *pplayer,
1502 struct city *pcity, struct impr_type *pimprove)
1503{
1504 struct adv_data *adv = adv_data_get(pplayer, NULL);
1505 adv_want final_want = 0;
1508
1509 if (adv->impr_calc[improvement_index(pimprove)] == ADV_IMPR_ESTIMATE) {
1510 return 0; /* Nothing to calculate here. */
1511 }
1512
1514 || (is_small_wonder(pimprove)
1515 && NULL != city_from_small_wonder(pplayer, pimprove))) {
1516 return 0;
1517 }
1518
1519 if (is_wonder(pimprove)) {
1520 if (is_great_wonder(pimprove)) {
1523 }
1524 wonder_city_id = pplayer->wonders[improvement_index(pimprove)];
1525 }
1526 /* Add the improvement */
1527 city_add_improvement(pcity, pimprove);
1528
1529 /* Stir, then compare notes */
1531 adv->impr_range[improvement_index(pimprove)], acity) {
1532 final_want += dai_city_want(pplayer, acity, adv, pimprove)
1533 - def_ai_city_data(acity, ait)->worth;
1535
1536 /* Restore */
1537 city_remove_improvement(pcity, pimprove);
1538 if (is_wonder(pimprove)) {
1539 if (is_great_wonder(pimprove)) {
1542 }
1543
1544 pplayer->wonders[improvement_index(pimprove)] = wonder_city_id;
1545 }
1546
1547 return final_want;
1548}
1549
1550/**********************************************************************/
1580 struct player *pplayer,
1581 struct city *pcity,
1582 struct impr_type *pimprove,
1583 const bool already)
1584{
1585 adv_want v = 0;
1587 int nplayers = normal_player_count();
1588 struct adv_data *ai = adv_data_get(pplayer, NULL);
1589 bool capital = is_capital(pcity);
1590 bool can_build = TRUE;
1591 struct universal source = {
1593 .value = {.building = pimprove}
1594 };
1595 const bool is_convert = is_convert_improvement(pimprove);
1596 int turns = 9999;
1598
1599 /* FIXME: Do we really need the effects check to be made *without*
1600 * passing the city tile? */
1601 const struct req_context effect_ctxt = {
1602 .player = pplayer,
1603 .city = pcity,
1604 .building = pimprove,
1605 };
1606
1607 /* Do NOT pass building here, as the action might be about
1608 * targeting some completely different building, AND
1609 * the check to see if the action is possible before
1610 * the building is there is also ignoring the buildings.
1611 * We don't want those two results to differ for
1612 * an unrelated reason to what we are evaluating. */
1613 const struct req_context actenabler_ctxt = {
1614 .player = pplayer,
1615 .city = pcity,
1616 .tile = city_tile(pcity),
1617 };
1618
1619 /* Remove team members from the equation */
1621 if (aplayer->team
1622 && aplayer->team == pplayer->team
1623 && aplayer != pplayer) {
1624 nplayers--;
1625 }
1627
1628 if (is_convert) {
1629 /* Since coinage-like improvements contains some entirely spurious
1630 * ruleset values, we need to hard-code a sensible want.
1631 * We must otherwise handle IG_CONVERT improvements like the others,
1632 * so the AI will research techs that make it available,
1633 * for rulesets that do not provide it from the start.
1634 */
1635 if (improvement_has_flag(pimprove, IF_GOLD)) {
1636 v += TRADE_WEIGHTING / 10;
1637 } else if (improvement_has_flag(pimprove, IF_INFRA)) {
1638 v += INFRA_WEIGHTING / 10;
1639 }
1640 /* Without relevant flags, base want remains 0. */
1641 } else {
1642 /* Base want is calculated above using a more direct approach. */
1643 v += base_want(ait, pplayer, pcity, pimprove);
1644 if (v != 0) {
1645 CITY_LOG(LOG_DEBUG, pcity, "%s base_want is " ADV_WANT_PRINTF " (range=%d)",
1646 improvement_rule_name(pimprove),
1647 v,
1648 ai->impr_range[improvement_index(pimprove)]);
1649 }
1650 }
1651
1652 if (!is_convert) {
1653 /* Adjust by building cost */
1654 /* FIXME: ought to reduce by upkeep cost and amortise by building cost */
1655 v -= (impr_build_shield_cost(pcity, pimprove)
1656 / (pcity->surplus[O_SHIELD] * 10 + 1));
1657 }
1658
1659 /* Find number of cities per range. */
1661 /* kludge -- Number of *our* cities in these ranges. */
1664
1665 if (place < 0) {
1667 } else {
1669 }
1670
1671 /* All the trade partners and the city being considered. */
1673
1675
1676 /* Invalid building range */
1678 = cities[REQ_RANGE_TILE] = 0;
1679
1681 int potential = (aplayer->server.bulbs_last_turn
1682 + city_list_size(aplayer->cities) + 1);
1683
1684 if (potential > 0) {
1686 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1687 turns = MIN(turns,
1689 advance_number(pobs->source.value.advance))
1690 / (potential + 1));
1691 }
1693 }
1695
1698 bool active = TRUE;
1699 int n_needed_techs = 0;
1701 bool present = TRUE;
1702 bool impossible_to_get = FALSE;
1703
1705
1707 /* Check if all the requirements for the currently evaluated effect
1708 * are met, except for having the building that we are evaluating. */
1710 if (preq->range < range) {
1711 /* More limited range */
1712 range = preq->range;
1713 }
1714 present = preq->present;
1715 continue;
1716 }
1717
1719 active = FALSE;
1720 if (VUT_ADVANCE == preq->source.kind && preq->present) {
1721 /* This missing requirement is a missing tech requirement.
1722 * This will be for some additional effect
1723 * (For example, in the default ruleset, Mysticism increases
1724 * the effect of Temples). */
1725 tech_vector_append(&needed_techs, preq->source.value.advance);
1726 } else if (!dai_can_requirement_be_met_in_city(preq, pplayer, pcity)) {
1728 }
1729 }
1731
1733 if ((active || n_needed_techs) && !impossible_to_get) {
1734 adv_want v1 = dai_effect_value(pplayer, ai, pcity, capital,
1735 turns, peffect, range, nplayers);
1736 /* v1 could be negative (the effect could be undesirable),
1737 * although it is usually positive.
1738 * For example, in the default ruleset, Communism decreases the
1739 * effectiveness of a Cathedral. */
1740
1741 if (!present) {
1742 /* Building removes the effect */
1743 /* Currently v1 is (v + delta). Make it (v - delta) instead */
1744 v1 = -v1;
1745 }
1746
1747 if (active) {
1748 v += v1;
1749 } else if (v1 > 0) {
1750 /* If value of the effect is negative, do not hold it against
1751 * the tech - having the tech wont force one to build the
1752 * building. */
1753
1754 /* We might want the technology that will enable this
1755 * (additional) effect.
1756 * The better the effect, the more we want the technology.
1757 * We are more interested in (additional) effects that enhance
1758 * buildings we already have.
1759 */
1760 const int a = already? 5: 4; /* WAG */
1761 const adv_want dv = v1 * a / (4 * n_needed_techs);
1762
1763 want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1764 &needed_techs, dv);
1765 }
1766 }
1767
1770
1771 /* Can the city be the target of an action? */
1772 action_iterate (act_id) {
1773 bool is_possible;
1774 bool will_be_possible = FALSE;
1775 enum req_range max_range;
1776 int act_neg_util;
1777
1778 /* Is the action relevant? */
1779 if (action_id_get_target_kind(act_id) != ATK_CITY) {
1780 continue;
1781 }
1782
1783 /* No range found yet. Local is the most narrow range. */
1785
1786 /* Is it possible to do the action to the city right now?
1787 *
1788 * (DiplRel requirements are ignored since actor_player is NULL) */
1790
1791 /* Will it be possible to do the action to the city if the building is
1792 * built? */
1794 enabler) {
1795 bool active = TRUE;
1797
1798 requirement_vector_iterate(&(enabler->target_reqs), preq) {
1800 /* Pretend the building is there */
1801 if (preq->present) {
1802 range = preq->range; /* Assumption: Max one pr vector */
1803 continue;
1804 } else {
1805 active = FALSE;
1806 break;
1807 }
1809 active = FALSE;
1810 break;
1811 }
1813
1814 if (active) {
1816
1817 /* Store the widest range that enables the action. */
1818 if (max_range < range) {
1819 max_range = range;
1820 }
1821
1822 /* Don't break the iteration even if the action is enabled. There
1823 * could be a wider range in an active action enabler not yet seen.
1824 */
1825 }
1827
1828 /* Will the building significantly change the ability to target
1829 * the city? */
1831 continue;
1832 }
1833
1834 /* How undesirable is it that the city may be a target? */
1836
1837 /* Multiply the desire by number of cities in range.
1838 * Note: This is a simplification. If the action can be done or not
1839 * _may_ be uncanged or changed in the opposite direction in the other
1840 * cities in the range. */
1842
1843 /* Consider the utility of being a potential target.
1844 * Remember: act_util is the negative utility of being a target. */
1845 if (will_be_possible) {
1846 v -= act_neg_util;
1847 } else {
1848 v += act_neg_util;
1849 }
1851
1852 if (already) {
1853 /* Discourage research of the technology that would make this building
1854 * obsolete. The bigger the desire for this building, the more
1855 * we want to discourage the technology. */
1856 dont_want_tech_obsoleting_impr(ait, pplayer, pcity, pimprove, v);
1857 } else {
1858 /* Increase the want for technologies that will enable
1859 * construction of this improvement, if necessary.
1860 */
1861 const bool all_met = adjust_wants_for_reqs(ait, pplayer, pcity, pimprove, v);
1863 }
1864
1865 if (is_convert && can_build) {
1866 /* Could have a negative want for coinage-like improvements,
1867 * if we have some stock in a building already. */
1868 pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1869 } else if (!already && can_build) {
1870 const struct research *presearch = research_get(pplayer);
1871
1872 /* Convert the base 'want' into a building want
1873 * by applying various adjustments */
1874
1875 /* Would it mean losing shields? */
1876 if ((VUT_UTYPE == pcity->production.kind
1877 || (is_wonder(pcity->production.value.building)
1878 && !is_wonder(pimprove))
1879 || (!is_wonder(pcity->production.value.building)
1880 && is_wonder(pimprove)))
1881 && pcity->turn_last_built != game.info.turn) {
1882 if (has_handicap(pplayer, H_PRODCHGPEN)) {
1883 v -= pcity->shield_stock * SHIELD_WEIGHTING / 4;
1884 } else {
1885 v -= pcity->shield_stock * SHIELD_WEIGHTING / 15;
1886 }
1887 }
1888
1889 /* Reduce want if building gets obsoleted soon */
1891 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1893 advance_number(pobs->source.value.advance));
1894
1895 v -= v / MAX(1, num_tech);
1896 }
1898
1899 /* Are we wonder city? Try to avoid building non-wonders very much. */
1900 if (pcity->id == ai->wonder_city && !is_wonder(pimprove)) {
1901 v /= 5;
1902 }
1903
1904 /* Set */
1905 pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1906 }
1907 /* Else we either have the improvement already,
1908 * or we can not build it (yet) */
1909}
1910
1911/**********************************************************************/
1920static bool should_force_recalc(struct city *pcity)
1921{
1923 || (VUT_IMPROVEMENT == pcity->production.kind
1924 && !is_convert_improvement(pcity->production.value.building)
1926 (pcity, pcity->production.value.building));
1927}
1928
1929/**********************************************************************/
1933void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
1934{
1935 struct adv_data *ai = adv_data_get(pplayer, NULL);
1936
1937 /* Find current worth of cities and cache this. */
1938 city_list_iterate(pplayer->cities, pcity) {
1939 def_ai_city_data(pcity, ait)->worth = dai_city_want(pplayer, pcity, ai, NULL);
1941}
1942
1943/**********************************************************************/
1949void dai_build_adv_adjust(struct ai_type *ait, struct player *pplayer,
1950 struct city *wonder_city)
1951{
1952 /* Clear old building wants.
1953 * Do this separately from the iteration over improvement types
1954 * because each iteration could actually update more than one improvement,
1955 * if improvements have improvements as requirements.
1956 */
1957 city_list_iterate(pplayer->cities, pcity) {
1958 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1959
1960 if (city_data->building_turn <= game.info.turn) {
1961 /* Do a scheduled recalculation this turn */
1962 improvement_iterate(pimprove) {
1963 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1965 } else if (should_force_recalc(pcity)) {
1966 /* Do an emergency recalculation this turn. */
1967 city_data->building_wait = city_data->building_turn
1968 - game.info.turn;
1969 city_data->building_turn = game.info.turn;
1970
1971 improvement_iterate(pimprove) {
1972 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1974 }
1976
1977 improvement_iterate(pimprove) {
1978 if (can_player_build_improvement_later(pplayer, pimprove)) {
1979 city_list_iterate(pplayer->cities, pcity) {
1980 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1981
1982 if (pcity != wonder_city && is_wonder(pimprove)) {
1983 /* Only wonder city should build wonders! */
1984 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1985 } else if (city_data->building_turn <= game.info.turn) {
1986 /* Building wants vary relatively slowly, so not worthwhile
1987 * recalculating them every turn.
1988 * We DO want to calculate (tech) wants because of buildings
1989 * we already have. */
1990 const bool already = city_has_building(pcity, pimprove);
1991 int idx = improvement_index(pimprove);
1992
1994 pimprove, already);
1995
1997 && 0 < pcity->server.adv->building_want[idx]));
1998
1999 if (is_great_wonder(pimprove)) {
2000 /* Not only would we get the wonder, but we would also prevent
2001 * opponents from getting it. */
2002 pcity->server.adv->building_want[idx] *= 1.5;
2003
2004 if (pcity->production.kind == VUT_IMPROVEMENT
2005 && is_great_wonder(pcity->production.value.building)) {
2006 /* If we already are building a great wonder, prefer continuing
2007 * to do so over stopping it */
2008 pcity->server.adv->building_want[idx] *= 1.25;
2009 }
2010 }
2011
2012 /* If I am not an expansionist, I want buildings more than units */
2013 if (pcity->server.adv->building_want[idx] > 0) {
2014 pcity->server.adv->building_want[idx]
2015 = pcity->server.adv->building_want[idx]
2018 }
2019 }
2020 /* else wait until a later turn */
2022 } else {
2023 /* An impossible improvement */
2024 city_list_iterate(pplayer->cities, pcity) {
2025 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
2027 }
2029
2030#ifdef FREECIV_DEBUG
2031 /* This logging is relatively expensive, so activate only if necessary */
2032 city_list_iterate(pplayer->cities, pcity) {
2033 improvement_iterate(pimprove) {
2034 if (pcity->server.adv->building_want[improvement_index(pimprove)] != 0) {
2035 CITY_LOG(LOG_DEBUG, pcity, "want to build %s with " ADV_WANT_PRINTF,
2036 improvement_rule_name(pimprove),
2037 pcity->server.adv->building_want[improvement_index(pimprove)]);
2038 }
2041#endif /* FREECIV_DEBUG */
2042
2043 /* Reset recalc counter */
2044 city_list_iterate(pplayer->cities, pcity) {
2045 struct ai_city *city_data = def_ai_city_data(pcity, ait);
2046
2047 if (city_data->building_turn <= game.info.turn) {
2048 /* This will spread recalcs out so that no one turn end is
2049 * much longer than others */
2051 city_data->building_turn = game.info.turn
2052 + city_data->building_wait;
2053 }
2055}
2056
2057/**********************************************************************/
2060void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity, bool *result)
2061{
2062 if (def_ai_city_data(pcity, ait)->grave_danger > 0) {
2063 *result = FALSE;
2064 } else {
2065 *result = TRUE;
2066 }
2067}
2068
2069/**********************************************************************/
2073static bool dai_cant_help_req(const struct req_context *context,
2074 const struct requirement *req)
2075{
2076 switch (req->source.kind) {
2077 /* Unskilled in channel digging and merchantry */
2078 case VUT_TERRAIN:
2079 case VUT_EXTRA:
2080 case VUT_GOOD:
2081 case VUT_TERRAINCLASS:
2082 case VUT_TERRFLAG:
2083 case VUT_TERRAINALTER:
2084 case VUT_CITYTILE:
2085 return !is_req_active(context, NULL, req, RPT_POSSIBLE);
2086 default:
2088 }
2089}
2090
2091/**********************************************************************/
2095 const struct impr_type *pimprove)
2096{
2097 const struct req_context city_ctxt = {
2099 .city = pcity,
2100 .tile = city_tile(pcity),
2101 };
2102
2103 /* FIXME: AI may be too stupid to sell obsoleting improvements
2104 * from the city that are _not_ checked here. */
2106 pimprove)) {
2107 return FALSE;
2108 }
2109 /* Check for requirements that aren't met and that are unchanging (so
2110 * they can never be met). */
2111 requirement_vector_iterate(&pimprove->reqs, preq) {
2113 return FALSE;
2114 }
2116
2117 return TRUE;
2118}
2119
2120/**********************************************************************/
2123bool
2125 const struct impr_type *pimprove)
2126{
2127 const struct req_context context = { .player = p };
2128
2129 if (!valid_improvement(pimprove)) {
2130 return FALSE;
2131 }
2132 if (improvement_obsolete(p, pimprove, NULL)) {
2133 return FALSE;
2134 }
2135 if (is_great_wonder(pimprove) && !great_wonder_is_available(pimprove)) {
2136 /* Can't build wonder if already built */
2137 return FALSE;
2138 }
2139
2140 /* Check for requirements that aren't met and that are unchanging (so
2141 * they can never be met). */
2142 requirement_vector_iterate(&pimprove->reqs, preq) {
2143 if (preq->range >= REQ_RANGE_PLAYER
2145 return FALSE;
2146 }
2148 /* FIXME: should check some "unchanging" reqs here - like if there's
2149 * a nation requirement, we can go ahead and check it now. */
2150
2151 return TRUE;
2152}
2153
2154/**********************************************************************/
2161 enum effect_type effect_type,
2162 const struct unit_type *utype)
2163{
2164 int greatest_value = 0;
2165 const struct impr_type *best_building = NULL;
2166 const struct req_context context = {
2168 .city = pcity,
2169 .tile = city_tile(pcity),
2170 .unittype = utype,
2171 };
2172
2173 effect_list_iterate(get_effects(effect_type), peffect) {
2174 if (peffect->value > greatest_value) {
2175 const struct impr_type *building = NULL;
2176 bool wrong_unit = FALSE;
2177
2179 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2180 building = preq->source.value.building;
2181
2183 || !is_improvement(building)) {
2184 building = NULL;
2185 break;
2186 }
2187 } else if (VUT_IMPR_FLAG == preq->source.kind && preq->present) {
2188 /* TODO: Ruleset cache for buildings with specific flag */
2189 improvement_iterate(impr) {
2190 if (improvement_has_flag(impr, preq->source.value.impr_flag)) {
2192 && is_improvement(impr)) {
2193 if (building == NULL) {
2194 building = impr;
2195 break;
2196 }
2197 }
2198 }
2200
2201 if (building == NULL) {
2202 break;
2203 }
2204 } else if (utype != NULL
2206 /* Effect requires other kind of unit than what we are interested about */
2207 wrong_unit = TRUE;
2208 break;
2209 }
2211 if (!wrong_unit && building != NULL) {
2212 best_building = building;
2213 greatest_value = peffect->value;
2214 }
2215 }
2217
2218 if (best_building) {
2220 }
2221 return B_LAST;
2222}
bool is_action_possible_on_city(action_id act_id, const struct player *actor_player, const struct city *target_city)
Definition actions.c:5491
void action_array_add_all_by_result(action_id *act_array, int *position, enum action_result result)
Definition actions.c:5787
void action_array_end(action_id *act_array, int size)
Definition actions.c:5770
bool action_ever_possible(action_id action)
Definition actions.c:7550
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:3091
bool action_id_exists(const action_id act_id)
Definition actions.c:1068
enum action_target_kind action_get_target_kind(const struct action *paction)
Definition actions.c:1108
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:3481
struct action_enabler_list * action_enablers_for_action(action_id action)
Definition actions.c:1559
static struct action * action_by_number(action_id act_id)
Definition actions.h:396
#define action_array_iterate(_act_array_, _act_id_)
Definition actions.h:257
#define action_enabler_list_iterate_end
Definition actions.h:190
#define action_id_get_role(act_id)
Definition actions.h:457
#define action_array_iterate_end
Definition actions.h:269
#define action_iterate_end
Definition actions.h:214
#define MAX_NUM_ACTIONS
Definition actions.h:58
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition actions.h:188
#define action_iterate(_act_)
Definition actions.h:210
#define action_id_get_target_kind(act_id)
Definition actions.h:413
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
void dai_calc_data(const struct player *pplayer, int *trade, int *expenses, int *income)
Definition aihand.c:117
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:3626
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:3642
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:3396
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:3382
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:505
#define city_tile(_pcity_)
Definition city.h:561
static citizens city_size_get(const struct city *pcity)
Definition city.h:566
@ CITIZEN_ANGRY
Definition city.h:268
@ CITIZEN_UNHAPPY
Definition city.h:267
#define output_type_iterate(output)
Definition city.h:842
#define city_owner(_pcity_)
Definition city.h:560
#define city_list_iterate_end
Definition city.h:507
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:227
@ FEELING_FINAL
Definition city.h:281
#define city_tile_iterate_end
Definition city.h:235
#define city_built_iterate(_pcity, _p)
Definition city.h:831
#define city_built_iterate_end
Definition city.h:837
#define output_type_iterate_end
Definition city.h:848
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:3277
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:3648
void sync_cities(void)
Definition citytools.c:3351
void change_build_target(struct player *pplayer, struct city *pcity, struct universal *target, enum event_type event)
Definition citytools.c:3188
void do_sell_building(struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const char *reason)
Definition citytools.c:3005
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:356
#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:1341
static void unit_do_disband_trad(struct player *owner, struct unit *punit, const enum action_requester requester)
Definition daicity.c:453
static bool dai_cant_help_req(const struct req_context *context, const struct requirement *req)
Definition daicity.c:2073
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:157
static void increase_maxbuycost(struct player *pplayer, int new_value)
Definition daicity.c:372
static void dai_barbarian_choose_build(struct player *pplayer, struct city *pcity, struct adv_choice *choice)
Definition daicity.c:201
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:1139
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:1166
#define LOG_BUY
Definition daicity.c:68
static void dai_city_sell_noncritical(struct city *pcity, bool redundant_only)
Definition daicity.c:988
#define city_range_iterate_end
Definition daicity.c:103
static int unit_food_upkeep(struct unit *punit)
Definition daicity.c:692
void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:852
void dai_city_alloc(struct ai_type *ait, struct city *pcity)
Definition daicity.c:1112
#define LOG_EMERGENCY
Definition daicity.c:69
void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity, bool *result)
Definition daicity.c:2060
static bool should_force_recalc(struct city *pcity)
Definition daicity.c:1920
bool dai_can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
Definition daicity.c:2124
static int action_target_neg_util(action_id act_id, const struct city *pcity)
Definition daicity.c:1208
#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:1031
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:178
void dai_build_adv_adjust(struct ai_type *ait, struct player *pplayer, struct city *wonder_city)
Definition daicity.c:1949
static void contemplate_terrain_improvements(struct ai_type *ait, struct city *pcity)
Definition daicity.c:750
static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:524
#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:1579
void dai_city_free(struct ai_type *ait, struct city *pcity)
Definition daicity.c:1125
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:128
Impr_type_id dai_find_source_building(struct city *pcity, enum effect_type effect_type, const struct unit_type *utype)
Definition daicity.c:2160
void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:1933
static void dai_upgrade_units(struct city *pcity, int limit, bool military)
Definition daicity.c:382
#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:2094
adv_want dai_city_want(struct player *pplayer, struct city *acity, struct adv_data *adv, struct impr_type *pimprove)
Definition daicity.c:1426
static adv_want base_want(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove)
Definition daicity.c:1501
static int unit_foodbox_cost(struct unit *punit)
Definition daicity.c:711
static bool building_crucial(const struct player *plr, struct impr_type *pimprove, const struct city *pcity)
Definition daicity.c:966
static void dai_city_choose_build(struct ai_type *ait, struct player *pplayer, struct city *pcity)
Definition daicity.c:251
#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
#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:1326
#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:3439
#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:1063
int Impr_type_id
Definition fc_types.h:235
@ RPT_POSSIBLE
Definition fc_types.h:532
int action_id
Definition fc_types.h:248
#define ACTRES_NONE
Definition fc_types.h:186
#define ADV_WANT_PRINTF
Definition fc_types.h:1064
@ O_SHIELD
Definition fc_types.h:101
@ O_FOOD
Definition fc_types.h:101
@ O_TRADE
Definition fc_types.h:101
@ O_SCIENCE
Definition fc_types.h:101
@ O_LUXURY
Definition fc_types.h:101
@ O_GOLD
Definition fc_types.h:101
@ O_LAST
Definition fc_types.h:101
signed short Continent_id
Definition fc_types.h:231
#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:61
struct world wld
Definition game.c:62
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:119
int player_get_expected_income(const struct player *pplayer)
Definition player.c:1286
const char * player_name(const struct player *pplayer)
Definition player.c:895
#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:750
int research_goal_bulbs_required(const struct research *presearch, Tech_type_id goal)
Definition research.c:772
struct research * research_get(const struct player *pplayer)
Definition research.c:128
#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:317
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:520
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:608
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:115
#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:553
bool unit_is_alive(int id)
Definition unit.c:2290
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1555
bool is_field_unit(const struct unit *punit)
Definition unit.c:438
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:383
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1661
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:638
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1766
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1284
#define unit_tile(_pu)
Definition unit.h:404
#define unit_owner(_pu)
Definition unit.h:403
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:3358
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:3338
#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:2277
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
int utype_upkeep_cost(const struct unit_type *ut, struct player *pplayer, Output_type_id otype)
Definition unittype.c:132
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2971
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2259
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1593
int num_role_units(int role)
Definition unittype.c:2209
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1709
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1584
bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
Definition unittype.c:214
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:196
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1444
int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Definition unittype.c:187
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1737