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 "environment.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)
42 NodeTimerRef *o = checkobject(L, 1);
43 ServerEnvironment *env = o->m_env;
44 if(env == NULL) return 0;
45 f32 t = luaL_checknumber(L,2);
46 f32 e = luaL_checknumber(L,3);
47 env->getMap().setNodeTimer(o->m_p,NodeTimer(t,e));
51 int NodeTimerRef::l_start(lua_State *L)
53 NodeTimerRef *o = checkobject(L, 1);
54 ServerEnvironment *env = o->m_env;
55 if(env == NULL) return 0;
56 f32 t = luaL_checknumber(L,2);
57 env->getMap().setNodeTimer(o->m_p,NodeTimer(t,0));
61 int NodeTimerRef::l_stop(lua_State *L)
63 NodeTimerRef *o = checkobject(L, 1);
64 ServerEnvironment *env = o->m_env;
65 if(env == NULL) return 0;
66 env->getMap().removeNodeTimer(o->m_p);
70 int NodeTimerRef::l_is_started(lua_State *L)
72 NodeTimerRef *o = checkobject(L, 1);
73 ServerEnvironment *env = o->m_env;
74 if(env == NULL) return 0;
76 NodeTimer t = env->getMap().getNodeTimer(o->m_p);
77 lua_pushboolean(L,(t.timeout != 0));
81 int NodeTimerRef::l_get_timeout(lua_State *L)
83 NodeTimerRef *o = checkobject(L, 1);
84 ServerEnvironment *env = o->m_env;
85 if(env == NULL) return 0;
87 NodeTimer t = env->getMap().getNodeTimer(o->m_p);
88 lua_pushnumber(L,t.timeout);
92 int NodeTimerRef::l_get_elapsed(lua_State *L)
94 NodeTimerRef *o = checkobject(L, 1);
95 ServerEnvironment *env = o->m_env;
96 if(env == NULL) return 0;
98 NodeTimer t = env->getMap().getNodeTimer(o->m_p);
99 lua_pushnumber(L,t.elapsed);
104 NodeTimerRef::NodeTimerRef(v3s16 p, ServerEnvironment *env):
110 NodeTimerRef::~NodeTimerRef()
114 // Creates an NodeTimerRef and leaves it on top of stack
115 // Not callable from Lua; all references are created on the C side.
116 void NodeTimerRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
118 NodeTimerRef *o = new NodeTimerRef(p, env);
119 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
120 luaL_getmetatable(L, className);
121 lua_setmetatable(L, -2);
124 void NodeTimerRef::set_null(lua_State *L)
126 NodeTimerRef *o = checkobject(L, -1);
130 void NodeTimerRef::Register(lua_State *L)
133 int methodtable = lua_gettop(L);
134 luaL_newmetatable(L, className);
135 int metatable = lua_gettop(L);
137 lua_pushliteral(L, "__metatable");
138 lua_pushvalue(L, methodtable);
139 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
141 lua_pushliteral(L, "__index");
142 lua_pushvalue(L, methodtable);
143 lua_settable(L, metatable);
145 lua_pushliteral(L, "__gc");
146 lua_pushcfunction(L, gc_object);
147 lua_settable(L, metatable);
149 lua_pop(L, 1); // drop metatable
151 luaL_openlib(L, 0, methods, 0); // fill methodtable
152 lua_pop(L, 1); // drop methodtable
154 // Cannot be created from Lua
155 //lua_register(L, className, create_object);
158 const char NodeTimerRef::className[] = "NodeTimerRef";
159 const luaL_reg NodeTimerRef::methods[] = {
160 luamethod(NodeTimerRef, start),
161 luamethod(NodeTimerRef, set),
162 luamethod(NodeTimerRef, stop),
163 luamethod(NodeTimerRef, is_started),
164 luamethod(NodeTimerRef, get_timeout),
165 luamethod(NodeTimerRef, get_elapsed),