Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
95 lines (95 loc) · 4.8 KB
/
Copy pathmain.lua
File metadata and controls
95 lines (95 loc) · 4.8 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
-- Ctrl+Shift+F6 requests one default use of the first matching inventory item.
local row = config.int("QuickPotion", "ItemRow", -1)
local pending, loaded, frame = nil, false, 0
local function log(s) bsx.log("quick_potion: " .. s) end
local function cancel(why)
if pending then log("cancelled: " .. why) end
pending = nil
end
local function offset(name) return assert(cdal.value(name)) end
local function ptr(at)
local p, why = mem.read_ptr(at)
assert(p and p ~= 0, why or "null pointer")
return p
end
local function u16(at) return assert(mem.read_u8(at)) + 256 * assert(mem.read_u8(at + 1)) end
local function player()
local actor = assert(game.player())
return actor, assert(mem.read_u32(actor + offset("Actor::Key")))
end
local function count()
local n = tonumber(assert(game.inventory.count(row)))
assert(n and n >= 0 and n <= 9007199254740991, "count exceeds exact Lua range")
return n
end
local function first_item(actor)
local holder = ptr(ptr(actor + offset("Actor::Components")) + offset("ActorComponents::Inventory"))
assert(ptr(holder) == cdal.address("pa::ClientInventoryActorComponent::vftable"), "inventory type")
local n = assert(mem.read_u32(holder + offset("InventoryHolder::BucketCount")))
assert(n <= 256, "bucket bound")
local buckets = n > 0 and ptr(holder + offset("InventoryHolder::Buckets")) or 0
local visited = 0
for b = 0, n - 1 do
local bucket = ptr(buckets + b * 8)
local capacity = u16(bucket + offset("InventoryBucket::SlotCount"))
visited = visited + capacity
assert(capacity <= 4096 and visited <= 4096, "inventory bound")
local slots = capacity > 0 and ptr(bucket) or 0
for i = 0, capacity - 1 do
local item = slots + i * offset("InventoryItem::Stride")
local amount = assert(mem.read_u64(item + offset("InventoryItem::Amount")))
if u16(item + offset("InventoryItem::Row")) == row and amount ~= "0"
and (#amount < 19 or (#amount == 19 and amount < "9223372036854775808")) then
return u16(bucket + 0x10), i -- external bucket tag, not ordinal
end
end
end
error("configured item is absent")
end
local events, event_error = pcall(function()
bsx.on("quick_slot_use_after", function(e)
if not pending or e.storage ~= pending.bucket or e.index ~= pending.index
or e.amount ~= "1" or e.use_parameter ~= 65535 then return end
log("QUICK_SLOT_USE_AFTER (22) result=" .. e.result .. "; return observation only")
pending.returned = true
end)
bsx.on("save_loaded", function() cancel("replacement save"); loaded = true end)
bsx.on("save_load", function() cancel("save request"); loaded = false end)
bsx.on("load_request", function() cancel("loading request"); loaded = false end)
bsx.on("title_screen", function() cancel("title screen"); loaded = false end)
end)
if not events then log("required events unavailable: " .. tostring(event_error)) end
if not game.request then log("read-only; changes approval required") end
if row < 0 or row > 65534 then log("choose ItemRow in settings.ini and approve its digest") end
bsx.on("key", function(e)
if e.virtual_key ~= 117 or e.modifiers ~= 5 or not e.down or e['repeat'] or e.reset then return end
if not events or not game.request or row < 0 or row > 65534 then return end
if not loaded then log("wait for a loaded save and normal player control"); return end
if pending then log("already waiting; no additional request"); return end
local ok, why = pcall(function()
local actor, key = player()
local bucket, index = first_item(actor)
pending = {actor = actor, key = key, bucket = bucket, index = index,
before = count(), deadline = frame + 600, returned = false}
local queued, reason = game.request.use_item(bucket, index)
if not queued then cancel("request refused or uncertain: " .. tostring(reason)); return end
log("queued row=" .. row .. " bucket=" .. bucket .. " index=" .. index)
end)
if not ok then cancel(tostring(why)); log("request stopped: " .. tostring(why)) end
end)
bsx.on("frame", function(e)
frame = e.frame
if not pending then return end
local ok, why = pcall(function()
local actor, key = player()
assert(actor == pending.actor and key == pending.key, "player changed")
local now = count()
if pending.returned and now == pending.before - 1 then
log("inventory row decreased by one; verify the potion effect in game")
pending = nil
elseif now ~= pending.before and now ~= pending.before - 1 then
cancel("inventory changed unexpectedly")
elseif frame >= pending.deadline then cancel("timeout; no automatic retry") end
end)
if not ok then cancel(tostring(why)) end
end)