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