]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_localplayer.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[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_clientobject.h"
21 #include "l_localplayer.h"
22 #include "l_internal.h"
23 #include "lua_api/l_item.h"
24 #include "script/common/c_converter.h"
25 #include "client/localplayer.h"
26 #include "hud.h"
27 #include "common/c_content.h"
28 #include "client/client.h"
29 #include "client/content_cao.h"
30 #include "client/game.h"
31
32 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
33 {
34 }
35
36 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
37 {
38         lua_getglobal(L, "core");
39         luaL_checktype(L, -1, LUA_TTABLE);
40         int objectstable = lua_gettop(L);
41         lua_getfield(L, -1, "localplayer");
42
43         // Duplication check
44         if (lua_type(L, -1) == LUA_TUSERDATA) {
45                 lua_pop(L, 1);
46                 return;
47         }
48
49         LuaLocalPlayer *o = new LuaLocalPlayer(m);
50         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
51         luaL_getmetatable(L, className);
52         lua_setmetatable(L, -2);
53
54         lua_pushvalue(L, lua_gettop(L));
55         lua_setfield(L, objectstable, "localplayer");
56 }
57
58 int LuaLocalPlayer::l_get_velocity(lua_State *L)
59 {
60         LocalPlayer *player = getobject(L, 1);
61
62         push_v3f(L, player->getSpeed() / BS);
63         return 1;
64 }
65
66 int LuaLocalPlayer::l_set_velocity(lua_State *L)
67 {
68         LocalPlayer *player = getobject(L, 1);
69
70         v3f pos = checkFloatPos(L, 2);
71         player->setSpeed(pos);
72
73         return 0;
74 }
75
76 int LuaLocalPlayer::l_get_yaw(lua_State *L)
77 {
78     lua_pushnumber(L, wrapDegrees_0_360(g_game->cam_view.camera_yaw));
79     return 1;
80 }
81
82 int LuaLocalPlayer::l_set_yaw(lua_State *L)
83 {
84         LocalPlayer *player = getobject(L, 1);
85
86         if (lua_isnumber(L, 2)) {
87                 double yaw = lua_tonumber(L, 2);
88                 player->setYaw(yaw);
89                 g_game->cam_view.camera_yaw = yaw;
90                 g_game->cam_view_target.camera_yaw = yaw;
91         }
92
93         return 0;
94 }
95
96 int LuaLocalPlayer::l_get_pitch(lua_State *L)
97 {
98     lua_pushnumber(L, -wrapDegrees_180(g_game->cam_view.camera_pitch) );
99     return 1;
100 }
101
102 int LuaLocalPlayer::l_set_pitch(lua_State *L)
103 {
104         LocalPlayer *player = getobject(L, 1);
105
106         if (lua_isnumber(L, 2)) {
107                 double pitch = lua_tonumber(L, 2);
108                 player->setPitch(pitch);
109                 g_game->cam_view.camera_pitch = pitch;
110                 g_game->cam_view_target.camera_pitch = pitch;
111         }
112
113         return 0;
114 }
115
116
117 int LuaLocalPlayer::l_get_hp(lua_State *L)
118 {
119         LocalPlayer *player = getobject(L, 1);
120
121         lua_pushinteger(L, player->hp);
122         return 1;
123 }
124
125 int LuaLocalPlayer::l_get_name(lua_State *L)
126 {
127         LocalPlayer *player = getobject(L, 1);
128
129         lua_pushstring(L, player->getName());
130         return 1;
131 }
132
133 // get_wield_index(self)
134 int LuaLocalPlayer::l_get_wield_index(lua_State *L)
135 {
136         LocalPlayer *player = getobject(L, 1);
137
138         lua_pushinteger(L, player->getWieldIndex() + 1);
139         return 1;
140 }
141
142 // set_wield_index(self)
143 int LuaLocalPlayer::l_set_wield_index(lua_State *L)
144 {
145         LocalPlayer *player = getobject(L, 1);
146         u32 index = luaL_checkinteger(L, 2) - 1;
147
148         player->setWieldIndex(index);
149         g_game->processItemSelection(&g_game->runData.new_playeritem);
150         ItemStack selected_item, hand_item;
151         ItemStack &tool_item = player->getWieldedItem(&selected_item, &hand_item);
152         g_game->camera->wield(tool_item);
153         return 0;
154 }
155
156 // get_wielded_item(self)
157 int LuaLocalPlayer::l_get_wielded_item(lua_State *L)
158 {
159         LocalPlayer *player = getobject(L, 1);
160
161         ItemStack selected_item;
162         player->getWieldedItem(&selected_item, nullptr);
163         LuaItemStack::create(L, selected_item);
164         return 1;
165 }
166
167 int LuaLocalPlayer::l_is_attached(lua_State *L)
168 {
169         LocalPlayer *player = getobject(L, 1);
170
171         lua_pushboolean(L, player->getParent() != nullptr);
172         return 1;
173 }
174
175 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
176 {
177         LocalPlayer *player = getobject(L, 1);
178
179         lua_pushboolean(L, player->touching_ground);
180         return 1;
181 }
182
183 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
184 {
185         LocalPlayer *player = getobject(L, 1);
186
187         lua_pushboolean(L, player->in_liquid);
188         return 1;
189 }
190
191 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
192 {
193         LocalPlayer *player = getobject(L, 1);
194
195         lua_pushboolean(L, player->in_liquid_stable);
196         return 1;
197 }
198
199 int LuaLocalPlayer::l_get_move_resistance(lua_State *L)
200 {
201         LocalPlayer *player = getobject(L, 1);
202
203         lua_pushinteger(L, player->move_resistance);
204         return 1;
205 }
206
207 int LuaLocalPlayer::l_is_climbing(lua_State *L)
208 {
209         LocalPlayer *player = getobject(L, 1);
210
211         lua_pushboolean(L, player->is_climbing);
212         return 1;
213 }
214
215 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
216 {
217         LocalPlayer *player = getobject(L, 1);
218
219         lua_pushboolean(L, player->swimming_vertical);
220         return 1;
221 }
222
223 // get_physics_override(self)
224 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
225 {
226         LocalPlayer *player = getobject(L, 1);
227
228         push_physics_override(L, player->physics_override_speed, player->physics_override_jump, player->physics_override_gravity, player->physics_override_sneak, player->physics_override_sneak_glitch, player->physics_override_new_move);
229
230         return 1;
231 }
232
233 // set_physics_override(self, override)
234 int LuaLocalPlayer::l_set_physics_override(lua_State *L)
235 {
236         LocalPlayer *player = getobject(L, 1);
237
238         player->physics_override_speed = getfloatfield_default(
239                         L, 2, "speed", player->physics_override_speed);
240         player->physics_override_jump = getfloatfield_default(
241                         L, 2, "jump", player->physics_override_jump);
242         player->physics_override_gravity = getfloatfield_default(
243                         L, 2, "gravity", player->physics_override_gravity);
244         player->physics_override_sneak = getboolfield_default(
245                         L, 2, "sneak", player->physics_override_sneak);
246         player->physics_override_sneak_glitch = getboolfield_default(
247                         L, 2, "sneak_glitch", player->physics_override_sneak_glitch);
248         player->physics_override_new_move = getboolfield_default(
249                         L, 2, "new_move", player->physics_override_new_move);
250
251         return 0;
252 }
253
254 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
255 {
256         LocalPlayer *player = getobject(L, 1);
257
258         push_v3f(L, player->last_position / BS);
259         return 1;
260 }
261
262 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
263 {
264         LocalPlayer *player = getobject(L, 1);
265
266         push_v3f(L, player->last_speed);
267         return 1;
268 }
269
270 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
271 {
272         LocalPlayer *player = getobject(L, 1);
273
274         lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
275         return 1;
276 }
277
278 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
279 {
280         LocalPlayer *player = getobject(L, 1);
281
282         lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
283         return 1;
284 }
285
286 // get_control(self)
287 int LuaLocalPlayer::l_get_control(lua_State *L)
288 {
289         LocalPlayer *player = getobject(L, 1);
290         const PlayerControl &c = player->getPlayerControl();
291
292         auto set = [L] (const char *name, bool value) {
293                 lua_pushboolean(L, value);
294                 lua_setfield(L, -2, name);
295         };
296
297         lua_createtable(L, 0, 12);
298         set("jump",  c.jump);
299         set("aux1",  c.aux1);
300         set("sneak", c.sneak);
301         set("zoom",  c.zoom);
302         set("dig",   c.dig);
303         set("place", c.place);
304         // Player movement in polar coordinates and non-binary speed
305         lua_pushnumber(L, c.movement_speed);
306         lua_setfield(L, -2, "movement_speed");
307         lua_pushnumber(L, c.movement_direction);
308         lua_setfield(L, -2, "movement_direction");
309         // Provide direction keys to ensure compatibility
310         set("up",    c.direction_keys & (1 << 0));
311         set("down",  c.direction_keys & (1 << 1));
312         set("left",  c.direction_keys & (1 << 2));
313         set("right", c.direction_keys & (1 << 3));
314
315         return 1;
316 }
317
318 // get_breath(self)
319 int LuaLocalPlayer::l_get_breath(lua_State *L)
320 {
321         LocalPlayer *player = getobject(L, 1);
322
323         lua_pushinteger(L, player->getBreath());
324         return 1;
325 }
326
327 // get_pos(self)
328 int LuaLocalPlayer::l_get_pos(lua_State *L)
329 {
330         LocalPlayer *player = getobject(L, 1);
331
332         push_v3f(L, player->getPosition() / BS);
333         return 1;
334 }
335
336 // set_pos(self, pos)
337 int LuaLocalPlayer::l_set_pos(lua_State *L)
338 {
339         LocalPlayer *player = getobject(L, 1);
340
341         v3f pos = checkFloatPos(L, 2);
342         player->setPosition(pos);
343         getClient(L)->sendPlayerPos();
344         return 0;
345 }
346
347 // get_movement_acceleration(self)
348 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
349 {
350         LocalPlayer *player = getobject(L, 1);
351
352         lua_newtable(L);
353         lua_pushnumber(L, player->movement_acceleration_default);
354         lua_setfield(L, -2, "default");
355
356         lua_pushnumber(L, player->movement_acceleration_air);
357         lua_setfield(L, -2, "air");
358
359         lua_pushnumber(L, player->movement_acceleration_fast);
360         lua_setfield(L, -2, "fast");
361
362         return 1;
363 }
364
365 // get_movement_speed(self)
366 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
367 {
368         LocalPlayer *player = getobject(L, 1);
369
370         lua_newtable(L);
371         lua_pushnumber(L, player->movement_speed_walk);
372         lua_setfield(L, -2, "walk");
373
374         lua_pushnumber(L, player->movement_speed_crouch);
375         lua_setfield(L, -2, "crouch");
376
377         lua_pushnumber(L, player->movement_speed_fast);
378         lua_setfield(L, -2, "fast");
379
380         lua_pushnumber(L, player->movement_speed_climb);
381         lua_setfield(L, -2, "climb");
382
383         lua_pushnumber(L, player->movement_speed_jump);
384         lua_setfield(L, -2, "jump");
385
386         return 1;
387 }
388
389 // get_movement(self)
390 int LuaLocalPlayer::l_get_movement(lua_State *L)
391 {
392         LocalPlayer *player = getobject(L, 1);
393
394         lua_newtable(L);
395
396         lua_pushnumber(L, player->movement_liquid_fluidity);
397         lua_setfield(L, -2, "liquid_fluidity");
398
399         lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
400         lua_setfield(L, -2, "liquid_fluidity_smooth");
401
402         lua_pushnumber(L, player->movement_liquid_sink);
403         lua_setfield(L, -2, "liquid_sink");
404
405         lua_pushnumber(L, player->movement_gravity);
406         lua_setfield(L, -2, "gravity");
407
408         return 1;
409 }
410
411 // get_armor_groups(self)
412 int LuaLocalPlayer::l_get_armor_groups(lua_State *L)
413 {
414         LocalPlayer *player = getobject(L, 1);
415         push_groups(L, player->getCAO()->getGroups());
416         return 1;
417 }
418
419 // hud_add(self, form)
420 int LuaLocalPlayer::l_hud_add(lua_State *L)
421 {
422         LocalPlayer *player = getobject(L, 1);
423
424         HudElement *elem = new HudElement;
425         read_hud_element(L, elem);
426
427         u32 id = player->addHud(elem);
428         if (id == U32_MAX) {
429                 delete elem;
430                 return 0;
431         }
432         lua_pushnumber(L, id);
433         return 1;
434 }
435
436 // hud_remove(self, id)
437 int LuaLocalPlayer::l_hud_remove(lua_State *L)
438 {
439         LocalPlayer *player = getobject(L, 1);
440         u32 id = luaL_checkinteger(L, 2);
441         HudElement *element = player->removeHud(id);
442         if (!element)
443                 lua_pushboolean(L, false);
444         else
445                 lua_pushboolean(L, true);
446         delete element;
447         return 1;
448 }
449
450 // hud_change(self, id, stat, data)
451 int LuaLocalPlayer::l_hud_change(lua_State *L)
452 {
453         LocalPlayer *player = getobject(L, 1);
454
455         u32 id = luaL_checkinteger(L, 2);
456
457         HudElement *element = player->getHud(id);
458         if (!element)
459                 return 0;
460
461         HudElementStat stat;
462         void *unused;
463         bool ok = read_hud_change(L, stat, element, &unused);
464
465         lua_pushboolean(L, ok);
466         return 1;
467 }
468
469 // hud_get(self, id)
470 int LuaLocalPlayer::l_hud_get(lua_State *L)
471 {
472         LocalPlayer *player = getobject(L, 1);
473
474         u32 id = luaL_checkinteger(L, -1);
475
476         HudElement *e = player->getHud(id);
477         if (!e) {
478                 lua_pushnil(L);
479                 return 1;
480         }
481
482         push_hud_element(L, e);
483         return 1;
484 }
485
486 // get_object(self)
487 int LuaLocalPlayer::l_get_object(lua_State *L)
488 {
489         LocalPlayer *player = getobject(L, 1);
490         ClientEnvironment &env = getClient(L)->getEnv();
491         ClientActiveObject *obj = env.getGenericCAO(player->getCAO()->getId());
492
493         push_objectRef(L, obj->getId());
494
495         return 1;
496 }
497
498 // get_hotbar_size(self)
499 int LuaLocalPlayer::l_get_hotbar_size(lua_State *L)
500 {
501         LocalPlayer *player = getobject(L, 1);
502         lua_pushnumber(L, player->hud_hotbar_itemcount);
503
504         return 1;
505 }
506
507 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
508 {
509         luaL_checktype(L, narg, LUA_TUSERDATA);
510
511         void *ud = luaL_checkudata(L, narg, className);
512         if (!ud)
513                 luaL_typerror(L, narg, className);
514
515         return *(LuaLocalPlayer **)ud;
516 }
517
518 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
519 {
520         return ref->m_localplayer;
521 }
522
523 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
524 {
525         LuaLocalPlayer *ref = checkobject(L, narg);
526         assert(ref);
527         LocalPlayer *player = getobject(ref);
528         assert(player);
529         return player;
530 }
531
532 int LuaLocalPlayer::gc_object(lua_State *L)
533 {
534         LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
535         delete o;
536         return 0;
537 }
538
539 void LuaLocalPlayer::Register(lua_State *L)
540 {
541         lua_newtable(L);
542         int methodtable = lua_gettop(L);
543         luaL_newmetatable(L, className);
544         int metatable = lua_gettop(L);
545
546         lua_pushliteral(L, "__metatable");
547         lua_pushvalue(L, methodtable);
548         lua_settable(L, metatable); // hide metatable from lua getmetatable()
549
550         lua_pushliteral(L, "__index");
551         lua_pushvalue(L, methodtable);
552         lua_settable(L, metatable);
553
554         lua_pushliteral(L, "__gc");
555         lua_pushcfunction(L, gc_object);
556         lua_settable(L, metatable);
557
558         lua_pop(L, 1); // Drop metatable
559
560         luaL_register(L, nullptr, methods); // fill methodtable
561         lua_pop(L, 1);                  // Drop methodtable
562 }
563
564 const char LuaLocalPlayer::className[] = "LocalPlayer";
565 const luaL_Reg LuaLocalPlayer::methods[] = {
566                 luamethod(LuaLocalPlayer, get_velocity),
567                 luamethod(LuaLocalPlayer, set_velocity),
568                 luamethod(LuaLocalPlayer, get_yaw),
569                 luamethod(LuaLocalPlayer, set_yaw),
570                 luamethod(LuaLocalPlayer, get_pitch),
571                 luamethod(LuaLocalPlayer, set_pitch),
572                 luamethod(LuaLocalPlayer, get_hp),
573                 luamethod(LuaLocalPlayer, get_name),
574                 luamethod(LuaLocalPlayer, get_wield_index),
575                 luamethod(LuaLocalPlayer, set_wield_index),
576                 luamethod(LuaLocalPlayer, get_wielded_item),
577                 luamethod(LuaLocalPlayer, is_attached),
578                 luamethod(LuaLocalPlayer, is_touching_ground),
579                 luamethod(LuaLocalPlayer, is_in_liquid),
580                 luamethod(LuaLocalPlayer, is_in_liquid_stable),
581                 luamethod(LuaLocalPlayer, is_climbing),
582                 luamethod(LuaLocalPlayer, swimming_vertical),
583                 luamethod(LuaLocalPlayer, get_physics_override),
584                 luamethod(LuaLocalPlayer, set_physics_override),
585                 // TODO: figure our if these are useful in any way
586                 luamethod(LuaLocalPlayer, get_last_pos),
587                 luamethod(LuaLocalPlayer, get_last_velocity),
588                 luamethod(LuaLocalPlayer, get_last_look_horizontal),
589                 luamethod(LuaLocalPlayer, get_last_look_vertical),
590                 //
591                 luamethod(LuaLocalPlayer, get_control),
592                 luamethod(LuaLocalPlayer, get_breath),
593                 luamethod(LuaLocalPlayer, get_pos),
594                 luamethod(LuaLocalPlayer, set_pos),
595                 luamethod(LuaLocalPlayer, get_movement_acceleration),
596                 luamethod(LuaLocalPlayer, get_movement_speed),
597                 luamethod(LuaLocalPlayer, get_movement),
598                 luamethod(LuaLocalPlayer, get_armor_groups),
599                 luamethod(LuaLocalPlayer, hud_add),
600                 luamethod(LuaLocalPlayer, hud_remove),
601                 luamethod(LuaLocalPlayer, hud_change),
602                 luamethod(LuaLocalPlayer, hud_get),
603                 luamethod(LuaLocalPlayer, get_object),
604                 luamethod(LuaLocalPlayer, get_hotbar_size),
605
606                 luamethod(LuaLocalPlayer, get_move_resistance),
607
608                 {0, 0}
609 };