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