robathome

RoboMiner_remote

Oct 18th, 2025 (edited)
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. -- pocket_startup.lua
  2. -- Pocket controller for CC:Tweaked. REPL to drive miners via the base controller.
  3. -- Requires: pocket with Ender (wireless) modem upgrade.
  4.  
  5. -- Protocols (match your controller)
  6. local PROTOCOL = "minesys:v1" -- miner heartbeats (read-only here)
  7. local CTL_PROTO = "minesys:ctl" -- controller RPC (goto, clear, face, fwd, up, dn)
  8.  
  9. -- ---- init ----
  10. local modem = peripheral.find("modem", function(_,p) return p.isWireless() end)
  11. if not modem then error("No wireless modem on pocket") end
  12. rednet.open(peripheral.getName(modem))
  13. pcall(function() rednet.host("minesys:pocket", "pocket_"..(os.getComputerLabel() or os.getComputerID())) end)
  14.  
  15. -- ---- state ----
  16. local function now() return os.epoch("utc") end
  17. local turtles = {} -- by label or id: {id,label,pos,fuel,freePct,status,seen}
  18.  
  19. local function upsertStatus(m)
  20. local key = m.label or m.id
  21. if not key then return end
  22. local t = turtles[key] or {}
  23. t.id = m.id or t.id
  24. t.label = m.label or t.label or ("t"..tostring(m.id or ""))
  25. t.pos = m.pos or t.pos
  26. t.fuel = m.fuel or t.fuel
  27. t.status = m.status or t.status
  28. if m.extra and m.extra.inv then t.freePct = m.extra.inv.freePct end
  29. t.seen = now()
  30. turtles[key] = t
  31. end
  32.  
  33. -- ---- UI helpers ----
  34. local function listTurtles()
  35. term.clear(); term.setCursorPos(1,1)
  36. print("Turtles (label fuel free% x,y,z status):")
  37. for _,t in pairs(turtles) do
  38. local pos = t.pos and (t.pos.x..","..t.pos.y..","..t.pos.z) or "?, ?, ?"
  39. local free = t.freePct and string.format("%d", math.floor(t.freePct+0.5)) or "--"
  40. print(string.format("%-14s %-6s %-4s %s [%s]",
  41. t.label or "?", tostring(t.fuel or "?"), free, pos, t.status or "?"))
  42. end
  43. end
  44.  
  45. local function help()
  46. print("list")
  47. print("fac <label> <N|E|S|W>")
  48. print("fwd <label> <n>")
  49. print("up <label> <n>")
  50. print("dn <label> <n>")
  51. print("clear <label> <L> <W> <H> <N|E|S|W>")
  52. print("goto <label> <x> <y> <z> [order yxz|xyz|...]\n")
  53. end
  54.  
  55. -- ---- controller RPC ----
  56. local function ctlReq(payload)
  57. payload.op = "req"
  58. payload.ref = math.random(1,1e9)
  59. rednet.broadcast(payload, CTL_PROTO)
  60. local tm = os.startTimer(4)
  61. while true do
  62. local ev,a,b,c = os.pullEvent()
  63. if ev=="rednet_message" then
  64. local sid,msg,proto = a,b,c
  65. if proto==CTL_PROTO and type(msg)=="table" and msg.op=="resp" and msg.ref==payload.ref then
  66. return msg.ok, msg.err
  67. end
  68. elseif ev=="timer" and a==tm then
  69. return false, "timeout"
  70. end
  71. end
  72. end
  73.  
  74. -- ---- REPL command wrappers ----
  75. local function cmd_face(lab, dir)
  76. if dir=="" then print("need dir"); return end
  77. local ok,err = ctlReq({act="face", label=lab, dir=dir})
  78. print(ok and "ok" or ("err: "..tostring(err)))
  79. end
  80.  
  81. local function cmd_fwd(lab, n)
  82. local ok,err = ctlReq({act="fwd", label=lab, n=tonumber(n) or 1})
  83. print(ok and "ok" or ("err: "..tostring(err)))
  84. end
  85.  
  86. local function cmd_up(lab, n)
  87. local ok,err = ctlReq({act="up", label=lab, n=tonumber(n) or 1})
  88. print(ok and "ok" or ("err: "..tostring(err)))
  89. end
  90.  
  91. local function cmd_dn(lab, n)
  92. local ok,err = ctlReq({act="dn", label=lab, n=tonumber(n) or 1})
  93. print(ok and "ok" or ("err: "..tostring(err)))
  94. end
  95.  
  96. local function cmd_clear(lab, L, W, H, dir)
  97. local ok,err = ctlReq({
  98. act="clear", label=lab,
  99. L=tonumber(L), W=tonumber(W), H=tonumber(H), dir=dir
  100. })
  101. print(ok and "ok" or ("err: "..tostring(err)))
  102. end
  103.  
  104. local function parseOrder(ord)
  105. if ord and #ord==3 then return {ord:sub(1,1), ord:sub(2,2), ord:sub(3,3)} end
  106. return nil
  107. end
  108.  
  109. local function cmd_goto(lab,x,y,z,ord)
  110. local function parseOrder(o) return (o and #o==3) and {o:sub(1,1),o:sub(2,2),o:sub(3,3)} or nil end
  111. local ok,err = ctlReq({act="goto", label=lab, x=tonumber(x), y=tonumber(y), z=tonumber(z), order=parseOrder(ord)})
  112. print(ok and "ok" or ("err: "..tostring(err)))
  113. end
  114.  
  115. -- ---- threads ----
  116. local function inputThread()
  117. term.clear(); term.setCursorPos(1,1)
  118. print("Pocket miner controller. type 'help'.")
  119. while true do
  120. io.write("> ")
  121. local line = read()
  122. local a,b,c,d,e,f = line:match("^(%S+)%s*(%S*)%s*(%S*)%s*(%S*)%s*(%S*)%s*(%S*)")
  123. if a=="help" then help()
  124. elseif a=="list" then listTurtles()
  125. elseif a=="fac" then cmd_face(b,c)
  126. elseif a=="fwd" then cmd_fwd(b,c)
  127. elseif a=="up" then cmd_up(b,c)
  128. elseif a=="dn" then cmd_dn(b,c)
  129. elseif a=="clear" then cmd_clear(b,c,d,e,f)
  130. elseif a=="goto" then cmd_goto(b,c,d,e,f)
  131. elseif a=="" then
  132. else print("unknown") end
  133. end
  134. end
  135.  
  136. local function netThread()
  137. while true do
  138. local id,msg,proto = rednet.receive(PROTOCOL, 5)
  139. if id and proto==PROTOCOL and type(msg)=="table" and msg.op=="status" then
  140. upsertStatus(msg)
  141. end
  142. end
  143. end
  144.  
  145. parallel.waitForAny(inputThread, netThread)
  146.  
Advertisement
Add Comment
Please, Sign In to add comment