]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_minimap.cpp
bbe9aef8275b5a4e803e1949ba2ad21fd06caa0b
[dragonfireclient.git] / src / script / lua_api / l_minimap.cpp
1 /*
2 Minetest
3 Copyright (C) 2017 Loic Blot <loic.blot@unix-experience.fr>
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
21 #include "lua_api/l_minimap.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_converter.h"
24 #include "minimap.h"
25
26 LuaMinimap::LuaMinimap(Minimap *m)
27 {
28         m_minimap = m;
29 }
30
31 void LuaMinimap::create(lua_State *L, Minimap *m)
32 {
33         LuaMinimap *o = new LuaMinimap(m);
34         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
35         luaL_getmetatable(L, className);
36         lua_setmetatable(L, -2);
37
38         // Keep minimap object stack id
39         int minimap_object = lua_gettop(L);
40
41         lua_getglobal(L, "core");
42         lua_getfield(L, -1, "ui");
43         luaL_checktype(L, -1, LUA_TTABLE);
44         int uitable = lua_gettop(L);
45
46         lua_pushvalue(L, minimap_object); // Copy object to top of stack
47         lua_setfield(L, uitable, "minimap");
48 }
49
50 int LuaMinimap::l_get_pos(lua_State *L)
51 {
52         LuaMinimap *ref = checkobject(L, 1);
53         Minimap *m = getobject(ref);
54
55         push_v3s16(L, m->getPos());
56         return 1;
57 }
58
59 int LuaMinimap::l_set_pos(lua_State *L)
60 {
61         LuaMinimap *ref = checkobject(L, 1);
62         Minimap *m = getobject(ref);
63
64         m->setPos(read_v3s16(L, 2));
65         return 1;
66 }
67
68 int LuaMinimap::l_get_angle(lua_State *L)
69 {
70         LuaMinimap *ref = checkobject(L, 1);
71         Minimap *m = getobject(ref);
72
73         lua_pushinteger(L, m->getAngle());
74         return 1;
75 }
76
77 int LuaMinimap::l_set_angle(lua_State *L)
78 {
79         LuaMinimap *ref = checkobject(L, 1);
80         Minimap *m = getobject(ref);
81
82         m->setAngle(lua_tointeger(L, 2));
83         return 1;
84 }
85
86 int LuaMinimap::l_get_mode(lua_State *L)
87 {
88         LuaMinimap *ref = checkobject(L, 1);
89         Minimap *m = getobject(ref);
90
91         lua_pushinteger(L, m->getMinimapMode());
92         return 1;
93 }
94
95 int LuaMinimap::l_set_mode(lua_State *L)
96 {
97         LuaMinimap *ref = checkobject(L, 1);
98         Minimap *m = getobject(ref);
99
100         s32 mode = lua_tointeger(L, 2);
101         if (mode < MINIMAP_MODE_OFF ||
102                 mode >= MINIMAP_MODE_COUNT) {
103                 return 0;
104         }
105
106         m->setMinimapMode((MinimapMode) mode);
107         return 1;
108 }
109
110 int LuaMinimap::l_toggle_shape(lua_State *L)
111 {
112         LuaMinimap *ref = checkobject(L, 1);
113         Minimap *m = getobject(ref);
114
115         m->toggleMinimapShape();
116         return 1;
117 }
118
119 int LuaMinimap::l_show(lua_State *L)
120 {
121         Client *client = getClient(L);
122         assert(client);
123         client->setMinimapShownByMod(true);
124         return 1;
125 }
126
127 int LuaMinimap::l_hide(lua_State *L)
128 {
129         Client *client = getClient(L);
130         assert(client);
131         client->setMinimapShownByMod(false);
132         return 1;
133 }
134
135 LuaMinimap *LuaMinimap::checkobject(lua_State *L, int narg)
136 {
137         NO_MAP_LOCK_REQUIRED;
138
139         luaL_checktype(L, narg, LUA_TUSERDATA);
140
141         void *ud = luaL_checkudata(L, narg, className);
142         if (!ud)
143                 luaL_typerror(L, narg, className);
144
145         return *(LuaMinimap **)ud;  // unbox pointer
146 }
147
148 Minimap* LuaMinimap::getobject(LuaMinimap *ref)
149 {
150         return ref->m_minimap;
151 }
152
153 int LuaMinimap::gc_object(lua_State *L) {
154         LuaMinimap *o = *(LuaMinimap **)(lua_touserdata(L, 1));
155         delete o;
156         return 0;
157 }
158
159 void LuaMinimap::Register(lua_State *L)
160 {
161         lua_newtable(L);
162         int methodtable = lua_gettop(L);
163         luaL_newmetatable(L, className);
164         int metatable = lua_gettop(L);
165
166         lua_pushliteral(L, "__metatable");
167         lua_pushvalue(L, methodtable);
168         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
169
170         lua_pushliteral(L, "__index");
171         lua_pushvalue(L, methodtable);
172         lua_settable(L, metatable);
173
174         lua_pushliteral(L, "__gc");
175         lua_pushcfunction(L, gc_object);
176         lua_settable(L, metatable);
177
178         lua_pop(L, 1);  // drop metatable
179
180         luaL_openlib(L, 0, methods, 0);  // fill methodtable
181         lua_pop(L, 1);  // drop methodtable
182 }
183
184 const char LuaMinimap::className[] = "Minimap";
185 const luaL_reg LuaMinimap::methods[] = {
186         luamethod(LuaMinimap, show),
187         luamethod(LuaMinimap, hide),
188         luamethod(LuaMinimap, get_pos),
189         luamethod(LuaMinimap, set_pos),
190         luamethod(LuaMinimap, get_angle),
191         luamethod(LuaMinimap, set_angle),
192         luamethod(LuaMinimap, get_mode),
193         luamethod(LuaMinimap, set_mode),
194         luamethod(LuaMinimap, toggle_shape),
195         {0,0}
196 };