Freeciv21
Develop your civilization from humble roots to a global empire
unittype.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include <cmath> // ceil
15 #include <cstring>
16 
17 // utility
18 #include "astring.h"
19 #include "fcintl.h"
20 #include "log.h"
21 #include "shared.h"
22 #include "support.h"
23 
24 // common
25 #include "ai.h"
26 #include "combat.h"
27 #include "game.h"
28 #include "government.h"
29 #include "movement.h"
30 #include "player.h"
31 #include "research.h"
32 #include "unitlist.h"
33 
34 #include "unittype.h"
35 
36 #define MAX_UNIT_ROLES L_LAST + ACTION_COUNT
37 
38 static struct unit_type unit_types[U_LAST];
39 static struct unit_class unit_classes[UCL_LAST];
40 /* the unit_types and unit_classes arrays are now setup in:
41  server/ruleset.c (for the server)
42  client/packhand.c (for the client)
43 */
44 
46 
48 
53 {
54  if (game.control.num_unit_types > 0) {
55  return unit_types;
56  }
57  return nullptr;
58 }
59 
64 {
65  if (game.control.num_unit_types > 0) {
66  return &unit_types[game.control.num_unit_types - 1];
67  }
68  return nullptr;
69 }
70 
74 Unit_type_id utype_count() { return game.control.num_unit_types; }
75 
82 Unit_type_id utype_index(const struct unit_type *punittype)
83 {
84  fc_assert_ret_val(punittype, 0);
85  return punittype - unit_types;
86 }
87 
91 Unit_type_id utype_number(const struct unit_type *punittype)
92 {
93  fc_assert_ret_val(punittype, 0);
94  return punittype->item_number;
95 }
96 
104 {
105  if (id < 0 || id >= game.control.num_unit_types) {
106  return nullptr;
107  }
108  return &unit_types[id];
109 }
110 
114 const struct unit_type *unit_type_get(const struct unit *punit)
115 {
116  fc_assert_ret_val(nullptr != punit, nullptr);
117  return punit->utype;
118 }
119 
123 int utype_upkeep_cost(const struct unit_type *ut, struct player *pplayer,
124  Output_type_id otype)
125 {
126  int val = ut->upkeep[otype], gold_upkeep_factor;
127 
128  if (BV_ISSET(ut->flags, UTYF_FANATIC)
129  && get_player_bonus(pplayer, EFT_FANATICS) > 0) {
130  // Special case: fanatics have no upkeep under fanaticism.
131  return 0;
132  }
133 
134  /* switch shield upkeep to gold upkeep if
135  - the effect 'EFT_SHIELD2GOLD_FACTOR' is non-zero (it gives the
136  conversion factor in percent) and
137  - the unit has the corresponding flag set (UTYF_SHIELD2GOLD)
138  FIXME: Should the ai know about this? */
139  if (utype_has_flag(ut, UTYF_SHIELD2GOLD)
140  && (otype == O_GOLD || otype == O_SHIELD)) {
141  gold_upkeep_factor =
142  get_unittype_bonus(pplayer, nullptr, ut, EFT_SHIELD2GOLD_FACTOR);
143  if (gold_upkeep_factor > 0) {
144  switch (otype) {
145  case O_GOLD:
146  val = ceil((0.01 * gold_upkeep_factor) * ut->upkeep[O_SHIELD]);
147  break;
148  case O_SHIELD:
149  val = 0;
150  break;
151  default:
152  fc_assert(otype == O_GOLD || otype == O_SHIELD);
153  break;
154  }
155  }
156  }
157 
158  val *= get_player_output_bonus(pplayer, get_output_type(otype),
159  EFT_UPKEEP_FACTOR);
160  return val;
161 }
162 
167 int utype_happy_cost(const struct unit_type *ut,
168  const struct player *pplayer)
169 {
170  return ut->happy_cost * get_player_bonus(pplayer, EFT_UNHAPPY_FACTOR);
171 }
172 
176 bool unit_has_type_flag(const struct unit *punit,
177  enum unit_type_flag_id flag)
178 {
179  return utype_has_flag(unit_type_get(punit), flag);
180 }
181 
186 bool utype_has_role(const struct unit_type *punittype, int role)
187 {
188  fc_assert_ret_val(role >= L_FIRST && role < L_LAST, false);
189  return BV_ISSET(punittype->roles, role - L_FIRST);
190 }
191 
195 bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
196 {
197  return utype_has_role(unit_type_get(punit), role);
198 }
199 
203 bool unit_can_take_over(const struct unit *punit)
204 {
205  /* TODO: Should unit state dependent action enablers be considered?
206  * Some callers aren't yet ready for changeable unit state (like current
207  * location) playing a role. */
208  return unit_owner(punit)->ai_common.barbarian_type != ANIMAL_BARBARIAN
210 }
211 
215 bool utype_can_take_over(const struct unit_type *punittype)
216 {
217  /* FIXME: "Paradrop Unit" can in certain circumstances result in city
218  * conquest. */
219  return utype_can_do_action_result(punittype, ACTRES_CONQUER_CITY);
220 }
221 
227 bool utype_can_freely_load(const struct unit_type *pcargotype,
228  const struct unit_type *ptranstype)
229 {
230  return BV_ISSET(pcargotype->embarks,
231  uclass_index(utype_class(ptranstype)));
232 }
233 
239 bool utype_can_freely_unload(const struct unit_type *pcargotype,
240  const struct unit_type *ptranstype)
241 {
242  return BV_ISSET(pcargotype->disembarks,
243  uclass_index(utype_class(ptranstype)));
244 }
245 
249 static bool action_is_hostile(action_id act_id)
250 {
251  struct action *paction = action_by_number(act_id);
252 
253  switch (paction->result) {
254  case ACTRES_SPY_INVESTIGATE_CITY:
255  case ACTRES_SPY_POISON:
256  case ACTRES_SPY_STEAL_GOLD:
257  case ACTRES_SPY_SABOTAGE_CITY:
258  case ACTRES_SPY_TARGETED_SABOTAGE_CITY:
259  case ACTRES_SPY_SABOTAGE_CITY_PRODUCTION:
260  case ACTRES_SPY_STEAL_TECH:
261  case ACTRES_SPY_TARGETED_STEAL_TECH:
262  case ACTRES_SPY_INCITE_CITY:
263  case ACTRES_SPY_BRIBE_UNIT:
264  case ACTRES_SPY_SABOTAGE_UNIT:
265  case ACTRES_CAPTURE_UNITS:
266  case ACTRES_STEAL_MAPS:
267  case ACTRES_BOMBARD:
268  case ACTRES_SPY_NUKE:
269  case ACTRES_NUKE:
270  case ACTRES_NUKE_CITY:
271  case ACTRES_NUKE_UNITS:
272  case ACTRES_DESTROY_CITY:
273  case ACTRES_EXPEL_UNIT:
274  case ACTRES_STRIKE_BUILDING:
275  case ACTRES_STRIKE_PRODUCTION:
276  case ACTRES_ATTACK:
277  case ACTRES_CONQUER_CITY:
278  case ACTRES_PILLAGE:
279  case ACTRES_SPY_ATTACK:
280  case ACTRES_SPY_SPREAD_PLAGUE:
281  return true;
282  case ACTRES_ESTABLISH_EMBASSY:
283  case ACTRES_TRADE_ROUTE:
284  case ACTRES_MARKETPLACE:
285  case ACTRES_HELP_WONDER:
286  case ACTRES_FOUND_CITY:
287  case ACTRES_JOIN_CITY:
288  case ACTRES_RECYCLE_UNIT:
289  case ACTRES_DISBAND_UNIT:
290  case ACTRES_HOME_CITY:
291  case ACTRES_UPGRADE_UNIT:
292  case ACTRES_PARADROP:
293  case ACTRES_AIRLIFT:
294  case ACTRES_HEAL_UNIT:
295  case ACTRES_TRANSFORM_TERRAIN:
296  case ACTRES_CULTIVATE:
297  case ACTRES_PLANT:
298  case ACTRES_CLEAN_POLLUTION:
299  case ACTRES_CLEAN_FALLOUT:
300  case ACTRES_FORTIFY:
301  case ACTRES_ROAD:
302  case ACTRES_CONVERT:
303  case ACTRES_BASE:
304  case ACTRES_MINE:
305  case ACTRES_IRRIGATE:
306  case ACTRES_TRANSPORT_ALIGHT:
307  case ACTRES_TRANSPORT_UNLOAD:
308  case ACTRES_TRANSPORT_DISEMBARK:
309  case ACTRES_TRANSPORT_BOARD:
310  case ACTRES_TRANSPORT_EMBARK:
311  return false;
312  case ACTRES_NONE:
313  // Assume they are up to something.
314  return true;
315  }
316 
317  // Should not be reached.
318  fc_assert(false);
319  return false;
320 }
321 
322 // Fake action id representing any hostile action.
323 #define ACTION_HOSTILE ACTION_COUNT + 1
324 
325 // Number of real and fake actions.
326 #define ACTION_AND_FAKES ACTION_HOSTILE + 1
327 
328 /* Cache of what generalized (ruleset defined) action enabler controlled
329  * actions units of each type can perform. Checking if any action can be
330  * done at all is done via the fake action id ACTION_ANY. If any hostile
331  * action can be performed is done via ACTION_HOSTILE. */
332 static bv_unit_types unit_can_act_cache[ACTION_AND_FAKES];
333 
338 static void unit_can_act_cache_set(struct unit_type *putype)
339 {
340  // Clear old values.
341  action_iterate(act_id)
342  {
343  BV_CLR(unit_can_act_cache[act_id], utype_index(putype));
344  }
348 
349  /* See if the unit type can do an action controlled by generalized action
350  * enablers */
351  action_enablers_iterate(enabler)
352  {
353  const struct action *paction = action_by_number(enabler->action);
354  if (action_id_get_actor_kind(enabler->action) == AAK_UNIT
355  && action_actor_utype_hard_reqs_ok(paction->result, putype)
357  &(enabler->actor_reqs))) {
358  log_debug("act_cache: %s can %s", utype_rule_name(putype),
359  action_id_rule_name(enabler->action));
360  BV_SET(unit_can_act_cache[enabler->action], utype_index(putype));
362  if (action_is_hostile(enabler->action)) {
364  }
365  }
366  }
368 }
369 
374 bool utype_may_act_at_all(const struct unit_type *putype)
375 {
376  return utype_can_do_action(putype, ACTION_ANY);
377 }
378 
386 bool utype_can_do_action(const struct unit_type *putype,
387  const action_id act_id)
388 {
389  fc_assert_ret_val(putype, false);
390  fc_assert_ret_val(act_id >= 0 && act_id < ACTION_AND_FAKES, false);
391 
392  return BV_ISSET(unit_can_act_cache[act_id], utype_index(putype));
393 }
394 
402 bool utype_can_do_action_result(const struct unit_type *putype,
403  enum action_result result)
404 {
405  fc_assert_ret_val(putype, false);
406 
407  action_by_result_iterate(paction, act_id, result)
408  {
409  if (utype_can_do_action(putype, paction->id)) {
410  return true;
411  }
412  }
414 
415  return false;
416 }
417 
422 static bool utype_can_do_action_role(const struct unit_type *putype,
423  const int role)
424 {
425  return utype_can_do_action(putype, role - L_LAST);
426 }
427 
432 bool utype_acts_hostile(const struct unit_type *putype)
433 {
434  return utype_can_do_action(putype, ACTION_HOSTILE);
435 }
436 
437 /* Cache if any action at all may be possible when the actor unit's state
438  * is...
439  * bit 0 to USP_COUNT - 1: Possible when the corresponding property is TRUE
440  * bit USP_COUNT to ((USP_COUNT * 2) - 1): Possible when the corresponding
441  * property is FALSE
442  */
443 BV_DEFINE(bv_ustate_act_cache, USP_COUNT * 2);
444 
445 /* Cache what actions may be possible when the target's local citytile state
446  * is
447  * bit 0 to CITYT_LAST - 1: Possible when the corresponding property is TRUE
448  * bit USP_COUNT to ((CITYT_LAST * 2) - 1): Possible when the corresponding
449  * property is FALSE
450  */
451 BV_DEFINE(bv_citytile_cache, CITYT_LAST * 2);
452 
453 // Cache position lookup functions
454 #define requirement_unit_state_ereq(_id_, _present_) \
455  requirement_kind_ereq(_id_, REQ_RANGE_LOCAL, _present_, USP_COUNT)
456 #define requirement_citytile_ereq(_id_, _present_) \
457  requirement_kind_ereq(_id_, REQ_RANGE_LOCAL, _present_, CITYT_LAST)
458 
459 // Caches for each unit type
460 static bv_ustate_act_cache ustate_act_cache[U_LAST][ACTION_AND_FAKES];
461 static bv_diplrel_all_reqs dipl_rel_action_cache[U_LAST][ACTION_AND_FAKES];
462 static bv_citytile_cache ctile_tgt_act_cache[U_LAST][ACTION_AND_FAKES];
463 
469 static void unit_state_action_cache_set(struct unit_type *putype)
470 {
471  struct requirement req;
472  int uidx = utype_index(putype);
473 
474  /* The unit is not yet known to be allowed to perform any actions no
475  * matter what its unit state is. */
476  action_iterate(act_id) { BV_CLR_ALL(ustate_act_cache[uidx][act_id]); }
480 
481  if (!utype_may_act_at_all(putype)) {
482  // Not an actor unit.
483  return;
484  }
485 
486  // Common for every situation
487  req.range = REQ_RANGE_LOCAL;
488  req.survives = false;
489  req.source.kind = VUT_UNITSTATE;
490 
491  for (req.source.value.unit_state = ustate_prop_begin();
492  req.source.value.unit_state != ustate_prop_end();
493  req.source.value.unit_state =
494  ustate_prop_next(req.source.value.unit_state)) {
495  /* No action will ever be possible in a specific unit state if the
496  * opposite unit state is required in all action enablers.
497  * No unit state except present and !present of the same property
498  * implies or conflicts with another so the tests can be simple. */
499  action_enablers_iterate(enabler)
500  {
501  if (requirement_fulfilled_by_unit_type(putype, &(enabler->actor_reqs))
502  && action_id_get_actor_kind(enabler->action) == AAK_UNIT) {
503  bool present = true;
504  do {
505  // OK if not mentioned
506  req.present = present;
507  if (!is_req_in_vec(&req, &(enabler->actor_reqs))) {
508  BV_SET(ustate_act_cache[utype_index(putype)][enabler->action],
510  !req.present));
511  if (action_is_hostile(enabler->action)) {
514  !req.present));
515  }
518  !req.present));
519  }
520  present = !present;
521  } while (!present);
522  }
523  }
525  }
526 }
527 
536 static void local_dipl_rel_action_cache_set(struct unit_type *putype)
537 {
538  struct requirement req;
539  struct requirement tile_is_claimed;
540  int uidx = utype_index(putype);
541 
542  /* The unit is not yet known to be allowed to perform any actions no
543  * matter what the diplomatic state is. */
544  action_iterate(act_id) { BV_CLR_ALL(dipl_rel_action_cache[uidx][act_id]); }
548 
549  if (!utype_may_act_at_all(putype)) {
550  // Not an actor unit.
551  return;
552  }
553 
554  // Tile is claimed as a requirement.
555  tile_is_claimed.range = REQ_RANGE_LOCAL;
556  tile_is_claimed.survives = false;
557  tile_is_claimed.source.kind = VUT_CITYTILE;
558  tile_is_claimed.present = true;
559  tile_is_claimed.source.value.citytile = CITYT_CLAIMED;
560 
561  // Common for every situation
562  req.range = REQ_RANGE_LOCAL;
563  req.survives = false;
564  req.source.kind = VUT_DIPLREL;
565 
566  // DiplRel starts with diplstate_type and ends with diplrel_other
567  for (req.source.value.diplrel = diplstate_type_begin();
568  req.source.value.diplrel != DRO_LAST; req.source.value.diplrel++) {
569  /* No action will ever be possible in a specific diplomatic relation if
570  * its presence contradicts all action enablers.
571  * Everything was set to false above. It is therefore OK to only change
572  * the cache when units can do an action given a certain diplomatic
573  * relationship property value. */
574  action_enablers_iterate(enabler)
575  {
576  if (requirement_fulfilled_by_unit_type(putype, &(enabler->actor_reqs))
577  && action_id_get_actor_kind(enabler->action) == AAK_UNIT
578  && (action_id_get_target_kind(enabler->action) != ATK_TILE
579  // No diplomatic relation to Nature
580  || !does_req_contradicts_reqs(&tile_is_claimed,
581  &enabler->target_reqs))) {
582  bool present = true;
583  do {
584  req.present = present;
585  if (!does_req_contradicts_reqs(&req, &(enabler->actor_reqs))) {
586  BV_SET(dipl_rel_action_cache[uidx][enabler->action],
588  REQ_RANGE_LOCAL, req.present));
589  if (action_is_hostile(enabler->action)) {
592  REQ_RANGE_LOCAL, req.present));
593  }
596  REQ_RANGE_LOCAL, req.present));
597  }
598  present = !present;
599  } while (!present);
600  }
601  }
603  }
604 }
605 
611 static void tgt_citytile_act_cache_set(struct unit_type *putype)
612 {
613  struct requirement req;
614  int uidx = utype_index(putype);
615 
616  /* The unit is not yet known to be allowed to perform any actions no
617  * matter what its target's CityTile state is. */
618  action_iterate(act_id) { BV_CLR_ALL(ctile_tgt_act_cache[uidx][act_id]); }
622 
623  if (!utype_may_act_at_all(putype)) {
624  // Not an actor unit.
625  return;
626  }
627 
628  // Common for every situation
629  req.range = REQ_RANGE_LOCAL;
630  req.survives = false;
631  req.source.kind = VUT_CITYTILE;
632 
633  for (req.source.value.citytile = citytile_type_begin();
634  req.source.value.citytile != citytile_type_end();
635  req.source.value.citytile =
636  citytile_type_next(req.source.value.citytile)) {
637  /* No action will ever be possible in a target CityTile state if the
638  * opposite target CityTile state is required in all action enablers.
639  * No CityTile property state except present and !present of the same
640  * property implies or conflicts with another so the tests can be
641  * simple. */
642  action_enablers_iterate(enabler)
643  {
644  if (requirement_fulfilled_by_unit_type(putype, &(enabler->target_reqs))
645  && action_id_get_actor_kind(enabler->action) == AAK_UNIT) {
646  bool present = true;
647  do {
648  // OK if not mentioned
649  req.present = present;
650  if (!is_req_in_vec(&req, &(enabler->target_reqs))) {
651  BV_SET(ctile_tgt_act_cache[utype_index(putype)][enabler->action],
653  !req.present));
654  if (action_is_hostile(enabler->action)) {
655  BV_SET(
658  !req.present));
659  }
662  !req.present));
663  }
664  present = !present;
665  } while (!present);
666  }
667  }
669  }
670 }
671 
672 struct range {
673  int min;
674  int max;
675 };
676 
677 #define MOVES_LEFT_INFINITY -1
678 
683 static struct range *moves_left_range(struct requirement_vector *reqs)
684 {
685  auto *prange = new range;
686 
687  prange->min = 0;
688  prange->max = MOVES_LEFT_INFINITY;
689 
691  {
692  if (preq->source.kind == VUT_MINMOVES) {
693  if (preq->present) {
694  prange->min = preq->source.value.minmoves;
695  } else {
696  prange->max = preq->source.value.minmoves;
697  }
698  }
699  }
701 
702  return prange;
703 }
704 
711 {
712  unit_can_act_cache_set(ptype);
716 }
717 
723 {
726 }
727 
733 bool can_unit_act_when_ustate_is(const struct unit_type *punit_type,
734  const enum ustate_prop prop,
735  const bool is_there)
736 {
737  return utype_can_do_act_when_ustate(punit_type, ACTION_ANY, prop,
738  is_there);
739 }
740 
746 bool utype_can_do_act_when_ustate(const struct unit_type *punit_type,
747  const action_id act_id,
748  const enum ustate_prop prop,
749  const bool is_there)
750 {
751  fc_assert_ret_val(punit_type, false);
752  fc_assert_ret_val(act_id >= 0 && act_id < ACTION_AND_FAKES, false);
753 
754  return BV_ISSET(ustate_act_cache[utype_index(punit_type)][act_id],
755  requirement_unit_state_ereq(prop, is_there));
756 }
757 
763 bool utype_can_do_act_if_tgt_citytile(const struct unit_type *punit_type,
764  const action_id act_id,
765  const enum citytile_type prop,
766  const bool is_there)
767 {
768  fc_assert_ret_val(punit_type, false);
769  fc_assert_ret_val(act_id >= 0 && act_id < ACTION_AND_FAKES, false);
770 
771  return BV_ISSET(ctile_tgt_act_cache[utype_index(punit_type)][act_id],
772  requirement_citytile_ereq(prop, is_there));
773 }
774 
784 bool can_utype_do_act_if_tgt_diplrel(const struct unit_type *punit_type,
785  const action_id act_id, const int prop,
786  const bool is_there)
787 {
788  fc_assert_ret_val(punit_type, false);
789  fc_assert_ret_val(act_id >= 0 && act_id < ACTION_AND_FAKES, false);
790 
791  return BV_ISSET(dipl_rel_action_cache[utype_index(punit_type)][act_id],
792  requirement_diplrel_ereq(prop, REQ_RANGE_LOCAL, is_there));
793 }
794 
804 bool utype_may_act_move_frags(const struct unit_type *punit_type,
805  const action_id act_id,
806  const int move_fragments)
807 {
808  struct range *ml_range;
809 
810  fc_assert(action_id_exists(act_id) || act_id == ACTION_ANY);
811 
812  if (!utype_may_act_at_all(punit_type)) {
813  // Not an actor unit.
814  return false;
815  }
816 
817  if (act_id == ACTION_ANY) {
818  // Any action is OK.
819  action_iterate(alt_act)
820  {
821  if (utype_may_act_move_frags(punit_type, alt_act, move_fragments)) {
822  // It only has to be true for one action.
823  return true;
824  }
825  }
827 
828  // No action enabled.
829  return false;
830  }
831 
832  if (action_id_get_actor_kind(act_id) != AAK_UNIT) {
833  /* This action isn't performed by any unit at all so this unit type
834  * can't do it. */
835  return false;
836  }
837 
839  {
840  if (!requirement_fulfilled_by_unit_type(punit_type,
841  &(enabler->actor_reqs))) {
842  // This action enabler isn't for this unit type at all.
843  continue;
844  }
845 
846  ml_range = moves_left_range(&(enabler->actor_reqs));
847  if (ml_range->min <= move_fragments
848  && (ml_range->max == MOVES_LEFT_INFINITY
849  || ml_range->max > move_fragments)) {
850  // The number of move fragments is in range of the action enabler.
851  delete ml_range;
852  return true;
853  }
854 
855  delete ml_range;
856  }
858 
859  return false;
860 }
861 
870 bool utype_may_act_tgt_city_tile(const struct unit_type *punit_type,
871  const action_id act_id,
872  const enum citytile_type prop,
873  const bool is_there)
874 {
875  struct requirement req;
876 
877  if (!utype_may_act_at_all(punit_type)) {
878  // Not an actor unit.
879  return false;
880  }
881 
882  if (act_id == ACTION_ANY) {
883  // Any action is OK.
884  action_iterate(alt_act)
885  {
886  if (utype_may_act_tgt_city_tile(punit_type, alt_act, prop, is_there)) {
887  // It only has to be true for one action.
888  return true;
889  }
890  }
892 
893  // No action enabled.
894  return false;
895  }
896 
897  if (action_id_get_actor_kind(act_id) != AAK_UNIT) {
898  /* This action isn't performed by any unit at all so this unit type
899  * can't do it. */
900  return false;
901  }
902 
903  // Common for every situation
904  req.range = REQ_RANGE_LOCAL;
905  req.survives = false;
906  req.source.kind = VUT_CITYTILE;
907 
908  // Will only check for the specified is_there
909  req.present = is_there;
910 
911  // Will only check the specified property
912  req.source.value.citytile = prop;
913 
915  {
916  if (!requirement_fulfilled_by_unit_type(punit_type,
917  &(enabler->actor_reqs))) {
918  // This action enabler isn't for this unit type at all.
919  continue;
920  }
921 
922  if (!does_req_contradicts_reqs(&req, &(enabler->target_reqs))) {
923  // This action isn't blocked by the given target tile property.
924  return true;
925  }
926  }
928 
929  return false;
930 }
931 
936 bool utype_is_consumed_by_action(const struct action *paction,
937  const struct unit_type *utype)
938 {
939  Q_UNUSED(utype)
940  return paction->actor_consuming_always;
941 }
942 
947 bool utype_is_consumed_by_action_result(enum action_result result,
948  const struct unit_type *utype)
949 {
950  action_by_result_iterate(paction, act_id, result)
951  {
952  if (!utype_can_do_action(utype, paction->id)) {
953  continue;
954  }
955 
956  if (utype_is_consumed_by_action(paction, utype)) {
957  return true;
958  }
959  }
961 
962  return false;
963 }
964 
969 bool utype_is_moved_to_tgt_by_action(const struct action *paction,
970  const struct unit_type *utype)
971 {
972  Q_UNUSED(utype)
973  fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, false);
974 
975  if (paction->actor_consuming_always) {
976  return false;
977  }
978 
979  switch (paction->actor.is_unit.moves_actor) {
980  case MAK_STAYS:
981  // Stays at the tile were it was.
982  return false;
983  case MAK_REGULAR:
984  /* A "regular" move. Does it charge the move cost of a regular move?
985  * Update utype_pays_for_regular_move_to_tgt() if yes. */
986  return true;
987  case MAK_TELEPORT:
988  // A teleporting move.
989  return true;
990  case MAK_FORCED:
991  // Tries a forced move under certain conditions.
992  return false;
993  case MAK_ESCAPE:
994  // May die, may be teleported to a safe city.
995  return false;
996  case MAK_UNREPRESENTABLE:
997  // Too complex to determine.
998  return false;
999  }
1000 
1001  fc_assert_msg(false, "Should not reach this code.");
1002  return false;
1003 }
1004 
1009 bool utype_is_unmoved_by_action(const struct action *paction,
1010  const struct unit_type *utype)
1011 {
1012  Q_UNUSED(utype)
1013  fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, false);
1014 
1015  if (paction->actor_consuming_always) {
1016  // Dead counts as moved off the board
1017  return false;
1018  }
1019 
1020  switch (paction->actor.is_unit.moves_actor) {
1021  case MAK_STAYS:
1022  // Stays at the tile were it was.
1023  return true;
1024  case MAK_REGULAR:
1025  /* A "regular" move. Does it charge the move cost of a regular move?
1026  * Update utype_pays_for_regular_move_to_tgt() if yes. */
1027  return false;
1028  case MAK_TELEPORT:
1029  // A teleporting move.
1030  return false;
1031  case MAK_FORCED:
1032  // Tries a forced move under certain conditions.
1033  return false;
1034  case MAK_ESCAPE:
1035  // May die, may be teleported to a safe city.
1036  return false;
1037  case MAK_UNREPRESENTABLE:
1038  // Too complex to determine.
1039  return false;
1040  }
1041 
1042  fc_assert_msg(false, "Should not reach this code.");
1043  return false;
1044 }
1045 
1052 bool utype_pays_for_regular_move_to_tgt(const struct action *paction,
1053  const struct unit_type *utype)
1054 {
1055  if (!utype_is_moved_to_tgt_by_action(paction, utype)) {
1056  // Not even a move.
1057  return false;
1058  }
1059 
1060  if (action_has_result(paction, ACTRES_CONQUER_CITY)) {
1061  // Moves into the city to occupy it.
1062  return true;
1063  }
1064 
1065  if (action_has_result(paction, ACTRES_TRANSPORT_DISEMBARK)) {
1066  // Moves out of the transport to disembark.
1067  return true;
1068  }
1069 
1070  if (action_has_result(paction, ACTRES_TRANSPORT_EMBARK)) {
1071  // Moves into the transport to embark.
1072  return true;
1073  }
1074 
1075  return false;
1076 }
1077 
1083 int utype_pays_mp_for_action_base(const struct action *paction,
1084  const struct unit_type *putype)
1085 {
1086  int mpco = 0;
1087 
1088  if (action_has_result(paction, ACTRES_ATTACK)
1089  && !utype_is_consumed_by_action(paction, putype)) {
1090  if (utype_has_flag(putype, UTYF_ONEATTACK)) {
1091  mpco += MAX_MOVE_FRAGS;
1092  } else {
1093  mpco += SINGLE_MOVE;
1094  }
1095  }
1096 
1097  return mpco;
1098 }
1099 
1104 int utype_pays_mp_for_action_estimate(const struct action *paction,
1105  const struct unit_type *putype,
1106  const struct player *act_player,
1107  const struct tile *act_tile,
1108  const struct tile *tgt_tile)
1109 {
1110  const struct tile *post_action_tile;
1111  int mpco = utype_pays_mp_for_action_base(paction, putype);
1112 
1113  if (utype_is_moved_to_tgt_by_action(paction, putype)) {
1114  post_action_tile = tgt_tile;
1115  } else {
1116  /* FIXME: Not 100% true. May escape, have a probability for moving to
1117  * target tile etc. */
1118  post_action_tile = act_tile;
1119  }
1120 
1121  if (utype_pays_for_regular_move_to_tgt(paction, putype)) {
1122  // Add the cost from the move.
1123  mpco +=
1124  map_move_cost(&(wld.map), act_player, putype, act_tile, tgt_tile);
1125  }
1126 
1127  /* FIXME: Probably wrong result if the effect
1128  * EFT_ACTION_SUCCESS_MOVE_COST depends on unit state. Add unit state
1129  * parameters? */
1130  mpco += get_target_bonus_effects(
1131  nullptr, act_player, nullptr, tile_city(post_action_tile), nullptr,
1132  tgt_tile, nullptr, putype, nullptr, nullptr, paction,
1133  EFT_ACTION_SUCCESS_MOVE_COST);
1134 
1135  return mpco;
1136 }
1137 
1141 int utype_build_shield_cost(const struct city *pcity,
1142  const struct unit_type *punittype)
1143 {
1144  int base;
1145  struct player *owner;
1146  struct tile *ptile;
1147 
1148  if (pcity != nullptr) {
1149  owner = city_owner(pcity);
1150  ptile = city_tile(pcity);
1151  } else {
1152  owner = nullptr;
1153  ptile = nullptr;
1154  }
1155 
1156  base = punittype->build_cost
1157  * (100
1158  + get_unittype_bonus(owner, ptile, punittype,
1159  EFT_UNIT_BUILD_COST_PCT))
1160  / 100;
1161 
1162  return MAX(base * game.info.shieldbox / 100, 1);
1163 }
1164 
1168 int utype_build_shield_cost_base(const struct unit_type *punittype)
1169 {
1170  return MAX(punittype->build_cost * game.info.shieldbox / 100, 1);
1171 }
1172 
1176 int unit_build_shield_cost(const struct city *pcity,
1177  const struct unit *punit)
1178 {
1179  return utype_build_shield_cost(pcity, unit_type_get(punit));
1180 }
1181 
1185 int unit_build_shield_cost_base(const struct unit *punit)
1186 {
1188 }
1189 
1193 int utype_buy_gold_cost(const struct city *pcity,
1194  const struct unit_type *punittype,
1195  int shields_in_stock)
1196 {
1197  int cost = 0;
1198  const int missing =
1199  utype_build_shield_cost(pcity, punittype) - shields_in_stock;
1200  struct player *owner;
1201  struct tile *ptile;
1202 
1203  if (missing > 0) {
1204  cost = 2 * missing + (missing * missing) / 20;
1205  }
1206 
1207  if (shields_in_stock == 0) {
1208  cost *= 2;
1209  }
1210 
1211  if (pcity != nullptr) {
1212  owner = city_owner(pcity);
1213  ptile = city_tile(pcity);
1214  } else {
1215  owner = nullptr;
1216  ptile = nullptr;
1217  }
1218 
1219  cost = cost
1220  * (100
1221  + get_unittype_bonus(owner, ptile, punittype,
1222  EFT_UNIT_BUY_COST_PCT))
1223  / 100;
1224 
1225  return cost;
1226 }
1227 
1231 int utype_pop_value(const struct unit_type *punittype)
1232 {
1233  return (punittype->pop_cost);
1234 }
1235 
1239 int unit_pop_value(const struct unit *punit)
1240 {
1241  return utype_pop_value(unit_type_get(punit));
1242 }
1243 
1247 enum unit_move_type utype_move_type(const struct unit_type *punittype)
1248 {
1249  return utype_class(punittype)->move_type;
1250 }
1251 
1256 const char *utype_name_translation(const struct unit_type *punittype)
1257 {
1258  return name_translation_get(&punittype->name);
1259 }
1260 
1265 const char *unit_name_translation(const struct unit *punit)
1266 {
1267  return utype_name_translation(unit_type_get(punit));
1268 }
1269 
1274 const char *utype_rule_name(const struct unit_type *punittype)
1275 {
1276  return rule_name_get(&punittype->name);
1277 }
1278 
1283 const char *unit_rule_name(const struct unit *punit)
1284 {
1285  return utype_rule_name(unit_type_get(punit));
1286 }
1287 
1292 const char *utype_values_string(const struct unit_type *punittype)
1293 {
1294  static char buffer[256];
1295 
1296  // Print in two parts as move_points_text() returns a static buffer
1297  fc_snprintf(buffer, sizeof(buffer), "%d/%d/%s", punittype->attack_strength,
1298  punittype->defense_strength,
1299  move_points_text(punittype->move_rate, true));
1300  if (utype_fuel(punittype)) {
1301  cat_snprintf(buffer, sizeof(buffer), "(%d)", utype_fuel(punittype));
1302  }
1303  return buffer;
1304 }
1305 
1310 const char *utype_values_translation(const struct unit_type *punittype)
1311 {
1312  static char buffer[256];
1313 
1314  fc_snprintf(buffer, sizeof(buffer), "%s [%s]",
1315  utype_name_translation(punittype),
1316  utype_values_string(punittype));
1317  return buffer;
1318 }
1319 
1324 const char *uclass_name_translation(const struct unit_class *pclass)
1325 {
1326  return name_translation_get(&pclass->name);
1327 }
1328 
1333 const char *uclass_rule_name(const struct unit_class *pclass)
1334 {
1335  return rule_name_get(&pclass->name);
1336 }
1337 
1343 bool role_units_translations(QString &astr, int flag, bool alts)
1344 {
1345  const int count = num_role_units(flag);
1346 
1347  if (4 < count) {
1348  if (alts) {
1349  astr = QString(_("%1 or similar units"))
1350  .arg(utype_name_translation(get_role_unit(flag, 0)));
1351  } else {
1352  astr = QString(_("%1 and similar units"))
1353  .arg(utype_name_translation(get_role_unit(flag, 0)));
1354  }
1355  return true;
1356  } else if (0 < count) {
1357  QVector<QString> vec;
1358  vec.reserve(count);
1359 
1360  for (int i = 0; i < count; i++) {
1361  vec.append(utype_name_translation(get_role_unit(flag, i)));
1362  }
1363 
1364  if (alts) {
1365  astr = strvec_to_or_list(vec);
1366  } else {
1367  astr = strvec_to_and_list(vec);
1368  }
1369  return true;
1370  }
1371  return false;
1372 }
1373 
1378 const struct unit_type *
1379 can_upgrade_unittype(const struct player *pplayer,
1380  const struct unit_type *punittype)
1381 {
1382  const struct unit_type *upgrade = punittype;
1383  const struct unit_type *best_upgrade = nullptr;
1384 
1385  /* For some reason this used to check
1386  * can_player_build_unit_direct() for the unittype
1387  * we're updating from.
1388  * That check is now removed as it prevented legal updates
1389  * of units player had acquired via bribing, capturing,
1390  * diplomatic treaties, or lua script. */
1391 
1392  while ((upgrade = upgrade->obsoleted_by) != U_NOT_OBSOLETED) {
1393  if (can_player_build_unit_direct(pplayer, upgrade)) {
1394  best_upgrade = upgrade;
1395  }
1396  }
1397 
1398  return best_upgrade;
1399 }
1400 
1407 int unit_upgrade_price(const struct player *pplayer,
1408  const struct unit_type *from,
1409  const struct unit_type *to)
1410 {
1411  /* Upgrade price is only payed for "Upgrade Unit" so it is safe to hard
1412  * code the action ID for now. paction will have to become a parameter
1413  * before generalized actions appears. */
1414  const struct action *paction = action_by_number(ACTION_UPGRADE_UNIT);
1415  int missing = (utype_build_shield_cost_base(to)
1416  - unit_shield_value(nullptr, from, paction));
1417  int base_cost = 2 * missing + (missing * missing) / 20;
1418 
1419  return base_cost * (100 + get_player_bonus(pplayer, EFT_UPGRADE_PRICE_PCT))
1420  / 100;
1421 }
1422 
1428 {
1429  unit_type_iterate(punittype)
1430  {
1431  if (0 == strcmp(utype_name_translation(punittype), name)) {
1432  return punittype;
1433  }
1434  }
1436 
1437  return nullptr;
1438 }
1439 
1445 {
1446  const char *qname = Qn_(name);
1447 
1448  unit_type_iterate(punittype)
1449  {
1450  if (0 == fc_strcasecmp(utype_rule_name(punittype), qname)) {
1451  return punittype;
1452  }
1453  }
1455 
1456  return nullptr;
1457 }
1458 
1463 struct unit_class *unit_class_by_rule_name(const char *s)
1464 {
1465  const char *qs = Qn_(s);
1466 
1467  unit_class_iterate(pclass)
1468  {
1469  if (0 == fc_strcasecmp(uclass_rule_name(pclass), qs)) {
1470  return pclass;
1471  }
1472  }
1474 
1475  return nullptr;
1476 }
1477 
1482 {
1483  int i;
1484 
1485  for (i = 0; i < MAX_NUM_USER_UCLASS_FLAGS; i++) {
1487  }
1488 }
1489 
1493 void set_user_unit_class_flag_name(enum unit_class_flag_id id,
1494  const char *name, const char *helptxt)
1495 {
1496  int ufid = id - UCF_USER_FLAG_1;
1497 
1498  fc_assert_ret(id >= UCF_USER_FLAG_1 && id <= UCF_LAST_USER_FLAG);
1499 
1500  if (user_class_flags[ufid].name != nullptr) {
1501  delete[] user_class_flags[ufid].name;
1502  user_class_flags[ufid].name = nullptr;
1503  }
1504 
1505  if (name && name[0] != '\0') {
1507  }
1508 
1509  if (user_class_flags[ufid].helptxt != nullptr) {
1510  delete[] user_class_flags[ufid].helptxt;
1511  user_class_flags[ufid].helptxt = nullptr;
1512  }
1513 
1514  if (helptxt && helptxt[0] != '\0') {
1515  user_class_flags[ufid].helptxt = fc_strdup(helptxt);
1516  }
1517 }
1518 
1522 const char *unit_class_flag_id_name_cb(enum unit_class_flag_id flag)
1523 {
1524  if (flag < UCF_USER_FLAG_1 || flag > UCF_LAST_USER_FLAG) {
1525  return nullptr;
1526  }
1527 
1528  return user_class_flags[flag - UCF_USER_FLAG_1].name;
1529 }
1530 
1534 const char *unit_class_flag_helptxt(enum unit_class_flag_id id)
1535 {
1536  fc_assert(id >= UCF_USER_FLAG_1 && id <= UCF_LAST_USER_FLAG);
1537 
1538  return user_class_flags[id - UCF_USER_FLAG_1].helptxt;
1539 }
1540 
1545 {
1546  int i;
1547 
1548  for (i = 0; i < MAX_NUM_USER_UNIT_FLAGS; i++) {
1550  }
1551 }
1552 
1556 void set_user_unit_type_flag_name(enum unit_type_flag_id id,
1557  const char *name, const char *helptxt)
1558 {
1559  int ufid = id - UTYF_USER_FLAG_1;
1560 
1561  fc_assert_ret(id >= UTYF_USER_FLAG_1 && id <= UTYF_LAST_USER_FLAG);
1562  delete[] user_type_flags[ufid].name;
1563  user_type_flags[ufid].name = nullptr;
1564  if (name && name[0] != '\0') {
1566  }
1567  delete[] user_type_flags[ufid].helptxt;
1568  user_type_flags[ufid].helptxt = nullptr;
1569  if (helptxt && helptxt[0] != '\0') {
1570  user_type_flags[ufid].helptxt = fc_strdup(helptxt);
1571  }
1572 }
1573 
1577 const char *unit_type_flag_id_name_cb(enum unit_type_flag_id flag)
1578 {
1579  if (flag < UTYF_USER_FLAG_1 || flag > UTYF_LAST_USER_FLAG) {
1580  return nullptr;
1581  }
1582 
1583  return user_type_flags[flag - UTYF_USER_FLAG_1].name;
1584 }
1585 
1589 const char *unit_type_flag_helptxt(enum unit_type_flag_id id)
1590 {
1591  fc_assert(id >= UTYF_USER_FLAG_1 && id <= UTYF_LAST_USER_FLAG);
1592 
1593  return user_type_flags[id - UTYF_USER_FLAG_1].helptxt;
1594 }
1595 
1600  const struct unit_type *putype)
1601 {
1602  if (!utype_has_flag(putype, UTYF_UNIQUE)) {
1603  // This isn't a unique unit type.
1604  return false;
1605  }
1606 
1607  unit_list_iterate(pplayer->units, existing_unit)
1608  {
1609  if (putype == unit_type_get(existing_unit)) {
1610  /* FIXME: This could be slow if we have lots of units. We could
1611  * consider keeping an array of unittypes updated with this info
1612  * instead. */
1613 
1614  return true;
1615  }
1616  }
1618 
1619  // The player doesn't already have one.
1620  return false;
1621 }
1622 
1629  const struct unit_type *punittype)
1630 {
1631  fc_assert_ret_val(nullptr != punittype, false);
1632 
1633  if (is_barbarian(p) && !utype_has_role(punittype, L_BARBARIAN_BUILD)
1634  && !utype_has_role(punittype, L_BARBARIAN_BUILD_TECH)) {
1635  // Barbarians can build only role units
1636  return false;
1637  }
1638 
1639  if ((utype_can_do_action_result(punittype, ACTRES_NUKE_CITY)
1640  || utype_can_do_action_result(punittype, ACTRES_NUKE_UNITS)
1641  || utype_can_do_action_result(punittype, ACTRES_NUKE))
1642  && get_player_bonus(p, EFT_ENABLE_NUKE) <= 0) {
1643  return false;
1644  }
1645  if (utype_has_flag(punittype, UTYF_NOBUILD)) {
1646  return false;
1647  }
1648 
1649  if (utype_has_flag(punittype, UTYF_BARBARIAN_ONLY) && !is_barbarian(p)) {
1650  /* Unit can be built by barbarians only and this player is
1651  * not barbarian */
1652  return false;
1653  }
1654 
1655  if (utype_has_flag(punittype, UTYF_NEWCITY_GAMES_ONLY)
1656  && game.scenario.prevent_new_cities) {
1657  /* Unit is usable only in games where founding of new cities is allowed.
1658  */
1659  return false;
1660  }
1661 
1662  requirement_vector_iterate(&punittype->build_reqs, preq)
1663  {
1664  if (preq->source.kind == VUT_IMPROVEMENT && preq->range == REQ_RANGE_CITY
1665  && preq->present) {
1666  /* If the unit has a building requirement, we check to see if the
1667  * player can build that building. Note that individual cities may
1668  * not have that building, so they still may not be able to build the
1669  * unit. */
1670  if (is_great_wonder(preq->source.value.building)
1671  && (great_wonder_is_built(preq->source.value.building)
1672  || great_wonder_is_destroyed(preq->source.value.building))) {
1673  // It's already built great wonder
1674  if (great_wonder_owner(preq->source.value.building) != p) {
1675  /* Not owned by this player. Either destroyed or owned by somebody
1676  * else. */
1677  return false;
1678  }
1679  } else {
1681  p, preq->source.value.building)) {
1682  return false;
1683  }
1684  }
1685  }
1686  if (preq->range < REQ_RANGE_PLAYER) {
1687  // The question *here* is if the *player* can build this unit
1688  continue;
1689  }
1690  if (!is_req_active(p, nullptr, nullptr, nullptr, nullptr, nullptr,
1691  punittype, nullptr, nullptr, nullptr, preq,
1692  RPT_CERTAIN)) {
1693  return false;
1694  }
1695  }
1697 
1699  advance_number(punittype->require_advance))
1700  != TECH_KNOWN) {
1701  if (!is_barbarian(p)) {
1702  /* Normal players can never build units without knowing tech
1703  * requirements. */
1704  return false;
1705  }
1706  if (!utype_has_role(punittype, L_BARBARIAN_BUILD)) {
1707  // Even barbarian cannot build this unit without tech
1708 
1709  /* Unit has to have L_BARBARIAN_BUILD_TECH role
1710  * In the beginning of this function we checked that
1711  * barbarian player tries to build only role
1712  * L_BARBARIAN_BUILD or L_BARBARIAN_BUILD_TECH units. */
1713  fc_assert_ret_val(utype_has_role(punittype, L_BARBARIAN_BUILD_TECH),
1714  false);
1715 
1716  /* Client does not know all the advances other players have
1717  * got. So following gives wrong answer in the client.
1718  * This is called at the client when received create_city
1719  * packet for a barbarian city. City initialization tries
1720  * to find L_FIRSTBUILD unit. */
1721 
1722  if (!game.info
1723  .global_advances[advance_index(punittype->require_advance)]) {
1724  // Nobody knows required tech
1725  return false;
1726  }
1727  }
1728  }
1729 
1730  if (utype_player_already_has_this_unique(p, punittype)) {
1731  // A player can only have one unit of each unique unit type.
1732  return false;
1733  }
1734 
1735  return true;
1736 }
1737 
1742 bool can_player_build_unit_now(const struct player *p,
1743  const struct unit_type *punittype)
1744 {
1745  if (!can_player_build_unit_direct(p, punittype)) {
1746  return false;
1747  }
1748  while ((punittype = punittype->obsoleted_by) != U_NOT_OBSOLETED) {
1749  if (can_player_build_unit_direct(p, punittype)) {
1750  return false;
1751  }
1752  }
1753  return true;
1754 }
1755 
1762  const struct unit_type *punittype)
1763 {
1764  fc_assert_ret_val(nullptr != punittype, false);
1765  if (utype_has_flag(punittype, UTYF_NOBUILD)) {
1766  return false;
1767  }
1768  while ((punittype = punittype->obsoleted_by) != U_NOT_OBSOLETED) {
1769  if (can_player_build_unit_direct(p, punittype)) {
1770  return false;
1771  }
1772  }
1773  return true;
1774 }
1775 
1783 static bool first_init = true;
1786 
1791 static void precalc_one(int i,
1792  bool (*func_has)(const struct unit_type *, int))
1793 {
1794  int j;
1795 
1796  // Count:
1797  fc_assert(n_with_role[i] == 0);
1799  {
1800  if (func_has(u, i)) {
1801  n_with_role[i]++;
1802  }
1803  }
1805 
1806  if (n_with_role[i] > 0) {
1807  with_role[i] = new unit_type *[n_with_role[i]];
1808  j = 0;
1810  {
1811  if (func_has(u, i)) {
1812  with_role[i][j++] = u;
1813  }
1814  }
1816  fc_assert(j == n_with_role[i]);
1817  }
1818 }
1819 
1824 {
1825  int i;
1826 
1827  for (i = 0; i < MAX_UNIT_ROLES; i++) {
1828  delete[] with_role[i];
1829  with_role[i] = nullptr;
1830  n_with_role[i] = 0;
1831  }
1832 }
1833 
1839 {
1840  int i;
1841 
1842  if (first_init) {
1843  for (i = 0; i < MAX_UNIT_ROLES; i++) {
1844  with_role[i] = nullptr;
1845  n_with_role[i] = 0;
1846  }
1847  } else {
1849  }
1850 
1851  for (i = 0; i <= UTYF_LAST_USER_FLAG; i++) {
1853  }
1854  for (i = L_FIRST; i < L_LAST; i++) {
1856  }
1857  for (i = L_LAST; i < MAX_UNIT_ROLES; i++) {
1859  }
1860  first_init = false;
1861 }
1862 
1866 int num_role_units(int role)
1867 {
1868  fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
1869  || (role >= L_FIRST && role < L_LAST)
1870  || (role >= L_LAST && role < MAX_UNIT_ROLES),
1871  0);
1873  return n_with_role[role];
1874 }
1875 
1882 struct unit_type *
1884 {
1885  int i;
1886 
1887  for (i = n_with_role[role] - 1; i >= 0; i--) {
1888  if (cb(with_role[role][i], data)) {
1889  return with_role[role][i];
1890  }
1891  }
1892 
1893  return nullptr;
1894 }
1895 
1900 struct unit_type *get_role_unit(int role, int role_index)
1901 {
1902  fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
1903  || (role >= L_FIRST && role < L_LAST)
1904  || (role >= L_LAST && role < MAX_UNIT_ROLES),
1905  nullptr);
1906  fc_assert_ret_val(!first_init, nullptr);
1907  if (role_index == -1) {
1908  role_index = n_with_role[role] - 1;
1909  }
1910  fc_assert_ret_val(role_index >= 0 && role_index < n_with_role[role],
1911  nullptr);
1912 
1913  return with_role[role][role_index];
1914 }
1915 
1920 struct unit_type *best_role_unit(const struct city *pcity, int role)
1921 {
1922  struct unit_type *u;
1923  int j;
1924 
1925  fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
1926  || (role >= L_FIRST && role < L_LAST)
1927  || (role >= L_LAST && role < MAX_UNIT_ROLES),
1928  nullptr);
1929  fc_assert_ret_val(!first_init, nullptr);
1930 
1931  for (j = n_with_role[role] - 1; j >= 0; j--) {
1932  u = with_role[role][j];
1933  if ((1 != utype_fuel(u)
1934  || utype_is_consumed_by_action_result(ACTRES_ATTACK, u))
1935  && can_city_build_unit_now(pcity, u)) {
1936  // Allow fuel == 1 units when pathfinding can handle them.
1937  return u;
1938  }
1939  }
1940 
1941  return nullptr;
1942 }
1943 
1950 struct unit_type *best_role_unit_for_player(const struct player *pplayer,
1951  int role)
1952 {
1953  int j;
1954 
1955  fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
1956  || (role >= L_FIRST && role < L_LAST)
1957  || (role >= L_LAST && role < MAX_UNIT_ROLES),
1958  nullptr);
1959  fc_assert_ret_val(!first_init, nullptr);
1960 
1961  for (j = n_with_role[role] - 1; j >= 0; j--) {
1962  struct unit_type *utype = with_role[role][j];
1963 
1964  if (can_player_build_unit_now(pplayer, utype)) {
1965  return utype;
1966  }
1967  }
1968 
1969  return nullptr;
1970 }
1971 
1976 struct unit_type *first_role_unit_for_player(const struct player *pplayer,
1977  int role)
1978 {
1979  int j;
1980 
1981  fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
1982  || (role >= L_FIRST && role < L_LAST)
1983  || (role >= L_LAST && role < MAX_UNIT_ROLES),
1984  nullptr);
1985  fc_assert_ret_val(!first_init, nullptr);
1986 
1987  for (j = 0; j < n_with_role[role]; j++) {
1988  struct unit_type *utype = with_role[role][j];
1989 
1990  if (can_player_build_unit_now(pplayer, utype)) {
1991  return utype;
1992  }
1993  }
1994 
1995  return nullptr;
1996 }
1997 
2002 {
2003  int i;
2004 
2005  /* Can't use unit_type_iterate or utype_by_number here because
2006  * num_unit_types isn't known yet. */
2007  for (i = 0; i < ARRAY_SIZE(unit_types); i++) {
2008  unit_types[i].item_number = i;
2009  requirement_vector_init(&(unit_types[i].build_reqs));
2010  unit_types[i].helptext = nullptr;
2011  unit_types[i].veteran = nullptr;
2012  unit_types[i].bonuses = combat_bonus_list_new();
2013  unit_types[i].ruledit_disabled = false;
2014  }
2015 }
2016 
2020 static void unit_type_free(struct unit_type *punittype)
2021 {
2022  if (nullptr != punittype->helptext) {
2023  delete punittype->helptext;
2024  punittype->helptext = nullptr;
2025  requirement_vector_free(&(punittype->build_reqs));
2026  }
2027 
2028  veteran_system_destroy(punittype->veteran);
2029  combat_bonus_list_iterate(punittype->bonuses, pbonus)
2030  {
2031  delete pbonus;
2032  pbonus = nullptr;
2033  }
2035  combat_bonus_list_destroy(punittype->bonuses);
2036 }
2037 
2042 {
2043  int i;
2044 
2045  /* Can't use unit_type_iterate or utype_by_number here because
2046  * we want to free what unit_types_init() has allocated. */
2047  for (i = 0; i < ARRAY_SIZE(unit_types); i++) {
2049  }
2050 }
2051 
2056 {
2057  int i;
2058 
2059  for (i = 0; i < MAX_NUM_USER_UNIT_FLAGS; i++) {
2061  }
2062 }
2063 
2068 {
2069  int i;
2070 
2071  for (i = 0; i < MAX_NUM_USER_UCLASS_FLAGS; i++) {
2073  }
2074 }
2075 
2080 {
2081  if (game.control.num_unit_classes > 0) {
2082  return unit_classes;
2083  }
2084  return nullptr;
2085 }
2086 
2091 {
2092  if (game.control.num_unit_classes > 0) {
2093  return &unit_classes[game.control.num_unit_classes - 1];
2094  }
2095  return nullptr;
2096 }
2097 
2101 Unit_Class_id uclass_count() { return game.control.num_unit_classes; }
2102 
2103 #ifndef uclass_index
2110 Unit_Class_id uclass_index(const struct unit_class *pclass)
2111 {
2112  fc_assert_ret_val(pclass, 0);
2113  return pclass - unit_classes;
2114 }
2115 #endif // uclass_index
2116 
2121 {
2122  fc_assert_ret_val(pclass, 0);
2123  return pclass->item_number;
2124 }
2125 
2130 {
2131  if (id < 0 || id >= game.control.num_unit_classes) {
2132  return nullptr;
2133  }
2134  return &unit_classes[id];
2135 }
2136 
2137 #ifndef utype_class
2141 struct unit_class *utype_class(const struct unit_type *punittype)
2142 {
2143  fc_assert(nullptr != punittype->uclass);
2144  return punittype->uclass;
2145 }
2146 #endif // utype_class
2147 
2151 struct unit_class *unit_class_get(const struct unit *punit)
2152 {
2153  return utype_class(unit_type_get(punit));
2154 }
2155 
2160 {
2161  int i;
2162 
2163  /* Can't use unit_class_iterate or uclass_by_number here because
2164  * num_unit_classes isn't known yet. */
2165  for (i = 0; i < ARRAY_SIZE(unit_classes); i++) {
2166  unit_classes[i].item_number = i;
2167  unit_classes[i].cache.refuel_bases = nullptr;
2168  unit_classes[i].cache.native_tile_extras = nullptr;
2169  unit_classes[i].cache.bonus_roads = nullptr;
2170  unit_classes[i].cache.subset_movers = nullptr;
2171  unit_classes[i].helptext = nullptr;
2172  unit_classes[i].ruledit_disabled = false;
2173  }
2174 }
2175 
2180 {
2181  int i;
2182 
2183  for (i = 0; i < ARRAY_SIZE(unit_classes); i++) {
2184  if (unit_classes[i].cache.refuel_bases != nullptr) {
2185  extra_type_list_destroy(unit_classes[i].cache.refuel_bases);
2186  unit_classes[i].cache.refuel_bases = nullptr;
2187  }
2188  if (unit_classes[i].cache.native_tile_extras != nullptr) {
2189  extra_type_list_destroy(unit_classes[i].cache.native_tile_extras);
2190  unit_classes[i].cache.native_tile_extras = nullptr;
2191  }
2192  if (unit_classes[i].cache.bonus_roads != nullptr) {
2193  extra_type_list_destroy(unit_classes[i].cache.bonus_roads);
2194  unit_classes[i].cache.bonus_roads = nullptr;
2195  }
2196  if (unit_classes[i].cache.subset_movers != nullptr) {
2197  unit_class_list_destroy(unit_classes[i].cache.subset_movers);
2198  }
2199  delete unit_classes[i].helptext;
2200  unit_classes[i].helptext = nullptr;
2201  }
2202 }
2203 
2207 const struct veteran_system *
2208 utype_veteran_system(const struct unit_type *punittype)
2209 {
2210  fc_assert_ret_val(punittype != nullptr, nullptr);
2211 
2212  if (punittype->veteran) {
2213  return punittype->veteran;
2214  }
2215 
2216  fc_assert_ret_val(game.veteran != nullptr, nullptr);
2217  return game.veteran;
2218 }
2219 
2223 const struct veteran_level *
2224 utype_veteran_level(const struct unit_type *punittype, int level)
2225 {
2226  const struct veteran_system *vsystem = utype_veteran_system(punittype);
2227 
2228  fc_assert_ret_val(vsystem != nullptr, nullptr);
2229  fc_assert_ret_val(vsystem->definitions != nullptr, nullptr);
2230  fc_assert_ret_val(vsystem->levels > level, nullptr);
2231 
2232  return (vsystem->definitions + level);
2233 }
2234 
2239 const char *utype_veteran_name_translation(const struct unit_type *punittype,
2240  int level)
2241 {
2242  if (utype_veteran_levels(punittype) <= 1) {
2243  return nullptr;
2244  } else {
2245  const struct veteran_level *vlvl = utype_veteran_level(punittype, level);
2246 
2247  return name_translation_get(&vlvl->name);
2248  }
2249 }
2250 
2254 int utype_veteran_levels(const struct unit_type *punittype)
2255 {
2256  const struct veteran_system *vsystem = utype_veteran_system(punittype);
2257 
2258  fc_assert_ret_val(vsystem != nullptr, 1);
2259 
2260  return vsystem->levels;
2261 }
2262 
2267 bool utype_veteran_has_power_bonus(const struct unit_type *punittype)
2268 {
2269  int i, initial_power_fact = utype_veteran_level(punittype, 0)->power_fact;
2270  for (i = 1; i < utype_veteran_levels(punittype); i++) {
2271  if (utype_veteran_level(punittype, i)->power_fact > initial_power_fact) {
2272  return true;
2273  }
2274  }
2275  return false;
2276 }
2277 
2282 {
2283  struct veteran_system *vsystem;
2284 
2285  // There must be at least one level.
2286  fc_assert_ret_val(count > 0, nullptr);
2287 
2288  vsystem = new veteran_system{};
2289  vsystem->levels = count;
2290  vsystem->definitions = new veteran_level[vsystem->levels]();
2291 
2292  return vsystem;
2293 }
2294 
2299 {
2300  if (vsystem) {
2301  delete[] vsystem->definitions;
2302  delete vsystem;
2303  vsystem = nullptr;
2304  }
2305 }
2306 
2311  const char *vlist_name, int vlist_power,
2312  int vlist_move, int vlist_raise,
2313  int vlist_wraise)
2314 {
2315  struct veteran_level *vlevel;
2316 
2317  fc_assert_ret(vsystem != nullptr);
2318  fc_assert_ret(vsystem->levels > level);
2319 
2320  vlevel = vsystem->definitions + level;
2321 
2322  names_set(&vlevel->name, nullptr, vlist_name, nullptr);
2323  vlevel->power_fact = vlist_power;
2324  vlevel->move_bonus = vlist_move;
2325  vlevel->base_raise_chance = vlist_raise;
2326  vlevel->work_raise_chance = vlist_wraise;
2327 }
2328 
2332 void *utype_ai_data(const struct unit_type *ptype, const struct ai_type *ai)
2333 {
2334  return ptype->ais[ai_type_number(ai)];
2335 }
2336 
2340 void utype_set_ai_data(struct unit_type *ptype, const struct ai_type *ai,
2341  void *data)
2342 {
2343  ptype->ais[ai_type_number(ai)] = data;
2344 }
2345 
2349 void set_unit_class_caches(struct unit_class *pclass)
2350 {
2351  pclass->cache.refuel_bases = extra_type_list_new();
2352  pclass->cache.native_tile_extras = extra_type_list_new();
2353  pclass->cache.bonus_roads = extra_type_list_new();
2354  pclass->cache.subset_movers = unit_class_list_new();
2355 
2356  extra_type_iterate(pextra)
2357  {
2358  if (is_native_extra_to_uclass(pextra, pclass)) {
2359  struct road_type *proad = extra_road_get(pextra);
2360 
2361  if (extra_has_flag(pextra, EF_REFUEL)) {
2362  extra_type_list_append(pclass->cache.refuel_bases, pextra);
2363  }
2364  if (extra_has_flag(pextra, EF_NATIVE_TILE)) {
2365  extra_type_list_append(pclass->cache.native_tile_extras, pextra);
2366  }
2367  if (proad != nullptr && road_provides_move_bonus(proad)) {
2368  extra_type_list_append(pclass->cache.bonus_roads, pextra);
2369  }
2370  }
2371  }
2373 
2374  unit_class_iterate(pcharge)
2375  {
2376  bool subset_mover = true;
2377 
2378  terrain_type_iterate(pterrain)
2379  {
2380  if (BV_ISSET(pterrain->native_to, uclass_index(pcharge))
2381  && !BV_ISSET(pterrain->native_to, uclass_index(pclass))) {
2382  subset_mover = false;
2383  }
2384  }
2386 
2387  if (subset_mover) {
2388  extra_type_iterate(pextra)
2389  {
2390  if (is_native_extra_to_uclass(pextra, pcharge)
2391  && !is_native_extra_to_uclass(pextra, pclass)) {
2392  subset_mover = false;
2393  }
2394  }
2396  }
2397 
2398  if (subset_mover) {
2399  unit_class_list_append(pclass->cache.subset_movers, pcharge);
2400  }
2401  }
2403 }
2404 
2408 void set_unit_type_caches(struct unit_type *ptype)
2409 {
2411 
2412  unit_type_iterate(utype)
2413  {
2414  int idx = utype_index(utype);
2415 
2416  ptype->cache.defense_mp_bonuses_pct[idx] =
2417  combat_bonus_against(ptype->bonuses, utype,
2418  CBONUS_DEFENSE_MULTIPLIER_PCT)
2419  + 100
2420  * combat_bonus_against(ptype->bonuses, utype,
2421  CBONUS_DEFENSE_MULTIPLIER);
2422 
2423  if (ptype->cache.defense_mp_bonuses_pct[idx]
2424  > ptype->cache.max_defense_mp_pct) {
2425  ptype->cache.max_defense_mp_pct =
2426  ptype->cache.defense_mp_bonuses_pct[idx];
2427  }
2428  }
2430 }
2431 
2435 static enum unit_move_type move_type_from_extra(struct extra_type *pextra,
2436  struct unit_class *puc)
2437 {
2438  bool land_allowed = true;
2439  bool sea_allowed = true;
2440 
2441  if (!extra_has_flag(pextra, EF_NATIVE_TILE)) {
2442  return unit_move_type_invalid();
2443  }
2444  if (!is_native_extra_to_uclass(pextra, puc)) {
2445  return unit_move_type_invalid();
2446  }
2447 
2448  if (is_extra_caused_by(pextra, EC_ROAD)
2449  && road_has_flag(extra_road_get(pextra), RF_RIVER)) {
2450  // Natural rivers are created to land only
2451  sea_allowed = false;
2452  }
2453 
2454  requirement_vector_iterate(&pextra->reqs, preq)
2455  {
2456  if (preq->source.kind == VUT_TERRAINCLASS) {
2457  if (!preq->present) {
2458  if (preq->source.value.terrainclass == TC_LAND) {
2459  land_allowed = false;
2460  } else if (preq->source.value.terrainclass == TC_OCEAN) {
2461  sea_allowed = false;
2462  }
2463  } else {
2464  if (preq->source.value.terrainclass == TC_LAND) {
2465  sea_allowed = false;
2466  } else if (preq->source.value.terrainclass == TC_OCEAN) {
2467  land_allowed = false;
2468  }
2469  }
2470  } else if (preq->source.kind == VUT_TERRAIN) {
2471  if (!preq->present) {
2472  if (preq->source.value.terrain->tclass == TC_LAND) {
2473  land_allowed = false;
2474  } else if (preq->source.value.terrain->tclass == TC_OCEAN) {
2475  sea_allowed = false;
2476  }
2477  } else {
2478  if (preq->source.value.terrain->tclass == TC_LAND) {
2479  sea_allowed = false;
2480  } else if (preq->source.value.terrain->tclass == TC_OCEAN) {
2481  land_allowed = false;
2482  }
2483  }
2484  }
2485  }
2487 
2488  if (land_allowed && sea_allowed) {
2489  return UMT_BOTH;
2490  }
2491  if (land_allowed && !sea_allowed) {
2492  return UMT_LAND;
2493  }
2494  if (!land_allowed && sea_allowed) {
2495  return UMT_SEA;
2496  }
2497 
2498  return unit_move_type_invalid();
2499 }
2500 
2504 void set_unit_move_type(struct unit_class *puclass)
2505 {
2506  bool land_moving = false;
2507  bool sea_moving = false;
2508 
2509  extra_type_iterate(pextra)
2510  {
2511  enum unit_move_type eut = move_type_from_extra(pextra, puclass);
2512 
2513  if (eut == UMT_BOTH) {
2514  land_moving = true;
2515  sea_moving = true;
2516  } else if (eut == UMT_LAND) {
2517  land_moving = true;
2518  } else if (eut == UMT_SEA) {
2519  sea_moving = true;
2520  }
2521  }
2523 
2524  terrain_type_iterate(pterrain)
2525  {
2526  if (is_native_to_class(puclass, pterrain, nullptr)) {
2527  if (is_ocean(pterrain)) {
2528  sea_moving = true;
2529  } else {
2530  land_moving = true;
2531  }
2532  }
2533  }
2535 
2536  if (land_moving && sea_moving) {
2537  puclass->move_type = UMT_BOTH;
2538  } else if (sea_moving) {
2539  puclass->move_type = UMT_SEA;
2540  } else {
2541  // If unit has no native terrains, it is considered land moving
2542  puclass->move_type = UMT_LAND;
2543  }
2544 }
2545 
2549 bool utype_is_cityfounder(const struct unit_type *utype)
2550 {
2551  if (game.scenario.prevent_new_cities) {
2552  // No unit is allowed to found new cities
2553  return false;
2554  }
2555 
2556  return utype_can_do_action_result(utype, ACTRES_FOUND_CITY);
2557 }
enum action_actor_kind action_get_actor_kind(const struct action *paction)
Get the actor kind of an action.
Definition: actions.cpp:1188
struct action * action_by_number(action_id act_id)
Return the action with the given id.
Definition: actions.cpp:1149
bool action_actor_utype_hard_reqs_ok(enum action_result result, const struct unit_type *actor_unittype)
Returns TRUE if the specified unit type can perform an action with the wanted result given that an ac...
Definition: actions.cpp:2776
bool action_id_exists(const action_id act_id)
Returns TRUE iff the specified action ID refers to a valid action.
Definition: actions.cpp:1138
bool action_has_result(const struct action *paction, enum action_result result)
Returns TRUE iff performing the specified action has the specified result.
Definition: actions.cpp:1248
const char * action_id_rule_name(action_id act_id)
Get the rule name of the action.
Definition: actions.cpp:1362
struct action_enabler_list * action_enablers_for_action(action_id action)
Get all enablers for an action in the current ruleset.
Definition: actions.cpp:1884
#define action_enablers_iterate_end
Definition: actions.h:425
#define action_enabler_list_iterate_end
Definition: actions.h:376
#define action_by_result_iterate_end
Definition: actions.h:396
#define action_by_result_iterate(_paction_, _act_id_, _result_)
Definition: actions.h:387
#define action_iterate_end
Definition: actions.h:383
#define action_id_get_actor_kind(act_id)
Definition: actions.h:519
#define action_enablers_iterate(_enabler_)
Definition: actions.h:417
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition: actions.h:374
#define action_iterate(_act_)
Definition: actions.h:378
#define ACTION_ANY
Definition: actions.h:217
#define action_id_get_target_kind(act_id)
Definition: actions.h:522
int ai_type_number(const struct ai_type *ai)
Returns id of the given ai_type.
Definition: ai.cpp:47
QString strvec_to_and_list(const QVector< QString > &psv)
Definition: astring.cpp:43
QString strvec_to_or_list(const QVector< QString > &psv)
Definition: astring.cpp:20
#define BV_CLR_ALL(bv)
Definition: bitvector.h:62
#define BV_SET(bv, bit)
Definition: bitvector.h:44
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
#define BV_CLR(bv, bit)
Definition: bitvector.h:49
struct output_type * get_output_type(Output_type_id output)
Return the output type for this index.
Definition: city.cpp:611
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Return whether given city can build given unit; returns FALSE if unit is obsolete.
Definition: city.cpp:894
int combat_bonus_against(const struct combat_bonus_list *list, const struct unit_type *enemy, enum combat_bonus_type type)
Get bonus value against given unit type from bonus list.
Definition: combat.cpp:852
static void base(QVariant data1, QVariant data2)
Action "Build Base" for choice dialog.
Definition: dialogs.cpp:2393
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_player_output_bonus(const struct player *pplayer, const struct output_type *poutput, enum effect_type effect_type)
Returns the player effect bonus of an output.
Definition: effects.cpp:760
struct @19::@20 reqs
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_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Returns the effect bonus for a player.
Definition: effects.cpp:673
bool extra_has_flag(const struct extra_type *pextra, enum extra_flag_id flag)
Check if extra has given flag.
Definition: extras.cpp:779
bool is_native_extra_to_uclass(const struct extra_type *pextra, const struct unit_class *pclass)
Is extra native to unit class?
Definition: extras.cpp:761
#define extra_type_iterate(_p)
Definition: extras.h:279
#define extra_type_iterate_end
Definition: extras.h:285
#define is_extra_caused_by(e, c)
Definition: extras.h:182
#define extra_type_list_iterate_end
Definition: extras.h:147
#define extra_road_get(_e_)
Definition: extras.h:171
@ RPT_CERTAIN
Definition: fc_types.h:568
int Unit_Class_id
Definition: fc_types.h:333
int action_id
Definition: fc_types.h:306
int Unit_type_id
Definition: fc_types.h:299
@ O_SHIELD
Definition: fc_types.h:86
@ O_GOLD
Definition: fc_types.h:88
#define UCL_LAST
Definition: fc_types.h:332
enum output_type_id Output_type_id
Definition: fc_types.h:295
#define _(String)
Definition: fcintl.h:50
#define Qn_(String)
Definition: fcintl.h:66
void user_flag_init(struct user_flag *flag)
Initialize user flag.
Definition: game.cpp:788
void user_flag_free(struct user_flag *flag)
Free user flag.
Definition: game.cpp:797
struct civ_game game
Definition: game.cpp:47
struct world wld
Definition: game.cpp:48
bool can_player_build_improvement_direct(const struct player *p, const struct impr_type *pimprove)
Whether player can build given building somewhere, ignoring whether it is obsolete.
bool great_wonder_is_built(const struct impr_type *pimprove)
Returns whether this wonder is currently built.
bool great_wonder_is_destroyed(const struct impr_type *pimprove)
Returns whether this wonder has been destroyed.
bool is_great_wonder(const struct impr_type *pimprove)
Is this building a great wonder?
struct player * great_wonder_owner(const struct impr_type *pimprove)
Get the player owning this small wonder.
const char * name
Definition: inputfile.cpp:118
#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
#define log_debug(message,...)
Definition: log.h:65
static int map_move_cost(const struct civ_map *nmap, const struct player *pplayer, const struct unit_type *punittype, const struct tile *src_tile, const struct tile *dst_tile)
Definition: map.h:232
bool is_native_to_class(const struct unit_class *punitclass, const struct terrain *pterrain, const bv_extras *extras)
This terrain is native to unit class.
Definition: movement.cpp:290
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_MOVE_FRAGS
Definition: movement.h:20
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
static void names_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name, const char *rule_name)
static bool is_barbarian(const struct player *pplayer)
Definition: player.h:474
bool is_req_in_vec(const struct requirement *req, const struct requirement_vector *vec)
Returns TRUE iff the requirement vector vec contains the requirement req.
bool is_req_active(const struct player *target_player, const struct player *other_player, const struct city *target_city, const struct impr_type *target_building, const struct tile *target_tile, const struct unit *target_unit, const struct unit_type *target_unittype, const struct output_type *target_output, const struct specialist *target_specialist, const struct action *target_action, const struct requirement *req, const enum req_problem_type prob_type, const enum vision_layer vision_layer, const enum national_intelligence nintel)
Checks the requirement to see if it is active on the given target.
bool does_req_contradicts_reqs(const struct requirement *req, const struct requirement_vector *vec)
Returns TRUE if the given requirement contradicts the given requirement vector.
#define requirement_fulfilled_by_unit_type(_ut_, _rqs_)
Definition: requirements.h:306
#define requirement_diplrel_ereq(_id_, _range_, _present_)
Definition: requirements.h:320
#define requirement_vector_iterate_end
Definition: requirements.h:80
#define requirement_vector_iterate(req_vec, preq)
Definition: requirements.h:78
struct research * research_get(const struct player *pplayer)
Returns the research structure associated with the player.
Definition: research.cpp:110
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Returns state of the tech for current research.
Definition: research.cpp:609
bool road_has_flag(const struct road_type *proad, enum road_flag_id flag)
Check if road provides effect.
Definition: road.cpp:367
bool road_provides_move_bonus(const struct road_type *proad)
Does road type provide move bonus.
Definition: road.cpp:437
struct setting_list * level[OLEVELS_NUM]
Definition: settings.cpp:167
#define ARRAY_SIZE(x)
Definition: shared.h:79
#define FC_INFINITY
Definition: shared.h:32
#define MAX(x, y)
Definition: shared.h:48
struct action::@10::@11 is_unit
bool actor_consuming_always
Definition: actions.h:337
enum action_result result
Definition: actions.h:308
union action::@10 actor
Definition: ai.h:42
Definition: city.h:291
struct packet_ruleset_control control
Definition: game.h:74
struct packet_game_info info
Definition: game.h:80
struct packet_scenario_info scenario
Definition: game.h:78
struct veteran_system * veteran
Definition: game.h:91
struct requirement_vector reqs
Definition: extras.h:90
Definition: player.h:231
struct unit_list * units
Definition: player.h:264
int max
Definition: unittype.cpp:674
int min
Definition: unittype.cpp:673
enum req_range range
Definition: requirements.h:69
struct universal source
Definition: requirements.h:68
Definition: road.h:54
Definition: tile.h:42
struct player * owner
Definition: tile.h:52
struct unit_class::@81 cache
Unit_Class_id item_number
Definition: unittype.h:121
enum unit_move_type move_type
Definition: unittype.h:124
struct unit_class_list * subset_movers
Definition: unittype.h:143
struct extra_type_list * native_tile_extras
Definition: unittype.h:141
struct extra_type_list * bonus_roads
Definition: unittype.h:142
QVector< QString > * helptext
Definition: unittype.h:132
struct extra_type_list * refuel_bases
Definition: unittype.h:140
struct name_translation name
Definition: unittype.h:122
bool ruledit_disabled
Definition: unittype.h:123
struct unit_class * uclass
Definition: unittype.h:519
int pop_cost
Definition: unittype.h:477
void * ais[FREECIV_AI_MOD_LAST]
Definition: unittype.h:554
struct requirement_vector build_reqs
Definition: unittype.h:485
Unit_type_id item_number
Definition: unittype.h:466
int defense_strength
Definition: unittype.h:480
QVector< QString > * helptext
Definition: unittype.h:534
int build_cost
Definition: unittype.h:476
struct veteran_system * veteran
Definition: unittype.h:509
const struct unit_type * obsoleted_by
Definition: unittype.h:494
int move_rate
Definition: unittype.h:481
struct advance * require_advance
Definition: unittype.h:484
struct unit_type::@83 cache
struct name_translation name
Definition: unittype.h:467
bv_unit_type_roles roles
Definition: unittype.h:500
int upkeep[O_LAST]
Definition: unittype.h:503
bv_unit_classes disembarks
Definition: unittype.h:530
int max_defense_mp_pct
Definition: unittype.h:542
bv_unit_type_flags flags
Definition: unittype.h:499
bool ruledit_disabled
Definition: unittype.h:468
bv_unit_classes embarks
Definition: unittype.h:527
int attack_strength
Definition: unittype.h:479
int happy_cost
Definition: unittype.h:502
int defense_mp_bonuses_pct[U_LAST]
Definition: unittype.h:545
struct combat_bonus_list * bonuses
Definition: unittype.h:491
Definition: unit.h:134
const struct unit_type * utype
Definition: unit.h:135
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
Definition: game.h:64
char * name
Definition: game.h:65
char * helptxt
Definition: game.h:66
int power_fact
Definition: unittype.h:453
int base_raise_chance
Definition: unittype.h:455
struct name_translation name
Definition: unittype.h:452
int move_bonus
Definition: unittype.h:454
int work_raise_chance
Definition: unittype.h:456
struct veteran_level * definitions
Definition: unittype.h:462
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
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
Definition: support.cpp:89
int cat_snprintf(char *str, size_t n, const char *format,...)
cat_snprintf is like a combination of fc_snprintf and fc_strlcat; it does snprintf to the end of an e...
Definition: support.cpp:564
#define fc_strdup(str)
Definition: support.h:111
Tech_type_id advance_index(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:76
Tech_type_id advance_number(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:85
#define terrain_type_iterate(_p)
Definition: terrain.h:331
#define is_ocean(pterrain)
Definition: terrain.h:276
#define terrain_type_iterate_end
Definition: terrain.h:337
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
enum citytile_type citytile
Definition: fc_types.h:594
enum ustate_prop unit_state
Definition: fc_types.h:615
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
#define unit_owner(_pu)
Definition: unit.h:370
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
bool utype_may_act_tgt_city_tile(const struct unit_type *punit_type, const action_id act_id, const enum citytile_type prop, const bool is_there)
Return TRUE iff the given (action enabler controlled) action may be performed by a unit of the given ...
Definition: unittype.cpp:870
int utype_buy_gold_cost(const struct city *pcity, const struct unit_type *punittype, int shields_in_stock)
Returns the amount of gold it takes to rush this unit.
Definition: unittype.cpp:1193
bool can_unit_act_when_ustate_is(const struct unit_type *punit_type, const enum ustate_prop prop, const bool is_there)
Return TRUE iff there exists an (action enabler controlled) action that a unit of the type punit_type...
Definition: unittype.cpp:733
const struct unit_class * unit_class_array_last()
Return the last item of unit_classes.
Definition: unittype.cpp:2090
const char * unit_rule_name(const struct unit *punit)
Return the (untranslated) rule name of the unit.
Definition: unittype.cpp:1283
void set_unit_class_caches(struct unit_class *pclass)
Set caches for unit class.
Definition: unittype.cpp:2349
static struct unit_type unit_types[U_LAST]
Definition: unittype.cpp:38
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
struct unit_type * best_role_unit(const struct city *pcity, int role)
Return "best" unit this city can build, with given role/flag.
Definition: unittype.cpp:1920
int utype_pop_value(const struct unit_type *punittype)
How much city shrinks when it builds unit of this type.
Definition: unittype.cpp:1231
const char * uclass_name_translation(const struct unit_class *pclass)
Return the (translated) name of the unit class.
Definition: unittype.cpp:1324
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
const char * unit_type_flag_id_name_cb(enum unit_type_flag_id flag)
Unit type flag name callback, called from specenum code.
Definition: unittype.cpp:1577
struct unit_type * utype_by_number(const Unit_type_id id)
Return a pointer for the unit type struct for the given unit type id.
Definition: unittype.cpp:103
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
#define ACTION_HOSTILE
Definition: unittype.cpp:323
bool utype_pays_for_regular_move_to_tgt(const struct action *paction, const struct unit_type *utype)
Returns TRUE iff successfully performing the specified action always will cost the actor unit of the ...
Definition: unittype.cpp:1052
static void unit_type_free(struct unit_type *punittype)
Frees the memory associated with this unit type.
Definition: unittype.cpp:2020
const char * utype_values_translation(const struct unit_type *punittype)
Return string with translated unit name and list of its values.
Definition: unittype.cpp:1310
static void precalc_one(int i, bool(*func_has)(const struct unit_type *, int))
Do the real work for role_unit_precalcs, for one role (or flag), given by i.
Definition: unittype.cpp:1791
static struct user_flag user_class_flags[MAX_NUM_USER_UCLASS_FLAGS]
Definition: unittype.cpp:47
static bv_diplrel_all_reqs dipl_rel_action_cache[U_LAST][ACTION_AND_FAKES]
Definition: unittype.cpp:461
int unit_build_shield_cost(const struct city *pcity, const struct unit *punit)
Returns the number of shields it takes to build this unit.
Definition: unittype.cpp:1176
const char * unit_class_flag_helptxt(enum unit_class_flag_id id)
Return the (untranslated) help text of the user unit class flag.
Definition: unittype.cpp:1534
BV_DEFINE(bv_ustate_act_cache, USP_COUNT *2)
struct unit_type * unit_type_array_first()
Return the first item of unit_types.
Definition: unittype.cpp:52
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Returns TRUE iff successfully performing the specified action always will move the actor unit of the ...
Definition: unittype.cpp:969
const struct unit_type * unit_type_array_last()
Return the last item of unit_types.
Definition: unittype.cpp:63
static bv_citytile_cache ctile_tgt_act_cache[U_LAST][ACTION_AND_FAKES]
Definition: unittype.cpp:462
int utype_upkeep_cost(const struct unit_type *ut, struct player *pplayer, Output_type_id otype)
Returns the upkeep of a unit of this type under the given government.
Definition: unittype.cpp:123
enum unit_move_type utype_move_type(const struct unit_type *punittype)
Return move type of the unit type.
Definition: unittype.cpp:1247
const char * utype_rule_name(const struct unit_type *punittype)
Return the (untranslated) rule name of the unit type.
Definition: unittype.cpp:1274
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Return "best" unit the player can build, with given role/flag.
Definition: unittype.cpp:1950
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
void unit_classes_init()
Initialize unit_class structures.
Definition: unittype.cpp:2159
#define MAX_UNIT_ROLES
Definition: unittype.cpp:36
void unit_class_flags_free()
Frees the memory associated with all unit class flags.
Definition: unittype.cpp:2067
bool utype_has_role(const struct unit_type *punittype, int role)
Return whether the given unit type has the role.
Definition: unittype.cpp:186
static enum unit_move_type move_type_from_extra(struct extra_type *pextra, struct unit_class *puc)
What move types nativity of this extra will give?
Definition: unittype.cpp:2435
void set_unit_move_type(struct unit_class *puclass)
Set move_type for unit class.
Definition: unittype.cpp:2504
int utype_build_shield_cost(const struct city *pcity, const struct unit_type *punittype)
Returns the number of shields it takes to build this unit type.
Definition: unittype.cpp:1141
static bool action_is_hostile(action_id act_id)
Returns TRUE iff the specified action is hostile.
Definition: unittype.cpp:249
Unit_type_id utype_count()
Return the number of unit types.
Definition: unittype.cpp:74
int utype_build_shield_cost_base(const struct unit_type *punittype)
Returns the number of shields this unit type represents.
Definition: unittype.cpp:1168
void set_unit_type_caches(struct unit_type *ptype)
Set caches for unit types.
Definition: unittype.cpp:2408
const char * unit_type_flag_helptxt(enum unit_type_flag_id id)
Return the (untranslated) helptxt of the user unit flag.
Definition: unittype.cpp:1589
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
bool utype_is_consumed_by_action_result(enum action_result result, const struct unit_type *utype)
Returns TRUE iff performing an action with the specified action result will consume an actor unit of ...
Definition: unittype.cpp:947
static struct range * moves_left_range(struct requirement_vector *reqs)
Get the legal range of move fragments left of the specified requirement vector.
Definition: unittype.cpp:683
void user_unit_class_flags_init()
Initialize user unit class flags.
Definition: unittype.cpp:1481
int num_role_units(int role)
How many unit types have specified role/flag.
Definition: unittype.cpp:1866
static bool first_init
The following functions use static variables so we can quickly look up which unit types have given fl...
Definition: unittype.cpp:1783
bool utype_player_already_has_this_unique(const struct player *pplayer, const struct unit_type *putype)
Returns TRUE iff the unit type is unique and the player already has one.
Definition: unittype.cpp:1599
static bool utype_can_do_action_role(const struct unit_type *putype, const int role)
Return TRUE iff the unit type can perform the action corresponding to the unit type role.
Definition: unittype.cpp:422
bool utype_may_act_at_all(const struct unit_type *putype)
Return TRUE iff units of this type can do actions controlled by generalized (ruleset defined) action ...
Definition: unittype.cpp:374
int unit_build_shield_cost_base(const struct unit *punit)
Returns the number of shields this unit represents.
Definition: unittype.cpp:1185
struct unit_class * unit_class_by_rule_name(const char *s)
Returns the unit class that has the given (untranslated) rule name.
Definition: unittype.cpp:1463
static void local_dipl_rel_action_cache_set(struct unit_type *putype)
Cache what actions may be possible for a unit of the type putype for each local DiplRel variation.
Definition: unittype.cpp:536
static void tgt_citytile_act_cache_set(struct unit_type *putype)
Cache if any action may be possible for a unit of the type putype for each target local city tile pro...
Definition: unittype.cpp:611
struct veteran_system * veteran_system_new(int count)
Allocate new veteran system structure with given veteran level count.
Definition: unittype.cpp:2281
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
void unit_classes_free()
Free resources allocated for unit classes.
Definition: unittype.cpp:2179
void veteran_system_definition(struct veteran_system *vsystem, int level, const char *vlist_name, int vlist_power, int vlist_move, int vlist_raise, int vlist_wraise)
Fill veteran level in given veteran system with given information.
Definition: unittype.cpp:2310
#define MOVES_LEFT_INFINITY
Definition: unittype.cpp:677
Unit_Class_id uclass_count()
Return the unit_class count.
Definition: unittype.cpp:2101
struct unit_class * unit_class_get(const struct unit *punit)
Returns unit class pointer for a unit.
Definition: unittype.cpp:2151
void unit_type_flags_free()
Frees the memory associated with all unit type flags.
Definition: unittype.cpp:2055
const char * utype_values_string(const struct unit_type *punittype)
Return string describing unit type values.
Definition: unittype.cpp:1292
#define requirement_citytile_ereq(_id_, _present_)
Definition: unittype.cpp:456
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype)
Whether player can build given unit somewhere, ignoring whether unit is obsolete and assuming the pla...
Definition: unittype.cpp:1628
static void unit_state_action_cache_set(struct unit_type *putype)
Cache if any action may be possible for a unit of the type putype for each unit state property.
Definition: unittype.cpp:469
static bv_ustate_act_cache ustate_act_cache[U_LAST][ACTION_AND_FAKES]
Definition: unittype.cpp:460
void role_unit_precalcs_free()
Free memory allocated by role_unit_precalcs().
Definition: unittype.cpp:1823
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_act_when_ustate(const struct unit_type *punit_type, const action_id act_id, const enum ustate_prop prop, const bool is_there)
Return TRUE iff the unit type can do the specified (action enabler controlled) action while its unit ...
Definition: unittype.cpp:746
void veteran_system_destroy(struct veteran_system *vsystem)
Free veteran system.
Definition: unittype.cpp:2298
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Return TRUE iff units of the given type can do any enabler controlled action with the specified actio...
Definition: unittype.cpp:402
bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
Return whether the unit has the given role.
Definition: unittype.cpp:195
static void unit_can_act_cache_set(struct unit_type *putype)
Cache what generalized (ruleset defined) action enabler controlled actions a unit of the given type c...
Definition: unittype.cpp:338
bool role_units_translations(QString &astr, int flag, bool alts)
Return a string with all the names of units with this flag.
Definition: unittype.cpp:1343
const struct veteran_system * utype_veteran_system(const struct unit_type *punittype)
Return veteran system used for this unit type.
Definition: unittype.cpp:2208
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
Definition: unittype.cpp:176
bool unit_can_take_over(const struct unit *punit)
Return whether the unit can take over enemy cities.
Definition: unittype.cpp:203
static bv_unit_types unit_can_act_cache[ACTION_AND_FAKES]
Definition: unittype.cpp:332
struct unit_type * unit_type_by_translated_name(const char *name)
Returns the unit type that has the given (translated) name.
Definition: unittype.cpp:1427
void role_unit_precalcs()
Initialize; it is safe to call this multiple times (e.g., if units have changed due to rulesets in cl...
Definition: unittype.cpp:1838
void unit_type_action_cache_set(struct unit_type *ptype)
Cache if any action may be possible for a unit of the type putype given the property tested for.
Definition: unittype.cpp:710
const char * utype_name_translation(const struct unit_type *punittype)
Return the (translated) name of the unit type.
Definition: unittype.cpp:1256
void unit_types_init()
Inialize unit-type structures.
Definition: unittype.cpp:2001
void user_unit_type_flags_init()
Initialize user unit type flags.
Definition: unittype.cpp:1544
struct unit_class * uclass_by_number(const Unit_Class_id id)
Returns unit class pointer for an ID value.
Definition: unittype.cpp:2129
int utype_pays_mp_for_action_estimate(const struct action *paction, const struct unit_type *putype, const struct player *act_player, const struct tile *act_tile, const struct tile *tgt_tile)
Returns an estimate of the amount of movement points successfully performing the specified action wil...
Definition: unittype.cpp:1104
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Returns TRUE iff performing the specified action will consume an actor unit of the specified type.
Definition: unittype.cpp:936
struct unit_type * unit_type_by_rule_name(const char *name)
Returns the unit type that has the given (untranslated) rule name.
Definition: unittype.cpp:1444
Unit_type_id utype_number(const struct unit_type *punittype)
Return the unit type index.
Definition: unittype.cpp:91
bool utype_veteran_has_power_bonus(const struct unit_type *punittype)
Return whether this unit type's veteran system, if any, confers a power factor bonus at any level (it...
Definition: unittype.cpp:2267
const char * unit_class_flag_id_name_cb(enum unit_class_flag_id flag)
Unit class flag name callback, called from specenum code.
Definition: unittype.cpp:1522
struct unit_type * get_role_unit(int role, int role_index)
Return index-th unit with specified role/flag.
Definition: unittype.cpp:1900
Unit_type_id utype_index(const struct unit_type *punittype)
Return the unit type index.
Definition: unittype.cpp:82
#define ACTION_AND_FAKES
Definition: unittype.cpp:326
static struct user_flag user_type_flags[MAX_NUM_USER_UNIT_FLAGS]
Definition: unittype.cpp:45
bool utype_can_take_over(const struct unit_type *punittype)
Return whether the unit type can take over enemy cities.
Definition: unittype.cpp:215
void utype_set_ai_data(struct unit_type *ptype, const struct ai_type *ai, void *data)
Attach ai data to unit type.
Definition: unittype.cpp:2340
struct unit_class * unit_class_array_first()
Return the first item of unit_classes.
Definition: unittype.cpp:2079
void set_user_unit_class_flag_name(enum unit_class_flag_id id, const char *name, const char *helptxt)
Sets user defined name for unit class flag.
Definition: unittype.cpp:1493
const char * uclass_rule_name(const struct unit_class *pclass)
Return the (untranslated) rule name of the unit class.
Definition: unittype.cpp:1333
const char * unit_name_translation(const struct unit *punit)
Return the (translated) name of the unit.
Definition: unittype.cpp:1265
bool can_player_build_unit_later(const struct player *p, const struct unit_type *punittype)
Whether player can eventually build given unit somewhere – ie, returns TRUE if unit is available with...
Definition: unittype.cpp:1761
static struct unit_class unit_classes[UCL_LAST]
Definition: unittype.cpp:39
int unit_pop_value(const struct unit *punit)
How much population is put to building this unit.
Definition: unittype.cpp:1239
#define requirement_unit_state_ereq(_id_, _present_)
Definition: unittype.cpp:454
const char * utype_veteran_name_translation(const struct unit_type *punittype, int level)
Return translated name of the given veteran level.
Definition: unittype.cpp:2239
Unit_Class_id uclass_number(const struct unit_class *pclass)
Return the unit_class index.
Definition: unittype.cpp:2120
bool can_player_build_unit_now(const struct player *p, const struct unit_type *punittype)
Whether player can build given unit somewhere; returns FALSE if unit is obsolete.
Definition: unittype.cpp:1742
void unit_types_free()
Frees the memory associated with all unit types.
Definition: unittype.cpp:2041
static struct unit_type ** with_role[MAX_UNIT_ROLES]
Definition: unittype.cpp:1785
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Returns TRUE iff successfully performing the specified action never will move the actor unit from its...
Definition: unittype.cpp:1009
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_act_if_tgt_citytile(const struct unit_type *punit_type, const action_id act_id, const enum citytile_type prop, const bool is_there)
Returns TRUE iff the unit type can do the specified (action enabler controlled) action while its targ...
Definition: unittype.cpp:763
struct unit_type * first_role_unit_for_player(const struct player *pplayer, int role)
Return first unit the player can build, with given role/flag.
Definition: unittype.cpp:1976
void unit_type_action_cache_init()
Cache what unit types may be allowed do what actions, both at all and when certain properties are tru...
Definition: unittype.cpp:722
static int n_with_role[MAX_UNIT_ROLES]
Definition: unittype.cpp:1784
bool can_utype_do_act_if_tgt_diplrel(const struct unit_type *punit_type, const action_id act_id, const int prop, const bool is_there)
Return TRUE iff the given (action enabler controlled) action can be performed by a unit of the given ...
Definition: unittype.cpp:784
struct unit_type * role_units_iterate_backwards(int role, role_unit_callback cb, void *data)
Iterate over all the role units and feed them to callback, starting from the last one.
Definition: unittype.cpp:1883
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Return TRUE iff units of the given type can do the specified generalized (ruleset defined) action ena...
Definition: unittype.cpp:386
void set_user_unit_type_flag_name(enum unit_type_flag_id id, const char *name, const char *helptxt)
Sets user defined name for unit flag.
Definition: unittype.cpp:1556
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 utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Return the "happy cost" (the number of citizens who are discontented) for this unit.
Definition: unittype.cpp:167
void * utype_ai_data(const struct unit_type *ptype, const struct ai_type *ai)
Return pointer to ai data of given unit type and ai type.
Definition: unittype.cpp:2332
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
#define UCF_LAST_USER_FLAG
Definition: unittype.h:97
#define utype_class(_t_)
Definition: unittype.h:691
#define utype_fuel(ptype)
Definition: unittype.h:772
#define combat_bonus_list_iterate_end
Definition: unittype.h:447
#define L_FIRST
Definition: unittype.h:319
#define combat_bonus_list_iterate(bonuslist, pbonus)
Definition: unittype.h:445
#define unit_class_iterate(_p)
Definition: unittype.h:823
#define MAX_NUM_USER_UNIT_FLAGS
Definition: unittype.h:303
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition: unittype.h:584
#define UTYF_LAST_USER_FLAG
Definition: unittype.h:302
#define L_LAST
Definition: unittype.h:406
bool(* role_unit_callback)(struct unit_type *ptype, void *data)
Definition: unittype.h:664
#define unit_type_iterate(_p)
Definition: unittype.h:785
#define U_LAST
Definition: unittype.h:31
#define uclass_index(_c_)
Definition: unittype.h:684
#define unit_class_iterate_end
Definition: unittype.h:829
#define unit_type_iterate_end
Definition: unittype.h:791
#define MAX_NUM_USER_UCLASS_FLAGS
Definition: unittype.h:98
#define U_NOT_OBSOLETED
Definition: unittype.h:493