agot137

turtle_manager.lua

Aug 4th, 2025 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. -- Turtle Manager for EXTERNAL Advanced Monitor (right)
  2. local modemSide = "top"
  3. local monitorSide = "right"
  4. local SIZE = 5
  5.  
  6. -- Peripheral wrap monitor
  7. local monitor = peripheral.wrap(monitorSide)
  8. assert(monitor ~= nil, "No advanced monitor found on the right!")
  9. monitor.setTextScale(1) -- Optional: Set monitor text scale
  10. term.redirect(monitor)
  11.  
  12. -- Colors
  13. local COLOR_BG = colors.black
  14. local COLOR_TITLE_BG = colors.cyan
  15. local COLOR_TITLE_FG = colors.white
  16. local COLOR_ICON_ACTIVE = colors.orange
  17. local COLOR_ACTIVE_FG = colors.black
  18. local COLOR_LIST_BG = colors.gray
  19. local COLOR_LIST_FG = colors.white
  20. local COLOR_BTN_BG = colors.blue
  21. local COLOR_BTN_FG = colors.white
  22.  
  23. local width, height = monitor.getSize()
  24. local turtlestatus = {}
  25. rednet.open(modemSide)
  26.  
  27. local function drawTurtleIcon(x, y)
  28. monitor.setCursorPos(x, y)
  29. monitor.setBackgroundColor(COLOR_ICON_ACTIVE)
  30. monitor.setTextColor(COLOR_ACTIVE_FG)
  31. monitor.write(" ")
  32. monitor.setCursorPos(x, y+1)
  33. monitor.write("TT")
  34. monitor.setBackgroundColor(COLOR_BG)
  35. end
  36.  
  37. local function drawButton(x, y, text, keychar)
  38. monitor.setCursorPos(x, y)
  39. monitor.setBackgroundColor(COLOR_BTN_BG)
  40. monitor.setTextColor(COLOR_BTN_FG)
  41. monitor.write("["..keychar.."] "..text)
  42. monitor.setBackgroundColor(COLOR_BG)
  43. end
  44.  
  45. local function calcRelPos(data)
  46. local rel_x, rel_z = 1,1
  47. if data.layerStartX and data.layerStartZ then
  48. rel_x = (data.x - data.layerStartX) + 1
  49. rel_z = (data.z - data.layerStartZ) + 1
  50. elseif data.rel_x and data.rel_z then
  51. rel_x = data.rel_x
  52. rel_z = data.rel_z
  53. else
  54. rel_x = (data.x % SIZE) + 1
  55. rel_z = (data.z % SIZE) + 1
  56. end
  57. return rel_x, rel_z
  58. end
  59.  
  60. local function drawList()
  61. monitor.setBackgroundColor(COLOR_BG)
  62. monitor.clear()
  63. monitor.setCursorPos(1,1)
  64. monitor.setBackgroundColor(COLOR_TITLE_BG)
  65. monitor.setTextColor(COLOR_TITLE_FG)
  66. monitor.write(string.rep(" ",width))
  67. monitor.setCursorPos(math.floor((width-18)/2), 1)
  68. monitor.write(" Turtle Manager ")
  69. monitor.setBackgroundColor(COLOR_BG)
  70. local idx = 0
  71. for id, data in pairs(turtlestatus) do
  72. if data.active then
  73. idx = idx + 1
  74. local ypos = 2 + (idx-1)*3
  75. drawTurtleIcon(3, ypos)
  76. monitor.setCursorPos(7, ypos)
  77. monitor.setBackgroundColor(COLOR_LIST_BG)
  78. monitor.setTextColor(COLOR_LIST_FG)
  79. monitor.write("Turtle #"..id)
  80. monitor.setCursorPos(7, ypos+1)
  81. local rel_x, rel_z = calcRelPos(data)
  82. monitor.write(string.format("Pos: %3d,%3d,%3d Grid: %d,%d Fuel: %d",
  83. data.x or 0, data.y or 0, data.z or 0, rel_x, rel_z, data.fuel or 0))
  84. monitor.setBackgroundColor(COLOR_BG)
  85. end
  86. end
  87. monitor.setCursorPos(1,height)
  88. monitor.setTextColor(colors.lightGray)
  89. monitor.setBackgroundColor(COLOR_BG)
  90. monitor.write("On external monitor, 5x5 map, BOTTOM LEFT is (1,1)")
  91. end
  92.  
  93. local function drawDetail(id, data)
  94. monitor.setBackgroundColor(COLOR_BG)
  95. monitor.clear()
  96. monitor.setCursorPos(1,1)
  97. monitor.setBackgroundColor(COLOR_TITLE_BG)
  98. monitor.setTextColor(COLOR_TITLE_FG)
  99. monitor.write(string.rep(" ",width))
  100. monitor.setCursorPos(math.floor((width-18)/2), 1)
  101. monitor.write(" Turtle Manager ")
  102. monitor.setBackgroundColor(COLOR_BG)
  103. drawTurtleIcon(4,3)
  104. monitor.setCursorPos(8, 3)
  105. monitor.setBackgroundColor(COLOR_LIST_BG)
  106. monitor.setTextColor(COLOR_LIST_FG)
  107. monitor.write("Turtle #" .. id)
  108. monitor.setCursorPos(8,4)
  109. local rel_x, rel_z = calcRelPos(data)
  110. monitor.write(string.format("XYZ: %d,%d,%d Level: %d Fuel: %d",
  111. data.x or 0, data.y or 0, data.z or 0, data.y or 0, data.fuel or 0))
  112. -- 5x5 grid, bottom left (1,1) -- top left is (1,5)
  113. for dz=SIZE,1,-1 do
  114. monitor.setCursorPos(10,6+SIZE-dz+1)
  115. for dx=1,SIZE do
  116. if dx==rel_x and dz==rel_z then
  117. monitor.setBackgroundColor(colors.lime)
  118. monitor.setTextColor(colors.black)
  119. monitor.write("X")
  120. else
  121. monitor.setBackgroundColor(colors.gray)
  122. monitor.setTextColor(colors.white)
  123. monitor.write(".")
  124. end
  125. end
  126. end
  127. monitor.setBackgroundColor(COLOR_BG)
  128. drawButton(12, 18, "Home", "H")
  129. drawButton(32, 18, "Resume", "B")
  130. drawButton(57, 18, "Back", "SPACE")
  131. end
  132.  
  133. local function findTurtleAtClick(mx, my)
  134. local idx = 0
  135. for id, data in pairs(turtlestatus) do
  136. if data.active then
  137. idx=idx+1
  138. local yy = 2+(idx-1)*3
  139. if mx>=3 and mx<=4 and (my == yy or my == yy+1) then
  140. return id, data
  141. end
  142. end
  143. end
  144. return nil
  145. end
  146.  
  147. function turtle_home(id) rednet.send(id,"home","turtle_cmd") end
  148. function turtle_resume(id) rednet.send(id,"resume","turtle_cmd") end
  149.  
  150. local function main()
  151. local lastDraw = 0
  152. local detailId = nil
  153. while true do
  154. local ev = {os.pullEventRaw()}
  155. if ev[1]=="rednet_message" and ev[4]=="turtle_status" then
  156. local id = ev[2]
  157. local data = textutils.unserialize(ev[3]) or {}
  158. data.active = true
  159. turtlestatus[id] = data
  160. if os.clock()-lastDraw > 1 then drawList() lastDraw = os.clock() end
  161. elseif ev[1]=="monitor_touch" and ev[2]==monitorSide then
  162. local mx, my = ev[3], ev[4]
  163. if detailId then
  164. if mx >= 12 and mx <= 24 and my == 18 then
  165. turtle_home(detailId)
  166. elseif mx >= 32 and mx <= 54 and my == 18 then
  167. turtle_resume(detailId)
  168. elseif mx >= 57 and my == 18 then
  169. detailId = nil
  170. drawList()
  171. end
  172. else
  173. local id, data = findTurtleAtClick(mx,my)
  174. if id then
  175. detailId = id
  176. drawDetail(id, data)
  177. end
  178. end
  179. elseif ev[1]=="key" and detailId then
  180. local key = ev[2]
  181. if key==keys.h then
  182. turtle_home(detailId)
  183. elseif key==keys.b then
  184. turtle_resume(detailId)
  185. elseif key==keys.space then
  186. detailId = nil
  187. drawList()
  188. end
  189. end
  190. if os.clock()-lastDraw>1 then
  191. if detailId and turtlestatus[detailId] then
  192. drawDetail(detailId, turtlestatus[detailId])
  193. else
  194. drawList()
  195. end
  196. lastDraw = os.clock()
  197. end
  198. os.startTimer(0.1)
  199. end
  200. end
  201.  
  202. monitor.setBackgroundColor(COLOR_BG)
  203. monitor.setTextColor(COLOR_TITLE_FG)
  204. monitor.clear()
  205. main()
  206.  
Advertisement
Add Comment
Please, Sign In to add comment