Restore pass-through of direction keys (#11924)

This moves relevant code into the PlayerControl class and gets rid of separate keyPressed variable.
master
sfan5 2022-01-09 18:46:36 +01:00 committed by GitHub
parent 76dbd0d2d0
commit 5eb45e1ea0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 165 additions and 98 deletions

View File

@ -931,7 +931,7 @@ void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *
v3f sf = myplayer->getSpeed() * 100; v3f sf = myplayer->getSpeed() * 100;
s32 pitch = myplayer->getPitch() * 100; s32 pitch = myplayer->getPitch() * 100;
s32 yaw = myplayer->getYaw() * 100; s32 yaw = myplayer->getYaw() * 100;
u32 keyPressed = myplayer->keyPressed; u32 keyPressed = myplayer->control.getKeysPressed();
// scaled by 80, so that pi can fit into a u8 // scaled by 80, so that pi can fit into a u8
u8 fov = clientMap->getCameraFov() * 80; u8 fov = clientMap->getCameraFov() * 80;
u8 wanted_range = MYMIN(255, u8 wanted_range = MYMIN(255,
@ -1287,22 +1287,24 @@ void Client::sendPlayerPos()
if (!player) if (!player)
return; return;
ClientMap &map = m_env.getClientMap();
u8 camera_fov = map.getCameraFov();
u8 wanted_range = map.getControl().wanted_range;
// Save bandwidth by only updating position when // Save bandwidth by only updating position when
// player is not dead and something changed // player is not dead and something changed
if (m_activeobjects_received && player->isDead()) if (m_activeobjects_received && player->isDead())
return; return;
ClientMap &map = m_env.getClientMap();
u8 camera_fov = map.getCameraFov();
u8 wanted_range = map.getControl().wanted_range;
u32 keyPressed = player->control.getKeysPressed();
if ( if (
player->last_position == player->getPosition() && player->last_position == player->getPosition() &&
player->last_speed == player->getSpeed() && player->last_speed == player->getSpeed() &&
player->last_pitch == player->getPitch() && player->last_pitch == player->getPitch() &&
player->last_yaw == player->getYaw() && player->last_yaw == player->getYaw() &&
player->last_keyPressed == player->keyPressed && player->last_keyPressed == keyPressed &&
player->last_camera_fov == camera_fov && player->last_camera_fov == camera_fov &&
player->last_wanted_range == wanted_range) player->last_wanted_range == wanted_range)
return; return;
@ -1311,7 +1313,7 @@ void Client::sendPlayerPos()
player->last_speed = player->getSpeed(); player->last_speed = player->getSpeed();
player->last_pitch = player->getPitch(); player->last_pitch = player->getPitch();
player->last_yaw = player->getYaw(); player->last_yaw = player->getYaw();
player->last_keyPressed = player->keyPressed; player->last_keyPressed = keyPressed;
player->last_camera_fov = camera_fov; player->last_camera_fov = camera_fov;
player->last_wanted_range = wanted_range; player->last_wanted_range = wanted_range;

View File

@ -70,10 +70,10 @@ static void dump_start_data(const GameStartData &data)
ClientLauncher::~ClientLauncher() ClientLauncher::~ClientLauncher()
{ {
delete receiver;
delete input; delete input;
delete receiver;
delete g_fontengine; delete g_fontengine;
delete g_gamecallback; delete g_gamecallback;

View File

@ -2481,6 +2481,10 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
//TimeTaker tt("update player control", NULL, PRECISION_NANO); //TimeTaker tt("update player control", NULL, PRECISION_NANO);
PlayerControl control( PlayerControl control(
isKeyDown(KeyType::FORWARD),
isKeyDown(KeyType::BACKWARD),
isKeyDown(KeyType::LEFT),
isKeyDown(KeyType::RIGHT),
isKeyDown(KeyType::JUMP) || player->getAutojump(), isKeyDown(KeyType::JUMP) || player->getAutojump(),
isKeyDown(KeyType::AUX1), isKeyDown(KeyType::AUX1),
isKeyDown(KeyType::SNEAK), isKeyDown(KeyType::SNEAK),
@ -2511,39 +2515,7 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
} }
#endif #endif
u32 keypress_bits = (
( (u32)(control.jump & 0x1) << 4) |
( (u32)(control.aux1 & 0x1) << 5) |
( (u32)(control.sneak & 0x1) << 6) |
( (u32)(control.dig & 0x1) << 7) |
( (u32)(control.place & 0x1) << 8) |
( (u32)(control.zoom & 0x1) << 9)
);
// Set direction keys to ensure mod compatibility
if (control.movement_speed > 0.001f) {
float absolute_direction;
// Check in original orientation (absolute value indicates forward / backward)
absolute_direction = abs(control.movement_direction);
if (absolute_direction < (3.0f / 8.0f * M_PI))
keypress_bits |= (u32)(0x1 << 0); // Forward
if (absolute_direction > (5.0f / 8.0f * M_PI))
keypress_bits |= (u32)(0x1 << 1); // Backward
// Rotate entire coordinate system by 90 degrees (absolute value indicates left / right)
absolute_direction = control.movement_direction + M_PI_2;
if (absolute_direction >= M_PI)
absolute_direction -= 2 * M_PI;
absolute_direction = abs(absolute_direction);
if (absolute_direction < (3.0f / 8.0f * M_PI))
keypress_bits |= (u32)(0x1 << 2); // Left
if (absolute_direction > (5.0f / 8.0f * M_PI))
keypress_bits |= (u32)(0x1 << 3); // Right
}
client->setPlayerControl(control); client->setPlayerControl(control);
player->keyPressed = keypress_bits;
//tt.stop(); //tt.stop();
} }

View File

@ -152,8 +152,14 @@ public:
// in the subsequent iteration of Game::processPlayerInteraction // in the subsequent iteration of Game::processPlayerInteraction
bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; } bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }
void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); } void listenForKey(const KeyPress &keyCode)
void dontListenForKeys() { keysListenedFor.clear(); } {
keysListenedFor.set(keyCode);
}
void dontListenForKeys()
{
keysListenedFor.clear();
}
s32 getMouseWheel() s32 getMouseWheel()
{ {
@ -189,8 +195,6 @@ public:
#endif #endif
} }
s32 mouse_wheel = 0;
JoystickController *joystick = nullptr; JoystickController *joystick = nullptr;
#ifdef HAVE_TOUCHSCREENGUI #ifdef HAVE_TOUCHSCREENGUI
@ -198,6 +202,8 @@ public:
#endif #endif
private: private:
s32 mouse_wheel = 0;
// The current state of keys // The current state of keys
KeyList keyIsDown; KeyList keyIsDown;
@ -272,6 +278,12 @@ public:
{ {
m_receiver->joystick = &joystick; m_receiver->joystick = &joystick;
} }
virtual ~RealInputHandler()
{
m_receiver->joystick = nullptr;
}
virtual bool isKeyDown(GameKeyType k) virtual bool isKeyDown(GameKeyType k)
{ {
return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k); return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
@ -288,6 +300,7 @@ public:
{ {
return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k); return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
} }
virtual float getMovementSpeed() virtual float getMovementSpeed()
{ {
bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]), bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
@ -307,6 +320,7 @@ public:
} }
return joystick.getMovementSpeed(); return joystick.getMovementSpeed();
} }
virtual float getMovementDirection() virtual float getMovementDirection()
{ {
float x = 0, z = 0; float x = 0, z = 0;
@ -326,10 +340,12 @@ public:
else else
return joystick.getMovementDirection(); return joystick.getMovementDirection();
} }
virtual bool cancelPressed() virtual bool cancelPressed()
{ {
return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey); return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
} }
virtual void clearWasKeyPressed() virtual void clearWasKeyPressed()
{ {
m_receiver->clearWasKeyPressed(); m_receiver->clearWasKeyPressed();
@ -338,17 +354,21 @@ public:
{ {
m_receiver->clearWasKeyReleased(); m_receiver->clearWasKeyReleased();
} }
virtual void listenForKey(const KeyPress &keyCode) virtual void listenForKey(const KeyPress &keyCode)
{ {
m_receiver->listenForKey(keyCode); m_receiver->listenForKey(keyCode);
} }
virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); } virtual void dontListenForKeys()
{
m_receiver->dontListenForKeys();
}
virtual v2s32 getMousePos() virtual v2s32 getMousePos()
{ {
if (RenderingEngine::get_raw_device()->getCursorControl()) { auto control = RenderingEngine::get_raw_device()->getCursorControl();
return RenderingEngine::get_raw_device() if (control) {
->getCursorControl() return control->getPosition();
->getPosition();
} }
return m_mousepos; return m_mousepos;
@ -356,16 +376,18 @@ public:
virtual void setMousePos(s32 x, s32 y) virtual void setMousePos(s32 x, s32 y)
{ {
if (RenderingEngine::get_raw_device()->getCursorControl()) { auto control = RenderingEngine::get_raw_device()->getCursorControl();
RenderingEngine::get_raw_device() if (control) {
->getCursorControl() control->setPosition(x, y);
->setPosition(x, y);
} else { } else {
m_mousepos = v2s32(x, y); m_mousepos = v2s32(x, y);
} }
} }
virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); } virtual s32 getMouseWheel()
{
return m_receiver->getMouseWheel();
}
void clear() void clear()
{ {

View File

@ -1096,10 +1096,8 @@ void LocalPlayer::handleAutojump(f32 dtime, Environment *env,
if (m_autojump) if (m_autojump)
return; return;
bool control_forward = keyPressed & (1 << 0);
bool could_autojump = bool could_autojump =
m_can_jump && !control.jump && !control.sneak && control_forward; m_can_jump && !control.jump && !control.sneak && control.isMoving();
if (!could_autojump) if (!could_autojump)
return; return;

View File

@ -86,7 +86,7 @@ public:
v3f last_speed; v3f last_speed;
float last_pitch = 0.0f; float last_pitch = 0.0f;
float last_yaw = 0.0f; float last_yaw = 0.0f;
unsigned int last_keyPressed = 0; u32 last_keyPressed = 0;
u8 last_camera_fov = 0; u8 last_camera_fov = 0;
u8 last_wanted_range = 0; u8 last_wanted_range = 0;

View File

@ -482,7 +482,6 @@ void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
f32 yaw = (f32)f32yaw / 100.0f; f32 yaw = (f32)f32yaw / 100.0f;
u32 keyPressed = 0; u32 keyPressed = 0;
// default behavior (in case an old client doesn't send these)
f32 fov = 0; f32 fov = 0;
u8 wanted_range = 0; u8 wanted_range = 0;
@ -508,13 +507,7 @@ void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
playersao->setFov(fov); playersao->setFov(fov);
playersao->setWantedRange(wanted_range); playersao->setWantedRange(wanted_range);
player->keyPressed = keyPressed; player->control.unpackKeysPressed(keyPressed);
player->control.jump = (keyPressed & (0x1 << 4));
player->control.aux1 = (keyPressed & (0x1 << 5));
player->control.sneak = (keyPressed & (0x1 << 6));
player->control.dig = (keyPressed & (0x1 << 7));
player->control.place = (keyPressed & (0x1 << 8));
player->control.zoom = (keyPressed & (0x1 << 9));
if (playersao->checkMovementCheat()) { if (playersao->checkMovementCheat()) {
// Call callbacks // Call callbacks

View File

@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "player.h" #include "player.h"
#include <cmath>
#include "threading/mutex_auto_lock.h" #include "threading/mutex_auto_lock.h"
#include "util/numeric.h" #include "util/numeric.h"
#include "hud.h" #include "hud.h"
@ -159,6 +160,64 @@ void Player::clearHud()
} }
} }
#ifndef SERVER
u32 PlayerControl::getKeysPressed() const
{
u32 keypress_bits =
( (u32)(jump & 1) << 4) |
( (u32)(aux1 & 1) << 5) |
( (u32)(sneak & 1) << 6) |
( (u32)(dig & 1) << 7) |
( (u32)(place & 1) << 8) |
( (u32)(zoom & 1) << 9)
;
// If any direction keys are pressed pass those through
if (direction_keys != 0)
{
keypress_bits |= direction_keys;
}
// Otherwise set direction keys based on joystick movement (for mod compatibility)
else if (isMoving())
{
float abs_d;
// (absolute value indicates forward / backward)
abs_d = abs(movement_direction);
if (abs_d < 3.0f / 8.0f * M_PI)
keypress_bits |= (u32)1; // Forward
if (abs_d > 5.0f / 8.0f * M_PI)
keypress_bits |= (u32)1 << 1; // Backward
// rotate entire coordinate system by 90 degree
abs_d = movement_direction + M_PI_2;
if (abs_d >= M_PI)
abs_d -= 2 * M_PI;
abs_d = abs(abs_d);
// (value now indicates left / right)
if (abs_d < 3.0f / 8.0f * M_PI)
keypress_bits |= (u32)1 << 2; // Left
if (abs_d > 5.0f / 8.0f * M_PI)
keypress_bits |= (u32)1 << 3; // Right
}
return keypress_bits;
}
#endif
void PlayerControl::unpackKeysPressed(u32 keypress_bits)
{
direction_keys = keypress_bits & 0xf;
jump = keypress_bits & (1 << 4);
aux1 = keypress_bits & (1 << 5);
sneak = keypress_bits & (1 << 6);
dig = keypress_bits & (1 << 7);
place = keypress_bits & (1 << 8);
zoom = keypress_bits & (1 << 9);
}
void PlayerSettings::readGlobalSettings() void PlayerSettings::readGlobalSettings()
{ {
free_move = g_settings->getBool("free_move"); free_move = g_settings->getBool("free_move");

View File

@ -49,18 +49,18 @@ struct PlayerControl
PlayerControl() = default; PlayerControl() = default;
PlayerControl( PlayerControl(
bool a_jump, bool a_up, bool a_down, bool a_left, bool a_right,
bool a_aux1, bool a_jump, bool a_aux1, bool a_sneak,
bool a_sneak,
bool a_zoom, bool a_zoom,
bool a_dig, bool a_dig, bool a_place,
bool a_place, float a_pitch, float a_yaw,
float a_pitch, float a_movement_speed, float a_movement_direction
float a_yaw,
float a_movement_speed,
float a_movement_direction
) )
{ {
// Encode direction keys into a single value so nobody uses it accidentally
// as movement_{speed,direction} is supposed to be the source of truth.
direction_keys = (a_up&1) | ((a_down&1) << 1) |
((a_left&1) << 2) | ((a_right&1) << 3);
jump = a_jump; jump = a_jump;
aux1 = a_aux1; aux1 = a_aux1;
sneak = a_sneak; sneak = a_sneak;
@ -72,15 +72,26 @@ struct PlayerControl
movement_speed = a_movement_speed; movement_speed = a_movement_speed;
movement_direction = a_movement_direction; movement_direction = a_movement_direction;
} }
#ifndef SERVER
// For client use
u32 getKeysPressed() const;
inline bool isMoving() const { return movement_speed > 0.001f; }
#endif
// For server use
void unpackKeysPressed(u32 keypress_bits);
u8 direction_keys = 0;
bool jump = false; bool jump = false;
bool aux1 = false; bool aux1 = false;
bool sneak = false; bool sneak = false;
bool zoom = false; bool zoom = false;
bool dig = false; bool dig = false;
bool place = false; bool place = false;
// Note: These four are NOT available on the server
float pitch = 0.0f; float pitch = 0.0f;
float yaw = 0.0f; float yaw = 0.0f;
// Note: These two are NOT available on the server
float movement_speed = 0.0f; float movement_speed = 0.0f;
float movement_direction = 0.0f; float movement_direction = 0.0f;
}; };
@ -189,8 +200,6 @@ public:
return m_fov_override_spec; return m_fov_override_spec;
} }
u32 keyPressed = 0;
HudElement* getHud(u32 id); HudElement* getHud(u32 id);
u32 addHud(HudElement* hud); u32 addHud(HudElement* hud);
HudElement* removeHud(u32 id); HudElement* removeHud(u32 id);

