The Battle for Wesnoth  1.15.12+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  * Evaluate the candidate action, resetting the internal state of the action
46  * @return the score
47  * @retval >0 if the action is good
48  * @retval <=0 if the action is not good
49  */
50  virtual double evaluate() = 0;
51 
52  /**
53  * Execute the candidate action
54  */
55  virtual void execute() = 0;
56 
57  /**
58  * Is this candidate action enabled ?
59  */
60  bool is_enabled() const;
61 
62  /**
63  * Enable the candidate action
64  */
65  void enable();
66 
67  /**
68  * Disable the candidate action
69  */
70  void disable();
71 
72  /**
73  * Get the usual score of the candidate action without re-evaluation
74  */
75  double get_score() const;
76 
77  /**
78  * Get the upper bound of the score of the candidate action without re-evaluation
79  */
80  double get_max_score() const;
81 
82  /**
83  * Get the unit filter for allowed units for this candidate action
84  */
85  std::shared_ptr<unit_filter> get_filter_own() const;
86 
87  /**
88  * Flag indicating whether unit may be used by this candidate action
89  */
90  bool is_allowed_unit(const unit& u) const;
91 
92  /**
93  * Get the name of the candidate action (useful for debug purposes)
94  */
95  virtual std::string get_name() const
96  { return name_; }
97 
98  /**
99  * Get the type of the candidate action (useful for debug purposes)
100  */
101  const std::string& get_type() const;
102 
103  virtual std::string get_id() const
104  { return id_; }
105 
106  virtual std::string get_engine() const
107  { return engine_; }
108 
109  int get_recursion_count() const;
110 
111  /**
112  * serialize
113  */
114  virtual config to_config() const;
115 
116  virtual void set_to_be_removed();
117 
118  virtual bool to_be_removed();
119 
120 private:
121 
123 
124  bool enabled_;
125 
126  std::string engine_;
127 
128  double score_;
129 
130  double max_score_;
131 
132  std::shared_ptr<unit_filter> filter_own_;
133 
134  std::string id_;
135 
136  std::string name_;
137 
138  std::string type_;
139 
141 
142 };
143 
144 typedef std::shared_ptr<candidate_action> candidate_action_ptr;
145 
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 
185  virtual candidate_action_ptr get_new_instance( rca_context &ai, const config &cfg ){
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::shared_ptr< unit_filter > get_filter_own() const
Get the unit filter for allowed units for this candidate action.
Definition: rca.cpp:84
std::string engine_
Definition: rca.hpp:126
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:144
double max_score_
Definition: rca.hpp:130
This class represents a single unit of a specific type.
Definition: unit.hpp:120
std::shared_ptr< unit_filter > filter_own_
Definition: rca.hpp:132
virtual std::string get_engine() const
Definition: rca.hpp:106
std::string name_
Definition: rca.hpp:136
const std::string & get_type() const
Get the type of the candidate action (useful for debug purposes)
Definition: rca.cpp:97
std::ostream & operator<<(std::ostream &s, const ai::candidate_action &ca)
Definition: rca.cpp:140
virtual std::string get_id() const
Definition: rca.hpp:103
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
virtual candidate_action_ptr get_new_instance(rca_context &ai, const config &cfg)
Definition: rca.hpp:185
bool is_enabled() const
Is this candidate action enabled ?
Definition: rca.cpp:54
recursion_counter recursion_counter_
Definition: rca.hpp:122
bool is_allowed_unit(const unit &u) const
Flag indicating whether unit may be used by this candidate action.
Definition: rca.cpp:89
A component of the AI framework.
candidate_action_factory(const std::string &name)
Definition: rca.hpp:165
void enable()
Enable the candidate action.
Definition: rca.cpp:59
virtual void set_to_be_removed()
Definition: rca.cpp:118
virtual bool to_be_removed()
Definition: rca.cpp:123
double get_max_score() const
Get the upper bound of the score of the candidate action without re-evaluation.
Definition: rca.cpp:79
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:152
virtual config to_config() const
serialize
Definition: rca.cpp:102
static map_location::DIRECTION s
virtual ~candidate_action_factory()
Definition: rca.hpp:174
std::string id_
Definition: rca.hpp:134
int get_recursion_count() const
Get the value of the recursion counter.
Definition: rca.cpp:64
std::string type_
Definition: rca.hpp:138
virtual std::string get_name() const
Get the name of the candidate action (useful for debug purposes)
Definition: rca.hpp:95
virtual double evaluate()=0
Evaluate the candidate action, resetting the internal state of the action.
void disable()
Disable the candidate action.
Definition: rca.cpp:69
std::shared_ptr< candidate_action_factory > factory_ptr
Definition: rca.hpp:151
virtual ~candidate_action()
Destructor.
Definition: rca.cpp:50
Composite AI contexts.
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: rca.hpp:153
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:59
double get_score() const
Get the usual score of the candidate action without re-evaluation.
Definition: rca.cpp:74
register_candidate_action_factory(const std::string &name)
Definition: rca.hpp:180
virtual void execute()=0
Execute the candidate action.