The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
testing.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2017 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project http://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 /**
16  * Gather statistics important for AI testing and output them
17  * @file
18  */
19 #include "ai/manager.hpp"
20 #include "ai/testing.hpp"
21 #include "log.hpp"
22 #include "game_board.hpp"
23 #include "replay.hpp"
24 #include "resources.hpp"
25 #include "team.hpp"
26 #include "units/unit.hpp"
27 #include "tod_manager.hpp"
28 
29 static lg::log_domain log_ai_testing("ai/testing");
30 #define DBG_AI_TESTING LOG_STREAM(debug, log_ai_testing)
31 #define LOG_AI_TESTING LOG_STREAM(info, log_ai_testing)
32 #define ERR_AI_TESTING LOG_STREAM(err, log_ai_testing)
33 
34 void ai_testing::log_turn_start(unsigned int side)
35 {
36  log_turn("TURN_START",side);
37 }
38 
39 void ai_testing::log_turn_end(unsigned int side)
40 {
41  log_turn("TURN_END",side);
42 }
43 
44 void ai_testing::log_turn(const char* msg, unsigned int side)
45 {
46  assert(side>=1);
47  team& current_team = resources::gameboard->get_team(side);
48 
49  int _turn_number = resources::tod_manager->turn();
50  int _units = resources::gameboard->side_units(side);
51  int _units_cost = resources::gameboard->side_units_cost(side);
52  int _gold = current_team.gold();
53  int _villages = current_team.villages().size();
54  int _income = current_team.total_income();
55 
56  DBG_AI_TESTING << msg << side << ": " << _turn_number << std::endl;
57  DBG_AI_TESTING << msg << "_UNITS " << side << ": " << _units << std::endl;
58  DBG_AI_TESTING << msg << "_UNITS_COST " << side << ": " << _units_cost << std::endl;
59  DBG_AI_TESTING << msg << "_GOLD " << side << ": " << _gold << std::endl;
60  DBG_AI_TESTING << msg << "_VILLAGES " << side << ": " << _villages << std::endl;
61  DBG_AI_TESTING << msg << "_INCOME " << side << ": " << _income << std::endl;
62 
63  config c;
64  c["side"] = int(side);
65  c["turn"] = _turn_number;
66  c["event"] = msg;
67  c["units"] = _units;
68  c["units_cost"] = _units_cost;
69  c["gold"] = _gold;
70  c["villages"] = _villages;
71  resources::recorder->add_log_data("ai_log","turn_info",c);
72 }
73 
75 {
76  LOG_AI_TESTING << "DRAW:" << std::endl;
77  resources::recorder->add_log_data("ai_log","result","draw");
78 }
79 
80 void ai_testing::log_victory(std::set<unsigned int> winners)
81 {
82  resources::recorder->add_log_data("ai_log","result","victory");
83  for(std::set<unsigned int>::const_iterator w = winners.begin(); w != winners.end(); ++w) {
84  LOG_AI_TESTING << "WINNER: "<< *w <<std::endl;
85  resources::recorder->add_log_data("ai_log","winner",std::to_string(*w));
86  }
87 }
88 
90 {
91  for (std::vector<team>::const_iterator tm = resources::gameboard->teams().begin(); tm != resources::gameboard->teams().end(); ++tm) {
92  int side = tm-resources::gameboard->teams().begin()+1;
93  LOG_AI_TESTING << "AI_IDENTIFIER " <<side << ": " << ai::manager::get_active_ai_identifier_for_side(side) << std::endl;
94  LOG_AI_TESTING << "TEAM " << side << ": " << tm->side() << std::endl;
95  resources::recorder->add_log_data("ai_log","ai_id"+std::to_string(side),ai::manager::get_active_ai_identifier_for_side(side));
96  ///@todo 1.9: add information about ai_config
97  }
98  LOG_AI_TESTING << "VERSION: " << game_config::revision << std::endl;
100 }
101 
103 {
104  LOG_AI_TESTING << "GAME_END_TURN: "<< resources::tod_manager->turn() <<std::endl;
105  resources::recorder->add_log_data("ai_log","end_turn",
106  std::to_string(resources::tod_manager->turn()));
107  for (std::vector<team>::const_iterator tm = resources::gameboard->teams().begin(); tm != resources::gameboard->teams().end(); ++tm) {
108  int side = tm-resources::gameboard->teams().begin()+1;
109  resources::recorder->add_log_data("ai_log","end_gold"+std::to_string(side),std::to_string(tm->gold()));
110  resources::recorder->add_log_data("ai_log","end_units"+std::to_string(side),std::to_string(resources::gameboard->side_units(side)));
111  }
112 }
int side_units(int side_num) const
Returns the number of units of the side side_num.
static void log_turn_start(unsigned int side)
Definition: testing.cpp:34
static void log_turn_end(unsigned int side)
Definition: testing.cpp:39
::tod_manager * tod_manager
Definition: resources.cpp:30
static void log_turn(const char *msg, unsigned int side)
Definition: testing.cpp:44
void add_log_data(const std::string &key, const std::string &var)
Definition: replay.cpp:301
static void log_victory(std::set< unsigned int > teams)
Definition: testing.cpp:80
Gather statistics important for AI testing and output them.
virtual const std::vector< team > & teams() const
Definition: game_board.hpp:92
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:102
static void log_draw()
Definition: testing.cpp:74
-file sdl_utils.hpp
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:44
team & get_team(int i)
Definition: game_board.hpp:94
#define DBG_AI_TESTING
Definition: testing.cpp:30
game_board * gameboard
Definition: resources.cpp:20
Managing the AIs lifecycle - headers.
replay * recorder
Definition: resources.cpp:29
const std::set< map_location > & villages() const
Definition: team.hpp:183
static void log_game_start()
Definition: testing.cpp:89
#define LOG_AI_TESTING
Definition: testing.cpp:31
const std::string revision
Definition: game_config.cpp:50
static 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:718
int turn() const
int w
static lg::log_domain log_ai_testing("ai/testing")
int side_units_cost(int side_num) const
Returns the total cost of units of side side_num.
int gold() const
Definition: team.hpp:188
Standard logging facilities (interface).
int total_income() const
Definition: team.hpp:195
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
mock_char c