The Battle for Wesnoth  1.19.5+dev
attack_prediction.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include <vector>
19 #include <array>
20 
22 
23 // This encapsulates all we need to know for this combat.
24 /** All combat-related info. */
25 struct combatant
26 {
27  /** Construct a combatant. */
28  combatant(const battle_context_unit_stats &u, const combatant *prev = nullptr);
29 
30  /** Copy constructor */
31  combatant(const combatant &that, const battle_context_unit_stats &u);
32 
33  combatant(const combatant &that) = delete;
34  combatant& operator=(const combatant &) = delete;
35 
36  /** Simulate a fight! Can be called multiple times for cumulative calculations. */
37  void fight(combatant &opponent, bool levelup_considered=true);
38 
39  /** Resulting probability distribution (might be not as large as max_hp) */
40  std::vector<double> hp_dist;
41 
42  /** Resulting chance we were not hit by this opponent (important if it poisons) */
43  double untouched;
44 
45  /** Resulting chance we are poisoned. */
46  double poisoned;
47 
48  /** Resulting chance we are slowed. */
49  double slowed;
50 
51  /** What's the average hp (weighted average of hp_dist). */
52  double average_hp(unsigned int healing = 0) const;
53 
54 #if defined(BENCHMARK) || defined(CHECK)
55  // Functions used in the stand-alone version of attack_prediction.cpp
56  void print(const char label[], unsigned int battle, unsigned int fighter) const;
57  void reset();
58 #endif
59 
61 
62 private:
63  static const unsigned int MONTE_CARLO_SIMULATION_THRESHOLD = 50000u;
64 
65  /** Summary of matrix used to calculate last battle (unslowed & slowed).
66  * Invariant: summary[1].size() == summary[0].size() or summary[1].empty() */
67  std::array<std::vector<double>, 2> summary;
68 };
map_location prev
Definition: astarsearch.cpp:64
static void print(std::stringstream &sstr, const std::string &queue, const std::string &id)
Definition: fire_event.cpp:30
std::string label
What to show in the filter's drop-down list.
Definition: manager.cpp:200
Structure describing the statistics of a unit involved in the battle.
Definition: attack.hpp:51
All combat-related info.
double slowed
Resulting chance we are slowed.
combatant & operator=(const combatant &)=delete
combatant(const combatant &that)=delete
std::vector< double > hp_dist
Resulting probability distribution (might be not as large as max_hp)
double poisoned
Resulting chance we are poisoned.
static const unsigned int MONTE_CARLO_SIMULATION_THRESHOLD
const battle_context_unit_stats & u_
std::array< std::vector< double >, 2 > summary
Summary of matrix used to calculate last battle (unslowed & slowed).
double average_hp(unsigned int healing=0) const
What's the average hp (weighted average of hp_dist).
void fight(combatant &opponent, bool levelup_considered=true)
Simulate a fight! Can be called multiple times for cumulative calculations.
combatant(const battle_context_unit_stats &u, const combatant *prev=nullptr)
Construct a combatant.
double untouched
Resulting chance we were not hit by this opponent (important if it poisons)