]> git.lizzy.rs Git - dragonfireclient.git/blob - src/scriptapi_nodetimer.cpp
unkn own block -> unkn own node
[dragonfireclient.git] / src / scriptapi_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 "scriptapi.h"
21 #include "scriptapi_nodetimer.h"
22 #include "map.h"
23
24
25 int NodeTimerRef::gc_object(lua_State *L) {
26         NodeTimerRef *o = *(NodeTimerRef **)(lua_touserdata(L, 1));
27         delete o;
28         return 0;
29 }
30
31 NodeTimerRef* NodeTimerRef::checkobject(lua_State *L, int narg)
32 {
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
37 }
38
39 int NodeTimerRef::l_set(lua_State *L)
40 {
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));
47         return 0;
48 }
49
50 int NodeTimerRef::l_start(lua_State *L)
51 {
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));
57         return 0;
58 }
59
60 int NodeTimerRef::l_stop(lua_State *L)
61 {
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);
66         return 0;
67 }
68
69 int NodeTimerRef::l_is_started(lua_State *L)
70 {
71         NodeTimerRef *o = checkobject(L, 1);
72         ServerEnvironment *env = o->m_env;
73         if(env == NULL) return 0;
74
75         NodeTimer t = env->getMap().getNodeTimer(o->m_p);
76         lua_pushboolean(L,(t.timeout != 0));
77         return 1;
78 }
79
80 int NodeTimerRef::l_get_timeout(lua_State *L)
81 {
82         NodeTimerRef *o = checkobject(L, 1);
83         ServerEnvironment *env = o->m_env;
84         if(env == NULL) return 0;
85
86         NodeTimer t = env->getMap().getNodeTimer(o->m_p);
87         lua_pushnumber(L,t.timeout);
88         return 1;
89 }
90
91 int NodeTimerRef::l_get_elapsed(lua_State *L)
92 {
93         NodeTimerRef *o = checkobject(L, 1);
94         ServerEnvironment *env = o->m_env;
95         if(env == NULL) return 0;
96
97         NodeTimer t = env->getMap().getNodeTimer(o->m_p);
98         lua_pushnumber(L,t.elapsed);
99         return 1;
100 }
101
102
103 NodeTimerRef::NodeTimerRef(v3s16 p, ServerEnvironment *env):
104         m_p(p),
105         m_env(env)
106 {
107 }
108
109 NodeTimerRef::~NodeTimerRef()
110 {
111 }
112
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)
116 {
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);
121 }
122
123 void NodeTimerRef::set_null(lua_State *L)
124 {
125         NodeTimerRef *o = checkobject(L, -1);
126         o->m_env = NULL;
127 }
128
129 void NodeTimerRef::Register(lua_State *L)
130 {
131         lua_newtable(L);
132         int methodtable = lua_gettop(L);
133         luaL_newmetatable(L, className);
134         int metatable = lua_gettop(L);
135
136         lua_pushliteral(L, "__metatable");
137         lua_pushvalue(L, methodtable);
138         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
139
140         lua_pushliteral(L, "__index");
141         lua_pushvalue(L, methodtable);
142         lua_settable(L, metatable);
143
144         lua_pushliteral(L, "__gc");
145         lua_pushcfunction(L, gc_object);
146         lua_settable(L, metatable);
147
148         lua_pop(L, 1);  // drop metatable
149
150         luaL_openlib(L, 0, methods, 0);  // fill methodtable
151         lua_pop(L, 1);  // drop methodtable
152
153         // Cannot be created from Lua
154         //lua_register(L, className, create_object);
155 }
156
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),
165         {0,0}
166 };