Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. local robot = require("robot")
  2. local cp = require("component")
  3. local navi = cp.navigation
  4. local inv = cp.inventory_controller
  5. --require specific used libraries
  6.  
  7. local chest_pos = navi.getFacing() - 2
  8. if(chest_pos < 1) then
  9. chest_pos = chest_pos + 4
  10. end
  11. --save charger and chest position
  12. --charger is on the left of the robot and chest is behind the robot
  13.  
  14. local waypoint = navi.findWaypoints(64)
  15. local relX = waypoint[1]["position"][1]
  16. local relZ = waypoint[1]["position"][3]
  17. --save ending position, Y is not needed
  18.  
  19. local area = (math.abs(relX) + 1) * (math.abs(relZ) + 1)
  20. --calculate total columns
  21.  
  22. mined = 0
  23. charge_time = 0
  24. --global varibles for mined columns and drill charge time
  25.  
  26. local function path_finding(m)
  27. local length = 0
  28. if(chest_pos % 2 == 1) then
  29. length = math.abs(relZ) + 1
  30. --facing z
  31. else
  32. length = math.abs(relX) + 1
  33. --facing x
  34. end
  35. local forwards = mined % length
  36. local rights = math.floor(mined / length)
  37. --calculate needed moves
  38. if(m == 0) then
  39. --return mode
  40. robot.turnRight()
  41. local i = 0
  42. while(i < forwards) do
  43. robot.forward()
  44. i = i + 1
  45. end
  46. i = 0
  47. robot.turnRight()
  48. while(i < rights) do
  49. robot.forward()
  50. i = i + 1
  51. end
  52. else
  53. --travel mode
  54. local i = 0
  55. while(i < forwards) do
  56. robot.forward()
  57. i = i + 1
  58. end
  59. i = 0
  60. robot.turnRight()
  61. while(i < rights) do
  62. robot.forward()
  63. i = i + 1
  64. end
  65. end
  66. end
  67.  
  68. local function dump()
  69. robot.turnLeft()
  70. for i=1,16 do
  71. robot.select(i)
  72. robot.drop(64)
  73. end
  74. robot.turnRight()
  75. robot.select(1)
  76. inv.equip()
  77. robot.drop()
  78. os.sleep(charge_time)
  79. robot.suck()
  80. inv.equip()
  81. robot.turnRight()
  82. end
  83. --dump robot inventory
  84.  
  85. local function excavate()
  86. while (mined < area) do
  87. path_finding(1)
  88. local relY = 0
  89. while 1 do
  90. robot.swingDown()
  91. os.sleep(1)
  92. if(robot.detectDown()) then
  93. break
  94. else
  95. robot.down()
  96. relY = relY + 1
  97. end
  98. end
  99. charge_time = math.ceil(relY / 10)
  100. while(relY > 0) do
  101. robot.up()
  102. relY = relY - 1
  103. end
  104. path_finding(0)
  105. dump()
  106. mined = mined + 1
  107. end
  108. end
  109.  
  110. excavate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement