The Battle for Wesnoth  1.13.4+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 - 2016 by David White <[email protected]>
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 
21 #ifndef AI_FORMULA_AI_HPP_INCLUDED
22 #define AI_FORMULA_AI_HPP_INCLUDED
23 
24 #include "ai/contexts.hpp"
25 #include "ai/formula/function_table.hpp" // for ai_function_symbol_table
26 #include "ai/formula/callable_objects.hpp" // for position_callable, etc
27 #include "ai/formula/candidates.hpp" // for candidate_action_ptr, etc
28 #include "config.hpp" // for config
29 #include "formula/callable.hpp" // for formula_callable, etc
30 #include "formula/formula_fwd.hpp" // for const_formula_ptr, etc
31 #include "generic_event.hpp" // for observer
32 #include "pathfind/teleport.hpp" // for teleport_map
33 #include "units/map.hpp"
34 #include <boost/noncopyable.hpp> // for noncopyable
35 #include <set> // for multiset
36 #include <string> // for string
37 #include <utility> // for pair
38 #include <vector> // for vector
39 
40 class variant;
41 
42 namespace ai { class ai_context; }
43 namespace game_logic { struct formula_error; }
44 namespace pathfind { struct plain_route; } // lines 57-57
45 struct map_location;
46 
47 #ifdef _MSC_VER
48 #pragma warning(push)
49 //silence "inherits via dominance" warnings
50 #pragma warning(disable:4250)
51 #endif
52 
53 namespace game_logic {
54 
55 typedef std::pair< unit_map::unit_iterator, int> unit_formula_pair;
56 
58  bool operator() (const unit_formula_pair& left,
59  const unit_formula_pair& right) const
60  {
61  return left.second > right.second;
62  }
63 };
64 
65 typedef std::multiset< unit_formula_pair, game_logic::unit_formula_compare > unit_formula_set;
66 
67 }
68 
69 namespace pathfind {
70 
71 struct plain_route;
72 
73 } // of namespace pathfind
74 
75 namespace ai {
76 
77 class formula_ai : public readonly_context_proxy, public game_logic::formula_callable, public boost::noncopyable {
78 public:
79  explicit formula_ai(readonly_context &context, const config &cfg);
80  virtual ~formula_ai() {}
81  virtual config to_config() const;
82 
83  std::string evaluate(const std::string& formula_str);
84 
85  virtual void add_formula_function(const std::string& name, game_logic::const_formula_ptr formula, game_logic::const_formula_ptr precondition, const std::vector<std::string>& args);
86 
87  //class responsible for looking for possible infinite loops when calling set_var or set_unit_var
89  {
90  static const int MAX_CALLS = 1000;
94  public:
97 
98  void handle_generic_event(const std::string& /*event_name*/);
99 
100  bool set_var_check();
101 
102  bool set_unit_var_check();
103 
104  bool continue_check();
105  };
106 
108 
109  void swap_move_map(move_map_backup& backup);
110 
111  variant get_keeps() const;
112 
113  void on_create();
114 
115  int get_recursion_count() const;
116 
117  const 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 
123  void handle_exception(game_logic::formula_error& e, const std::string& failed_operation) const;
124 
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  */
135 
137 
138  /** Evaluate the fai candidate action */
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  */
147 
148  void set_ai_context(ai_context *context);
149 
151 
152 private:
154  const config cfg_;
156  void display_message(const std::string& msg) const;
157  variant execute_variant(const variant& var, ai_context &ai_, bool commandline=false);
158  virtual variant get_value(const std::string& key) const;
159  virtual void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
160 
162 
166 
167  friend class ai_default;
168 };
169 
170 } //end of namespace ai
171 
172 #ifdef _MSC_VER
173 #pragma warning(pop)
174 #endif
175 
176 #endif
bool execute_candidate_action(game_logic::candidate_action_ptr fai_ca)
Execute the fai candidate action.
Definition: ai.cpp:980
game_logic::formula_ptr create_optional_formula(const std::string &formula_string)
Create a new formula from the string, using the symbol table which is stored in the AI...
Definition: ai.cpp:148
int get_recursion_count() const
Get the value of the recursion counter.
Definition: ai.cpp:100
Defines formula ai candidate actions - headers.
gamestate_change_observer infinite_loop_guardian_
Definition: ai.hpp:163
void swap_move_map(move_map_backup &backup)
const config cfg_
Definition: ai.hpp:154
bool operator()(const unit_formula_pair &left, const unit_formula_pair &right) const
Definition: ai.hpp:58
std::string evaluate(const std::string &formula_str)
Definition: ai.cpp:165
game_logic::ai_function_symbol_table function_table_
Definition: ai.hpp:165
GLenum src
Definition: glew.h:2392
Definitions for the interface to Wesnoth Markup Language (WML).
friend class ai_default
Definition: ai.hpp:167
virtual variant get_value(const std::string &key) const
Definition: ai.cpp:584
const formula_ai & ai_
void on_create()
Definition: ai.cpp:937
virtual void add_formula_function(const std::string &name, game_logic::const_formula_ptr formula, game_logic::const_formula_ptr precondition, const std::vector< std::string > &args)
Definition: ai.cpp:562
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
virtual ~formula_ai()
Definition: ai.hpp:80
variant make_action(game_logic::const_formula_ptr formula_, const game_logic::formula_callable &variables)
Definition: ai.cpp:194
void evaluate_candidate_action(game_logic::candidate_action_ptr fai_ca)
Evaluate the fai candidate action.
Definition: ai.cpp:974
game_logic::position_callable::move_map_backup move_map_backup
Definition: ai.hpp:107
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:132
variant get_keeps() const
Definition: ai.cpp:895
GLenum GLenum dst
Definition: glew.h:2392
variant keeps_cache_
Definition: ai.hpp:161
const variant & get_keeps_cache() const
Definition: ai.hpp:117
pathfind::teleport_map get_allowed_teleports(unit_map::iterator &unit_it) const
Definition: ai.cpp:266
std::vector< formula_input > inputs() const
Definition: callable.hpp:50
lu_byte right
Definition: lparser.cpp:1020
Encapsulates the map of the game.
Definition: location.hpp:38
std::pair< unit_map::unit_iterator, int > unit_formula_pair
Definition: ai.hpp:55
GLint left
Definition: glew.h:5907
variant execute_variant(const variant &var, ai_context &ai_, bool commandline=false)
Definition: ai.cpp:272
recursion_counter recursion_counter_
Definition: ai.hpp:155
bool can_reach_unit(map_location unit_A, map_location unit_B) const
Definition: ai.cpp:920
virtual config to_config() const
Definition: ai.cpp:1030
formula_ai(readonly_context &context, const config &cfg)
Definition: ai.cpp:105
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
void handle_generic_event(const std::string &)
Definition: ai.cpp:999
Helper functions for the object which operates in the context of AI for specific side this is part of...
game_logic::map_formula_callable vars_
Definition: ai.hpp:164
game_logic::candidate_action_ptr load_candidate_action_from_config(const config &cfg)
Definition: ai.cpp:80
GLuint const GLchar * name
Definition: glew.h:1782
std::multiset< unit_formula_pair, game_logic::unit_formula_compare > unit_formula_set
Definition: ai.hpp:65
virtual void get_inputs(std::vector< game_logic::formula_input > *inputs) const
Definition: ai.cpp:863
#define e
void display_message(const std::string &msg) const
Definition: ai.cpp:141
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
ai_context * ai_ptr_
Definition: ai.hpp:153
void set_ai_context(ai_context *context)
Definition: ai.cpp:159
GLsizei const GLcharARB ** string
Definition: glew.h:4503
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:212
void handle_exception(game_logic::formula_error &e) const
Definition: ai.cpp:122