Skip to content

Commit

Permalink
Entity.Position() for CCSPlayer entities (#44)
Browse files Browse the repository at this point in the history
Player positions are calculated differently from other entities.

Fixes #38

fixes #38
  • Loading branch information
markus-wa committed Sep 10, 2018
1 parent 95349a9 commit 4b006c8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
6 changes: 1 addition & 5 deletions datatables.go
Expand Up @@ -205,11 +205,7 @@ func (p *Parser) bindNewPlayer(playerEntity *st.Entity) {
})

// Position
playerEntity.FindProperty("cslocaldata.m_vecOrigin").OnUpdate(func(val st.PropertyValue) {
pl.Position.X = val.VectorVal.X
pl.Position.Y = val.VectorVal.Y
})
playerEntity.BindProperty("cslocaldata.m_vecOrigin[2]", &pl.Position.Z, st.ValTypeFloat64)
playerEntity.BindPosition(&pl.Position)

// General info
playerEntity.FindProperty("m_iTeamNum").OnUpdate(func(val st.PropertyValue) {
Expand Down
55 changes: 43 additions & 12 deletions sendtables/entity.go
Expand Up @@ -154,16 +154,34 @@ func (e *Entity) applyBaseline(baseline map[int]PropertyValue) {
}

const (
maxCoordInt = 16384
propCellBits = "m_cellbits"
propCellX = "m_cellX"
propCellY = "m_cellY"
propCellZ = "m_cellZ"
propVecOrigin = "m_vecOrigin"
maxCoordInt = 16384

propCellBits = "m_cellbits"
propCellX = "m_cellX"
propCellY = "m_cellY"
propCellZ = "m_cellZ"
propVecOrigin = "m_vecOrigin"
propVecOriginPlayerXY = "cslocaldata.m_vecOrigin"
propVecOriginPlayerZ = "cslocaldata.m_vecOrigin[2]"

serverClassPlayer = "CCSPlayer"
)

// Position returns the entity's position in world coordinates.
func (e *Entity) Position() r3.Vector {
// Player positions are calculated differently
if e.isPlayer() {
return e.positionPlayer()
}

return e.positionDefault()
}

func (e *Entity) isPlayer() bool {
return e.serverClass.name == serverClassPlayer
}

func (e *Entity) positionDefault() r3.Vector {
cellWidth := 1 << uint(e.FindProperty(propCellBits).value.IntVal)
cellX := e.FindProperty(propCellX).value.IntVal
cellY := e.FindProperty(propCellY).value.IntVal
Expand All @@ -177,9 +195,18 @@ func (e *Entity) Position() r3.Vector {
}
}

func (e *Entity) positionPlayer() r3.Vector {
xy := e.FindProperty(propVecOriginPlayerXY).value.VectorVal
z := float64(e.FindProperty(propVecOriginPlayerZ).value.FloatVal)
return r3.Vector{
X: xy.X,
Y: xy.Y,
Z: z,
}
}

// OnPositionUpdate registers a handler for the entity's position update.
// The handler is called with the new position every time a position-relevant property is updated.
// This does NOT work for players as their position is calculated differently.
//
// See also Position()
func (e *Entity) OnPositionUpdate(h func(pos r3.Vector)) {
Expand All @@ -192,15 +219,19 @@ func (e *Entity) OnPositionUpdate(h func(pos r3.Vector)) {
}
}

e.FindProperty(propCellX).OnUpdate(firePosUpdate)
e.FindProperty(propCellY).OnUpdate(firePosUpdate)
e.FindProperty(propCellZ).OnUpdate(firePosUpdate)
e.FindProperty(propVecOrigin).OnUpdate(firePosUpdate)
if e.isPlayer() {
e.FindProperty(propVecOriginPlayerXY).OnUpdate(firePosUpdate)
e.FindProperty(propVecOriginPlayerZ).OnUpdate(firePosUpdate)
} else {
e.FindProperty(propCellX).OnUpdate(firePosUpdate)
e.FindProperty(propCellY).OnUpdate(firePosUpdate)
e.FindProperty(propCellZ).OnUpdate(firePosUpdate)
e.FindProperty(propVecOrigin).OnUpdate(firePosUpdate)
}
}

// BindPosition binds the entity's position to a pointer variable.
// The pointer is updated every time a position-relevant property is updated.
// This does NOT work for players as their position is calculated differently.
//
// See also OnPositionUpdate()
func (e *Entity) BindPosition(pos *r3.Vector) {
Expand Down

0 comments on commit 4b006c8

Please sign in to comment.