Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Manager for EXTERNAL Advanced Monitor (right)
- local modemSide = "top"
- local monitorSide = "right"
- local SIZE = 5
- -- Peripheral wrap monitor
- local monitor = peripheral.wrap(monitorSide)
- assert(monitor ~= nil, "No advanced monitor found on the right!")
- monitor.setTextScale(1) -- Optional: Set monitor text scale
- term.redirect(monitor)
- -- Colors
- local COLOR_BG = colors.black
- local COLOR_TITLE_BG = colors.cyan
- local COLOR_TITLE_FG = colors.white
- local COLOR_ICON_ACTIVE = colors.orange
- local COLOR_ACTIVE_FG = colors.black
- local COLOR_LIST_BG = colors.gray
- local COLOR_LIST_FG = colors.white
- local COLOR_BTN_BG = colors.blue
- local COLOR_BTN_FG = colors.white
- local width, height = monitor.getSize()
- local turtlestatus = {}
- rednet.open(modemSide)
- local function drawTurtleIcon(x, y)
- monitor.setCursorPos(x, y)
- monitor.setBackgroundColor(COLOR_ICON_ACTIVE)
- monitor.setTextColor(COLOR_ACTIVE_FG)
- monitor.write(" ")
- monitor.setCursorPos(x, y+1)
- monitor.write("TT")
- monitor.setBackgroundColor(COLOR_BG)
- end
- local function drawButton(x, y, text, keychar)
- monitor.setCursorPos(x, y)
- monitor.setBackgroundColor(COLOR_BTN_BG)
- monitor.setTextColor(COLOR_BTN_FG)
- monitor.write("["..keychar.."] "..text)
- monitor.setBackgroundColor(COLOR_BG)
- end
- local function calcRelPos(data)
- local rel_x, rel_z = 1,1
- if data.layerStartX and data.layerStartZ then
- rel_x = (data.x - data.layerStartX) + 1
- rel_z = (data.z - data.layerStartZ) + 1
- elseif data.rel_x and data.rel_z then
- rel_x = data.rel_x
- rel_z = data.rel_z
- else
- rel_x = (data.x % SIZE) + 1
- rel_z = (data.z % SIZE) + 1
- end
- return rel_x, rel_z
- end
- local function drawList()
- monitor.setBackgroundColor(COLOR_BG)
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.setBackgroundColor(COLOR_TITLE_BG)
- monitor.setTextColor(COLOR_TITLE_FG)
- monitor.write(string.rep(" ",width))
- monitor.setCursorPos(math.floor((width-18)/2), 1)
- monitor.write(" Turtle Manager ")
- monitor.setBackgroundColor(COLOR_BG)
- local idx = 0
- for id, data in pairs(turtlestatus) do
- if data.active then
- idx = idx + 1
- local ypos = 2 + (idx-1)*3
- drawTurtleIcon(3, ypos)
- monitor.setCursorPos(7, ypos)
- monitor.setBackgroundColor(COLOR_LIST_BG)
- monitor.setTextColor(COLOR_LIST_FG)
- monitor.write("Turtle #"..id)
- monitor.setCursorPos(7, ypos+1)
- local rel_x, rel_z = calcRelPos(data)
- monitor.write(string.format("Pos: %3d,%3d,%3d Grid: %d,%d Fuel: %d",
- data.x or 0, data.y or 0, data.z or 0, rel_x, rel_z, data.fuel or 0))
- monitor.setBackgroundColor(COLOR_BG)
- end
- end
- monitor.setCursorPos(1,height)
- monitor.setTextColor(colors.lightGray)
- monitor.setBackgroundColor(COLOR_BG)
- monitor.write("On external monitor, 5x5 map, BOTTOM LEFT is (1,1)")
- end
- local function drawDetail(id, data)
- monitor.setBackgroundColor(COLOR_BG)
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.setBackgroundColor(COLOR_TITLE_BG)
- monitor.setTextColor(COLOR_TITLE_FG)
- monitor.write(string.rep(" ",width))
- monitor.setCursorPos(math.floor((width-18)/2), 1)
- monitor.write(" Turtle Manager ")
- monitor.setBackgroundColor(COLOR_BG)
- drawTurtleIcon(4,3)
- monitor.setCursorPos(8, 3)
- monitor.setBackgroundColor(COLOR_LIST_BG)
- monitor.setTextColor(COLOR_LIST_FG)
- monitor.write("Turtle #" .. id)
- monitor.setCursorPos(8,4)
- local rel_x, rel_z = calcRelPos(data)
- monitor.write(string.format("XYZ: %d,%d,%d Level: %d Fuel: %d",
- data.x or 0, data.y or 0, data.z or 0, data.y or 0, data.fuel or 0))
- -- 5x5 grid, bottom left (1,1) -- top left is (1,5)
- for dz=SIZE,1,-1 do
- monitor.setCursorPos(10,6+SIZE-dz+1)
- for dx=1,SIZE do
- if dx==rel_x and dz==rel_z then
- monitor.setBackgroundColor(colors.lime)
- monitor.setTextColor(colors.black)
- monitor.write("X")
- else
- monitor.setBackgroundColor(colors.gray)
- monitor.setTextColor(colors.white)
- monitor.write(".")
- end
- end
- end
- monitor.setBackgroundColor(COLOR_BG)
- drawButton(12, 18, "Home", "H")
- drawButton(32, 18, "Resume", "B")
- drawButton(57, 18, "Back", "SPACE")
- end
- local function findTurtleAtClick(mx, my)
- local idx = 0
- for id, data in pairs(turtlestatus) do
- if data.active then
- idx=idx+1
- local yy = 2+(idx-1)*3
- if mx>=3 and mx<=4 and (my == yy or my == yy+1) then
- return id, data
- end
- end
- end
- return nil
- end
- function turtle_home(id) rednet.send(id,"home","turtle_cmd") end
- function turtle_resume(id) rednet.send(id,"resume","turtle_cmd") end
- local function main()
- local lastDraw = 0
- local detailId = nil
- while true do
- local ev = {os.pullEventRaw()}
- if ev[1]=="rednet_message" and ev[4]=="turtle_status" then
- local id = ev[2]
- local data = textutils.unserialize(ev[3]) or {}
- data.active = true
- turtlestatus[id] = data
- if os.clock()-lastDraw > 1 then drawList() lastDraw = os.clock() end
- elseif ev[1]=="monitor_touch" and ev[2]==monitorSide then
- local mx, my = ev[3], ev[4]
- if detailId then
- if mx >= 12 and mx <= 24 and my == 18 then
- turtle_home(detailId)
- elseif mx >= 32 and mx <= 54 and my == 18 then
- turtle_resume(detailId)
- elseif mx >= 57 and my == 18 then
- detailId = nil
- drawList()
- end
- else
- local id, data = findTurtleAtClick(mx,my)
- if id then
- detailId = id
- drawDetail(id, data)
- end
- end
- elseif ev[1]=="key" and detailId then
- local key = ev[2]
- if key==keys.h then
- turtle_home(detailId)
- elseif key==keys.b then
- turtle_resume(detailId)
- elseif key==keys.space then
- detailId = nil
- drawList()
- end
- end
- if os.clock()-lastDraw>1 then
- if detailId and turtlestatus[detailId] then
- drawDetail(detailId, turtlestatus[detailId])
- else
- drawList()
- end
- lastDraw = os.clock()
- end
- os.startTimer(0.1)
- end
- end
- monitor.setBackgroundColor(COLOR_BG)
- monitor.setTextColor(COLOR_TITLE_FG)
- monitor.clear()
- main()
Advertisement
Add Comment
Please, Sign In to add comment