forked from Sidoine/Ovale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStance.lua
297 lines (263 loc) · 8.52 KB
/
Stance.lua
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
--[[--------------------------------------------------------------------
Copyright (C) 2013, 2014, 2015 Johnny C. Lam.
See the file LICENSE.txt for copying permission.
--]]--------------------------------------------------------------------
-- This addon tracks the player's current stance.
local OVALE, Ovale = ...
local OvaleStance = Ovale:NewModule("OvaleStance", "AceEvent-3.0")
Ovale.OvaleStance = OvaleStance
--<private-static-properties>
local L = Ovale.L
local OvaleDebug = Ovale.OvaleDebug
local OvaleProfiler = Ovale.OvaleProfiler
-- Forward declarations for module dependencies.
local OvaleData = nil
local OvaleState = nil
local ipairs = ipairs
local pairs = pairs
local substr = string.sub
local tconcat = table.concat
local tinsert = table.insert
local tonumber = tonumber
local tsort = table.sort
local type = type
local wipe = wipe
local API_GetNumShapeshiftForms = GetNumShapeshiftForms
local API_GetShapeshiftForm = GetShapeshiftForm
local API_GetShapeshiftFormInfo = GetShapeshiftFormInfo
local API_GetSpellInfo = GetSpellInfo
-- Register for profiling.
OvaleProfiler:RegisterProfiling(OvaleStance)
local SPELL_NAME_TO_STANCE = {
-- Druid
[API_GetSpellInfo( 768)] = "druid_cat_form",
[API_GetSpellInfo( 783)] = "druid_travel_form",
[API_GetSpellInfo( 1066)] = "druid_aquatic_form",
[API_GetSpellInfo( 5487)] = "druid_bear_form",
[API_GetSpellInfo( 24858)] = "druid_moonkin_form",
[API_GetSpellInfo( 33943)] = "druid_flight_form",
[API_GetSpellInfo( 40120)] = "druid_swift_flight_form",
[API_GetSpellInfo(171745)] = "druid_claws_of_shirvallah",
-- Rogue
[API_GetSpellInfo( 1784)] = "rogue_stealth"
}
-- Table of all valid stance names.
local STANCE_NAME = {}
do
for _, name in pairs(SPELL_NAME_TO_STANCE) do
STANCE_NAME[name] = true
end
end
do
local debugOptions = {
stance = {
name = L["Stances"],
type = "group",
args = {
stance = {
name = L["Stances"],
type = "input",
multiline = 25,
width = "full",
get = function(info) return OvaleStance:DebugStances() end,
},
},
},
}
-- Insert debug options into OvaleDebug.
for k, v in pairs(debugOptions) do
OvaleDebug.options.args[k] = v
end
end
--</private-static-properties>
--<public-static-properties>
-- Whether the stance information is ready for use by other modules.
OvaleStance.ready = false
-- List of available stances, populated by CreateStanceList()
OvaleStance.stanceList = {}
-- Map stance names to stance ID (index on shapeshift/stance bar).
OvaleStance.stanceId = {}
-- Player's current stance.
OvaleStance.stance = nil
-- Table of all valid stance names.
OvaleStance.STANCE_NAME = STANCE_NAME
--</public-static-properties>
--<public-static-methods>
function OvaleStance:OnInitialize()
-- Resolve module dependencies.
OvaleData = Ovale.OvaleData
OvaleState = Ovale.OvaleState
end
function OvaleStance:OnEnable()
self:RegisterEvent("PLAYER_ENTERING_WORLD", "UpdateStances")
self:RegisterEvent("UPDATE_SHAPESHIFT_FORM")
self:RegisterEvent("UPDATE_SHAPESHIFT_FORMS")
self:RegisterMessage("Ovale_SpellsChanged", "UpdateStances")
self:RegisterMessage("Ovale_TalentsChanged", "UpdateStances")
OvaleData:RegisterRequirement("stance", "RequireStanceHandler", self)
OvaleState:RegisterState(self, self.statePrototype)
end
function OvaleStance:OnDisable()
OvaleState:UnregisterState(self)
OvaleData:UnregisterRequirement("stance")
self:UnregisterEvent("PLAYER_ALIVE")
self:UnregisterEvent("PLAYER_ENTERING_WORLD")
self:UnregisterEvent("UPDATE_SHAPESHIFT_FORM")
self:UnregisterEvent("UPDATE_SHAPESHIFT_FORMS")
self:UnregisterMessage("Ovale_SpellsChanged")
self:UnregisterMessage("Ovale_TalentsChanged")
end
function OvaleStance:PLAYER_TALENT_UPDATE(event)
-- Clear old stance ID since talent update may overwrite old stance with new one with same ID.
self.stance = nil
self:UpdateStances()
end
function OvaleStance:UPDATE_SHAPESHIFT_FORM(event)
self:ShapeshiftEventHandler()
end
function OvaleStance:UPDATE_SHAPESHIFT_FORMS(event)
self:ShapeshiftEventHandler()
end
-- Fill OvaleStance.stanceList with stance bar index <-> Ovale stance name mappings.
function OvaleStance:CreateStanceList()
self:StartProfiling("OvaleStance_CreateStanceList")
wipe(self.stanceList)
wipe(self.stanceId)
local _, name, stanceName
for i = 1, API_GetNumShapeshiftForms() do
_, name = API_GetShapeshiftFormInfo(i)
stanceName = SPELL_NAME_TO_STANCE[name]
if stanceName then
self.stanceList[i] = stanceName
self.stanceId[stanceName] = i
end
end
self:StopProfiling("OvaleStance_CreateStanceList")
end
-- Print out the list of stances in alphabetical order.
do
local array = {}
function OvaleStance:DebugStances()
wipe(array)
for k, v in pairs(self.stanceList) do
if self.stance == k then
tinsert(array, v .. " (active)")
else
tinsert(array, v)
end
end
tsort(array)
return tconcat(array, "\n")
end
end
-- Return the name of the given stance or the current stance.
function OvaleStance:GetStance(stanceId)
stanceId = stanceId or self.stance
return self.stanceList[stanceId]
end
-- Return true if the current stance matches the given name.
-- NOTE: Mirrored in statePrototype below.
function OvaleStance:IsStance(name)
if name and self.stance then
if type(name) == "number" then
return name == self.stance
else
return name == OvaleStance:GetStance(self.stance)
end
end
return false
end
function OvaleStance:IsStanceSpell(spellId)
local name = API_GetSpellInfo(spellId)
return not not (name and SPELL_NAME_TO_STANCE[name])
end
function OvaleStance:ShapeshiftEventHandler()
self:StartProfiling("OvaleStance_ShapeshiftEventHandler")
local oldStance = self.stance
local newStance = API_GetShapeshiftForm()
if oldStance ~= newStance then
self.stance = newStance
Ovale.refreshNeeded[Ovale.playerGUID] = true
self:SendMessage("Ovale_StanceChanged", self:GetStance(newStance), self:GetStance(oldStance))
end
self:StopProfiling("OvaleStance_ShapeshiftEventHandler")
end
function OvaleStance:UpdateStances()
self:CreateStanceList()
self:ShapeshiftEventHandler()
self.ready = true
end
-- Run-time check that the player is in a certain stance.
-- NOTE: Mirrored in statePrototype below.
function OvaleStance:RequireStanceHandler(spellId, atTime, requirement, tokens, index, targetGUID)
local verified = false
-- If index isn't given, then tokens holds the actual token value.
local stance = tokens
if index then
stance = tokens[index]
index = index + 1
end
if stance then
local isBang = false
if substr(stance, 1, 1) == "!" then
isBang = true
stance = substr(stance, 2)
end
stance = tonumber(stance) or stance
local isStance = self:IsStance(stance)
if not isBang and isStance or isBang and not isStance then
verified = true
end
local result = verified and "passed" or "FAILED"
if isBang then
self:Log(" Require NOT stance '%s': %s", stance, result)
else
self:Log(" Require stance '%s': %s", stance, result)
end
else
Ovale:OneTimeMessage("Warning: requirement '%s' is missing a stance argument.", requirement)
end
return verified, requirement, index
end
--</public-static-methods>
--[[----------------------------------------------------------------------------
State machine for simulator.
--]]----------------------------------------------------------------------------
--<public-static-properties>
OvaleStance.statePrototype = {}
--</public-static-properties>
--<private-static-properties>
local statePrototype = OvaleStance.statePrototype
--</private-static-properties>
--<state-properties>
statePrototype.stance = nil
--</state-properties>
--<public-static-methods>
-- Initialize the state.
function OvaleStance:InitializeState(state)
state.stance = nil
end
-- Reset the state to the current conditions.
function OvaleStance:ResetState(state)
self:StartProfiling("OvaleStance_ResetState")
state.stance = self.stance or 0
self:StopProfiling("OvaleStance_ResetState")
end
-- Apply the effects of the spell on the player's state, assuming the spellcast completes.
function OvaleStance:ApplySpellAfterCast(state, spellId, targetGUID, startCast, endCast, isChanneled, spellcast)
self:StartProfiling("OvaleStance_ApplySpellAfterCast")
local stance = state:GetSpellInfoProperty(spellId, endCast, "to_stance", targetGUID)
if stance then
if type(stance) == "string" then
stance = self.stanceId[stance]
end
state.stance = stance
end
self:StopProfiling("OvaleStance_ApplySpellAfterCast")
end
--</public-static-methods>
--<state-methods>
-- Mirrored methods.
statePrototype.IsStance = OvaleStance.IsStance
statePrototype.RequireStanceHandler = OvaleStance.RequireStanceHandler
--</state-methods>