The Battle for Wesnoth  1.17.0-dev
engine.cpp
Go to the documentation of this file.
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  * 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 void engine::parse_goal_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< goal_ptr >> b )
79 {
80  engine_ptr eng = context.get_engine_by_cfg(cfg);
81  if (eng){
82  //do not override that method in subclasses which cannot create goals
83  eng->do_parse_goal_from_config(cfg, b);
84  }
85 }
86 
87 void engine::parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b )
88 {
89  engine_ptr eng = context.get_engine_by_cfg(cfg);
90  if (eng){
91  //do not override that method in subclasses which cannot create stages
92  eng->do_parse_stage_from_config(context, cfg, b);
93  }
94 }
95 
96 void engine::do_parse_aspect_from_config( const config &/*cfg*/, const std::string &/*id*/, std::back_insert_iterator< std::vector<aspect_ptr>> /*b*/ )
97 {
98 
99 }
100 
101 void engine::do_parse_candidate_action_from_config( rca_context &/*context*/, const config &/*cfg*/, std::back_insert_iterator<std::vector< candidate_action_ptr >> /*b*/ ){
102 
103 }
104 
105 void engine::do_parse_engine_from_config( const config &/*cfg*/, std::back_insert_iterator<std::vector< engine_ptr >> /*b*/ ){
106 
107 }
108 
109 void engine::do_parse_goal_from_config( const config &/*cfg*/, std::back_insert_iterator<std::vector< goal_ptr >> /*b*/ ){
110 
111 }
112 
113 void engine::do_parse_stage_from_config( ai_context &/*context*/, const config &/*cfg*/, std::back_insert_iterator<std::vector< stage_ptr >> /*b*/ )
114 {
115 
116 }
117 
118 std::string engine::evaluate(const std::string& /*str*/)
119 {
120  return "evaluate command is not implemented by this engine";
121 }
122 
124 {
125  ai_context_ = ai_context;
126 }
127 
129 {
130  return ai_context_;
131 }
132 
134 {
135  config cfg;
136  cfg["engine"] = engine_;
137  cfg["name"] = get_name();
138  return cfg;
139 }
140 
142 {
143  return ai_;
144 }
145 
146 // This is defined in the source file so that it can easily access the logger
147 bool engine_factory::is_duplicate(const std::string& name)
148 {
149  if (get_list().find(name) != get_list().end()) {
150  ERR_AI_ENGINE << "Error: Attempt to double-register engine " << name << std::endl;
151  return true;
152  }
153  return false;
154 }
155 
156 } //end of namespace ai
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 std::string evaluate(const std::string &str)
Definition: engine.cpp:118
virtual std::string get_name() const
Definition: engine.hpp:92
virtual void set_ai_context(ai_context_ptr context)
set ai context (which is not available during early initialization)
Definition: engine.cpp:123
virtual config to_config() const
serialize
Definition: engine.cpp:133
engine(readonly_context &context, const config &cfg)
Definition: engine.cpp:32
#define LOG_AI_ENGINE
Definition: engine.cpp:29
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
AI Support engine - creating specific ai components from config.
static void parse_goal_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine.cpp:78
formula_ai & ai_
#define b
virtual bool is_ok() const
Definition: engine.cpp:46
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:60
virtual void do_parse_engine_from_config(const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine.cpp:105
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine.cpp:109
std::string engine_
name of the engine which has created this engine
Definition: engine.hpp:100
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
bool is_duplicate(const std::string &name)
Definition: engine.cpp:147
static lg::log_domain log_ai_engine("ai/engine")
ai_context_ptr ai_context_
Definition: engine.hpp:97
virtual ai_context_ptr get_ai_context()
Definition: engine.cpp:128
readonly_context & get_readonly_context()
Definition: engine.cpp:141
static void parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr >> b)
Definition: engine.cpp:87
Composite AI contexts.
std::string name_
Definition: engine.hpp:102
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
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 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:101
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:98
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
#define ERR_AI_ENGINE
Definition: engine.cpp:30
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:96
readonly_context & ai_
Definition: engine.hpp:96
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:113