jaklsfjlsak

UI 小地图跳船广播 飞船核心

May 26th, 2025 (edited)
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Mini-Map Guided Ship Jump UI + Chat-Driven Broadcaster
  2.  
  3. -- 1) Wrap terminal + any attached monitors
  4. local screens = {}
  5.  
  6. -- Terminal
  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. -- 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            = 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 (like your warp-system UI)
  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. 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) UI Text
  59. local titleText  = "Mini Map Guided Ship Jump"
  60. local statusText = "Mining Laser Offset (O): "
  61. local indicator  = "OFF"
  62. local promptO    = "Press O to Toggle Offset"
  63. local promptH    = "Press H to Return to Main Menu"
  64.  
  65. -- track last coords for redraw
  66. local lastX, lastY, lastZ = nil, nil, nil
  67.  
  68. -- 5) Draw static UI (title, status, prompts)
  69. local function drawUI()
  70.   forAll(function(s)
  71.     local w,h = s.getSize()
  72.     -- title (line 1)
  73.     local xT = math.floor((w - #titleText)/2) + 1
  74.     s.setTextColor(colors.white); s.setBackgroundColor(colors.black)
  75.     s.setCursorPos(xT,1); s.write(titleText)
  76.  
  77.     -- status (line 4)
  78.     local full = statusText..indicator
  79.     local xS   = math.floor((w - #full)/2) + 1
  80.     s.setTextColor(colors.black); s.setBackgroundColor(colors.lightGray)
  81.     s.setCursorPos(1,4); s.write(string.rep(" ", w))
  82.     s.setCursorPos(xS,4); s.write(statusText)
  83.     s.setTextColor(colors.white); s.setBackgroundColor(colors.red)
  84.     s.write(indicator)
  85.  
  86.     -- prompt O (line h-1)
  87.     s.setTextColor(colors.white); s.setBackgroundColor(colors.blue)
  88.     s.setCursorPos(1,h-1); s.write(string.rep(" ", w))
  89.     local xO2 = math.floor((w - #promptO)/2) + 1
  90.     s.setCursorPos(xO2,h-1); s.write(promptO)
  91.  
  92.     -- prompt H (line h)
  93.     s.setCursorPos(1,h); s.write(string.rep(" ", w))
  94.     local xH2 = math.floor((w - #promptH)/2) + 1
  95.     s.setCursorPos(xH2,h); s.write(promptH)
  96.   end)
  97. end
  98.  
  99. -- 6) Coords line renderer (now on line 6, leaving line 5 blank)
  100. local function updateCoords(x,y,z)
  101.   lastX, lastY, lastZ = x,y,z
  102.   local sx = x and tostring(x) or "    "
  103.   local sy = y and tostring(y) or "    "
  104.   local sz = z and tostring(z) or "    "
  105.   local coordLine = string.format("Target: X:%s   Y:%s   Z:%s", sx, sy, sz)
  106.   forAll(function(s)
  107.     local w,_ = s.getSize()
  108.     s.setTextColor(colors.black); s.setBackgroundColor(colors.lightGray)
  109.     s.setCursorPos(1,6); s.write(string.rep(" ", w))
  110.     local cx = math.floor((w - #coordLine)/2) + 1
  111.     s.setCursorPos(cx,6); s.write(coordLine)
  112.   end)
  113. end
  114.  
  115. -- 7) Initial draw
  116. clearAll()
  117. drawUI()
  118. updateCoords(nil,nil,nil)
  119.  
  120. -- 8) Chat-listener coroutine
  121. local chat = peripheral.find("warpdriveVirtualAssistant")
  122. assert(chat, "warpdriveVirtualAssistant not found!")
  123. chat.name("[name")
  124.  
  125. local function chatListener()
  126.   while true do
  127.     sleep(0.08)
  128.     local ok, CMD = chat.pullLastCommand()
  129.     if ok then
  130.       local txt = string.lower(CMD or "")
  131.       local x_val,y_val,z_val = txt:match(".*x:(%-?%d+),%s*y:(%-?%d+),%s*z:(%-?%d+)")
  132.       if x_val and y_val and z_val then
  133.         local x,y,z = tonumber(x_val), tonumber(y_val), tonumber(z_val)
  134.         print(("Broadcasting coordinates → x:%d, y:%d, z:%d"):format(x,y,z))
  135.         rednet.broadcast({ x=x, y=y, z=z }, "OFFJMcoordBroadcast")
  136.         updateCoords(x,y,z)
  137.       else
  138.         print("No valid coords in command:", CMD)
  139.       end
  140.     end
  141.   end
  142. end
  143.  
  144. -- 9) UI-handler coroutine
  145. local function uiHandler()
  146.   while true do
  147.     local ev, key = os.pullEventRaw("key")
  148.     if key == keys.o then
  149.       shell.run("minonj")
  150.       clearAll(); drawUI(); updateCoords(lastX,lastY,lastZ)
  151.     elseif key == keys.h then
  152.       os.reboot()
  153.     end
  154.   end
  155. end
  156.  
  157. -- 10) Open Rednet on every modem
  158. for _, side in ipairs(peripheral.getNames()) do
  159.   if peripheral.getType(side) == "modem" then
  160.     rednet.open(side)
  161.   end
  162. end
  163.  
  164. -- 11) Run both loops in parallel
  165. parallel.waitForAll(chatListener, uiHandler)
  166.  
Advertisement
Add Comment
Please, Sign In to add comment