Advertisement
Dlog_M125

goldMine with rednet terminate function

Jul 21st, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. -- Initialising...
  4. local run = true -- if this is false, the program will terminate
  5. local blocksToMove = 0
  6. local infinite = false
  7. local noAsking = false
  8. local placeTorches = false
  9. local placeChests = false
  10. local enderChestMode = false
  11. local blocksMoved = 0
  12. local torchMoves = 0 -- number of blocks moved since last torch placement
  13.  
  14. function printUsage()
  15. print("Usages:")
  16. print("goldMine <blocks> <placeTorches> <placeChests>")
  17. print("goldMine infinite <placeTorches> <placeBlocks>")
  18. end
  19.  
  20. if tArgs[1] == "infinite" then
  21. infinite = true
  22. elseif type(tonumber(tArgs[1])) == "number" then -- if it's a valid number
  23. blocksToMove = tonumber(tArgs[1])
  24. else
  25. printUsage()
  26. run = false
  27. end
  28.  
  29. if run then
  30. if tArgs[2] == "true" then
  31. placeTorches = true
  32. elseif tArgs[2] == "false" then
  33. placeTorches = false
  34. else
  35. printUsage()
  36. run = false
  37. end
  38. end
  39.  
  40. if run then
  41. if tArgs[3] == "true" then
  42. placeChests = true
  43. elseif tArgs[3] == "false" then
  44. placeChests = false
  45. else
  46. printUsage()
  47. run = false
  48. end
  49. end
  50.  
  51. if run then
  52. if tArgs[4] == "noAskingEnder" then -- i have some evil plans
  53. noAsking = true
  54. enderChestMode = true
  55. elseif tArgs[4] == "noAsking" then
  56. noAsking = true
  57. enderChestMode = false
  58. end
  59. end
  60.  
  61. if run then -- printing a whole bunch of text
  62. if placeTorches and (noAsking == false) then
  63. print("----------------------------")
  64. print("Put torches in the top left slot and press any button to continue...")
  65. os.pullEvent("key")
  66. print("----------------------------")
  67. end
  68.  
  69. if placeChests and (noAsking == false) then
  70. print("----------------------------")
  71. print("Put chests in the bottom right slot and press any button to continue...")
  72. os.pullEvent("key")
  73. print("----------------------------")
  74.  
  75. local validInput = false
  76. print("----------------------------")
  77. print("Enable Ender Chest mode? (Will break the chest after dropping off) <y/n>")
  78. while validInput == false do
  79. event, key = os.pullEvent("char")
  80. if key == 'y' then
  81. enderChestMode = true
  82. print("----------------------------")
  83. validInput = true
  84. elseif key == 'n' then
  85. enderChestMode = false
  86. print("----------------------------")
  87. validInput = true
  88. else
  89. print("Invalid input")
  90. end
  91. end
  92. end
  93.  
  94. if infinite then
  95. print("Mode: infinite")
  96. else
  97. print("Mode: finite. Blocks to break: "..blocksToMove)
  98. end
  99. print("Chest placing: "..tostring(placeChests))
  100. print("Torch placing: "..tostring(placeTorches))
  101. if placeChests then
  102. print("Ender Chest mode: "..tostring(enderChestMode))
  103. end
  104. end
  105.  
  106. function terminate()
  107. print("Goodbye")
  108. os.reboot()
  109. end
  110.  
  111. function refuelAdv(value)
  112. if turtle.getFuelLevel() == "unlimited" then
  113. return true -- If turtles don't need fuel
  114. elseif turtle.getFuelLevel() > value - 1 then
  115. return true
  116. else
  117. if value > turtle.getFuelLimit() then
  118. value = turtle.getFuelLimit()
  119. end
  120.  
  121. for slot = 1, 16 do
  122. turtle.select(slot)
  123. while turtle.refuel(0) == true do
  124. turtle.refuel(1)
  125. print(turtle.getFuelLevel())
  126. if turtle.getFuelLevel() > value - 1 then
  127. print("Enough fuel! "..turtle.getFuelLevel().."/"..value)
  128. return true
  129. end
  130. end
  131. end
  132. end
  133. if (turtle.getFuelLevel() > value - 1) then
  134. print("Enough fuel! "..turtle.getFuelLevel().."/"..value)
  135. else
  136. print("Not enough fuel! "..turtle.getFuelLevel().."/"..value)
  137. end
  138. return false
  139. end
  140.  
  141. function mineAllSides()
  142. if turtle.detectUp() then
  143. turtle.digUp()
  144. turtle.suckUp()
  145. end
  146.  
  147. if turtle.detectDown() then
  148. turtle.digDown()
  149. turtle.suckDown()
  150. end
  151.  
  152. if turtle.detect() then
  153. return turtle.dig() -- return false if there is an unbreakable block
  154. end
  155.  
  156. return true
  157. end
  158.  
  159. function tryBreaking()
  160. local success = false
  161. local tries = 0
  162.  
  163. print("Unbreakable block?")
  164. while (success == false) and (tries < 10) do
  165. tries = tries + 1
  166. print("Trying to break it...")
  167. success = turtle.dig()
  168. if turtle.detect() == false then
  169. success = true
  170. end
  171. os.sleep(2)
  172. end
  173. return success
  174. end
  175.  
  176. function tryAttacking()
  177. local success = false
  178. local tries = 0
  179.  
  180. print("Can't move. Mob or gravel blocking the way?")
  181. while (success == false) and (tries < 20) do
  182. tries = tries + 1
  183. print("Trying to attack it...")
  184. turtle.attack()
  185. print("Trying to mine it...")
  186. if turtle.forward() then
  187. success = true
  188. break
  189. else
  190. os.sleep(2)
  191. end
  192. end
  193. return success
  194. end
  195.  
  196. function placeTorch()
  197. turtle.select(1)
  198.  
  199. if turtle.getItemCount() == 1 then -- if it's the only torch left then it's the last time we're placing them
  200. placeTorches = false
  201. end
  202.  
  203. turtle.placeDown()
  204. end
  205.  
  206. function placeChest()
  207. local firstSlot = 1
  208. local lastSlot = 15
  209.  
  210. if placeTorches then
  211. firstSlot = 2 -- ignore torches in slot 1
  212. end
  213.  
  214. turtle.select(16)
  215. if turtle.getItemCount() > 0 then
  216. if (turtle.getItemCount() == 1) and (enderChestMode == false) then
  217. placeChests = false -- if it's the last chest then it's the last time we're placing it
  218. end
  219.  
  220. if turtle.placeDown() then
  221. for slot = firstSlot, lastSlot do
  222. turtle.select(slot)
  223. if turtle.refuel(0) == false then -- ignore fuel
  224. turtle.dropDown()
  225. end
  226. end
  227. if enderChestMode then
  228. turtle.select(16)
  229. turtle.digDown()
  230. end
  231. end
  232. end
  233. end
  234.  
  235. -- Actual loop
  236. while run do -- similar to "while true do" but won't run if there was an error while initializing
  237. local senderId, message, protocol = rednet.receive(0.1) -- Using a timeout to check for messages periodically
  238.  
  239. if message == "terminate" then
  240. run = false
  241. break
  242. end
  243.  
  244. local complete = false
  245. local moved = false
  246.  
  247. if infinite then
  248. print("Blocks moved: "..blocksMoved)
  249. else
  250. print("Blocks moved: "..blocksMoved.." out of "..blocksToMove)
  251. if blocksMoved == blocksToMove then
  252. run = false
  253. complete = true
  254. end
  255. end
  256.  
  257. if (turtle.getFuelLevel() < 100) and run then
  258. refuelAdv(100)
  259. run = turtle.getFuelLevel() ~= 0
  260. end
  261.  
  262. if (mineAllSides() == false) and run then
  263. run = tryBreaking()
  264. end
  265.  
  266. if placeTorches and run then
  267. if torchMoves == 8 then
  268. placeTorch()
  269. torchMoves = 0
  270. else
  271. torchMoves = torchMoves + 1
  272. end
  273. end
  274.  
  275. if placeChests and run then
  276. if turtle.getItemCount(15) > 0 then
  277. if turtle.getItemCount(16) > 0 then
  278. placeChest()
  279. end
  280. end
  281. end
  282.  
  283. if (turtle.forward() == false) and run then
  284. if tryAttacking() then
  285. moved = true -- if moving is successful after attacking
  286. else
  287. run = false
  288. end
  289. elseif run then
  290. moved = true -- if moving is successful without doing anything
  291. end
  292.  
  293. if moved then
  294. blocksMoved = blocksMoved + 1
  295. end
  296.  
  297. if run == false then
  298. if complete then
  299. print("Mining complete!")
  300. else
  301. print("Program terminated. Sorry!")
  302. end
  303. end
  304. end
  305.  
  306. terminate()
  307.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement