Advertisement
Tomyf4

OC_StripMiningTurtle

Jan 19th, 2022
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local inventory = component.inventory_controller
  4. local robot = require("robot")
  5. local generator = component.generator
  6.  
  7. if generator == nil then
  8. print("No generator component found, please install!")
  9. end
  10.  
  11. local status = "running"
  12. local StripsMined = 0
  13.  
  14.  
  15. function refuel()
  16. -- try to refuel from inventory
  17. for i = 1, 16 do
  18. robot.select(i)
  19. generator.insert()
  20. end
  21.  
  22. -- Check if we did refuel
  23. if generator.count() > 1 then
  24. return true
  25. else
  26. return false
  27. end
  28. end
  29.  
  30.  
  31.  
  32. -- Check fuel in generator
  33. function checkFuel()
  34. if generator.count() > 5 then
  35. return true
  36. else
  37. -- No fuel in generator, try to insert fuel from inventory
  38. refuel()
  39.  
  40. if generator.count() > 5 then
  41. return true
  42. else
  43. return false
  44. end
  45. end
  46. end
  47.  
  48.  
  49.  
  50. -- Check Energy in robot
  51. function checkEnergy()
  52. Energy = computer.energy() / computer.maxEnergy() * 100
  53. if Energy < 10 and checkFuel() == false then
  54. return false
  55. else
  56. return true
  57. end
  58. end
  59.  
  60.  
  61.  
  62. -- Check held item durability
  63. function checkDurability()
  64. -- robot.durability() returns durability percentage from 0-1
  65. if robot.durability() == nil then
  66. print("Robot has no tool in tool slot")
  67. return false
  68. end
  69. if robot.durability() > 0.05 then
  70. return true
  71. else
  72. return false
  73. end
  74. end
  75.  
  76.  
  77.  
  78. -- Check if we have space in the inventory
  79.  
  80. function checkInventorySpace()
  81. for i = 1, 16 do
  82. robot.select(i)
  83. if robot.space() == 64 then
  84. return true
  85. else
  86. end
  87. end
  88. return false
  89. end
  90.  
  91.  
  92. -- Notify chest placement position
  93. print("Please place a chest directly to the right of the robot")
  94. os.sleep(1)
  95.  
  96.  
  97.  
  98.  
  99. -- Check if we have fuel in the generator
  100. if generator.count() < 5 then
  101. print("WARNING: Low amounts of fuel in generator!")
  102. os.sleep(0.5)
  103. print("Do you wish to refuel now? (Y / N)")
  104. refuelAction = io.read()
  105. else
  106. refuelAction = nil
  107. end
  108.  
  109. if refuelAction ~= nil and refuelAction == "Y" then
  110. refuel()
  111. else
  112. end
  113.  
  114.  
  115.  
  116. print("Input the depth of the stripmine..")
  117. depth = io.read()
  118. print("Input amount of strips..")
  119. amount = io.read()
  120.  
  121.  
  122.  
  123. -- Move forward Function
  124.  
  125. function moveForward()
  126. if robot.detect(forward) == true then
  127. robot.swing()
  128. robot.forward()
  129. else
  130. robot.forward()
  131. end
  132. while robot.detectUp() == true do
  133. robot.swingUp()
  134. os.sleep(1) -- wait in case sand or gravel
  135. end
  136. end
  137.  
  138.  
  139.  
  140. -- Return to chest location
  141. -- Call only after exiting a strip facing outward
  142.  
  143.  
  144. function returnToChest()
  145.  
  146. -- turn to face chest
  147. robot.turnLeft()
  148.  
  149. if StripsMined > 0 then
  150. for i = 1, StripsMined do
  151. moveForward()
  152. moveForward()
  153. moveForward()
  154. end
  155. end
  156.  
  157. if robot.detect(front) == true then
  158.  
  159. local front = 3
  160.  
  161. for i = 1, 16 do
  162. robot.select(i)
  163. if robot.space() ~= 64 then
  164. for chestSlot = 1, 27 do
  165. if inventory.getStackInSlot(front,chestSlot) == nil then
  166. inventory.dropIntoSlot(front, chestSlot, 64)
  167. end
  168. end
  169. end
  170. end
  171.  
  172. --wait for emptying
  173. for k = 1, 16 do
  174. robot.select(k)
  175. if robot.space() ~= 64 then
  176. status = "no_inv_chest_full"
  177. print("Chest is full..")
  178. end
  179. end
  180.  
  181. -- Reset status if inventory was a problem
  182. if status == "no_inv" then
  183. status = "running"
  184. end
  185. robot.select(1)
  186.  
  187. else
  188. print("ERROR: No Chest Found in front of expected position")
  189. end
  190.  
  191. -- return to to strip mine position
  192. robot.turnRight()
  193. robot.turnRight()
  194. if StripsMined > 0 then
  195. for i = 1, StripsMined do
  196. moveForward()
  197. moveForward()
  198. moveForward()
  199. end
  200. end
  201.  
  202. end
  203.  
  204. -- Start Strip mining
  205.  
  206.  
  207. for i = 1, amount do
  208. local BlocksMovedInStrip = 0
  209.  
  210. for i = 1, depth do
  211. BlocksMovedInStrip = i
  212. if checkEnergy() == true then
  213. if checkDurability() == true then
  214. if checkInventorySpace() == true then
  215. moveForward()
  216. else
  217. status = "no_inv"
  218. print("No Inventory Space")
  219. break
  220. end
  221. else
  222. status = "no_durability"
  223. print("No Durability")
  224. break
  225. end
  226. else
  227. status = "no_energy"
  228. print("No Energy")
  229. break
  230. end
  231. end
  232.  
  233. -- After first loop completed or canceled
  234.  
  235. if status == "running" then
  236. robot.turnRight()
  237. robot.turnRight()
  238. for i = 1, depth do
  239. moveForward()
  240. end
  241. else
  242. if BlocksMovedInStrip > 1 then
  243. robot.turnRight()
  244. robot.turnRight()
  245. for i = 1, BlocksMovedInStrip do
  246. moveForward()
  247. end
  248. end
  249. end
  250.  
  251.  
  252. returnToChest()
  253.  
  254.  
  255. -- Check if we are still green
  256. if status == "running" then
  257. --robot.turnRight()
  258. moveForward()
  259. moveForward()
  260. moveForward()
  261. robot.turnRight()
  262. StripsMined = StripsMined + 1
  263. else
  264. robot.turnRight()
  265. robot.turnRight()
  266. end
  267.  
  268. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement