The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
callable_objects.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Bartosz Waresiak <[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 #ifndef FORMULA_AI_CALLABLE_OBJECTS_HPP_INCLUDED
16 #define FORMULA_AI_CALLABLE_OBJECTS_HPP_INCLUDED
17 
18 #include "ai/game_info.hpp"
19 #include "actions/attack.hpp"
20 #include "formula/callable_objects.hpp"
21 #include "formula/formula.hpp"
22 #include "formula/callable.hpp"
23 
24 namespace ai {
25  class formula_ai;
26 }
27 
28 namespace game_logic {
29 
31 public:
32  typedef std::multimap<map_location, map_location> move_map;
34  : units_(units), ai_(ai)
35  {}
36 private:
37  const unit_map& units_;
39 
40  variant get_value(const std::string& key) const;
41 
42  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
43 
44  /* add to vars all attacks on enemy units around <attack_position> tile. attacker_location is tile where unit is currently standing. It's moved to attack_position first and then performs attack.*/
45  void collect_possible_attacks(std::vector<variant>& vars, map_location attacker_location, map_location attack_position) const;
46 };
47 
51  variant get_value(const std::string& key) const;
52 
53  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
54 public:
56  const map_location& src, const map_location& dst, int weapon);
57 
58  const map_location& move_from() const { return move_from_; }
59  const map_location& src() const { return src_; }
60  const map_location& dst() const { return dst_; }
61  int weapon() const { return bc_.get_attacker_stats().attack_num; }
62  int defender_weapon() const { return bc_.get_defender_stats().attack_num; }
63 
64  /** Compare two attacks in deterministic way or compare pointers
65  * (nondeterministic in consequent game runs) if method argument is not
66  * attack_callable */
67  int do_compare(const game_logic::formula_callable* callable) const;
68 };
69 
70 
73  variant get_value(const std::string& key) const {
74  if(key == "src") {
75  return variant(new location_callable(src_));
76  } else if(key == "dst") {
77  return variant(new location_callable(dst_));
78  } else {
79  return variant();
80  }
81  }
82  void get_inputs(std::vector<game_logic::formula_input>* inputs) const {
85  }
86 
87  int do_compare(const formula_callable* callable) const;
88 public:
90  src_(src), dst_(dst)
91  {
92  type_ = MOVE_C;
93  }
94 
95  const map_location& src() const { return src_; }
96  const map_location& dst() const { return dst_; }
97 };
98 
99 
102  variant get_value(const std::string& key) const {
103  if(key == "src") {
104  return variant(new location_callable(src_));
105  } else if(key == "dst") {
106  return variant(new location_callable(dst_));
107  } else {
108  return variant();
109  }
110  }
111  void get_inputs(std::vector<game_logic::formula_input>* inputs) const {
112  inputs->push_back(game_logic::formula_input("src", game_logic::FORMULA_READ_ONLY));
113  inputs->push_back(game_logic::formula_input("dst", game_logic::FORMULA_READ_ONLY));
114  }
115 
116  int do_compare(const formula_callable* callable) const;
117 public:
119  src_(src), dst_(dst)
120  {
122  }
123 
124  const map_location& src() const { return src_; }
125  const map_location& dst() const { return dst_; }
126 };
127 
128 
129 
133 
134  variant get_value(const std::string& key) const;
135 
136  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
137 public:
139  : loc_(loc), id_(id)
140  {}
141 
142  const map_location& loc() const { return loc_; }
143  const std::string& id() const { return id_; }
144 };
145 
146 
150 
151  variant get_value(const std::string& key) const;
152 
153  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
154 public:
156  : loc_(loc), type_(type)
157  {}
158 
159  const map_location& loc() const { return loc_; }
160  const std::string& type() const { return type_; }
161 };
162 
163 
167  variant get_value(const std::string& key) const;
168 
169  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
170 public:
172  : key_(key), value_(value)
173  {}
174 
175  const std::string& key() const { return key_; }
176  variant value() const { return value_; }
177 };
178 
179 
184  variant get_value(const std::string& key) const;
185 
186  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
187 public:
189  : key_(key), value_(value), loc_(loc)
190  {}
191 
192  const std::string& key() const { return key_; }
193  variant value() const { return value_; }
194  const map_location loc() const { return loc_; }
195 };
196 
198  variant get_value(const std::string& /*key*/) const { return variant(); }
199 public:
200  explicit fallback_callable() {
201  }
202 };
203 
208  variant get_value(const std::string& key) const;
209 
210  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
211 public:
213  : main_(main)
214  , backup_()
215  , backup_formula_(backup)
216  {}
217 
218  const variant& get_main() const { return main_; }
219  const expression_ptr& get_backup() const { return backup_formula_; }
220 
221  void set_backup_result(const variant& v) {
222  backup_ = v;
223  }
224 };
225 
226 
230  const int status_;
231 
232  variant get_value(const std::string& key) const;
233 
234  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
235 
236 public:
237  safe_call_result(const formula_callable* callable, int status,
238  const map_location& loc = map_location() )
239  : failed_callable_(callable), current_unit_location_(loc), status_(status)
240  {}
241 };
242 
243 
245  typedef std::multimap<map_location, map_location> move_map;
246  const move_map& srcdst_;
247  const move_map& dstsrc_;
248  const unit_map& units_;
249 
250  variant get_value(const std::string& key) const;
251  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
252 public:
253  move_map_callable(const move_map& srcdst, const move_map& dstsrc, const unit_map& units)
254  : srcdst_(srcdst), dstsrc_(dstsrc), units_(units)
255  {
256  type_ = MOVE_MAP_C;
257  }
258 
259  const move_map& srcdst() const { return srcdst_; }
260  const move_map& dstsrc() const { return dstsrc_; }
261 };
262 
264  //unit_map units_;
265  int chance_;
266  variant get_value(const std::string& key) const;
267 
268  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
269 public:
270  position_callable(/*unit_map* units,*/ int chance) :
271  //units_(),
272  chance_(chance)
273  {
274  //units->swap(units_);
275  }
276 
279  srcdst(),
280  dstsrc(),
281  full_srcdst(),
282  full_dstsrc(),
283  enemy_srcdst(),
284  enemy_dstsrc(),
285  attacks_cache()
286  {
287  }
288 
291  };
292 };
293 
294 
296  std::vector<variant> hitLeft_, prob_, status_;
297  variant get_value(const std::string& key) const;
298 
299  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
300 public:
301  outcome_callable( const std::vector<variant>& hitLeft,
302  const std::vector<variant>& prob,
303  const std::vector<variant>& status)
304  : hitLeft_(hitLeft), prob_(prob), status_(status)
305  {
306  }
307 
308  const std::vector<variant>& hitLeft() const { return hitLeft_; }
309  const std::vector<variant>& prob() const { return prob_; }
310  const std::vector<variant>& status() const { return status_; }
311 };
312 
313 }
314 
315 #endif /* FORMULA_AI_CALLABLE_OBJECTS_HPP_INCLUDED */
316 
const expression_ptr & get_backup() const
variant get_value(const std::string &key) const
outcome_callable(const std::vector< variant > &hitLeft, const std::vector< variant > &prob, const std::vector< variant > &status)
const formula_callable * failed_callable_
const std::string & type() const
variant get_value(const std::string &key) const
variant get_value(const std::string &key) const
variant get_value(const std::string &key) const
int do_compare(const formula_callable *callable) const
variant get_value(const std::string &key) const
Various functions that implement attacks and attack calculations.
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
move_map_callable(const move_map &srcdst, const move_map &dstsrc, const unit_map &units)
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
variant get_value(const std::string &key) const
recruit_callable(const map_location &loc, const std::string &type)
std::vector< variant > prob_
const variant & get_main() const
variant get_value(const std::string &key) const
const std::string & key() const
attack_callable(const map_location &move_from, const map_location &src, const map_location &dst, int weapon)
move_partial_callable(const map_location &src, const map_location &dst)
GLenum src
Definition: glew.h:2392
set_unit_var_callable(const std::string &key, const variant &value, const map_location &loc)
const map_location & dst() const
const map_location & dst() const
attack_map_callable(const ai::formula_ai &ai, const unit_map &units)
const map_location loc() const
safe_call_callable(const variant &main, const expression_ptr &backup)
const std::vector< variant > & prob() const
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:47
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
const GLdouble * v
Definition: glew.h:1359
GLsizei const GLfloat * value
Definition: glew.h:1817
GLenum GLenum dst
Definition: glew.h:2392
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
const battle_context_unit_stats & get_defender_stats() const
This method returns the statistics of the defender.
Definition: attack.hpp:163
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:135
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
int attack_num
Index into unit->attacks() or -1 for none.
Definition: attack.hpp:52
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
const map_location & loc() const
const map_location & move_from() const
std::vector< formula_input > inputs() const
Definition: callable.hpp:50
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
variant get_value(const std::string &key) const
Encapsulates the map of the game.
Definition: location.hpp:38
const map_location & dst() const
recall_callable(const map_location &loc, const std::string &id)
const map_location current_unit_location_
const std::string & id() const
Game information for the AI.
std::multimap< map_location, map_location > move_map
void collect_possible_attacks(std::vector< variant > &vars, map_location attacker_location, map_location attack_position) const
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
variant get_value(const std::string &key) const
void set_backup_result(const variant &v)
variant get_value(const std::string &key) const
variant get_value(const std::string &) const
int main(int argc, char **argv)
const map_location & src() const
int do_compare(const formula_callable *callable) const
const move_map & dstsrc() const
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
const std::vector< variant > & hitLeft() const
variant get_value(const std::string &key) const
variant get_value(const std::string &key) const
Container associating units to locations.
Definition: map.hpp:90
const std::string & key() const
variant get_value(const std::string &key) const
int do_compare(const game_logic::formula_callable *callable) const
Compare two attacks in deterministic way or compare pointers (nondeterministic in consequent game run...
safe_call_result(const formula_callable *callable, int status, const map_location &loc=map_location())
const battle_context_unit_stats & get_attacker_stats() const
This method returns the statistics of the attacker.
Definition: attack.hpp:160
const move_map & srcdst() const
const map_location & src() const
const map_location & loc() const
move_callable(const map_location &src, const map_location &dst)
set_var_callable(const std::string &key, const variant &value)
const map_location & src() const
std::vector< variant > status_
std::multimap< map_location, map_location > move_map
std::vector< variant > hitLeft_
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
unit_map * units
Definition: resources.cpp:35
const std::vector< variant > & status() const