Freeciv-3.3
Loading...
Searching...
No Matches
Functions
combat.c File Reference
#include <math.h>
#include "bitvector.h"
#include "rand.h"
#include "log.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"

Go to the source code of this file.

Functions

static bool can_player_attack_tile (const struct player *pplayer, const struct tile *ptile)
 
static bool is_unit_reachable_by_unit (const struct unit *defender, const struct unit *attacker)
 
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)
 
static enum unit_attack_result unit_attack_all_at_tile_result (const struct unit *punit, const struct action *paction, const struct tile *ptile)
 
static enum unit_attack_result unit_attack_any_at_tile_result (const struct unit *punit, const struct action *paction, const struct tile *ptile)
 
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 *dest_tile)
 
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)
 
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)
 
static int get_defense_power (const struct unit *punit)
 
int get_total_attack_power (const struct unit *attacker, const struct unit *defender, const struct action *paction)
 
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)
 
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)
 
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)
 
static int get_defense_rating (const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender)
 
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)
 

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

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

Referenced by can_unit_attack_tile().

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

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

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

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 540 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 911 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 841 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_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 594 of file combat.c.

Referenced by get_fortified_defense_power(), and get_total_defense_power().

◆ get_defense_rating()

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

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,
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 947 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 786 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 411 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 615 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 772 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 717 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 994 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 532 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 86 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().

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

Referenced by is_unit_reachable_at().

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

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 action paction,
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 189 of file combat.c.

Referenced by unit_attack_units_at_tile_result().

◆ unit_attack_any_at_tile_result()

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

Referenced by unit_attack_units_at_tile_result().

◆ 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 123 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 257 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 1025 of file combat.c.

Referenced by unit_bombs_unit().

◆ 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 480 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 272 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 334 of file combat.c.

Referenced by unit_win_chance().