@@ -154,16 +154,34 @@ func (e *Entity) applyBaseline(baseline map[int]PropertyValue) {
154154}
155155
156156const (
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.
166171func (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()
185212func (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()
206237func (e * Entity ) BindPosition (pos * r3.Vector ) {
0 commit comments