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"
26 LuaMinimap::LuaMinimap(Minimap *m)
31 void LuaMinimap::create(lua_State *L, Minimap *m)
33 LuaMinimap *o = new LuaMinimap(m);
34 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
35 luaL_getmetatable(L, className);
36 lua_setmetatable(L, -2);
38 // Keep minimap object stack id
39 int minimap_object = lua_gettop(L);
41 lua_getglobal(L, "core");
42 lua_getfield(L, -1, "ui");
43 luaL_checktype(L, -1, LUA_TTABLE);
44 int uitable = lua_gettop(L);
46 lua_pushvalue(L, minimap_object); // Copy object to top of stack
47 lua_setfield(L, uitable, "minimap");
50 int LuaMinimap::l_get_pos(lua_State *L)
52 LuaMinimap *ref = checkobject(L, 1);
53 Minimap *m = getobject(ref);
55 push_v3s16(L, m->getPos());
59 int LuaMinimap::l_set_pos(lua_State *L)
61 LuaMinimap *ref = checkobject(L, 1);
62 Minimap *m = getobject(ref);
64 m->setPos(read_v3s16(L, 2));
68 int LuaMinimap::l_get_angle(lua_State *L)
70 LuaMinimap *ref = checkobject(L, 1);
71 Minimap *m = getobject(ref);
73 lua_pushinteger(L, m->getAngle());
77 int LuaMinimap::l_set_angle(lua_State *L)
79 LuaMinimap *ref = checkobject(L, 1);
80 Minimap *m = getobject(ref);
82 m->setAngle(lua_tointeger(L, 2));
86 int LuaMinimap::l_get_mode(lua_State *L)
88 LuaMinimap *ref = checkobject(L, 1);
89 Minimap *m = getobject(ref);
91 lua_pushinteger(L, m->getMinimapMode());
95 int LuaMinimap::l_set_mode(lua_State *L)
97 LuaMinimap *ref = checkobject(L, 1);
98 Minimap *m = getobject(ref);
100 s32 mode = lua_tointeger(L, 2);
101 if (mode < MINIMAP_MODE_OFF ||
102 mode >= MINIMAP_MODE_COUNT) {
106 m->setMinimapMode((MinimapMode) mode);
110 int LuaMinimap::l_toggle_shape(lua_State *L)
112 LuaMinimap *ref = checkobject(L, 1);
113 Minimap *m = getobject(ref);
115 m->toggleMinimapShape();
119 int LuaMinimap::l_show(lua_State *L)
121 Client *client = getClient(L);
123 client->setMinimapShownByMod(true);
127 int LuaMinimap::l_hide(lua_State *L)
129 Client *client = getClient(L);
131 client->setMinimapShownByMod(false);
135 LuaMinimap *LuaMinimap::checkobject(lua_State *L, int narg)
137 NO_MAP_LOCK_REQUIRED;
139 luaL_checktype(L, narg, LUA_TUSERDATA);
141 void *ud = luaL_checkudata(L, narg, className);
143 luaL_typerror(L, narg, className);
145 return *(LuaMinimap **)ud; // unbox pointer
148 Minimap* LuaMinimap::getobject(LuaMinimap *ref)
150 return ref->m_minimap;
153 int LuaMinimap::gc_object(lua_State *L) {
154 LuaMinimap *o = *(LuaMinimap **)(lua_touserdata(L, 1));
159 void LuaMinimap::Register(lua_State *L)
162 int methodtable = lua_gettop(L);
163 luaL_newmetatable(L, className);
164 int metatable = lua_gettop(L);
166 lua_pushliteral(L, "__metatable");
167 lua_pushvalue(L, methodtable);
168 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
170 lua_pushliteral(L, "__index");
171 lua_pushvalue(L, methodtable);
172 lua_settable(L, metatable);
174 lua_pushliteral(L, "__gc");
175 lua_pushcfunction(L, gc_object);
176 lua_settable(L, metatable);
178 lua_pop(L, 1); // drop metatable
180 luaL_openlib(L, 0, methods, 0); // fill methodtable
181 lua_pop(L, 1); // drop methodtable
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),