jaklsfjlsak

激光扫描发射器 水平采矿 投屏 v3

Apr 23rd, 2025 (edited)
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Laser-Guided Warp System (“ztc”) with H/I/O prompts and O-key to run otc
  2.  
  3. -- 1) Wrap terminal + any attached monitors
  4. local screens = {}
  5.  
  6. -- Terminal wrapper
  7. table.insert(screens, {
  8.   clear              = function() term.clear(); term.setCursorPos(1,1) end,
  9.   getSize            = function() return term.getSize() end,
  10.   setTextColor       = term.setTextColor,
  11.   setBackgroundColor = term.setBackgroundColor,
  12.   setCursorPos       = term.setCursorPos,
  13.   write              = term.write,
  14. })
  15.  
  16. -- Monitor wrappers
  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            = function() return m.getSize() end,
  23.       setTextColor       = function(c) m.setTextColor(c) end,
  24.       setBackgroundColor = function(c) m.setBackgroundColor(c) end,
  25.       setCursorPos       = function(x,y) m.setCursorPos(x,y) end,
  26.       write              = function(txt) m.write(txt) end,
  27.       monitor            = m,
  28.     })
  29.   end
  30. end
  31.  
  32. -- 2) Auto-scale monitors so at least 7 lines will 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) Helpers
  49. local function forAll(fn) for _,s in ipairs(screens) do fn(s) end end
  50.  
  51. -- Clear every screen to lightGray
  52. local function clearAll()
  53.   forAll(function(s)
  54.     s.setTextColor(colors.black)
  55.     s.setBackgroundColor(colors.lightGray)
  56.     s.clear()
  57.   end)
  58. end
  59.  
  60. -- 4) Static texts
  61. local titleText     = "Horizontal Mining System"
  62. local subtitleText  = "Emit Scanning Laser to Mine Target"
  63. local offsetText    = "Guided Mining System ( I ): "
  64. local promptH       = "Press H for Main Menu"
  65. local promptI       = "Press J to Switch to Laser Guided Jump"
  66. local promptO       = "Press T to Toggle Miner"
  67.  
  68. -- track offset state
  69. local isOffsetOn = false
  70.  
  71. -- Draw title, subtitle, and offset-state line (now on line 5)
  72. local function drawStatic()
  73.   forAll(function(s)
  74.     local w,_ = s.getSize()
  75.     -- Title (line 1): white on black
  76.     local xT = math.floor((w - #titleText)/2) + 1
  77.     s.setTextColor(colors.white)
  78.     s.setBackgroundColor(colors.black)
  79.     s.setCursorPos(xT,1)
  80.     s.write(titleText)
  81.  
  82.     -- Subtitle (line 3): black on lightGray
  83.     local xS = math.floor((w - #subtitleText)/2) + 1
  84.     s.setTextColor(colors.black)
  85.     s.setBackgroundColor(colors.lightGray)
  86.     s.setCursorPos(xS,3)
  87.     s.write(subtitleText)
  88.  
  89.     -- (line 4 is intentionally left blank)
  90.  
  91.     -- Mining Laser Offset (line 5)
  92.     local statusText = isOffsetOn and "ON" or "OFF"
  93.     local fullOffset = offsetText .. statusText
  94.     local xO = math.floor((w - #fullOffset)/2) + 1
  95.  
  96.     -- background bar
  97.     s.setTextColor(colors.black)
  98.     s.setBackgroundColor(colors.lightGray)
  99.     s.setCursorPos(1,5)
  100.     s.write(string.rep(" ", w))
  101.  
  102.     -- label
  103.     s.setCursorPos(xO,5)
  104.     s.write(offsetText)
  105.  
  106.     -- ON vs OFF coloring
  107.     s.setTextColor(colors.white)
  108.     s.setBackgroundColor(isOffsetOn and colors.lime or colors.red)
  109.     s.write(statusText)
  110.   end)
  111. end
  112.  
  113. -- Draw prompts on the bottom three rows
  114. local function drawPrompt()
  115.   forAll(function(s)
  116.     local w,h = s.getSize()
  117.     -- line h-2: O prompt
  118.     s.setTextColor(colors.white)
  119.     s.setBackgroundColor(colors.blue)
  120.     s.setCursorPos(1,h-2)
  121.     s.write(string.rep(" ", w))
  122.     local xO = math.floor((w - #promptO)/2) + 1
  123.     s.setCursorPos(xO,h-2)
  124.     s.write(promptO)
  125.     -- line h-1: H prompt
  126.     s.setTextColor(colors.white)
  127.     s.setBackgroundColor(colors.blue)
  128.     s.setCursorPos(1,h-1)
  129.     s.write(string.rep(" ", w))
  130.     local xH = math.floor((w - #promptH)/2) + 1
  131.     s.setCursorPos(xH,h-1)
  132.     s.write(promptH)
  133.     -- line h: I prompt
  134.     s.setBackgroundColor(colors.blue)
  135.     s.setCursorPos(1,h)
  136.     s.write(string.rep(" ", w))
  137.     local xI = math.floor((w - #promptI)/2) + 1
  138.     s.setCursorPos(xI,h)
  139.     s.write(promptI)
  140.   end)
  141. end
  142.  
  143. -- 5) Set up all modems for rednet, then broadcast a startup check
  144. for _, side in ipairs(peripheral.getNames()) do
  145.   if peripheral.getType(side) == "modem" then
  146.     rednet.open(side)
  147.   end
  148. end
  149. rednet.broadcast("check", "SMiner")
  150.  
  151. -- now set up warpdriveLaserCamera peripherals
  152. local cams = peripheral.getNames()
  153. for i=#cams,1,-1 do
  154.   if peripheral.getType(cams[i]) ~= "warpdriveLaserCamera" then
  155.     table.remove(cams,i)
  156.   else
  157.     peripheral.wrap(cams[i]).beamFrequency(1420)
  158.   end
  159. end
  160.  
  161. -- 6) Initial draw (blank coords)
  162. clearAll()
  163. drawStatic()
  164. drawPrompt()
  165.  
  166. -- 7) Coords line, accepts nil → blanks before first scan (on line 6)
  167. local function updateCoords(x,y,z)
  168.   local sx = x and tostring(x) or "    "
  169.   local sy = y and tostring(y) or "    "
  170.   local sz = z and tostring(z) or "    "
  171.   local coordLine = string.format("Target: X:%s   Y:%s   Z:%s", sx, sy, sz)
  172.   forAll(function(s)
  173.     local w,_ = s.getSize()
  174.     s.setTextColor(colors.black)
  175.     s.setBackgroundColor(colors.lightGray)
  176.     s.setCursorPos(1,6)
  177.     s.write(string.rep(" ", w))
  178.     local cx = math.floor((w - #coordLine)/2) + 1
  179.     s.setCursorPos(cx,6)
  180.     s.write(coordLine)
  181.   end)
  182. end
  183.  
  184. -- draw initial blanks (on line 6)
  185. updateCoords(nil,nil,nil)
  186.  
  187. -- keep last coords around for redraws
  188. local lastX, lastY, lastZ = nil, nil, nil
  189.  
  190. -- 8) Main loop: scan + key handling + status listener
  191. while true do
  192.   -- 8a) non-blocking check for SMiner status messages
  193.   local sender, msg, proto = rednet.receive("SMiner", 0)
  194.   if msg == "StatON" then
  195.     isOffsetOn = true
  196.     clearAll(); drawStatic(); updateCoords(lastX,lastY,lastZ); drawPrompt()
  197.   elseif msg == "StatOFF" then
  198.     isOffsetOn = false
  199.     clearAll(); drawStatic(); updateCoords(lastX,lastY,lastZ); drawPrompt()
  200.   end
  201.  
  202.   -- 8b) main events
  203.   local ev,k,b,c,d = os.pullEventRaw()
  204.   if ev == "laserScanning" then
  205.     lastX = tonumber(b)
  206.     lastY = tonumber(c)
  207.     lastZ = tonumber(d)
  208.     updateCoords(lastX,lastY,lastZ)
  209.     rednet.broadcast({ x = lastX, y = lastY, z = lastZ }, "SMineBroadcast")
  210.  
  211.   elseif ev == "key" then
  212.     if k == keys.h then
  213.       os.reboot()
  214.     elseif k == keys.j then
  215.     shell.run("otc")
  216. clearAll(); drawStatic(); updateCoords(tx,ty,tz); drawPrompt()
  217.     elseif k == keys.t then
  218.       rednet.broadcast("Toggle","SMiner")
  219.     end
  220.   end
  221. end
  222.  
Advertisement
Add Comment
Please, Sign In to add comment