The Battle for Wesnoth  1.17.14+dev
ai.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
3  by David White <dave@whitevine.net>
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 /**
17  * @file
18  * Defines formula ai
19  * */
20 
21 #pragma once
22 
23 #include "ai/contexts.hpp"
24 #include "ai/formula/function_table.hpp" // for ai_function_symbol_table
25 #include "ai/formula/callable_objects.hpp" // for position_callable, etc
26 #include "ai/formula/candidates.hpp" // for candidate_action_ptr, etc
27 #include "config.hpp" // for config
28 #include "formula/callable.hpp" // for formula_callable, etc
29 #include "formula/formula_fwd.hpp" // for const_formula_ptr, etc
30 #include "generic_event.hpp" // for observer
31 #include "pathfind/teleport.hpp" // for teleport_map
32 #include "units/map.hpp"
33 #include <set> // for multiset
34 #include <string> // for string
35 #include <utility> // for pair
36 #include <vector> // for vector
37 
38 namespace ai { class ai_context; }
39 namespace pathfind { struct plain_route; } // lines 57-57
40 struct map_location;
41 
42 namespace wfl {
43  struct formula_error;
44  class variant;
45 }
46 
47 namespace wfl {
48 
49 typedef std::pair< unit_map::unit_iterator, int> unit_formula_pair;
50 
52  bool operator() (const unit_formula_pair& left,
53  const unit_formula_pair& right) const
54  {
55  return left.second > right.second;
56  }
57 };
58 
59 typedef std::multiset< unit_formula_pair, unit_formula_compare > unit_formula_set;
60 
61 }
62 
63 namespace pathfind {
64 
65 struct plain_route;
66 
67 } // of namespace pathfind
68 
69 namespace ai {
70 
72 public:
73  formula_ai(const formula_ai&) = delete;
74  formula_ai& operator=(const formula_ai&) = delete;
75 
76  explicit formula_ai(readonly_context &context, const config &cfg);
77  virtual ~formula_ai() {}
78 
79  virtual config to_config() const;
80 
81  std::string evaluate(const std::string& formula_str);
82 
83  virtual void add_formula_function(const std::string& name, wfl::const_formula_ptr formula, wfl::const_formula_ptr precondition, const std::vector<std::string>& args);
84 
85 #if 0
86  //class responsible for looking for possible infinite loops when calling set_var or set_unit_var
87  class gamestate_change_observer : public events::observer
88  {
89  static const int MAX_CALLS = 1000;
90  int set_var_counter_;
91  int set_unit_var_counter_;
92  int continue_counter_;
93  public:
94  gamestate_change_observer();
95  ~gamestate_change_observer();
96 
97  void handle_generic_event(const std::string& /*event_name*/);
98 
99  bool set_var_check();
100 
101  bool set_unit_var_check();
102 
103  bool continue_check();
104  };
105 #endif
106 
108 
109  void swap_move_map(move_map_backup& backup);
110 
111  wfl::variant get_keeps() const;
112 
113  void on_create();
114 
115  int get_recursion_count() const override;
116 
117  const wfl::variant& get_keeps_cache() const { return keeps_cache_; }
118 
119  // Check if given unit can reach another unit
120  bool can_reach_unit(map_location unit_A, map_location unit_B) const;
121 
122  void handle_exception(const wfl::formula_error& e) const;
123  void handle_exception(const wfl::formula_error& e, const std::string& failed_operation) const;
124 
125  pathfind::teleport_map get_allowed_teleports(unit_map::iterator& unit_it) const;
126  pathfind::plain_route shortest_path_calculator(const map_location& src, const map_location& dst, unit_map::iterator& unit_it, pathfind::teleport_map& allowed_teleports) const;
127 
128  /** Create a new formula from the string, using the symbol table which is stored in the AI.
129  *
130  * @param formula_string the string from which a formula should be created
131  * @return pointer to created function or
132  * @retval game_logic::formula_ptr() if there were any problems
133  */
134  wfl::formula_ptr create_optional_formula(const std::string& formula_string) const;
135 
136  wfl::candidate_action_ptr load_candidate_action_from_config(const config& cfg);
137 
138  /** Evaluate the fai candidate action */
139  void evaluate_candidate_action(wfl::candidate_action_ptr fai_ca);
140 
141  /**
142  * Execute the fai candidate action
143  * @return true if game state was changed
144  * @return false if game state was not changed
145  */
146  bool execute_candidate_action(wfl::candidate_action_ptr fai_ca);
147 
148  void set_ai_context(ai_context *context);
149 
150  wfl::variant make_action(wfl::const_formula_ptr formula_, const wfl::formula_callable& variables);
151 
152 private:
154  const config cfg_;
156  void display_message(const std::string& msg) const;
157  virtual wfl::variant get_value(const std::string& key) const override;
158  void set_value(const std::string& key, const wfl::variant& value) override;
159  virtual void get_inputs(wfl::formula_input_vector& inputs) const override;
160 
163 
164 // gamestate_change_observer infinite_loop_guardian_;
166  // Making this mutable is a bit of a hack, but I don't have a better idea.
167  // (It's needed because a formula could define new functions.)
169 
170  friend class ai_default;
172 };
173 
174 } //end of namespace ai
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:145
Defines formula ai candidate actions - headers.
wfl::position_callable::move_map_backup move_map_backup
Definition: ai.hpp:107
const config cfg_
Definition: ai.hpp:154
std::vector< formula_input > formula_input_vector
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
std::multiset< unit_formula_pair, unit_formula_compare > unit_formula_set
Definition: ai.hpp:59
Definitions for the interface to Wesnoth Markup Language (WML).
ai_context & get_ai_context(wfl::const_formula_callable_ptr for_fai)
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
virtual ~formula_ai()
Definition: ai.hpp:77
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:132
wfl::attack_map_callable attacks_callable
Definition: ai.hpp:162
Encapsulates the map of the game.
Definition: location.hpp:38
wfl::map_formula_callable vars_
Definition: ai.hpp:165
recursion_counter recursion_counter_
Definition: ai.hpp:155
std::shared_ptr< const formula_callable > const_formula_callable_ptr
std::shared_ptr< const formula > const_formula_ptr
Definition: formula_fwd.hpp:24
Helper functions for the object which operates in the context of AI for specific side this is part of...
std::pair< unit_map::unit_iterator, int > unit_formula_pair
Definition: ai.hpp:49
const wfl::variant & get_keeps_cache() const
Definition: ai.hpp:117
Definition: contexts.hpp:44
wfl::ai_function_symbol_table function_table_
Definition: ai.hpp:168
#define e
wfl::variant keeps_cache_
Definition: ai.hpp:161
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
ai_context * ai_ptr_
Definition: ai.hpp:153
std::shared_ptr< formula > formula_ptr
Definition: formula_fwd.hpp:22