forked from markus-wa/demoinfocs-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities.go
52 lines (43 loc) · 1.27 KB
/
entities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package demoinfocs
import (
"bytes"
bit "github.com/markus-wa/demoinfocs-golang/bitread"
"github.com/markus-wa/demoinfocs-golang/msg"
)
const entitySentinel = 9999
func (p *Parser) handlePacketEntities(pe *msg.CSVCMsg_PacketEntities) {
defer func() {
p.setError(recoverFromUnexpectedEOF(recover()))
}()
r := bit.NewSmallBitReader(bytes.NewReader(pe.EntityData))
currentEntity := -1
for i := 0; i < int(pe.UpdatedEntries); i++ {
currentEntity += 1 + int(r.ReadUBitInt())
if currentEntity > entitySentinel {
break
}
if r.ReadBit() {
// Leave PVS
if entity := p.gameState.entities[currentEntity]; entity != nil {
entity.Destroy()
delete(p.gameState.entities, currentEntity)
}
// 'Force Delete' flag, not exactly sure what it's supposed to do
r.ReadBit()
} else if r.ReadBit() {
// Enter PVS
if existing := p.gameState.entities[currentEntity]; existing != nil {
// Sometimes entities don't get destroyed when they should be
// For instance when a player is replaced by a BOT
existing.Destroy()
}
p.gameState.entities[currentEntity] = p.stParser.ReadEnterPVS(r, currentEntity)
} else {
// Delta Update
if entity := p.gameState.entities[currentEntity]; entity != nil {
entity.ApplyUpdate(r)
}
}
}
r.Pool()
}