View File

@ -230,13 +230,15 @@ int LuaLocalPlayer::l_get_control(lua_State *L)
set("dig", c.dig); set("dig", c.dig);
set("place", c.place); set("place", c.place);
// Player movement in polar coordinates and non-binary speed // Player movement in polar coordinates and non-binary speed
set("movement_speed", c.movement_speed); lua_pushnumber(L, c.movement_speed);
set("movement_direction", c.movement_direction); lua_setfield(L, -2, "movement_speed");
lua_pushnumber(L, c.movement_direction);
lua_setfield(L, -2, "movement_direction");
// Provide direction keys to ensure compatibility // Provide direction keys to ensure compatibility
set("up", player->keyPressed & (1 << 0)); // Up, down, left, and right were removed in favor of set("up", c.direction_keys & (1 << 0));
set("down", player->keyPressed & (1 << 1)); // analog direction indicators and are therefore not set("down", c.direction_keys & (1 << 1));
set("left", player->keyPressed & (1 << 2)); // available as booleans anymore. The corresponding values set("left", c.direction_keys & (1 << 2));
set("right", player->keyPressed & (1 << 3)); // can still be read from the keyPressed bits though. set("right", c.direction_keys & (1 << 3));
return 1; return 1;
} }

View File

@ -1367,20 +1367,18 @@ int ObjectRef::l_get_player_control(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == nullptr) { if (player == nullptr)
lua_pushlstring(L, "", 0); return 0;
return 1;
}
const PlayerControl &control = player->getPlayerControl(); const PlayerControl &control = player->getPlayerControl();
lua_newtable(L); lua_newtable(L);
lua_pushboolean(L, player->keyPressed & (1 << 0)); lua_pushboolean(L, control.direction_keys & (1 << 0));
lua_setfield(L, -2, "up"); 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_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_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_setfield(L, -2, "right");
lua_pushboolean(L, control.jump); lua_pushboolean(L, control.jump);
lua_setfield(L, -2, "jump"); lua_setfield(L, -2, "jump");
@ -1408,12 +1406,24 @@ int ObjectRef::l_get_player_control_bits(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref); RemotePlayer *player = getplayer(ref);
if (player == nullptr) { if (player == nullptr)
lua_pushlstring(L, "", 0); return 0;
return 1;
}
lua_pushnumber(L, player->keyPressed); 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_pushinteger(L, keypress_bits);
return 1; return 1;
} }