The Battle for Wesnoth  1.17.21+dev
callable_objects.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2023
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 
51  variant get_value(const std::string& key) const override;
52 
53  void get_inputs(formula_input_vector& inputs) const override;
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; }
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:
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  {
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;
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 
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)
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 }
Various functions that implement attacks and attack calculations.
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:168
const battle_context_unit_stats & get_defender_stats() const
This method returns the statistics of the defender.
Definition: attack.hpp:200
const battle_context_unit_stats & get_attacker_stats() const
This method returns the statistics of the attacker.
Definition: attack.hpp:194
Container associating units to locations.
Definition: map.hpp:99
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:61
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:44
std::vector< formula_input > formula_input_vector
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