Skip to content

Commit 51ac188

Browse files
committed
feat: add player velocity
1 parent 0c77732 commit 51ac188

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

pkg/demoinfocs/common/player.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ type Player struct {
3232
IsDefusing bool
3333
IsPlanting bool
3434
IsReloading bool
35-
IsUnknown bool // Used to identify unknown/broken players. see https://github.com/markus-wa/demoinfocs-golang/issues/162
35+
IsUnknown bool // Used to identify unknown/broken players. see https://github.com/markus-wa/demoinfocs-golang/issues/162
36+
LastPositions []r3.Vector // CS2 only, used to compute velocity as it's not networked in CS2 demos
3637
}
3738

3839
func (p *Player) PlayerPawnEntity() st.Entity {
@@ -521,7 +522,17 @@ func (p *Player) PositionEyes() r3.Vector {
521522
// Velocity returns the player's velocity.
522523
func (p *Player) Velocity() r3.Vector {
523524
if p.demoInfoProvider.IsSource2() {
524-
panic("Velocity() is not supported for Source 2 demos")
525+
if !p.IsAlive() || len(p.LastPositions) != 2 {
526+
return r3.Vector{}
527+
}
528+
529+
t := 64.0
530+
diff := p.LastPositions[1].Sub(p.LastPositions[0])
531+
532+
return r3.Vector{
533+
X: diff.X * t,
534+
Y: diff.Y * t,
535+
}
525536
}
526537

527538
if p.Entity == nil {
@@ -801,8 +812,9 @@ type demoInfoProvider interface {
801812
// Intended for internal use only.
802813
func NewPlayer(demoInfoProvider demoInfoProvider) *Player {
803814
return &Player{
804-
Inventory: make(map[int]*Equipment),
805-
demoInfoProvider: demoInfoProvider,
815+
Inventory: make(map[int]*Equipment),
816+
demoInfoProvider: demoInfoProvider,
817+
LastAlivePosition: r3.Vector{},
806818
}
807819
}
808820

pkg/demoinfocs/datatables.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,12 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
569569
}
570570
if pl.IsAlive() {
571571
pl.LastAlivePosition = pos
572+
pl.LastPositions = append(pl.LastPositions, pos)
573+
if len(pl.LastPositions) > 2 {
574+
pl.LastPositions = pl.LastPositions[1:]
575+
}
576+
} else {
577+
pl.LastPositions = nil
572578
}
573579
})
574580

0 commit comments

Comments
 (0)