Skip to content

Commit

Permalink
feat: add player velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
akiver committed Nov 12, 2023
1 parent 60ef60a commit 3c93edb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
20 changes: 16 additions & 4 deletions pkg/demoinfocs/common/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type Player struct {
IsDefusing bool
IsPlanting bool
IsReloading bool
IsUnknown bool // Used to identify unknown/broken players. see https://github.com/markus-wa/demoinfocs-golang/issues/162
IsUnknown bool // Used to identify unknown/broken players. see https://github.com/markus-wa/demoinfocs-golang/issues/162
LastPositions []r3.Vector // CS2 only, used to compute velocity as it's not networked in CS2 demos
}

func (p *Player) PlayerPawnEntity() st.Entity {
Expand Down Expand Up @@ -521,7 +522,17 @@ func (p *Player) PositionEyes() r3.Vector {
// Velocity returns the player's velocity.
func (p *Player) Velocity() r3.Vector {
if p.demoInfoProvider.IsSource2() {
panic("Velocity() is not supported for Source 2 demos")
if !p.IsAlive() || len(p.LastPositions) != 2 {
return r3.Vector{}
}

t := 64.0
diff := p.LastPositions[1].Sub(p.LastPositions[0])

return r3.Vector{
X: diff.X * t,
Y: diff.Y * t,
}
}

if p.Entity == nil {
Expand Down Expand Up @@ -801,8 +812,9 @@ type demoInfoProvider interface {
// Intended for internal use only.
func NewPlayer(demoInfoProvider demoInfoProvider) *Player {
return &Player{
Inventory: make(map[int]*Equipment),
demoInfoProvider: demoInfoProvider,
Inventory: make(map[int]*Equipment),
demoInfoProvider: demoInfoProvider,
LastAlivePosition: r3.Vector{},
}
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/demoinfocs/datatables.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,12 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
pawnEntity.OnPositionUpdate(func(pos r3.Vector) {
if pl.IsAlive() {
pl.LastAlivePosition = pos
pl.LastPositions = append(pl.LastPositions, pos)
if len(pl.LastPositions) > 2 {
pl.LastPositions = pl.LastPositions[1:]
}
} else {
pl.LastPositions = nil
}
})

Expand Down

0 comments on commit 3c93edb

Please sign in to comment.