The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
rca.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Yurii Chernyi <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef AI_COMPOSITE_RCA_HPP_INCLUDED
21 #define AI_COMPOSITE_RCA_HPP_INCLUDED
22 
25 
26 #ifdef _MSC_VER
27 #pragma warning(push)
28 //silence "inherits via dominance" warnings
29 #pragma warning(disable:4250)
30 #endif
31 
32 //============================================================================
33 namespace ai {
34 
35 class candidate_action : public virtual rca_context_proxy, public component {
36 public:
37  //this is a score guaranteed to be <=0, thus candidate action with this score will not be selected for execution
38  static const double BAD_SCORE;
39 
40  //this is a score guaranteed to be very high, higher than any 'normal' candidate action score
41  static const double HIGH_SCORE;
42 
43  candidate_action( rca_context &context, const config &cfg );
44 
45  /**
46  * Destructor
47  */
48  virtual ~candidate_action();
49 
50 
51  /**
52  * Evaluate the candidate action, resetting the internal state of the action
53  * @return the score
54  * @retval >0 if the action is good
55  * @retval <=0 if the action is not good
56  */
57  virtual double evaluate() = 0;
58 
59  /**
60  * Execute the candidate action
61  */
62  virtual void execute() = 0;
63 
64  /**
65  * Is this candidate action enabled ?
66  */
67  bool is_enabled() const;
68 
69  /**
70  * Enable the candidate action
71  */
72  void enable();
73 
74  /**
75  * Disable the candidate action
76  */
77  void disable();
78 
79  /**
80  * Get the usual score of the candidate action without re-evaluation
81  */
82  double get_score() const;
83 
84 
85  /**
86  * Get the upper bound of the score of the candidate action without re-evaluation
87  */
88  double get_max_score() const;
89 
90  /**
91  * Get the name of the candidate action (useful for debug purposes)
92  */
93  virtual std::string get_name() const
94  { return name_; }
95 
96  /**
97  * Get the type of the candidate action (useful for debug purposes)
98  */
99  const std::string& get_type() const;
100 
101  virtual std::string get_id() const
102  { return id_; }
103 
104  virtual std::string get_engine() const
105  { return engine_; }
106 
107  int get_recursion_count() const;
108 
109 
110  /**
111  * serialize
112  */
113  virtual config to_config() const;
114 
115  virtual void set_to_be_removed();
116 
117  virtual bool to_be_removed();
118 
119 private:
120 
122 
123  bool enabled_;
124 
125 
127 
128 
129  double score_;
130 
131 
132  double max_score_;
133 
134 
136 
137 
139 
140 
142 
143 
145 
146 };
147 
149 
151 
153  bool is_duplicate(const std::string &name);
154 public:
156  typedef std::map<std::string, factory_ptr> factory_map;
157  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
158 
159  static factory_map& get_list() {
160  static factory_map *candidate_action_factories;
161  if (candidate_action_factories==nullptr) {
162  candidate_action_factories = new factory_map;
163  }
164  return *candidate_action_factories;
165  }
166 
167  virtual candidate_action_ptr get_new_instance( rca_context &context, const config &cfg ) = 0;
168 
170  {
171  if (is_duplicate(name)) {
172  return;
173  }
174  factory_ptr ptr_to_this(this);
175  get_list().insert(make_pair(name,ptr_to_this));
176  }
177 
179 };
180 
181 
182 template<class CANDIDATE_ACTION>
184 public:
186  : candidate_action_factory( name )
187  {
188  }
189 
190  virtual candidate_action_ptr get_new_instance( rca_context &ai, const config &cfg ){
191  return candidate_action_ptr(new CANDIDATE_ACTION(ai,cfg));
192  }
193 };
194 
195 //============================================================================
196 
197 std::ostream &operator<<(std::ostream &s, ai::candidate_action const &ca);
198 
199 } //end of namespace ai
200 
201 #ifdef _MSC_VER
202 #pragma warning(pop)
203 #endif
204 
205 #endif
static factory_map & get_list()
Definition: rca.hpp:159
std::string engine_
Definition: rca.hpp:126
double max_score_
Definition: rca.hpp:132
double get_max_score() const
Get the upper bound of the score of the candidate action without re-evaluation.
Definition: rca.cpp:78
virtual std::string get_id() const
Definition: rca.hpp:101
virtual std::string get_name() const
Get the name of the candidate action (useful for debug purposes)
Definition: rca.hpp:93
double get_score() const
Get the usual score of the candidate action without re-evaluation.
Definition: rca.cpp:72
std::ostream & operator<<(std::ostream &s, ai::candidate_action const &ca)
Definition: rca.cpp:123
std::string name_
Definition: rca.hpp:138
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
virtual candidate_action_ptr get_new_instance(rca_context &ai, const config &cfg)
Definition: rca.hpp:190
bool is_enabled() const
Is this candidate action enabled ?
Definition: rca.cpp:50
recursion_counter recursion_counter_
Definition: rca.hpp:121
A component of the AI framework.
candidate_action_factory(const std::string &name)
Definition: rca.hpp:169
void enable()
Enable the candidate action.
Definition: rca.cpp:56
virtual config to_config() const
serialize
Definition: rca.cpp:88
virtual void set_to_be_removed()
Definition: rca.cpp:101
int get_recursion_count() const
Get the value of the recursion counter.
Definition: rca.cpp:61
virtual candidate_action_ptr get_new_instance(rca_context &context, const config &cfg)=0
virtual bool to_be_removed()
Definition: rca.cpp:106
virtual std::string get_engine() const
Definition: rca.hpp:104
static const double HIGH_SCORE
Definition: rca.hpp:41
candidate_action(rca_context &context, const config &cfg)
Definition: rca.cpp:35
std::map< std::string, factory_ptr > factory_map
Definition: rca.hpp:156
bool is_duplicate(const std::string &name)
Definition: rca.cpp:112
virtual ~candidate_action_factory()
Definition: rca.hpp:178
std::string id_
Definition: rca.hpp:135
boost::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:148
std::string type_
Definition: rca.hpp:141
GLuint const GLchar * name
Definition: glew.h:1782
virtual double evaluate()=0
Evaluate the candidate action, resetting the internal state of the action.
void disable()
Disable the candidate action.
Definition: rca.cpp:66
const std::string & get_type() const
Get the type of the candidate action (useful for debug purposes)
Definition: rca.cpp:83
virtual ~candidate_action()
Destructor.
Definition: rca.cpp:45
Composite AI contexts.
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: rca.hpp:157
static const double BAD_SCORE
Definition: rca.hpp:38
boost::shared_ptr< candidate_action_factory > factory_ptr
Definition: rca.hpp:155
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLdouble s
Definition: glew.h:1358
GLsizei const GLcharARB ** string
Definition: glew.h:4503
register_candidate_action_factory(const std::string &name)
Definition: rca.hpp:185
virtual void execute()=0
Execute the candidate action.