]> 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                 int 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                 int 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_liquid_viscosity(lua_State *L)
200 {
201         LocalPlayer *player = getobject(L, 1);
202
203         lua_pushinteger(L, player->liquid_viscosity);
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("up", c.up);
299         set("down", c.down);
300         set("left", c.left);
301         set("right", c.right);
302         set("jump", c.jump);
303         set("aux1", c.aux1);
304         set("sneak", c.sneak);
305         set("zoom", c.zoom);
306         set("dig", c.dig);
307         set("place", c.place);
308
309         return 1;
310 }
311
312 // get_breath(self)
313 int LuaLocalPlayer::l_get_breath(lua_State *L)
314 {
315         LocalPlayer *player = getobject(L, 1);
316
317         lua_pushinteger(L, player->getBreath());
318         return 1;
319 }
320
321 // get_pos(self)
322 int LuaLocalPlayer::l_get_pos(lua_State *L)
323 {
324         LocalPlayer *player = getobject(L, 1);
325
326         push_v3f(L, player->getPosition() / BS);
327         return 1;
328 }
329
330 // set_pos(self, pos)
331 int LuaLocalPlayer::l_set_pos(lua_State *L)
332 {
333         LocalPlayer *player = getobject(L, 1);
334
335         v3f pos = checkFloatPos(L, 2);
336         player->setPosition(pos);
337         getClient(L)->sendPlayerPos();
338         return 0;
339 }
340
341 // get_movement_acceleration(self)
342 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
343 {
344         LocalPlayer *player = getobject(L, 1);
345
346         lua_newtable(L);
347         lua_pushnumber(L, player->movement_acceleration_default);
348         lua_setfield(L, -2, "default");
349
350         lua_pushnumber(L, player->movement_acceleration_air);
351         lua_setfield(L, -2, "air");
352
353         lua_pushnumber(L, player->movement_acceleration_fast);
354         lua_setfield(L, -2, "fast");
355
356         return 1;
357 }
358
359 // get_movement_speed(self)
360 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
361 {
362         LocalPlayer *player = getobject(L, 1);
363
364         lua_newtable(L);
365         lua_pushnumber(L, player->movement_speed_walk);
366         lua_setfield(L, -2, "walk");
367
368         lua_pushnumber(L, player->movement_speed_crouch);
369         lua_setfield(L, -2, "crouch");
370
371         lua_pushnumber(L, player->movement_speed_fast);
372         lua_setfield(L, -2, "fast");
373
374         lua_pushnumber(L, player->movement_speed_climb);
375         lua_setfield(L, -2, "climb");
376
377         lua_pushnumber(L, player->movement_speed_jump);
378         lua_setfield(L, -2, "jump");
379
380         return 1;
381 }
382
383 // get_movement(self)
384 int LuaLocalPlayer::l_get_movement(lua_State *L)
385 {
386         LocalPlayer *player = getobject(L, 1);
387
388         lua_newtable(L);
389
390         lua_pushnumber(L, player->movement_liquid_fluidity);
391         lua_setfield(L, -2, "liquid_fluidity");
392
393         lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
394         lua_setfield(L, -2, "liquid_fluidity_smooth");
395
396         lua_pushnumber(L, player->movement_liquid_sink);
397         lua_setfield(L, -2, "liquid_sink");
398
399         lua_pushnumber(L, player->movement_gravity);
400         lua_setfield(L, -2, "gravity");
401
402         return 1;
403 }
404
405 // get_armor_groups(self)
406 int LuaLocalPlayer::l_get_armor_groups(lua_State *L)
407 {
408         LocalPlayer *player = getobject(L, 1);
409         push_groups(L, player->getCAO()->getGroups());
410         return 1;
411 }
412
413 // hud_add(self, form)
414 int LuaLocalPlayer::l_hud_add(lua_State *L)
415 {
416         LocalPlayer *player = getobject(L, 1);
417
418         HudElement *elem = new HudElement;
419         read_hud_element(L, elem);
420
421         u32 id = player->addHud(elem);
422         if (id == U32_MAX) {
423                 delete elem;
424                 return 0;
425         }
426         lua_pushnumber(L, id);
427         return 1;
428 }
429
430 // hud_remove(self, id)
431 int LuaLocalPlayer::l_hud_remove(lua_State *L)
432 {
433         LocalPlayer *player = getobject(L, 1);
434         u32 id = luaL_checkinteger(L, 2);
435         HudElement *element = player->removeHud(id);
436         if (!element)
437                 lua_pushboolean(L, false);
438         else
439                 lua_pushboolean(L, true);
440         delete element;
441         return 1;
442 }
443
444 // hud_change(self, id, stat, data)
445 int LuaLocalPlayer::l_hud_change(lua_State *L)
446 {
447         LocalPlayer *player = getobject(L, 1);
448
449         u32 id = luaL_checkinteger(L, 2);
450
451         HudElement *element = player->getHud(id);
452         if (!element)
453                 return 0;
454
455         void *unused;
456         read_hud_change(L, element, &unused);
457
458         lua_pushboolean(L, true);
459         return 1;
460 }
461
462 // hud_get(self, id)
463 int LuaLocalPlayer::l_hud_get(lua_State *L)
464 {
465         LocalPlayer *player = getobject(L, 1);
466
467         u32 id = luaL_checkinteger(L, -1);
468
469         HudElement *e = player->getHud(id);
470         if (!e) {
471                 lua_pushnil(L);
472                 return 1;
473         }
474
475         push_hud_element(L, e);
476         return 1;
477 }
478
479 // get_object(self)
480 int LuaLocalPlayer::l_get_object(lua_State *L)
481 {
482         LocalPlayer *player = getobject(L, 1);
483         ClientEnvironment &env = getClient(L)->getEnv();
484         ClientActiveObject *obj = env.getGenericCAO(player->getCAO()->getId());
485
486         ClientObjectRef::create(L, obj);
487
488         return 1;
489 }
490
491 // get_hotbar_size(self)
492 int LuaLocalPlayer::l_get_hotbar_size(lua_State *L)
493 {
494         LocalPlayer *player = getobject(L, 1);
495         lua_pushnumber(L, player->hud_hotbar_itemcount);
496
497         return 1;
498 }
499
500 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
501 {
502         luaL_checktype(L, narg, LUA_TUSERDATA);
503
504         void *ud = luaL_checkudata(L, narg, className);
505         if (!ud)
506                 luaL_typerror(L, narg, className);
507
508         return *(LuaLocalPlayer **)ud;
509 }
510
511 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
512 {
513         return ref->m_localplayer;
514 }
515
516 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
517 {
518         LuaLocalPlayer *ref = checkobject(L, narg);
519         assert(ref);
520         LocalPlayer *player = getobject(ref);
521         assert(player);
522         return player;
523 }
524
525 int LuaLocalPlayer::gc_object(lua_State *L)
526 {
527         LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
528         delete o;
529         return 0;
530 }
531
532 void LuaLocalPlayer::Register(lua_State *L)
533 {
534         lua_newtable(L);
535         int methodtable = lua_gettop(L);
536         luaL_newmetatable(L, className);
537         int metatable = lua_gettop(L);
538
539         lua_pushliteral(L, "__metatable");
540         lua_pushvalue(L, methodtable);
541         lua_settable(L, metatable); // hide metatable from lua getmetatable()
542
543         lua_pushliteral(L, "__index");
544         lua_pushvalue(L, methodtable);
545         lua_settable(L, metatable);
546
547         lua_pushliteral(L, "__gc");
548         lua_pushcfunction(L, gc_object);
549         lua_settable(L, metatable);
550
551         lua_pop(L, 1); // Drop metatable
552
553         luaL_openlib(L, 0, methods, 0); // fill methodtable
554         lua_pop(L, 1);                  // Drop methodtable
555 }
556
557 const char LuaLocalPlayer::className[] = "LocalPlayer";
558 const luaL_Reg LuaLocalPlayer::methods[] = {
559                 luamethod(LuaLocalPlayer, get_velocity),
560                 luamethod(LuaLocalPlayer, set_velocity),
561                 luamethod(LuaLocalPlayer, get_yaw),
562                 luamethod(LuaLocalPlayer, set_yaw),
563                 luamethod(LuaLocalPlayer, get_pitch),
564                 luamethod(LuaLocalPlayer, set_pitch),
565                 luamethod(LuaLocalPlayer, get_hp),
566                 luamethod(LuaLocalPlayer, get_name),
567                 luamethod(LuaLocalPlayer, get_wield_index),
568                 luamethod(LuaLocalPlayer, set_wield_index),
569                 luamethod(LuaLocalPlayer, get_wielded_item),
570                 luamethod(LuaLocalPlayer, is_attached),
571                 luamethod(LuaLocalPlayer, is_touching_ground),
572                 luamethod(LuaLocalPlayer, is_in_liquid),
573                 luamethod(LuaLocalPlayer, is_in_liquid_stable),
574                 luamethod(LuaLocalPlayer, get_liquid_viscosity),
575                 luamethod(LuaLocalPlayer, is_climbing),
576                 luamethod(LuaLocalPlayer, swimming_vertical),
577                 luamethod(LuaLocalPlayer, get_physics_override),
578                 luamethod(LuaLocalPlayer, set_physics_override),
579                 // TODO: figure our if these are useful in any way
580                 luamethod(LuaLocalPlayer, get_last_pos),
581                 luamethod(LuaLocalPlayer, get_last_velocity),
582                 luamethod(LuaLocalPlayer, get_last_look_horizontal),
583                 luamethod(LuaLocalPlayer, get_last_look_vertical),
584                 //
585                 luamethod(LuaLocalPlayer, get_control),
586                 luamethod(LuaLocalPlayer, get_breath),
587                 luamethod(LuaLocalPlayer, get_pos),
588                 luamethod(LuaLocalPlayer, set_pos),
589                 luamethod(LuaLocalPlayer, get_movement_acceleration),
590                 luamethod(LuaLocalPlayer, get_movement_speed),
591                 luamethod(LuaLocalPlayer, get_movement),
592                 luamethod(LuaLocalPlayer, get_armor_groups),
593                 luamethod(LuaLocalPlayer, hud_add),
594                 luamethod(LuaLocalPlayer, hud_remove),
595                 luamethod(LuaLocalPlayer, hud_change),
596                 luamethod(LuaLocalPlayer, hud_get),
597                 luamethod(LuaLocalPlayer, get_object),
598                 luamethod(LuaLocalPlayer, get_hotbar_size),
599
600                 {0, 0}
601 };