ai/composite/stage.hpp

Go to the documentation of this file.
00001 /* $Id: stage.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  * Composite AI stages
00019  */
00020 
00021 #ifndef AI_COMPOSITE_STAGE_HPP_INCLUDED
00022 #define AI_COMPOSITE_STAGE_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 namespace ai {
00034 
00035 class ai_composite;
00036 
00037 class stage : public virtual ai_context_proxy, public component {
00038 public:
00039 
00040     /**
00041      * Constructor
00042      */
00043     stage( ai_context &context, const config &cfg );
00044 
00045     /**
00046      * Initialization
00047      */
00048     virtual void on_create();
00049 
00050 
00051     /**
00052      * Destructor
00053      */
00054     virtual ~stage();
00055 
00056     /**
00057      * Play the turn - strategy
00058      * @return true only if game state has changed. Really only needed for ministages. Returning false is always safe.
00059      */
00060     bool play_stage();
00061 
00062 
00063     /**
00064      * get the value of the recursion counter
00065      */
00066     int get_recursion_count() const;
00067 
00068 
00069     /**
00070      * serialize
00071      */
00072     virtual config to_config() const;
00073 
00074     virtual std::string get_id() const;
00075     virtual std::string get_name() const;
00076     virtual std::string get_engine() const;
00077 
00078 protected:
00079     /**
00080      * Play the turn - implementation
00081      * @return true only if game state has changed. Really only needed for ministages. Returning false is always safe.
00082      */
00083     virtual bool do_play_stage() = 0;
00084 
00085     recursion_counter recursion_counter_;
00086 
00087     config cfg_;
00088 
00089 };
00090 
00091 
00092 class idle_stage : public stage {
00093 public:
00094     idle_stage( ai_context &context, const config &cfg );
00095 
00096     ~idle_stage();
00097 
00098     virtual bool do_play_stage();
00099 };
00100 
00101 
00102 class stage_factory{
00103 public:
00104     typedef boost::shared_ptr< stage_factory > factory_ptr;
00105     typedef std::map<std::string, factory_ptr> factory_map;
00106     typedef std::pair<const std::string, factory_ptr> factory_map_pair;
00107 
00108     static factory_map& get_list() {
00109         static factory_map *stage_factories;
00110         if (stage_factories==NULL) {
00111             stage_factories = new factory_map;
00112         }
00113         return *stage_factories;
00114     }
00115 
00116     virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ) = 0;
00117 
00118     stage_factory( const std::string &name )
00119     {
00120         factory_ptr ptr_to_this(this);
00121         get_list().insert(make_pair(name,ptr_to_this));
00122     }
00123 
00124     virtual ~stage_factory() {}
00125 };
00126 
00127 
00128 template<class STAGE>
00129 class register_stage_factory : public stage_factory {
00130 public:
00131     register_stage_factory( const std::string &name )
00132         : stage_factory( name )
00133     {
00134     }
00135 
00136     virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ){
00137         stage_ptr a(new STAGE(context,cfg));
00138         a->on_create();
00139         return a;
00140     }
00141 };
00142 
00143 
00144 /** this class is a lazily-initializing proxy for a stage **/
00145 class ministage {
00146 public:
00147     ministage(const config &cfg);
00148     virtual ~ministage();
00149     stage_ptr get_stage_ptr(ai_context &context);
00150     config to_config() const;
00151 
00152 private:
00153     config cfg_;
00154     stage_ptr stage_;
00155 };
00156 
00157 } //end of namespace ai
00158 
00159 #ifdef _MSC_VER
00160 #pragma warning(pop)
00161 #endif
00162 
00163 #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