The Battle for Wesnoth  1.17.0-dev
testing.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2021
3  by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
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,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * Gather statistics important for AI testing and output them
18  * @file
19  */
20 #include "ai/manager.hpp"
21 #include "ai/testing.hpp"
22 #include "log.hpp"
23 #include "game_board.hpp"
24 #include "replay.hpp"
25 #include "resources.hpp"
26 #include "team.hpp"
27 #include "units/unit.hpp"
28 #include "tod_manager.hpp"
29 #include "game_version.hpp"
30 
31 static lg::log_domain log_ai_testing("ai/testing");
32 #define DBG_AI_TESTING LOG_STREAM(debug, log_ai_testing)
33 #define LOG_AI_TESTING LOG_STREAM(info, log_ai_testing)
34 #define ERR_AI_TESTING LOG_STREAM(err, log_ai_testing)
35 
36 void ai_testing::log_turn_start(unsigned int side)
37 {
38  log_turn("TURN_START",side);
39 }
40 
41 void ai_testing::log_turn_end(unsigned int side)
42 {
43  log_turn("TURN_END",side);
44 }
45 
46 void ai_testing::log_turn(const char* msg, unsigned int side)
47 {
48  assert(side>=1);
49  team& current_team = resources::gameboard->get_team(side);
50 
51  int _turn_number = resources::tod_manager->turn();
52  int _units = resources::gameboard->side_units(side);
53  int _units_cost = resources::gameboard->side_units_cost(side);
54  int _gold = current_team.gold();
55  int _villages = current_team.villages().size();
56  int _income = current_team.total_income();
57 
58  DBG_AI_TESTING << msg << side << ": " << _turn_number << std::endl;
59  DBG_AI_TESTING << msg << "_UNITS " << side << ": " << _units << std::endl;
60  DBG_AI_TESTING << msg << "_UNITS_COST " << side << ": " << _units_cost << std::endl;
61  DBG_AI_TESTING << msg << "_GOLD " << side << ": " << _gold << std::endl;
62  DBG_AI_TESTING << msg << "_VILLAGES " << side << ": " << _villages << std::endl;
63  DBG_AI_TESTING << msg << "_INCOME " << side << ": " << _income << std::endl;
64 
65  config c;
66  c["side"] = static_cast<int>(side);
67  c["turn"] = _turn_number;
68  c["event"] = msg;
69  c["units"] = _units;
70  c["units_cost"] = _units_cost;
71  c["gold"] = _gold;
72  c["villages"] = _villages;
73  resources::recorder->add_log_data("ai_log","turn_info",c);
74 }
75 
77 {
78  LOG_AI_TESTING << "DRAW:" << std::endl;
79  resources::recorder->add_log_data("ai_log","result","draw");
80 }
81 
82 void ai_testing::log_victory(std::set<unsigned int> winners)
83 {
84  resources::recorder->add_log_data("ai_log","result","victory");
85  for(std::set<unsigned int>::const_iterator w = winners.begin(); w != winners.end(); ++w) {
86  LOG_AI_TESTING << "WINNER: "<< *w <<std::endl;
87  resources::recorder->add_log_data("ai_log","winner",std::to_string(*w));
88  }
89 }
90 
92 {
93  for (std::vector<team>::const_iterator tm = resources::gameboard->teams().begin(); tm != resources::gameboard->teams().end(); ++tm) {
94  int side = tm-resources::gameboard->teams().begin()+1;
95  LOG_AI_TESTING << "AI_IDENTIFIER " << side << ": " << ai::manager::get_singleton().get_active_ai_identifier_for_side(side) << std::endl;
96  LOG_AI_TESTING << "TEAM " << side << ": " << tm->side() << std::endl;
97  resources::recorder->add_log_data("ai_log", "ai_id" + std::to_string(side), ai::manager::get_singleton().get_active_ai_identifier_for_side(side));
98  }
99  LOG_AI_TESTING << "VERSION: " << game_config::revision << std::endl;
101 }
102 
104 {
105  LOG_AI_TESTING << "GAME_END_TURN: "<< resources::tod_manager->turn() <<std::endl;
106  resources::recorder->add_log_data("ai_log","end_turn",
107  std::to_string(resources::tod_manager->turn()));
108  for (std::vector<team>::const_iterator tm = resources::gameboard->teams().begin(); tm != resources::gameboard->teams().end(); ++tm) {
109  int side = tm-resources::gameboard->teams().begin()+1;
110  resources::recorder->add_log_data("ai_log","end_gold"+std::to_string(side),std::to_string(tm->gold()));
111  resources::recorder->add_log_data("ai_log","end_units"+std::to_string(side),std::to_string(resources::gameboard->side_units(side)));
112  }
113 }
static void log_turn_start(unsigned int side)
Definition: testing.cpp:36
static void log_turn_end(unsigned int side)
Definition: testing.cpp:41
::tod_manager * tod_manager
Definition: resources.cpp:30
static void log_turn(const char *msg, unsigned int side)
Definition: testing.cpp:46
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:85
Interfaces for manipulating version numbers of engine, add-ons, etc.
static manager & get_singleton()
Definition: manager.hpp:145
void add_log_data(const std::string &key, const std::string &var)
Definition: replay.cpp:311
static void log_victory(std::set< unsigned int > teams)
Definition: testing.cpp:82
Gather statistics important for AI testing and output them.
Replay control code.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
static void log_game_end()
Definition: testing.cpp:103
static void log_draw()
Definition: testing.cpp:76
int gold() const
Definition: team.hpp:201
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:72
team & get_team(int i)
Definition: game_board.hpp:97
#define DBG_AI_TESTING
Definition: testing.cpp:32
game_board * gameboard
Definition: resources.cpp:21
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands...
replay * recorder
Definition: resources.cpp:29
static void log_game_start()
Definition: testing.cpp:91
int side_units_cost(int side_num) const
Returns the total cost of units of side side_num.
#define LOG_AI_TESTING
Definition: testing.cpp:33
const std::string revision
std::string get_active_ai_identifier_for_side(side_number side)
Gets AI algorithm identifier for active AI of the given side.
Definition: manager.cpp:685
int w
static lg::log_domain log_ai_testing("ai/testing")
Standard logging facilities (interface).
int turn() const
int side_units(int side_num) const
Returns the number of units of the side side_num.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
mock_char c
int total_income() const
Definition: team.hpp:208
const std::set< map_location > & villages() const
Definition: team.hpp:196