The Battle for Wesnoth  1.17.0-dev
engine.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  * AI Support engine - creating specific ai components from config
18  * @file
19  */
20 
21 #include "ai/composite/engine.hpp"
23 
24 #include "log.hpp"
25 
26 namespace ai {
27 
28 static lg::log_domain log_ai_engine("ai/engine");
29 #define DBG_AI_ENGINE LOG_STREAM(debug, log_ai_engine)
30 #define LOG_AI_ENGINE LOG_STREAM(info, log_ai_engine)
31 #define ERR_AI_ENGINE LOG_STREAM(err, log_ai_engine)
32 
33 engine::engine( readonly_context &context, const config &cfg )
34  : ai_(context)
35  , ai_context_(nullptr)
36  , engine_(cfg["engine"])
37  , id_(cfg["id"])
38  , name_(cfg["name"])
39 {
40  LOG_AI_ENGINE << "side "<< ai_.get_side() << " : "<<" created engine with name=["<<name_<<"]"<<std::endl;
41 }
42 
44 {
45 }
46 
47 bool engine::is_ok() const
48 {
49  return true;
50 }
51 
52 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 )
53 {
54  engine_ptr eng = context.get_engine_by_cfg(cfg);
55  if (eng){
56  //do not override that method in subclasses which cannot create aspects
57  eng->do_parse_aspect_from_config(cfg, id, b);
58  }
59 }
60 
61 void engine::parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr >> b )
62 {
63  engine_ptr eng = context.get_engine_by_cfg(cfg);
64  if (eng){
65  //do not override that method in subclasses which cannot create candidate actions
66  eng->do_parse_candidate_action_from_config(context, cfg, b);
67  }
68 }
69 
70 void engine::parse_engine_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< engine_ptr >> b )
71 {
72  engine_ptr eng = context.get_engine_by_cfg(cfg);
73  if (eng){
74  //do not override that method in subclasses which cannot create engines
75  eng->do_parse_engine_from_config(cfg, b);
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 void engine::parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b )
89 {
90  engine_ptr eng = context.get_engine_by_cfg(cfg);
91  if (eng){
92  //do not override that method in subclasses which cannot create stages
93  eng->do_parse_stage_from_config(context, cfg, b);
94  }
95 }
96 
97 void engine::do_parse_aspect_from_config( const config &/*cfg*/, const std::string &/*id*/, std::back_insert_iterator< std::vector<aspect_ptr>> /*b*/ )
98 {
99 
100 }
101 
102 void engine::do_parse_candidate_action_from_config( rca_context &/*context*/, const config &/*cfg*/, std::back_insert_iterator<std::vector< candidate_action_ptr >> /*b*/ ){
103 
104 }
105 
106 void engine::do_parse_engine_from_config( const config &/*cfg*/, std::back_insert_iterator<std::vector< engine_ptr >> /*b*/ ){
107 
108 }
109 
110 void engine::do_parse_goal_from_config( const config &/*cfg*/, std::back_insert_iterator<std::vector< goal_ptr >> /*b*/ ){
111 
112 }
113 
114 void engine::do_parse_stage_from_config( ai_context &/*context*/, const config &/*cfg*/, std::back_insert_iterator<std::vector< stage_ptr >> /*b*/ )
115 {
116 
117 }
118 
119 std::string engine::evaluate(const std::string& /*str*/)
120 {
121  return "evaluate command is not implemented by this engine";
122 }
123 
125 {
126  ai_context_ = ai_context;
127 }
128 
130 {
131  return ai_context_;
132 }
133 
135 {
136  config cfg;
137  cfg["engine"] = engine_;
138  cfg["name"] = get_name();
139  return cfg;
140 }
141 
143 {
144  return ai_;
145 }
146 
147 // This is defined in the source file so that it can easily access the logger
148 bool engine_factory::is_duplicate(const std::string& name)
149 {
150  if (get_list().find(name) != get_list().end()) {
151  ERR_AI_ENGINE << "Error: Attempt to double-register engine " << name << std::endl;
152  return true;
153  }
154  return false;
155 }
156 
157 } //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:52
virtual std::string evaluate(const std::string &str)
Definition: engine.cpp:119
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:124
virtual config to_config() const
serialize
Definition: engine.cpp:134
engine(readonly_context &context, const config &cfg)
Definition: engine.cpp:33
#define LOG_AI_ENGINE
Definition: engine.cpp:30
static void parse_engine_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine.cpp:70
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:79
formula_ai & ai_
#define b
virtual bool is_ok() const
Definition: engine.cpp:47
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
virtual void do_parse_engine_from_config(const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine.cpp:106
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine.cpp:110
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:148
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:129
readonly_context & get_readonly_context()
Definition: engine.cpp:142
static void parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr >> b)
Definition: engine.cpp:88
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:61
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:102
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:99
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
#define ERR_AI_ENGINE
Definition: engine.cpp:31
virtual ~engine()
Definition: engine.cpp:43
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:97
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:114