Skip to content

Commit f9fdb48

Browse files
SmallJokerparamat
authored andcommittedMay 3, 2017
Sneak: Improve and fix various things
Remove useless `got_teleported`. Fix jitter when walking against the sneak limits. Fix damage evading on sneak ladders.
1 parent bd921a7 commit f9fdb48

File tree

6 files changed

+36
-32
lines changed

6 files changed

+36
-32
lines changed
 

Diff for: ‎doc/client_lua_api.md

-2
Original file line numberDiff line numberDiff line change
@@ -810,8 +810,6 @@ Methods:
810810
* returns player HP
811811
* `get_name()`
812812
* returns player name
813-
* `got_teleported()`
814-
* returns true if player was teleported
815813
* `is_attached()`
816814
* returns true if player is attached
817815
* `is_touching_ground()`

Diff for: ‎src/localplayer.cpp

+31-17
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ LocalPlayer::LocalPlayer(Client *client, const char *name):
3535
Player(name, client->idef()),
3636
parent(0),
3737
hp(PLAYER_MAX_HP),
38-
got_teleported(false),
3938
isAttached(false),
4039
touching_ground(false),
4140
in_liquid(false),
@@ -305,29 +304,43 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
305304
if (control.sneak && m_sneak_node_exists &&
306305
!(fly_allowed && g_settings->getBool("free_move")) &&
307306
!in_liquid && !is_climbing &&
308-
physics_override_sneak && !got_teleported) {
309-
v3f sn_f = intToFloat(m_sneak_node, BS);
310-
const v3f bmin = m_sneak_node_bb_top.MinEdge;
311-
const v3f bmax = m_sneak_node_bb_top.MaxEdge;
307+
physics_override_sneak) {
308+
const v3f sn_f = intToFloat(m_sneak_node, BS);
309+
const v3f bmin = sn_f + m_sneak_node_bb_top.MinEdge;
310+
const v3f bmax = sn_f + m_sneak_node_bb_top.MaxEdge;
311+
const v3f old_pos = position;
312+
const v3f old_speed = m_speed;
312313

313314
position.X = rangelim(position.X,
314-
sn_f.X+bmin.X - sneak_max.X, sn_f.X+bmax.X + sneak_max.X);
315+
bmin.X - sneak_max.X, bmax.X + sneak_max.X);
315316
position.Z = rangelim(position.Z,
316-
sn_f.Z+bmin.Z - sneak_max.Z, sn_f.Z+bmax.Z + sneak_max.Z);
317+
bmin.Z - sneak_max.Z, bmax.Z + sneak_max.Z);
318+
319+
if (position.X != old_pos.X)
320+
m_speed.X = 0;
321+
if (position.Z != old_pos.Z)
322+
m_speed.Z = 0;
317323

318324
// Because we keep the player collision box on the node, limiting
319325
// position.Y is not necessary but useful to prevent players from
320326
// being inside a node if sneaking on e.g. the lower part of a stair
321327
if (!m_sneak_ladder_detected) {
322-
position.Y = MYMAX(position.Y, sn_f.Y+bmax.Y);
328+
position.Y = MYMAX(position.Y, bmax.Y);
323329
} else {
324330
// legacy behaviour that sometimes causes some weird slow sinking
325331
m_speed.Y = MYMAX(m_speed.Y, 0);
326332
}
327-
}
328333

329-
if (got_teleported)
330-
got_teleported = false;
334+
if (collision_info != NULL &&
335+
m_speed.Y - old_speed.Y > BS) {
336+
// Collide with sneak node, report fall damage
337+
CollisionInfo sn_info;
338+
sn_info.node_p = m_sneak_node;
339+
sn_info.old_speed = old_speed;
340+
sn_info.new_speed = m_speed;
341+
collision_info->push_back(sn_info);
342+
}
343+
}
331344

332345
// TODO: this shouldn't be hardcoded but transmitted from server
333346
float player_stepheight = touching_ground ? (BS*0.6) : (BS*0.2);
@@ -449,9 +462,11 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
449462
m_ledge_detected = detectLedge(map, nodemgr, floatToInt(position, BS));
450463

