forked from Hydra9268/ZGESO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.lua
More file actions
552 lines (489 loc) · 17.5 KB
/
Functions.lua
File metadata and controls
552 lines (489 loc) · 17.5 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
local ZGV = _G.ZGV
-----------------------------------------
-- LOCAL REFERENCES
-----------------------------------------
local tinsert, max, type, pairs, ipairs = table.insert, math.max, type, pairs, ipairs
-----------------------------------------
-- LOCAL VARIABLES
-----------------------------------------
local Utils = {}
-----------------------------------------
-- SAVED REFERENCES
-----------------------------------------
ZGV.Utils = Utils
-----------------------------------------
-- UTIL FUNCTIONS
-----------------------------------------
-- Usage: local obj = CHAIN(wm:CreateControl(...)) :SetPoint(...) :SetDimensions(...) .__END
-- https://wiki.esoui.com/Controls
-- .__END returns the object
function Utils.ChainCall(obj)
local T={}
setmetatable(T,{__index = function(self,fun)
if fun=="__END" then
return obj
end
return function(self,...)
assert(obj[fun],fun.." missing in object")
obj[fun](obj,...)
return self
end
end})
return T
end
function Utils.GetFaction(unitTag,novet,onlyvet)
unitTag = unitTag or "player"
if unitTag=="player" then
if ZGV.FAKE_FACTION then
return ZGV.FAKE_FACTION
end
if ZGV.VETERAN_FACTION and not novet then
return ZGV.VETERAN_FACTION
end
if onlyvet then return
ZGV.VETERAN_FACTION or "NOTVET"
end
end
local alliance = _G.GetUnitAlliance(unitTag)
if alliance == _G.ALLIANCE_ALDMERI_DOMINION then
return "AD"
elseif alliance == _G.ALLIANCE_EBONHEART_PACT then
return "EP"
elseif alliance == _G.ALLIANCE_DAGGERFALL_COVENANT then
return "DC"
else
return _G.ALLIANCE_NONE
end
end
Utils.faction_names_full = { AD="Aldmeri Dominion", DC="Daggerfall Covenant", EP="Ebonheart Pact" }
Utils.faction_names_short = { AD="Aldmeri", DC="Daggerfall", EP="Ebonheart" }
--TODO better than strings pls
function Utils.IsFaction(faction)
local fac = Utils.GetFaction()
return (fac==faction)
or (fac == "DC" and faction == "Daggerfall Covenant")
or (fac == "EP" and faction == "Ebonheart Pact")
or (fac == "AD" and faction == "Aldmeri Dominion")
end
--- Checks if the player's race/class matches the requirements.
-- @param requirement May be a string or a table of strings (which are then ORed).
-- @return true if matching, false if not.
function Utils.RaceClassMatch(fit)
if type(fit)=="table" then
for v in fit do
if Utils.RaceClassMatch(v) then
return true
end
end
return false --otherwise
end
local race,class = "",""
local faction = Utils.GetFaction("player","novet") -- DON'T match veterans anymore in here.
faction = faction:upper()
fit = fit:upper() :gsub("EBONHEART PACT","EP") :gsub("ALDMERI DOMINION","AD") :gsub("DAGGERFALL COVENANT","DC")
local neg = false
if fit:sub(1,1) == "!" then
neg = true
fit = fit:sub(2)
end
if fit:sub(1,4) == "NOT " then
neg = true
fit = fit:sub(5)
end
local ret = (race == fit or class == fit or faction == fit or race.." "..class == fit or (fit == "VET" and ZGV.VETERAN_FACTION))
if neg then
return not ret
else
return ret
end
end
function Utils.FormatLevel(l,...)
if type(l) == "table" then l = ... end -- dummy proof ZGV.Utils:FormatLevel(5)
return tostring(l) -- Nothing special atm
end
function Utils.GetPlayerPreciseLevel()
if ZGV.db.char.fakelevel and ZGV.db.char.fakelevel>0 then
return ZGV.db.char.fakelevel
else
local GetUnitLevel, GetUnitXP, GetUnitXPMax = _G.GetUnitLevel, _G.GetUnitXP, _G.GetUnitXPMax
return GetUnitLevel("player") + GetUnitXP("player")/max(GetUnitXPMax("player"),1)
end
end
function Utils.GetPlayerName()
local name = _G.GetUnitName("player")
return name
end
function Utils.GetMapNameByTexture()
local _, _, word = string.find( GetMapTileTexture(), "%a+/%a+/(%a+)/" ) -- pattern "Art/maps/mapname <- we want this
local name = zo_strformat("<<C:1>>", word) -- Uppercase first letter
return name
end
-- /dump d(ZGV.Utils.CheckIfInSkillGuild(1))
-- 1: Dark Brotherhood
-- 2: Fighters Guild
-- 3: Mages Guild
-- 4: Psijic Order
-- 5: Thieves Guild
-- 6: Undaunted
function Utils.CheckIfInSkillGuild(guildSkillIndex)
if guildSkillIndex <= 0 then return end
local _, _, isActive, _ = GetSkillLineDynamicInfo(SKILL_TYPE_GUILD, guildSkillIndex)
return isActive
end
-- /dump d(ZGV.Utils.SkillLines(false,true,false,true,true))
function Utils.SkillLines(showType,showLineInfo,showLineXP,showSkillAbilities,showAbilityInfo)
if showType then
d("Number of Skill Types: "..GetNumSkillTypes().."\n-----------------------------")
end
if showLineInfo then
for index = 0,GetNumSkillLines(SKILL_TYPE_GUILD) do
d(index..": "..GetSkillLineInfo(SKILL_TYPE_GUILD, index))
end
d("-----------------------------")
end
if showLineXP then
for index = 0,GetSkillLineXPInfo(SKILL_TYPE_GUILD) do
d(index..": "..GetSkillLineXPInfo(SKILL_TYPE_GUILD, index))
end
d("-----------------------------")
end
if showSkillAbilities then
for index = 0,GetNumSkillAbilities(SKILL_TYPE_GUILD) do
d(index..": "..GetNumSkillAbilities(SKILL_TYPE_GUILD, index))
end
d("-----------------------------")
end
if showAbilityInfo then
local hasProgression, progressionIndex, lastRankXP, nextRankXP, currentXP, atMorph = GetAbilityProgressionXPInfoFromAbilityId(abilityId)
local skillType, skillIndex, abilityIndex = GetSkillAbilityIndicesFromProgressionIndex(progressionIndex)
local abilityId2 = GetSkillAbilityId(skillType, skillIndex, abilityIndex)
d("GetSkillAbilityId: "..abilityId2)
d("skillType: "..skillType)
d("skillIndex: "..skillIndex)
d("abilityIndex: "..abilityIndex)
d("-----------------------------")
if hasProgression then d("hasProgression: true") else d("hasProgression: false") end
d("progressionIndex: "..progressionIndex)
d("lastRankXP: "..lastRankXP)
d("nextRankXP: "..nextRankXP)
d("currentXP: "..currentXP)
if atMorph then d("atMorph: true") else d("atMorph: false") end
d("-----------------------------")
end
end
function Utils:IsPlayerInCombat()
local IsUnitInCombat = _G.IsUnitInCombat
return ZGV.db.profile.fakecombat or IsUnitInCombat("player")
end
function Utils.ShowFloatingMessage(msg,event,font,sound,publicfloat,publictext)
if ZGV.DEV or publicfloat then
_G.CENTER_SCREEN_ANNOUNCE:AddMessage(event or _G.EVENT_OBJECTIVE_COMPLETED,font or _G.CSA_EVENT_SMALL_TEXT,sound or _G.SOUNDS.QUEST_OBJECTIVE_STARTED,"|cffaa00[|cf8fbffZ|cffaa00]|r "..msg)
end
if ZGV.DEV or publictext then
local print = ZGV.print
print(msg)
end
end
function Utils.escape(s)
return s:gsub("\'","\\\'"):gsub("\"","\\\""):gsub("%[","\\["):gsub("%]","\\]")
end
local esc=Utils.escape
local strrep=string.rep
function Utils.serialize(tab,indent)
if type(tab)~="table" then
return tab
end
local t = ""
indent = indent or 0
local keys={}
for k in tab do
tinsert(keys,k)
end
table.sort(keys)
t = t .. strrep(" ",indent) .. "{\n"
for key in keys do
while 1 do
local val = tab[key]
t = t .. strrep(" ",indent+1)
if tonumber(key) then
t = t .. "[" .. key .. "]"
else
t = t .. "[\"" .. esc(key) .. "\"]"
end
t = t .. " = "
if type(val)=="string" then
t = t .. "\"" .. val .. "\""
elseif type(val)=="number" then
t = t .. val
elseif type(val)=="function" then
t = t .. "nil --function"
elseif type(val)=="userdata" then
t = t .. "nil --userdata"
elseif type(val)=="nil" then
t = t .. "nil"
elseif type(val)=="table" then
t = t .. "\n"
t = t .. Utils.serialize(val,indent+1)
end
t = t .. ",\n"
break
end
end
t = t .. strrep(" ",indent) .. "}\n"
return t
end
-- Letters, numbers or spaces
function Utils.IsAlphanumeric(str)
if not str then return end
local zo_strfind = _G.zo_strfind
return not zo_strfind(str,"[^%w ]")
end
-----------------------------------------
-- OTHER FUNCTIONS
-----------------------------------------
-- Prototype inheritance for tables that will inherit all functions
function table.zginherits(self,tbl)
self.__UNSTRICT_CLASS=1
for f,fun in pairs(tbl) do
if type(fun)=="function" -- Functions are the only thing we want to copy
and f~="New" then -- Don't copy :New because those are specific to the Frames and don't want to overwrite them
if self[f] then self["saved"..f] = self[f] end -- Don't strictly overwrite. Save it first incase it is still needed.
self[f] = fun
elseif f == "class" then
self.class = self.class or fun -- Don't overwrite class class of orginal obj
end
end
self.__UNSTRICT_CLASS=nil
end
function table.zgclone(self)
local t={}
if type(self)=="table" then
-- Note: Be very careful about convert ipairs or pairs into standard for loops
for k,v in pairs(self) do
t[k]=rawget(self,k)
end
end
return t
end
-- This gets at the actual metatable of userdata
function getusermetatable(tab)
local meta = getmetatable(tab)
local index = meta.__index
return index
end
function class(obj)
if type(obj)~="table" and type(obj)~="userdata" then return end
return obj.class
end
function Utils.table_join (target,source)
if type(source)~="table" then return end
if type(target)~="table" then return end
for k,v in pairs(source) do
target[k] = v
end
end
function Utils.table_wipe (tab)
while #tab>0 do table.remove(tab) end
end
function Utils.table_wipe_keys (tab)
while true do
local k=next(tab)
if not k then break end
tab[k]=nil
end
end
-- HAR HAR we can into hexaccurate colors năo
-- at least we're as precise as WoW lua allows us to
function HTMLColor(code)
assert(code:match("#[0-9A-Fa-f]+$") and (#code==7 or #code==9),"Bogus code given: \""..code.."\")")
local r,g,b,a=tonumber("0x"..code:sub(2,3))/0xff,
tonumber("0x"..code:sub(4,5))/0xff,
tonumber("0x"..code:sub(6,7))/0xff,
#code==9 and tonumber("0x"..code:sub(8,9))/0xff
return r,g,b,a or 1
end
local safenext = function(table,index)
local ok,k,v = pcall(next,table,index)
if ok then
return k,v
else
-- when pcall fails, it gives an error message. The failing index will be there!
local newk = k:match(" function '(.-)'")
if newk then
return newk,"__PROT/PRIV__"
else
-- sad failure
end
end -- k has the error message, important
end
pairs = function(table) -- iterator
return safenext,table,nil
end
-- NOTE: use zo_insecurePairs for a pairs implementation that SKIPS private/protected members.
PrefixPairs = function(prefix) -- iterator
local safeglobalnext = function(index)
local val
local safety = 0
repeat
index,val = safenext(_G,index)
if index and index:find("^"..prefix) then
return index,val
end
safety=safety+1
if safety>100000 then
return "ERR","ERR"
end
until not index
end
return safeglobalnext,_G,nil
end
GetByPrefix = function(prefix,value,strip) -- lookup func
local ret
for k,v in PrefixPairs(prefix) do
if v == value then
ret = k
break
end
end
if strip then
ret = ret:gsub(prefix,"")
if ret:sub(1,1)=="_" then
ret = ret:sub(2)
end
end
return ret
end
local HEADLEN=40
local TAILLEN=20
local LIMIT=HEADLEN+TAILLEN+12 -- making a buffer, so that texts cannot be (accidentally) excerpted twice.
function Utils.MakeExcerpt(text)
if not text then return "" end
if #text>LIMIT then
local n=HEADLEN/2
local head = text:sub(1,HEADLEN)
while head:sub(-1)~=" " and n>0 do head=head:sub(1,-2) n=n-1 end
if #head==0 then head=text:sub(1,HEADLEN) end -- oh well
local n=TAILLEN/2
local tail = text:sub(-TAILLEN)
while tail:sub(1,1)~=" " and n>0 do tail=tail:sub(2) n=n-1 end
if #tail==0 then tail=text:sub(-TAILLEN) end -- oh well
text=head.."___"..tail -- .."<"..#text..">"
end
return text
end
local MakeExcerpt=Utils.MakeExcerpt
-- /zgoo {ZGV.Utils.MatchExcerpt(shortem,lorem)}
function Utils.MatchExcerpt(exc,text)
if exc==text then return true end
if not exc or not text then return false end
if exc:find("___") then -- this is an excerpt all right
local txt,len = exc:match("^(.-)%s*<(%d+)>$")
if txt then exc=txt end
len=len and tonumber(len)
-- First try parts.
local safetext="%{%"..text.."%}%"
local parts={zo_strsplit("___","%{%"..exc.."%}%")}
for i,part in ipairs(parts) do
if not zo_plainstrfind(safetext,part) then return false,safetext,part end
end
if len and (len~=#text) then return false,len,#text end
return true
end
return text==exc
end
local MatchExcerpt=Utils.MatchExcerpt
Utils.quest_cond_counts = "%s*:%s*%d+%s*/%s*%d+%s*"
-- TEST
local lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
local shortem = MakeExcerpt(lorem)
assert (shortem=="Lorem ipsum dolor sit amet, consectetur ___ id est laborum.",'Utils:MakeExcerpt cannot do lorem ipsum properly!')
assert (shortem==MakeExcerpt(shortem),'Utils:MatchExcerpt can\'t eat its own tail!')
assert (MatchExcerpt(shortem,lorem),'Utils:MatchExcerpt doesn\'t work on normal long texts')
assert (MatchExcerpt("Blah___bleh___bloh","Blah, this is bleh because bloh"),'Utils:MatchShortText fails to do 3 parts')
assert (not MatchExcerpt("Blah___bleh___bloh","bleh, this is bloh because Blah"),'Utils:MatchShortText is confused by order')
function Utils.GetMyAddonInfo()
local AM = GetAddOnManager()
for i = 1, AM:GetNumAddOns() do
local dir, title, author, _1, _2, _3, _4 = AM:GetAddOnInfo(i)
if dir == ZGV.DIR then
return dir, title, _1, _2, _3, _4
end
end
error("Can't find addon info!")
end
function Utils.IsPOIComplete(map,poi)
if type(map)=="string" or (type(map)=="number" and map>1000) then
poi = map%1000
map = math.floor(map/1000)
end
if not map then map=GetCurrentMapZoneIndex() end
if type(poi)=="string" then
for i=1,GetNumPOIs(map) do
local text,level,subtextinc,subtextcom = GetPOIInfo(map,i)
if text==poi then poi=i break end
end
end
if type(poi)=="number" then
local x,y,typ,tex = GetPOIMapInfo(map,poi)
return typ==MAP_PIN_TYPE_POI_COMPLETE
end
end
function Utils.GetPOIForQuest(questid)
if not ZGV._QuestPOIData then return "" end
if questid<=999999 then questid=("%07d"):format(questid) end
poi = ZGV._QuestPOIData:match("(%d+):[^\n]*"..questid)
return poi
end
ZGV.VETERAN_FACTION = "UNCHECKED"
local function SetVeteran(fac)
local prev_check = ZGV.VETERAN_FACTION
ZGV.VETERAN_FACTION = fac
if prev_check~="UNCHECKED" and prev_check~=ZGV.VETERAN_FACTION then ZGV.VETERAN_FACTION_CHANGED=fac end
end
Utils.VETERAN_PROGRESSION={ ['AD']={'AD','EP','DC'}, ['EP']={'EP','DC','AD'}, ['DC']={'DC','AD','EP'} }
function Utils.GetVeteranFaction()
local natural_faction = Utils.GetFaction("player","novet")
table.insert(ZGV.PRELOG,"natural faction is "..tostring(natural_faction))
local progression = Utils.VETERAN_PROGRESSION[natural_faction]
local silver_complete = ZGV.QuestTracker:IsQuestComplete("Cadwell's Silver")
local gold_complete = silver_complete and ZGV.QuestTracker:IsQuestComplete("Cadwell's Gold")
table.insert(ZGV.PRELOG,"silver "..tostring(silver_complete)..", gold "..tostring(gold_complete))
if gold_complete then return progression[3],4 end
for ji=1,MAX_JOURNAL_QUESTS do if IsValidQuestIndex(ji) then
local title=GetJournalQuestName(ji)
local prog_step
if title=="Cadwell's Silver" then prog_step = 1
elseif title=="Cadwell's Gold" then prog_step = 2
end
if prog_step then
for si=1,GetJournalQuestNumSteps(ji) do
local steptext,visibility,steptype,tracker,numcond = GetJournalQuestStepInfo(ji,si)
if tracker and tracker:find(" to Cadwell") then return progression[prog_step+1],prog_step+1 end -- "next" faction
for ci=1,numcond do
local conditionText,current,maxv,isFailCondition,isComplete,isCreditShared = GetJournalQuestConditionInfo(ji,si,ci)
if conditionText=="Experience the Daggerfall Covenant" then return "DC",prog_step+1 end -- this is a bit of an assumption, but the player can't possibly be on anything but their "next" vet faction if they have this kind of goal.
if conditionText=="Experience the Ebonheart Pact" then return "EP",prog_step+1 end
if conditionText=="Experience the Aldmeri Dominion" then return "AD",prog_step+1 end
if conditionText:find("Light of Meridia") then return progression[prog_step],prog_step end -- still "current" faction
end
end
break
end
end end
if silver_complete then return progression[2],2 end
return nil,1
end
function Utils.GetVeteranStage() -- 0:original, 1:first vet, 2:second vet, 3:original again
local vet,stageplus = Utils.GetVeteranFaction()
return stageplus-1
end
function Utils.CheckVeteranFaction()
table.insert(ZGV.PRELOG,"Checking quests for Cadwell")
SetVeteran(Utils.GetVeteranFaction())
table.insert(ZGV.PRELOG,"Checked. Veteran faction is "..(ZGV.VETERAN_FACTION or "none"))
end
-- remove "^Ng,adv" and similar language tags
function Utils.Delocalize(localstring)
local zo_strformat = _G.zo_strformat
return zo_strformat("<<1>>",localstring)
end