fix: enhance monitor detection to handle adjacent peripherals correctly
This commit is contained in:
@@ -120,7 +120,24 @@ end
|
|||||||
-- Monitor setup
|
-- Monitor setup
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
local function findMonitor(side, excludeSide)
|
--- Check whether two peripheral names refer to the same physical block.
|
||||||
|
-- Needed because a monitor placed adjacent to the computer AND connected
|
||||||
|
-- via wired modem appears under both its side name (e.g. "left") and its
|
||||||
|
-- network name (e.g. "monitor_0"). A simple string comparison misses this.
|
||||||
|
local function sameMonitor(a, b)
|
||||||
|
if not a or not b then return false end
|
||||||
|
if a == b then return true end
|
||||||
|
-- Write a distinctive cursor position via one name and read it back
|
||||||
|
-- via the other; if they share state, they are the same peripheral.
|
||||||
|
local ok1 = pcall(peripheral.call, a, "setCursorPos", 9876, 5432)
|
||||||
|
if not ok1 then return false end
|
||||||
|
local ok2, x, y = pcall(peripheral.call, b, "getCursorPos")
|
||||||
|
pcall(peripheral.call, a, "setCursorPos", 1, 1)
|
||||||
|
return ok2 and x == 9876 and y == 5432
|
||||||
|
end
|
||||||
|
|
||||||
|
local function findMonitor(side, excludeNames)
|
||||||
|
excludeNames = excludeNames or {}
|
||||||
local mon = peripheral.wrap(side)
|
local mon = peripheral.wrap(side)
|
||||||
local monName
|
local monName
|
||||||
if mon and mon.setTextScale then
|
if mon and mon.setTextScale then
|
||||||
@@ -130,10 +147,16 @@ local function findMonitor(side, excludeSide)
|
|||||||
end
|
end
|
||||||
if not mon then
|
if not mon then
|
||||||
for _, name in ipairs(peripheral.getNames()) do
|
for _, name in ipairs(peripheral.getNames()) do
|
||||||
if peripheral.getType(name) == "monitor" and name ~= excludeSide then
|
if peripheral.getType(name) == "monitor" then
|
||||||
mon = peripheral.wrap(name)
|
local dominated = false
|
||||||
monName = name
|
for _, ex in ipairs(excludeNames) do
|
||||||
break
|
if sameMonitor(name, ex) then dominated = true; break end
|
||||||
|
end
|
||||||
|
if not dominated then
|
||||||
|
mon = peripheral.wrap(name)
|
||||||
|
monName = name
|
||||||
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -141,7 +164,7 @@ local function findMonitor(side, excludeSide)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function D.setupMonitor()
|
function D.setupMonitor()
|
||||||
D.mon, D.monName = findMonitor(cfg.MONITOR_SIDE, cfg.SMELTER_MONITOR_SIDE)
|
D.mon, D.monName = findMonitor(cfg.MONITOR_SIDE, { cfg.SMELTER_MONITOR_SIDE })
|
||||||
if not D.mon then return false end
|
if not D.mon then return false end
|
||||||
|
|
||||||
mainDevice = UI.Device({
|
mainDevice = UI.Device({
|
||||||
@@ -152,7 +175,7 @@ function D.setupMonitor()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function D.setupSmelterMonitor()
|
function D.setupSmelterMonitor()
|
||||||
D.smelterMon, D.smelterMonName = findMonitor(cfg.SMELTER_MONITOR_SIDE, D.monName)
|
D.smelterMon, D.smelterMonName = findMonitor(cfg.SMELTER_MONITOR_SIDE, { D.monName })
|
||||||
if not D.smelterMon then return false end
|
if not D.smelterMon then return false end
|
||||||
|
|
||||||
smelterDevice = UI.Device({
|
smelterDevice = UI.Device({
|
||||||
@@ -178,8 +201,8 @@ function D.setupBillboardMonitor()
|
|||||||
-- Auto-detect: find any monitor not already used by main/smelter
|
-- Auto-detect: find any monitor not already used by main/smelter
|
||||||
for _, name in ipairs(peripheral.getNames()) do
|
for _, name in ipairs(peripheral.getNames()) do
|
||||||
if peripheral.getType(name) == "monitor"
|
if peripheral.getType(name) == "monitor"
|
||||||
and name ~= D.monName
|
and not sameMonitor(name, D.monName)
|
||||||
and name ~= D.smelterMonName then
|
and not sameMonitor(name, D.smelterMonName) then
|
||||||
local mon = peripheral.wrap(name)
|
local mon = peripheral.wrap(name)
|
||||||
if mon and mon.setTextScale then
|
if mon and mon.setTextScale then
|
||||||
D.billboardMon = mon
|
D.billboardMon = mon
|
||||||
|
|||||||
Reference in New Issue
Block a user