Advertisement
TheTrueCubic

OC Robot Excavator

Aug 1st, 2022 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local robot = require("robot")
  4.  
  5. term.write("Enter length (number): ")
  6. local length = term.read()
  7.  
  8. term.write("Enter width (number): ")
  9. local width = term.read()
  10.  
  11. term.write("Enter depth (number): ")
  12. local depth = term.read()
  13.  
  14. local right = true
  15.  
  16. function turn()
  17. if (right) then robot.turnRight() else robot.turnLeft() end
  18. end
  19.  
  20. function trySwing()
  21. if (robot.durability() and robot.durability() == 0) then
  22. term.write("Tool durability is low! Mining stopped.\n")
  23. trySwing()
  24. else
  25. robot.swing()
  26. end
  27. end
  28.  
  29. function trySwingDown()
  30. if (robot.durability() and robot.durability() == 0) then
  31. term.write("Tool durability is low! Mining stopped.\n")
  32. trySwingDown()
  33. else
  34. robot.swingDown()
  35. end
  36. end
  37.  
  38. function tryForward()
  39. trySwing()
  40. if (not robot.forward()) then tryForward() end
  41. end
  42.  
  43. function tryDown()
  44. trySwingDown()
  45. if (not robot.down()) then tryDown() end
  46. end
  47.  
  48.  
  49. function mineX(j)
  50. for i = length, 1, -1 do
  51. trySwingDown()
  52. if (i > 1) then
  53. tryForward()
  54. elseif (j > 1) then
  55. turn()
  56. trySwingDown()
  57. tryForward()
  58. turn()
  59. end
  60. end
  61. end
  62.  
  63. function mineY(j)
  64. for i = width, 0, -1 do
  65. if (i > 0) then
  66. mineX(i)
  67. right = not right
  68. elseif (j > 1) then
  69. tryDown()
  70. robot.turnAround()
  71. right = not right
  72. end
  73. end
  74. end
  75.  
  76. for i = depth, 1, -1 do
  77. mineY(i)
  78. end
  79.  
  80. tryDown()
  81. term.write("Done mining! Going back to original position.\n")
  82. if (depth % 2 == 0) then
  83. robot.turnAround()
  84. elseif (width % 2 == 0) then
  85. robot.turnRight()
  86. for i = width, 2, -1 do
  87. tryForward()
  88. end
  89. robot.turnRight()
  90. else
  91. robot.turnAround()
  92. for i = length, 2, -1 do
  93. tryForward()
  94. end
  95. robot.turnRight()
  96. for i = width, 2, -1 do
  97. tryForward()
  98. end
  99. end
  100. term.write("Done.\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement