ai/interface.hpp

Go to the documentation of this file.
00001 /* $Id: interface.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 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  * Interface to the AI.
00019  */
00020 
00021 #ifndef AI_INTERFACE_HPP_INCLUDED
00022 #define AI_INTERFACE_HPP_INCLUDED
00023 
00024 #include "../savegame_config.hpp"
00025 #include "default/contexts.hpp"
00026 
00027 namespace ai {
00028 
00029 class interface : savegame::savegame_config {
00030 public:
00031     /**
00032      * The constructor.
00033      */
00034     interface()
00035         : savegame_config()
00036     {
00037     }
00038 
00039 
00040     virtual ~interface() {}
00041 
00042 
00043     /**
00044      * Function that is called when the AI must play its turn.
00045      * Derived classes should implement their AI algorithm in this function.
00046      */
00047     virtual void play_turn() = 0;
00048 
00049     /**
00050      * Function called when a a new turn is played
00051      */
00052     virtual void new_turn() = 0;
00053 
00054     /**
00055      * Function called after the new ai is created
00056      *
00057      */
00058     virtual void on_create() {
00059     }
00060 
00061     virtual void switch_side(ai::side_number side) = 0;
00062 
00063         /** Evaluate */
00064         virtual std::string evaluate(const std::string& /*str*/)
00065             { return "evaluate command not implemented by this AI"; }
00066 
00067     /** Describe self*/
00068     virtual std::string describe_self() const;
00069 
00070 
00071     /** serialize to config **/
00072     virtual config to_config() const = 0;
00073 };
00074 
00075 class ai_factory;
00076 
00077 class ai_factory{
00078 public:
00079     typedef boost::shared_ptr< ai_factory > factory_ptr;
00080     typedef std::map<std::string, factory_ptr> factory_map;
00081     typedef std::pair<const std::string, factory_ptr> factory_map_pair;
00082 
00083     static factory_map& get_list() {
00084         static factory_map *ai_factories;
00085         if (ai_factories==NULL) {
00086             ai_factories = new factory_map;
00087         }
00088         return *ai_factories;
00089     }
00090 
00091     virtual ai_ptr get_new_instance( ai_context &context, const config &cfg) = 0;
00092 
00093     ai_factory( const std::string &name )
00094     {
00095         factory_ptr ptr_to_this(this);
00096         get_list().insert(make_pair(name,ptr_to_this));
00097     }
00098 
00099     virtual ~ai_factory() {}
00100 };
00101 
00102 
00103 template<class AI>
00104 class register_ai_factory : public ai_factory {
00105 public:
00106     register_ai_factory( const std::string &name )
00107         : ai_factory( name )
00108     {
00109     }
00110 
00111     virtual ai_ptr get_new_instance( ai_context &context, const config &cfg){
00112         ai_ptr a(new AI(context,cfg));
00113         a->on_create();
00114         return a;
00115     }
00116 };
00117 
00118 
00119 
00120 } //end of namespace ai
00121 
00122 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Tue May 22 2012 01:03:38 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs