3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_nodetimer.h"
21 #include "lua_api/l_internal.h"
22 #include "serverenvironment.h"
26 int NodeTimerRef::gc_object(lua_State *L) {
27 NodeTimerRef *o = *(NodeTimerRef **)(lua_touserdata(L, 1));
32 NodeTimerRef* NodeTimerRef::checkobject(lua_State *L, int narg)
34 luaL_checktype(L, narg, LUA_TUSERDATA);
35 void *ud = luaL_checkudata(L, narg, className);
36 if(!ud) luaL_typerror(L, narg, className);
37 return *(NodeTimerRef**)ud; // unbox pointer
40 int NodeTimerRef::l_set(lua_State *L)
43 NodeTimerRef *o = checkobject(L, 1);
44 f32 t = readParam<float>(L,2);
45 f32 e = readParam<float>(L,3);
46 o->m_map->setNodeTimer(NodeTimer(t, e, o->m_p));
50 int NodeTimerRef::l_start(lua_State *L)
53 NodeTimerRef *o = checkobject(L, 1);
54 f32 t = readParam<float>(L,2);
55 o->m_map->setNodeTimer(NodeTimer(t, 0, o->m_p));
59 int NodeTimerRef::l_stop(lua_State *L)
62 NodeTimerRef *o = checkobject(L, 1);
63 o->m_map->removeNodeTimer(o->m_p);
67 int NodeTimerRef::l_is_started(lua_State *L)
70 NodeTimerRef *o = checkobject(L, 1);
71 NodeTimer t = o->m_map->getNodeTimer(o->m_p);
72 lua_pushboolean(L,(t.timeout != 0));
76 int NodeTimerRef::l_get_timeout(lua_State *L)
79 NodeTimerRef *o = checkobject(L, 1);
80 NodeTimer t = o->m_map->getNodeTimer(o->m_p);
81 lua_pushnumber(L,t.timeout);
85 int NodeTimerRef::l_get_elapsed(lua_State *L)
88 NodeTimerRef *o = checkobject(L, 1);
89 NodeTimer t = o->m_map->getNodeTimer(o->m_p);
90 lua_pushnumber(L,t.elapsed);
94 // Creates an NodeTimerRef and leaves it on top of stack
95 // Not callable from Lua; all references are created on the C side.
96 void NodeTimerRef::create(lua_State *L, v3s16 p, ServerMap *map)
98 NodeTimerRef *o = new NodeTimerRef(p, map);
99 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
100 luaL_getmetatable(L, className);
101 lua_setmetatable(L, -2);
104 void NodeTimerRef::Register(lua_State *L)
107 int methodtable = lua_gettop(L);
108 luaL_newmetatable(L, className);
109 int metatable = lua_gettop(L);
111 lua_pushliteral(L, "__metatable");
112 lua_pushvalue(L, methodtable);
113 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
115 lua_pushliteral(L, "__index");
116 lua_pushvalue(L, methodtable);
117 lua_settable(L, metatable);
119 lua_pushliteral(L, "__gc");
120 lua_pushcfunction(L, gc_object);
121 lua_settable(L, metatable);
123 lua_pop(L, 1); // drop metatable
125 luaL_openlib(L, 0, methods, 0); // fill methodtable
126 lua_pop(L, 1); // drop methodtable
128 // Cannot be created from Lua
129 //lua_register(L, className, create_object);
132 const char NodeTimerRef::className[] = "NodeTimerRef";
133 const luaL_Reg NodeTimerRef::methods[] = {
134 luamethod(NodeTimerRef, start),
135 luamethod(NodeTimerRef, set),
136 luamethod(NodeTimerRef, stop),
137 luamethod(NodeTimerRef, is_started),
138 luamethod(NodeTimerRef, get_timeout),
139 luamethod(NodeTimerRef, get_elapsed),