Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vigo/faster advance #3877

Merged
merged 3 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions sim/core/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (aura *Aura) Refresh(sim *Simulation) {
aura.expires = sim.CurrentTime + aura.Duration
if aura.expires < aura.Unit.minExpires {
aura.Unit.minExpires = aura.expires
sim.rescheduleTracker(aura.expires)
}
}
}
Expand Down Expand Up @@ -474,28 +475,28 @@ func (at *auraTracker) reset(sim *Simulation) {
resetEffect(sim)
}

at.minExpires = NeverExpires

for _, aura := range at.auras {
aura.reset(sim)
}
}

func (at *auraTracker) advance(sim *Simulation) {
func (at *auraTracker) advance(sim *Simulation) time.Duration {
if at.minExpires > sim.CurrentTime {
return
return at.minExpires
}

restart:
minExpires := NeverExpires
at.minExpires = NeverExpires
for _, aura := range at.activeAuras {
if aura.expires <= sim.CurrentTime && aura.expires != 0 {
aura.Deactivate(sim)
goto restart // activeAuras have changed
}
if aura.expires < minExpires {
minExpires = aura.expires
}
at.minExpires = min(at.minExpires, aura.expires)
}
at.minExpires = minExpires
return at.minExpires
}

func (at *auraTracker) doneIteration(sim *Simulation) {
Expand Down
2 changes: 1 addition & 1 deletion sim/core/aura_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (character *Character) NewTemporaryStatsAuraWrapped(auraLabel string, actio
if sim.Log != nil {
character.Log(sim, "Lost %s from fading %s.", buffs.FlatString(), actionID)
}
character.AddStatsDynamic(sim, buffs.Multiply(-1))
character.AddStatsDynamic(sim, buffs.Invert())
},
}

Expand Down
15 changes: 0 additions & 15 deletions sim/core/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,6 @@ func (character *Character) FillPlayerStats(playerStats *proto.PlayerStats) {
}
}

func (character *Character) init(sim *Simulation) {
character.Unit.init(sim)
}

func (character *Character) reset(sim *Simulation, agent Agent) {
character.Unit.reset(sim, agent)
character.majorCooldownManager.reset(sim)
Expand All @@ -512,17 +508,6 @@ func (character *Character) reset(sim *Simulation, agent Agent) {
}
}

// Advance moves time forward counting down auras, CDs, mana regen, etc
func (character *Character) advance(sim *Simulation) {
character.Unit.advance(sim)

for _, pet := range character.Pets {
if pet.enabled {
pet.Unit.advance(sim)
}
}
}

func (character *Character) HasProfession(prof proto.Profession) bool {
return prof == character.professions[0] || prof == character.professions[1]
}
Expand Down
2 changes: 1 addition & 1 deletion sim/core/debuffs.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ func apReductionEffect(aura *Aura, apReduction float64) *ExclusiveEffect {
ee.Aura.Unit.AddStatsDynamic(sim, statReduction)
},
OnExpire: func(ee *ExclusiveEffect, sim *Simulation) {
ee.Aura.Unit.AddStatsDynamic(sim, statReduction.Multiply(-1))
ee.Aura.Unit.AddStatsDynamic(sim, statReduction.Invert())
},
})
}
Expand Down
15 changes: 6 additions & 9 deletions sim/core/pet.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ func (pet *Pet) reset(sim *Simulation, agent PetAgent) {
pet.Enable(sim, agent)
}
}
func (pet *Pet) advance(sim *Simulation) {
pet.Character.advance(sim)
}

func (pet *Pet) doneIteration(sim *Simulation) {
pet.Character.doneIteration(sim)
pet.isReset = false
Expand Down Expand Up @@ -172,6 +168,8 @@ func (pet *Pet) Enable(sim *Simulation, petAgent PetAgent) {
pet.Log(sim, "Pet inherited stats: %s", pet.ApplyStatDependencies(pet.inheritedStats))
pet.Log(sim, "Pet summoned")
}

sim.addTracker(&pet.auraTracker)
}

// Helper for enabling a pet that will expire after a certain duration.
Expand Down Expand Up @@ -213,7 +211,7 @@ func (pet *Pet) Disable(sim *Simulation) {

// Remove inherited stats on dismiss if not permanent
if pet.isGuardian || pet.timeoutAction != nil {
pet.AddStatsDynamic(sim, pet.inheritedStats.Multiply(-1))
pet.AddStatsDynamic(sim, pet.inheritedStats.Invert())
pet.inheritedStats = stats.Stats{}
}

Expand Down Expand Up @@ -249,12 +247,11 @@ func (pet *Pet) Disable(sim *Simulation) {
pet.OnPetDisable(sim)
}

sim.removeTracker(&pet.auraTracker)

if sim.Log != nil {
pet.Log(sim, "Pet dismissed")

if sim.Log != nil {
pet.Log(sim, pet.GetStats().String())
}
pet.Log(sim, pet.GetStats().String())
}
}

