The Battle for Wesnoth  1.15.9+dev
engine_cpp.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  * CPP AI Support engine - creating specific ai components from config
17  * @file
18  */
19 
20 #include "ai/composite/ai.hpp"
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"];//@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<<"]" << std::endl;
51  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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<<"]"<< std::endl;
57  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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"]<<"]"<< std::endl;
67  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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"]<<"]"<< std::endl;
73  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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"]<<"]"<< std::endl;
85  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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"]<<"]"<< std::endl;
91  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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"]<<"]"<< std::endl;
102  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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"]<<"]"<< std::endl;
108  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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"]<<"]"<< std::endl;
119  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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"]<<"]"<< std::endl;
125  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
126  return;
127  }
128  *b = new_engine;
129 }
130 
131 } //end of namespace ai
static factory_map & get_list()
Definition: rca.hpp:155
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:144
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:101
virtual ~engine_cpp()
Definition: engine_cpp.cpp:41
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
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
static factory_map & get_list()
Definition: goal.hpp:158
#define ERR_AI_ENGINE_CPP
Definition: engine_cpp.cpp:33
CPP AI Support engine - creating specific ai components from config.
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine_cpp.cpp:97
Composite AI stages.
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
std::shared_ptr< aspect > aspect_ptr
Definition: game_info.hpp:94
static lg::log_domain log_ai_engine_cpp("ai/engine/cpp")
#define b
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
static factory_map & get_list()
Definition: stage.hpp:96
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:99
Composite AI with turn sequence which is a vector of stages.
static factory_map & get_list()
Definition: engine.hpp:114
engine_cpp(readonly_context &context, const config &cfg)
Definition: engine_cpp.cpp:35
#define DBG_AI_ENGINE_CPP
Definition: engine_cpp.cpp:31
#define f
std::string name_
Definition: engine.hpp:102
static factory_map & get_list()
Definition: aspect.hpp:452
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
virtual void do_parse_engine_from_config(const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine_cpp.cpp:114
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:60
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
readonly_context & ai_
Definition: engine.hpp:96
candidate action framework