Freeciv21
Develop your civilization from humble roots to a global empire
combat.cpp File Reference
#include <cmath>
#include "bitvector.h"
#include "log.h"
#include "rand.h"
#include "base.h"
#include "game.h"
#include "map.h"
#include "movement.h"
#include "packets.h"
#include "unit.h"
#include "unitlist.h"
#include "unittype.h"
#include "combat.h"
+ Include dependency graph for combat.cpp:

Go to the source code of this file.

Functions

static bool can_player_attack_tile (const struct player *pplayer, const struct tile *ptile)
 Checks if player is restricted diplomatically from attacking the tile. More...
 
static bool is_unit_reachable_by_unit (const struct unit *defender, const struct unit *attacker)
 Can unit attack other. More...
 
bool is_unit_reachable_at (const struct unit *defender, const struct unit *attacker, const struct tile *location)
 Can unit attack other at given location. More...
 
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). More...
 
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 there (not including transported units and UTYF_NEVER_PROTECTS units). More...
 
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 there (not including transported units). More...
 
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. More...
 
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? More...
 
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. More...
 
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. More...
 
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. More...
 
struct citysdi_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_PROOF. More...
 
int get_attack_power (const struct unit *punit)
 Convenience wrapper for base_get_attack_power. More...
 
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. More...
 
int base_get_defense_power (const struct unit *punit)
 Returns the defense power, modified by veteran status. More...
 
static int get_defense_power (const struct unit *punit)
 Returns the defense power, modified by terrain and veteran status. More...
 
int get_total_attack_power (const struct unit *attacker, const struct unit *defender)
 Return the modified attack power of a unit. More...
 
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. More...
 
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. More...
 
int get_total_defense_power (const struct unit *attacker, const struct unit *defender)
 return the modified defense power of a unit. More...
 
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. More...
 
static int get_defense_rating (const struct unit *attacker, const struct unit *defender)
 A number indicating the defense strength. More...
 
struct unitget_defender (const struct unit *attacker, const struct tile *ptile)
 Finds the best defender on the tile, given an attacker. More...
 
struct unitget_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 found. More...
 
bool is_stack_vulnerable (const struct tile *ptile)
 Is it a city/fortress/air base or will the whole stack die in an attack. More...
 
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. More...
 
bool is_tired_attack (int moves_left)
 Returns if the attack is going to be a tired attack. More...
 

Function Documentation

◆ base_get_attack_power()

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 at line 463 of file combat.cpp.

Referenced by adv_unittype_att_rating(), and get_attack_power().

◆ base_get_defense_power()

int base_get_defense_power ( const struct unit punit)

Returns the defense power, modified by veteran status.

Definition at line 487 of file combat.cpp.

Referenced by adv_unit_def_rating_basic(), get_defense_power(), and has_defense().

◆ can_player_attack_tile()

static bool can_player_attack_tile ( const struct player pplayer,
const struct tile ptile 
)
static

Checks if player is restricted diplomatically from attacking the tile.

Returns FALSE if 1) the tile is empty or 2) the tile contains a non-enemy city or 3) the tile contains a non-enemy unit

Definition at line 40 of file combat.cpp.

Referenced by can_unit_attack_tile().

◆ can_unit_attack_tile()

bool can_unit_attack_tile ( const struct unit punit,
const struct tile dest_tile 
)

◆ combat_bonus_against()

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.

Consider using cached values instead of calling this recalculation directly.

Definition at line 852 of file combat.cpp.

Referenced by dai_wants_defender_against(), defense_multiplication(), get_modified_firepower(), and move_type_from_extra().

◆ defense_multiplication()

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 
)
static

Return an increased defensepower.

Effects which increase the defensepower are:

  • unit type effects (horse vs pikemen for example)
  • defender in a fortress
  • fortified defender

May be called with a non-existing att_type to avoid any unit type effects.

Definition at line 549 of file combat.cpp.

Referenced by get_fortified_defense_power(), get_total_defense_power(), and get_virtual_defense_power().

◆ get_attack_power()

int get_attack_power ( const struct unit punit)

Convenience wrapper for base_get_attack_power.

Definition at line 453 of file combat.cpp.

Referenced by get_total_attack_power().

◆ get_defender()

struct unit* get_defender ( const struct unit attacker,
const struct tile ptile 
)

Finds the best defender on the tile, given an attacker.

The diplomatic relationship of attacker and defender is ignored; the caller should check this.

Definition at line 721 of file combat.cpp.

Referenced by dai_evaluate_tile_for_air_attack(), dai_hunter_manage(), dai_rampage_want(), do_attack(), find_best_tile_to_paradrop_to(), find_something_to_kill(), kill_something_with(), and unit_survive_autoattack().

◆ get_defense_power()

static int get_defense_power ( const struct unit punit)
static

Returns the defense power, modified by terrain and veteran status.

Note that rivers as special road types are not handled here as terrain property.

Definition at line 505 of file combat.cpp.

Referenced by get_fortified_defense_power(), and get_total_defense_power().

◆ get_defense_rating()

static int get_defense_rating ( const struct unit attacker,
const struct unit defender 
)
static

A number indicating the defense strength.

Unlike the one got from win chance this doesn't potentially get insanely small if the units are unevenly matched, unlike win_chance.

