Advertisement
guamie

Mik's Funct

Jun 23rd, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. --Mik's Funct
  2.  
  3. --Function to empty inventory into chest
  4. function emptyInventory()
  5. local returnX = turtleX
  6. local returnY = turtleY
  7. local returnZ = turtleZ
  8. local returnDirection = turtleDirection
  9. goHome()
  10. for i=1,16 do
  11. turtle.select(i)
  12. turtle.drop()
  13. end
  14. checkCounter = 16
  15. turtleGoTo(returnX,returnY,returnZ,returnDirection)
  16. end
  17.  
  18. --Function to send turtle to a specific location
  19. function turtleGoTo(intX, intY, intZ, intDirection)
  20. while intY > turtleY do
  21. goUp()
  22. end
  23. if turtleX ~= intX then
  24. while turtleDirection ~= 1 do
  25. rightFace()
  26. end
  27. while turtleX < intX do
  28. goForward()
  29. end
  30. end
  31. if turtleZ ~= intZ then
  32. while turtleDirection ~= 2 do
  33. rightFace()
  34. end
  35. while turtleZ < intZ do
  36. goForward()
  37. end
  38. end
  39. while turtleDirection ~= intDirection do
  40. rightFace()
  41. end
  42. end
  43.  
  44. --Function to determine how many inventory slots are empty
  45. function emptySlots()
  46. empty = 0
  47. for i=1,16 do
  48. count = turtle.getItemCount(i)
  49. if count == 0 then
  50. empty = empty + 1
  51. end
  52. end
  53. return empty
  54. end
  55.  
  56. --Turn turtle to the left and update turtle direction
  57. function leftFace()
  58. turtle.turnLeft()
  59. updateFacing( "left" )
  60. end
  61.  
  62. --Turn turtle to the right and update turtle direction
  63. function rightFace()
  64. turtle.turnRight()
  65. updateFacing( "right" )
  66. end
  67.  
  68. -- Ensure turtle goes up
  69. function goUp()
  70. while not turtle.up() do
  71. upDig()
  72. end
  73. turtleY = turtleY + 1
  74. end
  75.  
  76. --Ensure turtle goes down
  77. function goDown()
  78. while not turtle.down() do
  79. downDig()
  80. end
  81. turtleY = turtleY - 1
  82. end
  83.  
  84. -- Ensure turtle moves forward
  85. function goForward()
  86. while not turtle.forward() do
  87. forwardDig()
  88. end
  89. updatePos()
  90. end
  91.  
  92. -- Dig forward
  93. function forwardDig()
  94. while turtle.detect() do
  95. turtle.dig()
  96. checkInventory()
  97. end
  98. end
  99.  
  100. -- Dig up
  101. function upDig()
  102. while turtle.detectUp() do
  103. turtle.digUp()
  104. checkInventory()
  105. end
  106. end
  107.  
  108. -- Dig Down
  109. function downDig()
  110. turtle.digDown()
  111. checkInventory()
  112. end
  113.  
  114. -- Function to update turtle position relative to starting location
  115. function updatePos()
  116. if turtleDirection == 1 then
  117. turtleX = turtleX + 1
  118. elseif turtleDirection == 2 then
  119. turtleZ = turtleZ + 1
  120. elseif turtleDirection == 3 then
  121. turtleX = turtleX - 1
  122. elseif turtleDirection == 4 then
  123. turtleZ = turtleZ - 1
  124. end
  125. print( "UpdatePos: X = " .. turtleX .. " Y = " .. turtleY .. " Z = " .. turtleZ )
  126. end
  127.  
  128. --Function to update turtle facing direction relative to starting facing direction
  129. function updateFacing(turnValue)
  130. if ( turnValue == "left" ) then
  131. turtleDirection = turtleDirection - 1
  132. elseif ( turnValue == "right" ) then
  133. turtleDirection = turtleDirection + 1
  134. else
  135. print( "Invalid input to updateFacing" .. turnValue )
  136. end
  137. if turtleDirection == 5 then
  138. turtleDirection = 1
  139. end
  140. if turtleDirection == 0 then
  141. turtleDirection = 4
  142. end
  143. print("UpdateFacing: " .. turtleDirection )
  144. end
  145.  
  146. --Function to return turtle to starting area.
  147. function goHome()
  148. while turtleY > 2 do
  149. goDown()
  150. end
  151. while ( turtleDirection ~= 4 ) do
  152. rightFace()
  153. sleep(.1)
  154. print( turtleDirection )
  155. end
  156. while turtleZ > 1 do
  157. goForward()
  158. end
  159. leftFace()
  160. while turtleX > 1 do
  161. goForward()
  162. end
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement