aboutsummaryrefslogtreecommitdiff
path: root/pkg/messages/types/svcBspDecal.go
blob: 9ef24584dfceebd54d0c4d10e3286295a98277ad (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package messages

import (
	"github.com/pektezol/bitreader"
	"github.com/pektezol/sdp.go/pkg/types"
)

type SvcBspDecal struct {
	Pos               []vectorCoord `json:"pos"`
	DecalTextureIndex int16         `json:"decal_texture_index"`
	EntityIndex       uint16        `json:"entity_index"`
	ModelIndex        uint16        `json:"model_index"`
	LowPriority       bool          `json:"low_priority"`
}

type vectorCoord struct {
	Value float32 `json:"value"`
	Valid bool    `json:"valid"`
}

func ParseSvcBspDecal(reader *bitreader.Reader, demo *types.Demo) SvcBspDecal {
	svcBspDecal := SvcBspDecal{
		Pos:               readVectorCoords(reader),
		DecalTextureIndex: int16(reader.TryReadBits(9)),
	}
	if reader.TryReadBool() {
		svcBspDecal.EntityIndex = uint16(reader.TryReadBits(11))
		svcBspDecal.ModelIndex = uint16(reader.TryReadBits(11))
	}
	svcBspDecal.LowPriority = reader.TryReadBool()
	demo.Writer.TempAppendLine("\t\tPosition: %v", svcBspDecal.Pos)
	demo.Writer.TempAppendLine("\t\tDecal Texture Index: %d", svcBspDecal.DecalTextureIndex)
	demo.Writer.TempAppendLine("\t\tEntity Index: %d", svcBspDecal.EntityIndex)
	demo.Writer.TempAppendLine("\t\tModel Index: %d", svcBspDecal.ModelIndex)
	demo.Writer.TempAppendLine("\t\tLow Priority: %t", svcBspDecal.LowPriority)
	return svcBspDecal
}

func readVectorCoords(reader *bitreader.Reader) []vectorCoord {
	const COORD_INTEGER_BITS uint8 = 14
	const COORD_FRACTIONAL_BITS uint8 = 5
	const COORD_DENOMINATOR uint8 = 1 << COORD_FRACTIONAL_BITS
	const COORD_RESOLUTION float32 = 1.0 / float32(COORD_DENOMINATOR)
	readVectorCoord := func() float32 {
		value := float32(0)
		integer := reader.TryReadBits(1)
		fraction := reader.TryReadBits(1)
		if integer != 0 || fraction != 0 {
			sign := reader.TryReadBits(1)
			if integer != 0 {
				integer = reader.TryReadBits(uint64(COORD_INTEGER_BITS)) + 1
			}
			if fraction != 0 {
				fraction = reader.TryReadBits(uint64(COORD_FRACTIONAL_BITS))
			}
			value = float32(integer) + float32(fraction)*COORD_RESOLUTION
			if sign != 0 {
				value = -value
			}
		}
		return value
	}
	x := reader.TryReadBits(1)
	y := reader.TryReadBits(1)
	z := reader.TryReadBits(1)
	return []vectorCoord{{Value: readVectorCoord(), Valid: x != 0}, {Value: readVectorCoord(), Valid: y != 0}, {Value: readVectorCoord(), Valid: z != 0}}
}