robathome

AutoMapper_remoteCode

Oct 12th, 2025
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1.  
  2. -- ===== CONFIG =====
  3. local CENTRAL_ID = nil -- set with :cid <id>
  4. local PROTO_CMD = "central_cmd" -- protocol for sending commands to central
  5. local TIMEOUT_S = 2
  6. -- ==================
  7.  
  8. -- open wireless modem
  9. rednet.close()
  10. for _,s in ipairs(rs.getSides()) do if peripheral.getType(s)=="modem" then rednet.open(s) end end
  11. assert(rednet.isOpen(), "No wireless modem. Use an Advanced Wireless Pocket Computer.")
  12.  
  13. -- ui state
  14. local W,H = term.getSize()
  15. local log = {}
  16. local input = ""
  17. local cursor = 1
  18. local running = true
  19.  
  20. local function push(msg)
  21. log[#log+1] = msg
  22. if #log > 100 then table.remove(log,1) end
  23. end
  24.  
  25. local function redraw()
  26. term.setBackgroundColor(colors.black)
  27. term.setTextColor(colors.white)
  28. term.clear()
  29. W,H = term.getSize()
  30. -- header
  31. term.setCursorPos(1,1)
  32. term.setTextColor(colors.yellow)
  33. local hdr = ("central:%s proto:%s"):format(CENTRAL_ID and tostring(CENTRAL_ID) or "none", PROTO_CMD)
  34. term.write(hdr)
  35. -- log
  36. term.setTextColor(colors.white)
  37. local maxLines = math.max(0, H-3)
  38. local start = math.max(1, #log - maxLines + 1)
  39. local y = 2
  40. for i=start,#log do
  41. term.setCursorPos(1,y)
  42. term.write(("%-"..W.."s"):format(tostring(log[i]):sub(1,W)))
  43. y=y+1
  44. end
  45. -- input line
  46. term.setCursorPos(1,H)
  47. term.setBackgroundColor(colors.blue)
  48. term.setTextColor(colors.white)
  49. term.write((":%s"):format(input))
  50. term.setBackgroundColor(colors.black)
  51. term.setCursorPos(2+cursor, H)
  52. end
  53.  
  54. local function send_line(line)
  55. if not CENTRAL_ID then push("ERR: set central id with :cid <id>"); return end
  56. local ok = rednet.send(CENTRAL_ID, {type="cmd", line=line}, PROTO_CMD)
  57. if not ok then push("ERR: rednet.send failed") else push("> "..line) end
  58. end
  59.  
  60. local function ping()
  61. if not CENTRAL_ID then push("ERR: no central id") return end
  62. rednet.send(CENTRAL_ID, {type="ping"}, PROTO_CMD)
  63. local id,msg,proto = rednet.receive(PROTO_CMD, TIMEOUT_S)
  64. if id==CENTRAL_ID and type(msg)=="table" and msg.type=="pong" then
  65. push("pong:"..(msg.note or "ok"))
  66. else
  67. push("no pong")
  68. end
  69. end
  70.  
  71. local function help()
  72. push("Commands:")
  73. push(":cid <id> set central computer ID")
  74. push(":ping test connection")
  75. push(":proto <name> set protocol (default central_cmd)")
  76. push(":send <text> send raw line")
  77. push(":help show this help")
  78. push("Shortcuts (mapped to central commands):")
  79. push(" cam yaw <deg> | cam pitch <deg> | cam dist <n>")
  80. push(" track on|off | highlight x y z [r] | clear highlights")
  81. push(" center x y z | tid <id> | tfind")
  82. push(" fwd/back/up/down <n> | left/right | halt | goto x y z")
  83. end
  84.  
  85. local function parse_local(cmdline)
  86. local args = {}
  87. for w in tostring(cmdline):gmatch("%S+") do args[#args+1]=w end
  88. if #args==0 then return end
  89. local c = args[1]:lower()
  90.  
  91. if c=="cid" and args[2] then CENTRAL_ID = tonumber(args[2]); push("central id="..tostring(CENTRAL_ID)); return
  92. elseif c=="ping" then ping(); return
  93. elseif c=="proto" and args[2] then PROTO_CMD = args[2]; push("proto="..PROTO_CMD); return
  94. elseif c=="send" then table.remove(args,1); send_line(table.concat(args, " ")); return
  95. elseif c=="help" or c=="?" then help(); return end
  96.  
  97. -- otherwise treat whole line as central command
  98. send_line(table.concat(args, " "))
  99. end
  100.  
  101. -- receive loop (acks or server help)
  102. local function net_loop()
  103. while running do
  104. local id,msg,proto = rednet.receive(PROTO_CMD, 0.2)
  105. if id and type(msg)=="table" then
  106. if msg.type=="ack" then push("ack:"..tostring(msg.note or msg.line or "ok"))
  107. elseif msg.type=="err" then push("err:"..tostring(msg.note or ""))
  108. elseif msg.type=="info" then push(tostring(msg.text or ""))
  109. elseif msg.type=="pong" then push("pong")
  110. end
  111. end
  112. end
  113. end
  114.  
  115. -- input loop
  116. local function input_loop()
  117. redraw()
  118. while running do
  119. local e = {os.pullEvent()}
  120. if e[1]=="char" then
  121. input = input .. e[2]
  122. cursor = #input
  123. redraw()
  124. elseif e[1]=="key" then
  125. local k=e[2]
  126. if k==keys.enter then
  127. local line = input
  128. input=""; cursor=0
  129. redraw()
  130. parse_local(line)
  131. redraw()
  132. elseif k==keys.backspace then
  133. input = input:sub(1, math.max(0,#input-1))
  134. cursor = #input
  135. redraw()
  136. elseif k==keys.q then
  137. running=false
  138. end
  139. elseif e[1]=="term_resize" then
  140. redraw()
  141. end
  142. end
  143. end
  144.  
  145. parallel.waitForAny(net_loop, input_loop)
  146. term.setBackgroundColor(colors.black); term.setTextColor(colors.white); term.clear()
  147. print("Console closed.")
  148.  
Advertisement
Add Comment
Please, Sign In to add comment