The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
engine_cpp.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2017 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project http://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 
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<<"]" << std::endl;
52  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
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<<"]"<< std::endl;
58  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
59  return;
60  }
61  *b = new_aspect;
62 }
63 
64 
65 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 ){
67  if (f == candidate_action_factory::get_list().end()){
68  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN candidate_action["<<cfg["name"]<<"]"<< std::endl;
69  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
70  return;
71  }
72  candidate_action_ptr new_candidate_action = f->second->get_new_instance(context,cfg);
73  if (!new_candidate_action) {
74  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE candidate_action["<<cfg["name"]<<"]"<< std::endl;
75  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
76  return;
77  }
78  *b = new_candidate_action;
79 
80 }
81 
82 void engine_cpp::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b )
83 {
85  if (f == stage_factory::get_list().end()){
86  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN stage["<<cfg["name"]<<"]"<< std::endl;
87  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
88  return;
89  }
90  stage_ptr new_stage = f->second->get_new_instance(context,cfg);
91  if (!new_stage) {
92  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE stage["<<cfg["name"]<<"]"<< std::endl;
93  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
94  return;
95  }
96  *b = new_stage;
97 }
98 
99 
100 void engine_cpp::do_parse_goal_from_config(const config &cfg, std::back_insert_iterator<std::vector< goal_ptr > > b )
101 {
103  if (f == goal_factory::get_list().end()){
104  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN goal["<<cfg["name"]<<"]"<< std::endl;
105  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
106  return;
107  }
108  goal_ptr new_goal = f->second->get_new_instance(ai_,cfg);
109  if (!new_goal || !new_goal->ok()) {
110  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE goal["<<cfg["name"]<<"]"<< std::endl;
111  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
112  return;
113  }
114  *b = new_goal;
115 }
116 
117 
118 void engine_cpp::do_parse_engine_from_config(const config &cfg, std::back_insert_iterator<std::vector< engine_ptr > > b )
119 {
121  if (f == engine_factory::get_list().end()){
122  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN engine["<<cfg["name"]<<"]"<< std::endl;
123  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
124  return;
125  }
126  engine_ptr new_engine = f->second->get_new_instance(ai_,cfg);
127  if (!new_engine) {
128  ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE engine["<<cfg["name"]<<"]"<< std::endl;
129  DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
130  return;
131  }
132  *b = new_engine;
133 }
134 
135 
136 } //end of namespace ai
static factory_map & get_list()
Definition: rca.hpp:158
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:147
std::vector< char_t > string
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:65
virtual ~engine_cpp()
Definition: engine_cpp.cpp:41
static factory_map & get_list()
Definition: goal.hpp:183
#define ERR_AI_ENGINE_CPP
Definition: engine_cpp.cpp:33
CPP AI Support engine - creating specific ai components from config.
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
Composite AI stages.
std::shared_ptr< aspect > aspect_ptr
Definition: game_info.hpp:105
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:107
static lg::log_domain log_ai_engine_cpp("ai/engine/cpp")
#define b
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:82
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
static factory_map & get_list()
Definition: stage.hpp:107
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:110
Composite AI with turn sequence which is a vector of stages.
static factory_map & get_list()
Definition: engine.hpp:124
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:108
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
virtual void do_parse_engine_from_config(const config &cfg, std::back_insert_iterator< std::vector< engine_ptr > > b)
Definition: engine_cpp.cpp:118
std::string name_
Definition: engine.hpp:111
static factory_map & get_list()
Definition: aspect.hpp:479
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr > > b)
Definition: engine_cpp.cpp:100
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
readonly_context & ai_
Definition: engine.hpp:105
candidate action framework