The Battle for Wesnoth  1.13.10+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 - 2017 by Bartosz Waresiak <dragonking@o2.pl>
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 #pragma once
16 
17 #include "ai/game_info.hpp"
18 #include "actions/attack.hpp"
19 #include "formula/callable_objects.hpp"
20 #include "formula/formula.hpp"
21 #include "formula/callable.hpp"
22 
23 namespace ai {
24  class formula_ai;
25 }
26 
27 namespace wfl {
28 
30 public:
31  typedef std::multimap<map_location, map_location> move_map;
33  : units_(units), ai_(ai)
34  {}
35 private:
36  const unit_map& units_;
38 
39  variant get_value(const std::string& key) const override;
40 
41  void get_inputs(formula_input_vector& inputs) const override;
42 
43  /* 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.*/
44  void collect_possible_attacks(std::vector<variant>& vars, map_location attacker_location, map_location attack_position) const;
45 };
46 
50  variant get_value(const std::string& key) const override;
51 
52  void get_inputs(formula_input_vector& inputs) const override;
53 public:
55  const map_location& src, const map_location& dst, int weapon);
56 
57  const map_location& move_from() const { return move_from_; }
58  const map_location& src() const { return src_; }
59  const map_location& dst() const { return dst_; }
60  int weapon() const { return bc_.get_attacker_stats().attack_num; }
61  int defender_weapon() const { return bc_.get_defender_stats().attack_num; }
62 
63  /** Compare two attacks in deterministic way or compare pointers
64  * (nondeterministic in consequent game runs) if method argument is not
65  * attack_callable */
66  int do_compare(const formula_callable* callable) const override;
67  variant execute_self(variant ctxt) override;
68 };
69 
72  variant get_value(const std::string& key) const override {
73  if(key == "src") {
74  return variant(std::make_shared<location_callable>(src_));
75  } else if(key == "dst") {
76  return variant(std::make_shared<location_callable>(dst_));
77  } else {
78  return variant();
79  }
80  }
81  void get_inputs(formula_input_vector& inputs) const override {
82  add_input(inputs, "src");
83  add_input(inputs, "dst");
84  }
85 
86  int do_compare(const formula_callable* callable) const override;
87 public:
89  src_(src), dst_(dst)
90  {
91  type_ = MOVE_C;
92  }
93 
94  const map_location& src() const { return src_; }
95  const map_location& dst() const { return dst_; }
96  variant execute_self(variant ctxt) override;
97 };
98 
101  variant get_value(const std::string& key) const override {
102  if(key == "src") {
103  return variant(std::make_shared<location_callable>(src_));
104  } else if(key == "dst") {
105  return variant(std::make_shared<location_callable>(dst_));
106  } else {
107  return variant();
108  }
109  }
110  void get_inputs(formula_input_vector& inputs) const override {
111  add_input(inputs, "src");
112  add_input(inputs, "dst");
113  }
114 
115  int do_compare(const formula_callable* callable) const override;
116 public:
118  src_(src), dst_(dst)
119  {
121  }
122 
123  const map_location& src() const { return src_; }
124  const map_location& dst() const { return dst_; }
125  variant execute_self(variant ctxt) override;
126 };
127 
131 
132  variant get_value(const std::string& key) const override;
133 
134  void get_inputs(formula_input_vector& inputs) const override;
135 public:
137  : loc_(loc), id_(id)
138  {}
139 
140  const map_location& loc() const { return loc_; }
141  const std::string& id() const { return id_; }
142  variant execute_self(variant ctxt) override;
143 };
144 
148 
149  variant get_value(const std::string& key) const override;
150 
151  void get_inputs(formula_input_vector& inputs) const override;
152 public:
154  : loc_(loc), type_(type)
155  {}
156 
157  const map_location& loc() const { return loc_; }
158  const std::string& type() const { return type_; }
159  variant execute_self(variant ctxt) override;
160 };
161 
166  variant get_value(const std::string& key) const override;
167 
168  void get_inputs(formula_input_vector& inputs) const override;
169 public:
171  : key_(key), value_(value), loc_(loc)
172  {}
173 
174  const std::string& key() const { return key_; }
175  variant value() const { return value_; }
176  const map_location loc() const { return loc_; }
177  variant execute_self(variant ctxt) override;
178 };
179 
181  variant get_value(const std::string& /*key*/) const override { return variant(); }
182 public:
183  explicit fallback_callable() {
184  }
185  variant execute_self(variant ctxt) override;
186 };
187 
189  typedef std::multimap<map_location, map_location> move_map;
190  const move_map& srcdst_;
191  const move_map& dstsrc_;
192  const unit_map& units_;
193 
194  variant get_value(const std::string& key) const override;
195  void get_inputs(formula_input_vector& inputs) const override;
196 public:
197  move_map_callable(const move_map& srcdst, const move_map& dstsrc, const unit_map& units)
198  : srcdst_(srcdst), dstsrc_(dstsrc), units_(units)
199  {
200  type_ = MOVE_MAP_C;
201  }
202 
203  const move_map& srcdst() const { return srcdst_; }
204  const move_map& dstsrc() const { return dstsrc_; }
205 };
206 
208  //unit_map units_;
209  int chance_;
210  variant get_value(const std::string& key) const override;
211 
212  void get_inputs(formula_input_vector& inputs) const override;
213 public:
214  position_callable(/*unit_map* units,*/ int chance) :
215  //units_(),
216  chance_(chance)
217  {
218  //units->swap(units_);
219  }
220 
223  srcdst(),
224  dstsrc(),
225  full_srcdst(),
226  full_dstsrc(),
227  enemy_srcdst(),
228  enemy_dstsrc(),
229  attacks_cache()
230  {
231  }
232 
235  };
236 };
237 
238 
240  std::vector<variant> hitLeft_, prob_, status_;
241  variant get_value(const std::string& key) const override;
242 
243  void get_inputs(formula_input_vector& inputs) const override;
244 public:
245  outcome_callable( const std::vector<variant>& hitLeft,
246  const std::vector<variant>& prob,
247  const std::vector<variant>& status)
248  : hitLeft_(hitLeft), prob_(prob), status_(status)
249  {
250  }
251 
252  const std::vector<variant>& hitLeft() const { return hitLeft_; }
253  const std::vector<variant>& prob() const { return prob_; }
254  const std::vector<variant>& status() const { return status_; }
255 };
256 
257 }
int do_compare(const formula_callable *callable) const override
Compare two attacks in deterministic way or compare pointers (nondeterministic in consequent game run...
const std::string & id() const
variant execute_self(variant ctxt) override
variant get_value(const std::string &key) const override
recruit_callable(const map_location &loc, const std::string &type)
std::vector< char_t > string
const map_location & dst() const
recall_callable(const map_location &loc, const std::string &id)
const move_map & srcdst() const
const map_location & src() const
Various functions that implement attacks and attack calculations.
attack_map_callable(const ai::formula_ai &ai, const unit_map &units)
attack_callable(const map_location &move_from, const map_location &src, const map_location &dst, int weapon)
const map_location loc() const
const std::string & type() const
const map_location & dst() const
void get_inputs(formula_input_vector &inputs) const override
int defender_weapon() const
std::vector< formula_input > formula_input_vector
formula_input_vector inputs() const
Definition: callable.hpp:62
variant execute_self(variant ctxt) override
const ai::formula_ai & ai_
const map_location & src() const
void get_inputs(formula_input_vector &inputs) const override
variant get_value(const std::string &key) const override
std::multimap< map_location, map_location > move_map
void get_inputs(formula_input_vector &inputs) const override
std::multimap< map_location, map_location > move_map
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:39
const map_location & loc() const
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
const std::vector< variant > & prob() const
void get_inputs(formula_input_vector &inputs) const override
const map_location & loc() const
void get_inputs(formula_input_vector &inputs) const override
const battle_context_unit_stats & get_defender_stats() const
This method returns the statistics of the defender.
Definition: attack.hpp:202
variant get_value(const std::string &key) const override
move_callable(const map_location &src, const map_location &dst)
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:170
variant execute_self(variant ctxt) override
std::vector< variant > prob_
int attack_num
Index into unit->attacks() or -1 for none.
Definition: attack.hpp:50
variant execute_self(variant ctxt) override
variant get_value(const std::string &key) const override
Encapsulates the map of the game.
Definition: location.hpp:40
variant get_value(const std::string &) const override
void collect_possible_attacks(std::vector< variant > &vars, map_location attacker_location, map_location attack_position) const
void get_inputs(formula_input_vector &inputs) const override
variant get_value(const std::string &key) const override
variant get_value(const std::string &key) const override
Game information for the AI.
const std::string & key() const
variant get_value(const std::string &key) const override
void get_inputs(formula_input_vector &inputs) const override
outcome_callable(const std::vector< variant > &hitLeft, const std::vector< variant > &prob, const std::vector< variant > &status)
const std::vector< variant > & status() const
variant execute_self(variant ctxt) override
variant execute_self(variant ctxt) override
move_map_callable(const move_map &srcdst, const move_map &dstsrc, const unit_map &units)
int do_compare(const formula_callable *callable) const override
const map_location & src() const
variant get_value(const std::string &key) const override
move_partial_callable(const map_location &src, const map_location &dst)
void get_inputs(formula_input_vector &inputs) const override
void get_inputs(formula_input_vector &inputs) const override
Definition: contexts.hpp:42
std::vector< variant > hitLeft_
void get_inputs(formula_input_vector &inputs) const override
variant execute_self(variant ctxt) override
Container associating units to locations.
Definition: map.hpp:99
const map_location & dst() const
const battle_context_unit_stats & get_attacker_stats() const
This method returns the statistics of the attacker.
Definition: attack.hpp:196
int do_compare(const formula_callable *callable) const override
const std::vector< variant > & hitLeft() const
static void add_input(formula_input_vector &inputs, const std::string &key, FORMULA_ACCESS_TYPE access_type=FORMULA_READ_ONLY)
Definition: callable.hpp:135
const move_map & dstsrc() const
unit_map * units
Definition: resources.cpp:34
set_unit_var_callable(const std::string &key, const variant &value, const map_location &loc)
variant get_value(const std::string &key) const override
const map_location & move_from() const
variant get_value(const std::string &key) const override
std::vector< variant > status_