The Battle for Wesnoth  1.15.2+dev
rca.hpp
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  * @file
17  * candidate action framework
18  */
19 
20 #pragma once
21 
24 #include "units/filter.hpp"
25 
26 //============================================================================
27 namespace ai {
28 
29 class candidate_action : public virtual rca_context_proxy, public component {
30 public:
31  //this is a score guaranteed to be <=0, thus candidate action with this score will not be selected for execution
32  static const double BAD_SCORE;
33 
34  //this is a score guaranteed to be very high, higher than any 'normal' candidate action score
35  static const double HIGH_SCORE;
36 
37  candidate_action( rca_context &context, const config &cfg );
38 
39  /**
40  * Destructor
41  */
42  virtual ~candidate_action();
43 
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  /**
80  * Get the upper bound of the score of the candidate action without re-evaluation
81  */
82  double get_max_score() const;
83 
84 
85  /**
86  * Get the unit filter for allowed units for this candidate action
87  */
88  std::shared_ptr<unit_filter> get_filter_own() const;
89 
90 
91  /**
92  * Flag indicating whether unit may be used by this candidate action
93  */
94  bool is_allowed_unit(const unit& u) const;
95 
96  /**
97  * Get the name of the candidate action (useful for debug purposes)
98  */
99  virtual std::string get_name() const
100  { return name_; }
101 
102  /**
103  * Get the type of the candidate action (useful for debug purposes)
104  */
105  const std::string& get_type() const;
106 
107  virtual std::string get_id() const
108  { return id_; }
109 
110  virtual std::string get_engine() const
111  { return engine_; }
112 
113  int get_recursion_count() const;
114 
115 
116  /**
117  * serialize
118  */
119  virtual config to_config() const;
120 
121  virtual void set_to_be_removed();
122 
123  virtual bool to_be_removed();
124 
125 private:
126 
128 
129  bool enabled_;
130 
131 
132  std::string engine_;
133 
134 
135  double score_;
136 
137 
138  double max_score_;
139 
140 
141  std::shared_ptr<unit_filter> filter_own_;
142 
143 
144  std::string id_;
145 
146 
147  std::string name_;
148 
149 
150  std::string type_;
151 
152 
154 
155 };
156 
157 typedef std::shared_ptr<candidate_action> candidate_action_ptr;
158 
160 
162  bool is_duplicate(const std::string &name);
163 public:
164  typedef std::shared_ptr< candidate_action_factory > factory_ptr;
165  typedef std::map<std::string, factory_ptr> factory_map;
166  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
167 
168  static factory_map& get_list() {
169  static factory_map *candidate_action_factories;
170  if (candidate_action_factories==nullptr) {
171  candidate_action_factories = new factory_map;
172  }
173  return *candidate_action_factories;
174  }
175 
176  virtual candidate_action_ptr get_new_instance( rca_context &context, const config &cfg ) = 0;
177 
178  candidate_action_factory( const std::string &name )
179  {
180  if (is_duplicate(name)) {
181  return;
182  }
183  factory_ptr ptr_to_this(this);
184  get_list().emplace(name,ptr_to_this);
185  }
186 
188 };
189 
190 
191 template<class CANDIDATE_ACTION>
193 public:
194  register_candidate_action_factory( const std::string &name )
195  : candidate_action_factory( name )
196  {
197  }
198 
199  virtual candidate_action_ptr get_new_instance( rca_context &ai, const config &cfg ){
200  return std::make_shared<CANDIDATE_ACTION>(ai, cfg);
201  }
202 };
203 
204 //============================================================================
205 
206 std::ostream &operator<<(std::ostream &s, const ai::candidate_action& ca);
207 
208 } //end of namespace ai
static factory_map & get_list()
Definition: rca.hpp:168
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
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:157
double max_score_
Definition: rca.hpp:138
This class represents a single unit of a specific type.
Definition: unit.hpp:99
std::shared_ptr< unit_filter > filter_own_
Definition: rca.hpp:141
virtual std::string get_engine() const
Definition: rca.hpp:110
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
virtual std::string get_id() const
Definition: rca.hpp:107
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
virtual candidate_action_ptr get_new_instance(rca_context &ai, const config &cfg)
Definition: rca.hpp:199
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
A component of the AI framework.
candidate_action_factory(const std::string &name)
Definition: rca.hpp:178
void enable()
Enable the candidate action.
Definition: rca.cpp:61
virtual void set_to_be_removed()
Definition: rca.cpp:123
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
std::map< std::string, factory_ptr > factory_map
Definition: rca.hpp:165
virtual config to_config() const
serialize
Definition: rca.cpp:107
static map_location::DIRECTION s
virtual ~candidate_action_factory()
Definition: rca.hpp:187
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
virtual double evaluate()=0
Evaluate the candidate action, resetting the internal state of the action.
void disable()
Disable the candidate action.
Definition: rca.cpp:71
std::shared_ptr< candidate_action_factory > factory_ptr
Definition: rca.hpp:164
virtual ~candidate_action()
Destructor.
Definition: rca.cpp:50
Composite AI contexts.
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: rca.hpp:166
static const double BAD_SCORE
Definition: rca.hpp:32
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
register_candidate_action_factory(const std::string &name)
Definition: rca.hpp:194
virtual void execute()=0
Execute the candidate action.