3 Copyright (C) 2017 Loic Blot <loic.blot@unix-experience.fr>
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.
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.
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.
21 #include "lua_api/l_minimap.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_converter.h"
24 #include "client/client.h"
25 #include "client/minimap.h"
28 LuaMinimap::LuaMinimap(Minimap *m) : m_minimap(m)
32 void LuaMinimap::create(lua_State *L, Minimap *m)
34 LuaMinimap *o = new LuaMinimap(m);
35 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
36 luaL_getmetatable(L, className);
37 lua_setmetatable(L, -2);
39 // Keep minimap object stack id
40 int minimap_object = lua_gettop(L);
42 lua_getglobal(L, "core");
43 lua_getfield(L, -1, "ui");
44 luaL_checktype(L, -1, LUA_TTABLE);
45 int uitable = lua_gettop(L);
47 lua_pushvalue(L, minimap_object); // Copy object to top of stack
48 lua_setfield(L, uitable, "minimap");
51 int LuaMinimap::l_get_pos(lua_State *L)
53 LuaMinimap *ref = checkobject(L, 1);
54 Minimap *m = getobject(ref);
56 push_v3s16(L, m->getPos());
60 int LuaMinimap::l_set_pos(lua_State *L)
62 LuaMinimap *ref = checkobject(L, 1);
63 Minimap *m = getobject(ref);
65 m->setPos(read_v3s16(L, 2));
69 int LuaMinimap::l_get_angle(lua_State *L)
71 LuaMinimap *ref = checkobject(L, 1);
72 Minimap *m = getobject(ref);
74 lua_pushinteger(L, m->getAngle());
78 int LuaMinimap::l_set_angle(lua_State *L)
80 LuaMinimap *ref = checkobject(L, 1);
81 Minimap *m = getobject(ref);
83 m->setAngle(lua_tointeger(L, 2));
87 int LuaMinimap::l_get_mode(lua_State *L)
89 LuaMinimap *ref = checkobject(L, 1);
90 Minimap *m = getobject(ref);
92 lua_pushinteger(L, m->getMinimapMode());
96 int LuaMinimap::l_set_mode(lua_State *L)
98 LuaMinimap *ref = checkobject(L, 1);
99 Minimap *m = getobject(ref);
101 s32 mode = lua_tointeger(L, 2);
102 if (mode < MINIMAP_MODE_OFF ||
103 mode >= MINIMAP_MODE_COUNT) {
107 m->setMinimapMode((MinimapMode) mode);
111 int LuaMinimap::l_set_shape(lua_State *L)
113 LuaMinimap *ref = checkobject(L, 1);
114 Minimap *m = getobject(ref);
115 if (!lua_isnumber(L, 2))
118 m->setMinimapShape((MinimapShape)((int)lua_tonumber(L, 2)));
122 int LuaMinimap::l_get_shape(lua_State *L)
124 LuaMinimap *ref = checkobject(L, 1);
125 Minimap *m = getobject(ref);
127 lua_pushnumber(L, (int)m->getMinimapShape());
131 int LuaMinimap::l_show(lua_State *L)
133 // If minimap is disabled by config, don't show it.
134 if (!g_settings->getBool("enable_minimap"))
137 Client *client = getClient(L);
140 LuaMinimap *ref = checkobject(L, 1);
141 Minimap *m = getobject(ref);
143 if (m->getMinimapMode() == MINIMAP_MODE_OFF)
144 m->setMinimapMode(MINIMAP_MODE_SURFACEx1);
146 client->showMinimap(true);
150 int LuaMinimap::l_hide(lua_State *L)
152 Client *client = getClient(L);
155 LuaMinimap *ref = checkobject(L, 1);
156 Minimap *m = getobject(ref);
158 if (m->getMinimapMode() != MINIMAP_MODE_OFF)
159 m->setMinimapMode(MINIMAP_MODE_OFF);
161 client->showMinimap(false);
165 LuaMinimap *LuaMinimap::checkobject(lua_State *L, int narg)
167 NO_MAP_LOCK_REQUIRED;
169 luaL_checktype(L, narg, LUA_TUSERDATA);
171 void *ud = luaL_checkudata(L, narg, className);
173 luaL_typerror(L, narg, className);
175 return *(LuaMinimap **)ud; // unbox pointer
178 Minimap* LuaMinimap::getobject(LuaMinimap *ref)
180 return ref->m_minimap;
183 int LuaMinimap::gc_object(lua_State *L) {
184 LuaMinimap *o = *(LuaMinimap **)(lua_touserdata(L, 1));
189 void LuaMinimap::Register(lua_State *L)
192 int methodtable = lua_gettop(L);
193 luaL_newmetatable(L, className);
194 int metatable = lua_gettop(L);
196 lua_pushliteral(L, "__metatable");
197 lua_pushvalue(L, methodtable);
198 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
200 lua_pushliteral(L, "__index");
201 lua_pushvalue(L, methodtable);
202 lua_settable(L, metatable);
204 lua_pushliteral(L, "__gc");
205 lua_pushcfunction(L, gc_object);
206 lua_settable(L, metatable);
208 lua_pop(L, 1); // drop metatable
210 luaL_openlib(L, 0, methods, 0); // fill methodtable
211 lua_pop(L, 1); // drop methodtable
214 const char LuaMinimap::className[] = "Minimap";
215 const luaL_Reg LuaMinimap::methods[] = {
216 luamethod(LuaMinimap, show),
217 luamethod(LuaMinimap, hide),
218 luamethod(LuaMinimap, get_pos),
219 luamethod(LuaMinimap, set_pos),
220 luamethod(LuaMinimap, get_angle),
221 luamethod(LuaMinimap, set_angle),
222 luamethod(LuaMinimap, get_mode),
223 luamethod(LuaMinimap, set_mode),
224 luamethod(LuaMinimap, set_shape),
225 luamethod(LuaMinimap, get_shape),