-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFullEquipmentFormula.lua
More file actions
174 lines (148 loc) · 4.95 KB
/
Copy pathFullEquipmentFormula.lua
File metadata and controls
174 lines (148 loc) · 4.95 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
--[[
### ItemsOfPower ###
ItemsOfPower FullEquipmentFormula Class
Thanks to lrdx
--]]
-- Libraries
local L = AceLibrary("AceLocale-2.2"):new("ItemsOfPower")
local AceOO = AceLibrary("AceOO-2.0")
local ItemBonusLib = AceLibrary("ItemBonusLib-1.0")
local db
local FullEquipmentFormula = AceOO.Class()
-- Environment of the Formula, stats will be written into it, allows usage of math functions
local IVFD = { math = math }
setmetatable(IVFD, { ["__index"] = function() return 0 end })
function FullEquipmentFormula.LoadSets()
db = ItemsOfPower:AcquireDBNamespace("FullEquipmentFormula")
for SetName in pairs(db.profile) do
ItemsOfPower:RegisterSet(FullEquipmentFormula:new(SetName))
end
end
function FullEquipmentFormula.prototype:init(Name, argFormula, argBaseEquipment)
FullEquipmentFormula.super.prototype.init(self)
if not db.profile[Name] then
db.profile[Name] = {
Formula = argFormula or "",
BaseEquipment = argBaseEquipment or ItemBonusLib:GetUnitEquipment("player"),
}
end
self.Name = Name
self.Formula = db.profile[Name].Formula
self:UpdatePointsFunc()
self.BaseEquipment = db.profile[Name].BaseEquipment
self.Options = {
type = "group",
name = Name,
desc = string.format(L["Settings for Set \"%s\"."],Name),
args =
{
Formula =
{
order = 100,
type = "text",
name = L["Formula"],
desc = L["A formula using stat values of the full equipment."],
usage = L["<Formula>"],
get = function() return self.Formula or "" end,
set = function(v)
self.Formula = v
self:UpdatePointsFunc()
db.profile[Name].Formula = v
ItemsOfPower:ClearCache()
end
},
ParseEquipment =
{
order = 200,
type = "execute",
name = L["Use current equipment"],
desc = L["Sets the current equipment as base for calculations."],
func = function()
-- copy the table, because it gets recycled
local currentEquipment = { }
local eq = ItemBonusLib:GetUnitEquipment("player")
for k, v in pairs(eq) do
currentEquipment[k] = v
end
self.BaseEquipment = currentEquipment
db.profile[Name].BaseEquipment = currentEquipment
ItemsOfPower:ClearCache()
end
}
}
}
end
function FullEquipmentFormula.prototype:tostring()
return "ItemsOfPower FullEquipmentFormula " .. self.Name
end
function FullEquipmentFormula.prototype:OnDelete()
db.profile[self.Name] = nil
end
function FullEquipmentFormula.prototype:UpdatePointsFunc()
self.CalcPoints = loadstring("return " .. self.Formula)
setfenv(self.CalcPoints, IVFD)
end
function FullEquipmentFormula.prototype:GetEquipValue(eq)
for k, v in pairs(eq) do
IVFD[k] = v
end
local Points = self:CalcPoints()
for k in pairs(eq) do
IVFD[k] = nil
end
return Points or 0
end
local ItemType2EquipLoc = {
["INVTYPE_AMMO"] = "Ammo",
["INVTYPE_HEAD"] = "Head",
["INVTYPE_NECK"] = "Neck",
["INVTYPE_SHOULDER"] = "Shoulder",
["INVTYPE_BODY"] = "Shirt",
["INVTYPE_CHEST"] = "Chest",
["INVTYPE_ROBE"] = "Chest",
["INVTYPE_WAIST"] = "Waist",
["INVTYPE_LEGS"] = "Legs",
["INVTYPE_FEET"] = "Feet",
["INVTYPE_HAND"] = "Hands",
["INVTYPE_FINGER"] = "Finger0",
["INVTYPE_TRINKET"] = "Trinket0",
["INVTYPE_CLOAK"] = "Back",
["INVTYPE_WEAPON"] = "MainHand",
["INVTYPE_SHIELD"] = "SecondaryHand",
["INVTYPE_2HWEAPON"] = "MainHand",
["INVTYPE_WEAPONMAINHAND"] = "MainHand",
["INVTYPE_WEAPONOFFHAND"] = "SecondaryHand",
["INVTYPE_HOLDABLE"] = "SecondaryHand",
["INVTYPE_RANGED"] = "Ranged",
["INVTYPE_THROWN"] = "Ranged",
["INVTYPE_RANGEDRIGHT"] = "Ranged",
["INVTYPE_RELIC"] = "Ranged",
["INVTYPE_TABARD"] = "Tabard",
}
function FullEquipmentFormula.prototype:GetItemValue(newItemString)
local _, newItemLink, _, _, _, _, _, newItemType, _ = GetItemInfo(newItemString)
local newItemEquipLoc = ItemType2EquipLoc[newItemType]
if newItemEquipLoc and self.BaseEquipment and type(self.BaseEquipment) == "table" then
-- Replace item and switch old item back in after calculation
local oldItem = self.BaseEquipment[newItemEquipLoc]
self.BaseEquipment[newItemEquipLoc] = newItemLink
local value = self:GetEquipValue(ItemBonusLib:MergeDetails(ItemBonusLib:BuildBonusSet(self.BaseEquipment)))
self.BaseEquipment[newItemEquipLoc] = oldItem
return value
else
-- behavior like the normal formula if the item isnt equipable or no equipment set
return self:GetEquipValue(ItemBonusLib:ScanItem(newItemString, true))
end
end
function FullEquipmentFormula.prototype:Serialize()
return {
Type = "FullEquipmentFormula",
Name = self.Name,
Formula = self.Formula,
BaseEquipment = self.BaseEquipment,
}
end
function FullEquipmentFormula:Deserialize(t)
return FullEquipmentFormula:new(t.Name, t.Formula, t.BaseEquipment)
end
ItemsOfPower:RegisterSetType("FullEquipmentFormula", FullEquipmentFormula)