1local imbuingWindow
2local bankGold = 0
3local inventoryGold = 0
4local itemImbuements = {}
5local emptyImbue
6local groupsCombo
7local imbueLevelsCombo
8local protectionBtn
9local clearImbue
10local selectedImbue
11local imbueItems = {}
12local protection = false
13local clearConfirmWindow
14local imbueConfirmWindow
15
16function init()
17 connect(g_game, {
18 onGameEnd = hide,
19 onResourceBalance = onResourceBalance,
20 onImbuementWindow = onImbuementWindow,
21 onCloseImbuementWindow = onCloseImbuementWindow
22 })
23
24 imbuingWindow = g_ui.displayUI('imbuing')
25 emptyImbue = imbuingWindow.emptyImbue
26 groupsCombo = emptyImbue.groups
27 imbueLevelsCombo = emptyImbue.imbuement
28 protectionBtn = emptyImbue.protection
29 clearImbue = imbuingWindow.clearImbue
30 imbuingWindow:hide()
31
32 groupsCombo.onOptionChange = function(widget)
33 imbueLevelsCombo:clear()
34 if itemImbuements ~= nil then
35 local selectedGroup = groupsCombo:getCurrentOption().text
36 for _,imbuement in ipairs(itemImbuements) do
37 if imbuement["group"] == selectedGroup then
38 emptyImbue.imbuement:addOption(imbuement["name"])
39 end
40 end
41 imbueLevelsCombo.onOptionChange(imbueLevelsCombo) -- update options
42 end
43 end
44
45 imbueLevelsCombo.onOptionChange = function(widget)
46 setProtection(false)
47 local selectedGroup = groupsCombo:getCurrentOption().text
48 for _,imbuement in ipairs(itemImbuements) do
49 if imbuement["group"] == selectedGroup then
50 if #imbuement["sources"] == widget.currentIndex then
51 selectedImbue = imbuement
52 for i,source in ipairs(imbuement["sources"]) do
53 for _,item in ipairs(imbueItems) do
54 if item:getId() == source["item"]:getId() then
55 if item:getCount() >= source["item"]:getCount() then
56 emptyImbue.imbue:setImageSource("/images/game/imbuing/imbue_green")
57 emptyImbue.imbue:setEnabled(true)
58 emptyImbue.requiredItems:getChildByIndex(i).count:setColor("white")
59 end
60 if item:getCount() < source["item"]:getCount() then
61 emptyImbue.imbue:setEnabled(false)
62 emptyImbue.imbue:setImageSource("/images/game/imbuing/imbue_empty")
63 emptyImbue.requiredItems:getChildByIndex(i).count:setColor("red")
64 end
65 emptyImbue.requiredItems:getChildByIndex(i).count:setText(item:getCount() .. "/" .. source["item"]:getCount())
66 end
67 end
68 emptyImbue.requiredItems:getChildByIndex(i).item:setItemId(source["item"]:getId())
69 emptyImbue.requiredItems:getChildByIndex(i).item:setTooltip("The imbuement requires " .. source["description"] .. ".")
70 end
71 for i = 3, widget.currentIndex + 1, -1 do
72 emptyImbue.requiredItems:getChildByIndex(i).count:setText("")
73 emptyImbue.requiredItems:getChildByIndex(i).item:setItemId(0)
74 emptyImbue.requiredItems:getChildByIndex(i).item:setTooltip("")
75 end
76 emptyImbue.protectionCost:setText(imbuement["protectionCost"])
77 emptyImbue.cost:setText(imbuement["cost"])
78 if not protection and (bankGold + inventoryGold) < imbuement["cost"] then
79 emptyImbue.imbue:setEnabled(false)
80 emptyImbue.imbue:setImageSource("/images/game/imbuing/imbue_empty")
81 emptyImbue.cost:setColor("red")
82 end
83 if not protection and (bankGold + inventoryGold) >= imbuement["cost"] then
84 emptyImbue.cost:setColor("white")
85 end
86 if protection and (bankGold + inventoryGold) < (imbuement["cost"] + imbuement["protectionCost"]) then
87 emptyImbue.imbue:setEnabled(false)
88 emptyImbue.imbue:setImageSource("/images/game/imbuing/imbue_empty")
89 emptyImbue.cost:setColor("red")
90 end
91 if protection and (bankGold + inventoryGold) >= (imbuement["cost"] + imbuement["protectionCost"]) then
92 emptyImbue.cost:setColor("white")
93 end
94 emptyImbue.successRate:setText(imbuement["successRate"] .. "%")
95 if selectedImbue["successRate"] > 50 then
96 emptyImbue.successRate:setColor("white")
97 else
98 emptyImbue.successRate:setColor("red")
99 end
100 emptyImbue.description:setText(imbuement["description"])
101 end
102 end
103 end
104 end
105
106 protectionBtn.onClick = function()
107 setProtection(not protection)
108 end
109end
110
111function setProtection(value)
112 protection = value
113 if protection then
114 emptyImbue.cost:setText(selectedImbue["cost"] + selectedImbue["protectionCost"])
115 emptyImbue.successRate:setText("100%")
116 emptyImbue.successRate:setColor("green")
117 protectionBtn:setImageClip(torect("66 0 66 66"))
118 else
119 if selectedImbue then
120 emptyImbue.cost:setText(selectedImbue["cost"])
121 emptyImbue.successRate:setText(selectedImbue["successRate"] .. "%")
122 if selectedImbue["successRate"] > 50 then
123 emptyImbue.successRate:setColor("white")
124 else
125 emptyImbue.successRate:setColor("red")
126 end
127 end
128 protectionBtn:setImageClip(torect("0 0 66 66"))
129 end
130end
131
132function terminate()
133 disconnect(g_game, {
134 onGameEnd = hide,
135 onResourceBalance = onResourceBalance,
136 onImbuementWindow = onImbuementWindow,
137 onCloseImbuementWindow = onCloseImbuementWindow
138 })
139
140 imbuingWindow:destroy()
141end
142
143function resetSlots()
144 emptyImbue:setVisible(false)
145 clearImbue:setVisible(false)
146 for i=1,3 do
147 imbuingWindow.itemInfo.slots:getChildByIndex(i):setText("Slot " .. i)
148 imbuingWindow.itemInfo.slots:getChildByIndex(i):setEnabled(false)
149 imbuingWindow.itemInfo.slots:getChildByIndex(i):setTooltip("Items can have up to three imbuements slots. This slot is not available for this item.")
150 imbuingWindow.itemInfo.slots:getChildByIndex(i).onClick = nil
151 end
152end
153
154function selectSlot(widget, slotId, activeSlot)
155 if activeSlot then
156 emptyImbue:setVisible(false)
157 widget:setText(activeSlot[1]["name"])
158 clearImbue.title:setText('Clear Imbuement "' .. activeSlot[1]["name"] .. '"')
159 clearImbue.groups:clear()
160 clearImbue.groups:addOption(activeSlot[1]["group"])
161 clearImbue.imbuement:clear()
162 clearImbue.imbuement:addOption(activeSlot[1]["name"])
163 clearImbue.description:setText(activeSlot[1]["description"])
164
165 hours = string.format("%02.f", math.floor(activeSlot[2]/3600))
166 mins = string.format("%02.f", math.floor(activeSlot[2]/60 - (hours*60)))
167 clearImbue.time.timeRemaining:setText(hours..":"..mins.."h")
168
169 clearImbue.cost:setText(activeSlot[3])
170 if (bankGold + inventoryGold) < activeSlot[3] then
171 emptyImbue.clear:setEnabled(false)
172 emptyImbue.clear:setImageSource("/images/game/imbuing/imbue_empty")
173 emptyImbue.cost:setColor("red")
174 end
175
176 local yesCallback = function()
177 g_game.clearImbuement(slotId)
178 widget:setText("Slot " .. (slotId + 1))
179 if clearConfirmWindow then
180 clearConfirmWindow:destroy()
181 clearConfirmWindow=nil
182 end
183 end
184 local noCallback = function()
185 imbuingWindow:show()
186 if clearConfirmWindow then
187 clearConfirmWindow:destroy()
188 clearConfirmWindow=nil
189 end
190 end
191
192 clearImbue.clear.onClick = function()
193 imbuingWindow:hide()
194 clearConfirmWindow = displayGeneralBox(tr('Confirm Clearing'), tr('Do you wish to spend ' .. activeSlot[3] .. ' gold coins to clear the imbuement "' .. activeSlot[1]["name"] .. '" from your item?'), {
195 { text=tr('Yes'), callback=yesCallback },
196 { text=tr('No'), callback=noCallback },
197 anchor=AnchorHorizontalCenter}, yesCallback, noCallback)
198 end
199
200 clearImbue:setVisible(true)
201 else
202 emptyImbue:setVisible(true)
203 clearImbue:setVisible(false)
204
205 local yesCallback = function()
206 g_game.applyImbuement(slotId, selectedImbue["id"], protection)
207 if clearConfirmWindow then
208 clearConfirmWindow:destroy()
209 clearConfirmWindow=nil
210 end
211 widget:setText(selectedImbue["name"])
212 imbuingWindow:show()
213 end
214 local noCallback = function()
215 imbuingWindow:show()
216 if clearConfirmWindow then
217 clearConfirmWindow:destroy()
218 clearConfirmWindow=nil
219 end
220 end
221
222 emptyImbue.imbue.onClick = function()
223 imbuingWindow:hide()
224 local cost = selectedImbue["cost"]
225 local successRate = selectedImbue["successRate"]
226 if protection then
227 cost = cost + selectedImbue["protectionCost"]
228 successRate = "100"
229 end
230 clearConfirmWindow = displayGeneralBox(tr('Confirm Imbuing Attempt'), 'You are about to imbue your item with "' .. selectedImbue["name"] .. '".\nYour chance to succeed is ' .. successRate .. '%. It will consume the required astral sources and '.. cost ..' gold coins.\nDo you wish to proceed?', {
231 { text=tr('Yes'), callback=yesCallback },
232 { text=tr('No'), callback=noCallback },
233 anchor=AnchorHorizontalCenter}, yesCallback, noCallback)
234 end
235 end
236end
237
238function onImbuementWindow(itemId, slots, activeSlots, imbuements, needItems)
239 if not itemId then
240 return
241 end
242 resetSlots()
243 imbueItems = table.copy(needItems)
244 imbuingWindow.itemInfo.item:setItemId(itemId)
245
246 for i=1, slots do
247 local slot = imbuingWindow.itemInfo.slots:getChildByIndex(i)
248 slot.onClick = function(widget)
249 selectSlot(widget, i - 1)
250 end
251 slot:setTooltip("Use this slot to imbue your item. Depending on the item you can have up to three different imbuements.")
252 slot:setEnabled(true)
253
254 if slot:getId() == "slot0" then
255 selectSlot(slot, i - 1)
256 end
257 end
258
259 for i, slot in pairs(activeSlots) do
260 local activeSlotBtn = imbuingWindow.itemInfo.slots:getChildById("slot" .. i)
261 activeSlotBtn.onClick = function(widget)
262 selectSlot(widget, i, slot)
263 end
264 if activeSlotBtn:getId() == "slot0" then
265 selectSlot(activeSlotBtn, i, slot)
266 end
267 end
268
269 if imbuements ~= nil then
270 groupsCombo:clear()
271 imbueLevelsCombo:clear()
272 itemImbuements = table.copy(imbuements)
273 for _,imbuement in ipairs(itemImbuements) do
274 if not groupsCombo:isOption(imbuement["group"]) then
275 groupsCombo:addOption(imbuement["group"])
276 end
277 end
278 end
279 show()
280end
281
282function onResourceBalance(type, balance)
283 if type == 0 then
284 bankGold = balance
285 elseif type == 1 then
286 inventoryGold = balance
287 end
288 if type == 0 or type == 1 then
289 imbuingWindow.balance:setText(tr("Balance") .. ":\n" .. (bankGold + inventoryGold))
290 end
291end
292
293function onCloseImbuementWindow()
294 resetSlots()
295end
296
297function hide()
298 g_game.closeImbuingWindow()
299 imbuingWindow:hide()
300end
301
302function show()
303 imbuingWindow:show()
304 imbuingWindow:raise()
305 imbuingWindow:focus()
306end
307
308function toggle()
309 if imbuingWindow:isVisible() then
310 return hide()
311 end
312 show()
313end