Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add player velocity #469

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -569,6 +569,12 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
}
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