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)
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_look_dir(lua_State *L)
208 LocalPlayer *player = getobject(L, 1);
210 float pitch = -1.0 * player->getPitch() * core::DEGTORAD;
211 float yaw = (player->getYaw() + 90.) * core::DEGTORAD;
212 v3f v(cos(pitch) * cos(yaw), sin(pitch), cos(pitch) * sin(yaw));
218 int LuaLocalPlayer::l_get_look_horizontal(lua_State *L)
220 LocalPlayer *player = getobject(L, 1);
222 lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
226 int LuaLocalPlayer::l_get_look_vertical(lua_State *L)
228 LocalPlayer *player = getobject(L, 1);
230 lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
234 int LuaLocalPlayer::l_get_pos(lua_State *L)
236 LocalPlayer *player = getobject(L, 1);
238 push_v3f(L, player->getPosition() / BS);
242 int LuaLocalPlayer::l_get_eye_pos(lua_State *L)
244 LocalPlayer *player = getobject(L, 1);
246 push_v3f(L, player->getEyePosition());
250 int LuaLocalPlayer::l_get_eye_offset(lua_State *L)
252 LocalPlayer *player = getobject(L, 1);
254 push_v3f(L, player->getEyeOffset());
258 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
260 LocalPlayer *player = getobject(L, 1);
263 lua_pushnumber(L, player->movement_acceleration_default);
264 lua_setfield(L, -2, "default");
266 lua_pushnumber(L, player->movement_acceleration_air);
267 lua_setfield(L, -2, "air");
269 lua_pushnumber(L, player->movement_acceleration_fast);
270 lua_setfield(L, -2, "fast");
275 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
277 LocalPlayer *player = getobject(L, 1);
280 lua_pushnumber(L, player->movement_speed_walk);
281 lua_setfield(L, -2, "walk");
283 lua_pushnumber(L, player->movement_speed_crouch);
284 lua_setfield(L, -2, "crouch");
286 lua_pushnumber(L, player->movement_speed_fast);
287 lua_setfield(L, -2, "fast");
289 lua_pushnumber(L, player->movement_speed_climb);
290 lua_setfield(L, -2, "climb");
292 lua_pushnumber(L, player->movement_speed_jump);
293 lua_setfield(L, -2, "jump");
298 int LuaLocalPlayer::l_get_movement(lua_State *L)
300 LocalPlayer *player = getobject(L, 1);
304 lua_pushnumber(L, player->movement_liquid_fluidity);
305 lua_setfield(L, -2, "liquid_fluidity");
307 lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
308 lua_setfield(L, -2, "liquid_fluidity_smooth");
310 lua_pushnumber(L, player->movement_liquid_sink);
311 lua_setfield(L, -2, "liquid_sink");
313 lua_pushnumber(L, player->movement_gravity);
314 lua_setfield(L, -2, "gravity");
319 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
321 luaL_checktype(L, narg, LUA_TUSERDATA);
323 void *ud = luaL_checkudata(L, narg, className);
325 luaL_typerror(L, narg, className);
327 return *(LuaLocalPlayer **)ud;
330 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
332 return ref->m_localplayer;
335 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
337 LuaLocalPlayer *ref = checkobject(L, narg);
339 LocalPlayer *player = getobject(ref);
344 int LuaLocalPlayer::gc_object(lua_State *L)
346 LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
351 void LuaLocalPlayer::Register(lua_State *L)
354 int methodtable = lua_gettop(L);
355 luaL_newmetatable(L, className);
356 int metatable = lua_gettop(L);
358 lua_pushliteral(L, "__metatable");
359 lua_pushvalue(L, methodtable);
360 lua_settable(L, metatable); // hide metatable from lua getmetatable()
362 lua_pushliteral(L, "__index");
363 lua_pushvalue(L, methodtable);
364 lua_settable(L, metatable);
366 lua_pushliteral(L, "__gc");
367 lua_pushcfunction(L, gc_object);
368 lua_settable(L, metatable);
370 lua_pop(L, 1); // Drop metatable
372 luaL_openlib(L, 0, methods, 0); // fill methodtable
373 lua_pop(L, 1); // Drop methodtable
376 const char LuaLocalPlayer::className[] = "LocalPlayer";
377 const luaL_Reg LuaLocalPlayer::methods[] = {
378 luamethod(LuaLocalPlayer, get_velocity),
379 luamethod(LuaLocalPlayer, get_hp),
380 luamethod(LuaLocalPlayer, get_name),
381 luamethod(LuaLocalPlayer, is_attached),
382 luamethod(LuaLocalPlayer, is_touching_ground),
383 luamethod(LuaLocalPlayer, is_in_liquid),
384 luamethod(LuaLocalPlayer, is_in_liquid_stable),
385 luamethod(LuaLocalPlayer, get_liquid_viscosity),
386 luamethod(LuaLocalPlayer, is_climbing),
387 luamethod(LuaLocalPlayer, swimming_vertical),
388 luamethod(LuaLocalPlayer, get_physics_override),
389 luamethod(LuaLocalPlayer, get_override_pos),
390 luamethod(LuaLocalPlayer, get_last_pos),
391 luamethod(LuaLocalPlayer, get_last_velocity),
392 luamethod(LuaLocalPlayer, get_last_look_horizontal),
393 luamethod(LuaLocalPlayer, get_last_look_vertical),
394 luamethod(LuaLocalPlayer, get_key_pressed),
395 luamethod(LuaLocalPlayer, get_breath),
396 luamethod(LuaLocalPlayer, get_look_dir),
397 luamethod(LuaLocalPlayer, get_look_horizontal),
398 luamethod(LuaLocalPlayer, get_look_vertical),
399 luamethod(LuaLocalPlayer, get_pos),
400 luamethod(LuaLocalPlayer, get_eye_pos),
401 luamethod(LuaLocalPlayer, get_eye_offset),
402 luamethod(LuaLocalPlayer, get_movement_acceleration),
403 luamethod(LuaLocalPlayer, get_movement_speed),
404 luamethod(LuaLocalPlayer, get_movement),