Skip to content

Commit 5cd809a

Browse files
committed
Fix Entity.BindPosition() performance issue (#48)
1 parent 4b006c8 commit 5cd809a

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

sendtables/entity.go

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Entity struct {
1717

1818
onCreateFinished []func()
1919
onDestroy []func()
20+
position func() r3.Vector
2021
}
2122

2223
// ServerClass returns the entity's server-class.
@@ -167,42 +168,54 @@ const (
167168
serverClassPlayer = "CCSPlayer"
168169
)
169170

170-
// Position returns the entity's position in world coordinates.
171-
func (e *Entity) Position() r3.Vector {
171+
// Sets up the Entity.Position() function
172+
// Necessary because FindProperty() is fairly slow
173+
// This way we only need to find the necessary properties once
174+
func (e *Entity) initialize() {
172175
// Player positions are calculated differently
173176
if e.isPlayer() {
174-
return e.positionPlayer()
177+
xyProp := e.FindProperty(propVecOriginPlayerXY)
178+
zProp := e.FindProperty(propVecOriginPlayerZ)
179+
180+
e.position = func() r3.Vector {
181+
xy := xyProp.value.VectorVal
182+
z := float64(zProp.value.FloatVal)
183+
return r3.Vector{
184+
X: xy.X,
185+
Y: xy.Y,
186+
Z: z,
187+
}
188+
}
189+
} else {
190+
cellBitsProp := e.FindProperty(propCellBits)
191+
cellXProp := e.FindProperty(propCellX)
192+
cellYProp := e.FindProperty(propCellY)
193+
cellZProp := e.FindProperty(propCellZ)
194+
offsetProp := e.FindProperty(propVecOrigin)
195+
196+
e.position = func() r3.Vector {
197+
cellWidth := 1 << uint(cellBitsProp.value.IntVal)
198+
cellX := cellXProp.value.IntVal
199+
cellY := cellYProp.value.IntVal
200+
cellZ := cellZProp.value.IntVal
201+
offset := offsetProp.value.VectorVal
202+
203+
return r3.Vector{
204+
X: coordFromCell(cellX, cellWidth, offset.X),
205+
Y: coordFromCell(cellY, cellWidth, offset.Y),
206+
Z: coordFromCell(cellZ, cellWidth, offset.Z),
207+
}
208+
}
175209
}
176-
177-
return e.positionDefault()
178210
}
179211

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

184-
func (e *Entity) positionDefault() r3.Vector {
185-
cellWidth := 1 << uint(e.FindProperty(propCellBits).value.IntVal)
186-
cellX := e.FindProperty(propCellX).value.IntVal
187-
cellY := e.FindProperty(propCellY).value.IntVal
188-
cellZ := e.FindProperty(propCellZ).value.IntVal
189-
offset := e.FindProperty(propVecOrigin).value.VectorVal
190-
191-
return r3.Vector{
192-
X: coordFromCell(cellX, cellWidth, offset.X),
193-
Y: coordFromCell(cellY, cellWidth, offset.Y),
194-
Z: coordFromCell(cellZ, cellWidth, offset.Z),
195-
}
196-
}
197-
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-
}
216+
// Position returns the entity's position in world coordinates.
217+
func (e *Entity) Position() r3.Vector {
218+
return e.position()
206219
}
207220

208221
// OnPositionUpdate registers a handler for the entity's position update.

sendtables/sendtables.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ func (sc *ServerClass) newEntity(r *bit.BitReader, entityID int) *Entity {
101101

102102
entity := &Entity{serverClass: sc, id: entityID, props: props}
103103

104+
entity.initialize()
105+
104106
if sc.preprocessedBaseline != nil {
105107
entity.applyBaseline(sc.preprocessedBaseline)
106108
} else {

0 commit comments

Comments
 (0)