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