Freeciv21
Develop your civilization from humble roots to a global empire
daicity.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include <cstring>
15 
16 // utility
17 #include "rand.h"
18 #include "registry.h"
19 #include "registry_ini.h"
20 
21 // common
22 #include "actions.h"
23 #include "game.h"
24 #include "government.h"
25 #include "research.h"
26 #include "specialist.h"
27 
28 // server
29 #include "cityhand.h"
30 #include "citytools.h"
31 #include "cityturn.h"
32 #include "notify.h"
33 #include "plrhand.h"
34 #include "srv_log.h"
35 #include "unithand.h"
36 /* server/advisors */
37 #include "advbuilding.h"
38 #include "advdata.h"
39 #include "autosettlers.h"
40 #include "infracache.h"
41 
42 // ai
43 #include "aitraits.h"
44 #include "difficulty.h"
45 #include "handicaps.h"
46 
47 /* ai/default */
48 #include "aidata.h"
49 #include "aihand.h"
50 #include "aiplayer.h"
51 #include "aisettler.h"
52 #include "aitools.h"
53 #include "aiunit.h"
54 #include "daidiplomacy.h"
55 #include "daidomestic.h"
56 #include "daieffects.h"
57 #include "daimilitary.h"
58 
59 #include "daicity.h"
60 
61 #define LOG_BUY LOG_DEBUG
62 #define LOG_EMERGENCY LOG_VERBOSE
63 #define LOG_WANT LOG_VERBOSE
64 
65 /* TODO: AI_CITY_RECALC_SPEED should be configurable to ai difficulty.
66  -kauf */
67 #define AI_CITY_RECALC_SPEED 5
68 
69 #define AI_BA_RECALC_SPEED 5
70 
71 #define SPECVEC_TAG tech
72 #define SPECVEC_TYPE struct advance *
73 #include "specvec.h"
74 
75 #define SPECVEC_TAG impr
76 #define SPECVEC_TYPE const struct impr_type *
77 #include "specvec.h"
78 
79 /* Iterate over cities within a certain range around a given city
80  * (city_here) that exist within a given city list. */
81 #define city_range_iterate(city_here, list, range, city) \
82  { \
83  city_list_iterate(list, city) \
84  { \
85  if (range == REQ_RANGE_PLAYER || range == REQ_RANGE_TEAM \
86  || range == REQ_RANGE_ALLIANCE \
87  || (range == REQ_RANGE_TRADEROUTE \
88  && (city == city_here \
89  || have_cities_trade_route(city, city_here))) \
90  || ((range == REQ_RANGE_CITY || range == REQ_RANGE_LOCAL) \
91  && city == city_here) \
92  || (range == REQ_RANGE_CONTINENT \
93  && tile_continent(city->tile) \
94  == tile_continent(city_here->tile))) {
95 
96 #define city_range_iterate_end \
97  } \
98  } \
99  city_list_iterate_end; \
100  }
101 
102 #define CITY_EMERGENCY(pcity) \
103  (pcity->surplus[O_SHIELD] < 0 || city_unhappy(pcity) \
104  || pcity->food_stock + pcity->surplus[O_FOOD] < 0)
105 
106 static void dai_city_sell_noncritical(struct city *pcity,
107  bool redundant_only);
108 static void resolve_city_emergency(struct ai_type *ait,
109  struct player *pplayer,
110  struct city *pcity);
111 
125  struct ai_type *ait, struct player *pplayer, const struct city *pcity,
126  const struct impr_type *pimprove, const struct advance *tech,
127  adv_want building_want)
128 {
129  /* The conversion factor was determined by experiment,
130  * and might need adjustment. See also dai_tech_effect_values()
131  */
132  const adv_want tech_want =
133  building_want * def_ai_city_data(pcity, ait)->building_wait * 14 / 8;
134 #if 0
135  /* This logging is relatively expensive,
136  * so activate it only while necessary. */
137  TECH_LOG(LOG_DEBUG, pplayer, tech,
138  "wanted by %s for building: %d -> %d",
139  city_name_get(pcity), improvement_rule_name(pimprove),
140  building_want, tech_want);
141 #endif // 0
142  if (tech) {
143  def_ai_player_data(pplayer, ait)->tech_want[advance_index(tech)] +=
144  tech_want;
145  }
146 }
147 
153  struct player *pplayer,
154  const struct city *pcity,
155  const struct impr_type *pimprove,
156  struct tech_vector *needed_techs,
157  adv_want building_want)
158 {
159  int t;
160  int n_needed_techs = tech_vector_size(needed_techs);
161 
162  for (t = 0; t < n_needed_techs; t++) {
163  want_tech_for_improvement_effect(ait, pplayer, pcity, pimprove,
164  *tech_vector_get(needed_techs, t),
165  building_want);
166  }
167 }
168 
174  struct player *pplayer,
175  const struct city *pcity,
176  const struct impr_type *pimprove,
177  adv_want building_want)
178 {
179  requirement_vector_iterate(&pimprove->obsolete_by, pobs)
180  {
181  if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
182  want_tech_for_improvement_effect(ait, pplayer, pcity, pimprove,
183  pobs->source.value.advance,
184  -building_want);
185  }
186  }
188 }
189 
198 static void dai_barbarian_choose_build(struct player *pplayer,
199  struct city *pcity,
200  struct adv_choice *choice)
201 {
202  struct unit_type *bestunit = nullptr;
203  int i, bestattack = 0;
204 
205  // Choose the best unit among the basic ones
206  for (i = 0; i < num_role_units(L_BARBARIAN_BUILD); i++) {
207  struct unit_type *iunit = get_role_unit(L_BARBARIAN_BUILD, i);
208 
209  if (iunit->attack_strength > bestattack
210  && can_city_build_unit_now(pcity, iunit)) {
211  bestunit = iunit;
212  bestattack = iunit->attack_strength;
213  }
214  }
215 
216  // Choose among those made available through other civ's research
217  for (i = 0; i < num_role_units(L_BARBARIAN_BUILD_TECH); i++) {
218  struct unit_type *iunit = get_role_unit(L_BARBARIAN_BUILD_TECH, i);
219 
220  if (iunit->attack_strength > bestattack
221  && can_city_build_unit_now(pcity, iunit)) {
222  bestunit = iunit;
223  bestattack = iunit->attack_strength;
224  }
225  }
226 
227  // If found anything, put it into the choice
228  if (bestunit) {
229  choice->value.utype = bestunit;
230  // FIXME: 101 is the "overriding military emergency" indicator
231  choice->want = 101;
232  choice->type = CT_ATTACKER;
233  adv_choice_set_use(choice, "barbarian");
234  } else {
235  log_base(LOG_WANT, "Barbarians don't know what to build!");
236  }
237 }
238 
247 static void dai_city_choose_build(struct ai_type *ait,
248  struct player *pplayer, struct city *pcity)
249 {
250  struct adv_choice *newchoice;
251  struct adv_data *adv = adv_data_get(pplayer, nullptr);
252  struct ai_city *city_data = def_ai_city_data(pcity, ait);
253 
254  if (has_handicap(pplayer, H_AWAY) && city_built_last_turn(pcity)
255  && city_data->urgency == 0) {
256  // Don't change existing productions unless we have to.
257  return;
258  }
259 
260  if (is_barbarian(pplayer)) {
261  dai_barbarian_choose_build(pplayer, pcity, &(city_data->choice));
262  } else {
263  // FIXME: 101 is the "overriding military emergency" indicator
264  if ((city_data->choice.want <= 100 || city_data->urgency == 0)
265  && !(dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0
266  && pcity->id != adv->wonder_city)) {
267  newchoice = domestic_advisor_choose_build(ait, pplayer, pcity);
268  adv_choice_copy(&(city_data->choice),
269  adv_better_choice(&(city_data->choice), newchoice));
270  delete newchoice;
271  }
272  }
273 
274  // Fallbacks
275  if (city_data->choice.want == 0) {
276  // Fallbacks do happen with techlevel 0, which is now default. -- Per
277  CITY_LOG(LOG_WANT, pcity,
278  "Falling back - didn't want to build soldiers,"
279  " workers, caravans, settlers, or buildings!");
280  city_data->choice.want = 1;
281  if (best_role_unit(pcity, action_id_get_role(ACTION_TRADE_ROUTE))) {
282  city_data->choice.value.utype =
283  best_role_unit(pcity, action_id_get_role(ACTION_TRADE_ROUTE));
284  city_data->choice.type = CT_CIVILIAN;
285  adv_choice_set_use(&(city_data->choice), "fallback trade route");
286  } else {
287  int our_def = assess_defense_quadratic(ait, pcity);
288 
289  if (our_def == 0
290  && dai_process_defender_want(ait, pplayer, pcity, 1,
291  &(city_data->choice))) {
292  adv_choice_set_use(&(city_data->choice), "fallback defender");
293  CITY_LOG(LOG_DEBUG, pcity, "Building fallback defender");
294  } else if (best_role_unit(pcity, UTYF_SETTLERS)) {
295  city_data->choice.value.utype =
296  dai_role_utype_for_terrain_class(pcity, UTYF_SETTLERS, TC_LAND);
297  city_data->choice.type = CT_CIVILIAN;
298  adv_choice_set_use(&(city_data->choice), "fallback worker");
299  } else {
300  CITY_LOG(LOG_ERROR, pcity,
301  "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  qInfo("%s wants %s for %s with desire " ADV_WANT_PRINTF ".",
316  city_name_get(pcity), dai_choice_rule_name(&city_data->choice),
317  city_data->choice.use, city_data->choice.want);
318  }
319 #endif // ADV_CHOICE_TRACK
320 
321  CITY_LOG(LOG_DEBUG, pcity, "wants %s with desire " ADV_WANT_PRINTF ".",
322  dai_choice_rule_name(&city_data->choice),
323  city_data->choice.want);
324 
325  switch (city_data->choice.type) {
326  case CT_CIVILIAN:
327  case CT_ATTACKER:
328  case CT_DEFENDER:
329  build_new.kind = VUT_UTYPE;
330  build_new.value.utype = city_data->choice.value.utype;
331  break;
332  case CT_BUILDING:
333  build_new.kind = VUT_IMPROVEMENT;
334  build_new.value.building = city_data->choice.value.building;
335  break;
336  case CT_NONE:
337  build_new.kind = VUT_NONE;
338  build_new.value.age = 0; // Don't leave it uninitialized
339  break;
340  case CT_LAST:
341  build_new.kind = universals_n_invalid();
342  build_new.value.age = 0; // Don't leave it uninitialized
343  break;
344  };
345 
346  change_build_target(pplayer, pcity, &build_new,
347  E_CITY_PRODUCTION_CHANGED);
348  }
349 }
350 
354 static void try_to_sell_stuff(struct player *pplayer, struct city *pcity)
355 {
356  improvement_iterate(pimprove)
357  {
358  if (can_city_sell_building(pcity, pimprove)
359  && !building_has_effect(pimprove, EFT_DEFEND_BONUS)) {
360  // selling walls to buy defenders is counterproductive -- Syela
361  really_handle_city_sell(pplayer, pcity, pimprove);
362  break;
363  }
364  }
366 }
367 
372 static void increase_maxbuycost(struct player *pplayer, int new_value)
373 {
374  pplayer->ai_common.maxbuycost =
375  MAX(pplayer->ai_common.maxbuycost, new_value);
376 }
377 
383 static void dai_upgrade_units(struct city *pcity, int limit, bool military)
384 {
385  action_id upgrade_actions[MAX_NUM_ACTIONS];
386  struct player *pplayer = city_owner(pcity);
387  int expenses;
388 
389  {
390  // Find upgrade unit actions
391  int i = 0;
392  action_list_add_all_by_result(upgrade_actions, &i, ACTRES_UPGRADE_UNIT);
393  action_list_end(upgrade_actions, i);
394  }
395 
396  dai_calc_data(pplayer, nullptr, &expenses, nullptr);
397 
398  unit_list_iterate(pcity->tile->units, punit)
399  {
400  if (pcity->owner == punit->owner) {
401  // Only upgrade units you own, not allied ones
402 
403  const struct unit_type *old_type = unit_type_get(punit);
404  const struct unit_type *punittype =
405  can_upgrade_unittype(pplayer, old_type);
406 
407  if (military && !IS_ATTACKER(old_type)) {
408  // Only upgrade military units this round
409  continue;
410  } else if (!military && IS_ATTACKER(old_type)) {
411  // Only civilians or tranports this round
412  continue;
413  }
414 
415  if (punittype == nullptr) {
416  continue;
417  }
418 
419  action_list_iterate(upgrade_actions, act_id)
420  {
421  const struct action *paction = action_by_number(act_id);
422  int cost = unit_upgrade_price(pplayer, old_type, punittype);
423  int real_limit = limit;
424 
425  /* Sinking Triremes are DANGEROUS!! We'll do anything to upgrade 'em.
426  */
427  /* FIXME: This assumes rules to be quite close to civ/2.
428  * Of the supplied rulesets those are the only ones with
429  * UTYF_COAST unit, but... */
430  if (unit_has_type_flag(punit, UTYF_COAST)) {
431  real_limit = expenses;
432  }
433  if (pplayer->economic.gold - cost > real_limit) {
434  CITY_LOG(LOG_BUY, pcity, "Upgraded %s to %s for %d (%s)",
435  unit_rule_name(punit), utype_rule_name(punittype), cost,
436  military ? "military" : "civilian");
437  unit_do_action(city_owner(pcity), punit->id, pcity->id, 0, "",
438  paction->id);
439  } else {
440  increase_maxbuycost(pplayer, cost);
441  }
442  }
444  }
445  }
447 }
448 
455 static void unit_do_disband_trad(struct player *owner, struct unit *punit,
456  const enum action_requester requester)
457 {
458  const int punit_id_stored = punit->id;
459 
460  fc_assert_ret(owner == unit_owner(punit));
461 
462  /* Help Wonder gives 100% of the shields used to produce the unit to the
463  * city where it is located. */
464  if (unit_can_do_action(punit, ACTION_HELP_WONDER)) {
465  struct city *tgt_city;
466 
467  // Only a city at the same tile as the unit can benefit.
468  tgt_city = tile_city(unit_tile(punit));
469 
470  if (tgt_city
471  && is_action_enabled_unit_on_city(ACTION_HELP_WONDER, punit,
472  tgt_city)) {
473  if (unit_perform_action(owner, punit->id, tgt_city->id, 0, nullptr,
474  ACTION_HELP_WONDER, requester)) {
475  // No shields wasted. The unit did Help Wonder.
476  return;
477  }
478  }
479  }
480 
481  if (!unit_is_alive(punit_id_stored)) {
482  // The unit is gone. Maybe it was killed in Lua?
483  return;
484  }
485 
486  /* Disbanding a unit inside a city gives it 50% of the shields used to
487  * produce the unit. */
488  if (unit_can_do_action(punit, ACTION_RECYCLE_UNIT)) {
489  struct city *tgt_city;
490 
491  // Only a city at the same tile as the unit can benefit.
492  tgt_city = tile_city(unit_tile(punit));
493 
494  if (tgt_city
495  && is_action_enabled_unit_on_city(ACTION_RECYCLE_UNIT, punit,
496  tgt_city)) {
497  if (unit_perform_action(owner, punit->id, tgt_city->id, 0, nullptr,
498  ACTION_RECYCLE_UNIT, requester)) {
499  // The unit did Recycle Unit. 50% of the shields wasted.
500  return;
501  }
502  }
503  }
504 
505  if (!unit_is_alive(punit_id_stored)) {
506  // The unit is gone. Maybe it was killed in Lua?
507  return;
508  }
509 
510  // Try to disband even if all shields will be wasted.
511  if (unit_can_do_action(punit, ACTION_DISBAND_UNIT)) {
512  if (is_action_enabled_unit_on_self(ACTION_DISBAND_UNIT, punit)) {
513  if (unit_perform_action(owner, punit->id, punit->id, 0, nullptr,
514  ACTION_DISBAND_UNIT, requester)) {
515  // All shields wasted. The unit did Disband Unit.
516  return;
517  }
518  }
519  }
520 }
521 
525 static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
526 {
527  struct adv_choice bestchoice;
528  int cached_limit = dai_gold_reserve(pplayer);
529  int expenses;
530  bool war_footing = dai_on_war_footing(ait, pplayer);
531 
532  /* Disband explorers that are at home but don't serve a purpose.
533  * FIXME: This is a hack, and should be removed once we
534  * learn how to ferry explorers to new land. */
535  city_list_iterate(pplayer->cities, pcity)
536  {
537  struct tile *ptile = pcity->tile;
538  unit_list_iterate_safe(ptile->units, punit)
539  {
540  if (unit_has_type_role(punit, L_EXPLORER)
541  && pcity->id == punit->homecity
542  && def_ai_city_data(pcity, ait)->urgency == 0) {
543  CITY_LOG(LOG_BUY, pcity, "disbanding %s to increase production",
544  unit_rule_name(punit));
545  unit_do_disband_trad(pplayer, punit, ACT_REQ_PLAYER);
546  }
547  }
549  }
551 
552  dai_calc_data(pplayer, nullptr, &expenses, nullptr);
553 
554  do {
555  bool expensive; // don't buy when it costs x2 unless we must
556  int buycost;
557  int limit = cached_limit; // cached_limit is our gold reserve
558  struct city *pcity = nullptr;
559  struct ai_city *city_data;
560 
561  // Find highest wanted item on the buy list
562  adv_init_choice(&bestchoice);
563  city_list_iterate(pplayer->cities, acity)
564  {
565  struct ai_city *acity_data = def_ai_city_data(acity, ait);
566 
567  if (acity_data->choice.want > bestchoice.want
568  && ai_fuzzy(pplayer, true)) {
569  bestchoice.value = acity_data->choice.value;
570  bestchoice.want = acity_data->choice.want;
571  bestchoice.type = acity_data->choice.type;
572  pcity = acity;
573  }
574  }
576 
577  // We found nothing, so we're done
578  if (bestchoice.want == 0 || pcity == nullptr) {
579  break;
580  }
581 
582  city_data = def_ai_city_data(pcity, ait);
583 
584  // Not dealing with this city a second time
585  city_data->choice.want = 0;
586 
587  ADV_CHOICE_ASSERT(bestchoice);
588 
589  // Try upgrade units at danger location (high want is usually danger)
590  if (city_data->urgency > 1) {
591  if (bestchoice.type == CT_BUILDING
592  && is_wonder(bestchoice.value.building)) {
593  CITY_LOG(LOG_BUY, pcity,
594  "Wonder being built in dangerous position!");
595  } else {
596  // If we have urgent want, spend more
597  int upgrade_limit = limit;
598 
599  if (city_data->urgency > 1) {
600  upgrade_limit = expenses;
601  }
602  // Upgrade only military units now
603  dai_upgrade_units(pcity, upgrade_limit, true);
604  }
605  }
606 
607  if (pcity->anarchy != 0 && bestchoice.type != CT_BUILDING) {
608  continue; // Nothing we can do
609  }
610 
611  // Cost to complete production
612  buycost = city_production_buy_gold_cost(pcity);
613 
614  if (buycost <= 0) {
615  continue; // Already completed
616  }
617 
618  if (is_unit_choice_type(bestchoice.type)
619  && utype_is_cityfounder(bestchoice.value.utype)) {
620  if (get_city_bonus(pcity, EFT_GROWTH_FOOD) == 0
621  && bestchoice.value.utype->pop_cost > 0
622  && city_size_get(pcity) <= bestchoice.value.utype->pop_cost) {
623  /* Don't buy settlers in cities that cannot afford the population
624  * cost. */
625  /* This used to check also if city is about to grow to required size
626  * next turn and allow buying of settlers in that case, but current
627  * order of end/start turn activities is such that settler building
628  * fails already before city grows. */
629  continue;
630  } else if (city_list_size(pplayer->cities) > 6) {
631  /* Don't waste precious money buying settlers late game
632  * since this raises taxes, and we want science. Adjust this
633  * again when our tax algorithm is smarter. */
634  continue;
635  } else if (war_footing) {
636  continue;
637  }
638  } else {
639  /* We are not a settler. Therefore we increase the cash need we
640  * balance our buy desire with to keep cash at hand for emergencies
641  * and for upgrades */
642  limit *= 2;
643  }
644 
645  // It costs x2 to buy something with no shields contributed
646  expensive = (pcity->shield_stock == 0)
647  || (pplayer->economic.gold - buycost < limit);
648 
649  if (bestchoice.type == CT_ATTACKER
650  && buycost
651  > utype_build_shield_cost(pcity, bestchoice.value.utype) * 2
652  && !war_footing) {
653  // Too expensive for an offensive unit
654  continue;
655  }
656 
657  /* FIXME: Here Syela wanted some code to check if
658  * pcity was doomed, and we should therefore attempt
659  * to sell everything in it of non-military value */
660 
661  if (pplayer->economic.gold - expenses >= buycost
662  && (!expensive
663  || (city_data->grave_danger != 0
664  && assess_defense(ait, pcity) == 0)
665  || (bestchoice.want > 200 && city_data->urgency > 1))) {
666  // Buy stuff
667  CITY_LOG(LOG_BUY, pcity,
668  "Crash buy of %s for %d (want " ADV_WANT_PRINTF ")",
669  dai_choice_rule_name(&bestchoice), buycost, bestchoice.want);
670  really_handle_city_buy(pplayer, pcity);
671  } else if (city_data->grave_danger != 0 && bestchoice.type == CT_DEFENDER
672  && assess_defense(ait, pcity) == 0) {
673  // We have no gold but MUST have a defender
674  CITY_LOG(LOG_BUY, pcity, "must have %s but can't afford it (%d < %d)!",
675  dai_choice_rule_name(&bestchoice), pplayer->economic.gold,
676  buycost);
677  try_to_sell_stuff(pplayer, pcity);
678  if (pplayer->economic.gold - expenses >= buycost) {
679  CITY_LOG(LOG_BUY, pcity, "now we can afford it (sold something)");
680  really_handle_city_buy(pplayer, pcity);
681  }
682  increase_maxbuycost(pplayer, buycost);
683  }
684  } while (true);
685 
686  if (!war_footing) {
687  // Civilian upgrades now
688  city_list_iterate(pplayer->cities, pcity)
689  {
690  dai_upgrade_units(pcity, cached_limit, false);
691  }
693  }
694 
695  log_base(LOG_BUY, "%s wants to keep %d in reserve (tax factor %d)",
696  player_name(pplayer), cached_limit,
697  pplayer->ai_common.maxbuycost);
698 }
699 
703 static int unit_food_upkeep(struct unit *punit)
704 {
705  struct player *pplayer = unit_owner(punit);
706  int upkeep = utype_upkeep_cost(unit_type_get(punit), pplayer, O_FOOD);
707 
708  if (punit->id != 0 && punit->homecity == 0) {
709  upkeep = 0; // thanks, Peter
710  }
711 
712  return upkeep;
713 }
714 
723 static int unit_foodbox_cost(struct unit *punit)
724 {
725  int pop_cost = unit_type_get(punit)->pop_cost;
726 
727  if (pop_cost <= 0) {
728  return 0;
729  }
730 
731  if (punit->id == 0) {
732  // It is a virtual unit, so must start in a city...
733  struct city *pcity = tile_city(unit_tile(punit));
734  int size = city_size_get(pcity);
735  int cost = 0;
736  int i;
737 
738  // The default is to lose 100%. The growth bonus reduces this.
739  int foodloss_pct = 100 - get_city_bonus(pcity, EFT_GROWTH_FOOD);
740 
741  foodloss_pct = CLIP(0, foodloss_pct, 100);
742  fc_assert_ret_val(pcity != nullptr, 0);
743  fc_assert(size >= pop_cost);
744 
745  for (i = pop_cost; i > 0; i--) {
746  cost += city_granary_size(size--);
747  }
748  cost = cost * foodloss_pct / 100;
749 
750  return cost;
751  }
752 
753  return 30;
754 }
755 
763  struct city *pcity)
764 {
765  struct unit *virtualunit;
766  int want;
767  enum unit_activity best_act = ACTIVITY_IDLE;
768  struct extra_type *best_target;
769  struct tile *best_tile = nullptr; // May be accessed by log_*() calls.
770  struct tile *pcenter = city_tile(pcity);
771  struct player *pplayer = city_owner(pcity);
772  struct adv_data *adv = adv_data_get(pplayer, nullptr);
773  struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
774  struct unit_type *utype;
775  Continent_id place = tile_continent(pcenter);
776  struct ai_city *city_data = def_ai_city_data(pcity, ait);
777  struct dai_private_data *pprivate =
778  static_cast<struct dai_private_data *>(ait->pprivate);
779 
780  if (!pprivate->contemplace_workers) {
781  // AI type uses custom method to set worker want and type.
782  return;
783  }
784 
785  city_data->worker_want =
786  0; // Make sure old want does not stay if we don't want now
787 
788  utype = dai_role_utype_for_terrain_class(pcity, UTYF_SETTLERS, TC_LAND);
789 
790  if (utype == nullptr) {
791  log_debug("No UTYF_SETTLERS role unit available");
792  return;
793  }
794 
795  // Create a localized "virtual" unit to do operations with.
796  virtualunit = unit_virtual_create(pplayer, pcity, utype, 0);
797  /* Advisors data space not allocated as it's not needed in the
798  lifetime of the virtualunit. */
799  unit_tile_set(virtualunit, pcenter);
800  want = settler_evaluate_improvements(virtualunit, &best_act, &best_target,
801  &best_tile, nullptr, nullptr);
802  if (unit_type_get(virtualunit)->pop_cost >= city_size_get(pcity)) {
803  // We don't like disbanding the city as a side effect
804  unit_virtual_destroy(virtualunit);
805 
806  return;
807  }
808  /* We consider unit_food_upkeep with only half FOOD_WEIGHTING to
809  * balance the fact that unit can improve many tiles during its
810  * lifetime, and want is calculated for just one of them.
811  * Having full FOOD_WEIGHT here would mean that tile improvement of
812  * +1 food would give just zero want for settler. Other weights
813  * are lower, so +1 shield - unit food upkeep would be negative. */
814  want = (want - unit_food_upkeep(virtualunit) * FOOD_WEIGHTING / 2) * 100
815  / (40 + unit_foodbox_cost(virtualunit));
816  unit_virtual_destroy(virtualunit);
817 
818  /* Massage our desire based on available statistics to prevent
819  * overflooding with worker type units if they come cheap in
820  * the ruleset */
821  if (place >= 0) {
822  want /=
823  MAX(1, ai->stats.workers[place] / (adv->stats.cities[place] + 1));
824  want -= ai->stats.workers[place];
825  } else {
826  want /= MAX(1, ai->stats.ocean_workers[-place]
827  / (adv->stats.ocean_cities[-place] + 1));
828  want -= ai->stats.ocean_workers[-place];
829  }
830  want = MAX(want, 0);
831 
832  if (place >= 0) {
833  CITY_LOG(LOG_DEBUG, pcity,
834  "wants %s with want %d to do %s at (%d,%d), "
835  "we have %d workers and %d cities on the continent",
836  utype_rule_name(utype), want, get_activity_text(best_act),
837  TILE_XY(best_tile), ai->stats.workers[place],
838  adv->stats.cities[place]);
839  } else {
840  CITY_LOG(LOG_DEBUG, pcity,
841  "wants %s with want %d to do %s at (%d,%d), "
842  "we have %d workers and %d cities on the ocean",
843  utype_rule_name(utype), want, get_activity_text(best_act),
844  TILE_XY(best_tile), ai->stats.ocean_workers[-place],
845  adv->stats.ocean_cities[-place]);
846  }
847 
848  fc_assert(want >= 0);
849 
850  city_data->worker_want = want;
852  pcity, UTYF_SETTLERS, place >= 0 ? TC_LAND : TC_OCEAN);
853 }
854 
861 void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
862 {
863  pplayer->ai_common.maxbuycost = 0;
864 
866  city_list_iterate(pplayer->cities, pcity)
867  {
868  if (CITY_EMERGENCY(pcity)
869  || city_granary_size(city_size_get(pcity)) == pcity->food_stock) {
870  /* Having a full granary isn't an emergency, but we want to rearrange
871  */
872  auto_arrange_workers(pcity); // this usually helps
873  }
874  if (CITY_EMERGENCY(pcity)) {
875  // Fix critical shortages or unhappiness
876  resolve_city_emergency(ait, pplayer, pcity);
877  }
878  dai_city_sell_noncritical(pcity, true);
879  sync_cities();
880  }
882  if (pplayer->economic.tax >= 30 // Otherwise expect it to increase tax
883  && player_get_expected_income(pplayer) < -(pplayer->economic.gold)) {
884  int count = city_list_size(pplayer->cities);
885  struct city *sellers[count + 1];
886  int i;
887 
888  // Randomized order
889  i = 0;
890  city_list_iterate(pplayer->cities, pcity) { sellers[i++] = pcity; }
892  for (i = 0; i < count; i++) {
893  int replace = fc_rand(count);
894  struct city *tmp;
895 
896  tmp = sellers[i];
897  sellers[i] = sellers[replace];
898  sellers[replace] = tmp;
899  }
900 
901  i = 0;
902  while (player_get_expected_income(pplayer) < -(pplayer->economic.gold)
903  && i < count) {
904  dai_city_sell_noncritical(sellers[i++], false);
905  }
906  }
908 
910  building_advisor(pplayer);
912 
913  // Initialize the infrastructure cache, which is used shortly.
915  city_list_iterate(pplayer->cities, pcity)
916  {
917  struct ai_city *city_data = def_ai_city_data(pcity, ait);
918  struct adv_choice *choice;
919 
920  if (city_data->choice.want <= 0) {
921  // Note that this function mungs the seamap, but we don't care
923  choice = military_advisor_choose_build(ait, pplayer, pcity, &(wld.map),
924  nullptr);
925  adv_choice_copy(&(city_data->choice), choice);
926  adv_free_choice(choice);
928  }
929  if (dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0) {
930  city_data->worker_want = 0;
931  city_data->founder_want = 0;
932  city_data->founder_turn =
933  game.info.turn; /* Do not consider zero we set here
934  * valid value, if real want is needed.
935  * Recalculate immediately in such situation. */
936  continue; // Go, soldiers!
937  }
938  // Will record its findings in pcity->worker_want
942 
944  if (city_data->founder_turn <= game.info.turn) {
945  // Will record its findings in pcity->founder_want
946  contemplate_new_city(ait, pcity);
947  // Avoid recalculating all the time..
948  /* This means AI is not very opportunistic if there happens to open up
949  * spot for a new city. */
950  city_data->founder_turn = game.info.turn
953  } else if (pcity->server.debug) {
954  // recalculate every turn
955  contemplate_new_city(ait, pcity);
956  }
958  ADV_CHOICE_ASSERT(city_data->choice);
959  }
961  // Reset auto settler state for the next run.
962  dai_auto_settler_reset(ait, pplayer);
963 
964  city_list_iterate(pplayer->cities, pcity)
965  {
966  dai_city_choose_build(ait, pplayer, pcity);
967 
968  // Initialize for next turn
969  def_ai_city_data(pcity, ait)->choice.want = -1;
970  }
972 
973  dai_spend_gold(ait, pplayer);
974 }
975 
983 static bool building_crucial(const struct player *plr,
984  struct impr_type *pimprove,
985  const struct city *pcity)
986 {
987 #if 0 // This check will become more complicated now.
988  if (ai_wants_no_science(plr)
989  && building_has_effect(pimprove, EFT_SCIENCE_BONUS)) {
990  return false;
991  }
992 #endif
993  if (building_has_effect(pimprove, EFT_DEFEND_BONUS)
994  // selling city walls is really, really dumb -- Syela
995  || is_improvement_productive(pcity, pimprove)) {
996  return true;
997  }
998 
999  return false;
1000 }
1001 
1005 static void dai_city_sell_noncritical(struct city *pcity,
1006  bool redundant_only)
1007 {
1008  struct player *pplayer = city_owner(pcity);
1009 
1010  city_built_iterate(pcity, pimprove)
1011  {
1012  if (can_city_sell_building(pcity, pimprove)
1013  && !building_crucial(pplayer, pimprove, pcity)
1014  && (!redundant_only || is_improvement_redundant(pcity, pimprove))) {
1015  int gain = impr_sell_gold(pimprove);
1016 
1017  notify_player(
1018  pplayer, pcity->tile, E_IMP_SOLD, ftc_server,
1019  PL_("%s is selling %s for %d.", "%s is selling %s for %d.", gain),
1020  city_link(pcity), improvement_name_translation(pimprove), gain);
1021  do_sell_building(pplayer, pcity, pimprove, "sold");
1022 
1023  return; // max 1 building each turn
1024  }
1025  }
1027 }
1028 
1048 static void resolve_city_emergency(struct ai_type *ait,
1049  struct player *pplayer,
1050  struct city *pcity)
1051 {
1052  struct tile *pcenter = city_tile(pcity);
1053 
1055  "Emergency in %s (%s, angry%d, unhap%d food%d, prod%d)",
1056  city_name_get(pcity), city_unhappy(pcity) ? "unhappy" : "content",
1057  pcity->feel[CITIZEN_ANGRY][FEELING_FINAL],
1059  pcity->surplus[O_FOOD], pcity->surplus[O_SHIELD]);
1060 
1061  city_tile_iterate(city_map_radius_sq_get(pcity), pcenter, atile)
1062  {
1063  struct city *acity = tile_worked(atile);
1064 
1065  if (acity && acity != pcity && city_owner(acity) == city_owner(pcity)) {
1066  log_base(LOG_EMERGENCY, "%s taking over %s square in (%d, %d)",
1067  city_name_get(pcity), city_name_get(acity), TILE_XY(atile));
1068 
1069  int ax, ay;
1070  fc_assert_action(city_base_to_city_map(&ax, &ay, acity, atile),
1071  continue);
1072 
1073  if (is_city_center(acity, atile)) {
1074  // Can't remove a worker here.
1075  continue;
1076  }
1077 
1078  city_map_update_empty(acity, atile);
1079  acity->specialists[DEFAULT_SPECIALIST]++;
1081  }
1082  }
1084 
1085  auto_arrange_workers(pcity);
1086 
1087  if (!CITY_EMERGENCY(pcity)) {
1088  log_base(LOG_EMERGENCY, "Emergency in %s resolved",
1089  city_name_get(pcity));
1090  goto cleanup;
1091  }
1092 
1094  {
1095  if (city_unhappy(pcity)
1096  && (utype_happy_cost(unit_type_get(punit), pplayer) > 0
1097  && (unit_being_aggressive(punit) || is_field_unit(punit)))
1098  && def_ai_unit_data(punit, ait)->passenger == 0) {
1099  UNIT_LOG(LOG_EMERGENCY, punit, "is causing unrest, disbanded");
1100  /* TODO: if Help Wonder stops blocking Disband Unit there may be
1101  * cases where Disband Unit should be selected. Example: Field unit
1102  * in allied city that is building a wonder that makes the ally win
1103  * without sharing the victory. */
1104  /* TODO: Should the unit try to find legal targets at adjacent tiles?
1105  * Should it consider other self eliminating actions than the
1106  * components of the traditional disband? */
1107  unit_do_disband_trad(pplayer, punit, ACT_REQ_PLAYER);
1108  city_refresh(pcity);
1109  }
1110  }
1112 
1113  if (CITY_EMERGENCY(pcity)) {
1114  log_base(LOG_EMERGENCY, "Emergency in %s remains unresolved",
1115  city_name_get(pcity));
1116  } else {
1117  log_base(LOG_EMERGENCY, "Emergency in %s resolved by disbanding unit(s)",
1118  city_name_get(pcity));
1119  }
1120 
1121 cleanup:
1123  sync_cities();
1124 }
1125 
1129 void dai_city_alloc(struct ai_type *ait, struct city *pcity)
1130 {
1131  struct ai_city *city_data = new ai_city[1]();
1132 
1133  city_data->building_wait = BUILDING_WAIT_MINIMUM;
1134  adv_init_choice(&(city_data->choice));
1135 
1136  city_set_ai_data(pcity, ait, city_data);
1137 }
1138 
1142 void dai_city_free(struct ai_type *ait, struct city *pcity)
1143 {
1144  struct ai_city *city_data = def_ai_city_data(pcity, ait);
1145 
1146  if (city_data != nullptr) {
1147  adv_deinit_choice(&(city_data->choice));
1148  city_set_ai_data(pcity, ait, nullptr);
1149  delete[] city_data;
1150  city_data = nullptr;
1151  }
1152 }
1153 
1157 void dai_city_save(struct ai_type *ait, const char *aitstr,
1158  struct section_file *file, const struct city *pcity,
1159  const char *citystr)
1160 {
1161  struct ai_city *city_data = def_ai_city_data(pcity, ait);
1162 
1163  // FIXME: remove this when the urgency is properly recalculated.
1164  secfile_insert_int(file, city_data->urgency, "%s.%s.urgency", citystr,
1165  aitstr);
1166 
1167  // avoid fc_rand recalculations on subsequent reload.
1168  secfile_insert_int(file, city_data->building_turn, "%s.%s.building_turn",
1169  citystr, aitstr);
1170  secfile_insert_int(file, city_data->building_wait, "%s.%s.building_wait",
1171  citystr, aitstr);
1172 
1173  // avoid fc_rand and expensive recalculations on subsequent reload.
1174  secfile_insert_int(file, city_data->founder_turn, "%s.%s.founder_turn",
1175  citystr, aitstr);
1176  secfile_insert_int(file, city_data->founder_want, "%s.%s.founder_want",
1177  citystr, aitstr);
1178  secfile_insert_bool(file, city_data->founder_boat, "%s.%s.founder_boat",
1179  citystr, aitstr);
1180 }
1181 
1185 void dai_city_load(struct ai_type *ait, const char *aitstr,
1186  const struct section_file *file, struct city *pcity,
1187  const char *citystr)
1188 {
1189  struct ai_city *city_data = def_ai_city_data(pcity, ait);
1190 
1191  // FIXME: remove this when the urgency is properly recalculated.
1192  city_data->urgency =
1193  secfile_lookup_int_default(file, 0, "%s.%s.urgency", citystr, aitstr);
1194 
1195  // avoid fc_rand recalculations on subsequent reload.
1197  file, 0, "%s.%s.building_turn", citystr, aitstr);
1199  file, BUILDING_WAIT_MINIMUM, "%s.%s.building_wait", citystr, aitstr);
1200 
1201  // avoid fc_rand and expensive recalculations on subsequent reload.
1203  file, 0, "%s.%s.founder_turn", citystr, aitstr);
1205  file, 0, "%s.%s.founder_want", citystr, aitstr);
1206  city_data->founder_boat =
1207  secfile_lookup_bool_default(file, (city_data->founder_want < 0),
1208  "%s.%s.founder_boat", citystr, aitstr);
1209 }
1210 
1223 static int action_target_neg_util(action_id act_id, const struct city *pcity)
1224 {
1225  struct action *paction = action_by_number(act_id);
1226 
1227  fc_assert_msg(action_id_exists(act_id), "Action %d don't exist.", act_id);
1228 
1229  switch (paction->result) {
1230  case ACTRES_SPY_INCITE_CITY:
1231  // Copied from the evaluation of the No_Incite effect
1232  return MAX((game.server.diplchance * 2 - game.server.incite_total_factor)
1233  / 2
1234  - game.server.incite_improvement_factor * 5
1235  - game.server.incite_unit_factor * 5,
1236  0);
1237 
1238  // Really bad for the city owner.
1239  case ACTRES_SPY_NUKE:
1240  case ACTRES_CONQUER_CITY:
1241  /* The ai will never destroy his own city to keep it out of enemy
1242  * hands. If it starts supporting it this value should change. */
1243  case ACTRES_DESTROY_CITY:
1244  return 20;
1245 
1246  // Bad for the city owner.
1247  case ACTRES_SPY_POISON:
1248  case ACTRES_SPY_SPREAD_PLAGUE:
1249  case ACTRES_SPY_SABOTAGE_CITY:
1250  case ACTRES_SPY_TARGETED_SABOTAGE_CITY:
1251  case ACTRES_SPY_SABOTAGE_CITY_PRODUCTION:
1252  case ACTRES_STRIKE_BUILDING:
1253  case ACTRES_STRIKE_PRODUCTION:
1254  case ACTRES_SPY_STEAL_GOLD:
1255  case ACTRES_NUKE_CITY:
1256  // TODO: Individual and well balanced values
1257  return 10;
1258 
1259  // Good for an enemy
1260  case ACTRES_SPY_STEAL_TECH:
1261  case ACTRES_SPY_TARGETED_STEAL_TECH:
1262  case ACTRES_STEAL_MAPS:
1263  // TODO: Individual and well balanced values
1264  return 8;
1265 
1266  // Could be worse
1267  case ACTRES_ESTABLISH_EMBASSY:
1268  case ACTRES_SPY_INVESTIGATE_CITY:
1269  case ACTRES_MARKETPLACE:
1270  // TODO: Individual and well balanced values
1271  return 1;
1272 
1273  // Good for the city owner in most cases.
1274  case ACTRES_TRADE_ROUTE:
1275  case ACTRES_HELP_WONDER:
1276  case ACTRES_JOIN_CITY:
1277  case ACTRES_RECYCLE_UNIT:
1278  case ACTRES_HOME_CITY:
1279  case ACTRES_UPGRADE_UNIT:
1280  case ACTRES_AIRLIFT:
1281  // TODO: Individual and well balanced values
1282  return -1;
1283 
1284  // Ruleset defined actions. We have no idea what they do.
1285  case ACTRES_NONE:
1286  return 0;
1287 
1288  // Shouldn't happen.
1289  case ACTRES_SPY_BRIBE_UNIT:
1290  case ACTRES_SPY_SABOTAGE_UNIT:
1291  case ACTRES_SPY_ATTACK:
1292  case ACTRES_EXPEL_UNIT:
1293  case ACTRES_DISBAND_UNIT:
1294  case ACTRES_CAPTURE_UNITS:
1295  case ACTRES_BOMBARD:
1296  case ACTRES_FOUND_CITY:
1297  case ACTRES_NUKE:
1298  case ACTRES_NUKE_UNITS:
1299  case ACTRES_PARADROP:
1300  case ACTRES_ATTACK:
1301  case ACTRES_HEAL_UNIT:
1302  case ACTRES_TRANSFORM_TERRAIN:
1303  case ACTRES_CULTIVATE:
1304  case ACTRES_PLANT:
1305  case ACTRES_PILLAGE:
1306  case ACTRES_CLEAN_POLLUTION:
1307  case ACTRES_CLEAN_FALLOUT:
1308  case ACTRES_FORTIFY:
1309  case ACTRES_ROAD:
1310  case ACTRES_CONVERT:
1311  case ACTRES_BASE:
1312  case ACTRES_MINE:
1313  case ACTRES_IRRIGATE:
1314  case ACTRES_TRANSPORT_ALIGHT:
1315  case ACTRES_TRANSPORT_BOARD:
1316  case ACTRES_TRANSPORT_UNLOAD:
1317  case ACTRES_TRANSPORT_DISEMBARK:
1318  case ACTRES_TRANSPORT_EMBARK:
1319  fc_assert_msg(action_id_get_target_kind(act_id) == ATK_CITY,
1320  "Action not aimed at cities");
1321  }
1322 
1323  // Wrong action. Ignore it.
1324  return 0;
1325 }
1326 
1336 static bool adjust_wants_for_reqs(struct ai_type *ait,
1337  struct player *pplayer, struct city *pcity,
1338  const struct impr_type *pimprove,
1339  const adv_want v)
1340 {
1341  bool all_met = true;
1342  int n_needed_techs = 0;
1343  int n_needed_improvements = 0;
1344  struct tech_vector needed_techs;
1345  struct impr_vector needed_improvements;
1346 
1347  tech_vector_init(&needed_techs);
1348  impr_vector_init(&needed_improvements);
1349 
1350  requirement_vector_iterate(&pimprove->reqs, preq)
1351  {
1352  const bool active = is_req_active(pplayer, nullptr, pcity, pimprove,
1353  pcity->tile, nullptr, nullptr, nullptr,
1354  nullptr, nullptr, preq, RPT_POSSIBLE);
1355 
1356  if (VUT_ADVANCE == preq->source.kind && preq->present && !active) {
1357  // Found a missing technology requirement for this improvement.
1358  tech_vector_append(&needed_techs, preq->source.value.advance);
1359  } else if (VUT_IMPROVEMENT == preq->source.kind && preq->present
1360  && !active) {
1361  /* Found a missing improvement requirement for this improvement.
1362  * For example, in the default ruleset a city must have a Library
1363  * before it can have a University. */
1364  impr_vector_append(&needed_improvements, preq->source.value.building);
1365  }
1366  all_met = all_met && active;
1367  }
1369 
1370  /* If v is negative, the improvement is not worth building,
1371  * but there is no need to punish research of the technologies
1372  * that would make it available.
1373  */
1374  n_needed_techs = tech_vector_size(&needed_techs);
1375  if (0 < v && 0 < n_needed_techs) {
1376  /* Tell AI module how much we want this improvement and what techs are
1377  * required to get it. */
1378  const adv_want dv = v / n_needed_techs;
1379 
1380  want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1381  &needed_techs, dv);
1382  }
1383 
1384  /* If v is negative, the improvement is not worth building,
1385  * but there is no need to punish building the improvements
1386  * that would make it available.
1387  */
1388  n_needed_improvements = impr_vector_size(&needed_improvements);
1389  if (0 < v && 0 < n_needed_improvements) {
1390  /* Because we want this improvement,
1391  * we want the improvements that will make it possible */
1392  const adv_want dv = v / (n_needed_improvements * 4); // WAG
1393  int i;
1394 
1395  for (i = 0; i < n_needed_improvements; i++) {
1396  const struct impr_type *needed_impr =
1397  *impr_vector_get(&needed_improvements, i);
1398  /* TODO: increase the want for the needed_impr,
1399  * if we can build it now */
1400  // Recurse
1401  (void) adjust_wants_for_reqs(ait, pplayer, pcity, needed_impr, dv);
1402  }
1403  }
1404 
1405  /* TODO: use a similar method to increase wants for governments
1406  * that will make this improvement possible? */
1407 
1408  tech_vector_free(&needed_techs);
1409  impr_vector_free(&needed_improvements);
1410 
1411  return all_met;
1412 }
1413 
1419 adv_want dai_city_want(struct player *pplayer, struct city *acity,
1420  struct adv_data *adv, struct impr_type *pimprove)
1421 {
1422  adv_want want = 0;
1423  int prod[O_LAST], bonus[O_LAST], waste[O_LAST];
1424 
1425  memset(prod, 0, O_LAST * sizeof(*prod));
1426  if (nullptr != pimprove
1427  && adv->impr_calc[improvement_index(pimprove)]
1429  struct tile *acenter = city_tile(acity);
1430  bool celebrating = base_city_celebrating(acity);
1431 
1432  /* The below calculation mostly duplicates get_worked_tile_output().
1433  * We do this only for buildings that we know may change tile
1434  * outputs. */
1435  city_tile_iterate(city_map_radius_sq_get(acity), acenter, ptile)
1436  {
1437  if (tile_worked(ptile) == acity) {
1439  {
1440  prod[o] += city_tile_output(acity, ptile, celebrating,
1441  static_cast<output_type_id>(o));
1442  }
1444  }
1445  }
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) { prod[O_TRADE] += proute->value; }
1456  prod[O_GOLD] += get_city_tithes_bonus(acity);
1457  auto gov_centers = player_gov_centers(city_owner(acity));
1459  {
1460  bonus[o] =
1461  get_final_city_output_bonus(acity, static_cast<output_type_id>(o));
1462  waste[o] = city_waste(acity, static_cast<output_type_id>(o),
1463  prod[o] * bonus[o] / 100, nullptr, gov_centers);
1464  }
1467  pplayer, prod[O_TRADE] * bonus[O_TRADE] / 100 - waste[O_TRADE], prod);
1468  output_type_iterate(o) { prod[o] = prod[o] * bonus[o] / 100 - waste[o]; }
1470 
1471  city_built_iterate(acity, upkept)
1472  {
1473  prod[O_GOLD] -= city_improvement_upkeep(acity, upkept);
1474  }
1476  /* Unit upkeep isn't handled here. Unless we do a full city_refresh it
1477  * won't be changed anyway. */
1478 
1479  want += prod[O_FOOD] * adv->food_priority;
1480  if (prod[O_SHIELD] != 0) {
1481  want += prod[O_SHIELD] * adv->shield_priority;
1482  want -= city_pollution(acity, prod[O_SHIELD], prod[O_TRADE])
1483  * adv->pollution_priority;
1484  }
1485  want += prod[O_LUXURY] * adv->luxury_priority;
1486  want += prod[O_SCIENCE] * adv->science_priority;
1487  want += prod[O_GOLD] * adv->gold_priority;
1488 
1489  return want;
1490 }
1491 
1496 static adv_want base_want(struct ai_type *ait, struct player *pplayer,
1497  struct city *pcity, struct impr_type *pimprove)
1498 {
1499  struct adv_data *adv = adv_data_get(pplayer, nullptr);
1500  adv_want final_want = 0;
1501  int wonder_player_id = WONDER_NOT_OWNED;
1502  int wonder_city_id = WONDER_NOT_BUILT;
1503 
1504  if (adv->impr_calc[improvement_index(pimprove)] == ADV_IMPR_ESTIMATE) {
1505  return 0; // Nothing to calculate here.
1506  }
1507 
1508  if (!can_city_build_improvement_now(pcity, pimprove)
1509  || (is_small_wonder(pimprove)
1510  && nullptr != city_from_small_wonder(pplayer, pimprove))) {
1511  return 0;
1512  }
1513 
1514  if (is_wonder(pimprove)) {
1515  if (is_great_wonder(pimprove)) {
1516  wonder_player_id =
1517  game.info.great_wonder_owners[improvement_index(pimprove)];
1518  }
1519  wonder_city_id = pplayer->wonders[improvement_index(pimprove)];
1520  }
1521  // Add the improvement
1522  city_add_improvement(pcity, pimprove);
1523 
1524  // Stir, then compare notes
1525  city_range_iterate(pcity, pplayer->cities,
1526  adv->impr_range[improvement_index(pimprove)], acity)
1527  {
1528  final_want += dai_city_want(pplayer, acity, adv, pimprove)
1529  - def_ai_city_data(acity, ait)->worth;
1530  }
1532 
1533  // Restore
1534  city_remove_improvement(pcity, pimprove);
1535  if (is_wonder(pimprove)) {
1536  if (is_great_wonder(pimprove)) {
1537  game.info.great_wonder_owners[improvement_index(pimprove)] =
1538  wonder_player_id;
1539  }
1540 
1541  pplayer->wonders[improvement_index(pimprove)] = wonder_city_id;
1542  }
1543 
1544  return final_want;
1545 }
1546 
1577  struct player *pplayer,
1578  struct city *pcity,
1579  struct impr_type *pimprove,
1580  const bool already)
1581 {
1582  adv_want v = 0;
1583  int cities[REQ_RANGE_COUNT];
1584  int nplayers = normal_player_count();
1585  struct adv_data *ai = adv_data_get(pplayer, nullptr);
1586  bool capital = is_capital(pcity);
1587  bool can_build = true;
1588  struct government *gov = government_of_player(pplayer);
1589  struct universal source = {.value = {.building = pimprove},
1590  .kind = VUT_IMPROVEMENT};
1591  const bool is_coinage = improvement_has_flag(pimprove, IF_GOLD);
1592  int turns = 9999;
1593  int place = tile_continent(pcity->tile);
1594 
1595  // Remove team members from the equation
1596  players_iterate(aplayer)
1597  {
1598  if (aplayer->team && aplayer->team == pplayer->team
1599  && aplayer != pplayer) {
1600  nplayers--;
1601  }
1602  }
1604 
1605  if (is_coinage) {
1606  /* Since coinage contains some entirely spurious ruleset values,
1607  * we need to hard-code a sensible want.
1608  * We must otherwise handle the special IF_GOLD improvement
1609  * like the others, so the AI will research techs that make it available,
1610  * for rulesets that do not provide it from the start.
1611  */
1612  v += float(TRADE_WEIGHTING) / 10;
1613  } else {
1614  // Base want is calculated above using a more direct approach.
1615  v += base_want(ait, pplayer, pcity, pimprove);
1616  if (v != 0) {
1617  CITY_LOG(LOG_DEBUG, pcity,
1618  "%s base_want is " ADV_WANT_PRINTF " (range=%d)",
1619  improvement_rule_name(pimprove), v,
1620  ai->impr_range[improvement_index(pimprove)]);
1621  }
1622  }
1623 
1624  if (!is_coinage) {
1625  // Adjust by building cost
1626  // FIXME: ought to reduce by upkeep cost and amortise by building cost
1627  v -= (impr_build_shield_cost(pcity, pimprove)
1628  / (pcity->surplus[O_SHIELD] * 10 + 1));
1629  }
1630 
1631  // Find number of cities per range.
1632  cities[REQ_RANGE_PLAYER] = city_list_size(pplayer->cities);
1633  // kludge -- Number of *our* cities in these ranges.
1634  cities[REQ_RANGE_WORLD] = cities[REQ_RANGE_ALLIANCE] =
1635  cities[REQ_RANGE_TEAM] = cities[REQ_RANGE_PLAYER];
1636 
1637  if (place < 0) {
1638  cities[REQ_RANGE_CONTINENT] = 1;
1639  } else {
1640  cities[REQ_RANGE_CONTINENT] = ai->stats.cities[place];
1641  }
1642 
1643  // All the trade partners and the city being considered.
1644  cities[REQ_RANGE_TRADEROUTE] = city_num_trade_routes(pcity) + 1;
1645 
1646  cities[REQ_RANGE_CITY] = cities[REQ_RANGE_LOCAL] = 1;
1647 
1648  // Invalid building range
1649  cities[REQ_RANGE_ADJACENT] = cities[REQ_RANGE_CADJACENT] = 0;
1650 
1651  players_iterate(aplayer)
1652  {
1653  int potential = (aplayer->server.bulbs_last_turn
1654  + city_list_size(aplayer->cities) + 1);
1655 
1656  if (potential > 0) {
1657  requirement_vector_iterate(&pimprove->obsolete_by, pobs)
1658  {
1659  if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1660  turns = MIN(turns, research_goal_bulbs_required(
1661  research_get(aplayer),
1662  advance_number(pobs->source.value.advance))
1663  / (potential + 1));
1664  }
1665  }
1667  }
1668  }
1670 
1671  effect_list_iterate(get_req_source_effects(&source), peffect)
1672  {
1673  struct requirement *mypreq = nullptr;
1674  bool active = true;
1675  int n_needed_techs = 0;
1676  struct tech_vector needed_techs;
1677  bool present = true;
1678  bool impossible_to_get = false;
1679 
1680  tech_vector_init(&needed_techs);
1681 
1682  requirement_vector_iterate(&peffect->reqs, preq)
1683  {
1684  /* Check if all the requirements for the currently evaluated effect
1685  * are met, except for having the building that we are evaluating. */
1686  if (VUT_IMPROVEMENT == preq->source.kind
1687  && preq->source.value.building == pimprove) {
1688  mypreq = preq;
1689  present = preq->present;
1690  continue;
1691  }
1692  if (!is_req_active(pplayer, nullptr, pcity, pimprove, nullptr, nullptr,
1693  nullptr, nullptr, nullptr, nullptr, preq,
1694  RPT_POSSIBLE)) {
1695  active = false;
1696  if (VUT_ADVANCE == preq->source.kind && preq->present) {
1697  /* This missing requirement is a missing tech requirement.
1698  * This will be for some additional effect
1699  * (For example, in the default ruleset, Mysticism increases
1700  * the effect of Temples). */
1701  tech_vector_append(&needed_techs, preq->source.value.advance);
1702  } else if (!dai_can_requirement_be_met_in_city(preq, pplayer,
1703  pcity)) {
1704  impossible_to_get = true;
1705  }
1706  }
1707  }
1709 
1710  n_needed_techs = tech_vector_size(&needed_techs);
1711  if ((active || n_needed_techs) && !impossible_to_get) {
1712  fc_assert_ret_msg(mypreq, "AI error");
1713  adv_want v1 =
1714  dai_effect_value(pplayer, gov, ai, pcity, capital, turns, peffect,
1715  cities[mypreq->range], nplayers);
1716  /* v1 could be negative (the effect could be undesirable),
1717  * although it is usually positive.
1718  * For example, in the default ruleset, Communism decreases the
1719  * effectiveness of a Cathedral. */
1720 
1721  if (!present) {
1722  // Building removes the effect
1723  // Currently v1 is (v + delta). Make it (v - delta) instead
1724  v1 = -v1;
1725  }
1726 
1727  if (active) {
1728  v += v1;
1729  } else if (v1 > 0) {
1730  /* If value of the effect is negative, do not hold it against
1731  * the tech - having the tech wont force one to build the
1732  * building. */
1733 
1734  /* We might want the technology that will enable this
1735  * (additional) effect.
1736  * The better the effect, the more we want the technology.
1737  * We are more interested in (additional) effects that enhance
1738  * buildings we already have.
1739  */
1740  const int a = already ? 5 : 4; // WAG
1741  const adv_want dv = v1 * a / (4 * n_needed_techs);
1742 
1743  want_techs_for_improvement_effect(ait, pplayer, pcity, pimprove,
1744  &needed_techs, dv);
1745  }
1746  }
1747 
1748  tech_vector_free(&needed_techs);
1749  }
1751 
1752  // Can the city be the target of an action?
1753  action_iterate(act_id)
1754  {
1755  bool is_possible;
1756  bool will_be_possible = false;
1757  enum req_range max_range;
1758  int act_neg_util;
1759 
1760  // Is the action relevant?
1761  if (action_id_get_target_kind(act_id) != ATK_CITY) {
1762  continue;
1763  }
1764 
1765  // No range found yet. Local is the most narrow range.
1766  max_range = REQ_RANGE_LOCAL;
1767 
1768  /* Is it possible to do the action to the city right now?
1769  *
1770  * (DiplRel requirements are ignored since actor_player is nullptr) */
1771  is_possible = is_action_possible_on_city(act_id, nullptr, pcity);
1772 
1773  /* Will it be possible to do the action to the city if the building is
1774  * built? */
1776  {
1777  bool active = true;
1778  enum req_range range = REQ_RANGE_LOCAL;
1779 
1780  requirement_vector_iterate(&(enabler->target_reqs), preq)
1781  {
1782  if (VUT_IMPROVEMENT == preq->source.kind
1783  && preq->source.value.building == pimprove) {
1784  // Pretend the building is there
1785  if (preq->present) {
1786  range = preq->range; // Assumption: Max one pr vector
1787  continue;
1788  } else {
1789  active = false;
1790  break;
1791  }
1792  }
1793 
1794  if (!is_req_active(pplayer, nullptr, pcity, pimprove,
1795  city_tile(pcity), nullptr, nullptr, nullptr,
1796  nullptr, nullptr, preq, RPT_POSSIBLE)) {
1797  active = false;
1798  break;
1799  }
1800  }
1802 
1803  if (active) {
1804  will_be_possible = true;
1805 
1806  // Store the widest range that enables the action.
1807  if (max_range < range) {
1808  max_range = range;
1809  }
1810 
1811  /* Don't break the iteration even if the action is enabled. There
1812  * could be a wider range in an active action enabler not yet seen.
1813  */
1814  }
1815  }
1817 
1818  /* Will the building significantly change the ability to target
1819  * the city? */
1820  if (is_possible == will_be_possible) {
1821  continue;
1822  }
1823 
1824  // How undersireable is it that the city may be a target?
1825  act_neg_util = action_target_neg_util(act_id, pcity);
1826 
1827  /* Multiply the desire by number of cities in range.
1828  * Note: This is a simplification. If the action can be done or not
1829  * _may_ be uncanged or changed in the opposite direction in the other
1830  * cities in the range. */
1831  act_neg_util = cities[max_range] * act_neg_util;
1832 
1833  /* Consider the utility of being a potential target.
1834  * Remember: act_util is the negative utility of being a target. */
1835  if (will_be_possible) {
1836  v -= act_neg_util;
1837  } else {
1838  v += act_neg_util;
1839  }
1840  }
1842 
1843  if (already) {
1844  /* Discourage research of the technology that would make this building
1845  * obsolete. The bigger the desire for this building, the more
1846  * we want to discourage the technology. */
1847  dont_want_tech_obsoleting_impr(ait, pplayer, pcity, pimprove, v);
1848  } else {
1849  /* Increase the want for technologies that will enable
1850  * construction of this improvement, if necessary.
1851  */
1852  const bool all_met =
1853  adjust_wants_for_reqs(ait, pplayer, pcity, pimprove, v);
1854  can_build = can_build && all_met;
1855  }
1856 
1857  if (is_coinage && can_build) {
1858  /* Could have a negative want for coinage,
1859  * if we have some stock in a building already. */
1860  pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1861  } else if (!already && can_build) {
1862  const struct research *presearch = research_get(pplayer);
1863 
1864  /* Convert the base 'want' into a building want
1865  * by applying various adjustments */
1866 
1867  // Would it mean losing shields?
1868  if ((VUT_UTYPE == pcity->production.kind
1869  || (is_wonder(pcity->production.value.building)
1870  && !is_wonder(pimprove))
1871  || (!is_wonder(pcity->production.value.building)
1872  && is_wonder(pimprove)))
1873  && pcity->turn_last_built != game.info.turn) {
1874  if (has_handicap(pplayer, H_PRODCHGPEN)) {
1875  v -= pcity->shield_stock * SHIELD_WEIGHTING / 4;
1876  } else {
1877  v -= pcity->shield_stock * SHIELD_WEIGHTING / 15;
1878  }
1879  }
1880 
1881  // Reduce want if building gets obsoleted soon
1882  requirement_vector_iterate(&pimprove->obsolete_by, pobs)
1883  {
1884  if (pobs->source.kind == VUT_ADVANCE && pobs->present) {
1885  v -= v
1887  presearch,
1888  advance_number(pobs->source.value.advance)));
1889  }
1890  }
1892 
1893  // Are we wonder city? Try to avoid building non-wonders very much.
1894  if (pcity->id == ai->wonder_city && !is_wonder(pimprove)) {
1895  v /= 5;
1896  }
1897 
1898  // Set
1899  pcity->server.adv->building_want[improvement_index(pimprove)] += v;
1900  }
1901  /* Else we either have the improvement already,
1902  * or we can not build it (yet) */
1903 }
1904 
1914 static bool should_force_recalc(struct city *pcity)
1915 {
1916  return city_built_last_turn(pcity)
1917  || (VUT_IMPROVEMENT == pcity->production.kind
1919  IF_GOLD)
1921  pcity, pcity->production.value.building));
1922 }
1923 
1928 void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
1929 {
1930  struct adv_data *ai = adv_data_get(pplayer, nullptr);
1931 
1932  // Find current worth of cities and cache this.
1933  city_list_iterate(pplayer->cities, pcity)
1934  {
1935  def_ai_city_data(pcity, ait)->worth =
1936  dai_city_want(pplayer, pcity, ai, nullptr);
1937  }
1939 }
1940 
1947 void dai_build_adv_adjust(struct ai_type *ait, struct player *pplayer,
1948  struct city *wonder_city)
1949 {
1950  /* Clear old building wants.
1951  * Do this separately from the iteration over improvement types
1952  * because each iteration could actually update more than one improvement,
1953  * if improvements have improvements as requirements.
1954  */
1955  city_list_iterate(pplayer->cities, pcity)
1956  {
1957  struct ai_city *city_data = def_ai_city_data(pcity, ait);
1958 
1959  if (city_data->building_turn <= game.info.turn) {
1960  // Do a scheduled recalculation this turn
1961  improvement_iterate(pimprove)
1962  {
1963  pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1964  }
1966  } else if (should_force_recalc(pcity)) {
1967  // Do an emergency recalculation this turn.
1968  city_data->building_wait = city_data->building_turn - game.info.turn;
1969  city_data->building_turn = game.info.turn;
1970 
1971  improvement_iterate(pimprove)
1972  {
1973  pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1974  }
1976  }
1977  }
1979 
1980  improvement_iterate(pimprove)
1981  {
1982  const bool is_coinage = improvement_has_flag(pimprove, IF_GOLD);
1983 
1984  // Handle coinage specially because you can never complete coinage
1985  if (is_coinage
1986  || can_player_build_improvement_later(pplayer, pimprove)) {
1987  city_list_iterate(pplayer->cities, pcity)
1988  {
1989  struct ai_city *city_data = def_ai_city_data(pcity, ait);
1990 
1991  if (pcity != wonder_city && is_wonder(pimprove)) {
1992  // Only wonder city should build wonders!
1993  pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
1994  } else if (city_data->building_turn <= game.info.turn) {
1995  /* Building wants vary relatively slowly, so not worthwhile
1996  * recalculating them every turn.
1997  * We DO want to calculate (tech) wants because of buildings
1998  * we already have. */
1999  const bool already = city_has_building(pcity, pimprove);
2000  int idx = improvement_index(pimprove);
2001 
2002  adjust_improvement_wants_by_effects(ait, pplayer, pcity, pimprove,
2003  already);
2004 
2005  fc_assert(!(already && 0 < pcity->server.adv->building_want[idx]));
2006 
2007  if (is_great_wonder(pimprove)) {
2008  /* Not only would we get the wonder, but we would also prevent
2009  * opponents from getting it. */
2010  pcity->server.adv->building_want[idx] *= 1.5;
2011 
2012  if (pcity->production.kind == VUT_IMPROVEMENT
2013  && is_great_wonder(pcity->production.value.building)) {
2014  /* If we already are building a great wonder, prefer continuing
2015  * to do so over stopping it */
2016  pcity->server.adv->building_want[idx] *= 1.25;
2017  }
2018  }
2019 
2020  // If I am not an expansionist, I want buildings more than units
2021  if (pcity->server.adv->building_want[idx] > 0) {
2022  pcity->server.adv->building_want[idx] =
2023  pcity->server.adv->building_want[idx] * TRAIT_DEFAULT_VALUE
2024  / ai_trait_get_value(TRAIT_EXPANSIONIST, pplayer);
2025  }
2026  }
2027  // else wait until a later turn
2028  }
2030  } else {
2031  // An impossible improvement
2032  city_list_iterate(pplayer->cities, pcity)
2033  {
2034  pcity->server.adv->building_want[improvement_index(pimprove)] = 0;
2035  }
2037  }
2038  }
2040 
2041 #ifdef FREECIV_DEBUG
2042  // This logging is relatively expensive, so activate only if necessary
2043  city_list_iterate(pplayer->cities, pcity)
2044  {
2045  improvement_iterate(pimprove)
2046  {
2047  if (pcity->server.adv->building_want[improvement_index(pimprove)]
2048  != 0) {
2049  CITY_LOG(
2050  LOG_DEBUG, pcity, "want to build %s with " ADV_WANT_PRINTF,
2051  improvement_rule_name(pimprove),
2052  pcity->server.adv->building_want[improvement_index(pimprove)]);
2053  }
2054  }
2056  }
2058 #endif // FREECIV_DEBUG
2059 
2060  // Reset recalc counter
2061  city_list_iterate(pplayer->cities, pcity)
2062  {
2063  struct ai_city *city_data = def_ai_city_data(pcity, ait);
2064 
2065  if (city_data->building_turn <= game.info.turn) {
2066  /* This will spread recalcs out so that no one turn end is
2067  * much longer than others */
2068  city_data->building_wait =
2070  city_data->building_turn = game.info.turn + city_data->building_wait;
2071  }
2072  }
2074 }
2075 
2079 void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity,
2080  bool *result)
2081 {
2082  if (def_ai_city_data(pcity, ait)->grave_danger > 0) {
2083  *result = false;
2084  } else {
2085  *result = true;
2086  }
2087 }
2088 
2096  enum effect_type effect_type,
2097  const struct unit_type *utype)
2098 {
2099  int greatest_value = 0;
2100  const struct impr_type *best_building = nullptr;
2101 
2102  effect_list_iterate(get_effects(effect_type), peffect)
2103  {
2104  if (peffect->value > greatest_value) {
2105  const struct impr_type *building = nullptr;
2106  bool wrong_unit = false;
2107 
2108  requirement_vector_iterate(&peffect->reqs, preq)
2109  {
2110  if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2111  building = preq->source.value.building;
2112 
2113  if (!can_city_build_improvement_now(pcity, building)
2114  || !is_improvement(building)) {
2115  building = nullptr;
2116  break;
2117  }
2118  } else if (utype != nullptr
2119  && !is_req_active(city_owner(pcity), nullptr, pcity,
2120  nullptr, city_tile(pcity), nullptr,
2121  utype, nullptr, nullptr, nullptr, preq,
2122  RPT_POSSIBLE)) {
2123  /* Effect requires other kind of unit than what we are interested
2124  * about */
2125  wrong_unit = true;
2126  break;
2127  }
2128  }
2130  if (!wrong_unit && building != nullptr) {
2131  best_building = building;
2132  greatest_value = peffect->value;
2133  }
2134  }
2135  }
2137 
2138  if (best_building) {
2139  return improvement_number(best_building);
2140  }
2141  return B_LAST;
2142 }
bool is_action_possible_on_city(action_id act_id, const struct player *actor_player, const struct city *target_city)
Returns TRUE if the wanted action can be done to the target city.
Definition: actions.cpp:5734
void action_list_end(action_id *act_list, int size)
Terminate an action list of the specified size.
Definition: actions.cpp:5881
bool is_action_enabled_unit_on_self(const action_id wanted_action, const struct unit *actor_unit)
Returns TRUE if actor_unit can do wanted_action to itself as far as action enablers are concerned.
Definition: actions.cpp:4196
bool is_action_enabled_unit_on_city(const action_id wanted_action, const struct unit *actor_unit, const struct city *target_city)
Returns TRUE if actor_unit can do wanted_action to target_city as far as action enablers are concerne...
Definition: actions.cpp:3948
void action_list_add_all_by_result(action_id *act_list, int *position, enum action_result result)
Add all actions with the specified result to the specified action list starting at the specified posi...
Definition: actions.cpp:5898
struct action * action_by_number(action_id act_id)
Return the action with the given id.
Definition: actions.cpp:1149
bool action_id_exists(const action_id act_id)
Returns TRUE iff the specified action ID refers to a valid action.
Definition: actions.cpp:1138
struct action_enabler_list * action_enablers_for_action(action_id action)
Get all enablers for an action in the current ruleset.
Definition: actions.cpp:1884
#define action_list_iterate_end
Definition: actions.h:413
#define action_list_iterate(_act_list_, _act_id_)
Definition: actions.h:401
#define action_enabler_list_iterate_end
Definition: actions.h:376
#define action_id_get_role(act_id)
Definition: actions.h:580
#define action_iterate_end
Definition: actions.h:383
#define MAX_NUM_ACTIONS
Definition: actions.h:223
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition: actions.h:374
#define action_iterate(_act_)
Definition: actions.h:378
#define action_id_get_target_kind(act_id)
Definition: actions.h:522
void building_advisor(struct player *pplayer)
Prime pcity->server.adv.building_want[].
#define TRADE_WEIGHTING
Definition: advbuilding.h:18
#define FOOD_WEIGHTING
Definition: advbuilding.h:16
#define SHIELD_WEIGHTING
Definition: advbuilding.h:17
void adv_deinit_choice(struct adv_choice *choice)
Clear choice without freeing it itself.
Definition: advchoice.cpp:42
void adv_init_choice(struct adv_choice *choice)
Sets the values of the choice to initial values.
Definition: advchoice.cpp:27
void adv_free_choice(struct adv_choice *choice)
Free dynamically allocated choice.
Definition: advchoice.cpp:67
struct adv_choice * adv_better_choice(struct adv_choice *first, struct adv_choice *second)
Return better one of the choices given.
Definition: advchoice.cpp:81
bool is_unit_choice_type(enum choice_type type)
Does choice type refer to unit.
Definition: advchoice.cpp:111
#define adv_choice_set_use(_choice, _use)
Definition: advchoice.h:69
#define ADV_CHOICE_ASSERT(c)
Definition: advchoice.h:77
static void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
Definition: advchoice.h:62
@ CT_CIVILIAN
Definition: advchoice.h:25
@ CT_LAST
Definition: advchoice.h:28
@ CT_DEFENDER
Definition: advchoice.h:27
@ CT_ATTACKER
Definition: advchoice.h:26
@ CT_NONE
Definition: advchoice.h:23
@ CT_BUILDING
Definition: advchoice.h:24
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Return a pointer to our data.
Definition: advdata.cpp:591
@ ADV_IMPR_CALCULATE_FULL
Definition: advdata.h:31
@ ADV_IMPR_ESTIMATE
Definition: advdata.h:32
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Get current default ai data related to player.
Definition: aidata.cpp:304
void dai_calc_data(const struct player *pplayer, int *trade, int *expenses, int *income)
Returns the total amount of trade generated (trade) and total amount of gold needed as upkeep (expens...
Definition: aihand.cpp:111
#define TECH_LOG(ait, _, pplayer, padvance, msg,...)
Definition: ailog.h:32
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition: aiplayer.h:47
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition: aiplayer.h:35
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition: aiplayer.h:41
void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
Reset ai settler engine.
Definition: aisettler.cpp:1124
void contemplate_new_city(struct ai_type *ait, struct city *pcity)
Return want for city settler.
Definition: aisettler.cpp:1228
const char * dai_choice_rule_name(const struct adv_choice *choice)
Return the (untranslated) rule name of the adv_choice.
Definition: aitools.cpp:105
int dai_gold_reserve(struct player *pplayer)
Credits the AI wants to have in reserves.
Definition: aitools.cpp:1120
int ai_trait_get_value(enum trait tr, struct player *pplayer)
Get current value of player trait.
Definition: aitraits.cpp:62
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Get unit type player can build, suitable to role, with given move type.
Definition: aiunit.cpp:3344
#define IS_ATTACKER(ptype)
Definition: aiunit.h:75
adv_want settler_evaluate_improvements(struct unit *punit, enum unit_activity *best_act, struct extra_type **best_target, struct tile **best_tile, PFPath *path, struct settlermap *state)
Finds tiles to improve, using punit.
bool base_city_celebrating(const struct city *pcity)
Return TRUE if the city was celebrating at the start of the turn, and it still has sufficient size to...
Definition: city.cpp:1555
int city_granary_size(int city_size)
Generalized formula used to calculate granary size.
Definition: city.cpp:2034
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Attach ai data to city.
Definition: city.cpp:3517
void add_specialist_output(const struct city *pcity, int *output, const std::vector< std::array< int, O_LAST >> *pcsoutputs)
Calculate output (gold, science, and luxury) generated by the specialists of a city.
Definition: city.cpp:2239
bool city_built_last_turn(const struct city *pcity)
Return TRUE if the city built something last turn (meaning production was completed between last turn...
Definition: city.cpp:2183
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Return TRUE iff the city has this building in it.
Definition: city.cpp:1189
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
bool is_capital(const struct city *pcity)
Return TRUE iff this city is its nation's capital.
Definition: city.cpp:1495
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Removes an improvement (and its effects) from a city.
Definition: city.cpp:3287
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Return the upkeep (gold) needed each turn to upkeep the given improvement in the given city.
Definition: city.cpp:1202
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
void add_tax_income(const struct player *pplayer, int trade, int *output)
Add the incomes of a city according to the taxrates (ignore # of specialists).
Definition: city.cpp:2150
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Return whether given city can build given building; returns FALSE if the building is obsolete.
Definition: city.cpp:815
int city_pollution(const struct city *pcity, int shield_total, int trade_total)
Calculate pollution for the city.
Definition: city.cpp:2692
int get_city_tithes_bonus(const struct city *pcity)
Return the amount of gold generated by buildings under "tithe" attribute governments.
Definition: city.cpp:2132
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
bool city_unhappy(const struct city *pcity)
Return TRUE iff the city is unhappy.
Definition: city.cpp:1544
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Return the factor (in %) by which the city's output should be multiplied.
Definition: city.cpp:2114
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Adds an improvement (and its effects) to a city.
Definition: city.cpp:3272
int city_map_radius_sq_get(const struct city *pcity)
Returns the current squared radius of the city.
Definition: city.cpp:130
bool city_base_to_city_map(int *city_map_x, int *city_map_y, const struct city *const pcity, const struct tile *map_tile)
Finds the city map coordinate for a given map position and a city.
Definition: city.cpp:274
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown, const std::vector< city * > &gov_centers, const cached_waste *pcwaste)
Give corruption/waste generated by city.
Definition: city.cpp:3108
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Calculate the output for the tile.
Definition: city.cpp:1230
bool is_city_center(const struct city *pcity, const struct tile *ptile)
Return TRUE if the city is centered at the given tile.
Definition: city.cpp:3497
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
bool can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Return whether player can eventually build given building in the city; returns FALSE if improvement c...
Definition: city.cpp:832
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Return whether given city can build given unit; returns FALSE if unit is obsolete.
Definition: city.cpp:894
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define city_tile_iterate(_radius_sq, _city_tile, _tile)
Definition: city.h:202
@ CITIZEN_ANGRY
Definition: city.h:243
@ CITIZEN_UNHAPPY
Definition: city.h:242
#define output_type_iterate(output)
Definition: city.h:764
#define city_list_iterate_end
Definition: city.h:484
@ FEELING_FINAL
Definition: city.h:256
#define city_tile_iterate_end
Definition: city.h:209
#define city_built_iterate(_pcity, _p)
Definition: city.h:752
#define city_built_iterate_end
Definition: city.h:759
#define output_type_iterate_end
Definition: city.h:771
void really_handle_city_buy(struct player *pplayer, struct city *pcity)
Handle buying request.
Definition: cityhand.cpp:278
void really_handle_city_sell(struct player *pplayer, struct city *pcity, struct impr_type *pimprove)
Handle improvement selling request.
Definition: cityhand.cpp:225
void city_map_update_empty(struct city *pcity, struct tile *ptile)
Change from worked to empty.
Definition: citytools.cpp:3079
void city_freeze_workers_queue(struct city *pcity)
Queue pending auto_arrange_workers() for later.
Definition: citytools.cpp:141
int city_production_buy_gold_cost(const struct city *pcity)
Return the cost (gold) to buy the current city production.
Definition: citytools.cpp:3459
void city_thaw_workers_queue()
Process the frozen workers.
Definition: citytools.cpp:171
void change_build_target(struct player *pplayer, struct city *pcity, struct universal *target, enum event_type event)
Change the build target.
Definition: citytools.cpp:2986
void do_sell_building(struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const char *reason)
Sell the improvement from the city, and give the player the owner.
Definition: citytools.cpp:2849
void sync_cities()
Make sure all players (clients) have up-to-date information about all their cities.
Definition: citytools.cpp:3150
void auto_arrange_workers(struct city *pcity)
Call sync_cities() to send the affected cities to the clients.
Definition: cityturn.cpp:359
bool city_refresh(struct city *pcity)
Updates unit upkeeps and city internal cached data.
Definition: cityturn.cpp:147
static void try_to_sell_stuff(struct player *pplayer, struct city *pcity)
Sell building from city.
Definition: daicity.cpp:354
#define AI_BA_RECALC_SPEED
Definition: daicity.cpp:69
#define AI_CITY_RECALC_SPEED
Definition: daicity.cpp:67
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)
Increase the degree to which we want to meet a set of requirements, because they will enable construc...
Definition: daicity.cpp:1336
static void unit_do_disband_trad(struct player *owner, struct unit *punit, const enum action_requester requester)
Try to disband punit in the traditional way.
Definition: daicity.cpp:455
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)
Increase want for a technologies because of the value of that technology in providing an improvement ...
Definition: daicity.cpp:152
static void increase_maxbuycost(struct player *pplayer, int new_value)
Increase maxbuycost.
Definition: daicity.cpp:372
static void dai_barbarian_choose_build(struct player *pplayer, struct city *pcity, struct adv_choice *choice)
Choose a build for the barbarian player.
Definition: daicity.cpp:198
void dai_city_save(struct ai_type *ait, const char *aitstr, struct section_file *file, const struct city *pcity, const char *citystr)
Write ai city segments to savefile.
Definition: daicity.cpp:1157
void dai_city_load(struct ai_type *ait, const char *aitstr, const struct section_file *file, struct city *pcity, const char *citystr)
Load ai city segment from savefile.
Definition: daicity.cpp:1185
#define LOG_BUY
Definition: daicity.cpp:61
static void dai_city_sell_noncritical(struct city *pcity, bool redundant_only)
Sell an noncritical building if there are any in the city.
Definition: daicity.cpp:1005
#define city_range_iterate_end
Definition: daicity.cpp:96
static int unit_food_upkeep(struct unit *punit)
Calculates a unit's food upkeep (per turn).
Definition: daicity.cpp:703
void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
One of the top level AI functions.
Definition: daicity.cpp:861
void dai_city_alloc(struct ai_type *ait, struct city *pcity)
Initialize city for use with default AI.
Definition: daicity.cpp:1129
#define LOG_EMERGENCY
Definition: daicity.cpp:62
void dai_consider_wonder_city(struct ai_type *ait, struct city *pcity, bool *result)
Is it ok for advisor code to consider given city as wonder city?
Definition: daicity.cpp:2079
static bool should_force_recalc(struct city *pcity)
Whether the AI should calculate the building wants for this city this turn, ahead of schedule.
Definition: daicity.cpp:1914
static int action_target_neg_util(action_id act_id, const struct city *pcity)
How undesirable for the owner of a particular city is the fact that it can be a target of a particula...
Definition: daicity.cpp:1223
#define city_range_iterate(city_here, list, range, city)
Definition: daicity.cpp:81
static void resolve_city_emergency(struct ai_type *ait, struct player *pplayer, struct city *pcity)
This function tries desperately to save a city from going under by revolt or starvation of food or re...
Definition: daicity.cpp:1048
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)
Decrease want for a technology because of the value of that technology in obsoleting an improvement e...
Definition: daicity.cpp:173
void dai_build_adv_adjust(struct ai_type *ait, struct player *pplayer, struct city *wonder_city)
Calculate how much an AI player should want to build particular improvements, because of the effects ...
Definition: daicity.cpp:1947
static void contemplate_terrain_improvements(struct ai_type *ait, struct city *pcity)
Estimates the want for a terrain improver (aka worker) by creating a virtual unit and feeding it to s...
Definition: daicity.cpp:762
static void dai_spend_gold(struct ai_type *ait, struct player *pplayer)
Buy and upgrade stuff!
Definition: daicity.cpp:525
#define LOG_WANT
Definition: daicity.cpp:63
static void adjust_improvement_wants_by_effects(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const bool already)
Calculate effects of possible improvements and extra effects of existing improvements.
Definition: daicity.cpp:1576
void dai_city_free(struct ai_type *ait, struct city *pcity)
Free city from use with default AI.
Definition: daicity.cpp:1142
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)
Increase want for a technology because of the value of that technology in providing an improvement ef...
Definition: daicity.cpp:124
Impr_type_id dai_find_source_building(struct city *pcity, enum effect_type effect_type, const struct unit_type *utype)
Returns a buildable, non-obsolete building that can provide the effect.
Definition: daicity.cpp:2095
void dai_build_adv_init(struct ai_type *ait, struct player *pplayer)
Initialize building advisor.
Definition: daicity.cpp:1928
static void dai_upgrade_units(struct city *pcity, int limit, bool military)
Try to upgrade a city's units.
Definition: daicity.cpp:383
#define CITY_EMERGENCY(pcity)
Definition: daicity.cpp:102
adv_want dai_city_want(struct player *pplayer, struct city *acity, struct adv_data *adv, struct impr_type *pimprove)
Calculates city want from some input values.
Definition: daicity.cpp:1419
static adv_want base_want(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove)
Calculates want for some buildings by actually adding the building and measuring the effect.
Definition: daicity.cpp:1496
static int unit_foodbox_cost(struct unit *punit)
Returns how much food a settler will consume out of the city's foodbox when created.
Definition: daicity.cpp:723
static bool building_crucial(const struct player *plr, struct impr_type *pimprove, const struct city *pcity)
Are effects provided by this building not needed?
Definition: daicity.cpp:983
static void dai_city_choose_build(struct ai_type *ait, struct player *pplayer, struct city *pcity)
Chooses what the city will build.
Definition: daicity.cpp:247
#define BUILDING_WAIT_MINIMUM
Definition: daicity.h:36
bool dai_on_war_footing(struct ai_type *ait, struct player *pplayer)
Are we going to be declaring war in a few turns time? If so, go on a war footing, and try to buy out ...
struct adv_choice * domestic_advisor_choose_build(struct ai_type *ait, struct player *pplayer, struct city *pcity)
This function should fill the supplied choice structure.
bool dai_can_requirement_be_met_in_city(const struct requirement *preq, const struct player *pplayer, const struct city *pcity)
Does the AI expect to ever be able to meet this requirement.
Definition: daieffects.cpp:723
adv_want dai_effect_value(struct player *pplayer, struct government *gov, const struct adv_data *adv, const struct city *pcity, const bool capital, int turns, const struct effect *peffect, const int c, const int nplayers)
How desirable is a particular effect for a particular city, given the number of cities in range (c).
Definition: daieffects.cpp:130
int assess_defense(struct ai_type *ait, struct city *pcity)
Estimate defense strength of city.
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)
This function selects either a defender or an attacker to be built.
bool dai_process_defender_want(struct ai_type *ait, struct player *pplayer, struct city *pcity, int danger, struct adv_choice *choice)
What would be the best defender for that city? Records the best defender type in choice.
int assess_defense_quadratic(struct ai_type *ait, struct city *pcity)
Need positive feedback in m_a_c_b and bodyguard routines.
bool ai_fuzzy(const struct player *pplayer, bool normal_decision)
Return the value normal_decision (a boolean), except if the AI is fuzzy, then sometimes flip the valu...
Definition: difficulty.cpp:318
struct effect_list * get_req_source_effects(struct universal *psource)
Get a list of effects with this requirement source.
Definition: effects.cpp:132
const effect_list * get_effects()
Get a list of all effects.
Definition: effects.cpp:117
bool building_has_effect(const struct impr_type *pimprove, enum effect_type effect_type)
Returns TRUE if the building has any effect bonuses of the given type.
Definition: effects.cpp:504
int get_city_bonus(const struct city *pcity, enum effect_type effect_type, enum vision_layer vlayer)
Returns the effect bonus at a city.
Definition: effects.cpp:688
#define effect_list_iterate_end
Definition: effects.h:349
#define effect_list_iterate(effect_list, peffect)
Definition: effects.h:347
float adv_want
Definition: fc_types.h:1144
int Impr_type_id
Definition: fc_types.h:293
@ RPT_POSSIBLE
Definition: fc_types.h:567
int action_id
Definition: fc_types.h:306
#define ADV_WANT_PRINTF
Definition: fc_types.h:1145
output_type_id
Definition: fc_types.h:84
@ O_SHIELD
Definition: fc_types.h:86
@ O_FOOD
Definition: fc_types.h:85
@ O_TRADE
Definition: fc_types.h:87
@ O_SCIENCE
Definition: fc_types.h:90
@ O_LUXURY
Definition: fc_types.h:89
@ O_GOLD
Definition: fc_types.h:88
@ O_LAST
Definition: fc_types.h:91
signed short Continent_id
Definition: fc_types.h:289
#define PL_(String1, String2, n)
Definition: fcintl.h:54
const struct ft_color ftc_server
const char * city_link(const struct city *pcity)
Get a text link to a city.
struct civ_game game
Definition: game.cpp:47
struct world wld
Definition: game.cpp:48
struct government * government_of_player(const struct player *pplayer)
Return the government of a player.
Definition: government.cpp:107
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
AI players may have handicaps - allowing them to cheat or preventing them from using certain algorith...
Definition: handicaps.cpp:62
@ H_AWAY
Definition: handicaps.h:18
@ H_PRODCHGPEN
Definition: handicaps.h:34
int impr_sell_gold(const struct impr_type *pimprove)
Returns the amount of gold received when this improvement is sold.
bool can_city_sell_building(const struct city *pcity, const struct impr_type *pimprove)
Return TRUE iff the city can sell the given improvement.
const char * improvement_rule_name(const struct impr_type *pimprove)
Return the (untranslated) rule name of the improvement.
bool is_improvement_redundant(const struct city *pcity, const struct impr_type *pimprove)
Returns TRUE if an improvement in a city is redundant, that is, the city wouldn't lose anything by lo...
bool can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
Whether player can eventually build given building somewhere – i.e., returns TRUE if building is avai...
struct city * city_from_small_wonder(const struct player *pplayer, const struct impr_type *pimprove)
Get the player city with this small wonder.
bool is_improvement(const struct impr_type *pimprove)
Is this building a regular improvement?
Impr_type_id improvement_number(const struct impr_type *pimprove)
Return the improvement index.
Impr_type_id improvement_index(const struct impr_type *pimprove)
Return the improvement index.
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
Returns the number of shields it takes to build this improvement.
bool is_wonder(const struct impr_type *pimprove)
Returns whether improvement is some kind of wonder.
bool is_great_wonder(const struct impr_type *pimprove)
Is this building a great wonder?
bool improvement_has_flag(const struct impr_type *pimprove, enum impr_flag_id flag)
Return TRUE if the impr has this flag otherwise FALSE.
const char * improvement_name_translation(const struct impr_type *pimprove)
Return the (translated) name of the given improvement.
bool is_small_wonder(const struct impr_type *pimprove)
Is this building a small wonder?
bool is_improvement_productive(const struct city *pcity, const struct impr_type *pimprove)
Returns TRUE if an improvement in a city is productive, in some way.
#define WONDER_NOT_OWNED
Definition: improvement.h:138
#define WONDER_NOT_BUILT
Definition: improvement.h:144
#define improvement_iterate_end
Definition: improvement.h:199
#define improvement_iterate(_p)
Definition: improvement.h:193
#define B_LAST
Definition: improvement.h:33
void initialize_infrastructure_cache(struct player *pplayer)
Do all tile improvement calculations and cache them for later.
Definition: infracache.cpp:245
#define fc_assert_msg(condition, message,...)
Definition: log.h:96
constexpr auto LOG_DEBUG
Definition: log.h:27
#define fc_assert_ret(condition)
Definition: log.h:112
constexpr auto LOG_ERROR
Definition: log.h:23
#define fc_assert(condition)
Definition: log.h:89
#define fc_assert_ret_msg(condition, message,...)
Definition: log.h:129
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
#define fc_assert_action(condition, action)
Definition: log.h:104
#define log_debug(message,...)
Definition: log.h:65
#define log_base(level, message,...)
Definition: log.h:41
void notify_player(const struct player *pplayer, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Similar to notify_conn_packet (see also), but takes player as "destination".
Definition: notify.cpp:284
struct city_list * cities
Definition: packhand.cpp:122
int player_get_expected_income(const struct player *pplayer)
Return the expected net income of the player this turn.
Definition: player.cpp:1194
std::vector< city * > player_gov_centers(const struct player *pplayer)
Locate the player's government centers.
Definition: player.cpp:1264
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
Definition: player.cpp:816
#define players_iterate_end
Definition: player.h:520
#define players_iterate(_pplayer)
Definition: player.h:514
static bool is_barbarian(const struct player *pplayer)
Definition: player.h:474
int normal_player_count()
Return the number of non-barbarian players.
Definition: plrhand.cpp:3140
#define fc_rand(_size)
Definition: rand.h:16
bool secfile_lookup_bool_default(const struct section_file *secfile, bool def, const char *path,...)
Lookup a boolean value in the secfile.
int secfile_lookup_int_default(const struct section_file *secfile, int def, const char *path,...)
Lookup a integer value in the secfile.
#define secfile_insert_int(secfile, value, path,...)
Definition: registry_ini.h:116
#define secfile_insert_bool(secfile, value, path,...)
Definition: registry_ini.h:79
bool is_req_active(const struct player *target_player, const struct player *other_player, const struct city *target_city, const struct impr_type *target_building, const struct tile *target_tile, const struct unit *target_unit, const struct unit_type *target_unittype, const struct output_type *target_output, const struct specialist *target_specialist, const struct action *target_action, const struct requirement *req, const enum req_problem_type prob_type, const enum vision_layer vision_layer, const enum national_intelligence nintel)
Checks the requirement to see if it is active on the given target.
#define requirement_vector_iterate_end
Definition: requirements.h:80
#define requirement_vector_iterate(req_vec, preq)
Definition: requirements.h:78
int research_goal_unknown_techs(const struct research *presearch, Tech_type_id goal)
Returns the number of technologies the player need to research to get the goal technology.
Definition: research.cpp:745
struct research * research_get(const struct player *pplayer)
Returns the research structure associated with the player.
Definition: research.cpp:110
int research_goal_bulbs_required(const struct research *presearch, Tech_type_id goal)
Function to determine cost (in bulbs) of reaching goal technology.
Definition: research.cpp:767
#define CLIP(lower, current, upper)
Definition: shared.h:51
#define MIN(x, y)
Definition: shared.h:49
#define MAX(x, y)
Definition: shared.h:48
#define DEFAULT_SPECIALIST
Definition: specialist.h:37
size_t size
Definition: specvec.h:64
@ AIT_CITY_MILITARY
Definition: srv_log.h:65
@ AIT_BUILDINGS
Definition: srv_log.h:53
@ AIT_CITY_TERRAIN
Definition: srv_log.h:66
@ AIT_CITY_SETTLERS
Definition: srv_log.h:67
@ AIT_EMERGENCY
Definition: srv_log.h:64
#define UNIT_LOG(_, punit, msg,...)
Definition: srv_log.h:95
@ TIMER_STOP
Definition: srv_log.h:77
@ TIMER_START
Definition: srv_log.h:77
#define CITY_LOG(_, pcity, msg,...)
Definition: srv_log.h:80
#define TIMING_LOG(timer, activity)
Definition: srv_log.h:121
action_id id
Definition: actions.h:306
enum action_result result
Definition: actions.h:308
enum choice_type type
Definition: advchoice.h:32
adv_want want
Definition: advchoice.h:34
universals_u value
Definition: advchoice.h:33
int pollution_priority
Definition: advdata.h:110
int shield_priority
Definition: advdata.h:102
int * cities
Definition: advdata.h:86
int * ocean_cities
Definition: advdata.h:87
struct adv_data::@87 stats
int luxury_priority
Definition: advdata.h:104
int food_priority
Definition: advdata.h:103
enum adv_improvement_status impr_calc[B_LAST]
Definition: advdata.h:49
int wonder_city
Definition: advdata.h:46
enum req_range impr_range[B_LAST]
Definition: advdata.h:50
int science_priority
Definition: advdata.h:106
int gold_priority
Definition: advdata.h:105
Definition: tech.h:113
int urgency
Definition: daicity.h:46
struct adv_choice choice
Definition: daicity.h:38
bool founder_boat
Definition: daicity.h:61
int building_wait
Definition: daicity.h:35
int founder_turn
Definition: daicity.h:62
int founder_want
Definition: daicity.h:63
int worker_want
Definition: daicity.h:64
int building_turn
Definition: daicity.h:34
adv_want worth
Definition: daicity.h:32
int grave_danger
Definition: daicity.h:45
struct unit_type * worker_type
Definition: daicity.h:65
Definition: aidata.h:63
int * ocean_workers
Definition: aidata.h:75
int * workers
Definition: aidata.h:74
adv_want tech_want[A_LAST+1]
Definition: aidata.h:95
struct ai_plr::@155 stats
Definition: ai.h:42
void * pprivate
Definition: ai.h:45
Definition: city.h:291
int turn_last_built
Definition: city.h:358
int surplus[O_LAST]
Definition: city.h:324
int id
Definition: city.h:296
struct player * owner
Definition: city.h:294
int citizen_base[O_LAST]
Definition: city.h:328
int anarchy
Definition: city.h:355
struct universal production
Definition: city.h:368
struct city::@15::@17 server
citizens feel[CITIZEN_LAST][FEELING_LAST]
Definition: city.h:302
citizens specialists[SP_MAX]
Definition: city.h:305
struct tile * tile
Definition: city.h:293
int shield_stock
Definition: city.h:339
struct unit_list * units_supported
Definition: city.h:377
struct civ_game::@28::@32 server
struct packet_game_info info
Definition: game.h:80
bool contemplace_workers
Definition: aiplayer.h:54
struct requirement_vector obsolete_by
Definition: improvement.h:67
struct requirement_vector reqs
Definition: improvement.h:66
int maxbuycost
Definition: player.h:107
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
struct player_ai ai_common
Definition: player.h:270
int wonders[B_LAST]
Definition: player.h:283
struct team * team
Definition: player.h:243
struct player_economic economic
Definition: player.h:266
enum req_range range
Definition: requirements.h:69
Definition: tile.h:42
struct unit_list * units
Definition: tile.h:50
int pop_cost
Definition: unittype.h:477
int attack_strength
Definition: unittype.h:479
Definition: unit.h:134
int id
Definition: unit.h:141
int homecity
Definition: unit.h:142
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
struct civ_map map
Definition: world_object.h:21
Tech_type_id advance_index(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:76
Tech_type_id advance_number(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:85
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
#define tile_worked(_tile)
Definition: tile.h:97
#define TILE_XY(ptile)
Definition: tile.h:36
#define tile_continent(_tile)
Definition: tile.h:74
int city_num_trade_routes(const struct city *pcity)
Return number of trade route city has.
#define trade_routes_iterate_end
Definition: traderoutes.h:127
#define trade_routes_iterate(c, proute)
Definition: traderoutes.h:122
#define TRAIT_DEFAULT_VALUE
Definition: traits.h:27
const struct unit_type * utype
Definition: fc_types.h:585
const struct impr_type * building
Definition: fc_types.h:579
bool unit_being_aggressive(const struct unit *punit)
An "aggressive" unit is a unit which may cause unhappiness under a Republic or Democracy.
Definition: unit.cpp:1399
bool unit_is_alive(int id)
Check if unit with given id is still alive.
Definition: unit.cpp:2014
bool is_field_unit(const struct unit *punit)
This checks the "field unit" flag on the unit.
Definition: unit.cpp:355
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Return TRUE iff this unit can do the specified generalized (ruleset defined) action enabler controlle...
Definition: unit.cpp:309
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
Definition: unit.cpp:1588
const char * get_activity_text(enum unit_activity activity)
Return the name of the activity in a static buffer.
Definition: unit.cpp:586
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Create a virtual unit skeleton.
Definition: unit.cpp:1490
void unit_tile_set(struct unit *punit, struct tile *ptile)
Set the tile location of the unit.
Definition: unit.cpp:1200
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_owner(_pu)
Definition: unit.h:370
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)
Execute a request to perform an action and let the caller know if it was performed or not.
Definition: unithand.cpp:2737
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)
Handle unit action.
Definition: unithand.cpp:2718
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_safe(unitlist, _unit)
Definition: unitlist.h:33
#define unit_list_iterate_end
Definition: unitlist.h:27
#define unit_list_iterate_safe_end
Definition: unitlist.h:54
const char * unit_rule_name(const struct unit *punit)
Return the (untranslated) rule name of the unit.
Definition: unittype.cpp:1283
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
struct unit_type * best_role_unit(const struct city *pcity, int role)
Return "best" unit this city can build, with given role/flag.
Definition: unittype.cpp:1920
int utype_upkeep_cost(const struct unit_type *ut, struct player *pplayer, Output_type_id otype)
Returns the upkeep of a unit of this type under the given government.
Definition: unittype.cpp:123
const char * utype_rule_name(const struct unit_type *punittype)
Return the (untranslated) rule name of the unit type.
Definition: unittype.cpp:1274
bool utype_is_cityfounder(const struct unit_type *utype)
Is cityfounder type.
Definition: unittype.cpp:2549
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Return whether this player can upgrade this unit type (to any other unit type).
Definition: unittype.cpp:1379
int utype_build_shield_cost(const struct city *pcity, const struct unit_type *punittype)
Returns the number of shields it takes to build this unit type.
Definition: unittype.cpp:1141
int num_role_units(int role)
How many unit types have specified role/flag.
Definition: unittype.cpp:1866
bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
Return whether the unit has the given role.
Definition: unittype.cpp:195
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
Definition: unittype.cpp:176
struct unit_type * get_role_unit(int role, int role_index)
Return index-th unit with specified role/flag.
Definition: unittype.cpp:1900
int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Return the "happy cost" (the number of citizens who are discontented) for this unit.
Definition: unittype.cpp:167
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Return the cost (gold) of upgrading a single unit of the specified type to the new type.
Definition: unittype.cpp:1407