Advertisement
rooxin

Untitled

Oct 1st, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 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 Distance(xA, zA, xB, zB)
  64. return Abs(xB - xA) + Abs(zB - zA)
  65. end
  66.  
  67. function GetBlockId(p_pos)
  68. if (Distance(0, 0, p_pos[1], p_pos[3]) == 10) then
  69. return 1
  70. end
  71. if (Distance(10, 10, p_pos[1], p_pos[3]) == 5) then
  72. return 1
  73. end
  74. if (Distance(-10, 10, p_pos[1], p_pos[3]) == 5) then
  75. return 1
  76. end
  77. if (Distance(10, -10, p_pos[1], p_pos[3]) == 5) then
  78. return 1
  79. end
  80. if (Distance(-10, -10, p_pos[1], p_pos[3]) == 5) then
  81. return 1
  82. end
  83. return 2
  84. end
  85.  
  86. function UpdateBlocks(p_pos)
  87. Reload()
  88.  
  89. --[[ Determine what to place below --]]
  90. print("Position is " .. p_pos[1] .. " " .. p_pos[3])
  91.  
  92. turtle.select(GetBlockId(p_pos))
  93.  
  94. --[[ Check if below is already the block we want to place --]]
  95. if (not turtle.compareDown()) then
  96. if (turtle.detectDown()) then
  97. turtle.digDown()
  98. end
  99. turtle.placeDown()
  100. end
  101. end
  102.  
  103. function GetBlockFromNextSlot(slot_id)
  104. local slot_to_take_from = slot_id + nb_blocks
  105. print("Checking number of blocks of slot " .. slot_id)
  106.  
  107. --[[ If the slot we want to take blocks from doesn't have them, ask them to fetch them --]]
  108. if (turtle.getItemCount(slot_to_take_from) < 32) then
  109. GetBlockFromNextSlot(slot_to_take_from)
  110. end
  111. turtle.select(slot_to_take_from)
  112. turtle.transferTo(slot_id, 32)
  113. end
  114.  
  115. function Reload()
  116. local i = 1
  117. while (i < nb_blocks + 1) do
  118. print("We have " .. turtle.getItemCount(i) .. " items in slot")
  119. if (turtle.getItemCount(i) == 0) then
  120. GetBlockFromNextSlot(i)
  121. end
  122. i = i + 1
  123. end
  124. end
  125.  
  126. function Update()
  127. UpdateBlocks(GpsPos())
  128. UpdateMovement()
  129. end
  130.  
  131. function GoForwardFor(fwd)
  132. while (fwd > 0) do
  133. fwd = fwd - 1
  134. turtle.forward()
  135. end
  136. end
  137.  
  138. while(lateral_movement_left > 0) do
  139. Update()
  140. end
  141.  
  142. print("Let's go back home")
  143.  
  144. if (args[2] % 2 == 0) then
  145. turtle.turnRight()
  146.  
  147. GoForwardFor(tonumber(args[2]) - 1)
  148.  
  149. turtle.turnRight()
  150. else
  151. turtle.turnLeft()
  152.  
  153. GoForwardFor(tonumber(args[2]) - 1)
  154.  
  155. turtle.turnLeft()
  156.  
  157. GoForwardFor(tonumber(args[1]) - 1)
  158.  
  159. turtle.turnLeft()
  160. turtle.turnLeft()
  161. end
  162.  
  163. print("Job's done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement