The Battle for Wesnoth  1.15.13+dev
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <>
3  Part of the Battle for Wesnoth Project
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,
12  See the COPYING file for more details.
13 */
15 #pragma once
17 class config;
18 class config_writer;
19 class unit;
20 #include <string>
21 #include <map>
22 #include <vector>
24 namespace statistics
25 {
26  struct stats
27  {
28  stats();
29  explicit stats(const config& cfg);
31  config write() const;
32  void write(config_writer &out) const;
33  void read(const config& cfg);
35  typedef std::map<std::string,int> str_int_map;
39  /*
40  * A type that will map a string of hit/miss to the number of times
41  * that sequence has occurred.
42  */
43  typedef str_int_map battle_sequence_frequency_map;
45  /** A type that will map different % chances to hit to different results. */
46  typedef std::map<int,battle_sequence_frequency_map> battle_result_map;
48  /** Statistics of this side's attacks on its own turns. */
49  battle_result_map attacks_inflicted;
50  /** Statistics of this side's attacks on enemies' turns. */
51  battle_result_map defends_inflicted;
52  /** Statistics of enemies' counter attacks on this side's turns. */
53  battle_result_map attacks_taken;
54  /** Statistics of enemies' attacks against this side on their turns. */
55  battle_result_map defends_taken;
60  struct hitrate_t
61  {
62  int strikes; //< Number of strike attempts at the given CTH
63  int hits; //< Number of strikes that hit at the given CTH
64  hitrate_t() = default;
65  explicit hitrate_t(const config& cfg);
66  config write() const;
67  };
68  /** A type that maps chance-to-hit percentage to number of hits and strikes at that CTH. */
69  typedef std::map<int, hitrate_t> hitrate_map;
73  static const int decimal_shift = 1000;
75  // Expected value for damage inflicted/taken * 1000, based on
76  // probability to hit,
77  // Use this long term to see how lucky a side is.
81  std::string save_id;
82  };
84  int sum_str_int_map(const stats::str_int_map& m);
88  {
89  scenario_context(const std::string& name);
91  };
94  {
95  attack_context(const unit& a, const unit& d, int a_cth, int d_cth);
96  ~attack_context();
98  enum hit_result { MISSES, HITS, KILLS };
100  void attack_expected_damage(double attacker_inflict, double defender_inflict);
101  void attack_result(hit_result res, int cth, int damage, int drain);
102  void defend_result(hit_result res, int cth, int damage, int drain);
104  private:
106  std::string attacker_type, defender_type;
107  std::string attacker_side, defender_side;
108  int chance_to_hit_defender, chance_to_hit_attacker;
109  std::string attacker_res, defender_res;
111  stats& attacker_stats();
112  stats& defender_stats();
113  };
115  void recruit_unit(const unit& u);
116  void recall_unit(const unit& u);
117  void un_recall_unit(const unit& u);
118  void un_recruit_unit(const unit& u);
119  int un_recall_unit_cost(const unit& u);
121  void advance_unit(const unit& u);
124  void write_stats(config_writer &out);
125  void read_stats(const config& cfg);
126  void fresh_stats();
127  /** Delete the current scenario from the stats. */
128  void clear_current_scenario();
129  /** Reset the stats of the current scenario to the beginning. */
130  void reset_current_scenario();
132  void reset_turn_stats(const std::string & save_id);
133  stats calculate_stats(const std::string & save_id);
134  /** Stats (and name) for each scenario. The pointers are never nullptr. */
135  typedef std::vector< std::pair<const std::string *, const stats *>> levels;
136  /** Returns a list of names and stats for each scenario in the current campaign. */
137  levels level_stats(const std::string & save_id);
138 } // end namespace statistics
139 std::ostream& operator<<(std::ostream& outstream, const statistics::stats::hitrate_t& by_cth);
std::ostream & operator<<(std::ostream &outstream, const statistics::stats::hitrate_t &by_cth)
Definition: statistics.cpp:840
long long damage_inflicted
Definition: statistics.hpp:57
battle_result_map defends_taken
Statistics of enemies&#39; attacks against this side on their turns.
Definition: statistics.hpp:55
This class represents a single unit of a specific type.
Definition: unit.hpp:120
int sum_str_int_map(const stats::str_int_map &m)
Definition: statistics.cpp:803
void un_recall_unit(const unit &u)
Definition: statistics.cpp:660
#define a
long long turn_damage_taken
Definition: statistics.hpp:58
std::map< int, hitrate_t > hitrate_map
A type that maps chance-to-hit percentage to number of hits and strikes at that CTH.
Definition: statistics.hpp:69
void fresh_stats()
Definition: statistics.cpp:782
int sum_cost_str_int_map(const stats::str_int_map &m)
Definition: statistics.cpp:813
#define d
void reset_current_scenario()
Reset the stats of the current scenario to the beginning.
Definition: statistics.cpp:796
void reset_turn_stats(const std::string &save_id)
Definition: statistics.cpp:686
str_int_map advanced_to
Definition: statistics.hpp:36
void recruit_unit(const unit &u)
Definition: statistics.cpp:646
std::vector< std::pair< const std::string *, const stats * > > levels
Stats (and name) for each scenario.
Definition: statistics.hpp:135
long long expected_damage_taken
Definition: statistics.hpp:79
long long turn_expected_damage_inflicted
Definition: statistics.hpp:80
battle_result_map attacks_inflicted
Statistics of this side&#39;s attacks on its own turns.
Definition: statistics.hpp:49
config write() const
Definition: statistics.cpp:365
static const int decimal_shift
Definition: statistics.hpp:73
Class for writing a config out to a file in pieces.
std::map< int, battle_sequence_frequency_map > battle_result_map
A type that will map different % chances to hit to different results.
Definition: statistics.hpp:46
int un_recall_unit_cost(const unit &u)
Definition: statistics.cpp:674
battle_result_map defends_inflicted
Statistics of this side&#39;s attacks on enemies&#39; turns.
Definition: statistics.hpp:51
levels level_stats(const std::string &save_id)
Returns a list of names and stats for each scenario in the current campaign.
Definition: statistics.cpp:725
str_int_map recalls
Definition: statistics.hpp:36
hitrate_map turn_by_cth_taken
Definition: statistics.hpp:71
void read_stats(const config &cfg)
Definition: statistics.cpp:772
void read(const config &cfg)
Definition: statistics.cpp:452
str_int_map killed
Definition: statistics.hpp:36
battle_result_map attacks_taken
Statistics of enemies&#39; counter attacks on this side&#39;s turns.
Definition: statistics.hpp:53
hitrate_map turn_by_cth_inflicted
Definition: statistics.hpp:71
void advance_unit(const unit &u)
Definition: statistics.cpp:680
str_int_map recruits
Definition: statistics.hpp:36
stats calculate_stats(const std::string &save_id)
Definition: statistics.cpp:698
str_int_map deaths
Definition: statistics.hpp:36
hitrate_map by_cth_taken
Definition: statistics.hpp:70
long long damage_taken
Definition: statistics.hpp:57
long long turn_damage_inflicted
Definition: statistics.hpp:58
void recall_unit(const unit &u)
Definition: statistics.cpp:653
long long expected_damage_inflicted
Definition: statistics.hpp:79
str_int_map battle_sequence_frequency_map
Definition: statistics.hpp:43
std::map< std::string, int > str_int_map
Definition: statistics.hpp:35
config write_stats()
Definition: statistics.cpp:749
long long turn_expected_damage_taken
Definition: statistics.hpp:80
std::string save_id
Definition: statistics.hpp:81
void un_recruit_unit(const unit &u)
Definition: statistics.cpp:667
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
void clear_current_scenario()
Delete the current scenario from the stats.
Definition: statistics.cpp:788
hitrate_map by_cth_inflicted
Definition: statistics.hpp:70