Skip to content

Commit 0c6c50c

Browse files
committed
feat: check for m_lifeState value in player.IsAlive
1 parent 8acc3fc commit 0c6c50c

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

pkg/demoinfocs/common/common_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ func TestConvertSteamID64To32(t *testing.T) {
166166
assert.Equal(t, uint32(52686539), id)
167167
}
168168

169+
type fakeProp struct {
170+
propName string
171+
value st.PropertyValue
172+
}
173+
169174
type demoInfoProviderMock struct {
170175
tickRate float64
171176
ingameTick int
@@ -221,6 +226,21 @@ func entityWithProperty(propName string, value st.PropertyValue) *stfake.Entity
221226
return entity
222227
}
223228

229+
func entityWithProperties(properties []fakeProp) *stfake.Entity {
230+
entity := entityWithID(1)
231+
232+
for _, prop := range properties {
233+
property := new(stfake.Property)
234+
property.On("Value").Return(prop.value)
235+
236+
entity.On("Property", prop.propName).Return(property)
237+
entity.On("PropertyValue", prop.propName).Return(prop.value, true)
238+
entity.On("PropertyValueMust", prop.propName).Return(prop.value)
239+
}
240+
241+
return entity
242+
}
243+
224244
func entityWithoutProperty(propName string) *stfake.Entity {
225245
entity := entityWithID(1)
226246

pkg/demoinfocs/common/player.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func (p *Player) SteamID32() uint32 {
5151
return ConvertSteamID64To32(p.SteamID64)
5252
}
5353

54-
// IsAlive returns true if the Hp of the player are > 0.
54+
// IsAlive returns true if the player is alive.
5555
func (p *Player) IsAlive() bool {
56-
return p.Health() > 0
56+
return p.Health() > 0 || getInt(p.Entity, "m_lifeState") == 0
5757
}
5858

5959
// IsBlinded returns true if the player is currently flashed.
@@ -298,7 +298,7 @@ func (p *Player) Position() r3.Vector {
298298

299299
// PositionEyes returns the player's position with the Z value at eye height.
300300
// This is what you get from cl_showpos 1.
301-
// See lso Position().
301+
// See also Position().
302302
func (p *Player) PositionEyes() r3.Vector {
303303
if p.Entity == nil {
304304
return r3.Vector{}

pkg/demoinfocs/common/player_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,28 @@ func TestPlayerWeapons(t *testing.T) {
4242
func TestPlayerAlive(t *testing.T) {
4343
pl := newPlayer(0)
4444

45-
pl.Entity = entityWithProperty("m_iHealth", st.PropertyValue{IntVal: 100})
45+
pl.Entity = entityWithProperties([]fakeProp{
46+
{propName: "m_iHealth", value: st.PropertyValue{IntVal: 100}},
47+
{propName: "m_lifeState", value: st.PropertyValue{IntVal: 0}},
48+
})
4649
assert.Equal(t, true, pl.IsAlive(), "Should be alive")
4750

48-
pl.Entity = entityWithProperty("m_iHealth", st.PropertyValue{IntVal: 1})
51+
pl.Entity = entityWithProperties([]fakeProp{
52+
{propName: "m_iHealth", value: st.PropertyValue{IntVal: 1}},
53+
{propName: "m_lifeState", value: st.PropertyValue{IntVal: 0}},
54+
})
4955
assert.Equal(t, true, pl.IsAlive(), "Should be alive")
5056

51-
pl.Entity = entityWithProperty("m_iHealth", st.PropertyValue{IntVal: 0})
57+
pl.Entity = entityWithProperties([]fakeProp{
58+
{propName: "m_iHealth", value: st.PropertyValue{IntVal: 0}},
59+
{propName: "m_lifeState", value: st.PropertyValue{IntVal: 2}},
60+
})
5261
assert.Equal(t, false, pl.IsAlive(), "Should be dead")
5362

54-
pl.Entity = entityWithProperty("m_iHealth", st.PropertyValue{IntVal: -10})
63+
pl.Entity = entityWithProperties([]fakeProp{
64+
{propName: "m_iHealth", value: st.PropertyValue{IntVal: -10}},
65+
{propName: "m_lifeState", value: st.PropertyValue{IntVal: 2}},
66+
})
5567
assert.Equal(t, false, pl.IsAlive(), "Should be dead")
5668
}
5769

0 commit comments

Comments
 (0)