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"
27 LuaMinimap::LuaMinimap(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_toggle_shape(lua_State *L)
113 LuaMinimap *ref = checkobject(L, 1);
114 Minimap *m = getobject(ref);
116 m->toggleMinimapShape();
120 int LuaMinimap::l_show(lua_State *L)
122 // If minimap is disabled by config, don't show it.
123 if (!g_settings->getBool("enable_minimap"))
126 Client *client = getClient(L);
129 LuaMinimap *ref = checkobject(L, 1);
130 Minimap *m = getobject(ref);
132 if (m->getMinimapMode() == MINIMAP_MODE_OFF)
133 m->setMinimapMode(MINIMAP_MODE_SURFACEx1);
135 client->showMinimap(true);
139 int LuaMinimap::l_hide(lua_State *L)
141 Client *client = getClient(L);
144 LuaMinimap *ref = checkobject(L, 1);
145 Minimap *m = getobject(ref);
147 if (m->getMinimapMode() != MINIMAP_MODE_OFF)
148 m->setMinimapMode(MINIMAP_MODE_OFF);
150 client->showMinimap(false);
154 LuaMinimap *LuaMinimap::checkobject(lua_State *L, int narg)
156 NO_MAP_LOCK_REQUIRED;
158 luaL_checktype(L, narg, LUA_TUSERDATA);
160 void *ud = luaL_checkudata(L, narg, className);
162 luaL_typerror(L, narg, className);
164 return *(LuaMinimap **)ud; // unbox pointer
167 Minimap* LuaMinimap::getobject(LuaMinimap *ref)
169 return ref->m_minimap;
172 int LuaMinimap::gc_object(lua_State *L) {
173 LuaMinimap *o = *(LuaMinimap **)(lua_touserdata(L, 1));
178 void LuaMinimap::Register(lua_State *L)
181 int methodtable = lua_gettop(L);
182 luaL_newmetatable(L, className);
183 int metatable = lua_gettop(L);
185 lua_pushliteral(L, "__metatable");
186 lua_pushvalue(L, methodtable);
187 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
189 lua_pushliteral(L, "__index");
190 lua_pushvalue(L, methodtable);
191 lua_settable(L, metatable);
193 lua_pushliteral(L, "__gc");
194 lua_pushcfunction(L, gc_object);
195 lua_settable(L, metatable);
197 lua_pop(L, 1); // drop metatable
199 luaL_openlib(L, 0, methods, 0); // fill methodtable
200 lua_pop(L, 1); // drop methodtable
203 const char LuaMinimap::className[] = "Minimap";
204 const luaL_reg LuaMinimap::methods[] = {
205 luamethod(LuaMinimap, show),
206 luamethod(LuaMinimap, hide),
207 luamethod(LuaMinimap, get_pos),
208 luamethod(LuaMinimap, set_pos),
209 luamethod(LuaMinimap, get_angle),
210 luamethod(LuaMinimap, set_angle),
211 luamethod(LuaMinimap, get_mode),
212 luamethod(LuaMinimap, set_mode),
213 luamethod(LuaMinimap, toggle_shape),