00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef LUA_OBJECT_HPP_INCLUDED
00023 #define LUA_OBJECT_HPP_INCLUDED
00024
00025 #include <string>
00026 #include <vector>
00027 #include <boost/shared_ptr.hpp>
00028
00029 #include "lua/lualib.h"
00030 #include "../../scripting/lua_api.hpp"
00031 #include "config.hpp"
00032 #include "../default/contexts.hpp"
00033 #include "terrain_filter.hpp"
00034 #include "resources.hpp"
00035
00036 namespace ai {
00037
00038
00039 class lua_object_base {
00040
00041 public:
00042 lua_object_base();
00043 virtual ~lua_object_base() {}
00044
00045 virtual void store(lua_State* L, int n) = 0;
00046 };
00047
00048 template <typename T>
00049 class lua_object : public lua_object_base
00050 {
00051
00052 public:
00053
00054 lua_object()
00055 : value_()
00056 {
00057
00058 }
00059
00060 boost::shared_ptr<T> get()
00061 {
00062 return value_;
00063 }
00064
00065 void store(lua_State* L, int n)
00066 {
00067 this->value_ = boost::shared_ptr<T>(to_type(L, n));
00068 }
00069
00070 protected:
00071
00072
00073 boost::shared_ptr<T> to_type(lua_State *, int)
00074 {
00075 return boost::shared_ptr<T>();
00076 }
00077
00078 boost::shared_ptr<T> value_;
00079 };
00080
00081 template <>
00082 inline boost::shared_ptr<double> lua_object<double>::to_type(lua_State *L, int n)
00083 {
00084 return boost::shared_ptr<double>(new double(lua_tonumber(L, n)));
00085 }
00086
00087 template <>
00088 inline boost::shared_ptr<std::string> lua_object<std::string>::to_type(lua_State *L, int n)
00089 {
00090 return boost::shared_ptr<std::string>(new std::string(lua_tostring(L, n)));
00091 }
00092
00093 template <>
00094 inline boost::shared_ptr<bool> lua_object<bool>::to_type(lua_State *L, int n)
00095 {
00096 #ifdef _MSC_VER
00097 #pragma warning (push)
00098 #pragma warning (disable : 4800)
00099 #endif
00100
00101 return boost::shared_ptr<bool>(new bool(lua_toboolean(L, n)));
00102
00103 #ifdef _MSC_VER
00104 #pragma warning (pop)
00105 #endif
00106 }
00107
00108 template <>
00109 inline boost::shared_ptr<int> lua_object<int>::to_type(lua_State *L, int n)
00110 {
00111 return boost::shared_ptr<int>(new int(lua_tointeger(L, n)));
00112 }
00113
00114 template <>
00115 inline boost::shared_ptr< std::vector<std::string> > lua_object< std::vector<std::string> >::to_type(lua_State *L, int n)
00116 {
00117 boost::shared_ptr< std::vector<std::string> > v = boost::shared_ptr< std::vector<std::string> >(new std::vector<std::string>());
00118 int l = lua_rawlen(L, n);
00119 for (int i = 1; i < l + 1; ++i)
00120 {
00121 lua_pushinteger(L, i);
00122 lua_gettable(L, n);
00123 std::string s = lua_tostring(L, -1);
00124 lua_settop(L, n);
00125 v->push_back(s);
00126 }
00127
00128 return v;
00129 }
00130
00131 template <>
00132 inline boost::shared_ptr<config> lua_object<config>::to_type(lua_State *L, int n)
00133 {
00134 boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config());
00135 luaW_toconfig(L, n, *cfg);
00136 return cfg;
00137 }
00138
00139 template <>
00140 inline boost::shared_ptr<terrain_filter> lua_object<terrain_filter>::to_type(lua_State *L, int n)
00141 {
00142
00143 boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config());
00144 boost::shared_ptr<vconfig> vcfg = boost::shared_ptr<vconfig>(new vconfig(*cfg));
00145 luaW_tovconfig(L, n, *vcfg);
00146 boost::shared_ptr<terrain_filter> tf = boost::shared_ptr<terrain_filter>(new terrain_filter(*vcfg, *resources::units));
00147 return tf;
00148 }
00149
00150 template <>
00151 inline boost::shared_ptr<std::vector<target> > lua_object< std::vector<target> >::to_type(lua_State *L, int n)
00152 {
00153 boost::shared_ptr<std::vector<target> > targets = boost::shared_ptr<std::vector<target> >(new std::vector<target>());
00154 std::back_insert_iterator< std::vector<target> > tg(*targets);
00155 int l = lua_rawlen(L, n);
00156
00157 for (int i = 1; i <= l; ++i)
00158 {
00159 lua_rawgeti(L, n, i);
00160
00161 lua_pushstring(L, "loc");
00162 lua_rawget(L, -2);
00163
00164 lua_pushstring(L, "x");
00165 lua_rawget(L, -2);
00166 int x = lua_tointeger(L, -1);
00167 lua_pop(L, 1);
00168
00169 lua_pushstring(L, "y");
00170 lua_rawget(L, -2);
00171 int y = lua_tointeger(L, -1);
00172
00173 lua_pop(L, 2);
00174
00175 lua_pushstring(L, "type");
00176 lua_rawget(L, -2);
00177 target::TYPE type = static_cast<target::TYPE>(lua_tointeger(L, -1));
00178 lua_pop(L, 1);
00179
00180
00181 lua_pushstring(L, "value");
00182 lua_rawget(L, -2);
00183 int value = lua_tointeger(L, -1);
00184
00185 map_location ml(x - 1, y - 1);
00186
00187 *tg = target(ml, value, type);
00188 }
00189
00190 lua_settop(L, n);
00191 return targets;
00192 }
00193
00194 }
00195
00196
00197 #endif