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