Repository navigation
Expand file tree
/
Copy pathPlan.lua
More file actions
600 lines (559 loc) · 17.2 KB
/
Copy pathPlan.lua
File metadata and controls
600 lines (559 loc) · 17.2 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
-- The planner: scores every wearable item in the latest Auctionator scan
-- with Pawn's visible scale, compares it with what is equipped, and picks
-- the buys (at most one per slot group) with the largest total Pawn gain
-- that fit the budget. Random-suffix items are priced per version from
-- the /pb scan cache. Results go to the window, to an Auctionator
-- shopping list and, as plain text, to PawnBrokerDB.report.
local _, ns = ...
local BUCKETS = 1000 -- budget resolution of the knapsack
local GEAR_TRIES = 100 -- 0.1 s waits for Pawn to read your gear
local OVER_BUDGET = 5 -- out-of-budget items worth mentioning
local NEAR_MAX = 8 -- near-vendor alternatives worth mentioning
local REUSE = 120 -- seconds a run's scored items serve a quick re-plan
local SLOTS = {
Head = { 1 }, Neck = { 2 }, Shoulder = { 3 }, Chest = { 5 },
Waist = { 6 }, Legs = { 7 }, Feet = { 8 }, Wrist = { 9 },
Hands = { 10 }, Finger = { 11, 12 }, Trinket = { 13, 14 },
Back = { 15 }, Ranged = { 18 },
}
local NOTHING = "Nothing in the scan beats your gear within budget."
local busy = false
local last -- the last full run's context, inputs and time
-- Pawn's unenchanted score, the figure its own upgrade arrows compare.
local function score(item, scale)
local _, value = PawnGetSingleValueFromItem(item, scale)
return value or 0
end
-- nil while the slot holds an item Pawn hasn't read yet. Pawn also gives
-- nothing for an item without stats, which once loaded scores 0.
local function slotScore(slot, scale)
local item, had = PawnGetItemDataForInventorySlot(slot, true)
if item then
return score(item, scale)
end
local link = had and GetInventoryItemLink("player", slot)
if link and not C_Item.GetItemInfo(link) then
return nil
end
return 0
end
-- Scores to beat for each slot group, plus the weapon set-up. A slot
-- Pawn can't read counts as empty and its item goes in base.unread.
local function baselines(scale)
local base = { unread = {} }
local function read(slot)
local value = slotScore(slot, scale)
if value then
return value
end
table.insert(base.unread, GetInventoryItemLink("player", slot))
return 0
end
for group, slots in pairs(SLOTS) do
for _, slot in ipairs(slots) do
local value = read(slot)
base[group] = math.min(base[group] or value, value)
end
end
base.main, base.off = read(16), read(17)
base.twoHanded = ns.twoHanded()
return base
end
-- What an item of this equip location replaces, or nil when it can't be
-- worn with the current weapons (an off hand beside a two-hander).
local function baseline(base, loc)
local group = ns.GROUPS[loc]
if group ~= "Weapon" then
return base[group]
end
if loc == "INVTYPE_2HWEAPON" then
return base.main + base.off
end
if not ns.OFF_HAND[loc] then
return base.main
end
if not base.twoHanded then
return base.off
end
end
local function count(ctx, why)
ctx.skipped[why] = (ctx.skipped[why] or 0) + 1
end
-- Adds o (name, link, search, price, vendor, listed) as an option when Pawn
-- rates it above what it would replace. Its net cost is what it costs once
-- it's sold back to a vendor.
local function offer(ctx, o, loc, beat, level)
local item = PawnGetItemData(o.link)
if not item then
return count(ctx, "unread")
end
local gain = score(item, ctx.scale) - beat
-- Pawn ignores anything less than half a percent better.
if not PawnIsArmorBestTypeForPlayer(item) or gain <= 0
or gain <= beat * 0.005 then
return
end
o.gain, o.beat, o.level, o.group = gain, beat, level, ns.GROUPS[loc]
o.net = o.price - o.vendor
ctx.groups[o.group] = ctx.groups[o.group] or {}
table.insert(ctx.groups[o.group], o)
end
local function inScanRange(ctx, level)
return level >= ctx.level - ns.SCAN_BELOW
and level <= ctx.level + ns.SCAN_AHEAD
end
-- Each cached version of a random-suffix item is its own option. The
-- shopping list searches the base name, which finds every version.
local function offerVariants(ctx, id, loc, beat, level, vendor)
local variants, read = ns.variants(id)
if not variants then
if inScanRange(ctx, level) then
ctx.unscanned = ctx.unscanned + 1
end
return
end
ctx.suffixRead = math.min(ctx.suffixRead or read, read)
local base = C_Item.GetItemInfo(id)
for _, v in ipairs(variants) do
offer(ctx, {
name = v.link:match("%[(.-)%]") or base, link = v.link,
search = base, price = v.price, vendor = vendor,
listed = v.count,
}, loc, beat, level)
end
end
local function consider(ctx, id)
local name, link, quality, _, level, _, _, _, loc, _, sell =
C_Item.GetItemInfo(id)
if not link then
return count(ctx, "unread")
end
level = level or 0
local beat = baseline(ctx.base, loc)
if level > ctx.maxLevel or not beat then
return
end
local verdict = ns.verdict(link, quality)
if verdict.unusable then
return count(ctx, "unusable")
end
if verdict.random then
return offerVariants(ctx, id, loc, beat, level, sell or 0)
end
local price = Auctionator.API.v1.GetAuctionPriceByItemID(ns.ADDON, id)
if price then
offer(ctx, {
name = name, link = link, search = '"' .. name .. '"',
price = price, vendor = sell or 0,
listed = ns.listed(ctx.db, id),
}, loc, beat, level)
end
end
-- Cheapest first, dropping anything a cheaper option already beats, so
-- the strongest option is always last. Cost is the price, or the net cost
-- when planning by net cost.
local function frontier(options)
table.sort(options, function(a, b)
if a.cost ~= b.cost then
return a.cost < b.cost
end
return a.gain > b.gain
end)
local kept, best = {}, 0
for _, o in ipairs(options) do
if o.gain > best then
kept[#kept + 1] = o
best = o.gain
end
end
return kept
end
-- The best gain within capacity c, adding at most one of the options.
local function bestWith(best, options, c, unit)
local gain, pick = best[c], 0
for i, o in ipairs(options) do
local w = math.ceil(o.cost / unit)
if w <= c and best[c - w] + o.gain > gain then
gain, pick = best[c - w] + o.gain, i
end
end
return gain, pick
end
-- Multiple-choice knapsack: at most one option per group, total cost
-- within budget, total gain as high as possible. Costs round up to the
-- bucket size, so a plan never goes over.
local function knapsack(groups, budget)
local unit = math.max(1, math.ceil(budget / BUCKETS))
local cap = math.floor(budget / unit)
local best, picks = {}, {}
for c = 0, cap do
best[c] = 0
end
for g, options in ipairs(groups) do
local nextBest, pick = {}, {}
for c = 0, cap do
nextBest[c], pick[c] = bestWith(best, options, c, unit)
end
best, picks[g] = nextBest, pick
end
local chosen, c = {}, cap
for g = #groups, 1, -1 do
local o = groups[g][picks[g][c]]
if o then
chosen[#chosen + 1] = o
c = c - math.ceil(o.cost / unit)
end
end
return chosen
end
local function strongest(groups)
local chosen = {}
for _, options in ipairs(groups) do
chosen[#chosen + 1] = options[#options]
end
return chosen
end
-- The strongest item per group when it beats what the plan bought there.
local function overBudget(groups, chosen)
local picked, over = {}, {}
for _, o in ipairs(chosen) do
picked[o.group] = o
end
for _, best in ipairs(strongest(groups)) do
if best ~= picked[best.group] then
over[#over + 1] = best
end
end
table.sort(over, function(a, b) return a.gain > b.gain end)
for i = #over, OVER_BUDGET + 1, -1 do
over[i] = nil
end
return over
end
-- Upgrades within the budget that cost at most ctx.near once sold to a
-- vendor: the best one per slot group, unless the plan already buys it.
local function nearVendor(ctx, chosen)
if not ctx.near then
return {}
end
local picked, near = {}, {}
for _, o in ipairs(chosen) do
picked[o.group] = o
end
for group, options in pairs(ctx.groups) do
local best
for _, o in ipairs(options) do
if o.net <= ctx.near and (not ctx.budget or o.cost <= ctx.budget)
and (not best or o.gain > best.gain) then
best = o
end
end
if best and best ~= picked[group] then
near[#near + 1] = best
end
end
table.sort(near, function(a, b) return a.gain > b.gain end)
for i = #near, NEAR_MAX + 1, -1 do
near[i] = nil
end
return near
end
-- "+454%" against what the slot holds, or "empty slot".
function ns.percent(o)
if o.beat <= 0 then
return "empty slot"
end
return string.format("+%d%%", math.floor(o.gain / o.beat * 100 + 0.5))
end
local function describe(o)
return string.format("%s: %s %s, net %s, +%.1f Pawn (%s), req %d%s",
o.group, o.name, ns.money(o.price), ns.netText(o, ns.money), o.gain,
ns.percent(o), o.level,
o.listed and (", " .. o.listed .. " listed") or "")
end
local function header(ctx)
local scale = PawnGetScaleLocalizedName
and PawnGetScaleLocalizedName(ctx.scale) or ctx.scale
if ctx.visible > 1 then
scale = scale .. " (first of " .. ctx.visible
.. " visible scales; hide the others in Pawn to change)"
end
local ahead = ctx.maxLevel > ctx.level
and (", items up to " .. ctx.maxLevel) or ""
local budget = ctx.budget and ns.money(ctx.budget) or "unlimited"
local age = ctx.age == 0 and "today's AH scan"
or (ctx.age .. "-day-old AH scan")
return string.format("%s, level %d%s, %sbudget %s, %s.", scale,
ctx.level, ahead, ctx.net and "net " or "", budget, age)
end
local function unreadLine(base)
local names = {}
for _, link in ipairs(base.unread) do
names[#names + 1] = link:match("%[(.-)%]") or link
end
if #names > 0 then
local slots = #names == 1 and "that slot" or "those slots"
return "Pawn couldn't read your " .. table.concat(names, ", ")
.. ", so the plan treats " .. slots .. " as empty."
end
end
local function skippedLine(skipped)
local parts = {}
if skipped.unusable then
parts[#parts + 1] = skipped.unusable .. " you can't use yet"
end
local unread = skipped.unread
if unread then
parts[#parts + 1] = unread .. " that didn't load (plan again)"
end
if #parts > 0 then
return "Skipped " .. table.concat(parts, ", ") .. "."
end
end
local function suffixLine(ctx)
if ctx.unscanned > 0 then
return ctx.unscanned .. " random-suffix items for your level aren't priced yet. Scan suffixes with the AH open."
end
local notice = ns.suffixNotice()
if notice or not ctx.suffixRead then
return notice
end
local hours = math.floor((time() - ctx.suffixRead) / 3600)
local age = hours < 1 and "under an hour" or ("up to " .. hours .. "h")
return "Random-suffix prices from the suffix scan, " .. age .. " old."
end
local function shoppingList(chosen)
local terms, seen = {}, {}
for _, o in ipairs(chosen) do
if not seen[o.search] then
seen[o.search] = true
terms[#terms + 1] = o.search
end
end
Auctionator.API.v1.CreateShoppingList(ns.ADDON, ns.ADDON, terms)
end
-- "Buy 2 for 1g 40s (net 1g 5s), +24.3 Pawn", led by the net cost when the
-- plan was made by it. money is ns.money or ns.coins.
function ns.buyLine(r, money)
local total, net = money(r.total), ns.netText({ net = r.net }, money)
if r.byNet then
return string.format("Buy %d for %s net (%s up front), +%.1f Pawn",
#r.chosen, net, total, r.gain)
end
return string.format("Buy %d for %s (net %s), +%.1f Pawn", #r.chosen,
total, net, r.gain)
end
-- A plain-text copy of the plan for PawnBrokerDB.report, readable from the
-- saved variables file after a /reload.
local function plainLines(r)
local out = { r.header }
if #r.chosen == 0 then
out[#out + 1] = NOTHING
else
out[#out + 1] = ns.buyLine(r, ns.money) .. ":"
end
for _, o in ipairs(r.chosen) do
out[#out + 1] = " " .. describe(o)
end
if #r.over > 0 then
out[#out + 1] = "Better, but over budget:"
end
for _, o in ipairs(r.over) do
out[#out + 1] = " " .. describe(o)
end
if #r.near > 0 then
out[#out + 1] = string.format("Near vendor price (net %s or less):",
ns.money(r.nearLimit))
end
for _, o in ipairs(r.near) do
out[#out + 1] = " " .. describe(o)
end
for _, note in ipairs(r.notes) do
out[#out + 1] = note
end
return out
end
local function report(ctx, chosen, over)
local r = { header = header(ctx), chosen = chosen, over = over,
near = nearVendor(ctx, chosen), nearLimit = ctx.near,
byNet = ctx.net, notes = {}, total = 0, net = 0, gain = 0 }
for _, o in ipairs(chosen) do
r.total, r.net = r.total + o.price, r.net + o.net
r.gain = r.gain + o.gain
end
local function note(text)
r.notes[#r.notes + 1] = text
end
if ctx.net and r.total > GetMoney() then
note(string.format("That plan needs %s up front; you carry %s.",
ns.money(r.total), ns.money(GetMoney())))
end
note(unreadLine(ctx.base))
note(suffixLine(ctx))
note(skippedLine(ctx.skipped))
if #chosen > 0 then
shoppingList(chosen)
note('Auctionator shopping list "' .. ns.ADDON .. '" updated.')
end
ns.db().report = { time = date("%Y-%m-%d %H:%M"),
lines = plainLines(r) }
ns.ui:ShowPlan(r)
end
local function finish(ctx)
busy = false
local groups = {}
for _, options in pairs(ctx.groups) do
for _, o in ipairs(options) do
o.cost = ctx.net and math.max(0, o.net) or o.price
end
groups[#groups + 1] = frontier(options)
end
local chosen = ctx.budget and knapsack(groups, ctx.budget)
or strongest(groups)
table.sort(chosen, function(a, b) return a.gain > b.gain end)
report(ctx, chosen, overBudget(groups, chosen))
end
-- Builds the run's context, or returns nil and the reason it can't start.
local function prepare(opts)
local missing = ns.missing()
if missing then
return nil, missing
end
local db = ns.auctionator()
local scale, visible = ns.pickScale()
if not scale then
return nil, "no Pawn scale is visible. Show one in /pawn."
end
local ids, age = ns.freshItems(db, scale)
if not age then
return nil, "no AH data. Run a full Auctionator scan first."
end
local level = UnitLevel("player")
return {
db = db, scale = scale, visible = visible,
ids = ids, age = age, level = level,
maxLevel = level + opts.ahead, budget = opts.budget,
near = opts.near, net = opts.net,
groups = {}, skipped = {}, unscanned = 0,
}
end
-- Pawn reads an item only once the client has cached it, which can lag
-- a login or a gear change, so this waits up to GEAR_TRIES tenths of a
-- second before treating any slot it still can't read as empty.
local function readGear(ctx, tries, done)
local ok, err = pcall(function()
ctx.base = baselines(ctx.scale)
end)
if not ok then
busy = false
return geterrorhandler()(err)
end
if #ctx.base.unread == 0 or tries >= GEAR_TRIES then
return done()
end
C_Timer.After(0.1, function() readGear(ctx, tries + 1, done) end)
end
local function loadGear()
for slot = 1, 18 do
local id = GetInventoryItemID("player", slot)
if id and C_Item.RequestLoadItemDataByID then
C_Item.RequestLoadItemDataByID(id)
end
end
end
local function loading(loaded, total)
ns.status(string.format("loading item data %d/%d...", loaded, total))
end
-- What scoring depends on apart from prices. A change here means
-- scoring again rather than re-planning from the last run.
local function inputs(opts)
local parts = { UnitLevel("player"), opts.ahead, ns.pickScale() or "",
ns.suffixStamp(), ns.lastFullScan() }
for slot = 1, 18 do
parts[#parts + 1] = GetInventoryItemLink("player", slot) or ""
end
return table.concat(parts, "|")
end
-- A new budget, net cost or near-vendor limit only changes the final
-- pick, so a recent run's scored items do. Prices from AH searches made
-- since then show at the next full plan.
local function replan(opts)
if not last or GetTime() - last.at > REUSE
or inputs(opts) ~= last.inputs then
return false
end
local ctx = last.ctx
ctx.budget, ctx.net, ctx.near = opts.budget, opts.net, opts.near
finish(ctx)
return true
end
local function run(opts, quick)
if busy then
return ns.status("still working on the last plan.")
end
if quick and replan(opts) then
return
end
local ctx, err = prepare(opts)
if not ctx then
return ns.status(err)
end
local key = inputs(opts)
busy = true
ns.status(string.format("checking %d items...", #ctx.ids))
loadGear()
-- Gear and item data load side by side; scoring starts once both are.
local waiting = 2
local function ready()
waiting = waiting - 1
if waiting > 0 then
return
end
ns.each(ctx.ids, function(id) consider(ctx, id) end,
function()
last = { ctx = ctx, inputs = key, at = GetTime() }
finish(ctx)
end,
function() busy = false end)
end
-- An error here would otherwise leave busy set until a /reload.
local ok, e = pcall(ns.loadItems, ctx.ids, ready, loading)
if not ok then
busy = false
return geterrorhandler()(e)
end
readGear(ctx, 0, ready)
end
-- "1g50s" as copper.
function ns.copper(word)
local g = tonumber(word:match("(%d+)g")) or 0
local s = tonumber(word:match("(%d+)s")) or 0
local c = tonumber(word:match("(%d+)c")) or 0
return g * 10000 + s * 100 + c
end
-- "", "1g50s", "all +2", "net": the budget defaults to the gold you carry,
-- "all" lifts it, +N includes items up to N levels above yours, and "net"
-- plans by net cost (what's left after selling the items to a vendor).
function ns.parse(msg)
local opts = { ahead = 0, budget = GetMoney() }
for word in msg:lower():gmatch("%S+") do
local ahead = word:match("^%+(%d+)$")
if ahead then
opts.ahead = tonumber(ahead)
elseif word == "all" then
opts.budget = nil
elseif word == "net" then
opts.net = true
elseif word:find("^[%dgsc]+$") and word:find("%d[gsc]") then
opts.budget = ns.copper(word)
else
return nil
end
end
return opts
end
-- quick: re-plan from the last run's scored items when nothing they
-- depend on has changed.
function ns.plan(opts, quick)
run(opts, quick)
end