The Battle for Wesnoth  1.15.1+dev
statistics.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 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 class config;
20 class config_writer;
21 class unit;
22 #include <string>
23 #include <map>
24 #include <vector>
25 
26 namespace statistics
27 {
28  struct stats
29  {
30  stats();
31  explicit stats(const config& cfg);
32 
33  config write() const;
34  void write(config_writer &out) const;
35  void read(const config& cfg);
36 
37  typedef std::map<std::string,int> str_int_map;
40 
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;
46 
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;
49 
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;
58 
61 
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;
74 
75  static const int decimal_shift = 1000;
76 
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.
80 
83  std::string save_id;
84  };
85 
86  int sum_str_int_map(const stats::str_int_map& m);
88 
90  {
91  scenario_context(const std::string& name);
93  };
94 
96  {
97  attack_context(const unit& a, const unit& d, int a_cth, int d_cth);
98  ~attack_context();
99 
100  enum hit_result { MISSES, HITS, KILLS };
101 
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);
105 
106  private:
107 
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;
112 
113  stats& attacker_stats();
114  stats& defender_stats();
115  };
116 
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);
122 
123  void advance_unit(const unit& u);
124 
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();
133 
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:837
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:99
int sum_str_int_map(const stats::str_int_map &m)
Definition: statistics.cpp:800
void un_recall_unit(const unit &u)
Definition: statistics.cpp:657
#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:779
int sum_cost_str_int_map(const stats::str_int_map &m)
Definition: statistics.cpp:810
#define d
void reset_current_scenario()
Reset the stats of the current scenario to the beginning.
Definition: statistics.cpp:793
void reset_turn_stats(const std::string &save_id)
Definition: statistics.cpp:683
str_int_map advanced_to
Definition: statistics.hpp:38
void recruit_unit(const unit &u)
Definition: statistics.cpp:643
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
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
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:362
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:671
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:722
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:769
void read(const config &cfg)
Definition: statistics.cpp:449
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:677
str_int_map recruits
Definition: statistics.hpp:38
stats calculate_stats(const std::string &save_id)
Definition: statistics.cpp:695
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:650
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:746
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:664
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
void clear_current_scenario()
Delete the current scenario from the stats.
Definition: statistics.cpp:785
hitrate_map by_cth_inflicted
Definition: statistics.hpp:72