]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_localplayer.cpp
DevTest: Fix broken PNG textures
[dragonfireclient.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 "lua_api/l_item.h"
23 #include "script/common/c_converter.h"
24 #include "client/localplayer.h"
25 #include "hud.h"
26 #include "common/c_content.h"
27 #include "client/content_cao.h"
28
29 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
30 {
31 }
32
33 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
34 {
35         lua_getglobal(L, "core");
36         luaL_checktype(L, -1, LUA_TTABLE);
37         int objectstable = lua_gettop(L);
38         lua_getfield(L, -1, "localplayer");
39
40         // Duplication check
41         if (lua_type(L, -1) == LUA_TUSERDATA) {
42                 lua_pop(L, 1);
43                 return;
44         }
45
46         LuaLocalPlayer *o = new LuaLocalPlayer(m);
47         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
48         luaL_getmetatable(L, className);
49         lua_setmetatable(L, -2);
50
51         lua_pushvalue(L, lua_gettop(L));
52         lua_setfield(L, objectstable, "localplayer");
53 }
54
55 int LuaLocalPlayer::l_get_velocity(lua_State *L)
56 {
57         LocalPlayer *player = getobject(L, 1);
58
59         push_v3f(L, player->getSpeed() / BS);
60         return 1;
61 }
62
63 int LuaLocalPlayer::l_get_hp(lua_State *L)
64 {
65         LocalPlayer *player = getobject(L, 1);
66
67         lua_pushinteger(L, player->hp);
68         return 1;
69 }
70
71 int LuaLocalPlayer::l_get_name(lua_State *L)
72 {
73         LocalPlayer *player = getobject(L, 1);
74
75         lua_pushstring(L, player->getName());
76         return 1;
77 }
78
79 // get_wield_index(self)
80 int LuaLocalPlayer::l_get_wield_index(lua_State *L)
81 {
82         LocalPlayer *player = getobject(L, 1);
83
84         lua_pushinteger(L, player->getWieldIndex());
85         return 1;
86 }
87
88 // get_wielded_item(self)
89 int LuaLocalPlayer::l_get_wielded_item(lua_State *L)
90 {
91         LocalPlayer *player = getobject(L, 1);
92
93         ItemStack selected_item;
94         player->getWieldedItem(&selected_item, nullptr);
95         LuaItemStack::create(L, selected_item);
96         return 1;
97 }
98
99 int LuaLocalPlayer::l_is_attached(lua_State *L)
100 {
101         LocalPlayer *player = getobject(L, 1);
102
103         lua_pushboolean(L, player->getParent() != nullptr);
104         return 1;
105 }
106
107 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
108 {
109         LocalPlayer *player = getobject(L, 1);
110
111         lua_pushboolean(L, player->touching_ground);
112         return 1;
113 }
114
115 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
116 {
117         LocalPlayer *player = getobject(L, 1);
118
119         lua_pushboolean(L, player->in_liquid);
120         return 1;
121 }
122
123 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
124 {
125         LocalPlayer *player = getobject(L, 1);
126
127         lua_pushboolean(L, player->in_liquid_stable);
128         return 1;
129 }
130
131 int LuaLocalPlayer::l_get_move_resistance(lua_State *L)
132 {
133         LocalPlayer *player = getobject(L, 1);
134
135         lua_pushinteger(L, player->move_resistance);
136         return 1;
137 }
138
139 int LuaLocalPlayer::l_is_climbing(lua_State *L)
140 {
141         LocalPlayer *player = getobject(L, 1);
142
143         lua_pushboolean(L, player->is_climbing);
144         return 1;
145 }
146
147 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
148 {
149         LocalPlayer *player = getobject(L, 1);
150
151         lua_pushboolean(L, player->swimming_vertical);
152         return 1;
153 }
154
155 // get_physics_override(self)
156 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
157 {
158         LocalPlayer *player = getobject(L, 1);
159
160         lua_newtable(L);
161         lua_pushnumber(L, player->physics_override_speed);
162         lua_setfield(L, -2, "speed");
163
164         lua_pushnumber(L, player->physics_override_jump);
165         lua_setfield(L, -2, "jump");
166
167         lua_pushnumber(L, player->physics_override_gravity);
168         lua_setfield(L, -2, "gravity");
169
170         lua_pushboolean(L, player->physics_override_sneak);
171         lua_setfield(L, -2, "sneak");
172
173         lua_pushboolean(L, player->physics_override_sneak_glitch);
174         lua_setfield(L, -2, "sneak_glitch");
175
176         lua_pushboolean(L, player->physics_override_new_move);
177         lua_setfield(L, -2, "new_move");
178
179         return 1;
180 }
181
182 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
183 {
184         LocalPlayer *player = getobject(L, 1);
185
186         push_v3f(L, player->last_position / BS);
187         return 1;
188 }
189
190 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
191 {
192         LocalPlayer *player = getobject(L, 1);
193
194         push_v3f(L, player->last_speed);
195         return 1;
196 }
197
198 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
199 {
200         LocalPlayer *player = getobject(L, 1);
201
202         lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
203         return 1;
204 }
205
206 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
207 {
208         LocalPlayer *player = getobject(L, 1);
209
210         lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
211         return 1;
212 }
213
214 // get_control(self)
215 int LuaLocalPlayer::l_get_control(lua_State *L)
216 {
217         LocalPlayer *player = getobject(L, 1);
218         const PlayerControl &c = player->getPlayerControl();
219
220         auto set = [L] (const char *name, bool value) {
221                 lua_pushboolean(L, value);
222                 lua_setfield(L, -2, name);
223         };
224
225         lua_createtable(L, 0, 12);
226         set("jump",  c.jump);
227         set("aux1",  c.aux1);
228         set("sneak", c.sneak);
229         set("zoom",  c.zoom);
230         set("dig",   c.dig);
231         set("place", c.place);
232         // Player movement in polar coordinates and non-binary speed
233         lua_pushnumber(L, c.movement_speed);
234         lua_setfield(L, -2, "movement_speed");
235         lua_pushnumber(L, c.movement_direction);
236         lua_setfield(L, -2, "movement_direction");
237         // Provide direction keys to ensure compatibility
238         set("up",    c.direction_keys & (1 << 0));
239         set("down",  c.direction_keys & (1 << 1));
240         set("left",  c.direction_keys & (1 << 2));
241         set("right", c.direction_keys & (1 << 3));
242
243         return 1;
244 }
245
246 // get_breath(self)
247 int LuaLocalPlayer::l_get_breath(lua_State *L)
248 {
249         LocalPlayer *player = getobject(L, 1);
250
251         lua_pushinteger(L, player->getBreath());
252         return 1;
253 }
254
255 // get_pos(self)
256 int LuaLocalPlayer::l_get_pos(lua_State *L)
257 {
258         LocalPlayer *player = getobject(L, 1);
259
260         push_v3f(L, player->getPosition() / BS);
261         return 1;
262 }
263
264 // get_movement_acceleration(self)
265 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
266 {
267         LocalPlayer *player = getobject(L, 1);
268
269         lua_newtable(L);
270         lua_pushnumber(L, player->movement_acceleration_default);
271         lua_setfield(L, -2, "default");
272
273         lua_pushnumber(L, player->movement_acceleration_air);
274         lua_setfield(L, -2, "air");
275
276         lua_pushnumber(L, player->movement_acceleration_fast);
277         lua_setfield(L, -2, "fast");
278
279         return 1;
280 }
281
282 // get_movement_speed(self)
283 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
284 {
285         LocalPlayer *player = getobject(L, 1);
286
287         lua_newtable(L);
288         lua_pushnumber(L, player->movement_speed_walk);
289         lua_setfield(L, -2, "walk");
290
291         lua_pushnumber(L, player->movement_speed_crouch);
292         lua_setfield(L, -2, "crouch");
293
294         lua_pushnumber(L, player->movement_speed_fast);
295         lua_setfield(L, -2, "fast");
296
297         lua_pushnumber(L, player->movement_speed_climb);
298         lua_setfield(L, -2, "climb");
299
300         lua_pushnumber(L, player->movement_speed_jump);
301         lua_setfield(L, -2, "jump");
302
303         return 1;
304 }
305
306 // get_movement(self)
307 int LuaLocalPlayer::l_get_movement(lua_State *L)
308 {
309         LocalPlayer *player = getobject(L, 1);
310
311         lua_newtable(L);
312
313         lua_pushnumber(L, player->movement_liquid_fluidity);
314         lua_setfield(L, -2, "liquid_fluidity");
315
316         lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
317         lua_setfield(L, -2, "liquid_fluidity_smooth");
318
319         lua_pushnumber(L, player->movement_liquid_sink);
320         lua_setfield(L, -2, "liquid_sink");
321
322         lua_pushnumber(L, player->movement_gravity);
323         lua_setfield(L, -2, "gravity");
324
325         return 1;
326 }
327
328 // get_armor_groups(self)
329 int LuaLocalPlayer::l_get_armor_groups(lua_State *L)
330 {
331         LocalPlayer *player = getobject(L, 1);
332         push_groups(L, player->getCAO()->getGroups());
333         return 1;
334 }
335
336 // hud_add(self, form)
337 int LuaLocalPlayer::l_hud_add(lua_State *L)
338 {
339         LocalPlayer *player = getobject(L, 1);
340
341         HudElement *elem = new HudElement;
342         read_hud_element(L, elem);
343
344         u32 id = player->addHud(elem);
345         if (id == U32_MAX) {
346                 delete elem;
347                 return 0;
348         }
349         lua_pushnumber(L, id);
350         return 1;
351 }
352
353 // hud_remove(self, id)
354 int LuaLocalPlayer::l_hud_remove(lua_State *L)
355 {
356         LocalPlayer *player = getobject(L, 1);
357         u32 id = luaL_checkinteger(L, 2);
358         HudElement *element = player->removeHud(id);
359         if (!element)
360                 lua_pushboolean(L, false);
361         else
362                 lua_pushboolean(L, true);
363         delete element;
364         return 1;
365 }
366
367 // hud_change(self, id, stat, data)
368 int LuaLocalPlayer::l_hud_change(lua_State *L)
369 {
370         LocalPlayer *player = getobject(L, 1);
371
372         u32 id = luaL_checkinteger(L, 2);
373
374         HudElement *element = player->getHud(id);
375         if (!element)
376                 return 0;
377
378         HudElementStat stat;
379         void *unused;
380         bool ok = read_hud_change(L, stat, element, &unused);
381
382         lua_pushboolean(L, ok);
383         return 1;
384 }
385
386 // hud_get(self, id)
387 int LuaLocalPlayer::l_hud_get(lua_State *L)
388 {
389         LocalPlayer *player = getobject(L, 1);
390
391         u32 id = luaL_checkinteger(L, -1);
392
393         HudElement *e = player->getHud(id);
394         if (!e) {
395                 lua_pushnil(L);
396                 return 1;
397         }
398
399         push_hud_element(L, e);
400         return 1;
401 }
402
403 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
404 {
405         luaL_checktype(L, narg, LUA_TUSERDATA);
406
407         void *ud = luaL_checkudata(L, narg, className);
408         if (!ud)
409                 luaL_typerror(L, narg, className);
410
411         return *(LuaLocalPlayer **)ud;
412 }
413
414 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
415 {
416         return ref->m_localplayer;
417 }
418
419 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
420 {
421         LuaLocalPlayer *ref = checkobject(L, narg);
422         assert(ref);
423         LocalPlayer *player = getobject(ref);
424         assert(player);
425         return player;
426 }
427
428 int LuaLocalPlayer::gc_object(lua_State *L)
429 {
430         LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
431         delete o;
432         return 0;
433 }
434
435 void LuaLocalPlayer::Register(lua_State *L)
436 {
437         lua_newtable(L);
438         int methodtable = lua_gettop(L);
439         luaL_newmetatable(L, className);
440         int metatable = lua_gettop(L);
441
442         lua_pushliteral(L, "__metatable");
443         lua_pushvalue(L, methodtable);
444         lua_settable(L, metatable); // hide metatable from lua getmetatable()
445
446         lua_pushliteral(L, "__index");
447         lua_pushvalue(L, methodtable);
448         lua_settable(L, metatable);
449
450         lua_pushliteral(L, "__gc");
451         lua_pushcfunction(L, gc_object);
452         lua_settable(L, metatable);
453
454         lua_pop(L, 1); // Drop metatable
455
456         luaL_register(L, nullptr, methods); // fill methodtable
457         lua_pop(L, 1);                  // Drop methodtable
458 }
459
460 const char LuaLocalPlayer::className[] = "LocalPlayer";
461 const luaL_Reg LuaLocalPlayer::methods[] = {
462                 luamethod(LuaLocalPlayer, get_velocity),
463                 luamethod(LuaLocalPlayer, get_hp),
464                 luamethod(LuaLocalPlayer, get_name),
465                 luamethod(LuaLocalPlayer, get_wield_index),
466                 luamethod(LuaLocalPlayer, get_wielded_item),
467                 luamethod(LuaLocalPlayer, is_attached),
468                 luamethod(LuaLocalPlayer, is_touching_ground),
469                 luamethod(LuaLocalPlayer, is_in_liquid),
470                 luamethod(LuaLocalPlayer, is_in_liquid_stable),
471                 luamethod(LuaLocalPlayer, is_climbing),
472                 luamethod(LuaLocalPlayer, swimming_vertical),
473                 luamethod(LuaLocalPlayer, get_physics_override),
474                 // TODO: figure our if these are useful in any way
475                 luamethod(LuaLocalPlayer, get_last_pos),
476                 luamethod(LuaLocalPlayer, get_last_velocity),
477                 luamethod(LuaLocalPlayer, get_last_look_horizontal),
478                 luamethod(LuaLocalPlayer, get_last_look_vertical),
479                 //
480                 luamethod(LuaLocalPlayer, get_control),
481                 luamethod(LuaLocalPlayer, get_breath),
482                 luamethod(LuaLocalPlayer, get_pos),
483                 luamethod(LuaLocalPlayer, get_movement_acceleration),
484                 luamethod(LuaLocalPlayer, get_movement_speed),
485                 luamethod(LuaLocalPlayer, get_movement),
486                 luamethod(LuaLocalPlayer, get_armor_groups),
487                 luamethod(LuaLocalPlayer, hud_add),
488                 luamethod(LuaLocalPlayer, hud_remove),
489                 luamethod(LuaLocalPlayer, hud_change),
490                 luamethod(LuaLocalPlayer, hud_get),
491
492                 luamethod(LuaLocalPlayer, get_move_resistance),
493
494                 {0, 0}
495 };