-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCanICraftThis.lua
280 lines (263 loc) · 11.1 KB
/
CanICraftThis.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
local function isTraitKnown(trait, recipe)
local traitIndex
for traitIndex = 1, recipe.numTraits do
local traitId, _, isKnown = GetSmithingResearchLineTraitInfo(recipe.skill.id, recipe.researchLineIndex, traitIndex)
if traitId == trait.id then
return isKnown
end
end
CanICraftThis.reportUnexpected("GetSmithingResearchLineTraitInfo did not yield information for trait ID " .. tostring(trait.id))
return nil
end
local function getNumKnownTraits(recipe)
local n = 0
local traitIndex
for traitIndex = 1, recipe.numTraits do
local _, _, isKnown = GetSmithingResearchLineTraitInfo(recipe.skill.id, recipe.researchLineIndex, traitIndex)
if isKnown then
n = n + 1
end
end
return n
end
local function iterImprovementMaterials(skill, desiredQualityId)
local i = 0
return function()
while i < 100 do
i = i + 1
local material, _, _, _, _, _, _, materialQualityId = GetSmithingImprovementItemInfo(skill.id, i)
if material == nil or material == "" then break end
if materialQualityId <= desiredQualityId then
return {
name = CanICraftThis.Material:sanitise(material),
requiredQuantity = GetSmithingGuaranteedImprovementItemAmount(skill.id, i),
}
end
end
return nil
end
end
local function getMaterialQuantities()
local quantities = {}
local bagId
for _, bagId in ipairs({BAG_BACKPACK, BAG_VIRTUAL, BAG_BANK, BAG_SUBSCRIBER_BANK}) do
local bagCache = SHARED_INVENTORY:GetOrCreateBagCache(bagId)
local slotIndex
for slotIndex in ZO_IterateBagSlots(bagId) do
local slot = bagCache[slotIndex]
if slot then
local name = CanICraftThis.Material:sanitise(slot.name)
quantities[name] = (quantities[name] or 0) + slot.stackCount
end
end
end
return quantities
end
local function appendCriterion(options)
if options.have ~= nil then
CanICraftThis.assert(options.needed ~= nil, "options.needed ~= nil")
CanICraftThis.assert(options.textIfMet ~= nil, "options.textIfMet ~= nil")
CanICraftThis.assert(options.textIfNotMet ~= nil, "options.textIfNotMet ~= nil")
local suffix = " [" .. tostring(options.have) .. "/" .. tostring(options.needed) .. "]"
options.textIfMet = options.textIfMet .. suffix
options.textIfNotMet = options.textIfNotMet .. suffix
options.isMet = options.have >= options.needed
end
if options.isMet ~= nil then
CanICraftThis.assert(options.textIfMet ~= nil, "options.textIfMet ~= nil")
CanICraftThis.assert(options.textIfNotMet ~= nil, "options.textIfNotMet ~= nil")
if options.colorIfMet == nil then options.colorIfMet = ZO_SUCCEEDED_TEXT end
if options.colorIfNotMet == nil then options.colorIfNotMet = ZO_ERROR_COLOR end
if options.isMet then
options.text = options.textIfMet
options.color = options.colorIfMet
else
options.text = options.textIfNotMet
options.color = options.colorIfNotMet
end
end
CanICraftThis.assert(options.text ~= nil, "options.text ~= nil")
CanICraftThis.assert(options.color ~= nil, "options.color ~= nil")
ItemTooltip:AddLine(options.text, nil, options.color:UnpackRGB())
end
local function appendEqPassiveAbilityCriterion(skill, requiredPassiveAbility)
appendCriterion({
textIfMet = "Sufficient " .. skill.equipmentInfo.passiveAbilityName .. " skill.",
textIfNotMet = "Insufficient " .. skill.equipmentInfo.passiveAbilityName .. " skill.",
have = GetNonCombatBonus(skill.equipmentInfo.passiveAbilityId),
needed = requiredPassiveAbility,
})
end
local function appendEqTraitKnownCriterion(trait, recipe)
appendCriterion({
textIfMet = trait.name .. " trait known for " .. recipe.name .. ".",
textIfNotMet = trait.name .. " trait not known for " .. recipe.name .. ".",
isMet = isTraitKnown(trait, recipe),
})
end
local function appendEqStyleKnownCriterion(style, recipe, styleCollectibleCache)
local collectibleId = styleCollectibleCache:getCollectibleId(style, recipe)
if collectibleId == nil then return end -- this can happen due to bugs in ESO's database
appendCriterion({
textIfMet = style.name .. " style known for " .. recipe.name .. ".",
textIfNotMet = style.name .. " style not known for " .. recipe.name .. ".",
isMet = IsCollectibleUnlocked(collectibleId),
})
end
local function appendEqSetNumTraitsCriterion(set, recipe)
appendCriterion({
textIfMet = "Sufficient traits known for " .. recipe.name .. ".",
textIfNotMet = "Insufficient traits known for " .. recipe.name .. ".",
have = getNumKnownTraits(recipe),
needed = set.getNumRequiredTraits(recipe),
})
end
local function appendEqSetDLCCriterion(name)
local instance = CanICraftThis.DLC:tryFromName(name)
local isUnlocked
if instance ~= nil then
isUnlocked = IsCollectibleUnlocked(instance.collectibleId)
else
isUnlocked = false
end
appendCriterion({
textIfMet = name .. " DLC owned, which contains the set crafting station.",
textIfNotMet = name .. " DLC not owned, which contains the set crafting station.",
isMet = isUnlocked,
})
end
local function appendMaterialQuantityCriterion(options)
CanICraftThis.assert(options.material ~= nil, "options.material ~= nil")
CanICraftThis.assert(options.needed ~= nil, "options.needed ~= nil")
CanICraftThis.assert(options.materialQuantities ~= nil, "options.materialQuantities ~= nil")
options.suffix = options.suffix or ""
options.textIfMet = "Sufficient " .. options.material .. options.suffix .. "."
options.textIfNotMet = "Insufficient " .. options.material .. options.suffix .. "."
options.have = options.materialQuantities[options.material] or 0
appendCriterion(options)
end
local function appendStyleMaterialQuantityCriterion(options)
CanICraftThis.assert(options.material ~= nil, "options.material ~= nil")
CanICraftThis.assert(options.needed ~= nil, "options.needed ~= nil")
CanICraftThis.assert(options.materialQuantities ~= nil, "options.materialQuantities ~= nil")
options.suffix = " (style)"
local wildcardMaterial = CanICraftThis.Material.wildcardStyle
local have = options.materialQuantities[options.material] or 0
local wildcardHave = options.materialQuantities[wildcardMaterial] or 0
if have < options.needed and wildcardHave >= options.needed then
options.colorIfNotMet = ZO_SECOND_CONTRAST_TEXT
appendMaterialQuantityCriterion(options)
options.colorIfNotMet = nil
options.material = wildcardMaterial
appendMaterialQuantityCriterion(options)
else
appendMaterialQuantityCriterion(options)
end
end
local function extendTooltip(itemLink, craftingStationCache, styleCollectibleCache)
local itemType = GetItemLinkItemType(itemLink)
if itemType ~= ITEMTYPE_MASTER_WRIT then return end
local skill = CanICraftThis.Skill:tryFromWritName(GetItemLinkName(itemLink))
if skill == nil then return end
local writText = GenerateMasterWritBaseText(itemLink)
local qualityId = GetItemLinkDisplayQuality(itemLink)
if skill.equipmentInfo then
local mainMaterial = CanICraftThis.Material:eqMainFromWritText(writText)
local recipe = CanICraftThis.EqRecipe:fromWritText(writText)
local trait = CanICraftThis.EqTrait:fromWritText(writText, recipe)
local style = nil
if skill.equipmentInfo.usesStyle then
style = CanICraftThis.EqStyle:fromWritText(writText)
end
local set = CanICraftThis.EqSet:fromWritText(writText)
local requiredPassiveAbility = craftingStationCache:getRequiredPassiveAbility(mainMaterial)
local requiredMainMaterialQuantity = craftingStationCache:getRequiredMainMaterialQuantity(mainMaterial, recipe)
if requiredPassiveAbility == nil or requiredMainMaterialQuantity == nil then
ItemTooltip:AddLine("You must visit a " .. skill.name .. " Station before detailed information about this writ can be displayed.")
return
end
local materialQuantities = getMaterialQuantities()
ItemTooltip:AddLine("--- Knowledge ---")
appendEqPassiveAbilityCriterion(skill, requiredPassiveAbility)
appendEqTraitKnownCriterion(trait, recipe)
if style ~= nil then
appendEqStyleKnownCriterion(style, recipe, styleCollectibleCache)
end
appendEqSetNumTraitsCriterion(set, recipe)
if set.dlcName ~= nil then
appendEqSetDLCCriterion(set.dlcName)
end
ItemTooltip:AddLine("--- Materials ---")
appendMaterialQuantityCriterion({
material = mainMaterial,
needed = requiredMainMaterialQuantity,
materialQuantities = materialQuantities,
})
appendMaterialQuantityCriterion({
material = trait.material,
suffix = " (trait)",
needed = 1,
materialQuantities = materialQuantities,
})
if style ~= nil then
appendStyleMaterialQuantityCriterion({
material = style.material,
needed = 1,
materialQuantities = materialQuantities,
})
end
for improvementMaterial in iterImprovementMaterials(skill, qualityId) do
appendMaterialQuantityCriterion({
material = improvementMaterial.name,
suffix = " (improve)",
needed = improvementMaterial.requiredQuantity,
materialQuantities = materialQuantities,
})
end
end
end
local function installTooltipExtensionHooks(craftingStationCache, styleCollectibleCache)
-- TODO: InventoryEnter in inventoryslot.lua has the full list of hooks we should install.
ZO_PostHook(ItemTooltip, "SetBagItem", function(_, bagId, slotIndex)
extendTooltip(GetItemLink(bagId, slotIndex), craftingStationCache, styleCollectibleCache)
end)
ZO_PostHook(ItemTooltip, "SetTradingHouseItem", function(_, searchResultIndex)
extendTooltip(GetTradingHouseSearchResultItemLink(searchResultIndex), craftingStationCache, styleCollectibleCache)
end)
ZO_PostHook(ItemTooltip, "SetTradingHouseListing", function(_, listingIndex)
extendTooltip(GetTradingHouseListingItemLink(listingIndex), craftingStationCache, styleCollectibleCache)
end)
ZO_PostHook(ItemTooltip, "SetAttachedMailItem", function(_, mailId, attachmentIndex)
extendTooltip(GetAttachedItemLink(mailId, attachmentIndex), craftingStationCache, styleCollectibleCache)
end)
end
local function installDeveloperHooks()
local onlyOnce = true
EVENT_MANAGER:RegisterForEvent(CanICraftThis.addonName, EVENT_PLAYER_ACTIVATED, function()
if onlyOnce then
onlyOnce = false
if CanICraftThis.isHonorGuardCollectibleBugPresent then
CanICraftThis.reportInfo("Honor Guard collectible bug is still present.")
else
CanICraftThis.reportInfo("Honor Guard collectible bug is fixed!")
end
end
end)
end
local function initialise()
local craftingStationCache = CanICraftThis.CraftingStationCache:open()
local styleCollectibleCache = CanICraftThis.StyleCollectibleCache:open()
craftingStationCache:installHook()
styleCollectibleCache:populateIfNeeded()
styleCollectibleCache:verify()
installTooltipExtensionHooks(craftingStationCache, styleCollectibleCache)
if GetDisplayName() == CanICraftThis.authorName then
installDeveloperHooks()
end
end
local function onAddOnLoaded(eventCode, eventAddonName)
if eventAddonName == CanICraftThis.addonName then
initialise()
end
end
EVENT_MANAGER:RegisterForEvent(CanICraftThis.addonName, EVENT_ADD_ON_LOADED, onAddOnLoaded)