Advertisement
whitaleedee

bean turtle

Oct 3rd, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. sides = { "front", "left", "back", "top" }
  2. pushSides = { "south", "east", "north", "down" }
  3. map = {}
  4. filename = "beanMap"
  5. wireless = peripheral.wrap("right")
  6. turtleY = 1
  7.  
  8. channel = 300 -- This is the channel all the messageing traffic goes on
  9. wireless.open(channel)
  10.  
  11. ---functions---
  12. function split(pStr, pChar)
  13. return string.match(pStr, "([^" .. pChar .. "]+)" .. pChar .. "([^" .. pChar .. "]+)")
  14. end
  15.  
  16. function resetTurtle()
  17. while not turtle.detectDown() do
  18. turtleDown()
  19. end
  20.  
  21. turtleUp()
  22. turtleUp()
  23.  
  24. while turtle.detect() do
  25. turtle.turnRight()
  26. end
  27.  
  28. turtleY = 3
  29. turtle.select(1)
  30. print("turtle reset")
  31. end
  32.  
  33. function getBean(pDir, pAmt)
  34. local barrel = peripheral.wrap(sides[tonumber(pDir)])
  35. totQty = tonumber(barrel.getStackInSlot(2).qty)
  36. local amt = math.min(tonumber(pAmt), totQty-1)
  37. print("attempting get " .. amt .. " beans")
  38. local amtR = barrel.pushItem(pushSides[tonumber(pDir)],2,amt,1)
  39. print("received " .. amtR .. " beans")
  40. return amtR
  41. end
  42.  
  43. function getDirections(aspect)
  44. for k,v in pairs(map) do
  45. if k == aspect then
  46. print("found bean in location: " .. v)
  47. return split(v, ",")
  48. end
  49. end
  50. end
  51.  
  52. function send(msg)
  53. wireless.transmit(channel, 1, msg)
  54. print("send: " .. msg)
  55. end
  56.  
  57. function receive()
  58. e = {os.pullEvent("modem_message")}
  59. print("receive: " .. e[5])
  60. return e[5]
  61. end
  62.  
  63. function loadTable(fileName)
  64. local file=fs.open(fileName,"r")
  65. print("loading table")
  66. local data = file.readAll()
  67. file.close()
  68. return textutils.unserialize(data)
  69. end
  70.  
  71. function turtleUp()
  72. turtle.up()
  73. turtleY = turtleY + 1
  74. end
  75.  
  76. function turtleDown()
  77. turtle.down()
  78. turtleY = turtleY - 1
  79. end
  80.  
  81. function turtleTo(h)
  82. local turtleMoveAmt = tonumber(h) - turtleY
  83. if turtleMoveAmt > 0 then
  84. for i=1,turtleMoveAmt do
  85. turtleUp()
  86. end
  87. elseif turtleMoveAmt < 0 then
  88. for i=1,-1*turtleMoveAmt do
  89. turtleDown()
  90. end
  91. end
  92. end
  93.  
  94. function getAltAspectItems(aspect, amount)
  95. print("getting alt items")
  96. return 0
  97. end
  98.  
  99. function refill(aspect, amount)
  100. local amtMissing = 0
  101. if aspect and amount then
  102. local h,dir = getDirections(aspect)
  103. if h and dir then
  104. turtleTo(h)
  105. turtle.turnLeft()
  106.  
  107. local amtD = getBean(dir, amount)
  108. if amtD then
  109. if tonumber(amtD) < tonumber(amount) then
  110. amtMissing = getAltAspectItems(aspect, tonumber(amount) - tonumber(amtD))
  111. end
  112. else print("error pulling beans")
  113. end
  114. turtle.turnRight()
  115. turtleTo(2)
  116. turtle.drop()
  117. else print("aspect not in this col")
  118. end
  119. end
  120. return amtMissing
  121. end
  122.  
  123.  
  124. function refillAll()
  125. local missingTbl = {}
  126. local msgTable = textutils.unserialize(receive())
  127. for k,v in pairs(msgTable) do
  128. local refillAmt = 64-tonumber(v.quantity)
  129. if refillAmt > 0 then
  130. local amtMissing = refill(v.name, refillAmt)
  131. if amtMissing and amtMissing > 0 then
  132. local temp = {}
  133. temp.name = v.name
  134. temp.quantity = amtMissing
  135. table.insert(missingTbl, temp)
  136. end
  137. end
  138. end
  139.  
  140. send(textutils.serialize(missingTbl))
  141. end
  142.  
  143. ---end functions---
  144. map = loadTable(filename)
  145. resetTurtle()
  146.  
  147. while true do
  148. if turtle.getFuelLevel() < 10 then
  149. turtleTo(1)
  150. local c = peripheral.wrap("bottom")
  151. c.extractItem({id=263, qty=64}, "up")
  152. turtle.refuel()
  153. end
  154. refillAll()
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement