Advertisement
rooxin

Untitled

Sep 29th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 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. --[[ 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. --[[ Destroy block below --]]
  98. if (turtle.detectDown()) then
  99. turtle.digDown()
  100. end
  101. --[[ Determine what to place below --]]
  102. print("Position is " .. p_pos[1] .. " " .. p_pos[3])
  103.  
  104. turtle.select(GetBlockId(p_pos))
  105. turtle.placeDown()
  106. end
  107.  
  108. function GetBlockFromNextSlot(slot_id)
  109. local slot_to_take_from = slot_id + nb_blocks
  110.  
  111. --[[ If the slot we want to take blocks from doesn't have them, ask them to fetch them --]]
  112. if (turtle.getItemCount(slot_to_take_from) < 16) then
  113. GetBlockFromNextSlot(slot_to_take_from)
  114. end
  115.  
  116. turtle.transferTo(slot_id + nb_blocks, 16)
  117. end
  118.  
  119. function Reload()
  120. i = 1
  121. while (i < nb_blocks + 1) do
  122. if (turtle.getItemCount == 0) then
  123. GetBlockFromNextSlot(i)
  124. end
  125. end
  126. end
  127.  
  128. function Update()
  129. UpdateBlocks(GpsPos())
  130. UpdateMovement()
  131. end
  132.  
  133. while(lateral_movement_left > 0) do
  134. Update()
  135. end
  136.  
  137. print("Job's done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement