The Battle for Wesnoth  1.13.10+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 - 2017 by Yurii Chernyi <terraninfo@terraninfo.net>
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 #pragma once
21 
24 
25 #ifdef _MSC_VER
26 #pragma warning(push)
27 //silence "inherits via dominance" warnings
28 #pragma warning(disable:4250)
29 #endif
30 
31 //============================================================================
32 namespace ai {
33 
34 class candidate_action : public virtual rca_context_proxy, public component {
35 public:
36  //this is a score guaranteed to be <=0, thus candidate action with this score will not be selected for execution
37  static const double BAD_SCORE;
38 
39  //this is a score guaranteed to be very high, higher than any 'normal' candidate action score
40  static const double HIGH_SCORE;
41 
42  candidate_action( rca_context &context, const config &cfg );
43 
44  /**
45  * Destructor
46  */
47  virtual ~candidate_action();
48 
49 
50  /**
51  * Evaluate the candidate action, resetting the internal state of the action
52  * @return the score
53  * @retval >0 if the action is good
54  * @retval <=0 if the action is not good
55  */
56  virtual double evaluate() = 0;
57 
58  /**
59  * Execute the candidate action
60  */
61  virtual void execute() = 0;
62 
63  /**
64  * Is this candidate action enabled ?
65  */
66  bool is_enabled() const;
67 
68  /**
69  * Enable the candidate action
70  */
71  void enable();
72 
73  /**
74  * Disable the candidate action
75  */
76  void disable();
77 
78  /**
79  * Get the usual score of the candidate action without re-evaluation
80  */
81  double get_score() const;
82 
83 
84  /**
85  * Get the upper bound of the score of the candidate action without re-evaluation
86  */
87  double get_max_score() const;
88 
89  /**
90  * Get the name of the candidate action (useful for debug purposes)
91  */
92  virtual std::string get_name() const
93  { return name_; }
94 
95  /**
96  * Get the type of the candidate action (useful for debug purposes)
97  */
98  const std::string& get_type() const;
99 
100  virtual std::string get_id() const
101  { return id_; }
102 
103  virtual std::string get_engine() const
104  { return engine_; }
105 
106  int get_recursion_count() const;
107 
108 
109  /**
110  * serialize
111  */
112  virtual config to_config() const;
113 
114  virtual void set_to_be_removed();
115 
116  virtual bool to_be_removed();
117 
118 private:
119 
121 
122  bool enabled_;
123 
124 
126 
127 
128  double score_;
129 
130 
131  double max_score_;
132 
133 
135 
136 
138 
139 
141 
142 
144 
145 };
146 
147 typedef std::shared_ptr<candidate_action> candidate_action_ptr;
148 
150 
152  bool is_duplicate(const std::string &name);
153 public:
154  typedef std::shared_ptr< candidate_action_factory > factory_ptr;
155  typedef std::map<std::string, factory_ptr> factory_map;
156  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
157 
158  static factory_map& get_list() {
159  static factory_map *candidate_action_factories;
160  if (candidate_action_factories==nullptr) {
161  candidate_action_factories = new factory_map;
162  }
163  return *candidate_action_factories;
164  }
165 
166  virtual candidate_action_ptr get_new_instance( rca_context &context, const config &cfg ) = 0;
167 
169  {
170  if (is_duplicate(name)) {
171  return;
172  }
173  factory_ptr ptr_to_this(this);
174  get_list().emplace(name,ptr_to_this);
175  }
176 
178 };
179 
180 
181 template<class CANDIDATE_ACTION>
183 public:
185  : candidate_action_factory( name )
186  {
187  }
188 
189  virtual candidate_action_ptr get_new_instance( rca_context &ai, const config &cfg ){
190  return candidate_action_ptr(new CANDIDATE_ACTION(ai,cfg));
191  }
192 };
193 
194 //============================================================================
195 
196 std::ostream &operator<<(std::ostream &s, ai::candidate_action const &ca);
197 
198 } //end of namespace ai
199 
200 #ifdef _MSC_VER
201 #pragma warning(pop)
202 #endif
static factory_map & get_list()
Definition: rca.hpp:158
std::string engine_
Definition: rca.hpp:125
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:147
double max_score_
Definition: rca.hpp:131
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:100
std::vector< char_t > string
virtual std::string get_name() const
Get the name of the candidate action (useful for debug purposes)
Definition: rca.hpp:92
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:137
A small explanation about what'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:189
bool is_enabled() const
Is this candidate action enabled ?
Definition: rca.cpp:50
recursion_counter recursion_counter_
Definition: rca.hpp:120
A component of the AI framework.
candidate_action_factory(const std::string &name)
Definition: rca.hpp:168
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:103
static const double HIGH_SCORE
Definition: rca.hpp:40
candidate_action(rca_context &context, const config &cfg)
Definition: rca.cpp:35
std::map< std::string, factory_ptr > factory_map
Definition: rca.hpp:155
bool is_duplicate(const std::string &name)
Definition: rca.cpp:112
static map_location::DIRECTION s
virtual ~candidate_action_factory()
Definition: rca.hpp:177
std::string id_
Definition: rca.hpp:134
std::string type_
Definition: rca.hpp:140
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
std::shared_ptr< candidate_action_factory > factory_ptr
Definition: rca.hpp:154
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:156
static const double BAD_SCORE
Definition: rca.hpp:37
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
register_candidate_action_factory(const std::string &name)
Definition: rca.hpp:184
virtual void execute()=0
Execute the candidate action.