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