Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef AI_FORMULA_AI_HPP_INCLUDED
00023 #define AI_FORMULA_AI_HPP_INCLUDED
00024
00025 #include "callable_objects.hpp"
00026 #include "candidates.hpp"
00027 #include "function_table.hpp"
00028
00029 #include "../default/ai.hpp"
00030 #include "../../pathfind/teleport.hpp"
00031
00032 #include <boost/noncopyable.hpp>
00033
00034 #ifdef _MSC_VER
00035 #pragma warning(push)
00036
00037 #pragma warning(disable:4250)
00038 #endif
00039
00040 namespace game_logic {
00041
00042 typedef std::pair< unit_map::unit_iterator, int> unit_formula_pair;
00043
00044 struct unit_formula_compare {
00045 bool operator() (const unit_formula_pair& left,
00046 const unit_formula_pair& right) const
00047 {
00048 return left.second > right.second;
00049 }
00050 };
00051
00052 typedef std::multiset< unit_formula_pair, game_logic::unit_formula_compare > unit_formula_set;
00053
00054 }
00055
00056 namespace pathfind {
00057
00058 struct plain_route;
00059
00060 }
00061
00062 namespace ai {
00063
00064 class formula_ai : public readonly_context_proxy, public game_logic::formula_callable, public boost::noncopyable {
00065 public:
00066 explicit formula_ai(readonly_context &context, const config &cfg);
00067 virtual ~formula_ai() {};
00068 virtual config to_config() const;
00069
00070 std::string evaluate(const std::string& formula_str);
00071
00072 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);
00073
00074
00075 class gamestate_change_observer : public events::observer
00076 {
00077 static const int MAX_CALLS = 1000;
00078 int set_var_counter_;
00079 int set_unit_var_counter_;
00080 int continue_counter_;
00081 public:
00082 gamestate_change_observer();
00083 ~gamestate_change_observer();
00084
00085 void handle_generic_event(const std::string& );
00086
00087 bool set_var_check();
00088
00089 bool set_unit_var_check();
00090
00091 bool continue_check();
00092 };
00093
00094 typedef game_logic::position_callable::move_map_backup move_map_backup;
00095
00096 void swap_move_map(move_map_backup& backup);
00097
00098 variant get_keeps() const;
00099
00100 void on_create();
00101
00102 int get_recursion_count() const;
00103
00104 const variant& get_keeps_cache() const { return keeps_cache_; }
00105
00106
00107 bool can_reach_unit(map_location unit_A, map_location unit_B) const;
00108
00109 void handle_exception(game_logic::formula_error& e) const;
00110 void handle_exception(game_logic::formula_error& e, const std::string& failed_operation) const;
00111
00112 pathfind::teleport_map get_allowed_teleports(unit_map::iterator& unit_it) const;
00113 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;
00114
00115
00116
00117
00118
00119
00120
00121 game_logic::formula_ptr create_optional_formula(const std::string& formula_string);
00122
00123 game_logic::candidate_action_ptr load_candidate_action_from_config(const config& cfg);
00124
00125
00126 void evaluate_candidate_action(game_logic::candidate_action_ptr fai_ca);
00127
00128
00129
00130
00131
00132
00133 bool execute_candidate_action(game_logic::candidate_action_ptr fai_ca);
00134
00135 void set_ai_context(ai_context *context);
00136
00137 variant make_action(game_logic::const_formula_ptr formula_, const game_logic::formula_callable& variables);
00138
00139 private:
00140 ai_context *ai_ptr_;
00141 const config cfg_;
00142 recursion_counter recursion_counter_;
00143 void display_message(const std::string& msg) const;
00144 variant execute_variant(const variant& var, ai_context &ai_, bool commandline=false);
00145 virtual variant get_value(const std::string& key) const;
00146 virtual void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00147
00148 mutable variant keeps_cache_;
00149
00150 gamestate_change_observer infinite_loop_guardian_;
00151 game_logic::map_formula_callable vars_;
00152 game_logic::ai_function_symbol_table function_table_;
00153 game_logic::candidate_action_manager candidate_action_manager_;
00154
00155 friend class ai_default;
00156 };
00157
00158 }
00159
00160 #ifdef _MSC_VER
00161 #pragma warning(pop)
00162 #endif
00163
00164 #endif