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"
24 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
28 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
30 LuaLocalPlayer *o = new LuaLocalPlayer(m);
31 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
32 luaL_getmetatable(L, className);
33 lua_setmetatable(L, -2);
35 // Keep localplayer object stack id
36 int localplayer_object = lua_gettop(L);
38 lua_getglobal(L, "core");
39 luaL_checktype(L, -1, LUA_TTABLE);
40 int coretable = lua_gettop(L);
42 lua_pushvalue(L, localplayer_object);
43 lua_setfield(L, coretable, "localplayer");
46 int LuaLocalPlayer::l_get_velocity(lua_State *L)
48 LocalPlayer *player = getobject(L, 1);
50 push_v3f(L, player->getSpeed() / BS);
54 int LuaLocalPlayer::l_get_hp(lua_State *L)
56 LocalPlayer *player = getobject(L, 1);
58 lua_pushinteger(L, player->hp);
62 int LuaLocalPlayer::l_get_name(lua_State *L)
64 LocalPlayer *player = getobject(L, 1);
66 lua_pushstring(L, player->getName());
70 int LuaLocalPlayer::l_is_attached(lua_State *L)
72 LocalPlayer *player = getobject(L, 1);
74 lua_pushboolean(L, player->isAttached);
78 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
80 LocalPlayer *player = getobject(L, 1);
82 lua_pushboolean(L, player->touching_ground);
86 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
88 LocalPlayer *player = getobject(L, 1);
90 lua_pushboolean(L, player->in_liquid);
94 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
96 LocalPlayer *player = getobject(L, 1);
98 lua_pushboolean(L, player->in_liquid_stable);
102 int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
104 LocalPlayer *player = getobject(L, 1);
106 lua_pushinteger(L, player->liquid_viscosity);
110 int LuaLocalPlayer::l_is_climbing(lua_State *L)
112 LocalPlayer *player = getobject(L, 1);
114 lua_pushboolean(L, player->is_climbing);
118 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
120 LocalPlayer *player = getobject(L, 1);
122 lua_pushboolean(L, player->swimming_vertical);
126 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
128 LocalPlayer *player = getobject(L, 1);
131 lua_pushnumber(L, player->physics_override_speed);
132 lua_setfield(L, -2, "speed");
134 lua_pushnumber(L, player->physics_override_jump);
135 lua_setfield(L, -2, "jump");
137 lua_pushnumber(L, player->physics_override_gravity);
138 lua_setfield(L, -2, "gravity");
140 lua_pushboolean(L, player->physics_override_sneak);
141 lua_setfield(L, -2, "sneak");
143 lua_pushboolean(L, player->physics_override_sneak_glitch);
144 lua_setfield(L, -2, "sneak_glitch");
149 int LuaLocalPlayer::l_get_override_pos(lua_State *L)
151 LocalPlayer *player = getobject(L, 1);
153 push_v3f(L, player->overridePosition);
157 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
159 LocalPlayer *player = getobject(L, 1);
161 push_v3f(L, player->last_position / BS);
165 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
167 LocalPlayer *player = getobject(L, 1);
169 push_v3f(L, player->last_speed);
173 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
175 LocalPlayer *player = getobject(L, 1);
177 lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
181 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
183 LocalPlayer *player = getobject(L, 1);
185 lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
189 int LuaLocalPlayer::l_get_key_pressed(lua_State *L)
191 LocalPlayer *player = getobject(L, 1);
193 lua_pushinteger(L, player->last_keyPressed);
197 int LuaLocalPlayer::l_get_breath(lua_State *L)
199 LocalPlayer *player = getobject(L, 1);
201 lua_pushinteger(L, player->getBreath());
205 int LuaLocalPlayer::l_get_pos(lua_State *L)
207 LocalPlayer *player = getobject(L, 1);
209 push_v3f(L, player->getPosition() / BS);
213 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
215 LocalPlayer *player = getobject(L, 1);
218 lua_pushnumber(L, player->movement_acceleration_default);
219 lua_setfield(L, -2, "default");
221 lua_pushnumber(L, player->movement_acceleration_air);
222 lua_setfield(L, -2, "air");
224 lua_pushnumber(L, player->movement_acceleration_fast);
225 lua_setfield(L, -2, "fast");
230 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
232 LocalPlayer *player = getobject(L, 1);
235 lua_pushnumber(L, player->movement_speed_walk);
236 lua_setfield(L, -2, "walk");
238 lua_pushnumber(L, player->movement_speed_crouch);
239 lua_setfield(L, -2, "crouch");
241 lua_pushnumber(L, player->movement_speed_fast);
242 lua_setfield(L, -2, "fast");
244 lua_pushnumber(L, player->movement_speed_climb);
245 lua_setfield(L, -2, "climb");
247 lua_pushnumber(L, player->movement_speed_jump);
248 lua_setfield(L, -2, "jump");
253 int LuaLocalPlayer::l_get_movement(lua_State *L)
255 LocalPlayer *player = getobject(L, 1);
259 lua_pushnumber(L, player->movement_liquid_fluidity);
260 lua_setfield(L, -2, "liquid_fluidity");
262 lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
263 lua_setfield(L, -2, "liquid_fluidity_smooth");
265 lua_pushnumber(L, player->movement_liquid_sink);
266 lua_setfield(L, -2, "liquid_sink");
268 lua_pushnumber(L, player->movement_gravity);
269 lua_setfield(L, -2, "gravity");
274 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
276 luaL_checktype(L, narg, LUA_TUSERDATA);
278 void *ud = luaL_checkudata(L, narg, className);
280 luaL_typerror(L, narg, className);
282 return *(LuaLocalPlayer **)ud;
285 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
287 return ref->m_localplayer;
290 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
292 LuaLocalPlayer *ref = checkobject(L, narg);
294 LocalPlayer *player = getobject(ref);
299 int LuaLocalPlayer::gc_object(lua_State *L)
301 LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
306 void LuaLocalPlayer::Register(lua_State *L)
309 int methodtable = lua_gettop(L);
310 luaL_newmetatable(L, className);
311 int metatable = lua_gettop(L);
313 lua_pushliteral(L, "__metatable");
314 lua_pushvalue(L, methodtable);
315 lua_settable(L, metatable); // hide metatable from lua getmetatable()
317 lua_pushliteral(L, "__index");
318 lua_pushvalue(L, methodtable);
319 lua_settable(L, metatable);
321 lua_pushliteral(L, "__gc");
322 lua_pushcfunction(L, gc_object);
323 lua_settable(L, metatable);
325 lua_pop(L, 1); // Drop metatable
327 luaL_openlib(L, 0, methods, 0); // fill methodtable
328 lua_pop(L, 1); // Drop methodtable
331 const char LuaLocalPlayer::className[] = "LocalPlayer";
332 const luaL_Reg LuaLocalPlayer::methods[] = {
333 luamethod(LuaLocalPlayer, get_velocity),
334 luamethod(LuaLocalPlayer, get_hp),
335 luamethod(LuaLocalPlayer, get_name),
336 luamethod(LuaLocalPlayer, is_attached),
337 luamethod(LuaLocalPlayer, is_touching_ground),
338 luamethod(LuaLocalPlayer, is_in_liquid),
339 luamethod(LuaLocalPlayer, is_in_liquid_stable),
340 luamethod(LuaLocalPlayer, get_liquid_viscosity),
341 luamethod(LuaLocalPlayer, is_climbing),
342 luamethod(LuaLocalPlayer, swimming_vertical),
343 luamethod(LuaLocalPlayer, get_physics_override),
344 luamethod(LuaLocalPlayer, get_override_pos),
345 luamethod(LuaLocalPlayer, get_last_pos),
346 luamethod(LuaLocalPlayer, get_last_velocity),
347 luamethod(LuaLocalPlayer, get_last_look_horizontal),
348 luamethod(LuaLocalPlayer, get_last_look_vertical),
349 luamethod(LuaLocalPlayer, get_key_pressed),
350 luamethod(LuaLocalPlayer, get_breath),
351 luamethod(LuaLocalPlayer, get_pos),
352 luamethod(LuaLocalPlayer, get_movement_acceleration),
353 luamethod(LuaLocalPlayer, get_movement_speed),
354 luamethod(LuaLocalPlayer, get_movement),