The Battle for Wesnoth  1.17.0-dev
goal.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2021
3  by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  */
19 
20 #pragma once
21 
23 
24 #include "ai/default/contexts.hpp"
25 #include "ai/game_info.hpp"
26 #include "config.hpp"
27 
28 #include <iterator>
29 #include <map>
30 #include <set>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 class terrain_filter;
36 namespace ai { class lua_ai_action_handler; }
37 namespace ai { class lua_ai_context; }
38 namespace ai { struct target; }
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  virtual ~goal();
47 
48  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
49 
50  virtual config to_config() const;
51 
52  virtual void on_create();
53  virtual void on_create(std::shared_ptr<ai::lua_ai_context>);
54 
55  bool active() const;
56  bool ok() const;
57 
58  virtual std::string get_id() const;
59  virtual std::string get_name() const;
60  virtual std::string get_engine() const;
61 
62  bool redeploy(const config &cfg);
63 
64 protected:
65  void unrecognized();
67  bool ok_;
68 
69 };
70 
71 class target_unit_goal : public goal {
72 public:
73  target_unit_goal(readonly_context &context, const config &cfg);
74 
75  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
76 
77  virtual void on_create();
78 
79 private:
80  double value() const
81  {
82  return value_;
83  }
84  double value_;
85 };
86 
87 class target_location_goal : public goal {
88 public:
89  target_location_goal(readonly_context &context, const config &cfg);
90 
91  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
92 
93  virtual void on_create();
94 
95 private:
96  double value() const
97  {
98  return value_;
99  }
100  std::shared_ptr<terrain_filter> filter_ptr_;
101  double value_;
102 };
103 
104 class protect_goal : public goal {
105 public:
106  protect_goal(readonly_context &context, const config &cfg, bool protect_unit);
107 
108  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
109 
110  virtual void on_create();
111 
112 private:
113 
114  double value()
115  {
116  return value_;
117  }
118 
119  std::shared_ptr<terrain_filter> filter_ptr_;
121  int radius_;
122  double value_;
123 };
124 
126 public:
128  : protect_goal(context,cfg,false)
129  {
130  }
131 };
132 
134 public:
136  : protect_goal(context,cfg,true)
137  {
138  }
139 };
140 
141 class lua_goal : public goal {
142 public:
143  lua_goal(readonly_context& context, const config& cfg);
144  virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list);
145  void on_create(std::shared_ptr<ai::lua_ai_context>);
146 
147 private:
148  std::string code_;
149  std::shared_ptr<lua_ai_action_handler> handler_;
150 };
151 
153  bool is_duplicate(const std::string &name);
154 public:
155  typedef std::shared_ptr< goal_factory > factory_ptr;
156  typedef std::map<std::string, factory_ptr> factory_map;
157  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
158 
159  static factory_map& get_list() {
160  static factory_map *goal_factories;
161  if (goal_factories==nullptr) {
162  goal_factories = new factory_map;
163  }
164  return *goal_factories;
165  }
166 
167  virtual goal_ptr get_new_instance( readonly_context &context, const config &cfg ) = 0;
168 
169  goal_factory( const std::string &name )
170  {
171  if (is_duplicate(name)) {
172  return;
173  }
174  factory_ptr ptr_to_this(this);
175  get_list().emplace(name,ptr_to_this);
176  }
177 
178  virtual ~goal_factory() {}
179 };
180 
181 template<class GOAL>
183 public:
184  register_goal_factory( const std::string &name )
185  : goal_factory( name )
186  {
187  }
188 
189  virtual goal_ptr get_new_instance( readonly_context &context, const config &cfg ){
190  goal_ptr a = std::make_shared<GOAL>(context, cfg);
191  a->on_create();
192  return a;
193  }
194 };
195 
196 } //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:127
#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:159
goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:47
goal_factory(const std::string &name)
Definition: goal.hpp:169
config cfg_
Definition: goal.hpp:66
std::shared_ptr< lua_ai_action_handler > handler_
Definition: goal.hpp:149
double value() const
Definition: goal.hpp:96
std::map< std::string, factory_ptr > factory_map
Definition: goal.hpp:156
Definitions for the interface to Wesnoth Markup Language (WML).
virtual goal_ptr get_new_instance(readonly_context &context, const config &cfg)
Definition: goal.hpp:189
std::shared_ptr< terrain_filter > filter_ptr_
Definition: goal.hpp:119
double value_
Definition: goal.hpp:122
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
virtual void on_create()
Definition: goal.cpp:53
bool ok_
Definition: goal.hpp:67
virtual std::string get_engine() const
Definition: goal.cpp:92
virtual ~goal_factory()
Definition: goal.hpp:178
register_goal_factory(const std::string &name)
Definition: goal.hpp:184
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:100
A component of the AI framework.
void unrecognized()
Definition: goal.cpp:63
bool protect_unit_
Definition: goal.hpp:120
double value()
Definition: goal.hpp:114
protect_unit_goal(readonly_context &context, const config &cfg)
Definition: goal.hpp:135
bool redeploy(const config &cfg)
Definition: goal.cpp:97
Game information for the AI.
std::shared_ptr< terrain_filter > filter_ptr_
Definition: goal.hpp:100
std::string code_
Definition: goal.hpp:148
double value() const
Definition: goal.hpp:80
Default AI contexts.
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: goal.hpp:157
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:61
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:155