Advertisement
rooxin

Untitled

Oct 1st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. function GpsPos()
  2. local x, y, z = gps.locate()
  3. return {x, y, z}
  4. end
  5.  
  6. local args = { ... }
  7.  
  8. if (#args ~= 2) then
  9. print("Gib depth then gib width plsss")
  10. error()
  11. end
  12.  
  13. nb_blocks = 2
  14. movement = 1
  15. base_forward_movement = tonumber(args[1]) - 1
  16. forward_movement_left = base_forward_movement
  17. lateral_movement_left = tonumber(args[2])
  18.  
  19. function StrafeRight()
  20. turtle.turnRight()
  21. turtle.forward()
  22. turtle.turnLeft()
  23. end
  24.  
  25. function StrafeLeft()
  26. turtle.turnLeft()
  27. turtle.forward()
  28. turtle.turnRight()
  29. end
  30.  
  31. function Sign(p_int)
  32. if (p_int < 0) then
  33. return -1
  34. end
  35. return 1
  36. end
  37.  
  38. function Abs(p_int)
  39. return p_int * Sign(p_int)
  40. end
  41.  
  42. function UpdateMovement()
  43. --[[ Determine if we have gone far enough --]]
  44. if (forward_movement_left == 0) then
  45. print("Bounds detected")
  46. forward_movement_left = base_forward_movement
  47.  
  48. lateral_movement_left = lateral_movement_left - 1
  49.  
  50. if (lateral_movement_left == 0) then
  51. return
  52. end
  53.  
  54. --[[ Lateral movement --]]
  55. if (movement == 1) then
  56. StrafeRight()
  57. else
  58. StrafeLeft()
  59. end
  60.  
  61. UpdateBlocks(GpsPos())
  62.  
  63. --[[ Reverse current direction --]]
  64. movement = -movement
  65. turtle.turnRight()
  66. turtle.turnRight()
  67. end
  68.  
  69. turtle.forward()
  70. forward_movement_left = forward_movement_left - 1
  71. end
  72.  
  73. function Distance(xA, zA, xB, zB)
  74. return Abs(xB - xA) + Abs(zB - zA)
  75. end
  76.  
  77. function GetBlockId(p_pos)
  78. if (Distance(0, 0, p_pos[1], p_pos[3]) == 10) then
  79. return 1
  80. end
  81. if (Distance(10, 10, p_pos[1], p_pos[3]) == 5) then
  82. return 1
  83. end
  84. if (Distance(-10, 10, p_pos[1], p_pos[3]) == 5) then
  85. return 1
  86. end
  87. if (Distance(10, -10, p_pos[1], p_pos[3]) == 5) then
  88. return 1
  89. end
  90. if (Distance(-10, -10, p_pos[1], p_pos[3]) == 5) then
  91. return 1
  92. end
  93. return 2
  94. end
  95.  
  96. function UpdateBlocks(p_pos)
  97. Reload()
  98.  
  99. --[[ Determine what to place below --]]
  100. print("Position is " .. p_pos[1] .. " " .. p_pos[3])
  101.  
  102. turtle.select(GetBlockId(p_pos))
  103.  
  104. --[[ Check if below is already the block we want to place --]]
  105. if (not turtle.compareDown()) then
  106. if (turtle.detectDown() then
  107. turtle.digDown()
  108. end
  109. turtle.placeDown()
  110. end
  111. end
  112.  
  113. function GetBlockFromNextSlot(slot_id)
  114. local slot_to_take_from = slot_id + nb_blocks
  115. print("Checking number of blocks of slot " .. slot_id)
  116.  
  117. --[[ If the slot we want to take blocks from doesn't have them, ask them to fetch them --]]
  118. if (turtle.getItemCount(slot_to_take_from) < 32) then
  119. GetBlockFromNextSlot(slot_to_take_from)
  120. end
  121. turtle.select(slot_to_take_from)
  122. turtle.transferTo(slot_id, 32)
  123. end
  124.  
  125. function Reload()
  126. local i = 1
  127. while (i < nb_blocks + 1) do
  128. print("We have " .. turtle.getItemCount(i) .. " items in slot")
  129. if (turtle.getItemCount(i) == 0) then
  130. GetBlockFromNextSlot(i)
  131. end
  132. i = i + 1
  133. end
  134. end
  135.  
  136. function Update()
  137. UpdateBlocks(GpsPos())
  138. UpdateMovement()
  139. end
  140.  
  141. while(lateral_movement_left > 0) do
  142. Update()
  143. end
  144.  
  145. print("Let's go back home")
  146.  
  147. if (args[2] % 2 == 0) then
  148. turtle.turnRight()
  149. else
  150. turtle.turnLeft()
  151. end
  152.  
  153. local i = tonumber(args[2])
  154.  
  155. while (i > 0) do
  156. i = i - 1
  157. turtle.forward()
  158. end
  159.  
  160. if (args[2] % 2 == 0) then
  161. turtle.turnRight()
  162. else
  163. turtle.turnLeft()
  164. end
  165.  
  166. print("Job's done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement