Skip to content

Commit

Permalink
[core] simplify pre-pull
Browse files Browse the repository at this point in the history
  • Loading branch information
vigo2 committed Oct 17, 2023
1 parent 0726df5 commit 0a61e4c
Show file tree
Hide file tree
Showing 12 changed files with 994 additions and 1,005 deletions.
2 changes: 2 additions & 0 deletions sim/core/pending_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (

// DOTs need to be higher than anything else so that dots can properly expire before we take other actions.
ActionPriorityDOT ActionPriority = 3

ActionPriorityPrePull ActionPriority = 10
)

type PendingAction struct {
Expand Down
157 changes: 72 additions & 85 deletions sim/core/sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (sim *Simulation) run() *proto.RaidSimResult {
func (sim *Simulation) runOnce() {
sim.reset()
sim.PrePull()
sim.runPendingActions(NeverExpires)
sim.runPendingActions()
sim.Cleanup()
}

Expand Down Expand Up @@ -411,35 +411,28 @@ func (sim *Simulation) reset() {
sim.initManaTickAction()
}

func (sim *Simulation) runPrepullActions(max time.Duration) {
for {
if finished := len(sim.pendingActions) == 1 || sim.Step(max); finished {
return
}
}
}

func (sim *Simulation) PrePull() {
if len(sim.Environment.prepullActions) > 0 {
sim.CurrentTime = sim.Environment.PrepullStartTime()

for _, prepullAction := range sim.Environment.prepullActions {
if prepullAction.DoAt > sim.CurrentTime {
sim.runPrepullActions(prepullAction.DoAt)
sim.advance(prepullAction.DoAt)
}
prepullAction.Action(sim)
}

if sim.CurrentTime < 0 {
sim.runPrepullActions(0)
sim.advance(0)
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,
})
}
}

for _, unit := range sim.Environment.AllUnits {
unit.startPull(sim)
}
sim.AddPendingAction(&PendingAction{
NextActionAt: 0,
Priority: ActionPriorityPrePull,
OnAction: func(sim *Simulation) {
for _, unit := range sim.Environment.AllUnits {
unit.startPull(sim)
}
},
})
}

func (sim *Simulation) Cleanup() {
Expand All @@ -466,27 +459,15 @@ func (sim *Simulation) Cleanup() {
}
}

func (sim *Simulation) runPendingActions(max time.Duration) {
func (sim *Simulation) runPendingActions() {
for {
finished := sim.Step(max)
if finished {
if finished := sim.Step(); finished {
return
}
}
}

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
}
}

func (sim *Simulation) Step(max time.Duration) bool {
func (sim *Simulation) Step() bool {
last := len(sim.pendingActions) - 1
pa := sim.pendingActions[last]

Expand All @@ -508,12 +489,7 @@ func (sim *Simulation) Step(max time.Duration) bool {
}

if pa.NextActionAt > sim.CurrentTime {
if pa.NextActionAt < max {
sim.advance(pa.NextActionAt)
} else {
sim.pendingActions = append(sim.pendingActions, pa)
return true
}
sim.advance(pa.NextActionAt)
}
pa.consumed = true

Expand All @@ -524,29 +500,36 @@ func (sim *Simulation) Step(max time.Duration) bool {
return false
}

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
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.advance(sim))
}
}
//if sim.Log != nil {
// sim.Log("Adding action at end for time %s", pa.NextActionAt)
//}
sim.pendingActions = append(sim.pendingActions, pa)
}

// nextExecutePhase updates nextExecuteDuration and nextExecuteDamage based on executePhase.
Expand Down Expand Up @@ -577,25 +560,29 @@ func (sim *Simulation) nextExecutePhase() {
}
}

// 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.advance(sim))
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)) {
Expand Down
Loading

0 comments on commit 0a61e4c

Please sign in to comment.