Damage calculation dialog: use existing battle prediction

The game already predicts the outcome of all possible battles when the
player opens the unit attack dialog, for the purpose of choosing the best
weapon to attack. Simply use that data in the damage calculation dialog
instead of recalculating it.

The change in mouse_handler::fill_weapon_choices() ensures that the damage
prediction will be present for all weapons. If bc is first copied to the
vector and bc::better_attack() is called only after that, the copy of bc in
the vector will never receive the damage prediction result.
This commit is contained in:
Jyrki Vesterinen 2016-08-12 22:06:10 +03:00
parent 5f9a711dab
commit 265e41dbda
3 changed files with 9 additions and 7 deletions

View file

@ -48,7 +48,7 @@ const int battle_prediction_pane::inter_column_gap_ = 30;
const int battle_prediction_pane::inter_units_gap_ = 30; const int battle_prediction_pane::inter_units_gap_ = 30;
const int battle_prediction_pane::max_hp_distrib_rows_ = 10; const int battle_prediction_pane::max_hp_distrib_rows_ = 10;
battle_prediction_pane::battle_prediction_pane(const battle_context &bc, battle_prediction_pane::battle_prediction_pane(battle_context &bc,
const map_location &attacker_loc, const map_location &defender_loc) : const map_location &attacker_loc, const map_location &defender_loc) :
gui::preview_pane(resources::screen->video()), gui::preview_pane(resources::screen->video()),
attacker_loc_(attacker_loc), attacker_loc_(attacker_loc),
@ -86,9 +86,8 @@ battle_prediction_pane::battle_prediction_pane(const battle_context &bc,
dialog_height_(0) dialog_height_(0)
{ {
// Predict the battle outcome. // Predict the battle outcome.
combatant attacker_combatant(bc.get_attacker_stats()); const combatant& attacker_combatant = bc.get_attacker_combatant();
combatant defender_combatant(bc.get_defender_stats()); const combatant& defender_combatant = bc.get_defender_combatant();
attacker_combatant.fight(defender_combatant);
const battle_context_unit_stats& attacker_stats = bc.get_attacker_stats(); const battle_context_unit_stats& attacker_stats = bc.get_attacker_stats();
const battle_context_unit_stats& defender_stats = bc.get_defender_stats(); const battle_context_unit_stats& defender_stats = bc.get_defender_stats();

View file

@ -29,8 +29,11 @@ class battle_prediction_pane : public gui::preview_pane
public: public:
// Lengthy constructor. // Lengthy constructor.
battle_prediction_pane(const battle_context& bc, battle_prediction_pane(battle_context& bc,
const map_location& attacker_loc, const map_location& defender_loc); const map_location& attacker_loc, const map_location& defender_loc);
battle_prediction_pane(battle_context&& bc,
const map_location& attacker_loc, const map_location& defender_loc) :
battle_prediction_pane(bc, attacker_loc, defender_loc) {}
// This method is called to draw the dialog contents. // This method is called to draw the dialog contents.
void draw_contents(); void draw_contents();

View file

@ -946,11 +946,11 @@ int mouse_handler::fill_weapon_choices(std::vector<battle_context>& bc_vector, u
// skip weapons with attack_weight=0 // skip weapons with attack_weight=0
if (attacker->attacks()[i].attack_weight() > 0) { if (attacker->attacks()[i].attack_weight() > 0) {
battle_context bc(pc_.gamestate().board_.units_, attacker->get_location(), defender->get_location(), i); battle_context bc(pc_.gamestate().board_.units_, attacker->get_location(), defender->get_location(), i);
bc_vector.push_back(bc); if (!bc_vector.empty() && bc.better_attack(bc_vector[best], 0.5)) {
if (bc.better_attack(bc_vector[best], 0.5)) {
// as some weapons can be hidden, i is not a valid index into the resulting vector // as some weapons can be hidden, i is not a valid index into the resulting vector
best = bc_vector.size() - 1; best = bc_vector.size() - 1;
} }
bc_vector.push_back(bc);
} }
} }
return best; return best;