callable_objects.hpp

Go to the documentation of this file.
00001 /* $Id: callable_objects.hpp 54051 2012-04-30 21:52:38Z brilliand $ */
00002 /*
00003    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2 of the License, or
00008    (at your option) any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 
00016 #ifndef CALLABLE_OBJECTS_HPP_INCLUDED
00017 #define CALLABLE_OBJECTS_HPP_INCLUDED
00018 
00019 
00020 #include "map.hpp"
00021 #include "team.hpp"
00022 
00023 #define CALLABLE_WRAPPER_START(klass) \
00024 class klass##_callable : public game_logic::formula_callable { \
00025     const klass& object_; \
00026 public: \
00027     explicit klass##_callable(const klass& object) : object_(object) \
00028     {} \
00029     \
00030     const klass& get_##klass() const { return object_; } \
00031     void get_inputs(std::vector<game_logic::formula_input>* inputs) const \
00032     { \
00033         using game_logic::FORMULA_READ_ONLY;
00034 
00035 #define CALLABLE_WRAPPER_INPUT(VAR) \
00036     inputs->push_back(game_logic::formula_input(#VAR, FORMULA_READ_ONLY));
00037 
00038 #define CALLABLE_WRAPPER_INPUT_END \
00039     } \
00040     \
00041     variant get_value(const std::string& key) const {
00042 
00043 #define CALLABLE_WRAPPER_VAR(VAR) \
00044     if(key == #VAR) { \
00045         return variant(object_.VAR); \
00046     } else
00047 
00048 #define CALLABLE_WRAPPER_FN(VAR) \
00049     if(key == #VAR) { \
00050         return variant(object_.VAR()); \
00051     } else
00052 
00053 
00054 
00055 #define CALLABLE_WRAPPER_END \
00056         { return variant(); } \
00057     } \
00058 };
00059 
00060 template <typename T, typename K> variant convert_map( const std::map<T,K>& map );
00061 
00062 template <typename T> variant convert_vector( const std::vector<T>& input_vector );
00063 
00064 
00065 class terrain_callable : public game_logic::formula_callable {
00066 public:
00067     typedef map_location location;
00068     terrain_callable(const terrain_type& t, const location& loc)
00069       : loc_(loc), t_(t)
00070     {
00071         type_ = TERRAIN_C;
00072     }
00073 
00074     variant get_value(const std::string& key) const;
00075     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00076 
00077         int do_compare(const formula_callable* callable) const;
00078 private:
00079     const location loc_;
00080     const terrain_type &t_;
00081 };
00082 
00083 CALLABLE_WRAPPER_START(gamemap)
00084 CALLABLE_WRAPPER_INPUT(terrain)
00085 CALLABLE_WRAPPER_INPUT(w)
00086 CALLABLE_WRAPPER_INPUT(h)
00087 CALLABLE_WRAPPER_INPUT_END
00088     if(key == "terrain") {
00089         int w = object_.w();
00090         int h = object_.h();
00091         std::vector<variant> vars;
00092         for(int i = 0;i < w; i++) {
00093             for(int j = 0;j < h; j++) {
00094                 const map_location loc(i,j);
00095                 vars.push_back(variant(new terrain_callable(object_.get_terrain_info(loc), loc)));
00096             }
00097         }
00098         return variant(&vars);
00099     } else
00100     CALLABLE_WRAPPER_FN(w)
00101     CALLABLE_WRAPPER_FN(h)
00102 CALLABLE_WRAPPER_END
00103 
00104 class location_callable : public game_logic::formula_callable {
00105     map_location loc_;
00106 
00107     variant get_value(const std::string& key) const;
00108 
00109     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00110     int do_compare(const game_logic::formula_callable* callable) const;
00111 public:
00112     explicit location_callable(const map_location& loc) : loc_(loc)
00113     {
00114         type_ = LOCATION_C;
00115     }
00116     explicit location_callable(int x, int y) : loc_(map_location(x,y))
00117     {}
00118 
00119     const map_location& loc() const { return loc_; }
00120 
00121     void serialize_to_string(std::string& str) const;
00122 };
00123 
00124 
00125 class attack_type_callable : public game_logic::formula_callable {
00126 public:
00127     typedef map_location location;
00128     attack_type_callable(const attack_type& attack)
00129       : att_(attack)
00130     {
00131         type_ = ATTACK_TYPE_C;
00132     }
00133 
00134     const attack_type& get_attack_type() const { return att_; }
00135     variant get_value(const std::string& key) const;
00136     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00137 
00138     int do_compare(const formula_callable* callable) const;
00139 private:
00140     const attack_type att_;
00141 };
00142 
00143 
00144 class unit_callable : public game_logic::formula_callable {
00145 public:
00146     typedef map_location location;
00147     unit_callable(const std::pair<location, unit>& pair)
00148       : loc_(pair.first), u_(pair.second)
00149     {
00150         type_ = UNIT_C;
00151     }
00152 
00153     unit_callable(const unit &u)
00154         : loc_(u.get_location()), u_(u)
00155     {
00156         type_ = UNIT_C;
00157     }
00158 
00159     const unit& get_unit() const { return u_; }
00160     const location& get_location() const { return loc_; }
00161     variant get_value(const std::string& key) const;
00162     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00163 
00164     int do_compare(const formula_callable* callable) const;
00165 private:
00166     const location& loc_;
00167     const unit& u_;
00168 };
00169 
00170 
00171 class unit_type_callable : public game_logic::formula_callable {
00172 public:
00173     unit_type_callable(const unit_type& u)
00174       : u_(u)
00175     {
00176         type_ = UNIT_TYPE_C;
00177     }
00178 
00179     const unit_type& get_unit_type() const { return u_; }
00180     variant get_value(const std::string& key) const;
00181     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00182 
00183     int do_compare(const formula_callable* callable) const;
00184 private:
00185     const unit_type& u_;
00186 };
00187 
00188 
00189 CALLABLE_WRAPPER_START(team)
00190 CALLABLE_WRAPPER_INPUT(gold)
00191 CALLABLE_WRAPPER_INPUT(start_gold)
00192 CALLABLE_WRAPPER_INPUT(base_income)
00193 CALLABLE_WRAPPER_INPUT(village_gold)
00194 CALLABLE_WRAPPER_INPUT(village_support)
00195 CALLABLE_WRAPPER_INPUT(name)
00196 CALLABLE_WRAPPER_INPUT(is_human)
00197 CALLABLE_WRAPPER_INPUT(is_ai)
00198 CALLABLE_WRAPPER_INPUT(is_network)
00199 CALLABLE_WRAPPER_INPUT_END
00200 CALLABLE_WRAPPER_FN(gold)
00201     if(key == "start_gold") { \
00202         return variant(lexical_cast<int>(object_.start_gold())); \
00203     } else
00204 CALLABLE_WRAPPER_FN(base_income)
00205 CALLABLE_WRAPPER_FN(village_gold)
00206 CALLABLE_WRAPPER_FN(village_support)
00207 CALLABLE_WRAPPER_FN(name)
00208 CALLABLE_WRAPPER_FN(is_human)
00209 CALLABLE_WRAPPER_FN(is_ai)
00210 CALLABLE_WRAPPER_FN(is_network)
00211 CALLABLE_WRAPPER_END
00212 
00213 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Wed May 23 2012 01:02:31 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs