forked from phuein/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowMount.lua
More file actions
331 lines (273 loc) · 11 KB
/
Copy pathShowMount.lua
File metadata and controls
331 lines (273 loc) · 11 KB
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
ShowMount = {
name = "ShowMount", -- Matches folder and Manifest file names.
author = "phuein & Sponsored by ShadowOFMen from Tamriel Crown Exchange",
color = "ffc235", -- Used in menu titles and so on.
menuName = "ShowMount ", -- UNIQUE identifier for menu object.
-- Default settings.
savedVariables = {
FirstLoad = true, -- First time ever that the addon is loaded.
-- Mount selection.
MountOne = 0,
MountTwo = 0,
MountThree = 0,
MountFour = 0,
EnableChatCmds = true,
},
-- Sounds.
soundWarn = SOUNDS.QUICKSLOT_USE_EMPTY, -- Maybe SOUNDS.LOCKPICKING_BREAK instead?
soundInfo = SOUNDS.QUEST_SHARED,
soundSet = SOUNDS.QUEST_FOCUSED,
soundReset = SOUNDS.QUEST_ABANDONED,
-- Populate unlocked mounts list for settings, every time. Lists all match item indexes.
mountsIds = {},
mountsNames = {},
mountsNamesMenu = {}, -- Without affecting indexes, first item is always "-".
mountsLinks = {},
mountsTextures = {},
-- Tracking.
mountCurrent = 0 -- Last activate mount by mount_id.
}
-- Convert int to str for mount slots.
ShowMount.Num2Num = {"One", "Two", "Three", "Four"}
-- Wraps text with a color.
function ShowMount.Colorize(text, color)
-- Default to addon's .color.
if not color then color = ShowMount.color end
text = "|c" .. color .. text .. "|r"
return text
end
-- Verbosity.
ShowMount.Instructions = ShowMount.Colorize("Press U", "ffa500") .. " to set an active mount," ..
" \nand then use " ..
ShowMount.Colorize("/qm1", "ffa500") ..
" and " ..
ShowMount.Colorize("/qm2", "ffa500") ..
" \nto set your Main and Secondary mounts."
ShowMount.NoneActive = "A mount must be Set Active first."
-- Or EsoStrings[SI_COLLECTIBLEUSAGEBLOCKREASON9] -> "This collectible is not ready yet."
ShowMount.CooldownMsg = ShowMount.Colorize("Can't switch mounts that fast.", "ffa500") -- Orange.
-- Returns item index from chosen list.
function ShowMount.GetIndex(list, item)
local t={}
for k,v in pairs(ShowMount[list]) do
t[v]=k
end
return t[item]
end
-- Returns true if cooldown time hasn't elapsed; Falsy otherwise.
-- t - os.time()
function ShowMount.CoolingDown(t)
if os.time() - t <= ShowMount.lastEventCooldown then return true end
end
-- Return an in-game texture format for images to display.
-- NOTE: This is specifically designed for these notifications.
-- Defualts to 100x100 pixels.
-- Newlines to avoid overlap with text or images.
-- f - Filename with path.
function ShowMount.FormatTexture(f)
return "\n\n\n|t128:128:" .. f .. "|t\n\n"
end
-- Populate the unlocked mounts list on demand.
function ShowMount.PopulateMounts()
-- Reset.
ShowMount.mountsIds = {}
ShowMount.mountsNames = {}
ShowMount.mountsNamesMenu = {"-"}
ShowMount.mountsLinks = {}
ShowMount.mountsTextures = {}
-- Stop when matched as many as are unlocked.
local maxMatches = GetTotalUnlockedCollectiblesByCategoryType(COLLECTIBLE_CATEGORY_TYPE_MOUNT)
local len = GetTotalCollectiblesByCategoryType(COLLECTIBLE_CATEGORY_TYPE_MOUNT)
for i=1,len do
if maxMatches == 0 then break end
-- Get mount_id from index.
local mount_id = GetCollectibleId(8, nil, i)
-- Add if unlocked.
if GetCollectibleUnlockStateById(mount_id) ~= 0 then
local index = #ShowMount.mountsIds+1
ShowMount.mountsIds[index] = mount_id
ShowMount.mountsNames[index] = GetCollectibleName(mount_id)
ShowMount.mountsLinks[index] = GetCollectibleLink(mount_id)
ShowMount.mountsTextures[index] = GetCollectibleIcon(mount_id)
-- For menu lists without affecting indexes.
ShowMount.mountsNamesMenu[#ShowMount.mountsNamesMenu+1] = GetCollectibleName(mount_id)
-- Count.
maxMatches = maxMatches - 1
end
end
end
-- Returns True and prints message,
-- if current active mount is still in cooldown and can't be switched.
function ShowMount.IsCooldown()
local msg
-- Current mount.
local mount_id = GetActiveCollectibleByType(COLLECTIBLE_CATEGORY_TYPE_MOUNT)
-- Don't flood Collectible activations before cooldown.
local cooldown, duration = GetCollectibleCooldownAndDuration(mount_id)
if duration > 0 then
-- Verbose.
msg = ShowMount.CooldownMsg
-- d(msg)
ZO_AlertNoSuppression(UI_ALERT_CATEGORY_ALERT, ShowMount.soundWarn, msg)
return true
end
end
-- Return a mount_id from vague selection.
function ShowMount.SelectMount(m)
local mount_id
if m == "Random" then
-- Random mount selection from unlocked mounts.
mount_id = ShowMount.mountCurrent
if #ShowMount.mountsIds == 0 then
-- If no mounts are unlocked.
mount_id = 0
elseif #ShowMount.mountsIds == 1 then
-- If only one mount is unlocked, always return it.
mount_id = ShowMount.mountsIds[1]
else
-- Loop until selecting a mount different from current.
local i
while mount_id == ShowMount.mountCurrent do
i = math.random(1, #ShowMount.mountsIds)
mount_id = ShowMount.mountsIds[i]
end
end
else
-- Specific selection from slot.
mount_id = ShowMount.savedVariables["Mount" .. m]
end
return mount_id
end
-- Set mount as active, if set by user.
-- m - "Random", or "One", "Two", ...
function ShowMount.ActivateMount(m)
local msg
-- Can't switch during cooldown. Prints message.
if ShowMount.IsCooldown() then return end
local mount_id = ShowMount.SelectMount(m)
-- Selection not available.
-- Either slot is empty or no mounts are unlocked.
-- NOTE: nil is not expected.
if mount_id == 0 or mount_id == nil then
-- Silent fail.
return
end
-- Switch.
UseCollectible(mount_id)
-- Track.
ShowMount.mountCurrent = mount_id
-- Verbose.
msg = "Active mount set to " .. GetCollectibleLink(mount_id) .. "."
-- d(msg)
msg = msg .. ShowMount.FormatTexture(GetCollectibleIcon(mount_id)) -- Add image.
ZO_AlertNoSuppression(UI_ALERT_CATEGORY_ALERT, ShowMount.soundInfo, msg)
end
-- Reference helper for SLASH_COMMAND.
function ShowMount.ActivateMountOne()
ShowMount.ActivateMount("One")
end
-- Reference helper for SLASH_COMMAND.
function ShowMount.ActivateMountTwo()
ShowMount.ActivateMount("Two")
end
-- Reference helper for SLASH_COMMAND.
function ShowMount.ActivateMountThree()
ShowMount.ActivateMount("Three")
end
-- Reference helper for SLASH_COMMAND.
function ShowMount.ActivateMountFour()
ShowMount.ActivateMount("Four")
end
-- Reference helper for SLASH_COMMAND.
function ShowMount.ActivateMountRandom()
ShowMount.ActivateMount("Random")
end
-- Set Mount slot to selection or active mount.
-- NOTE: 0 should not be a possible id for any available collection item in the game.
function ShowMount.SetMount(mount_num, mount_id)
local msg
local mount_id = mount_id or GetActiveCollectibleByType(COLLECTIBLE_CATEGORY_TYPE_MOUNT)
-- 0 means none is active.
-- The player either has none, or hadn't activate any, yet.
if mount_id == 0 then
-- Verbose.
msg = ShowMount.NoneActive
-- d(msg)
ZO_AlertNoSuppression(UI_ALERT_CATEGORY_ALERT, ShowMount.soundSet, msg)
return
end
ShowMount.savedVariables["Mount" .. mount_num] = mount_id
-- Verbose.
msg = "Mount " .. mount_num .. " set to " .. GetCollectibleLink(mount_id) .. "."
-- d(msg)
msg = msg .. ShowMount.FormatTexture(GetCollectibleIcon(mount_id)) -- Add image.
ZO_AlertNoSuppression(UI_ALERT_CATEGORY_ALERT, ShowMount.soundSet, msg)
end
-- Reset mount by number, or reset all mounts if nil.
function ShowMount.ResetMounts(mount_num)
local msg
if mount_num == nil then
-- Reset all.
ShowMount.savedVariables["MountOne"] = 0
ShowMount.savedVariables["MountTwo"] = 0
ShowMount.savedVariables["MountThree"] = 0
ShowMount.savedVariables["MountFour"] = 0
msg = "All mounts have been reset."
else
-- Reset selection.
ShowMount.savedVariables["Mount" .. mount_num] = 0
msg = "Mount " .. mount_num .. " has been reset."
end
-- Verbose.
-- d(msg)
ZO_AlertNoSuppression(UI_ALERT_CATEGORY_ALERT, ShowMount.soundReset, msg)
end
-- Enable or disable chat commands.
function ShowMount.EnableChatCommands(b)
ShowMount.savedVariables.EnableChatCmds = b
if b then
-- Slash commands must be lowercase. Set to nil to disable.
-- SLASH_COMMANDS["/sm"] = ShowMount.ToggleMount
-- NOTE: Shows selection menu instead.
SLASH_COMMANDS["/sm1"] = ShowMount.ActivateMountOne
SLASH_COMMANDS["/sm2"] = ShowMount.ActivateMountTwo
SLASH_COMMANDS["/sm3"] = ShowMount.ActivateMountThree
SLASH_COMMANDS["/sm4"] = ShowMount.ActivateMountFour
SLASH_COMMANDS["/smr"] = ShowMount.ActivateMountRandom
else
-- SLASH_COMMANDS["/sm"] = nil
SLASH_COMMANDS["/sm1"] = nil
SLASH_COMMANDS["/sm2"] = nil
SLASH_COMMANDS["/sm3"] = nil
SLASH_COMMANDS["/sm4"] = nil
SLASH_COMMANDS["/smr"] = nil
end
-- Reset autocomplete cache to update it.
SLASH_COMMAND_AUTO_COMPLETE:InvalidateSlashCommandCache()
end
-- Tell player how to use the addon on first load ever.
function ShowMount.Activated(e)
EVENT_MANAGER:UnregisterForEvent(ShowMount.name, EVENT_PLAYER_ACTIVATED)
if ShowMount.savedVariables.FirstLoad then
ShowMount.savedVariables.FirstLoad = false
local msg = ShowMount.Instructions
d(msg)
ZO_AlertNoSuppression(UI_ALERT_CATEGORY_ALERT, ShowMount.soundWarn, msg)
end
-- Update current mount.
ShowMount.mountCurrent = GetActiveCollectibleByType(COLLECTIBLE_CATEGORY_TYPE_MOUNT)
end
-- When player is ready, after everything has been loaded.
EVENT_MANAGER:RegisterForEvent(ShowMount.name, EVENT_PLAYER_ACTIVATED, ShowMount.Activated)
function ShowMount.OnAddOnLoaded(event, addonName)
if addonName ~= ShowMount.name then return end
EVENT_MANAGER:UnregisterForEvent(ShowMount.name, EVENT_ADD_ON_LOADED)
ShowMount.savedVariables = ZO_SavedVars:New("ShowMountSavedVariables", 1, nil, ShowMount.savedVariables)
-- Populate unlocked mounts list.
ShowMount.PopulateMounts()
-- Settings menu in Settings.lua.
ShowMount.LoadSettings()
ShowMount.EnableChatCommands(ShowMount.savedVariables.EnableChatCmds)
end
-- When any addon is loaded, but before UI (Chat) is loaded.
EVENT_MANAGER:RegisterForEvent(ShowMount.name, EVENT_ADD_ON_LOADED, ShowMount.OnAddOnLoaded)