Freeciv-3.2: /fast/freeciv/nightly/S3_2/ai/default/daicity.c Source File

Freeciv-3.2

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