Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
83 lines (80 loc) · 3.75 KB
/
Copy pathmain.lua
File metadata and controls
83 lines (80 loc) · 3.75 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
-- Copy to BSX/scripts/supply_check and choose exact item rows in settings.ini.
-- Each count is the controlled character's own inventory, not the HUD total.
local configured = config.string("SupplyCheck", "Rows", "")
local minimum = config.int("SupplyCheck", "Min", 5)
local minutes = config.int("SupplyCheck", "IntervalMinutes", 5)
local function parse_rows(text)
if text == "" then return {} end
local rows, seen, start = {}, {}, 1
while true do
local comma = string.find(text, ",", start, true)
local token = string.sub(text, start, comma and comma - 1 or #text)
local left, right = 1, #token
while left <= right and (string.byte(token, left) == 32 or string.byte(token, left) == 9) do left = left + 1 end
while right >= left and (string.byte(token, right) == 32 or string.byte(token, right) == 9) do right = right - 1 end
if left > right then return nil, "Rows contains an empty row" end
local row = 0
for i = left, right do
local digit = string.byte(token, i)
if digit < 48 or digit > 57 then return nil, "Rows must contain decimal row numbers" end
row = row * 10 + digit - 48
if row > 65534 then return nil, "row must be 0..65534" end
end
if not seen[row] then
if #rows == 8 then return nil, "Rows allows at most 8 distinct rows" end
rows[#rows + 1], seen[row] = row, true
end
if not comma then return rows end
start = comma + 1
end
end
local rows, rows_why = parse_rows(configured)
if not rows then
bsx.log("supply check: " .. tostring(rows_why))
elseif minimum < 0 or minimum > 9007199254740991 then
bsx.log("supply check: Min must be a nonnegative exact integer")
elseif minutes < 1 or minutes > 1440 then
bsx.log("supply check: IntervalMinutes must be 1..1440")
elseif #rows > 0 then
-- Counts are decimal strings; compare their lengths and digits without rounding.
local min_text = string.format("%.0f", minimum)
local function below(count)
return #count < #min_text or (#count == #min_text and count < min_text)
end
local active, pending = false, false
local request_check
request_check = function()
if active then pending = true; return end
active = true
local index, low, failures = 1, {}, {}
local function next_row()
local row = rows[index]
local count, why = game.inventory.count(row)
if count == nil then
failures[#failures + 1] = "row " .. row .. " count unavailable (" .. string.sub(tostring(why), 1, 80) .. ")"
elseif below(count) then
local name, name_why = game.item_name(row)
if not name then name = "row " .. row .. " (name unavailable: " .. tostring(name_why) .. ")" end
low[#low + 1] = string.sub(name, 1, 90) .. "=" .. count
end
index = index + 1
if index <= #rows then
-- A large inventory is read once per row; spread checks over frames.
bsx.after(0, next_row)
return
end
if #failures > 0 then bsx.log("supply check: " .. table.concat(failures, ", ")) end
if #low > 0 then bsx.log("supply check: low on: " .. table.concat(low, ", ")) end
active = false
if pending then pending = false; bsx.after(0, request_check) end
end
next_row()
end
local ok, why = pcall(bsx.on, "save_loaded", request_check)
if not ok then bsx.log("supply check: save_loaded unavailable (" .. tostring(why) .. "); timer remains active") end
local function poll()
request_check()
bsx.after(minutes * 60000, poll)
end
bsx.after(minutes * 60000, poll)
end