The Battle for Wesnoth  1.19.5+dev
engine_cpp.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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/aspect.hpp"
22 #include "ai/composite/goal.hpp"
23 #include "ai/composite/rca.hpp"
24 #include "ai/composite/stage.hpp"
26 #include "log.hpp"
27 
28 namespace ai {
29 
30 static lg::log_domain log_ai_engine_cpp("ai/engine/cpp");
31 #define DBG_AI_ENGINE_CPP LOG_STREAM(debug, log_ai_engine_cpp)
32 #define LOG_AI_ENGINE_CPP LOG_STREAM(info, log_ai_engine_cpp)
33 #define ERR_AI_ENGINE_CPP LOG_STREAM(err, log_ai_engine_cpp)
34 
36  : engine(context,cfg)
37 {
38  name_ = "cpp";
39 }
40 
42 {
43 }
44 
45 void engine_cpp::do_parse_aspect_from_config( const config &cfg, const std::string &id, std::back_insert_iterator<std::vector< aspect_ptr >> b )
46 {
47  const std::string aspect_factory_key = id + "*" + cfg["name"].str(); //@note: hack which combines aspect id and name to get the std::string key of the aspect factory
49  if (f == aspect_factory::get_list().end()){
50  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN aspect["<<aspect_factory_key<<"]";
51  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
52  return;
53  }
54  aspect_ptr new_aspect = f->second->get_new_instance(ai_,cfg,id);
55  if (!new_aspect) {
56  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE aspect, key=["<<aspect_factory_key<<"]";
57  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
58  return;
59  }
60  *b = new_aspect;
61 }
62 
63 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 ){
65  if (f == candidate_action_factory::get_list().end()){
66  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN candidate_action["<<cfg["name"]<<"]";
67  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
68  return;
69  }
70  candidate_action_ptr new_candidate_action = f->second->get_new_instance(context,cfg);
71  if (!new_candidate_action) {
72  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE candidate_action["<<cfg["name"]<<"]";
73  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
74  return;
75  }
76  *b = new_candidate_action;
77 
78 }
79 
80 void engine_cpp::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b )
81 {
83  if (f == stage_factory::get_list().end()){
84  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN stage["<<cfg["name"]<<"]";
85  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
86  return;
87  }
88  stage_ptr new_stage = f->second->get_new_instance(context,cfg);
89  if (!new_stage) {
90  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE stage["<<cfg["name"]<<"]";
91  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
92  return;
93  }
94  *b = new_stage;
95 }
96 
97 void engine_cpp::do_parse_goal_from_config(const config &cfg, std::back_insert_iterator<std::vector< goal_ptr >> b )
98 {
100  if (f == goal_factory::get_list().end()){
101  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN goal["<<cfg["name"]<<"]";
102  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
103  return;
104  }
105  goal_ptr new_goal = f->second->get_new_instance(ai_,cfg);
106  if (!new_goal || !new_goal->ok()) {
107  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE goal["<<cfg["name"]<<"]";
108  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
109  return;
110  }
111  *b = new_goal;
112 }
113 
114 void engine_cpp::do_parse_engine_from_config(const config &cfg, std::back_insert_iterator<std::vector< engine_ptr >> b )
115 {
117  if (f == engine_factory::get_list().end()){
118  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN engine["<<cfg["name"]<<"]";
119  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
120  return;
121  }
122  engine_ptr new_engine = f->second->get_new_instance(ai_,cfg);
123  if (!new_engine) {
124  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE engine["<<cfg["name"]<<"]";
125  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg;
126  return;
127  }
128  *b = new_engine;
129 }
130 
131 } //end of namespace ai
static factory_map & get_list()
Definition: aspect.hpp:455
static factory_map & get_list()
Definition: rca.hpp:155
engine_cpp(readonly_context &context, const config &cfg)
Definition: engine_cpp.cpp:35
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:63
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:80
virtual ~engine_cpp()
Definition: engine_cpp.cpp:41
virtual void do_parse_engine_from_config(const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine_cpp.cpp:114
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine_cpp.cpp:97
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:45
static factory_map & get_list()
Definition: engine.hpp:110
std::string name_
Definition: engine.hpp:99
readonly_context & ai_
Definition: engine.hpp:93
static factory_map & get_list()
Definition: goal.hpp:158
virtual side_number get_side() const =0
Get the side number.
static factory_map & get_list()
Definition: stage.hpp:96
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
#define DBG_AI_ENGINE_CPP
Definition: engine_cpp.cpp:31
#define ERR_AI_ENGINE_CPP
Definition: engine_cpp.cpp:33
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:59
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