Freeciv21
Develop your civilization from humble roots to a global empire
aitools.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 // utility
15 #include "bitvector.h"
16 #include "log.h"
17 #include "shared.h"
18 
19 // common
20 #include "city.h"
21 #include "combat.h"
22 #include "game.h"
23 #include "government.h"
24 #include "map.h"
25 #include "movement.h"
26 #include "nation.h"
27 #include "packets.h"
28 #include "player.h"
29 #include "unit.h"
30 #include "unitlist.h"
31 
32 /* common/aicore */
33 #include "citymap.h"
34 #include "pf_tools.h"
35 
36 // server
37 #include "cityturn.h"
38 #include "maphand.h"
39 #include "score.h"
40 #include "srv_log.h"
41 #include "unithand.h"
42 #include "unittools.h"
43 
44 /* server/advisors */
45 #include "advdata.h"
46 #include "advgoto.h"
47 #include "advtools.h"
48 #include "infracache.h" // adv_city
49 
50 // ai
51 #include "handicaps.h"
52 
53 /* ai/default */
54 #include "aidata.h"
55 #include "aiferry.h"
56 #include "aiguard.h"
57 #include "ailog.h"
58 #include "aiplayer.h"
59 #include "aitech.h"
60 #include "aiunit.h"
61 #include "daimilitary.h"
62 
63 #include "aitools.h"
64 
65 class PFPath;
70 const char *dai_unit_task_rule_name(const enum ai_unit_task task)
71 {
72  switch (task) {
73  case AIUNIT_NONE:
74  return "None";
76  return "Auto settler";
77  case AIUNIT_BUILD_CITY:
78  return "Build city";
79  case AIUNIT_DEFEND_HOME:
80  return "Defend home";
81  case AIUNIT_ATTACK:
82  return "Attack";
83  case AIUNIT_ESCORT:
84  return "Escort";
85  case AIUNIT_EXPLORE:
86  return "Explore";
87  case AIUNIT_RECOVER:
88  return "Recover";
89  case AIUNIT_HUNTER:
90  return "Hunter";
91  case AIUNIT_TRADE:
92  return "Trade";
93  case AIUNIT_WONDER:
94  return "Wonder";
95  }
96  // no default, ensure all types handled somehow
97  qCritical("Unsupported ai_unit_task %d.", task);
98  return nullptr;
99 }
100 
105 const char *dai_choice_rule_name(const struct adv_choice *choice)
106 {
107  switch (choice->type) {
108  case CT_NONE:
109  return "(nothing)";
110  case CT_BUILDING:
111  return improvement_rule_name(choice->value.building);
112  case CT_CIVILIAN:
113  case CT_ATTACKER:
114  case CT_DEFENDER:
115  return utype_rule_name(choice->value.utype);
116  case CT_LAST:
117  return "(unknown)";
118  };
119  // no default, ensure all types handled somehow
120  qCritical("Unsupported ai_unit_task %d.", choice->type);
121  return nullptr;
122 }
123 
132 int military_amortize(struct player *pplayer, struct city *pcity, int value,
133  int delay, int build_cost)
134 {
135  struct adv_data *ai = adv_data_get(pplayer, nullptr);
136  int city_output = (pcity ? pcity->surplus[O_SHIELD] : 1);
137  int output = MAX(city_output, ai->stats.average_production);
138  int build_time = build_cost / MAX(output, 1);
139 
140  if (value <= 0) {
141  return 0;
142  }
143 
144  return amortize(value, delay + build_time);
145 }
146 
156 void dai_consider_plr_dangerous(struct ai_type *ait, struct player *plr1,
157  struct player *plr2,
158  enum override_bool *result)
159 {
160  struct ai_dip_intel *adip;
161 
162  adip = dai_diplomacy_get(ait, plr1, plr2);
163 
164  if (adip->countdown >= 0) {
165  // Don't trust our war target
166  *result = OVERRIDE_TRUE;
167  }
168 }
169 
175 static bool dai_gothere_bodyguard(struct ai_type *ait, struct unit *punit,
176  struct tile *dest_tile)
177 {
178  struct player *pplayer = unit_owner(punit);
179  int danger = 0;
180  struct city *dcity;
181  struct unit *guard = aiguard_guard_of(ait, punit);
182  const struct veteran_level *vlevel;
183  bool bg_needed = false;
184 
185  if (is_barbarian(unit_owner(punit))) {
186  // barbarians must have more courage (ie less brains)
187  aiguard_clear_guard(ait, punit);
188  return false;
189  }
190 
191  // Estimate enemy attack power.
192  unit_list_iterate(dest_tile->units, aunit)
193  {
194  if (POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, unit_owner(aunit))) {
195  danger += adv_unit_att_rating(aunit);
196  }
197  }
199  dcity = tile_city(dest_tile);
200  if (dcity && POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, city_owner(dcity))) {
201  // Assume enemy will build another defender, add it's attack strength
202  struct unit_type *d_type = dai_choose_defender_versus(dcity, punit);
203 
204  if (d_type) {
205  // Enemy really can build something
206  danger += adv_unittype_att_rating(
207  d_type, city_production_unit_veteran_level(dcity, d_type),
208  SINGLE_MOVE, d_type->hp);
209  }
210  }
211  danger *= POWER_DIVIDER;
212 
213  /* If we are fast, there is less danger.
214  * FIXME: that assumes that most units have move_rate == SINGLE_MOVE;
215  * not true for all rulesets */
216  danger /= (unit_type_get(punit)->move_rate / SINGLE_MOVE);
217  if (unit_has_type_flag(punit, UTYF_IGTER)) {
218  danger /= 1.5;
219  }
220 
221  vlevel = utype_veteran_level(unit_type_get(punit), punit->veteran);
222  fc_assert_ret_val(vlevel != nullptr, false);
223 
224  // We look for the bodyguard where we stand.
225  if (guard == nullptr || unit_tile(guard) != unit_tile(punit)) {
226  int my_def = (punit->hp * unit_type_get(punit)->defense_strength
227  * POWER_FACTOR * vlevel->power_fact / 100);
228 
229  if (danger >= my_def) {
231  "want bodyguard @(%d, %d) danger=%d, my_def=%d",
232  TILE_XY(dest_tile), danger, my_def);
233  aiguard_request_guard(ait, punit);
234  bg_needed = true;
235  } else {
236  aiguard_clear_guard(ait, punit);
237  bg_needed = false;
238  }
239  } else if (guard != nullptr) {
240  bg_needed = true;
241  }
242 
243  // What if we have a bodyguard, but don't need one?
244 
245  return bg_needed;
246 }
247 
248 #define LOGLEVEL_GOTHERE LOG_DEBUG
258 bool dai_gothere(struct ai_type *ait, struct player *pplayer,
259  struct unit *punit, struct tile *dest_tile)
260 {
261  CHECK_UNIT(punit);
262  bool bg_needed;
263 
264  if (same_pos(dest_tile, unit_tile(punit)) || punit->moves_left <= 0) {
265  // Nowhere to go
266  return true;
267  }
268 
269  // See if we need a bodyguard at our destination
270  // FIXME: If bodyguard is _really_ necessary, don't go anywhere
271  bg_needed = dai_gothere_bodyguard(ait, punit, dest_tile);
272 
273  if (unit_transported(punit) || !goto_is_sane(punit, dest_tile)) {
274  // Must go by boat, call an aiferryboat function
275  if (!aiferry_gobyboat(ait, pplayer, punit, dest_tile, bg_needed)) {
276  return false;
277  }
278  }
279 
280  /* Go where we should be going if we can, and are at our destination
281  * if we are on a ferry */
282  if (goto_is_sane(punit, dest_tile) && punit->moves_left > 0) {
283  punit->goto_tile = dest_tile;
284  UNIT_LOG(LOGLEVEL_GOTHERE, punit, "Walking to (%d,%d)",
285  TILE_XY(dest_tile));
286  if (!dai_unit_goto(ait, punit, dest_tile)) {
287  // died
288  return false;
289  }
290  // liable to bump into someone that will kill us. Should avoid?
291  } else {
292  UNIT_LOG(LOGLEVEL_GOTHERE, punit, "Not moving");
293  return false;
294  }
295 
296  if (def_ai_unit_data(punit, ait)->ferryboat > 0
297  && !unit_transported(punit)) {
298  // We probably just landed, release our boat
299  aiferry_clear_boat(ait, punit);
300  }
301 
302  // Dead unit shouldn't reach this point
303  CHECK_UNIT(punit);
304 
305  return (same_pos(unit_tile(punit), dest_tile)
306  || is_tiles_adjacent(unit_tile(punit), dest_tile));
307 }
308 
314 struct tile *immediate_destination(struct unit *punit,
315  struct tile *dest_tile)
316 {
317  if (!same_pos(unit_tile(punit), dest_tile)
318  && utype_fuel(unit_type_get(punit))) {
319  struct pf_parameter parameter;
320  struct pf_map *pfm;
321  size_t i;
322  struct player *pplayer = unit_owner(punit);
323 
324  pft_fill_unit_parameter(&parameter, punit);
325  parameter.omniscience = !has_handicap(pplayer, H_MAP);
326  pfm = pf_map_new(&parameter);
327  auto path = pf_map_path(pfm, punit->goto_tile);
328 
329  if (!path.empty()) {
330  for (i = 1; i < path.length(); i++) {
331  if (path[i].tile == path[i - 1].tile) {
332  // The path-finding code advices us to wait there to refuel.
333  struct tile *ptile = path[i].tile;
334  pf_map_destroy(pfm);
335  return ptile;
336  }
337  }
338  pf_map_destroy(pfm);
339  // Seems it's the immediate destination
340  return punit->goto_tile;
341  }
342 
343  pf_map_destroy(pfm);
344  qDebug("Did not find an air-route for "
345  "%s %s[%d] (%d,%d)->(%d,%d)",
347  punit->id, TILE_XY(unit_tile(punit)), TILE_XY(dest_tile));
348  // Prevent take off
349  return unit_tile(punit);
350  }
351 
352  // else does not need way-points
353  return dest_tile;
354 }
355 
359 void dai_log_path(struct unit *punit, const PFPath &path,
360  struct pf_parameter *parameter)
361 {
362  const struct pf_position last = path[-1];
363  const int cc =
364  PF_TURN_FACTOR * last.total_MC + parameter->move_rate * last.total_EC;
365  const int tc = cc / (PF_TURN_FACTOR * parameter->move_rate);
366 
367  UNIT_LOG(LOG_DEBUG, punit, "path L=%d T=%d(%d) MC=%d EC=%d CC=%d",
368  path.length() - 1, last.turn, tc, last.total_MC, last.total_EC,
369  cc);
370 }
371 
381 bool dai_unit_goto_constrained(struct ai_type *ait, struct unit *punit,
382  struct tile *ptile,
383  struct pf_parameter *parameter)
384 {
385  bool alive = true;
386  struct pf_map *pfm;
387 
388  UNIT_LOG(LOG_DEBUG, punit, "constrained goto to %d,%d", TILE_XY(ptile));
389 
390  ptile = immediate_destination(punit, ptile);
391 
392  UNIT_LOG(LOG_DEBUG, punit, "constrained goto: let's go to %d,%d",
393  TILE_XY(ptile));
394 
395  if (same_pos(unit_tile(punit), ptile)) {
396  /* Not an error; sometimes immediate_destination instructs the unit
397  * to stay here. For example, to refuel.*/
398  UNIT_LOG(LOG_DEBUG, punit, "constrained goto: already there!");
399  send_unit_info(nullptr, punit);
400 
401  return true;
402  } else if (!goto_is_sane(punit, ptile)) {
403  UNIT_LOG(LOG_DEBUG, punit, "constrained goto: 'insane' goto!");
404  punit->activity = ACTIVITY_IDLE;
405  send_unit_info(nullptr, punit);
406 
407  return true;
408  } else if (punit->moves_left == 0) {
409  UNIT_LOG(LOG_DEBUG, punit, "constrained goto: no moves left!");
410  send_unit_info(nullptr, punit);
411 
412  return true;
413  }
414 
415  pfm = pf_map_new(parameter);
416  auto path = pf_map_path(pfm, ptile);
417 
418  if (!path.empty()) {
419  dai_log_path(punit, path, parameter);
420  UNIT_LOG(LOG_DEBUG, punit, "constrained goto: following path.");
421  alive = adv_follow_path(punit, path, ptile);
422  } else {
423  UNIT_LOG(LOG_DEBUG, punit, "no path to destination");
424  }
425 
426  pf_map_destroy(pfm);
427 
428  return alive;
429 }
430 
436 bool goto_is_sane(struct unit *punit, struct tile *ptile)
437 {
438  bool can_get_there = false;
439 
440  if (same_pos(unit_tile(punit), ptile)) {
441  can_get_there = true;
442  } else {
443  struct pf_parameter parameter;
444  struct pf_map *pfm;
445 
446  pft_fill_unit_attack_param(&parameter, punit);
447  pfm = pf_map_new(&parameter);
448 
449  if (pf_map_move_cost(pfm, ptile) != PF_IMPOSSIBLE_MC) {
450  can_get_there = true;
451  }
452  pf_map_destroy(pfm);
453  }
454  return can_get_there;
455 }
456 
457 /*
458  * The length of time, in turns, which is long enough to be optimistic
459  * that enemy units will have moved from their current position.
460  * WAG
461  */
462 #define LONG_TIME 4
475 void dai_fill_unit_param(struct ai_type *ait, struct pf_parameter *parameter,
476  struct adv_risk_cost *risk_cost, struct unit *punit,
477  struct tile *ptile)
478 {
479  const bool long_path =
480  LONG_TIME < (map_distance(unit_tile(punit), unit_tile(punit))
481  * SINGLE_MOVE / unit_type_get(punit)->move_rate);
482  const bool barbarian = is_barbarian(unit_owner(punit));
483  bool is_ferry;
484  struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
485  struct player *pplayer = unit_owner(punit);
486 
487  /* This function is now always omniscient and should not be used
488  * for human players any more. */
489  fc_assert(is_ai(pplayer));
490 
491  // If a unit is hunting, don't expect it to be a ferry.
492  is_ferry = (unit_data->task != AIUNIT_HUNTER && dai_is_ferry(punit, ait));
493 
494  if (is_ferry) {
495  /* The destination may be a coastal land tile,
496  * in which case the ferry should stop on an adjacent tile. */
497  pft_fill_unit_overlap_param(parameter, punit);
498  } else if (!utype_fuel(unit_type_get(punit))
500  ACTRES_ATTACK)
501  && (unit_data->task == AIUNIT_DEFEND_HOME
502  || unit_data->task == AIUNIT_ATTACK
503  || unit_data->task == AIUNIT_ESCORT
504  || unit_data->task == AIUNIT_HUNTER)) {
505  /* Use attack movement for defenders and escorts so they can
506  * make defensive attacks */
507  pft_fill_unit_attack_param(parameter, punit);
508  } else {
509  pft_fill_unit_parameter(parameter, punit);
510  }
511  parameter->omniscience = !has_handicap(pplayer, H_MAP);
512 
513  /* Should we use the risk avoidance code?
514  * The risk avoidance code uses omniscience, so do not use for
515  * human-player units under temporary AI control.
516  * Barbarians bravely/stupidly ignore risks
517  */
518  if (!uclass_has_flag(unit_class_get(punit), UCF_UNREACHABLE)
519  && !barbarian) {
520  adv_avoid_risks(parameter, risk_cost, punit,
522  }
523 
524  /* Should we absolutely forbid ending a turn on a dangerous tile?
525  * Do not annoy human players by killing their units for them.
526  * For AI units be optimistic; allows attacks across dangerous terrain,
527  * and polar settlements.
528  * TODO: This is compatible with old code,
529  * but probably ought to be more cautious for non military units
530  */
531  if (!is_ferry && !utype_fuel(unit_type_get(punit))) {
532  parameter->get_moves_left_req = nullptr;
533  }
534 
535  if (long_path) {
536  /* Move as far along the path to the destination as we can;
537  * that is, ignore the presence of enemy units when computing the
538  * path.
539  * Hopefully, ai_avoid_risks will have produced a path that avoids enemy
540  * ZoCs. Ignoring ZoCs allows us to move closer to a destination
541  * for which there is not yet a clear path.
542  * That is good if the destination is several turns away,
543  * so we can reasonably expect blocking enemy units to move or
544  * be destroyed. But it can be bad if the destination is one turn away
545  * or our destination is far but there are enemy units near us and on the
546  * shortest path to the destination.
547  */
548  parameter->get_zoc = nullptr;
549  }
550 
551  if (unit_has_type_flag(punit, UTYF_SETTLERS)) {
552  parameter->get_TB = no_fights;
553  } else if (long_path && unit_is_cityfounder(punit)) {
554  /* Default tile behaviour;
555  * move as far along the path to the destination as we can;
556  * that is, ignore the presence of enemy units when computing the
557  * path.
558  */
559  } else if (unit_is_cityfounder(punit)) {
560  // Short path
561  parameter->get_TB = no_fights;
562  } else if (unit_has_type_role(punit, L_BARBARIAN_LEADER)) {
563  // Avoid capture
564  parameter->get_TB = no_fights;
565  } else if (is_ferry) {
566  // Ferries are not warships
567  parameter->get_TB = no_fights;
568  } else if (is_losing_hp(punit)) {
569  // Losing hitpoints over time (helicopter in default rules)
570  // Default tile behaviour
571  } else if (utype_may_act_at_all(unit_type_get(punit))) {
572  switch (unit_data->task) {
573  case AIUNIT_AUTO_SETTLER:
574  case AIUNIT_BUILD_CITY:
575  // Strange, but not impossible
576  parameter->get_TB = no_fights;
577  break;
578  case AIUNIT_DEFEND_HOME:
579  case AIUNIT_ATTACK: // Includes spy actions
580  case AIUNIT_ESCORT:
581  case AIUNIT_HUNTER:
582  case AIUNIT_TRADE:
583  case AIUNIT_WONDER:
584  parameter->get_TB = no_intermediate_fights;
585  break;
586  case AIUNIT_EXPLORE:
587  case AIUNIT_RECOVER:
588  parameter->get_TB = no_fights;
589  break;
590  case AIUNIT_NONE:
591  // Default tile behaviour
592  break;
593  }
594  } else {
595  // Probably an explorer
596  parameter->get_TB = no_fights;
597  }
598 
599  if (is_ferry) {
600  // Show the destination in the client when watching an AI:
601  punit->goto_tile = ptile;
602  }
603 }
604 
609 bool dai_unit_goto(struct ai_type *ait, struct unit *punit,
610  struct tile *ptile)
611 {
612  struct pf_parameter parameter;
613  struct adv_risk_cost risk_cost;
614 
615  UNIT_LOG(LOG_DEBUG, punit, "dai_unit_goto to %d,%d", TILE_XY(ptile));
616  dai_fill_unit_param(ait, &parameter, &risk_cost, punit, ptile);
617 
618  return dai_unit_goto_constrained(ait, punit, ptile, &parameter);
619 }
620 
624 void dai_unit_new_adv_task(struct ai_type *ait, struct unit *punit,
625  enum adv_unit_task task, struct tile *ptile)
626 {
627  // Keep ai_unit_task in sync with adv task
628  switch (task) {
629  case AUT_AUTO_SETTLER:
630  dai_unit_new_task(ait, punit, AIUNIT_AUTO_SETTLER, ptile);
631  break;
632  case AUT_BUILD_CITY:
633  dai_unit_new_task(ait, punit, AIUNIT_BUILD_CITY, ptile);
634  break;
635  case AUT_NONE:
636  dai_unit_new_task(ait, punit, AIUNIT_NONE, ptile);
637  break;
638  }
639 }
640 
648 void dai_unit_new_task(struct ai_type *ait, struct unit *punit,
649  enum ai_unit_task task, struct tile *ptile)
650 {
651  struct unit *bodyguard = aiguard_guard_of(ait, punit);
652  struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
653 
654  /* If the unit is under (human) orders we shouldn't control it.
655  * Allow removal of old role with AIUNIT_NONE. */
657 
658  UNIT_LOG(LOG_DEBUG, punit, "changing task from %s to %s",
659  dai_unit_task_rule_name(unit_data->task),
661 
662  // Free our ferry. Most likely it has been done already.
663  if (task == AIUNIT_NONE || task == AIUNIT_DEFEND_HOME) {
664  aiferry_clear_boat(ait, punit);
665  }
666 
667  if (punit->activity == ACTIVITY_GOTO) {
668  // It would indicate we're going somewhere otherwise
669  unit_activity_handling(punit, ACTIVITY_IDLE);
670  }
671 
672  if (unit_data->task == AIUNIT_BUILD_CITY) {
673  if (punit->goto_tile) {
674  citymap_free_city_spot(punit->goto_tile, punit->id);
675  } else {
676  /* Print error message instead of crashing in citymap_free_city_spot()
677  * This probably means that some city spot reservation has not been
678  * properly cleared; bad for the AI, as it will leave that area
679  * uninhabited. */
680  qCritical("%s was on city founding mission without target tile.",
681  unit_rule_name(punit));
682  }
683  }
684 
685  if (unit_data->task == AIUNIT_HUNTER) {
686  // Clear victim's hunted bit - we're no longer chasing.
687  struct unit *target = game_unit_by_number(unit_data->target);
688 
689  if (target) {
690  BV_CLR(def_ai_unit_data(target, ait)->hunted,
691  player_index(unit_owner(punit)));
692  UNIT_LOG(LOGLEVEL_HUNT, target,
693  "no longer hunted (new task %d, old %d)", task,
694  unit_data->task);
695  }
696  }
697 
698  aiguard_clear_charge(ait, punit);
699  // Record the city to defend; our goto may be to transport.
700  if (task == AIUNIT_DEFEND_HOME && ptile && tile_city(ptile)) {
701  aiguard_assign_guard_city(ait, tile_city(ptile), punit);
702  }
703 
704  unit_data->task = task;
705 
706  /* Verify and set the goto destination. Eventually this can be a lot more
707  * stringent, but for now we don't want to break things too badly. */
708  punit->goto_tile = ptile; // May be nullptr.
709 
710  if (unit_data->task == AIUNIT_NONE && bodyguard) {
711  dai_unit_new_task(ait, bodyguard, AIUNIT_NONE, nullptr);
712  }
713 
714  // Reserve city spot, _unless_ we want to add ourselves to a city.
715  if (unit_data->task == AIUNIT_BUILD_CITY && !tile_city(ptile)) {
716  citymap_reserve_city_spot(ptile, punit->id);
717  }
718  if (unit_data->task == AIUNIT_HUNTER) {
719  // Set victim's hunted bit - the hunt is on!
720  struct unit *target = game_unit_by_number(unit_data->target);
721 
722  fc_assert_ret(target != nullptr);
723  BV_SET(def_ai_unit_data(target, ait)->hunted,
724  player_index(unit_owner(punit)));
725  UNIT_LOG(LOGLEVEL_HUNT, target, "is being hunted");
726 
727  // Grab missiles lying around and bring them along
728  unit_list_iterate(unit_tile(punit)->units, missile)
729  {
730  if (unit_owner(missile) == unit_owner(punit)
731  && def_ai_unit_data(missile, ait)->task != AIUNIT_ESCORT
732  && !unit_transported(missile)
734  ACTION_SUICIDE_ATTACK)
735  && can_unit_load(missile, punit)) {
736  UNIT_LOG(LOGLEVEL_HUNT, missile, "loaded on hunter");
737  dai_unit_new_task(ait, missile, AIUNIT_ESCORT, unit_tile(target));
738  unit_transport_load_send(missile, punit);
739  }
740  }
742  }
743 
744  /* Map ai tasks to advisor tasks. For most ai tasks there is
745  no advisor, so AUT_NONE is set. */
746  switch (unit_data->task) {
747  case AIUNIT_AUTO_SETTLER:
748  punit->server.adv->task = AUT_AUTO_SETTLER;
749  break;
750  case AIUNIT_BUILD_CITY:
751  punit->server.adv->task = AUT_BUILD_CITY;
752  break;
753  default:
754  punit->server.adv->task = AUT_NONE;
755  break;
756  }
757 }
758 
763 bool dai_unit_make_homecity(struct unit *punit, struct city *pcity)
764 {
765  CHECK_UNIT(punit);
766  fc_assert_ret_val(unit_owner(punit) == city_owner(pcity), true);
767 
768  if (punit->homecity == 0 && !unit_has_type_role(punit, L_EXPLORER)) {
769  /* This unit doesn't pay any upkeep while it doesn't have a homecity,
770  * so it would be stupid to give it one. There can also be good reasons
771  * why it doesn't have a homecity. */
772  /* However, until we can do something more useful with them, we
773  will assign explorers to a city so that they can be disbanded for
774  the greater good -- Per */
775  return false;
776  }
777  if (pcity->surplus[O_SHIELD] >= unit_type_get(punit)->upkeep[O_SHIELD]
778  && pcity->surplus[O_FOOD] >= unit_type_get(punit)->upkeep[O_FOOD]) {
779  unit_do_action(unit_owner(punit), punit->id, pcity->id, 0, "",
780  ACTION_HOME_CITY);
781  return true;
782  }
783  return false;
784 }
785 
792 static void dai_unit_bodyguard_move(struct ai_type *ait,
793  struct unit *bodyguard,
794  struct tile *ptile)
795 {
796  struct unit *punit;
797  struct player *pplayer;
798 
799  fc_assert_ret(bodyguard != nullptr);
800  pplayer = unit_owner(bodyguard);
801  fc_assert_ret(pplayer != nullptr);
802  punit = aiguard_charge_unit(ait, bodyguard);
803  fc_assert_ret(punit != nullptr);
804 
805  CHECK_GUARD(ait, bodyguard);
806  CHECK_CHARGE_UNIT(ait, punit);
807 
808  if (!is_tiles_adjacent(ptile, unit_tile(bodyguard))) {
809  return;
810  }
811 
812  if (bodyguard->moves_left <= 0) {
813  // should generally should not happen
814  BODYGUARD_LOG(ait, LOG_DEBUG, bodyguard, "was left behind by charge");
815  return;
816  }
817 
818  unit_activity_handling(bodyguard, ACTIVITY_IDLE);
819  (void) dai_unit_move(ait, bodyguard, ptile);
820 }
821 
825 bool dai_unit_attack(struct ai_type *ait, struct unit *punit,
826  struct tile *ptile)
827 {
828  struct unit *ptrans;
829  struct unit *bodyguard = aiguard_guard_of(ait, punit);
830  int sanity = punit->id;
831  bool alive;
832  struct city *tcity;
833 
834  CHECK_UNIT(punit);
835  fc_assert_ret_val(is_ai(unit_owner(punit)), true);
836  fc_assert_ret_val(is_tiles_adjacent(unit_tile(punit), ptile), true);
837 
838  unit_activity_handling(punit, ACTIVITY_IDLE);
839  /* FIXME: try the next action if the unit tried to do an illegal action.
840  * That would allow the AI to stop using the omniscient
841  * is_action_enabled_unit_on_*() functions. */
842  if (is_action_enabled_unit_on_units(ACTION_CAPTURE_UNITS, punit, ptile)) {
843  // Choose capture.
844  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
845  ACTION_CAPTURE_UNITS);
846  } else if (is_action_enabled_unit_on_units(ACTION_BOMBARD, punit, ptile)) {
847  // Choose "Bombard".
848  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
849  ACTION_BOMBARD);
850  } else if (is_action_enabled_unit_on_units(ACTION_BOMBARD2, punit,
851  ptile)) {
852  // Choose "Bombard 2".
853  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
854  ACTION_BOMBARD2);
855  } else if (is_action_enabled_unit_on_units(ACTION_BOMBARD3, punit,
856  ptile)) {
857  // Choose "Bombard 3".
858  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
859  ACTION_BOMBARD3);
860  } else if (is_action_enabled_unit_on_units(ACTION_NUKE_UNITS, punit,
861  ptile)) {
862  // Choose "Nuke Units".
863  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
864  ACTION_NUKE_UNITS);
865  } else if ((tcity = tile_city(ptile))
866  && is_action_enabled_unit_on_city(ACTION_NUKE_CITY, punit,
867  tcity)) {
868  // Choose "Nuke City".
869  unit_do_action(unit_owner(punit), punit->id, tcity->id, 0, "",
870  ACTION_NUKE_CITY);
871  } else if (is_action_enabled_unit_on_units(ACTION_ATTACK, punit, ptile)) {
872  // Choose regular attack.
873  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
874  ACTION_ATTACK);
875  } else if (is_action_enabled_unit_on_units(ACTION_SUICIDE_ATTACK, punit,
876  ptile)) {
877  // Choose suicide attack (explode missile).
878  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
879  ACTION_SUICIDE_ATTACK);
880  } else if ((tcity = tile_city(ptile))
881  && is_action_enabled_unit_on_city(ACTION_CONQUER_CITY, punit,
882  tcity)) {
883  // Choose "Conquer City".
884  unit_do_action(unit_owner(punit), punit->id, tcity->id, 0, "",
885  ACTION_CONQUER_CITY);
886  } else if ((tcity = tile_city(ptile))
887  && is_action_enabled_unit_on_city(ACTION_CONQUER_CITY2, punit,
888  tcity)) {
889  // Choose "Conquer City 2".
890  unit_do_action(unit_owner(punit), punit->id, tcity->id, 0, "",
891  ACTION_CONQUER_CITY2);
892  } else if (!can_unit_survive_at_tile(&(wld.map), punit, ptile)
893  && ((ptrans = transporter_for_unit_at(punit, ptile)))
894  && is_action_enabled_unit_on_unit(ACTION_TRANSPORT_EMBARK,
895  punit, ptrans)) {
896  // "Transport Embark".
897  unit_do_action(unit_owner(punit), punit->id, ptrans->id, 0, "",
898  ACTION_TRANSPORT_EMBARK);
899  } else if (is_action_enabled_unit_on_tile(ACTION_TRANSPORT_DISEMBARK1,
900  punit, ptile, nullptr)) {
901  // "Transport Disembark".
902  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
903  ACTION_TRANSPORT_DISEMBARK1);
904  } else if (is_action_enabled_unit_on_tile(ACTION_TRANSPORT_DISEMBARK2,
905  punit, ptile, nullptr)) {
906  // "Transport Disembark 2".
907  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
908  ACTION_TRANSPORT_DISEMBARK2);
909  } else {
910  // Other move.
911  (void) unit_move_handling(punit, ptile, false, true);
912  }
913  alive = (game_unit_by_number(sanity) != nullptr);
914 
915  if (alive && same_pos(ptile, unit_tile(punit)) && bodyguard != nullptr
916  && def_ai_unit_data(bodyguard, ait)->charge == punit->id) {
917  dai_unit_bodyguard_move(ait, bodyguard, ptile);
918  // Clumsy bodyguard might trigger an auto-attack
919  alive = (game_unit_by_number(sanity) != nullptr);
920  }
921 
922  return alive;
923 }
924 
928 void dai_unit_move_or_attack(struct ai_type *ait, struct unit *punit,
929  struct tile *ptile, const PFPath &path,
930  int step)
931 {
932  if (step == path.length() - 1) {
933  (void) dai_unit_attack(ait, punit, ptile);
934  } else {
935  (void) dai_unit_move(ait, punit, ptile);
936  }
937 }
938 
947 bool dai_unit_move(struct ai_type *ait, struct unit *punit,
948  struct tile *ptile)
949 {
950  struct action *paction;
951  struct unit *bodyguard;
952  struct unit *ptrans = nullptr;
953  int sanity = punit->id;
954  struct player *pplayer = unit_owner(punit);
955  const bool is_plr_ai = is_ai(pplayer);
956 
957  CHECK_UNIT(punit);
958  fc_assert_ret_val_msg(is_tiles_adjacent(unit_tile(punit), ptile), false,
959  "Tiles not adjacent: Unit = %d, "
960  "from = (%d, %d]) to = (%d, %d).",
961  punit->id, TILE_XY(unit_tile(punit)),
962  TILE_XY(ptile));
963 
964  /* if enemy, stop and give a chance for the ai attack function
965  * to handle this case */
966  if (is_enemy_unit_tile(ptile, pplayer)
967  || is_enemy_city_tile(ptile, pplayer)) {
968  UNIT_LOG(LOG_DEBUG, punit, "movement halted due to enemy presence");
969  return false;
970  }
971 
972  // barbarians shouldn't enter huts
973  // FIXME: use unit_can_displace_hut(punit, ptile) better
974  if (is_barbarian(pplayer) && hut_on_tile(ptile)) {
975  return false;
976  }
977 
978  // don't leave bodyguard behind
979  if (is_plr_ai && (bodyguard = aiguard_guard_of(ait, punit))
980  && same_pos(unit_tile(punit), unit_tile(bodyguard))
981  && bodyguard->moves_left == 0) {
983  "does not want to leave "
984  "its bodyguard");
985  return false;
986  }
987 
988  // Select move kind.
989  if (!can_unit_survive_at_tile(&(wld.map), punit, ptile)
990  && ((ptrans = transporter_for_unit_at(punit, ptile)))
991  && is_action_enabled_unit_on_unit(ACTION_TRANSPORT_EMBARK, punit,
992  ptrans)) {
993  // "Transport Embark".
994  paction = action_by_number(ACTION_TRANSPORT_EMBARK);
995  } else if (is_action_enabled_unit_on_tile(ACTION_TRANSPORT_DISEMBARK1,
996  punit, ptile, nullptr)) {
997  // "Transport Disembark".
998  paction = action_by_number(ACTION_TRANSPORT_DISEMBARK1);
999  } else if (is_action_enabled_unit_on_tile(ACTION_TRANSPORT_DISEMBARK2,
1000  punit, ptile, nullptr)) {
1001  // "Transport Disembark 2".
1002  paction = action_by_number(ACTION_TRANSPORT_DISEMBARK2);
1003  } else {
1004  // Other move.
1005  paction = nullptr;
1006  }
1007 
1008  // Try not to end move next to an enemy if we can avoid it by waiting
1009  if (paction == nullptr // Regular move
1010  || action_has_result(paction, ACTRES_TRANSPORT_DISEMBARK)) {
1011  // The unit will have to move it self rather than being moved.
1012  int mcost = map_move_cost_unit(&(wld.map), punit, ptile);
1013 
1014  if (paction) {
1015  struct tile *from_tile;
1016 
1017  /* Ugly hack to understand the OnNativeTile unit state requirements
1018  * used in the Action_Success_Actor_Move_Cost effect. */
1019  fc_assert(
1021  from_tile = unit_tile(punit);
1022  punit->tile = ptile;
1023 
1024  mcost += unit_pays_mp_for_action(paction, punit);
1025 
1026  punit->tile = from_tile;
1027  }
1028 
1029  if (punit->moves_left <= mcost && unit_move_rate(punit) > mcost
1030  && adv_danger_at(punit, ptile)
1031  && !adv_danger_at(punit, unit_tile(punit))) {
1032  UNIT_LOG(LOG_DEBUG, punit, "ending move early to stay out of trouble");
1033  return false;
1034  }
1035  }
1036 
1037  // go
1038  unit_activity_handling(punit, ACTIVITY_IDLE);
1039  // Move
1040  if (paction && ptrans
1041  && action_has_result(paction, ACTRES_TRANSPORT_EMBARK)) {
1042  // "Transport Embark".
1043  unit_do_action(unit_owner(punit), punit->id, ptrans->id, 0, "",
1044  action_number(paction));
1045  } else if (paction
1046  && (action_has_result(paction, ACTRES_TRANSPORT_DISEMBARK))) {
1047  // "Transport Disembark" or "Transport Disembark 2".
1048  unit_do_action(unit_owner(punit), punit->id, tile_index(ptile), 0, "",
1049  action_number(paction));
1050  } else {
1051  // Other move.
1052  (void) unit_move_handling(punit, ptile, false, true);
1053  }
1054 
1055  // handle the results
1056  if (game_unit_by_number(sanity) && same_pos(ptile, unit_tile(punit))) {
1057  bodyguard = aiguard_guard_of(ait, punit);
1058 
1059  if (is_plr_ai && bodyguard != nullptr
1060  && def_ai_unit_data(bodyguard, ait)->charge == punit->id) {
1061  dai_unit_bodyguard_move(ait, bodyguard, ptile);
1062  }
1063  return true;
1064  }
1065  return false;
1066 }
1067 
1072 int stack_cost(struct unit *pattacker, struct unit *pdefender)
1073 {
1074  struct tile *ptile = unit_tile(pdefender);
1075  int victim_cost = 0;
1076 
1077  if (is_stack_vulnerable(ptile)) {
1078  // lotsa people die
1079  unit_list_iterate(ptile->units, aunit)
1080  {
1081  if (unit_attack_unit_at_tile_result(pattacker, aunit, ptile)
1082  == ATT_OK) {
1083  victim_cost += unit_build_shield_cost_base(aunit);
1084  }
1085  }
1087  } else if (unit_attack_unit_at_tile_result(pattacker, pdefender, ptile)
1088  == ATT_OK) {
1089  // Only one unit dies if attack is successful
1090  victim_cost = unit_build_shield_cost_base(pdefender);
1091  }
1092 
1093  return victim_cost;
1094 }
1095 
1099 void dai_government_change(struct player *pplayer, struct government *gov)
1100 {
1101  if (gov == government_of_player(pplayer)) {
1102  return;
1103  }
1104 
1106 
1107  city_list_iterate(pplayer->cities, pcity)
1108  {
1109  auto_arrange_workers(pcity); // update cities
1110  }
1112 }
1113 
1120 int dai_gold_reserve(struct player *pplayer)
1121 {
1122  int i = total_player_citizens(pplayer) * 2;
1123 
1124  return MAX(pplayer->ai_common.maxbuycost, i);
1125 }
1126 
1131 bool dai_choose_role_unit(struct ai_type *ait, struct player *pplayer,
1132  struct city *pcity, struct adv_choice *choice,
1133  enum choice_type type, int role, int want,
1134  bool need_boat)
1135 {
1136  struct unit_type *iunit =
1137  dai_wants_role_unit(ait, pplayer, pcity, role, want);
1138 
1139  if (iunit != nullptr) {
1140  choice->type = type;
1141  choice->value.utype = iunit;
1142  choice->want = want;
1143 
1144  choice->need_boat = need_boat;
1145 
1146  return true;
1147  }
1148 
1149  return false;
1150 }
1151 
1155 void dai_build_adv_override(struct ai_type *ait, struct city *pcity,
1156  struct adv_choice *choice)
1157 {
1158  const struct impr_type *chosen;
1159  int want;
1160 
1161  if (choice->type == CT_NONE) {
1162  want = 0;
1163  chosen = nullptr;
1164  } else {
1165  want = choice->want;
1166  chosen = choice->value.building;
1167  }
1168 
1169  improvement_iterate(pimprove)
1170  {
1171  // Advisor code did not consider wonders, let's do it here
1172  if (is_wonder(pimprove)) {
1173  if (pcity->server.adv->building_want[improvement_index(pimprove)]
1174  > want
1175  && can_city_build_improvement_now(pcity, pimprove)) {
1176  want = pcity->server.adv->building_want[improvement_index(pimprove)];
1177  chosen = pimprove;
1178  }
1179  }
1180  }
1182 
1183  choice->want = want;
1184  choice->value.building = chosen;
1185 
1186  if (chosen) {
1187  choice->type = CT_BUILDING; // In case advisor had not chosen anything
1188 
1189  CITY_LOG(LOG_DEBUG, pcity, "ai wants most to build %s at %d",
1190  improvement_rule_name(chosen), want);
1191  }
1192 }
1193 
1204 {
1205  int free_unhappy = get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL);
1206  int unhap = 0;
1207 
1208  // bail out now if happy_cost is 0
1209  if (get_player_bonus(city_owner(pcity), EFT_UNHAPPY_FACTOR) == 0) {
1210  return false;
1211  }
1212 
1213  unit_list_iterate(pcity->units_supported, punit)
1214  {
1215  int happy_cost = city_unit_unhappiness(punit, &free_unhappy);
1216 
1217  if (happy_cost > 0) {
1218  unhap += happy_cost;
1219  }
1220  }
1222 
1223  if (unhap < 0) {
1224  unhap = 0;
1225  }
1226  return (unhap > 0);
1227 }
bool is_action_enabled_unit_on_unit(const action_id wanted_action, const struct unit *actor_unit, const struct unit *target_unit)
Returns TRUE if actor_unit can do wanted_action to target_unit as far as action enablers are concerne...
Definition: actions.cpp:4007
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
int action_number(const struct action *action)
Get the universal number of the action.
Definition: actions.cpp:1338
bool is_action_enabled_unit_on_units(const action_id wanted_action, const struct unit *actor_unit, const struct tile *target_tile)
Returns TRUE if actor_unit can do wanted_action to all units on the target_tile as far as action enab...
Definition: actions.cpp:4078
bool is_action_enabled_unit_on_tile(const action_id wanted_action, const struct unit *actor_unit, const struct tile *target_tile, const struct extra_type *target_extra)
Returns TRUE if actor_unit can do wanted_action to the target_tile as far as action enablers are conc...
Definition: actions.cpp:4136
struct action * action_by_number(action_id act_id)
Return the action with the given id.
Definition: actions.cpp:1149
bool action_has_result(const struct action *paction, enum action_result result)
Returns TRUE iff performing the specified action has the specified result.
Definition: actions.cpp:1248
choice_type
Definition: advchoice.h:22
@ 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
bool adv_danger_at(struct unit *punit, struct tile *ptile)
Are there dangerous enemies at or adjacent to the tile 'ptile'?
Definition: advgoto.cpp:317
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
bool adv_follow_path(struct unit *punit, const PFPath &path, struct tile *ptile)
Move a unit along a path without disturbing its activity, role or assigned destination Return FALSE i...
Definition: advgoto.cpp:43
void adv_avoid_risks(struct pf_parameter *parameter, struct adv_risk_cost *risk_cost, struct unit *punit, const double fearfulness)
Set PF callbacks to favour paths that do not create tall stacks or cross dangerous tiles.
Definition: advgoto.cpp:490
#define NORMAL_STACKING_FEARFULNESS
Definition: advgoto.h:20
adv_want amortize(adv_want benefit, int delay)
Amortize means gradually paying off a cost or debt over time.
Definition: advtools.cpp:25
#define POWER_DIVIDER
Definition: advtools.h:25
struct ai_dip_intel * dai_diplomacy_get(struct ai_type *ait, const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: aidata.cpp:397
bool aiferry_gobyboat(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile, bool with_bodyguard)
This function is to be called if punit needs to use a boat to get to the destination.
Definition: aiferry.cpp:759
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Should unit be considered a ferry?
Definition: aiferry.cpp:157
void aiferry_clear_boat(struct ai_type *ait, struct unit *punit)
Use on a unit which no longer needs a boat.
Definition: aiferry.cpp:248
struct unit * aiguard_charge_unit(struct ai_type *ait, struct unit *guard)
Which unit (if any) has a guard been assigned to? Returns nullptr if the unit is not the guard for a ...
Definition: aiguard.cpp:270
void aiguard_clear_charge(struct ai_type *ait, struct unit *guard)
Remove the assignment of a charge to a guard.
Definition: aiguard.cpp:109
void aiguard_request_guard(struct ai_type *ait, struct unit *punit)
Request a (new) bodyguard for the unit.
Definition: aiguard.cpp:218
void aiguard_assign_guard_city(struct ai_type *ait, struct city *charge, struct unit *guard)
Assign a guard to a city.
Definition: aiguard.cpp:188
void aiguard_clear_guard(struct ai_type *ait, struct unit *charge)
Remove assignment of bodyguard for a unit.
Definition: aiguard.cpp:138
struct unit * aiguard_guard_of(struct ai_type *ait, struct unit *charge)
Which unit, if any, is the body guard of a unit? Returns nullptr if the unit has not been assigned a ...
Definition: aiguard.cpp:260
#define CHECK_CHARGE_UNIT(ait, charge)
Definition: aiguard.h:17
#define CHECK_GUARD(ait, guard)
Definition: aiguard.h:16
#define BODYGUARD_LOG(ait, loglevel, punit, msg,...)
Definition: ailog.h:65
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition: aiplayer.h:41
struct unit_type * dai_wants_role_unit(struct ai_type *ait, struct player *pplayer, struct city *pcity, int role, int want)
Returns the best unit we can build, or nullptr if none.
Definition: aitech.cpp:518
const char * dai_choice_rule_name(const struct adv_choice *choice)
Return the (untranslated) rule name of the adv_choice.
Definition: aitools.cpp:105
void dai_government_change(struct player *pplayer, struct government *gov)
Change government, pretty fast...
Definition: aitools.cpp:1099
void dai_fill_unit_param(struct ai_type *ait, struct pf_parameter *parameter, struct adv_risk_cost *risk_cost, struct unit *punit, struct tile *ptile)
Set up the constraints on a path for an AI unit.
Definition: aitools.cpp:475
int stack_cost(struct unit *pattacker, struct unit *pdefender)
Calculate the value of the target unit including the other units which will die in a successful attac...
Definition: aitools.cpp:1072
bool goto_is_sane(struct unit *punit, struct tile *ptile)
Use pathfinding to determine whether a GOTO is possible, considering all aspects of the unit being mo...
Definition: aitools.cpp:436
void dai_log_path(struct unit *punit, const PFPath &path, struct pf_parameter *parameter)
Log the cost of travelling a path.
Definition: aitools.cpp:359
#define LONG_TIME
Definition: aitools.cpp:462
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
static bool dai_gothere_bodyguard(struct ai_type *ait, struct unit *punit, struct tile *dest_tile)
A helper function for ai_gothere.
Definition: aitools.cpp:175
void dai_build_adv_override(struct ai_type *ait, struct city *pcity, struct adv_choice *choice)
Consider overriding building target selected by common advisor code.
Definition: aitools.cpp:1155
bool dai_unit_attack(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Move and attack with an ai unit.
Definition: aitools.cpp:825
const char * dai_unit_task_rule_name(const enum ai_unit_task task)
Return the (untranslated) rule name of the ai_unit_task.
Definition: aitools.cpp:70
bool dai_unit_goto(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Go to specified destination but do not disturb existing role or activity and do not clear the role's ...
Definition: aitools.cpp:609
bool dai_unit_goto_constrained(struct ai_type *ait, struct unit *punit, struct tile *ptile, struct pf_parameter *parameter)
Go to specified destination, subject to given PF constraints, but do not disturb existing role or act...
Definition: aitools.cpp:381
struct tile * immediate_destination(struct unit *punit, struct tile *dest_tile)
Returns the destination for a unit moving towards a given final destination.
Definition: aitools.cpp:314
void dai_unit_move_or_attack(struct ai_type *ait, struct unit *punit, struct tile *ptile, const PFPath &path, int step)
Ai unit moving function called from AI interface.
Definition: aitools.cpp:928
void dai_consider_plr_dangerous(struct ai_type *ait, struct player *plr1, struct player *plr2, enum override_bool *result)
There are some signs that a player might be dangerous: We are at war with him, he has done lots of ig...
Definition: aitools.cpp:156
void dai_unit_new_task(struct ai_type *ait, struct unit *punit, enum ai_unit_task task, struct tile *ptile)
Ensure unit sanity by telling charge that we won't bodyguard it anymore, tell bodyguard it can roam f...
Definition: aitools.cpp:648
int dai_gold_reserve(struct player *pplayer)
Credits the AI wants to have in reserves.
Definition: aitools.cpp:1120
bool dai_unit_make_homecity(struct unit *punit, struct city *pcity)
Try to make pcity our new homecity.
Definition: aitools.cpp:763
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
static void dai_unit_bodyguard_move(struct ai_type *ait, struct unit *bodyguard, struct tile *ptile)
Move a bodyguard along with another unit.
Definition: aitools.cpp:792
#define LOGLEVEL_GOTHERE
Definition: aitools.cpp:248
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
bool dai_gothere(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile)
This is ferry-enabled goto.
Definition: aitools.cpp:258
bool dai_unit_move(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Move a unit.
Definition: aitools.cpp:947
void dai_unit_new_adv_task(struct ai_type *ait, struct unit *punit, enum adv_unit_task task, struct tile *ptile)
Adviser task for unit has been changed.
Definition: aitools.cpp:624
#define POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer)
Definition: aiunit.h:77
ai_unit_task
Definition: aiunit.h:25
@ AIUNIT_BUILD_CITY
Definition: aiunit.h:28
@ AIUNIT_NONE
Definition: aiunit.h:26
@ AIUNIT_ATTACK
Definition: aiunit.h:30
@ AIUNIT_EXPLORE
Definition: aiunit.h:32
@ AIUNIT_HUNTER
Definition: aiunit.h:34
@ AIUNIT_RECOVER
Definition: aiunit.h:33
@ AIUNIT_TRADE
Definition: aiunit.h:35
@ AIUNIT_DEFEND_HOME
Definition: aiunit.h:29
@ AIUNIT_ESCORT
Definition: aiunit.h:31
@ AIUNIT_AUTO_SETTLER
Definition: aiunit.h:27
@ AIUNIT_WONDER
Definition: aiunit.h:36
#define BV_SET(bv, bit)
Definition: bitvector.h:44
#define BV_CLR(bv, bit)
Definition: bitvector.h:49
struct city * is_enemy_city_tile(const struct tile *ptile, const struct player *pplayer)
Is there an enemy city on this tile?
Definition: city.cpp:1923
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
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
int city_unit_unhappiness(const unit *punit, int *free_unhappy)
Query unhappiness caused by a given unit.
Definition: city.cpp:2921
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define city_list_iterate_end
Definition: city.h:484
void citymap_free_city_spot(struct tile *ptile, int id)
Reverse any reservations we have made in the surrounding area.
Definition: citymap.cpp:141
void citymap_reserve_city_spot(struct tile *ptile, int id)
This function reserves a single tile for a (possibly virtual) city with a settler's or a city's id.
Definition: citymap.cpp:113
void auto_arrange_workers(struct city *pcity)
Call sync_cities() to send the affected cities to the clients.
Definition: cityturn.cpp:359
int length() const
enum unit_attack_result unit_attack_unit_at_tile_result(const struct unit *punit, const struct unit *pdefender, const struct tile *dest_tile)
Checks if a unit can physically attack pdefender at the tile (assuming it is adjacent and at war).
Definition: combat.cpp:121
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
#define POWER_FACTOR
Definition: combat.h:23
@ ATT_OK
Definition: combat.h:26
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
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
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Returns the effect bonus for a player.
Definition: effects.cpp:673
bool hut_on_tile(const struct tile *ptile)
Returns TRUE iff an extra on the tile is a hut (removed by entering).
Definition: extras.cpp:626
adv_unit_task
Definition: fc_types.h:287
@ AUT_BUILD_CITY
Definition: fc_types.h:287
@ AUT_NONE
Definition: fc_types.h:287
@ AUT_AUTO_SETTLER
Definition: fc_types.h:287
@ O_SHIELD
Definition: fc_types.h:86
@ O_FOOD
Definition: fc_types.h:85
override_bool
Definition: fc_types.h:78
@ OVERRIDE_TRUE
Definition: fc_types.h:78
struct unit * game_unit_by_number(int id)
Find unit out of all units in game: now uses fast idex method, instead of looking through all units o...
Definition: game.cpp:112
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
Government_type_id government_number(const struct government *pgovern)
Return the government index.
Definition: government.cpp:84
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
const char * improvement_rule_name(const struct impr_type *pimprove)
Return the (untranslated) rule name of the improvement.
Impr_type_id improvement_index(const struct impr_type *pimprove)
Return the improvement index.
bool is_wonder(const struct impr_type *pimprove)
Returns whether improvement is some kind of wonder.
#define improvement_iterate_end
Definition: improvement.h:199
#define improvement_iterate(_p)
Definition: improvement.h:193
constexpr auto LOG_DEBUG
Definition: log.h:27
#define fc_assert_ret(condition)
Definition: log.h:112
#define fc_assert(condition)
Definition: log.h:89
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition: log.h:132
bool is_tiles_adjacent(const struct tile *tile0, const struct tile *tile1)
Are two tiles adjacent to each other.
Definition: map.cpp:878
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Are (x1,y1) and (x2,y2) really the same when adjusted? This function might be necessary ALOT of place...
Definition: map.cpp:887
int map_distance(const struct tile *tile0, const struct tile *tile1)
Return Manhattan distance between two tiles.
Definition: map.cpp:623
static int map_move_cost_unit(const struct civ_map *nmap, struct unit *punit, const struct tile *ptile)
Definition: map.h:221
int unit_move_rate(const struct unit *punit)
This function calculates the move rate of the unit.
Definition: movement.cpp:78
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Return TRUE iff the unit can "survive" at this location.
Definition: movement.cpp:452
#define SINGLE_MOVE
Definition: movement.h:17
const char * nation_rule_name(const struct nation_type *pnation)
Return the (untranslated) rule name of the nation (adjective form).
Definition: nation.cpp:115
struct nation_type * nation_of_unit(const struct unit *punit)
Return the nation of the player who owns the unit.
Definition: nation.cpp:438
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
Factory function to create a new map according to the parameter.
PFPath pf_map_path(struct pf_map *pfm, struct tile *ptile)
CHECK DOCS AFTER FULL CONVERSTION OF pf_path to class PFPath Tries to find the best path in the given...
void pf_map_destroy(struct pf_map *pfm)
After usage the map must be destroyed.
int pf_map_move_cost(struct pf_map *pfm, struct tile *ptile)
Tries to find the minimal move cost to reach ptile.
#define PF_IMPOSSIBLE_MC
Definition: path_finding.h:244
#define PF_TURN_FACTOR
Definition: path_finding.h:250
enum tile_behavior no_fights(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
PF callback to prohibit attacking anyone.
Definition: pf_tools.cpp:484
void pft_fill_unit_overlap_param(struct pf_parameter *parameter, const struct unit *punit)
Switch on one tile overlapping into the non-native terrain.
Definition: pf_tools.cpp:871
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct unit *punit)
Fill classic parameters for an unit.
Definition: pf_tools.cpp:822
enum tile_behavior no_intermediate_fights(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
PF callback to prohibit attacking anyone, except at the destination.
Definition: pf_tools.cpp:499
void pft_fill_unit_attack_param(struct pf_parameter *parameter, const struct unit *punit)
pft_fill_*_attack_param() base function.
Definition: pf_tools.cpp:913
int player_index(const struct player *pplayer)
Return the player index.
Definition: player.cpp:748
static bool is_barbarian(const struct player *pplayer)
Definition: player.h:474
#define is_ai(plr)
Definition: player.h:227
void handle_player_change_government(struct player *pplayer, Government_type_id government)
Called by the client or AI to change government.
Definition: plrhand.cpp:427
int total_player_citizens(const struct player *pplayer)
Return the total number of citizens in the player's nation.
Definition: score.cpp:408
#define MAX(x, y)
Definition: shared.h:48
int step
Definition: specpq.h:83
#define LOGLEVEL_HUNT
Definition: srv_log.h:37
#define UNIT_LOG(_, punit, msg,...)
Definition: srv_log.h:95
#define CITY_LOG(_, pcity, msg,...)
Definition: srv_log.h:80
#define LOGLEVEL_BODYGUARD
Definition: srv_log.h:32
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 average_production
Definition: advdata.h:88
struct adv_data::@87 stats
int countdown
Definition: aidata.h:54
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
struct unit_list * units_supported
Definition: city.h:377
enum tile_behavior(* get_TB)(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
Definition: path_finding.h:380
int(* get_moves_left_req)(const struct tile *ptile, enum known_type, const struct pf_parameter *param)
Definition: path_finding.h:423
bool(* get_zoc)(const struct player *pplayer, const struct tile *ptile, const struct civ_map *zmap)
Definition: path_finding.h:412
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
Definition: tile.h:42
struct unit_list * units
Definition: tile.h:50
Definition: aiunit.h:39
enum ai_unit_task task
Definition: aiunit.h:53
int charge
Definition: aiunit.h:44
int target
Definition: aiunit.h:49
int defense_strength
Definition: unittype.h:480
int move_rate
Definition: unittype.h:481
int hp
Definition: unittype.h:489
Definition: unit.h:134
int upkeep[O_LAST]
Definition: unit.h:145
enum unit_activity activity
Definition: unit.h:154
int moves_left
Definition: unit.h:147
int id
Definition: unit.h:141
int hp
Definition: unit.h:148
struct unit::@76::@79 server
struct tile * tile
Definition: unit.h:136
int homecity
Definition: unit.h:142
struct tile * goto_tile
Definition: unit.h:152
int veteran
Definition: unit.h:149
int power_fact
Definition: unittype.h:453
struct civ_map map
Definition: world_object.h:21
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_index(_pt_)
Definition: tile.h:70
#define TILE_XY(ptile)
Definition: tile.h:36
const struct unit_type * utype
Definition: fc_types.h:585
const struct impr_type * building
Definition: fc_types.h:579
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Returns the amount of movement points successfully performing the specified action will consume in th...
Definition: unit.cpp:1973
bool is_losing_hp(const struct unit *punit)
Does unit lose hitpoints each turn?
Definition: unit.cpp:1992
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Return TRUE iff the given unit can be loaded into the transporter.
Definition: unit.cpp:693
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Find the best transporter at the given location for the unit.
Definition: unit.cpp:1789
bool unit_is_cityfounder(const struct unit *punit)
Is a cityfounder unit?
Definition: unit.cpp:2389
bool unit_transported(const struct unit *pcargo)
Returns TRUE iff the unit is transported.
Definition: unit.cpp:2176
bool unit_has_orders(const struct unit *punit)
Return TRUE iff the unit is following client-side orders.
Definition: unit.cpp:195
struct unit * is_enemy_unit_tile(const struct tile *ptile, const struct player *pplayer)
Is there an enemy unit on this tile? Returns the unit or nullptr if none.
Definition: unit.cpp:1235
#define unit_tile(_pu)
Definition: unit.h:371
#define CHECK_UNIT(punit)
Definition: unit.h:264
#define unit_owner(_pu)
Definition: unit.h:370
bool unit_move_handling(struct unit *punit, struct tile *pdesttile, bool igzoc, bool move_do_not_act)
Will try to move to/attack the tile dest_x,dest_y.
Definition: unithand.cpp:4654
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Handle request for changing activity.
Definition: unithand.cpp:5485
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_end
Definition: unitlist.h:27
void send_unit_info(struct conn_list *dest, struct unit *punit)
Send the unit to the players who need the info.
Definition: unittools.cpp:2808
void unit_transport_load_send(struct unit *punit, struct unit *ptrans)
Put the unit onto the transporter, and tell everyone.
Definition: unittools.cpp:3261
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
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Returns TRUE iff successfully performing the specified action always will move the actor unit of the ...
Definition: unittype.cpp:969
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_may_act_at_all(const struct unit_type *putype)
Return TRUE iff units of this type can do actions controlled by generalized (ruleset defined) action ...
Definition: unittype.cpp:374
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 utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Return TRUE iff units of the given type can do any enabler controlled action with the specified actio...
Definition: unittype.cpp:402
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
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
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Return veteran level properties of given unit in given veterancy level.
Definition: unittype.cpp:2224
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition: unittype.h:704
#define utype_fuel(ptype)
Definition: unittype.h:772