Advertisement
rooxin

Untitled

Sep 29th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 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])
  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. forward_movement_left = forward_movement_left - 1
  44.  
  45. --[[ Determine if we have gone far enough --]]
  46. if (forward_movement_left == 0) then
  47. print("Bounds detected")
  48. forward_movement_left = base_forward_movement
  49.  
  50. lateral_movement_left = lateral_movement_left - 1
  51.  
  52. if (lateral_movement_left == 0) then
  53. return
  54. end
  55.  
  56. --[[ Lateral movement --]]
  57. if (movement == 1) then
  58. StrafeRight()
  59. else
  60. StrafeLeft()
  61. end
  62.  
  63. UpdateBlocks(GpsPos())
  64.  
  65. --[[ Reverse current direction --]]
  66. movement = -movement
  67. turtle.turnRight()
  68. turtle.turnRight()
  69. end
  70.  
  71. turtle.forward()
  72. end
  73.  
  74. function Distance(xA, zA, xB, zB)
  75. return Abs(xB - xA) + Abs(zB - zA)
  76. end
  77.  
  78. function GetBlockId(p_pos)
  79. if (Distance(0, 0, p_pos[1], p_pos[3]) == 10) then
  80. return 1
  81. end
  82. if (Distance(10, 10, p_pos[1], p_pos[3]) == 5) then
  83. return 1
  84. end
  85. if (Distance(-10, 10, p_pos[1], p_pos[3]) == 5) then
  86. return 1
  87. end
  88. if (Distance(10, -10, p_pos[1], p_pos[3]) == 5) then
  89. return 1
  90. end
  91. if (Distance(-10, -10, p_pos[1], p_pos[3]) == 5) then
  92. return 1
  93. end
  94. return 2
  95. end
  96.  
  97. function UpdateBlocks(p_pos)
  98. --[[ Destroy block below --]]
  99. if (turtle.detectDown()) then
  100. turtle.digDown()
  101. end
  102. --[[ Determine what to place below --]]
  103. print("Position is " .. p_pos[1] .. " " .. p_pos[3])
  104.  
  105. turtle.select(GetBlockId(p_pos))
  106. turtle.placeDown()
  107. end
  108.  
  109. function GetBlockFromNextSlot(slot_id)
  110. local slot_to_take_from = slot_id + nb_blocks
  111.  
  112. --[[ If the slot we want to take blocks from doesn't have them, ask them to fetch them --]]
  113. if (turtle.getItemCount(slot_to_take_from) < 16) then
  114. GetBlockFromNextSlot(slot_to_take_from)
  115. end
  116.  
  117. turtle.transferTo(slot_id + nb_blocks, 16)
  118. end
  119.  
  120. function Reload()
  121. local i = 1
  122. while (i < nb_blocks + 1) do
  123. if (turtle.getItemCount == 0) then
  124. GetBlockFromNextSlot(i)
  125. end
  126. i = i - 1
  127. end
  128. end
  129.  
  130. function Update()
  131. Reload()
  132. UpdateBlocks(GpsPos())
  133. UpdateMovement()
  134. end
  135.  
  136. while(lateral_movement_left > 0) do
  137. Update()
  138. end
  139.  
  140. print("Let's go back home")
  141.  
  142. turtle.turnRight()
  143.  
  144. local i = tonumber(args[2])
  145.  
  146. while (i > 0) do
  147. i = i - 1
  148. turtle.forward()
  149. end
  150.  
  151. print("Job's done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement