]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_nodetimer.cpp
c2df52c0523983c30fd6c7a1b516b7742e796468
[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 "serverenvironment.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         f32 t = readParam<float>(L,2);
45         f32 e = readParam<float>(L,3);
46         o->m_map->setNodeTimer(NodeTimer(t, e, o->m_p));
47         return 0;
48 }
49
50 int NodeTimerRef::l_start(lua_State *L)
51 {
52         MAP_LOCK_REQUIRED;
53         NodeTimerRef *o = checkobject(L, 1);
54         f32 t = readParam<float>(L,2);
55         o->m_map->setNodeTimer(NodeTimer(t, 0, o->m_p));
56         return 0;
57 }
58
59 int NodeTimerRef::l_stop(lua_State *L)
60 {
61         MAP_LOCK_REQUIRED;
62         NodeTimerRef *o = checkobject(L, 1);
63         o->m_map->removeNodeTimer(o->m_p);
64         return 0;
65 }
66
67 int NodeTimerRef::l_is_started(lua_State *L)
68 {
69         MAP_LOCK_REQUIRED;
70         NodeTimerRef *o = checkobject(L, 1);
71         NodeTimer t = o->m_map->getNodeTimer(o->m_p);
72         lua_pushboolean(L,(t.timeout != 0));
73         return 1;
74 }
75
76 int NodeTimerRef::l_get_timeout(lua_State *L)
77 {
78         MAP_LOCK_REQUIRED;
79         NodeTimerRef *o = checkobject(L, 1);
80         NodeTimer t = o->m_map->getNodeTimer(o->m_p);
81         lua_pushnumber(L,t.timeout);
82         return 1;
83 }
84
85 int NodeTimerRef::l_get_elapsed(lua_State *L)
86 {
87         MAP_LOCK_REQUIRED;
88         NodeTimerRef *o = checkobject(L, 1);
89         NodeTimer t = o->m_map->getNodeTimer(o->m_p);
90         lua_pushnumber(L,t.elapsed);
91         return 1;
92 }
93
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)
97 {
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);
102 }
103
104 void NodeTimerRef::Register(lua_State *L)
105 {
106         lua_newtable(L);
107         int methodtable = lua_gettop(L);
108         luaL_newmetatable(L, className);
109         int metatable = lua_gettop(L);
110
111         lua_pushliteral(L, "__metatable");
112         lua_pushvalue(L, methodtable);
113         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
114
115         lua_pushliteral(L, "__index");
116         lua_pushvalue(L, methodtable);
117         lua_settable(L, metatable);
118
119         lua_pushliteral(L, "__gc");
120         lua_pushcfunction(L, gc_object);
121         lua_settable(L, metatable);
122
123         lua_pop(L, 1);  // drop metatable
124
125         luaL_openlib(L, 0, methods, 0);  // fill methodtable
126         lua_pop(L, 1);  // drop methodtable
127
128         // Cannot be created from Lua
129         //lua_register(L, className, create_object);
130 }
131
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),
140         {0,0}
141 };