From 5eb45e1ea03c6104f007efec6dd9c351f310193d Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 9 Jan 2022 18:46:36 +0100 Subject: Restore pass-through of direction keys (#11924) This moves relevant code into the PlayerControl class and gets rid of separate keyPressed variable. --- src/script/lua_api/l_localplayer.cpp | 14 ++++++++------ src/script/lua_api/l_object.cpp | 36 +++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 19 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index bdbe98cb0..2efb976c7 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -230,13 +230,15 @@ int LuaLocalPlayer::l_get_control(lua_State *L) set("dig", c.dig); set("place", c.place); // Player movement in polar coordinates and non-binary speed - set("movement_speed", c.movement_speed); - set("movement_direction", c.movement_direction); + lua_pushnumber(L, c.movement_speed); + lua_setfield(L, -2, "movement_speed"); + lua_pushnumber(L, c.movement_direction); + lua_setfield(L, -2, "movement_direction"); // Provide direction keys to ensure compatibility - set("up", player->keyPressed & (1 << 0)); // Up, down, left, and right were removed in favor of - set("down", player->keyPressed & (1 << 1)); // analog direction indicators and are therefore not - set("left", player->keyPressed & (1 << 2)); // available as booleans anymore. The corresponding values - set("right", player->keyPressed & (1 << 3)); // can still be read from the keyPressed bits though. + set("up", c.direction_keys & (1 << 0)); + set("down", c.direction_keys & (1 << 1)); + set("left", c.direction_keys & (1 << 2)); + set("right", c.direction_keys & (1 << 3)); return 1; } diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 072b13d80..7d937b306 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1367,20 +1367,18 @@ int ObjectRef::l_get_player_control(lua_State *L) NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); RemotePlayer *player = getplayer(ref); - if (player == nullptr) { - lua_pushlstring(L, "", 0); - return 1; - } + if (player == nullptr) + return 0; const PlayerControl &control = player->getPlayerControl(); lua_newtable(L); - lua_pushboolean(L, player->keyPressed & (1 << 0)); + lua_pushboolean(L, control.direction_keys & (1 << 0)); lua_setfield(L, -2, "up"); - lua_pushboolean(L, player->keyPressed & (1 << 1)); + lua_pushboolean(L, control.direction_keys & (1 << 1)); lua_setfield(L, -2, "down"); - lua_pushboolean(L, player->keyPressed & (1 << 2)); + lua_pushboolean(L, control.direction_keys & (1 << 2)); lua_setfield(L, -2, "left"); - lua_pushboolean(L, player->keyPressed & (1 << 3)); + lua_pushboolean(L, control.direction_keys & (1 << 3)); lua_setfield(L, -2, "right"); lua_pushboolean(L, control.jump); lua_setfield(L, -2, "jump"); @@ -1408,12 +1406,24 @@ int ObjectRef::l_get_player_control_bits(lua_State *L) NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); RemotePlayer *player = getplayer(ref); - if (player == nullptr) { - lua_pushlstring(L, "", 0); - return 1; - } + if (player == nullptr) + return 0; + + const auto &c = player->getPlayerControl(); + + // This is very close to PlayerControl::getKeysPressed() but duplicated + // here so the encoding in the API is not inadvertedly changed. + u32 keypress_bits = + c.direction_keys | + ( (u32)(c.jump & 1) << 4) | + ( (u32)(c.aux1 & 1) << 5) | + ( (u32)(c.sneak & 1) << 6) | + ( (u32)(c.dig & 1) << 7) | + ( (u32)(c.place & 1) << 8) | + ( (u32)(c.zoom & 1) << 9) + ; - lua_pushnumber(L, player->keyPressed); + lua_pushinteger(L, keypress_bits); return 1; } -- cgit v1.2.3