-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoLoot.lua
More file actions
642 lines (502 loc) · 18 KB
/
Copy pathAutoLoot.lua
File metadata and controls
642 lines (502 loc) · 18 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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
-----------------------------------------------------------------------------------------------
-- Client Lua Script for AutoLoot
-- Copyright (c) NCsoft. All rights reserved
-----------------------------------------------------------------------------------------------
require "Window"
require "GameLib"
require "Item"
require "GroupLib"
require "GuildLib"
-----------------------------------------------------------------------------------------------
-- AutoLoot Module Definition
-----------------------------------------------------------------------------------------------
local AutoLoot = {}
-----------------------------------------------------------------------------------------------
-- Constants
-----------------------------------------------------------------------------------------------
local tRuleNames = {
[1] = "Need",
[2] = "Greed",
[3] = "Pass",
[4] = "Ignore"
}
local tItemCategory = {
Survivalist = 110,
Dyes = 54,
Housing = 67
}
local tDefault = {
bEnabled = true,
bNoNeedWhenFullGuild = false,
bMessageOnRoll = false,
nNonNeedableRule = 2,
nUnlockedDyeRule = 4,
tLootRules = {
tByName = {},
tByCategory = {
[tItemCategory.Survivalist] = 4,
[tItemCategory.Housing] = 4
}
}
}
-----------------------------------------------------------------------------------------------
-- Initialization
-----------------------------------------------------------------------------------------------
function AutoLoot:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
-- initialize variables here
o.tVersion = {
nMajor = 1,
nMinor = 3,
nBuild = 2
}
o.tSettings = self:tableClone(tDefault)
o.SortMode = 1
o.bDocLoaded = false
o.bFullGuildGroup = false
--o.tCheckedLoot = {} --need reset sometimes
o.tGuildMembers = {}
return o
end
function AutoLoot:Init()
local bHasConfigureFunction = true
local strConfigureButtonText = "Auto Loot"
local tDependencies = {
-- "UnitOrPackageName",
}
Apollo.RegisterAddon(self, bHasConfigureFunction, strConfigureButtonText, tDependencies)
end
-----------------------------------------------------------------------------------------------
-- AutoLoot OnLoad
-----------------------------------------------------------------------------------------------
function AutoLoot:OnLoad()
-- load our form file
self.xmlDoc = XmlDoc.CreateFromFile("AutoLoot.xml")
self.xmlDoc:RegisterCallback("OnDocLoaded", self)
end
-----------------------------------------------------------------------------------------------
-- AutoLoot OnSave and OnRestore
-----------------------------------------------------------------------------------------------
function AutoLoot:OnSave(eLevel)
if eLevel ~= GameLib.CodeEnumAddonSaveLevel.Character then return nil end
return self.tSettings
end
function AutoLoot:OnRestore(eLevel, tData)
if tData == nil then return end
self.tSettings = self:tableMerge(self.tSettings, tData)
end
-----------------------------------------------------------------------------------------------
-- AutoLoot OnDocLoaded
-----------------------------------------------------------------------------------------------
function AutoLoot:OnDocLoaded()
if self.xmlDoc ~= nil and self.xmlDoc:IsLoaded() then
self.bDocLoaded = true
Apollo.RegisterSlashCommand("autoloot", "OnSlashCommand", self)
Apollo.RegisterEventHandler("LootRollUpdate", "OnLootUpdate", self)
Apollo.RegisterEventHandler("Group_Join", "OnGroupUpdated", self)
Apollo.RegisterEventHandler("Group_Left", "OnGroupUpdated", self)
Apollo.RegisterEventHandler("Group_Add", "OnGroupUpdated", self)
Apollo.RegisterEventHandler("Group_Remove", "OnGroupUpdated", self)
Apollo.RegisterEventHandler("GuildRoster", "OnGuildRoster", self)
Apollo.RegisterEventHandler("GuildMemberChange", "OnGuildMemberChange", self)
for _, tGuild in ipairs(GuildLib.GetGuilds()) do
if tGuild:GetType() == GuildLib.GuildType_Guild then
tGuild:RequestMembers()
end
end
end
end
-----------------------------------------------------------------------------------------------
-- AutoLoot Functions
-----------------------------------------------------------------------------------------------
function AutoLoot:OnLootUpdate()
if not self.tSettings.bEnabled then return end
local tLoot = GameLib.GetLootRolls()
for _, tLootItem in ipairs(tLoot) do
local nLootId = tLootItem.nLootId
--need to reset tCheckedLoot sometimes
--if not self.tCheckedLoot[nLootId] then
self:HandleItem(nLootId, tLootItem.itemDrop)
--self.tCheckedLoot[nLootId] = true
--end
end
end
function AutoLoot:HandleItem(nLootId, itemDrop)
local strItemName = itemDrop:GetName()
--If option set, greed non-needable items
if self.tSettings.nNonNeedableRule ~= 4 and not GameLib.IsNeedRollAllowed(nLootId) then
self:UseRule(self.tSettings.nNonNeedableRule, nLootId, strItemName)
return
end
--Check the LootRules
if self:ByNameFindMatch(strItemName, nLootId) then return end
if self.tSettings.nUnlockedDyeRule ~= 4 and itemDrop:GetItemCategory() == tItemCategory.Dyes then
local tItemInfo = itemDrop:GetDetailedInfo()
if tItemInfo.tPrimary.arUnlocks then
for _, tCur in ipairs(tItemInfo.tPrimary.arUnlocks) do
if tCur.bUnlocked then self:UseRule(self.tSettings.nUnlockedDyeRule, nLootId, strItemName) else return end
end
end
end
if self:UseRule(self.tSettings.tLootRules.tByCategory[itemDrop:GetItemCategory()], nLootId, strItemName) then return end
end
function AutoLoot:ByNameFindMatch(strItemName, nLootId)
local nFoundRule = nil
local nPriority = 0
for k,v in pairs(self.tSettings.tLootRules.tByName) do
if nPriority < #k then
if string.find(strItemName, k) ~= nil then
nFoundRule = v.nRule
nPriority = #k
end
end
end
if nFoundRule == nil then return false end
return self:UseRule(nFoundRule, nLootId, strItemName)
end
function AutoLoot:UseRule(nRule, nLootId, strItemName)
local bSuccess = false
if nRule == 1 then
-- Need
if not (self.tSettings.bNoNeedWhenFullGuild and self.bFullGuildGroup) then
GameLib.RollOnLoot(nLootId, true)
end
bSuccess = true
elseif nRule == 2 then
-- Greed
GameLib.RollOnLoot(nLootId, false)
bSuccess = true
elseif nRule == 3 then
-- Pass
GameLib.PassOnLoot(nLootId)
bSuccess = true
elseif nRule == 4 then
-- Ignore
bSuccess = true
end
if bSuccess and self.tSettings.bMessageOnRoll and nRule < 4 and strItemName then
local msg = "AutoLoot: "..tRuleNames[nRule].."ed on item: "..strItemName
ChatSystemLib.PostOnChannel(ChatSystemLib.ChatChannel_System, msg)
end
return bSuccess
end
-- Full Guild checks
function AutoLoot:OnGuildMemberChange(guildCurr)
if guildCurr:GetType() ~= GuildLib.GuildType_Guild then return end
guildCurr:RequestMembers()
end
function AutoLoot:OnGuildRoster(guildCurr, tRoster)
if guildCurr:GetType() ~= GuildLib.GuildType_Guild then return end
if #self.tGuildMembers == #tRoster then return end
self.tGuildMembers = {}
for k,v in ipairs(tRoster) do
self.tGuildMembers[k] = v.strName
end
self:OnGroupUpdated()
end
function AutoLoot:OnGroupUpdated()
local nGroupCount = GroupLib.GetMemberCount()
if nGroupCount <= 1 then self.bFullGuildGroup = false return end
local bFullGuild = true
for nIdx = 1, nGroupCount do
local tMember = GroupLib.GetGroupMember(nIdx)
local bIsGuildMember = false
for _,v in ipairs(self.tGuildMembers) do
if tMember.strCharacterName == v then
bIsGuildMember = true
break
end
end
if not bIsGuildMember then
bFullGuild = false
break
end
end
self.bFullGuildGroup = bFullGuild
end
-----------------------------------------------------------------------------------------------
-- AutoLoot Helpers
-----------------------------------------------------------------------------------------------
function AutoLoot:tableMerge(t1, t2)
for k,v in pairs(t2) do
if type(v) == "table" then
if type(t1[k] or false) == "table" then
self:tableMerge(t1[k] or {}, t2[k] or {})
else
t1[k] = v
end
else
t1[k] = v
end
end
return t1
end
function AutoLoot:tableClone(t)
if type(t) ~= "table" then return t end
local meta = getmetatable(t)
local target = {}
for k, v in pairs(t) do
if type(v) == "table" then
target[k] = self:tableClone(v)
else
target[k] = v
end
end
setmetatable(target, meta)
return target
end
function AutoLoot:SystemPrint(msg)
ChatSystemLib.PostOnChannel(ChatSystemLib.ChatChannel_System, msg)
end
function AutoLoot:Print(strText)
if strText then self:SystemPrint("Auto Loot: "..strText) end
end
-----------------------------------------------------------------------------------------------
-- AutoLoot Command Functions
-----------------------------------------------------------------------------------------------
function AutoLoot:OnSlashCommand(slash, args)
local tBoolText = { [true] = "Enabled", [false] = "Disabled" }
if args == "config" or args == "options" then
self:OnConfigure()
elseif args == "toggle" then
self.tSettings.bEnabled = not self.tSettings.bEnabled
self:Print(tBoolText[self.tSettings.bEnabled])
elseif args == "enable" then
self.tSettings.bEnabled = true
self:Print(tBoolText[true])
elseif args == "disable" then
self.tSettings.bEnabled = false
self:Print(tBoolText[false])
elseif args == "rollmsg" then
self.tSettings.bMessageOnRoll = not self.tSettings.bMessageOnRoll
self:Print("Roll Messages "..tBoolText[self.tSettings.bMessageOnRoll])
elseif args == "reset" then
self:OnResetSettings()
self:Print("Settings Reset")
else
self:SystemPrint("--- Auto Loot Help ---")
self:SystemPrint("config | Open settings window")
self:SystemPrint("toggle | Enable / Disable")
self:SystemPrint("enable | Enable the addon")
self:SystemPrint("disable | Disable the addon")
self:SystemPrint("rollmsg | Toggle messages on roll")
self:SystemPrint("reset | Reset to default settings")
end
end
-----------------------------------------------------------------------------------------------
-- AutoLootForm Functions
-----------------------------------------------------------------------------------------------
function AutoLoot:OnConfigure()
if not self.bDocLoaded then return end
if self.wndMain == nil then
self.wndMain = Apollo.LoadForm(self.xmlDoc, "AutoLootForm", nil, self)
if self.wndMain == nil then
Apollo.AddAddonErrorText(self, "Could not load the main window for some reason.")
return
end
local tVer = self.tVersion
self.wndMain:FindChild("Version"):SetText(string.format("v%d.%d.%d", tVer.nMajor, tVer.nMinor, tVer.nBuild))
self.wndMain:Show(false, true)
end
self.wndMain:Show(true, false)
self:LoadSettings()
end
function AutoLoot:LoadSettings()
self:RuleListClear()
self.wndListRuleChoices = nil
self.wndMain:FindChild("btnName"):SetCheck(self.SortMode == 1)
self.wndMain:FindChild("btnRule"):SetCheck(self.SortMode == 2)
self.wndMain:FindChild("ButtonEnabled"):SetCheck(self.tSettings.bEnabled)
self.wndMain:FindChild("ButtonFullGuild"):SetCheck(self.tSettings.bNoNeedWhenFullGuild)
self.wndMain:FindChild("ButtonRollMessage"):SetCheck(self.tSettings.bMessageOnRoll)
self:SetupRuleButton(self.wndMain:FindChild("AddRule"), 2)
self:SetupRuleButton(self.wndMain:FindChild("SelectRuleNonNeed"), self.tSettings.nNonNeedableRule)
self:SetupRuleButton(self.wndMain:FindChild("SelectRuleSurvivalist"), self.tSettings.tLootRules.tByCategory[tItemCategory.Survivalist])
self:SetupRuleButton(self.wndMain:FindChild("SelectRuleHousing"), self.tSettings.tLootRules.tByCategory[tItemCategory.Housing])
self:SetupRuleButton(self.wndMain:FindChild("SelectRuleUnlockedDye"), self.tSettings.nUnlockedDyeRule)
self:RefreshNameList()
end
function AutoLoot:SetupRuleButton(btn, nRule)
btn:SetCheck(false)
self:SetRuleButton(btn, nRule)
end
function AutoLoot:RefreshNameList()
local wndList = self.wndMain:FindChild("RuleList")
wndList:DestroyChildren()
for k,v in pairs(self.tSettings.tLootRules.tByName) do
local wndNewEntry = Apollo.LoadForm(self.xmlDoc, "ListEntry", wndList, self)
wndNewEntry:FindChild("Name"):SetText(k)
self:SetRuleButton(wndNewEntry:FindChild("Rule"), v.nRule)
end
self:SortEntries(wndList)
end
-- Sort Start
function AutoLoot:SortEntries(wndList)
if wndList == nil then return end
if self.SortMode == 1 then
wndList:ArrangeChildrenVert(0, AutoLoot.SortByName)
elseif self.SortMode == -1 then
wndList:ArrangeChildrenVert(0, AutoLoot.SortByNameBack)
elseif self.SortMode == 2 then
wndList:ArrangeChildrenVert(0, AutoLoot.SortByRule)
elseif self.SortMode == -2 then
wndList:ArrangeChildrenVert(0, AutoLoot.SortByRuleBack)
end
end
function AutoLoot.SortByName(a,b)
return a:FindChild("Name"):GetText() <= b:FindChild("Name"):GetText()
end
function AutoLoot.SortByNameBack(a,b)
return not AutoLoot.SortByName(a,b)
end
function AutoLoot.SortByRule(a,b)
local aRule = a:FindChild("Rule"):GetData()
local bRule = b:FindChild("Rule"):GetData()
if aRule == bRule then
return AutoLoot.SortByName(a,b)
else
return (aRule < bRule)
end
end
function AutoLoot.SortByRuleBack(a,b)
return not AutoLoot.SortByRule(a,b)
end
-- Sort End
function AutoLoot:OnClose()
self.wndMain:Close()
end
function AutoLoot:OnClosed()
self.wndMain:FindChild("RuleList"):DestroyChildren()
self:RuleListClear()
end
function AutoLoot:OnMove()
self:RuleListClear()
end
function AutoLoot:OnResetSettings()
self.tSettings = self:tableClone(tDefault)
if self.wndMain ~= nil then self:LoadSettings() end
end
function AutoLoot:ButtonEnabled(wndHandler, wndControl)
self.tSettings.bEnabled = wndControl:IsChecked()
end
function AutoLoot:ButtonFullGuild(wndHandler, wndControl)
self.tSettings.bNoNeedWhenFullGuild = wndControl:IsChecked()
end
function AutoLoot:ButtonRollMessage(wndHandler, wndControl)
self.tSettings.bMessageOnRoll = wndControl:IsChecked()
end
-- Header
function AutoLoot:OnHeaderButton(wndHandler, wndControl)
local nSortMode = wndHandler:GetContentId()
if self.SortMode == nSortMode then
self.SortMode = -nSortMode
else
self.SortMode = nSortMode
end
self:SortEntries(self.wndMain:FindChild("RuleList"))
end
-- List
function AutoLoot:OnRemoveEntry(wndHandler, wndControl)
local wndEntry = wndControl:GetParent()
self.tSettings.tLootRules.tByName[wndEntry:FindChild("Name"):GetText()] = nil
wndEntry:Destroy()
self:SortEntries(self.wndMain:FindChild("RuleList"))
end
-- Rule Select
function AutoLoot:OnRuleSelectCheck(wndHandler, wndControl)
self:RuleListClear()
local wndChoices = Apollo.LoadForm(self.xmlDoc, "RuleChoiceContainer", nil, self)
wndControl:AttachWindow(wndChoices)
--Set Position
wndChoices:SetData(wndControl)
local nPosX, nPosY = wndControl:GetPos()
local wndParents = wndControl:GetParent()
while wndParents do
local nPosX2, nPosY2 = wndParents:GetPos()
nPosX = nPosX + nPosX2
nPosY = nPosY + nPosY2
wndParents = wndParents:GetParent()
end
local nChoicesWidth = wndChoices:GetWidth()
local nChoicesHeight = wndChoices:GetHeight()
local nButtonWidth = wndControl:GetWidth()
local nButtonHeight = wndControl:GetHeight()
wndChoices:Move(nPosX + nButtonWidth - 30, nPosY - nChoicesHeight / 2 + nButtonHeight / 2, nChoicesWidth, nChoicesHeight)
local nCurrentData = wndControl:GetData()
for _,v in ipairs(wndChoices:FindChild("Controls"):GetChildren()) do
v:SetCheck(nCurrentData == v:GetContentId())
end
if wndControl:GetContentId() == 1 then
wndChoices:FindChild("Controls"):FindChild("Need"):Enable(false)
end
wndChoices:Show(true, false)
end
function AutoLoot:OnRuleRadio(wndHandler, wndControl)
local wndChoices = wndControl:GetParent():GetParent()
local btnRule = wndChoices:GetData()
local nRule = wndControl:GetContentId()
wndChoices:Show(false)
if not btnRule:IsValid() then return end
self:SetRuleButton(btnRule, nRule)
btnRule:SetCheck(false)
local btnName = btnRule:GetName()
if btnName == "Rule" then
self.tSettings.tLootRules.tByName[btnRule:GetParent():FindChild("Name"):GetText()].nRule = nRule
elseif btnName == "SelectRuleNonNeed" then
self.tSettings.nNonNeedableRule = nRule
elseif btnName == "SelectRuleSurvivalist" then
self.tSettings.tLootRules.tByCategory[tItemCategory.Survivalist] = nRule
elseif btnName == "SelectRuleHousing" then
self.tSettings.tLootRules.tByCategory[tItemCategory.Housing] = nRule
elseif btnName == "SelectRuleUnlockedDye" then
self.tSettings.nUnlockedDyeRule = nRule
end
end
function AutoLoot:SetRuleButton(btnRule, nRule)
btnRule:SetText(tRuleNames[nRule])
btnRule:SetData(nRule)
end
function AutoLoot:OnRuleListShow(wndHandler, wndControl)
self.wndListRuleChoices = wndHandler
wndHandler:ToFront()
end
function AutoLoot:OnRuleListHide(wndHandler, wndControl)
wndControl:Close()
end
function AutoLoot:OnRuleListClosed(wndHandler, wndControl)
self.wndListRuleChoices = nil
end
function AutoLoot:RuleListClear()
if self.wndListRuleChoices ~= nil and self.wndListRuleChoices:IsValid() then
self.wndListRuleChoices:Close()
end
end
-- Add Window Functions
function AutoLoot:OnDragDropItem(wndHandler, wndControl, x, y, wndSource, strType, nItemSourceLoc)
if strType ~= "DDBagItem" or wndHandler ~= wndControl then return end
local itemDropped = Item.GetItemFromInventoryLoc(nItemSourceLoc)
if not itemDropped then return end
wndControl:SetText(itemDropped:GetName())
end
function AutoLoot:OnDragDropItemQuery(wndHandler, wndControl, x, y, wndSource, strType, nItemSourceLoc)
if strType ~= "DDBagItem" or wndHandler ~= wndControl then return end
local itemSource = Item.GetItemFromInventoryLoc(nItemSourceLoc)
if not itemSource then return end
return Apollo.DragDropQueryResult.Accept
end
function AutoLoot:OnAddConfirm(wndHandler, wndControl)
local wndAddOptions = wndControl:GetParent()
local strAddName = wndAddOptions:FindChild("AddName"):GetText()
local nAddRule = wndAddOptions:FindChild("AddRule"):GetData()
if strAddName == "" then return end
self.tSettings.tLootRules.tByName[strAddName] = { nRule = nAddRule }
wndAddOptions:FindChild("AddName"):SetText("")
self:RefreshNameList()
end
-----------------------------------------------------------------------------------------------
-- AutoLoot Instance
-----------------------------------------------------------------------------------------------
local AutoLootInst = AutoLoot:new()
AutoLootInst:Init()