The Battle for Wesnoth  1.15.0-dev
Public Member Functions | Static Public Member Functions | 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, const unit *attacker_ptr=nullptr)
 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 (const battle_context &other)
 
 battle_context (battle_context &&other)=default
 
battle_contextoperator= (const battle_context &other)
 
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...
 

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

int choose_attacker_weapon (const unit &attacker, const unit &defender, const unit_map &units, const map_location &attacker_loc, const map_location &defender_loc, double harm_weight, int *defender_weapon, const combatant *prev_def)
 
int choose_defender_weapon (const unit &attacker, const unit &defender, unsigned attacker_weapon, const unit_map &units, 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 170 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,
const unit attacker_ptr = nullptr 
)

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 352 of file attack.cpp.

References _(), attacker_combatant_, attacker_stats_, unit::attacks(), choose_attacker_weapon(), choose_defender_weapon(), defender_combatant_, defender_stats_, unit_map::find(), and VALIDATE.

Referenced by 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 411 of file attack.cpp.

◆ battle_context() [3/4]

battle_context::battle_context ( const battle_context other)

Definition at line 419 of file attack.cpp.

◆ battle_context() [4/4]

battle_context::battle_context ( battle_context &&  other)
default

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 477 of file attack.cpp.

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

◆ 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

◆ choose_attacker_weapon()

int battle_context::choose_attacker_weapon ( const unit attacker,
const unit defender,
const unit_map units,
const map_location attacker_loc,
const map_location defender_loc,
double  harm_weight,
int *  defender_weapon,
const combatant prev_def 
)
private

◆ choose_defender_weapon()

int battle_context::choose_defender_weapon ( const unit attacker,
const unit defender,
unsigned  attacker_weapon,
const unit_map units,
const map_location attacker_loc,
const map_location defender_loc,
const combatant prev_def 
)
private
Todo:
FIXME: Hand previous defender unit in here.

Definition at line 648 of file attack.cpp.

References _(), a_, config::add_child(), unit::anim_comp(), statistics::attack_context::attack_expected_damage(), statistics::attack_context::attack_result(), attacker_combatant_, attacker_stats_, unit::attacks(), battle_context(), better_combat(), battle_context_unit_stats::chance_to_hit, checkup_instance, config::clear(), game_data::clear_variable(), unit::create(), battle_context_unit_stats::damage, DBG_NG, statistics::attack_context::defend_result(), defender_combatant_, defender_stats_, attack_type::defense_weight(), battle_context_unit_stats::disable, battle_context_unit_stats::drain_constant, battle_context_unit_stats::drain_percent, battle_context_unit_stats::drains, unit::dying_unit_loc, preferences::encountered_units(), unit::experience(), unit_race::FEMALE, combatant::fight(), unit_type_data::find(), game_events::wml_event_pump::fire(), gui2::event::implementation::fire_event(), resources::game_events, resources::gameboard, resources::gamedata, unit::gender(), randomness::generator, preferences::get(), map_location::get_opposite_dir(), randomness::rng::get_random_int(), game_display::get_singleton(), display::get_singleton(), unit::get_state(), game_board::get_team(), game_data::get_variable(), statistics::attack_context::HITS, i, config::insert(), game_config::kill_xp(), statistics::attack_context::KILLS, loc_, checkup::local_checkup(), LOG_NG, unit_race::MALE, statistics::attack_context::MISSES, n, map_location::null_location(), battle_context_unit_stats::num_blows, game_config::sounds::status::petrified, battle_context_unit_stats::petrifies, battle_context_unit_stats::plague_type, battle_context_unit_stats::plagues, game_config::sounds::status::poisoned, battle_context_unit_stats::poisons, replay::process_error(), game_events::manager::pump(), attack_type::range(), actions::recalculate_fog(), display::redraw_minimap(), s, unit::set_experience(), unit_animation_component::set_standing(), unit::set_state(), battle_context_unit_stats::slow_damage, game_config::sounds::status::slowed, battle_context_unit_stats::slows, unit::STATE_NOT_MOVED, unit::STATE_PETRIFIED, unit::STATE_POISONED, unit::STATE_SLOWED, unit::STATE_UNCOVERED, unit_display::unit_attack(), unit_display::unit_die(), unit_display::unit_draw_weapon(), unit_display::unit_sheath_weapon(), unit_types, unit_map::iterator_base< iter_types >::valid(), VALIDATE, battle_context_unit_stats::weapon, resources::whiteboard, map_location::wml_x(), and map_location::wml_y().

Referenced by battle_context(), and choose_attacker_weapon().

◆ 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 446 of file attack.cpp.

References attacker_combatant_, attacker_stats_, defender_combatant_, and defender_stats_.

Referenced by ai::attack_analysis::analyze(), better_attack(), and ai::attack_result::do_execute().

◆ 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 198 of file attack.hpp.

Referenced by ai::attack_analysis::analyze(), ai::attack_result::do_execute(), 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 204 of file attack.hpp.

Referenced by ai::attack_analysis::analyze(), wfl::attack_callable::defender_weapon(), and ai::attack_result::do_execute().

◆ operator=() [1/2]

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

Definition at line 428 of file attack.cpp.

References attacker_combatant_, attacker_stats_, defender_combatant_, and defender_stats_.

◆ operator=() [2/2]

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

Member Data Documentation

◆ attacker_combatant_

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

◆ attacker_stats_

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

◆ defender_combatant_

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

◆ defender_stats_

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

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