The Battle for Wesnoth  1.17.0-dev
rca.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2021
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 
148 
150  bool is_duplicate(const std::string &name);
151 public:
152  typedef std::shared_ptr< candidate_action_factory > factory_ptr;
153  typedef std::map<std::string, factory_ptr> factory_map;
154  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
155 
156  static factory_map& get_list() {
157  static factory_map *candidate_action_factories;
158  if (candidate_action_factories==nullptr) {
159  candidate_action_factories = new factory_map;
160  }
161  return *candidate_action_factories;
162  }
163 
164  virtual candidate_action_ptr get_new_instance( rca_context &context, const config &cfg ) = 0;
165 
166  candidate_action_factory( const std::string &name )
167  {
168  if (is_duplicate(name)) {
169  return;
170  }
171  factory_ptr ptr_to_this(this);
172  get_list().emplace(name,ptr_to_this);
173  }
174 
176 };
177 
178 template<class CANDIDATE_ACTION>
180 public:
181  register_candidate_action_factory( const std::string &name )
182  : candidate_action_factory( name )
183  {
184  }
185 
186  virtual candidate_action_ptr get_new_instance( rca_context &ai, const config &cfg ){
187  return std::make_shared<CANDIDATE_ACTION>(ai, cfg);
188  }
189 };
190 
191 //============================================================================
192 
193 std::ostream &operator<<(std::ostream &s, const ai::candidate_action& ca);
194 
195 } //end of namespace ai
static factory_map & get_list()
Definition: rca.hpp:156
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 engine_
Definition: rca.hpp:127
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:145
double max_score_
Definition: rca.hpp:131
This class represents a single unit of a specific type.
Definition: unit.hpp:121
std::shared_ptr< unit_filter > filter_own_
Definition: rca.hpp:133
virtual std::string get_engine() const
Definition: rca.hpp:107
std::string name_
Definition: rca.hpp:137
const std::string & get_type() const
Get the type of the candidate action (useful for debug purposes)
Definition: rca.cpp:98
std::ostream & operator<<(std::ostream &s, const ai::candidate_action &ca)
Definition: rca.cpp:141
virtual std::string get_id() const
Definition: rca.hpp:104
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
virtual candidate_action_ptr get_new_instance(rca_context &ai, const config &cfg)
Definition: rca.hpp:186
bool is_enabled() const
Is this candidate action enabled ?
Definition: rca.cpp:55
recursion_counter recursion_counter_
Definition: rca.hpp:123
bool is_allowed_unit(const unit &u) const
Flag indicating whether unit may be used by this candidate action.
Definition: rca.cpp:90
A component of the AI framework.
candidate_action_factory(const std::string &name)
Definition: rca.hpp:166
void enable()
Enable the candidate action.
Definition: rca.cpp:60
virtual void set_to_be_removed()
Definition: rca.cpp:119
virtual bool to_be_removed()
Definition: rca.cpp:124
double get_max_score() const
Get the upper bound of the score of the candidate action without re-evaluation.
Definition: rca.cpp:80
static const double HIGH_SCORE
Definition: rca.hpp:36
candidate_action(rca_context &context, const config &cfg)
Definition: rca.cpp:36
std::map< std::string, factory_ptr > factory_map
Definition: rca.hpp:153
virtual config to_config() const
serialize
Definition: rca.cpp:103
static map_location::DIRECTION s
virtual ~candidate_action_factory()
Definition: rca.hpp:175
std::string id_
Definition: rca.hpp:135
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
virtual double evaluate()=0
Evaluate the candidate action, resetting the internal state of the action.
void disable()
Disable the candidate action.
Definition: rca.cpp:70
std::shared_ptr< candidate_action_factory > factory_ptr
Definition: rca.hpp:152
virtual ~candidate_action()
Destructor.
Definition: rca.cpp:51
Composite AI contexts.
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: rca.hpp:154
static const double BAD_SCORE
Definition: rca.hpp:33
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
double get_score() const
Get the usual score of the candidate action without re-evaluation.
Definition: rca.cpp:75
register_candidate_action_factory(const std::string &name)
Definition: rca.hpp:181
virtual void execute()=0
Execute the candidate action.