Expand Down
33 changes: 27 additions & 6 deletions sim/core/sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"runtime"
"runtime/debug"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -42,6 +43,24 @@ type Simulation struct {

nextExecuteDuration time.Duration
nextExecuteDamage float64

minTrackerTime time.Duration
trackers []*auraTracker
}

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 RunSim(rsr *proto.RaidSimRequest, progress chan *proto.ProgressMetrics) *proto.RaidSimResult {
Expand Down Expand Up @@ -337,6 +356,9 @@ func (sim *Simulation) reset() {

sim.CurrentTime = 0

sim.trackers = sim.trackers[:0]
sim.minTrackerTime = NeverExpires

sim.Environment.reset(sim)

sim.initManaTickAction()
Expand Down Expand Up @@ -502,12 +524,11 @@ func (sim *Simulation) advance(nextTime time.Duration) {
}
}

for _, character := range sim.characters {
character.advance(sim)
}

for _, target := range sim.Encounter.Targets {
target.Advance(sim)
if sim.CurrentTime >= sim.minTrackerTime {
sim.minTrackerTime = sim.trackers[0].advance(sim)
for _, at := range sim.trackers[1:] {
sim.minTrackerTime = min(sim.minTrackerTime, at.advance(sim))
}
}
}

Expand Down
44 changes: 21 additions & 23 deletions sim/core/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,20 @@ func (s Stat) StatName() string {
}

func FromFloatArray(values []float64) Stats {
stats := Stats{}
var stats Stats
copy(stats[:], values)
return stats
}

// Adds two Stats together, returning the new Stats.
func (stats Stats) Add(other Stats) Stats {
var newStats Stats
for k, v := range stats {
newStats[k] = v + other[k]
for k := range stats {
stats[k] += other[k]
}
return newStats
return stats
}

// Adds another to Stats to this, in-place.
// Adds another to Stats to this, in-place. For performance, only.
func (stats *Stats) AddInplace(other *Stats) {
for k := range stats {
stats[k] += other[k]
Expand All @@ -206,38 +205,37 @@ func (stats *Stats) AddInplace(other *Stats) {

// Subtracts another Stats from this one, returning the new Stats.
func (stats Stats) Subtract(other Stats) Stats {
var newStats Stats
for k := range stats {
stats[k] -= other[k]
}
return stats
}

func (stats Stats) Invert() Stats {
for k, v := range stats {
newStats[k] = v - other[k]
stats[k] = -v
}
return newStats
return stats
}

func (stats Stats) Multiply(multiplier float64) Stats {
var newStats Stats
for k, v := range stats {
newStats[k] = v * multiplier
for k := range stats {
stats[k] *= multiplier
}
return newStats
return stats
}

// Multiplies two Stats together by multiplying the values of corresponding
// stats, like a dot product operation.
func (stats Stats) DotProduct(other Stats) Stats {
var newStats Stats
for k, v := range stats {
newStats[k] = v * other[k]
for k := range stats {
stats[k] *= other[k]
}
return newStats
return stats
}

func (stats Stats) Equals(other Stats) bool {
for k, v := range stats {
if v != other[k] {
return false
}
}
return true
return stats == other
}

func (stats Stats) EqualsWithTolerance(other Stats, tolerance float64) bool {
Expand Down
16 changes: 0 additions & 16 deletions sim/core/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,6 @@ func NewTarget(options *proto.Target, targetIndex int32) *Target {
return target
}

func (target *Target) finalize() {
target.Unit.finalize()
}

func (target *Target) init(sim *Simulation) {
target.Unit.init(sim)
}

func (target *Target) Reset(sim *Simulation) {
target.Unit.reset(sim, nil)
target.SetGCDTimer(sim, 0)
Expand All @@ -176,14 +168,6 @@ func (target *Target) Reset(sim *Simulation) {
}
}

func (target *Target) Advance(sim *Simulation) {
target.Unit.advance(sim)
}

func (target *Target) doneIteration(sim *Simulation) {
target.Unit.doneIteration(sim)
}

func (target *Target) NextTarget() *Target {
nextIndex := target.Index + 1
if nextIndex >= target.Env.GetNumTargets() {
Expand Down
8 changes: 4 additions & 4 deletions sim/core/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ func (unit *Unit) reset(sim *Simulation, _ Agent) {

unit.DynamicStatsPets = unit.DynamicStatsPets[:0]
unit.DynamicMeleeSpeedPets = unit.DynamicMeleeSpeedPets[:0]

if unit.Type != PetUnit {
sim.addTracker(&unit.auraTracker)
}
}

func (unit *Unit) startPull(sim *Simulation) {
Expand All @@ -488,10 +492,6 @@ func (unit *Unit) startPull(sim *Simulation) {
}

// Advance moves time forward counting down auras, and nothing else, currently.
func (unit *Unit) advance(sim *Simulation) {
unit.auraTracker.advance(sim)
}

func (unit *Unit) doneIteration(sim *Simulation) {
unit.Hardcast = Hardcast{}
unit.doneIterationGCD(sim)
Expand Down
Loading
Loading