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