jaklsfjlsak

护盾控制 红网投屏发射

May 6th, 2025 (edited)
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. -- Horizontal Mining Offset UI with Toggle & Status Monitoring Only
  2.  
  3. -- 1) Screen wrappers (computer + monitors)
  4. local screens = {}
  5.  
  6. -- wrap the terminal itself
  7. table.insert(screens, {
  8.   clear              = function() term.clear(); term.setCursorPos(1,1) end,
  9.   getSize            = term.getSize,
  10.   setTextColor       = term.setTextColor,
  11.   setBackgroundColor = term.setBackgroundColor,
  12.   setCursorPos       = term.setCursorPos,
  13.   write              = term.write,
  14. })
  15.  
  16. -- wrap any attached monitors
  17. for _, side in ipairs(peripheral.getNames()) do
  18.   if peripheral.getType(side) == "monitor" then
  19.     local m = peripheral.wrap(side)
  20.     table.insert(screens, {
  21.       clear              = function() m.clear(); m.setCursorPos(1,1) end,
  22.       getSize            = m.getSize,
  23.       setTextColor       = m.setTextColor,
  24.       setBackgroundColor = m.setBackgroundColor,
  25.       setCursorPos       = m.setCursorPos,
  26.       write              = m.write,
  27.       monitor            = m,
  28.     })
  29.   end
  30. end
  31.  
  32. -- 2) Auto-scale monitors so at least 7 lines fit
  33. for _, s in ipairs(screens) do
  34.   if s.monitor then
  35.     local best = 0.5
  36.     for _, scale in ipairs({1, 0.5}) do
  37.       s.monitor.setTextScale(scale)
  38.       local w,h = s.getSize()
  39.       if w >= 40 and h >= 7 then
  40.         best = scale
  41.         break
  42.       end
  43.     end
  44.     s.monitor.setTextScale(best)
  45.   end
  46. end
  47.  
  48. -- 3) Drawing helpers
  49. local function forAll(fn) for _,s in ipairs(screens) do fn(s) end end
  50. local function clearAll()
  51.   forAll(function(s)
  52.     s.setTextColor(colors.black)
  53.     s.setBackgroundColor(colors.lightGray)
  54.     s.clear()
  55.   end)
  56. end
  57.  
  58. -- 4) Static UI texts
  59. local titleText    = "Horizontal Mining System"
  60. local subtitleText = "Guided Mining Offset Only"
  61. local offsetText   = "Offset Mode (T): "
  62. local promptH      = "Press H for Main Menu"
  63. local promptT      = "Press T to Toggle Offset"
  64.  
  65. -- offset state
  66. local isOffsetOn = false
  67.  
  68. -- 5) Draw header + offset-state line
  69. local function drawStatic()
  70.   forAll(function(s)
  71.     local w,_ = s.getSize()
  72.     -- Title (line 1)
  73.     s.setTextColor(colors.white); s.setBackgroundColor(colors.black)
  74.     s.setCursorPos(math.floor((w-#titleText)/2)+1,1); s.write(titleText)
  75.     -- Subtitle (line 3)
  76.     s.setTextColor(colors.black); s.setBackgroundColor(colors.lightGray)
  77.     s.setCursorPos(math.floor((w-#subtitleText)/2)+1,3); s.write(subtitleText)
  78.     -- Blank line 4
  79.     -- Offset-state (line 5)
  80.     local status = isOffsetOn and "ON" or "OFF"
  81.     local full   = offsetText..status
  82.     s.setTextColor(colors.black); s.setBackgroundColor(colors.lightGray)
  83.     s.setCursorPos(1,5); s.write(string.rep(" ", w))
  84.     s.setCursorPos(math.floor((w-#full)/2)+1,5); s.write(offsetText)
  85.     s.setTextColor(colors.white)
  86.     s.setBackgroundColor(isOffsetOn and colors.lime or colors.red)
  87.     s.write(status)
  88.   end)
  89. end
  90.  
  91. -- 6) Draw bottom prompt lines (lines h-1 and h)
  92. local function drawPrompt()
  93.   forAll(function(s)
  94.     local w,h = s.getSize()
  95.     -- line h-1: Toggle (T)
  96.     s.setTextColor(colors.white); s.setBackgroundColor(colors.blue)
  97.     s.setCursorPos(1,h-1); s.write(string.rep(" ",w))
  98.     s.setCursorPos(math.floor((w-#promptT)/2)+1,h-1); s.write(promptT)
  99.     -- line h: Main menu (H)
  100.     s.setTextColor(colors.white); s.setBackgroundColor(colors.blue)
  101.     s.setCursorPos(1,h);   s.write(string.rep(" ",w))
  102.     s.setCursorPos(math.floor((w-#promptH)/2)+1,h);   s.write(promptH)
  103.   end)
  104. end
  105.  
  106. -- 7) Open all modems for Rednet and send initial check
  107. for _, side in ipairs(peripheral.getNames()) do
  108.   if peripheral.getType(side) == "modem" then
  109.     rednet.open(side)
  110.   end
  111. end
  112. rednet.broadcast("check", "SMiner")
  113.  
  114. -- 8) Initial draw
  115. clearAll()
  116. drawStatic()
  117. drawPrompt()
  118.  
  119. -- 9) Listen for SMiner status updates so we can refresh UI if another controller toggles
  120. local function statusListener()
  121.   while true do
  122.     local _, msg = rednet.receive("SMiner")
  123.     if msg == "StatON" then
  124.       isOffsetOn = true
  125.       clearAll(); drawStatic(); drawPrompt()
  126.     elseif msg == "StatOFF" then
  127.       isOffsetOn = false
  128.       clearAll(); drawStatic(); drawPrompt()
  129.     end
  130.   end
  131. end
  132.  
  133. -- 10) Main event loop: handle key presses to toggle or reboot
  134. local function mainLoop()
  135.   while true do
  136.     local ev, key = os.pullEvent("key")
  137.     if key == keys.t then
  138.       -- flip local state, broadcast to others, and redraw
  139.       isOffsetOn = not isOffsetOn
  140.       rednet.broadcast(isOffsetOn and "StatON" or "StatOFF", "SMiner")
  141.       clearAll(); drawStatic(); drawPrompt()
  142.     elseif key == keys.h then
  143.       os.reboot()
  144.     end
  145.   end
  146. end
  147.  
  148. -- 11) Run both loops in parallel
  149. parallel.waitForAny(statusListener, mainLoop)
  150.  
Advertisement
Add Comment
Please, Sign In to add comment