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 #include "common/c_content.h"
27 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
31 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
33 LuaLocalPlayer *o = new LuaLocalPlayer(m);
34 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
35 luaL_getmetatable(L, className);
36 lua_setmetatable(L, -2);
38 // Keep localplayer object stack id
39 int localplayer_object = lua_gettop(L);
41 lua_getglobal(L, "core");
42 luaL_checktype(L, -1, LUA_TTABLE);
43 int coretable = lua_gettop(L);
45 lua_pushvalue(L, localplayer_object);
46 lua_setfield(L, coretable, "localplayer");
49 int LuaLocalPlayer::l_get_velocity(lua_State *L)
51 LocalPlayer *player = getobject(L, 1);
53 push_v3f(L, player->getSpeed() / BS);
57 int LuaLocalPlayer::l_get_hp(lua_State *L)
59 LocalPlayer *player = getobject(L, 1);
61 lua_pushinteger(L, player->hp);
65 int LuaLocalPlayer::l_get_name(lua_State *L)
67 LocalPlayer *player = getobject(L, 1);
69 lua_pushstring(L, player->getName());
73 int LuaLocalPlayer::l_is_attached(lua_State *L)
75 LocalPlayer *player = getobject(L, 1);
77 lua_pushboolean(L, player->isAttached);
81 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
83 LocalPlayer *player = getobject(L, 1);
85 lua_pushboolean(L, player->touching_ground);
89 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
91 LocalPlayer *player = getobject(L, 1);
93 lua_pushboolean(L, player->in_liquid);
97 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
99 LocalPlayer *player = getobject(L, 1);
101 lua_pushboolean(L, player->in_liquid_stable);
105 int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
107 LocalPlayer *player = getobject(L, 1);
109 lua_pushinteger(L, player->liquid_viscosity);
113 int LuaLocalPlayer::l_is_climbing(lua_State *L)
115 LocalPlayer *player = getobject(L, 1);
117 lua_pushboolean(L, player->is_climbing);
121 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
123 LocalPlayer *player = getobject(L, 1);
125 lua_pushboolean(L, player->swimming_vertical);
129 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
131 LocalPlayer *player = getobject(L, 1);
134 lua_pushnumber(L, player->physics_override_speed);
135 lua_setfield(L, -2, "speed");
137 lua_pushnumber(L, player->physics_override_jump);
138 lua_setfield(L, -2, "jump");
140 lua_pushnumber(L, player->physics_override_gravity);
141 lua_setfield(L, -2, "gravity");
143 lua_pushboolean(L, player->physics_override_sneak);
144 lua_setfield(L, -2, "sneak");
146 lua_pushboolean(L, player->physics_override_sneak_glitch);
147 lua_setfield(L, -2, "sneak_glitch");
152 int LuaLocalPlayer::l_get_override_pos(lua_State *L)
154 LocalPlayer *player = getobject(L, 1);
156 push_v3f(L, player->overridePosition);
160 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
162 LocalPlayer *player = getobject(L, 1);
164 push_v3f(L, player->last_position / BS);
168 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
170 LocalPlayer *player = getobject(L, 1);
172 push_v3f(L, player->last_speed);
176 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
178 LocalPlayer *player = getobject(L, 1);
180 lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
184 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
186 LocalPlayer *player = getobject(L, 1);
188 lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
192 int LuaLocalPlayer::l_get_key_pressed(lua_State *L)
194 LocalPlayer *player = getobject(L, 1);
196 lua_pushinteger(L, player->last_keyPressed);
200 int LuaLocalPlayer::l_get_breath(lua_State *L)
202 LocalPlayer *player = getobject(L, 1);
204 lua_pushinteger(L, player->getBreath());
208 int LuaLocalPlayer::l_get_pos(lua_State *L)
210 LocalPlayer *player = getobject(L, 1);
212 push_v3f(L, player->getPosition() / BS);
216 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
218 LocalPlayer *player = getobject(L, 1);
221 lua_pushnumber(L, player->movement_acceleration_default);
222 lua_setfield(L, -2, "default");
224 lua_pushnumber(L, player->movement_acceleration_air);
225 lua_setfield(L, -2, "air");
227 lua_pushnumber(L, player->movement_acceleration_fast);
228 lua_setfield(L, -2, "fast");
233 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
235 LocalPlayer *player = getobject(L, 1);
238 lua_pushnumber(L, player->movement_speed_walk);
239 lua_setfield(L, -2, "walk");
241 lua_pushnumber(L, player->movement_speed_crouch);
242 lua_setfield(L, -2, "crouch");
244 lua_pushnumber(L, player->movement_speed_fast);
245 lua_setfield(L, -2, "fast");
247 lua_pushnumber(L, player->movement_speed_climb);
248 lua_setfield(L, -2, "climb");
250 lua_pushnumber(L, player->movement_speed_jump);
251 lua_setfield(L, -2, "jump");
256 int LuaLocalPlayer::l_get_movement(lua_State *L)
258 LocalPlayer *player = getobject(L, 1);
262 lua_pushnumber(L, player->movement_liquid_fluidity);
263 lua_setfield(L, -2, "liquid_fluidity");
265 lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
266 lua_setfield(L, -2, "liquid_fluidity_smooth");
268 lua_pushnumber(L, player->movement_liquid_sink);
269 lua_setfield(L, -2, "liquid_sink");
271 lua_pushnumber(L, player->movement_gravity);
272 lua_setfield(L, -2, "gravity");
278 // hud_add(self, form)
279 int LuaLocalPlayer::l_hud_add(lua_State *L)
281 LocalPlayer *player = getobject(L, 1);
283 HudElement *elem = new HudElement;
284 read_hud_element(L, elem);
286 u32 id = player->addHud(elem);
291 lua_pushnumber(L, id);
295 // hud_remove(self, id)
296 int LuaLocalPlayer::l_hud_remove(lua_State *L)
298 LocalPlayer *player = getobject(L, 1);
299 u32 id = luaL_checkinteger(L, 2);
300 HudElement *element = player->removeHud(id);
302 lua_pushboolean(L, false);
304 lua_pushboolean(L, true);
309 // hud_change(self, id, stat, data)
310 int LuaLocalPlayer::l_hud_change(lua_State *L)
312 LocalPlayer *player = getobject(L, 1);
314 u32 id = luaL_checkinteger(L, 2);
316 HudElement *element = player->getHud(id);
321 read_hud_change(L, element, &unused);
323 lua_pushboolean(L, true);
328 int LuaLocalPlayer::l_hud_get(lua_State *L)
330 LocalPlayer *player = getobject(L, 1);
332 u32 id = luaL_checkinteger(L, -1);
334 HudElement *e = player->getHud(id);
340 push_hud_element(L, e);
344 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
346 luaL_checktype(L, narg, LUA_TUSERDATA);
348 void *ud = luaL_checkudata(L, narg, className);
350 luaL_typerror(L, narg, className);
352 return *(LuaLocalPlayer **)ud;
355 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
357 return ref->m_localplayer;
360 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
362 LuaLocalPlayer *ref = checkobject(L, narg);
364 LocalPlayer *player = getobject(ref);
369 int LuaLocalPlayer::gc_object(lua_State *L)
371 LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
376 void LuaLocalPlayer::Register(lua_State *L)
379 int methodtable = lua_gettop(L);
380 luaL_newmetatable(L, className);
381 int metatable = lua_gettop(L);
383 lua_pushliteral(L, "__metatable");
384 lua_pushvalue(L, methodtable);
385 lua_settable(L, metatable); // hide metatable from lua getmetatable()
387 lua_pushliteral(L, "__index");
388 lua_pushvalue(L, methodtable);
389 lua_settable(L, metatable);
391 lua_pushliteral(L, "__gc");
392 lua_pushcfunction(L, gc_object);
393 lua_settable(L, metatable);
395 lua_pop(L, 1); // Drop metatable
397 luaL_openlib(L, 0, methods, 0); // fill methodtable
398 lua_pop(L, 1); // Drop methodtable
401 const char LuaLocalPlayer::className[] = "LocalPlayer";
402 const luaL_Reg LuaLocalPlayer::methods[] = {
403 luamethod(LuaLocalPlayer, get_velocity),
404 luamethod(LuaLocalPlayer, get_hp),
405 luamethod(LuaLocalPlayer, get_name),
406 luamethod(LuaLocalPlayer, is_attached),
407 luamethod(LuaLocalPlayer, is_touching_ground),
408 luamethod(LuaLocalPlayer, is_in_liquid),
409 luamethod(LuaLocalPlayer, is_in_liquid_stable),
410 luamethod(LuaLocalPlayer, get_liquid_viscosity),
411 luamethod(LuaLocalPlayer, is_climbing),
412 luamethod(LuaLocalPlayer, swimming_vertical),
413 luamethod(LuaLocalPlayer, get_physics_override),
414 luamethod(LuaLocalPlayer, get_override_pos),
415 luamethod(LuaLocalPlayer, get_last_pos),
416 luamethod(LuaLocalPlayer, get_last_velocity),
417 luamethod(LuaLocalPlayer, get_last_look_horizontal),
418 luamethod(LuaLocalPlayer, get_last_look_vertical),
419 luamethod(LuaLocalPlayer, get_key_pressed),
420 luamethod(LuaLocalPlayer, get_breath),
421 luamethod(LuaLocalPlayer, get_pos),
422 luamethod(LuaLocalPlayer, get_movement_acceleration),
423 luamethod(LuaLocalPlayer, get_movement_speed),
424 luamethod(LuaLocalPlayer, get_movement),
425 luamethod(LuaLocalPlayer, hud_add),
426 luamethod(LuaLocalPlayer, hud_remove),
427 luamethod(LuaLocalPlayer, hud_change),
428 luamethod(LuaLocalPlayer, hud_get),