Fix Andesite vault discovery and monitor scaling
This commit is contained in:
+43
-22
@@ -2,8 +2,8 @@
|
|||||||
-- Two Create Item Vaults are discovered by their contents and remembered.
|
-- Two Create Item Vaults are discovered by their contents and remembered.
|
||||||
|
|
||||||
local REFRESH_SECONDS = 1
|
local REFRESH_SECONDS = 1
|
||||||
local MIN_WIDTH = 42
|
local MIN_WIDTH = 28
|
||||||
local MIN_HEIGHT = 15
|
local MIN_HEIGHT = 13
|
||||||
local CLUTCH_OUTPUT_SIDE = "left"
|
local CLUTCH_OUTPUT_SIDE = "left"
|
||||||
local STATE_FILE = "create_andesite_state.txt"
|
local STATE_FILE = "create_andesite_state.txt"
|
||||||
local DEVICE_FILE = "create_andesite_devices.txt"
|
local DEVICE_FILE = "create_andesite_devices.txt"
|
||||||
@@ -142,7 +142,14 @@ local function countItems(inventory, wanted)
|
|||||||
if not inventory then return 0 end
|
if not inventory then return 0 end
|
||||||
local count = 0
|
local count = 0
|
||||||
for _, item in pairs(inventory.list) do
|
for _, item in pairs(inventory.list) do
|
||||||
if wanted[item.name] then count = count + (tonumber(item.count) or 0) end
|
local name = tostring(item.name or ""):lower()
|
||||||
|
local matches = wanted[item.name]
|
||||||
|
if wanted == ZINC_ITEMS and name:find("zinc", 1, true) and name:find("nugget", 1, true) then
|
||||||
|
matches = true
|
||||||
|
elseif wanted == ALLOY_ITEMS and name:find("andesite_alloy", 1, true) then
|
||||||
|
matches = true
|
||||||
|
end
|
||||||
|
if matches then count = count + (tonumber(item.count) or 0) end
|
||||||
end
|
end
|
||||||
return count
|
return count
|
||||||
end
|
end
|
||||||
@@ -155,11 +162,13 @@ local function discoverVaults()
|
|||||||
local oldZinc, oldAlloy = devices.zinc, devices.alloy
|
local oldZinc, oldAlloy = devices.zinc, devices.alloy
|
||||||
local savedZinc = getInventory(devices.zinc)
|
local savedZinc = getInventory(devices.zinc)
|
||||||
local savedAlloy = getInventory(devices.alloy)
|
local savedAlloy = getInventory(devices.alloy)
|
||||||
|
local connectedVaults = {}
|
||||||
|
|
||||||
for _, name in ipairs(peripheral.getNames()) do
|
for _, name in ipairs(peripheral.getNames()) do
|
||||||
if isVault(name) then
|
if isVault(name) then
|
||||||
local inventory = getInventory(name)
|
local inventory = getInventory(name)
|
||||||
if inventory then
|
if inventory then
|
||||||
|
connectedVaults[#connectedVaults + 1] = inventory
|
||||||
if containsItems(inventory, ZINC_ITEMS) then
|
if containsItems(inventory, ZINC_ITEMS) then
|
||||||
devices.zinc = name
|
devices.zinc = name
|
||||||
savedZinc = inventory
|
savedZinc = inventory
|
||||||
@@ -172,6 +181,27 @@ local function discoverVaults()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- This controller intentionally owns exactly two vaults. If a mod supplies
|
||||||
|
-- Zinc Nuggets under an unexpected registry name, the non-alloy vault is
|
||||||
|
-- still unambiguous and becomes the input vault.
|
||||||
|
if not savedZinc and savedAlloy and #connectedVaults == 2 then
|
||||||
|
for _, inventory in ipairs(connectedVaults) do
|
||||||
|
if inventory.name ~= savedAlloy.name then
|
||||||
|
devices.zinc = inventory.name
|
||||||
|
savedZinc = inventory
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not savedAlloy and savedZinc and #connectedVaults == 2 then
|
||||||
|
for _, inventory in ipairs(connectedVaults) do
|
||||||
|
if inventory.name ~= savedZinc.name then
|
||||||
|
devices.alloy = inventory.name
|
||||||
|
savedAlloy = inventory
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if devices.zinc ~= oldZinc or devices.alloy ~= oldAlloy then
|
if devices.zinc ~= oldZinc or devices.alloy ~= oldAlloy then
|
||||||
saveText(DEVICE_FILE, textutils.serialize(devices))
|
saveText(DEVICE_FILE, textutils.serialize(devices))
|
||||||
end
|
end
|
||||||
@@ -198,11 +228,6 @@ local function discoverStressometer()
|
|||||||
return nil, nil, nil, nil
|
return nil, nil, nil, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function percentage(value, maximum)
|
|
||||||
if not maximum or maximum <= 0 then return 0 end
|
|
||||||
return math.min(100, value / maximum * 100)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function evaluate()
|
local function evaluate()
|
||||||
local zincVault, alloyVault = discoverVaults()
|
local zincVault, alloyVault = discoverVaults()
|
||||||
local zinc = countItems(zincVault, ZINC_ITEMS)
|
local zinc = countItems(zincVault, ZINC_ITEMS)
|
||||||
@@ -248,13 +273,10 @@ local function drawInventory(y, label, count, inventory, color)
|
|||||||
center(y, label .. ": NO DATA", colors.red)
|
center(y, label .. ": NO DATA", colors.red)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local fill = percentage(inventory.total, inventory.capacity)
|
local text = ("%s %s / %s"):format(
|
||||||
local fillText = ("%.1f"):format(fill):gsub("%.", ",")
|
|
||||||
local text = ("%s: %s / %s %s%%"):format(
|
|
||||||
label,
|
label,
|
||||||
formatInteger(count),
|
formatInteger(count),
|
||||||
formatInteger(inventory.capacity),
|
formatInteger(inventory.capacity)
|
||||||
fillText
|
|
||||||
)
|
)
|
||||||
center(y, text, color or colors.white)
|
center(y, text, color or colors.white)
|
||||||
end
|
end
|
||||||
@@ -263,7 +285,7 @@ local function drawButton(height, data)
|
|||||||
local width = monitor.getSize()
|
local width = monitor.getSize()
|
||||||
local label = requestedRunning and " CONTROL: ENABLED " or " CONTROL: DISABLED "
|
local label = requestedRunning and " CONTROL: ENABLED " or " CONTROL: DISABLED "
|
||||||
local x = math.max(1, math.floor((width - #label) / 2) + 1)
|
local x = math.max(1, math.floor((width - #label) / 2) + 1)
|
||||||
local y = math.max(14, height - 1)
|
local y = math.max(12, height - 1)
|
||||||
|
|
||||||
button.x1 = x
|
button.x1 = x
|
||||||
button.x2 = math.min(width, x + #label - 1)
|
button.x2 = math.min(width, x + #label - 1)
|
||||||
@@ -285,11 +307,11 @@ local function render()
|
|||||||
|
|
||||||
monitor.setBackgroundColor(colors.black)
|
monitor.setBackgroundColor(colors.black)
|
||||||
monitor.clear()
|
monitor.clear()
|
||||||
center(1, "ANDESITE ALLOY FACTORY", colors.cyan)
|
center(1, "ANDESITE FACTORY", colors.cyan)
|
||||||
|
|
||||||
drawInventory(3, "ZINC NUGGETS", data.zinc, data.zincVault,
|
drawInventory(3, "ZINC", data.zinc, data.zincVault,
|
||||||
data.zinc <= 0 and colors.orange or colors.lime)
|
data.zinc <= 0 and colors.orange or colors.lime)
|
||||||
drawInventory(4, "ANDESITE ALLOY", data.alloy, data.alloyVault,
|
drawInventory(4, "ALLOY", data.alloy, data.alloyVault,
|
||||||
data.alloyVault and data.alloyVault.total >= data.alloyVault.capacity and colors.orange or colors.lime)
|
data.alloyVault and data.alloyVault.total >= data.alloyVault.capacity and colors.orange or colors.lime)
|
||||||
|
|
||||||
center(6, "MACHINE SHAFT", colors.cyan)
|
center(6, "MACHINE SHAFT", colors.cyan)
|
||||||
@@ -299,15 +321,14 @@ local function render()
|
|||||||
center(7, "Speed: NO DATA", colors.red)
|
center(7, "Speed: NO DATA", colors.red)
|
||||||
end
|
end
|
||||||
if data.capacity then
|
if data.capacity then
|
||||||
center(8, "Capacity: " .. formatInteger(data.capacity) .. " SU", colors.white)
|
center(8, "SU: " .. formatInteger(data.usage) .. " / " .. formatInteger(data.capacity), colors.white)
|
||||||
center(9, "Usage: " .. formatInteger(data.usage) .. " SU", colors.white)
|
local loadText = ("Load: %.1f %%"):format(data.load):gsub("%.", ",")
|
||||||
local loadText = ("Load: %.1f %%"):format(data.load):gsub("%.", ",")
|
center(9, loadText, data.load >= 90 and colors.red or data.load >= 70 and colors.orange or colors.lime)
|
||||||
center(10, loadText, data.load >= 90 and colors.red or data.load >= 70 and colors.orange or colors.lime)
|
|
||||||
else
|
else
|
||||||
center(8, "Shaft network offline", colors.gray)
|
center(8, "Shaft network offline", colors.gray)
|
||||||
end
|
end
|
||||||
|
|
||||||
center(12, data.status, data.statusColor)
|
center(11, data.status, data.statusColor)
|
||||||
drawButton(height, data)
|
drawButton(height, data)
|
||||||
center(height, "Touch = Anlage freigeben/stoppen", colors.gray)
|
center(height, "Touch = Anlage freigeben/stoppen", colors.gray)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user