The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
engine_cpp.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2009 - 2025
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
22#include "ai/composite/goal.hpp"
23#include "ai/composite/rca.hpp"
26#include "log.hpp"
27
28namespace ai {
29
30static 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
44
45void 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
48 aspect_factory::factory_map::iterator f = aspect_factory::get_list().find(aspect_factory_key);
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
63void engine_cpp::do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr >> b ){
64 candidate_action_factory::factory_map::iterator f = candidate_action_factory::get_list().find(cfg["name"]);
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
80void engine_cpp::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b )
81{
82 stage_factory::factory_map::iterator f = stage_factory::get_list().find(cfg["name"]);
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
97void engine_cpp::do_parse_goal_from_config(const config &cfg, std::back_insert_iterator<std::vector< goal_ptr >> b )
98{
99 goal_factory::factory_map::iterator f = goal_factory::get_list().find(cfg["name"]);
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
114void engine_cpp::do_parse_engine_from_config(const config &cfg, std::back_insert_iterator<std::vector< engine_ptr >> b )
115{
116 engine_factory::factory_map::iterator f = engine_factory::get_list().find(cfg["name"]);
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:418
static factory_map & get_list()
Definition rca.hpp:155
virtual void do_parse_engine_from_config(const config &cfg, std::back_insert_iterator< std::vector< engine_ptr > > b)
void do_parse_aspect_from_config(const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr > > b)
virtual void do_parse_candidate_action_from_config(rca_context &context, const config &cfg, std::back_insert_iterator< std::vector< candidate_action_ptr > > b)
engine_cpp(readonly_context &context, const config &cfg)
virtual ~engine_cpp()
virtual void do_parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr > > b)
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr > > b)
static factory_map & get_list()
Definition engine.hpp:107
std::string name_
Definition engine.hpp:96
readonly_context & ai_
Definition engine.hpp:90
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:157
#define DBG_AI_ENGINE_CPP
#define ERR_AI_ENGINE_CPP
CPP AI Support engine - creating specific ai components from config.
const config * cfg
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:97
std::shared_ptr< goal > goal_ptr
std::shared_ptr< stage > stage_ptr
std::shared_ptr< candidate_action > candidate_action_ptr
Definition rca.hpp:145
candidate action framework
Composite AI stages.
#define f
#define b