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_liquid_viscosity(lua_State *L)
133 LocalPlayer *player = getobject(L, 1);
135 lua_pushinteger(L, player->liquid_viscosity);
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);
229 set("right", c.right);
232 set("sneak", c.sneak);
241 int LuaLocalPlayer::l_get_breath(lua_State *L)
243 LocalPlayer *player = getobject(L, 1);
245 lua_pushinteger(L, player->getBreath());
250 int LuaLocalPlayer::l_get_pos(lua_State *L)
252 LocalPlayer *player = getobject(L, 1);
254 push_v3f(L, player->getPosition() / BS);
258 // get_movement_acceleration(self)
259 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
261 LocalPlayer *player = getobject(L, 1);
264 lua_pushnumber(L, player->movement_acceleration_default);
265 lua_setfield(L, -2, "default");
267 lua_pushnumber(L, player->movement_acceleration_air);
268 lua_setfield(L, -2, "air");
270 lua_pushnumber(L, player->movement_acceleration_fast);
271 lua_setfield(L, -2, "fast");
276 // get_movement_speed(self)
277 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
279 LocalPlayer *player = getobject(L, 1);
282 lua_pushnumber(L, player->movement_speed_walk);
283 lua_setfield(L, -2, "walk");
285 lua_pushnumber(L, player->movement_speed_crouch);
286 lua_setfield(L, -2, "crouch");
288 lua_pushnumber(L, player->movement_speed_fast);
289 lua_setfield(L, -2, "fast");
291 lua_pushnumber(L, player->movement_speed_climb);
292 lua_setfield(L, -2, "climb");
294 lua_pushnumber(L, player->movement_speed_jump);
295 lua_setfield(L, -2, "jump");
300 // get_movement(self)
301 int LuaLocalPlayer::l_get_movement(lua_State *L)
303 LocalPlayer *player = getobject(L, 1);
307 lua_pushnumber(L, player->movement_liquid_fluidity);
308 lua_setfield(L, -2, "liquid_fluidity");
310 lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
311 lua_setfield(L, -2, "liquid_fluidity_smooth");
313 lua_pushnumber(L, player->movement_liquid_sink);
314 lua_setfield(L, -2, "liquid_sink");
316 lua_pushnumber(L, player->movement_gravity);
317 lua_setfield(L, -2, "gravity");
322 // get_armor_groups(self)
323 int LuaLocalPlayer::l_get_armor_groups(lua_State *L)
325 LocalPlayer *player = getobject(L, 1);
326 push_groups(L, player->getCAO()->getGroups());
330 // hud_add(self, form)
331 int LuaLocalPlayer::l_hud_add(lua_State *L)
333 LocalPlayer *player = getobject(L, 1);
335 HudElement *elem = new HudElement;
336 read_hud_element(L, elem);
338 u32 id = player->addHud(elem);
343 lua_pushnumber(L, id);
347 // hud_remove(self, id)
348 int LuaLocalPlayer::l_hud_remove(lua_State *L)
350 LocalPlayer *player = getobject(L, 1);
351 u32 id = luaL_checkinteger(L, 2);
352 HudElement *element = player->removeHud(id);
354 lua_pushboolean(L, false);
356 lua_pushboolean(L, true);
361 // hud_change(self, id, stat, data)
362 int LuaLocalPlayer::l_hud_change(lua_State *L)
364 LocalPlayer *player = getobject(L, 1);
366 u32 id = luaL_checkinteger(L, 2);
368 HudElement *element = player->getHud(id);
373 read_hud_change(L, element, &unused);
375 lua_pushboolean(L, true);
380 int LuaLocalPlayer::l_hud_get(lua_State *L)
382 LocalPlayer *player = getobject(L, 1);
384 u32 id = luaL_checkinteger(L, -1);
386 HudElement *e = player->getHud(id);
392 push_hud_element(L, e);
396 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
398 luaL_checktype(L, narg, LUA_TUSERDATA);
400 void *ud = luaL_checkudata(L, narg, className);
402 luaL_typerror(L, narg, className);
404 return *(LuaLocalPlayer **)ud;
407 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
409 return ref->m_localplayer;
412 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
414 LuaLocalPlayer *ref = checkobject(L, narg);
416 LocalPlayer *player = getobject(ref);
421 int LuaLocalPlayer::gc_object(lua_State *L)
423 LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
428 void LuaLocalPlayer::Register(lua_State *L)
431 int methodtable = lua_gettop(L);
432 luaL_newmetatable(L, className);
433 int metatable = lua_gettop(L);
435 lua_pushliteral(L, "__metatable");
436 lua_pushvalue(L, methodtable);
437 lua_settable(L, metatable); // hide metatable from lua getmetatable()
439 lua_pushliteral(L, "__index");
440 lua_pushvalue(L, methodtable);
441 lua_settable(L, metatable);
443 lua_pushliteral(L, "__gc");
444 lua_pushcfunction(L, gc_object);
445 lua_settable(L, metatable);
447 lua_pop(L, 1); // Drop metatable
449 luaL_openlib(L, 0, methods, 0); // fill methodtable
450 lua_pop(L, 1); // Drop methodtable
453 const char LuaLocalPlayer::className[] = "LocalPlayer";
454 const luaL_Reg LuaLocalPlayer::methods[] = {
455 luamethod(LuaLocalPlayer, get_velocity),
456 luamethod(LuaLocalPlayer, get_hp),
457 luamethod(LuaLocalPlayer, get_name),
458 luamethod(LuaLocalPlayer, get_wield_index),
459 luamethod(LuaLocalPlayer, get_wielded_item),
460 luamethod(LuaLocalPlayer, is_attached),
461 luamethod(LuaLocalPlayer, is_touching_ground),
462 luamethod(LuaLocalPlayer, is_in_liquid),
463 luamethod(LuaLocalPlayer, is_in_liquid_stable),
464 luamethod(LuaLocalPlayer, get_liquid_viscosity),
465 luamethod(LuaLocalPlayer, is_climbing),
466 luamethod(LuaLocalPlayer, swimming_vertical),
467 luamethod(LuaLocalPlayer, get_physics_override),
468 // TODO: figure our if these are useful in any way
469 luamethod(LuaLocalPlayer, get_last_pos),
470 luamethod(LuaLocalPlayer, get_last_velocity),
471 luamethod(LuaLocalPlayer, get_last_look_horizontal),
472 luamethod(LuaLocalPlayer, get_last_look_vertical),
474 luamethod(LuaLocalPlayer, get_control),
475 luamethod(LuaLocalPlayer, get_breath),
476 luamethod(LuaLocalPlayer, get_pos),
477 luamethod(LuaLocalPlayer, get_movement_acceleration),
478 luamethod(LuaLocalPlayer, get_movement_speed),
479 luamethod(LuaLocalPlayer, get_movement),
480 luamethod(LuaLocalPlayer, get_armor_groups),
481 luamethod(LuaLocalPlayer, hud_add),
482 luamethod(LuaLocalPlayer, hud_remove),
483 luamethod(LuaLocalPlayer, hud_change),
484 luamethod(LuaLocalPlayer, hud_get),