The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
goal.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  */
18 
19 #pragma once
20 
21 #ifdef _MSC_VER
22 #pragma warning(push)
23 //silence "inherits via dominance" warnings
24 #pragma warning(disable:4250)
25 #endif
26 
28 
29 #include "ai/default/contexts.hpp"
30 #include "ai/game_info.hpp"
31 #include "config.hpp"
32 
33 #include <iterator>
34 #include <map>
35 #include <set>
36 #include <string>
37 #include <utility>
38 #include <vector>
39 
40 class terrain_filter;
41 namespace ai { class lua_ai_action_handler; }
42 namespace ai { class lua_ai_context; }
43 namespace ai { struct target; }
44 
45 
46 namespace ai {
47 
48 class goal : public readonly_context_proxy, public component {
49 public:
50  goal(readonly_context &context, const config &cfg);
51 
52 
53  virtual ~goal();
54 
55 
56  virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
57 
58 
59  virtual config to_config() const;
60 
61 
62  virtual void on_create();
63  virtual void on_create(std::shared_ptr<ai::lua_ai_context>);
64 
65 
66  bool active() const;
67  bool ok() const;
68 
69  virtual std::string get_id() const;
70  virtual std::string get_name() const;
71  virtual std::string get_engine() const;
72 
73  bool redeploy(const config &cfg);
74 
75 
76 protected:
77  void unrecognized();
79  bool ok_;
80 
81 };
82 
83 
84 class target_unit_goal : public goal {
85 public:
86  target_unit_goal(readonly_context &context, const config &cfg);
87 
88 
89  virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
90 
91 
92  virtual void on_create();
93 
94 private:
95  double value() const
96  {
97  return value_;
98  }
99  double value_;
100 };
101 
102 
103 class target_location_goal : public goal {
104 public:
105  target_location_goal(readonly_context &context, const config &cfg);
106 
107 
108  virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
109 
110 
111  virtual void on_create();
112 
113 private:
114  double value() const
115  {
116  return value_;
117  }
118  std::shared_ptr<terrain_filter> filter_ptr_;
119  double value_;
120 };
121 
122 
123 class protect_goal : public goal {
124 public:
125  protect_goal(readonly_context &context, const config &cfg, bool protect_unit);
126 
127 
128  virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
129 
130 
131  virtual void on_create();
132 
133 private:
134 
135  double value()
136  {
137  return value_;
138  }
139 
140  std::shared_ptr<terrain_filter> filter_ptr_;
142  int radius_;
143  double value_;
144 };
145 
146 
148 public:
150  : protect_goal(context,cfg,false)
151  {
152  }
153 };
154 
155 
157 public:
159  : protect_goal(context,cfg,true)
160  {
161  }
162 };
163 
164 class lua_goal : public goal {
165 public:
166  lua_goal(readonly_context& context, const config& cfg);
167  virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
168  void on_create(std::shared_ptr<ai::lua_ai_context>);
169 
170 private:
172  std::shared_ptr<lua_ai_action_handler> handler_;
173 };
174 
175 
177  bool is_duplicate(const std::string &name);
178 public:
179  typedef std::shared_ptr< goal_factory > factory_ptr;
180  typedef std::map<std::string, factory_ptr> factory_map;
181  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
182 
183  static factory_map& get_list() {
184  static factory_map *goal_factories;
185  if (goal_factories==nullptr) {
186  goal_factories = new factory_map;
187  }
188  return *goal_factories;
189  }
190 
191  virtual goal_ptr get_new_instance( readonly_context &context, const config &cfg ) = 0;
192 
193  goal_factory( const std::string &name )
194  {
195  if (is_duplicate(name)) {
196  return;
197  }
198  factory_ptr ptr_to_this(this);
199  get_list().emplace(name,ptr_to_this);
200  }
201 
202  virtual ~goal_factory() {}
203 };
204 
205 
206 template<class GOAL>
208 public:
210  : goal_factory( name )
211  {
212  }
213 
214  virtual goal_ptr get_new_instance( readonly_context &context, const config &cfg ){
215  goal_ptr a(new GOAL(context,cfg));
216  a->on_create();
217  return a;
218  }
219 };
220 
221 
222 } //end of namespace ai
223 
224 
225 #ifdef _MSC_VER
226 #pragma warning(pop)
227 #endif
virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list)
Definition: goal.cpp:138
virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list)
Definition: goal.cpp:238
std::vector< char_t > string
protect_location_goal(readonly_context &context, const config &cfg)
Definition: goal.hpp:149
#define a
target_location_goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:202
static factory_map & get_list()
Definition: goal.hpp:183
goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:48
virtual void on_create()
Definition: goal.cpp:167
goal_factory(const std::string &name)
Definition: goal.hpp:193
config cfg_
Definition: goal.hpp:78
std::shared_ptr< lua_ai_action_handler > handler_
Definition: goal.hpp:172
std::map< std::string, factory_ptr > factory_map
Definition: goal.hpp:180
Definitions for the interface to Wesnoth Markup Language (WML).
virtual goal_ptr get_new_instance(readonly_context &context, const config &cfg)
Definition: goal.hpp:214
virtual std::string get_engine() const
Definition: goal.cpp:99
std::shared_ptr< terrain_filter > filter_ptr_
Definition: goal.hpp:140
virtual void on_create()
Definition: goal.cpp:211
double value_
Definition: goal.hpp:143
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
virtual void on_create()
Definition: goal.cpp:56
bool ok_
Definition: goal.hpp:79
virtual ~goal_factory()
Definition: goal.hpp:202
register_goal_factory(const std::string &name)
Definition: goal.hpp:209
bool active() const
Definition: goal.cpp:119
A component of the AI framework.
void unrecognized()
Definition: goal.cpp:66
virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list)
Definition: goal.cpp:184
bool protect_unit_
Definition: goal.hpp:141
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:108
virtual goal_ptr get_new_instance(readonly_context &context, const config &cfg)=0
lua_goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:307
double value()
Definition: goal.hpp:135
virtual config to_config() const
Definition: goal.cpp:84
protect_unit_goal(readonly_context &context, const config &cfg)
Definition: goal.hpp:158
virtual std::string get_id() const
Definition: goal.cpp:89
bool redeploy(const config &cfg)
Definition: goal.cpp:105
Game information for the AI.
std::shared_ptr< terrain_filter > filter_ptr_
Definition: goal.hpp:118
std::string code_
Definition: goal.hpp:171
virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list)
Definition: goal.cpp:78
Default AI contexts.
virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list)
Definition: goal.cpp:326
virtual std::string get_name() const
Definition: goal.cpp:94
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: goal.hpp:181
protect_goal(readonly_context &context, const config &cfg, bool protect_unit)
Definition: goal.cpp:298
double value() const
Definition: goal.hpp:114
virtual void on_create()
Definition: goal.cpp:125
target_unit_goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:160
bool is_duplicate(const std::string &name)
Definition: goal.cpp:346
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
double value() const
Definition: goal.hpp:95
bool ok() const
Definition: goal.cpp:113
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
virtual ~goal()
Definition: goal.cpp:73
std::shared_ptr< goal_factory > factory_ptr
Definition: goal.hpp:179