The Battle for Wesnoth  1.17.12+dev
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
3  by David White <>
4  Part of the Battle for Wesnoth Project
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,
13  See the COPYING file for more details.
14 */
16 #pragma once
18 class config;
19 class config_writer;
20 class unit;
21 #include <string>
22 #include <map>
23 #include <vector>
25 namespace statistics
26 {
27  struct stats
28  {
29  stats();
30  explicit stats(const config& cfg);
32  config write() const;
33  void write(config_writer &out) const;
34  void read(const config& cfg);
36  typedef std::map<std::string,int> str_int_map;
40  /*
41  * A type that will map a string of hit/miss to the number of times
42  * that sequence has occurred.
43  */
44  typedef str_int_map battle_sequence_frequency_map;
46  /** A type that will map different % chances to hit to different results. */
47  typedef std::map<int,battle_sequence_frequency_map> battle_result_map;
49  /** Statistics of this side's attacks on its own turns. */
50  battle_result_map attacks_inflicted;
51  /** Statistics of this side's attacks on enemies' turns. */
52  battle_result_map defends_inflicted;
53  /** Statistics of enemies' counter attacks on this side's turns. */
54  battle_result_map attacks_taken;
55  /** Statistics of enemies' attacks against this side on their turns. */
56  battle_result_map defends_taken;
61  struct hitrate_t
62  {
63  int strikes; //< Number of strike attempts at the given CTH
64  int hits; //< Number of strikes that hit at the given CTH
65  hitrate_t() = default;
66  explicit hitrate_t(const config& cfg);
67  config write() const;
68  };
69  /** A type that maps chance-to-hit percentage to number of hits and strikes at that CTH. */
70  typedef std::map<int, hitrate_t> hitrate_map;
74  static const int decimal_shift = 1000;
76  // Expected value for damage inflicted/taken * 1000, based on
77  // probability to hit,
78  // Use this long term to see how lucky a side is.
82  std::string save_id;
83  };
85  int sum_str_int_map(const std::map<std::string,int>& m);
86  int sum_cost_str_int_map(const std::map<std::string,int>& m);
89  {
90  scenario_context(const std::string& name);
92  };
95  {
96  attack_context(const unit& a, const unit& d, int a_cth, int d_cth);
97  ~attack_context();
99  enum hit_result { MISSES, HITS, KILLS };
101  void attack_expected_damage(double attacker_inflict, double defender_inflict);
102  void attack_result(hit_result res, int cth, int damage, int drain);
103  void defend_result(hit_result res, int cth, int damage, int drain);
105  private:
107  std::string attacker_type, defender_type;
108  std::string attacker_side, defender_side;
109  int chance_to_hit_defender, chance_to_hit_attacker;
110  std::string attacker_res, defender_res;
112  stats& attacker_stats();
113  stats& defender_stats();
114  };
116  void recruit_unit(const unit& u);
117  void recall_unit(const unit& u);
118  void un_recall_unit(const unit& u);
119  void un_recruit_unit(const unit& u);
120  int un_recall_unit_cost(const unit& u);
122  void advance_unit(const unit& u);
125  void write_stats(config_writer &out);
126  void read_stats(const config& cfg);
127  void fresh_stats();
128  /** Delete the current scenario from the stats. */
129  void clear_current_scenario();
130  /** Reset the stats of the current scenario to the beginning. */
131  void reset_current_scenario();
133  void reset_turn_stats(const std::string & save_id);
134  stats calculate_stats(const std::string & save_id);
135  /** Stats (and name) for each scenario. The pointers are never nullptr. */
136  typedef std::vector< std::pair<const std::string *, const stats *>> levels;
137  /** Returns a list of names and stats for each scenario in the current campaign. */
138  levels level_stats(const std::string & save_id);
139 } // end namespace statistics
140 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:841
long long damage_inflicted
Definition: statistics.hpp:58
battle_result_map defends_taken
Statistics of enemies&#39; attacks against this side on their turns.
Definition: statistics.hpp:56
This class represents a single unit of a specific type.
Definition: unit.hpp:133
void un_recall_unit(const unit &u)
Definition: statistics.cpp:661
#define a
long long turn_damage_taken
Definition: statistics.hpp:59
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:70
int sum_str_int_map(const std::map< std::string, int > &m)
Definition: statistics.cpp:804
void fresh_stats()
Definition: statistics.cpp:783
#define d
void reset_current_scenario()
Reset the stats of the current scenario to the beginning.
Definition: statistics.cpp:797
void reset_turn_stats(const std::string &save_id)
Definition: statistics.cpp:687
str_int_map advanced_to
Definition: statistics.hpp:37
void recruit_unit(const unit &u)
Definition: statistics.cpp:647
std::vector< std::pair< const std::string *, const stats * > > levels
Stats (and name) for each scenario.
Definition: statistics.hpp:136
long long expected_damage_taken
Definition: statistics.hpp:80
long long turn_expected_damage_inflicted
Definition: statistics.hpp:81
battle_result_map attacks_inflicted
Statistics of this side&#39;s attacks on its own turns.
Definition: statistics.hpp:50
config write() const
Definition: statistics.cpp:366
static const int decimal_shift
Definition: statistics.hpp:74
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:47
int un_recall_unit_cost(const unit &u)
Definition: statistics.cpp:675
battle_result_map defends_inflicted
Statistics of this side&#39;s attacks on enemies&#39; turns.
Definition: statistics.hpp:52
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:726
str_int_map recalls
Definition: statistics.hpp:37
hitrate_map turn_by_cth_taken
Definition: statistics.hpp:72
void read_stats(const config &cfg)
Definition: statistics.cpp:773
void read(const config &cfg)
Definition: statistics.cpp:453
str_int_map killed
Definition: statistics.hpp:37
battle_result_map attacks_taken
Statistics of enemies&#39; counter attacks on this side&#39;s turns.
Definition: statistics.hpp:54
hitrate_map turn_by_cth_inflicted
Definition: statistics.hpp:72
int sum_cost_str_int_map(const std::map< std::string, int > &m)
Definition: statistics.cpp:814
void advance_unit(const unit &u)
Definition: statistics.cpp:681
str_int_map recruits
Definition: statistics.hpp:37
stats calculate_stats(const std::string &save_id)
Definition: statistics.cpp:699
str_int_map deaths
Definition: statistics.hpp:37
hitrate_map by_cth_taken
Definition: statistics.hpp:71
long long damage_taken
Definition: statistics.hpp:58
long long turn_damage_inflicted
Definition: statistics.hpp:59
void recall_unit(const unit &u)
Definition: statistics.cpp:654
long long expected_damage_inflicted
Definition: statistics.hpp:80
str_int_map battle_sequence_frequency_map
Definition: statistics.hpp:44
std::map< std::string, int > str_int_map
Definition: statistics.hpp:36
config write_stats()
Definition: statistics.cpp:750
long long turn_expected_damage_taken
Definition: statistics.hpp:81
std::string save_id
Definition: statistics.hpp:82
void un_recruit_unit(const unit &u)
Definition: statistics.cpp:668
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
void clear_current_scenario()
Delete the current scenario from the stats.
Definition: statistics.cpp:789
hitrate_map by_cth_inflicted
Definition: statistics.hpp:71