@@ -6,6 +6,7 @@ Npc = (function()
66 local talk = Core .talk
77 local checkSoftBoots = Core .checkSoftBoots
88 local getXenoVersion = Core .getXenoVersion
9+ local debug = Core .debug
910 local error = Console .error
1011 local getTotalItemCount = Container .getTotalItemCount
1112 local containerMoveItems = Container .containerMoveItems
@@ -15,11 +16,13 @@ Npc = (function()
1516 local function moveTransactionGoldChange (container , callback )
1617 -- Move loose gold change from container to gold backpack
1718 -- uses main bp if gold backpack isn't assigned.
19+ debug (' moveTransactionGoldChange: ' .. container .. ' , ' .. _backpacks [' Gold' ])
1820 containerMoveItems ({
1921 src = container ,
20- dest = _backpacks [' Gold' ] or _backpacks [ ' Main ' ] ,
22+ dest = _backpacks [' Gold' ],
2123 items = {
22- [3031 ] = true
24+ [3031 ] = true ,
25+ [3035 ] = true
2326 },
2427 disableSourceCascade = true ,
2528 openwindow = false
@@ -115,6 +118,7 @@ Npc = (function()
115118
116119 -- No more to sell
117120 if amount <= 0 then
121+ debug (' shopSellItem: sellItem -> callback()' )
118122 callback ()
119123 return
120124 end
@@ -130,14 +134,17 @@ Npc = (function()
130134 setTimeout (function ()
131135 -- Remaining count to sell, recurse
132136 if remaining > 0 then
137+ debug (' shopSellItem: sellItem()' )
133138 sellItem ()
134139 -- Sold all items, callback
135140 else
141+ debug (' shopSellItem: callback()' )
136142 callback ()
137143 end
138144 end , pingDelay (DELAY .TRADE_TRANSACTION ))
139145 -- Failed to sell, retrying
140146 elseif tries > 0 then
147+ debug (' shopSellItem: retry(' .. tries - 1 .. ' )' )
141148 shopSellItem (itemid , callback , neededCount , tries - 1 )
142149 -- Out of tries. Failed to sell stack.
143150 else
@@ -146,6 +153,7 @@ Npc = (function()
146153 return
147154 end
148155 -- Start recursive selling
156+ debug (' shopSellItem: sellItem() [start]' )
149157 sellItem ()
150158 end
151159
@@ -156,43 +164,52 @@ Npc = (function()
156164 itemlist [# itemlist + 1 ] = itemid
157165 end
158166 local itemcount = # itemlist
167+ debug (' shopSellLoot: itemcount = ' .. itemcount .. ' ' )
159168
160169 function sell (index )
161170 local itemid = itemlist [index ]
162171
163172 -- No more items, finish
164173 if not itemid then
165174 -- Move change to gold
175+ debug (' shopSellLoot: moveTransactionGoldChange()' )
166176 moveTransactionGoldChange (0 , function ()
177+ debug (' shopSellLoot: callback()' )
167178 callback ()
168179 end )
169180 return
170181 end
171182
172183 local amount = shopSellableCount (itemid )
184+ debug (' shopSellLoot: shopSellableCount(' .. itemid .. ' ) = ' .. amount )
173185
174186 if amount > 0 then
175187 shopSellItem (itemid , function ()
188+ debug (' shopSellLoot: shopSellItem(' .. itemid .. ' )' )
176189 -- Recurse to next item in list
177190 setTimeout (function ()
178191 sell (index + 1 )
192+ debug (' shopSellLoot: sell(' .. index + 1 .. ' )' )
179193 end , pingDelay (DELAY .TRADE_TRANSACTION ))
180194 end )
181195 else
182196 -- If we don't have the item, recurse without a wait.
183197 sell (index + 1 )
198+ debug (' shopSellLoot: sell(' .. index + 1 .. ' ) [no item]' )
184199 end
185200 end
186201
187202 if nodialog then
188203 sell (1 )
204+ debug (' shopSellLoot: sell(1)' )
189205 return
190206 end
191207
192208 talk ({' hi' , ' trade' }, function ()
193209 -- Todo: use NPC proxy to verify trade window
194210 setTimeout (function ()
195211 sell (1 )
212+ debug (' shopSellLoot: sell(1) [greet]' )
196213 end , pingDelay (DELAY .RANGE_TALK ))
197214 end )
198215 end
@@ -202,6 +219,7 @@ Npc = (function()
202219 callback ()
203220 return
204221 end
222+ debug (' shopSellFlasks: shopSellLoot()' )
205223 shopSellLoot (ITEM_LIST_FLASKS , callback , true )
206224 end
207225
@@ -245,6 +263,7 @@ Npc = (function()
245263
246264 -- Price not found
247265 if price <= 0 then
266+ debug (' shopBuyItemUpToCount: callback' )
248267 callback ()
249268 return
250269 end
@@ -258,20 +277,24 @@ Npc = (function()
258277 -- Remaining count to buy, continue
259278 if remaining > 0 then
260279 buyItem ()
280+ debug (' shopBuyItemUpToCount -> buyAgain: buyItem()' )
261281 -- Bought all items, destination is not main, callback
262282 elseif destination > 0 then
263283 -- Final cleanup
284+ debug (' shopBuyItemUpToCount -> buyAgain: containerMoveItems()' )
264285 containerMoveItems ({
265286 src = mainbp ,
266287 dest = destination ,
267288 items = {[itemid ] = true },
268289 disableSourceCascade = true ,
269290 openwindow = false
270291 }, function (success )
292+ debug (' shopBuyItemUpToCount -> buyAgain: callback() [move]' )
271293 callback ()
272294 end )
273295 -- Bought all items, destination is main
274296 else
297+ debug (' shopBuyItemUpToCount -> buyAgain: callback()' )
275298 callback ()
276299 end
277300 end
@@ -282,6 +305,7 @@ Npc = (function()
282305 -- and we have less than 3 free slots
283306 local freeSlots = xeno .getContainerItemCapacity (mainbp ) - xeno .getContainerItemCount (mainbp )
284307 if destination > 0 and freeSlots < 4 then
308+ debug (' shopBuyItemUpToCount: containerMoveItems()' )
285309 -- Move to destination after buying stack
286310 containerMoveItems ({
287311 src = mainbp ,
@@ -291,14 +315,17 @@ Npc = (function()
291315 openwindow = false
292316 }, function (success )
293317 buyAgain ()
318+ debug (' shopBuyItemUpToCount: buyAgain() [retry]' )
294319 end )
295320 else
296321 buyAgain ()
322+ debug (' shopBuyItemUpToCount: buyAgain()' )
297323 end
298324 end , pingDelay (DELAY .TRADE_TRANSACTION ))
299325 -- Failed to buy, retrying
300326 elseif tries > 0 then
301327 shopBuyItemUpToCount (itemid , neededCount , destination , callback , tries - 1 )
328+ debug (' shopBuyItemUpToCount: retry(' .. tries - 1 .. ' )' )
302329 -- Out of tries. Failed to buy stack.
303330 else
304331 error (' Failed to buy ' .. xeno .getItemNameByID (itemid ) .. ' (' .. neededStackCount .. ' x). ' .. ' Make sure you have enough capacity and gold.' )
@@ -314,7 +341,9 @@ Npc = (function()
314341 local backpack = _backpacks [group ] and _backpacks [group ] or nil
315342 local function buyListItem (index )
316343 -- Reached end of list, callback
344+
317345 if index > # items then
346+ debug (' shopBuySupplies: callback' )
318347 callback ()
319348 return
320349 end
@@ -324,12 +353,14 @@ Npc = (function()
324353
325354 -- Item doesn't exist or not needed
326355 if not item or not item .needed or item .needed < 1 then
356+ debug (' shopBuySupplies: buyListItem(' .. index + 1 .. ' ) [skip]' )
327357 buyListItem (index + 1 )
328358 return
329359 end
330360
331361 -- Buy item
332362 shopBuyItemUpToCount (item .id , item .needed , backpack , function ()
363+ debug (' shopBuySupplies: buyListItem(' .. index + 1 .. ' )' )
333364 buyListItem (index + 1 )
334365 end )
335366 end
@@ -358,11 +389,13 @@ Npc = (function()
358389 if group == ' Potions' then
359390 talk ({' trade' }, function ()
360391 shopSellFlasks (function ()
392+ debug (' shopBuySupplies: buyListItem(1) [greet, potions]' )
361393 buyListItem (1 )
362394 end )
363395 end )
364396 else
365397 talk ({' trade' }, function ()
398+ debug (' shopBuySupplies: buyListItem(' .. index + 1 .. ' ) [greet]' )
366399 buyListItem (1 )
367400 end )
368401 end
@@ -371,9 +404,11 @@ Npc = (function()
371404 -- Try to sell flasks if we may be at the magic shop
372405 if group == ' Potions' then
373406 shopSellFlasks (function ()
407+ debug (' shopBuySupplies: buyListItem(1) [potions]' )
374408 buyListItem (1 )
375409 end )
376410 else
411+ debug (' shopBuySupplies: buyListItem(1)' )
377412 buyListItem (1 )
378413 end
379414 end
0 commit comments