Freeciv-3.3: /fast/freeciv/nightly/main/ai/default/daicity.c Source File

Freeciv-3.3

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