]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_minimap.cpp
Refactor CSM restriction code a bit
[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 "client/client.h"
25 #include "client/minimap.h"
26 #include "settings.h"
27
28 LuaMinimap::LuaMinimap(Minimap *m) : m_minimap(m)
29 {
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_set_shape(lua_State *L)
112 {
113         LuaMinimap *ref = checkobject(L, 1);
114         Minimap *m = getobject(ref);
115         if (!lua_isnumber(L, 2))
116                 return 0;
117
118         m->setMinimapShape((MinimapShape)((int)lua_tonumber(L, 2)));
119         return 0;
120 }
121
122 int LuaMinimap::l_get_shape(lua_State *L)
123 {
124         LuaMinimap *ref = checkobject(L, 1);
125         Minimap *m = getobject(ref);
126
127         lua_pushnumber(L, (int)m->getMinimapShape());
128         return 1;
129 }
130
131 int LuaMinimap::l_show(lua_State *L)
132 {
133         // If minimap is disabled by config, don't show it.
134         if (!g_settings->getBool("enable_minimap"))
135                 return 1;
136
137         Client *client = getClient(L);
138         assert(client);
139
140         LuaMinimap *ref = checkobject(L, 1);
141         Minimap *m = getobject(ref);
142
143         if (m->getMinimapMode() == MINIMAP_MODE_OFF)
144                 m->setMinimapMode(MINIMAP_MODE_SURFACEx1);
145
146         client->showMinimap(true);
147         return 1;
148 }
149
150 int LuaMinimap::l_hide(lua_State *L)
151 {
152         Client *client = getClient(L);
153         assert(client);
154
155         LuaMinimap *ref = checkobject(L, 1);
156         Minimap *m = getobject(ref);
157
158         if (m->getMinimapMode() != MINIMAP_MODE_OFF)
159                 m->setMinimapMode(MINIMAP_MODE_OFF);
160
161         client->showMinimap(false);
162         return 1;
163 }
164
165 LuaMinimap *LuaMinimap::checkobject(lua_State *L, int narg)
166 {
167         NO_MAP_LOCK_REQUIRED;
168
169         luaL_checktype(L, narg, LUA_TUSERDATA);
170
171         void *ud = luaL_checkudata(L, narg, className);
172         if (!ud)
173                 luaL_typerror(L, narg, className);
174
175         return *(LuaMinimap **)ud;  // unbox pointer
176 }
177
178 Minimap* LuaMinimap::getobject(LuaMinimap *ref)
179 {
180         return ref->m_minimap;
181 }
182
183 int LuaMinimap::gc_object(lua_State *L) {
184         LuaMinimap *o = *(LuaMinimap **)(lua_touserdata(L, 1));
185         delete o;
186         return 0;
187 }
188
189 void LuaMinimap::Register(lua_State *L)
190 {
191         lua_newtable(L);
192         int methodtable = lua_gettop(L);
193         luaL_newmetatable(L, className);
194         int metatable = lua_gettop(L);
195
196         lua_pushliteral(L, "__metatable");
197         lua_pushvalue(L, methodtable);
198         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
199
200         lua_pushliteral(L, "__index");
201         lua_pushvalue(L, methodtable);
202         lua_settable(L, metatable);
203
204         lua_pushliteral(L, "__gc");
205         lua_pushcfunction(L, gc_object);
206         lua_settable(L, metatable);
207
208         lua_pop(L, 1);  // drop metatable
209
210         luaL_openlib(L, 0, methods, 0);  // fill methodtable
211         lua_pop(L, 1);  // drop methodtable
212 }
213
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),
226         {0,0}
227 };