The Battle for Wesnoth  1.19.7+dev
callable_objects.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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/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; }
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 
130  std::string id_;
131 
132  variant get_value(const std::string& key) const override;
133 
134  void get_inputs(formula_input_vector& inputs) const override;
135 public:
136  recall_callable(const map_location& loc, const std::string& id)
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 
147  std::string type_;
148 
149  variant get_value(const std::string& key) const override;
150 
151  void get_inputs(formula_input_vector& inputs) const override;
152 public:
153  recruit_callable(const map_location& loc, const std::string& type)
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 
163  std::string key_;
166  variant get_value(const std::string& key) const override;
167 
168  void get_inputs(formula_input_vector& inputs) const override;
169 public:
170  set_unit_var_callable(const std::string& key, const variant& value, const map_location& loc)
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;
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 
239  std::vector<variant> hitLeft_, prob_, status_;
240  variant get_value(const std::string& key) const override;
241 
242  void get_inputs(formula_input_vector& inputs) const override;
243 public:
244  outcome_callable( const std::vector<variant>& hitLeft,
245  const std::vector<variant>& prob,
246  const std::vector<variant>& status)
248  {
249  }
250 
251  const std::vector<variant>& hitLeft() const { return hitLeft_; }
252  const std::vector<variant>& prob() const { return prob_; }
253  const std::vector<variant>& status() const { return status_; }
254 };
255 
256 }
Various functions that implement attacks and attack calculations.
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:167
const battle_context_unit_stats & get_defender_stats() const
This method returns the statistics of the defender.
Definition: attack.hpp:199
const battle_context_unit_stats & get_attacker_stats() const
This method returns the statistics of the attacker.
Definition: attack.hpp:193
Container associating units to locations.
Definition: map.hpp:98
const map_location & src() const
const map_location & move_from() const
void get_inputs(formula_input_vector &inputs) const override
attack_callable(const map_location &move_from, const map_location &src, const map_location &dst, int weapon)
variant execute_self(variant ctxt) override
const map_location & dst() const
variant get_value(const std::string &key) const override
int do_compare(const formula_callable *callable) const override
Compare two attacks in deterministic way or compare pointers (nondeterministic in consequent game run...
variant get_value(const std::string &key) const override
const ai::formula_ai & 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(formula_input_vector &inputs) const override
attack_map_callable(const ai::formula_ai &ai, const unit_map &units)
variant execute_self(variant ctxt) override
variant get_value(const std::string &) const override
formula_input_vector inputs() const
Definition: callable.hpp:63
static void add_input(formula_input_vector &inputs, const std::string &key, formula_access access_type=formula_access::read_only)
Definition: callable.hpp:136
void get_inputs(formula_input_vector &inputs) const override
const map_location & dst() const
const map_location & src() const
move_callable(const map_location &src, const map_location &dst)
variant execute_self(variant ctxt) override
variant get_value(const std::string &key) const override
int do_compare(const formula_callable *callable) const override
move_map_callable(const move_map &srcdst, const move_map &dstsrc, const unit_map &units)
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
const move_map & dstsrc() const
const move_map & srcdst() const
variant get_value(const std::string &key) const override
void get_inputs(formula_input_vector &inputs) const override
int do_compare(const formula_callable *callable) const override
move_partial_callable(const map_location &src, const map_location &dst)
const map_location & src() const
variant execute_self(variant ctxt) override
const map_location & dst() const
outcome_callable(const std::vector< variant > &hitLeft, const std::vector< variant > &prob, const std::vector< variant > &status)
variant get_value(const std::string &key) const override
const std::vector< variant > & prob() const
const std::vector< variant > & hitLeft() const
std::vector< variant > hitLeft_
std::vector< variant > status_
std::vector< variant > prob_
void get_inputs(formula_input_vector &inputs) const override
const std::vector< variant > & status() const
void get_inputs(formula_input_vector &inputs) const override
variant get_value(const std::string &key) const override
const map_location & loc() const
void get_inputs(formula_input_vector &inputs) const override
variant execute_self(variant ctxt) override
const std::string & id() const
variant get_value(const std::string &key) const override
recall_callable(const map_location &loc, const std::string &id)
void get_inputs(formula_input_vector &inputs) const override
const std::string & type() const
recruit_callable(const map_location &loc, const std::string &type)
variant execute_self(variant ctxt) override
variant get_value(const std::string &key) const override
const map_location & loc() const
const std::string & key() const
set_unit_var_callable(const std::string &key, const variant &value, const map_location &loc)
void get_inputs(formula_input_vector &inputs) const override
variant execute_self(variant ctxt) override
const map_location loc() const
variant get_value(const std::string &key) const override
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::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:43
Definition: contexts.hpp:43
std::vector< formula_input > formula_input_vector
int attack_num
Index into unit->attacks() or -1 for none.
Definition: attack.hpp:53
Encapsulates the map of the game.
Definition: location.hpp:45