Skip to content

Commit 0b5a3f0

Browse files
committed
common: try to improve accuracy of Player.IsDucking()
1 parent 268e36e commit 0b5a3f0

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

pkg/demoinfocs/common/player.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ func (p *Player) IsScoped() bool {
187187
}
188188

189189
// IsDucking returns true if the player is currently crouching.
190+
// See also: Flags().Ducking() & Flags().DuckingKeyPressed()
190191
func (p *Player) IsDucking() bool {
191-
return getBool(p.Entity, "localdata.m_Local.m_bDucking")
192+
return p.Flags().Ducking() && p.Flags().DuckingKeyPressed()
192193
}
193194

194195
// HasDefuseKit returns true if the player currently has a defuse kit in his inventory.
@@ -284,6 +285,51 @@ func (p *Player) Velocity() r3.Vector {
284285
}
285286
}
286287

288+
// see https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L146-L188
289+
const (
290+
flOnGround = 1 << iota
291+
flDucking
292+
flAnimDucking
293+
)
294+
295+
// PlayerFlags wraps m_fFlags and provides accessors for the various known flags a player may have set.
296+
type PlayerFlags uint32
297+
298+
func (pf PlayerFlags) Get(f PlayerFlags) bool {
299+
return pf&f != 0
300+
}
301+
302+
// OnGround returns true if the player is touching the ground.
303+
// See m_fFlags FL_ONGROUND https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L146-L188
304+
func (pf PlayerFlags) OnGround() bool {
305+
return pf.Get(flOnGround)
306+
}
307+
308+
// Ducking returns true if the player is/was fully crouched.
309+
// Fully ducked: Ducking() && DuckingKeyPressed()
310+
// Previously fully ducked, unducking in progress: Ducking() && !DuckingKeyPressed()
311+
// Fully unducked: !Ducking() && !DuckingKeyPressed()
312+
// Previously fully unducked, ducking in progress: !Ducking() && DuckingKeyPressed()
313+
// See m_fFlags FL_DUCKING https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L146-L188
314+
func (pf PlayerFlags) Ducking() bool {
315+
return pf.Get(flDucking)
316+
}
317+
318+
// DuckingKeyPressed returns true if the player is holding the crouch key pressed.
319+
// Fully ducked: Ducking() && DuckingKeyPressed()
320+
// Previously fully ducked, unducking in progress: Ducking() && !DuckingKeyPressed()
321+
// Fully unducked: !Ducking() && !DuckingKeyPressed()
322+
// Previously fully unducked, ducking in progress: !Ducking() && DuckingKeyPressed()
323+
// See m_fFlags FL_ANIMDUCKING https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L146-L188
324+
func (pf PlayerFlags) DuckingKeyPressed() bool {
325+
return pf.Get(flAnimDucking)
326+
}
327+
328+
// Flags returns flags currently set on m_fFlags.
329+
func (p *Player) Flags() PlayerFlags {
330+
return PlayerFlags(getInt(p.Entity, "m_fFlags"))
331+
}
332+
287333
/////////////////////////////
288334
// CCSPlayerResource stuff //
289335
/////////////////////////////

pkg/demoinfocs/common/player_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,33 @@ func TestPlayer_IsAirborne(t *testing.T) {
209209
}
210210

211211
func TestPlayer_IsDucking(t *testing.T) {
212-
pl := playerWithProperty("localdata.m_Local.m_bDucking", st.PropertyValue{IntVal: 0})
212+
pl := playerWithProperty("m_fFlags", st.PropertyValue{IntVal: 0})
213213

214214
assert.False(t, pl.IsDucking())
215215

216-
pl = playerWithProperty("localdata.m_Local.m_bDucking", st.PropertyValue{IntVal: 1})
216+
pl = playerWithProperty("m_fFlags", st.PropertyValue{IntVal: 1 << 1})
217+
218+
assert.False(t, pl.IsDucking())
219+
220+
pl = playerWithProperty("m_fFlags", st.PropertyValue{IntVal: 1 << 2})
221+
222+
assert.False(t, pl.IsDucking())
223+
224+
pl = playerWithProperty("m_fFlags", st.PropertyValue{IntVal: 1<<1 | 1<<2})
217225

218226
assert.True(t, pl.IsDucking())
219227
}
220228

229+
func TestPlayerFlags_OnGround(t *testing.T) {
230+
pl := playerWithProperty("m_fFlags", st.PropertyValue{IntVal: 0})
231+
232+
assert.False(t, pl.Flags().OnGround())
233+
234+
pl = playerWithProperty("m_fFlags", st.PropertyValue{IntVal: 1})
235+
236+
assert.True(t, pl.Flags().OnGround())
237+
}
238+
221239
func TestPlayer_HasDefuseKit(t *testing.T) {
222240
pl := playerWithProperty("m_bHasDefuser", st.PropertyValue{IntVal: 0})
223241

0 commit comments

Comments
 (0)