The Battle for Wesnoth  1.15.0-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 
25 //============================================================================
26 namespace ai {
27 
28 class candidate_action : public virtual rca_context_proxy, public component {
29 public:
30  //this is a score guaranteed to be <=0, thus candidate action with this score will not be selected for execution
31  static const double BAD_SCORE;
32 
33  //this is a score guaranteed to be very high, higher than any 'normal' candidate action score
34  static const double HIGH_SCORE;
35 
36  candidate_action( rca_context &context, const config &cfg );
37 
38  /**
39  * Destructor
40  */
41  virtual ~candidate_action();
42 
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  /**
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 name of the candidate action (useful for debug purposes)
85  */
86  virtual std::string get_name() const
87  { return name_; }
88 
89  /**
90  * Get the type of the candidate action (useful for debug purposes)
91  */
92  const std::string& get_type() const;
93 
94  virtual std::string get_id() const
95  { return id_; }
96 
97  virtual std::string get_engine() const
98  { return engine_; }
99 
100  int get_recursion_count() const;
101 
102 
103  /**
104  * serialize
105  */
106  virtual config to_config() const;
107 
108  virtual void set_to_be_removed();
109 
110  virtual bool to_be_removed();
111 
112 private:
113 
115 
116  bool enabled_;
117 
118 
119  std::string engine_;
120 
121 
122  double score_;
123 
124 
125  double max_score_;
126 
127 
128  std::string id_;
129 
130 
131  std::string name_;
132 
133 
134  std::string type_;
135 
136 
138 
139 };
140 
141 typedef std::shared_ptr<candidate_action> candidate_action_ptr;
142 
144 
146  bool is_duplicate(const std::string &name);
147 public:
148  typedef std::shared_ptr< candidate_action_factory > factory_ptr;
149  typedef std::map<std::string, factory_ptr> factory_map;
150  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
151 
152  static factory_map& get_list() {
153  static factory_map *candidate_action_factories;
154  if (candidate_action_factories==nullptr) {
155  candidate_action_factories = new factory_map;
156  }
157  return *candidate_action_factories;
158  }
159 
160  virtual candidate_action_ptr get_new_instance( rca_context &context, const config &cfg ) = 0;
161 
162  candidate_action_factory( const std::string &name )
163  {
164  if (is_duplicate(name)) {
165  return;
166  }
167  factory_ptr ptr_to_this(this);
168  get_list().emplace(name,ptr_to_this);
169  }
170 
172 };
173 
174 
175 template<class CANDIDATE_ACTION>
177 public:
179  : candidate_action_factory( name )
180  {
181  }
182 
183  virtual candidate_action_ptr get_new_instance( rca_context &ai, const config &cfg ){
184  return std::make_shared<CANDIDATE_ACTION>(ai, cfg);
185  }
186 };
187 
188 //============================================================================
189 
190 std::ostream &operator<<(std::ostream &s, const ai::candidate_action& ca);
191 
192 } //end of namespace ai
static factory_map & get_list()
Definition: rca.hpp:152
std::string engine_
Definition: rca.hpp:119
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:141
double max_score_
Definition: rca.hpp:125
virtual std::string get_engine() const
Definition: rca.hpp:97
std::string name_
Definition: rca.hpp:131
const std::string & get_type() const
Get the type of the candidate action (useful for debug purposes)
Definition: rca.cpp:83
std::ostream & operator<<(std::ostream &s, const ai::candidate_action &ca)
Definition: rca.cpp:123
virtual std::string get_id() const
Definition: rca.hpp:94
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:183
const t_string name
bool is_enabled() const
Is this candidate action enabled ?
Definition: rca.cpp:50
recursion_counter recursion_counter_
Definition: rca.hpp:114
A component of the AI framework.
candidate_action_factory(const std::string &name)
Definition: rca.hpp:162
void enable()
Enable the candidate action.
Definition: rca.cpp:56
virtual void set_to_be_removed()
Definition: rca.cpp:101
virtual bool to_be_removed()
Definition: rca.cpp:106
double get_max_score() const
Get the upper bound of the score of the candidate action without re-evaluation.
Definition: rca.cpp:78
static const double HIGH_SCORE
Definition: rca.hpp:34
candidate_action(rca_context &context, const config &cfg)
Definition: rca.cpp:35
std::map< std::string, factory_ptr > factory_map
Definition: rca.hpp:149
virtual config to_config() const
serialize
Definition: rca.cpp:88
static map_location::DIRECTION s
virtual ~candidate_action_factory()
Definition: rca.hpp:171
std::string id_
Definition: rca.hpp:128
int get_recursion_count() const
Get the value of the recursion counter.
Definition: rca.cpp:61
std::string type_
Definition: rca.hpp:134
virtual std::string get_name() const
Get the name of the candidate action (useful for debug purposes)
Definition: rca.hpp:86
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:148
virtual ~candidate_action()
Destructor.
Definition: rca.cpp:45
Composite AI contexts.
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: rca.hpp:150
static const double BAD_SCORE
Definition: rca.hpp:31
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:72
register_candidate_action_factory(const std::string &name)
Definition: rca.hpp:178
virtual void execute()=0
Execute the candidate action.