Skip to content

Commit 7f64986

Browse files
committed
S2: fix pos + smaller fixes
1 parent 0b1b9fe commit 7f64986

File tree

7 files changed

+73
-80
lines changed

7 files changed

+73
-80
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ linters:
1313
disable-all: true
1414
enable:
1515
- bodyclose
16-
- depguard
1716
- dogsled
1817
- dupl
1918
- exportloopref

pkg/demoinfocs/common/common.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ func NewTeamState(team Team, membersCallback func(Team) []*Player) TeamState {
217217
// ConvertSteamIDTxtTo32 converts a Steam-ID in text format to a 32-bit variant.
218218
// See https://developer.valvesoftware.com/wiki/SteamID
219219
func ConvertSteamIDTxtTo32(steamID string) (uint32, error) {
220-
if strings.HasSuffix(steamID, "]") {
221-
steamID = strings.TrimSuffix(steamID, "]") // Source 2 has [U:1:397560266] instead of STEAM_0:1:198780133
222-
}
220+
steamID = strings.TrimSuffix(steamID, "]") // Source 2 has [U:1:397560266] instead of STEAM_0:1:198780133
223221

224222
arr := strings.Split(steamID, ":")
225223

pkg/demoinfocs/common/player.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ func (p *Player) SteamID32() uint32 {
6565

6666
// IsAlive returns true if the player is alive.
6767
func (p *Player) IsAlive() bool {
68-
return p.Health() > 0 || getInt(p.Entity, "m_lifeState") == 0
68+
s1LifeState := p.Entity.Property("m_lifeState")
69+
70+
return p.Health() > 0 || (s1LifeState != nil && s1LifeState.Value().Int() == 0)
6971
}
7072

7173
// IsBlinded returns true if the player is currently flashed.
@@ -257,6 +259,12 @@ func (p *Player) ControlledBot() *Player {
257259

258260
// Health returns the player's health points, normally 0-100.
259261
func (p *Player) Health() int {
262+
s2Prop := p.Entity.Property("m_iPawnHealth")
263+
264+
if s2Prop != nil {
265+
return int(s2Prop.Value().S2UInt64())
266+
}
267+
260268
return getInt(p.Entity, "m_iHealth")
261269
}
262270

pkg/demoinfocs/datatables.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,6 @@ func (p *parser) bindNewPlayerControllerS2(controllerEntity st.Entity) {
339339
pl.Entity = nil
340340
})
341341

342-
// Position
343-
controllerEntity.OnPositionUpdate(func(pos r3.Vector) {
344-
if pl.IsAlive() {
345-
pl.LastAlivePosition = pos
346-
}
347-
})
348-
349342
if isNew {
350343
if pl.SteamID64 != 0 {
351344
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
@@ -360,6 +353,18 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
360353
return p.gameState.Participants().FindByHandle64(pawnEntity.PropertyValueMust("m_hController").Handle())
361354
}
362355

356+
// Position
357+
pawnEntity.OnPositionUpdate(func(pos r3.Vector) {
358+
pl := player()
359+
if pl == nil {
360+
return
361+
}
362+
363+
if pl.IsAlive() {
364+
pl.LastAlivePosition = pos
365+
}
366+
})
367+
363368
pawnEntity.Property("m_flFlashDuration").OnUpdate(func(val st.PropertyValue) {
364369
pl := player()
365370
if pl == nil {

pkg/demoinfocs/sendtables2/entity.go

Lines changed: 45 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -167,66 +167,51 @@ func (e *Entity) ApplyUpdate(reader *bit.BitReader) {
167167
const (
168168
serverClassPlayer = "CCSPlayerPawn"
169169

170-
maxCoordInt = 16384
171-
172-
propCellBits = "m_cellbits"
173-
propCellX = "CBodyComponent.m_cellX"
174-
propCellY = "CBodyComponent.m_cellY"
175-
propCellZ = "CBodyComponent.m_cellZ"
176-
propVecOrigin = "m_vecOrigin"
177-
propVecOriginPlayerXY = "cslocaldata.m_vecOrigin"
178-
propVecOriginPlayerZ = "cslocaldata.m_vecOrigin[2]"
170+
propCellX = "CBodyComponent.m_cellX"
171+
propCellY = "CBodyComponent.m_cellY"
172+
propCellZ = "CBodyComponent.m_cellZ"
173+
propVecX = "CBodyComponent.m_vecX"
174+
propVecY = "CBodyComponent.m_vecY"
175+
propVecZ = "CBodyComponent.m_vecZ"
179176
)
180177

181178
func (e *Entity) isPlayer() bool {
182179
return e.class.name == serverClassPlayer
183180
}
184181

185182
// Returns a coordinate from a cell + offset
186-
func coordFromCell(cell, cellWidth int, offset float64) float64 {
187-
return float64(cell*cellWidth-maxCoordInt) + offset
183+
func coordFromCell(cell uint64, offset float32) float64 {
184+
const (
185+
cellBits = 9
186+
maxCoordInt = 16384
187+
)
188+
189+
return float64(cell*(1<<cellBits)-maxCoordInt) + float64(offset)
188190
}
189191

190192
func (e *Entity) Position() r3.Vector {
191-
return r3.Vector{} // FIXME: implement
192-
193-
if e.isPlayer() {
194-
// FIXME: POV demo support
195-
xyProp := e.Property(propVecOriginPlayerXY)
196-
zProp := e.Property(propVecOriginPlayerZ)
197-
198-
xy := xyProp.Value().VectorVal
199-
z := float64(zProp.Value().FloatVal)
200-
201-
return r3.Vector{
202-
X: xy.X,
203-
Y: xy.Y,
204-
Z: z,
205-
}
206-
}
207-
208-
cellBitsProp := e.Property(propCellBits)
209193
cellXProp := e.Property(propCellX)
210194
cellYProp := e.Property(propCellY)
211195
cellZProp := e.Property(propCellZ)
212-
offsetProp := e.Property(propVecOrigin)
196+
offsetXProp := e.Property(propVecX)
197+
offsetYProp := e.Property(propVecY)
198+
offsetZProp := e.Property(propVecZ)
213199

214-
cellWidth := 1 << uint(cellBitsProp.Value().IntVal)
215-
cellX := cellXProp.Value().IntVal
216-
cellY := cellYProp.Value().IntVal
217-
cellZ := cellZProp.Value().IntVal
218-
offset := offsetProp.Value().VectorVal
200+
cellX := cellXProp.Value().S2UInt64()
201+
cellY := cellYProp.Value().S2UInt64()
202+
cellZ := cellZProp.Value().S2UInt64()
203+
offsetX := offsetXProp.Value().Float()
204+
offsetY := offsetYProp.Value().Float()
205+
offsetZ := offsetZProp.Value().Float()
219206

220207
return r3.Vector{
221-
X: coordFromCell(cellX, cellWidth, offset.X),
222-
Y: coordFromCell(cellY, cellWidth, offset.Y),
223-
Z: coordFromCell(cellZ, cellWidth, offset.Z),
208+
X: coordFromCell(cellX, offsetX),
209+
Y: coordFromCell(cellY, offsetY),
210+
Z: coordFromCell(cellZ, offsetZ),
224211
}
225212
}
226213

227214
func (e *Entity) OnPositionUpdate(h func(pos r3.Vector)) {
228-
return // FIXME: implement
229-
230215
pos := new(r3.Vector)
231216
firePosUpdate := func(st.PropertyValue) {
232217
newPos := e.Position()
@@ -236,15 +221,12 @@ func (e *Entity) OnPositionUpdate(h func(pos r3.Vector)) {
236221
}
237222
}
238223

239-
if e.isPlayer() {
240-
e.Property(propVecOriginPlayerXY).OnUpdate(firePosUpdate) // FIXME: POV demos use different property names
241-
e.Property(propVecOriginPlayerZ).OnUpdate(firePosUpdate)
242-
} else {
243-
e.Property(propCellX).OnUpdate(firePosUpdate)
244-
e.Property(propCellY).OnUpdate(firePosUpdate)
245-
e.Property(propCellZ).OnUpdate(firePosUpdate)
246-
e.Property(propVecOrigin).OnUpdate(firePosUpdate)
247-
}
224+
e.Property(propCellX).OnUpdate(firePosUpdate)
225+
e.Property(propCellY).OnUpdate(firePosUpdate)
226+
e.Property(propCellZ).OnUpdate(firePosUpdate)
227+
e.Property(propVecX).OnUpdate(firePosUpdate)
228+
e.Property(propVecY).OnUpdate(firePosUpdate)
229+
e.Property(propVecZ).OnUpdate(firePosUpdate)
248230
}
249231

250232
func (e *Entity) OnDestroy(delegate func()) {
@@ -417,12 +399,14 @@ func (p *Parser) FindEntityByHandle(handle uint64) *Entity {
417399

418400
// FilterEntity finds entities by callback
419401
func (p *Parser) FilterEntity(fb func(*Entity) bool) []*Entity {
420-
entities := make([]*Entity, 0, 0)
402+
entities := make([]*Entity, 0)
403+
421404
for _, et := range p.entities {
422405
if fb(et) {
423406
entities = append(entities, et)
424407
}
425408
}
409+
426410
return entities
427411
}
428412

@@ -435,20 +419,17 @@ func (e *Entity) readFields(r *reader) {
435419
val := decoder(r)
436420
e.state.set(fp, val)
437421

438-
upd := e.updateHandlers[e.class.getNameForFieldPath(fp)]
439-
if upd != nil {
440-
for _, h := range upd {
441-
h(st.PropertyValue{
442-
VectorVal: r3.Vector{},
443-
IntVal: 0,
444-
Int64Val: 0,
445-
ArrayVal: nil,
446-
StringVal: "",
447-
FloatVal: 0,
448-
Any: val,
449-
S2: true,
450-
})
451-
}
422+
for _, h := range e.updateHandlers[e.class.getNameForFieldPath(fp)] {
423+
h(st.PropertyValue{
424+
VectorVal: r3.Vector{},
425+
IntVal: 0,
426+
Int64Val: 0,
427+
ArrayVal: nil,
428+
StringVal: "",
429+
FloatVal: 0,
430+
Any: val,
431+
S2: true,
432+
})
452433
}
453434

454435
fp.release()

pkg/demoinfocs/sendtables2/field_path.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,13 @@ func (fp *fieldPath) release() {
309309
func readFieldPaths(r *reader) []*fieldPath {
310310
fp := newFieldPath()
311311

312-
node, next := huffTree, huffTree
312+
node := huffTree
313313

314314
paths := []*fieldPath{}
315315

316316
for !fp.done {
317+
var next huffmanTree
318+
317319
if r.readBits(1) == 1 {
318320
next = node.Right()
319321
} else {

pkg/demoinfocs/sendtables2/reader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,13 @@ func (r *reader) readUBitVar() uint32 {
166166
switch ret & 0x30 {
167167
case 16:
168168
ret = (ret & 15) | (r.readBits(4) << 4)
169-
break
169+
170170
case 32:
171171
ret = (ret & 15) | (r.readBits(8) << 4)
172-
break
172+
173173
case 48:
174174
ret = (ret & 15) | (r.readBits(28) << 4)
175-
break
175+
176176
}
177177

178178
return ret

0 commit comments

Comments
 (0)