ai/composite/rca.hpp

Go to the documentation of this file.
00001 /* $Id: rca.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Yurii Chernyi <terraninfo@terraninfo.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  * candidate action framework
00019  */
00020 
00021 #ifndef AI_COMPOSITE_RCA_HPP_INCLUDED
00022 #define AI_COMPOSITE_RCA_HPP_INCLUDED
00023 
00024 #include "component.hpp"
00025 #include "contexts.hpp"
00026 
00027 #ifdef _MSC_VER
00028 #pragma warning(push)
00029 //silence "inherits via dominance" warnings
00030 #pragma warning(disable:4250)
00031 #endif
00032 
00033 //============================================================================
00034 namespace ai {
00035 
00036 class candidate_action : public virtual rca_context_proxy, public component {
00037 public:
00038     //this is a score guaranteed to be <=0, thus candidate action with this score will not be selected for execution
00039     static const double BAD_SCORE;
00040 
00041     //this is a score guaranteed to be very high, higher than any 'normal' candidate action score
00042     static const double HIGH_SCORE;
00043 
00044     /**
00045      * Constructor
00046      * @param ai context of the candidate action
00047      * @param name name of the action (for debug purposes)
00048      * @param type type of the action (for debug purposes)
00049      */
00050     candidate_action( rca_context &context, const config &cfg );
00051 
00052     /**
00053      * Destructor
00054      */
00055     virtual ~candidate_action();
00056 
00057 
00058     /**
00059      * Evaluate the candidate action, resetting the internal state of the action
00060      * @return the score
00061      * @retval >0 if the action is good
00062      * @retval <=0 if the action is not good
00063      */
00064     virtual double evaluate() = 0;
00065 
00066     /**
00067      * Execute the candidate action
00068      */
00069     virtual void execute() = 0;
00070 
00071     /**
00072      * Is this candidate action enabled ?
00073      */
00074     bool is_enabled() const;
00075 
00076     /**
00077      * Enable the candidate action
00078      */
00079     void enable();
00080 
00081     /**
00082      * Disable the candidate action
00083      */
00084     void disable();
00085 
00086     /**
00087      * Get the usual score of the candidate action without re-evaluation
00088      */
00089     double get_score() const;
00090 
00091 
00092     /**
00093      * Get the upper bound of the score of the candidate action without re-evaluation
00094      */
00095     double get_max_score() const;
00096 
00097     /**
00098      * Get the name of the candidate action (useful for debug purposes)
00099      */
00100     virtual std::string get_name() const
00101     { return name_; }
00102 
00103     /**
00104      * Get the type of the candidate action (useful for debug purposes)
00105      */
00106     const std::string& get_type() const;
00107 
00108     virtual std::string get_id() const
00109     { return id_; }
00110 
00111     virtual std::string get_engine() const
00112     { return engine_; }
00113 
00114     int get_recursion_count() const;
00115 
00116 
00117     /**
00118      * serialize
00119      */
00120     virtual config to_config() const;
00121 
00122     virtual void set_to_be_removed();
00123 
00124     virtual bool to_be_removed();
00125 
00126 private:
00127 
00128     recursion_counter recursion_counter_;
00129 
00130     bool enabled_;
00131 
00132 
00133     std::string engine_;
00134 
00135 
00136     double score_;
00137 
00138 
00139     double max_score_;
00140 
00141 
00142     std::string id_;
00143 
00144 
00145     std::string name_;
00146 
00147 
00148     std::string type_;
00149 
00150 
00151     bool to_be_removed_;
00152 
00153 };
00154 
00155 typedef boost::shared_ptr<candidate_action> candidate_action_ptr;
00156 
00157 class candidate_action_factory;
00158 
00159 class candidate_action_factory{
00160 public:
00161     typedef boost::shared_ptr< candidate_action_factory > factory_ptr;
00162     typedef std::map<std::string, factory_ptr> factory_map;
00163     typedef std::pair<const std::string, factory_ptr> factory_map_pair;
00164 
00165     static factory_map& get_list() {
00166         static factory_map *candidate_action_factories;
00167         if (candidate_action_factories==NULL) {
00168             candidate_action_factories = new factory_map;
00169         }
00170         return *candidate_action_factories;
00171     }
00172 
00173     virtual candidate_action_ptr get_new_instance( rca_context &context, const config &cfg ) = 0;
00174 
00175     candidate_action_factory( const std::string &name )
00176     {
00177         factory_ptr ptr_to_this(this);
00178         get_list().insert(make_pair(name,ptr_to_this));
00179     }
00180 
00181     virtual ~candidate_action_factory() {}
00182 };
00183 
00184 
00185 template<class CANDIDATE_ACTION>
00186 class register_candidate_action_factory : public candidate_action_factory {
00187 public:
00188     register_candidate_action_factory( const std::string &name )
00189         : candidate_action_factory( name )
00190     {
00191     }
00192 
00193     virtual candidate_action_ptr get_new_instance( rca_context &ai, const config &cfg ){
00194         return candidate_action_ptr(new CANDIDATE_ACTION(ai,cfg));
00195     }
00196 };
00197 
00198 
00199 //============================================================================
00200 
00201 } //end of namespace ai
00202 
00203 std::ostream &operator<<(std::ostream &s, ai::candidate_action const &ca);
00204 
00205 #ifdef _MSC_VER
00206 #pragma warning(pop)
00207 #endif
00208 
00209 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:43 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs