Advertisement
rooxin

Untitled

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