The Battle for Wesnoth  1.19.0-dev
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
battle_context Class Reference

Computes the statistics of a battle between an attacker and a defender unit. More...

#include <attack.hpp>

Public Member Functions

 battle_context (const unit_map &units, const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon=-1, int defender_weapon=-1, double aggression=0.0, const combatant *prev_def=nullptr, unit_const_ptr attacker_ptr=unit_const_ptr(), unit_const_ptr defender_ptr=unit_const_ptr())
 If no attacker_weapon is given, we select the best one, based on harm_weight (1.0 means 1 hp lost counters 1 hp damage, 0.0 means we ignore harm weight). More...
 
 battle_context (const battle_context_unit_stats &att, const battle_context_unit_stats &def)
 Used by the AI which caches battle_context_unit_stats. More...
 
 battle_context (battle_context &&other)=default
 
battle_contextoperator= (battle_context &&other)=default
 
const battle_context_unit_statsget_attacker_stats () const
 This method returns the statistics of the attacker. More...
 
const battle_context_unit_statsget_defender_stats () const
 This method returns the statistics of the defender. More...
 
const combatantget_attacker_combatant (const combatant *prev_def=nullptr)
 Get the simulation results. More...
 
const combatantget_defender_combatant (const combatant *prev_def=nullptr)
 
bool better_attack (class battle_context &that, double harm_weight)
 Given this harm_weight, is this attack better than that? More...
 
bool better_defense (class battle_context &that, double harm_weight)
 Given this harm_weight, is this attack better than that? More...
 
void simulate (const combatant *prev_def)
 

Static Public Member Functions

static bool better_combat (const combatant &us_a, const combatant &them_a, const combatant &us_b, const combatant &them_b, double harm_weight)
 

Private Member Functions

 battle_context (nonempty_unit_const_ptr attacker, const map_location &attacker_loc, int attacker_weapon, nonempty_unit_const_ptr defender, const map_location &defender_loc, int defender_weapon)
 

Static Private Member Functions

static battle_context choose_attacker_weapon (nonempty_unit_const_ptr attacker, nonempty_unit_const_ptr defender, const map_location &attacker_loc, const map_location &defender_loc, double harm_weight, const combatant *prev_def)
 
static battle_context choose_defender_weapon (nonempty_unit_const_ptr attacker, nonempty_unit_const_ptr defender, unsigned attacker_weapon, const map_location &attacker_loc, const map_location &defender_loc, const combatant *prev_def)
 

Private Attributes

std::unique_ptr< battle_context_unit_statsattacker_stats_
 Statistics of the units. More...
 
std::unique_ptr< battle_context_unit_statsdefender_stats_
 
std::unique_ptr< combatantattacker_combatant_
 Outcome of simulated fight. More...
 
std::unique_ptr< combatantdefender_combatant_
 

Detailed Description

Computes the statistics of a battle between an attacker and a defender unit.

Definition at line 166 of file attack.hpp.

Constructor & Destructor Documentation

◆ battle_context() [1/4]

battle_context::battle_context ( const unit_map units,
const map_location attacker_loc,
const map_location defender_loc,
int  attacker_weapon = -1,
int  defender_weapon = -1,
double  aggression = 0.0,
const combatant prev_def = nullptr,
unit_const_ptr  attacker_ptr = unit_const_ptr(),
unit_const_ptr  defender_ptr = unit_const_ptr() 
)

If no attacker_weapon is given, we select the best one, based on harm_weight (1.0 means 1 hp lost counters 1 hp damage, 0.0 means we ignore harm weight).

prev_def is for predicting multiple attacks against a defender.

Definition at line 381 of file attack.cpp.

References attacker_stats_, choose_attacker_weapon(), choose_defender_weapon(), defender_stats_, unit_map::find(), and unit_map::iterator_base< iter_types >::get_shared_ptr().

Referenced by choose_attacker_weapon(), and choose_defender_weapon().

◆ battle_context() [2/4]

battle_context::battle_context ( const battle_context_unit_stats att,
const battle_context_unit_stats def 
)

Used by the AI which caches battle_context_unit_stats.

Definition at line 425 of file attack.cpp.

◆ battle_context() [3/4]

battle_context::battle_context ( battle_context &&  other)
default

◆ battle_context() [4/4]

