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 "scriptapi.h"
21 #include "scriptapi_nodetimer.h"
25 int NodeTimerRef::gc_object(lua_State *L) {
26 NodeTimerRef *o = *(NodeTimerRef **)(lua_touserdata(L, 1));
31 NodeTimerRef* NodeTimerRef::checkobject(lua_State *L, int narg)
33 luaL_checktype(L, narg, LUA_TUSERDATA);
34 void *ud = luaL_checkudata(L, narg, className);
35 if(!ud) luaL_typerror(L, narg, className);
36 return *(NodeTimerRef**)ud; // unbox pointer
39 int NodeTimerRef::l_set(lua_State *L)
41 NodeTimerRef *o = checkobject(L, 1);
42 ServerEnvironment *env = o->m_env;
43 if(env == NULL) return 0;
44 f32 t = luaL_checknumber(L,2);
45 f32 e = luaL_checknumber(L,3);
46 env->getMap().setNodeTimer(o->m_p,NodeTimer(t,e));
50 int NodeTimerRef::l_start(lua_State *L)
52 NodeTimerRef *o = checkobject(L, 1);
53 ServerEnvironment *env = o->m_env;
54 if(env == NULL) return 0;
55 f32 t = luaL_checknumber(L,2);
56 env->getMap().setNodeTimer(o->m_p,NodeTimer(t,0));
60 int NodeTimerRef::l_stop(lua_State *L)
62 NodeTimerRef *o = checkobject(L, 1);
63 ServerEnvironment *env = o->m_env;
64 if(env == NULL) return 0;
65 env->getMap().removeNodeTimer(o->m_p);
69 int NodeTimerRef::l_is_started(lua_State *L)
71 NodeTimerRef *o = checkobject(L, 1);
72 ServerEnvironment *env = o->m_env;
73 if(env == NULL) return 0;
75 NodeTimer t = env->getMap().getNodeTimer(o->m_p);
76 lua_pushboolean(L,(t.timeout != 0));
80 int NodeTimerRef::l_get_timeout(lua_State *L)
82 NodeTimerRef *o = checkobject(L, 1);
83 ServerEnvironment *env = o->m_env;
84 if(env == NULL) return 0;
86 NodeTimer t = env->getMap().getNodeTimer(o->m_p);
87 lua_pushnumber(L,t.timeout);
91 int NodeTimerRef::l_get_elapsed(lua_State *L)
93 NodeTimerRef *o = checkobject(L, 1);
94 ServerEnvironment *env = o->m_env;
95 if(env == NULL) return 0;
97 NodeTimer t = env->getMap().getNodeTimer(o->m_p);
98 lua_pushnumber(L,t.elapsed);
103 NodeTimerRef::NodeTimerRef(v3s16 p, ServerEnvironment *env):
109 NodeTimerRef::~NodeTimerRef()
113 // Creates an NodeTimerRef and leaves it on top of stack
114 // Not callable from Lua; all references are created on the C side.
115 void NodeTimerRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
117 NodeTimerRef *o = new NodeTimerRef(p, env);
118 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
119 luaL_getmetatable(L, className);
120 lua_setmetatable(L, -2);
123 void NodeTimerRef::set_null(lua_State *L)
125 NodeTimerRef *o = checkobject(L, -1);
129 void NodeTimerRef::Register(lua_State *L)
132 int methodtable = lua_gettop(L);
133 luaL_newmetatable(L, className);
134 int metatable = lua_gettop(L);
136 lua_pushliteral(L, "__metatable");
137 lua_pushvalue(L, methodtable);
138 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
140 lua_pushliteral(L, "__index");
141 lua_pushvalue(L, methodtable);
142 lua_settable(L, metatable);
144 lua_pushliteral(L, "__gc");
145 lua_pushcfunction(L, gc_object);
146 lua_settable(L, metatable);
148 lua_pop(L, 1); // drop metatable
150 luaL_openlib(L, 0, methods, 0); // fill methodtable
151 lua_pop(L, 1); // drop methodtable
153 // Cannot be created from Lua
154 //lua_register(L, className, create_object);
157 const char NodeTimerRef::className[] = "NodeTimerRef";
158 const luaL_reg NodeTimerRef::methods[] = {
159 luamethod(NodeTimerRef, start),
160 luamethod(NodeTimerRef, set),
161 luamethod(NodeTimerRef, stop),
162 luamethod(NodeTimerRef, is_started),
163 luamethod(NodeTimerRef, get_timeout),
164 luamethod(NodeTimerRef, get_elapsed),