The Battle for Wesnoth  1.17.17+dev
engine_cpp.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2023
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  * CPP AI Support engine - creating specific ai components from config
18  * @file
19  */
20 
21 #include "ai/composite/ai.hpp"
22 #include "ai/composite/aspect.hpp"
23 #include "ai/composite/goal.hpp"
24 #include "ai/composite/rca.hpp"
25 #include "ai/composite/stage.hpp"
27 #include "log.hpp"
28 
29 namespace ai {
30 
31 static lg::log_domain log_ai_engine_cpp("ai/engine/cpp");
32 #define DBG_AI_ENGINE_CPP LOG_STREAM(debug, log_ai_engine_cpp)
33 #define LOG_AI_ENGINE_CPP LOG_STREAM(info, log_ai_engine_cpp)
34 #define ERR_AI_ENGINE_CPP LOG_STREAM(err, log_ai_engine_cpp)
35 
37  : engine(context,cfg)
38 {
39  name_ = "cpp";
40 }
41 
43 {
44 }
45 
46 void engine_cpp::do_parse_aspect_from_config( const config &cfg, const std::string &id, std::back_insert_iterator<std::vector< aspect_ptr >> b )
47 {
48  const std::string aspect_factory_key = id+"*"+cfg["name"];//@note: hack which combines aspect id and name to get the std::string key of the aspect factory
50  if (f == aspect_factory::get_list().end()){
51  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN aspect["<<aspect_factory_key<<"]";
52  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
53  return;
54  }
55  aspect_ptr new_aspect = f->second->get_new_instance(ai_,cfg,id);
56  if (!new_aspect) {
57  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE aspect, key=["<<aspect_factory_key<<"]";
58  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
59  return;
60  }
61  *b = new_aspect;
62 }
63 
64 void engine_cpp::do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr >> b ){
66  if (f == candidate_action_factory::get_list().end()){
67  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN candidate_action["<<cfg["name"]<<"]";
68  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
69  return;
70  }
71  candidate_action_ptr new_candidate_action = f->second->get_new_instance(context,cfg);
72  if (!new_candidate_action) {
73  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE candidate_action["<<cfg["name"]<<"]";
74  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
75  return;
76  }
77  *b = new_candidate_action;
78 
79 }
80 
81 void engine_cpp::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b )
82 {
84  if (f == stage_factory::get_list().end()){
85  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN stage["<<cfg["name"]<<"]";
86  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
87  return;
88  }
89  stage_ptr new_stage = f->second->get_new_instance(context,cfg);
90  if (!new_stage) {
91  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE stage["<<cfg["name"]<<"]";
92  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
93  return;
94  }
95  *b = new_stage;
96 }
97 
98 void engine_cpp::do_parse_goal_from_config(const config &cfg, std::back_insert_iterator<std::vector< goal_ptr >> b )
99 {
101  if (f == goal_factory::get_list().end()){
102  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN goal["<<cfg["name"]<<"]";
103  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
104  return;
105  }
106  goal_ptr new_goal = f->second->get_new_instance(ai_,cfg);
107  if (!new_goal || !new_goal->ok()) {
108  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE goal["<<cfg["name"]<<"]";
109  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
110  return;
111  }
112  *b = new_goal;
113 }
114 
115 void engine_cpp::do_parse_engine_from_config(const config &cfg, std::back_insert_iterator<std::vector< engine_ptr >> b )
116 {
118  if (f == engine_factory::get_list().end()){
119  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN engine["<<cfg["name"]<<"]";
120  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
121  return;
122  }
123  engine_ptr new_engine = f->second->get_new_instance(ai_,cfg);
124  if (!new_engine) {
125  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE engine["<<cfg["name"]<<"]";
126  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
127  return;
128  }
129  *b = new_engine;
130 }
131 
132 } //end of namespace ai
static factory_map & get_list()
Definition: aspect.hpp:455
static factory_map & get_list()
Definition: rca.hpp:156
engine_cpp(readonly_context &context, const config &cfg)
Definition: engine_cpp.cpp:36
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.cpp:64
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.cpp:81
virtual ~engine_cpp()
Definition: engine_cpp.cpp:42
virtual void do_parse_engine_from_config(const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine_cpp.cpp:115
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine_cpp.cpp:98
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.cpp:46
static factory_map & get_list()
Definition: engine.hpp:114
std::string name_
Definition: engine.hpp:102
readonly_context & ai_
Definition: engine.hpp:96
static factory_map & get_list()
Definition: goal.hpp:159
virtual side_number get_side() const =0
Get the side number.
static factory_map & get_list()
Definition: stage.hpp:97
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
Composite AI with turn sequence which is a vector of stages.
#define DBG_AI_ENGINE_CPP
Definition: engine_cpp.cpp:32
#define ERR_AI_ENGINE_CPP
Definition: engine_cpp.cpp:34
CPP AI Support engine - creating specific ai components from config.
Standard logging facilities (interface).
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:99
static lg::log_domain log_ai_engine_cpp("ai/engine/cpp")
std::shared_ptr< aspect > aspect_ptr
Definition: game_info.hpp:95
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:100
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:102
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:145
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
candidate action framework
Composite AI stages.
#define f
#define b