-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprint-overlap.go
430 lines (382 loc) · 12.1 KB
/
print-overlap.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
demoinfocs "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs"
common "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/common"
events "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/events"
st "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/sendtables"
)
type MoveData struct {
pl *common.Player
Tracking bool
LastWSOverlapTick int
LastADOverlapTick int
WSOverlapTicks []int
ADOverlapTicks []int
IsWSOverlapping bool
IsADOverlapping bool
LastMoveAttemptTick int
NumMoveTicks int
GoodGroundSwitchCount int
OldButtons uint64
OldYaw float32
TurnState int8
GoodTurns uint32
AirTime uint32
AirTurnData []float64
}
const (
IN_FORWARD = uint64(0x8)
IN_BACK = uint64(0x10)
IN_MOVELEFT = uint64(0x200)
IN_MOVERIGHT = uint64(0x400)
)
func (mv MoveData) GetWSTotalOverlap() int {
total := 0
for _, v := range mv.WSOverlapTicks {
total += v
}
return total
}
func (mv MoveData) GetADTotalOverlap() int {
total := 0
for _, v := range mv.ADOverlapTicks {
total += v
}
return total
}
func (mv MoveData) GetTurning() int8 {
curYaw := mv.pl.ViewDirectionY()
turning := curYaw != mv.OldYaw
if !turning {
return 0
}
if curYaw < mv.OldYaw-180 || (curYaw > mv.OldYaw && curYaw < mv.OldYaw+180) {
return -1
}
return 1
}
func (mv MoveData) checkGoodSwitch(newButtons uint64) bool {
if mv.OldButtons&IN_FORWARD != 0 && // Used to press forward
newButtons&IN_FORWARD == 0 && // Not anymore
mv.OldButtons&IN_BACK == 0 && // Did not press backward
newButtons&IN_BACK != 0 { // Now do though
return true
}
if mv.OldButtons&IN_MOVELEFT != 0 &&
newButtons&IN_MOVELEFT == 0 &&
mv.OldButtons&IN_MOVERIGHT == 0 &&
newButtons&IN_MOVERIGHT != 0 {
return true
}
if mv.OldButtons&IN_BACK != 0 &&
newButtons&IN_BACK == 0 &&
mv.OldButtons&IN_FORWARD == 0 &&
newButtons&IN_FORWARD != 0 {
return true
}
if mv.OldButtons&IN_MOVERIGHT != 0 &&
newButtons&IN_MOVERIGHT == 0 &&
mv.OldButtons&IN_MOVELEFT == 0 &&
newButtons&IN_MOVELEFT != 0 {
return true
}
return false
}
// Run like this: go run print-overlap.go -demo="/path/to/demo.dem"
// Run like this: go run print-overlap.go -dir="/path/to/"
type Result struct {
Path string
Error error
}
func main() {
dir := flag.String("dir", "", "Directory to process")
demo := flag.String("demo", "", "Demo file `path`")
verbose := flag.Bool("v", false, "Enable verbose stdout")
max := flag.Int("max-concurrent", 8, "Maximum amount of demos parsed at the same time")
result := make(chan Result)
// Parse the flags
flag.Parse()
// WaitGroup to wait for all goroutines to finish
var wg sync.WaitGroup
semaphore := make(chan struct{}, *max)
if (*dir == "" && *demo == "") || (*dir != "" && *demo != "") {
fmt.Println("Error: -dir OR -demo flag is required")
flag.Usage()
os.Exit(1)
}
fmt.Println("Movement Input Parser by zer0.k")
fmt.Println("Keep in mind that this overlap data can be inaccurate and does not contain subtick information.")
fmt.Println("----")
if *demo != "" {
wg.Add(1)
go func() {
defer wg.Done()
parseDemo(*demo, *verbose, result)
}()
} else {
fmt.Println("Parsing dir", *dir)
err := filepath.Walk(*dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(info.Name()) == ".dem" {
fmt.Println("Parsing demo file:", path)
wg.Add(1)
semaphore <- struct{}{} // Acquire semaphore
go func(path string, verbose bool) {
defer wg.Done()
defer func() { <-semaphore }() // Release the semaphore
parseDemo(path, verbose, result)
}(path, *verbose)
}
return nil
})
checkError(err)
}
go func() {
wg.Wait()
close(result)
}()
for res := range result {
if res.Error != nil {
fmt.Printf("Failed goroutines: Path=%s, Error: %v\n", res.Path, res.Error)
}
}
fmt.Println("Parsing done.")
}
func parseDemo(path string, verbose bool, result chan<- Result) {
reported := false
mapPlayerEx := make(map[uint64]*MoveData)
outputPath := strings.TrimSuffix(path, filepath.Ext(path)) + ".csv"
var res Result
res.Path = path
defer func() {
if err := recover(); err != nil {
fmt.Printf("panic occurred for path %s: %s\n", path, err)
os.Remove(outputPath)
res.Error = fmt.Errorf("panic occurred: %v", err)
result <- res
}
}()
output, err := os.Create(outputPath)
checkError(err)
defer output.Close()
f, err := os.Open(path)
checkError(err)
defer f.Close()
stat, err := f.Stat()
checkError(err)
p := demoinfocs.NewParser(f)
defer p.Close()
// Do not use this at the end of the game.
getOverlapDataFromPawnEntity := func(pawnEntity st.Entity) *MoveData {
controllerProp, hasProp := pawnEntity.PropertyValue("m_hController")
if !hasProp {
return nil
}
player := p.GameState().Participants().FindByHandle64(controllerProp.Handle())
if player == nil {
return nil
}
if player.SteamID64 == 0 {
return nil
}
if mapPlayerEx[player.SteamID64] == nil {
mapPlayerEx[player.SteamID64] = &MoveData{pl: player, Tracking: true}
}
return mapPlayerEx[player.SteamID64]
}
p.RegisterEventHandler(func(events.FrameDone) {
for _, pl := range p.GameState().Participants().All() {
mv := mapPlayerEx[pl.SteamID64]
if mv == nil {
//fmt.Printf("Player %s is not valid\n", pl.Name)
continue
}
if !mv.Tracking {
continue
}
pawnEntity := mv.pl.PlayerPawnEntity()
if pawnEntity == nil || pawnEntity.ServerClass().Name() != "CCSPlayerPawn" {
continue
}
prop, _ := pawnEntity.PropertyValue("m_hGroundEntity")
groundEnt := p.GameState().EntityByHandle(prop.Handle())
if groundEnt == nil {
mv.AirTime++
if mv.GetTurning() > 0 {
diff := mv.OldYaw - mv.pl.ViewDirectionY()
if diff < 0 {
diff += 360
}
mv.AirTurnData = append(mv.AirTurnData, float64(diff))
} else if mv.GetTurning() < 0 {
diff := mv.pl.ViewDirectionY() - mv.OldYaw
if diff < 0 {
diff += 360
}
mv.AirTurnData = append(mv.AirTurnData, float64(diff))
}
if mv.TurnState+mv.GetTurning() == 0 && mv.TurnState != 0 {
mv.GoodTurns++
}
}
mv.TurnState = mv.GetTurning()
mv.OldYaw = mv.pl.ViewDirectionY()
}
})
p.RegisterEventHandler(func(events.DataTablesParsed) {
p.ServerClasses().FindByName("CCSPlayerPawn").OnEntityCreated(func(pawnEntity st.Entity) {
buttonProp := pawnEntity.Property("m_pMovementServices.m_nButtonDownMaskPrev")
if buttonProp != nil {
buttonChanged := func(val st.PropertyValue) {
// Can dead players press buttons? What about freeze time?
// Let's just ignore these questions for now.
mv := getOverlapDataFromPawnEntity(pawnEntity)
if mv == nil || !mv.Tracking {
return
}
// Pressing any key?
if val.S2UInt64()&0x618 != 0 && mv.OldButtons == 0 {
mv.LastMoveAttemptTick = p.GameState().IngameTick()
//fmt.Printf("Player %s started moving @%d\n", ol.pl.Name, ol.LastMoveAttemptTick)
} else if val.S2UInt64()&0x618 == 0 && mv.OldButtons != 0 {
mv.NumMoveTicks += p.GameState().IngameTick() - mv.LastMoveAttemptTick
//fmt.Printf("Player %s stopped moving @%d (+%d)\n", ol.pl.Name, p.GameState().IngameTick(), p.GameState().IngameTick()-ol.LastMoveAttemptTick)
}
// Overlapping?
if (val.S2UInt64()&0x10 != 0) && (val.S2UInt64()&0x8 != 0) {
mv.IsWSOverlapping = true
mv.LastWSOverlapTick = p.GameState().IngameTick()
// fmt.Printf("%s W/S overlapped at tick %d\n",
// ol.pl.Name,
// p.GameState().IngameTick())
} else if mv.IsWSOverlapping {
mv.IsWSOverlapping = false
numOverlapTick := p.GameState().IngameTick() - mv.LastWSOverlapTick
if numOverlapTick > 1 {
mv.WSOverlapTicks = append(mv.WSOverlapTicks, numOverlapTick)
} else {
mv.GoodGroundSwitchCount++
}
}
if (val.S2UInt64()&0x200 != 0) && (val.S2UInt64()&0x400 != 0) {
mv.IsADOverlapping = true
mv.LastADOverlapTick = p.GameState().IngameTick()
// fmt.Printf("%s A/D overlapped at tick %d\n",
// ol.pl.Name,
// p.GameState().IngameTick())
} else if mv.IsADOverlapping {
mv.IsADOverlapping = false
numOverlapTick := p.GameState().IngameTick() - mv.LastADOverlapTick
if numOverlapTick > 1 {
mv.ADOverlapTicks = append(mv.ADOverlapTicks, numOverlapTick)
} else {
mv.GoodGroundSwitchCount++
}
}
if mv.checkGoodSwitch(val.S2UInt64()) {
mv.GoodGroundSwitchCount++
}
// Doesn't really need other buttons.
mv.OldButtons = val.S2UInt64() & 0x618
}
buttonProp.OnUpdate(buttonChanged)
}
})
})
spewReport := func() {
if reported {
return
}
if verbose {
fmt.Printf("Game duration: %d ticks (%f minutes)\n", p.GameState().IngameTick(), float64(p.GameState().IngameTick())/64.0/60.0)
}
output.WriteString("Date,SteamID64,Name,A/D overlap (instances),A/D overlap (ticks),A/D overlap (tick/instance),W/S overlap (instances),W/S overlap (ticks),W/S overlap (tick/instance),Good Strafe Switch,Total Move Ticks,Good Airstrafe Turns,Total Airtime\n")
for _, pl := range p.GameState().Participants().All() {
mv := mapPlayerEx[pl.SteamID64]
if mv == nil {
//fmt.Printf("Player %s is not valid\n", pl.Name)
continue
}
mv.Tracking = false
// Finalize the stats.
if mv.OldButtons != 0 {
mv.NumMoveTicks += p.GameState().IngameTick() - mv.LastMoveAttemptTick
}
if mv.IsWSOverlapping {
mv.IsWSOverlapping = false
numOverlapTick := p.GameState().IngameTick() - mv.LastWSOverlapTick
if numOverlapTick > 1 {
mv.WSOverlapTicks = append(mv.WSOverlapTicks, numOverlapTick)
} else {
mv.GoodGroundSwitchCount++
}
}
if mv.IsADOverlapping {
mv.IsADOverlapping = false
numOverlapTick := p.GameState().IngameTick() - mv.LastADOverlapTick
if numOverlapTick > 1 {
mv.ADOverlapTicks = append(mv.ADOverlapTicks, numOverlapTick)
} else {
mv.GoodGroundSwitchCount++
}
}
if verbose {
fmt.Printf("%s (%d): W/S overlap ticks %d, A/D overlap ticks %d, good key switch count %d, total move ticks %d, good turns %d, airtime %d",
mv.pl.Name, mv.pl.SteamID64, mv.GetWSTotalOverlap(), mv.GetADTotalOverlap(), mv.GoodGroundSwitchCount, mv.NumMoveTicks, mv.GoodTurns, mv.AirTime)
fmt.Println("")
}
// Airstrafe speed stuff
// mean, _ := stats.Mean(ol.AirTurnData)
// fmt.Printf(", average turn speed %f (%d samples total)\n", mean, len(ol.AirTurnData))
// for i := 10; i < 100; i += 20 {
// percentile, _ := stats.Percentile(ol.AirTurnData, float64(i))
// fmt.Printf("%d%%: %f\n", i, percentile)
// }
WSavg := float32(0)
if len(mv.WSOverlapTicks) > 0 {
WSavg = float32(mv.GetWSTotalOverlap()) / float32(len(mv.WSOverlapTicks))
}
ADavg := float32(0)
if len(mv.ADOverlapTicks) > 0 {
ADavg = float32(mv.GetADTotalOverlap()) / float32(len(mv.ADOverlapTicks))
}
line := fmt.Sprintf("%s,%d,%s,%d,%d,%f,%d,%d,%f,%d,%d,%d,%d\n",
stat.ModTime().Format(time.DateTime),
mv.pl.SteamID64,
mv.pl.Name,
len(mv.ADOverlapTicks),
mv.GetADTotalOverlap(),
ADavg,
len(mv.WSOverlapTicks),
mv.GetWSTotalOverlap(),
WSavg,
mv.GoodGroundSwitchCount,
mv.NumMoveTicks,
mv.GoodTurns,
mv.AirTime)
output.WriteString(line)
}
reported = true
}
p.RegisterEventHandler(func(events.AnnouncementWinPanelMatch) {
spewReport()
})
// Parse to end
err = p.ParseToEnd()
spewReport()
checkError(err)
}
func checkError(err error) {
if err != nil {
panic(err)
}
}