ai/formula/ai.hpp

Go to the documentation of this file.
00001 /* $Id: ai.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  * Defines formula ai
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 //silence "inherits via dominance" warnings
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 } // of namespace pathfind
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     //class responsible for looking for possible infinite loops when calling set_var or set_unit_var
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& /*event_name*/);
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     // Check if given unit can reach another unit
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     /** Create a new formula from the string, using the symbol table which is stored in the AI.
00116     *
00117     *   @param formula_string the string from which a formula should be created
00118     *   @return pointer to created function or
00119     *   @retval game_logic::formula_ptr() if there were any problems
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     /** Evaluate the fai candidate action */
00126     void evaluate_candidate_action(game_logic::candidate_action_ptr fai_ca);
00127 
00128     /**
00129      * Execute the fai candidate action
00130      * @return true if game state was changed
00131      * @return false if game state was not changed
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 } //end of namespace ai
00159 
00160 #ifdef _MSC_VER
00161 #pragma warning(pop)
00162 #endif
00163 
00164 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:28 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs