overshoot

Branch Mine with Lava refuel - ZpKSLTgW

Jan 28th, 2021
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. -- Branch Mine with Lava refuel - ZpKSLTgW
  2. -- Supposedly recursive
  3. -- https://forums.computercraft.cc/index.php?topic=36.0
  4.  
  5.  
  6. --[[
  7. Slot 1: Stone
  8. Slot 2: Dirt
  9. Slot 3: Sand
  10. Slot 4: Gravel
  11. Slot 15: Bucket
  12. Slot 16: Fuel
  13. ]]--
  14.  
  15. local ok, tArgs, ignoredFuel, oldprint, fuelAmount, nSlots = true, { ... }, 0, print, nil
  16.  
  17. for i = 1, 13 do
  18. if turtle.getItemCount( i ) == 0 then
  19. nSlots = i - 1
  20. print( "You have "..nSlots.." stacks of waste blocks, is this correct? Y/N" )
  21. while true do
  22. local _, char = os.pullEvent( "char" )
  23. if char:lower() == "n" then
  24. error()
  25. elseif char:lower() == "y" then
  26. break
  27. end
  28. end
  29. break
  30. end
  31. end
  32.  
  33. if turtle.getItemCount( 15 ) ~= 1 then
  34. error( "Place a single bucket in slot 15" )
  35. end
  36. if turtle.getItemCount( 16 ) == 0 then
  37. print( "Are you sure you wish to continue with no fuel in slot 16? Y/N" )
  38. while true do
  39. local _, char = os.pullEvent( "char" )
  40. if char:lower() == "n" then
  41. error()
  42. elseif char:lower() == "y" then
  43. break
  44. end
  45. end
  46. end
  47.  
  48. local function print( text )
  49. oldprint( "[" .. os.time() .. "]" .. text )
  50. local file = fs.open( "turtleLog", "a" )
  51. file.writeLine( "[" .. os.time() .. "]" .. text )
  52. file.close()
  53. end
  54.  
  55. function dumpWaste()
  56. while ok do
  57. for i = 1, nSlots do
  58. local count = turtle.getItemCount( i )
  59. if count > 1 then
  60. turtle.select( i )
  61. turtle.drop( count - 1 )
  62. end
  63. end
  64. local id = os.startTimer( 10 )
  65. while true do
  66. local _, tid = os.pullEvent( "timer" )
  67. if tid == id then
  68. break
  69. end
  70. end
  71. end
  72. end
  73.  
  74. function notwaste( func )
  75. for i = 1, nSlots do
  76. turtle.select( i )
  77. if func() then
  78. return false
  79. end
  80. end
  81. if func == turtle.compare then
  82. return turtle.detect()
  83. elseif func == turtle.compareDown then
  84. return turtle.detectDown()
  85. elseif func == turtle.compareUp then
  86. return turtle.detectUp()
  87. end
  88. end
  89.  
  90. function check( nLevel )
  91. if not nLevel then
  92. nLevel = 1
  93. elseif nLevel > 200 then
  94. return
  95. end
  96. if not ok then return end
  97. --check for lava
  98. turtle.select( 14 )
  99. if turtle.getItemCount( 14 ) == 0 and not turtle.compare() and not turtle.detect() then
  100. turtle.select( 15 )
  101. if turtle.place() then
  102. print( "[check]: Liquid detected!" )
  103. if turtle.refuel() then
  104. print( "[check]: Refueled using lava source!" )
  105. turtle.forward()
  106. check( nLevel + 1 )
  107. while not turtle.back() do end
  108. ignoredFuel = ignoredFuel + 2
  109. else
  110. print( "[check]: Liquid was not lava!" )
  111. turtle.place()
  112. end
  113. end
  114. end
  115. --check for inventories
  116. if turtle.detect() and turtle.suck() then
  117. while turtle.suck() do end
  118. end
  119. --check for ore
  120. if notwaste( turtle.compare ) then
  121. print( "[check]: Ore Detected!" )
  122. repeat turtle.dig() until turtle.forward()
  123. print( "[check]: Dug ore!" )
  124. check( nLevel + 1 )
  125. while not turtle.back() do end
  126. ignoredFuel = ignoredFuel + 2
  127. end
  128. if not ok then return end
  129. turtle.turnLeft()
  130. --check for lava
  131. turtle.select( 14 )
  132. if turtle.getItemCount( 14 ) == 0 and not turtle.compare() and not turtle.detect() then
  133. turtle.select( 15 )
  134. if turtle.place() then
  135. print( "[check]: Liquid detected!" )
  136. if turtle.refuel() then
  137. print( "[check]: Refueled using lava source!" )
  138. turtle.forward()
  139. check( nLevel + 1 )
  140. while not turtle.back() do end
  141. ignoredFuel = ignoredFuel + 2
  142. else
  143. print( "[check]: Liquid was not lava!" )
  144. turtle.place()
  145. end
  146. end
  147. end
  148. --check for inventories
  149. if turtle.detect() and turtle.suck() then
  150. while turtle.suck() do end
  151. end
  152. --check for ore
  153. if notwaste( turtle.compare ) then
  154. print( "[check]: Ore Detected!" )
  155. repeat turtle.dig() until turtle.forward()
  156. print( "[check]: Dug ore!" )
  157. check( nLevel + 1 )
  158. while not turtle.back() do end
  159. ignoredFuel = ignoredFuel + 2
  160. end
  161. turtle.turnRight()
  162. if not ok then return end
  163. turtle.turnRight()
  164. --check for lava
  165. turtle.select( 14 )
  166. if turtle.getItemCount( 14 ) == 0 and not turtle.compare() and not turtle.detect() then
  167. turtle.select( 15 )
  168. if turtle.place() then
  169. print( "[check]: Liquid detected!" )
  170. if turtle.refuel() then
  171. print( "[check]: Refueled using lava source!" )
  172. turtle.forward()
  173. check( nLevel + 1 )
  174. while not turtle.back() do end
  175. ignoredFuel = ignoredFuel + 2
  176. else
  177. print( "[check]: Liquid was not lava!" )
  178. turtle.place()
  179. end
  180. end
  181. end
  182. --check for inventories
  183. if turtle.detect() and turtle.suck() then
  184. while turtle.suck() do end
  185. end
  186. --check for ore
  187. if notwaste( turtle.compare ) then
  188. print( "[check]: Ore Detected!" )
  189. repeat turtle.dig() until turtle.forward()
  190. print( "[check]: Dug ore!" )
  191. check( nLevel + 1 )
  192. while not turtle.back() do end
  193. ignoredFuel = ignoredFuel + 2
  194. end
  195. turtle.turnLeft()
  196. if not ok then return end
  197. --check for lava
  198. turtle.select( 14 )
  199. if turtle.getItemCount( 14 ) == 0 and not turtle.compareUp() and not turtle.detectUp() then
  200. turtle.select( 15 )
  201. if turtle.placeUp() then
  202. print( "[check]: Liquid detected!" )
  203. if turtle.refuel() then
  204. print( "[check]: Refueled using lava source!" )
  205. turtle.up()
  206. check( nLevel + 1 )
  207. while not turtle.down() do end
  208. ignoredFuel = ignoredFuel + 2
  209. else
  210. print( "[check]: Liquid was not lava!" )
  211. turtle.placeUp()
  212. end
  213. end
  214. end
  215. --check for inventories
  216. if turtle.detectUp() and turtle.suckUp() then
  217. while turtle.suckUp() do end
  218. end
  219. --check for ore
  220. if notwaste( turtle.compareUp ) then
  221. print( "[check]: Ore Detected!" )
  222. repeat turtle.digUp() until turtle.up()
  223. print( "[check]: Dug ore!" )
  224. check( nLevel + 1 )
  225. while not turtle.down() do end
  226. ignoredFuel = ignoredFuel + 2
  227. end
  228. if not ok then return end
  229. --check for lava
  230. turtle.select( 14 )
  231. if turtle.getItemCount( 14 ) == 0 and not turtle.compareDown() and not turtle.detectDown() then
  232. turtle.select( 15 )
  233. if turtle.placeDown() then
  234. print( "[check]: Liquid detected!" )
  235. if turtle.refuel() then
  236. print( "[check]: Refueled using lava source!" )
  237. turtle.down()
  238. check( nLevel + 1 )
  239. while not turtle.up() do end
  240. ignoredFuel = ignoredFuel + 2
  241. else
  242. print( "[check]: Liquid was not lava!" )
  243. turtle.placeDown()
  244. end
  245. end
  246. end
  247. --check for inventories
  248. if turtle.detectDown() and turtle.suckDown() then
  249. while turtle.suckDown() do end
  250. end
  251. --check for ore
  252. if notwaste( turtle.compareDown ) then
  253. print( "[check]: Ore Detected!" )
  254. repeat turtle.digDown() until turtle.down()
  255. print( "[check]: Dug ore!" )
  256. check( nLevel + 1 )
  257. while not turtle.up() do end
  258. ignoredFuel = ignoredFuel + 2
  259. end
  260. end
  261.  
  262. function branch()
  263. local gone = 0
  264. for i = 1, 15 do
  265. repeat turtle.dig() until turtle.forward()
  266. print( "[branch]: Dug forward!" )
  267. gone = gone + 1
  268. if not ok then break end
  269. check()
  270. if not ok then break end
  271. end
  272. print( "[branch]: Returning!" )
  273. turtle.turnLeft()
  274. turtle.turnLeft()
  275. for i = 1, gone do
  276. while not turtle.forward() do
  277. while turtle.dig() do end
  278. while turtle.attack() do end
  279. end
  280. end
  281. ignoredFuel = ignoredFuel + ( gone * 2 )
  282. print( "[branch]: Returned!" )
  283. end
  284.  
  285. function main()
  286. local gone = 0
  287. while ok do
  288. for i = 1, 3 do
  289. repeat turtle.dig() until turtle.forward()
  290. print( "[main]: Dug forward!" )
  291. gone = gone + 1
  292. if not ok then break end --not ok, don't keep running
  293. check()
  294. if not ok then break end
  295. end
  296. if not ok then break end
  297. turtle.turnLeft()
  298. print( "[main]: Initiating branch!" )
  299. branch()
  300. turtle.turnLeft()
  301. if not ok then break end --not ok, don't run second branch
  302. turtle.turnRight()
  303. print( "[main]: Intiating branch!" )
  304. branch()
  305. turtle.turnRight()
  306. end
  307. --not ok, return to base
  308. print( "[main]: Returning to base!" )
  309. turtle.turnLeft()
  310. turtle.turnLeft()
  311. repeat
  312. while not turtle.forward() do
  313. while turtle.attack() do end
  314. while turtle.dig() do end
  315. end
  316. gone = gone - 1
  317. until gone == 0
  318. end
  319.  
  320.  
  321. function findMaxLevel()
  322. local level = turtle.getFuelLevel()
  323. if turtle.getItemCount( 16 ) > 1 then
  324. if not fuelAmount then
  325. turtle.select( 16 )
  326. turtle.refuel( 1 )
  327. fuelAmount = turtle.getFuelLevel() - level
  328. print( "[findMaxLevel]: Found fuelAmount: "..fuelAmount)
  329. end
  330. print( "[findMaxLevel]: Found max level: " .. turtle.getItemCount( 16 ) * fuelAmount + turtle.getFuelLevel() .. "!")
  331. return turtle.getItemCount( 16 ) * fuelAmount + turtle.getFuelLevel()
  332. else
  333. print( "[findMaxLevel]: Found max level: " .. turtle.getFuelLevel() .. "!" )
  334. return turtle.getFuelLevel()
  335. end
  336. end
  337.  
  338. function isOk()
  339. local okLevel = findMaxLevel() / 2 + 10
  340. while ok do
  341. local currentLevel = turtle.getFuelLevel()
  342. if currentLevel < 100 then --check fuel
  343. print( "[isOk]: Fuel Level Low!" )
  344. if turtle.getItemCount( 16 ) > 0 then
  345. print( "[isOk]: Refueling!" )
  346. repeat
  347. turtle.select( 16 )
  348. until turtle.refuel( 1 ) or turtle.getSelectedSlot() == 16
  349. if turtle.getFuelLevel() > currentLevel then
  350. print( "[isOk]: Refuel Successful!" )
  351. else
  352. print( "[isOk]: Refuel Unsuccessful, Initiating return!" )
  353. ok = false
  354. end
  355. end
  356. elseif okLevel - ignoredFuel > findMaxLevel() then
  357. print("[isOk]: Fuel Reserves Depleted! Initiating return!")
  358. ok = false
  359. end
  360. --make sure turtle can take new items
  361. local hasSpace = false
  362. for i = 5, 15 do
  363. if turtle.getItemCount( i ) == 0 then
  364. hasSpace = true
  365. end
  366. end
  367. if not hasSpace then
  368. print( "[isOk]: Out of space! Intiating return!" )
  369. ok = false
  370. elseif ok then
  371. print( "[isOk]: Everything is OK!" )
  372. local id = os.startTimer( 10 )
  373. while true do
  374. local _, tid = os.pullEvent( "timer" )
  375. if tid == id then
  376. break
  377. end
  378. end
  379. end
  380. end
  381. end
  382.  
  383.  
  384. function trackTime()
  385. local sTime = table.concat( tArgs, " " )
  386. local nSeconds = 0
  387. for i, period in sTime:gmatch( "(%d+)%s+(%a+)s?" ) do
  388. if period:lower() == "second" then
  389. nSeconds = nSeconds + i
  390. elseif period:lower() == "minute" then
  391. nSeconds = nSeconds + ( i * 60 )
  392. elseif period:lower() == "hour" then
  393. nSeconds = nSeconds + ( i * 3600 )
  394. end
  395. end
  396. print( "[trackTime]: Starting timer for "..nSeconds.." seconds!" )
  397. local id = os.startTimer( nSeconds )
  398. while ok do
  399. local _, tid = os.pullEvent( "timer" )
  400. if id == tid then
  401. print( "[trackTime]: End of session reached! Returning to base!" )
  402. ok = false
  403. end
  404. end
  405. end
  406.  
  407. parallel.waitForAll( trackTime, isOk, main, dumpWaste )
  408. for i = 5, 14 do
  409. turtle.select( i )
  410. turtle.dropDown()
  411. end
  412.  
  413.  
Advertisement
Add Comment
Please, Sign In to add comment