bldng1

lib

Jul 28th, 2021 (edited)
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.88 KB | None | 0 0
  1. stack = {}--save
  2. stacksize=0--save
  3.  
  4.  
  5. function push(x)
  6. stacksize=stacksize+1
  7. stack[stacksize]=x
  8. fsave({stack=stack,stacksize=stacksize})
  9. end
  10.  
  11. function pop()
  12. a=get()
  13. stack[stacksize]=nil
  14. stacksize=stacksize-1
  15. fsave({stack=stack,stacksize=stacksize})
  16. return a
  17. end
  18.  
  19. function get()
  20. return stack[stacksize]
  21. end
  22.  
  23. function tablelength(T)
  24. local count = 0
  25. for _ in pairs(T) do count = count + 1 end
  26. return count
  27. end
  28.  
  29. goingback=false--saved
  30. vein=false--saved
  31. x=0--FORWARDS+ saved
  32. y=0--RIGHT+ saved
  33. z=0--UP+ saved
  34. sx=0--saved
  35. sy=0--saved
  36. sz=0--saved
  37. delay=0.05
  38.  
  39. facing=0--saved
  40. FORWARD=0
  41. RIGHT=1
  42. BACK=2
  43. LEFT=3
  44. DOWN=4
  45. UP=5
  46. junk={}
  47. worth={}
  48. clearfrom=2
  49. function clearfroms(slot)
  50. clearfrom=slot
  51. end
  52. function addtoworth(filter)
  53. if type(filter)=="table" then
  54. for index, value in ipairs(filter) do
  55. worth[#worth+1]=value
  56. end
  57. else
  58. worth[#worth+1]=filter
  59. end
  60. end
  61. function addtojunk(filter)
  62. if type(filter)=="table" then
  63. for index, value in ipairs(filter) do
  64. junk[#junk+1]=value
  65. end
  66. else
  67. junk[#junk+1]=filter
  68. end
  69. end
  70. function split(inputstr, sep)
  71. if sep == nil then
  72. sep = "%s"
  73. end
  74. local t={}
  75. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  76. table.insert(t, str)
  77. end
  78. return t
  79. end
  80. function toItem(id)--minecraft:cobblestone
  81. if id==nil then
  82. return "None"
  83. end
  84. local ls=split(id,":")
  85. if ls[2]~=nil then
  86. return string.lower(split(id,":")[2])
  87. end
  88. return id
  89. end
  90. function contains(tab, val)
  91. for index, value in ipairs(tab) do
  92. if value == val then
  93. return true
  94. end
  95. end
  96. return false
  97. end
  98. function containsc(tab, val)
  99. for index, value in ipairs(tab) do
  100. print(value.." "..val)
  101. if string.find(val,value) then
  102. return true
  103. end
  104. end
  105. return false
  106. end
  107. function rotate(dir)
  108. while not (dir==facing) do
  109. if dir<facing then
  110. turtle.turnLeft()
  111. facing=facing-1
  112. else
  113. turtle.turnRight()
  114. facing=facing+1
  115. end
  116. end
  117. fsave({facing=facing})
  118. end
  119. function checkOreslite()
  120. local blocku=getFBlocku()
  121. local blockd=getFBlockd()
  122. local block=getFBlock()
  123. if turtle.getFuelLevel()<turtle.getFuelLimit() then
  124. if block=="lava" and getFBlockdata().state.level==0 then
  125. setItem("bucket",1)
  126. turtle.select(1)
  127. turtle.place()
  128. turtle.refuel(1)
  129. end
  130. if blocku=="lava" and getFBlockdatau().state.level==0 then
  131. setItem("bucket",1)
  132. turtle.select(1)
  133. turtle.placeUp()
  134. turtle.refuel(1)
  135. end
  136. if blockd=="lava" and getFBlockdatad().state.level==0 then
  137. setItem("bucket",1)
  138. turtle.select(1)
  139. turtle.placeDown()
  140. turtle.refuel(1)
  141. end
  142. end
  143. if not vein then
  144. if isWorth(blocku) then
  145. veinmine(UP)
  146. end
  147. if isWorth(blockd) then
  148. veinmine(DOWN)
  149. end
  150. if isWorth(block) then
  151. veinmine(facing)
  152. end
  153. end
  154. if isChest(blocku) then
  155. lootChest(UP)
  156. end
  157. if isChest(blockd) then
  158. lootChest(DOWN)
  159. end
  160. if isChest(block) then
  161. lootChest(FORWARD)
  162. end
  163. end
  164. function movef()
  165. checkOreslite()
  166. if(turtle.detect()) then
  167. while not turtle.forward() do
  168. turtle.dig()
  169. turtle.attack()
  170. sleep(delay)
  171. end
  172. else
  173. turtle.forward()
  174. end
  175. if(facing==FORWARD) then
  176. x=x+1
  177. elseif(facing==BACK) then
  178. x=x-1
  179. elseif(facing==RIGHT) then
  180. y=y+1
  181. elseif(facing==LEFT) then
  182. y=y-1
  183. end
  184. fsave({y=y,x=x})
  185. checkOreslite()
  186. end
  187. function movev(dir)
  188. if dir==DOWN then
  189. while not turtle.down() do
  190. turtle.digDown()
  191. sleep(delay)
  192. end
  193. z=z-1;
  194. else
  195. while not turtle.up() do
  196. turtle.digUp()
  197. sleep(delay)
  198. end
  199. z=z+1;
  200. end
  201. fsave({z=z})
  202. end
  203. function move(mx,my,mz)
  204. local dir=0;
  205. if not (mx==0) then
  206. rotate(getMoveDir(mx,0))
  207. mx=math.abs(mx);
  208. while not (mx==0) do
  209. movef()
  210. mx=mx-1;
  211. end
  212. end
  213. if not (my==0) then
  214. rotate(getMoveDir(0,my))
  215. my=math.abs(my);
  216. while not (my==0) do
  217. movef()
  218. my=my-1;
  219. end
  220. end
  221. while not (mz==0) do
  222. checkOreslite()
  223. if mz<0 then
  224. movev(DOWN)
  225. mz=mz+1;
  226. else
  227. movev(UP)
  228. mz=mz-1;
  229. end
  230. checkOreslite()
  231. end
  232. checkOreslite()
  233. end
  234. function getMoveDir(mx,my)
  235. if mx==0 and my==0 then
  236. return facing;
  237. end
  238. if mx>0 and my==0 then
  239. return FORWARD;
  240. end
  241. if mx<0 and my==0 then
  242. return BACK;
  243. end
  244. if mx==0 and my<0 then
  245. return LEFT;
  246. end
  247. if mx==0 and my>0 then
  248. return RIGHT;
  249. end
  250. end
  251. function getFBlockd()
  252. local success, ydata = turtle.inspectDown()
  253. if success then
  254. return toItem(ydata.name)
  255. else
  256. return nil
  257. end
  258. end
  259. function getFBlocku()
  260. local success, ydata = turtle.inspectUp()
  261. if success then
  262. return toItem(ydata.name)
  263. else
  264. return nil
  265. end
  266. end
  267. function getRedstone()
  268. local success, ydata = turtle.inspect()
  269. if success and string.find(getFBlock(), "redstone") then
  270. return tonumber(ydata.state.power)
  271. else
  272. return 0
  273. end
  274. end
  275. function getFBlock()
  276. local success, ydata = turtle.inspect()
  277. if success then
  278. return toItem(ydata.name)
  279. else
  280. return nil
  281. end
  282. end
  283. function getFBlockdata()
  284. local success, ydata = turtle.inspect()
  285. if success then
  286. return ydata
  287. else
  288. return nil
  289. end
  290. end
  291. function getFBlockdatau()
  292. local success, ydata = turtle.inspectUp()
  293. if success then
  294. return ydata
  295. else
  296. return nil
  297. end
  298. end
  299. function getFBlockdatad()
  300. local success, ydata = turtle.inspectDown()
  301. if success then
  302. return ydata
  303. else
  304. return nil
  305. end
  306. end
  307. function isWorth(block)
  308. if block==nil then
  309. return false
  310. end
  311. return containsc(worth, block)
  312. end
  313. function isChest(block)
  314. if block==nil then
  315. return false
  316. end
  317. return string.find(block, "chest")
  318. end
  319. function shouldret()
  320. return turtle.getFuelLevel()-(math.abs(x)+math.abs(y)+math.abs(z))<200;
  321. end
  322. function shouldretc(lfuel)
  323. return turtle.getFuelLevel()-(lfuel+math.abs(x)+math.abs(y)+math.abs(z))<200;
  324. end
  325. function findOre()
  326. if isWorth(getFBlocku()) then
  327. return UP
  328. end
  329. if isWorth(getFBlockd()) then
  330. return DOWN
  331. end
  332. if isChest(getFBlocku()) then
  333. lootChest(UP)
  334. end
  335. if isChest(getFBlockd()) then
  336. lootChest(DOWN)
  337. end
  338. rotate(FORWARD)
  339. if isChest(getFBlock()) then
  340. lootChest(FORWARD)
  341. end
  342. if isWorth(getFBlock()) then
  343. return FORWARD
  344. end
  345. rotate(RIGHT)
  346. if isChest(getFBlock()) then
  347. lootChest(FORWARD)
  348. end
  349. if isWorth(getFBlock()) then
  350. return RIGHT
  351. end
  352. rotate(BACK)
  353. if isChest(getFBlock()) then
  354. lootChest(FORWARD)
  355. end
  356. if isWorth(getFBlock()) then
  357. return BACK
  358. end
  359. rotate(LEFT)
  360. if isChest(getFBlock()) then
  361. lootChest(FORWARD)
  362. end
  363. if isWorth(getFBlock()) then
  364. return LEFT
  365. end
  366. return -1
  367. end
  368. function veinmine(dir)
  369. if goingback then
  370. return
  371. end
  372. vein=true
  373. fsave({vein=vein})
  374. print("Mining Vein")
  375. sx=x
  376. sy=y
  377. sz=z
  378. fsave({sx=sx,sy=sy,sz=sz})
  379. local nx=0
  380. local ny=0
  381. local nz=0
  382. push(nz)
  383. push(ny)
  384. push(nx)
  385. startdir=facing
  386. while dir>=0 or stacksize>1 do
  387. if dir>=0 then
  388. -- move to next and look for ores
  389. if dir<4 then
  390. rotate(dir)
  391. movef()
  392. else
  393. if dir==DOWN then
  394. move(0,0,-1)
  395. else
  396. move(0,0,1)
  397. end
  398. end
  399. nx=x-sx
  400. ny=y-sy
  401. nz=z-sz
  402. push(nz)
  403. push(ny)
  404. push(nx)
  405. dir=findOre()
  406. else
  407. local vx=pop()-nx
  408. local vy=pop()-ny
  409. local vz=pop()-nz
  410. move(vx,vy,vz)
  411. nx=x-sx
  412. ny=y-sy
  413. nz=z-sz
  414. dir=findOre()
  415. end
  416. end
  417. rotate(startdir)
  418. print("Mined Vein")
  419. vein=false
  420. fsave({vein=vein})
  421. end
  422. function goback()
  423. goingback=true
  424. fsave({goingback=goingback})
  425. print("Going Back")
  426. move(-x,-y,-z)
  427. rotate(FORWARD)
  428. goingback=false
  429. fsave({goingback=goingback})
  430. end
  431. function checkOres()
  432. if findOre()>0 then
  433. veinmine(facing)
  434. end
  435. end
  436. function deposit()
  437. if not isChest(getFBlock()) then
  438. return false
  439. end
  440. for i=clearfrom,16 do
  441. turtle.select(i)
  442. while turtle.drop() == false and turtle.getItemCount() ~= 0 do -- Waits until turtle can deposit item
  443. sleep(1)
  444. end
  445. turtle.drop()
  446. end
  447. turtle.select(1)
  448. return true
  449. end
  450. function check()
  451. if turtle.getItemCount(15) == 0 then
  452. return false
  453. else
  454. return true
  455. end
  456. end
  457. function suck(dir)
  458. if dir==UP then
  459. return turtle.suckUp()
  460. elseif dir==DOWN then
  461. return turtle.suckDown()
  462. else
  463. rotate(dir)
  464. return turtle.suck()
  465. end
  466. end
  467. function lootChest(dir)
  468. if not (x==0 and y==0 and z==0) then
  469. if check() then
  470. clearInv()
  471. end
  472. while suck(dir) do
  473. if check() then
  474. clearInv()
  475. end
  476. end
  477. end
  478. end
  479. function clearInv()
  480. print("Clearing Inv")
  481. for i=clearfrom,16 do
  482. if turtle.getItemCount(i) > 0 then
  483. print(toItem(turtle.getItemDetail(i).name))
  484. if containsc(junk,toItem(turtle.getItemDetail(i)["name"])) then
  485. turtle.select(i)
  486. turtle.drop()
  487. end
  488. end
  489. end
  490. turtle.select(1)
  491. if check() then
  492. print("Going back to clear inventory")
  493. local cdir=facing
  494. if stacksize>1 then
  495. for i=stacksize,2,-3 do
  496. local vx=stack[i]-(x-sx)
  497. local vy=stack[i-1]-(y-sy)
  498. local vz=stack[i-2]-(z-sz)
  499. move(vx,vy,vz)
  500. end
  501. end
  502. local hx=x
  503. local hy=y
  504. local hz=z
  505. goback()
  506. rotate(BACK)
  507. if not deposit() then
  508. print("Couldnt find Chest")
  509. exit()
  510. end
  511. if shouldretc((hx+hy+hz)*2) then
  512. print("Got no fuel left")
  513. exit()
  514. end
  515. move(hx,hy,hz)
  516. if stacksize>1 then
  517. for i=3,stacksize,3 do
  518. local vx=stack[i]-(x-sx)
  519. local vy=stack[i-1]-(y-sy)
  520. local vz=stack[i-2]-(z-sz)
  521. move(vx,vy,vz)
  522. end
  523. end
  524. rotate(cdir)
  525. end
  526. end
  527. function getItem(slot)
  528. local ydata = turtle.getItemDetail(slot)
  529. if ydata ~= nil then
  530. return toItem(ydata.name)
  531. else
  532. return nil
  533. end
  534. end
  535. function freespace(slot)
  536. turtle.select(slot)
  537. for i=1,16 do
  538. if i~=slot then
  539. if turtle.getItemCount(i)==0 then
  540. turtle.transferTo(i)
  541. return
  542. end
  543. end
  544. end
  545. clearInv()
  546. for i=1,16 do
  547. if i~=slot then
  548. if turtle.getItemCount(i)==0 then
  549. turtle.transferTo(i)
  550. return
  551. end
  552. end
  553. end
  554. end
  555. function setItem(item,slot)
  556. if string.find(toItem(getItem(slot)), item) then
  557. return
  558. end
  559. if turtle.getItemCount(slot)>0 then
  560. freespace(slot)
  561. end
  562. for i=1,16 do
  563. if string.find(toItem(getItem(i)), item) then
  564. turtle.select(i)
  565. turtle.transferTo(slot)
  566. end
  567. end
  568. end
  569. function atSpawn()
  570. return x==0 and y==0 and z==0
  571. end
  572. save={}
  573. --shell.getRunningProgram()
  574. function registerRunning(path)
  575. local r=fs.open("resume","w")
  576. r.write(path)
  577. r.close()
  578. local s=fs.open("save","w")
  579. s.close()
  580. end
  581. function unregister()
  582. fs.delete("resume")
  583. fs.delete("save")
  584. end
  585.  
  586. function fstd(tab)
  587. for k, v in pairs(tab) do
  588. if save[k]==nil then
  589. save[k]=v
  590. end
  591. end
  592. end
  593. function fsave(tab)
  594. for k, v in pairs(tab) do
  595. save[k]=v
  596. print("Saving",k)
  597. end
  598. local f=fs.open("save","w")
  599. f.write(textutils.serialize(save))
  600. f.flush()
  601. f.close()
  602. end
  603. function fload(name)
  604. return save["name"]
  605. end
  606. function isResumed()
  607. return fs.exists("resume")
  608. end
  609. function tobool(str)
  610. return str=="true"
  611. end
  612. function loadstatus()
  613. if isResumed() then
  614. if fs.exists("save") then
  615. local savefile=fs.open("save","r")
  616. save=textutils.unserialize(savefile.readAll())
  617. savefile.close()
  618. end
  619. fstd({
  620. x=x,
  621. y=y,
  622. z=z,
  623. facing=facing,
  624. vein=vein,
  625. goingback=goingback,
  626. stack=stack,
  627. stacksize=stacksize,
  628. sx=sx,
  629. sy=sy,
  630. sz=sz
  631. })
  632. print("Loading last coords")
  633. print(textutils.tabulate(save))
  634. x=tonumber(fload("x"))
  635. y=tonumber(fload("y"))
  636. z=tonumber(fload("z"))
  637. facing=tonumber(fload("facing"))
  638. vein=tobool(fload("vein"))
  639. if vein then
  640. --resume
  641. --TODO return to veinmine and handle clear Inventory
  642. end
  643. goingback=tobool(fload("goingback"))
  644. if goingback then
  645. goback()
  646. end
  647. print("table type: "..type(fload("stack")))
  648. stack =fload("stack")
  649. stacksize =tonumber(fload("stacksize"))
  650. sx =tonumber(fload("sx"))
  651. sy =tonumber(fload("sy"))
  652. sz =tonumber(fload("sz"))
  653. end
  654. end
  655. loadstatus()
Advertisement
Add Comment
Please, Sign In to add comment