]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_nodetimer.cpp
SAPI: Mark all Lua API functions requiring envlock
[dragonfireclient.git] / src / script / lua_api / l_nodetimer.cpp
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 int NodeTimerRef::gc_object(lua_State *L) {
27         NodeTimerRef *o = *(NodeTimerRef **)(lua_touserdata(L, 1));
28         delete o;
29         return 0;
30 }
31
32 NodeTimerRef* NodeTimerRef::checkobject(lua_State *L, int narg)
33 {
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
38 }
39
40 int NodeTimerRef::l_set(lua_State *L)
41 {
42         MAP_LOCK_REQUIRED;
43         NodeTimerRef *o = checkobject(L, 1);
44         ServerEnvironment *env = o->m_env;
45         if(env == NULL) return 0;
46         f32 t = luaL_checknumber(L,2);
47         f32 e = luaL_checknumber(L,3);
48         env->getMap().setNodeTimer(o->m_p,NodeTimer(t,e));
49         return 0;
50 }
51
52 int NodeTimerRef::l_start(lua_State *L)
53 {
54         MAP_LOCK_REQUIRED;
55         NodeTimerRef *o = checkobject(L, 1);
56         ServerEnvironment *env = o->m_env;
57         if(env == NULL) return 0;
58         f32 t = luaL_checknumber(L,2);
59         env->getMap().setNodeTimer(o->m_p,NodeTimer(t,0));
60         return 0;
61 }
62
63 int NodeTimerRef::l_stop(lua_State *L)
64 {
65         MAP_LOCK_REQUIRED;
66         NodeTimerRef *o = checkobject(L, 1);
67         ServerEnvironment *env = o->m_env;
68         if(env == NULL) return 0;
69         env->getMap().removeNodeTimer(o->m_p);
70         return 0;
71 }
72
73 int NodeTimerRef::l_is_started(lua_State *L)
74 {
75         MAP_LOCK_REQUIRED;
76         NodeTimerRef *o = checkobject(L, 1);
77         ServerEnvironment *env = o->m_env;
78         if(env == NULL) return 0;
79
80         NodeTimer t = env->getMap().getNodeTimer(o->m_p);
81         lua_pushboolean(L,(t.timeout != 0));
82         return 1;
83 }
84
85 int NodeTimerRef::l_get_timeout(lua_State *L)
86 {
87         MAP_LOCK_REQUIRED;
88         NodeTimerRef *o = checkobject(L, 1);
89         ServerEnvironment *env = o->m_env;
90         if(env == NULL) return 0;
91
92         NodeTimer t = env->getMap().getNodeTimer(o->m_p);
93         lua_pushnumber(L,t.timeout);
94         return 1;
95 }
96
97 int NodeTimerRef::l_get_elapsed(lua_State *L)
98 {
99         MAP_LOCK_REQUIRED;
100         NodeTimerRef *o = checkobject(L, 1);
101         ServerEnvironment *env = o->m_env;
102         if(env == NULL) return 0;
103
104         NodeTimer t = env->getMap().getNodeTimer(o->m_p);
105         lua_pushnumber(L,t.elapsed);
106         return 1;
107 }
108
109
110 NodeTimerRef::NodeTimerRef(v3s16 p, ServerEnvironment *env):
111         m_p(p),
112         m_env(env)
113 {
114 }
115
116 NodeTimerRef::~NodeTimerRef()
117 {
118 }
119
120 // Creates an NodeTimerRef and leaves it on top of stack
121 // Not callable from Lua; all references are created on the C side.
122 void NodeTimerRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
123 {
124         NodeTimerRef *o = new NodeTimerRef(p, env);
125         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
126         luaL_getmetatable(L, className);
127         lua_setmetatable(L, -2);
128 }
129
130 void NodeTimerRef::set_null(lua_State *L)
131 {
132         NodeTimerRef *o = checkobject(L, -1);
133         o->m_env = NULL;
134 }
135
136 void NodeTimerRef::Register(lua_State *L)
137 {
138         lua_newtable(L);
139         int methodtable = lua_gettop(L);
140         luaL_newmetatable(L, className);
141         int metatable = lua_gettop(L);
142
143         lua_pushliteral(L, "__metatable");
144         lua_pushvalue(L, methodtable);
145         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
146
147         lua_pushliteral(L, "__index");
148         lua_pushvalue(L, methodtable);
149         lua_settable(L, metatable);
150
151         lua_pushliteral(L, "__gc");
152         lua_pushcfunction(L, gc_object);
153         lua_settable(L, metatable);
154
155         lua_pop(L, 1);  // drop metatable
156
157         luaL_openlib(L, 0, methods, 0);  // fill methodtable
158         lua_pop(L, 1);  // drop methodtable
159
160         // Cannot be created from Lua
161         //lua_register(L, className, create_object);
162 }
163
164 const char NodeTimerRef::className[] = "NodeTimerRef";
165 const luaL_reg NodeTimerRef::methods[] = {
166         luamethod(NodeTimerRef, start),
167         luamethod(NodeTimerRef, set),
168         luamethod(NodeTimerRef, stop),
169         luamethod(NodeTimerRef, is_started),
170         luamethod(NodeTimerRef, get_timeout),
171         luamethod(NodeTimerRef, get_elapsed),
172         {0,0}
173 };