Freeciv21
Develop your civilization from humble roots to a global empire
unit.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 "fcintl.h"
17 #include "shared.h"
18 #include "support.h"
19 
20 // common
21 #include "actions.h"
22 #include "ai.h"
23 #include "city.h"
24 #include "game.h"
25 #include "log.h"
26 #include "map.h"
27 #include "movement.h"
28 #include "packets.h"
29 #include "player.h"
30 #include "unitlist.h"
31 
32 #include "unit.h"
33 
34 static bool is_real_activity(enum unit_activity activity);
35 
37 
39  ACTIVITY_PILLAGE, ACTIVITY_GEN_ROAD, ACTIVITY_IRRIGATE,
40  ACTIVITY_MINE, ACTIVITY_BASE, ACTIVITY_CULTIVATE,
41  ACTIVITY_PLANT, ACTIVITY_TRANSFORM, ACTIVITY_POLLUTION,
42  ACTIVITY_FALLOUT, ACTIVITY_LAST};
43 
44 struct cargo_iter {
45  struct iterator vtable;
46  const struct unit_list_link *links[GAME_TRANSPORT_MAX_RECURSIVE];
47  int depth;
48 };
49 #define CARGO_ITER(iter) ((struct cargo_iter *) (iter))
50 
54 bool are_unit_orders_equal(const struct unit_order *order1,
55  const struct unit_order *order2)
56 {
57  return order1->order == order2->order
58  && order1->activity == order2->activity
59  && order1->target == order2->target
60  && order1->sub_target == order2->sub_target
61  && order1->action == order2->action && order1->dir == order2->dir;
62 }
63 
75 test_unit_can_airlift_to(const struct player *restriction,
76  const struct unit *punit,
77  const struct city *pdest_city)
78 {
79  const struct city *psrc_city = tile_city(unit_tile(punit));
80  const struct player *punit_owner;
81  enum unit_airlift_result ok_result = AR_OK;
82 
83  if (0 == punit->moves_left
84  && !utype_may_act_move_frags(unit_type_get(punit), ACTION_AIRLIFT,
85  0)) {
86  // No moves left.
87  return AR_NO_MOVES;
88  }
89 
90  if (!unit_can_do_action(punit, ACTION_AIRLIFT)) {
91  return AR_WRONG_UNITTYPE;
92  }
93 
94  if (0 < get_transporter_occupancy(punit)) {
95  // Units with occupants can't be airlifted currently.
96  return AR_OCCUPIED;
97  }
98 
99  if (nullptr == psrc_city) {
100  // No city there.
101  return AR_NOT_IN_CITY;
102  }
103 
104  if (psrc_city == pdest_city) {
105  // Airlifting to our current position doesn't make sense.
106  return AR_BAD_DST_CITY;
107  }
108 
109  if (pdest_city
110  && (nullptr == restriction
111  || (tile_get_known(city_tile(pdest_city), restriction)
112  == TILE_KNOWN_SEEN))
113  && !can_unit_exist_at_tile(&(wld.map), punit, city_tile(pdest_city))) {
114  // Can't exist at the destination tile.
115  return AR_BAD_DST_CITY;
116  }
117 
118  punit_owner = unit_owner(punit);
119 
120  /* Check validity of both source and destination before checking capacity,
121  * to avoid misleadingly optimistic returns. */
122 
123  if (punit_owner != city_owner(psrc_city)
124  && !(game.info.airlifting_style & AIRLIFTING_ALLIED_SRC
125  && pplayers_allied(punit_owner, city_owner(psrc_city)))) {
126  // Not allowed to airlift from this source.
127  return AR_BAD_SRC_CITY;
128  }
129 
130  if (pdest_city && punit_owner != city_owner(pdest_city)
131  && !(game.info.airlifting_style & AIRLIFTING_ALLIED_DEST
132  && pplayers_allied(punit_owner, city_owner(pdest_city)))) {
133  // Not allowed to airlift to this destination.
134  return AR_BAD_DST_CITY;
135  }
136 
137  if (nullptr == restriction || city_owner(psrc_city) == restriction) {
138  // We know for sure whether or not src can airlift this turn.
139  if (0 >= psrc_city->airlift) {
140  /* The source cannot airlift for this turn (maybe already airlifted
141  * or no airport).
142  *
143  * Note that (game.info.airlifting_style & AIRLIFTING_UNLIMITED_SRC)
144  * is not handled here because it applies only when the source city
145  * has at least one remaining airlift.
146  * See also do_airline() in server/unittools.h. */
147  return AR_SRC_NO_FLIGHTS;
148  } // else, there is capacity; continue to other checks
149  } else {
150  /* We don't have access to the 'airlift' field. Assume it's OK; can
151  * only find out for sure by trying it. */
152  ok_result = AR_OK_SRC_UNKNOWN;
153  }
154 
155  if (pdest_city) {
156  if (nullptr == restriction || city_owner(pdest_city) == restriction) {
157  if (0 >= pdest_city->airlift
158  && !(game.info.airlifting_style & AIRLIFTING_UNLIMITED_DEST)) {
159  /* The destination cannot support airlifted units for this turn
160  * (maybe already airlifed or no airport).
161  * See also do_airline() in server/unittools.h. */
162  return AR_DST_NO_FLIGHTS;
163  } // else continue
164  } else {
165  ok_result = AR_OK_DST_UNKNOWN;
166  }
167  }
168 
169  return ok_result;
170 }
171 
179 bool unit_can_airlift_to(const struct unit *punit,
180  const struct city *pdest_city)
181 {
182  fc_assert_ret_val(pdest_city, false);
183 
184  if (is_server()) {
185  return is_action_enabled_unit_on_city(ACTION_AIRLIFT, punit, pdest_city);
186  } else {
187  return action_prob_possible(
188  action_prob_vs_city(punit, ACTION_AIRLIFT, pdest_city));
189  }
190 }
191 
195 bool unit_has_orders(const struct unit *punit) { return punit->has_orders; }
196 
204 int unit_shield_value(const struct unit *punit,
205  const struct unit_type *punittype,
206  const struct action *paction)
207 {
208  int value;
209 
210  bool has_unit;
211  const struct player *act_player;
212 
213  has_unit = punit != nullptr;
214 
215  if (has_unit && punittype == nullptr) {
216  punittype = unit_type_get(punit);
217  }
218 
219  fc_assert_ret_val(punittype != nullptr, 0);
220  fc_assert(punit == nullptr || unit_type_get(punit) == punittype);
221  fc_assert_ret_val(paction != nullptr, 0);
222 
223  act_player = has_unit ? unit_owner(punit) : nullptr;
224  /* TODO: determine if tile and city should be where the unit currently is
225  * located or the target city. Those two may differ. Wait for ruleset
226  * author feed back. */
227 
228  value = utype_build_shield_cost_base(punittype);
229  value +=
230  ((value
232  nullptr, act_player, nullptr, nullptr, nullptr, nullptr, punit,
233  punittype, nullptr, nullptr, paction, EFT_UNIT_SHIELD_VALUE_PCT))
234  / 100);
235 
236  return value;
237 }
238 
243 bool unit_can_help_build_wonder_here(const struct unit *punit)
244 {
245  struct city *pcity = tile_city(unit_tile(punit));
246 
247  if (!pcity) {
248  // No city to help at this tile.
249  return false;
250  }
251 
252  if (!utype_can_do_action(unit_type_get(punit), ACTION_HELP_WONDER)) {
253  // This unit can never do help wonder.
254  return false;
255  }
256 
257  // Evaluate all action enablers for extra accuracy.
258  // TODO: Is it worth it?
259  return action_prob_possible(
260  action_prob_vs_city(punit, ACTION_HELP_WONDER, pcity));
261 }
262 
267 bool unit_can_est_trade_route_here(const struct unit *punit)
268 {
269  struct city *phomecity, *pdestcity;
270 
271  return (utype_can_do_action(unit_type_get(punit), ACTION_TRADE_ROUTE)
272  && (pdestcity = tile_city(unit_tile(punit)))
273  && (phomecity = game_city_by_number(punit->homecity))
274  && can_cities_trade(phomecity, pdestcity));
275 }
276 
280 int get_transporter_capacity(const struct unit *punit)
281 {
282  return unit_type_get(punit)->transport_capacity;
283 }
284 
288 bool is_attack_unit(const struct unit *punit)
289 {
290  return ((unit_can_do_action_result(punit, ACTRES_ATTACK)
291  || unit_can_do_action_result(punit, ACTRES_BOMBARD))
292  && unit_type_get(punit)->attack_strength > 0);
293 }
294 
300 bool is_military_unit(const struct unit *punit)
301 {
302  return !unit_has_type_flag(punit, UTYF_CIVILIAN);
303 }
304 
309 bool unit_can_do_action(const struct unit *punit, const action_id act_id)
310 {
311  return utype_can_do_action(unit_type_get(punit), act_id);
312 }
313 
318 bool unit_can_do_action_result(const struct unit *punit,
319  enum action_result result)
320 {
321  return utype_can_do_action_result(unit_type_get(punit), result);
322 }
323 
327 bool is_square_threatened(const struct player *pplayer,
328  const struct tile *ptile, bool omniscient)
329 {
330  square_iterate(&(wld.map), ptile, 2, ptile1)
331  {
332  unit_list_iterate(ptile1->units, punit)
333  {
334  if ((omniscient || can_player_see_unit(pplayer, punit))
335  && pplayers_at_war(pplayer, unit_owner(punit))
337  && (is_native_tile(unit_type_get(punit), ptile)
340  ptile)))) {
341  return true;
342  }
343  }
345  }
347 
348  return false;
349 }
350 
355 bool is_field_unit(const struct unit *punit)
356 {
357  return unit_has_type_flag(punit, UTYF_FIELDUNIT);
358 }
359 
367 bool is_hiding_unit(const struct unit *punit)
368 {
369  enum vision_layer vl = unit_type_get(punit)->vlayer;
370 
371  if (vl == V_INVIS || vl == V_SUBSURFACE) {
372  return true;
373  }
374 
375  if (unit_transported(punit)) {
377  if (vl == V_INVIS || vl == V_SUBSURFACE) {
378  return true;
379  }
380  }
381 
382  return false;
383 }
384 
389 bool kills_citizen_after_attack(const struct unit *punit)
390 {
391  return game.info.killcitizen
392  && uclass_has_flag(unit_class_get(punit), UCF_KILLCITIZEN);
393 }
394 
399 bool unit_can_add_or_build_city(const struct unit *punit)
400 {
401  struct city *tgt_city;
402 
403  if ((tgt_city = tile_city(unit_tile(punit)))) {
404  return action_prob_possible(
405  action_prob_vs_city(punit, ACTION_JOIN_CITY, tgt_city));
406  } else {
408  punit, ACTION_FOUND_CITY, unit_tile(punit), nullptr));
409  }
410 }
411 
415 bool can_unit_change_homecity_to(const struct unit *punit,
416  const struct city *pcity)
417 {
418  if (pcity == nullptr) {
419  // Can't change home city to a non existing city.
420  return false;
421  }
422 
423  return action_prob_possible(
424  action_prob_vs_city(punit, ACTION_HOME_CITY, pcity));
425 }
426 
430 bool can_unit_change_homecity(const struct unit *punit)
431 {
432  return can_unit_change_homecity_to(punit, tile_city(unit_tile(punit)));
433 }
434 
443 int get_activity_rate(const struct unit *punit)
444 {
445  const struct veteran_level *vlevel;
446 
447  fc_assert_ret_val(punit != nullptr, 0);
448 
449  vlevel = utype_veteran_level(unit_type_get(punit), punit->veteran);
450  fc_assert_ret_val(vlevel != nullptr, 0);
451 
452  /* The speed of the settler depends on its base move_rate, not on
453  * the number of moves actually remaining or the adjusted move rate.
454  * This means sea formers won't have their activity rate increased by
455  * Magellan's, and it means injured units work just as fast as
456  * uninjured ones. Note the value is never less than SINGLE_MOVE. */
457  int move_rate = unit_type_get(punit)->move_rate;
458 
459  // All settler actions are multiplied by ACTIVITY_FACTOR.
460  return ACTIVITY_FACTOR * static_cast<float>(vlevel->power_fact) / 100
461  * move_rate / SINGLE_MOVE;
462 }
463 
470 int get_activity_rate_this_turn(const struct unit *punit)
471 {
472  /* This logic is also coded in client/goto.c. */
473  if (punit->moves_left > 0) {
474  return get_activity_rate(punit);
475  } else {
476  return 0;
477  }
478 }
479 
486 int get_turns_for_activity_at(const struct unit *punit,
487  enum unit_activity activity,
488  const struct tile *ptile,
489  struct extra_type *tgt)
490 {
491  /* FIXME: This is just an approximation since we don't account for
492  * get_activity_rate_this_turn. */
493  int speed = get_activity_rate(punit);
494  int points_needed = tile_activity_time(activity, ptile, tgt);
495 
496  if (points_needed >= 0 && speed > 0) {
497  return (points_needed - 1) / speed + 1; // round up
498  } else {
499  return FC_INFINITY;
500  }
501 }
502 
506 bool activity_requires_target(enum unit_activity activity)
507 {
508  switch (activity) {
509  case ACTIVITY_PILLAGE:
510  case ACTIVITY_BASE:
511  case ACTIVITY_GEN_ROAD:
512  case ACTIVITY_IRRIGATE:
513  case ACTIVITY_MINE:
514  case ACTIVITY_POLLUTION:
515  case ACTIVITY_FALLOUT:
516  return true;
517  case ACTIVITY_IDLE:
518  case ACTIVITY_FORTIFIED:
519  case ACTIVITY_SENTRY:
520  case ACTIVITY_GOTO:
521  case ACTIVITY_EXPLORE:
522  case ACTIVITY_TRANSFORM:
523  case ACTIVITY_CULTIVATE:
524  case ACTIVITY_PLANT:
525  case ACTIVITY_FORTIFYING:
526  case ACTIVITY_CONVERT:
527  return false;
528  // These shouldn't be kicking around internally.
529  case ACTIVITY_FORTRESS:
530  case ACTIVITY_AIRBASE:
531  case ACTIVITY_PATROL_UNUSED:
532  default:
533  fc_assert_ret_val(false, false);
534  }
535 
536  return false;
537 }
538 
548 bool can_unit_do_autosettlers(const struct unit *punit)
549 {
550  return unit_type_get(punit)->adv.worker;
551 }
552 
557 {
558  int i = 0;
559 
560  for (int act = 0; act < ACTIVITY_LAST; act++) {
561  if (is_real_activity(unit_activity(act))) {
562  real_activities[i++] = unit_activity(act);
563  }
564  }
565 
566  real_activities[i] = ACTIVITY_LAST;
567 }
568 
573 static bool is_real_activity(enum unit_activity activity)
574 {
575  /* ACTIVITY_FORTRESS, ACTIVITY_AIRBASE, ACTIVITY_OLD_ROAD, and
576  * ACTIVITY_OLD_RAILROAD are deprecated */
577  return (activity < ACTIVITY_LAST) && activity != ACTIVITY_FORTRESS
578  && activity != ACTIVITY_AIRBASE && activity != ACTIVITY_OLD_ROAD
579  && activity != ACTIVITY_OLD_RAILROAD && activity != ACTIVITY_UNKNOWN
580  && activity != ACTIVITY_PATROL_UNUSED;
581 }
582 
586 const char *get_activity_text(enum unit_activity activity)
587 {
588  /* The switch statement has just the activities listed with no "default"
589  * handling. This enables the compiler to detect missing entries
590  * automatically, and still handles everything correctly. */
591  switch (activity) {
592  case ACTIVITY_IDLE:
593  return _("Idle");
594  case ACTIVITY_POLLUTION:
595  return _("Pollution");
596  case ACTIVITY_MINE:
597  // TRANS: Activity name, verb in English
598  return _("Mine");
599  case ACTIVITY_PLANT:
600  // TRANS: Activity name, verb in English
601  return _("Plant");
602  case ACTIVITY_IRRIGATE:
603  return _("Irrigate");
604  case ACTIVITY_CULTIVATE:
605  return _("Cultivate");
606  case ACTIVITY_FORTIFYING:
607  return _("Fortifying");
608  case ACTIVITY_FORTIFIED:
609  return _("Fortified");
610  case ACTIVITY_SENTRY:
611  return _("Sentry");
612  case ACTIVITY_PILLAGE:
613  return _("Pillage");
614  case ACTIVITY_GOTO:
615  return _("Goto");
616  case ACTIVITY_EXPLORE:
617  return _("Explore");
618  case ACTIVITY_TRANSFORM:
619  return _("Transform");
620  case ACTIVITY_FALLOUT:
621  return _("Fallout");
622  case ACTIVITY_BASE:
623  return _("Base");
624  case ACTIVITY_GEN_ROAD:
625  return _("Road");
626  case ACTIVITY_CONVERT:
627  return _("Convert");
628  case ACTIVITY_OLD_ROAD:
629  case ACTIVITY_OLD_RAILROAD:
630  case ACTIVITY_FORTRESS:
631  case ACTIVITY_AIRBASE:
632  case ACTIVITY_UNKNOWN:
633  case ACTIVITY_PATROL_UNUSED:
634  case ACTIVITY_LAST:
635  break;
636  }
637 
638  fc_assert(false);
639  return _("Unknown");
640 }
641 
646 bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
647 {
648  if (!pcargo || !ptrans || pcargo == ptrans) {
649  return false;
650  }
651 
652  /* Double-check ownership of the units: you can load into an allied unit
653  * (of course only allied units can be on the same tile). */
654  if (!pplayers_allied(unit_owner(pcargo), unit_owner(ptrans))) {
655  return false;
656  }
657 
658  // Make sure this transporter can carry this type of unit.
659  if (!can_unit_transport(ptrans, pcargo)) {
660  return false;
661  }
662 
663  // Un-embarkable transport must be in city or base to load cargo.
664  if (!utype_can_freely_load(unit_type_get(pcargo), unit_type_get(ptrans))
665  && !tile_city(unit_tile(ptrans))
666  && !tile_has_native_base(unit_tile(ptrans), unit_type_get(ptrans))) {
667  return false;
668  }
669 
670  // Make sure there's room in the transporter.
671  if (get_transporter_occupancy(ptrans)
672  >= get_transporter_capacity(ptrans)) {
673  return false;
674  }
675 
676  // Check iff this is a valid transport.
677  if (!unit_transport_check(pcargo, ptrans)) {
678  return false;
679  }
680 
681  // Check transport depth.
683  < 1 + unit_transport_depth(ptrans) + unit_cargo_depth(pcargo)) {
684  return false;
685  }
686 
687  return true;
688 }
689 
693 bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
694 {
695  // This function needs to check EVERYTHING.
696 
697  /* Check positions of the units. Of course you can't load a unit onto
698  * a transporter on a different tile... */
699  if (!same_pos(unit_tile(pcargo), unit_tile(ptrans))) {
700  return false;
701  }
702 
703  // Cannot load if cargo is already loaded onto something else.
704  if (unit_transported(pcargo)) {
705  return false;
706  }
707 
708  return could_unit_load(pcargo, ptrans);
709 }
710 
720 bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
721 {
722  if (!pcargo || !ptrans) {
723  return false;
724  }
725 
726  // Make sure the unit's transporter exists and is known.
727  if (unit_transport_get(pcargo) != ptrans) {
728  return false;
729  }
730 
731  // Un-disembarkable transport must be in city or base to unload cargo.
733  && !tile_city(unit_tile(ptrans))
734  && !tile_has_native_base(unit_tile(ptrans), unit_type_get(ptrans))) {
735  return false;
736  }
737 
738  return true;
739 }
740 
745 bool can_unit_alight_or_be_unloaded(const struct unit *pcargo,
746  const struct unit *ptrans)
747 {
748  if (!pcargo || !ptrans) {
749  return false;
750  }
751 
752  fc_assert_ret_val(unit_transport_get(pcargo) == ptrans, false);
753 
754  if (is_server()) {
755  return (is_action_enabled_unit_on_unit(ACTION_TRANSPORT_ALIGHT, pcargo,
756  ptrans)
757  || is_action_enabled_unit_on_unit(ACTION_TRANSPORT_UNLOAD,
758  ptrans, pcargo));
759  } else {
760  return (action_prob_possible(
761  action_prob_vs_unit(pcargo, ACTION_TRANSPORT_ALIGHT, ptrans))
763  ptrans, ACTION_TRANSPORT_UNLOAD, pcargo)));
764  }
765 }
766 
772 bool can_unit_paradrop(const struct unit *punit)
773 {
774  return action_maybe_possible_actor_unit(ACTION_PARADROP, punit);
775 }
776 
781 {
782  enum unit_activity current = punit->activity;
783  struct extra_type *target = punit->activity_target;
784  enum unit_activity current2 =
785  (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
786  bool result;
787 
788  punit->activity = ACTIVITY_IDLE;
789  punit->activity_target = nullptr;
790 
791  result = can_unit_do_activity_targeted(punit, current2, target);
792 
793  punit->activity = current;
794  punit->activity_target = target;
795 
796  return result;
797 }
798 
806 bool can_unit_do_activity(const struct unit *punit,
807  enum unit_activity activity)
808 {
809  struct extra_type *target = nullptr;
810 
811  /* FIXME: lots of callers (usually client real_menus_update()) rely on
812  * being able to find out whether an activity is in general possible.
813  * Find one for them, but when they come to do the activity, they will
814  * have to determine the target themselves */
815  {
816  struct tile *ptile = unit_tile(punit);
817  struct terrain *pterrain = tile_terrain(ptile);
818 
819  if (activity == ACTIVITY_IRRIGATE
820  && pterrain->irrigation_result == pterrain) {
821  target = next_extra_for_tile(ptile, EC_IRRIGATION, unit_owner(punit),
822  punit);
823  if (nullptr == target) {
824  return false; // No more irrigation extras available.
825  }
826  } else if (activity == ACTIVITY_MINE
827  && pterrain->mining_result == pterrain) {
828  target = next_extra_for_tile(ptile, EC_MINE, unit_owner(punit), punit);
829  if (nullptr == target) {
830  return false; // No more mine extras available.
831  }
832  }
833  }
834 
835  return can_unit_do_activity_targeted(punit, activity, target);
836 }
837 
842 bool can_unit_do_activity_targeted(const struct unit *punit,
843  enum unit_activity activity,
844  struct extra_type *target)
845 {
846  return can_unit_do_activity_targeted_at(punit, activity, target,
847  unit_tile(punit));
848 }
849 
854 bool can_unit_do_activity_targeted_at(const struct unit *punit,
855  enum unit_activity activity,
856  struct extra_type *target,
857  const struct tile *ptile)
858 {
859  /* Check that no build activity conflicting with one already in progress
860  * gets executed. */
861  /* FIXME: Should check also the cases where one of the activities is
862  * terrain change that destroys the target of the other activity */
863  if (target != nullptr && is_build_activity(activity, ptile)) {
864  if (tile_is_placing(ptile)) {
865  return false;
866  }
867 
868  unit_list_iterate(ptile->units, tunit)
869  {
870  if (is_build_activity(tunit->activity, ptile)
871  && !can_extras_coexist(target, tunit->activity_target)) {
872  return false;
873  }
874  }
876  }
877 
878  switch (activity) {
879  case ACTIVITY_IDLE:
880  case ACTIVITY_GOTO:
881  return true;
882 
883  case ACTIVITY_POLLUTION:
884  // The call below doesn't support actor tile speculation.
885  fc_assert_msg(unit_tile(punit) == ptile,
886  "Please use action_speculate_unit_on_tile()");
887  return is_action_enabled_unit_on_tile(ACTION_CLEAN_POLLUTION, punit,
888  ptile, target);
889 
890  case ACTIVITY_FALLOUT:
891  // The call below doesn't support actor tile speculation.
892  fc_assert_msg(unit_tile(punit) == ptile,
893  "Please use action_speculate_unit_on_tile()");
894  return is_action_enabled_unit_on_tile(ACTION_CLEAN_FALLOUT, punit, ptile,
895  target);
896 
897  case ACTIVITY_MINE:
898  // The call below doesn't support actor tile speculation.
899  fc_assert_msg(unit_tile(punit) == ptile,
900  "Please use action_speculate_unit_on_tile()");
901  return is_action_enabled_unit_on_tile(ACTION_MINE, punit, ptile, target);
902 
903  case ACTIVITY_PLANT:
904  // The call below doesn't support actor tile speculation.
905  fc_assert_msg(unit_tile(punit) == ptile,
906  "Please use action_speculate_unit_on_tile()");
907  return is_action_enabled_unit_on_tile(ACTION_PLANT, punit, ptile,
908  nullptr);
909 
910  case ACTIVITY_IRRIGATE:
911  // The call below doesn't support actor tile speculation.
912  fc_assert_msg(unit_tile(punit) == ptile,
913  "Please use action_speculate_unit_on_tile()");
914  return is_action_enabled_unit_on_tile(ACTION_IRRIGATE, punit, ptile,
915  target);
916 
917  case ACTIVITY_CULTIVATE:
918  // The call below doesn't support actor tile speculation.
919  fc_assert_msg(unit_tile(punit) == ptile,
920  "Please use action_speculate_unit_on_tile()");
921  return is_action_enabled_unit_on_tile(ACTION_CULTIVATE, punit, ptile,
922  nullptr);
923 
924  case ACTIVITY_FORTIFYING:
925  // The call below doesn't support actor tile speculation.
926  fc_assert_msg(unit_tile(punit) == ptile,
927  "Please use action_speculate_unit_on_self()");
928  return is_action_enabled_unit_on_self(ACTION_FORTIFY, punit);
929 
930  case ACTIVITY_FORTIFIED:
931  return false;
932 
933  case ACTIVITY_BASE:
934  // The call below doesn't support actor tile speculation.
935  fc_assert_msg(unit_tile(punit) == ptile,
936  "Please use action_speculate_unit_on_tile()");
937  return is_action_enabled_unit_on_tile(ACTION_BASE, punit, ptile, target);
938 
939  case ACTIVITY_GEN_ROAD:
940  // The call below doesn't support actor tile speculation.
941  fc_assert_msg(unit_tile(punit) == ptile,
942  "Please use action_speculate_unit_on_tile()");
943  return is_action_enabled_unit_on_tile(ACTION_ROAD, punit, ptile, target);
944 
945  case ACTIVITY_SENTRY:
946  if (!can_unit_survive_at_tile(&(wld.map), punit, unit_tile(punit))
947  && !unit_transported(punit)) {
948  // Don't let units sentry on tiles they will die on.
949  return false;
950  }
951  return true;
952 
953  case ACTIVITY_PILLAGE:
954  // The call below doesn't support actor tile speculation.
955  fc_assert_msg(unit_tile(punit) == ptile,
956  "Please use action_speculate_unit_on_tile()");
957  return is_action_enabled_unit_on_tile(ACTION_PILLAGE, punit, ptile,
958  target);
959 
960  case ACTIVITY_EXPLORE:
961  return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
962 
963  case ACTIVITY_TRANSFORM:
964  // The call below doesn't support actor tile speculation.
965  fc_assert_msg(unit_tile(punit) == ptile,
966  "Please use action_speculate_unit_on_tile()");
967  return is_action_enabled_unit_on_tile(ACTION_TRANSFORM_TERRAIN, punit,
968  ptile, nullptr);
969 
970  case ACTIVITY_CONVERT:
971  // The call below doesn't support actor tile speculation.
972  fc_assert_msg(unit_tile(punit) == ptile,
973  "Please use action_speculate_unit_on_self()");
974  return is_action_enabled_unit_on_self(ACTION_CONVERT, punit);
975 
976  case ACTIVITY_OLD_ROAD:
977  case ACTIVITY_OLD_RAILROAD:
978  case ACTIVITY_FORTRESS:
979  case ACTIVITY_AIRBASE:
980  case ACTIVITY_PATROL_UNUSED:
981  case ACTIVITY_LAST:
982  case ACTIVITY_UNKNOWN:
983  break;
984  }
985  qCritical("can_unit_do_activity_targeted_at() unknown activity %d",
986  activity);
987  return false;
988 }
989 
993 static void set_unit_activity_internal(struct unit *punit,
994  enum unit_activity new_activity)
995 {
996  fc_assert_ret(new_activity != ACTIVITY_FORTRESS
997  && new_activity != ACTIVITY_AIRBASE);
998 
999  punit->activity = new_activity;
1000  punit->activity_count = 0;
1001  punit->activity_target = nullptr;
1002  if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1003  // No longer done.
1004  punit->done_moving = false;
1005  }
1006 }
1007 
1011 void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
1012 {
1013  fc_assert_ret(!activity_requires_target(new_activity));
1014 
1015  if (new_activity == ACTIVITY_FORTIFYING
1016  && punit->changed_from == ACTIVITY_FORTIFIED) {
1017  new_activity = ACTIVITY_FORTIFIED;
1018  }
1019  set_unit_activity_internal(punit, new_activity);
1020  if (new_activity == punit->changed_from) {
1021  punit->activity_count = punit->changed_from_count;
1022  }
1023 }
1024 
1029  enum unit_activity new_activity,
1030  struct extra_type *new_target)
1031 {
1033  || new_target == nullptr);
1034 
1035  set_unit_activity_internal(punit, new_activity);
1036  punit->activity_target = new_target;
1037  if (new_activity == punit->changed_from
1038  && new_target == punit->changed_from_target) {
1039  punit->activity_count = punit->changed_from_count;
1040  }
1041 }
1042 
1046 bool is_unit_activity_on_tile(enum unit_activity activity,
1047  const struct tile *ptile)
1048 {
1049  unit_list_iterate(ptile->units, punit)
1050  {
1051  if (punit->activity == activity) {
1052  return true;
1053  }
1054  }
1056  return false;
1057 }
1058 
1063 bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
1064 {
1065  bv_extras tgt_ret;
1066 
1067  BV_CLR_ALL(tgt_ret);
1068  unit_list_iterate(ptile->units, punit)
1069  {
1070  if (punit->activity == ACTIVITY_PILLAGE) {
1071  BV_SET(tgt_ret, extra_index(punit->activity_target));
1072  }
1073  }
1075 
1076  return tgt_ret;
1077 }
1078 
1085 const QString unit_activity_text(const struct unit *punit)
1086 {
1087  QString str;
1088  unit_activity_astr(punit, str);
1089 
1090  return str;
1091 }
1092 
1096 void unit_activity_astr(const struct unit *punit, QString &s)
1097 {
1098  if (!punit) {
1099  return;
1100  }
1101 
1102  switch (punit->activity) {
1103  case ACTIVITY_IDLE:
1104  if (utype_fuel(unit_type_get(punit))) {
1105  s += QString(_("Moves: (%1T) %2"))
1106  .arg(QString::number(punit->fuel - 1),
1107  move_points_text(punit->moves_left, false));
1108  } else {
1109  s += QStringLiteral("%1: %2\n")
1110  .arg(_("Moves"), move_points_text(punit->moves_left, false));
1111  }
1112  return;
1113  case ACTIVITY_POLLUTION:
1114  case ACTIVITY_FALLOUT:
1115  case ACTIVITY_OLD_ROAD:
1116  case ACTIVITY_OLD_RAILROAD:
1117  case ACTIVITY_TRANSFORM:
1118  case ACTIVITY_FORTIFYING:
1119  case ACTIVITY_FORTIFIED:
1120  case ACTIVITY_AIRBASE:
1121  case ACTIVITY_FORTRESS:
1122  case ACTIVITY_SENTRY:
1123  case ACTIVITY_GOTO:
1124  case ACTIVITY_EXPLORE:
1125  case ACTIVITY_CONVERT:
1126  case ACTIVITY_CULTIVATE:
1127  case ACTIVITY_PLANT:
1128  s += QStringLiteral("%1\n").arg(get_activity_text(punit->activity));
1129  return;
1130  case ACTIVITY_MINE:
1131  case ACTIVITY_IRRIGATE:
1132  if (punit->activity_target == nullptr) {
1133  s += QStringLiteral("%1\n").arg(get_activity_text(punit->activity));
1134  } else {
1135  s += QStringLiteral("Building %1\n")
1137  }
1138  return;
1139  case ACTIVITY_PILLAGE:
1140  if (punit->activity_target != nullptr) {
1141  s += QStringLiteral("%1: %2\n")
1142  .arg(get_activity_text(punit->activity),
1144  } else {
1145  s += QStringLiteral("%1\n").arg(get_activity_text(punit->activity));
1146  }
1147  return;
1148  case ACTIVITY_BASE:
1149  s += QStringLiteral("%1: %2\n")
1150  .arg(get_activity_text(punit->activity),
1152  return;
1153  case ACTIVITY_GEN_ROAD:
1154  s += QStringLiteral("%1: %2\n")
1155  .arg(get_activity_text(punit->activity),
1157  return;
1158  case ACTIVITY_UNKNOWN:
1159  case ACTIVITY_PATROL_UNUSED:
1160  case ACTIVITY_LAST:
1161  break;
1162  }
1163 
1164  qCritical("Unknown unit activity %d for %s (nb %d) in %s()",
1165  punit->activity, unit_rule_name(punit), punit->id, __FUNCTION__);
1166 }
1167 
1174 void unit_upkeep_astr(const struct unit *punit, QString &s)
1175 {
1176  if (!punit) {
1177  return;
1178  }
1179 
1180  s += QStringLiteral("%1 %2/%3/%4\n")
1181  .arg(_("Food/Shield/Gold:"),
1182  QString::number(punit->upkeep[O_FOOD]),
1183  QString::number(punit->upkeep[O_SHIELD]),
1184  QString::number(punit->upkeep[O_GOLD]));
1185 }
1186 
1190 struct player *unit_nationality(const struct unit *punit)
1191 {
1192  fc_assert_ret_val(nullptr != punit, nullptr);
1193  return punit->nationality;
1194 }
1195 
1200 void unit_tile_set(struct unit *punit, struct tile *ptile)
1201 {
1202  fc_assert_ret(nullptr != punit);
1203  punit->tile = ptile;
1204 }
1205 
1211 struct unit *is_allied_unit_tile(const struct tile *ptile,
1212  const struct player *pplayer)
1213 {
1214  struct unit *punit = nullptr;
1215 
1216  unit_list_iterate(ptile->units, cunit)
1217  {
1218  if (pplayers_allied(pplayer, unit_owner(cunit))) {
1219  punit = cunit;
1220  } else {
1221  return nullptr;
1222  }
1223  }
1225 
1226  return punit;
1227 }
1228 
1235 struct unit *is_enemy_unit_tile(const struct tile *ptile,
1236  const struct player *pplayer)
1237 {
1238  unit_list_iterate(ptile->units, punit)
1239  {
1240  if (pplayers_at_war(unit_owner(punit), pplayer)) {
1241  return punit;
1242  }
1243  }
1245 
1246  return nullptr;
1247 }
1248 
1252 struct unit *is_non_allied_unit_tile(const struct tile *ptile,
1253  const struct player *pplayer)
1254 {
1255  unit_list_iterate(ptile->units, punit)
1256  {
1257  if (!pplayers_allied(unit_owner(punit), pplayer)) {
1258  return punit;
1259  }
1260  }
1262 
1263  return nullptr;
1264 }
1265 
1269 struct unit *is_other_players_unit_tile(const struct tile *ptile,
1270  const struct player *pplayer)
1271 {
1272  unit_list_iterate(ptile->units, punit)
1273  {
1274  if (unit_owner(punit) != pplayer) {
1275  return punit;
1276  }
1277  }
1279 
1280  return nullptr;
1281 }
1282 
1286 struct unit *is_non_attack_unit_tile(const struct tile *ptile,
1287  const struct player *pplayer)
1288 {
1289  unit_list_iterate(ptile->units, punit)
1290  {
1291  if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1292  return punit;
1293  }
1294  }
1296 
1297  return nullptr;
1298 }
1299 
1308 struct unit *unit_occupies_tile(const struct tile *ptile,
1309  const struct player *pplayer)
1310 {
1311  unit_list_iterate(ptile->units, punit)
1312  {
1313  if (!is_military_unit(punit)) {
1314  continue;
1315  }
1316 
1317  if (uclass_has_flag(unit_class_get(punit), UCF_DOESNT_OCCUPY_TILE)
1318  || unit_type_get(punit)->vlayer != V_MAIN) {
1319  continue;
1320  }
1321 
1322  if (pplayers_at_war(unit_owner(punit), pplayer)) {
1323  return punit;
1324  }
1325  }
1327 
1328  return nullptr;
1329 }
1330 
1341 bool is_my_zoc(const struct player *pplayer, const struct tile *ptile0,
1342  const struct civ_map *zmap)
1343 {
1344  struct terrain *pterrain;
1345  bool srv = is_server();
1346 
1347  square_iterate(zmap, ptile0, 1, ptile)
1348  {
1349  struct city *pcity;
1350 
1351  pterrain = tile_terrain(ptile);
1352  if (T_UNKNOWN == pterrain || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1353  continue;
1354  }
1355 
1356  pcity = is_non_allied_city_tile(ptile, pplayer);
1357  if (pcity != nullptr) {
1358  if ((srv && unit_list_size(ptile->units) > 0)
1359  || (!srv
1360  && (pcity->client.occupied
1361  || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)))) {
1362  /* Occupied enemy city, it doesn't matter if units inside have
1363  * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1364  return false;
1365  }
1366  } else {
1367  unit_list_iterate(ptile->units, punit)
1368  {
1369  if (!pplayers_allied(unit_owner(punit), pplayer)
1370  && !unit_has_type_flag(punit, UTYF_NOZOC)) {
1371  return false;
1372  }
1373  }
1375  }
1376  }
1378 
1379  return true;
1380 }
1381 
1385 bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
1386 {
1387  return (!uclass_has_flag(utype_class(punittype), UCF_ZOC)
1388  || utype_has_flag(punittype, UTYF_IGZOC));
1389 }
1390 
1399 bool unit_being_aggressive(const struct unit *punit)
1400 {
1401  if (!is_attack_unit(punit)) {
1402  return false;
1403  }
1404  if (tile_city(unit_tile(punit))) {
1405  return false;
1406  }
1407  if (BORDERS_DISABLED != game.info.borders) {
1408  switch (game.info.happyborders) {
1409  case HB_DISABLED:
1410  break;
1411  case HB_NATIONAL:
1412  if (tile_owner(unit_tile(punit)) == unit_owner(punit)) {
1413  return false;
1414  }
1415  break;
1416  case HB_ALLIANCE:
1417  if (pplayers_allied(tile_owner(unit_tile(punit)), unit_owner(punit))) {
1418  return false;
1419  }
1420  break;
1421  }
1422  }
1424  BF_NOT_AGGRESSIVE)) {
1425  return !is_unit_near_a_friendly_city(punit);
1426  }
1427 
1428  return true;
1429 }
1430 
1434 bool is_build_activity(enum unit_activity activity, const struct tile *ptile)
1435 {
1436  switch (activity) {
1437  case ACTIVITY_MINE:
1438  case ACTIVITY_IRRIGATE:
1439  case ACTIVITY_BASE:
1440  case ACTIVITY_GEN_ROAD:
1441  return true;
1442  default:
1443  return false;
1444  }
1445 }
1446 
1450 bool is_clean_activity(enum unit_activity activity)
1451 {
1452  switch (activity) {
1453  case ACTIVITY_PILLAGE:
1454  case ACTIVITY_POLLUTION:
1455  case ACTIVITY_FALLOUT:
1456  return true;
1457  default:
1458  return false;
1459  }
1460 }
1461 
1465 bool is_terrain_change_activity(enum unit_activity activity)
1466 {
1467  switch (activity) {
1468  case ACTIVITY_CULTIVATE:
1469  case ACTIVITY_PLANT:
1470  case ACTIVITY_TRANSFORM:
1471  return true;
1472  default:
1473  return false;
1474  }
1475 }
1476 
1480 bool is_tile_activity(enum unit_activity activity)
1481 {
1482  return is_build_activity(activity, nullptr) || is_clean_activity(activity)
1483  || is_terrain_change_activity(activity);
1484 }
1485 
1490 struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1491  const struct unit_type *punittype,
1492  int veteran_level)
1493 {
1494  fc_assert_ret_val(nullptr != punittype, nullptr); // No untyped units!
1495  fc_assert_ret_val(nullptr != pplayer, nullptr); // No unowned units!
1496 
1497  // Make sure that contents of unit structure are correctly initialized.
1498  struct unit *punit = new unit();
1499  int max_vet_lvl;
1500 
1501  // It does not register the unit so the id is set to 0.
1502  punit->id = IDENTITY_NUMBER_ZERO;
1503 
1504  punit->utype = punittype;
1505 
1506  punit->owner = pplayer;
1507  punit->nationality = pplayer;
1508 
1509  punit->refcount = 1;
1510  punit->facing = rand_direction();
1511 
1512  if (pcity) {
1513  unit_tile_set(punit, pcity->tile);
1514  punit->homecity = pcity->id;
1515  } else {
1516  unit_tile_set(punit, nullptr);
1517  punit->homecity = IDENTITY_NUMBER_ZERO;
1518  }
1519 
1520  memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1521  punit->goto_tile = nullptr;
1522  max_vet_lvl = utype_veteran_levels(punittype) - 1;
1523  punit->veteran = MIN(veteran_level, max_vet_lvl);
1524  // A unit new and fresh ...
1525  punit->fuel = utype_fuel(unit_type_get(punit));
1526  punit->hp = unit_type_get(punit)->hp;
1527  punit->moves_left = unit_move_rate(punit);
1528  punit->moved = false;
1529 
1530  punit->ssa_controller = SSA_NONE;
1531  punit->paradropped = false;
1532  punit->done_moving = false;
1533 
1534  punit->transporter = nullptr;
1535  punit->transporting = unit_list_new();
1536 
1537  punit->carrying = nullptr;
1538 
1539  punit->changed_from = ACTIVITY_IDLE;
1540  punit->changed_from_count = 0;
1541  set_unit_activity(punit, ACTIVITY_IDLE);
1542  punit->battlegroup = BATTLEGROUP_NONE;
1543  punit->has_orders = false;
1544 
1545  punit->action_decision_want = ACT_DEC_NOTHING;
1546  punit->action_decision_tile = nullptr;
1547 
1548  punit->stay = false;
1549  punit->action_timestamp = 0;
1550 
1551  if (is_server()) {
1552  punit->server.debug = false;
1553  punit->server.birth_turn = game.info.turn;
1554 
1555  punit->server.dying = false;
1556 
1557  punit->server.removal_callback = nullptr;
1558 
1559  memset(punit->server.upkeep_payed, 0,
1560  O_LAST * sizeof(*punit->server.upkeep_payed));
1561 
1562  punit->server.ord_map = 0;
1563  punit->server.ord_city = 0;
1564 
1565  punit->server.vision = nullptr; // No vision.
1566  /* Must be an invalid turn number, and an invalid previous turn
1567  * number. */
1568  punit->action_turn = -2;
1569  // punit->server.moving = nullptr; set by fc_calloc().
1570 
1571  punit->server.adv = new unit_adv[1]();
1572 
1573  CALL_FUNC_EACH_AI(unit_alloc, punit);
1574  } else {
1575  punit->client.focus_status = FOCUS_AVAIL;
1576  punit->client.transported_by = -1;
1577  punit->client.colored = false;
1578  punit->client.act_prob_cache = nullptr;
1579  }
1580 
1581  return punit;
1582 }
1583 
1588 void unit_virtual_destroy(struct unit *punit)
1589 {
1590  free_unit_orders(punit);
1591 
1592  // Unload unit if transported.
1593  unit_transport_unload(punit);
1594  fc_assert(!unit_transported(punit));
1595 
1596  // Check for transported units. Use direct access to the list.
1597  if (unit_list_size(punit->transporting) != 0) {
1598  // Unload all units.
1599  unit_list_iterate_safe(punit->transporting, pcargo)
1600  {
1601  unit_transport_unload(pcargo);
1602  }
1604  }
1605  fc_assert(unit_list_size(punit->transporting) == 0);
1606 
1607  if (punit->transporting) {
1608  unit_list_destroy(punit->transporting);
1609  }
1610 
1611  CALL_FUNC_EACH_AI(unit_free, punit);
1612 
1613  if (is_server() && punit->server.adv) {
1614  delete[] punit->server.adv;
1615  punit->server.adv = nullptr;
1616  } else {
1617  if (punit->client.act_prob_cache) {
1618  delete[] punit->client.act_prob_cache;
1619  punit->client.act_prob_cache = nullptr;
1620  }
1621  }
1622 
1623  if (--punit->refcount <= 0) {
1624  delete punit;
1625  punit = nullptr;
1626  }
1627 }
1628 
1633 void free_unit_orders(struct unit *punit)
1634 {
1635  if (punit->has_orders) {
1636  punit->goto_tile = nullptr;
1637  delete[] punit->orders.list;
1638  punit->orders.list = nullptr;
1639  }
1640  punit->orders.length = 0;
1641  punit->has_orders = false;
1642 }
1643 
1647 int get_transporter_occupancy(const struct unit *ptrans)
1648 {
1649  fc_assert_ret_val(ptrans, 0);
1650 
1651  return unit_list_size(ptrans->transporting);
1652 }
1653 
1658  const struct unit *pcargo, const struct tile *ptile,
1659  bool (*unit_load_test)(const struct unit *pc, const struct unit *pt))
1660 {
1661  struct unit *best_trans = nullptr;
1662  struct {
1663  bool has_orders, is_idle, can_freely_unload;
1664  int depth, outermost_moves_left, total_moves;
1665  } cur, best = {false};
1666 
1667  unit_list_iterate(ptile->units, ptrans)
1668  {
1669  if (!unit_load_test(pcargo, ptrans)) {
1670  continue;
1671  } else if (best_trans == nullptr) {
1672  best_trans = ptrans;
1673  }
1674 
1675  /* Gather data from transport stack in a single pass, for use in
1676  * various conditions below. */
1677  cur.has_orders = unit_has_orders(ptrans);
1678  cur.outermost_moves_left = ptrans->moves_left;
1679  cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1680  unit_transports_iterate(ptrans, ptranstrans)
1681  {
1682  if (unit_has_orders(ptranstrans)) {
1683  cur.has_orders = true;
1684  }
1685  cur.outermost_moves_left = ptranstrans->moves_left;
1686  cur.total_moves +=
1687  ptranstrans->moves_left + unit_move_rate(ptranstrans);
1688  }
1690 
1691  /* Criteria for deciding the 'best' transport to load onto.
1692  * The following tests are applied in order; earlier ones have
1693  * lexicographically greater significance than later ones. */
1694 
1695  /* Transports which have orders, or are on transports with orders,
1696  * are less preferable to transport stacks without orders (to
1697  * avoid loading on units that are just passing through). */
1698  if (best_trans != ptrans) {
1699  if (!cur.has_orders && best.has_orders) {
1700  best_trans = ptrans;
1701  } else if (cur.has_orders && !best.has_orders) {
1702  continue;
1703  }
1704  }
1705 
1706  /* Else, transports which are idle are preferable (giving players
1707  * some control over loading) -- this does not check transports
1708  * of transports. */
1709  cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1710  if (best_trans != ptrans) {
1711  if (cur.is_idle && !best.is_idle) {
1712  best_trans = ptrans;
1713  } else if (!cur.is_idle && best.is_idle) {
1714  continue;
1715  }
1716  }
1717 
1718  /* Else, transports from which the cargo could unload at any time
1719  * are preferable to those where the cargo can only disembark in
1720  * cities/bases. */
1721  cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1722  unit_type_get(ptrans));
1723  if (best_trans != ptrans) {
1724  if (cur.can_freely_unload && !best.can_freely_unload) {
1725  best_trans = ptrans;
1726  } else if (!cur.can_freely_unload && best.can_freely_unload) {
1727  continue;
1728  }
1729  }
1730 
1731  // Else, transports which are less deeply nested are preferable.
1732  cur.depth = unit_transport_depth(ptrans);
1733  if (best_trans != ptrans) {
1734  if (cur.depth < best.depth) {
1735  best_trans = ptrans;
1736  } else if (cur.depth > best.depth) {
1737  continue;
1738  }
1739  }
1740 
1741  /* Else, transport stacks where the outermost transport has more
1742  * moves left are preferable (on the assumption that it's the
1743  * outermost transport that's about to move). */
1744  if (best_trans != ptrans) {
1745  if (cur.outermost_moves_left > best.outermost_moves_left) {
1746  best_trans = ptrans;
1747  } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1748  continue;
1749  }
1750  }
1751 
1752  /* All other things being equal, as a tie-breaker, compare the total
1753  * moves left (this turn) and move rate (future turns) for the whole
1754  * stack, to take into account total potential movement for both
1755  * short and long journeys (we don't know which the cargo intends to
1756  * make). Doesn't try to account for whether transports can unload,
1757  * etc. */
1758  if (best_trans != ptrans) {
1759  if (cur.total_moves > best.total_moves) {
1760  best_trans = ptrans;
1761  } else {
1762  continue;
1763  }
1764  }
1765 
1766  fc_assert(best_trans == ptrans);
1767  best = cur;
1768  }
1770 
1771  return best_trans;
1772 }
1773 
1779 struct unit *transporter_for_unit(const struct unit *pcargo)
1780 {
1781  return base_transporter_for_unit(pcargo, unit_tile(pcargo), can_unit_load);
1782 }
1783 
1789 struct unit *transporter_for_unit_at(const struct unit *pcargo,
1790  const struct tile *ptile)
1791 {
1792  return base_transporter_for_unit(pcargo, ptile, could_unit_load);
1793 }
1794 
1800  const struct unit *punit)
1801 {
1803  return false;
1804  }
1805 
1806  unit_list_iterate(punit->transporting, pcargo)
1807  {
1808  if (!can_unit_type_transport(utype, unit_class_get(pcargo))) {
1809  return false;
1810  }
1811  }
1813 
1814  return true;
1815 }
1816 
1826 enum unit_upgrade_result unit_upgrade_test(const struct unit *punit,
1827  bool is_free)
1828 {
1829  struct player *pplayer = unit_owner(punit);
1830  const struct unit_type *to_unittype =
1831  can_upgrade_unittype(pplayer, unit_type_get(punit));
1832  struct city *pcity;
1833  int cost;
1834 
1835  if (!to_unittype) {
1836  return UU_NO_UNITTYPE;
1837  }
1838 
1839  if (!is_free) {
1840  cost = unit_upgrade_price(pplayer, unit_type_get(punit), to_unittype);
1841  if (pplayer->economic.gold < cost) {
1842  return UU_NO_MONEY;
1843  }
1844 
1845  pcity = tile_city(unit_tile(punit));
1846  if (!pcity) {
1847  return UU_NOT_IN_CITY;
1848  }
1849  if (city_owner(pcity) != pplayer) {
1850  // TODO: should upgrades in allied cities be possible?
1851  return UU_NOT_CITY_OWNER;
1852  }
1853  }
1854 
1855  if (!can_type_transport_units_cargo(to_unittype, punit)) {
1856  /* TODO: allow transported units to be reassigned. Check here
1857  * and make changes to upgrade_unit. */
1858  return UU_NOT_ENOUGH_ROOM;
1859  }
1860 
1861  if (punit->transporter != nullptr) {
1863  utype_class(to_unittype))) {
1864  return UU_UNSUITABLE_TRANSPORT;
1865  }
1866  } else if (!can_exist_at_tile(&(wld.map), to_unittype, unit_tile(punit))) {
1867  // The new unit type can't survive on this terrain.
1868  return UU_NOT_TERRAIN;
1869  }
1870 
1871  return UU_OK;
1872 }
1873 
1877 bool unit_can_convert(const struct unit *punit)
1878 {
1879  const struct unit_type *tgt = unit_type_get(punit)->converted_to;
1880 
1881  if (tgt == nullptr) {
1882  return false;
1883  }
1884 
1885  if (!can_type_transport_units_cargo(tgt, punit)) {
1886  return false;
1887  }
1888 
1889  if (!can_exist_at_tile(&(wld.map), tgt, unit_tile(punit))) {
1890  return false;
1891  }
1892 
1893  return true;
1894 }
1895 
1900 enum unit_upgrade_result unit_upgrade_info(const struct unit *punit,
1901  char *buf, size_t bufsz)
1902 {
1903  struct player *pplayer = unit_owner(punit);
1904  enum unit_upgrade_result result = unit_upgrade_test(punit, false);
1905  int upgrade_cost;
1906  const struct unit_type *from_unittype = unit_type_get(punit);
1907  const struct unit_type *to_unittype =
1908  can_upgrade_unittype(pplayer, unit_type_get(punit));
1909  char tbuf[MAX_LEN_MSG];
1910 
1911  fc_snprintf(tbuf, ARRAY_SIZE(tbuf),
1912  PL_("Treasury contains %d gold.", "Treasury contains %d gold.",
1913  pplayer->economic.gold),
1914  pplayer->economic.gold);
1915 
1916  switch (result) {
1917  case UU_OK:
1918  upgrade_cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
1919  // This message is targeted toward the GUI callers.
1920  // TRANS: Last %s is pre-pluralised "Treasury contains %d gold."
1921  fc_snprintf(buf, bufsz,
1922  PL_("Upgrade %s to %s for %d gold?\n%s",
1923  "Upgrade %s to %s for %d gold?\n%s", upgrade_cost),
1924  utype_name_translation(from_unittype),
1925  utype_name_translation(to_unittype), upgrade_cost, tbuf);
1926  break;
1927  case UU_NO_UNITTYPE:
1928  fc_snprintf(buf, bufsz, _("Sorry, cannot upgrade %s (yet)."),
1929  utype_name_translation(from_unittype));
1930  break;
1931  case UU_NO_MONEY:
1932  upgrade_cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
1933  // TRANS: Last %s is pre-pluralised "Treasury contains %d gold."
1934  fc_snprintf(buf, bufsz,
1935  PL_("Upgrading %s to %s costs %d gold.\n%s",
1936  "Upgrading %s to %s costs %d gold.\n%s", upgrade_cost),
1937  utype_name_translation(from_unittype),
1938  utype_name_translation(to_unittype), upgrade_cost, tbuf);
1939  break;
1940  case UU_NOT_IN_CITY:
1941  case UU_NOT_CITY_OWNER:
1942  fc_snprintf(buf, bufsz, _("You can only upgrade units in your cities."));
1943  break;
1944  case UU_NOT_ENOUGH_ROOM:
1945  fc_snprintf(buf, bufsz,
1946  _("Upgrading this %s would strand units it transports."),
1947  utype_name_translation(from_unittype));
1948  break;
1949  case UU_NOT_TERRAIN:
1950  fc_snprintf(buf, bufsz,
1951  _("Upgrading this %s would result in a %s which can not "
1952  "survive at this place."),
1953  utype_name_translation(from_unittype),
1954  utype_name_translation(to_unittype));
1955  break;
1957  fc_snprintf(buf, bufsz,
1958  _("Upgrading this %s would result in a %s which its "
1959  "current transport, %s, could not transport."),
1960  utype_name_translation(from_unittype),
1961  utype_name_translation(to_unittype),
1963  break;
1964  }
1965 
1966  return result;
1967 }
1968 
1973 int unit_pays_mp_for_action(const struct action *paction,
1974  const struct unit *punit)
1975 {
1976  int mpco;
1977 
1978  mpco = get_target_bonus_effects(
1979  nullptr, unit_owner(punit), nullptr,
1980  unit_tile(punit) ? tile_city(unit_tile(punit)) : nullptr, nullptr,
1981  unit_tile(punit), punit, unit_type_get(punit), nullptr, nullptr,
1982  paction, EFT_ACTION_SUCCESS_MOVE_COST);
1983 
1984  mpco += utype_pays_mp_for_action_base(paction, unit_type_get(punit));
1985 
1986  return mpco;
1987 }
1988 
1992 bool is_losing_hp(const struct unit *punit)
1993 {
1994  const struct unit_type *punittype = unit_type_get(punit);
1995 
1996  return get_unit_bonus(punit, EFT_UNIT_RECOVER)
1997  < (punittype->hp * utype_class(punittype)->hp_loss_pct / 100);
1998 }
1999 
2003 bool unit_type_is_losing_hp(const struct player *pplayer,
2004  const struct unit_type *punittype)
2005 {
2006  return get_unittype_bonus(pplayer, nullptr, punittype, EFT_UNIT_RECOVER)
2007  < (punittype->hp * utype_class(punittype)->hp_loss_pct / 100);
2008 }
2009 
2014 bool unit_is_alive(int id)
2015 {
2016  // Check if unit exist in game
2017  return game_unit_by_number(id) != nullptr;
2018 }
2019 
2027 bool unit_is_virtual(const struct unit *punit)
2028 {
2029  if (!punit) {
2030  return false;
2031  }
2032 
2033  return punit != game_unit_by_number(punit->id);
2034 }
2035 
2039 void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2040 {
2041  return punit->server.ais[ai_type_number(ai)];
2042 }
2043 
2047 void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2048  void *data)
2049 {
2050  punit->server.ais[ai_type_number(ai)] = data;
2051 }
2052 
2061 int unit_bribe_cost(struct unit *punit, struct player *briber)
2062 {
2063  int cost, default_hp, dist = 0;
2064  struct tile *ptile = unit_tile(punit);
2065 
2066  fc_assert_ret_val(punit != nullptr, 0);
2067 
2068  default_hp = unit_type_get(punit)->hp;
2069  cost = unit_owner(punit)->economic.gold + game.info.base_bribe_cost;
2070 
2071  // Consider the distance to the capital.
2072  dist = GAME_UNIT_BRIBE_DIST_MAX;
2073  city_list_iterate(unit_owner(punit)->cities, capital)
2074  {
2075  if (is_capital(capital)) {
2076  int tmp = map_distance(capital->tile, ptile);
2077 
2078  if (tmp < dist) {
2079  dist = tmp;
2080  }
2081  }
2082  }
2084 
2085  cost /= dist + 2;
2086 
2087  // Consider the build cost.
2088  cost *= unit_build_shield_cost_base(punit) / 10;
2089 
2090  // Rule set specific cost modification
2091  cost += (cost
2092  * get_target_bonus_effects(nullptr, unit_owner(punit), briber,
2093  game_city_by_number(punit->homecity),
2094  nullptr, ptile, punit,
2095  unit_type_get(punit), nullptr, nullptr,
2096  nullptr, EFT_UNIT_BRIBE_COST_PCT))
2097  / 100;
2098 
2099  // Veterans are not cheap.
2100  {
2101  const struct veteran_level *vlevel =
2102  utype_veteran_level(unit_type_get(punit), punit->veteran);
2103 
2104  fc_assert_ret_val(vlevel != nullptr, 0);
2105  cost = cost * vlevel->power_fact / 100;
2106  if (unit_type_get(punit)->move_rate > 0) {
2107  cost += cost * vlevel->move_bonus / unit_type_get(punit)->move_rate;
2108  } else {
2109  cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2110  }
2111  }
2112 
2113  /* Cost now contains the basic bribe cost. We now reduce it by:
2114  * bribecost = cost/2 + cost/2 * damage/hp
2115  * = cost/2 * (1 + damage/hp) */
2116  return (static_cast<float>(cost) / 2
2117  * (1.0 + static_cast<float>(punit->hp) / default_hp));
2118 }
2119 
2123 bool unit_transport_load(struct unit *pcargo, struct unit *ptrans,
2124  bool force)
2125 {
2126  fc_assert_ret_val(ptrans != nullptr, false);
2127  fc_assert_ret_val(pcargo != nullptr, false);
2128 
2129  fc_assert_ret_val(!unit_list_search(ptrans->transporting, pcargo), false);
2130 
2131  if (force || can_unit_load(pcargo, ptrans)) {
2132  pcargo->transporter = ptrans;
2133  unit_list_append(ptrans->transporting, pcargo);
2134 
2135  return true;
2136  }
2137 
2138  return false;
2139 }
2140 
2144 bool unit_transport_unload(struct unit *pcargo)
2145 {
2146  struct unit *ptrans;
2147 
2148  fc_assert_ret_val(pcargo != nullptr, false);
2149 
2150  if (!unit_transported(pcargo)) {
2151  // 'pcargo' is not transported.
2152  return false;
2153  }
2154 
2155  // Get the transporter; must not be defined on the client!
2156  ptrans = unit_transport_get(pcargo);
2157  if (ptrans) {
2158  bool success;
2159 
2160  // 'pcargo' and 'ptrans' should be on the same tile.
2161  fc_assert(same_pos(unit_tile(pcargo), unit_tile(ptrans)));
2162  // It is an error if 'pcargo' can not be removed from the 'ptrans'.
2163  success = unit_list_remove(ptrans->transporting, pcargo);
2164  fc_assert(success);
2165  }
2166 
2167  // For the server (also safe for the client).
2168  pcargo->transporter = nullptr;
2169 
2170  return true;
2171 }
2172 
2176 bool unit_transported(const struct unit *pcargo)
2177 {
2178  fc_assert_ret_val(pcargo != nullptr, false);
2179 
2180  /* The unit is transported if a transporter unit is set or, (for the
2181  * client) if the transported_by field is set. */
2182  return pcargo->transporter != nullptr
2183  || (!is_server() && pcargo->client.transported_by != -1);
2184 }
2185 
2189 struct unit *unit_transport_get(const struct unit *pcargo)
2190 {
2191  fc_assert_ret_val(pcargo != nullptr, nullptr);
2192 
2193  return pcargo->transporter;
2194 }
2195 
2199 struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2200 {
2201  fc_assert_ret_val(ptrans != nullptr, nullptr);
2202  fc_assert_ret_val(ptrans->transporting != nullptr, nullptr);
2203 
2204  return ptrans->transporting;
2205 }
2206 
2210 static inline bool
2211 unit_transport_check_one(const struct unit_type *cargo_utype,
2212  const struct unit_type *trans_utype)
2213 {
2214  return (
2215  trans_utype != cargo_utype
2216  && !can_unit_type_transport(cargo_utype, utype_class(trans_utype)));
2217 }
2218 
2227 bool unit_transport_check(const struct unit *pcargo,
2228  const struct unit *ptrans)
2229 {
2230  const struct unit_type *cargo_utype = unit_type_get(pcargo);
2231 
2232  // Check 'pcargo' against 'ptrans'.
2233  if (!unit_transport_check_one(cargo_utype, unit_type_get(ptrans))) {
2234  return false;
2235  }
2236 
2237  // Check 'pcargo' against 'ptrans' parents.
2238  unit_transports_iterate(ptrans, pparent)
2239  {
2240  if (!unit_transport_check_one(cargo_utype, unit_type_get(pparent))) {
2241  return false;
2242  }
2243  }
2245 
2246  // Check cargo children...
2247  unit_cargo_iterate(pcargo, pchild)
2248  {
2249  cargo_utype = unit_type_get(pchild);
2250 
2251  // ...against 'ptrans'.
2252  if (!unit_transport_check_one(cargo_utype, unit_type_get(ptrans))) {
2253  return false;
2254  }
2255 
2256  // ...and against 'ptrans' parents.
2257  unit_transports_iterate(ptrans, pparent)
2258  {
2259  if (!unit_transport_check_one(cargo_utype, unit_type_get(pparent))) {
2260  return false;
2261  }
2262  }
2264  }
2266 
2267  return true;
2268 }
2269 
2274 bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2275 {
2276  unit_transports_iterate(pcargo, plevel)
2277  {
2278  if (ptrans == plevel) {
2279  return true;
2280  }
2281  }
2283  return false;
2284 }
2285 
2289 int unit_cargo_depth(const struct unit *ptrans)
2290 {
2291  struct cargo_iter iter;
2292  struct iterator *it;
2293  int depth = 0;
2294 
2295  for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2296  iterator_next(it)) {
2297  if (iter.depth > depth) {
2298  depth = iter.depth;
2299  }
2300  }
2301  return depth;
2302 }
2303 
2307 int unit_transport_depth(const struct unit *pcargo)
2308 {
2309  int level = 0;
2310 
2311  unit_transports_iterate(pcargo, plevel) { level++; }
2313  return level;
2314 }
2315 
2319 size_t cargo_iter_sizeof() { return sizeof(struct cargo_iter); }
2320 
2324 static void *cargo_iter_get(const struct iterator *it)
2325 {
2326  const struct cargo_iter *iter = CARGO_ITER(it);
2327 
2328  return unit_list_link_data(iter->links[iter->depth - 1]);
2329 }
2330 
2334 static void cargo_iter_next(struct iterator *it)
2335 {
2336  struct cargo_iter *iter = CARGO_ITER(it);
2337  const struct unit_list_link *piter = iter->links[iter->depth - 1];
2338  const struct unit_list_link *pnext;
2339 
2340  // Variant 1: unit has cargo.
2341  pnext = unit_list_head(unit_transport_cargo(unit_list_link_data(piter)));
2342  if (nullptr != pnext) {
2343  fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2344  iter->links[iter->depth++] = pnext;
2345  return;
2346  }
2347 
2348  do {
2349  // Variant 2: there are other cargo units at same level.
2350  pnext = unit_list_link_next(piter);
2351  if (nullptr != pnext) {
2352  iter->links[iter->depth - 1] = pnext;
2353  return;
2354  }
2355 
2356  // Variant 3: return to previous level, and do same tests.
2357  piter = iter->links[iter->depth-- - 2];
2358  } while (0 < iter->depth);
2359 }
2360 
2364 static bool cargo_iter_valid(const struct iterator *it)
2365 {
2366  return (0 < CARGO_ITER(it)->depth);
2367 }
2368 
2372 struct iterator *cargo_iter_init(struct cargo_iter *iter,
2373  const struct unit *ptrans)
2374 {
2375  struct iterator *it = ITERATOR(iter);
2376 
2377  it->get = cargo_iter_get;
2378  it->next = cargo_iter_next;
2379  it->valid = cargo_iter_valid;
2380  iter->links[0] = unit_list_head(unit_transport_cargo(ptrans));
2381  iter->depth = (nullptr != iter->links[0] ? 1 : 0);
2382 
2383  return it;
2384 }
2385 
2389 bool unit_is_cityfounder(const struct unit *punit)
2390 {
2391  return utype_is_cityfounder(unit_type_get(punit));
2392 }
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 action_prob_possible(const struct act_prob probability)
Returns TRUE iff the given action probability belongs to an action that may be possible.
Definition: actions.cpp:5380
bool is_action_enabled_unit_on_self(const action_id wanted_action, const struct unit *actor_unit)
Returns TRUE if actor_unit can do wanted_action to itself as far as action enablers are concerned.
Definition: actions.cpp:4196
bool is_action_enabled_unit_on_city(const action_id wanted_action, const struct unit *actor_unit, const struct city *target_city)
Returns TRUE if actor_unit can do wanted_action to target_city as far as action enablers are concerne...
Definition: actions.cpp:3948
struct act_prob action_prob_vs_city(const struct unit *actor_unit, const action_id act_id, const struct city *target_city)
Get the actor unit's probability of successfully performing the chosen action on the target city.
Definition: actions.cpp:4809
struct act_prob action_prob_vs_unit(const struct unit *actor_unit, const action_id act_id, const struct unit *target_unit)
Get the actor unit's probability of successfully performing the chosen action on the target unit.
Definition: actions.cpp:4871
struct act_prob action_prob_vs_tile(const struct unit *actor_unit, const action_id act_id, const struct tile *target_tile, const struct extra_type *target_extra)
Get the actor unit's probability of successfully performing the chosen action on the target tile.
Definition: actions.cpp:5090
bool action_maybe_possible_actor_unit(const action_id act_id, const struct unit *actor_unit)
Returns TRUE if the wanted action (as far as the player knows) can be performed right now by the spec...
Definition: actions.cpp:5765
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
int ai_type_number(const struct ai_type *ai)
Returns id of the given ai_type.
Definition: ai.cpp:47
#define CALL_FUNC_EACH_AI(_func,...)
Definition: ai.h:393
#define BV_CLR_ALL(bv)
Definition: bitvector.h:62
#define BV_SET(bv, bit)
Definition: bitvector.h:44
struct city * is_non_allied_city_tile(const struct tile *ptile, const struct player *pplayer)
Is there an non_allied city on this tile?
Definition: city.cpp:1968
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
bool is_capital(const struct city *pcity)
Return TRUE iff this city is its nation's capital.
Definition: city.cpp:1495
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
bool is_unit_near_a_friendly_city(const struct unit *punit)
Return TRUE if there is a friendly city near to this unit (within 3 steps).
Definition: city.cpp:1984
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define city_list_iterate_end
Definition: city.h:484
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_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Returns the effect bonus at a unit.
Definition: effects.cpp:869
const char * extra_name_translation(const struct extra_type *pextra)
Return the (translated) name of the extra type.
Definition: extras.cpp:165
bool can_extras_coexist(const struct extra_type *pextra1, const struct extra_type *pextra2)
Can two extras coexist in same tile?
Definition: extras.cpp:902
struct extra_type * next_extra_for_tile(const struct tile *ptile, enum extra_cause cause, const struct player *pplayer, const struct unit *punit)
Returns next extra by cause that unit or player can build to tile.
Definition: extras.cpp:687
#define extra_index(_e_)
Definition: extras.h:163
enum unit_activity Activity_type_id
Definition: fc_types.h:296
int action_id
Definition: fc_types.h:306
@ HB_ALLIANCE
Definition: fc_types.h:1094
@ HB_DISABLED
Definition: fc_types.h:1094
@ HB_NATIONAL
Definition: fc_types.h:1094
@ O_SHIELD
Definition: fc_types.h:86
@ O_FOOD
Definition: fc_types.h:85
@ O_GOLD
Definition: fc_types.h:88
@ O_LAST
Definition: fc_types.h:91
@ BORDERS_DISABLED
Definition: fc_types.h:863
#define IDENTITY_NUMBER_ZERO
Definition: fc_types.h:76
#define PL_(String1, String2, n)
Definition: fcintl.h:54
#define _(String)
Definition: fcintl.h:50
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 civ_game game
Definition: game.cpp:47
bool is_server()
Is program type server?
Definition: game.cpp:57
struct world wld
Definition: game.cpp:48
struct city * game_city_by_number(int id)
Often used function to get a city pointer from a city ID.
Definition: game.cpp:103
#define GAME_UNIT_BRIBE_DIST_MAX
Definition: game.h:692
#define GAME_TRANSPORT_MAX_RECURSIVE
Definition: game.h:695
static bool iterator_valid(const struct iterator *it)
Definition: iterator.h:47
#define ITERATOR(p)
Definition: iterator.h:26
static void iterator_next(struct iterator *it)
Definition: iterator.h:31
#define fc_assert_msg(condition, message,...)
Definition: log.h:96
#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
enum direction8 rand_direction()
Return random direction that is valid in current map.
Definition: map.cpp:1581
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
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition: map.h:312
#define square_iterate_end
Definition: map.h:315
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Return TRUE iff a unit of the given unit type can "exist" at this location.
Definition: movement.cpp:236
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Return TRUE iff the unit can "exist" at this location.
Definition: movement.cpp:267
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_unit_transport(const struct unit *transporter, const struct unit *transported)
Return true iff transporter has ability to transport transported.
Definition: movement.cpp:684
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
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_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Return TRUE iff transporter type has ability to transport transported class.
Definition: movement.cpp:698
bool can_attack_non_native(const struct unit_type *utype)
This unit can attack non-native tiles (eg.
Definition: movement.cpp:164
const char * move_points_text(int mp, bool reduce)
Simple version of move_points_text_full() – render positive movement points as text without any prefi...
Definition: movement.cpp:856
#define SINGLE_MOVE
Definition: movement.h:17
#define MAX_LEN_MSG
Definition: packets.h:37
struct city_list * cities
Definition: packhand.cpp:122
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Checks if a unit can be seen by pplayer at its current location.
Definition: player.cpp:1016
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players can attack each other.
Definition: player.cpp:1317
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied.
Definition: player.cpp:1334
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players have peace, cease-fire, or armistice.
Definition: player.cpp:1388
struct setting_list * level[OLEVELS_NUM]
Definition: settings.cpp:167
#define ARRAY_SIZE(x)
Definition: shared.h:79
#define MIN(x, y)
Definition: shared.h:49
#define FC_INFINITY
Definition: shared.h:32
Definition: ai.h:42
struct iterator vtable
Definition: unit.cpp:45
int depth
Definition: unit.cpp:47
const struct unit_list_link * links[GAME_TRANSPORT_MAX_RECURSIVE]
Definition: unit.cpp:46
Definition: city.h:291
struct city::@15::@18 client
int id
Definition: city.h:296
int airlift
Definition: city.h:349
struct tile * tile
Definition: city.h:293
struct packet_game_info info
Definition: game.h:80
bool(* valid)(const struct iterator *it)
Definition: iterator.h:23
void *(* get)(const struct iterator *it)
Definition: iterator.h:22
void(* next)(struct iterator *it)
Definition: iterator.h:21
Definition: player.h:231
struct player_economic economic
Definition: player.h:266
struct terrain * irrigation_result
Definition: terrain.h:200
struct terrain * mining_result
Definition: terrain.h:204
Definition: tile.h:42
struct unit_list * units
Definition: tile.h:50
Definition: unit.h:75
enum unit_activity activity
Definition: unit.h:81
enum unit_orders order
Definition: unit.h:80
int action
Definition: unit.h:87
enum direction8 dir
Definition: unit.h:89
int target
Definition: unit.h:84
int sub_target
Definition: unit.h:85
int transport_capacity
Definition: unittype.h:488
struct unit_type::@82 adv
bool worker
Definition: unittype.h:538
int move_rate
Definition: unittype.h:481
enum vision_layer vlayer
Definition: unittype.h:532
int hp
Definition: unittype.h:489
const struct unit_type * converted_to
Definition: unittype.h:495
Definition: unit.h:134
time_t action_timestamp
Definition: unit.h:203
int length
Definition: unit.h:192
int upkeep[O_LAST]
Definition: unit.h:145
bool has_orders
Definition: unit.h:190
enum action_decision action_decision_want
Definition: unit.h:199
int battlegroup
Definition: unit.h:188
enum unit_activity activity
Definition: unit.h:154
int moves_left
Definition: unit.h:147
struct unit::@76::@78 client
int refcount
Definition: unit.h:137
int id
Definition: unit.h:141
struct unit_list * transporting
Definition: unit.h:181
bool moved
Definition: unit.h:170
int hp
Definition: unit.h:148
int fuel
Definition: unit.h:150
struct extra_type * changed_from_target
Definition: unit.h:167
bool stay
Definition: unit.h:202
enum direction8 facing
Definition: unit.h:138
struct unit::@76::@79 server
struct tile * tile
Definition: unit.h:136
struct extra_type * activity_target
Definition: unit.h:161
int activity_count
Definition: unit.h:159
struct unit_order * list
Definition: unit.h:195
enum unit_activity changed_from
Definition: unit.h:165
struct player * nationality
Definition: unit.h:140
int action_turn
Definition: unit.h:204
int homecity
Definition: unit.h:142
bool paradropped
Definition: unit.h:171
bool done_moving
Definition: unit.h:178
struct unit * transporter
Definition: unit.h:180
struct goods_type * carrying
Definition: unit.h:183
struct tile * goto_tile
Definition: unit.h:152
struct unit::@75 orders
struct tile * action_decision_tile
Definition: unit.h:200
const struct unit_type * utype
Definition: unit.h:135
int veteran
Definition: unit.h:149
int changed_from_count
Definition: unit.h:166
struct player * owner
Definition: unit.h:139
enum server_side_agent ssa_controller
Definition: unit.h:169
int power_fact
Definition: unittype.h:453
int move_bonus
Definition: unittype.h:454
struct civ_map map
Definition: world_object.h:21
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
Definition: support.cpp:537
#define T_UNKNOWN
Definition: terrain.h:51
#define terrain_has_flag(terr, flag)
Definition: terrain.h:260
bool tile_is_placing(const struct tile *ptile)
Is there a placing ongoing?
Definition: tile.cpp:1138
bool tile_has_base_flag_for_unit(const struct tile *ptile, const struct unit_type *punittype, enum base_flag_id flag)
Check if tile contains base providing effect for unit.
Definition: tile.cpp:182
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Time to complete the given activity on the given tile.
Definition: tile.cpp:427
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Check if tile contains base native for unit.
Definition: tile.cpp:327
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Return a known_type enumeration value for the tile.
Definition: tile.cpp:398
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
@ TILE_KNOWN_UNSEEN
Definition: tile.h:30
@ TILE_KNOWN_SEEN
Definition: tile.h:31
#define ACTIVITY_FACTOR
Definition: tile.h:151
#define tile_terrain(_tile)
Definition: tile.h:93
#define tile_owner(_tile)
Definition: tile.h:78
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
Return TRUE iff the two cities are capable of trade; i.e., if a caravan from one city can enter the o...
Activity_type_id real_activities[ACTIVITY_LAST]
Definition: unit.cpp:36
bool is_tile_activity(enum unit_activity activity)
Returns true if given activity affects tile.
Definition: unit.cpp:1480
bool unit_being_aggressive(const struct unit *punit)
An "aggressive" unit is a unit which may cause unhappiness under a Republic or Democracy.
Definition: unit.cpp:1399
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Takes into account unit class flag UCF_ZOC as well as IGZOC.
Definition: unit.cpp:1385
size_t cargo_iter_sizeof()
Returns the size of the unit cargo iterator.
Definition: unit.cpp:2319
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Does unit lose hitpoints each turn?
Definition: unit.cpp:2003
bool is_terrain_change_activity(enum unit_activity activity)
Returns true if given activity changes terrain.
Definition: unit.cpp:1465
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Returns how many shields the unit (type) is worth.
Definition: unit.cpp:204
int get_transporter_occupancy(const struct unit *ptrans)
Return how many units are in the transport.
Definition: unit.cpp:1647
static void cargo_iter_next(struct iterator *it)
Try to find next unit for the cargo iterator.
Definition: unit.cpp:2334
void free_unit_orders(struct unit *punit)
Free and reset the unit's goto route (punit->pgr).
Definition: unit.cpp:1633
bool can_unit_change_homecity(const struct unit *punit)
Return TRUE iff the unit can change homecity at its current location.
Definition: unit.cpp:430
struct unit * transporter_for_unit(const struct unit *pcargo)
Find the best transporter at the given location for the unit.
Definition: unit.cpp:1779
enum unit_upgrade_result unit_upgrade_test(const struct unit *punit, bool is_free)
Tests if the unit could be updated.
Definition: unit.cpp:1826
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Return pointer to ai data of given unit and ai type.
Definition: unit.cpp:2039
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Attach ai data to unit.
Definition: unit.cpp:2047
bool unit_is_alive(int id)
Check if unit with given id is still alive.
Definition: unit.cpp:2014
bool unit_can_est_trade_route_here(const struct unit *punit)
Return TRUE iff this unit can be disbanded at its current location to provide a trade route from the ...
Definition: unit.cpp:267
bool is_hiding_unit(const struct unit *punit)
Is the unit one that is invisible on the map.
Definition: unit.cpp:367
int get_activity_rate_this_turn(const struct unit *punit)
Returns the amount of work a unit does (will do) on an activity this turn.
Definition: unit.cpp:470
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
int get_turns_for_activity_at(const struct unit *punit, enum unit_activity activity, const struct tile *ptile, struct extra_type *tgt)
Return the estimated number of turns for the worker unit to start and complete the activity at the gi...
Definition: unit.cpp:486
bool can_unit_paradrop(const struct unit *punit)
Return whether the unit can be paradropped - that is, if the unit is in a friendly city or on an airb...
Definition: unit.cpp:772
struct unit * is_other_players_unit_tile(const struct tile *ptile, const struct player *pplayer)
Is there an unit belonging to another player on this tile?
Definition: unit.cpp:1269
void unit_upkeep_astr(const struct unit *punit, QString &s)
Append a line of text describing the unit's upkeep to the astring.
Definition: unit.cpp:1174
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Load pcargo onto ptrans.
Definition: unit.cpp:2123
bool unit_can_add_or_build_city(const struct unit *punit)
Return TRUE iff this unit can add to a current city or build a new city at its current location.
Definition: unit.cpp:399
bool is_my_zoc(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Is this square controlled by the pplayer?
Definition: unit.cpp:1341
int unit_transport_depth(const struct unit *pcargo)
Returns the number of unit transport layers which carry unit 'pcargo'.
Definition: unit.cpp:2307
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Assign a new untargeted task to a unit.
Definition: unit.cpp:1011
bool is_losing_hp(const struct unit *punit)
Does unit lose hitpoints each turn?
Definition: unit.cpp:1992
bool can_unit_change_homecity_to(const struct unit *punit, const struct city *pcity)
Return TRUE iff the unit can change homecity to the given city.
Definition: unit.cpp:415
struct unit * is_allied_unit_tile(const struct tile *ptile, const struct player *pplayer)
Returns true if the tile contains an allied unit and only allied units.
Definition: unit.cpp:1211
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
int get_activity_rate(const struct unit *punit)
Returns the speed of a unit doing an activity.
Definition: unit.cpp:443
struct unit * is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer)
Is there an non-allied unit on this tile?
Definition: unit.cpp:1252
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Returns whether 'pcargo' in 'ptrans' is a valid transport.
Definition: unit.cpp:2227
bool is_attack_unit(const struct unit *punit)
Is the unit capable of attacking?
Definition: unit.cpp:288
const Activity_type_id tile_changing_activities[]
Definition: unit.cpp:38
static bool is_real_activity(enum unit_activity activity)
Return if given activity really is in game.
Definition: unit.cpp:573
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 unit_transport_unload(struct unit *pcargo)
Unload pcargo from ptrans.
Definition: unit.cpp:2144
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Returns whether 'pcargo' is transported by 'ptrans', either directly or indirectly.
Definition: unit.cpp:2274
bool can_unit_continue_current_activity(struct unit *punit)
Check if the unit's current activity is actually legal.
Definition: unit.cpp:780
bool is_military_unit(const struct unit *punit)
Military units are capable of enforcing martial law.
Definition: unit.cpp:300
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Is there an occupying unit on this tile?
Definition: unit.cpp:1308
bool can_unit_do_activity_targeted_at(const struct unit *punit, enum unit_activity activity, struct extra_type *target, const struct tile *ptile)
Return TRUE if the unit can do the targeted activity at the given location.
Definition: unit.cpp:854
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
int unit_bribe_cost(struct unit *punit, struct player *briber)
Calculate how expensive it is to bribe the unit.
Definition: unit.cpp:2061
bool is_build_activity(enum unit_activity activity, const struct tile *ptile)
Returns true if given activity is some kind of building.
Definition: unit.cpp:1434
bool is_square_threatened(const struct player *pplayer, const struct tile *ptile, bool omniscient)
Return TRUE iff this tile is threatened from any unit within 2 tiles.
Definition: unit.cpp:327
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Return TRUE iff the given unit could be loaded into the transporter if we moved there.
Definition: unit.cpp:646
bool unit_is_cityfounder(const struct unit *punit)
Is a cityfounder unit?
Definition: unit.cpp:2389
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Return whether any units on the tile are doing this activity.
Definition: unit.cpp:1046
bool can_unit_do_autosettlers(const struct unit *punit)
Return whether the unit can be put in auto-settler mode.
Definition: unit.cpp:548
bool unit_can_help_build_wonder_here(const struct unit *punit)
Return TRUE unless it is known to be imposible to disband this unit at its current position to get fu...
Definition: unit.cpp:243
bool can_unit_alight_or_be_unloaded(const struct unit *pcargo, const struct unit *ptrans)
Return TRUE iff the given unit can leave its current transporter without doing any other action or mo...
Definition: unit.cpp:745
bool is_field_unit(const struct unit *punit)
This checks the "field unit" flag on the unit.
Definition: unit.cpp:355
static bool cargo_iter_valid(const struct iterator *it)
Return whether the iterator is still valid.
Definition: unit.cpp:2364
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
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Return TRUE iff this unit can do any enabler controlled action with the specified action result.
Definition: unit.cpp:318
void set_unit_activity_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type *new_target)
assign a new targeted task to a unit.
Definition: unit.cpp:1028
bool unit_can_convert(const struct unit *punit)
Tests if unit can be converted to another type.
Definition: unit.cpp:1877
int unit_cargo_depth(const struct unit *ptrans)
Returns the number of unit cargo layers within transport 'ptrans'.
Definition: unit.cpp:2289
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Helper for unit_transport_check().
Definition: unit.cpp:2211
bool can_unit_do_activity_targeted(const struct unit *punit, enum unit_activity activity, struct extra_type *target)
Return whether the unit can do the targeted activity at its current location.
Definition: unit.cpp:842
#define CARGO_ITER(iter)
Definition: unit.cpp:49
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Returns the list of cargo units.
Definition: unit.cpp:2199
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Return a mask of the extras which are actively (currently) being pillaged on the given tile.
Definition: unit.cpp:1063
int get_transporter_capacity(const struct unit *punit)
Return the number of units the transporter can hold (or 0).
Definition: unit.cpp:280
struct unit * is_non_attack_unit_tile(const struct tile *ptile, const struct player *pplayer)
Is there an unit we have peace or ceasefire with on this tile?
Definition: unit.cpp:1286
bool is_clean_activity(enum unit_activity activity)
Returns true if given activity is some kind of cleaning.
Definition: unit.cpp:1450
bool unit_can_airlift_to(const struct unit *punit, const struct city *pdest_city)
Determines if punit can be airlifted to dest_city now! So punit needs to be in a city now.
Definition: unit.cpp:179
enum unit_upgrade_result unit_upgrade_info(const struct unit *punit, char *buf, size_t bufsz)
Find the result of trying to upgrade the unit, and a message that most callers can use directly.
Definition: unit.cpp:1900
bool can_unit_do_activity(const struct unit *punit, enum unit_activity activity)
Return TRUE iff the unit can do the given untargeted activity at its current location.
Definition: unit.cpp:806
static struct unit * base_transporter_for_unit(const struct unit *pcargo, const struct tile *ptile, bool(*unit_load_test)(const struct unit *pc, const struct unit *pt))
Helper for transporter_for_unit() and transporter_for_unit_at()
Definition: unit.cpp:1657
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
Definition: unit.cpp:1588
const char * get_activity_text(enum unit_activity activity)
Return the name of the activity in a static buffer.
Definition: unit.cpp:586
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Create a virtual unit skeleton.
Definition: unit.cpp:1490
void unit_tile_set(struct unit *punit, struct tile *ptile)
Set the tile location of the unit.
Definition: unit.cpp:1200
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Initialize the cargo iterator.
Definition: unit.cpp:2372
static void * cargo_iter_get(const struct iterator *it)
Get the unit of the cargo iterator.
Definition: unit.cpp:2324
enum unit_airlift_result test_unit_can_airlift_to(const struct player *restriction, const struct unit *punit, const struct city *pdest_city)
Determines if punit can be airlifted to dest_city now! So punit needs to be in a city now.
Definition: unit.cpp:75
const QString unit_activity_text(const struct unit *punit)
Return text describing the unit's current activity as a static string.
Definition: unit.cpp:1085
bool kills_citizen_after_attack(const struct unit *punit)
Return TRUE iff an attack from this unit would kill a citizen in a city (city walls protect against t...
Definition: unit.cpp:389
bool unit_transported(const struct unit *pcargo)
Returns TRUE iff the unit is transported.
Definition: unit.cpp:2176
bool are_unit_orders_equal(const struct unit_order *order1, const struct unit_order *order2)
Checks unit orders for equality.
Definition: unit.cpp:54
bool unit_is_virtual(const struct unit *punit)
Return TRUE if this is a valid unit pointer but does not correspond to any unit that exists in the ga...
Definition: unit.cpp:2027
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity)
Assign a new task to a unit.
Definition: unit.cpp:993
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Return TRUE iff the given unit can be unloaded from its current transporter.
Definition: unit.cpp:720
void unit_activity_astr(const struct unit *punit, QString &s)
Append text describing the unit's current activity to the given astring.
Definition: unit.cpp:1096
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Check if unit of given type would be able to transport all of transport's cargo.
Definition: unit.cpp:1799
bool unit_has_orders(const struct unit *punit)
Return TRUE iff the unit is following client-side orders.
Definition: unit.cpp:195
struct player * unit_nationality(const struct unit *punit)
Return the nationality of the unit.
Definition: unit.cpp:1190
bool activity_requires_target(enum unit_activity activity)
Return TRUE if activity requires some sort of target to be specified.
Definition: unit.cpp:506
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
void setup_real_activities_array()
Setup array of real activities.
Definition: unit.cpp:556
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_transports_iterate_end
Definition: unit.h:452
#define unit_cargo_iterate_end
Definition: unit.h:464
@ FOCUS_AVAIL
Definition: unit.h:46
#define BATTLEGROUP_NONE
Definition: unit.h:187
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition: unit.h:461
#define unit_owner(_pu)
Definition: unit.h:370
#define unit_transports_iterate(_pcargo, _ptrans)
Definition: unit.h:447
unit_upgrade_result
Definition: unit.h:48
@ UU_NO_MONEY
Definition: unit.h:51
@ UU_NOT_IN_CITY
Definition: unit.h:52
@ UU_NO_UNITTYPE
Definition: unit.h:50
@ UU_NOT_TERRAIN
Definition: unit.h:55
@ UU_UNSUITABLE_TRANSPORT
Definition: unit.h:56
@ UU_NOT_CITY_OWNER
Definition: unit.h:53
@ UU_NOT_ENOUGH_ROOM
Definition: unit.h:54
@ UU_OK
Definition: unit.h:49
unit_airlift_result
Definition: unit.h:59
@ AR_SRC_NO_FLIGHTS
Definition: unit.h:71
@ AR_OK_SRC_UNKNOWN
Definition: unit.h:62
@ AR_OK_DST_UNKNOWN
Definition: unit.h:63
@ AR_NO_MOVES
Definition: unit.h:65
@ AR_BAD_DST_CITY
Definition: unit.h:70
@ AR_NOT_IN_CITY
Definition: unit.h:68
@ AR_OCCUPIED
Definition: unit.h:67
@ AR_OK
Definition: unit.h:61
@ AR_DST_NO_FLIGHTS
Definition: unit.h:72
@ AR_WRONG_UNITTYPE
Definition: unit.h:66
@ AR_BAD_SRC_CITY
Definition: unit.h:69
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_safe(unitlist, _unit)
Definition: unitlist.h:33
#define unit_list_iterate_end
Definition: unitlist.h:27
#define unit_list_iterate_safe_end
Definition: unitlist.h:54
const char * unit_rule_name(const struct unit *punit)
Return the (untranslated) rule name of the unit.
Definition: unittype.cpp:1283
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
int utype_pays_mp_for_action_base(const struct action *paction, const struct unit_type *putype)
Returns the amount of movement points successfully performing the specified action will consume in th...
Definition: unittype.cpp:1083
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Return TRUE iff the given cargo type has no restrictions on when it can unload from the given transpo...
Definition: unittype.cpp:239
bool utype_is_cityfounder(const struct unit_type *utype)
Is cityfounder type.
Definition: unittype.cpp:2549
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Return whether this player can upgrade this unit type (to any other unit type).
Definition: unittype.cpp:1379
int utype_build_shield_cost_base(const struct unit_type *punittype)
Returns the number of shields this unit type represents.
Definition: unittype.cpp:1168
bool utype_may_act_move_frags(const struct unit_type *punit_type, const action_id act_id, const int move_fragments)
Return TRUE iff the given (action enabler controlled) action may be performed by a unit of the given ...
Definition: unittype.cpp:804
int unit_build_shield_cost_base(const struct unit *punit)
Returns the number of shields this unit represents.
Definition: unittype.cpp:1185
bool utype_can_freely_load(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Return TRUE iff the given cargo type has no restrictions on when it can load onto the given transport...
Definition: unittype.cpp:227
struct unit_class * unit_class_get(const struct unit *punit)
Returns unit class pointer for a unit.
Definition: unittype.cpp:2151
int utype_veteran_levels(const struct unit_type *punittype)
Return veteran levels of the given unit type.
Definition: unittype.cpp:2254
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_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
Definition: unittype.cpp:176
const char * utype_name_translation(const struct unit_type *punittype)
Return the (translated) name of the unit type.
Definition: unittype.cpp:1256
const char * unit_name_translation(const struct unit *punit)
Return the (translated) name of the unit.
Definition: unittype.cpp:1265
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
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
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Return the cost (gold) of upgrading a single unit of the specified type to the new type.
Definition: unittype.cpp:1407
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition: unittype.h:704
#define utype_class(_t_)
Definition: unittype.h:691
#define utype_fuel(ptype)
Definition: unittype.h:772
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition: unittype.h:584