Advertisement
Zupalicious

qtest

Apr 14th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. print("Downloading 'quarry'")
  2. local quarry = fs.open("quarry", "w")
  3. quarry.write('os.loadAPI("inv")
  4. os.loadAPI("t")
  5.  
  6. local x = 0
  7. local y = 0
  8. local z = 0
  9. local max = 16
  10. local deep = 64
  11. local facingfw = true
  12.  
  13. local OK = 0
  14. local ERROR = 1
  15. local LAYERCOMPLETE = 2
  16. local OUTOFFUEL = 3
  17. local FULLINV = 4
  18. local BLOCKEDMOV = 5
  19. local USRINTERRUPT = 6
  20.  
  21. local CHARCOALONLY = false
  22. local USEMODEM = false
  23.  
  24.  
  25. -- Arguments
  26. local tArgs = {...}
  27. for i=1,#tArgs do
  28. local arg = tArgs[i]
  29. if string.find(arg, "-") == 1 then
  30. for c=2,string.len(arg) do
  31. local ch = string.sub(arg,c,c)
  32. if ch == 'c' then
  33. CHARCOALONLY = true
  34. elseif ch == 'm' then
  35. USEMODEM = true
  36. else
  37. write("Invalid flag '")
  38. write(ch)
  39. print("'")
  40. end
  41. end
  42. end
  43. end
  44.  
  45.  
  46. function out(s)
  47.  
  48. s2 = s .. " @ [" .. x .. ", " .. y .. ", " .. z .. "]"
  49.  
  50. print(s2)
  51. if USEMODEM then
  52. rednet.broadcast(s2, "miningTurtle")
  53. end
  54. end
  55.  
  56. function dropInChest()
  57. turtle.turnLeft()
  58.  
  59. local success, data = turtle.inspect()
  60.  
  61. if success then
  62. if data.name == "minecraft:chest" then
  63.  
  64. out("Dropping items in chest")
  65.  
  66. for i=1, 16 do
  67. turtle.select(i)
  68.  
  69. data = turtle.getItemDetail()
  70.  
  71. if data ~= nil and
  72. data.name ~= "minecraft:charcoal" and
  73. (data.name == "minecraft:coal" and CHARCOALONLY == false) == false and
  74. (data.damage == nil or data.name .. data.damage ~= "minecraft:coal1") then
  75.  
  76. turtle.drop()
  77. end
  78. end
  79. end
  80. end
  81.  
  82. turtle.turnRight()
  83.  
  84. end
  85.  
  86. function goDown()
  87. while true do
  88. if turtle.getFuelLevel() <= fuelNeededToGoBack() then
  89. if not refuel() then
  90. return OUTOFFUEL
  91. end
  92. end
  93.  
  94. if not turtle.down() then
  95. turtle.up()
  96. z = z+1
  97. return
  98. end
  99. z = z-1
  100. end
  101. end
  102.  
  103. function fuelNeededToGoBack()
  104. return -z + x + y + 2
  105. end
  106.  
  107. function refuel()
  108. for i=1, 16 do
  109. -- Only run on Charcoal
  110. turtle.select(i)
  111.  
  112. item = turtle.getItemDetail()
  113. if item and
  114. (item.name == "minecraft:charcoal" or (item.name == "minecraft:coal" and
  115. (CHARCOALONLY == false or item.damage == 1))) and
  116. turtle.refuel(1) then
  117. return true
  118. end
  119. end
  120.  
  121. return false
  122. end
  123.  
  124. function moveH()
  125. if inv.isInventoryFull() then
  126. out("Dropping thrash")
  127. inv.dropThrash()
  128.  
  129. if inv.isInventoryFull() then
  130. out ("Stacking items")
  131. inv.stackItems()
  132. end
  133.  
  134. if inv.isInventoryFull() then
  135. out("Full inventory!")
  136. return FULLINV
  137. end
  138. end
  139.  
  140. if turtle.getFuelLevel() <= fuelNeededToGoBack() then
  141. if not refuel() then
  142. out("Out of fuel!")
  143. return OUTOFFUEL
  144. end
  145. end
  146.  
  147. if facingfw and y<max-1 then
  148. -- Going one way
  149. local dugFw = t.dig()
  150. if dugFw == false then
  151. out("Hit bedrock, can't keep going")
  152. return BLOCKEDMOV
  153. end
  154. t.digUp()
  155. t.digDown()
  156.  
  157. if t.fw() == false then
  158. return BLOCKEDMOV
  159. end
  160.  
  161. y = y+1
  162.  
  163. elseif not facingfw and y>0 then
  164. -- Going the other way
  165. t.dig()
  166. t.digUp()
  167. t.digDown()
  168.  
  169. if t.fw() == false then
  170. return BLOCKEDMOV
  171. end
  172.  
  173. y = y-1
  174.  
  175. else
  176. if x+1 >= max then
  177. t.digUp()
  178. t.digDown()
  179. return LAYERCOMPLETE -- Done with this Y level
  180. end
  181.  
  182. -- If not done, turn around
  183. if facingfw then
  184. turtle.turnRight()
  185. else
  186. turtle.turnLeft()
  187. end
  188.  
  189. t.dig()
  190. t.digUp()
  191. t.digDown()
  192.  
  193. if t.fw() == false then
  194. return BLOCKEDMOV
  195. end
  196.  
  197. x = x+1
  198.  
  199. if facingfw then
  200. turtle.turnRight()
  201. else
  202. turtle.turnLeft()
  203. end
  204.  
  205. facingfw = not facingfw
  206. end
  207.  
  208. return OK
  209. end
  210.  
  211. function digLayer()
  212.  
  213. local errorcode = OK
  214.  
  215. while errorcode == OK do
  216. if USEMODEM then
  217. local msg = rednet.receive(1)
  218. if msg ~= nil and string.find(msg, "return") ~= nil then
  219. return USRINTERRUPT
  220. end
  221. end
  222. errorcode = moveH()
  223. end
  224.  
  225. if errorcode == LAYERCOMPLETE then
  226. return OK
  227. end
  228.  
  229. return errorcode
  230. end
  231.  
  232. function goToOrigin()
  233.  
  234. if facingfw then
  235.  
  236. turtle.turnLeft()
  237.  
  238. t.fw(x)
  239.  
  240. turtle.turnLeft()
  241.  
  242. t.fw(y)
  243.  
  244. turtle.turnRight()
  245. turtle.turnRight()
  246.  
  247. else
  248.  
  249. turtle.turnRight()
  250.  
  251. t.fw(x)
  252.  
  253. turtle.turnLeft()
  254.  
  255. t.fw(y)
  256.  
  257. turtle.turnRight()
  258. turtle.turnRight()
  259.  
  260. end
  261.  
  262. x = 0
  263. y = 0
  264. facingfw = true
  265.  
  266. end
  267.  
  268. function goUp()
  269.  
  270. while z < 0 do
  271.  
  272. t.up()
  273.  
  274. z = z+1
  275.  
  276. end
  277.  
  278. goToOrigin()
  279.  
  280. end
  281.  
  282. function mainloop()
  283.  
  284. while true do
  285.  
  286. local errorcode = digLayer()
  287.  
  288. if errorcode ~= OK then
  289. goUp()
  290. return errorcode
  291. end
  292.  
  293. goToOrigin()
  294.  
  295. for i=1, 3 do
  296. t.digDown()
  297. success = t.down()
  298.  
  299. if not success then
  300. goUp()
  301. return BLOCKEDMOV
  302. end
  303.  
  304. z = z-1
  305. out("Z: " .. z)
  306.  
  307. end
  308. end
  309. end
  310.  
  311. if USEMODEM then
  312. rednet.open("right")
  313. end
  314.  
  315. out("\n\n\n-- WELCOME TO THE MINING TURTLE --\n\n")
  316.  
  317. while true do
  318.  
  319. goDown()
  320.  
  321. local errorcode = mainloop()
  322. dropInChest()
  323.  
  324. if errorcode ~= FULLINV then
  325. break
  326. end
  327. end
  328.  
  329. if USEMODEM then
  330. rednet.close("right")
  331. end')
  332. quarry.close()
  333. write("done!")
  334.  
  335. print("Downloading 'inv'")
  336. local inv = fs.open("inv", "w")
  337. inv.write('function isInventoryFull()
  338. for i=1, 16 do
  339. if turtle.getItemCount(i) == 0 then
  340. return false
  341. end
  342. end
  343.  
  344. return true
  345. end
  346.  
  347. -- Fixes inventory scattering.
  348. function stackItems()
  349. -- Remember seen items
  350. m = {}
  351.  
  352. for i=1, 16 do
  353. local this = turtle.getItemDetail(i)
  354.  
  355. if this ~= nil then
  356. -- Slot is not empty
  357.  
  358. local saved = m[this.name .. (this.damage or "")]
  359.  
  360. if saved ~= nil then
  361. -- We've seen this item before in the inventory
  362.  
  363. local ammount = this.count
  364.  
  365. turtle.select(i)
  366. turtle.transferTo(saved.slot)
  367.  
  368. if ammount > saved.space then
  369. -- We have leftovers, and now the
  370. -- saved slot is full, so we replace
  371. -- it by the current one
  372.  
  373. saved.slot = i
  374. saved.count = ammount - saved.space
  375. -- Update on table.
  376. m[this.name .. (this.damage or "")] = saved
  377.  
  378. elseif ammount == saved.space then
  379. -- Just delete the entry
  380.  
  381. m[this.name .. (this.damage or "")] = nil
  382.  
  383. end
  384.  
  385. else
  386. -- There isn't another slot with this
  387. -- item so far, so sign this one up.
  388.  
  389. this.slot = i
  390. this.space = turtle.getItemSpace(i)
  391.  
  392. m[this.name .. (this.damage or "")] = this
  393.  
  394. end
  395. end
  396. end
  397. end
  398.  
  399. function selectItem(name)
  400. for i=1, 16 do
  401. local data = turtle.getItemDetail(i)
  402. if data and data.name == name then
  403. turtle.select(i)
  404. return true
  405. end
  406. end
  407. return false
  408. end
  409.  
  410. function getItemCount(name)
  411. local count = 0
  412. for i=1, 16 do
  413. local data = turtle.getItemDetail(i)
  414. if data and data.name == name then
  415. count = count + data.count
  416. end
  417. end
  418. return count
  419. end
  420.  
  421. function dropThrash()
  422. local thrash = {"minecraft:stone", "minecraft:granite", "minecraft:andesite", "minecraft:diorite", "minecraft:cobbled_deepslate", "minecraft:tuff", "minecraft:dirt", "minecraft:coarse_dirt", "minecraft:gravel", "minecraft:sand", "minecraft:red_sand", "minecraft:cobblestone", "minecraft:sandstone", "minecraft:red_sandstone", "minecraft:bedrock", "chisel:limestone", "chisel:marble", "chisel:diorite", "chisel:granite", "chisel:andesite", "harvestcraft:salt"}
  423.  
  424. for i=1, 16 do
  425.  
  426. details = turtle.getItemDetail(i)
  427.  
  428. if details then
  429.  
  430. for j=1, #thrash do
  431. if details.name == thrash[j] then
  432. turtle.select(i)
  433. turtle.drop()
  434. end
  435. end
  436. end
  437. end
  438. end')
  439. inv.close()
  440. print("done!")
  441.  
  442. print("Downloading 't'")
  443. local t = fs.open("t", "w")
  444. t.write('local MAXTRIES = 100
  445.  
  446. function turnAround()
  447. local success = false
  448.  
  449. success = turtle.turnRight()
  450. success = success and turtle.turnRight()
  451.  
  452. return success
  453. end
  454.  
  455. function dig()
  456. local tries = 0
  457.  
  458. while turtle.detect() do
  459.  
  460. local s, data = turtle.inspect()
  461. if data.name == "minecraft:bedrock" then
  462. printError("Hit bedrock forwards!")
  463. return false
  464. end
  465.  
  466. turtle.dig()
  467. sleep(0.4)
  468.  
  469. tries = tries+1
  470. if tries > MAXTRIES then
  471.  
  472. printError("Can't dig forward")
  473. return false
  474.  
  475. end
  476. end
  477.  
  478. return true
  479. end
  480.  
  481. function digDown()
  482. local tries = 0
  483.  
  484. while turtle.detectDown() do
  485.  
  486. local s, data = turtle.inspectDown()
  487. if data.name == "minecraft:bedrock" then
  488. printError("Hit bedrock below!")
  489. return false
  490. end
  491.  
  492. turtle.digDown()
  493. sleep(0.4)
  494.  
  495. tries = tries+1
  496. if tries > MAXTRIES then
  497. printError("Can't dig down")
  498. return false
  499. end
  500. end
  501.  
  502. return true
  503. end
  504.  
  505. function digUp()
  506. local tries = 0
  507.  
  508. while turtle.detectUp() do
  509.  
  510. local s, data = turtle.inspectUp()
  511. if data.name == "minecraft:bedrock" then
  512. printError("Hit bedrock above!")
  513. return false
  514. end
  515.  
  516. turtle.digUp()
  517. sleep(0.4)
  518.  
  519. tries = tries+1
  520. if tries > MAXTRIES then
  521. printError("Can't dig up")
  522. return false
  523. end
  524. end
  525.  
  526. return true
  527. end
  528.  
  529.  
  530. function fw(l)
  531. l=l or 1
  532.  
  533. for i=1, l do
  534.  
  535. local tries = 0
  536.  
  537. while turtle.forward() ~= true do
  538.  
  539. turtle.dig()
  540. turtle.attack()
  541. sleep(0.2)
  542.  
  543. tries = tries+1
  544. if tries > MAXTRIES then
  545. printError("Can't move forward")
  546. return false
  547. end
  548. end
  549. end
  550.  
  551. return true
  552. end
  553.  
  554. function up(l)
  555. l=l or 1
  556.  
  557. for i=1, l do
  558.  
  559. local tries = 0
  560.  
  561. while turtle.up() ~= true do
  562.  
  563. turtle.digUp()
  564. turtle.attackUp()
  565. sleep(0.2)
  566.  
  567. tries = tries+1
  568. if tries > MAXTRIES then
  569. printError("Can't move up")
  570. return false
  571. end
  572. end
  573. end
  574.  
  575. return true
  576. end
  577.  
  578. function down(l)
  579. l=l or 1
  580.  
  581. for i=1, l do
  582.  
  583. local tries = 0
  584.  
  585. while turtle.down() ~= true do
  586.  
  587. turtle.digDown()
  588. turtle.attackDown()
  589. sleep(0.2)
  590.  
  591. tries = tries+1
  592. if tries > MAXTRIES then
  593. printError("Can't move down")
  594. return false
  595. end
  596. end
  597. end
  598.  
  599. return true
  600. end
  601.  
  602. function back(l)
  603. l=l or 1
  604.  
  605. for i=1, l do
  606.  
  607. if turtle.back() ~= true then
  608. turnAround()
  609. fw()
  610. turnAround()
  611. end
  612. end
  613. end')
  614. t.close()
  615. print("done!")
  616.  
  617. print("")
  618. print("Download successful! run with 'quarry'")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement