The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
engine.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  * AI Support engine - creating specific ai components from config
17  * @file
18  */
19 
20 #include "ai/composite/engine.hpp"
22 
23 #include "log.hpp"
24 
25 namespace ai {
26 
27 static lg::log_domain log_ai_engine("ai/engine");
28 #define DBG_AI_ENGINE LOG_STREAM(debug, log_ai_engine)
29 #define LOG_AI_ENGINE LOG_STREAM(info, log_ai_engine)
30 #define ERR_AI_ENGINE LOG_STREAM(err, log_ai_engine)
31 
32 engine::engine( readonly_context &context, const config &cfg )
33  : ai_(context)
34  , ai_context_(nullptr)
35  , engine_(cfg["engine"])
36  , id_(cfg["id"])
37  , name_(cfg["name"])
38 {
39  LOG_AI_ENGINE << "side "<< ai_.get_side() << " : "<<" created engine with name=["<<name_<<"]"<<std::endl;
40 }
41 
43 {
44 }
45 
46 bool engine::is_ok() const
47 {
48  return true;
49 }
50 
51 void engine::parse_aspect_from_config( readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr > > b )
52 {
53  engine_ptr eng = context.get_engine_by_cfg(cfg);
54  if (eng){
55  //do not override that method in subclasses which cannot create aspects
56  eng->do_parse_aspect_from_config(cfg, id, b);
57  }
58 }
59 
60 void engine::parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b )
61 {
62  engine_ptr eng = context.get_engine_by_cfg(cfg);
63  if (eng){
64  //do not override that method in subclasses which cannot create candidate actions
65  eng->do_parse_candidate_action_from_config(context, cfg, b);
66  }
67 }
68 
69 void engine::parse_engine_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< engine_ptr > > b )
70 {
71  engine_ptr eng = context.get_engine_by_cfg(cfg);
72  if (eng){
73  //do not override that method in subclasses which cannot create engines
74  eng->do_parse_engine_from_config(cfg, b);
75  }
76 }
77 
78 
79 void engine::parse_goal_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< goal_ptr > > b )
80 {
81  engine_ptr eng = context.get_engine_by_cfg(cfg);
82  if (eng){
83  //do not override that method in subclasses which cannot create goals
84  eng->do_parse_goal_from_config(cfg, b);
85  }
86 }
87 
88 
89 void engine::parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b )
90 {
91  engine_ptr eng = context.get_engine_by_cfg(cfg);
92  if (eng){
93  //do not override that method in subclasses which cannot create stages
94  eng->do_parse_stage_from_config(context, cfg, b);
95  }
96 }
97 
98 void engine::do_parse_aspect_from_config( const config &/*cfg*/, const std::string &/*id*/, std::back_insert_iterator< std::vector<aspect_ptr> > /*b*/ )
99 {
100 
101 }
102 
103 
104 void engine::do_parse_candidate_action_from_config( rca_context &/*context*/, const config &/*cfg*/, std::back_insert_iterator<std::vector< candidate_action_ptr > > /*b*/ ){
105 
106 }
107 
108 void engine::do_parse_engine_from_config( const config &/*cfg*/, std::back_insert_iterator<std::vector< engine_ptr > > /*b*/ ){
109 
110 }
111 
112 
113 void engine::do_parse_goal_from_config( const config &/*cfg*/, std::back_insert_iterator<std::vector< goal_ptr > > /*b*/ ){
114 
115 }
116 
117 
118 void engine::do_parse_stage_from_config( ai_context &/*context*/, const config &/*cfg*/, std::back_insert_iterator<std::vector< stage_ptr > > /*b*/ )
119 {
120 
121 }
122 
124 {
125  return "evaluate command is not implemented by this engine";
126 }
127 
129 {
130  ai_context_ = ai_context;
131 }
132 
134 {
135  return ai_context_;
136 }
137 
139 {
140  config cfg;
141  cfg["engine"] = engine_;
142  cfg["name"] = get_name();
143  return cfg;
144 }
145 
147 {
148  return ai_;
149 }
150 
151 // This is defined in the source file so that it can easily access the logger
153 {
154  if (get_list().find(name) != get_list().end()) {
155  ERR_AI_ENGINE << "Error: Attempt to double-register engine " << name << std::endl;
156  return true;
157  }
158  return false;
159 }
160 
161 
162 } //end of namespace ai
std::vector< char_t > string
virtual std::string evaluate(const std::string &str)
Definition: engine.cpp:123
virtual std::string get_name() const
Definition: engine.hpp:101
virtual void set_ai_context(ai_context_ptr context)
set ai context (which is not available during early initialization)
Definition: engine.cpp:128
static void parse_engine_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< engine_ptr > > b)
Definition: engine.cpp:69
engine(readonly_context &context, const config &cfg)
Definition: engine.cpp:32
#define LOG_AI_ENGINE
Definition: engine.cpp:29
AI Support engine - creating specific ai components from config.
virtual void do_parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr > > b)
Definition: engine.cpp:118
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:107
const formula_ai & ai_
#define b
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
virtual config to_config() const
serialize
Definition: engine.cpp:138
static factory_map & get_list()
Definition: engine.hpp:124
std::string engine_
name of the engine which has created this engine
Definition: engine.hpp:109
virtual void do_parse_engine_from_config(const config &cfg, std::back_insert_iterator< std::vector< engine_ptr > > b)
Definition: engine.cpp:108
static void parse_candidate_action_from_config(rca_context &context, const config &cfg, std::back_insert_iterator< std::vector< candidate_action_ptr > > b)
Definition: engine.cpp:60
virtual engine_ptr get_engine_by_cfg(const config &cfg)=0
get engine by cfg, creating it if it is not created yet but known
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr > > b)
Definition: engine.cpp:113
bool is_duplicate(const std::string &name)
Definition: engine.cpp:152
static lg::log_domain log_ai_engine("ai/engine")
static void parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr > > b)
Definition: engine.cpp:89
static void parse_aspect_from_config(readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr > > b)
Definition: engine.cpp:51
virtual bool is_ok() const
Definition: engine.cpp:46
virtual void do_parse_candidate_action_from_config(rca_context &context, const config &cfg, std::back_insert_iterator< std::vector< candidate_action_ptr > > b)
Definition: engine.cpp:104
ai_context_ptr ai_context_
Definition: engine.hpp:106
virtual ai_context_ptr get_ai_context()
Definition: engine.cpp:133
bool find(E event, F functor)
Tests whether an event handler is available.
readonly_context & get_readonly_context()
Definition: engine.cpp:146
Composite AI contexts.
std::string name_
Definition: engine.hpp:111
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
#define ERR_AI_ENGINE
Definition: engine.cpp:30
static void parse_goal_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< goal_ptr > > b)
Definition: engine.cpp:79
virtual ~engine()
Definition: engine.cpp:42
virtual void do_parse_aspect_from_config(const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr > > b)
Definition: engine.cpp:98
readonly_context & ai_
Definition: engine.hpp:105