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 "localplayer.h"
25 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
29 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
31 LuaLocalPlayer *o = new LuaLocalPlayer(m);
32 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
33 luaL_getmetatable(L, className);
34 lua_setmetatable(L, -2);
36 // Keep localplayer object stack id
37 int localplayer_object = lua_gettop(L);
39 lua_getglobal(L, "core");
40 luaL_checktype(L, -1, LUA_TTABLE);
41 int coretable = lua_gettop(L);
43 lua_pushvalue(L, localplayer_object);
44 lua_setfield(L, coretable, "localplayer");
47 int LuaLocalPlayer::l_get_velocity(lua_State *L)
49 LocalPlayer *player = getobject(L, 1);
51 push_v3f(L, player->getSpeed() / BS);
55 int LuaLocalPlayer::l_get_hp(lua_State *L)
57 LocalPlayer *player = getobject(L, 1);
59 lua_pushinteger(L, player->hp);
63 int LuaLocalPlayer::l_get_name(lua_State *L)
65 LocalPlayer *player = getobject(L, 1);
67 lua_pushstring(L, player->getName());
71 int LuaLocalPlayer::l_is_attached(lua_State *L)
73 LocalPlayer *player = getobject(L, 1);
75 lua_pushboolean(L, player->isAttached);
79 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
81 LocalPlayer *player = getobject(L, 1);
83 lua_pushboolean(L, player->touching_ground);
87 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
89 LocalPlayer *player = getobject(L, 1);
91 lua_pushboolean(L, player->in_liquid);
95 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
97 LocalPlayer *player = getobject(L, 1);
99 lua_pushboolean(L, player->in_liquid_stable);
103 int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
105 LocalPlayer *player = getobject(L, 1);
107 lua_pushinteger(L, player->liquid_viscosity);
111 int LuaLocalPlayer::l_is_climbing(lua_State *L)
113 LocalPlayer *player = getobject(L, 1);
115 lua_pushboolean(L, player->is_climbing);
119 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
121 LocalPlayer *player = getobject(L, 1);
123 lua_pushboolean(L, player->swimming_vertical);
127 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
129 LocalPlayer *player = getobject(L, 1);
132 lua_pushnumber(L, player->physics_override_speed);
133 lua_setfield(L, -2, "speed");
135 lua_pushnumber(L, player->physics_override_jump);
136 lua_setfield(L, -2, "jump");
138 lua_pushnumber(L, player->physics_override_gravity);
139 lua_setfield(L, -2, "gravity");
141 lua_pushboolean(L, player->physics_override_sneak);
142 lua_setfield(L, -2, "sneak");
144 lua_pushboolean(L, player->physics_override_sneak_glitch);
145 lua_setfield(L, -2, "sneak_glitch");
150 int LuaLocalPlayer::l_get_override_pos(lua_State *L)
152 LocalPlayer *player = getobject(L, 1);
154 push_v3f(L, player->overridePosition);
158 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
160 LocalPlayer *player = getobject(L, 1);
162 push_v3f(L, player->last_position / BS);
166 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
168 LocalPlayer *player = getobject(L, 1);
170 push_v3f(L, player->last_speed);
174 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
176 LocalPlayer *player = getobject(L, 1);
178 lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
182 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
184 LocalPlayer *player = getobject(L, 1);
186 lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
190 int LuaLocalPlayer::l_get_key_pressed(lua_State *L)
192 LocalPlayer *player = getobject(L, 1);
194 lua_pushinteger(L, player->last_keyPressed);
198 int LuaLocalPlayer::l_get_breath(lua_State *L)
200 LocalPlayer *player = getobject(L, 1);
202 lua_pushinteger(L, player->getBreath());
206 int LuaLocalPlayer::l_get_pos(lua_State *L)
208 LocalPlayer *player = getobject(L, 1);
210 push_v3f(L, player->getPosition() / BS);
214 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
216 LocalPlayer *player = getobject(L, 1);
219 lua_pushnumber(L, player->movement_acceleration_default);
220 lua_setfield(L, -2, "default");
222 lua_pushnumber(L, player->movement_acceleration_air);
223 lua_setfield(L, -2, "air");
225 lua_pushnumber(L, player->movement_acceleration_fast);
226 lua_setfield(L, -2, "fast");
231 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
233 LocalPlayer *player = getobject(L, 1);
236 lua_pushnumber(L, player->movement_speed_walk);
237 lua_setfield(L, -2, "walk");
239 lua_pushnumber(L, player->movement_speed_crouch);
240 lua_setfield(L, -2, "crouch");
242 lua_pushnumber(L, player->movement_speed_fast);
243 lua_setfield(L, -2, "fast");
245 lua_pushnumber(L, player->movement_speed_climb);
246 lua_setfield(L, -2, "climb");
248 lua_pushnumber(L, player->movement_speed_jump);
249 lua_setfield(L, -2, "jump");
254 int LuaLocalPlayer::l_get_movement(lua_State *L)
256 LocalPlayer *player = getobject(L, 1);
260 lua_pushnumber(L, player->movement_liquid_fluidity);
261 lua_setfield(L, -2, "liquid_fluidity");
263 lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
264 lua_setfield(L, -2, "liquid_fluidity_smooth");
266 lua_pushnumber(L, player->movement_liquid_sink);
267 lua_setfield(L, -2, "liquid_sink");
269 lua_pushnumber(L, player->movement_gravity);
270 lua_setfield(L, -2, "gravity");
275 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
277 luaL_checktype(L, narg, LUA_TUSERDATA);
279 void *ud = luaL_checkudata(L, narg, className);
281 luaL_typerror(L, narg, className);
283 return *(LuaLocalPlayer **)ud;
286 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
288 return ref->m_localplayer;
291 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
293 LuaLocalPlayer *ref = checkobject(L, narg);
295 LocalPlayer *player = getobject(ref);
300 int LuaLocalPlayer::gc_object(lua_State *L)
302 LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
307 void LuaLocalPlayer::Register(lua_State *L)
310 int methodtable = lua_gettop(L);
311 luaL_newmetatable(L, className);
312 int metatable = lua_gettop(L);
314 lua_pushliteral(L, "__metatable");
315 lua_pushvalue(L, methodtable);
316 lua_settable(L, metatable); // hide metatable from lua getmetatable()
318 lua_pushliteral(L, "__index");
319 lua_pushvalue(L, methodtable);
320 lua_settable(L, metatable);
322 lua_pushliteral(L, "__gc");
323 lua_pushcfunction(L, gc_object);
324 lua_settable(L, metatable);
326 lua_pop(L, 1); // Drop metatable
328 luaL_openlib(L, 0, methods, 0); // fill methodtable
329 lua_pop(L, 1); // Drop methodtable
332 const char LuaLocalPlayer::className[] = "LocalPlayer";
333 const luaL_Reg LuaLocalPlayer::methods[] = {
334 luamethod(LuaLocalPlayer, get_velocity),
335 luamethod(LuaLocalPlayer, get_hp),
336 luamethod(LuaLocalPlayer, get_name),
337 luamethod(LuaLocalPlayer, is_attached),
338 luamethod(LuaLocalPlayer, is_touching_ground),
339 luamethod(LuaLocalPlayer, is_in_liquid),
340 luamethod(LuaLocalPlayer, is_in_liquid_stable),
341 luamethod(LuaLocalPlayer, get_liquid_viscosity),
342 luamethod(LuaLocalPlayer, is_climbing),
343 luamethod(LuaLocalPlayer, swimming_vertical),
344 luamethod(LuaLocalPlayer, get_physics_override),
345 luamethod(LuaLocalPlayer, get_override_pos),
346 luamethod(LuaLocalPlayer, get_last_pos),
347 luamethod(LuaLocalPlayer, get_last_velocity),
348 luamethod(LuaLocalPlayer, get_last_look_horizontal),
349 luamethod(LuaLocalPlayer, get_last_look_vertical),
350 luamethod(LuaLocalPlayer, get_key_pressed),
351 luamethod(LuaLocalPlayer, get_breath),
352 luamethod(LuaLocalPlayer, get_pos),
353 luamethod(LuaLocalPlayer, get_movement_acceleration),
354 luamethod(LuaLocalPlayer, get_movement_speed),
355 luamethod(LuaLocalPlayer, get_movement),