-
Notifications
You must be signed in to change notification settings - Fork 139
/
sim.go
636 lines (533 loc) · 17.8 KB
/
sim.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package core
import (
"fmt"
"log"
"math"
"math/rand"
"runtime"
"runtime/debug"
"slices"
"strconv"
"strings"
"time"
"github.com/wowsims/wotlk/sim/core/proto"
)
type Task interface {
RunTask(sim *Simulation) time.Duration
}
type Simulation struct {
*Environment
Options *proto.SimOptions
rand Rand
rseed int64
// Used for testing only, see RandomFloat().
isTest bool
testRands map[string]Rand
// Current Simulation State
pendingActions []*PendingAction
CurrentTime time.Duration // duration that has elapsed in the sim since starting
Duration time.Duration // Duration of current iteration
NeedsInput bool // Sim is in interactive mode and needs input
ProgressReport func(*proto.ProgressMetrics)
Log func(string, ...interface{})
executePhase int32 // 20, 25, or 35 for the respective execute range, 100 otherwise
executePhaseCallbacks []func(*Simulation, int32) // 2nd parameter is 35 for 35%, 25 for 25% and 20 for 20%
nextExecuteDuration time.Duration
nextExecuteDamage float64
endOfCombatDuration time.Duration
endOfCombatDamage float64
minTrackerTime time.Duration
trackers []*auraTracker
minWeaponAttackTime time.Duration
weaponAttacks []*WeaponAttack
minTaskTime time.Duration
tasks []Task
}
func (sim *Simulation) rescheduleTracker(trackerTime time.Duration) {
sim.minTrackerTime = min(sim.minTrackerTime, trackerTime)
}
func (sim *Simulation) addTracker(tracker *auraTracker) {
sim.trackers = append(sim.trackers, tracker)
sim.rescheduleTracker(tracker.minExpires)
}
func (sim *Simulation) removeTracker(tracker *auraTracker) {
if idx := slices.Index(sim.trackers, tracker); idx != -1 {
sim.trackers = removeBySwappingToBack(sim.trackers, idx)
}
}
func (sim *Simulation) rescheduleWeaponAttack(weaponAttackTime time.Duration) {
sim.minWeaponAttackTime = min(sim.minWeaponAttackTime, weaponAttackTime)
}
func (sim *Simulation) addWeaponAttack(weaponAttack *WeaponAttack) {
sim.weaponAttacks = append(sim.weaponAttacks, weaponAttack)
}
func (sim *Simulation) removeWeaponAttack(weaponAttack *WeaponAttack) {
if idx := slices.Index(sim.weaponAttacks, weaponAttack); idx != -1 {
sim.weaponAttacks = removeBySwappingToBack(sim.weaponAttacks, idx)
}
}
func (sim *Simulation) RescheduleTask(taskTime time.Duration) {
sim.minTaskTime = min(sim.minTaskTime, taskTime)
}
func (sim *Simulation) AddTask(task Task) {
sim.tasks = append(sim.tasks, task)
}
func (sim *Simulation) RemoveTask(task Task) {
if idx := slices.Index(sim.tasks, task); idx != -1 {
sim.tasks = removeBySwappingToBack(sim.tasks, idx)
}
}
func RunSim(rsr *proto.RaidSimRequest, progress chan *proto.ProgressMetrics) *proto.RaidSimResult {
return runSim(rsr, progress, false)
}
func runSim(rsr *proto.RaidSimRequest, progress chan *proto.ProgressMetrics, skipPresim bool) (result *proto.RaidSimResult) {
if !rsr.SimOptions.IsTest {
defer func() {
if err := recover(); err != nil {
errStr := ""
switch errt := err.(type) {
case string:
errStr = errt
case error:
errStr = errt.Error()
}
errStr += "\nStack Trace:\n" + string(debug.Stack())
result = &proto.RaidSimResult{
ErrorResult: errStr,
}
if progress != nil {
progress <- &proto.ProgressMetrics{
FinalRaidResult: result,
}
}
}
if progress != nil {
close(progress)
}
}()
}
sim := NewSim(rsr)
if !skipPresim {
if progress != nil {
progress <- &proto.ProgressMetrics{
TotalIterations: sim.Options.Iterations,
PresimRunning: true,
}
runtime.Gosched() // allow time for message to make it back out.
}
presimResult := sim.runPresims(rsr)
if presimResult != nil && presimResult.ErrorResult != "" {
if progress != nil {
progress <- &proto.ProgressMetrics{
TotalIterations: sim.Options.Iterations,
FinalRaidResult: presimResult,
}
}
return presimResult
}
if progress != nil {
progress <- &proto.ProgressMetrics{
TotalIterations: sim.Options.Iterations,
PresimRunning: false,
}
sim.ProgressReport = func(progMetric *proto.ProgressMetrics) {
progress <- progMetric
}
runtime.Gosched() // allow time for message to make it back out.
}
// Use pre-sim as estimate for length of fight (when using health fight)
if sim.Encounter.EndFightAtHealth > 0 && presimResult != nil {
sim.BaseDuration = time.Duration(presimResult.AvgIterationDuration) * time.Second
sim.Duration = time.Duration(presimResult.AvgIterationDuration) * time.Second
sim.Encounter.DurationIsEstimate = false // we now have a pretty good value for duration
}
}
// using a variable here allows us to mutate it in the deferred recover, sending out error info
result = sim.run()
return result
}
func NewSim(rsr *proto.RaidSimRequest) *Simulation {
env, _, _ := NewEnvironment(rsr.Raid, rsr.Encounter, false)
return newSimWithEnv(env, rsr.SimOptions)
}
func newSimWithEnv(env *Environment, simOptions *proto.SimOptions) *Simulation {
rseed := simOptions.RandomSeed
if rseed == 0 {
rseed = time.Now().UnixNano()
}
return &Simulation{
Environment: env,
Options: simOptions,
rand: NewSplitMix(uint64(rseed)),
rseed: rseed,
isTest: simOptions.IsTest,
testRands: make(map[string]Rand),
}
}
// Returns a random float64 between 0.0 (inclusive) and 1.0 (exclusive).
//
// In tests, although we can set the initial seed, test results are still very
// sensitive to the exact order of RandomFloat() calls. To mitigate this, when
// testing we use a separate rand object for each RandomFloat callsite,
// distinguished by the label string.
func (sim *Simulation) RandomFloat(label string) float64 {
return sim.labelRand(label).NextFloat64()
}
func (sim *Simulation) labelRand(label string) Rand {
if !sim.isTest {
return sim.rand
}
labelRng, ok := sim.testRands[label]
if !ok {
// Add rseed to the label, so we still have run-run variance for stat weights.
labelRng = NewSplitMix(uint64(makeTestRandSeed(sim.rseed, label)))
sim.testRands[label] = labelRng
}
return labelRng
}
func (sim *Simulation) reseedRands(i int64) {
rseed := sim.Options.RandomSeed + i
sim.rand.Seed(rseed)
if sim.isTest {
for label, rng := range sim.testRands {
rng.Seed(makeTestRandSeed(rseed, label))
}
}
}
func makeTestRandSeed(rseed int64, label string) int64 {
return int64(hash(label + strconv.FormatInt(rseed, 16)))
}
func (sim *Simulation) RandomExpFloat(label string) float64 {
return rand.New(sim.labelRand(label)).ExpFloat64()
}
// Shorthand for commonly-used RNG behavior.
// Returns a random number between min and max.
func (sim *Simulation) Roll(min float64, max float64) float64 {
return sim.RollWithLabel(min, max, "Damage Roll")
}
func (sim *Simulation) RollWithLabel(min float64, max float64, label string) float64 {
return min + (max-min)*sim.RandomFloat(label)
}
func (sim *Simulation) Proc(p float64, label string) bool {
switch {
case p >= 1:
return true
case p <= 0:
return false
default:
return sim.RandomFloat(label) < p
}
}
func (sim *Simulation) Reset() {
sim.reset()
}
func (sim *Simulation) Reseed(seed int64) {
sim.reseedRands(seed)
}
// Run runs the simulation for the configured number of iterations, and
// collects all the metrics together.
func (sim *Simulation) run() *proto.RaidSimResult {
t0 := time.Now()
logsBuffer := &strings.Builder{}
if sim.Options.Debug || sim.Options.DebugFirstIteration {
sim.Log = func(message string, vals ...interface{}) {
logsBuffer.WriteString(fmt.Sprintf("[%0.2f] "+message+"\n", append([]interface{}{sim.CurrentTime.Seconds()}, vals...)...))
}
}
// Uncomment this to print logs directly to console.
// sim.Options.Debug = true
// sim.Log = func(message string, vals ...interface{}) {
// fmt.Printf(fmt.Sprintf("[%0.1f] "+message+"\n", append([]interface{}{sim.CurrentTime.Seconds()}, vals...)...))
// }
sim.runOnce()
firstIterationDuration := sim.Duration
if sim.Encounter.EndFightAtHealth != 0 {
firstIterationDuration = sim.CurrentTime
}
totalDuration := firstIterationDuration
if !sim.Options.Debug {
sim.Log = nil
}
var st time.Time
for i := int32(1); i < sim.Options.Iterations; i++ {
// fmt.Printf("Iteration: %d\n", i)
if sim.ProgressReport != nil && time.Since(st) > time.Millisecond*100 {
metrics := sim.Raid.GetMetrics()
sim.ProgressReport(&proto.ProgressMetrics{TotalIterations: sim.Options.Iterations, CompletedIterations: i, Dps: metrics.Dps.Avg, Hps: metrics.Hps.Avg})
runtime.Gosched() // ensure that reporting threads are given time to report, mostly only important in wasm (only 1 thread)
st = time.Now()
}
// Before each iteration, reset state to seed+iterations
sim.reseedRands(int64(i))
sim.runOnce()
iterDuration := sim.Duration
if sim.Encounter.EndFightAtHealth != 0 {
iterDuration = sim.CurrentTime
}
totalDuration += iterDuration
}
result := &proto.RaidSimResult{
RaidMetrics: sim.Raid.GetMetrics(),
EncounterMetrics: sim.Encounter.GetMetricsProto(),
Logs: logsBuffer.String(),
FirstIterationDuration: firstIterationDuration.Seconds(),
AvgIterationDuration: totalDuration.Seconds() / float64(sim.Options.Iterations),
}
// Final progress report
if sim.ProgressReport != nil {
sim.ProgressReport(&proto.ProgressMetrics{TotalIterations: sim.Options.Iterations, CompletedIterations: sim.Options.Iterations, Dps: result.RaidMetrics.Dps.Avg, FinalRaidResult: result})
}
if d := sim.Options.Iterations; d > 3000 {
log.Printf("running %d iterations took %s", d, time.Since(t0))
}
return result
}
// RunOnce is the main event loop. It will run the simulation for number of seconds.
func (sim *Simulation) runOnce() {
sim.reset()
sim.PrePull()
sim.runPendingActions()
sim.Cleanup()
}
var (
sentinelPendingAction = &PendingAction{
NextActionAt: NeverExpires,
OnAction: func(sim *Simulation) {
panic("running sentinel pending action")
},
}
)
// Reset will set sim back and erase all current state.
// This is automatically called before every 'Run'.
func (sim *Simulation) reset() {
if sim.Encounter.DurationIsEstimate && sim.CurrentTime != 0 {
sim.BaseDuration = sim.CurrentTime
sim.Encounter.DurationIsEstimate = false
}
sim.Duration = sim.BaseDuration
if sim.DurationVariation != 0 {
variation := sim.DurationVariation * 2
sim.Duration += time.Duration(sim.RandomFloat("sim duration")*float64(variation)) - sim.DurationVariation
}
sim.pendingActions = sim.pendingActions[:0]
sim.pendingActions = append(sim.pendingActions, sentinelPendingAction)
sim.executePhase = 0
sim.nextExecutePhase()
sim.executePhaseCallbacks = nil
// Use duration as an end check if not using health.
sim.endOfCombatDuration = sim.Duration
sim.endOfCombatDamage = math.MaxFloat64
if sim.Encounter.EndFightAtHealth > 0 {
sim.endOfCombatDuration = NeverExpires
sim.endOfCombatDamage = sim.Encounter.EndFightAtHealth
}
sim.CurrentTime = 0
sim.trackers = sim.trackers[:0]
sim.minTrackerTime = NeverExpires
sim.weaponAttacks = sim.weaponAttacks[:0]
sim.minWeaponAttackTime = NeverExpires
sim.tasks = sim.tasks[:0]
sim.minTaskTime = NeverExpires
sim.Environment.reset(sim)
sim.initManaTickAction()
}
func (sim *Simulation) PrePull() {
if len(sim.prepullActions) > 0 {
sim.CurrentTime = sim.prepullActions[0].DoAt
for i, ppa := range sim.prepullActions {
sim.AddPendingAction(&PendingAction{
NextActionAt: ppa.DoAt,
Priority: ActionPriorityPrePull + ActionPriority(len(sim.prepullActions)-i),
OnAction: ppa.Action,
})
}
}
sim.AddPendingAction(&PendingAction{
NextActionAt: 0,
Priority: ActionPriorityPrePull,
OnAction: func(sim *Simulation) {
for _, unit := range sim.Environment.AllUnits {
if unit.enabled {
unit.startPull(sim)
}
}
},
})
}
func (sim *Simulation) Cleanup() {
// The last event loop will leave CurrentTime at some value close to but not
// quite at the Duration. Explicitly set this so that accesses to CurrentTime
// during the doneIteration phase will return the Duration value, which is
// intuitive.
sim.CurrentTime = sim.Duration
for _, pa := range sim.pendingActions {
if pa.CleanUp != nil {
pa.CleanUp(sim)
}
}
sim.Raid.doneIteration(sim)
sim.Encounter.doneIteration(sim)
for _, unit := range sim.Raid.AllUnits {
unit.Metrics.doneIteration(unit, sim)
}
for _, target := range sim.Encounter.TargetUnits {
target.Metrics.doneIteration(target, sim)
}
}
func (sim *Simulation) runPendingActions() {
for {
if finished := sim.Step(); finished {
return
}
}
}
func (sim *Simulation) Step() bool {
last := len(sim.pendingActions) - 1
pa := sim.pendingActions[last]
if pa.NextActionAt >= sim.minWeaponAttackTime && sim.minWeaponAttackTime <= sim.minTaskTime {
if sim.minWeaponAttackTime > sim.endOfCombatDuration || sim.Encounter.DamageTaken > sim.endOfCombatDamage {
return true
}
sim.advanceWeaponAttacks()
return false
}
if pa.NextActionAt >= sim.minTaskTime {
if sim.minTaskTime > sim.endOfCombatDuration || sim.Encounter.DamageTaken > sim.endOfCombatDamage {
return true
}
sim.advanceTasks()
return false
}
sim.pendingActions = sim.pendingActions[:last]
if pa.cancelled {
return false
}
if pa.NextActionAt > sim.endOfCombatDuration || sim.Encounter.DamageTaken > sim.endOfCombatDamage {
return true
}
if pa.NextActionAt > sim.CurrentTime {
sim.advance(pa.NextActionAt)
}
pa.consumed = true
if pa.cancelled {
return false
}
pa.OnAction(sim)
return false
}
func (sim *Simulation) advanceWeaponAttacks() {
if sim.minWeaponAttackTime > sim.CurrentTime {
sim.advance(sim.minWeaponAttackTime)
}
sim.minWeaponAttackTime = NeverExpires
for _, wa := range sim.weaponAttacks {
sim.minWeaponAttackTime = min(sim.minWeaponAttackTime, wa.trySwing(sim))
}
}
func (sim *Simulation) advanceTasks() {
if sim.minTaskTime > sim.CurrentTime {
sim.advance(sim.minTaskTime)
}
sim.minTaskTime = NeverExpires
for _, t := range sim.tasks {
sim.minTaskTime = min(sim.minTaskTime, t.RunTask(sim)) // RunTask() might alter sim.tasks
}
}
// Advance moves time forward counting down auras, CDs, mana regen, etc
func (sim *Simulation) advance(nextTime time.Duration) {
sim.CurrentTime = nextTime
// this is a loop to handle duplicate ExecuteProportions, e.g. if they're all set to 100%, you reach
// execute phases 35%, 25%, and 20% in the first advance() call.
for sim.CurrentTime >= sim.nextExecuteDuration || sim.Encounter.DamageTaken >= sim.nextExecuteDamage {
sim.nextExecutePhase()
for _, callback := range sim.executePhaseCallbacks {
callback(sim, sim.executePhase)
}
}
if sim.CurrentTime >= sim.minTrackerTime {
sim.minTrackerTime = NeverExpires
for _, t := range sim.trackers {
sim.minTrackerTime = min(sim.minTrackerTime, t.tryAdvance(sim))
}
}
}
// nextExecutePhase updates nextExecuteDuration and nextExecuteDamage based on executePhase.
func (sim *Simulation) nextExecutePhase() {
setup := func(phase int32, damage float64, health float64) {
sim.executePhase = phase
if sim.Encounter.EndFightAtHealth > 0 {
sim.nextExecuteDamage = (1 - damage) * sim.Encounter.EndFightAtHealth
} else {
sim.nextExecuteDuration = time.Duration((1 - health) * float64(sim.Duration))
}
}
sim.nextExecuteDuration = NeverExpires
sim.nextExecuteDamage = math.MaxFloat64
switch sim.executePhase {
case 0: // reset, waiting for 35%
setup(100, 0.35, sim.Encounter.ExecuteProportion_35)
case 100: // at 35%, waiting for 25%
setup(35, 0.25, sim.Encounter.ExecuteProportion_25)
case 35: // at 25%, waiting for 20%
setup(25, 0.20, sim.Encounter.ExecuteProportion_20)
case 25: // at 20%, done waiting
sim.executePhase = 20 // could also be used for end of fight handling
default:
panic(fmt.Sprintf("executePhase = %d invalid", sim.executePhase))
}
}
func (sim *Simulation) AddPendingAction(pa *PendingAction) {
//if pa.NextActionAt < sim.CurrentTime {
// panic(fmt.Sprintf("Cant add action in the past: %s", pa.NextActionAt))
//}
pa.consumed = false
for index, v := range sim.pendingActions[1:] {
if v.NextActionAt < pa.NextActionAt || (v.NextActionAt == pa.NextActionAt && v.Priority >= pa.Priority) {
//if sim.Log != nil {
// sim.Log("Adding action at index %d for time %s", index - len(sim.pendingActions), pa.NextActionAt)
// for i := index; i < len(sim.pendingActions); i++ {
// sim.Log("Upcoming action at %s", sim.pendingActions[i].NextActionAt)
// }
//}
sim.pendingActions = append(sim.pendingActions, pa)
copy(sim.pendingActions[index+2:], sim.pendingActions[index+1:])
sim.pendingActions[index+1] = pa
return
}
}
//if sim.Log != nil {
// sim.Log("Adding action at end for time %s", pa.NextActionAt)
//}
sim.pendingActions = append(sim.pendingActions, pa)
}
func (sim *Simulation) RegisterExecutePhaseCallback(callback func(sim *Simulation, isExecute int32)) {
sim.executePhaseCallbacks = append(sim.executePhaseCallbacks, callback)
}
func (sim *Simulation) IsExecutePhase20() bool {
return sim.executePhase <= 20
}
func (sim *Simulation) IsExecutePhase25() bool {
return sim.executePhase <= 25
}
func (sim *Simulation) IsExecutePhase35() bool {
return sim.executePhase <= 35
}
func (sim *Simulation) GetRemainingDuration() time.Duration {
if sim.Encounter.EndFightAtHealth > 0 {
if !sim.Encounter.DurationIsEstimate || sim.CurrentTime < time.Second*5 {
return sim.Duration - sim.CurrentTime
}
// Estimate time remaining via avg dps
dps := sim.Encounter.DamageTaken / sim.CurrentTime.Seconds()
dur := time.Duration((sim.Encounter.EndFightAtHealth-sim.Encounter.DamageTaken)/dps) * time.Second
return dur
}
return sim.Duration - sim.CurrentTime
}
// Returns the percentage of time remaining in the current iteration, as a value from 0-1.
func (sim *Simulation) GetRemainingDurationPercent() float64 {
if sim.Encounter.EndFightAtHealth > 0 {
return 1.0 - sim.Encounter.DamageTaken/sim.Encounter.EndFightAtHealth
}
return float64(sim.Duration-sim.CurrentTime) / float64(sim.Duration)
}