Freeciv21
Develop your civilization from humble roots to a global empire
daimilitary.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 "log.h"
18 
19 // common
20 #include "combat.h"
21 #include "game.h"
22 #include "government.h"
23 #include "map.h"
24 #include "movement.h"
25 #include "research.h"
26 #include "specialist.h"
27 #include "unitlist.h"
28 
29 /* common/aicore */
30 #include "pf_tools.h"
31 
32 // server
33 #include "srv_log.h"
34 #include "srv_main.h"
35 
36 /* server/advisors */
37 #include "advbuilding.h"
38 #include "advchoice.h"
39 #include "advdata.h"
40 #include "advtools.h"
41 #include "infracache.h" // adv_city
42 
43 // ai
44 #include "aitraits.h"
45 #include "difficulty.h"
46 #include "handicaps.h"
47 
48 /* ai/default */
49 #include "aiair.h"
50 #include "aidata.h"
51 #include "aidiplomat.h"
52 #include "aiferry.h"
53 #include "aihand.h"
54 #include "aihunt.h"
55 #include "ailog.h"
56 #include "aiparatrooper.h"
57 #include "aiplayer.h"
58 #include "aitech.h"
59 #include "aitools.h"
60 #include "aiunit.h"
61 #include "daicity.h"
62 #include "daieffects.h"
63 #include "daimilitary.h"
64 
65 static int assess_danger(struct ai_type *ait, struct city *pcity,
66  const struct civ_map *dmap,
68 
73  struct unit *attacker)
74 {
75  struct unit_type *bestunit = nullptr;
76  double best = 0;
77  int best_cost = FC_INFINITY;
78  struct player *pplayer = city_owner(pcity);
79 
81  {
82  if (can_city_build_unit_now(pcity, punittype)) {
83  int fpatt, fpdef, defense, attack;
84  double want, loss, cost = utype_build_shield_cost(pcity, punittype);
85  struct unit *defender;
86  int veteran = get_unittype_bonus(city_owner(pcity), pcity->tile,
87  punittype, EFT_VETERAN_BUILD);
88 
89  defender = unit_virtual_create(pplayer, pcity, punittype, veteran);
90  defense = get_total_defense_power(attacker, defender);
91  attack = get_total_attack_power(attacker, defender);
92  get_modified_firepower(attacker, defender, &fpatt, &fpdef);
93 
94  /* Greg's algorithm. loss is the average number of health lost by
95  * defender. If loss > attacker's hp then we should win the fight,
96  * which is always a good thing, since we avoid shield loss. */
97  loss = static_cast<double>(defense) * punittype->hp * fpdef
98  / (attack * fpatt);
99  want = (loss + MAX(0, loss - attacker->hp)) / cost;
100 
101  if (want > best || (want == best && cost <= best_cost)) {
102  best = want;
103  bestunit = punittype;
104  best_cost = cost;
105  }
106  unit_virtual_destroy(defender);
107  }
108  }
110 
111  return bestunit;
112 }
113 
119 static struct unit_type *dai_choose_attacker(struct ai_type *ait,
120  struct city *pcity,
121  enum terrain_class tc,
122  bool allow_gold_upkeep)
123 {
124  struct unit_type *bestid = nullptr;
125  int best = -1;
126  int cur;
127  struct player *pplayer = city_owner(pcity);
128 
130  {
131  if (!allow_gold_upkeep
132  && utype_upkeep_cost(putype, pplayer, O_GOLD) > 0) {
133  continue;
134  }
135  if (!bestid) {
136  bestid = putype;
137  }
138  cur = dai_unit_attack_desirability(ait, putype);
139  if ((tc == TC_LAND && utype_class(putype)->adv.land_move != MOVE_NONE)
140  || (tc == TC_OCEAN
141  && utype_class(putype)->adv.sea_move != MOVE_NONE)) {
142  if (can_city_build_unit_now(pcity, putype)
143  && (cur > best
144  || (cur == best
145  && utype_build_shield_cost(pcity, putype)
146  <= utype_build_shield_cost(pcity, bestid)))) {
147  best = cur;
148  bestid = putype;
149  }
150  }
151  }
153 
154  return bestid;
155 }
156 
165 static struct unit_type *dai_choose_bodyguard(struct ai_type *ait,
166  struct city *pcity,
167  enum terrain_class tc,
168  enum unit_role_id role,
169  bool allow_gold_upkeep)
170 {
171  struct unit_type *bestid = nullptr;
172  int best = 0;
173  struct player *pplayer = city_owner(pcity);
174 
176  {
177  // Only consider units of given role, or any if invalid given
178  if (unit_role_id_is_valid(role)) {
179  if (!utype_has_role(putype, role)) {
180  continue;
181  }
182  }
183 
184  if (!bestid) {
185  bestid = putype;
186  }
187  if (!allow_gold_upkeep
188  && utype_upkeep_cost(putype, pplayer, O_GOLD) > 0) {
189  continue;
190  }
191 
192  // Only consider units of same move type
193  if ((tc == TC_LAND && utype_class(putype)->adv.land_move == MOVE_NONE)
194  || (tc == TC_OCEAN
195  && utype_class(putype)->adv.sea_move == MOVE_NONE)) {
196  continue;
197  }
198 
199  // Now find best
200  if (can_city_build_unit_now(pcity, putype)) {
201  const int desire = dai_unit_defence_desirability(ait, putype);
202 
203  if (desire > best
204  || (desire == best
205  && utype_build_shield_cost(pcity, putype)
206  <= utype_build_shield_cost(pcity, bestid))) {
207  best = desire;
208  bestid = putype;
209  }
210  }
211  }
213 
214  return bestid;
215 }
216 
220 static int base_assess_defense_unit(struct city *pcity, struct unit *punit,
221  bool igwall, bool quadratic,
222  int wall_value)
223 {
224  int defense;
225  bool do_wall = false;
226 
227  if (!is_military_unit(punit)) {
228  return 0;
229  }
230 
231  defense = get_fortified_defense_power(nullptr, punit) * punit->hp;
232  if (unit_has_type_flag(punit, UTYF_BADCITYDEFENDER)) {
233  // Attacker firepower doubled, defender firepower set to 1
234  defense /= 2;
235  } else {
236  defense *= unit_type_get(punit)->firepower;
237  }
238 
239  if (pcity) {
240  /* FIXME: We check if city got defense effect against *some*
241  * unit type. Sea unit danger might cause us to build defenses
242  * against air units... */
243  do_wall = (!igwall && city_got_defense_effect(pcity, nullptr));
244  }
245  defense /= POWER_DIVIDER;
246 
247  if (quadratic) {
248  defense *= defense;
249  }
250 
251  if (do_wall) {
252  defense *= wall_value;
253  defense /= 10;
254  }
255 
256  return defense;
257 }
258 
262 int assess_defense_quadratic(struct ai_type *ait, struct city *pcity)
263 {
264  int defense = 0, walls = 0;
265  // This can be an arg if needed, but we don't need to change it now.
266  const bool igwall = false;
267  struct ai_city *city_data = def_ai_city_data(pcity, ait);
268 
269  /* wallvalue = 10, walls = 10,
270  * wallvalue = 40, walls = 20,
271  * wallvalue = 90, walls = 30 */
272 
273  while (walls * walls < city_data->wallvalue * 10) {
274  walls++;
275  }
276 
277  unit_list_iterate(pcity->tile->units, punit)
278  {
279  defense += base_assess_defense_unit(pcity, punit, igwall, false, walls);
280  }
282 
283  if (defense > 1 << 12) {
284  CITY_LOG(LOG_VERBOSE, pcity,
285  "Overflow danger in assess_defense_quadratic:"
286  " %d",
287  defense);
288  if (defense > 1 << 15) {
289  defense = 1 << 15; // more defense than we know what to do with!
290  }
291  }
292 
293  return defense * defense;
294 }
295 
300 int assess_defense_unit(struct ai_type *ait, struct city *pcity,
301  struct unit *punit, bool igwall)
302 {
303  return base_assess_defense_unit(pcity, punit, igwall, true,
304  def_ai_city_data(pcity, ait)->wallvalue);
305 }
306 
315 static int assess_defense_backend(struct ai_type *ait, struct city *pcity,
316  bool igwall)
317 {
318  // Estimate of our total city defensive might
319  int defense = 0;
320 
321  unit_list_iterate(pcity->tile->units, punit)
322  {
323  defense += assess_defense_unit(ait, pcity, punit, igwall);
324  }
326 
327  return defense;
328 }
329 
333 int assess_defense(struct ai_type *ait, struct city *pcity)
334 {
335  return assess_defense_backend(ait, pcity, false);
336 }
337 
342 static int assess_defense_igwall(struct ai_type *ait, struct city *pcity)
343 {
344  return assess_defense_backend(ait, pcity, true);
345 }
346 
350 static int assess_danger_unit(const struct city *pcity,
351  struct pf_reverse_map *pcity_map,
352  const struct unit *punit, int *move_time)
353 {
354  struct pf_position pos;
355  const struct unit_type *punittype = unit_type_get(punit);
356  const struct tile *ptile = city_tile(pcity);
357  const struct unit *ferry;
358  int danger;
359  int mod;
360 
361  *move_time = PF_IMPOSSIBLE_MC;
362 
363  int dist = real_map_distance(punit->tile, pcity->tile);
364  if (dist > 30) {
365  return 0;
366  }
367  if (dist > 15 && punit->tile->continent != pcity->tile->continent) {
368  return 0;
369  }
370 
371  if (utype_can_do_action(punittype, ACTION_PARADROP)
372  && 0 < punittype->paratroopers_range) {
373  *move_time = (real_map_distance(ptile, unit_tile(punit))
374  / punittype->paratroopers_range);
375  }
376 
377  if (pf_reverse_map_unit_position(pcity_map, punit, &pos)
378  && (PF_IMPOSSIBLE_MC == *move_time || *move_time > pos.turn)) {
379  *move_time = pos.turn;
380  }
381 
382  if (unit_transported(punit) && (ferry = unit_transport_get(punit))
383  && pf_reverse_map_unit_position(pcity_map, ferry, &pos)) {
384  if ((PF_IMPOSSIBLE_MC == *move_time || *move_time > pos.turn)) {
385  *move_time = pos.turn;
386  if (!can_attack_from_non_native(punittype)) {
387  (*move_time)++;
388  }
389  }
390  }
391 
392  if (PF_IMPOSSIBLE_MC == *move_time) {
393  return 0;
394  }
395  if (!is_native_tile(punittype, ptile)
396  && !can_attack_non_native(punittype)) {
397  return 0;
398  }
399  if (!is_native_near_tile(&(wld.map), unit_class_get(punit), ptile)) {
400  return 0;
401  }
402 
403  danger = adv_unit_att_rating(punit);
404  mod = 100
405  + get_unittype_bonus(city_owner(pcity), ptile, punittype,
406  EFT_DEFEND_BONUS);
407  return danger * 100 / MAX(mod, 1);
408 }
409 
415 void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer,
416  const struct civ_map *dmap)
417 {
418  // Do nothing if game is not running
419  if (S_S_RUNNING == server_state()) {
420  city_list_iterate(pplayer->cities, pcity)
421  {
422  (void) assess_danger(ait, pcity, dmap, nullptr);
423  }
425  }
426 }
427 
447 static void dai_reevaluate_building(struct city *pcity, adv_want *value,
448  int urgency, int danger, int defense)
449 {
450  if (*value == 0 || danger <= 0) {
451  return;
452  }
453 
454  *value = MAX(*value, 100 + urgency); // default
455 
456  if (urgency > 0 && danger > defense * 2) {
457  *value += 100;
458  } else if (defense != 0 && danger > defense) {
459  *value = MAX(danger * 100.f / defense, *value);
460  }
461 }
462 
481 static int assess_danger(struct ai_type *ait, struct city *pcity,
482  const struct civ_map *dmap,
484 {
485  struct player *pplayer = city_owner(pcity);
486  struct tile *ptile = city_tile(pcity);
487  struct ai_city *city_data = def_ai_city_data(pcity, ait);
488  int danger_reduced[B_LAST]; /* How much such danger there is that
489  * building would help against. */
490  int i;
491  int defender;
492  int urgency = 0;
493  int defense;
494  int total_danger = 0;
495  int defense_bonuses_pct[U_LAST];
496  bool defender_type_handled[U_LAST] = {false};
497  int assess_turns;
498  bool omnimap;
499 
501 
502  // Initialize data.
503  memset(&danger_reduced, 0, sizeof(danger_reduced));
504  if (has_handicap(pplayer, H_DANGER)) {
505  // Always thinks that city is in grave danger
506  city_data->grave_danger = 1;
507  } else {
508  city_data->grave_danger = 0;
509  }
510  city_data->diplomat_threat = false;
511  city_data->has_diplomat = false;
512 
513  unit_type_iterate(utype)
514  {
515  defense_bonuses_pct[utype_index(utype)] = 0;
516  defender_type_handled[utype_index(utype)] = false;
517  }
519 
520  unit_list_iterate(ptile->units, punit)
521  {
522  const struct unit_type *def = unit_type_get(punit);
523 
524  if (unit_has_type_flag(punit, UTYF_DIPLOMAT)) {
525  city_data->has_diplomat = true;
526  }
527  if (!defender_type_handled[utype_index(def)]) {
528  /* This is first defender of this type. Check defender type
529  * specific bonuses. */
530 
531  /* Skip defenders that have no bonuses at all. Acceptable
532  * side-effect is that we can't consider negative bonuses at
533  * all ("No bonuses" should be better than "negative bonus") */
534  if (def->cache.max_defense_mp_pct > 0) {
535  unit_type_iterate(utype)
536  {
537  int idx = utype_index(utype);
538 
539  if (def->cache.defense_mp_bonuses_pct[idx]
540  > defense_bonuses_pct[idx]) {
541  defense_bonuses_pct[idx] =
542  def->cache.defense_mp_bonuses_pct[idx];
543  }
544  }
546  }
547 
548  defender_type_handled[utype_index(def)] = true;
549  }
550  }
552 
553  if (player_is_cpuhog(pplayer)) {
554  assess_turns = 6;
555  } else {
556  assess_turns = 3;
557  }
558 
559  omnimap = !has_handicap(pplayer, H_MAP);
560 
561  // Check.
562  players_iterate(aplayer)
563  {
564  struct pf_reverse_map *pcity_map;
565  struct unit_list *units;
566 
567  if (!adv_is_player_dangerous(pplayer, aplayer)) {
568  continue;
569  }
570  /* Note that we still consider the units of players we are not (yet)
571  * at war with. */
572 
573  pcity_map = pf_reverse_map_new_for_city(pcity, aplayer, assess_turns,
574  omnimap, dmap);
575 
576  if (ul_cb != nullptr) {
577  units = ul_cb(aplayer);
578  } else {
579  units = aplayer->units;
580  }
581  unit_list_iterate(units, punit)
582  {
583  int move_time;
584  int vulnerability;
585  int defbonus_pct;
586  const struct unit_type *utype = unit_type_get(punit);
587  struct unit_type_ai *utai =
588  static_cast<unit_type_ai *>(utype_ai_data(utype, ait));
589 
590  if (!utai->carries_occupiers && !utype_acts_hostile(utype)) {
591  // Harmless unit.
592  continue;
593  }
594 
595  vulnerability =
596  assess_danger_unit(pcity, pcity_map, punit, &move_time);
597 
598  if (PF_IMPOSSIBLE_MC == move_time) {
599  continue;
600  }
601 
602  if ((0 < vulnerability && unit_can_take_over(punit))
603  || utai->carries_occupiers) {
604  if (3 >= move_time) {
605  urgency++;
606  if (1 >= move_time) {
607  city_data->grave_danger++;
608  }
609  }
610  }
611 
612  defbonus_pct = defense_bonuses_pct[utype_index(utype)];
613  if (defbonus_pct > 100) {
614  defbonus_pct = (defbonus_pct + 100) / 2;
615  }
616  vulnerability = vulnerability * 100 / (defbonus_pct + 100);
617  (void) dai_wants_defender_against(ait, pplayer, pcity, utype,
618  vulnerability / MAX(move_time, 1));
619 
620  if (utype_acts_hostile(unit_type_get(punit)) && 2 >= move_time) {
621  city_data->diplomat_threat = true;
622  }
623 
624  vulnerability *= vulnerability; // positive feedback
625  if (1 < move_time) {
626  vulnerability /= move_time;
627  }
628 
629  if (unit_can_do_action(punit, ACTION_NUKE)
630  || unit_can_do_action(punit, ACTION_NUKE_CITY)
631  || unit_can_do_action(punit, ACTION_NUKE_UNITS)) {
632  defender = dai_find_source_building(pcity, EFT_NUKE_PROOF,
633  unit_type_get(punit));
634  if (defender != B_LAST) {
635  danger_reduced[defender] += vulnerability / MAX(move_time, 1);
636  }
637  } else {
638  defender = dai_find_source_building(pcity, EFT_DEFEND_BONUS,
639  unit_type_get(punit));
640  if (defender != B_LAST) {
641  danger_reduced[defender] += vulnerability / MAX(move_time, 1);
642  }
643  }
644 
645  total_danger += vulnerability;
646  }
648 
649  pf_reverse_map_destroy(pcity_map);
650  }
652 
653  if (total_danger) {
654  city_data->wallvalue = 90;
655  } else {
656  /* No danger.
657  * This is half of the wallvalue of what danger 1 would produce. */
658  city_data->wallvalue = 5;
659  }
660 
661  if (0 < city_data->grave_danger) {
662  // really, REALLY urgent to defend
663  urgency += 10 * city_data->grave_danger;
664  }
665 
666  /* HACK: This needs changing if multiple improvements provide
667  * this effect. */
668  /* FIXME: Accept only buildings helping unit classes we actually use.
669  * Now we consider any land mover helper suitable. */
670  defense = assess_defense_igwall(ait, pcity);
671 
672  for (i = 0; i < B_LAST; i++) {
673  if (0 < danger_reduced[i]) {
674  dai_reevaluate_building(pcity, &pcity->server.adv->building_want[i],
675  urgency, danger_reduced[i], defense);
676  }
677  }
678 
679  if (has_handicap(pplayer, H_DANGER) && 0 == total_danger) {
680  /* Has to have some danger
681  * Otherwise grave_danger will be ignored. */
682  city_data->danger = 1;
683  } else {
684  city_data->danger = total_danger;
685  }
686  city_data->urgency = urgency;
687 
689 
690  return urgency;
691 }
692 
698  const struct unit_type *punittype)
699 {
700  int desire = punittype->hp;
701  int attack = punittype->attack_strength;
702  int defense = punittype->defense_strength;
703  int maxbonus_pct = 0;
704 
705  /* Sea and helicopters often have their firepower set to 1 when
706  * defending. We can't have such units as defenders. */
707  if (!utype_has_flag(punittype, UTYF_BADCITYDEFENDER)
708  && !(static_cast<struct unit_type_ai *>(utype_ai_data(punittype, ait)))
709  ->firepower1) {
710  /* Sea units get 1 firepower in Pearl Harbour,
711  * and helicopters very bad against fighters */
712  desire *= punittype->firepower;
713  }
714  desire *= defense;
715  desire += punittype->move_rate / SINGLE_MOVE;
716  desire += attack;
717 
718  maxbonus_pct = punittype->cache.max_defense_mp_pct;
719  if (maxbonus_pct > 100) {
720  maxbonus_pct = (maxbonus_pct + 100) / 2;
721  }
722  desire += desire * maxbonus_pct / 100;
723  if (utype_has_flag(punittype, UTYF_GAMELOSS)) {
724  desire /= 10; // but might actually be worth it
725  }
726 
727  return desire;
728 }
729 
734  const struct unit_type *punittype)
735 {
736  int desire = punittype->hp;
737  int attack = punittype->attack_strength;
738  int defense = punittype->defense_strength;
739 
740  desire *= punittype->move_rate;
741  desire *= punittype->firepower;
742  desire *= attack;
743  desire += defense;
744  if (utype_has_flag(punittype, UTYF_IGTER)) {
745  desire += desire / 2;
746  }
747  if (utype_has_flag(punittype, UTYF_GAMELOSS)) {
748  desire /= 10; // but might actually be worth it
749  }
750  if (utype_has_flag(punittype, UTYF_CITYBUSTER)) {
751  desire += desire / 2;
752  }
753  if (can_attack_from_non_native(punittype)) {
754  desire += desire / 4;
755  }
756  if (punittype->adv.igwall) {
757  desire += desire / 4;
758  }
759  return desire;
760 }
761 
767 bool dai_process_defender_want(struct ai_type *ait, struct player *pplayer,
768  struct city *pcity, int danger,
769  struct adv_choice *choice)
770 {
771  const struct research *presearch = research_get(pplayer);
772  /* FIXME: We check if the city has *some* defensive structure,
773  * but not whether the city has a defensive structure against
774  * any specific attacker. The actual danger may not be mitigated
775  * by the defense selected... */
776  bool walls = city_got_defense_effect(pcity, nullptr);
777  // Technologies we would like to have.
778  int tech_desire[U_LAST];
779  // Our favourite unit.
780  int best = -1;
781  struct unit_type *best_unit_type = nullptr;
782  int best_unit_cost = 1;
783  struct ai_city *city_data = def_ai_city_data(pcity, ait);
784  struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
785 
786  memset(tech_desire, 0, sizeof(tech_desire));
787 
788  simple_ai_unit_type_iterate(punittype)
789  {
790  int desire; // How much we want the unit?
791 
792  /* Only consider proper defenders - otherwise waste CPU and
793  * bump tech want needlessly. */
794  if (!utype_has_role(punittype, L_DEFEND_GOOD)
795  && !utype_has_role(punittype, L_DEFEND_OK)) {
796  continue;
797  }
798 
799  desire = dai_unit_defence_desirability(ait, punittype);
800 
801  if (!utype_has_role(punittype, L_DEFEND_GOOD)) {
802  desire /= 2; // not good, just ok
803  }
804 
805  if (utype_has_flag(punittype, UTYF_FIELDUNIT)) {
806  /* Causes unhappiness even when in defense, so not a good
807  * idea for a defender, unless it is _really_ good */
808  desire /= 2;
809  }
810 
811  desire /= POWER_DIVIDER / 2; // Good enough, no rounding errors.
812  desire *= desire;
813 
814  if (can_city_build_unit_now(pcity, punittype)) {
815  // We can build the unit now...
816 
817  int build_cost = utype_build_shield_cost(pcity, punittype);
818  int limit_cost = pcity->shield_stock + 40;
819 
820  if (walls && !utype_has_flag(punittype, UTYF_BADCITYDEFENDER)) {
821  desire *= city_data->wallvalue;
822  // TODO: More use of POWER_FACTOR !
823  desire /= POWER_FACTOR;
824  }
825 
826  if ((best_unit_cost > limit_cost && build_cost < best_unit_cost)
827  || ((desire > best
828  || (desire == best && build_cost <= best_unit_cost))
829  && (best_unit_type == nullptr
830  // In case all units are more expensive than limit_cost
831  || limit_cost <= pcity->shield_stock + 40))) {
832  best = desire;
833  best_unit_type = punittype;
834  best_unit_cost = build_cost;
835  }
836  } else if (can_city_build_unit_later(pcity, punittype)) {
837  // We first need to develop the tech required by the unit...
838 
839  // Cost (shield equivalent) of gaining these techs.
840  /* FIXME? Katvrr advises that this should be weighted more heavily in
841  * big danger. */
842  int notzero = city_list_size(pplayer->cities);
843  fc_assert_ret_val(notzero, false);
844  int tech_cost =
846  presearch, advance_number(punittype->require_advance))
847  / 4 / notzero;
848 
849  /* Contrary to the above, we don't care if walls are actually built
850  * - we're looking into the future now. */
851  if (!utype_has_flag(punittype, UTYF_BADCITYDEFENDER)) {
852  desire *= city_data->wallvalue;
853  desire /= POWER_FACTOR;
854  }
855 
856  // Yes, there's some similarity with kill_desire().
857  // TODO: Explain what shield cost has to do with tech want.
858  tech_desire[utype_index(punittype)] =
859  (desire * danger
860  / (utype_build_shield_cost(pcity, punittype) + tech_cost));
861  }
862  }
864 
865  if (best == -1) {
866  CITY_LOG(LOG_DEBUG, pcity, "Ooops - we cannot build any defender!");
867  }
868 
869  if (best_unit_type) {
870  if (!walls && !utype_has_flag(best_unit_type, UTYF_BADCITYDEFENDER)) {
871  best *= city_data->wallvalue;
872  best /= POWER_FACTOR;
873  }
874  } else {
875  best_unit_cost = 100; /* Building impossible is considered costly.
876  * This should increase want for tech providing
877  * first defender type. */
878  }
879 
880  if (best <= 0) {
881  best = 1; // Avoid division by zero below.
882  }
883 
884  // Update tech_want for appropriate techs for units we want to build.
885  simple_ai_unit_type_iterate(punittype)
886  {
887  if (tech_desire[utype_index(punittype)] > 0) {
888  /* TODO: Document or fix the algorithm below. I have no idea why
889  * it is written this way, and the results seem strange to me. - Per */
890  int desire =
891  tech_desire[utype_index(punittype)] * best_unit_cost / best;
892 
893  plr_data->tech_want[advance_index(punittype->require_advance)] +=
894  desire;
895  TECH_LOG(ait, LOG_DEBUG, pplayer, punittype->require_advance,
896  "+ %d for %s to defend %s", desire,
897  utype_rule_name(punittype), city_name_get(pcity));
898  }
899  }
901 
902  if (!best_unit_type) {
903  return false;
904  }
905 
906  choice->value.utype = best_unit_type;
907  choice->want = danger;
908  choice->type = CT_DEFENDER;
909  return true;
910 }
911 
929 static void
930 process_attacker_want(struct ai_type *ait, struct city *pcity, int value,
931  const struct unit_type *victim_unit_type,
932  struct player *victim_player, int veteran,
933  struct tile *ptile, struct adv_choice *best_choice,
934  struct pf_map *ferry_map, struct unit *boat,
935  const struct unit_type *boattype)
936 {
937  struct player *pplayer = city_owner(pcity);
938  const struct research *presearch = research_get(pplayer);
939  // The enemy city. acity == nullptr means stray enemy unit
940  struct city *acity = tile_city(ptile);
941  struct pf_parameter parameter;
942  struct pf_map *pfm;
943  struct pf_position pos;
944  const struct unit_type *orig_utype = best_choice->value.utype;
945  int victim_count = 1;
946  int needferry = 0;
947  bool unhap = dai_assess_military_unhappiness(pcity);
948  struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
949 
950  // Has to be initialized to make gcc happy
951  struct ai_city *acity_data = nullptr;
952 
953  if (acity != nullptr) {
954  acity_data = def_ai_city_data(acity, ait);
955  }
956 
957  if (utype_class(orig_utype)->adv.sea_move == MOVE_NONE && !boat
958  && boattype) {
959  // cost of ferry
960  needferry = utype_build_shield_cost(pcity, boattype);
961  }
962 
963  if (!is_stack_vulnerable(ptile)) {
964  /* If it is a city, a fortress or an air base,
965  * we may have to whack it many times */
966  victim_count += unit_list_size(ptile->units);
967  }
968 
969  simple_ai_unit_type_iterate(punittype)
970  {
971  Tech_type_id tech_req = advance_number(punittype->require_advance);
972  int tech_dist = research_goal_unknown_techs(presearch, tech_req);
973 
974  if (dai_can_unit_type_follow_unit_type(punittype, orig_utype, ait)
975  && is_native_near_tile(&(wld.map), utype_class(punittype), ptile)
976  && (U_NOT_OBSOLETED == punittype->obsoleted_by
977  || !can_city_build_unit_direct(pcity, punittype->obsoleted_by))
978  && punittype->attack_strength > 0 /* or we'll get SIGFPE */) {
979  // Values to be computed
980  int desire;
981  adv_want want;
982  int move_time;
983  int vuln;
985  nullptr, pplayer, nullptr, pcity, nullptr, city_tile(pcity),
986  nullptr, punittype, nullptr, nullptr, nullptr, EFT_VETERAN_BUILD);
987  // Cost (shield equivalent) of gaining these techs.
988  /* FIXME? Katvrr advises that this should be weighted more heavily in
989  * big danger. */
990  int notzero = city_list_size(pplayer->cities);
991  fc_assert_ret_msg(notzero, "div by zero");
992  int tech_cost =
993  research_goal_bulbs_required(presearch, tech_req) / 4 / notzero;
994  int bcost_balanced = build_cost_balanced(punittype);
995  // See description of kill_desire() for info about this variables.
996  int bcost = utype_build_shield_cost(pcity, punittype);
998  SINGLE_MOVE, punittype->hp);
999 
1000  // Take into account reinforcements strength
1001  if (acity) {
1002  attack += acity_data->attack;
1003  }
1004 
1005  if (attack == 0) {
1006  /* Yes, it can happen that a military unit has attack = 1,
1007  * for example militia with HP = 1 (in civ1 ruleset). */
1008  continue;
1009  }
1010 
1011  attack *= attack;
1012 
1013  pft_fill_utype_parameter(&parameter, punittype, city_tile(pcity),
1014  pplayer);
1015  parameter.omniscience = !has_handicap(pplayer, H_MAP);
1016  pfm = pf_map_new(&parameter);
1017 
1018  // Set the move_time appropriatelly.
1019  move_time = -1;
1020  if (nullptr != ferry_map) {
1021  struct tile *dest_tile;
1022 
1023  if (find_beachhead(pplayer, ferry_map, ptile, punittype, &dest_tile,
1024  nullptr)
1025  && pf_map_position(ferry_map, dest_tile, &pos)) {
1026  move_time = pos.turn;
1027  dest_tile = pf_map_parameter(ferry_map)->start_tile;
1028  pf_map_tiles_iterate(pfm, atile, false)
1029  {
1030  if (atile == dest_tile) {
1031  pf_map_iter_position(pfm, &pos);
1032  move_time += pos.turn;
1033  break;
1034  } else if (atile == ptile) {
1035  // Reaching directly seems better.
1036  pf_map_iter_position(pfm, &pos);
1037  move_time = pos.turn;
1038  break;
1039  }
1040  }
1042  }
1043  }
1044 
1045  if (-1 == move_time) {
1046  if (pf_map_position(pfm, ptile, &pos)) {
1047  move_time = pos.turn;
1048  } else {
1049  pf_map_destroy(pfm);
1050  continue;
1051  }
1052  }
1053  pf_map_destroy(pfm);
1054 
1055  // Estimate strength of the enemy.
1056 
1057  if (victim_unit_type) {
1058  vuln = unittype_def_rating_squared(punittype, victim_unit_type,
1059  victim_player, ptile, false,
1060  veteran);
1061  } else {
1062  vuln = 0;
1063  }
1064 
1065  /* Not bothering to s/!vuln/!pdef/ here for the time being. -- Syela
1066  * (this is noted elsewhere as terrible bug making warships yoyoing)
1067  * as the warships will go to enemy cities hoping that the enemy builds
1068  * something for them to kill*/
1069  if (vuln == 0
1070  && (utype_class(punittype)->adv.land_move == MOVE_NONE
1071  || 0 < utype_fuel(punittype))) {
1072  desire = 0;
1073 
1074  } else {
1075  if (acity && utype_can_take_over(punittype)
1076  && acity_data->invasion.attack > 0
1077  && acity_data->invasion.occupy == 0) {
1078  int owner_size = city_list_size(city_owner(acity)->cities);
1079  float finishing_factor = 1;
1080 
1081  if (owner_size <= FINISH_HIM_CITY_COUNT) {
1082  finishing_factor =
1083  (2 - static_cast<float>(owner_size) / FINISH_HIM_CITY_COUNT);
1084  }
1085  desire = acity_data->worth * 10 * finishing_factor;
1086  } else {
1087  desire = 0;
1088  }
1089 
1090  if (!acity) {
1091  desire = kill_desire(value, attack, bcost, vuln, victim_count);
1092  } else {
1093  int kd;
1094  int city_attack = acity_data->attack * acity_data->attack;
1095 
1096  // See aiunit.c:find_something_to_kill() for comments.
1097  kd = kill_desire(value, attack, (bcost + acity_data->bcost), vuln,
1098  victim_count);
1099 
1100  if (value * city_attack > acity_data->bcost * vuln) {
1101  kd -= kill_desire(value, city_attack, acity_data->bcost, vuln,
1102  victim_count);
1103  }
1104 
1105  desire = MAX(desire, kd);
1106  }
1107  }
1108 
1109  desire -= tech_cost * SHIELD_WEIGHTING;
1110  /* We can be possibly making some people of our homecity unhappy - then
1111  * we don't want to travel too far away to our victims. */
1112  // TODO: Unify the 'unhap' dependency to some common function.
1113  desire -= move_time
1114  * (unhap ? SHIELD_WEIGHTING + 2 * TRADE_WEIGHTING
1115  : SHIELD_WEIGHTING);
1116 
1117  want = military_amortize(pplayer, pcity, desire, MAX(1, move_time),
1118  bcost_balanced + needferry);
1119 
1120  if (want > 0) {
1121  if (tech_dist > 0) {
1122  // This is a future unit, tell the scientist how much we need it
1123  plr_data->tech_want[advance_index(punittype->require_advance)] +=
1124  want;
1125  TECH_LOG(ait, LOG_DEBUG, pplayer, punittype->require_advance,
1126  "+ " ADV_WANT_PRINTF " for %s vs %s(%d,%d)", want,
1127  utype_rule_name(punittype),
1128  (acity ? city_name_get(acity)
1129  : utype_rule_name(victim_unit_type)),
1130  TILE_XY(ptile));
1131  } else if (want > best_choice->want) {
1132  const struct impr_type *impr_req;
1133 
1134  if (can_city_build_unit_now(pcity, punittype)) {
1135  // This is a real unit and we really want it
1136 
1137  CITY_LOG(LOG_DEBUG, pcity,
1138  "overriding %s(" ADV_WANT_PRINTF
1139  ") with %s(" ADV_WANT_PRINTF ")"
1140  " [attack=%d,value=%d,move_time=%d,vuln=%d,bcost=%d]",
1141  utype_rule_name(best_choice->value.utype),
1142  best_choice->want, utype_rule_name(punittype), want,
1143  attack, value, move_time, vuln, bcost);
1144 
1145  best_choice->value.utype = punittype;
1146  best_choice->want = want;
1147  best_choice->type = CT_ATTACKER;
1148  } else if (!((impr_req =
1149  utype_needs_improvement(punittype, pcity)))) {
1150  CITY_LOG(LOG_DEBUG, pcity, "cannot build unit %s",
1151  utype_rule_name(punittype));
1152  } else if (can_city_build_improvement_now(pcity, impr_req)) {
1153  /* Building this unit requires a specific type of improvement.
1154  * So we build this improvement instead. This may not be the
1155  * best behavior. */
1156  CITY_LOG(LOG_DEBUG, pcity, "building %s to build unit %s",
1157  improvement_rule_name(impr_req),
1158  utype_rule_name(punittype));
1159  best_choice->value.building = impr_req;
1160  best_choice->want = want;
1161  best_choice->type = CT_BUILDING;
1162  } else {
1163  // This should never happen?
1164  CITY_LOG(LOG_DEBUG, pcity, "cannot build %s or unit %s",
1165  improvement_rule_name(impr_req),
1166  utype_rule_name(punittype));
1167  }
1168  }
1169  }
1170  }
1171  }
1173 }
1174 
1186 static struct adv_choice *kill_something_with(struct ai_type *ait,
1187  struct player *pplayer,
1188  struct city *pcity,
1189  struct unit *myunit,
1190  struct adv_choice *choice)
1191 {
1192  // Our attack rating (with reinforcements)
1193  int attack;
1194  // Benefit from fighting the target
1195  int benefit;
1196  /* Defender of the target city/tile */
1197  struct unit *pdef;
1198  const struct unit_type *def_type;
1199  struct player *def_owner;
1200  int def_vet; // Is the defender veteran?
1201  // Target coordinates
1202  struct tile *ptile;
1203  // Our transport
1204  struct unit *ferryboat;
1205  // Our target
1206  struct city *acity;
1207  // Type of the boat (real or a future one)
1208  const struct unit_type *boattype;
1209  struct pf_map *ferry_map = nullptr;
1210  int move_time;
1211  struct adv_choice *best_choice;
1212  struct ai_city *city_data = def_ai_city_data(pcity, ait);
1213  struct ai_city *acity_data;
1214  auto path = PFPath();
1215 
1217  && !utype_fuel(unit_type_get(myunit)),
1218  choice);
1219 
1220  best_choice = adv_new_choice();
1221  best_choice->value.utype = unit_type_get(myunit);
1222  best_choice->type = CT_ATTACKER;
1223  adv_choice_set_use(best_choice, "attacker");
1224 
1225  if (city_data->danger != 0 && assess_defense(ait, pcity) == 0) {
1226  // Defence comes first!
1227  goto cleanup;
1228  }
1229 
1230  best_choice->want =
1231  find_something_to_kill(ait, pplayer, myunit, &ptile, &path, &ferry_map,
1232  &ferryboat, &boattype, &move_time);
1233  if (nullptr == ptile || ptile == unit_tile(myunit)
1234  || !can_unit_attack_tile(myunit, ptile)) {
1235  goto cleanup;
1236  }
1237 
1238  acity = tile_city(ptile);
1239 
1240  if (myunit->id != 0) {
1241  qCritical("%s(): non-virtual unit!", __FUNCTION__);
1242  goto cleanup;
1243  }
1244 
1245  attack = adv_unit_att_rating(myunit);
1246  if (acity) {
1247  acity_data = def_ai_city_data(acity, ait);
1248  attack += acity_data->attack;
1249  }
1250  attack *= attack;
1251 
1252  if (nullptr != acity) {
1253  // Rating of enemy defender
1254  int vulnerability;
1255 
1256  if (!POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, city_owner(acity))) {
1257  // Not a valid target
1258  goto cleanup;
1259  }
1260 
1261  def_type = dai_choose_defender_versus(acity, myunit);
1262  def_owner = city_owner(acity);
1263  if (1 < move_time && def_type) {
1264  def_vet = city_production_unit_veteran_level(acity, def_type);
1265  vulnerability = unittype_def_rating_squared(
1266  unit_type_get(myunit), def_type, city_owner(acity), ptile, false,
1267  def_vet);
1268  benefit = utype_build_shield_cost_base(def_type);
1269  } else {
1270  vulnerability = 0;
1271  benefit = 0;
1272  def_vet = 0;
1273  }
1274 
1275  pdef = get_defender(myunit, ptile);
1276  if (pdef) {
1278  unit_type_get(myunit), unit_type_get(pdef), city_owner(acity),
1279  ptile, false, pdef->veteran);
1280  if (vulnerability < m) {
1281  vulnerability = m;
1282  benefit = unit_build_shield_cost_base(pdef);
1283  def_vet = pdef->veteran;
1284  def_type = unit_type_get(pdef);
1285  def_owner = unit_owner(pdef);
1286  }
1287  }
1288  if (unit_can_take_over(myunit) || acity_data->invasion.occupy > 0) {
1289  // Bonus for getting the city
1290  int owner_size = city_list_size(city_owner(acity)->cities);
1291  float finishing_factor = 1;
1292 
1293  if (owner_size <= FINISH_HIM_CITY_COUNT) {
1294  finishing_factor =
1295  (2 - static_cast<float>(owner_size) / FINISH_HIM_CITY_COUNT);
1296  }
1297  benefit += acity_data->worth * finishing_factor / 3;
1298  }
1299 
1300  // end dealing with cities
1301  } else {
1302  if (nullptr != ferry_map) {
1303  pf_map_destroy(ferry_map);
1304  ferry_map = nullptr;
1305  }
1306 
1307  pdef = get_defender(myunit, ptile);
1308  if (!pdef) {
1309  // Nobody to attack!
1310  goto cleanup;
1311  }
1312 
1313  benefit = unit_build_shield_cost_base(pdef);
1314 
1315  def_type = unit_type_get(pdef);
1316  def_vet = pdef->veteran;
1317  def_owner = unit_owner(pdef);
1318  // end dealing with units
1319  }
1320 
1321  if (nullptr == ferry_map) {
1322  process_attacker_want(ait, pcity, benefit, def_type, def_owner, def_vet,
1323  ptile, best_choice, nullptr, nullptr, nullptr);
1324  } else {
1325  // Attract a boat to our city or retain the one that's already here
1327  choice);
1328  best_choice->need_boat = true;
1329  process_attacker_want(ait, pcity, benefit, def_type, def_owner, def_vet,
1330  ptile, best_choice, ferry_map, ferryboat,
1331  boattype);
1332  }
1333 
1334  if (best_choice->want > choice->want) {
1335  // We want attacker more than what we have selected before
1337  choice = best_choice;
1338  CITY_LOG(LOG_DEBUG, pcity,
1339  "kill_something_with()"
1340  " %s has chosen attacker, %s, want=" ADV_WANT_PRINTF,
1341  city_name_get(pcity), utype_rule_name(best_choice->value.utype),
1342  best_choice->want);
1343 
1344  if (nullptr != ferry_map && !ferryboat) { // need a new ferry
1345  /* We might need a new boat even if there are boats free,
1346  * if they are blockaded or in inland seas*/
1348  choice);
1349  if (dai_choose_role_unit(ait, pplayer, pcity, choice, CT_ATTACKER,
1350  L_FERRYBOAT, choice->want, true)
1351  && dai_is_ferry_type(choice->value.utype, ait)) {
1352 #ifdef FREECIV_DEBUG
1353  struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
1354 
1355  log_debug("kill_something_with() %s has chosen attacker ferry, "
1356  "%s, want=" ADV_WANT_PRINTF ", %d of %d free",
1357  city_name_get(pcity), utype_rule_name(choice->value.utype),
1358  choice->want, ai->stats.available_boats, ai->stats.boats);
1359 #endif // FREECIV_DEBUG
1360 
1361  adv_choice_set_use(choice, "attacker ferry");
1362  } // else can not build ferries yet
1363  }
1364  }
1365 
1366 cleanup:
1367  if (best_choice != choice) {
1368  /* It was not taken to use.
1369  * This hackery needed since 'goto cleanup:' might skip
1370  * sensible points to do adv_free_choice(). */
1371  adv_free_choice(best_choice);
1372  }
1373  if (nullptr != ferry_map) {
1374  pf_map_destroy(ferry_map);
1375  }
1376 
1377  return choice;
1378 }
1379 
1385 static void dai_unit_consider_bodyguard(struct ai_type *ait,
1386  struct city *pcity,
1387  struct unit_type *punittype,
1388  struct adv_choice *choice)
1389 {
1390  struct unit *virtualunit;
1391  struct player *pplayer = city_owner(pcity);
1392  struct unit *aunit = nullptr;
1393  struct city *acity = nullptr;
1394 
1395  virtualunit = unit_virtual_create(
1396  pplayer, pcity, punittype,
1397  city_production_unit_veteran_level(pcity, punittype));
1398 
1399  if (choice->want < 100) {
1400  const int want =
1401  look_for_charge(ait, pplayer, virtualunit, &aunit, &acity);
1402 
1403  if (want > choice->want) {
1404  choice->want = want;
1405  choice->value.utype = punittype;
1406  choice->type = CT_DEFENDER;
1407  adv_choice_set_use(choice, "bodyguard");
1408  }
1409  }
1410  unit_virtual_destroy(virtualunit);
1411 }
1412 
1421 static void adjust_ai_unit_choice(struct city *pcity,
1422  struct adv_choice *choice)
1423 {
1424  Impr_type_id id;
1425 
1426  // Sanity
1427  if (!is_unit_choice_type(choice->type)
1428  || utype_has_flag(choice->value.utype, UTYF_CIVILIAN)
1429  || city_production_unit_veteran_level(pcity, choice->value.utype)) {
1430  return;
1431  }
1432 
1433  // N.B.: have to check that we haven't already built the building --mck
1434  if ((id = dai_find_source_building(pcity, EFT_VETERAN_BUILD,
1435  choice->value.utype))
1436  != B_LAST
1437  && !city_has_building(pcity, improvement_by_number(id))) {
1438  choice->value.building = improvement_by_number(id);
1439  choice->want =
1440  choice->want
1441  * (0.5
1442  + double((ai_trait_get_value(TRAIT_BUILDER, city_owner(pcity)))
1443  / static_cast<double>(TRAIT_DEFAULT_VALUE) / 2));
1444  choice->type = CT_BUILDING;
1445  adv_choice_set_use(choice, "veterancy building");
1446  }
1447 }
1448 
1455  struct ai_type *ait, struct player *pplayer, struct city *pcity,
1456  const struct civ_map *mamap, player_unit_list_getter ul_cb)
1457 {
1458  struct adv_data *ai = adv_data_get(pplayer, nullptr);
1459  struct unit_type *punittype;
1460  int our_def, urgency;
1461  struct tile *ptile = pcity->tile;
1462  struct unit *virtualunit;
1463  struct ai_city *city_data = def_ai_city_data(pcity, ait);
1464  adv_want martial_value = 0;
1465  bool martial_need = false;
1466  struct adv_choice *choice = adv_new_choice();
1467  bool allow_gold_upkeep;
1468 
1469  urgency = assess_danger(ait, pcity, mamap, ul_cb);
1470  /* Changing to quadratic to stop AI from building piles
1471  * of small units -- Syela */
1472  // It has to be AFTER assess_danger thanks to wallvalue.
1473  our_def = assess_defense_quadratic(ait, pcity);
1474 
1475  dai_choose_diplomat_defensive(ait, pplayer, pcity, choice, our_def);
1476 
1479  > 0) {
1480  martial_need = true;
1481  }
1482 
1483  if (!martial_need) {
1485  {
1486  if (pcity->specialists[sp] > 0
1487  && get_specialist_output(pcity, sp, O_LUXURY) > 0) {
1488  martial_need = true;
1489  break;
1490  }
1491  }
1493  }
1494 
1495  if (martial_need
1496  && unit_list_size(pcity->tile->units)
1497  < get_city_bonus(pcity, EFT_MARTIAL_LAW_MAX)) {
1498  martial_value = dai_content_effect_value(
1499  pplayer, pcity, get_city_bonus(pcity, EFT_MARTIAL_LAW_EACH), 1,
1500  FEELING_FINAL);
1501  }
1502 
1503  // Otherwise no need to defend yet
1504  if (city_data->danger != 0 || martial_value > 0) {
1505  struct impr_type *pimprove;
1506  int num_defenders = unit_list_size(ptile->units);
1507  int wall_id, danger;
1508  bool build_walls = true;
1509 
1510  /* First determine the danger. It is measured in percents of our
1511  * defensive strength, capped at 200 + urgency */
1512  if (city_data->danger >= our_def) {
1513  if (urgency == 0) {
1514  // don't waste money
1515  danger = 100;
1516  } else if (our_def == 0) {
1517  danger = 200 + urgency;
1518  } else {
1519  danger = MIN(200, 100 * city_data->danger / our_def) + urgency;
1520  }
1521  } else {
1522  danger = 100 * city_data->danger / our_def;
1523  }
1524  if (pcity->surplus[O_SHIELD] <= 0 && our_def != 0) {
1525  // Won't be able to support anything
1526  danger = 0;
1527  }
1528 
1529  CITY_LOG(LOG_DEBUG, pcity,
1530  "m_a_c_d urgency=%d danger=%d num_def=%d "
1531  "our_def=%d",
1532  urgency, danger, num_defenders, our_def);
1533 
1534  if (our_def == 0) {
1535  /* Build defensive unit first! Walls will help nothing if there's
1536  * nobody behind them. */
1537  if (dai_process_defender_want(ait, pplayer, pcity, danger, choice)) {
1538  choice->want = 100 + danger;
1539  adv_choice_set_use(choice, "first defender");
1540  build_walls = false;
1541 
1542  CITY_LOG(LOG_DEBUG, pcity,
1543  "m_a_c_d wants first defender with " ADV_WANT_PRINTF,
1544  choice->want);
1545  }
1546  }
1547  if (build_walls) {
1548  /* FIXME: 1. Does not consider what kind of future danger is likely, so
1549  * may build SAM batteries when enemy has only land units. */
1550  /* We will build walls if we can and want and (have "enough" defenders
1551  * or can just buy the walls straight away) */
1552 
1553  /* HACK: This needs changing if multiple improvements provide
1554  * this effect. */
1555  wall_id = dai_find_source_building(pcity, EFT_DEFEND_BONUS, nullptr);
1556  pimprove = improvement_by_number(wall_id);
1557 
1558  if (wall_id != B_LAST && pcity->server.adv->building_want[wall_id] != 0
1559  && our_def != 0 && can_city_build_improvement_now(pcity, pimprove)
1560  && (danger < 101 || num_defenders > 1
1561  || (city_data->grave_danger == 0
1562  && pplayer->economic.gold > impr_buy_gold_cost(
1563  pcity, pimprove, pcity->shield_stock)))
1564  && ai_fuzzy(pplayer, true)) {
1565  if (pcity->server.adv->building_want[wall_id] > 0) {
1566  // NB: great wall is under domestic
1567  choice->value.building = pimprove;
1568  // building_want is hacked by assess_danger
1569  choice->want = pcity->server.adv->building_want[wall_id];
1570  choice->want =
1571  choice->want
1572  * (0.5
1573  + double((ai_trait_get_value(TRAIT_BUILDER, pplayer))
1574  / static_cast<double>(TRAIT_DEFAULT_VALUE) / 2));
1575  if (urgency == 0 && choice->want > 100) {
1576  choice->want = 100;
1577  }
1578  choice->type = CT_BUILDING;
1579  adv_choice_set_use(choice, "defense building");
1580  CITY_LOG(LOG_DEBUG, pcity,
1581  "m_a_c_d wants defense building with " ADV_WANT_PRINTF,
1582  choice->want);
1583  } else {
1584  build_walls = false;
1585  }
1586  } else {
1587  build_walls = false;
1588  }
1589  if ((danger > 0 && num_defenders <= urgency) || martial_value > 0) {
1590  struct adv_choice uchoice;
1591 
1592  adv_init_choice(&uchoice);
1593 
1594  // Consider building defensive units
1595  if (dai_process_defender_want(ait, pplayer, pcity, danger,
1596  &uchoice)) {
1597  // Potential defender found
1598  if (urgency == 0 && uchoice.value.utype->defense_strength == 1) {
1599  // FIXME: check other reqs (unit class?)
1600  if (get_city_bonus(pcity, EFT_HP_REGEN) > 0) {
1601  // unlikely
1602  uchoice.want = MIN(49, danger);
1603  } else {
1604  uchoice.want = MIN(25, danger);
1605  }
1606  } else {
1607  uchoice.want = danger;
1608  }
1609 
1610  uchoice.want += martial_value;
1611  adv_choice_set_use(&uchoice, "defender");
1612 
1613  if (!build_walls || uchoice.want > choice->want) {
1614  adv_choice_copy(choice, &uchoice);
1615  }
1616  adv_deinit_choice(&uchoice);
1617 
1618  CITY_LOG(LOG_DEBUG, pcity,
1619  "m_a_c_d wants %s with desire " ADV_WANT_PRINTF,
1620  utype_rule_name(choice->value.utype), choice->want);
1621  } else {
1622  CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d cannot select defender");
1623  }
1624  } else {
1625  CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d does not want defenders");
1626  }
1627  }
1628  } // ok, don't need to defend
1629 
1630  if (pcity->surplus[O_SHIELD] <= 0
1631  || pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
1633  || pcity->id == ai->wonder_city) {
1634  /* Things we consider below are not life-saving so we don't want to
1635  * build them if our populace doesn't feel like it */
1636  return choice;
1637  }
1638 
1639  if (pplayer->economic.tax <= 50
1640  || city_total_unit_gold_upkeep(pcity) <= 0) {
1641  /* Always allow one unit with real gold upkeep (after
1642  * EFT_UNIT_UPKEEP_FREE_PER_CITY) Allow more if economics is so strong
1643  * that we have not increased taxes. */
1644  allow_gold_upkeep = true;
1645  } else {
1646  allow_gold_upkeep = false;
1647  }
1648 
1649  // Consider making a land bodyguard
1650  punittype = dai_choose_bodyguard(ait, pcity, TC_LAND, L_DEFEND_GOOD,
1651  allow_gold_upkeep);
1652  if (punittype) {
1653  dai_unit_consider_bodyguard(ait, pcity, punittype, choice);
1654  }
1655 
1656  /* If we are in severe danger, don't consider attackers. This is probably
1657  too general. In many cases we will want to buy attackers to
1658  counterattack.
1659  -- Per */
1660  if (choice->want - martial_value > 100 && city_data->grave_danger > 0) {
1661  CITY_LOG(LOGLEVEL_BUILD, pcity,
1662  "severe danger (want " ADV_WANT_PRINTF "), force defender",
1663  choice->want);
1664  return choice;
1665  }
1666 
1667  // Consider making an offensive diplomat
1668  dai_choose_diplomat_offensive(ait, pplayer, pcity, choice);
1669 
1670  // Consider making a sea bodyguard
1671  punittype = dai_choose_bodyguard(ait, pcity, TC_OCEAN, L_DEFEND_GOOD,
1672  allow_gold_upkeep);
1673  if (punittype) {
1674  dai_unit_consider_bodyguard(ait, pcity, punittype, choice);
1675  }
1676 
1677  // Consider making an airplane
1678  (void) dai_choose_attacker_air(ait, pplayer, pcity, choice,
1679  allow_gold_upkeep);
1680 
1681  // Consider making a paratrooper
1682  dai_choose_paratrooper(ait, pplayer, pcity, choice, allow_gold_upkeep);
1683 
1684  /* Check if we want a sailing attacker. Have to put sailing first
1685  before we mung the seamap */
1686  punittype = dai_choose_attacker(ait, pcity, TC_OCEAN, allow_gold_upkeep);
1687  if (punittype) {
1688  virtualunit = unit_virtual_create(
1689  pplayer, pcity, punittype,
1690  city_production_unit_veteran_level(pcity, punittype));
1691  choice = kill_something_with(ait, pplayer, pcity, virtualunit, choice);
1692  unit_virtual_destroy(virtualunit);
1693  }
1694 
1695  /* Consider a land attacker or a ferried land attacker
1696  * (in which case, we might want a ferry before an attacker)
1697  */
1698  punittype = dai_choose_attacker(ait, pcity, TC_LAND, allow_gold_upkeep);
1699  if (punittype) {
1700  virtualunit = unit_virtual_create(pplayer, pcity, punittype, 1);
1701  choice = kill_something_with(ait, pplayer, pcity, virtualunit, choice);
1702  unit_virtual_destroy(virtualunit);
1703  }
1704 
1705  // Consider a hunter
1706  dai_hunter_choice(ait, pplayer, pcity, choice, allow_gold_upkeep);
1707 
1708  // Consider veteran level enhancing buildings before non-urgent units
1709  adjust_ai_unit_choice(pcity, choice);
1710 
1711  if (choice->want <= 0) {
1712  CITY_LOG(LOGLEVEL_BUILD, pcity, "military advisor has no advice");
1713  } else {
1714  CITY_LOG(LOGLEVEL_BUILD, pcity,
1715  "military advisor choice: %s (want " ADV_WANT_PRINTF ")",
1716  dai_choice_rule_name(choice), choice->want);
1717  }
1718 
1719  return choice;
1720 }
#define TRADE_WEIGHTING
Definition: advbuilding.h:18
#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
struct adv_choice * adv_new_choice()
Dynamically allocate a new choice.
Definition: advchoice.cpp:55
void adv_free_choice(struct adv_choice *choice)
Free dynamically allocated choice.
Definition: advchoice.cpp:67
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
static void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
Definition: advchoice.h:62
@ CT_DEFENDER
Definition: advchoice.h:27
@ CT_ATTACKER
Definition: advchoice.h:26
@ 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
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
There are some signs that a player might be dangerous: We are at war with him, he has done lots of ig...
Definition: advdata.cpp:1062
int adv_unittype_att_rating(const struct unit_type *punittype, int veteran, int moves_left, int hp)
Attack rating of this kind of unit.
Definition: advgoto.cpp:277
int adv_unit_att_rating(const struct unit *punit)
Attack rating of this particular unit assuming that it has a complete move left.
Definition: advgoto.cpp:288
#define POWER_DIVIDER
Definition: advtools.h:25
bool dai_choose_attacker_air(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, bool allow_gold_upkeep)
Chooses the best available and usable air unit and records it in choice, if it's better than previous...
Definition: aiair.cpp:429
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_choose_diplomat_defensive(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, int def)
Calculates our need for diplomats as defensive units.
Definition: aidiplomat.cpp:108
void dai_choose_diplomat_offensive(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice)
Calculates our need for diplomats as offensive units.
Definition: aidiplomat.cpp:155
bool dai_is_ferry_type(const struct unit_type *pferry, struct ai_type *ait)
Print the list of boats of pplayer.
Definition: aiferry.cpp:146
void dai_hunter_choice(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, bool allow_gold_upkeep)
Check if we want to build a hunter.
Definition: aihunt.cpp:262
#define TECH_LOG(ait, _, pplayer, padvance, msg,...)
Definition: ailog.h:32
void dai_choose_paratrooper(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, bool allow_gold_upkeep)
Chooses to build a paratroopers if necessary.
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
struct unit_type * dai_wants_defender_against(struct ai_type *ait, struct player *pplayer, struct city *pcity, const struct unit_type *att, int want)
Returns the best defense multiplier unit we can build, or nullptr if none.
Definition: aitech.cpp:397
const char * dai_choice_rule_name(const struct adv_choice *choice)
Return the (untranslated) rule name of the adv_choice.
Definition: aitools.cpp:105
bool dai_assess_military_unhappiness(struct city *pcity)
"The following evaluates the unhappiness caused by military units in the field (or aggressive) at a c...
Definition: aitools.cpp:1203
bool dai_choose_role_unit(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, enum choice_type type, int role, int want, bool need_boat)
Calls dai_wants_role_unit to choose the best unit with the given role and set tech wants.
Definition: aitools.cpp:1131
int military_amortize(struct player *pplayer, struct city *pcity, int value, int delay, int build_cost)
Amortize a want modified by the shields (build_cost) we risk losing.
Definition: aitools.cpp:132
int ai_trait_get_value(enum trait tr, struct player *pplayer)
Get current value of player trait.
Definition: aitraits.cpp:62
int look_for_charge(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity)
See if we can find something to defend.
Definition: aiunit.cpp:707
int find_something_to_kill(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile **pdest_tile, PFPath *ppath, struct pf_map **pferrymap, struct unit **pferryboat, const struct unit_type **pboattype, int *pmove_time)
Find something to kill! This function is called for units to find targets to destroy and for cities t...
Definition: aiunit.cpp:1118
int unittype_def_rating_squared(const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
Defence rating of def_type unit against att_type unit, squared.
Definition: aiunit.cpp:296
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Returns an improvement that will make it possible to build units of the specified type the specified ...
Definition: aiunit.cpp:2802
int build_cost_balanced(const struct unit_type *punittype)
In the words of Syela: "Using funky fprime variable instead of f in the denom, so that def=1 units ar...
Definition: aiunit.cpp:240
bool dai_can_unit_type_follow_unit_type(const struct unit_type *follower, const struct unit_type *followee, struct ai_type *ait)
See if the follower can follow the followee.
Definition: aiunit.cpp:834
bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map, struct tile *dest_tile, const struct unit_type *cargo_type, struct tile **ferry_dest, struct tile **beachhead_tile)
Returns TRUE if a beachhead as been found to reach 'dest_tile'.
Definition: aiunit.cpp:1037
int kill_desire(int benefit, int attack, int loss, int vuln, int victim_count)
Compute how much we want to kill certain victim we've chosen, counted in SHIELDs.
Definition: aiunit.cpp:334
#define POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer)
Definition: aiunit.h:77
#define simple_ai_unit_type_iterate_end
Definition: aiunit.h:148
#define simple_ai_unit_type_iterate(_ut)
Definition: aiunit.h:142
bool can_city_build_unit_later(const struct city *pcity, const struct unit_type *punittype)
Returns whether player can eventually build given unit in the city; returns FALSE if unit can never p...
Definition: city.cpp:912
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
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
How many veteran levels will created unit of this type get?
Definition: city.cpp:768
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
bool city_got_defense_effect(const struct city *pcity, const struct unit_type *attacker)
This can be City Walls, Coastal defense...
Definition: city.cpp:1512
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Return whether given city can build given unit, ignoring whether unit is obsolete.
Definition: city.cpp:860
int city_total_unit_gold_upkeep(const struct city *pcity)
Get the total amount of gold needed to pay upkeep costs for all supported units of the city.
Definition: city.cpp:1168
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
@ CITIZEN_ANGRY
Definition: city.h:243
@ CITIZEN_UNHAPPY
Definition: city.h:242
#define city_list_iterate_end
Definition: city.h:484
@ FEELING_EFFECT
Definition: city.h:253
@ FEELING_FINAL
Definition: city.h:256
@ FEELING_NATIONALITY
Definition: city.h:254
struct unit * get_defender(const struct unit *attacker, const struct tile *ptile)
Finds the best defender on the tile, given an attacker.
Definition: combat.cpp:721
bool is_stack_vulnerable(const struct tile *ptile)
Is it a city/fortress/air base or will the whole stack die in an attack.
Definition: combat.cpp:839
int get_fortified_defense_power(const struct unit *attacker, struct unit *defender)
Return total defense power of the unit if it fortifies, if possible, where it is.
Definition: combat.cpp:667
int get_total_defense_power(const struct unit *attacker, const struct unit *defender)
return the modified defense power of a unit.
Definition: combat.cpp:654
int get_total_attack_power(const struct unit *attacker, const struct unit *defender)
Return the modified attack power of a unit.
Definition: combat.cpp:526
bool can_unit_attack_tile(const struct unit *punit, const struct tile *dest_tile)
Is unit (1) diplomatically allowed to attack and (2) physically able to do so?
Definition: combat.cpp:251
void get_modified_firepower(const struct unit *attacker, const struct unit *defender, int *att_fp, int *def_fp)
A unit's effective firepower depend on the situation.
Definition: combat.cpp:352
#define POWER_FACTOR
Definition: combat.h:23
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
adv_want dai_content_effect_value(const struct player *pplayer, const struct city *pcity, int amount, int num_cities, int happiness_step)
How desirable particular effect making people content is for a particular city?
Definition: daieffects.cpp:71
static struct adv_choice * kill_something_with(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct unit *myunit, struct adv_choice *choice)
This function.
static void process_attacker_want(struct ai_type *ait, struct city *pcity, int value, const struct unit_type *victim_unit_type, struct player *victim_player, int veteran, struct tile *ptile, struct adv_choice *best_choice, struct pf_map *ferry_map, struct unit *boat, const struct unit_type *boattype)
This function decides, what unit would be best for erasing enemy.
void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer, const struct civ_map *dmap)
Call assess_danger() for all cities owned by pplayer.
static int assess_danger_unit(const struct city *pcity, struct pf_reverse_map *pcity_map, const struct unit *punit, int *move_time)
How dangerous and far a unit is for a city?
int assess_defense(struct ai_type *ait, struct city *pcity)
Estimate defense strength of city.
struct unit_type * dai_choose_defender_versus(struct city *pcity, struct unit *attacker)
Choose the best unit the city can build to defend against attacker v.
Definition: daimilitary.cpp:72
static struct unit_type * dai_choose_bodyguard(struct ai_type *ait, struct city *pcity, enum terrain_class tc, enum unit_role_id role, bool allow_gold_upkeep)
Choose best defender based on movement type.
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.
int dai_unit_attack_desirability(struct ai_type *ait, const struct unit_type *punittype)
How much we would want that unit to attack with?
static void dai_unit_consider_bodyguard(struct ai_type *ait, struct city *pcity, struct unit_type *punittype, struct adv_choice *choice)
This function should assign a value to choice and want and type, where want is a value between 1 and ...
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.
static void adjust_ai_unit_choice(struct city *pcity, struct adv_choice *choice)
Before building a military unit, AI builds a barracks/port/airport NB: It is assumed this function is...
static void dai_reevaluate_building(struct city *pcity, adv_want *value, int urgency, int danger, int defense)
Set (overwrite) our want for a building.
int dai_unit_defence_desirability(struct ai_type *ait, const struct unit_type *punittype)
How much we would want that unit to defend a city? (Do not use this function to find bodyguards for s...
int assess_defense_quadratic(struct ai_type *ait, struct city *pcity)
Need positive feedback in m_a_c_b and bodyguard routines.
static int base_assess_defense_unit(struct city *pcity, struct unit *punit, bool igwall, bool quadratic, int wall_value)
Helper for assess_defense_quadratic and assess_defense_unit.
static struct unit_type * dai_choose_attacker(struct ai_type *ait, struct city *pcity, enum terrain_class tc, bool allow_gold_upkeep)
Choose best attacker based on movement type.
static int assess_danger(struct ai_type *ait, struct city *pcity, const struct civ_map *dmap, player_unit_list_getter ul_cb)
Create cached information about danger, urgency and grave danger to our cities.
static int assess_defense_backend(struct ai_type *ait, struct city *pcity, bool igwall)
Most of the time we don't need/want positive feedback.
int assess_defense_unit(struct ai_type *ait, struct city *pcity, struct unit *punit, bool igwall)
One unit only, mostly for findjob; handling boats correctly.
static int assess_defense_igwall(struct ai_type *ait, struct city *pcity)
Estimate defense strength of city without considering how buildings help defense.
struct unit_list *() player_unit_list_getter(struct player *pplayer)
Definition: daimilitary.h:26
#define FINISH_HIM_CITY_COUNT
Definition: daimilitary.h:24
static void attack(QVariant data1, QVariant data2)
Action "Attack" for choice dialog.
Definition: dialogs.cpp:2464
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
int get_target_bonus_effects(struct effect_list *plist, 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, enum effect_type effect_type, enum vision_layer vision_layer, enum national_intelligence nintel)
Returns the effect bonus of a given type for any target.
Definition: effects.cpp:611
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, enum effect_type effect_type, enum vision_layer vision_layer)
Returns the effect bonus that applies at a tile for a given unittype.
Definition: effects.cpp:841
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
float adv_want
Definition: fc_types.h:1144
int Tech_type_id
Definition: fc_types.h:294
int Impr_type_id
Definition: fc_types.h:293
#define ADV_WANT_PRINTF
Definition: fc_types.h:1145
@ O_SHIELD
Definition: fc_types.h:86
@ O_LUXURY
Definition: fc_types.h:89
@ O_GOLD
Definition: fc_types.h:88
struct world wld
Definition: game.cpp:48
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_MAP
Definition: handicaps.h:27
@ H_DANGER
Definition: handicaps.h:31
const char * improvement_rule_name(const struct impr_type *pimprove)
Return the (untranslated) rule name of the improvement.
struct impr_type * improvement_by_number(const Impr_type_id id)
Returns the improvement type for the given index/ID.
int impr_buy_gold_cost(const struct city *pcity, const struct impr_type *pimprove, int shields_in_stock)
Returns the amount of gold it takes to rush this improvement.
#define B_LAST
Definition: improvement.h:33
constexpr auto LOG_DEBUG
Definition: log.h:27
constexpr auto LOG_VERBOSE
Definition: log.h:26
#define fc_assert_ret_msg(condition, message,...)
Definition: log.h:129
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
#define log_debug(message,...)
Definition: log.h:65
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Return real distance between two tiles.
Definition: map.cpp:599
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
This tile is native to unit.
Definition: movement.cpp:279
bool can_attack_from_non_native(const struct unit_type *utype)
This unit can attack from non-native tiles (Marines can attack from transport, ships from harbour cit...
Definition: movement.cpp:177
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Is there native tile adjacent to given tile.
Definition: movement.cpp:424
bool can_attack_non_native(const struct unit_type *utype)
This unit can attack non-native tiles (eg.
Definition: movement.cpp:164
#define SINGLE_MOVE
Definition: movement.h:17
struct city_list * cities
Definition: packhand.cpp:122
bool pf_reverse_map_unit_position(struct pf_reverse_map *pfrm, const struct unit *punit, struct pf_position *pos)
Fill the position.
const struct pf_parameter * pf_map_parameter(const struct pf_map *pfm)
Return the pf_parameter for given pf_map.
bool pf_map_position(struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
Get info about position at ptile and put it in pos.
void pf_map_iter_position(struct pf_map *pfm, struct pf_position *pos)
Read all info about the current position into pos.
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
Factory function to create a new map according to the parameter.
struct pf_reverse_map * pf_reverse_map_new_for_city(const struct city *pcity, const struct player *attacker, int max_turns, bool omniscient, const struct civ_map *map)
'pf_reverse_map' constructor for city.
void pf_reverse_map_destroy(struct pf_reverse_map *pfrm)
'pf_reverse_map' destructor.
void pf_map_destroy(struct pf_map *pfm)
After usage the map must be destroyed.
#define PF_IMPOSSIBLE_MC
Definition: path_finding.h:244
#define pf_map_tiles_iterate(ARG_pfm, NAME_tile, COND_from_start)
Definition: path_finding.h:509
#define pf_map_tiles_iterate_end
Definition: path_finding.h:516
void pft_fill_utype_parameter(struct pf_parameter *parameter, const struct unit_type *punittype, struct tile *pstart_tile, struct player *pplayer)
Fill classic parameters for an unit type.
Definition: pf_tools.cpp:809
#define players_iterate_end
Definition: player.h:520
#define players_iterate(_pplayer)
Definition: player.h:514
static bool player_is_cpuhog(const struct player *pplayer)
Definition: player.h:558
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 MIN(x, y)
Definition: shared.h:49
#define FC_INFINITY
Definition: shared.h:32
#define MAX(x, y)
Definition: shared.h:48
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Return the output for the specialist type with this output type.
Definition: specialist.cpp:218
#define specialist_type_iterate_end
Definition: specialist.h:73
#define specialist_type_iterate(sp)
Definition: specialist.h:67
@ AIT_DANGER
Definition: srv_log.h:54
#define LOGLEVEL_BUILD
Definition: srv_log.h:36
@ 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
enum server_states server_state()
Return current server state.
Definition: srv_main.cpp:238
enum choice_type type
Definition: advchoice.h:32
adv_want want
Definition: advchoice.h:34
universals_u value
Definition: advchoice.h:33
bool need_boat
Definition: advchoice.h:35
int wonder_city
Definition: advdata.h:46
int urgency
Definition: daicity.h:46
struct adv_choice choice
Definition: daicity.h:38
int danger
Definition: daicity.h:44
bool has_diplomat
Definition: daicity.h:56
int bcost
Definition: daicity.h:41
bool diplomat_threat
Definition: daicity.h:55
adv_want worth
Definition: daicity.h:32
int wallvalue
Definition: daicity.h:48
int grave_danger
Definition: daicity.h:45
int attack
Definition: daicity.h:41
struct ai_invasion invasion
Definition: daicity.h:40
int attack
Definition: daicity.h:27
int occupy
Definition: daicity.h:28
Definition: aidata.h:63
int boats
Definition: aidata.h:71
adv_want tech_want[A_LAST+1]
Definition: aidata.h:95
int available_boats
Definition: aidata.h:72
struct ai_plr::@155 stats
Definition: ai.h:42
Definition: city.h:291
int surplus[O_LAST]
Definition: city.h:324
int id
Definition: city.h:296
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 tile * start_tile
Definition: path_finding.h:341
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
struct player_economic economic
Definition: player.h:266
struct adv_data * adv
Definition: player.h:322
Definition: tile.h:42
struct unit_list * units
Definition: tile.h:50
Continent_id continent
Definition: tile.h:46
enum move_level sea_move
Definition: unittype.h:136
bool carries_occupiers
Definition: aiunit.h:60
bool firepower1
Definition: aiunit.h:57
struct unit_type::@82 adv
int defense_strength
Definition: unittype.h:480
int firepower
Definition: unittype.h:490
int paratroopers_range
Definition: unittype.h:506
int move_rate
Definition: unittype.h:481
struct unit_type::@83 cache
int hp
Definition: unittype.h:489
bool igwall
Definition: unittype.h:537
int max_defense_mp_pct
Definition: unittype.h:542
int attack_strength
Definition: unittype.h:479
int defense_mp_bonuses_pct[U_LAST]
Definition: unittype.h:545
Definition: unit.h:134
int id
Definition: unit.h:141
int hp
Definition: unit.h:148
struct tile * tile
Definition: unit.h:136
int veteran
Definition: unit.h:149
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
tech_req
Definition: tech.h:104
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_XY(ptile)
Definition: tile.h:36
#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
struct unit * unit_transport_get(const struct unit *pcargo)
Returns the transporter of the unit or nullptr if it is not transported.
Definition: unit.cpp:2189
bool is_military_unit(const struct unit *punit)
Military units are capable of enforcing martial law.
Definition: unit.cpp:300
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
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
bool unit_transported(const struct unit *pcargo)
Returns TRUE iff the unit is transported.
Definition: unit.cpp:2176
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_owner(_pu)
Definition: unit.h:370
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
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_has_role(const struct unit_type *punittype, int role)
Return whether the given unit type has the role.
Definition: unittype.cpp:186
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 utype_build_shield_cost_base(const struct unit_type *punittype)
Returns the number of shields this unit type represents.
Definition: unittype.cpp:1168
int unit_build_shield_cost_base(const struct unit *punit)
Returns the number of shields this unit represents.
Definition: unittype.cpp:1185
struct unit_class * unit_class_get(const struct unit *punit)
Returns unit class pointer for a unit.
Definition: unittype.cpp:2151
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
bool unit_can_take_over(const struct unit *punit)
Return whether the unit can take over enemy cities.
Definition: unittype.cpp:203
Unit_type_id utype_index(const struct unit_type *punittype)
Return the unit type index.
Definition: unittype.cpp:82
bool utype_can_take_over(const struct unit_type *punittype)
Return whether the unit type can take over enemy cities.
Definition: unittype.cpp:215
bool utype_acts_hostile(const struct unit_type *putype)
Return TRUE iff units of this type can do hostile actions controlled by generalized (ruleset defined)...
Definition: unittype.cpp:432
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Return TRUE iff units of the given type can do the specified generalized (ruleset defined) action ena...
Definition: unittype.cpp:386
void * utype_ai_data(const struct unit_type *ptype, const struct ai_type *ai)
Return pointer to ai data of given unit type and ai type.
Definition: unittype.cpp:2332
#define utype_class(_t_)
Definition: unittype.h:691
#define utype_fuel(ptype)
Definition: unittype.h:772
@ MOVE_FULL
Definition: unittype.h:115
@ MOVE_NONE
Definition: unittype.h:115
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition: unittype.h:584
#define unit_type_iterate(_p)
Definition: unittype.h:785
#define U_LAST
Definition: unittype.h:31
#define unit_type_iterate_end
Definition: unittype.h:791
#define U_NOT_OBSOLETED
Definition: unittype.h:493