Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 :
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.
9 :
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.
14 :
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.
18 : */
19 :
20 : #include "lua_api/l_nodetimer.h"
21 : #include "lua_api/l_internal.h"
22 : #include "environment.h"
23 : #include "map.h"
24 :
25 :
26 0 : int NodeTimerRef::gc_object(lua_State *L) {
27 0 : NodeTimerRef *o = *(NodeTimerRef **)(lua_touserdata(L, 1));
28 0 : delete o;
29 0 : return 0;
30 : }
31 :
32 0 : NodeTimerRef* NodeTimerRef::checkobject(lua_State *L, int narg)
33 : {
34 0 : luaL_checktype(L, narg, LUA_TUSERDATA);
35 0 : void *ud = luaL_checkudata(L, narg, className);
36 0 : if(!ud) luaL_typerror(L, narg, className);
37 0 : return *(NodeTimerRef**)ud; // unbox pointer
38 : }
39 :
40 0 : int NodeTimerRef::l_set(lua_State *L)
41 : {
42 0 : NodeTimerRef *o = checkobject(L, 1);
43 0 : ServerEnvironment *env = o->m_env;
44 0 : if(env == NULL) return 0;
45 0 : f32 t = luaL_checknumber(L,2);
46 0 : f32 e = luaL_checknumber(L,3);
47 0 : env->getMap().setNodeTimer(o->m_p,NodeTimer(t,e));
48 0 : return 0;
49 : }
50 :
51 0 : int NodeTimerRef::l_start(lua_State *L)
52 : {
53 0 : NodeTimerRef *o = checkobject(L, 1);
54 0 : ServerEnvironment *env = o->m_env;
55 0 : if(env == NULL) return 0;
56 0 : f32 t = luaL_checknumber(L,2);
57 0 : env->getMap().setNodeTimer(o->m_p,NodeTimer(t,0));
58 0 : return 0;
59 : }
60 :
61 0 : int NodeTimerRef::l_stop(lua_State *L)
62 : {
63 0 : NodeTimerRef *o = checkobject(L, 1);
64 0 : ServerEnvironment *env = o->m_env;
65 0 : if(env == NULL) return 0;
66 0 : env->getMap().removeNodeTimer(o->m_p);
67 0 : return 0;
68 : }
69 :
70 0 : int NodeTimerRef::l_is_started(lua_State *L)
71 : {
72 0 : NodeTimerRef *o = checkobject(L, 1);
73 0 : ServerEnvironment *env = o->m_env;
74 0 : if(env == NULL) return 0;
75 :
76 0 : NodeTimer t = env->getMap().getNodeTimer(o->m_p);
77 0 : lua_pushboolean(L,(t.timeout != 0));
78 0 : return 1;
79 : }
80 :
81 0 : int NodeTimerRef::l_get_timeout(lua_State *L)
82 : {
83 0 : NodeTimerRef *o = checkobject(L, 1);
84 0 : ServerEnvironment *env = o->m_env;
85 0 : if(env == NULL) return 0;
86 :
87 0 : NodeTimer t = env->getMap().getNodeTimer(o->m_p);
88 0 : lua_pushnumber(L,t.timeout);
89 0 : return 1;
90 : }
91 :
92 0 : int NodeTimerRef::l_get_elapsed(lua_State *L)
93 : {
94 0 : NodeTimerRef *o = checkobject(L, 1);
95 0 : ServerEnvironment *env = o->m_env;
96 0 : if(env == NULL) return 0;
97 :
98 0 : NodeTimer t = env->getMap().getNodeTimer(o->m_p);
99 0 : lua_pushnumber(L,t.elapsed);
100 0 : return 1;
101 : }
102 :
103 :
104 0 : NodeTimerRef::NodeTimerRef(v3s16 p, ServerEnvironment *env):
105 : m_p(p),
106 0 : m_env(env)
107 : {
108 0 : }
109 :
110 0 : NodeTimerRef::~NodeTimerRef()
111 : {
112 0 : }
113 :
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 0 : void NodeTimerRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
117 : {
118 0 : NodeTimerRef *o = new NodeTimerRef(p, env);
119 0 : *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
120 0 : luaL_getmetatable(L, className);
121 0 : lua_setmetatable(L, -2);
122 0 : }
123 :
124 0 : void NodeTimerRef::set_null(lua_State *L)
125 : {
126 0 : NodeTimerRef *o = checkobject(L, -1);
127 0 : o->m_env = NULL;
128 0 : }
129 :
130 0 : void NodeTimerRef::Register(lua_State *L)
131 : {
132 0 : lua_newtable(L);
133 0 : int methodtable = lua_gettop(L);
134 0 : luaL_newmetatable(L, className);
135 0 : int metatable = lua_gettop(L);
136 :
137 0 : lua_pushliteral(L, "__metatable");
138 0 : lua_pushvalue(L, methodtable);
139 0 : lua_settable(L, metatable); // hide metatable from Lua getmetatable()
140 :
141 0 : lua_pushliteral(L, "__index");
142 0 : lua_pushvalue(L, methodtable);
143 0 : lua_settable(L, metatable);
144 :
145 0 : lua_pushliteral(L, "__gc");
146 0 : lua_pushcfunction(L, gc_object);
147 0 : lua_settable(L, metatable);
148 :
149 0 : lua_pop(L, 1); // drop metatable
150 :
151 0 : luaL_openlib(L, 0, methods, 0); // fill methodtable
152 0 : lua_pop(L, 1); // drop methodtable
153 :
154 : // Cannot be created from Lua
155 : //lua_register(L, className, create_object);
156 0 : }
157 :
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),
166 : {0,0}
167 3 : };
|