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