The Battle for Wesnoth  1.17.23+dev
rca.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  * Candidate actions framework
18  * @file
19  */
20 
21 #include "ai/composite/ai.hpp"
22 #include "ai/composite/engine.hpp"
23 #include "ai/composite/rca.hpp"
24 #include "log.hpp"
25 
26 namespace ai {
27 
28 static lg::log_domain log_ai_stage_rca("ai/stage/rca");
29 #define DBG_AI_STAGE_RCA LOG_STREAM(debug, log_ai_stage_rca)
30 #define LOG_AI_STAGE_RCA LOG_STREAM(info, log_ai_stage_rca)
31 #define ERR_AI_STAGE_RCA LOG_STREAM(err, log_ai_stage_rca)
32 
33 const double candidate_action::BAD_SCORE = 0;
34 const double candidate_action::HIGH_SCORE = 10000000;
35 
37  recursion_counter_(context.get_recursion_count()),
38  enabled_(cfg["enabled"].to_bool(true)), engine_(cfg["engine"]),
39  score_(cfg["score"].to_double(BAD_SCORE)),
40  max_score_(cfg["max_score"].to_double(HIGH_SCORE)),
41  id_(cfg["id"]), name_(cfg["name"]), type_(cfg["type"]), to_be_removed_(false)
42 {
43  if (auto filter_own = cfg.optional_child("filter_own")) {
44  vconfig vcfg(*filter_own);
45  vcfg.make_safe();
46  filter_own_.reset(new unit_filter(vcfg));
47  }
48  init_rca_context_proxy(context);
49 }
50 
52 {
53 }
54 
56 {
57  return enabled_;
58 }
59 
61 {
62  enabled_ = true;
63 }
64 
66 {
68 }
69 
71 {
72  enabled_ = false;
73 }
74 
76 {
77  return score_;
78 }
79 
81 {
82  return max_score_;
83 }
84 
85 std::shared_ptr<unit_filter> candidate_action::get_filter_own() const
86 {
87  return filter_own_;
88 }
89 
91 {
92  if (filter_own_) {
93  return (*filter_own_)(u);
94  }
95  return true;
96 }
97 
98 const std::string& candidate_action::get_type() const
99 {
100  return type_;
101 }
102 
104 {
105  config cfg;
106  cfg["enabled"] = enabled_;
107  cfg["engine"] = engine_;
108  cfg["id"] = id_;
109  cfg["name"] = name_;
110  cfg["score"] = score_;
111  cfg["max_score"] = max_score_;
112  if (filter_own_ && !filter_own_->empty()) {
113  cfg.add_child("filter_own", filter_own_->to_config());
114  }
115  cfg["type"] = type_;
116  return cfg;
117 }
118 
120 {
121  to_be_removed_ = true;
122 }
123 
125 {
126  return to_be_removed_;
127 }
128 
129 // This is defined in the source file so that it can easily access the logger
130 bool candidate_action_factory::is_duplicate(const std::string& name)
131 {
132  if (get_list().find(name) != get_list().end()) {
133  ERR_AI_STAGE_RCA << "Error: Attempt to double-register candidate action " << name;
134  return true;
135  }
136  return false;
137 }
138 
139 //============================================================================
140 
141 std::ostream &operator<<(std::ostream &s, const ai::candidate_action& ca) {
142  s << "candidate action with name ["<< ca.get_name() <<"]";
143  return s;
144 }
145 
146 } // of namespace ai
static factory_map & get_list()
Definition: rca.hpp:156
bool is_duplicate(const std::string &name)
Definition: rca.cpp:130
void enable()
Enable the candidate action.
Definition: rca.cpp:60
std::shared_ptr< unit_filter > filter_own_
Definition: rca.hpp:133
virtual void set_to_be_removed()
Definition: rca.cpp:119
std::string name_
Definition: rca.hpp:137
void disable()
Disable the candidate action.
Definition: rca.cpp:70
candidate_action(rca_context &context, const config &cfg)
Definition: rca.cpp:36
virtual ~candidate_action()
Destructor.
Definition: rca.cpp:51
virtual bool to_be_removed()
Definition: rca.cpp:124
static const double HIGH_SCORE
Definition: rca.hpp:36
static const double BAD_SCORE
Definition: rca.hpp:33
bool is_enabled() const
Is this candidate action enabled ?
Definition: rca.cpp:55
double max_score_
Definition: rca.hpp:131
int get_recursion_count() const
Get the value of the recursion counter.
Definition: rca.cpp:65
std::string type_
Definition: rca.hpp:139
virtual std::string get_name() const
Get the name of the candidate action (useful for debug purposes)
Definition: rca.hpp:96
recursion_counter recursion_counter_
Definition: rca.hpp:123
const std::string & get_type() const
Get the type of the candidate action (useful for debug purposes)
Definition: rca.cpp:98
bool is_allowed_unit(const unit &u) const
Flag indicating whether unit may be used by this candidate action.
Definition: rca.cpp:90
std::string engine_
Definition: rca.hpp:127
double get_max_score() const
Get the upper bound of the score of the candidate action without re-evaluation.
Definition: rca.cpp:80
virtual config to_config() const
serialize
Definition: rca.cpp:103
double get_score() const
Get the usual score of the candidate action without re-evaluation.
Definition: rca.cpp:75
std::shared_ptr< unit_filter > get_filter_own() const
Get the unit filter for allowed units for this candidate action.
Definition: rca.cpp:85
std::string id_
Definition: rca.hpp:135
void init_rca_context_proxy(rca_context &target)
Definition: contexts.hpp:117
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:70
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Euivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:389
config & add_child(config_key_type key)
Definition: config.cpp:445
This class represents a single unit of a specific type.
Definition: unit.hpp:135
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
const vconfig & make_safe() const
instruct the vconfig to make a private copy of its underlying data.
Definition: variable.cpp:163
Composite AI with turn sequence which is a vector of stages.
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
static lg::log_domain log_ai_stage_rca("ai/stage/rca")
std::ostream & operator<<(std::ostream &s, const ai::candidate_action &ca)
Definition: rca.cpp:141
#define ERR_AI_STAGE_RCA
Definition: rca.cpp:31
candidate action framework
static map_location::DIRECTION s