The Battle for Wesnoth  1.19.7+dev
rca.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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  * @file
18  * candidate action framework
19  */
20 
21 #pragma once
22 
25 #include "units/filter.hpp"
26 
27 //============================================================================
28 namespace ai {
29 
30 class candidate_action : public virtual rca_context_proxy, public component {
31 public:
32  //this is a score guaranteed to be <=0, thus candidate action with this score will not be selected for execution
33  static const double BAD_SCORE;
34 
35  //this is a score guaranteed to be very high, higher than any 'normal' candidate action score
36  static const double HIGH_SCORE;
37 
38  candidate_action( rca_context &context, const config &cfg );
39 
40  /**
41  * Destructor
42  */
43  virtual ~candidate_action();
44 
45  /**
46  * Evaluate the candidate action, resetting the internal state of the action
47  * @return the score
48  * @retval >0 if the action is good
49  * @retval <=0 if the action is not good
50  */
51  virtual double evaluate() = 0;
52 
53  /**
54  * Execute the candidate action
55  */
56  virtual void execute() = 0;
57 
58  /**
59  * Is this candidate action enabled ?
60  */
61  bool is_enabled() const;
62 
63  /**
64  * Enable the candidate action
65  */
66  void enable();
67 
68  /**
69  * Disable the candidate action
70  */
71  void disable();
72 
73  /**
74  * Get the usual score of the candidate action without re-evaluation
75  */
76  double get_score() const;
77 
78  /**
79  * Get the upper bound of the score of the candidate action without re-evaluation
80  */
81  double get_max_score() const;
82 
83  /**
84  * Get the unit filter for allowed units for this candidate action
85  */
86  std::shared_ptr<unit_filter> get_filter_own() const;
87 
88  /**
89  * Flag indicating whether unit may be used by this candidate action
90  */
91  bool is_allowed_unit(const unit& u) const;
92 
93  /**
94  * Get the name of the candidate action (useful for debug purposes)
95  */
96  virtual std::string get_name() const
97  { return name_; }
98 
99  /**
100  * Get the type of the candidate action (useful for debug purposes)
101  */
102  const std::string& get_type() const;
103 
104  virtual std::string get_id() const
105  { return id_; }
106 
107  virtual std::string get_engine() const
108  { return engine_; }
109 
110  int get_recursion_count() const;
111 
112  /**
113  * serialize
114  */
115  virtual config to_config() const;
116 
117  virtual void set_to_be_removed();
118 
119  virtual bool to_be_removed();
120 
121 private:
122 
124 
125  bool enabled_;
126 
127  std::string engine_;
128 
129  double score_;
130 
131  double max_score_;
132 
133  std::shared_ptr<unit_filter> filter_own_;
134 
135  std::string id_;
136 
137  std::string name_;
138 
139  std::string type_;
140 
142 
143 };
144 
145 typedef std::shared_ptr<candidate_action> candidate_action_ptr;
146 
147 
149  bool is_duplicate(const std::string &name);
150 public:
151  typedef std::shared_ptr< candidate_action_factory > factory_ptr;
152  typedef std::map<std::string, factory_ptr> factory_map;
153  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
154 
155  static factory_map& get_list() {
156  static factory_map *candidate_action_factories;
157  if (candidate_action_factories==nullptr) {
158  candidate_action_factories = new factory_map;
159  }
160  return *candidate_action_factories;
161  }
162 
163  virtual candidate_action_ptr get_new_instance( rca_context &context, const config &cfg ) = 0;
164 
165  candidate_action_factory( const std::string &name )
166  {
167  if (is_duplicate(name)) {
168  return;
169  }
170  factory_ptr ptr_to_this(this);
171  get_list().emplace(name,ptr_to_this);
172  }
173 
175 };
176 
177 template<class CANDIDATE_ACTION>
179 public:
180  register_candidate_action_factory( const std::string &name )
181  : candidate_action_factory( name )
182  {
183  }
184 
186  return std::make_shared<CANDIDATE_ACTION>(ai, cfg);
187  }
188 };
189 
190 //============================================================================
191 
192 std::ostream &operator<<(std::ostream &s, const ai::candidate_action& ca);
193 
194 } //end of namespace ai
static factory_map & get_list()
Definition: rca.hpp:155
std::map< std::string, factory_ptr > factory_map
Definition: rca.hpp:152
virtual candidate_action_ptr get_new_instance(rca_context &context, const config &cfg)=0
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: rca.hpp:153
bool is_duplicate(const std::string &name)
Definition: rca.cpp:128
candidate_action_factory(const std::string &name)
Definition: rca.hpp:165
virtual ~candidate_action_factory()
Definition: rca.hpp:174
std::shared_ptr< candidate_action_factory > factory_ptr
Definition: rca.hpp:151
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 std::string get_id() const
Definition: rca.hpp:104
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
virtual std::string get_engine() const
Definition: rca.hpp:107
static const double BAD_SCORE
Definition: rca.hpp:33
bool is_enabled() const
Is this candidate action enabled ?
Definition: rca.cpp:53
double max_score_
Definition: rca.hpp:131
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
virtual double evaluate()=0
Evaluate the candidate action, resetting the internal state of the action.
virtual void execute()=0
Execute the candidate action.
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
register_candidate_action_factory(const std::string &name)
Definition: rca.hpp:180
virtual candidate_action_ptr get_new_instance(rca_context &ai, const config &cfg)
Definition: rca.hpp:185
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
This class represents a single unit of a specific type.
Definition: unit.hpp:133
A component of the AI framework.
Composite AI contexts.
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< candidate_action > candidate_action_ptr
Definition: rca.hpp:145
std::ostream & operator<<(std::ostream &s, const ai::candidate_action &ca)
Definition: rca.cpp:139
static map_location::direction s