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);
124 LuaMinimap *ref = checkobject(L, 1);
125 Minimap *m = getobject(ref);
127 if (m->getMinimapMode() == MINIMAP_MODE_OFF)
128 m->setMinimapMode(MINIMAP_MODE_SURFACEx1);
130 client->showMinimap(true);
134 int LuaMinimap::l_hide(lua_State *L)
136 Client *client = getClient(L);
139 LuaMinimap *ref = checkobject(L, 1);
140 Minimap *m = getobject(ref);
142 if (m->getMinimapMode() != MINIMAP_MODE_OFF)
143 m->setMinimapMode(MINIMAP_MODE_OFF);
145 client->showMinimap(false);
149 LuaMinimap *LuaMinimap::checkobject(lua_State *L, int narg)
151 NO_MAP_LOCK_REQUIRED;
153 luaL_checktype(L, narg, LUA_TUSERDATA);
155 void *ud = luaL_checkudata(L, narg, className);
157 luaL_typerror(L, narg, className);
159 return *(LuaMinimap **)ud; // unbox pointer
162 Minimap* LuaMinimap::getobject(LuaMinimap *ref)
164 return ref->m_minimap;
167 int LuaMinimap::gc_object(lua_State *L) {
168 LuaMinimap *o = *(LuaMinimap **)(lua_touserdata(L, 1));
173 void LuaMinimap::Register(lua_State *L)
176 int methodtable = lua_gettop(L);
177 luaL_newmetatable(L, className);
178 int metatable = lua_gettop(L);
180 lua_pushliteral(L, "__metatable");
181 lua_pushvalue(L, methodtable);
182 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
184 lua_pushliteral(L, "__index");
185 lua_pushvalue(L, methodtable);
186 lua_settable(L, metatable);
188 lua_pushliteral(L, "__gc");
189 lua_pushcfunction(L, gc_object);
190 lua_settable(L, metatable);
192 lua_pop(L, 1); // drop metatable
194 luaL_openlib(L, 0, methods, 0); // fill methodtable
195 lua_pop(L, 1); // drop methodtable
198 const char LuaMinimap::className[] = "Minimap";
199 const luaL_reg LuaMinimap::methods[] = {
200 luamethod(LuaMinimap, show),
201 luamethod(LuaMinimap, hide),
202 luamethod(LuaMinimap, get_pos),
203 luamethod(LuaMinimap, set_pos),
204 luamethod(LuaMinimap, get_angle),
205 luamethod(LuaMinimap, set_angle),
206 luamethod(LuaMinimap, get_mode),
207 luamethod(LuaMinimap, set_mode),
208 luamethod(LuaMinimap, toggle_shape),