1 /*
2    Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.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 /**
16  * Stage of a composite AI
17  * @file
18  */
19 
20 #include "ai/composite/ai.hpp"
21 #include "ai/composite/engine.hpp"
22 #include "ai/composite/stage.hpp"
23 #include "ai/contexts.hpp"
24 #include "log.hpp"
25 #include "resources.hpp"
26 #include "tod_manager.hpp"
27 #include <map>
28 #include <string>
29 
30 namespace ai {
31 
32 static lg::log_domain log_ai_stage("ai/stage");
33 #define DBG_AI_STAGE LOG_STREAM(debug, log_ai_stage)
34 #define LOG_AI_STAGE LOG_STREAM(info, log_ai_stage)
35 #define ERR_AI_STAGE LOG_STREAM(err, log_ai_stage)
36 
37 // =======================================================================
38 // COMPOSITE AI STAGE
39 // =======================================================================
40 
stage(ai_context & context,const config & cfg)41 stage::stage( ai_context &context, const config &cfg )
42 	: recursion_counter_(context.get_recursion_count()), cfg_(cfg)
43 {
44 	init_ai_context_proxy(context);
45 }
46 
on_create()47 void stage::on_create()
48 {
49 	LOG_AI_STAGE << "side "<< get_side() << " : "<<" created stage with name=["<<cfg_["name"]<<"]"<<std::endl;
50 }
51 
~stage()52 stage::~stage()
53 {
54 }
55 
play_stage()56 bool stage::play_stage()
57 {
58 	return do_play_stage();
59 }
60 
get_recursion_count() const61 int stage::get_recursion_count() const
62 {
63 	return recursion_counter_.get_count();
64 }
65 
to_config() const66 config stage::to_config() const
67 {
68 	config cfg;
69 	cfg["engine"] = cfg_["engine"];
70 	cfg["name"] = cfg_["name"];
71 	cfg["id"] = cfg_["id"];
72 	return cfg;
73 }
74 
get_id() const75 std::string stage::get_id() const
76 {
77 	return cfg_["id"];
78 }
79 
get_engine() const80 std::string stage::get_engine() const
81 {
82 	return cfg_["engine"];
83 }
84 
get_name() const85 std::string stage::get_name() const
86 {
87 	return cfg_["name"];
88 }
89 
90 
91 
92 
93 // =======================================================================
94 // COMPOSITE AI IDLE STAGE
95 // =======================================================================
96 
97 
idle_stage(ai_context & context,const config & cfg)98 idle_stage::idle_stage( ai_context &context, const config &cfg )
99 	: stage(context,cfg)
100 {
101 }
102 
~idle_stage()103 idle_stage::~idle_stage()
104 {
105 }
106 
do_play_stage()107 bool idle_stage::do_play_stage(){
108 	LOG_AI_STAGE << "Turn " << resources::tod_manager->turn() << ": playing idle stage for side: "<< get_side() << std::endl;
109 	return false;
110 }
111 
112 
113 // This is defined in the source file so that it can easily access the logger
is_duplicate(const std::string & name)114 bool stage_factory::is_duplicate(const std::string& name)
115 {
116 	if (get_list().find(name) != get_list().end()) {
117 		ERR_AI_STAGE << "Error: Attempt to double-register stage " << name << std::endl;
118 		return true;
119 	}
120 	return false;
121 }
122 
123 
124 } //end of namespace ai
125