battle_context::battle_context ( nonempty_unit_const_ptr  attacker,
const map_location attacker_loc,
int  attacker_weapon,
nonempty_unit_const_ptr  defender,
const map_location defender_loc,
int  defender_weapon 
)
private

Definition at line 346 of file attack.cpp.

References attacker_stats_, defender_stats_, and utf8::size().

Member Function Documentation

◆ better_attack()

bool battle_context::better_attack ( class battle_context that,
double  harm_weight 
)

Given this harm_weight, is this attack better than that?

Definition at line 451 of file attack.cpp.

References better_combat(), get_attacker_combatant(), and get_defender_combatant().

Referenced by choose_attacker_weapon(), and events::mouse_handler::fill_weapon_choices().

◆ better_combat()

bool battle_context::better_combat ( const combatant us_a,
const combatant them_a,
const combatant us_b,
const combatant them_b,
double  harm_weight 
)
static

◆ better_defense()

bool battle_context::better_defense ( class battle_context that,
double  harm_weight 
)

Given this harm_weight, is this attack better than that?

Definition at line 463 of file attack.cpp.

References better_combat(), get_attacker_combatant(), and get_defender_combatant().

Referenced by choose_defender_weapon().

◆ choose_attacker_weapon()

battle_context battle_context::choose_attacker_weapon ( nonempty_unit_const_ptr  attacker,
nonempty_unit_const_ptr  defender,
const map_location attacker_loc,
const map_location defender_loc,
double  harm_weight,
const combatant prev_def 
)
staticprivate

◆ choose_defender_weapon()

battle_context battle_context::choose_defender_weapon ( nonempty_unit_const_ptr  attacker,
nonempty_unit_const_ptr  defender,
unsigned  attacker_weapon,
const map_location attacker_loc,
const map_location defender_loc,
const combatant prev_def 
)
staticprivate

◆ get_attacker_combatant()

const combatant & battle_context::get_attacker_combatant ( const combatant prev_def = nullptr)

Get the simulation results.

Todo:
FIXME: better to initialize combatant initially (move into battle_context_unit_stats?), just do fight() when required.

Definition at line 436 of file attack.cpp.

References attacker_combatant_, and simulate().

Referenced by better_attack(), better_defense(), ai::attack_result::do_execute(), and game_lua_kernel::intf_simulate_combat().

◆ get_attacker_stats()

const battle_context_unit_stats& battle_context::get_attacker_stats ( ) const
inline

This method returns the statistics of the attacker.

Definition at line 193 of file attack.hpp.

References attacker_stats_.

Referenced by ai::attack_result::do_execute(), events::mouse_handler::fill_weapon_choices(), game_lua_kernel::intf_simulate_combat(), and wfl::attack_callable::weapon().

◆ get_defender_combatant()

const combatant & battle_context::get_defender_combatant ( const combatant prev_def = nullptr)

◆ get_defender_stats()

const battle_context_unit_stats& battle_context::get_defender_stats ( ) const
inline

This method returns the statistics of the defender.

Definition at line 199 of file attack.hpp.

References defender_stats_.

Referenced by wfl::attack_callable::defender_weapon(), ai::attack_result::do_execute(), and game_lua_kernel::intf_simulate_combat().

◆ operator=()

battle_context& battle_context::operator= ( battle_context &&  other)
default

◆ simulate()

void battle_context::simulate ( const combatant prev_def)

Member Data Documentation

◆ attacker_combatant_

std::unique_ptr<combatant> battle_context::attacker_combatant_
private

Outcome of simulated fight.

Definition at line 248 of file attack.hpp.

Referenced by get_attacker_combatant(), and simulate().

◆ attacker_stats_

std::unique_ptr<battle_context_unit_stats> battle_context::attacker_stats_
private

Statistics of the units.

Definition at line 244 of file attack.hpp.

Referenced by battle_context(), choose_attacker_weapon(), choose_defender_weapon(), get_attacker_stats(), and simulate().

◆ defender_combatant_

std::unique_ptr<combatant> battle_context::defender_combatant_
private

Definition at line 249 of file attack.hpp.

Referenced by get_defender_combatant(), and simulate().

◆ defender_stats_

std::unique_ptr<battle_context_unit_stats> battle_context::defender_stats_
private

Definition at line 245 of file attack.hpp.

Referenced by battle_context(), choose_defender_weapon(), get_defender_stats(), and simulate().


The documentation for this class was generated from the following files: