The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ai.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2017 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 #ifdef _MSC_VER
47 #pragma warning(push)
48 //silence "inherits via dominance" warnings
49 #pragma warning(disable:4250)
50 #endif
51 
52 namespace wfl {
53 
54 typedef std::pair< unit_map::unit_iterator, int> unit_formula_pair;
55 
57  bool operator() (const unit_formula_pair& left,
58  const unit_formula_pair& right) const
59  {
60  return left.second > right.second;
61  }
62 };
63 
64 typedef std::multiset< unit_formula_pair, unit_formula_compare > unit_formula_set;
65 
66 }
67 
68 namespace pathfind {
69 
70 struct plain_route;
71 
72 } // of namespace pathfind
73 
74 namespace ai {
75 
77 public:
78  formula_ai(const formula_ai&) = delete;
79  formula_ai& operator=(const formula_ai&) = delete;
80 
81  explicit formula_ai(readonly_context &context, const config &cfg);
82  virtual ~formula_ai() {}
83 
84  virtual config to_config() const;
85 
86  std::string evaluate(const std::string& formula_str);
87 
88  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);
89 
90 #if 0
91  //class responsible for looking for possible infinite loops when calling set_var or set_unit_var
92  class gamestate_change_observer : public events::observer
93  {
94  static const int MAX_CALLS = 1000;
95  int set_var_counter_;
96  int set_unit_var_counter_;
97  int continue_counter_;
98  public:
99  gamestate_change_observer();
100  ~gamestate_change_observer();
101 
102  void handle_generic_event(const std::string& /*event_name*/);
103 
104  bool set_var_check();
105 
106  bool set_unit_var_check();
107 
108  bool continue_check();
109  };
110 #endif
111 
113 
114  void swap_move_map(move_map_backup& backup);
115 
116  wfl::variant get_keeps() const;
117 
118  void on_create();
119 
120  int get_recursion_count() const override;
121 
122  const wfl::variant& get_keeps_cache() const { return keeps_cache_; }
123 
124  // Check if given unit can reach another unit
125  bool can_reach_unit(map_location unit_A, map_location unit_B) const;
126 
128  void handle_exception(wfl::formula_error& e, const std::string& failed_operation) const;
129 
131  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;
132 
133  /** Create a new formula from the string, using the symbol table which is stored in the AI.
134  *
135  * @param formula_string the string from which a formula should be created
136  * @return pointer to created function or
137  * @retval game_logic::formula_ptr() if there were any problems
138  */
139  wfl::formula_ptr create_optional_formula(const std::string& formula_string) const;
140 
142 
143  /** Evaluate the fai candidate action */
145 
146  /**
147  * Execute the fai candidate action
148  * @return true if game state was changed
149  * @return false if game state was not changed
150  */
152 
153  void set_ai_context(ai_context *context);
154 
156 
157 private:
159  const config cfg_;
161  void display_message(const std::string& msg) const;
162  virtual wfl::variant get_value(const std::string& key) const override;
163  void set_value(const std::string& key, const wfl::variant& value) override;
164  virtual void get_inputs(wfl::formula_input_vector& inputs) const override;
165 
168 
169 // gamestate_change_observer infinite_loop_guardian_;
171  // Making this mutable is a bit of a hack, but I don't have a better idea.
172  // (It's needed because a formula could define new functions.)
174 
175  friend class ai_default;
177 };
178 
179 } //end of namespace ai
180 
181 #ifdef _MSC_VER
182 #pragma warning(pop)
183 #endif
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:147
friend ai_context & get_ai_context(wfl::const_formula_callable_ptr for_fai)
std::vector< char_t > string
formula_ai(const formula_ai &)=delete
Defines formula ai candidate actions - headers.
wfl::position_callable::move_map_backup move_map_backup
Definition: ai.hpp:112
const wfl::variant & get_keeps_cache() const
Definition: ai.hpp:122
void swap_move_map(move_map_backup &backup)
const config cfg_
Definition: ai.hpp:159
bool execute_candidate_action(wfl::candidate_action_ptr fai_ca)
Execute the fai candidate action.
Definition: ai.cpp:685
bool operator()(const unit_formula_pair &left, const unit_formula_pair &right) const
Definition: ai.hpp:57
void handle_exception(wfl::formula_error &e) const
Definition: ai.cpp:117
std::vector< formula_input > formula_input_vector
std::string evaluate(const std::string &formula_str)
Definition: ai.cpp:160
formula_input_vector inputs() const
Definition: callable.hpp:62
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
formula_ai & operator=(const formula_ai &)=delete
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:143
virtual wfl::variant get_value(const std::string &key) const override
Definition: ai.cpp:287
std::multiset< unit_formula_pair, unit_formula_compare > unit_formula_set
Definition: ai.hpp:64
Definitions for the interface to Wesnoth Markup Language (WML).
friend class ai_default
Definition: ai.hpp:175
void on_create()
Definition: ai.cpp:643
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
virtual ~formula_ai()
Definition: ai.hpp:82
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:131
wfl::variant get_keeps() const
Definition: ai.cpp:601
void set_value(const std::string &key, const wfl::variant &value) override
Definition: ai.cpp:597
void evaluate_candidate_action(wfl::candidate_action_ptr fai_ca)
Evaluate the fai candidate action.
Definition: ai.cpp:679
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:265
pathfind::teleport_map get_allowed_teleports(unit_map::iterator &unit_it) const
Definition: ai.cpp:260
wfl::attack_map_callable attacks_callable
Definition: ai.hpp:167
lu_byte right
Definition: lparser.cpp:1027
Encapsulates the map of the game.
Definition: location.hpp:40
virtual void get_inputs(wfl::formula_input_vector &inputs) const override
Definition: ai.cpp:566
wfl::map_formula_callable vars_
Definition: ai.hpp:170
recursion_counter recursion_counter_
Definition: ai.hpp:160
bool can_reach_unit(map_location unit_A, map_location unit_B) const
Definition: ai.cpp:626
virtual config to_config() const
Definition: ai.cpp:736
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:54
wfl::candidate_action_ptr load_candidate_action_from_config(const config &cfg)
Definition: ai.cpp:75
Definition: contexts.hpp:42
wfl::ai_function_symbol_table function_table_
Definition: ai.hpp:173
lu_byte left
Definition: lparser.cpp:1026
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
#define e
wfl::variant keeps_cache_
Definition: ai.hpp:166
void display_message(const std::string &msg) const
Definition: ai.cpp:136
int get_recursion_count() const override
Get the value of the recursion counter.
Definition: ai.cpp:95
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
wfl::variant make_action(wfl::const_formula_ptr formula_, const wfl::formula_callable &variables)
Definition: ai.cpp:188
ai_context * ai_ptr_
Definition: ai.hpp:158
void set_ai_context(ai_context *context)
Definition: ai.cpp:154
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:206
std::shared_ptr< formula > formula_ptr
Definition: formula_fwd.hpp:21