Advertisement
pedrosgali

Turtle Screen

Nov 26th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.70 KB | None | 0 0
  1. m = peripheral.wrap("top")
  2. rednet.open("right")
  3.  
  4. woodTimer = 7200
  5. lapTimer = 2800
  6.  
  7. x, y = m.getSize()
  8. name = 1
  9. channel = 2
  10. tMsg = 3
  11. xPos = 4
  12. yPos = 5
  13. zPos = 6
  14. hPos = 7
  15.  
  16. tabSize = math.floor(x / 7)
  17.  
  18. iTurtle = {}
  19. iTurtle[1] = 1
  20. jobTab = {}
  21.  
  22.  
  23. function getData()
  24.     if fs.exists("tData") then
  25.         data = fs.open("tData", "r")
  26.         tData = textutils.unserialize(data.readAll())
  27.         data.close()
  28.     else
  29.         tData = {}
  30.         tData[1] = {}
  31.         tData[1][name] = "Name:"
  32.         tData[1][channel] = "ID:"
  33.         tData[1][tMsg] = "Status:"
  34.         tData[1][xPos] = "X:"
  35.         tData[1][yPos] = "Y:"
  36.         tData[1][zPos] = "Z:"
  37.         tData[1][hPos] = "H:"
  38.     end
  39. end
  40.  
  41. function dumpFile(msg)
  42.     data = fs.open("tSheet", "w")
  43.     data.write(msg)
  44.     data.close()
  45. end
  46.  
  47. function readFile()
  48.     data = fs.open("tSheet", "r")
  49.     tx = data.readLine()
  50.     ty = data.readLine()
  51.     tz = data.readLine()
  52.     th = data.readLine()
  53.     tName = data.readLine()
  54.     tState = data.readLine()
  55.     data.close()
  56.     if tState == "idle" then
  57.         iTurtle[1] = id
  58.     end
  59. end
  60.  
  61. function saveData()
  62.     data = fs.open("tData", "w")
  63.     data.writeLine(textutils.serialize(tData))
  64.     data.close()
  65. end
  66.  
  67. function addTurtle()
  68.     newMax = #tData + 1
  69.     tData[newMax] = {}
  70.     tData[newMax][name] = tName
  71.     tData[newMax][channel] = id
  72.     tData[newMax][tMsg] = tState
  73.     tData[newMax][xPos] = tx
  74.     tData[newMax][yPos] = ty
  75.     tData[newMax][zPos] = tz
  76.     tData[newMax][hPos] = th
  77. end
  78.  
  79. function checkList()
  80.     readFile()
  81.     listEntry = false
  82.     for i = 1, #tData do
  83.         if id == tData[i][channel] then
  84.            tData[i][name] = tName
  85.            tData[i][channel] = id
  86.            tData[i][tMsg] = tState
  87.            tData[i][xPos] = tx
  88.            tData[i][yPos] = ty
  89.            tData[i][zPos] = tz
  90.            tData[i][hPos] = th
  91.            listEntry = true
  92.            break
  93.         end
  94.     end
  95.     if not listEntry then
  96.         addTurtle()
  97.     end
  98.     saveData()
  99. end
  100.  
  101. function centerText(stX, maX, tY, bCol, tCol, text)
  102.     boundry = maX - stX
  103.     width = (boundry / 2) - (string.len(text) / 2)
  104.     m.setBackgroundColor(bCol)
  105.     m.setTextColor(tCol)
  106.     m.setCursorPos(width + stX, tY)
  107.     m.write(text)
  108. end
  109.  
  110. function drawScreen()
  111.     m.setBackgroundColor(colors.black)
  112.     m.setTextColor(colors.lime)
  113.     m.clear()
  114.     for i = 1, #tData do
  115.         alter = true
  116.         if alter then
  117.             sx = 1
  118.             ex = tabSize
  119.             for j = 1, #tData[i] do
  120.                 centerText(sx, ex, i, colors.black, colors.white, tData[i][j])
  121.                 sx = sx + tabSize
  122.                 ex = ex + tabSize
  123.             end
  124.         else
  125.             sx = 1
  126.             ex = tabSize
  127.             for j = 1, #tData[i] do
  128.                 centerText(sx, ex, i, colors.white, colors.black, tData[i][j])
  129.                 sx = sx + tabSize
  130.                 ex = ex + tabSize
  131.             end
  132.         end
  133.     end
  134. end
  135.  
  136. function check()
  137.     event, p1, p2, p3, p4, p5 = os.pullEvent()
  138.     if event == "rednet_message" then
  139.         id = p1
  140.         dumpFile(p2)
  141.         checkList()
  142.     elseif event == "timer" then
  143.         woodTimer = woodTimer - 1
  144.         lapTimer = lapTimer - 1
  145.         if woodTimer == 0 then
  146.             rednet.send(2, "chop wood")
  147.             woodTimer = 7200
  148.         elseif lapTimer == 0 then
  149.             rednet.send(2, "farm 90 lapis")
  150.             lapTimer = 2800
  151.         end
  152.         os.startTimer(1)
  153.     end
  154. end
  155.  
  156. getData()
  157. os.startTimer(1)
  158. while true do
  159.     term.clear()
  160.     term.setCursorPos(1, 1)
  161.     print("Wood timer:  "..woodTimer)
  162.     print("Lapis timer: "..lapTimer)
  163.     check()
  164.     drawScreen()
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement