Skip to content

Commit 4b006c8

Browse files
authored
Entity.Position() for CCSPlayer entities (#44)
Player positions are calculated differently from other entities. Fixes #38 fixes #38
1 parent 95349a9 commit 4b006c8

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

datatables.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,7 @@ func (p *Parser) bindNewPlayer(playerEntity *st.Entity) {
205205
})
206206

207207
// Position
208-
playerEntity.FindProperty("cslocaldata.m_vecOrigin").OnUpdate(func(val st.PropertyValue) {
209-
pl.Position.X = val.VectorVal.X
210-
pl.Position.Y = val.VectorVal.Y
211-
})
212-
playerEntity.BindProperty("cslocaldata.m_vecOrigin[2]", &pl.Position.Z, st.ValTypeFloat64)
208+
playerEntity.BindPosition(&pl.Position)
213209

214210
// General info
215211
playerEntity.FindProperty("m_iTeamNum").OnUpdate(func(val st.PropertyValue) {

sendtables/entity.go

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,34 @@ func (e *Entity) applyBaseline(baseline map[int]PropertyValue) {
154154
}
155155

156156
const (
157-
maxCoordInt = 16384
158-
propCellBits = "m_cellbits"
159-
propCellX = "m_cellX"
160-
propCellY = "m_cellY"
161-
propCellZ = "m_cellZ"
162-
propVecOrigin = "m_vecOrigin"
157+
maxCoordInt = 16384
158+
159+
propCellBits = "m_cellbits"
160+
propCellX = "m_cellX"
161+
propCellY = "m_cellY"
162+
propCellZ = "m_cellZ"
163+
propVecOrigin = "m_vecOrigin"
164+
propVecOriginPlayerXY = "cslocaldata.m_vecOrigin"
165+
propVecOriginPlayerZ = "cslocaldata.m_vecOrigin[2]"
166+
167+
serverClassPlayer = "CCSPlayer"
163168
)
164169

165170
// Position returns the entity's position in world coordinates.
166171
func (e *Entity) Position() r3.Vector {
172+
// Player positions are calculated differently
173+
if e.isPlayer() {
174+
return e.positionPlayer()
175+
}
176+
177+
return e.positionDefault()
178+
}
179+
180+
func (e *Entity) isPlayer() bool {
181+
return e.serverClass.name == serverClassPlayer
182+
}
183+
184+
func (e *Entity) positionDefault() r3.Vector {
167185
cellWidth := 1 << uint(e.FindProperty(propCellBits).value.IntVal)
168186
cellX := e.FindProperty(propCellX).value.IntVal
169187
cellY := e.FindProperty(propCellY).value.IntVal
@@ -177,9 +195,18 @@ func (e *Entity) Position() r3.Vector {
177195
}
178196
}
179197

198+
func (e *Entity) positionPlayer() r3.Vector {
199+
xy := e.FindProperty(propVecOriginPlayerXY).value.VectorVal
200+
z := float64(e.FindProperty(propVecOriginPlayerZ).value.FloatVal)
201+
return r3.Vector{
202+
X: xy.X,
203+
Y: xy.Y,
204+
Z: z,
205+
}
206+
}
207+
180208
// OnPositionUpdate registers a handler for the entity's position update.
181209
// The handler is called with the new position every time a position-relevant property is updated.
182-
// This does NOT work for players as their position is calculated differently.
183210
//
184211
// See also Position()
185212
func (e *Entity) OnPositionUpdate(h func(pos r3.Vector)) {
@@ -192,15 +219,19 @@ func (e *Entity) OnPositionUpdate(h func(pos r3.Vector)) {
192219
}
193220
}
194221

195-
e.FindProperty(propCellX).OnUpdate(firePosUpdate)
196-
e.FindProperty(propCellY).OnUpdate(firePosUpdate)
197-
e.FindProperty(propCellZ).OnUpdate(firePosUpdate)
198-
e.FindProperty(propVecOrigin).OnUpdate(firePosUpdate)
222+
if e.isPlayer() {
223+
e.FindProperty(propVecOriginPlayerXY).OnUpdate(firePosUpdate)
224+
e.FindProperty(propVecOriginPlayerZ).OnUpdate(firePosUpdate)
225+
} else {
226+
e.FindProperty(propCellX).OnUpdate(firePosUpdate)
227+
e.FindProperty(propCellY).OnUpdate(firePosUpdate)
228+
e.FindProperty(propCellZ).OnUpdate(firePosUpdate)
229+
e.FindProperty(propVecOrigin).OnUpdate(firePosUpdate)
230+
}
199231
}
200232

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

0 commit comments

Comments
 (0)