Add physics overrides for walk speed and Fast Mode (#14475)

Co-authored-by: Wuzzy <Wuzzy@disroot.org>
This commit is contained in:
grorp 2024-04-30 13:50:01 +02:00 committed by GitHub
parent c044a3c1ca
commit 2bdd0a6bdb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 94 additions and 33 deletions

@ -702,8 +702,18 @@ Methods:
```lua ```lua
{ {
speed = float, speed = float,
speed_climb = float,
speed_crouch = float,
speed_fast = float,
speed_walk = float,
acceleration_default = float,
acceleration_air = float,
acceleration_fast = float,
jump = float, jump = float,
gravity = float, gravity = float,
liquid_fluidity = float,
liquid_fluidity_smooth = float,
liquid_sink = float,
sneak = boolean, sneak = boolean,
sneak_glitch = boolean, sneak_glitch = boolean,
new_move = boolean, new_move = boolean,
@ -719,7 +729,8 @@ Methods:
* `get_breath()` * `get_breath()`
* returns the player's breath * returns the player's breath
* `get_movement_acceleration()` * `get_movement_acceleration()`
* returns acceleration of the player in different environments: * returns acceleration of the player in different environments
(note: does not take physics overrides into account):
```lua ```lua
{ {
@ -730,7 +741,8 @@ Methods:
``` ```
* `get_movement_speed()` * `get_movement_speed()`
* returns player's speed in different environments: * returns player's speed in different environments
(note: does not take physics overrides into account):
```lua ```lua
{ {
@ -743,7 +755,8 @@ Methods:
``` ```
* `get_movement()` * `get_movement()`
* returns player's movement in different environments: * returns player's movement in different environments
(note: does not take physics overrides into account):
```lua ```lua
{ {

@ -8089,13 +8089,18 @@ child will follow movement and rotation of that bone.
* `set_physics_override(override_table)` * `set_physics_override(override_table)`
* Overrides the physics attributes of the player * Overrides the physics attributes of the player
* `override_table` is a table with the following fields: * `override_table` is a table with the following fields:
* `speed`: multiplier to default movement speed and acceleration values (default: `1`) * `speed`: multiplier to *all* movement speed (`speed_*`) and
* `jump`: multiplier to default jump value (default: `1`) acceleration (`acceleration_*`) values (default: `1`)
* `gravity`: multiplier to default gravity value (default: `1`) * `speed_walk`: multiplier to default walk speed value (default: `1`)
* Note: The actual walk speed is the product of `speed` and `speed_walk`
* `speed_climb`: multiplier to default climb speed value (default: `1`) * `speed_climb`: multiplier to default climb speed value (default: `1`)
* Note: The actual climb speed is the product of `speed` and `speed_climb` * Note: The actual climb speed is the product of `speed` and `speed_climb`
* `speed_crouch`: multiplier to default sneak speed value (default: `1`) * `speed_crouch`: multiplier to default sneak speed value (default: `1`)
* Note: The actual sneak speed is the product of `speed` and `speed_crouch` * Note: The actual sneak speed is the product of `speed` and `speed_crouch`
* `speed_fast`: multiplier to default speed value in Fast Mode (default: `1`)
* Note: The actual fast speed is the product of `speed` and `speed_fast`
* `jump`: multiplier to default jump value (default: `1`)
* `gravity`: multiplier to default gravity value (default: `1`)
* `liquid_fluidity`: multiplier to liquid movement resistance value * `liquid_fluidity`: multiplier to liquid movement resistance value
(for nodes with `liquid_move_physics`); the higher this value, the lower the (for nodes with `liquid_move_physics`); the higher this value, the lower the
resistance to movement. At `math.huge`, the resistance is zero and you can resistance to movement. At `math.huge`, the resistance is zero and you can
@ -8113,6 +8118,8 @@ child will follow movement and rotation of that bone.
* `acceleration_air`: multiplier to acceleration * `acceleration_air`: multiplier to acceleration
when jumping or falling (default: `1`) when jumping or falling (default: `1`)
* Note: The actual acceleration is the product of `speed` and `acceleration_air` * Note: The actual acceleration is the product of `speed` and `acceleration_air`
* `acceleration_fast`: multiplier to acceleration in Fast Mode (default: `1`)
* Note: The actual acceleration is the product of `speed` and `acceleration_fast`
* `sneak`: whether player can sneak (default: `true`) * `sneak`: whether player can sneak (default: `true`)
* `sneak_glitch`: whether player can use the new move code replications * `sneak_glitch`: whether player can use the new move code replications
of the old sneak side-effects: sneak ladders and 2 node sneak jump of the old sneak side-effects: sneak ladders and 2 node sneak jump

@ -1804,6 +1804,7 @@ void GenericCAO::processMessage(const std::string &data)
bool sneak_glitch = !readU8(is); bool sneak_glitch = !readU8(is);
bool new_move = !readU8(is); bool new_move = !readU8(is);
// new overrides since 5.8.0
float override_speed_climb = readF32(is); float override_speed_climb = readF32(is);
float override_speed_crouch = readF32(is); float override_speed_crouch = readF32(is);
float override_liquid_fluidity = readF32(is); float override_liquid_fluidity = readF32(is);
@ -1811,7 +1812,6 @@ void GenericCAO::processMessage(const std::string &data)
float override_liquid_sink = readF32(is); float override_liquid_sink = readF32(is);
float override_acceleration_default = readF32(is); float override_acceleration_default = readF32(is);
float override_acceleration_air = readF32(is); float override_acceleration_air = readF32(is);
// fallback for new overrides (since 5.8.0)
if (is.eof()) { if (is.eof()) {
override_speed_climb = 1.0f; override_speed_climb = 1.0f;
override_speed_crouch = 1.0f; override_speed_crouch = 1.0f;
@ -1822,6 +1822,16 @@ void GenericCAO::processMessage(const std::string &data)
override_acceleration_air = 1.0f; override_acceleration_air = 1.0f;
} }
// new overrides since 5.9.0
float override_speed_fast = readF32(is);
float override_acceleration_fast = readF32(is);
float override_speed_walk = readF32(is);
if (is.eof()) {
override_speed_fast = 1.0f;
override_acceleration_fast = 1.0f;
override_speed_walk = 1.0f;
}
if (m_is_local_player) { if (m_is_local_player) {
auto &phys = m_env->getLocalPlayer()->physics_override; auto &phys = m_env->getLocalPlayer()->physics_override;
phys.speed = override_speed; phys.speed = override_speed;
@ -1837,6 +1847,9 @@ void GenericCAO::processMessage(const std::string &data)
phys.liquid_sink = override_liquid_sink; phys.liquid_sink = override_liquid_sink;
phys.acceleration_default = override_acceleration_default; phys.acceleration_default = override_acceleration_default;
phys.acceleration_air = override_acceleration_air; phys.acceleration_air = override_acceleration_air;
phys.speed_fast = override_speed_fast;
phys.acceleration_fast = override_acceleration_fast;
phys.speed_walk = override_speed_walk;
} }
} else if (cmd == AO_CMD_SET_ANIMATION) { } else if (cmd == AO_CMD_SET_ANIMATION) {
// TODO: change frames send as v2s32 value // TODO: change frames send as v2s32 value

@ -586,6 +586,9 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
// Whether superspeed mode is used or not // Whether superspeed mode is used or not
bool superspeed = false; bool superspeed = false;
const f32 speed_walk = movement_speed_walk * physics_override.speed_walk;
const f32 speed_fast = movement_speed_fast * physics_override.speed_fast;
if (always_fly_fast && free_move && fast_move) if (always_fly_fast && free_move && fast_move)
superspeed = true; superspeed = true;
@ -600,11 +603,11 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
if (free_move) { if (free_move) {
// In free movement mode, aux1 descends // In free movement mode, aux1 descends
if (fast_move) if (fast_move)
speedV.Y = -movement_speed_fast; speedV.Y = -speed_fast;
else else
speedV.Y = -movement_speed_walk; speedV.Y = -speed_walk;
} else if ((in_liquid || in_liquid_stable) && !m_disable_descend) { } else if ((in_liquid || in_liquid_stable) && !m_disable_descend) {
speedV.Y = -movement_speed_walk; speedV.Y = -speed_walk;
swimming_vertical = true; swimming_vertical = true;
} else if (is_climbing && !m_disable_descend) { } else if (is_climbing && !m_disable_descend) {
speedV.Y = -movement_speed_climb * physics_override.speed_climb; speedV.Y = -movement_speed_climb * physics_override.speed_climb;
@ -632,18 +635,18 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
if (free_move) { if (free_move) {
// In free movement mode, sneak descends // In free movement mode, sneak descends
if (fast_move && (control.aux1 || always_fly_fast)) if (fast_move && (control.aux1 || always_fly_fast))
speedV.Y = -movement_speed_fast; speedV.Y = -speed_fast;
else else
speedV.Y = -movement_speed_walk; speedV.Y = -speed_walk;
} else if ((in_liquid || in_liquid_stable) && !m_disable_descend) { } else if ((in_liquid || in_liquid_stable) && !m_disable_descend) {
if (fast_climb) if (fast_climb)
speedV.Y = -movement_speed_fast; speedV.Y = -speed_fast;
else else
speedV.Y = -movement_speed_walk; speedV.Y = -speed_walk;
swimming_vertical = true; swimming_vertical = true;
} else if (is_climbing && !m_disable_descend) { } else if (is_climbing && !m_disable_descend) {
if (fast_climb) if (fast_climb)
speedV.Y = -movement_speed_fast; speedV.Y = -speed_fast;
else else
speedV.Y = -movement_speed_climb * physics_override.speed_climb; speedV.Y = -movement_speed_climb * physics_override.speed_climb;
} }
@ -666,14 +669,14 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
// Don't fly up if sneak key is pressed // Don't fly up if sneak key is pressed
if (player_settings.aux1_descends || always_fly_fast) { if (player_settings.aux1_descends || always_fly_fast) {
if (fast_move) if (fast_move)
speedV.Y = movement_speed_fast; speedV.Y = speed_fast;
else else
speedV.Y = movement_speed_walk; speedV.Y = speed_walk;
} else { } else {
if (fast_move && control.aux1) if (fast_move && control.aux1)
speedV.Y = movement_speed_fast; speedV.Y = speed_fast;
else else
speedV.Y = movement_speed_walk; speedV.Y = speed_walk;
} }
} }
} else if (m_can_jump) { } else if (m_can_jump) {
@ -690,13 +693,13 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
} }
} else if (in_liquid && !m_disable_jump && !control.sneak) { } else if (in_liquid && !m_disable_jump && !control.sneak) {
if (fast_climb) if (fast_climb)
speedV.Y = movement_speed_fast; speedV.Y = speed_fast;
else else
speedV.Y = movement_speed_walk; speedV.Y = speed_walk;
swimming_vertical = true; swimming_vertical = true;
} else if (is_climbing && !m_disable_jump && !control.sneak) { } else if (is_climbing && !m_disable_jump && !control.sneak) {
if (fast_climb) if (fast_climb)
speedV.Y = movement_speed_fast; speedV.Y = speed_fast;
else else
speedV.Y = movement_speed_climb * physics_override.speed_climb; speedV.Y = movement_speed_climb * physics_override.speed_climb;
} }
@ -705,11 +708,11 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
// The speed of the player (Y is ignored) // The speed of the player (Y is ignored)
if (superspeed || (is_climbing && fast_climb) || if (superspeed || (is_climbing && fast_climb) ||
((in_liquid || in_liquid_stable) && fast_climb)) ((in_liquid || in_liquid_stable) && fast_climb))
speedH = speedH.normalize() * movement_speed_fast; speedH = speedH.normalize() * speed_fast;
else if (control.sneak && !free_move && !in_liquid && !in_liquid_stable) else if (control.sneak && !free_move && !in_liquid && !in_liquid_stable)
speedH = speedH.normalize() * movement_speed_crouch * physics_override.speed_crouch; speedH = speedH.normalize() * movement_speed_crouch * physics_override.speed_crouch;
else else
speedH = speedH.normalize() * movement_speed_walk; speedH = speedH.normalize() * speed_walk;
speedH *= control.movement_speed; /* Apply analog input */ speedH *= control.movement_speed; /* Apply analog input */
@ -720,13 +723,13 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
(!free_move && m_can_jump && control.jump)) { (!free_move && m_can_jump && control.jump)) {
// Jumping and falling // Jumping and falling
if (superspeed || (fast_move && control.aux1)) if (superspeed || (fast_move && control.aux1))
incH = movement_acceleration_fast * BS * dtime; incH = movement_acceleration_fast * physics_override.acceleration_fast * BS * dtime;
else else
incH = movement_acceleration_air * physics_override.acceleration_air * BS * dtime; incH = movement_acceleration_air * physics_override.acceleration_air * BS * dtime;
incV = 0.0f; // No vertical acceleration in air incV = 0.0f; // No vertical acceleration in air
} else if (superspeed || (is_climbing && fast_climb) || } else if (superspeed || (is_climbing && fast_climb) ||
((in_liquid || in_liquid_stable) && fast_climb)) { ((in_liquid || in_liquid_stable) && fast_climb)) {
incH = incV = movement_acceleration_fast * BS * dtime; incH = incV = movement_acceleration_fast * physics_override.acceleration_fast * BS * dtime;
} else { } else {
incH = incV = movement_acceleration_default * physics_override.acceleration_default * BS * dtime; incH = incV = movement_acceleration_default * physics_override.acceleration_default * BS * dtime;
} }

@ -126,6 +126,9 @@ struct PlayerPhysicsOverride
float liquid_sink = 1.f; float liquid_sink = 1.f;
float acceleration_default = 1.f; float acceleration_default = 1.f;
float acceleration_air = 1.f; float acceleration_air = 1.f;
float speed_fast = 1.f;
float acceleration_fast = 1.f;
float speed_walk = 1.f;
private: private:
auto tie() const { auto tie() const {
@ -133,7 +136,7 @@ private:
return std::tie( return std::tie(
speed, jump, gravity, sneak, sneak_glitch, new_move, speed_climb, speed_crouch, speed, jump, gravity, sneak, sneak_glitch, new_move, speed_climb, speed_crouch,
liquid_fluidity, liquid_fluidity_smooth, liquid_sink, acceleration_default, liquid_fluidity, liquid_fluidity_smooth, liquid_sink, acceleration_default,
acceleration_air acceleration_air, speed_fast, acceleration_fast, speed_walk
); );
} }

@ -198,6 +198,15 @@ int LuaLocalPlayer::l_get_physics_override(lua_State *L)
lua_pushnumber(L, phys.acceleration_air); lua_pushnumber(L, phys.acceleration_air);
lua_setfield(L, -2, "acceleration_air"); lua_setfield(L, -2, "acceleration_air");
lua_pushnumber(L, phys.speed_fast);
lua_setfield(L, -2, "speed_fast");
lua_pushnumber(L, phys.acceleration_fast);
lua_setfield(L, -2, "acceleration_fast");
lua_pushnumber(L, phys.speed_walk);
lua_setfield(L, -2, "speed_walk");
return 1; return 1;
} }

@ -1610,6 +1610,9 @@ int ObjectRef::l_set_physics_override(lua_State *L)
getfloatfield(L, 2, "liquid_sink", phys.liquid_sink); getfloatfield(L, 2, "liquid_sink", phys.liquid_sink);
getfloatfield(L, 2, "acceleration_default", phys.acceleration_default); getfloatfield(L, 2, "acceleration_default", phys.acceleration_default);
getfloatfield(L, 2, "acceleration_air", phys.acceleration_air); getfloatfield(L, 2, "acceleration_air", phys.acceleration_air);
getfloatfield(L, 2, "speed_fast", phys.speed_fast);
getfloatfield(L, 2, "acceleration_fast", phys.acceleration_fast);
getfloatfield(L, 2, "speed_walk", phys.speed_walk);
if (phys != old) if (phys != old)
playersao->m_physics_override_sent = false; playersao->m_physics_override_sent = false;
@ -1653,6 +1656,12 @@ int ObjectRef::l_get_physics_override(lua_State *L)
lua_setfield(L, -2, "acceleration_default"); lua_setfield(L, -2, "acceleration_default");
lua_pushnumber(L, phys.acceleration_air); lua_pushnumber(L, phys.acceleration_air);
lua_setfield(L, -2, "acceleration_air"); lua_setfield(L, -2, "acceleration_air");
lua_pushnumber(L, phys.speed_fast);
lua_setfield(L, -2, "speed_fast");
lua_pushnumber(L, phys.acceleration_fast);
lua_setfield(L, -2, "acceleration_fast");
lua_pushnumber(L, phys.speed_walk);
lua_setfield(L, -2, "speed_walk");
return 1; return 1;
} }

