ai/composite/goal.hpp

Go to the documentation of this file.
00001 /* $Id: goal.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  */
00019 
00020 #ifndef AI_COMPOSITE_GOAL_HPP_INCLUDED
00021 #define AI_COMPOSITE_GOAL_HPP_INCLUDED
00022 
00023 
00024 #ifdef _MSC_VER
00025 #pragma warning(push)
00026 //silence "inherits via dominance" warnings
00027 #pragma warning(disable:4250)
00028 #endif
00029 
00030 //included for 'target' markers
00031 #include "../default/contexts.hpp"
00032 
00033 #include "component.hpp"
00034 
00035 #include <stack>
00036 #include <deque>
00037 
00038 class terrain_filter;
00039 
00040 namespace ai {
00041 
00042 class lua_ai_context;
00043 class lua_ai_action_handler;
00044 
00045 class goal : public readonly_context_proxy, public component {
00046 public:
00047     goal(readonly_context &context, const config &cfg);
00048 
00049 
00050     virtual ~goal();
00051 
00052 
00053     virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
00054 
00055 
00056     virtual config to_config() const;
00057 
00058 
00059     virtual void on_create();
00060     virtual void on_create(boost::shared_ptr<ai::lua_ai_context>);
00061 
00062 
00063     bool active() const;
00064 
00065     virtual std::string get_id() const;
00066     virtual std::string get_name() const;
00067     virtual std::string get_engine() const;
00068 
00069     bool redeploy(const config &cfg);
00070 
00071 
00072 protected:
00073     config cfg_;
00074 
00075 
00076 };
00077 
00078 
00079 class target_unit_goal : public goal {
00080 public:
00081     target_unit_goal(readonly_context &context, const config &cfg);
00082 
00083 
00084     virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
00085 
00086 
00087     virtual void on_create();
00088 
00089 private:
00090     double value() const
00091     {
00092         return value_;
00093     }
00094     double value_;
00095 };
00096 
00097 
00098 class target_location_goal : public goal {
00099 public:
00100     target_location_goal(readonly_context &context, const config &cfg);
00101 
00102 
00103     virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
00104 
00105 
00106     virtual void on_create();
00107 
00108 private:
00109     double value() const
00110     {
00111         return value_;
00112     }
00113     boost::shared_ptr<terrain_filter> filter_ptr_;
00114     double value_;
00115 };
00116 
00117 
00118 class protect_goal : public goal {
00119 public:
00120     protect_goal(readonly_context &context, const config &cfg, bool protect_only_own_unit, bool protect_unit);
00121 
00122 
00123     virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
00124 
00125 
00126     virtual void on_create();
00127 
00128 private:
00129 
00130     double value()
00131     {
00132         return value_;
00133     }
00134 
00135     boost::shared_ptr<terrain_filter> filter_ptr_;
00136     bool protect_only_own_unit_;
00137     bool protect_unit_;
00138     int radius_;
00139     double value_;
00140 };
00141 
00142 
00143 class protect_location_goal : public protect_goal {
00144 public:
00145     protect_location_goal(readonly_context &context, const config &cfg)
00146     : protect_goal(context,cfg,false,false)
00147     {
00148     }
00149 };
00150 
00151 
00152 class protect_unit_goal : public protect_goal {
00153 public:
00154     protect_unit_goal(readonly_context &context, const config &cfg)
00155     : protect_goal(context,cfg,false,true)
00156     {
00157     }
00158 };
00159 
00160 
00161 class protect_my_unit_goal : public protect_goal {
00162 public:
00163     protect_my_unit_goal(readonly_context &context, const config &cfg)
00164     : protect_goal(context,cfg,true,true)
00165     {
00166     }
00167 };
00168 
00169 class lua_goal : public goal {
00170 public:
00171     lua_goal(readonly_context& context, const config& cfg);
00172     virtual void add_targets(std::back_insert_iterator< std::vector< target > > target_list);
00173     void on_create(boost::shared_ptr<ai::lua_ai_context>);
00174 
00175 private:
00176     std::string code_;
00177     boost::shared_ptr<lua_ai_action_handler> handler_;
00178 };
00179 
00180 
00181 class goal_factory{
00182 public:
00183     typedef boost::shared_ptr< goal_factory > factory_ptr;
00184     typedef std::map<std::string, factory_ptr> factory_map;
00185     typedef std::pair<const std::string, factory_ptr> factory_map_pair;
00186 
00187     static factory_map& get_list() {
00188         static factory_map *goal_factories;
00189         if (goal_factories==NULL) {
00190             goal_factories = new factory_map;
00191         }
00192         return *goal_factories;
00193     }
00194 
00195     virtual goal_ptr get_new_instance( readonly_context &context, const config &cfg ) = 0;
00196 
00197     goal_factory( const std::string &name )
00198     {
00199         factory_ptr ptr_to_this(this);
00200         get_list().insert(make_pair(name,ptr_to_this));
00201     }
00202 
00203     virtual ~goal_factory() {}
00204 };
00205 
00206 
00207 template<class GOAL>
00208 class register_goal_factory : public goal_factory {
00209 public:
00210     register_goal_factory( const std::string &name )
00211         : goal_factory( name )
00212     {
00213     }
00214 
00215     virtual goal_ptr get_new_instance( readonly_context &context, const config &cfg ){
00216         goal_ptr a(new GOAL(context,cfg));
00217         a->on_create();
00218         return a;
00219     }
00220 };
00221 
00222 
00223 } //end of namespace ai
00224 
00225 
00226 #ifdef _MSC_VER
00227 #pragma warning(pop)
00228 #endif
00229 
00230 #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