Skip to content

Commit 7238df4

Browse files
committedAug 15, 2015
Fix sneaking (fixes #665 and #3045)
1 parent c4b5561 commit 7238df4

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed
 

‎src/localplayer.cpp

+44-28
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name):
4848
light_color(255,255,255,255),
4949
m_sneak_node(32767,32767,32767),
5050
m_sneak_node_exists(false),
51+
m_need_to_get_new_sneak_node(true),
52+
m_sneak_node_bb_ymax(0),
5153
m_old_node_below(32767,32767,32767),
5254
m_old_node_below_type("air"),
53-
m_need_to_get_new_sneak_node(true),
5455
m_can_jump(false),
5556
m_cao(NULL)
5657
{
@@ -179,25 +180,26 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
179180
If sneaking, keep in range from the last walked node and don't
180181
fall off from it
181182
*/
182-
if(control.sneak && m_sneak_node_exists &&
183+
if (control.sneak && m_sneak_node_exists &&
183184
!(fly_allowed && g_settings->getBool("free_move")) && !in_liquid &&
184-
physics_override_sneak)
185-
{
186-
f32 maxd = 0.5*BS + sneak_max;
185+
physics_override_sneak) {
186+
f32 maxd = 0.5 * BS + sneak_max;
187187
v3f lwn_f = intToFloat(m_sneak_node, BS);
188188
position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd);
189189
position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd);
190190

191-
if(!is_climbing)
192-
{
193-
f32 min_y = lwn_f.Y + 0.5*BS;
194-
if(position.Y < min_y)
195-
{
196-
position.Y = min_y;
197-
198-
if(m_speed.Y < 0)
199-
m_speed.Y = 0;
200-
}
191+
if (!is_climbing) {
192+
// Move up if necessary
193+
f32 new_y = (lwn_f.Y - 0.5 * BS) + m_sneak_node_bb_ymax;
194+
if (position.Y < new_y)
195+
position.Y = new_y;
196+
/*
197+
Collision seems broken, since player is sinking when
198+
sneaking over the edges of current sneaking_node.
199+
TODO (when fixed): Set Y-speed only to 0 when position.Y < new_y.
200+
*/
201+
if (m_speed.Y < 0)
202+
m_speed.Y = 0;
201203
}
202204
}
203205

@@ -230,27 +232,28 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
230232
player is sneaking from, if any. If the node from under
231233
the player has been removed, the player falls.
232234
*/
233-
v3s16 current_node = floatToInt(position - v3f(0, 0.05 * BS, 0), BS);
234-
if(m_sneak_node_exists &&
235-
nodemgr->get(map->getNodeNoEx(m_old_node_below)).name == "air" &&
236-
m_old_node_below_type != "air")
237-
{
235+
f32 position_y_mod = 0.05 * BS;
236+
if (m_sneak_node_bb_ymax > 0)
237+
position_y_mod = m_sneak_node_bb_ymax - position_y_mod;
238+
v3s16 current_node = floatToInt(position - v3f(0, position_y_mod, 0), BS);
239+
if (m_sneak_node_exists &&
240+
nodemgr->get(map->getNodeNoEx(m_old_node_below)).name == "air" &&
241+
m_old_node_below_type != "air") {
238242
// Old node appears to have been removed; that is,
239243
// it wasn't air before but now it is
240244
m_need_to_get_new_sneak_node = false;
241245
m_sneak_node_exists = false;
242-
}
243-
else if(nodemgr->get(map->getNodeNoEx(current_node)).name != "air")
244-
{
246+
} else if (nodemgr->get(map->getNodeNoEx(current_node)).name != "air") {
245247
// We are on something, so make sure to recalculate the sneak
246248
// node.
247249
m_need_to_get_new_sneak_node = true;
248250
}
249-
if(m_need_to_get_new_sneak_node && physics_override_sneak)
250-
{
251-
v3s16 pos_i_bottom = floatToInt(position - v3f(0, 0.05 * BS ,0), BS);
251+
252+
if (m_need_to_get_new_sneak_node && physics_override_sneak) {
253+
m_sneak_node_bb_ymax = 0;
254+
v3s16 pos_i_bottom = floatToInt(position - v3f(0, position_y_mod, 0), BS);
252255
v2f player_p2df(position.X, position.Z);
253-
f32 min_distance_f = 100000.0*BS;
256+
f32 min_distance_f = 100000.0 * BS;
254257
// If already seeking from some node, compare to it.
255258
/*if(m_sneak_node_exists)
256259
{
@@ -298,11 +301,24 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
298301
new_sneak_node = p;
299302
}
300303

301-
bool sneak_node_found = (min_distance_f < 100000.0*BS*0.9);
304+
bool sneak_node_found = (min_distance_f < 100000.0 * BS * 0.9);
302305

303306
m_sneak_node = new_sneak_node;
304307
m_sneak_node_exists = sneak_node_found;
305308

309+
if (sneak_node_found) {
310+
f32 cb_max = 0;
311+
MapNode n = map->getNodeNoEx(m_sneak_node);
312+
std::vector<aabb3f> nodeboxes = n.getCollisionBoxes(nodemgr);
313+
for (std::vector<aabb3f>::iterator it = nodeboxes.begin();
314+
it != nodeboxes.end(); ++it) {
315+
aabb3f box = *it;
316+
if (box.MaxEdge.Y > cb_max)
317+
cb_max = box.MaxEdge.Y;
318+
}
319+
m_sneak_node_bb_ymax = cb_max;
320+
}
321+
306322
/*
307323
If sneaking, the player's collision box can be in air, so
308324
this has to be set explicitly

‎src/localplayer.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,15 @@ class LocalPlayer : public Player
8585
v3s16 m_sneak_node;
8686
// Whether the player is allowed to sneak
8787
bool m_sneak_node_exists;
88+
// Whether recalculation of the sneak node is needed
89+
bool m_need_to_get_new_sneak_node;
90+
// Stores the max player uplift by m_sneak_node and is updated
91+
// when m_need_to_get_new_sneak_node == true
92+
f32 m_sneak_node_bb_ymax;
8893
// Node below player, used to determine whether it has been removed,
8994
// and its old type
9095
v3s16 m_old_node_below;
9196
std::string m_old_node_below_type;
92-
// Whether recalculation of the sneak node is needed
93-
bool m_need_to_get_new_sneak_node;
9497
bool m_can_jump;
9598

9699
GenericCAO* m_cao;

0 commit comments

Comments
 (0)
Please sign in to comment.