The Battle for Wesnoth  1.19.0-dev
goal.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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 <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 goal_ptr get_new_instance(readonly_context &context, const config &cfg)=0
std::shared_ptr< goal_factory > factory_ptr
Definition: goal.hpp:154
virtual ~goal_factory()
Definition: goal.hpp:177
std::map< std::string, factory_ptr > factory_map
Definition: goal.hpp:155
static factory_map & get_list()
Definition: goal.hpp:158
bool is_duplicate(const std::string &name)
Definition: goal.cpp:324
goal_factory(const std::string &name)
Definition: goal.hpp:168
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: goal.hpp:156
bool ok_
Definition: goal.hpp:66
bool redeploy(const config &cfg)
Definition: goal.cpp:95
virtual config to_config() const
Definition: goal.cpp:75
virtual ~goal()
Definition: goal.cpp:67
void unrecognized()
Definition: goal.cpp:61
virtual std::string get_engine() const
Definition: goal.cpp:90
virtual std::string get_name() const
Definition: goal.cpp:85
config cfg_
Definition: goal.hpp:65
virtual void on_create()
Definition: goal.cpp:51
virtual std::string get_id() const
Definition: goal.cpp:80
goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:45
bool active() const
Definition: goal.cpp:107
bool ok() const
Definition: goal.cpp:102
virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list)
Definition: goal.cpp:71
std::string code_
Definition: goal.hpp:147
lua_goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:289
std::shared_ptr< lua_ai_action_handler > handler_
Definition: goal.hpp:148
virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list)
Definition: goal.cpp:308
std::shared_ptr< terrain_filter > filter_ptr_
Definition: goal.hpp:118
bool protect_unit_
Definition: goal.hpp:119
protect_goal(readonly_context &context, const config &cfg, bool protect_unit)
Definition: goal.cpp:280
double value()
Definition: goal.hpp:113
virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list)
Definition: goal.cpp:218
virtual void on_create()
Definition: goal.cpp:193
double value_
Definition: goal.hpp:121
protect_location_goal(readonly_context &context, const config &cfg)
Definition: goal.hpp:126
protect_unit_goal(readonly_context &context, const config &cfg)
Definition: goal.hpp:134
register_goal_factory(const std::string &name)
Definition: goal.hpp:183
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:99
target_location_goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:186
virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list)
Definition: goal.cpp:168
double value() const
Definition: goal.hpp:95
virtual void on_create()
Definition: goal.cpp:151
virtual void on_create()
Definition: goal.cpp:112
virtual void add_targets(std::back_insert_iterator< std::vector< target >> target_list)
Definition: goal.cpp:125
double value() const
Definition: goal.hpp:79
target_unit_goal(readonly_context &context, const config &cfg)
Definition: goal.cpp:145
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A component of the AI framework.
Default AI contexts.
Game information for the AI.
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:100
#define a