aboutsummaryrefslogtreecommitdiff
path: root/utils/bitreader.go
blob: ec7454ea16bdc88c9cd9506039e6c0d0c8950f9c (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package utils

import (
	"fmt"
	"math"
	"strconv"
)

func ReadButtonsDataFromInt32(input int32) []string {
	buttonList := [32]string{
		"Attack",
		"Jump",
		"Duck",
		"Forward",
		"Back",
		"Use",
		"Cancel",
		"Left",
		"Right",
		"MoveLeft",
		"MoveRight",
		"Attack2",
		"Run",
		"Reload",
		"Alt1",
		"Alt2",
		"Score",
		"Speed",
		"Walk",
		"Zoom",
		"Weapon1",
		"Weapon2",
		"BullRush",
		"Grenade1",
		"Grenade2",
		"LookSpin",
		"CurrentAbility",
		"PreviousAbility",
		"Ability1",
		"Ability2",
		"Ability3",
		"Ability4",
	}
	var buttons []string
	if input == 0 {
		buttons = append(buttons, buttonList[0])
		return buttons
	}
	for i := 1; i < 33; i++ {
		if ReadBitState(input, i) {
			buttons = append(buttons, buttonList[i])
		}
	}
	return buttons
}

func ReadBitState(input int32, index int) bool {
	value := input & (1 << index)
	return value > 0
}

func ReadBitStateLSB(input byte, index int) (bool, error) {
	if index < 0 && index > 7 {
		return false, fmt.Errorf("IndexOutOfBounds for type byte")
	}
	value := input & (1 << index)
	return (value > 0), nil
}

func Read32BitsAfterFirstBitInt32(input []byte, index int, step int) int32 {
	binary := ""
	binary += fmt.Sprintf("%08b", input[step])[8-index : 8]
	binary += fmt.Sprintf("%08b", input[step-1])
	binary += fmt.Sprintf("%08b", input[step-2])
	binary += fmt.Sprintf("%08b", input[step-3])
	binary += fmt.Sprintf("%08b", input[step-4])[:8-index]
	output, err := strconv.ParseInt(binary, 2, 32)
	CheckError(err)
	return int32(output)

}

func Read32BitsAfterFirstBitFloat32(input []byte, index int, step int) float32 {
	binary := ""
	binary += fmt.Sprintf("%08b", input[step])[8-index : 8]
	binary += fmt.Sprintf("%08b", input[step-1])
	binary += fmt.Sprintf("%08b", input[step-2])
	binary += fmt.Sprintf("%08b", input[step-3])
	binary += fmt.Sprintf("%08b", input[step-4])[:8-index]
	output, err := strconv.ParseUint(binary, 2, 32)
	CheckError(err)
	return math.Float32frombits(uint32(output))

}