Freeciv-3.1: /fast/freeciv/nightly/S3_1/ai/default/daicity.c Source File

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