Freeciv-3.1
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 "research.h"
30#include "specialist.h"
31
32/* server */
33#include "cityhand.h"
34#include "citytools.h"
35#include "cityturn.h"
36#include "notify.h"
37#include "plrhand.h"
38#include "srv_log.h"
39#include "unithand.h"
40#include "unittools.h"
41
42/* server/advisors */
43#include "advdata.h"
44#include "advtools.h"
45#include "autosettlers.h"
46#include "advbuilding.h"
47#include "infracache.h"
48
49/* ai */
50#include "aitraits.h"
51#include "difficulty.h"
52#include "handicaps.h"
53
54/* ai/default */
55#include "aidata.h"
56#include "aihand.h"
57#include "ailog.h"
58#include "aiplayer.h"
59#include "aitools.h"
60#include "aiunit.h"
61#include "daidiplomacy.h"
62#include "daidomestic.h"
63#include "daimilitary.h"
64#include "daieffects.h"
65#include "daisettler.h"
66
67#include "daicity.h"
68
69#define LOG_BUY LOG_DEBUG
70#define LOG_EMERGENCY LOG_VERBOSE
71#define LOG_WANT LOG_VERBOSE
72
73/* TODO: AI_CITY_RECALC_SPEED should be configurable to ai difficulty.
74 -kauf */
75#define AI_CITY_RECALC_SPEED 5
76
77#define AI_BA_RECALC_SPEED 5
78
79#define SPECVEC_TAG tech
80#define SPECVEC_TYPE struct advance *
81#include "specvec.h"
82
83#define SPECVEC_TAG impr
84#define SPECVEC_TYPE const struct impr_type *
85#include "specvec.h"
86
87/* Iterate over cities within a certain range around a given city
88 * (city_here) that exist within a given city list. */
89#define city_range_iterate(city_here, list, range, city) \
90{ \
91 city_list_iterate(list, city) { \
92 if (range == REQ_RANGE_PLAYER \
93 || range == REQ_RANGE_TEAM \
94 || range == REQ_RANGE_ALLIANCE \
95 || (range == REQ_RANGE_TRADE_ROUTE \
96 && (city == city_here \
97 || have_cities_trade_route(city, city_here))) \
98 || ((range == REQ_RANGE_CITY || range == REQ_RANGE_LOCAL) \
99 && city == city_here) \
100 || (range == REQ_RANGE_CONTINENT \
101 && tile_continent(city->tile) == \
102 tile_continent(city_here->tile))) {
103
104#define city_range_iterate_end \
105 } \
106 } city_list_iterate_end; \
107}
108
109#define CITY_EMERGENCY(pcity) \
110 (pcity->surplus[O_SHIELD] < 0 || city_unhappy(pcity) \
111 || pcity->food_stock + pcity->surplus[O_FOOD] < 0)
112
113static void dai_city_sell_noncritical(struct city *pcity, bool redundant_only);
114static void resolve_city_emergency(struct ai_type *ait, struct player *pplayer,
115 struct city *pcity);
116
117/**********************************************************************/
130 struct player *pplayer,
131 const struct city *pcity,
132 const struct impr_type *pimprove,
133 const struct advance *tech,
134 adv_want building_want)
135{
136 /* The conversion factor was determined by experiment,
137 * and might need adjustment. See also dai_tech_effect_values()
138 */
139 const adv_want tech_want = building_want * def_ai_city_data(pcity, ait)->building_wait
140 * 14 / 8;
141#if 0
142 /* This logging is relatively expensive,
143 * so activate it only while necessary. */
144 TECH_LOG(LOG_DEBUG, pplayer, tech,
145 "wanted by %s for building: %d -> %d",
146 city_name_get(pcity), improvement_rule_name(pimprove),
147 building_want, tech_want);
148#endif /* 0 */
149 if (tech) {
150 def_ai_player_data(pplayer, ait)->tech_want[advance_index(tech)] += tech_want;
151 }
152}
153
154/**********************************************************************/
159 struct player *pplayer,
160 const struct city *pcity,
161 const struct impr_type *pimprove,
162 struct tech_vector *needed_techs,
163 adv_want building_want)
164{
165 int t;
166 int n_needed_techs = tech_vector_size(needed_techs);
167
168 for (t = 0; t < n_needed_techs; t++) {
169 want_tech_for_improvement_effect(ait, pplayer, pcity, pimprove,
170 *tech_vector_get(needed_techs, t),
171 building_want);
172 }
173}
174
175/**********************************************************************/
180 struct player *pplayer,
181 const struct city *pcity,
182 const struct impr_type *pimprove,
183 adv_want building_want)
184{
185 requirement_vector_iterate(&pimprove->obsolete_by, pobs) {
186 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
187 want_tech_for_improvement_effect(ait, pplayer, pcity, pimprove,
188 pobs->source.value.advance,
189 -building_want);
190 }
192}
193
194/**********************************************************************/
202static void dai_barbarian_choose_build(struct player *pplayer,
203 struct city *pcity,
204 struct adv_choice *choice)
205{
206 struct unit_type *bestunit = NULL;
207 int i, bestattack = 0;
208 const struct civ_map *nmap = &(wld.map);
209
210 /* Choose the best unit among the basic ones */
211 for (i = 0; i < num_role_units(L_BARBARIAN_BUILD); i++) {
212 struct unit_type *iunit = get_role_unit(L_BARBARIAN_BUILD, i);
213
214 if (iunit->attack_strength > bestattack
215 && can_city_build_unit_now(nmap, pcity, iunit)) {
216 bestunit = iunit;
217 bestattack = iunit->attack_strength;
218 }
219 }
220
221 /* Choose among those made available through other civ's research */
222 for (i = 0; i < num_role_units(L_BARBARIAN_BUILD_TECH); i++) {
223 struct unit_type *iunit = get_role_unit(L_BARBARIAN_BUILD_TECH, i);
224
225 if (iunit->attack_strength > bestattack
226 && can_city_build_unit_now(nmap, pcity, iunit)) {
227 bestunit = iunit;
228 bestattack = iunit->attack_strength;
229 }
230 }
231
232 /* If found anything, put it into the choice */
233 if (bestunit) {
234 choice->value.utype = bestunit;
235 /* "Overriding military emergency" indicator */
237 choice->type = CT_ATTACKER;
238 adv_choice_set_use(choice, "barbarian");
239 } else {
240 log_base(LOG_WANT, "Barbarians don't know what to build!");
241 }
242}
243
244/**********************************************************************/
252static void dai_city_choose_build(struct ai_type *ait, struct player *pplayer,
253 struct city *pcity)
254{
255 struct adv_choice *newchoice;
256 struct adv_data *adv = adv_data_get(pplayer, NULL);
257 struct ai_city *city_data = def_ai_city_data(pcity, ait);
258 const struct civ_map *nmap = &(wld.map);
259
260 if (has_handicap(pplayer, H_AWAY)
261 && !city_built_last_turn(pcity)
262 && city_data->urgency == 0) {
263 /* Don't change existing productions unless we have to. */
264 return;
265 }
266
267 if (is_barbarian(pplayer)) {
268 dai_barbarian_choose_build(pplayer, pcity, &(city_data->choice));
269 } else {
270 if ((city_data->choice.want < DAI_WANT_MILITARY_EMERGENCY
271 || city_data->urgency == 0)
272 && !(dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0
273 && pcity->id != adv->wonder_city)) {
274 newchoice = domestic_advisor_choose_build(ait, pplayer, pcity);
275 adv_choice_copy(&(city_data->choice), adv_better_choice(&(city_data->choice), newchoice));
276 adv_free_choice(newchoice);
277 }
278 }
279
280 /* Fallbacks */
281 if (city_data->choice.want == 0) {
282 /* Fallbacks do happen with techlevel 0, which is now default. -- Per */
283 CITY_LOG(LOG_WANT, pcity, "Falling back - didn't want to build soldiers,"
284 " workers, caravans, settlers, or buildings!");
285 city_data->choice.want = 1;
286 if (best_role_unit(pcity, action_id_get_role(ACTION_TRADE_ROUTE))) {
287 city_data->choice.value.utype
288 = best_role_unit(pcity, action_id_get_role(ACTION_TRADE_ROUTE));
289 city_data->choice.type = CT_CIVILIAN;
290 adv_choice_set_use(&(city_data->choice), "fallback trade route");
291 } else {
292 unsigned int our_def = assess_defense_quadratic(ait, pcity);
293
294 if (our_def == 0
295 && dai_process_defender_want(ait, nmap, pplayer, pcity, 1,
296 &(city_data->choice), 0)) {
297 adv_choice_set_use(&(city_data->choice), "fallback defender");
298 CITY_LOG(LOG_DEBUG, pcity, "Building fallback defender");
299 } else if (best_role_unit(pcity, UTYF_SETTLERS)) {
300 city_data->choice.value.utype
301 = dai_role_utype_for_terrain_class(pcity, UTYF_SETTLERS, TC_LAND);
302 city_data->choice.type = CT_CIVILIAN;
303 adv_choice_set_use(&(city_data->choice), "fallback worker");
304 } else {
305 CITY_LOG(LOG_ERROR, pcity, "Cannot even build a fallback "
306 "(caravan/coinage/settlers). Fix the ruleset!");
307 city_data->choice.want = 0;
308 }
309 }
310 }
311
312 if (city_data->choice.want != 0) {
313 struct universal build_new;
314
315 ADV_CHOICE_ASSERT(city_data->choice);
316
317#ifdef ADV_CHOICE_TRACK
318 if (city_data->choice.log_if_chosen) {
319 log_normal("%s wants %s for %s with desire " ADV_WANT_PRINTF ".",
320 city_name_get(pcity),
321 adv_choice_rule_name(&city_data->choice),
322 city_data->choice.use,
323 city_data->choice.want);
324 }
325#endif /* ADV_CHOICE_TRACK */
326
327 CITY_LOG(LOG_DEBUG, pcity, "wants %s with desire " ADV_WANT_PRINTF ".",
328 adv_choice_rule_name(&city_data->choice),
329 city_data->choice.want);
330
331 switch (city_data->choice.type) {
332 case CT_CIVILIAN:
333 case CT_ATTACKER:
334 case CT_DEFENDER:
335 build_new.kind = VUT_UTYPE;
336 build_new.value.utype = city_data->choice.value.utype;
337 break;
338 case CT_BUILDING:
339 build_new.kind = VUT_IMPROVEMENT;
340 build_new.value.building = city_data->choice.value.building;
341 break;
342 case CT_NONE:
343 build_new.kind = VUT_NONE;
344 break;
345 case CT_LAST:
346 build_new.kind = universals_n_invalid();
347 break;
348 };
349
350 change_build_target(pplayer, pcity, &build_new, E_CITY_PRODUCTION_CHANGED);
351 }
352}
353
354/**********************************************************************/
357static void try_to_sell_stuff(struct player *pplayer, struct city *pcity)
358{
359 improvement_iterate(pimprove) {
360 if (can_city_sell_building(pcity, pimprove)
361 && !building_has_effect(pimprove, EFT_DEFEND_BONUS)) {
362 /* selling walls to buy defenders is counterproductive -- Syela */
363 really_handle_city_sell(pplayer, pcity, pimprove);
364 break;
365 }
367}
368
369/**********************************************************************/
373static void increase_maxbuycost(struct player *pplayer, int new_value)
374{
375 pplayer->ai_common.maxbuycost = MAX(pplayer->ai_common.maxbuycost, new_value);
376}
377
378/**********************************************************************/
383static void dai_upgrade_units(struct city *pcity, int limit, bool military)
384{
385 action_id upgrade_actions[MAX_NUM_ACTIONS];
386 struct player *pplayer = city_owner(pcity);
387 int expenses;
388
389 {
390 /* Find upgrade unit actions */
391 int i = 0;
392 action_list_add_all_by_result(upgrade_actions, &i, ACTRES_UPGRADE_UNIT);
393 action_list_end(upgrade_actions, i);
394 }
395
396 dai_calc_data(pplayer, NULL, &expenses, NULL);
397
399 if (pcity->owner == punit->owner) {
400 /* Only upgrade units you own, not allied ones */
401
402 const struct unit_type *old_type = unit_type_get(punit);
403 const struct unit_type *punittype = can_upgrade_unittype(pplayer, old_type);
404
405 if (military && !IS_ATTACKER(old_type)) {
406 /* Only upgrade military units this round */
407 continue;
408 } else if (!military && IS_ATTACKER(old_type)) {
409 /* Only civilians or transports this round */
410 continue;
411 }
412
413 if (punittype == NULL) {
414 continue;
415 }
416
417 action_array_iterate(upgrade_actions, act_id) {
418 if (action_ever_possible(act_id)) {
419 const struct action *paction = action_by_number(act_id);
420 int cost = unit_upgrade_price(pplayer, old_type, punittype);
421 int real_limit = limit;
422
423 /* Sinking Triremes are DANGEROUS!! We'll do anything to upgrade 'em. */
424 /* FIXME: This assumes rules to be quite close to civ/2.
425 * Of the supplied rulesets those are the only ones with
426 * UTYF_COAST unit, but... */
427 if (unit_has_type_flag(punit, UTYF_COAST)) {
428 real_limit = expenses;
429 }
430 if (pplayer->economic.gold - cost > real_limit) {
431 CITY_LOG(LOG_BUY, pcity, "Upgraded %s to %s for %d (%s)",
433 utype_rule_name(punittype),
434 cost,
435 military ? "military" : "civilian");
437 pcity->id, 0, "",
438 paction->id);
439 } else {
440 increase_maxbuycost(pplayer, cost);
441 }
442 }
444 }
446}
447
448/**********************************************************************/
454static void unit_do_disband_trad(struct player *owner, struct unit *punit,
455 const enum action_requester requester)
456{
457 const int punit_id_stored = punit->id;
458 const struct civ_map *nmap = &(wld.map);
459
461
462 /* Help Wonder gives 100% of the shields used to produce the unit to the
463 * city where it is located. */
464 if (unit_can_do_action(punit, ACTION_HELP_WONDER)) {
465 struct city *tgt_city;
466
467 /* Only a city at the same tile as the unit can benefit. */
468 tgt_city = tile_city(unit_tile(punit));
469
470 if (tgt_city
471 && is_action_enabled_unit_on_city(nmap, ACTION_HELP_WONDER,
472 punit, tgt_city)) {
473 if (unit_perform_action(owner, punit->id, tgt_city->id,
474 0, NULL, ACTION_HELP_WONDER, requester)) {
475 /* No shields wasted. The unit did Help Wonder. */
476 return;
477 }
478
479 if (!unit_is_alive(punit_id_stored)) {
480 /* The unit is gone. Maybe it was killed in Lua? */
481 return;
482 }
483 }
484 }
485
486 /* Disbanding a unit inside a city gives it 50% of the shields used to
487 * produce the unit. */
488 if (unit_can_do_action(punit, ACTION_DISBAND_UNIT_RECOVER)) {
489 struct city *tgt_city;
490
491 /* Only a city at the same tile as the unit can benefit. */
492 tgt_city = tile_city(unit_tile(punit));
493
494 if (tgt_city
495 && is_action_enabled_unit_on_city(nmap, ACTION_DISBAND_UNIT_RECOVER,
496 punit, tgt_city)) {
497 if (unit_perform_action(owner, punit->id, tgt_city->id,
498 0, NULL, ACTION_DISBAND_UNIT_RECOVER, requester)) {
499 /* The unit did Disband Unit Recover. 50% of the shields wasted. */
500 return;
501 }
502
503 if (!unit_is_alive(punit_id_stored)) {
504 /* The unit is gone. Maybe it was killed in Lua? */
505 return;
506 }
507 }
508 }
509
510 /* Try to disband even if all shields will be wasted. */
511 if (unit_can_do_action(punit, ACTION_DISBAND_UNIT)) {
512 if (is_action_enabled_unit_on_self(nmap, ACTION_DISBAND_UNIT, punit)) {
514 0, NULL, ACTION_DISBAND_UNIT, requester)) {
515 /* All shields wasted. The unit did Disband Unit. */
516 return;
517 }
518 }
519 }
520}
521
522/**********************************************************************/
525static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
526{
527 struct adv_choice bestchoice;
528 int cached_limit = dai_gold_reserve(pplayer);
529 int expenses;
530 bool war_footing = dai_on_war_footing(ait, pplayer);
531
532 /* Disband explorers that are at home but don't serve a purpose.
533 * FIXME: This is a hack, and should be removed once we
534 * learn how to ferry explorers to new land. */
535 city_list_iterate(pplayer->cities, pcity) {
536 struct tile *ptile = pcity->tile;
538 if (unit_has_type_role(punit, L_EXPLORER)
539 && pcity->id == punit->homecity
540 && def_ai_city_data(pcity, ait)->urgency == 0) {
541 CITY_LOG(LOG_BUY, pcity, "disbanding %s to increase production",
543 unit_do_disband_trad(pplayer, punit, ACT_REQ_PLAYER);
544 }
547
548 dai_calc_data(pplayer, NULL, &expenses, NULL);
549
550 do {
551 bool expensive; /* don't buy when it costs x2 unless we must */
552 int buycost;
553 int limit = cached_limit; /* cached_limit is our gold reserve */
554 struct city *pcity = NULL;
555 struct ai_city *city_data;
556
557 /* Find highest wanted item on the buy list */
558 adv_init_choice(&bestchoice);
559 city_list_iterate(pplayer->cities, acity) {
560 struct ai_city *acity_data = def_ai_city_data(acity, ait);
561
562 if (acity_data->choice.want
563 > bestchoice.want && ai_fuzzy(pplayer, TRUE)) {
564 adv_choice_copy(&bestchoice, &(acity_data->choice));
565 pcity = acity;
566 }
568
569 /* We found nothing, so we're done */
570 if (pcity == NULL) {
571 break;
572 }
573
574 city_data = def_ai_city_data(pcity, ait);
575
576 /* Not dealing with this city a second time */
577 city_data->choice.want = -1;
578
579 ADV_CHOICE_ASSERT(bestchoice);
580
581 /* Try upgrade units at danger location (high want is usually danger) */
582 if (city_data->urgency > 1) {
583 if (bestchoice.type == CT_BUILDING
584 && is_wonder(bestchoice.value.building)) {
585 CITY_LOG(LOG_BUY, pcity, "Wonder being built in dangerous position!");
586 } else {
587 /* If we have urgent want, spend more */
588 int upgrade_limit = limit;
589
590 if (city_data->urgency > 1) {
591 upgrade_limit = expenses;
592 }
593 /* Upgrade only military units now */
594 dai_upgrade_units(pcity, upgrade_limit, TRUE);
595 }
596 }
597
598 if (pcity->anarchy != 0 && bestchoice.type != CT_BUILDING) {
599 continue; /* Nothing we can do */
600 }
601
602 /* Cost to complete production */
603 buycost = city_production_buy_gold_cost(pcity);
604
605 if (buycost <= 0) {
606 continue; /* Already completed */
607 }
608
609 if (is_unit_choice_type(bestchoice.type)
610 && utype_is_cityfounder(bestchoice.value.utype)) {
611 if (get_city_bonus(pcity, EFT_GROWTH_FOOD) <= 0
612 && bestchoice.value.utype->pop_cost > 0
613 && city_size_get(pcity) <= bestchoice.value.utype->pop_cost) {
614 /* Don't buy settlers in cities that cannot afford the population cost. */
615 /* This used to check also if city is about to grow to required size
616 * next turn and allow buying of settlers in that case, but current
617 * order of end/start turn activities is such that settler building
618 * fails already before city grows. */
619 continue;
620 } else if (city_list_size(pplayer->cities) > 6) {
621 /* Don't waste precious money buying settlers late game
622 * since this raises taxes, and we want science. Adjust this
623 * again when our tax algorithm is smarter. */
624 continue;
625 } else if (war_footing) {
626 continue;
627 }
628 } else {
629 /* We are not a settler. Therefore we increase the cash need we
630 * balance our buy desire with to keep cash at hand for emergencies
631 * and for upgrades */
632 limit *= 2;
633 }
634
635 /* It costs x2 to buy something with no shields contributed */
636 expensive = (pcity->shield_stock == 0)
637 || (pplayer->economic.gold - buycost < limit);
638
639 if (bestchoice.type == CT_ATTACKER
640 && buycost
641 > utype_build_shield_cost(pcity, NULL, bestchoice.value.utype) * 2
642 && !war_footing) {
643 /* Too expensive for an offensive unit */
644 continue;
645 }
646
647 /* FIXME: Here Syela wanted some code to check if
648 * pcity was doomed, and we should therefore attempt
649 * to sell everything in it of non-military value */
650
651 if (pplayer->economic.gold - expenses >= buycost
652 && (!expensive
653 || (city_data->grave_danger != 0
654 && assess_defense(ait, pcity) == 0)
655 || (bestchoice.want > 200 && city_data->urgency > 1))) {
656 /* Buy stuff */
657 CITY_LOG(LOG_BUY, pcity, "Crash buy of %s for %d (want " ADV_WANT_PRINTF ")",
658 adv_choice_rule_name(&bestchoice),
659 buycost,
660 bestchoice.want);
661 really_handle_city_buy(pplayer, pcity);
662 } else if (city_data->grave_danger != 0
663 && bestchoice.type == CT_DEFENDER
664 && assess_defense(ait, pcity) == 0) {
665 /* We have no gold but MUST have a defender */
666 CITY_LOG(LOG_BUY, pcity, "must have %s but can't afford it (%d < %d)!",
667 adv_choice_rule_name(&bestchoice),
668 pplayer->economic.gold,
669 buycost);
670 try_to_sell_stuff(pplayer, pcity);
671 if (pplayer->economic.gold - expenses >= buycost) {
672 CITY_LOG(LOG_BUY, pcity, "now we can afford it (sold something)");
673 really_handle_city_buy(pplayer, pcity);
674 }
675 increase_maxbuycost(pplayer, buycost);
676 }
677 } while (TRUE);
678
679 if (!war_footing) {
680 /* Civilian upgrades now */
681 city_list_iterate(pplayer->cities, pcity) {
682 dai_upgrade_units(pcity, cached_limit, FALSE);
684 }
685
686 log_base(LOG_BUY, "%s wants to keep %d in reserve (tax factor %d)",
687 player_name(pplayer), cached_limit, pplayer->ai_common.maxbuycost);
688}
689
690/**********************************************************************/
693static int unit_food_upkeep(struct unit *punit)
694{
695 struct player *pplayer = unit_owner(punit);
696 int upkeep = utype_upkeep_cost(unit_type_get(punit), pplayer, O_FOOD);
697
698 if (punit->id != 0 && punit->homecity == 0)
699 upkeep = 0; /* thanks, Peter */
700
701 return upkeep;
702}
703
704/**********************************************************************/
712static int unit_foodbox_cost(struct unit *punit)
713{
714 int pop_cost = unit_type_get(punit)->pop_cost;
715
716 if (pop_cost <= 0) {
717 return 0;
718 }
719
720 if (punit->id == 0) {
721 /* It is a virtual unit, so must start in a city... */
722 struct city *pcity = tile_city(unit_tile(punit));
723 int size = city_size_get(pcity);
724 int cost = 0;
725 int i;
726
727 /* The default is to lose 100%. The growth bonus reduces this. */
728 int foodloss_pct = 100 - city_granary_savings(pcity);
729
730 foodloss_pct = CLIP(0, foodloss_pct, 100);
731 fc_assert_ret_val(pcity != NULL, -1);
732 fc_assert(size >= pop_cost);
733
734 for (i = pop_cost; i > 0 ; i--) {
736 }
737 cost = cost * foodloss_pct / 100;
738
739 return cost;
740 }
741
742 return 30;
743}
744
745/**********************************************************************/
752 struct city *pcity)
753{
754 struct unit *virtualunit;
755 int want;
756 enum unit_activity best_act;
757 struct extra_type *best_target;
758 struct tile *best_tile = NULL; /* May be accessed by log_*() calls. */
759 struct tile *pcenter = city_tile(pcity);
760 struct player *pplayer = city_owner(pcity);
761 struct adv_data *adv = adv_data_get(pplayer, NULL);
762 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, NULL);
763 struct unit_type *utype;
764 Continent_id place = tile_continent(pcenter);
765 struct ai_city *city_data = def_ai_city_data(pcity, ait);
766 struct dai_private_data *private = (struct dai_private_data *)ait->private;
767 const struct civ_map *nmap = &(wld.map);
768
769 if (!private->contemplace_workers) {
770 /* AI type uses custom method to set worker want and type. */
771 return;
772 }
773
774 city_data->worker_want = 0; /* Make sure old want does not stay if we don't want now */
775
776 utype = dai_role_utype_for_terrain_class(pcity, UTYF_SETTLERS, TC_LAND);
777
778 if (utype == NULL) {
779 log_debug("No UTYF_SETTLERS role unit available");
780 return;
781 }
782
783 /* Create a localized "virtual" unit to do operations with. */
784 virtualunit = unit_virtual_create(pplayer, pcity, utype, 0);
785 /* Advisors data space not allocated as it's not needed in the
786 lifetime of the virtualunit. */
787 unit_tile_set(virtualunit, pcenter);
788 want = settler_evaluate_improvements(nmap, virtualunit,
789 &best_act, &best_target,
790 &best_tile,
791 NULL, NULL);
792 if (unit_type_get(virtualunit)->pop_cost >= city_size_get(pcity)) {
793 /* We don't like disbanding the city as a side effect */
794 unit_virtual_destroy(virtualunit);
795
796 return;
797 }
798 /* We consider unit_food_upkeep with only half FOOD_WEIGHTING to
799 * balance the fact that unit can improve many tiles during its
800 * lifetime, and want is calculated for just one of them.
801 * Having full FOOD_WEIGHT here would mean that tile improvement of
802 * +1 food would give just zero want for settler. Other weights
803 * are lower, so +1 shield - unit food upkeep would be negative. */
804 want = (want - unit_food_upkeep(virtualunit) * FOOD_WEIGHTING / 2) * 100
805 / (40 + unit_foodbox_cost(virtualunit));
806 unit_virtual_destroy(virtualunit);
807
808 /* Massage our desire based on available statistics to prevent
809 * overflooding with worker type units if they come cheap in
810 * the ruleset */
811 if (place >= 0) {
812 want /= MAX(1, ai->stats.workers[place] / (adv->stats.cities[place] + 1));
813 want -= ai->stats.workers[place];
814 } else {
815 want /= MAX(1, ai->stats.ocean_workers[-place] / (adv->stats.ocean_cities[-place] + 1));
816 want -= ai->stats.ocean_workers[-place];
817 }
818 want = MAX(want, 0);
819
820 if (place >= 0) {
821 CITY_LOG(LOG_DEBUG, pcity, "wants %s with want %d to do %s at (%d,%d), "
822 "we have %d workers and %d cities on the continent",
823 utype_rule_name(utype),
824 want,
825 get_activity_text(best_act),
826 TILE_XY(best_tile),
827 ai->stats.workers[place],
828 adv->stats.cities[place]);
829 } else {
830 CITY_LOG(LOG_DEBUG, pcity, "wants %s with want %d to do %s at (%d,%d), "
831 "we have %d workers and %d cities on the ocean",
832 utype_rule_name(utype),
833 want,
834 get_activity_text(best_act),
835 TILE_XY(best_tile),
836 ai->stats.ocean_workers[-place],
837 adv->stats.ocean_cities[-place]);
838 }
839
840 fc_assert(want >= 0);
841
842 city_data->worker_want = want;
843 city_data->worker_type = dai_role_utype_for_terrain_class(pcity, UTYF_SETTLERS,
844 place >= 0 ? TC_LAND : TC_OCEAN);
845}
846
847/**********************************************************************/
853void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
854{
855 pplayer->ai_common.maxbuycost = 0;
856
858 city_list_iterate(pplayer->cities, pcity) {
859 if (CITY_EMERGENCY(pcity)
860 || city_granary_size(city_size_get(pcity)) == pcity->food_stock) {
861 /* Having a full granary isn't an emergency, but we want to rearrange */
862 auto_arrange_workers(pcity); /* this usually helps */
863 }
864 if (CITY_EMERGENCY(pcity)) {
865 /* Fix critical shortages or unhappiness */
866 resolve_city_emergency(ait, pplayer, pcity);
867 }
869 sync_cities();
871 if (pplayer->economic.tax >= 30 /* Otherwise expect it to increase tax */
872 && player_get_expected_income(pplayer) < -(pplayer->economic.gold)) {
873 int count = city_list_size(pplayer->cities);
874 struct city *sellers[count + 1];
875 int i;
876
877 /* Randomized order */
878 count = 0;
879 city_list_iterate(pplayer->cities, pcity) {
880 sellers[count++] = pcity;
882
883 for (i = 0; i < count; i++) {
884 int replace = fc_rand(count);
885 struct city *tmp;
886
887 tmp = sellers[i];
888 sellers[i] = sellers[replace];
889 sellers[replace] = tmp;
890 }
891
892 i = 0;
893 while (player_get_expected_income(pplayer) < -(pplayer->economic.gold)
894 && i < count) {
895 dai_city_sell_noncritical(sellers[i++], FALSE);
896 }
897 }
899
901 building_advisor(pplayer);
903
904 /* Initialize the infrastructure cache, which is used shortly. */
906 city_list_iterate(pplayer->cities, pcity) {
907 struct ai_city *city_data = def_ai_city_data(pcity, ait);
908 struct adv_choice *choice;
909
910 if (city_data->choice.want <= 0) {
911 /* Note that this function mungs the seamap, but we don't care */
913 choice = military_advisor_choose_build(ait, &(wld.map), pplayer, pcity, NULL);
914 adv_choice_copy(&(city_data->choice), choice);
915 adv_free_choice(choice);
917 }
918 if (dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0) {
919 city_data->worker_want = 0;
920 city_data->founder_want = 0;
921 city_data->founder_turn = game.info.turn; /* Do not consider zero we set here
922 * valid value, if real want is needed.
923 * Recalculate immediately in such situation. */
924 continue; /* Go, soldiers! */
925 }
926 /* Will record its findings in pcity->worker_want */
930
932 if (city_data->founder_turn <= game.info.turn) {
933 /* Will record its findings in pcity->founder_want */
934 contemplate_new_city(ait, pcity);
935 /* Avoid recalculating all the time.. */
936 /* This means AI is not very opportunistic if there happens to open up spot for
937 * a new city. */
938 city_data->founder_turn =
940 } else if (pcity->server.debug) {
941 /* recalculate every turn */
942 contemplate_new_city(ait, pcity);
943 }
945 ADV_CHOICE_ASSERT(city_data->choice);
947 /* Reset auto settler state for the next run. */
948 dai_auto_settler_reset(ait, pplayer);
949
950 city_list_iterate(pplayer->cities, pcity) {
951 dai_city_choose_build(ait, pplayer, pcity);
952
953 /* Initialize for next turn */
954 def_ai_city_data(pcity, ait)->choice.want = -1;
956
957 dai_spend_gold(ait, pplayer);
958}
959
960/**********************************************************************/
967static bool building_crucial(const struct player *plr,
968 struct impr_type *pimprove,
969 const struct city *pcity)
970{
971#if 0 /* This check will become more complicated now. */
972 if (ai_wants_no_science(plr)
973 && building_has_effect(pimprove, EFT_SCIENCE_BONUS)) {
974 return FALSE;
975 }
976#endif
977 if (building_has_effect(pimprove, EFT_DEFEND_BONUS)
978 /* selling city walls is really, really dumb -- Syela */
979 || is_improvement_productive(pcity, pimprove)) {
980 return TRUE;
981 }
982
983 return FALSE;
984}
985
986/**********************************************************************/
989static void dai_city_sell_noncritical(struct city *pcity,
990 bool redundant_only)
991{
992 struct player *pplayer = city_owner(pcity);
993
994 city_built_iterate(pcity, pimprove) {
995 if (can_city_sell_building(pcity, pimprove)
996 && !building_crucial(pplayer, pimprove, pcity)
997 && (!redundant_only || is_improvement_redundant(pcity, pimprove))) {
998 int gain = impr_sell_gold(pimprove);
999
1000 notify_player(pplayer, pcity->tile, E_IMP_SOLD, ftc_server,
1001 PL_("%s is selling %s for %d.",
1002 "%s is selling %s for %d.", gain),
1003 city_link(pcity),
1005 gain);
1006 do_sell_building(pplayer, pcity, pimprove, "sold");
1007
1008 return; /* max 1 building each turn */
1009 }
1011}
1012
1013/**********************************************************************/
1032static void resolve_city_emergency(struct ai_type *ait, struct player *pplayer,
1033 struct city *pcity)
1034{
1035 struct tile *pcenter = city_tile(pcity);
1036 const struct civ_map *nmap = &(wld.map);
1037
1039 "Emergency in %s (%s, angry%d, unhap%d food%d, prod%d)",
1040 city_name_get(pcity),
1041 city_unhappy(pcity) ? "unhappy" : "content",
1044 pcity->surplus[O_FOOD],
1045 pcity->surplus[O_SHIELD]);
1046
1047 city_tile_iterate(nmap, city_map_radius_sq_get(pcity), pcenter, atile) {
1048 struct city *acity = tile_worked(atile);
1049
1050 if (acity && acity != pcity && city_owner(acity) == city_owner(pcity)) {
1051 log_base(LOG_EMERGENCY, "%s taking over %s square in (%d, %d)",
1052 city_name_get(pcity), city_name_get(acity), TILE_XY(atile));
1053
1054#ifndef FREECIV_NDEBUG
1055 int ax, ay;
1056#endif
1057 fc_assert_action(city_base_to_city_map(&ax, &ay, acity, atile),
1058 continue);
1059
1060 if (is_free_worked(acity, atile)) {
1061 /* Can't remove a worker here. */
1062 continue;
1063 }
1064
1065 city_map_update_empty(acity, atile);
1068 }
1070
1071 auto_arrange_workers(pcity);
1072
1073 if (!CITY_EMERGENCY(pcity)) {
1074 log_base(LOG_EMERGENCY, "Emergency in %s resolved", city_name_get(pcity));
1075 goto cleanup;
1076 }
1077
1079 if (city_unhappy(pcity)
1080 && (utype_happy_cost(unit_type_get(punit), pplayer) > 0
1082 && def_ai_unit_data(punit, ait)->passenger == 0) {
1083 UNIT_LOG(LOG_EMERGENCY, punit, "is causing unrest, disbanded");
1084 /* TODO: If Help Wonder stops blocking Disband Unit there may be
1085 * cases where Disband Unit should be selected. Example: Field unit
1086 * in allied city that is building a wonder that makes the ally win
1087 * without sharing the victory. */
1088 /* TODO: Should the unit try to find legal targets at adjacent tiles?
1089 * Should it consider other self eliminating actions than the
1090 * components of the traditional disband? */
1091 unit_do_disband_trad(pplayer, punit, ACT_REQ_PLAYER);
1092 city_refresh(pcity);
1093 }
1095
1096 if (CITY_EMERGENCY(pcity)) {
1097 log_base(LOG_EMERGENCY, "Emergency in %s remains unresolved",
1098 city_name_get(pcity));
1099 } else {
1101 "Emergency in %s resolved by disbanding unit(s)",
1102 city_name_get(pcity));
1103 }
1104
1105 cleanup:
1107 sync_cities();
1108}
1109
1110/**********************************************************************/
1113void dai_city_alloc(struct ai_type *ait, struct city *pcity)
1114{
1115 struct ai_city *city_data = fc_calloc(1, sizeof(struct ai_city));
1116
1118 adv_init_choice(&(city_data->choice));
1119
1120 city_set_ai_data(pcity, ait, city_data);
1121}
1122
1123/**********************************************************************/
1126void dai_city_free(struct ai_type *ait, struct city *pcity)
1127{
1128 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1129
1130 if (city_data != NULL) {
1131 adv_deinit_choice(&(city_data->choice));
1132 city_set_ai_data(pcity, ait, NULL);
1133 FC_FREE(city_data);
1134 }
1135}
1136
1137/**********************************************************************/
1140void dai_city_save(struct ai_type *ait, const char *aitstr,
1141 struct section_file *file, const struct city *pcity,
1142 const char *citystr)
1143{
1144 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1145
1146 /* FIXME: remove this when the urgency is properly recalculated. */
1147 secfile_insert_int(file, city_data->urgency, "%s.%s.urgency", citystr, aitstr);
1148
1149 /* avoid fc_rand recalculations on subsequent reload. */
1150 secfile_insert_int(file, city_data->building_turn, "%s.%s.building_turn",
1151 citystr, aitstr);
1152 secfile_insert_int(file, city_data->building_wait, "%s.%s.building_wait",
1153 citystr, aitstr);
1154
1155 /* avoid fc_rand and expensive recalculations on subsequent reload. */
1156 secfile_insert_int(file, city_data->founder_turn, "%s.%s.founder_turn",
1157 citystr, aitstr);
1158 secfile_insert_int(file, city_data->founder_want, "%s.%s.founder_want",
1159 citystr, aitstr);
1160 secfile_insert_bool(file, city_data->founder_boat, "%s.%s.founder_boat",
1161 citystr, aitstr);
1162}
1163
1164/**********************************************************************/
1167void dai_city_load(struct ai_type *ait, const char *aitstr,
1168 const struct section_file *file,
1169 struct city *pcity, const char *citystr)
1170{
1171 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1172
1173 /* FIXME: remove this when the urgency is properly recalculated. */
1174 city_data->urgency
1175 = secfile_lookup_int_default(file, 0, "%s.%s.urgency", citystr, aitstr);
1176
1177 /* avoid fc_rand recalculations on subsequent reload. */
1178 city_data->building_turn
1179 = secfile_lookup_int_default(file, 0, "%s.%s.building_turn", citystr,
1180 aitstr);
1181 city_data->building_wait
1183 "%s.%s.building_wait", citystr, aitstr);
1184
1185 /* avoid fc_rand and expensive recalculations on subsequent reload. */
1186 city_data->founder_turn
1187 = secfile_lookup_int_default(file, 0, "%s.%s.founder_turn", citystr,
1188 aitstr);
1189 city_data->founder_want
1190 = secfile_lookup_int_default(file, 0, "%s.%s.founder_want", citystr,
1191 aitstr);
1192 city_data->founder_boat
1193 = secfile_lookup_bool_default(file, (city_data->founder_want < 0),
1194 "%s.%s.founder_boat", citystr, aitstr);
1195}
1196
1197/**********************************************************************/
1210 const struct city *pcity)
1211{
1212 struct action *paction = action_by_number(act_id);
1213
1215 "Action %d don't exist.", act_id);
1216
1217 if (action_get_target_kind(paction) != ATK_CITY) {
1218 /* Not city targeted in this ruleset. */
1219 return 0;
1220 }
1221
1222 switch (paction->result) {
1223 case ACTRES_SPY_INCITE_CITY:
1224 /* Copied from the evaluation of the No_Incite effect */
1225 return MAX((game.server.diplchance * 2
1228 - game.server.incite_unit_factor * 5, 0);
1229
1230 /* Really bad for the city owner. */
1231 case ACTRES_SPY_NUKE:
1232 case ACTRES_CONQUER_CITY:
1233 /* The ai will never destroy their own city to keep it out of enemy
1234 * hands. If it starts supporting it this value should change. */
1235 case ACTRES_DESTROY_CITY:
1236 return 20;
1237
1238 /* Bad for the city owner. */
1239 case ACTRES_SPY_POISON:
1240 case ACTRES_SPY_SPREAD_PLAGUE:
1241 case ACTRES_SPY_SABOTAGE_CITY:
1242 case ACTRES_SPY_TARGETED_SABOTAGE_CITY:
1243 case ACTRES_SPY_SABOTAGE_CITY_PRODUCTION:
1244 case ACTRES_STRIKE_BUILDING:
1245 case ACTRES_STRIKE_PRODUCTION:
1246 case ACTRES_SPY_STEAL_GOLD:
1247 case ACTRES_NUKE:
1248 /* TODO: Individual and well balanced values */
1249 return 10;
1250
1251 /* Good for an enemy */
1252 case ACTRES_SPY_STEAL_TECH:
1253 case ACTRES_SPY_TARGETED_STEAL_TECH:
1254 case ACTRES_STEAL_MAPS:
1255 /* TODO: Individual and well balanced values */
1256 return 8;
1257
1258 /* Could be worse */
1259 case ACTRES_ESTABLISH_EMBASSY:
1260 case ACTRES_SPY_INVESTIGATE_CITY:
1261 case ACTRES_MARKETPLACE:
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:
1269 case ACTRES_DISBAND_UNIT_RECOVER:
1270 case ACTRES_HOME_CITY:
1271 case ACTRES_UPGRADE_UNIT:
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. */
1281 case ACTRES_PARADROP_CONQUER:
1282 /* Against the tile so potential city effects are overlooked for now. */
1283 case ACTRES_SPY_BRIBE_UNIT:
1284 case ACTRES_SPY_SABOTAGE_UNIT:
1285 case ACTRES_SPY_ATTACK:
1286 case ACTRES_EXPEL_UNIT:
1287 case ACTRES_DISBAND_UNIT:
1288 case ACTRES_CAPTURE_UNITS:
1289 case ACTRES_BOMBARD:
1290 case ACTRES_FOUND_CITY:
1291 case ACTRES_NUKE_UNITS:
1292 case ACTRES_PARADROP:
1293 case ACTRES_ATTACK:
1294 case ACTRES_HEAL_UNIT:
1295 case ACTRES_TRANSFORM_TERRAIN:
1296 case ACTRES_CULTIVATE:
1297 case ACTRES_PLANT:
1298 case ACTRES_PILLAGE:
1299 case ACTRES_CLEAN_POLLUTION:
1300 case ACTRES_CLEAN_FALLOUT:
1301 case ACTRES_FORTIFY:
1302 case ACTRES_ROAD:
1303 case ACTRES_CONVERT:
1304 case ACTRES_BASE:
1305 case ACTRES_MINE:
1306 case ACTRES_IRRIGATE:
1307 case ACTRES_TRANSPORT_ALIGHT:
1308 case ACTRES_TRANSPORT_BOARD:
1309 case ACTRES_TRANSPORT_UNLOAD:
1310 case ACTRES_TRANSPORT_DISEMBARK:
1311 case ACTRES_TRANSPORT_EMBARK:
1312 case ACTRES_CONQUER_EXTRAS:
1313 case ACTRES_HUT_ENTER:
1314 case ACTRES_HUT_FRIGHTEN:
1315 case ACTRES_UNIT_MOVE:
1316 case ACTRES_HOMELESS:
1317 fc_assert_msg(action_id_get_target_kind(act_id) == ATK_CITY,
1318 "Action not aimed at cities");
1319 }
1320
1321 /* Wrong action. Ignore it. */
1322 return 0;
1323}
1324
1325/**********************************************************************/
1334static bool adjust_wants_for_reqs(struct ai_type *ait,
1335 struct player *pplayer,
1336 struct city *pcity,
1337 const struct impr_type *pimprove,
1338 const adv_want v)
1339{
1340 bool all_met = TRUE;
1341 int n_needed_techs = 0;
1342 int n_needed_improvements = 0;
1343 struct tech_vector needed_techs;
1344 struct impr_vector needed_improvements;
1345
1346 const struct req_context context = {
1347 .player = pplayer,
1348 .city = pcity,
1349 .tile = city_tile(pcity),
1350 .building = pimprove,
1351 };
1352
1353 tech_vector_init(&needed_techs);
1354 impr_vector_init(&needed_improvements);
1355
1356 requirement_vector_iterate(&pimprove->reqs, preq) {
1357 const bool active = is_req_active(&context, NULL, preq, RPT_POSSIBLE);
1358
1359 if (VUT_ADVANCE == preq->source.kind && preq->present && !active) {
1360 /* Found a missing technology requirement for this improvement. */
1361 tech_vector_append(&needed_techs, preq->source.value.advance);
1362 } else if (VUT_IMPROVEMENT == preq->source.kind && preq->present && !active) {
1363 /* Found a missing improvement requirement for this improvement.
1364 * For example, in the default ruleset a city must have a Library
1365 * before it can have a University. */
1366 impr_vector_append(&needed_improvements, preq->source.value.building);
1367 }
1368 all_met = all_met && active;
1370
1371 /* If v is negative, the improvement is not worth building,
1372 * but there is no need to punish research of the technologies
1373 * that would make it available.
1374 */
1375 n_needed_techs = tech_vector_size(&needed_techs);
1376 if (0 < v && 0 < n_needed_techs) {
1377 /* Tell AI module how much we want this improvement and what techs are
1378 * required to get it. */
1379 const adv_want dv = v / n_needed_techs;
1380
1381 want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1382 &needed_techs, dv);
1383 }
1384
1385 /* If v is negative, the improvement is not worth building,
1386 * but there is no need to punish building the improvements
1387 * that would make it available.
1388 */
1389 n_needed_improvements = impr_vector_size(&needed_improvements);
1390 if (0 < v && 0 < n_needed_improvements) {
1391 /* Because we want this improvement,
1392 * we want the improvements that will make it possible */
1393 const adv_want dv = v / (n_needed_improvements * 4); /* WAG */
1394 int i;
1395
1396 for (i = 0; i < n_needed_improvements; i++) {
1397 const struct impr_type *needed_impr = *impr_vector_get(&needed_improvements, i);
1398 /* TODO: increase the want for the needed_impr,
1399 * if we can build it now */
1400 /* Recurse */
1401 (void) adjust_wants_for_reqs(ait, pplayer, pcity, needed_impr, dv);
1402 }
1403 }
1404
1405 /* TODO: use a similar method to increase wants for governments
1406 * that will make this improvement possible? */
1407
1408 tech_vector_free(&needed_techs);
1409 impr_vector_free(&needed_improvements);
1410
1411 return all_met;
1412}
1413
1414/**********************************************************************/
1419adv_want dai_city_want(struct player *pplayer, struct city *acity,
1420 struct adv_data *adv, struct impr_type *pimprove)
1421{
1422 adv_want want = 0;
1423 int prod[O_LAST], bonus[O_LAST], waste[O_LAST];
1424 const struct civ_map *nmap = &(wld.map);
1425
1426 memset(prod, 0, O_LAST * sizeof(*prod));
1427 if (NULL != pimprove
1428 && adv->impr_calc[improvement_index(pimprove)] == ADV_IMPR_CALCULATE_FULL) {
1429 struct tile *acenter = city_tile(acity);
1430 bool celebrating = base_city_celebrating(acity);
1431
1432 /* The below calculation mostly duplicates get_worked_tile_output().
1433 * We do this only for buildings that we know may change tile
1434 * outputs. */
1435 city_tile_iterate(nmap, city_map_radius_sq_get(acity), acenter, ptile) {
1436 if (tile_worked(ptile) == acity) {
1438 prod[o] += city_tile_output(acity, ptile, celebrating, o);
1440 }
1442
1443 add_specialist_output(acity, prod);
1444 } else {
1445 fc_assert(sizeof(*prod) == sizeof(*acity->citizen_base));
1446 memcpy(prod, acity->citizen_base, O_LAST * sizeof(*prod));
1447 }
1448
1449 trade_routes_iterate(acity, proute) {
1450 prod[O_TRADE] += proute->value;
1452 prod[O_GOLD] += get_city_tithes_bonus(acity);
1454 bonus[o] = get_final_city_output_bonus(acity, o);
1455 waste[o] = city_waste(acity, o, prod[o] * bonus[o] / 100, NULL);
1457 add_tax_income(pplayer,
1458 prod[O_TRADE] * bonus[O_TRADE] / 100 - waste[O_TRADE],
1459 prod);
1461 prod[o] = prod[o] * bonus[o] / 100 - waste[o];
1463
1464 city_built_iterate(acity, upkept) {
1465 prod[O_GOLD] -= city_improvement_upkeep(acity, upkept);
1467 /* Unit upkeep isn't handled here. Unless we do a full city_refresh it
1468 * won't be changed anyway. */
1469
1470 want += prod[O_FOOD] * adv->food_priority;
1471 if (prod[O_SHIELD] != 0) {
1472 want += prod[O_SHIELD] * adv->shield_priority;
1473 want -= city_pollution(acity, prod[O_SHIELD]) * adv->pollution_priority;
1474 }
1475 want += prod[O_LUXURY] * adv->luxury_priority;
1476 want += prod[O_SCIENCE] * adv->science_priority;
1477 if (pplayer->economic.tax > 50) {
1478 /* Increased tax rate indicates that we've had gold shortage which
1479 * we are trying to fill with taxes. Consider gold more critical
1480 * than usually.
1481 * Smallest tax rate we can have here is 60% -> factor (60 - 40) / 14.0 = 1.43 */
1482 want += prod[O_GOLD] * adv->gold_priority * (pplayer->economic.tax - 40) / 14.0;
1483 } else {
1484 want += prod[O_GOLD] * adv->gold_priority;
1485 }
1486
1487 return want;
1488}
1489
1490/**********************************************************************/
1494static adv_want base_want(struct ai_type *ait, struct player *pplayer,
1495 struct city *pcity, struct impr_type *pimprove)
1496{
1497 struct adv_data *adv = adv_data_get(pplayer, NULL);
1498 adv_want final_want = 0;
1499 int wonder_player_id = WONDER_NOT_OWNED;
1500 int wonder_city_id = WONDER_NOT_BUILT;
1501
1502 if (adv->impr_calc[improvement_index(pimprove)] == ADV_IMPR_ESTIMATE) {
1503 return 0; /* Nothing to calculate here. */
1504 }
1505
1506 if (!can_city_build_improvement_now(pcity, pimprove)
1507 || (is_small_wonder(pimprove)
1508 && NULL != city_from_small_wonder(pplayer, pimprove))) {
1509 return 0;
1510 }
1511
1512 if (is_wonder(pimprove)) {
1513 if (is_great_wonder(pimprove)) {
1514 wonder_player_id =
1516 }
1517 wonder_city_id = pplayer->wonders[improvement_index(pimprove)];
1518 }
1519 /* Add the improvement */
1520 city_add_improvement(pcity, pimprove);
1521
1522 /* Stir, then compare notes */
1523 city_range_iterate(pcity, pplayer->cities,
1524 adv->impr_range[improvement_index(pimprove)], acity) {
1525 final_want += dai_city_want(pplayer, acity, adv, pimprove)
1526 - def_ai_city_data(acity, ait)->worth;
1528
1529 /* Restore */
1530 city_remove_improvement(pcity, pimprove);
1531 if (is_wonder(pimprove)) {
1532 if (is_great_wonder(pimprove)) {
1534 wonder_player_id;
1535 }
1536
1537 pplayer->wonders[improvement_index(pimprove)] = wonder_city_id;
1538 }
1539
1540 return final_want;
1541}
1542
1543/**********************************************************************/
1573 struct player *pplayer,
1574 struct city *pcity,
1575 struct impr_type *pimprove,
1576 const bool already)
1577{
1578 adv_want v = 0;
1579 int cities[REQ_RANGE_COUNT];
1580 int nplayers = normal_player_count();
1581 struct adv_data *ai = adv_data_get(pplayer, NULL);
1582 bool capital = is_capital(pcity);
1583 bool can_build = TRUE;
1584 struct universal source = {
1585 .kind = VUT_IMPROVEMENT,
1586 .value = {.building = pimprove}
1587 };
1588 const bool is_coinage = improvement_has_flag(pimprove, IF_GOLD);
1589 int turns = 9999;
1590 int place = tile_continent(pcity->tile);
1591
1592 /* FIXME: Do we really need the effects check to be made *without*
1593 * passing the city tile? */
1594 const struct req_context effect_ctxt = {
1595 .player = pplayer,
1596 .city = pcity,
1597 .building = pimprove,
1598 };
1599
1600 /* Do NOT pass building here, as the action might be about
1601 * targeting some completely different building, AND
1602 * the check to see if the action is possible before
1603 * the building is there is also ignoring the buildings.
1604 * We don't want those two results to differ for
1605 * an unrelated reason to what we are evaluating. */
1606 const struct req_context actenabler_ctxt = {
1607 .player = pplayer,
1608 .city = pcity,
1609 .tile = city_tile(pcity),
1610 };
1611
1612 /* Remove team members from the equation */
1613 players_iterate(aplayer) {
1614 if (aplayer->team
1615 && aplayer->team == pplayer->team
1616 && aplayer != pplayer) {
1617 nplayers--;
1618 }
1620
1621 if (is_coinage) {
1622 /* Since coinage contains some entirely spurious ruleset values,
1623 * we need to hard-code a sensible want.
1624 * We must otherwise handle the special IF_GOLD improvement
1625 * like the others, so the AI will research techs that make it available,
1626 * for rulesets that do not provide it from the start.
1627 */
1628 v += TRADE_WEIGHTING / 10;
1629 } else {
1630 /* Base want is calculated above using a more direct approach. */
1631 v += base_want(ait, pplayer, pcity, pimprove);
1632 if (v != 0) {
1633 CITY_LOG(LOG_DEBUG, pcity, "%s base_want is " ADV_WANT_PRINTF " (range=%d)",
1634 improvement_rule_name(pimprove),
1635 v,
1636 ai->impr_range[improvement_index(pimprove)]);
1637 }
1638 }
1639
1640 if (!is_coinage) {
1641 /* Adjust by building cost */
1642 /* FIXME: ought to reduce by upkeep cost and amortise by building cost */
1643 v -= (impr_build_shield_cost(pcity, pimprove)
1644 / (pcity->surplus[O_SHIELD] * 10 + 1));
1645 }
1646
1647 /* Find number of cities per range. */
1648 cities[REQ_RANGE_PLAYER] = city_list_size(pplayer->cities);
1649 /* kludge -- Number of *our* cities in these ranges. */
1650 cities[REQ_RANGE_WORLD] = cities[REQ_RANGE_ALLIANCE] = cities[REQ_RANGE_TEAM]
1651 = cities[REQ_RANGE_PLAYER];
1652
1653 if (place < 0) {
1654 cities[REQ_RANGE_CONTINENT] = 1;
1655 } else {
1656 cities[REQ_RANGE_CONTINENT] = ai->stats.cities[place];
1657 }
1658
1659 /* All the trade partners and the city being considered. */
1660 cities[REQ_RANGE_TRADE_ROUTE] = city_num_trade_routes(pcity) + 1;
1661
1662 cities[REQ_RANGE_CITY] = cities[REQ_RANGE_LOCAL] = 1;
1663
1664 /* Invalid building range */
1665 cities[REQ_RANGE_ADJACENT] = cities[REQ_RANGE_CADJACENT] = 0;
1666
1667 players_iterate(aplayer) {
1668 int potential = (aplayer->server.bulbs_last_turn
1669 + city_list_size(aplayer->cities) + 1);
1670
1671 if (potential > 0) {
1672 requirement_vector_iterate(&pimprove->obsolete_by, pobs) {
1673 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1674 turns = MIN(turns,
1676 advance_number(pobs->source.value.advance))
1677 / (potential + 1));
1678 }
1680 }
1682
1684 enum req_range range = REQ_RANGE_MAX;
1685 bool active = TRUE;
1686 int n_needed_techs = 0;
1687 struct tech_vector needed_techs;
1688 bool present = TRUE;
1689 bool impossible_to_get = FALSE;
1690
1691 tech_vector_init(&needed_techs);
1692
1693 requirement_vector_iterate(&peffect->reqs, preq) {
1694 /* Check if all the requirements for the currently evaluated effect
1695 * are met, except for having the building that we are evaluating. */
1696 if (VUT_IMPROVEMENT == preq->source.kind
1697 && preq->source.value.building == pimprove) {
1698 if (preq->range < range) {
1699 /* More limited range */
1700 range = preq->range;
1701 }
1702 present = preq->present;
1703 continue;
1704 }
1705 if (!is_req_active(&effect_ctxt, NULL, preq, RPT_POSSIBLE)) {
1706 active = FALSE;
1707 if (VUT_ADVANCE == preq->source.kind && preq->present) {
1708 /* This missing requirement is a missing tech requirement.
1709 * This will be for some additional effect
1710 * (For example, in the default ruleset, Mysticism increases
1711 * the effect of Temples). */
1712 tech_vector_append(&needed_techs, preq->source.value.advance);
1713 } else if (!dai_can_requirement_be_met_in_city(preq, pplayer, pcity)) {
1714 impossible_to_get = TRUE;
1715 }
1716 }
1718
1719 n_needed_techs = tech_vector_size(&needed_techs);
1720 if ((active || n_needed_techs) && !impossible_to_get) {
1721 adv_want v1 = dai_effect_value(pplayer, ai, pcity, capital,
1722 turns, peffect, range, nplayers);
1723 /* v1 could be negative (the effect could be undesirable),
1724 * although it is usually positive.
1725 * For example, in the default ruleset, Communism decreases the
1726 * effectiveness of a Cathedral. */
1727
1728 if (!present) {
1729 /* Building removes the effect */
1730 /* Currently v1 is (v + delta). Make it (v - delta) instead */
1731 v1 = -v1;
1732 }
1733
1734 if (active) {
1735 v += v1;
1736 } else if (v1 > 0) {
1737 /* If value of the effect is negative, do not hold it against
1738 * the tech - having the tech wont force one to build the
1739 * building. */
1740
1741 /* We might want the technology that will enable this
1742 * (additional) effect.
1743 * The better the effect, the more we want the technology.
1744 * We are more interested in (additional) effects that enhance
1745 * buildings we already have.
1746 */
1747 const int a = already? 5: 4; /* WAG */
1748 const adv_want dv = v1 * a / (4 * n_needed_techs);
1749
1750 want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1751 &needed_techs, dv);
1752 }
1753 }
1754
1755 tech_vector_free(&needed_techs);
1757
1758 /* Can the city be the target of an action? */
1759 action_iterate (act_id) {
1760 bool is_possible;
1761 bool will_be_possible = FALSE;
1762 enum req_range max_range;
1763 int act_neg_util;
1764
1765 /* Is the action relevant? */
1766 if (action_id_get_target_kind(act_id) != ATK_CITY) {
1767 continue;
1768 }
1769
1770 /* No range found yet. Local is the most narrow range. */
1771 max_range = REQ_RANGE_LOCAL;
1772
1773 /* Is it possible to do the action to the city right now?
1774 *
1775 * (DiplRel requirements are ignored since actor_player is NULL) */
1776 is_possible = is_action_possible_on_city(act_id, NULL, pcity);
1777
1778 /* Will it be possible to do the action to the city if the building is
1779 * built? */
1781 enabler) {
1782 bool active = TRUE;
1783 enum req_range range = REQ_RANGE_LOCAL;
1784
1785 requirement_vector_iterate(&(enabler->target_reqs), preq) {
1786 if (VUT_IMPROVEMENT == preq->source.kind
1787 && preq->source.value.building == pimprove) {
1788 /* Pretend the building is there */
1789 if (preq->present) {
1790 range = preq->range; /* Assumption: Max one pr vector */
1791 continue;
1792 } else {
1793 active = FALSE;
1794 break;
1795 }
1796 } else if (!is_req_active(&actenabler_ctxt, NULL, preq, RPT_POSSIBLE)) {
1797 active = FALSE;
1798 break;
1799 }
1801
1802 if (active) {
1803 will_be_possible = TRUE;
1804
1805 /* Store the widest range that enables the action. */
1806 if (max_range < range) {
1807 max_range = range;
1808 }
1809
1810 /* Don't break the iteration even if the action is enabled. There
1811 * could be a wider range in an active action enabler not yet seen.
1812 */
1813 }
1815
1816 /* Will the building significantly change the ability to target
1817 * the city? */
1818 if (is_possible == will_be_possible) {
1819 continue;
1820 }
1821
1822 /* How undesirable is it that the city may be a target? */
1823 act_neg_util = action_target_neg_util(act_id, pcity);
1824
1825 /* Multiply the desire by number of cities in range.
1826 * Note: This is a simplification. If the action can be done or not
1827 * _may_ be uncanged or changed in the opposite direction in the other
1828 * cities in the range. */
1829 act_neg_util = cities[max_range] * act_neg_util;
1830
1831 /* Consider the utility of being a potential target.
1832 * Remember: act_util is the negative utility of being a target. */
1833 if (will_be_possible) {
1834 v -= act_neg_util;
1835 } else {
1836 v += act_neg_util;
1837 }
1839
1840 if (already) {
1841 /* Discourage research of the technology that would make this building
1842 * obsolete. The bigger the desire for this building, the more
1843 * we want to discourage the technology. */
1844 dont_want_tech_obsoleting_impr(ait, pplayer, pcity, pimprove, v);
1845 } else {
1846 /* Increase the want for technologies that will enable
1847 * construction of this improvement, if necessary.
1848 */
1849 const bool all_met = adjust_wants_for_reqs(ait, pplayer, pcity, pimprove, v);
1850 can_build = can_build && all_met;
1851 }
1852
1853 if (is_coinage && can_build) {
1854 /* Could have a negative want for coinage,
1855 * if we have some stock in a building already. */
1856 pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1857 } else if (!already && can_build) {
1858 const struct research *presearch = research_get(pplayer);
1859
1860 /* Convert the base 'want' into a building want
1861 * by applying various adjustments */
1862
1863 /* Would it mean losing shields? */
1864 if ((VUT_UTYPE == pcity->production.kind
1866 && !is_wonder(pimprove))
1867 || (!is_wonder(pcity->production.value.building)
1868 && is_wonder(pimprove)))
1869 && pcity->turn_last_built != game.info.turn) {
1870 if (has_handicap(pplayer, H_PRODCHGPEN)) {
1871 v -= pcity->shield_stock * SHIELD_WEIGHTING / 4;
1872 } else {
1873 v -= pcity->shield_stock * SHIELD_WEIGHTING / 15;
1874 }
1875 }
1876
1877 /* Reduce want if building gets obsoleted soon */
1878 requirement_vector_iterate(&pimprove->obsolete_by, pobs) {
1879 if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1880 int num_tech = research_goal_unknown_techs(presearch,
1881 advance_number(pobs->source.value.advance));
1882
1883 v -= v / MAX(1, num_tech);
1884 }
1886
1887 /* Are we wonder city? Try to avoid building non-wonders very much. */
1888 if (pcity->id == ai->wonder_city && !is_wonder(pimprove)) {
1889 v /= 5;
1890 }
1891
1892 /* Set */
1893 pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1894 }
1895 /* Else we either have the improvement already,
1896 * or we can not build it (yet) */
1897}
1898
1899/**********************************************************************/
1908static bool should_force_recalc(struct city *pcity)
1909{
1910 return city_built_last_turn(pcity)
1911 || (VUT_IMPROVEMENT == pcity->production.kind
1912 && !improvement_has_flag(pcity->production.value.building, IF_GOLD)
1914 (pcity, pcity->production.value.building));
1915}
1916
1917/**********************************************************************/
1921void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
1922{
1923 struct adv_data *ai = adv_data_get(pplayer, NULL);
1924
1925 /* Find current worth of cities and cache this. */
1926 city_list_iterate(pplayer->cities, pcity) {
1927 def_ai_city_data(pcity, ait)->worth = dai_city_want(pplayer, pcity, ai, NULL);
1929}
1930
1931/**********************************************************************/
1937void dai_build_adv_adjust(struct ai_type *ait, struct player *pplayer,
1938 struct city *wonder_city)
1939{
1940 /* Clear old building wants.
1941 * Do this separately from the iteration over improvement types
1942 * because each iteration could actually update more than one improvement,
1943 * if improvements have improvements as requirements.
1944 */
1945 city_list_iterate(pplayer->cities, pcity) {
1946 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1947
1948 if (city_data->building_turn <= game.info.turn) {
1949 /* Do a scheduled recalculation this turn */
1950 improvement_iterate(pimprove) {
1951 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1953 } else if (should_force_recalc(pcity)) {
1954 /* Do an emergency recalculation this turn. */
1955 city_data->building_wait = city_data->building_turn
1956 - game.info.turn;
1957 city_data->building_turn = game.info.turn;
1958
1959 improvement_iterate(pimprove) {
1960 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1962 }
1964
1965 improvement_iterate(pimprove) {
1966 const bool is_coinage = improvement_has_flag(pimprove, IF_GOLD);
1967
1968 /* Handle coinage specially because you can never complete coinage */
1969 if (is_coinage
1970 || can_player_build_improvement_later(pplayer, pimprove)) {
1971 city_list_iterate(pplayer->cities, pcity) {
1972 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1973
1974 if (pcity != wonder_city && is_wonder(pimprove)) {
1975 /* Only wonder city should build wonders! */
1976 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1977 } else if (city_data->building_turn <= game.info.turn) {
1978 /* Building wants vary relatively slowly, so not worthwhile
1979 * recalculating them every turn.
1980 * We DO want to calculate (tech) wants because of buildings
1981 * we already have. */
1982 const bool already = city_has_building(pcity, pimprove);
1983 int idx = improvement_index(pimprove);
1984
1985 adjust_improvement_wants_by_effects(ait, pplayer, pcity,
1986 pimprove, already);
1987
1988 fc_assert(!(already
1989 && 0 < pcity->server.adv->building_want[idx]));
1990
1991 if (is_great_wonder(pimprove)) {
1992 /* Not only would we get the wonder, but we would also prevent
1993 * opponents from getting it. */
1994 pcity->server.adv->building_want[idx] *= 1.5;
1995
1996 if (pcity->production.kind == VUT_IMPROVEMENT
1997 && is_great_wonder(pcity->production.value.building)) {
1998 /* If we already are building a great wonder, prefer continuing
1999 * to do so over stopping it */
2000 pcity->server.adv->building_want[idx] *= 1.25;
2001 }
2002 }
2003
2004 /* If I am not an expansionist, I want buildings more than units */
2005 if (pcity->server.adv->building_want[idx] > 0) {
2006 pcity->server.adv->building_want[idx]
2007 = pcity->server.adv->building_want[idx]
2009 / ai_trait_get_value(TRAIT_EXPANSIONIST, pplayer);
2010 }
2011 }
2012 /* else wait until a later turn */
2014 } else {
2015 /* An impossible improvement */
2016 city_list_iterate(pplayer->cities, pcity) {
2017 pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
2019 }
2021
2022#ifdef FREECIV_DEBUG
2023 /* This logging is relatively expensive, so activate only if necessary */
2024 city_list_iterate(pplayer->cities, pcity) {
2025 improvement_iterate(pimprove) {
2026 if (pcity->server.adv->building_want[improvement_index(pimprove)] != 0) {
2027 CITY_LOG(LOG_DEBUG, pcity, "want to build %s with " ADV_WANT_PRINTF,
2028 improvement_rule_name(pimprove),
2029 pcity->server.adv->building_want[improvement_index(pimprove)]);
2030 }
2033#endif /* FREECIV_DEBUG */
2034
2035 /* Reset recalc counter */
2036 city_list_iterate(pplayer->cities, pcity) {
2037 struct ai_city *city_data = def_ai_city_data(pcity, ait);
2038
2039 if (city_data->building_turn <= game.info.turn) {
2040 /* This will spread recalcs out so that no one turn end is
2041 * much longer than others */
2043 city_data->building_turn = game.info.turn
2044 + city_data->building_wait;
2045 }
2047}
2048
2049/**********************************************************************/
2052void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity, bool *result)
2053{
2054 if (def_ai_city_data(pcity, ait)->grave_danger > 0) {
2055 *result = FALSE;
2056 } else {
2057 *result = TRUE;
2058 }
2059}
2060
2061/**********************************************************************/
2065static bool dai_cant_help_req(const struct req_context *context,
2066 const struct requirement *req)
2067{
2068 switch (req->source.kind) {
2069 /* Unskilled in channel digging and merchantry */
2070 case VUT_TERRAIN:
2071 case VUT_EXTRA:
2072 case VUT_GOOD:
2073 case VUT_TERRAINCLASS:
2074 case VUT_TERRFLAG:
2075 case VUT_TERRAINALTER:
2076 return !is_req_active(context, NULL, req, RPT_POSSIBLE);
2077 default:
2078 return is_req_preventing(context, NULL, req, RPT_POSSIBLE) > REQUCH_NO;
2079 }
2080}
2081
2082/**********************************************************************/
2086 const struct impr_type *pimprove)
2087{
2088 const struct req_context city_ctxt = {
2089 .player = city_owner(pcity),
2090 .city = pcity,
2091 .tile = city_tile(pcity),
2092 };
2093
2094 /* FIXME: AI may be too stupid to sell obsoleting improvements
2095 * from the city that are _not_ checked here. */
2097 pimprove)) {
2098 return FALSE;
2099 }
2100
2101 /* Check for requirements that aren't met and that are unchanging (so
2102 * they can never be met). */
2103 requirement_vector_iterate(&pimprove->reqs, preq) {
2104 if (dai_cant_help_req(&city_ctxt, preq)) {
2105 return FALSE;
2106 }
2108
2109 return TRUE;
2110}
2111
2112/**********************************************************************/
2115bool
2117 const struct impr_type *pimprove)
2118{
2119 const struct req_context context = { .player = p };
2120
2121 if (!valid_improvement(pimprove)) {
2122 return FALSE;
2123 }
2124 if (improvement_obsolete(p, pimprove, NULL)) {
2125 return FALSE;
2126 }
2127 if (is_great_wonder(pimprove) && !great_wonder_is_available(pimprove)) {
2128 /* Can't build wonder if already built */
2129 return FALSE;
2130 }
2131
2132 /* Check for requirements that aren't met and that are unchanging (so
2133 * they can never be met). */
2134 requirement_vector_iterate(&pimprove->reqs, preq) {
2135 if (preq->range >= REQ_RANGE_PLAYER
2136 && dai_cant_help_req(&context, preq)) {
2137 return FALSE;
2138 }
2140 /* FIXME: should check some "unchanging" reqs here - like if there's
2141 * a nation requirement, we can go ahead and check it now. */
2142
2143 return TRUE;
2144}
2145
2146/**********************************************************************/
2153 enum effect_type effect_type,
2154 const struct unit_type *utype)
2155{
2156 int greatest_value = 0;
2157 const struct impr_type *best_building = NULL;
2158 const struct req_context context = {
2159 .player = city_owner(pcity),
2160 .city = pcity,
2161 .tile = city_tile(pcity),
2162 .unittype = utype,
2163 };
2164
2165 effect_list_iterate(get_effects(effect_type), peffect) {
2166 if (peffect->value > greatest_value) {
2167 const struct impr_type *building = NULL;
2168 bool wrong_unit = FALSE;
2169
2170 requirement_vector_iterate(&peffect->reqs, preq) {
2171 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2172 building = preq->source.value.building;
2173
2174 if (!can_city_build_improvement_now(pcity, building)
2175 || !is_improvement(building)) {
2176 building = NULL;
2177 break;
2178 }
2179 } else if (utype != NULL
2180 && !is_req_active(&context, NULL, preq, RPT_POSSIBLE)) {
2181 /* Effect requires other kind of unit than what we are interested about */
2182 wrong_unit = TRUE;
2183 break;
2184 }
2186 if (!wrong_unit && building != NULL) {
2187 best_building = building;
2188 greatest_value = peffect->value;
2189 }
2190 }
2192
2193 if (best_building) {
2194 return improvement_number(best_building);
2195 }
2196 return B_LAST;
2197}
bool is_action_possible_on_city(action_id act_id, const struct player *actor_player, const struct city *target_city)
Definition actions.c:7142
void action_list_end(action_id *act_list, int size)
Definition actions.c:7397
bool action_ever_possible(action_id action)
Definition actions.c:9305
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:4755
void action_list_add_all_by_result(action_id *act_list, int *position, enum action_result result)
Definition actions.c:7414
bool action_id_exists(const action_id act_id)
Definition actions.c:1697
enum action_target_kind action_get_target_kind(const struct action *paction)
Definition actions.c:1740
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:5145
struct action_enabler_list * action_enablers_for_action(action_id action)
Definition actions.c:2475
static struct action * action_by_number(action_id act_id)
Definition actions.h:638
#define action_enabler_list_iterate_end
Definition actions.h:457
#define action_id_get_role(act_id)
Definition actions.h:708
#define action_array_iterate(_act_list_, _act_id_)
Definition actions.h:504
#define action_array_iterate_end
Definition actions.h:516
#define action_iterate_end
Definition actions.h:472
#define MAX_NUM_ACTIONS
Definition actions.h:296
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition actions.h:455
#define action_iterate(_act_)
Definition actions.h:467
#define action_id_get_target_kind(act_id)
Definition actions.h:655
void building_advisor(struct player *pplayer)
#define TRADE_WEIGHTING
Definition advbuilding.h:21
#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:593
@ ADV_IMPR_CALCULATE_FULL
Definition advdata.h:35
@ ADV_IMPR_ESTIMATE
Definition advdata.h:36
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Definition aidata.c:308
void dai_calc_data(const struct player *pplayer, int *trade, int *expenses, int *income)
Definition aihand.c:118
#define TECH_LOG(ait, loglevel, pplayer, padvance, msg,...)
Definition ailog.h:36
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition aiplayer.h:54
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition aiplayer.h:42
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition aiplayer.h:48
int dai_gold_reserve(struct player *pplayer)
Definition aitools.c:1309
#define DAI_WANT_MILITARY_EMERGENCY
Definition aitools.h:32
int ai_trait_get_value(enum trait tr, struct player *pplayer)
Definition aitraits.c:68
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Definition aiunit.c:3431
#define IS_ATTACKER(ptype)
Definition aiunit.h:70
adv_want settler_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 settlermap *state)
bool base_city_celebrating(const struct city *pcity)
Definition city.c:1610
bool is_free_worked(const struct city *pcity, const struct tile *ptile)
Definition city.c:3500
int city_granary_size(int city_size)
Definition city.c:2104
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Definition city.c:3516
bool city_built_last_turn(const struct city *pcity)
Definition city.c:2251
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown)
Definition city.c:3122
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1216
bool is_capital(const struct city *pcity)
Definition city.c:1552
const char * city_name_get(const struct city *pcity)
Definition city.c:1115
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3284
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1231
void add_tax_income(const struct player *pplayer, int trade, int *output)
Definition city.c:2217
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:839
int get_city_tithes_bonus(const struct city *pcity)
Definition city.c:2199
bool city_unhappy(const struct city *pcity)
Definition city.c:1599
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Definition city.c:2184
int city_pollution(const struct city *pcity, int shield_total)
Definition city.c:2723
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3270
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:132
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:276
void add_specialist_output(const struct city *pcity, int *output)
Definition city.c:2304
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1259
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:927
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_tile(_pcity_)
Definition city.h:544
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
@ CITIZEN_ANGRY
Definition city.h:263
@ CITIZEN_UNHAPPY
Definition city.h:262
#define output_type_iterate(output)
Definition city.h:821
#define city_owner(_pcity_)
Definition city.h:543
#define city_list_iterate_end
Definition city.h:490
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:222
@ FEELING_FINAL
Definition city.h:276
#define city_tile_iterate_end
Definition city.h:230
#define city_built_iterate(_pcity, _p)
Definition city.h:810
#define city_built_iterate_end
Definition city.h:816
#define output_type_iterate_end
Definition city.h:827
void really_handle_city_buy(struct player *pplayer, struct city *pcity)
Definition cityhand.c:322
void really_handle_city_sell(struct player *pplayer, struct city *pcity, struct impr_type *pimprove)
Definition cityhand.c:261
void city_map_update_empty(struct city *pcity, struct tile *ptile)
Definition citytools.c:3164
void city_freeze_workers_queue(struct city *pcity)
Definition citytools.c:159
int city_production_buy_gold_cost(const struct city *pcity)
Definition citytools.c:3537
void sync_cities(void)
Definition citytools.c:3238
void change_build_target(struct player *pplayer, struct city *pcity, struct universal *target, enum event_type event)
Definition citytools.c:3075
void do_sell_building(struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const char *reason)
Definition citytools.c:2910
void city_thaw_workers_queue(void)
Definition citytools.c:196
int city_granary_savings(const struct city *pcity)
Definition cityturn.c:924
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:369
bool city_refresh(struct city *pcity)
Definition cityturn.c:161
static void try_to_sell_stuff(struct player *pplayer, struct city *pcity)
Definition daicity.c:357
#define AI_BA_RECALC_SPEED
Definition daicity.c:77
#define AI_CITY_RECALC_SPEED
Definition daicity.c:75
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:1334
static void unit_do_disband_trad(struct player *owner, struct unit *punit, const enum action_requester requester)
Definition daicity.c:454
static bool dai_cant_help_req(const struct req_context *context, const struct requirement *req)
Definition daicity.c:2065
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:158
static void increase_maxbuycost(struct player *pplayer, int new_value)
Definition daicity.c:373
static void dai_barbarian_choose_build(struct player *pplayer, struct city *pcity, struct adv_choice *choice)
Definition daicity.c:202
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:1140
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:1167
#define LOG_BUY
Definition daicity.c:69
static void dai_city_sell_noncritical(struct city *pcity, bool redundant_only)
Definition daicity.c:989
#define city_range_iterate_end
Definition daicity.c:104
static int unit_food_upkeep(struct unit *punit)
Definition daicity.c:693
void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:853
void dai_city_alloc(struct ai_type *ait, struct city *pcity)
Definition daicity.c:1113
#define LOG_EMERGENCY
Definition daicity.c:70
void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity, bool *result)
Definition daicity.c:2052
static bool should_force_recalc(struct city *pcity)
Definition daicity.c:1908
bool dai_can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
Definition daicity.c:2116
static int action_target_neg_util(action_id act_id, const struct city *pcity)
Definition daicity.c:1209
#define city_range_iterate(city_here, list, range, city)
Definition daicity.c:89
static void resolve_city_emergency(struct ai_type *ait, struct player *pplayer, struct city *pcity)
Definition daicity.c:1032
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:179
void dai_build_adv_adjust(struct ai_type *ait, struct player *pplayer, struct city *wonder_city)
Definition daicity.c:1937
static void contemplate_terrain_improvements(struct ai_type *ait, struct city *pcity)
Definition daicity.c:751
static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:525
#define LOG_WANT
Definition daicity.c:71
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:1572
void dai_city_free(struct ai_type *ait, struct city *pcity)
Definition daicity.c:1126
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:129
Impr_type_id dai_find_source_building(struct city *pcity, enum effect_type effect_type, const struct unit_type *utype)
Definition daicity.c:2152
void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
Definition daicity.c:1921
static void dai_upgrade_units(struct city *pcity, int limit, bool military)
Definition daicity.c:383
#define CITY_EMERGENCY(pcity)
Definition daicity.c:109
bool dai_can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Definition daicity.c:2085
adv_want dai_city_want(struct player *pplayer, struct city *acity, struct adv_data *adv, struct impr_type *pimprove)
Definition daicity.c:1419
static adv_want base_want(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove)
Definition daicity.c:1494
static int unit_foodbox_cost(struct unit *punit)
Definition daicity.c:712
static bool building_crucial(const struct player *plr, struct impr_type *pimprove, const struct city *pcity)
Definition daicity.c:967
static void dai_city_choose_build(struct ai_type *ait, struct player *pplayer, struct city *pcity)
Definition daicity.c:252
#define BUILDING_WAIT_MINIMUM
Definition daicity.h:39
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:718
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
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)
void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
void contemplate_new_city(struct ai_type *ait, struct city *pcity)
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
struct 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:73
bool ai_fuzzy(const struct player *pplayer, bool normal_decision)
Definition difficulty.c:339
struct effect_list * get_req_source_effects(struct universal *psource)
Definition effects.c:146
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:789
struct effect_list * get_effects(enum effect_type effect_type)
Definition effects.c:136
bool building_has_effect(const struct impr_type *pimprove, enum effect_type effect_type)
Definition effects.c:587
#define effect_list_iterate_end
Definition effects.h:375
#define effect_list_iterate(effect_list, peffect)
Definition effects.h:373
float adv_want
Definition fc_types.h:1206
int Impr_type_id
Definition fc_types.h:346
@ RPT_POSSIBLE
Definition fc_types.h:585
int action_id
Definition fc_types.h:359
#define ADV_WANT_PRINTF
Definition fc_types.h:1207
@ O_SHIELD
Definition fc_types.h:91
@ O_FOOD
Definition fc_types.h:91
@ O_TRADE
Definition fc_types.h:91
@ O_SCIENCE
Definition fc_types.h:91
@ O_LUXURY
Definition fc_types.h:91
@ O_GOLD
Definition fc_types.h:91
@ O_LAST
Definition fc_types.h:91
signed short Continent_id
Definition fc_types.h:342
#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:57
struct world wld
Definition game.c:58
struct city * owner
Definition citydlg.c:219
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)
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:181
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define fc_assert_action(condition, action)
Definition log.h:187
#define log_debug(message,...)
Definition log.h:115
#define log_normal(message,...)
Definition log.h:107
#define log_base(level, message,...)
Definition log.h:94
@ LOG_ERROR
Definition log.h:30
@ LOG_DEBUG
Definition log.h:34
#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:117
int player_get_expected_income(const struct player *pplayer)
Definition player.c:1262
const char * player_name(const struct player *pplayer)
Definition player.c:886
#define players_iterate_end
Definition player.h:535
#define players_iterate(_pplayer)
Definition player.h:530
static bool is_barbarian(const struct player *pplayer)
Definition player.h:488
int normal_player_count(void)
Definition plrhand.c:3034
#define fc_rand(_size)
Definition rand.h:34
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 player *other_player, 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 player *other_player, const struct requirement *req, enum req_problem_type prob_type)
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
#define REQ_RANGE_MAX
@ REQUCH_NO
int research_goal_unknown_techs(const struct research *presearch, Tech_type_id goal)
Definition research.c:747
int research_goal_bulbs_required(const struct research *presearch, Tech_type_id goal)
Definition research.c:769
struct research * research_get(const struct player *pplayer)
Definition research.c:126
#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
action_id id
Definition actions.h:380
enum action_result result
Definition actions.h:382
enum choice_type type
Definition advchoice.h:46
adv_want want
Definition advchoice.h:48
universals_u value
Definition advchoice.h:47
adv_want building_want[B_LAST]
Definition infracache.h:32
int pollution_priority
Definition advdata.h:115
struct adv_data::@92 stats
int shield_priority
Definition advdata.h:107
int * cities
Definition advdata.h:90
int * ocean_cities
Definition advdata.h:91
int luxury_priority
Definition advdata.h:109
int food_priority
Definition advdata.h:108
enum adv_improvement_status impr_calc[B_LAST]
Definition advdata.h:53
int wonder_city
Definition advdata.h:50
enum req_range impr_range[B_LAST]
Definition advdata.h:54
int science_priority
Definition advdata.h:111
int gold_priority
Definition advdata.h:110
struct adv_choice choice
Definition daicity.h:41
bool founder_boat
Definition daicity.h:64
int building_wait
Definition daicity.h:38
int founder_turn
Definition daicity.h:65
int founder_want
Definition daicity.h:66
unsigned int urgency
Definition daicity.h:49
int worker_want
Definition daicity.h:67
int building_turn
Definition daicity.h:37
adv_want worth
Definition daicity.h:35
unsigned int grave_danger
Definition daicity.h:48
struct unit_type * worker_type
Definition daicity.h:68
int * ocean_workers
Definition aidata.h:83
int * workers
Definition aidata.h:82
struct ai_plr::@267 stats
adv_want tech_want[A_LAST+1]
Definition aidata.h:103
Definition ai.h:50
void * private
Definition ai.h:53
Definition city.h:309
int turn_last_built
Definition city.h:373
int surplus[O_LAST]
Definition city.h:343
int id
Definition city.h:315
struct player * owner
Definition city.h:312
int citizen_base[O_LAST]
Definition city.h:347
int anarchy
Definition city.h:370
struct universal production
Definition city.h:382
struct adv_city * adv
Definition city.h:435
citizens feel[CITIZEN_LAST][FEELING_LAST]
Definition city.h:321
citizens specialists[SP_MAX]
Definition city.h:324
struct tile * tile
Definition city.h:311
int shield_stock
Definition city.h:355
struct city::@17::@19 server
struct unit_list * units_supported
Definition city.h:391
int incite_total_factor
Definition game.h:149
struct civ_game::@30::@34 server
int diplchance
Definition game.h:137
struct packet_game_info info
Definition game.h:89
int incite_improvement_factor
Definition game.h:148
int incite_unit_factor
Definition game.h:150
struct requirement_vector obsolete_by
Definition improvement.h:76
struct requirement_vector reqs
Definition improvement.h:75
int great_wonder_owners[B_LAST]
int maxbuycost
Definition player.h:120
struct city_list * cities
Definition player.h:281
struct player_ai ai_common
Definition player.h:288
int wonders[B_LAST]
Definition player.h:301
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:49
struct unit_list * units
Definition tile.h:57
int pop_cost
Definition unittype.h:493
int attack_strength
Definition unittype.h:495
Definition unit.h:138
int id
Definition unit.h:145
int homecity
Definition unit.h:146
struct player * owner
Definition unit.h:143
enum universals_n kind
Definition fc_types.h:758
universals_u value
Definition fc_types.h:757
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:113
#define TILE_XY(ptile)
Definition tile.h:42
#define tile_continent(_tile)
Definition tile.h:91
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:604
const struct impr_type * building
Definition fc_types.h:598
bool unit_is_alive(int id)
Definition unit.c:2237
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1524
bool is_field_unit(const struct unit *punit)
Definition unit.c:383
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:328
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1617
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:625
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1713
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1281
#define unit_tile(_pu)
Definition unit.h:395
#define unit_owner(_pu)
Definition unit.h:394
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:3175
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:3155
#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:2319
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:2981
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2301
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1639
int num_role_units(int role)
Definition unittype.c:2251
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1755
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
Definition unittype.c:202
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:184
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1490
int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Definition unittype.c:175
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1783