Advertisement
zeplintwo

DW_turtles

Nov 11th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. -- pastebin get 8z3eSRTZ books
  2. local data = {}
  3. rednet.open("right")
  4. function writeFile()
  5. file = io.open("locs", "w")
  6. outp = textutils.serialize(data)
  7. file:write(outp)
  8. file:flush()
  9. file:close()
  10. end
  11.  
  12. function readFile()
  13. file = io.open("locs", "r")
  14. if file == nil then writeFile() else
  15. inp = file:read()
  16. data = textutils.unserialize(inp)
  17. end
  18. file:close()
  19. end
  20.  
  21. function printFile()
  22. file = io.open("locs", "r")
  23. print(file:read())
  24. file:close()
  25. end
  26.  
  27. function addName(slot,name)
  28. data[slot] = name
  29. writeFile()
  30. end
  31.  
  32. function removeSlot(slot)
  33. turtle.select(slot)
  34. --turtle.down()
  35. turtle.dropDown()
  36. data[slot] = null
  37. writeFile()
  38. --turtle.up()
  39. turtle.select(1)
  40. end
  41.  
  42. function checkSlots(id)
  43. for i = 1,12 do
  44. if turtle.getItemCount(i) > 0 and data[i] == null then
  45. print("send")
  46. rednet.send(id, "getName")
  47. local id, msg, dist = rednet.receive()
  48. addName(i, msg)
  49. end
  50. if turtle.getItemCount(i) == 0 and data[i] ~= null then
  51. data[i] = null
  52. writeFile()
  53. end
  54. end
  55. rednet.send(id,"done")
  56. end
  57.  
  58. function book(slot,id)
  59. turtle.select(slot)
  60. turtle.drop()
  61. sleep(5)
  62. getBook()
  63. turtle.select(1)
  64. rednet.send(tonumber(id), "done")
  65. end
  66.  
  67. function getBook()
  68. rs.setBundledOutput("back", 1)
  69. sleep(1.5)
  70. --turtle.down()
  71. turtle.suck()
  72. --turtle.up()
  73. rs.setBundledOutput("back", 0)
  74. end
  75.  
  76. function getNames(id)
  77. local nameTbl = textutils.serialize(data)
  78. rednet.send(tonumber(id), nameTbl)
  79. end
  80.  
  81. readFile()
  82.  
  83. while true do
  84. printFile()
  85. local id, msg, dis = rednet.receive()
  86. local newmsg = string.match(msg, "%a+")
  87. print(msg)
  88. if newmsg == "checkSlots" then
  89. checkSlots(id)
  90. elseif newmsg == "getNames" then
  91. getNames(id)
  92. elseif newmsg == "remove" then
  93. removeSlot(tonumber(string.match(msg, "%d+")))
  94. rednet.send(id,"done")
  95. elseif newmsg == "books" then
  96. slot = string.match(msg, "%d+")
  97. book(tonumber(slot), id)
  98. end
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement