-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
162 lines (142 loc) · 5.58 KB
/
utils.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package parser
import (
"math"
encoder "github.com/hx-w/minidemo-encoder/internal/encoder"
ilog "github.com/hx-w/minidemo-encoder/internal/logger"
common "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/common"
)
const Pi = 3.14159265358979323846
var bufWeaponMap map[string]int32 = make(map[string]int32)
var playerLastZ map[string]float32 = make(map[string]float32)
// Function to handle errors
func checkError(err error) {
if err != nil {
ilog.ErrorLogger.Println(err.Error())
}
}
func parsePlayerInitFrame(player *common.Player) {
iFrameInit := encoder.FrameInitInfo{
PlayerName: player.Name,
}
iFrameInit.Position[0] = float32(player.Position().X)
iFrameInit.Position[1] = float32(player.Position().Y)
iFrameInit.Position[2] = float32(player.Position().Z)
iFrameInit.Angles[0] = float32(player.ViewDirectionY())
iFrameInit.Angles[1] = float32(player.ViewDirectionX())
encoder.InitPlayer(iFrameInit)
delete(bufWeaponMap, player.Name)
delete(encoder.PlayerFramesMap, player.Name)
playerLastZ[player.Name] = float32(player.Position().Z)
}
func normalizeDegree(degree float64) float64 {
if degree < 0.0 {
degree = degree + 360.0
}
return degree
}
// accept radian, return degree in [0, 360)
func radian2degree(radian float64) float64 {
return normalizeDegree(radian * 180 / Pi)
}
func parsePlayerFrame(player *common.Player, addonButton int32, tickrate float64, fullsnap bool) {
if !player.IsAlive() {
return
}
iFrameInfo := new(encoder.FrameInfo)
iFrameInfo.PredictedVelocity[0] = 0.0
iFrameInfo.PredictedVelocity[1] = 0.0
iFrameInfo.PredictedVelocity[2] = 0.0
iFrameInfo.ActualVelocity[0] = float32(player.Velocity().X)
iFrameInfo.ActualVelocity[1] = float32(player.Velocity().Y)
iFrameInfo.ActualVelocity[2] = float32(player.Velocity().Z)
iFrameInfo.PredictedAngles[0] = player.ViewDirectionY()
iFrameInfo.PredictedAngles[1] = player.ViewDirectionX()
iFrameInfo.PlayerImpulse = 0
iFrameInfo.PlayerSeed = 0
iFrameInfo.PlayerSubtype = 0
// ----- button encode
iFrameInfo.PlayerButtons = ButtonConvert(player, addonButton)
// ---- weapon encode
var currWeaponID int32 = 0
if player.ActiveWeapon() != nil {
currWeaponID = int32(WeaponStr2ID(player.ActiveWeapon().String()))
}
if len(encoder.PlayerFramesMap[player.Name]) == 0 {
iFrameInfo.CSWeaponID = currWeaponID
bufWeaponMap[player.Name] = currWeaponID
} else if currWeaponID == bufWeaponMap[player.Name] {
iFrameInfo.CSWeaponID = int32(CSWeapon_NONE)
} else {
iFrameInfo.CSWeaponID = currWeaponID
bufWeaponMap[player.Name] = currWeaponID
}
lastIdx := len(encoder.PlayerFramesMap[player.Name]) - 1
// addons
if fullsnap || (lastIdx < 2000 && (lastIdx+1)%int(tickrate) == 0) || (lastIdx >= 2000 && (lastIdx+1)%int(tickrate) == 0) {
// if false {
iFrameInfo.AdditionalFields |= encoder.FIELDS_ORIGIN
iFrameInfo.AtOrigin[0] = float32(player.Position().X)
iFrameInfo.AtOrigin[1] = float32(player.Position().Y)
iFrameInfo.AtOrigin[2] = float32(player.Position().Z)
// iFrameInfo.AdditionalFields |= encoder.FIELDS_ANGLES
// iFrameInfo.AtAngles[0] = float32(player.ViewDirectionY())
// iFrameInfo.AtAngles[1] = float32(player.ViewDirectionX())
iFrameInfo.AdditionalFields |= encoder.FIELDS_VELOCITY
iFrameInfo.AtVelocity[0] = float32(player.Velocity().X)
iFrameInfo.AtVelocity[1] = float32(player.Velocity().Y)
iFrameInfo.AtVelocity[2] = float32(player.Velocity().Z)
}
// record Z velocity
deltaZ := float32(player.Position().Z) - playerLastZ[player.Name]
playerLastZ[player.Name] = float32(player.Position().Z)
// velocity in Z direction need to be recorded specially
iFrameInfo.ActualVelocity[2] = deltaZ * float32(tickrate)
// Since I don't know how to get player's button bits in a tick frame,
// I have to use *actual vels* and *angles* to generate *predicted vels* approximately
// This will cause some error, but it's not a big deal
if lastIdx >= 0 { // not first frame
// We assume that actual velocity in tick N
// is influenced by predicted velocity in tick N-1
_preVel := &encoder.PlayerFramesMap[player.Name][lastIdx].PredictedVelocity
// PV = 0.0 when AV(tick N-1) = 0.0 and AV(tick N) = 0.0 ?
// Note: AV=Actual Velocity, PV=Predicted Velocity
if !(iFrameInfo.ActualVelocity[0] == 0.0 &&
iFrameInfo.ActualVelocity[1] == 0.0 &&
encoder.PlayerFramesMap[player.Name][lastIdx].ActualVelocity[0] == 0.0 &&
encoder.PlayerFramesMap[player.Name][lastIdx].ActualVelocity[1] == 0.0) {
var velAngle float64 = 0.0
if iFrameInfo.ActualVelocity[0] == 0.0 {
if iFrameInfo.ActualVelocity[1] < 0.0 {
velAngle = 270.0
} else {
velAngle = 90.0
}
} else {
velAngle = radian2degree(math.Atan2(float64(iFrameInfo.ActualVelocity[1]), float64(iFrameInfo.ActualVelocity[0])))
}
faceFront := normalizeDegree(float64(iFrameInfo.PredictedAngles[1]))
deltaAngle := normalizeDegree(velAngle - faceFront)
const threshold = 30.0
if 0.0+threshold < deltaAngle && deltaAngle < 180.0-threshold {
_preVel[1] = -450.0 // left
}
if 90.0+threshold < deltaAngle && deltaAngle < 270.0-threshold {
_preVel[0] = -450.0 // back
}
if 180.0+threshold < deltaAngle && deltaAngle < 360.0-threshold {
_preVel[1] = 450.0 // right
}
if 270.0+threshold < deltaAngle || deltaAngle < 90.0-threshold {
_preVel[0] = 450.0 // front
}
}
}
encoder.PlayerFramesMap[player.Name] = append(encoder.PlayerFramesMap[player.Name], *iFrameInfo)
}
func saveToRecFile(player *common.Player, roundNum int32) {
if player.Team == common.TeamTerrorists {
encoder.WriteToRecFile(player.Name, roundNum, "t")
} else {
encoder.WriteToRecFile(player.Name, roundNum, "ct")
}
}