00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _TMWSERV_SCRIPTING_LUAUTIL_HPP
00023 #define _TMWSERV_SCRIPTING_LUAUTIL_HPP
00024
00025 extern "C" {
00026 #include <lualib.h>
00027 #include <lauxlib.h>
00028 }
00029 #include <string>
00030 #include <list>
00031 #include <map>
00032 #include <set>
00033 #include <vector>
00034
00035 class Being;
00036 class NPC;
00037 class Character;
00038 class Thing;
00039
00040 void raiseScriptError(lua_State *s, const char *format, ...);
00041
00042 NPC *getNPC(lua_State *s, int p);
00043 Character *getCharacter(lua_State *s, int p);
00044 Being *getBeing(lua_State *s, int p);
00045
00046
00047
00048
00049 void push(lua_State *s, int val);
00050 void push(lua_State *s, const std::string &val);
00051 void push(lua_State *s, Thing* val);
00052 void push(lua_State *s, double val);
00053
00054
00055
00056 template <typename T> void pushSTLContainer(lua_State *s, const std::list<T> &container)
00057 {
00058 int len = container.size();
00059 lua_newtable(s);
00060 int table = lua_gettop(s);
00061 typename std::list<T>::const_iterator i;
00062 i = container.begin();
00063
00064 for (int key = 1; key <= len; key++)
00065 {
00066 push(s, key);
00067 push(s, *i);
00068 lua_settable(s, table);
00069 i++;
00070 }
00071 }
00072
00073
00074 template <typename T> void pushSTLContainer(lua_State *s, const std::vector<T> &container)
00075 {
00076 int len = container.size();
00077 lua_createtable(s, 0, len);
00078 int table = lua_gettop(s);
00079
00080 for (int key = 0; key < len; key++)
00081 {
00082 push(s, key+1);
00083 push(s, container.at(key).c_str());
00084 lua_settable(s, table);
00085 }
00086 }
00087
00088
00089 template <typename Tkey, typename Tval> void pushSTLContainer(lua_State *s, const std::map<Tkey, Tval> &container)
00090 {
00091 int len = container.size();
00092 lua_createtable(s, 0, len);
00093 int table = lua_gettop(s);
00094 typename std::map<Tkey, Tval>::const_iterator i;
00095 i = container.begin();
00096
00097 for (int key = 1; key <= len; key++)
00098 {
00099 push(s, i->first.c_str());
00100 push(s, i->second.c_str());
00101 lua_settable(s, table);
00102 i++;
00103 }
00104 }
00105
00106
00107 template <typename T> void pushSTLContainer(lua_State *s, const std::set<T> &container)
00108 {
00109 int len = container.size();
00110 lua_newtable(s);
00111 int table = lua_gettop(s);
00112 typename std::set<T>::const_iterator i;
00113 i = container.begin();
00114
00115 for (int key = 1; key <= len; key++)
00116 {
00117 push(s, key);
00118 push(s, *i);
00119 lua_settable(s, table);
00120 i++;
00121 }
00122 }
00123
00124 #endif