Freeciv-3.2
Loading...
Searching...
No Matches
Macros | Enumerations | Functions
combat.h File Reference
#include "fc_types.h"
#include "unittype.h"

Go to the source code of this file.

Macros

#define POWER_FACTOR   10
 

Enumerations

enum  unit_attack_result {
  ATT_OK , ATT_NON_ATTACK , ATT_UNREACHABLE , ATT_NONNATIVE_SRC ,
  ATT_NONNATIVE_DST , ATT_NOT_WIPABLE
}
 

Functions

bool is_unit_reachable_at (const struct unit *defender, const struct unit *attacker, const struct tile *location)
 
enum unit_attack_result unit_attack_unit_at_tile_result (const struct unit *punit, const struct action *paction, const struct unit *pdefender, const struct tile *dest_tile)
 
enum unit_attack_result unit_attack_units_at_tile_result (const struct unit *punit, const struct action *paction, const struct tile *ptile)
 
enum unit_attack_result unit_wipe_units_at_tile_result (const struct unit *punit, const struct tile *ptile)
 
bool can_unit_attack_tile (const struct unit *punit, const struct action *paction, const struct tile *ptile)
 
double win_chance (int as, int ahp, int afp, int ds, int dhp, int dfp)
 
void get_modified_firepower (const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender, int *att_fp, int *def_fp)
 
double unit_win_chance (const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender, const struct action *paction)
 
bool unit_really_ignores_citywalls (const struct unit *punit)
 
struct citysdi_try_defend (const struct civ_map *nmap, const struct player *owner, const struct tile *ptile)
 
bool is_tired_attack (int moves_left)
 
int get_attack_power (const struct unit *punit)
 
int base_get_attack_power (const struct unit_type *punittype, int veteran, int moves_left)
 
int base_get_defense_power (const struct unit *punit)
 
int get_total_defense_power (const struct unit *attacker, const struct unit *defender)
 
int get_fortified_defense_power (const struct unit *attacker, struct unit *defender)
 
int get_virtual_defense_power (const struct civ_map *nmap, const struct unit_type *attacker, const struct unit_type *defender, struct player *defending_player, struct tile *ptile, bool fortified, int veteran)
 
int get_total_attack_power (const struct unit *attacker, const struct unit *defender, const struct action *paction)
 
struct unitget_defender (const struct civ_map *nmap, const struct unit *attacker, const struct tile *ptile, const struct action *paction)
 
struct unitget_attacker (const struct civ_map *nmap, const struct unit *defender, const struct tile *ptile)
 
struct unitget_diplomatic_defender (const struct unit *act_unit, const struct unit *pvictim, const struct tile *tgt_tile, const struct action *paction)
 
bool is_stack_vulnerable (const struct tile *ptile)
 
int combat_bonus_against (const struct combat_bonus_list *list, const struct unit_type *enemy, enum combat_bonus_type type)
 
int unit_bombard_rate (struct unit *punit)
 

Macro Definition Documentation

◆ POWER_FACTOR

#define POWER_FACTOR   10

Definition at line 32 of file combat.h.

Enumeration Type Documentation

◆ unit_attack_result

Enumerator
ATT_OK 
ATT_NON_ATTACK 
ATT_UNREACHABLE 
ATT_NONNATIVE_SRC 
ATT_NONNATIVE_DST 
ATT_NOT_WIPABLE 

Definition at line 34 of file combat.h.

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 545 of file combat.c.

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 569 of file combat.c.

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

◆ can_unit_attack_tile()

