The intended usecase is knockback, but there's potential for more.
* `get_player_name()`: returns `""` if is not a player
* `get_player_velocity()`: returns `nil` if is not a player, otherwise a
table {x, y, z} representing the player's instantaneous velocity in nodes/s
+* `add_player_velocity(vel)`
+ * Adds to player velocity, this happens client-side and only once.
+ * Does not apply during free_move.
+ * Note that since the player speed is normalized at each move step,
+ increasing e.g. Y velocity beyond what would usually be achieved
+ (see: physics overrides) will cause existing X/Z velocity to be reduced.
+ * Example: `add_player_velocity({x=0, y=6.5, z=0})` is equivalent to
+ pressing the jump key (assuming default settings)
* `get_look_dir()`: get camera direction as a unit vector
* `get_look_vertical()`: pitch in radians
* Angle ranges between -pi/2 and pi/2, which are straight up and down
void handleCommand_SrpBytesSandB(NetworkPacket *pkt);
void handleCommand_FormspecPrepend(NetworkPacket *pkt);
void handleCommand_CSMRestrictionFlags(NetworkPacket *pkt);
+ void handleCommand_PlayerSpeed(NetworkPacket *pkt);
void ProcessData(NetworkPacket *pkt);
// Copy parent position if local player is attached
if (isAttached) {
setPosition(overridePosition);
+ added_velocity = v3f(); // ignored
return;
}
if (noclip && free_move) {
position += m_speed * dtime;
setPosition(position);
+ added_velocity = v3f(); // ignored
return;
}
+ m_speed += added_velocity;
+ added_velocity = v3f();
+
/*
Collision detection
*/
if (isAttached) {
setPosition(overridePosition);
m_sneak_node_exists = false;
+ added_velocity = v3f();
return;
}
position += m_speed * dtime;
setPosition(position);
m_sneak_node_exists = false;
+ added_velocity = v3f();
return;
}
+ m_speed += added_velocity;
+ added_velocity = v3f();
+
/*
Collision detection
*/
bool getAutojump() const { return m_autojump; }
+ inline void addVelocity(const v3f &vel)
+ {
+ added_velocity += vel;
+ }
+
private:
void accelerate(const v3f &target_speed, const f32 max_increase_H,
const f32 max_increase_V, const bool use_pitch);
float m_zoom_fov = 0.0f;
bool m_autojump = false;
float m_autojump_time = 0.0f;
+ v3f added_velocity = v3f(0.0f, 0.0f, 0.0f); // cleared on each move()
GenericCAO *m_cao = nullptr;
Client *m_client;
m_time_from_last_teleport += dtime;
m_time_from_last_punch += dtime;
m_nocheat_dig_time += dtime;
+ m_max_speed_override_time = MYMAX(m_max_speed_override_time - dtime, 0.0f);
// Each frame, parent position is copied if the object is attached,
// otherwise it's calculated normally.
return gob_cmd_set_properties(m_prop);
}
+void PlayerSAO::setMaxSpeedOverride(const v3f &vel)
+{
+ if (m_max_speed_override_time == 0.0f)
+ m_max_speed_override = vel;
+ else
+ m_max_speed_override += vel;
+ if (m_player) {
+ float accel = MYMIN(m_player->movement_acceleration_default,
+ m_player->movement_acceleration_air);
+ m_max_speed_override_time = m_max_speed_override.getLength() / accel / BS;
+ }
+}
+
bool PlayerSAO::checkMovementCheat()
{
if (isAttached() || m_is_singleplayer ||
too, and much more lightweight.
*/
+ float override_max_H, override_max_V;
+ if (m_max_speed_override_time > 0.0f) {
+ override_max_H = MYMAX(fabs(m_max_speed_override.X), fabs(m_max_speed_override.Z));
+ override_max_V = fabs(m_max_speed_override.Y);
+ } else {
+ override_max_H = override_max_V = 0.0f;
+ }
+
float player_max_walk = 0; // horizontal movement
float player_max_jump = 0; // vertical upwards movement
else
player_max_walk = m_player->movement_speed_walk; // Normal speed
player_max_walk *= m_physics_override_speed;
+ player_max_walk = MYMAX(player_max_walk, override_max_H);
+
player_max_jump = m_player->movement_speed_jump * m_physics_override_jump;
// FIXME: Bouncy nodes cause practically unbound increase in Y speed,
// until this can be verified correctly, tolerate higher jumping speeds
player_max_jump *= 2.0;
+ player_max_jump = MYMAX(player_max_jump, override_max_V);
// Don't divide by zero!
if (player_max_walk < 0.0001f)
{
return m_dig_pool;
}
+ void setMaxSpeedOverride(const v3f &vel);
// Returns true if cheated
bool checkMovementCheat();
float m_time_from_last_punch = 0.0f;
v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
float m_nocheat_dig_time = 0.0f;
+ float m_max_speed_override_time = 0.0f;
+ v3f m_max_speed_override = v3f(0.0f, 0.0f, 0.0f);
// Timers
IntervalLimiter m_breathing_interval;
null_command_handler,
{ "TOCLIENT_TIME_OF_DAY", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_TimeOfDay }, // 0x29
{ "TOCLIENT_CSM_RESTRICTION_FLAGS", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_CSMRestrictionFlags }, // 0x2A
- null_command_handler,
+ { "TOCLIENT_PLAYER_SPEED", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_PlayerSpeed }, // 0x2B
null_command_handler,
null_command_handler,
null_command_handler,
loadMods();
}
+void Client::handleCommand_PlayerSpeed(NetworkPacket *pkt)
+{
+ v3f added_vel;
+
+ *pkt >> added_vel;
+
+ LocalPlayer *player = m_env.getLocalPlayer();
+ assert(player != NULL);
+ player->addVelocity(added_vel);
+}
+
/*
* Mod channels
*/
New network float format
ContentFeatures version 13
Add full Euler rotations instead of just yaw
+ Add TOCLIENT_PLAYER_SPEED
*/
#define LATEST_PROTOCOL_VERSION 37
u32 CSMRestrictionFlags byteflag
*/
+ TOCLIENT_PLAYER_SPEED = 0x2B,
+ /*
+ v3f added_vel
+ */
+
// (oops, there is some gap here)
TOCLIENT_CHAT_MESSAGE = 0x2F,
null_command_factory,
{ "TOCLIENT_TIME_OF_DAY", 0, true }, // 0x29
{ "TOCLIENT_CSM_RESTRICTION_FLAGS", 0, true }, // 0x2A
- null_command_factory,
+ { "TOCLIENT_PLAYER_SPEED", 0, true }, // 0x2B
null_command_factory,
null_command_factory,
null_command_factory,
return 1;
}
+// add_player_velocity(self, {x=num, y=num, z=num})
+int ObjectRef::l_add_player_velocity(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ObjectRef *ref = checkobject(L, 1);
+ v3f vel = checkFloatPos(L, 2);
+
+ RemotePlayer *player = getplayer(ref);
+ PlayerSAO *co = getplayersao(ref);
+ if (!player || !co)
+ return 0;
+
+ session_t peer_id = player->getPeerId();
+ if (peer_id == PEER_ID_INEXISTENT)
+ return 0;
+ // Do it
+ co->setMaxSpeedOverride(vel);
+ getServer(L)->SendPlayerSpeed(peer_id, vel);
+ return 0;
+}
+
// get_look_dir(self)
int ObjectRef::l_get_look_dir(lua_State *L)
{
luamethod(ObjectRef, is_player_connected),
luamethod(ObjectRef, get_player_name),
luamethod(ObjectRef, get_player_velocity),
+ luamethod(ObjectRef, add_player_velocity),
luamethod(ObjectRef, get_look_dir),
luamethod(ObjectRef, get_look_pitch),
luamethod(ObjectRef, get_look_yaw),
// get_player_velocity(self)
static int l_get_player_velocity(lua_State *L);
+ // add_player_velocity(self, {x=num, y=num, z=num})
+ static int l_add_player_velocity(lua_State *L);
+
// get_look_dir(self)
static int l_get_look_dir(lua_State *L);
Send(&pkt);
}
+void Server::SendPlayerSpeed(session_t peer_id, const v3f &added_vel)
+{
+ NetworkPacket pkt(TOCLIENT_PLAYER_SPEED, 0, peer_id);
+ pkt << added_vel;
+ Send(&pkt);
+}
+
s32 Server::playSound(const SimpleSoundSpec &spec,
const ServerSoundParams ¶ms)
{
void SendPlayerBreath(PlayerSAO *sao);
void SendInventory(PlayerSAO* playerSAO);
void SendMovePlayer(session_t peer_id);
+ void SendPlayerSpeed(session_t peer_id, const v3f &added_vel);
virtual bool registerModStorage(ModMetadata *storage);
virtual void unregisterModStorage(const std::string &name);