Skip to content

Commit 0456e59

Browse files
committed
Entity.Position() for CCSPlayer entities
Player positions are calculated differently from other entities. fixes #38
1 parent 3e6ec8f commit 0456e59

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

sendtables/entity.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,40 @@ func (e *Entity) ApplyBaseline(baseline map[int]PropValue) {
141141
}
142142
}
143143

144-
const maxCoordInt = 16384
144+
const (
145+
maxCoordInt = 16384
146+
147+
propCellBits = "m_cellbits"
148+
propCellX = "m_cellX"
149+
propCellY = "m_cellY"
150+
propCellZ = "m_cellZ"
151+
propVecOrigin = "m_vecOrigin"
152+
propVecOriginPlayerXY = "cslocaldata.m_vecOrigin"
153+
propVecOriginPlayerZ = "cslocaldata.m_vecOrigin[2]"
154+
155+
serverClassPlayer = "CCSPlayer"
156+
)
145157

146158
// Position returns the entity's position in world coordinates.
147159
func (e *Entity) Position() r3.Vector {
148-
cellWidth := 1 << uint(e.FindProperty("m_cellbits").value.IntVal)
149-
cellX := e.FindProperty("m_cellX").value.IntVal
150-
cellY := e.FindProperty("m_cellY").value.IntVal
151-
cellZ := e.FindProperty("m_cellZ").value.IntVal
152-
offset := e.FindProperty("m_vecOrigin").value.VectorVal
160+
// Player positions are calculated differently
161+
if e.isPlayer() {
162+
return e.positionPlayer()
163+
}
164+
165+
return e.positionDefault()
166+
}
167+
168+
func (e *Entity) isPlayer() bool {
169+
return e.ServerClass.Name == serverClassPlayer
170+
}
171+
172+
func (e *Entity) positionDefault() r3.Vector {
173+
cellWidth := 1 << uint(e.FindProperty(propCellBits).value.IntVal)
174+
cellX := e.FindProperty(propCellX).value.IntVal
175+
cellY := e.FindProperty(propCellY).value.IntVal
176+
cellZ := e.FindProperty(propCellZ).value.IntVal
177+
offset := e.FindProperty(propVecOrigin).value.VectorVal
153178

154179
return r3.Vector{
155180
X: coordFromCell(cellX, cellWidth, offset.X),
@@ -158,6 +183,16 @@ func (e *Entity) Position() r3.Vector {
158183
}
159184
}
160185

186+
func (e *Entity) positionPlayer() r3.Vector {
187+
xy := e.FindProperty(propVecOriginPlayerXY).value.VectorVal
188+
z := float64(e.FindProperty(propVecOriginPlayerZ).value.FloatVal)
189+
return r3.Vector{
190+
X: xy.X,
191+
Y: xy.Y,
192+
Z: z,
193+
}
194+
}
195+
161196
// Returns a coordinate from a cell + offset
162197
func coordFromCell(cell, cellWidth int, offset float64) float64 {
163198
return float64(cell*cellWidth-maxCoordInt) + offset

0 commit comments

Comments
 (0)