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