]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_nodetimer.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[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         NodeTimerRef *o = checkobject(L, 1);
43         ServerEnvironment *env = o->m_env;
44         if(env == NULL) return 0;
45         f32 t = luaL_checknumber(L,2);
46         f32 e = luaL_checknumber(L,3);
47         env->getMap().setNodeTimer(o->m_p,NodeTimer(t,e));
48         return 0;
49 }
50
51 int NodeTimerRef::l_start(lua_State *L)
52 {
53         NodeTimerRef *o = checkobject(L, 1);
54         ServerEnvironment *env = o->m_env;
55         if(env == NULL) return 0;
56         f32 t = luaL_checknumber(L,2);
57         env->getMap().setNodeTimer(o->m_p,NodeTimer(t,0));
58         return 0;
59 }
60
61 int NodeTimerRef::l_stop(lua_State *L)
62 {
63         NodeTimerRef *o = checkobject(L, 1);
64         ServerEnvironment *env = o->m_env;
65         if(env == NULL) return 0;
66         env->getMap().removeNodeTimer(o->m_p);
67         return 0;
68 }
69
70 int NodeTimerRef::l_is_started(lua_State *L)
71 {
72         NodeTimerRef *o = checkobject(L, 1);
73         ServerEnvironment *env = o->m_env;
74         if(env == NULL) return 0;
75
76         NodeTimer t = env->getMap().getNodeTimer(o->m_p);
77         lua_pushboolean(L,(t.timeout != 0));
78         return 1;
79 }
80
81 int NodeTimerRef::l_get_timeout(lua_State *L)
82 {
83         NodeTimerRef *o = checkobject(L, 1);
84         ServerEnvironment *env = o->m_env;
85         if(env == NULL) return 0;
86
87         NodeTimer t = env->getMap().getNodeTimer(o->m_p);
88         lua_pushnumber(L,t.timeout);
89         return 1;
90 }
91
92 int NodeTimerRef::l_get_elapsed(lua_State *L)
93 {
94         NodeTimerRef *o = checkobject(L, 1);
95         ServerEnvironment *env = o->m_env;
96         if(env == NULL) return 0;
97
98         NodeTimer t = env->getMap().getNodeTimer(o->m_p);
99         lua_pushnumber(L,t.elapsed);
100         return 1;
101 }
102
103
104 NodeTimerRef::NodeTimerRef(v3s16 p, ServerEnvironment *env):
105         m_p(p),
106         m_env(env)
107 {
108 }
109
110 NodeTimerRef::~NodeTimerRef()
111 {
112 }
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 void NodeTimerRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
117 {
118         NodeTimerRef *o = new NodeTimerRef(p, env);
119         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
120         luaL_getmetatable(L, className);
121         lua_setmetatable(L, -2);
122 }
123
124 void NodeTimerRef::set_null(lua_State *L)
125 {
126         NodeTimerRef *o = checkobject(L, -1);
127         o->m_env = NULL;
128 }
129
130 void NodeTimerRef::Register(lua_State *L)
131 {
132         lua_newtable(L);
133         int methodtable = lua_gettop(L);
134         luaL_newmetatable(L, className);
135         int metatable = lua_gettop(L);
136
137         lua_pushliteral(L, "__metatable");
138         lua_pushvalue(L, methodtable);
139         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
140
141         lua_pushliteral(L, "__index");
142         lua_pushvalue(L, methodtable);
143         lua_settable(L, metatable);
144
145         lua_pushliteral(L, "__gc");
146         lua_pushcfunction(L, gc_object);
147         lua_settable(L, metatable);
148
149         lua_pop(L, 1);  // drop metatable
150
151         luaL_openlib(L, 0, methods, 0);  // fill methodtable
152         lua_pop(L, 1);  // drop methodtable
153
154         // Cannot be created from Lua
155         //lua_register(L, className, create_object);
156 }
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 };