[CSM] Add basic HUD manipulation. (#6067)
[oweals/minetest.git] / src / script / lua_api / l_localplayer.cpp
1 /*
2 Minetest
3 Copyright (C) 2017 Dumbeldor, Vincent Glize <vincent.glize@live.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 #include "l_localplayer.h"
21 #include "l_internal.h"
22 #include "script/common/c_converter.h"
23 #include "localplayer.h"
24 #include "hud.h"
25 #include "common/c_content.h"
26
27 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
28 {
29 }
30
31 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
32 {
33         LuaLocalPlayer *o = new LuaLocalPlayer(m);
34         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
35         luaL_getmetatable(L, className);
36         lua_setmetatable(L, -2);
37
38         // Keep localplayer object stack id
39         int localplayer_object = lua_gettop(L);
40
41         lua_getglobal(L, "core");
42         luaL_checktype(L, -1, LUA_TTABLE);
43         int coretable = lua_gettop(L);
44
45         lua_pushvalue(L, localplayer_object);
46         lua_setfield(L, coretable, "localplayer");
47 }
48
49 int LuaLocalPlayer::l_get_velocity(lua_State *L)
50 {
51         LocalPlayer *player = getobject(L, 1);
52
53         push_v3f(L, player->getSpeed() / BS);
54         return 1;
55 }
56
57 int LuaLocalPlayer::l_get_hp(lua_State *L)
58 {
59         LocalPlayer *player = getobject(L, 1);
60
61         lua_pushinteger(L, player->hp);
62         return 1;
63 }
64
65 int LuaLocalPlayer::l_get_name(lua_State *L)
66 {
67         LocalPlayer *player = getobject(L, 1);
68
69         lua_pushstring(L, player->getName());
70         return 1;
71 }
72
73 int LuaLocalPlayer::l_is_attached(lua_State *L)
74 {
75         LocalPlayer *player = getobject(L, 1);
76
77         lua_pushboolean(L, player->isAttached);
78         return 1;
79 }
80
81 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
82 {
83         LocalPlayer *player = getobject(L, 1);
84
85         lua_pushboolean(L, player->touching_ground);
86         return 1;
87 }
88
89 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
90 {
91         LocalPlayer *player = getobject(L, 1);
92
93         lua_pushboolean(L, player->in_liquid);
94         return 1;
95 }
96
97 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
98 {
99         LocalPlayer *player = getobject(L, 1);
100
101         lua_pushboolean(L, player->in_liquid_stable);
102         return 1;
103 }
104
105 int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
106 {
107         LocalPlayer *player = getobject(L, 1);
108
109         lua_pushinteger(L, player->liquid_viscosity);
110         return 1;
111 }
112
113 int LuaLocalPlayer::l_is_climbing(lua_State *L)
114 {
115         LocalPlayer *player = getobject(L, 1);
116
117         lua_pushboolean(L, player->is_climbing);
118         return 1;
119 }
120
121 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
122 {
123         LocalPlayer *player = getobject(L, 1);
124
125         lua_pushboolean(L, player->swimming_vertical);
126         return 1;
127 }
128
129 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
130 {
131         LocalPlayer *player = getobject(L, 1);
132
133         lua_newtable(L);
134         lua_pushnumber(L, player->physics_override_speed);
135         lua_setfield(L, -2, "speed");
136
137         lua_pushnumber(L, player->physics_override_jump);
138         lua_setfield(L, -2, "jump");
139
140         lua_pushnumber(L, player->physics_override_gravity);
141         lua_setfield(L, -2, "gravity");
142
143         lua_pushboolean(L, player->physics_override_sneak);
144         lua_setfield(L, -2, "sneak");
145
146         lua_pushboolean(L, player->physics_override_sneak_glitch);
147         lua_setfield(L, -2, "sneak_glitch");
148
149         return 1;
150 }
151
152 int LuaLocalPlayer::l_get_override_pos(lua_State *L)
153 {
154         LocalPlayer *player = getobject(L, 1);
155
156         push_v3f(L, player->overridePosition);
157         return 1;
158 }
159
160 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
161 {
162         LocalPlayer *player = getobject(L, 1);
163
164         push_v3f(L, player->last_position / BS);
165         return 1;
166 }
167
168 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
169 {
170         LocalPlayer *player = getobject(L, 1);
171
172         push_v3f(L, player->last_speed);
173         return 1;
174 }
175
176 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
177 {
178         LocalPlayer *player = getobject(L, 1);
179
180         lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
181         return 1;
182 }
183
184 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
185 {
186         LocalPlayer *player = getobject(L, 1);
187
188         lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
189         return 1;
190 }
191
192 int LuaLocalPlayer::l_get_key_pressed(lua_State *L)
193 {
194         LocalPlayer *player = getobject(L, 1);
195
196         lua_pushinteger(L, player->last_keyPressed);
197         return 1;
198 }
199
200 int LuaLocalPlayer::l_get_breath(lua_State *L)
201 {
202         LocalPlayer *player = getobject(L, 1);
203
204         lua_pushinteger(L, player->getBreath());
205         return 1;
206 }
207
208 int LuaLocalPlayer::l_get_pos(lua_State *L)
209 {
210         LocalPlayer *player = getobject(L, 1);
211
212         push_v3f(L, player->getPosition() / BS);
213         return 1;
214 }
215
216 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
217 {
218         LocalPlayer *player = getobject(L, 1);
219
220         lua_newtable(L);
221         lua_pushnumber(L, player->movement_acceleration_default);
222         lua_setfield(L, -2, "default");
223
224         lua_pushnumber(L, player->movement_acceleration_air);
225         lua_setfield(L, -2, "air");
226
227         lua_pushnumber(L, player->movement_acceleration_fast);
228         lua_setfield(L, -2, "fast");
229
230         return 1;
231 }
232
233 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
234 {
235         LocalPlayer *player = getobject(L, 1);
236
237         lua_newtable(L);
238         lua_pushnumber(L, player->movement_speed_walk);
239         lua_setfield(L, -2, "walk");
240
241         lua_pushnumber(L, player->movement_speed_crouch);
242         lua_setfield(L, -2, "crouch");
243
244         lua_pushnumber(L, player->movement_speed_fast);
245         lua_setfield(L, -2, "fast");
246
247         lua_pushnumber(L, player->movement_speed_climb);
248         lua_setfield(L, -2, "climb");
249
250         lua_pushnumber(L, player->movement_speed_jump);
251         lua_setfield(L, -2, "jump");
252
253         return 1;
254 }
255
256 int LuaLocalPlayer::l_get_movement(lua_State *L)
257 {
258         LocalPlayer *player = getobject(L, 1);
259
260         lua_newtable(L);
261
262         lua_pushnumber(L, player->movement_liquid_fluidity);
263         lua_setfield(L, -2, "liquid_fluidity");
264
265         lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
266         lua_setfield(L, -2, "liquid_fluidity_smooth");
267
268         lua_pushnumber(L, player->movement_liquid_sink);
269         lua_setfield(L, -2, "liquid_sink");
270
271         lua_pushnumber(L, player->movement_gravity);
272         lua_setfield(L, -2, "gravity");
273
274         return 1;
275 }
276
277
278 // hud_add(self, form)
279 int LuaLocalPlayer::l_hud_add(lua_State *L)
280 {
281         LocalPlayer *player = getobject(L, 1);
282
283         HudElement *elem = new HudElement;
284         read_hud_element(L, elem);
285
286         u32 id = player->addHud(elem);
287         if (id == U32_MAX) {
288                 delete elem;
289                 return 0;
290         }
291         lua_pushnumber(L, id);
292         return 1;
293 }
294
295 // hud_remove(self, id)
296 int LuaLocalPlayer::l_hud_remove(lua_State *L)
297 {
298         LocalPlayer *player = getobject(L, 1);
299         u32 id = luaL_checkinteger(L, 2);
300         HudElement *element = player->removeHud(id);
301         if (!element)
302                 lua_pushboolean(L, false);
303         else
304                 lua_pushboolean(L, true);
305         delete element;
306         return 1;
307 }
308
309 // hud_change(self, id, stat, data)
310 int LuaLocalPlayer::l_hud_change(lua_State *L)
311 {
312         LocalPlayer *player = getobject(L, 1);
313
314         u32 id = luaL_checkinteger(L, 2);
315
316         HudElement *element = player->getHud(id);
317         if (!element)
318                 return 0;
319
320         void *unused;
321         read_hud_change(L, element, &unused);
322
323         lua_pushboolean(L, true);
324         return 1;
325 }
326
327 // hud_get(self, id)
328 int LuaLocalPlayer::l_hud_get(lua_State *L)
329 {
330         LocalPlayer *player = getobject(L, 1);
331
332         u32 id = luaL_checkinteger(L, -1);
333
334         HudElement *e = player->getHud(id);
335         if (!e) {
336                 lua_pushnil(L);
337                 return 1;
338         }
339
340         push_hud_element(L, e);
341         return 1;
342 }
343
344 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
345 {
346         luaL_checktype(L, narg, LUA_TUSERDATA);
347
348         void *ud = luaL_checkudata(L, narg, className);
349         if (!ud)
350                 luaL_typerror(L, narg, className);
351
352         return *(LuaLocalPlayer **)ud;
353 }
354
355 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
356 {
357         return ref->m_localplayer;
358 }
359
360 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
361 {
362         LuaLocalPlayer *ref = checkobject(L, narg);
363         assert(ref);
364         LocalPlayer *player = getobject(ref);
365         assert(player);
366         return player;
367 }
368
369 int LuaLocalPlayer::gc_object(lua_State *L)
370 {
371         LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
372         delete o;
373         return 0;
374 }
375
376 void LuaLocalPlayer::Register(lua_State *L)
377 {
378         lua_newtable(L);
379         int methodtable = lua_gettop(L);
380         luaL_newmetatable(L, className);
381         int metatable = lua_gettop(L);
382
383         lua_pushliteral(L, "__metatable");
384         lua_pushvalue(L, methodtable);
385         lua_settable(L, metatable); // hide metatable from lua getmetatable()
386
387         lua_pushliteral(L, "__index");
388         lua_pushvalue(L, methodtable);
389         lua_settable(L, metatable);
390
391         lua_pushliteral(L, "__gc");
392         lua_pushcfunction(L, gc_object);
393         lua_settable(L, metatable);
394
395         lua_pop(L, 1); // Drop metatable
396
397         luaL_openlib(L, 0, methods, 0); // fill methodtable
398         lua_pop(L, 1);                  // Drop methodtable
399 }
400
401 const char LuaLocalPlayer::className[] = "LocalPlayer";
402 const luaL_Reg LuaLocalPlayer::methods[] = {
403                 luamethod(LuaLocalPlayer, get_velocity),
404                 luamethod(LuaLocalPlayer, get_hp),
405                 luamethod(LuaLocalPlayer, get_name),
406                 luamethod(LuaLocalPlayer, is_attached),
407                 luamethod(LuaLocalPlayer, is_touching_ground),
408                 luamethod(LuaLocalPlayer, is_in_liquid),
409                 luamethod(LuaLocalPlayer, is_in_liquid_stable),
410                 luamethod(LuaLocalPlayer, get_liquid_viscosity),
411                 luamethod(LuaLocalPlayer, is_climbing),
412                 luamethod(LuaLocalPlayer, swimming_vertical),
413                 luamethod(LuaLocalPlayer, get_physics_override),
414                 luamethod(LuaLocalPlayer, get_override_pos),
415                 luamethod(LuaLocalPlayer, get_last_pos),
416                 luamethod(LuaLocalPlayer, get_last_velocity),
417                 luamethod(LuaLocalPlayer, get_last_look_horizontal),
418                 luamethod(LuaLocalPlayer, get_last_look_vertical),
419                 luamethod(LuaLocalPlayer, get_key_pressed),
420                 luamethod(LuaLocalPlayer, get_breath),
421                 luamethod(LuaLocalPlayer, get_pos),
422                 luamethod(LuaLocalPlayer, get_movement_acceleration),
423                 luamethod(LuaLocalPlayer, get_movement_speed),
424                 luamethod(LuaLocalPlayer, get_movement),
425                 luamethod(LuaLocalPlayer, hud_add),
426                 luamethod(LuaLocalPlayer, hud_remove),
427                 luamethod(LuaLocalPlayer, hud_change),
428                 luamethod(LuaLocalPlayer, hud_get),
429
430                 {0, 0}
431 };