bool can_unit_attack_tile ( const struct unit punit,
const struct action paction,
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 1002 of file combat.c.

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

◆ get_attack_power()

int get_attack_power ( const struct unit punit)

Convenience wrapper for base_get_attack_power().

Definition at line 535 of file combat.c.

Referenced by get_total_attack_power().

◆ get_attacker()

struct unit * get_attacker ( const struct civ_map nmap,
const struct unit defender,
const struct tile ptile 
)

Get unit at (x, y) that wants to kill defender.

Works like get_defender(); see comment there. This function is mostly used by the AI.

Definition at line 906 of file combat.c.

Referenced by popup_advanced_terrain_dialog().

◆ get_defender()

struct unit * get_defender ( const struct civ_map nmap,
const struct unit attacker,
const struct tile ptile,
const struct action paction 
)

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 836 of file combat.c.

Referenced by action_prob(), 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(), popup_advanced_terrain_dialog(), and unit_survive_autoattack().

◆ get_diplomatic_defender()

struct unit * get_diplomatic_defender ( const struct unit act_unit,
const struct unit pvictim,
const struct tile tgt_tile,
const struct action paction 
)

Returns the defender of the tile in a diplomatic battle or NULL 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.
pactionaction that the attacker performs.
Returns
the defender or NULL if no diplomatic defender could be found.

Definition at line 942 of file combat.c.

Referenced by ap_diplomat_battle(), and 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 NULL to skip calculating attacker specific bonuses.

Definition at line 781 of file combat.c.

Referenced by base_assess_defense_unit().

◆ get_modified_firepower()

void get_modified_firepower ( const struct civ_map nmap,
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 409 of file combat.c.

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,
const struct action paction 
)

Return the modified attack power of a unit.

Definition at line 610 of file combat.c.

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 767 of file combat.c.

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

◆ get_virtual_defense_power()

int get_virtual_defense_power ( const struct civ_map nmap,
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 712 of file combat.c.

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 989 of file combat.c.

Referenced by collect_ransom(), 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 527 of file combat.c.

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 85 of file combat.c.

Referenced by actres_possible(), kill_unit(), unit_attack_unit_at_tile_result(), unit_bombard(), and unit_wipe_units_at_tile_result().

◆ sdi_try_defend()

struct city * sdi_try_defend ( const struct civ_map nmap,
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 NULL.

Definition at line 501 of file combat.c.

Referenced by unit_nuke().

◆ unit_attack_unit_at_tile_result()

enum unit_attack_result unit_attack_unit_at_tile_result ( const struct unit punit,
const struct action paction,
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 122 of file combat.c.

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

◆ unit_attack_units_at_tile_result()

enum unit_attack_result unit_attack_units_at_tile_result ( const struct unit punit,
const struct action paction,
const struct tile ptile 
)

Check if unit can attack unit stack at tile.

Definition at line 256 of file combat.c.

Referenced by adv_danger_at(), can_unit_attack_tile(), dai_consider_tile_dangerous(), expl_act_not_enabl(), is_action_possible(), and is_my_turn().

◆ unit_bombard_rate()

int unit_bombard_rate ( struct unit punit)

Get unit's current bombard rate.

Definition at line 1020 of file combat.c.

Referenced by unit_bombs_unit().

◆ unit_really_ignores_citywalls()

bool unit_really_ignores_citywalls ( const struct unit punit)

◆ unit_win_chance()

double unit_win_chance ( const struct civ_map nmap,
const struct unit attacker,
const struct unit defender,
const struct action paction 
)

Returns a double in the range [0;1] indicating the attackers chance of winning. The calculation takes all factors into account.

Definition at line 478 of file combat.c.

Referenced by action_prob(), combat_odds_to_astr(), dai_evaluate_tile_for_air_attack(), dai_hunter_manage(), dai_rampage_want(), find_best_tile_to_paradrop_to(), get_attacker(), get_defender(), sdl_get_chance_to_win(), and unit_survive_autoattack().

◆ unit_wipe_units_at_tile_result()

enum unit_attack_result unit_wipe_units_at_tile_result ( const struct unit punit,
const struct tile ptile 
)

Check if unit can wipe unit stack from tile.

Definition at line 271 of file combat.c.

Referenced by expl_act_not_enabl().

◆ 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 332 of file combat.c.

Referenced by unit_win_chance().