Skip to content

Commit

Permalink
Restore pass-through of direction keys (#11924)
Browse files Browse the repository at this point in the history
This moves relevant code into the PlayerControl class and gets rid of separate keyPressed variable.
  • Loading branch information
sfan5 committed Jan 9, 2022
1 parent 76dbd0d commit 5eb45e1
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 98 deletions.
16 changes: 9 additions & 7 deletions src/client/client.cpp
Expand Up @@ -931,7 +931,7 @@ void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *
v3f sf = myplayer->getSpeed() * 100;
s32 pitch = myplayer->getPitch() * 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
u8 fov = clientMap->getCameraFov() * 80;
u8 wanted_range = MYMIN(255,
Expand Down Expand Up @@ -1287,22 +1287,24 @@ void Client::sendPlayerPos()
if (!player)
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
// player is not dead and something changed

if (m_activeobjects_received && player->isDead())
return;

ClientMap &map = m_env.getClientMap();
u8 camera_fov = map.getCameraFov();
u8 wanted_range = map.getControl().wanted_range;

u32 keyPressed = player->control.getKeysPressed();

if (
player->last_position == player->getPosition() &&
player->last_speed == player->getSpeed() &&
player->last_pitch == player->getPitch() &&
player->last_yaw == player->getYaw() &&
player->last_keyPressed == player->keyPressed &&
player->last_keyPressed == keyPressed &&
player->last_camera_fov == camera_fov &&
player->last_wanted_range == wanted_range)
return;
Expand All @@ -1311,7 +1313,7 @@ void Client::sendPlayerPos()
player->last_speed = player->getSpeed();
player->last_pitch = player->getPitch();
player->last_yaw = player->getYaw();
player->last_keyPressed = player->keyPressed;
player->last_keyPressed = keyPressed;
player->last_camera_fov = camera_fov;
player->last_wanted_range = wanted_range;

Expand Down
4 changes: 2 additions & 2 deletions src/client/clientlauncher.cpp
Expand Up @@ -70,10 +70,10 @@ static void dump_start_data(const GameStartData &data)

ClientLauncher::~ClientLauncher()
{
delete receiver;

delete input;

delete receiver;

delete g_fontengine;
delete g_gamecallback;

Expand Down
36 changes: 4 additions & 32 deletions src/client/game.cpp
Expand Up @@ -2481,6 +2481,10 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
//TimeTaker tt("update player control", NULL, PRECISION_NANO);

PlayerControl control(
isKeyDown(KeyType::FORWARD),
isKeyDown(KeyType::BACKWARD),
isKeyDown(KeyType::LEFT),
isKeyDown(KeyType::RIGHT),
isKeyDown(KeyType::JUMP) || player->getAutojump(),
isKeyDown(KeyType::AUX1),
isKeyDown(KeyType::SNEAK),
Expand Down Expand Up @@ -2511,39 +2515,7 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
}
#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);
player->keyPressed = keypress_bits;

//tt.stop();
}
Expand Down
50 changes: 36 additions & 14 deletions src/client/inputhandler.h
Expand Up @@ -152,8 +152,14 @@ class MyEventReceiver : public IEventReceiver
// in the subsequent iteration of Game::processPlayerInteraction
bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }

void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
void dontListenForKeys() { keysListenedFor.clear(); }
void listenForKey(const KeyPress &keyCode)
{
keysListenedFor.set(keyCode);
}
void dontListenForKeys()
{
keysListenedFor.clear();
}

s32 getMouseWheel()
{
Expand Down Expand Up @@ -189,15 +195,15 @@ class MyEventReceiver : public IEventReceiver
#endif
}

s32 mouse_wheel = 0;

JoystickController *joystick = nullptr;

#ifdef HAVE_TOUCHSCREENGUI
TouchScreenGUI *m_touchscreengui;
#endif

private:
s32 mouse_wheel = 0;

// The current state of keys
KeyList keyIsDown;

Expand Down Expand Up @@ -272,6 +278,12 @@ class RealInputHandler : public InputHandler
{
m_receiver->joystick = &joystick;
}

virtual ~RealInputHandler()
{
m_receiver->joystick = nullptr;
}

virtual bool isKeyDown(GameKeyType k)
{
return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
Expand All @@ -288,6 +300,7 @@ class RealInputHandler : public InputHandler
{
return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
}

virtual float getMovementSpeed()
{
bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
Expand All @@ -307,6 +320,7 @@ class RealInputHandler : public InputHandler
}
return joystick.getMovementSpeed();
}

virtual float getMovementDirection()
{
float x = 0, z = 0;
Expand All @@ -326,10 +340,12 @@ class RealInputHandler : public InputHandler
else
return joystick.getMovementDirection();
}

virtual bool cancelPressed()
{
return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
}

virtual void clearWasKeyPressed()
{
m_receiver->clearWasKeyPressed();
Expand All @@ -338,34 +354,40 @@ class RealInputHandler : public InputHandler
{
m_receiver->clearWasKeyReleased();
}

virtual void listenForKey(const KeyPress &keyCode)
{
m_receiver->listenForKey(keyCode);
}
virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
virtual void dontListenForKeys()
{
m_receiver->dontListenForKeys();
}

virtual v2s32 getMousePos()
{
if (RenderingEngine::get_raw_device()->getCursorControl()) {
return RenderingEngine::get_raw_device()
->getCursorControl()
->getPosition();
auto control = RenderingEngine::get_raw_device()->getCursorControl();
if (control) {
return control->getPosition();
}

return m_mousepos;
}

virtual void setMousePos(s32 x, s32 y)
{
if (RenderingEngine::get_raw_device()->getCursorControl()) {
RenderingEngine::get_raw_device()
->getCursorControl()
->setPosition(x, y);
auto control = RenderingEngine::get_raw_device()->getCursorControl();
if (control) {
control->setPosition(x, y);
} else {
m_mousepos = v2s32(x, y);
}
}

virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
virtual s32 getMouseWheel()
{
return m_receiver->getMouseWheel();
}

void clear()
{
Expand Down
4 changes: 1 addition & 3 deletions src/client/localplayer.cpp
Expand Up @@ -1096,10 +1096,8 @@ void LocalPlayer::handleAutojump(f32 dtime, Environment *env,
if (m_autojump)
return;

bool control_forward = keyPressed & (1 << 0);

bool could_autojump =
m_can_jump && !control.jump && !control.sneak && control_forward;
m_can_jump && !control.jump && !control.sneak && control.isMoving();

if (!could_autojump)
return;
Expand Down
2 changes: 1 addition & 1 deletion src/client/localplayer.h
Expand Up @@ -86,7 +86,7 @@ class LocalPlayer : public Player
v3f last_speed;
float last_pitch = 0.0f;
float last_yaw = 0.0f;
unsigned int last_keyPressed = 0;
u32 last_keyPressed = 0;
u8 last_camera_fov = 0;
u8 last_wanted_range = 0;

Expand Down
9 changes: 1 addition & 8 deletions src/network/serverpackethandler.cpp
Expand Up @@ -482,7 +482,6 @@ void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
f32 yaw = (f32)f32yaw / 100.0f;
u32 keyPressed = 0;

// default behavior (in case an old client doesn't send these)
f32 fov = 0;
u8 wanted_range = 0;

Expand All @@ -508,13 +507,7 @@ void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
playersao->setFov(fov);
playersao->setWantedRange(wanted_range);

player->keyPressed = 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));
player->control.unpackKeysPressed(keyPressed);

if (playersao->checkMovementCheat()) {
// Call callbacks
Expand Down
59 changes: 59 additions & 0 deletions src/player.cpp
Expand Up @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "player.h"

#include <cmath>
#include "threading/mutex_auto_lock.h"
#include "util/numeric.h"
#include "hud.h"
Expand Down Expand Up @@ -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()
{
free_move = g_settings->getBool("free_move");
Expand Down

0 comments on commit 5eb45e1

Please sign in to comment.