The Battle for Wesnoth  1.15.0-dev
ai.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Defines formula ai
18  * */
19 
20 #pragma once
21 
22 #include "ai/contexts.hpp"
23 #include "ai/formula/function_table.hpp" // for ai_function_symbol_table
24 #include "ai/formula/callable_objects.hpp" // for position_callable, etc
25 #include "ai/formula/candidates.hpp" // for candidate_action_ptr, etc
26 #include "config.hpp" // for config
27 #include "formula/callable.hpp" // for formula_callable, etc
28 #include "formula/formula_fwd.hpp" // for const_formula_ptr, etc
29 #include "generic_event.hpp" // for observer
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 namespace pathfind { struct plain_route; } // lines 57-57
39 struct map_location;
40 
41 namespace wfl {
42  struct formula_error;
43  class variant;
44 }
45 
46 namespace wfl {
47 
48 typedef std::pair< unit_map::unit_iterator, int> unit_formula_pair;
49 
51  bool operator() (const unit_formula_pair& left,
52  const unit_formula_pair& right) const
53  {
54  return left.second > right.second;
55  }
56 };
57 
58 typedef std::multiset< unit_formula_pair, unit_formula_compare > unit_formula_set;
59 
60 }
61 
62 namespace pathfind {
63 
64 struct plain_route;
65 
66 } // of namespace pathfind
67 
68 namespace ai {
69 
71 public:
72  formula_ai(const formula_ai&) = delete;
73  formula_ai& operator=(const formula_ai&) = delete;
74 
75  explicit formula_ai(readonly_context &context, const config &cfg);
76  virtual ~formula_ai() {}
77 
78  virtual config to_config() const;
79 
80  std::string evaluate(const std::string& formula_str);
81 
82  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);
83 
84 #if 0
85  //class responsible for looking for possible infinite loops when calling set_var or set_unit_var
86  class gamestate_change_observer : public events::observer
87  {
88  static const int MAX_CALLS = 1000;
89  int set_var_counter_;
90  int set_unit_var_counter_;
91  int continue_counter_;
92  public:
93  gamestate_change_observer();
94  ~gamestate_change_observer();
95 
96  void handle_generic_event(const std::string& /*event_name*/);
97 
98  bool set_var_check();
99 
100  bool set_unit_var_check();
101 
102  bool continue_check();
103  };
104 #endif
105 
107 
108  void swap_move_map(move_map_backup& backup);
109 
110  wfl::variant get_keeps() const;
111 
112  void on_create();
113 
114  int get_recursion_count() const override;
115 
116  const wfl::variant& get_keeps_cache() const { return keeps_cache_; }
117 
118  // Check if given unit can reach another unit
119  bool can_reach_unit(map_location unit_A, map_location unit_B) const;
120 
121  void handle_exception(const wfl::formula_error& e) const;
122  void handle_exception(const wfl::formula_error& e, const std::string& failed_operation) const;
123 
124  pathfind::teleport_map get_allowed_teleports(unit_map::iterator& unit_it) const;
125  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;
126 
127  /** Create a new formula from the string, using the symbol table which is stored in the AI.
128  *
129  * @param formula_string the string from which a formula should be created
130  * @return pointer to created function or
131  * @retval game_logic::formula_ptr() if there were any problems
132  */
133  wfl::formula_ptr create_optional_formula(const std::string& formula_string) const;
134 
135  wfl::candidate_action_ptr load_candidate_action_from_config(const config& cfg);
136 
137  /** Evaluate the fai candidate action */
138  void evaluate_candidate_action(wfl::candidate_action_ptr fai_ca);
139 
140  /**
141  * Execute the fai candidate action
142  * @return true if game state was changed
143  * @return false if game state was not changed
144  */
145  bool execute_candidate_action(wfl::candidate_action_ptr fai_ca);
146 
147  void set_ai_context(ai_context *context);
148 
149  wfl::variant make_action(wfl::const_formula_ptr formula_, const wfl::formula_callable& variables);
150 
151 private:
153  const config cfg_;
155  void display_message(const std::string& msg) const;
156  virtual wfl::variant get_value(const std::string& key) const override;
157  void set_value(const std::string& key, const wfl::variant& value) override;
158  virtual void get_inputs(wfl::formula_input_vector& inputs) const override;
159 
162 
163 // gamestate_change_observer infinite_loop_guardian_;
165  // Making this mutable is a bit of a hack, but I don't have a better idea.
166  // (It's needed because a formula could define new functions.)
168 
169  friend class ai_default;
171 };
172 
173 } //end of namespace ai
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:141
Defines formula ai candidate actions - headers.
wfl::position_callable::move_map_backup move_map_backup
Definition: ai.hpp:106
const config cfg_
Definition: ai.hpp:153
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:109
std::multiset< unit_formula_pair, unit_formula_compare > unit_formula_set
Definition: ai.hpp:58
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:58
virtual ~formula_ai()
Definition: ai.hpp:76
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:131
wfl::attack_map_callable attacks_callable
Definition: ai.hpp:161
lu_byte right
Definition: lparser.cpp:1027
Encapsulates the map of the game.
Definition: location.hpp:42
wfl::map_formula_callable vars_
Definition: ai.hpp:164
recursion_counter recursion_counter_
Definition: ai.hpp:154
std::shared_ptr< const formula_callable > const_formula_callable_ptr
std::shared_ptr< const formula > const_formula_ptr
Definition: formula_fwd.hpp:23
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:48
const wfl::variant & get_keeps_cache() const
Definition: ai.hpp:116
Definition: contexts.hpp:43
wfl::ai_function_symbol_table function_table_
Definition: ai.hpp:167
lu_byte left
Definition: lparser.cpp:1026
#define e
wfl::variant keeps_cache_
Definition: ai.hpp:160
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
ai_context * ai_ptr_
Definition: ai.hpp:152
std::shared_ptr< formula > formula_ptr
Definition: formula_fwd.hpp:21