The Battle for Wesnoth  1.17.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 namespace ai {
40 
41 class goal : public readonly_context_proxy, public component {
42 public:
43  goal(readonly_context &context, const config &cfg);
44 
45  virtual ~goal();
46 
47  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
48 
49  virtual config to_config() const;
50 
51  virtual void on_create();
52  virtual void on_create(std::shared_ptr<ai::lua_ai_context>);
53 
54  bool active() const;
55  bool ok() const;
56 
57  virtual std::string get_id() const;
58  virtual std::string get_name() const;
59  virtual std::string get_engine() const;
60 
61  bool redeploy(const config &cfg);
62 
63 protected:
64  void unrecognized();
66  bool ok_;
67 
68 };
69 
70 class target_unit_goal : public goal {
71 public:
72  target_unit_goal(readonly_context &context, const config &cfg);
73 
74  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
75 
76  virtual void on_create();
77 
78 private:
79  double value() const
80  {
81  return value_;
82  }
83  double value_;
84 };
85 
86 class target_location_goal : public goal {
87 public:
88  target_location_goal(readonly_context &context, const config &cfg);
89 
90  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
91 
92  virtual void on_create();
93 
94 private:
95  double value() const
96  {
97  return value_;
98  }
99  std::shared_ptr<terrain_filter> filter_ptr_;
100  double value_;
101 };
102 
103 class protect_goal : public goal {
104 public:
105  protect_goal(readonly_context &context, const config &cfg, bool protect_unit);
106 
107  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
108 
109  virtual void on_create();
110 
111 private:
112 
113  double value()
114  {
115  return value_;
116  }
117 
118  std::shared_ptr<terrain_filter> filter_ptr_;
120  int radius_;
121  double value_;
122 };
123 
125 public:
127  : protect_goal(context,cfg,false)
128  {
129  }
130 };
131 
133 public:
135  : protect_goal(context,cfg,true)
136  {
137  }
138 };
139 
140 class lua_goal : public goal {
141 public:
142  lua_goal(readonly_context& context, const config& cfg);
143  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
144  void on_create(std::shared_ptr<ai::lua_ai_context>);
145 
146 private:
147  std::string code_;
148  std::shared_ptr<lua_ai_action_handler> handler_;
149 };
150 
152  bool is_duplicate(const std::string &name);
153 public:
154  typedef std::shared_ptr< goal_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 *goal_factories;
160  if (goal_factories==nullptr) {
161  goal_factories = new factory_map;
162  }
163  return *goal_factories;
164  }
165 
166  virtual goal_ptr get_new_instance( readonly_context &context, const config &cfg ) = 0;
167 
168  goal_factory( const std::string &name )
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 
177  virtual ~goal_factory() {}
178 };
179 
180 template<class GOAL>
182 public:
183  register_goal_factory( const std::string &name )
184  : goal_factory( name )
185  {
186  }
187 
188  virtual goal_ptr get_new_instance( readonly_context &context, const config &cfg ){
189  goal_ptr a = std::make_shared<GOAL>(context, cfg);
190  a->on_create();
191  return a;
192  }
193 };
194 
195 } //end of namespace ai
virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list)
Definition: goal.cpp:73
protect_location_goal(readonly_context &context, const config &cfg)
Definition: goal.hpp:126
#define a
bool active() const
Definition: goal.cpp:109
virtual std::string get_id() const
Definition: goal.cpp:82
static factory_map & get_list()
Definition: goal.hpp:158
goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:47
goal_factory(const std::string &name)
Definition: goal.hpp:168
config cfg_
Definition: goal.hpp:65
std::shared_ptr< lua_ai_action_handler > handler_
Definition: goal.hpp:148
double value() const
Definition: goal.hpp:95
std::map< std::string, factory_ptr > factory_map
Definition: goal.hpp:155
Definitions for the interface to Wesnoth Markup Language (WML).
virtual goal_ptr get_new_instance(readonly_context &context, const config &cfg)
Definition: goal.hpp:188
std::shared_ptr< terrain_filter > filter_ptr_
Definition: goal.hpp:118
double value_
Definition: goal.hpp:121
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:60
virtual void on_create()
Definition: goal.cpp:53
bool ok_
Definition: goal.hpp:66
virtual std::string get_engine() const
Definition: goal.cpp:92
virtual ~goal_factory()
Definition: goal.hpp:177
register_goal_factory(const std::string &name)
Definition: goal.hpp:183
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:99
A component of the AI framework.
void unrecognized()
Definition: goal.cpp:63
bool protect_unit_
Definition: goal.hpp:119
double value()
Definition: goal.hpp:113
protect_unit_goal(readonly_context &context, const config &cfg)
Definition: goal.hpp:134
bool redeploy(const config &cfg)
Definition: goal.cpp:97
Game information for the AI.
std::shared_ptr< terrain_filter > filter_ptr_
Definition: goal.hpp:99
std::string code_
Definition: goal.hpp:147
double value() const
Definition: goal.hpp:79
Default AI contexts.
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: goal.hpp:156
bool ok() const
Definition: goal.cpp:104
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
virtual std::string get_name() const
Definition: goal.cpp:87
virtual config to_config() const
Definition: goal.cpp:77
virtual ~goal()
Definition: goal.cpp:69
std::shared_ptr< goal_factory > factory_ptr
Definition: goal.hpp:154