Definition at line 700 of file combat.cpp.

Referenced by get_defender().

◆ get_diplomatic_defender()

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 found.

Parameters
act_unitthe diplomatic attacker, trying to perform an action.
pvictimunit that should be excluded as a defender.
tgt_tilethe tile to defend.
Returns
the defender or nullptr if no diplomatic defender could be found.

Definition at line 792 of file combat.cpp.

Referenced by diplomat_infiltrate_tile().

◆ get_fortified_defense_power()

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.

attacker might be nullptr to skip calculating attacker specific bonuses.

Definition at line 667 of file combat.cpp.

Referenced by base_assess_defense_unit().

◆ get_modified_firepower()

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 at line 352 of file combat.cpp.

Referenced by dai_choose_defender_versus(), do_attack(), get_defense_rating(), unit_bombs_unit(), unit_versus_unit(), and unit_win_chance().

◆ get_total_attack_power()

int get_total_attack_power ( const struct unit attacker,
const struct unit defender 
)

Return the modified attack power of a unit.

Definition at line 526 of file combat.cpp.

Referenced by dai_choose_defender_versus(), do_attack(), unit_bombs_unit(), unit_versus_unit(), and unit_win_chance().

◆ get_total_defense_power()

int get_total_defense_power ( const struct unit attacker,
const struct unit defender 
)

return the modified defense power of a unit.

An veteran aegis cruiser in a mountain city with SAM and SDI defense being attacked by a missile gets defense 288.

Definition at line 654 of file combat.cpp.

Referenced by dai_choose_defender_versus(), do_attack(), get_defense_rating(), unit_bombs_unit(), unit_def_rating(), unit_versus_unit(), and unit_win_chance().

◆ get_virtual_defense_power()

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 at line 601 of file combat.cpp.

Referenced by find_a_good_partisan_spot(), is_my_turn(), and unittype_def_rating_squared().

◆ is_stack_vulnerable()

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 at line 839 of file combat.cpp.

Referenced by dai_rampage_want(), find_best_tile_to_paradrop_to(), get_defender(), kill_unit(), process_attacker_want(), stack_cost(), and stack_value().

◆ is_tired_attack()

bool is_tired_attack ( int  moves_left)

Returns if the attack is going to be a tired attack.

Definition at line 872 of file combat.cpp.

Referenced by base_get_attack_power(), and unit_tired_attack_string().

◆ is_unit_reachable_at()

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 at line 83 of file combat.cpp.

Referenced by kill_unit(), and unit_bombard().

◆ is_unit_reachable_by_unit()

static bool is_unit_reachable_by_unit ( const struct unit defender,
const struct unit attacker 
)
static

Can unit attack other.

Definition at line 71 of file combat.cpp.

Referenced by is_unit_reachable_at().

◆ sdi_try_defend()

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_PROOF.

If the attack was successful return nullptr.

Definition at line 430 of file combat.cpp.

Referenced by unit_nuke().

◆ unit_attack_all_at_tile_result()

static enum unit_attack_result unit_attack_all_at_tile_result ( const struct unit punit,
const struct tile ptile 
)
static

When unreachable_protects setting is TRUE: To attack a stack, unit must be able to attack every unit there (not including transported units and UTYF_NEVER_PROTECTS units).

Definition at line 83 of file combat.cpp.

◆ unit_attack_any_at_tile_result()

static enum unit_attack_result unit_attack_any_at_tile_result ( const struct unit punit,
const struct tile ptile 
)
static

When unreachable_protects setting is FALSE: To attack a stack, unit must be able to attack some unit there (not including transported units).

Definition at line 83 of file combat.cpp.

◆ unit_attack_unit_at_tile_result()

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).

Unit can NOT attack if: 1) its unit type is unable to perform any attack action. 2) it is a ground unit without marine ability and it attacks from ocean. 3) it is a ground unit and it attacks a target on an ocean square or it is a sailing unit without shore bombardment capability and it attempts to attack land. 4) it is not a fighter and defender is a flying unit (except city/airbase).

Does NOT check: 1) Moves left 2) Adjacency 3) Diplomatic status

Definition at line 83 of file combat.cpp.

Referenced by adv_danger_at(), dai_consider_tile_dangerous(), find_best_tile_to_paradrop_to(), get_defender(), is_my_turn(), and stack_cost().

◆ unit_attack_units_at_tile_result()

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 at line 83 of file combat.cpp.

Referenced by action_not_enabled_reason(), adv_danger_at(), can_unit_attack_tile(), dai_consider_tile_dangerous(), and is_my_turn().

◆ unit_win_chance()

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.

The calculation takes all factors into account.

Definition at line 408 of file combat.cpp.

Referenced by dai_evaluate_tile_for_air_attack(), dai_hunter_manage(), dai_rampage_want(), find_best_tile_to_paradrop_to(), get_defender(), popup_info_text(), and unit_survive_autoattack().

◆ win_chance()

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.

If you want the chance that the defender wins just use 1-chance(...)

NOTE: this number can be very small, fx in a battle between an ironclad and a battleship the ironclad has less than 1/100000 chance of winning.

The algoritm calculates the probability of each possible number of HP's the attacker has left. Maybe that info should be preserved for use in the AI.

Definition at line 270 of file combat.cpp.

Referenced by unit_win_chance().