The Battle for Wesnoth  1.17.0-dev
callable_objects.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2021
3  by Bartosz Waresiak <dragonking@o2.pl>
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 #pragma once
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 wfl {
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 override;
41 
42  void get_inputs(formula_input_vector& inputs) const override;
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 
49  map_location move_from_, src_, dst_;
51  variant get_value(const std::string& key) const override;
52 
53  void get_inputs(formula_input_vector& inputs) const override;
54 public:
55  attack_callable(const map_location& move_from,
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 formula_callable* callable) const override;
68  variant execute_self(variant ctxt) override;
69 };
70 
73  variant get_value(const std::string& key) const override {
74  if(key == "src") {
75  return variant(std::make_shared<location_callable>(src_));
76  } else if(key == "dst") {
77  return variant(std::make_shared<location_callable>(dst_));
78  } else {
79  return variant();
80  }
81  }
82  void get_inputs(formula_input_vector& inputs) const override {
83  add_input(inputs, "src");
84  add_input(inputs, "dst");
85  }
86 
87  int do_compare(const formula_callable* callable) const override;
88 public:
89  move_callable(const map_location& src, const map_location& dst) :
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  variant execute_self(variant ctxt) override;
98 };
99 
102  variant get_value(const std::string& key) const override {
103  if(key == "src") {
104  return variant(std::make_shared<location_callable>(src_));
105  } else if(key == "dst") {
106  return variant(std::make_shared<location_callable>(dst_));
107  } else {
108  return variant();
109  }
110  }
111  void get_inputs(formula_input_vector& inputs) const override {
112  add_input(inputs, "src");
113  add_input(inputs, "dst");
114  }
115 
116  int do_compare(const formula_callable* callable) const override;
117 public:
119  src_(src), dst_(dst)
120  {
121  type_ = MOVE_PARTIAL_C;
122  }
123 
124  const map_location& src() const { return src_; }
125  const map_location& dst() const { return dst_; }
126  variant execute_self(variant ctxt) override;
127 };
128 
131  std::string id_;
132 
133  variant get_value(const std::string& key) const override;
134 
135  void get_inputs(formula_input_vector& inputs) const override;
136 public:
137  recall_callable(const map_location& loc, const std::string& id)
138  : loc_(loc), id_(id)
139  {}
140 
141  const map_location& loc() const { return loc_; }
142  const std::string& id() const { return id_; }
143  variant execute_self(variant ctxt) override;
144 };
145 
148  std::string type_;
149 
150  variant get_value(const std::string& key) const override;
151 
152  void get_inputs(formula_input_vector& inputs) const override;
153 public:
154  recruit_callable(const map_location& loc, const std::string& type)
155  : loc_(loc), type_(type)
156  {}
157 
158  const map_location& loc() const { return loc_; }
159  const std::string& type() const { return type_; }
160  variant execute_self(variant ctxt) override;
161 };
162 
164  std::string key_;
167  variant get_value(const std::string& key) const override;
168 
169  void get_inputs(formula_input_vector& inputs) const override;
170 public:
171  set_unit_var_callable(const std::string& key, const variant& value, const map_location& loc)
172  : key_(key), value_(value), loc_(loc)
173  {}
174 
175  const std::string& key() const { return key_; }
176  variant value() const { return value_; }
177  const map_location loc() const { return loc_; }
178  variant execute_self(variant ctxt) override;
179 };
180 
182  variant get_value(const std::string& /*key*/) const override { return variant(); }
183 public:
184  explicit fallback_callable() {
185  }
186  variant execute_self(variant ctxt) override;
187 };
188 
190  typedef std::multimap<map_location, map_location> move_map;
191  const move_map& srcdst_;
192  const move_map& dstsrc_;
193  const unit_map& units_;
194 
195  variant get_value(const std::string& key) const override;
196  void get_inputs(formula_input_vector& inputs) const override;
197 public:
198  move_map_callable(const move_map& srcdst, const move_map& dstsrc, const unit_map& units)
199  : srcdst_(srcdst), dstsrc_(dstsrc), units_(units)
200  {
201  type_ = MOVE_MAP_C;
202  }
203 
204  const move_map& srcdst() const { return srcdst_; }
205  const move_map& dstsrc() const { return dstsrc_; }
206 };
207 
209  //unit_map units_;
210  int chance_;
211  variant get_value(const std::string& key) const override;
212 
213  void get_inputs(formula_input_vector& inputs) const override;
214 public:
215  position_callable(/*unit_map* units,*/ int chance) :
216  //units_(),
217  chance_(chance)
218  {
219  //units->swap(units_);
220  }
221 
224  srcdst(),
225  dstsrc(),
226  full_srcdst(),
227  full_dstsrc(),
228  enemy_srcdst(),
229  enemy_dstsrc(),
230  attacks_cache()
231  {
232  }
233 
234  ai::move_map srcdst, dstsrc, full_srcdst, full_dstsrc, enemy_srcdst, enemy_dstsrc;
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 }
recruit_callable(const map_location &loc, const std::string &type)
recall_callable(const map_location &loc, const std::string &id)
const battle_context_unit_stats & get_defender_stats() const
This method returns the statistics of the defender.
Definition: attack.hpp:206
Various functions that implement attacks and attack calculations.
attack_map_callable(const ai::formula_ai &ai, const unit_map &units)
const map_location & src() const
std::vector< formula_input > formula_input_vector
const map_location & dst() const
const ai::formula_ai & ai_
void get_inputs(formula_input_vector &inputs) const override
const std::vector< variant > & hitLeft() const
formula_ai & ai_
map_location loc_
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:43
std::multimap< map_location, map_location > move_map
const std::vector< variant > & prob() const
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
const move_map & srcdst() const
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:173
const move_map & dstsrc() const
const std::string & type() const
int attack_num
Index into unit->attacks() or -1 for none.
Definition: attack.hpp:54
Encapsulates the map of the game.
Definition: location.hpp:38
const map_location loc() const
variant get_value(const std::string &) const override
const std::vector< variant > & status() const
const map_location & dst() const
variant get_value(const std::string &key) const override
Game information for the AI.
outcome_callable(const std::vector< variant > &hitLeft, const std::vector< variant > &prob, const std::vector< variant > &status)
const std::string & id() const
move_map_callable(const move_map &srcdst, const move_map &dstsrc, const unit_map &units)
const map_location & move_from() const
move_partial_callable(const map_location &src, const map_location &dst)
void get_inputs(formula_input_vector &inputs) const override
const map_location & loc() const
Definition: contexts.hpp:44
Container associating units to locations.
Definition: map.hpp:98
const map_location & loc() const
const map_location & dst() const
const map_location & src() const
const map_location & src() const
const battle_context_unit_stats & get_attacker_stats() const
This method returns the statistics of the attacker.
Definition: attack.hpp:200
set_unit_var_callable(const std::string &key, const variant &value, const map_location &loc)
const std::string & key() const
std::vector< variant > status_