3 Copyright (C) 2017 Dumbeldor, Vincent Glize <vincent.glize@live.fr>
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.
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.
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.
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"
26 #include "common/c_content.h"
27 #include "client/content_cao.h"
29 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
33 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
35 lua_getglobal(L, "core");
36 luaL_checktype(L, -1, LUA_TTABLE);
37 int objectstable = lua_gettop(L);
38 lua_getfield(L, -1, "localplayer");
41 if (lua_type(L, -1) == LUA_TUSERDATA) {
46 LuaLocalPlayer *o = new LuaLocalPlayer(m);
47 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
48 luaL_getmetatable(L, className);
49 lua_setmetatable(L, -2);
51 lua_pushvalue(L, lua_gettop(L));
52 lua_setfield(L, objectstable, "localplayer");
55 int LuaLocalPlayer::l_get_velocity(lua_State *L)
57 LocalPlayer *player = getobject(L, 1);
59 push_v3f(L, player->getSpeed() / BS);
63 int LuaLocalPlayer::l_get_hp(lua_State *L)
65 LocalPlayer *player = getobject(L, 1);
67 lua_pushinteger(L, player->hp);
71 int LuaLocalPlayer::l_get_name(lua_State *L)
73 LocalPlayer *player = getobject(L, 1);
75 lua_pushstring(L, player->getName());
79 // get_wield_index(self)
80 int LuaLocalPlayer::l_get_wield_index(lua_State *L)
82 LocalPlayer *player = getobject(L, 1);
84 lua_pushinteger(L, player->getWieldIndex());
88 // get_wielded_item(self)
89 int LuaLocalPlayer::l_get_wielded_item(lua_State *L)
91 LocalPlayer *player = getobject(L, 1);
93 ItemStack selected_item;
94 player->getWieldedItem(&selected_item, nullptr);
95 LuaItemStack::create(L, selected_item);
99 int LuaLocalPlayer::l_is_attached(lua_State *L)
101 LocalPlayer *player = getobject(L, 1);
103 lua_pushboolean(L, player->getParent() != nullptr);
107 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
109 LocalPlayer *player = getobject(L, 1);
111 lua_pushboolean(L, player->touching_ground);
115 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
117 LocalPlayer *player = getobject(L, 1);
119 lua_pushboolean(L, player->in_liquid);
123 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
125 LocalPlayer *player = getobject(L, 1);
127 lua_pushboolean(L, player->in_liquid_stable);
131 int LuaLocalPlayer::l_get_move_resistance(lua_State *L)
133 LocalPlayer *player = getobject(L, 1);
135 lua_pushinteger(L, player->move_resistance);
139 int LuaLocalPlayer::l_is_climbing(lua_State *L)
141 LocalPlayer *player = getobject(L, 1);
143 lua_pushboolean(L, player->is_climbing);
147 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
149 LocalPlayer *player = getobject(L, 1);
151 lua_pushboolean(L, player->swimming_vertical);
155 // get_physics_override(self)
156 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
158 LocalPlayer *player = getobject(L, 1);
161 lua_pushnumber(L, player->physics_override_speed);
162 lua_setfield(L, -2, "speed");
164 lua_pushnumber(L, player->physics_override_jump);
165 lua_setfield(L, -2, "jump");
167 lua_pushnumber(L, player->physics_override_gravity);
168 lua_setfield(L, -2, "gravity");
170 lua_pushboolean(L, player->physics_override_sneak);
171 lua_setfield(L, -2, "sneak");
173 lua_pushboolean(L, player->physics_override_sneak_glitch);
174 lua_setfield(L, -2, "sneak_glitch");
176 lua_pushboolean(L, player->physics_override_new_move);
177 lua_setfield(L, -2, "new_move");
182 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
184 LocalPlayer *player = getobject(L, 1);
186 push_v3f(L, player->last_position / BS);
190 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
192 LocalPlayer *player = getobject(L, 1);
194 push_v3f(L, player->last_speed);
198 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
200 LocalPlayer *player = getobject(L, 1);
202 lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
206 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
208 LocalPlayer *player = getobject(L, 1);
210 lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
215 int LuaLocalPlayer::l_get_control(lua_State *L)
217 LocalPlayer *player = getobject(L, 1);
218 const PlayerControl &c = player->getPlayerControl();
220 auto set = [L] (const char *name, bool value) {
221 lua_pushboolean(L, value);
222 lua_setfield(L, -2, name);
225 lua_createtable(L, 0, 12);
228 set("sneak", c.sneak);
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));
247 int LuaLocalPlayer::l_get_breath(lua_State *L)
249 LocalPlayer *player = getobject(L, 1);
251 lua_pushinteger(L, player->getBreath());
256 int LuaLocalPlayer::l_get_pos(lua_State *L)
258 LocalPlayer *player = getobject(L, 1);
260 push_v3f(L, player->getPosition() / BS);
264 // get_movement_acceleration(self)
265 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
267 LocalPlayer *player = getobject(L, 1);
270 lua_pushnumber(L, player->movement_acceleration_default);
271 lua_setfield(L, -2, "default");
273 lua_pushnumber(L, player->movement_acceleration_air);
274 lua_setfield(L, -2, "air");
276 lua_pushnumber(L, player->movement_acceleration_fast);
277 lua_setfield(L, -2, "fast");
282 // get_movement_speed(self)
283 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
285 LocalPlayer *player = getobject(L, 1);
288 lua_pushnumber(L, player->movement_speed_walk);
289 lua_setfield(L, -2, "walk");
291 lua_pushnumber(L, player->movement_speed_crouch);
292 lua_setfield(L, -2, "crouch");
294 lua_pushnumber(L, player->movement_speed_fast);
295 lua_setfield(L, -2, "fast");
297 lua_pushnumber(L, player->movement_speed_climb);
298 lua_setfield(L, -2, "climb");
300 lua_pushnumber(L, player->movement_speed_jump);
301 lua_setfield(L, -2, "jump");
306 // get_movement(self)
307 int LuaLocalPlayer::l_get_movement(lua_State *L)
309 LocalPlayer *player = getobject(L, 1);
313 lua_pushnumber(L, player->movement_liquid_fluidity);
314 lua_setfield(L, -2, "liquid_fluidity");
316 lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
317 lua_setfield(L, -2, "liquid_fluidity_smooth");
319 lua_pushnumber(L, player->movement_liquid_sink);
320 lua_setfield(L, -2, "liquid_sink");
322 lua_pushnumber(L, player->movement_gravity);
323 lua_setfield(L, -2, "gravity");
328 // get_armor_groups(self)
329 int LuaLocalPlayer::l_get_armor_groups(lua_State *L)
331 LocalPlayer *player = getobject(L, 1);
332 push_groups(L, player->getCAO()->getGroups());
336 // hud_add(self, form)
337 int LuaLocalPlayer::l_hud_add(lua_State *L)
339 LocalPlayer *player = getobject(L, 1);
341 HudElement *elem = new HudElement;
342 read_hud_element(L, elem);
344 u32 id = player->addHud(elem);
349 lua_pushnumber(L, id);
353 // hud_remove(self, id)
354 int LuaLocalPlayer::l_hud_remove(lua_State *L)
356 LocalPlayer *player = getobject(L, 1);
357 u32 id = luaL_checkinteger(L, 2);
358 HudElement *element = player->removeHud(id);
360 lua_pushboolean(L, false);
362 lua_pushboolean(L, true);
367 // hud_change(self, id, stat, data)
368 int LuaLocalPlayer::l_hud_change(lua_State *L)
370 LocalPlayer *player = getobject(L, 1);
372 u32 id = luaL_checkinteger(L, 2);
374 HudElement *element = player->getHud(id);
380 bool ok = read_hud_change(L, stat, element, &unused);
382 lua_pushboolean(L, ok);
387 int LuaLocalPlayer::l_hud_get(lua_State *L)
389 LocalPlayer *player = getobject(L, 1);
391 u32 id = luaL_checkinteger(L, -1);
393 HudElement *e = player->getHud(id);
399 push_hud_element(L, e);
403 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
405 luaL_checktype(L, narg, LUA_TUSERDATA);
407 void *ud = luaL_checkudata(L, narg, className);
409 luaL_typerror(L, narg, className);
411 return *(LuaLocalPlayer **)ud;
414 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
416 return ref->m_localplayer;
419 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
421 LuaLocalPlayer *ref = checkobject(L, narg);
423 LocalPlayer *player = getobject(ref);
428 int LuaLocalPlayer::gc_object(lua_State *L)
430 LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
435 void LuaLocalPlayer::Register(lua_State *L)
438 int methodtable = lua_gettop(L);
439 luaL_newmetatable(L, className);
440 int metatable = lua_gettop(L);
442 lua_pushliteral(L, "__metatable");
443 lua_pushvalue(L, methodtable);
444 lua_settable(L, metatable); // hide metatable from lua getmetatable()
446 lua_pushliteral(L, "__index");
447 lua_pushvalue(L, methodtable);
448 lua_settable(L, metatable);
450 lua_pushliteral(L, "__gc");
451 lua_pushcfunction(L, gc_object);
452 lua_settable(L, metatable);
454 lua_pop(L, 1); // Drop metatable
456 luaL_register(L, nullptr, methods); // fill methodtable
457 lua_pop(L, 1); // Drop methodtable
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),
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),
492 luamethod(LuaLocalPlayer, get_move_resistance),