@ -336,6 +336,9 @@ std::string PlayerSAO::generateUpdatePhysicsOverrideCommand() const
writeF32(os, phys.liquid_sink); writeF32(os, phys.liquid_sink);
writeF32(os, phys.acceleration_default); writeF32(os, phys.acceleration_default);
writeF32(os, phys.acceleration_air); writeF32(os, phys.acceleration_air);
writeF32(os, phys.speed_fast);
writeF32(os, phys.acceleration_fast);
writeF32(os, phys.speed_walk);
return os.str(); return os.str();
} }
@ -652,10 +655,11 @@ bool PlayerSAO::checkMovementCheat()
float player_max_walk = 0; // horizontal movement float player_max_walk = 0; // horizontal movement
float player_max_jump = 0; // vertical upwards movement float player_max_jump = 0; // vertical upwards movement
float speed_walk = m_player->movement_speed_walk; float speed_walk = m_player->movement_speed_walk * m_player->physics_override.speed_walk;
float speed_fast = m_player->movement_speed_fast; float speed_fast = m_player->movement_speed_fast * m_player->physics_override.speed_fast;
float speed_crouch = m_player->movement_speed_crouch * m_player->physics_override.speed_crouch; float speed_crouch = m_player->movement_speed_crouch * m_player->physics_override.speed_crouch;
float speed_climb = m_player->movement_speed_climb * m_player->physics_override.speed_climb; float speed_climb = m_player->movement_speed_climb * m_player->physics_override.speed_climb;
speed_walk *= m_player->physics_override.speed; speed_walk *= m_player->physics_override.speed;
speed_fast *= m_player->physics_override.speed; speed_fast *= m_player->physics_override.speed;
speed_crouch *= m_player->physics_override.speed; speed_crouch *= m_player->physics_override.speed;