451464
/*
452-
Set new position
465+
Set new position but keep sneak node set
453466
*/
467+
bool sneak_node_exists = m_sneak_node_exists;
454468
setPosition(position);
469+
m_sneak_node_exists = sneak_node_exists;
455470

456471
/*
457472
Report collisions
@@ -917,7 +932,7 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
917932
*/
918933
if (control.sneak && m_sneak_node_exists &&
919934
!(fly_allowed && g_settings->getBool("free_move")) && !in_liquid &&
920-
physics_override_sneak && !got_teleported) {
935+
physics_override_sneak) {
921936
f32 maxd = 0.5 * BS + sneak_max;
922937
v3f lwn_f = intToFloat(m_sneak_node, BS);
923938
position.X = rangelim(position.X, lwn_f.X - maxd, lwn_f.X + maxd);
@@ -938,9 +953,6 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
938953
}
939954
}
940955

941-
if (got_teleported)
942-
got_teleported = false;
943-
944956
// this shouldn't be hardcoded but transmitted from server
945957
float player_stepheight = touching_ground ? (BS * 0.6) : (BS * 0.2);
946958

@@ -1055,9 +1067,11 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
10551067
}
10561068

10571069
/*
1058-
Set new position
1070+
Set new position but keep sneak node set
10591071
*/
1072+
bool sneak_node_exists = m_sneak_node_exists;
10601073
setPosition(position);
1074+
m_sneak_node_exists = sneak_node_exists;
10611075

10621076
/*
10631077
Report collisions

Diff for: ‎src/localplayer.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class LocalPlayer : public Player
4747
ClientActiveObject *parent;
4848

4949
u16 hp;
50-
bool got_teleported;
5150
bool isAttached;
5251
bool touching_ground;
5352
// This oscillates so that the player jumps a bit above the surface
@@ -126,7 +125,11 @@ class LocalPlayer : public Player
126125

127126
f32 getPitch() const { return m_pitch; }
128127

129-
void setPosition(const v3f &position) { m_position = position; }
128+
inline void setPosition(const v3f &position)
129+
{
130+
m_position = position;
131+
m_sneak_node_exists = false;
132+
}
130133

131134
v3f getPosition() const { return m_position; }
132135
v3f getEyePosition() const { return m_position + getEyeOffset(); }

Diff for: ‎src/network/clientpackethandler.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ void Client::handleCommand_MovePlayer(NetworkPacket* pkt)
561561

562562
*pkt >> pos >> pitch >> yaw;
563563

564-
player->got_teleported = true;
565564
player->setPosition(pos);
566565

567566
infostream << "Client got TOCLIENT_MOVE_PLAYER"

Diff for: ‎src/script/lua_api/l_localplayer.cpp

-9
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ int LuaLocalPlayer::l_get_name(lua_State *L)
6868
return 1;
6969
}
7070

71-
int LuaLocalPlayer::l_is_teleported(lua_State *L)
72-
{
73-
LocalPlayer *player = getobject(L, 1);
74-
75-
lua_pushboolean(L, player->got_teleported);
76-
return 1;
77-
}
78-
7971
int LuaLocalPlayer::l_is_attached(lua_State *L)
8072
{
8173
LocalPlayer *player = getobject(L, 1);
@@ -386,7 +378,6 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
386378
luamethod(LuaLocalPlayer, get_velocity),
387379
luamethod(LuaLocalPlayer, get_hp),
388380
luamethod(LuaLocalPlayer, get_name),
389-
luamethod(LuaLocalPlayer, is_teleported),
390381
luamethod(LuaLocalPlayer, is_attached),
391382
luamethod(LuaLocalPlayer, is_touching_ground),
392383
luamethod(LuaLocalPlayer, is_in_liquid),

Diff for: ‎src/script/lua_api/l_localplayer.h

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class LuaLocalPlayer : public ModApiBase
3939

4040
static int l_get_name(lua_State *L);
4141

42-
static int l_is_teleported(lua_State *L);
4342
static int l_is_attached(lua_State *L);
4443
static int l_is_touching_ground(lua_State *L);
4544
static int l_is_in_liquid(lua_State *L);

0 commit comments

Comments
 (0)
Please sign in to comment.