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