-
Notifications
You must be signed in to change notification settings - Fork 28
/
apl.go
295 lines (251 loc) · 8.9 KB
/
apl.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
package core
import (
"fmt"
"time"
"github.com/wowsims/sod/sim/core/proto"
"google.golang.org/protobuf/encoding/protojson"
)
type APLRotation struct {
unit *Unit
prepullActions []*APLAction
priorityList []*APLAction
// Action currently controlling this rotation (only used for certain actions, such as StrictSequence).
controllingActions []APLActionImpl
// Value that should evaluate to 'true' if the current channel is to be interrupted.
// Will be nil when there is no active channel.
interruptChannelIf APLValue
// If true, can recast channel when interrupted.
allowChannelRecastOnInterrupt bool
// Used inside of actions/value to determine whether they will occur during the prepull or regular rotation.
parsingPrepull bool
// Used to avoid recursive APL loops.
inLoop bool
// Validation warnings that occur during proto parsing.
// We return these back to the user for display in the UI.
curWarnings []string
prepullWarnings [][]string
priorityListWarnings [][]string
}
func (rot *APLRotation) ValidationWarning(message string, vals ...interface{}) {
warning := fmt.Sprintf(message, vals...)
rot.curWarnings = append(rot.curWarnings, warning)
}
// Invokes the fn function, and attributes all warnings generated during its invocation
// to the provided warningsList.
func (rot *APLRotation) doAndRecordWarnings(warningsList *[]string, isPrepull bool, fn func()) {
rot.parsingPrepull = isPrepull
fn()
if warningsList != nil {
*warningsList = append(*warningsList, rot.curWarnings...)
}
rot.curWarnings = nil
rot.parsingPrepull = false
}
func (unit *Unit) newCustomRotation() *APLRotation {
return unit.newAPLRotation(&proto.APLRotation{
Type: proto.APLRotation_TypeAPL,
PriorityList: []*proto.APLListItem{
{
Action: &proto.APLAction{
Action: &proto.APLAction_CustomRotation{},
},
},
},
})
}
func (unit *Unit) newAPLRotation(config *proto.APLRotation) *APLRotation {
if config == nil {
return nil
}
rotation := &APLRotation{
unit: unit,
prepullWarnings: make([][]string, len(config.PrepullActions)),
priorityListWarnings: make([][]string, len(config.PriorityList)),
}
// Parse prepull actions
for i, prepullItem := range config.PrepullActions {
prepullIdx := i // Save to local variable for correct lambda capture behavior
rotation.doAndRecordWarnings(&rotation.prepullWarnings[prepullIdx], true, func() {
if !prepullItem.Hide {
doAtVal := rotation.newAPLValue(prepullItem.DoAtValue)
if doAtVal != nil {
doAt := doAtVal.GetDuration(nil)
if doAt > 0 {
rotation.ValidationWarning("Invalid time for 'Do At', ignoring this Prepull Action")
} else {
action := rotation.newAPLAction(prepullItem.Action)
if action != nil {
rotation.prepullActions = append(rotation.prepullActions, action)
unit.RegisterPrepullAction(doAt, func(sim *Simulation) {
// Warnings for prepull cast failure are detected by running a fake prepull,
// so this action.Execute needs to record warnings.
rotation.doAndRecordWarnings(&rotation.prepullWarnings[prepullIdx], true, func() {
action.Execute(sim)
})
})
}
}
}
}
})
}
// Parse priority list
var configIdxs []int
for i, aplItem := range config.PriorityList {
rotation.doAndRecordWarnings(&rotation.priorityListWarnings[i], false, func() {
if !aplItem.Hide {
action := rotation.newAPLAction(aplItem.Action)
if action != nil {
rotation.priorityList = append(rotation.priorityList, action)
configIdxs = append(configIdxs, i)
}
}
})
}
// Finalize
for i, action := range rotation.prepullActions {
rotation.doAndRecordWarnings(&rotation.prepullWarnings[i], true, func() {
action.Finalize(rotation)
})
}
for i, action := range rotation.priorityList {
rotation.doAndRecordWarnings(&rotation.priorityListWarnings[i], false, func() {
action.Finalize(rotation)
})
}
// Remove MCDs that are referenced by APL actions, so that the Autocast Other Cooldowns
// action does not include them.
agent := unit.Env.GetAgentFromUnit(unit)
if agent != nil {
character := agent.GetCharacter()
for _, action := range rotation.allAPLActions() {
if castSpellAction, ok := action.impl.(*APLActionCastSpell); ok {
character.removeInitialMajorCooldown(castSpellAction.spell.ActionID)
}
}
}
// If user has a Prepull potion set but does not use it in their APL settings, we enable it here.
rotation.doAndRecordWarnings(nil, true, func() {
prepotSpell := rotation.GetAPLSpell(ActionID{OtherID: proto.OtherAction_OtherActionPotion}.ToProto())
if prepotSpell != nil {
found := false
for _, prepullAction := range rotation.allPrepullActions() {
if castSpellAction, ok := prepullAction.impl.(*APLActionCastSpell); ok &&
(castSpellAction.spell == prepotSpell || castSpellAction.spell.Flags.Matches(SpellFlagPotion)) {
found = true
}
}
if !found {
unit.RegisterPrepullAction(-1*time.Second, func(sim *Simulation) {
prepotSpell.Cast(sim, nil)
})
}
}
})
return rotation
}
func (rot *APLRotation) getStats() *proto.APLStats {
return &proto.APLStats{
PrepullActions: MapSlice(rot.prepullWarnings, func(warnings []string) *proto.APLActionStats { return &proto.APLActionStats{Warnings: warnings} }),
PriorityList: MapSlice(rot.priorityListWarnings, func(warnings []string) *proto.APLActionStats { return &proto.APLActionStats{Warnings: warnings} }),
}
}
// Returns all action objects as an unstructured list. Used for easily finding specific actions.
func (rot *APLRotation) allAPLActions() []*APLAction {
return Flatten(MapSlice(rot.priorityList, func(action *APLAction) []*APLAction { return action.GetAllActions() }))
}
// Returns all action objects from the prepull as an unstructured list. Used for easily finding specific actions.
func (rot *APLRotation) allPrepullActions() []*APLAction {
return Flatten(MapSlice(rot.prepullActions, func(action *APLAction) []*APLAction { return action.GetAllActions() }))
}
func (rot *APLRotation) reset(sim *Simulation) {
rot.controllingActions = nil
rot.inLoop = false
rot.interruptChannelIf = nil
rot.allowChannelRecastOnInterrupt = false
for _, action := range rot.allAPLActions() {
action.impl.Reset(sim)
}
}
// We intentionally try to mimic the behavior of simc APL to avoid confusion
// and leverage the community's existing familiarity.
// https://github.com/simulationcraft/simc/wiki/ActionLists
func (apl *APLRotation) DoNextAction(sim *Simulation) {
if sim.CurrentTime < 0 {
return
}
if apl.inLoop {
return
}
if apl.unit.ChanneledDot != nil {
return
}
i := 0
apl.inLoop = true
for nextAction := apl.getNextAction(sim); nextAction != nil; i, nextAction = i+1, apl.getNextAction(sim) {
if i > 1000 {
panic(fmt.Sprintf("[USER_ERROR] Infinite loop detected, current action:\n%s", nextAction))
}
nextAction.Execute(sim)
}
apl.inLoop = false
if sim.Log != nil && i == 0 {
apl.unit.Log(sim, "No available actions!")
}
gcdReady := apl.unit.GCD.IsReady(sim)
if gcdReady {
apl.unit.WaitUntil(sim, sim.CurrentTime+time.Millisecond*50)
}
}
func (apl *APLRotation) getNextAction(sim *Simulation) *APLAction {
if len(apl.controllingActions) != 0 {
return apl.controllingActions[len(apl.controllingActions)-1].GetNextAction(sim)
}
for _, action := range apl.priorityList {
if action.IsReady(sim) {
return action
}
}
return nil
}
func (apl *APLRotation) pushControllingAction(ca APLActionImpl) {
apl.controllingActions = append(apl.controllingActions, ca)
}
func (apl *APLRotation) popControllingAction(ca APLActionImpl) {
if len(apl.controllingActions) == 0 || apl.controllingActions[len(apl.controllingActions)-1] != ca {
panic("Wrong APL controllingAction in pop()")
}
apl.controllingActions = apl.controllingActions[:len(apl.controllingActions)-1]
}
func (apl *APLRotation) shouldInterruptChannel(sim *Simulation) bool {
channeledDot := apl.unit.ChanneledDot
if channeledDot == nil {
return false
}
if channeledDot.MaxTicksRemaining() == 0 {
// Channel has ended, but apl.unit.ChanneledDot hasn't been cleared yet meaning the aura is still active.
return false
}
if apl.interruptChannelIf == nil || !apl.interruptChannelIf.GetBool(sim) {
// Continue the channel.
return false
}
// Allow next action to interrupt the channel, but if the action is the same action then it still needs to continue.
nextAction := apl.getNextAction(sim)
if nextAction == nil {
return false
}
if channelAction, ok := nextAction.impl.(*APLActionChannelSpell); ok && channelAction.spell == channeledDot.Spell {
// Newly selected action is channeling the same spell, so continue the channel unless recast is allowed.
return apl.allowChannelRecastOnInterrupt
}
return true
}
func APLRotationFromJsonString(jsonString string) *proto.APLRotation {
apl := &proto.APLRotation{}
data := []byte(jsonString)
if err := protojson.Unmarshal(data, apl); err != nil {
panic(err)
}
return apl
}