Freeciv21
Develop your civilization from humble roots to a global empire
combat.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>
15 
16 // utility
17 #include "bitvector.h"
18 #include "log.h"
19 #include "rand.h"
20 
21 // common
22 #include "base.h"
23 #include "game.h"
24 #include "map.h"
25 #include "movement.h"
26 #include "packets.h"
27 #include "unit.h"
28 #include "unitlist.h"
29 #include "unittype.h"
30 
31 #include "combat.h"
32 
40 static bool can_player_attack_tile(const struct player *pplayer,
41  const struct tile *ptile)
42 {
43  struct city *pcity = tile_city(ptile);
44 
45  // 1. Is there anyone there at all?
46  if (!pcity && unit_list_size((ptile->units)) == 0) {
47  return false;
48  }
49 
50  // 2. If there is a city there, can we attack it?
51  if (pcity && !pplayers_at_war(city_owner(pcity), pplayer)) {
52  return false;
53  }
54 
55  // 3. Are we allowed to attack _all_ units there?
56  unit_list_iterate(ptile->units, aunit)
57  {
58  if (!pplayers_at_war(unit_owner(aunit), pplayer)) {
59  /* Enemy hiding behind a human/diplomatic shield */
60  return false;
61  }
62  }
64 
65  return true;
66 }
67 
71 static bool is_unit_reachable_by_unit(const struct unit *defender,
72  const struct unit *attacker)
73 {
74  struct unit_class *dclass = unit_class_get(defender);
75  const struct unit_type *atype = unit_type_get(attacker);
76 
77  return BV_ISSET(atype->targets, uclass_index(dclass));
78 }
79 
83 bool is_unit_reachable_at(const struct unit *defender,
84  const struct unit *attacker,
85  const struct tile *location)
86 {
87  if (nullptr != tile_city(location)) {
88  return true;
89  }
90 
91  if (is_unit_reachable_by_unit(defender, attacker)) {
92  return true;
93  }
94 
95  if (tile_has_native_base(location, unit_type_get(defender))) {
96  return true;
97  }
98 
99  return false;
100 }
101 
121 unit_attack_unit_at_tile_result(const struct unit *punit,
122  const struct unit *pdefender,
123  const struct tile *dest_tile)
124 {
125  // 1. Can we attack _anything_ ?
126  if (!(utype_can_do_action(unit_type_get(punit), ACTION_ATTACK)
127  || utype_can_do_action(unit_type_get(punit), ACTION_SUICIDE_ATTACK)
128  /* Needed because ACTION_NUKE_UNITS uses this when evaluating its
129  * hard requirements. */
130  || utype_can_do_action(unit_type_get(punit), ACTION_NUKE_UNITS))) {
131  return ATT_NON_ATTACK;
132  }
133 
134  // 2. Can't attack with ground unit from ocean, except for marines
135  if (!is_native_tile(unit_type_get(punit), unit_tile(punit))
136  && !utype_can_do_act_when_ustate(unit_type_get(punit), ACTION_ATTACK,
137  USP_NATIVE_TILE, false)
139  ACTION_SUICIDE_ATTACK,
140  USP_NATIVE_TILE, false)) {
141  return ATT_NONNATIVE_SRC;
142  }
143 
144  /* 3. Most units can not attack non-native terrain.
145  * Most ships can attack land tiles (shore bombardment) */
146  if (!is_native_tile(unit_type_get(punit), dest_tile)
147  && !can_attack_non_native(unit_type_get(punit))) {
148  return ATT_NONNATIVE_DST;
149  }
150 
151  // 4. Only fighters can attack planes, except in city or airbase attacks
152  if (!is_unit_reachable_at(pdefender, punit, dest_tile)) {
153  return ATT_UNREACHABLE;
154  }
155 
156  /* Unreachability check must remain the last check, as callers interpret
157  * ATT_UNREACHABLE as "ATT_OK except that unreachable." */
158 
159  return ATT_OK;
160 }
161 
167 static enum unit_attack_result
168 unit_attack_all_at_tile_result(const struct unit *punit,
169  const struct tile *ptile)
170 {
171  bool any_reachable_unit = false;
172  bool any_neverprotect_unit = false;
173 
174  unit_list_iterate(ptile->units, aunit)
175  {
176  /* HACK: we don't count transported units here. This prevents some
177  * bugs like a submarine carrying a cruise missile being invulnerable
178  * to other sea units. However from a gameplay perspective it's a hack,
179  * since players can load and unload their units manually to protect
180  * their transporters. */
181  if (!unit_transported(aunit)) {
182  enum unit_attack_result result;
183 
184  result = unit_attack_unit_at_tile_result(punit, aunit, ptile);
185  if (result == ATT_UNREACHABLE
186  && unit_has_type_flag(aunit, UTYF_NEVER_PROTECTS)) {
187  // Doesn't prevent us from attacking other units on the tile
188  any_neverprotect_unit = true;
189  continue;
190  } else if (result != ATT_OK) {
191  return result;
192  }
193  any_reachable_unit = true;
194  }
195  }
197 
198  /* If there are only unreachable, UTYF_NEVER_PROTECTS units, we still have
199  * to return ATT_UNREACHABLE. */
200  return (any_reachable_unit || !any_neverprotect_unit) ? ATT_OK
201  : ATT_UNREACHABLE;
202 }
203 
209 static enum unit_attack_result
210 unit_attack_any_at_tile_result(const struct unit *punit,
211  const struct tile *ptile)
212 {
213  enum unit_attack_result result = ATT_OK;
214 
215  unit_list_iterate(ptile->units, aunit)
216  {
217  /* HACK: we don't count transported units here. This prevents some
218  * bugs like a cargoplane carrying a land unit being vulnerable. */
219  if (!unit_transported(aunit)) {
220  result = unit_attack_unit_at_tile_result(punit, aunit, ptile);
221  if (result == ATT_OK) {
222  return result;
223  }
224  }
225  }
227 
228  /* That's result from check against last unit on tile, not first.
229  * Shouldn't matter. */
230  return result;
231 }
232 
237 unit_attack_units_at_tile_result(const struct unit *punit,
238  const struct tile *ptile)
239 {
240  if (game.info.unreachable_protects) {
241  return unit_attack_all_at_tile_result(punit, ptile);
242  } else {
243  return unit_attack_any_at_tile_result(punit, ptile);
244  }
245 }
246 
251 bool can_unit_attack_tile(const struct unit *punit,
252  const struct tile *dest_tile)
253 {
254  return (can_player_attack_tile(unit_owner(punit), dest_tile)
255  && unit_attack_units_at_tile_result(punit, dest_tile) == ATT_OK);
256 }
257 
270 double win_chance(int as, int ahp, int afp, int ds, int dhp, int dfp)
271 {
272  if (afp == 0)
273  return 0.0;
274  if (dfp == 0)
275  return 1.0;
276  // number of rounds a unit can fight without dying
277  int att_N_lose = (ahp + dfp - 1) / dfp;
278  int def_N_lose = (dhp + afp - 1) / afp;
279  // Probability of losing one round
280  double att_P_lose1 =
281  (as + ds == 0) ? 0.5 : static_cast<double>(ds) / (as + ds);
282  double def_P_lose1 = 1 - att_P_lose1;
283 
284  /*
285  This calculates
286 
287  binomial_coeff(def_N_lose-1 + lr, lr)
288  * def_P_lose1^(def_N_lose-1)
289  * att_P_lose1^(lr)
290  * def_P_lose1
291 
292  for each possible number of rounds lost (rl) by the winning unit.
293  rl is of course less than the number of rounds the winning unit
294  should lose to lose all it's hit points.
295  The probabilities are then summed.
296 
297  To see this is correct consider the set of series for all valid fights.
298  These series are the type (win, lose, lose...). The possible lenghts are
299  def_N_lose to def_N_lose+att_N_lose-1. A series is not valid unless it
300  contains def_N_lose wins, and one of the wins must be the last one, or
301  the series would be equivalent the a shorter series (the attacker would
302  have won one or more fights previously).
303  So since the last fight is a win we disregard it while calculating. Now
304  a series contains def_N_lose-1 wins. So for each possible lenght of a
305  series we find the probability of every valid series and then sum.
306  For a specific lenght (a "lr") every series have the probability
307  def_P_lose1^(def_N_lose-1) * att_P_lose1^(lr)
308  and then getting from that to the real series requires a win, ie factor
309  def_N_lose. The number of series with lenght (def_N_lose-1 + lr) and
310  "lr" lost fights is
311  binomial_coeff(def_N_lose-1 + lr, lr)
312  And by multiplying we get the formula on the top of this code block.
313  Adding the cumulative probability for each valid lenght then gives the
314  total probability.
315 
316  We clearly have all valid series this way. To see that we have counted
317  none twice note that would require a series with a smaller series
318  inbedded. But since the smaller series already included def_N_lose wins,
319  and the larger series ends with a win, it would have too many wins and
320  therefore cannot exist.
321 
322  In practice each binomial coefficient for a series lenght can be
323  calculated from the previous. In the coefficient (n, k) n is increased
324  and k is unchanged. The "* def_P_lose1" is multiplied on the sum
325  afterwards.
326 
327  (lots of talk for so little code)
328  */
329 
330  double binom_save = pow(def_P_lose1, static_cast<double>(def_N_lose - 1));
331  double accum_prob = binom_save; // lr = 0
332 
333  int lr; // the number of Lost Rounds by the attacker
334  for (lr = 1; lr < att_N_lose; lr++) {
335  // update the coefficient
336  int n = lr + def_N_lose - 1;
337  binom_save *= n;
338  binom_save /= lr;
339  binom_save *= att_P_lose1;
340  // use it for this lr
341  accum_prob += binom_save;
342  }
343  // Every element of the sum needs a factor for the very last fight round
344  accum_prob *= def_P_lose1;
345 
346  return accum_prob;
347 }
348 
352 void get_modified_firepower(const struct unit *attacker,
353  const struct unit *defender, int *att_fp,
354  int *def_fp)
355 {
356  struct city *pcity = tile_city(unit_tile(defender));
357 
358  *att_fp = unit_type_get(attacker)->firepower;
359  *def_fp = unit_type_get(defender)->firepower;
360 
361  // Check CityBuster flag
362  if (unit_has_type_flag(attacker, UTYF_CITYBUSTER) && pcity) {
363  *att_fp *= 2;
364  }
365 
366  /*
367  * UTYF_BADWALLATTACKER sets the firepower of the attacking unit to 1 if
368  * an EFT_DEFEND_BONUS applies (such as a land unit attacking a city with
369  * city walls).
370  */
371  if (unit_has_type_flag(attacker, UTYF_BADWALLATTACKER)
372  && get_unittype_bonus(unit_owner(defender), unit_tile(defender),
373  unit_type_get(attacker), EFT_DEFEND_BONUS)
374  > 0) {
375  *att_fp = 1;
376  }
377 
378  /* pearl harbour - defender's firepower is reduced to one,
379  * attacker's is multiplied by two */
380  if (unit_has_type_flag(defender, UTYF_BADCITYDEFENDER)
381  && tile_city(unit_tile(defender))) {
382  *att_fp *= 2;
383  *def_fp = 1;
384  }
385 
386  /*
387  * When attacked by fighters, helicopters have their firepower
388  * reduced to 1.
389  */
390  if (combat_bonus_against(unit_type_get(attacker)->bonuses,
391  unit_type_get(defender), CBONUS_FIREPOWER1)) {
392  *def_fp = 1;
393  }
394 
395  // In land bombardment both units have their firepower reduced to 1
396  if (!is_native_tile(unit_type_get(attacker), unit_tile(defender))
397  && !can_exist_at_tile(&(wld.map), unit_type_get(defender),
398  unit_tile(attacker))) {
399  *att_fp = 1;
400  *def_fp = 1;
401  }
402 }
403 
408 double unit_win_chance(const struct unit *attacker,
409  const struct unit *defender)
410 {
411  int def_power = get_total_defense_power(attacker, defender);
412  int att_power = get_total_attack_power(attacker, defender);
413 
414  double chance;
415 
416  int def_fp, att_fp;
417  get_modified_firepower(attacker, defender, &att_fp, &def_fp);
418 
419  chance = win_chance(att_power, attacker->hp, att_fp, def_power,
420  defender->hp, def_fp);
421 
422  return chance;
423 }
424 
430 struct city *sdi_try_defend(const struct player *owner,
431  const struct tile *ptile)
432 {
433  square_iterate(&(wld.map), ptile, 2, ptile1)
434  {
435  struct city *pcity = tile_city(ptile1);
436 
437  if (pcity
439  nullptr, city_owner(pcity), owner, pcity, nullptr, ptile,
440  nullptr, nullptr, nullptr, nullptr, nullptr,
441  EFT_NUKE_PROOF)) {
442  return pcity;
443  }
444  }
446 
447  return nullptr;
448 }
449 
453 int get_attack_power(const struct unit *punit)
454 {
455  return base_get_attack_power(unit_type_get(punit), punit->veteran,
456  punit->moves_left);
457 }
458 
463 int base_get_attack_power(const struct unit_type *punittype, int veteran,
464  int moves_left)
465 {
466  int power;
467  const struct veteran_level *vlevel;
468 
469  fc_assert_ret_val(punittype != nullptr, 0);
470 
471  vlevel = utype_veteran_level(punittype, veteran);
472  fc_assert_ret_val(vlevel != nullptr, 0);
473 
474  power =
475  punittype->attack_strength * POWER_FACTOR * vlevel->power_fact / 100;
476 
477  if (is_tired_attack(moves_left)) {
478  power = (power * moves_left) / SINGLE_MOVE;
479  }
480 
481  return power;
482 }
483 
487 int base_get_defense_power(const struct unit *punit)
488 {
489  const struct veteran_level *vlevel;
490 
491  fc_assert_ret_val(punit != nullptr, 0);
492 
493  vlevel = utype_veteran_level(unit_type_get(punit), punit->veteran);
494  fc_assert_ret_val(vlevel != nullptr, 0);
495 
497  * vlevel->power_fact / 100;
498 }
499 
505 static int get_defense_power(const struct unit *punit)
506 {
507  int db, power = base_get_defense_power(punit);
508  struct tile *ptile = unit_tile(punit);
509  struct unit_class *pclass = unit_class_get(punit);
510 
511  if (uclass_has_flag(pclass, UCF_TERRAIN_DEFENSE)) {
512  db = 100 + tile_terrain(ptile)->defense_bonus;
513  power = (power * db) / 100;
514  }
515 
516  if (!is_native_tile_to_class(pclass, ptile)) {
517  power = power * pclass->non_native_def_pct / 100;
518  }
519 
520  return power;
521 }
522 
526 int get_total_attack_power(const struct unit *attacker,
527  const struct unit *defender)
528 {
529  int mod;
530  int attackpower = get_attack_power(attacker);
531 
532  mod = 100
533  + get_unittype_bonus(unit_owner(attacker), unit_tile(defender),
534  unit_type_get(attacker), EFT_ATTACK_BONUS);
535 
536  return attackpower * mod / 100;
537 }
538 
549 static int defense_multiplication(const struct unit_type *att_type,
550  const struct unit *def,
551  const struct player *def_player,
552  const struct tile *ptile, int defensepower)
553 {
554  int mod;
555  const struct unit_type *def_type = unit_type_get(def);
556 
557  fc_assert_ret_val(nullptr != def_type, 0);
558 
559  if (nullptr != att_type) {
560  int defense_divider_pct;
561  int defense_multiplier_pct =
562  100 + def_type->cache.defense_mp_bonuses_pct[utype_index(att_type)];
563 
564  defensepower = defensepower * defense_multiplier_pct / 100;
565 
566  // This applies even if pcity is nullptr.
567  mod =
568  100
569  + get_unittype_bonus(def_player, ptile, att_type, EFT_DEFEND_BONUS);
570  defensepower = MAX(0, defensepower * mod / 100);
571 
572  defense_divider_pct =
573  100
574  + combat_bonus_against(att_type->bonuses, def_type,
575  CBONUS_DEFENSE_DIVIDER_PCT)
576  + 100
577  * combat_bonus_against(att_type->bonuses, def_type,
578  CBONUS_DEFENSE_DIVIDER);
579 
580  defensepower = defensepower * 100 / defense_divider_pct;
581  }
582 
583  defensepower +=
584  defensepower * tile_extras_defense_bonus(ptile, def_type) / 100;
585 
586  defensepower = defensepower
587  * (100
589  nullptr, unit_owner(def), nullptr, tile_city(ptile),
590  nullptr, ptile, def, unit_type_get(def), nullptr,
591  nullptr, nullptr, EFT_FORTIFY_DEFENSE_BONUS))
592  / 100;
593 
594  return defensepower;
595 }
596 
601 int get_virtual_defense_power(const struct unit_type *att_type,
602  const struct unit_type *def_type,
603  struct player *def_player, struct tile *ptile,
604  bool fortified, int veteran)
605 {
606  int defensepower = def_type->defense_strength;
607  int db;
608  const struct veteran_level *vlevel;
609  struct unit_class *defclass;
610  struct unit *vdef;
611  int def;
612 
613  fc_assert_ret_val(def_type != nullptr, 0);
614 
615  if (!can_exist_at_tile(&(wld.map), def_type, ptile)) {
616  // Ground units on ship doesn't defend.
617  return 0;
618  }
619 
620  vlevel = utype_veteran_level(def_type, veteran);
621  fc_assert_ret_val(vlevel != nullptr, 0);
622 
623  defclass = utype_class(def_type);
624 
625  vdef = unit_virtual_create(def_player, nullptr, def_type, veteran);
626  unit_tile_set(vdef, ptile);
627  if (fortified) {
628  vdef->activity = ACTIVITY_FORTIFIED;
629  }
630 
631  db = POWER_FACTOR;
632  if (uclass_has_flag(defclass, UCF_TERRAIN_DEFENSE)) {
633  db += tile_terrain(ptile)->defense_bonus / (100 / POWER_FACTOR);
634  }
635  defensepower *= db;
636  defensepower *= vlevel->power_fact / 100;
637  if (!is_native_tile_to_class(defclass, ptile)) {
638  defensepower = defensepower * defclass->non_native_def_pct / 100;
639  }
640 
641  def = defense_multiplication(att_type, vdef, def_player, ptile,
642  defensepower);
643 
644  unit_virtual_destroy(vdef);
645 
646  return def;
647 }
648 
654 int get_total_defense_power(const struct unit *attacker,
655  const struct unit *defender)
656 {
657  return defense_multiplication(unit_type_get(attacker), defender,
658  unit_owner(defender), unit_tile(defender),
659  get_defense_power(defender));
660 }
661 
667 int get_fortified_defense_power(const struct unit *attacker,
668  struct unit *defender)
669 {
670  const struct unit_type *att_type = nullptr;
671  enum unit_activity real_act;
672  int def;
673  const struct unit_type *utype;
674 
675  if (attacker != nullptr) {
676  att_type = unit_type_get(attacker);
677  }
678 
679  real_act = defender->activity;
680 
681  utype = unit_type_get(defender);
682  if (utype_can_do_action_result(utype, ACTRES_FORTIFY)) {
683  defender->activity = ACTIVITY_FORTIFIED;
684  }
685 
686  def = defense_multiplication(att_type, defender, unit_owner(defender),
687  unit_tile(defender),
688  get_defense_power(defender));
689 
690  defender->activity = real_act;
691 
692  return def;
693 }
694 
700 static int get_defense_rating(const struct unit *attacker,
701  const struct unit *defender)
702 {
703  int afp, dfp;
704 
705  int rating = get_total_defense_power(attacker, defender);
706  get_modified_firepower(attacker, defender, &afp, &dfp);
707 
708  // How many rounds the defender will last
709  rating *= (defender->hp + afp - 1) / (afp ?: 1);
710 
711  rating *= dfp;
712 
713  return rating;
714 }
715 
721 struct unit *get_defender(const struct unit *attacker,
722  const struct tile *ptile)
723 {
724  struct unit *bestdef = nullptr;
725  int bestvalue = -99, best_cost = 0, rating_of_best = 0;
726 
727  /* Simply call win_chance with all the possible defenders in turn, and
728  * take the best one. It currently uses build cost as a tiebreaker in
729  * case 2 units are identical, but this is crude as build cost does not
730  * neccesarily have anything to do with the value of a unit. This function
731  * could be improved to take the value of the unit into account. It would
732  * also be nice if the function was a bit more fuzzy about prioritizing,
733  * making it able to fx choose a 1a/9d unit over a 10a/10d unit. It should
734  * also be able to spare units without full hp's to some extent, as these
735  * could be more valuable later. */
736  unit_list_iterate(ptile->units, defender)
737  {
738  /* We used to skip over allied units, but the logic for that is
739  * complicated and is now handled elsewhere. */
740  if (unit_can_defend_here(&(wld.map), defender)
741  && unit_attack_unit_at_tile_result(attacker, defender, ptile)
742  == ATT_OK) {
743  bool change = false;
744  int build_cost = unit_build_shield_cost_base(defender);
745  int defense_rating = get_defense_rating(attacker, defender);
746  // This will make units roughly evenly good defenders look alike.
747  int unit_def = static_cast<int>(
748  100000 * (1 - unit_win_chance(attacker, defender)));
749 
750  fc_assert_action(0 <= unit_def, continue);
751 
752  if (unit_has_type_flag(defender, UTYF_GAMELOSS)
753  && !is_stack_vulnerable(unit_tile(defender))) {
754  unit_def = -1; // then always use leader as last defender.
755  /* FIXME: multiple gameloss units with varying defense value
756  * not handled. */
757  }
758 
759  if (unit_def > bestvalue) {
760  change = true;
761  } else if (unit_def == bestvalue) {
762  if (build_cost < best_cost) {
763  change = true;
764  } else if (build_cost == best_cost) {
765  if (rating_of_best < defense_rating) {
766  change = true;
767  }
768  }
769  }
770 
771  if (change) {
772  bestvalue = unit_def;
773  bestdef = defender;
774  best_cost = build_cost;
775  rating_of_best = defense_rating;
776  }
777  }
778  }
780 
781  return bestdef;
782 }
783 
792 struct unit *get_diplomatic_defender(const struct unit *act_unit,
793  const struct unit *pvictim,
794  const struct tile *tgt_tile)
795 {
796  fc_assert_ret_val(act_unit, nullptr);
797  fc_assert_ret_val(tgt_tile, nullptr);
798 
799  unit_list_iterate(tgt_tile->units, punit)
800  {
801  if (unit_owner(punit) == unit_owner(act_unit)) {
802  /* I can't confirm if we won't deny that we weren't involved.
803  * (Won't defend against its owner.) */
804  continue;
805  }
806 
807  if (punit == pvictim && !unit_has_type_flag(punit, UTYF_SUPERSPY)) {
808  /* The victim unit is defenseless unless it's a SuperSpy.
809  * Rationalization: A regular diplomat don't mind being bribed. A
810  * SuperSpy is high enough up the chain that accepting a bribe is
811  * against his own interests. */
812  continue;
813  }
814 
815  if (!(unit_has_type_flag(punit, UTYF_DIPLOMAT)
816  || unit_has_type_flag(punit, UTYF_SUPERSPY))) {
817  /* A UTYF_SUPERSPY unit may not actually be a spy, but a superboss
818  * which we cannot allow puny diplomats from getting the better
819  * of. UTYF_SUPERSPY vs UTYF_SUPERSPY in a diplomatic contest always
820  * kills the attacker. */
821 
822  // The unit can't defend in a diplomatic battle.
823  continue;
824  }
825 
826  /* The first potential defender found is chosen. No priority is given
827  * to the best defender. */
828  return punit;
829  }
831 
832  // No diplomatic defender found.
833  return nullptr;
834 }
835 
839 bool is_stack_vulnerable(const struct tile *ptile)
840 {
841  return (game.info.killstack
842  && !tile_has_extra_flag(ptile, EF_NO_STACK_DEATH)
843  && nullptr == tile_city(ptile));
844 }
845 
852 int combat_bonus_against(const struct combat_bonus_list *list,
853  const struct unit_type *enemy,
854  enum combat_bonus_type type)
855 {
856  int value = 0;
857 
859  {
860  if (pbonus->type == type && utype_has_flag(enemy, pbonus->flag)) {
861  value += pbonus->value;
862  }
863  }
865 
866  return value;
867 }
868 
873 {
874  return game.info.tired_attack && moves_left < SINGLE_MOVE;
875 }
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
struct unit * get_defender(const struct unit *attacker, const struct tile *ptile)
Finds the best defender on the tile, given an attacker.
Definition: combat.cpp:721
bool is_unit_reachable_at(const struct unit *defender, const struct unit *attacker, const struct tile *location)
Can unit attack other at given location.
Definition: combat.cpp:83
enum unit_attack_result unit_attack_unit_at_tile_result(const struct unit *punit, const struct unit *pdefender, const struct tile *dest_tile)
Checks if a unit can physically attack pdefender at the tile (assuming it is adjacent and at war).
Definition: combat.cpp:121
enum unit_attack_result unit_attack_units_at_tile_result(const struct unit *punit, const struct tile *ptile)
Check if unit can attack unit stack at tile.
Definition: combat.cpp:237
bool is_stack_vulnerable(const struct tile *ptile)
Is it a city/fortress/air base or will the whole stack die in an attack.
Definition: combat.cpp:839
int get_attack_power(const struct unit *punit)
Convenience wrapper for base_get_attack_power.
Definition: combat.cpp:453
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
int get_fortified_defense_power(const struct unit *attacker, struct unit *defender)
Return total defense power of the unit if it fortifies, if possible, where it is.
Definition: combat.cpp:667
int base_get_attack_power(const struct unit_type *punittype, int veteran, int moves_left)
Returns the attack power, modified by moves left, and veteran status.
Definition: combat.cpp:463
int get_total_defense_power(const struct unit *attacker, const struct unit *defender)
return the modified defense power of a unit.
Definition: combat.cpp:654
static enum unit_attack_result unit_attack_any_at_tile_result(const struct unit *punit, const struct tile *ptile)
When unreachable_protects setting is FALSE: To attack a stack, unit must be able to attack some unit ...
Definition: combat.cpp:210
int get_virtual_defense_power(const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
May be called with a non-existing att_type to avoid any effects which depend on the attacker.
Definition: combat.cpp:601
static int get_defense_power(const struct unit *punit)
Returns the defense power, modified by terrain and veteran status.
Definition: combat.cpp:505
static int get_defense_rating(const struct unit *attacker, const struct unit *defender)
A number indicating the defense strength.
Definition: combat.cpp:700
struct city * sdi_try_defend(const struct player *owner, const struct tile *ptile)
Try defending against nuclear attack; if successful, return a city which had enough luck and EFT_NUKE...
Definition: combat.cpp:430
static enum unit_attack_result unit_attack_all_at_tile_result(const struct unit *punit, const struct tile *ptile)
When unreachable_protects setting is TRUE: To attack a stack, unit must be able to attack every unit ...
Definition: combat.cpp:168
bool is_tired_attack(int moves_left)
Returns if the attack is going to be a tired attack.
Definition: combat.cpp:872
struct unit * get_diplomatic_defender(const struct unit *act_unit, const struct unit *pvictim, const struct tile *tgt_tile)
Returns the defender of the tile in a diplomatic battle or nullptr if no diplomatic defender could be...
Definition: combat.cpp:792
static int defense_multiplication(const struct unit_type *att_type, const struct unit *def, const struct player *def_player, const struct tile *ptile, int defensepower)
Return an increased defensepower.
Definition: combat.cpp:549
double win_chance(int as, int ahp, int afp, int ds, int dhp, int dfp)
Returns the chance of the attacker winning, a number between 0 and 1.
Definition: combat.cpp:270
int get_total_attack_power(const struct unit *attacker, const struct unit *defender)
Return the modified attack power of a unit.
Definition: combat.cpp:526
int base_get_defense_power(const struct unit *punit)
Returns the defense power, modified by veteran status.
Definition: combat.cpp:487
double unit_win_chance(const struct unit *attacker, const struct unit *defender)
Returns a double in the range [0;1] indicating the attackers chance of winning.
Definition: combat.cpp:408
bool can_unit_attack_tile(const struct unit *punit, const struct tile *dest_tile)
Is unit (1) diplomatically allowed to attack and (2) physically able to do so?
Definition: combat.cpp:251
void get_modified_firepower(const struct unit *attacker, const struct unit *defender, int *att_fp, int *def_fp)
A unit's effective firepower depend on the situation.
Definition: combat.cpp:352
static bool can_player_attack_tile(const struct player *pplayer, const struct tile *ptile)
Checks if player is restricted diplomatically from attacking the tile.
Definition: combat.cpp:40
static bool is_unit_reachable_by_unit(const struct unit *defender, const struct unit *attacker)
Can unit attack other.
Definition: combat.cpp:71
#define POWER_FACTOR
Definition: combat.h:23
unit_attack_result
Definition: combat.h:25
@ ATT_NONNATIVE_DST
Definition: combat.h:30
@ ATT_OK
Definition: combat.h:26
@ ATT_NON_ATTACK
Definition: combat.h:27
@ ATT_UNREACHABLE
Definition: combat.h:28
@ ATT_NONNATIVE_SRC
Definition: combat.h:29
int get_target_bonus_effects(struct effect_list *plist, const struct player *target_player, const struct player *other_player, const struct city *target_city, const struct impr_type *target_building, const struct tile *target_tile, const struct unit *target_unit, const struct unit_type *target_unittype, const struct output_type *target_output, const struct specialist *target_specialist, const struct action *target_action, enum effect_type effect_type, enum vision_layer vision_layer, enum national_intelligence nintel)
Returns the effect bonus of a given type for any target.
Definition: effects.cpp:611
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, enum effect_type effect_type, enum vision_layer vision_layer)
Returns the effect bonus that applies at a tile for a given unittype.
Definition: effects.cpp:841
struct civ_game game
Definition: game.cpp:47
struct world wld
Definition: game.cpp:48
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
#define fc_assert_action(condition, action)
Definition: log.h:104
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition: map.h:312
#define square_iterate_end
Definition: map.h:315
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Return TRUE iff a unit of the given unit type can "exist" at this location.
Definition: movement.cpp:236
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
This tile is native to unit.
Definition: movement.cpp:279
bool unit_can_defend_here(const struct civ_map *nmap, const struct unit *punit)
Return TRUE iff the unit can be a defender at its current location.
Definition: movement.cpp:142
bool can_attack_non_native(const struct unit_type *utype)
This unit can attack non-native tiles (eg.
Definition: movement.cpp:164
#define SINGLE_MOVE
Definition: movement.h:17
static bool is_native_tile_to_class(const struct unit_class *punitclass, const struct tile *ptile)
Definition: movement.h:73
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players can attack each other.
Definition: player.cpp:1317
#define fc_rand(_size)
Definition: rand.h:16
#define MAX(x, y)
Definition: shared.h:48
Definition: city.h:291
struct player * owner
Definition: city.h:294
struct packet_game_info info
Definition: game.h:80
Definition: player.h:231
Definition: tile.h:42
struct unit_list * units
Definition: tile.h:50
int non_native_def_pct
Definition: unittype.h:128
int defense_strength
Definition: unittype.h:480
int firepower
Definition: unittype.h:490
struct veteran_system * veteran
Definition: unittype.h:509
bv_unit_classes targets
Definition: unittype.h:524
struct unit_type::@83 cache
int attack_strength
Definition: unittype.h:479
int defense_mp_bonuses_pct[U_LAST]
Definition: unittype.h:545
struct combat_bonus_list * bonuses
Definition: unittype.h:491
Definition: unit.h:134
enum unit_activity activity
Definition: unit.h:154
int moves_left
Definition: unit.h:147
int hp
Definition: unit.h:148
struct unit_order * list
Definition: unit.h:195
int veteran
Definition: unit.h:149
int power_fact
Definition: unittype.h:453
struct civ_map map
Definition: world_object.h:21
bool tile_has_extra_flag(const struct tile *ptile, enum extra_flag_id flag)
Check if tile contains extra providing effect.
Definition: tile.cpp:914
int tile_extras_defense_bonus(const struct tile *ptile, const struct unit_type *punittype)
Calculate defense bonus given for unit type by bases and roads.
Definition: tile.cpp:223
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Check if tile contains base native for unit.
Definition: tile.cpp:327
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
#define tile_terrain(_tile)
Definition: tile.h:93
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
Definition: unit.cpp:1588
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Create a virtual unit skeleton.
Definition: unit.cpp:1490
void unit_tile_set(struct unit *punit, struct tile *ptile)
Set the tile location of the unit.
Definition: unit.cpp:1200
bool unit_transported(const struct unit *pcargo)
Returns TRUE iff the unit is transported.
Definition: unit.cpp:2176
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_owner(_pu)
Definition: unit.h:370
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
int unit_build_shield_cost_base(const struct unit *punit)
Returns the number of shields this unit represents.
Definition: unittype.cpp:1185
struct unit_class * unit_class_get(const struct unit *punit)
Returns unit class pointer for a unit.
Definition: unittype.cpp:2151
bool utype_can_do_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
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Return TRUE iff units of the given type can do any enabler controlled action with the specified actio...
Definition: unittype.cpp:402
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
Definition: unittype.cpp:176
Unit_type_id utype_index(const struct unit_type *punittype)
Return the unit type index.
Definition: unittype.cpp:82
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Return TRUE iff units of the given type can do the specified generalized (ruleset defined) action ena...
Definition: unittype.cpp:386
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Return veteran level properties of given unit in given veterancy level.
Definition: unittype.cpp:2224
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition: unittype.h:704
#define utype_class(_t_)
Definition: unittype.h:691
#define combat_bonus_list_iterate_end
Definition: unittype.h:447
#define combat_bonus_list_iterate(bonuslist, pbonus)
Definition: unittype.h:445
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition: unittype.h:584
#define uclass_index(_c_)
Definition: unittype.h:684