]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_object.cpp
Restore pass-through of direction keys (#11924)
[dragonfireclient.git] / src / script / lua_api / l_object.cpp
index 072b13d806cb476d74f11d0e0847331c1ac0db9a..7d937b30609a65dfab4af62ba87e46c297c349bf 100644 (file)
@@ -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;
 }