Advertisement
Ignius12

turtle_adv_display

Jul 26th, 2021 (edited)
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. mon = peripheral.find("monitor")
  2. modem = peripheral.find("modem")
  3.  
  4. ids = {0,1,2, 9}
  5.  
  6. data = {}
  7. for i=1, 6 do
  8.     data[i] = 0
  9. end
  10.  
  11. rednet.open("left")
  12.  
  13. selected = 1
  14. num_turtles = table.getn(ids)
  15.  
  16. function draw_text(x, y, text, text_color, bg_color)
  17.   mon.setBackgroundColor(bg_color)
  18.   mon.setTextColor(text_color)
  19.   mon.setCursorPos(x,y)
  20.   mon.write(text)
  21. end
  22.  
  23.  
  24. function draw_line(x, y, length, color)
  25.     if length < 0 then
  26.       length = 0
  27.     end
  28.     mon.setBackgroundColor(color)
  29.     mon.setCursorPos(x,y)
  30.     mon.write(string.rep(" ", length))
  31. end
  32.  
  33. function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  34.   draw_line(x, y, length, bg_color) --backgoround bar
  35.   local barSize = math.floor((minVal/maxVal) * length)
  36.   draw_line(x, y, barSize, bar_color) --progress so far
  37. end
  38.  
  39.  
  40. function clear()
  41.     --term.clear()
  42.     --term.setCursorPos(1,1)
  43.     mon.setBackgroundColor(colors.black)
  44.     mon.clear()
  45.     mon.setCursorPos(1,1)
  46. end
  47.  
  48. function baseGUI(label, fuel, posX, posY, posZ, status)
  49.     draw_text(2, 2, "<--", colors.white, colors.gray)
  50.     draw_text(25, 2, "-->", colors.white, colors.gray)
  51.     draw_text((13 - string.len(label) / 2), 2, label, colors.green, colors.white)
  52.     draw_text(4, 6, "Status: " .. status, colors.blue, colors.white)
  53.     draw_text(4, 8, "Fuel level: " .. ((fuel / 20000) * 100) .. "%", colors.orange, colors.white)
  54.     progress_bar(4, 10, 19, fuel, 20000, colors.green, colors.red)  draw_text(4, 12, "X: " .. posX, colors.black, colors.orange)
  55.     draw_text(4, 14, "Y: " .. posY, colors.black, colors.orange)
  56.     draw_text(4, 16, "Z: " .. posZ, colors.black, colors.orange)
  57.     draw_text( 5, 2, ids[selected], colors.red, colors.gray)
  58. end
  59.  
  60. function draw()
  61.    while(true) do
  62.         baseGUI(data[1], data[2], data[3], data[4], data[5], data[6])
  63.         sleep(0.0001)
  64.         clear()
  65.    end
  66. end
  67. function ping()
  68.     while(true) do
  69.         rednet.send(ids[selected], "send", "ping")
  70.         sleep(0.01)
  71.     end
  72. end
  73.  
  74. function update()
  75.     while true do
  76.         local sender, message, protocol = rednet.receive("miners")
  77.         if(sender ==  ids[selected]) then
  78.             local k = 1
  79.             for token in string.gmatch(message, "[^%s]+") do
  80.                 if(token == null) then
  81.                     data[k] = 0
  82.                 else
  83.                     data[k] = token
  84.                 end
  85.                 k = k + 1
  86.             end
  87.         end
  88.         sleep(0.0001)
  89.     end
  90. end
  91.  
  92. function buttons()
  93.     while true do
  94.         event, side, xPos, yPos = os.pullEvent("monitor_touch")
  95.         if(yPos == 2) then
  96.             if(xPos >=2 and xPos <= 4) then
  97.                 selected = selected - 1
  98.                 if(selected < 1) then
  99.                     selected = num_turtles
  100.                 end
  101.             else if(xPos >= 25 and xPos <= 27) then
  102.                 selected = selected + 1
  103.                 if(selected > num_turtles) then
  104.                     selected = 1
  105.                 end
  106.             end
  107.             end
  108.         end
  109.         sleep(0.01)
  110.     end
  111. end
  112.  
  113. function failsafe()
  114.     while true do
  115.         rednet.broadcast(" ", "failsafe")
  116.         sleep(2)
  117.     end
  118. end
  119. parallel.waitForAll(update, buttons, failsafe, ping, draw)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement