The Battle for Wesnoth  1.15.3+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 /** @file */
17 #pragma once
19 class config;
20 class config_writer;
21 class unit;
22 #include <string>
23 #include <map>
24 #include <vector>
26 namespace statistics
27 {
28  struct stats
29  {
30  stats();
31  explicit stats(const config& cfg);
33  config write() const;
34  void write(config_writer &out) const;
35  void read(const config& cfg);
37  typedef std::map<std::string,int> str_int_map;
41  /*
42  * A type that will map a string of hit/miss to the number of times
43  * that sequence has occurred.
44  */
45  typedef str_int_map battle_sequence_frequency_map;
47  /** A type that will map different % chances to hit to different results. */
48  typedef std::map<int,battle_sequence_frequency_map> battle_result_map;
50  /// Statistics of this side's attacks on its own turns.
51  battle_result_map attacks_inflicted;
52  /// Statistics of this side's attacks on enemies' turns.
53  battle_result_map defends_inflicted;
54  /// Statistics of enemies' counter attacks on this side's turns.
55  battle_result_map attacks_taken;
56  /// Statistics of enemies' attacks against this side on their turns.
57  battle_result_map defends_taken;
62  struct hitrate_t
63  {
64  int strikes; //< Number of strike attempts at the given CTH
65  int hits; //< Number of strikes that hit at the given CTH
66  hitrate_t() = default;
67  explicit hitrate_t(const config& cfg);
68  config write() const;
69  };
70  /// A type that maps chance-to-hit percentage to number of hits and strikes at that CTH.
71  typedef std::map<int, hitrate_t> hitrate_map;
75  static const int decimal_shift = 1000;
77  // Expected value for damage inflicted/taken * 1000, based on
78  // probability to hit,
79  // Use this long term to see how lucky a side is.
83  std::string save_id;
84  };
86  int sum_str_int_map(const stats::str_int_map& m);
90  {
91  scenario_context(const std::string& name);
93  };
96  {
97  attack_context(const unit& a, const unit& d, int a_cth, int d_cth);
98  ~attack_context();
100  enum hit_result { MISSES, HITS, KILLS };
102  void attack_expected_damage(double attacker_inflict, double defender_inflict);
103  void attack_result(hit_result res, int cth, int damage, int drain);
104  void defend_result(hit_result res, int cth, int damage, int drain);
106  private:
108  std::string attacker_type, defender_type;
109  std::string attacker_side, defender_side;
110  int chance_to_hit_defender, chance_to_hit_attacker;
111  std::string attacker_res, defender_res;
113  stats& attacker_stats();
114  stats& defender_stats();
115  };
117  void recruit_unit(const unit& u);
118  void recall_unit(const unit& u);
119  void un_recall_unit(const unit& u);
120  void un_recruit_unit(const unit& u);
121  int un_recall_unit_cost(const unit& u);
123  void advance_unit(const unit& u);
126  void write_stats(config_writer &out);
127  void read_stats(const config& cfg);
128  void fresh_stats();
129  /// Delete the current scenario from the stats.
130  void clear_current_scenario();
131  /// Reset the stats of the current scenario to the beginning.
132  void reset_current_scenario();
134  void reset_turn_stats(const std::string & save_id);
135  stats calculate_stats(const std::string & save_id);
136  /// Stats (and name) for each scenario. The pointers are never nullptr.
137  typedef std::vector< std::pair<const std::string *, const stats *>> levels;
138  /// Returns a list of names and stats for each scenario in the current campaign.
139  levels level_stats(const std::string & save_id);
140 } // end namespace statistics
141 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:838
long long damage_inflicted
Definition: statistics.hpp:59
battle_result_map defends_taken
Statistics of enemies&#39; attacks against this side on their turns.
Definition: statistics.hpp:57
This class represents a single unit of a specific type.
Definition: unit.hpp:129
int sum_str_int_map(const stats::str_int_map &m)
Definition: statistics.cpp:801
void un_recall_unit(const unit &u)
Definition: statistics.cpp:658
#define a
long long turn_damage_taken
Definition: statistics.hpp:60
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:71
void fresh_stats()
Definition: statistics.cpp:780
int sum_cost_str_int_map(const stats::str_int_map &m)
Definition: statistics.cpp:811
#define d
void reset_current_scenario()
Reset the stats of the current scenario to the beginning.
Definition: statistics.cpp:794
void reset_turn_stats(const std::string &save_id)
Definition: statistics.cpp:684
str_int_map advanced_to
Definition: statistics.hpp:38
void recruit_unit(const unit &u)
Definition: statistics.cpp:644
std::vector< std::pair< const std::string *, const stats * > > levels
Stats (and name) for each scenario. The pointers are never nullptr.
Definition: statistics.hpp:137
long long expected_damage_taken
Definition: statistics.hpp:81
long long turn_expected_damage_inflicted
Definition: statistics.hpp:82
battle_result_map attacks_inflicted
Statistics of this side&#39;s attacks on its own turns.
Definition: statistics.hpp:51
config write() const
Definition: statistics.cpp:363
static const int decimal_shift
Definition: statistics.hpp:75
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:48
int un_recall_unit_cost(const unit &u)
Definition: statistics.cpp:672
battle_result_map defends_inflicted
Statistics of this side&#39;s attacks on enemies&#39; turns.
Definition: statistics.hpp:53
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:723
str_int_map recalls
Definition: statistics.hpp:38
hitrate_map turn_by_cth_taken
Definition: statistics.hpp:73
void read_stats(const config &cfg)
Definition: statistics.cpp:770
void read(const config &cfg)
Definition: statistics.cpp:450
str_int_map killed
Definition: statistics.hpp:38
battle_result_map attacks_taken
Statistics of enemies&#39; counter attacks on this side&#39;s turns.
Definition: statistics.hpp:55
hitrate_map turn_by_cth_inflicted
Definition: statistics.hpp:73
void advance_unit(const unit &u)
Definition: statistics.cpp:678
str_int_map recruits
Definition: statistics.hpp:38
stats calculate_stats(const std::string &save_id)
Definition: statistics.cpp:696
str_int_map deaths
Definition: statistics.hpp:38
hitrate_map by_cth_taken
Definition: statistics.hpp:72
long long damage_taken
Definition: statistics.hpp:59
long long turn_damage_inflicted
Definition: statistics.hpp:60
void recall_unit(const unit &u)
Definition: statistics.cpp:651
long long expected_damage_inflicted
Definition: statistics.hpp:81
str_int_map battle_sequence_frequency_map
Definition: statistics.hpp:45
std::map< std::string, int > str_int_map
Definition: statistics.hpp:37
config write_stats()
Definition: statistics.cpp:747
long long turn_expected_damage_taken
Definition: statistics.hpp:82
std::string save_id
Definition: statistics.hpp:83
void un_recruit_unit(const unit &u)
Definition: statistics.cpp:665
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
void clear_current_scenario()
Delete the current scenario from the stats.
Definition: statistics.cpp:786
hitrate_map by_cth_inflicted
Definition: statistics.hpp:72