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 "script/common/c_converter.h"
23 #include "client/localplayer.h"
25 #include "common/c_content.h"
27 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
31 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
33 lua_getglobal(L, "core");
34 luaL_checktype(L, -1, LUA_TTABLE);
35 int objectstable = lua_gettop(L);
36 lua_getfield(L, -1, "localplayer");
39 if (lua_type(L, -1) == LUA_TUSERDATA) {
44 LuaLocalPlayer *o = new LuaLocalPlayer(m);
45 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
46 luaL_getmetatable(L, className);
47 lua_setmetatable(L, -2);
49 lua_pushvalue(L, lua_gettop(L));
50 lua_setfield(L, objectstable, "localplayer");
53 int LuaLocalPlayer::l_get_velocity(lua_State *L)
55 LocalPlayer *player = getobject(L, 1);
57 push_v3f(L, player->getSpeed() / BS);
61 int LuaLocalPlayer::l_get_hp(lua_State *L)
63 LocalPlayer *player = getobject(L, 1);
65 lua_pushinteger(L, player->hp);
69 int LuaLocalPlayer::l_get_name(lua_State *L)
71 LocalPlayer *player = getobject(L, 1);
73 lua_pushstring(L, player->getName());
77 int LuaLocalPlayer::l_is_attached(lua_State *L)
79 LocalPlayer *player = getobject(L, 1);
81 lua_pushboolean(L, player->getParent() != nullptr);
85 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
87 LocalPlayer *player = getobject(L, 1);
89 lua_pushboolean(L, player->touching_ground);
93 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
95 LocalPlayer *player = getobject(L, 1);
97 lua_pushboolean(L, player->in_liquid);
101 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
103 LocalPlayer *player = getobject(L, 1);
105 lua_pushboolean(L, player->in_liquid_stable);
109 int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
111 LocalPlayer *player = getobject(L, 1);
113 lua_pushinteger(L, player->liquid_viscosity);
117 int LuaLocalPlayer::l_is_climbing(lua_State *L)
119 LocalPlayer *player = getobject(L, 1);
121 lua_pushboolean(L, player->is_climbing);
125 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
127 LocalPlayer *player = getobject(L, 1);
129 lua_pushboolean(L, player->swimming_vertical);
133 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
135 LocalPlayer *player = getobject(L, 1);
138 lua_pushnumber(L, player->physics_override_speed);
139 lua_setfield(L, -2, "speed");
141 lua_pushnumber(L, player->physics_override_jump);
142 lua_setfield(L, -2, "jump");
144 lua_pushnumber(L, player->physics_override_gravity);
145 lua_setfield(L, -2, "gravity");
147 lua_pushboolean(L, player->physics_override_sneak);
148 lua_setfield(L, -2, "sneak");
150 lua_pushboolean(L, player->physics_override_sneak_glitch);
151 lua_setfield(L, -2, "sneak_glitch");
156 int LuaLocalPlayer::l_get_override_pos(lua_State *L)
158 LocalPlayer *player = getobject(L, 1);
160 push_v3f(L, player->getPosition());
164 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
166 LocalPlayer *player = getobject(L, 1);
168 push_v3f(L, player->last_position / BS);
172 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
174 LocalPlayer *player = getobject(L, 1);
176 push_v3f(L, player->last_speed);
180 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
182 LocalPlayer *player = getobject(L, 1);
184 lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
188 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
190 LocalPlayer *player = getobject(L, 1);
192 lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
196 int LuaLocalPlayer::l_get_key_pressed(lua_State *L)
198 LocalPlayer *player = getobject(L, 1);
200 lua_pushinteger(L, player->last_keyPressed);
204 int LuaLocalPlayer::l_get_breath(lua_State *L)
206 LocalPlayer *player = getobject(L, 1);
208 lua_pushinteger(L, player->getBreath());
212 int LuaLocalPlayer::l_get_pos(lua_State *L)
214 LocalPlayer *player = getobject(L, 1);
216 push_v3f(L, player->getPosition() / BS);
220 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
222 LocalPlayer *player = getobject(L, 1);
225 lua_pushnumber(L, player->movement_acceleration_default);
226 lua_setfield(L, -2, "default");
228 lua_pushnumber(L, player->movement_acceleration_air);
229 lua_setfield(L, -2, "air");
231 lua_pushnumber(L, player->movement_acceleration_fast);
232 lua_setfield(L, -2, "fast");
237 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
239 LocalPlayer *player = getobject(L, 1);
242 lua_pushnumber(L, player->movement_speed_walk);
243 lua_setfield(L, -2, "walk");
245 lua_pushnumber(L, player->movement_speed_crouch);
246 lua_setfield(L, -2, "crouch");
248 lua_pushnumber(L, player->movement_speed_fast);
249 lua_setfield(L, -2, "fast");
251 lua_pushnumber(L, player->movement_speed_climb);
252 lua_setfield(L, -2, "climb");
254 lua_pushnumber(L, player->movement_speed_jump);
255 lua_setfield(L, -2, "jump");
260 int LuaLocalPlayer::l_get_movement(lua_State *L)
262 LocalPlayer *player = getobject(L, 1);
266 lua_pushnumber(L, player->movement_liquid_fluidity);
267 lua_setfield(L, -2, "liquid_fluidity");
269 lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
270 lua_setfield(L, -2, "liquid_fluidity_smooth");
272 lua_pushnumber(L, player->movement_liquid_sink);
273 lua_setfield(L, -2, "liquid_sink");
275 lua_pushnumber(L, player->movement_gravity);
276 lua_setfield(L, -2, "gravity");
282 // hud_add(self, form)
283 int LuaLocalPlayer::l_hud_add(lua_State *L)
285 LocalPlayer *player = getobject(L, 1);
287 HudElement *elem = new HudElement;
288 read_hud_element(L, elem);
290 u32 id = player->addHud(elem);
295 lua_pushnumber(L, id);
299 // hud_remove(self, id)
300 int LuaLocalPlayer::l_hud_remove(lua_State *L)
302 LocalPlayer *player = getobject(L, 1);
303 u32 id = luaL_checkinteger(L, 2);
304 HudElement *element = player->removeHud(id);
306 lua_pushboolean(L, false);
308 lua_pushboolean(L, true);
313 // hud_change(self, id, stat, data)
314 int LuaLocalPlayer::l_hud_change(lua_State *L)
316 LocalPlayer *player = getobject(L, 1);
318 u32 id = luaL_checkinteger(L, 2);
320 HudElement *element = player->getHud(id);
325 read_hud_change(L, element, &unused);
327 lua_pushboolean(L, true);
332 int LuaLocalPlayer::l_hud_get(lua_State *L)
334 LocalPlayer *player = getobject(L, 1);
336 u32 id = luaL_checkinteger(L, -1);
338 HudElement *e = player->getHud(id);
344 push_hud_element(L, e);
348 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
350 luaL_checktype(L, narg, LUA_TUSERDATA);
352 void *ud = luaL_checkudata(L, narg, className);
354 luaL_typerror(L, narg, className);
356 return *(LuaLocalPlayer **)ud;
359 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
361 return ref->m_localplayer;
364 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
366 LuaLocalPlayer *ref = checkobject(L, narg);
368 LocalPlayer *player = getobject(ref);
373 int LuaLocalPlayer::gc_object(lua_State *L)
375 LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
380 void LuaLocalPlayer::Register(lua_State *L)
383 int methodtable = lua_gettop(L);
384 luaL_newmetatable(L, className);
385 int metatable = lua_gettop(L);
387 lua_pushliteral(L, "__metatable");
388 lua_pushvalue(L, methodtable);
389 lua_settable(L, metatable); // hide metatable from lua getmetatable()
391 lua_pushliteral(L, "__index");
392 lua_pushvalue(L, methodtable);
393 lua_settable(L, metatable);
395 lua_pushliteral(L, "__gc");
396 lua_pushcfunction(L, gc_object);
397 lua_settable(L, metatable);
399 lua_pop(L, 1); // Drop metatable
401 luaL_openlib(L, 0, methods, 0); // fill methodtable
402 lua_pop(L, 1); // Drop methodtable
405 const char LuaLocalPlayer::className[] = "LocalPlayer";
406 const luaL_Reg LuaLocalPlayer::methods[] = {
407 luamethod(LuaLocalPlayer, get_velocity),
408 luamethod(LuaLocalPlayer, get_hp),
409 luamethod(LuaLocalPlayer, get_name),
410 luamethod(LuaLocalPlayer, is_attached),
411 luamethod(LuaLocalPlayer, is_touching_ground),
412 luamethod(LuaLocalPlayer, is_in_liquid),
413 luamethod(LuaLocalPlayer, is_in_liquid_stable),
414 luamethod(LuaLocalPlayer, get_liquid_viscosity),
415 luamethod(LuaLocalPlayer, is_climbing),
416 luamethod(LuaLocalPlayer, swimming_vertical),
417 luamethod(LuaLocalPlayer, get_physics_override),
418 luamethod(LuaLocalPlayer, get_override_pos),
419 luamethod(LuaLocalPlayer, get_last_pos),
420 luamethod(LuaLocalPlayer, get_last_velocity),
421 luamethod(LuaLocalPlayer, get_last_look_horizontal),
422 luamethod(LuaLocalPlayer, get_last_look_vertical),
423 luamethod(LuaLocalPlayer, get_key_pressed),
424 luamethod(LuaLocalPlayer, get_breath),
425 luamethod(LuaLocalPlayer, get_pos),
426 luamethod(LuaLocalPlayer, get_movement_acceleration),
427 luamethod(LuaLocalPlayer, get_movement_speed),
428 luamethod(LuaLocalPlayer, get_movement),
429 luamethod(LuaLocalPlayer, hud_add),
430 luamethod(LuaLocalPlayer, hud_remove),
431 luamethod(LuaLocalPlayer, hud_change),
432 luamethod(LuaLocalPlayer, hud_get),