Sedrowow

FancyDIG

May 1st, 2025 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 34.33 KB | None | 0 0
  1. -- This is an API for digging and
  2. -- keeping track of motion
  3.  
  4.  
  5. -- x is right, y is up, z is forward
  6. -- r is clockwise rotation in degrees
  7.  
  8. -------------------------------------
  9. -- |¯\  [¯¯]  /¯¯]    /\  |¯\ [¯¯] --
  10. -- |  |  ][  | [¯|   |  | | /  ][  --
  11. -- |_/  [__]  \__|   |||| ||  [__] --
  12. -------------------------------------
  13.  
  14. os.loadAPI("flex.lua")
  15.  
  16. local options = {
  17.   "outputblocks",
  18.   "blacklist",
  19.   "buildingblocks",
  20.   "dumplist",
  21.   "fluids"  }
  22.  
  23.  
  24. -- Fluids, aka blocks treated as "Empty"
  25. options[options[5]] = {
  26.   ":air", "water", "lava", "acid", "poison",
  27.   "sewage", "sludge", "blood"  }
  28.  
  29.  
  30. -- Turtle will only drop loot into blocks
  31. -- that contain any of these keywords.
  32. options[options[1]] = {
  33.   "chest", "storage", "box", "turtle",
  34.   "hopper", "dropper", "backpack" }
  35.  
  36.  
  37. -- Turtle will avoid breaking (or getting
  38. -- stuck by) these blocks.
  39. -- Disabled by default;
  40. -- enable with dig.doBlacklist()
  41. options[options[2]] = { "chest", "spawn",
  42.     "hopper", "dropper", "portal", "turtle",
  43.     "hive", "openblocks:grave" }
  44. doblacklist = false
  45. function doBlacklist(x)
  46.  if x == nil then
  47.   x = true
  48.  end --if
  49.  doblacklist = (x==true)
  50. end --function
  51.  
  52.  
  53. -- Run dig.setBlockSlot(n) to choose
  54. -- a slot to keep building blocks stocked
  55. -- The list of valid keywords:
  56. options[options[3]] = { "cobblestone",
  57.   "minecraft:stone", "dirt", "netherrack",
  58.   "basalt", "soul_s", "magma_block",
  59.   "terracotta", "rock", "sandstone",
  60.   "andesite", "diorite", "granite",
  61.   "marble", "bricks", "smooth_stone",
  62.   "glass" }
  63. function isBuildingBlock(n)
  64.  return flex.isItem(options[options[3]],n)
  65. end --function
  66.  
  67.  
  68.  
  69. -- Turtle will dispose of these blocks
  70. -- at the command dig.doDump()/
  71. -- dig.doDumpUp()/dig.doDumpDown().
  72. -- You may add/remove entries by using
  73. -- dig.addToDumpList(<string>) or
  74. -- dig.removeFromDumpList(<string>).
  75. local dump = options[4]
  76. options[dump] = {}
  77.  
  78. function resetDumpList(n)
  79.  if n == 0 then
  80.   options[dump] = {}
  81.  else
  82.   options[dump] = {
  83.      "cobblestone", "dirt", "gravel",
  84.      "andesite", "diorite", "granite",
  85.      "netherrack", "soul_s", "magma_block",
  86.      "rotten_flesh", "rock", "marble",
  87.      "limestone", "soapstone", "dolomite",
  88.      "gabbro", "scoria" }
  89.  end --if/else
  90. end --function
  91.  
  92. function isDumpItem(x)
  93.  return flex.isItem(options["dump"],x)
  94. end --function
  95.  
  96. function addToDumpList(newkey)
  97.  options[dump][#options[dump]+1] =
  98.      tostring(newkey)
  99. end --function
  100.  
  101. function removeFromDumpList(key)
  102.  local x,z
  103.  z = {}
  104.  for x=1,#options[dump] do
  105.   if string.find(options[dump][x],key)==nil then
  106.    z[#z+1] = options[dump][x]
  107.   end --if
  108.  end --for
  109.  options[dump] = z
  110. end --function
  111.  
  112. resetDumpList()
  113.  
  114.  
  115. function doDump(z)
  116.  z = z or "fwd"
  117.  local slot = turtle.getSelectedSlot()
  118.  local blockCount = blockStacks
  119.  local x
  120.  for x=1,16 do
  121.   turtle.select(x)
  122.   if flex.isItem(options[dump]) then
  123.  
  124.    if flex.isItem(options["buildingblocks"]) then
  125.     blockCount = blockCount - 1
  126.    end --if
  127.  
  128.    if blockCount <= 0 then
  129.     if z == "fwd" then
  130.      turtle.drop()
  131.     elseif z == "down" then
  132.      turtle.dropDown()
  133.     elseif z == "up" then
  134.      turtle.dropUp()
  135.     end --if/else (direction)
  136.    end --if (no more blocks needed)
  137.  
  138.   end --if dump item
  139.  end --for
  140.  checkBlocks()
  141.  flex.condense(blockSlot)
  142.  turtle.select(slot)
  143. end --function
  144.  
  145. function doDumpDown() doDump("down") end
  146. function doDumpUp() doDump("up") end
  147.  
  148.  
  149.  
  150.  
  151. -- Attack entities that block the way
  152. attack = false
  153. function doAttack(x)
  154.  if x == nil then
  155.   x = true
  156.  end --if
  157.  attack = ( x == true )
  158. end --function
  159.  
  160.  
  161. function isChest(dir)
  162.  return flex.isBlock(
  163.    options["outputblocks"],dir)
  164. end --function
  165. function isChestUp()
  166.  return isChest("up")
  167. end
  168. function isChestDown()
  169.  return isChest("down")
  170. end
  171.  
  172.  
  173. local knownBedrock = {}
  174. function getKnownBedrock()
  175.  return knownBedrock
  176. end --function
  177.  
  178.  
  179.  
  180.  
  181. ------------------------------------
  182. --  /¯\ |¯\[¯¯][¯¯] /¯\ |\ ||/¯¯\ --
  183. -- | O || / ||  ][ | O || \ |\_¯\ --
  184. --  \_/ ||  || [__] \_/ || \|\__/ --
  185. ------------------------------------
  186. --      |¯¯][¯¯]||  |¯¯]          --
  187. --      | ]  ][ ||_ | ]           --
  188. --      ||  [__]|__]|__]          --
  189. ------------------------------------
  190.  
  191. local options_file = "dig_options.cfg"
  192.  
  193. function optionsEdit()
  194.  shell.run("edit "..options_file)
  195.  optionsImport()
  196. end --function
  197.  
  198.  
  199. function optionsImport()
  200.  if not fs.exists(options_file) then
  201.   return false
  202.  end --if
  203.  local file,x,y,z
  204.  
  205.  for y=1,#options do
  206.   z = options[y]
  207.   file = fs.open(options_file,"r")
  208.  
  209.   x = file.readLine()
  210.   while x ~= nil do
  211.  
  212.    if x == "["..z.."]" then
  213.     options[z] = {}
  214.     x = file.readLine()
  215.     while x ~= "[/"..z.."]" and x ~= nil do
  216.      options[z][#options[z]+1] = x
  217.      x = file.readLine()
  218.     end --while
  219.     break
  220.    end --if
  221.  
  222.    x = file.readLine()
  223.   end --while
  224.  
  225.   file.close()
  226.  end --for
  227.  
  228.  return true
  229. end --function
  230.  
  231.  
  232. function optionsExport()
  233.  if fs.exists(options_file) then
  234.   fs.delete(options_file)
  235.  end --if
  236.  
  237.  local file,x,y,z
  238.  while file == nil do
  239.   file = fs.open(options_file,"w")
  240.  end --while
  241.  file.writeLine("# Dig API Options File #\n")
  242.  
  243.  for y=1,#options do
  244.   z = options[y]
  245.   file.writeLine("["..z.."]")
  246.   for x=1,#options[z] do
  247.    file.writeLine(options[z][x])
  248.   end --for
  249.   file.writeLine("[/"..z.."]\n")
  250.  end --for
  251.  
  252.  file.close()
  253.  return true
  254. end --function
  255.  
  256.  
  257. if not optionsImport() then
  258.  optionsExport()
  259. end --if
  260.  
  261.  
  262.  
  263.  
  264. ----------------------------------
  265. --    /¯¯\  /\  ||  || |¯¯]     --
  266. --    \_¯\ |  |  \\//  | ]      --
  267. --    \__/ ||||   \/   |__]     --
  268. ----------------------------------
  269. -- |¯\  /\   /¯] ||// || || |¯\ --
  270. -- | < |  | | [  | <  ||_|| | / --
  271. -- |_/ ||||  \_] ||\\  \__| ||  --
  272. ----------------------------------
  273.  
  274. local save = {"dig_save", ".cfg"}
  275. local savefile = save[1] .. save[2]
  276. local start = {"startup", ".lua"}
  277. local startfile = start[1] .. start[2]
  278.  
  279. function saveExists()
  280.  return ( fs.exists(startfile) and
  281.     fs.exists(savefile) )
  282. end --function
  283.  
  284. function saveClear()
  285.  if fs.exists(startfile) then
  286.   fs.delete(startfile)
  287.  end --if
  288. end --function
  289. function clearSave()
  290.  saveClear()
  291. end --function
  292.  
  293.  
  294. local args = {...}
  295. if #args > 0 then
  296.  local a,file
  297.  
  298.  if args[1] == "save" then
  299.   if not saveExists() then
  300.    flex.send("Nothing to save",colors.yellow)
  301.    return
  302.   end --if
  303.   if #args == 1 then
  304.    a = 1
  305.    while fs.exists(start[1].."_"..
  306.         tostring(a)..start[2]) do
  307.     a = a + 1
  308.    end --while
  309.   else -- #args > 1
  310.    a = args[2]
  311.   end --if
  312.   a = tostring(a)
  313.   shell.run("mv "..startfile.." "..
  314.       start[1].."_"..a..start[2])
  315.   shell.run("mv "..savefile.." " ..
  316.       save[1].."_"..a..save[2])
  317.   flex.send("Enter 'dig load "..a..
  318.       "' to restore",colors.lightBlue)
  319.  
  320.  elseif args[1] == "load" then
  321.   if #args == 1 then
  322.    flex.send("Please specify load file",
  323.      colors.red)
  324.    return
  325.   end --if
  326.   saveClear()
  327.   a = args[2]
  328.   shell.run("mv "..start[1].."_"..a..
  329.      start[2].." "..startfile)
  330.   shell.run("mv "..save[1].."_"..a..
  331.      save[2].." "..savefile)
  332.   flex.send("Save files restored",
  333.      colors.lightBlue)
  334.  
  335.  elseif args[1] == "clear" then
  336.   saveClear()
  337.   flex.send("Save files cleared",
  338.       colors.lightBlue)
  339.  
  340.  elseif args[1] == "edit" then
  341.   optionsEdit()
  342.  
  343.  else -- not save/load/clear/edit
  344.   flex.send("Invalid parameter: "..
  345.       args[1],colors.red)
  346.  
  347.  end --if/else save/load
  348.  return
  349. end --function
  350.  
  351. -----------------------------------------
  352. -- ||   /¯\  /¯] /\ [¯¯][¯¯] /¯\ |\ || --
  353. -- ||_ | O || [ |  | ||  ][ | O || \ | --
  354. -- |__] \_/  \_]|||| || [__] \_/ || \| --
  355. -----------------------------------------
  356. --||  || /\ |¯\[¯¯] /\ |¯\||  |¯¯]/¯¯\ --
  357. -- \\// |  || / ][ |  || <||_ | ] \_¯\ --
  358. --  \/  ||||| \[__]|||||_/|__]|__]\__/ --
  359. -----------------------------------------
  360. xdist = 0
  361. xlast = -1
  362. xmin = 0
  363. xmax = 0
  364.  
  365. ydist = 0
  366. ylast = -1
  367. ymin = 0
  368. ymax = 0
  369.  
  370. zdist = 0
  371. zlast = -1
  372. zmin = 0
  373. zmax = 0
  374.  
  375. rdist = 0
  376. rlast = -1
  377.  
  378. lastmove = "r-"
  379. dugtotal = 0
  380.  
  381.  
  382. function getx() return xdist end
  383. function gety() return ydist end
  384. function getz() return zdist end
  385. function getr() return rdist end
  386.  
  387. function setx(x) xdist = x end
  388. function sety(y) ydist = y end
  389. function setz(z) zdist = z end
  390. function setr(r) rdist = r end
  391.  
  392. function getxmin() return xmin end
  393. function getxmax() return xmax end
  394. function getymin() return ymin end
  395. function getymax() return ymax end
  396. function getzmin() return zmin end
  397. function getzmax() return zmax end
  398.  
  399. function setxmin(x) xmin = x end
  400. function setxmax(x) xmax = x end
  401. function setymin(y) ymin = y end
  402. function setymax(y) ymax = y end
  403. function setzmin(z) zmin = z end
  404. function setzmax(z) zmax = z end
  405.  
  406. function getxlast() return xlast end
  407. function getylast() return ylast end
  408. function getzlast() return zlast end
  409. function getrlast() return rlast end
  410.  
  411. function setxlast(x) xlast = x end
  412. function setylast(y) ylast = y end
  413. function setzlast(z) zlast = z end
  414. function setrlast(r) rlast = r end
  415.  
  416. function getlast() return lastmove end
  417. function setlast(lm) lastmove = lm end
  418.  
  419. function getdug() return dugtotal end
  420. function setdug(d) dugtotal = d end
  421.  
  422.  
  423.  
  424.  
  425. -------------------------------------------
  426. --||   /¯\  /\ |¯\   ///¯¯\ /\ ||  |||¯¯]--
  427. --||_ | O ||  ||  | // \_¯\|  | \\// | ] --
  428. --|__] \_/ |||||_/ //  \__/||||  \/  |__]--
  429. -------------------------------------------
  430.  
  431. function location()
  432.  return
  433.   { xdist, ydist, zdist, rdist,
  434.     xmin, xmax, ymin, ymax, zmin, zmax,
  435.     xlast, ylast, zlast, rlast,
  436.     lastmove, dugtotal }
  437. end
  438.  
  439.  
  440. function saveCoords(loc,save)
  441.  loc = loc or location()
  442.  save = save or savefile
  443.  local file,x
  444.  
  445.  --if fs.exists(save.."_old") then
  446.  -- fs.delete(save.."_old")
  447.  --end
  448.  --if fs.exists(save) then
  449.   --fs.move(save, save.."_old")
  450.  --end
  451.  
  452.  while file == nil do
  453.   file = fs.open(save,"w")
  454.  end --while
  455.  
  456.  for x=1,#loc do
  457.   file.writeLine(tostring(loc[x]))
  458.   if x == 4 then file.flush() end
  459.  end --for
  460.  
  461.  file.close()
  462. end --function
  463.  
  464.  
  465.  
  466. function loadCoords(save)
  467.  save = save or savefile
  468.  local file
  469.  if fs.exists(save) then
  470.   while file == nil do
  471.    file = fs.open(save,"r")
  472.   end --while
  473.   xdist = tonumber(file.readLine() or xdist)
  474.   ydist = tonumber(file.readLine() or ydist)
  475.   zdist = tonumber(file.readLine() or zdist)
  476.   rdist = tonumber(file.readLine() or rdist)
  477.   xmin = tonumber(file.readLine() or xmin)
  478.   xmax = tonumber(file.readLine() or xmax)
  479.   ymin = tonumber(file.readLine() or ymin)
  480.   ymax = tonumber(file.readLine() or ymax)
  481.   zmin = tonumber(file.readLine() or zmin)
  482.   zmax = tonumber(file.readLine() or zmax)
  483.   xlast = tonumber(file.readLine() or xlast)
  484.   ylast = tonumber(file.readLine() or ylast)
  485.   zlast = tonumber(file.readLine() or zlast)
  486.   rlast = tonumber(file.readLine() or rlast)
  487.   lastmove = file.readLine() or lastmove
  488.   dugtotal = tonumber(file.readLine() or dugtotal)
  489.   file.close()
  490.   return true
  491.  
  492.  else
  493.   --if fs.exists(save.."_old") then
  494.   -- loadCoords(save.."_old")
  495.   -- return true
  496.   --end
  497.  
  498.   return false
  499.  end --if/else
  500.  
  501. end -- function
  502.  
  503.  
  504.  
  505. function makeStartup(command, args)
  506.  command = tostring(command)
  507.  args = args or {}
  508.  local x
  509.  for x=1,#args do
  510.   command = command.." "..args[x]
  511.  end --for
  512.  
  513.  local file = fs.open(startfile,"w")
  514.  file.writeLine("print(\"> "..command.."\")")
  515.  file.writeLine("for x=5,1,-1 do")
  516.  file.writeLine(" term.write(tostring(x)..\" \")")
  517.  file.writeLine(" sleep(1)")
  518.  file.writeLine("end --for")
  519.  file.writeLine("print(\" \")")
  520.  file.writeLine("shell.run(\""..command.."\")")
  521.  file.close()
  522. end --function
  523.  
  524.  
  525.  
  526.  
  527. -------------------------------------
  528. --[¯¯]|¯\ /\  /¯]||//[¯¯]|\ || /¯¯]--
  529. -- || | /|  || [ | <  ][ | \ || [¯|--
  530. -- || | \|||| \_]||\\[__]|| \| \__|--
  531. -------------------------------------
  532.  
  533. function update(n)
  534.  
  535.  if n=="fwd" then
  536.  
  537.   if rdist%360 == 0 then -- 12:00
  538.    zdist = zdist + 1
  539.    zlast = 1
  540.    lastmove = "z+"
  541.  
  542.   elseif rdist%360 == 90 then -- 3:00
  543.    xdist = xdist + 1
  544.    xlast = 1
  545.    lastmove = "x+"
  546.  
  547.   elseif rdist%360 == 180 then -- 6:00
  548.    zdist = zdist - 1
  549.    zlast = -1
  550.    lastmove = "z-"
  551.  
  552.   elseif rdist%360 == 270 then -- 9:00
  553.    xdist = xdist - 1
  554.    xlast = -1
  555.    lastmove = "x-"
  556.  
  557.   end --if/else
  558.  
  559.  
  560.  elseif n=="back" then
  561.  
  562.   if rdist%360 == 0 then -- 12:00
  563.    zdist = zdist - 1
  564.    zlast = -1
  565.    lastmove = "z-"
  566.  
  567.   elseif rdist%360 == 90 then -- 3:00
  568.    xdist = xdist - 1
  569.    xlast = -1
  570.    lastmove = "x-"
  571.  
  572.   elseif rdist%360 == 180 then -- 6:00
  573.    zdist = zdist + 1
  574.    zlast = 1
  575.    lastmove = "z+"
  576.  
  577.   elseif rdist%360 == 270 then -- 9:00
  578.    xdist = xdist + 1
  579.    xlast = 1
  580.    lastmove = "x+"
  581.  
  582.   end --if/else
  583.  
  584.  
  585.  elseif n=="up" then
  586.   ydist = ydist + 1
  587.   ylast = 1
  588.   lastmove = "y+"
  589.  
  590.  elseif n=="down" then
  591.   ydist = ydist - 1
  592.   ylast = -1
  593.   lastmove = "y-"
  594.  
  595.  elseif n=="right" then
  596.   rdist = rdist + 90
  597.   rlast = 1
  598.   lastmove = "r+"
  599.   while rdist > 999 do
  600.    rdist = rdist - 360
  601.   end --while
  602.  
  603.  elseif n=="left" then
  604.   rdist = rdist - 90
  605.   rlast = -1
  606.   lastmove = "r-"
  607.   while rdist < -999 do
  608.    rdist = rdist + 360
  609.   end --while
  610.  
  611.  end --if/else
  612.  
  613.  
  614.  if xdist < xmin then
  615.   xmin = xdist
  616.  elseif xdist > xmax then
  617.   xmax = xdist
  618.  end
  619.  
  620.  if ydist < ymin then
  621.   ymin = ydist
  622.  elseif ydist > ymax then
  623.   ymax = ydist
  624.  end
  625.  
  626.  if zdist < zmin then
  627.   zmin = zdist
  628.  elseif zdist > zmax then
  629.   zmax = zdist
  630.  end
  631.  
  632.  saveCoords()
  633.  
  634. end
  635.  
  636.  
  637.  
  638.  
  639. ------------------------------------------
  640. -- |\/| /¯\ ||  |||¯¯]|\/||¯¯]|\ ||[¯¯] --
  641. -- |  || O | \\// | ] |  || ] | \ | ||  --
  642. -- |||| \_/   \/  |__]|||||__]|| \| ||  --
  643. ------------------------------------------
  644.  
  645. stuck = false
  646. function isStuck()
  647.  return stuck
  648. end
  649. stuckDir = "none"
  650. function getStuckDir()
  651.  return stuckDir
  652. end
  653.  
  654.  
  655. function left(n)
  656.  n = (n or 1)
  657.  if n < 0 then
  658.   return right(-n)
  659.  end --if
  660.  local x
  661.  for x=1,n do
  662.   turtle.turnLeft()
  663.   update("left")
  664.  end --if
  665.  return true
  666. end --function
  667.  
  668.  
  669. function right(n)
  670.  n = (n or 1)
  671.  if n < 0 then
  672.   return left(-n)
  673.  end --if
  674.  local x
  675.  for x=1,n do
  676.   turtle.turnRight()
  677.   update("right")
  678.  end --if
  679.  return true
  680. end --function
  681.  
  682.  
  683.  
  684. local unbreak = "Unbreakable block detected"
  685. local protect = "Cannot break protected block"
  686.  
  687.  
  688.  
  689. function up(n)
  690.  n = n or 1
  691.  if n < 0 then
  692.   return down(-n)
  693.  end --if
  694.  
  695.  local x,a,b,t
  696.  
  697.  for x=1, n do
  698.   refuel()
  699.  
  700.   if not turtle.up() then
  701.  
  702.    if doblacklist then
  703.     -- Blocks not to mine
  704.     if flex.isBlockUp(options["blacklist"]) then
  705.      if not back() then return false end
  706.      if not up() then return false end
  707.      return true
  708.     end --if
  709.    end --if
  710.  
  711.    t = os.time()
  712.    stuck = false
  713.    while not turtle.up() do
  714.     a,b = turtle.digUp()
  715.     while a do
  716.      dugtotal = dugtotal + 1
  717.      a,b = turtle.digUp()
  718.     end --while
  719.  
  720.     if b == protect then
  721.      if doblacklist then
  722.       options["blacklist"][
  723.          #options["blacklist"]+1]
  724.          = flex.getBlockUp()
  725.       return up(n-x+1)
  726.      else
  727.       b = unbreak
  728.      end --if
  729.     end --if
  730.  
  731.     if b == unbreak then
  732.      flex.send("#1"..b.."#0: {#8"..
  733.        tostring(getx()).."#0,#8"..
  734.        tostring(gety()+1).."#0,#8"..
  735.        tostring(getz()).."#0}")
  736.      knownBedrock[#knownBedrock+1] = {
  737.        getx(), gety()+1, getz() }
  738.      stuck = true
  739.      stuckDir = "up"
  740.      return false
  741.     end --if
  742.     if a then
  743.      dugtotal = dugtotal + 1
  744.     end --if
  745.     if attack then turtle.attackUp() end
  746.  
  747.     if (os.time()-t)/24*20*60 > 20 then
  748.      -- Stuck for at least 20 seconds
  749.      -- Usually caused by height limits
  750.      flex.send("Unable to move up",colors.orange)
  751.      stuck = true
  752.      stuckDir = "up"
  753.      return false
  754.     end --if
  755.  
  756.    end --while
  757.  
  758.   end --if can't move
  759.  
  760.   update("up")
  761.  end --for
  762.  
  763.  return true
  764. end --function
  765.  
  766.  
  767.  
  768. function down(n)
  769.  n = n or 1
  770.  if n < 0 then
  771.   return up(-n)
  772.  end --if
  773.  
  774.  local x,a,b,t
  775.  
  776.  for x=1, n do
  777.   refuel()
  778.  
  779.   if not turtle.down() then
  780.  
  781.    if doblacklist then
  782.     -- Blocks not to mine
  783.     if flex.isBlockDown(options["blacklist"]) then
  784.      if not fwd() then return false end
  785.      if not down() then return false end
  786.      return true
  787.     end --if
  788.    end --if
  789.  
  790.    t = os.time()
  791.    stuck = false
  792.    while not turtle.down() do
  793.     a,b = turtle.digDown()
  794.     while a do
  795.      dugtotal = dugtotal + 1
  796.      a,b = turtle.digDown()
  797.     end --while
  798.  
  799.     if b == protect then
  800.      if doblacklist then
  801.       options["blacklist"][
  802.         #options["blacklist"]+1 ]
  803.         = flex.getBlockDown()
  804.       return down(n-x+1)
  805.      else
  806.       b = unbreak
  807.      end --if
  808.     end --if
  809.  
  810.     if b == unbreak then
  811.      flex.send("#1"..b.."#0: {#8"..
  812.        tostring(getx()).."#0,#8"..
  813.        tostring(gety()-1).."#0,#8"..
  814.        tostring(getz()).."#0}")
  815.      knownBedrock[#knownBedrock+1] = {
  816.        getx(), gety()-1, getz() }
  817.      stuck = true
  818.      stuckDir = "down"
  819.      return false
  820.     end --if
  821.     if a then
  822.      dugtotal = dugtotal + 1
  823.     end --if
  824.     if attack then turtle.attackDown() end
  825.  
  826.     if (os.time()-t)/24*20*60 > 20 then
  827.      -- Stuck for at least 20 seconds
  828.      -- Usually caused by height limits
  829.      flex.send("Unable to move down",colors.orange)
  830.      stuck = true
  831.      stuckDir = "down"
  832.      return false
  833.     end --if
  834.  
  835.    end --while
  836.  
  837.   end --if can't move
  838.  
  839.   update("down")
  840.  end --for
  841.  
  842.  return true
  843. end --function
  844.  
  845.  
  846.  
  847. function fwd(n)
  848.  n = n or 1
  849.  if n < 0 then
  850.   return back(-n)
  851.  end --if
  852.  
  853.  local x,z,a,b,t
  854.  for x=1, n do
  855.   refuel()
  856.  
  857.   if not turtle.forward() then
  858.  
  859.    if doblacklist then
  860.     -- Blocks not to mine
  861.     if flex.isBlock(options["blacklist"]) then
  862.      if flex.isBlockUp(options["blacklist"]) then
  863.       if flex.isBlockDown(options["blacklist"]) then
  864.  
  865.        -- Blocks above, below and in front
  866.        if not back() then return false end
  867.        if not up(2) then return false end
  868.        if not fwd(2) then return false end
  869.        if not down(2) then return false end
  870.        return true
  871.  
  872.       else -- Blocks in front and above
  873.        if not down() then return false end
  874.        x = 0
  875.        while flex.isBlock(options["blacklist"]) do
  876.         x = x + 1
  877.         if not down() then return false end
  878.        end --while
  879.        if not fwd(2) then return false end
  880.        if not up(x) then return false end
  881.        return
  882.       end --if/else
  883.  
  884.      else
  885.       -- Block in front
  886.       if not up() then return false end
  887.       if not fwd(2) then return false end
  888.       if not down() then return false end
  889.       return true
  890.      end --if/else
  891.     end --if
  892.    end --if doblacklist
  893.  
  894.    t = os.time()
  895.    stuck = false
  896.    while not turtle.forward() do
  897.     a,b = turtle.dig()
  898.     while a do
  899.      dugtotal = dugtotal + 1
  900.      a,b = turtle.dig()
  901.     end --while
  902.  
  903.     if b == protect then
  904.      if doblacklist then
  905.       options["blacklist"][
  906.           #options["blacklist"]+1]
  907.           = flex.getBlock()
  908.       return fwd(n-x+1)
  909.      else
  910.       b = unbreak
  911.      end --if
  912.     end --if
  913.  
  914.     if b == unbreak then
  915.      z = { getx(), gety(),
  916.            getz(), getr()%360 }
  917.  
  918.      if z[4] == 0 then
  919.       z[3] = z[3] + 1
  920.      elseif z[4] == 90 then
  921.       z[1] = z[1] + 1
  922.      elseif z[4] == 180 then
  923.       z[3] = z[3] - 1
  924.      elseif z[4] == 270 then
  925.       z[1] = z[1] - 1
  926.      end --if/else
  927.  
  928.      flex.send("#1"..b.."#0: {#8"..
  929.        tostring(z[1]).."#0,#8"..
  930.        tostring(z[2]).."#0,#8"..
  931.        tostring(z[3]).."#0}")
  932.      knownBedrock[#knownBedrock+1] =
  933.        { z[1], z[2], z[3] }
  934.      stuck = true
  935.      stuckDir = "fwd"
  936.      return false
  937.     end --if
  938.  
  939.     if a then
  940.      dugtotal = dugtotal + 1
  941.     end --if
  942.     if attack then turtle.attack() end
  943.  
  944.     --if (os.time()-t)/24*20*60 > 120 then
  945.      -- Stuck for at least 20 seconds
  946.      --flex.send("Unable to move forward",colors.orange)
  947.      --stuck = true
  948.      --stuckDir = "fwd"
  949.      --return false
  950.      --t = os.time()
  951.     --end --if
  952.  
  953.    end --while not turtle.forward()
  954.  
  955.   end --if can't move
  956.  
  957.   update("fwd")
  958.  end --if
  959.  
  960.  return true
  961. end --function
  962.  
  963.  
  964.  
  965. function back(n)
  966.  n = n or 1
  967.  if n < 0 then
  968.   return fwd(-n)
  969.  end --if
  970.  
  971.  local x,turn
  972.  turn = false
  973.  for x=1,n do
  974.   if turn then
  975.    if not fwd() then return false end
  976.   elseif turtle.back() then
  977.    update("back")
  978.   else
  979.    turn = true
  980.    gotor(rdist+180)
  981.    if not fwd() then return false end
  982.   end --if/else
  983.  end --for
  984.  
  985.  if turn then gotor(rdist-180) end
  986.  return true
  987. end --function
  988.  
  989.  
  990. function dig(x)
  991.  local a,b
  992.  x = x or "fwd"
  993.  
  994.  if x=="fwd" then
  995.   while turtle.dig() and not stuck do
  996.    dugtotal = dugtotal + 1
  997.   end --while
  998.  
  999.  elseif x=="up" then
  1000.   while turtle.digUp() and not stuck do
  1001.    dugtotal = dugtotal + 1
  1002.   end --while
  1003.  
  1004.  elseif x=="down" then
  1005.   while turtle.digDown() and not stuck do
  1006.    dugtotal = dugtotal + 1
  1007.   end --while
  1008.  
  1009.  end --if/else
  1010. end --function
  1011.  
  1012. function digUp() dig("up") end
  1013. function digDown() dig("down") end
  1014.  
  1015.  
  1016.  
  1017. -- Agressive placement functions
  1018.  
  1019. function place()
  1020.  local x = flex.getBlock("fwd")
  1021.  local t = os.time()
  1022.  while not turtle.place()
  1023.        and flex.isBlock(options["fluids"]) do
  1024.   if attack then
  1025.    turtle.attackUp()
  1026.    turtle.attack()
  1027.    turtle.attackDown()
  1028.   end --if
  1029.  
  1030.   if (os.time()-t)/24*20*60 > 20 then
  1031.    local z = { getx(), gety(),
  1032.          getz(), getr()%360 }
  1033.    if z[4] == 0 then
  1034.     z[3] = z[3] + 1
  1035.    elseif z[4] == 90 then
  1036.     z[1] = z[1] + 1
  1037.    elseif z[4] == 180 then
  1038.     z[3] = z[3] - 1
  1039.    elseif z[4] == 270 then
  1040.     z[1] = z[1] - 1
  1041.    end --if/else
  1042.  
  1043.    flex.send("#1End of world detected#0: {#8"..
  1044.      tostring(z[1]).."#0,#8"..
  1045.      tostring(z[2]).."#0,#8"..
  1046.      tostring(z[3]).."#0}")
  1047.    knownBedrock[#knownBedrock+1] =
  1048.      { z[1], z[2], z[3] }
  1049.  
  1050.    stuck = true
  1051.    stuckDir = "fwd"
  1052.    return false
  1053.   end --if
  1054.  
  1055.   sleep(0.1)
  1056.   x = flex.getBlock()
  1057.  end --while
  1058.  if turtle.getSelectedSlot() == blockSlot then
  1059.   checkBlocks()
  1060.  end --if
  1061.  return true
  1062. end --function
  1063.  
  1064.  
  1065. function placeUp()
  1066.  local x = flex.getBlockUp()
  1067.  local t = os.time()
  1068.  while not turtle.placeUp()
  1069.        and flex.isBlockUp(options["fluids"]) do
  1070.   if attack then
  1071.    turtle.attack()
  1072.    turtle.attackUp()
  1073.   end --if
  1074.  
  1075.   if (os.time()-t)/24*20*60 > 20 then
  1076.    flex.send("#1Edge of world detected#0: {#8"..
  1077.      tostring(getx()).."#0,#8"..
  1078.      tostring(gety()+1).."#0,#8"..
  1079.      tostring(getz()).."#0}")
  1080.    knownBedrock[#knownBedrock+1] = {
  1081.      getx(), gety()+1, getz() }
  1082.  
  1083.    stuck = true
  1084.    stuckDir = "up"
  1085.    return false
  1086.   end --if
  1087.  
  1088.   sleep(0.1)
  1089.   x = flex.getBlockUp()
  1090.  end --while
  1091.  if turtle.getSelectedSlot == blockSlot then
  1092.   checkBlocks()
  1093.  end --if
  1094.  return true
  1095. end --function
  1096.  
  1097.  
  1098. function placeDown()
  1099.  local x = flex.getBlockDown()
  1100.  local t = os.time()
  1101.  while not turtle.placeDown()
  1102.        and flex.isBlockDown(options["fluids"]) do
  1103.   if attack then
  1104.    turtle.attack()
  1105.    turtle.attackDown()
  1106.   end --if
  1107.  
  1108.   if (os.time()-t)/24*20*60 > 20 then
  1109.    flex.send("#1Edge of world detected#0: {#8"..
  1110.      tostring(getx()).."#0,#8"..
  1111.      tostring(gety()-1).."#0,#8"..
  1112.      tostring(getz()).."#0}")
  1113.    knownBedrock[#knownBedrock+1] = {
  1114.      getx(), gety()-1, getz() }
  1115.    up()
  1116.    placeDown()
  1117.    ymin = gety()
  1118.  
  1119.    stuck = true
  1120.    stuckDir = "down"
  1121.    return false
  1122.   end --if
  1123.  
  1124.   sleep(0.1)
  1125.   x = flex.getBlockDown()
  1126.  end --while
  1127.  if turtle.getSelectedSlot() == blockSlot then
  1128.   checkBlocks()
  1129.  end --if
  1130.  return true
  1131. end --function
  1132.  
  1133.  
  1134.  
  1135.  
  1136. ----------------------------
  1137. --  /¯¯]  /¯\  [¯¯]  /¯\  --
  1138. -- | [¯| | O |  ||  | O | --
  1139. --  \__|  \_/   ||   \_/  --
  1140. ----------------------------
  1141.  
  1142.  
  1143. function gotor(r)
  1144.  if r == nil then
  1145.   error("Number expected, got nil", 2)
  1146.   return
  1147.  end --if
  1148.  local x = (r-rdist)%360
  1149.  -- X is the target direction relative to current rotation
  1150.  
  1151.  if x == 90 then
  1152.   right()
  1153.  elseif x == 180 then
  1154.   left(rlast*2)
  1155.   -- Rotate opposite to last rotation
  1156.  elseif x == 270 then
  1157.   left()
  1158.  elseif x ~= 0 then
  1159.   error("Invalid rotation parameter", 2)
  1160.   return false
  1161.  end --if/else
  1162.  
  1163.  return true
  1164. end --function
  1165.  
  1166.  
  1167.  
  1168. function gotoy(y)
  1169.  if y == nil then
  1170.   error("Number expected, got nil", 2)
  1171.   return
  1172.  end --if
  1173.  
  1174.  while ydist < y do
  1175.   if not up() then return false end
  1176.  end
  1177.  while ydist > y do
  1178.   if not down() then return false end
  1179.  end
  1180.  return true
  1181. end
  1182.  
  1183.  
  1184. function gotox(x)
  1185.  if x == nil then
  1186.   error("Number expected, got nil", 2)
  1187.   return
  1188.  end --if
  1189.  
  1190.  if xdist < x then
  1191.   if rdist == 270 then
  1192.    return back(x-xdist)
  1193.  
  1194.   else
  1195.    gotor(90)
  1196.    return fwd(x-xdist)
  1197.   end
  1198.  end
  1199.  
  1200.  if xdist > x then
  1201.   if rdist == 90 then
  1202.    return back(xdist-x)
  1203.  
  1204.   else
  1205.    gotor(270)
  1206.    return fwd(xdist-x)
  1207.   end
  1208.  end
  1209.  
  1210.  return true
  1211. end
  1212.  
  1213.  
  1214. function gotoz(z)
  1215.  if z == nil then
  1216.   error("Number expected, got nil", 2)
  1217.   return
  1218.  end --if
  1219.  
  1220.  if zdist < z then
  1221.   if rdist == 180 then
  1222.    return back(z-zdist)
  1223.  
  1224.   else
  1225.    gotor(0)
  1226.    return fwd(z-zdist)
  1227.   end
  1228.  end
  1229.  
  1230.  if zdist > z then
  1231.   if rdist == 0 then
  1232.    return back(zdist-z)
  1233.  
  1234.   else
  1235.    gotor(180)
  1236.    return fwd(zdist-z)
  1237.   end
  1238.  end
  1239.  
  1240.  return true
  1241. end
  1242.  
  1243.  
  1244. function goto(x,y,z,r,lm)
  1245.  if type(x) == "table" then
  1246.   if #x < 4 then
  1247.    error("Invalid Goto Table",2)
  1248.   end
  1249.   y = x[2]
  1250.   z = x[3]
  1251.   r = x[4]
  1252.   lm = x[15]
  1253.   x = x[1]
  1254.  end
  1255.  gotox(x or 0)
  1256.  gotoz(z or 0)
  1257.  gotor(r or 0)
  1258.  gotoy(y or 0)
  1259.  if lm~=nil then setlast(lm) end
  1260. end
  1261.  
  1262.  
  1263.  
  1264.  
  1265. ---------------------------------------
  1266. -- |¯¯]|| |||¯¯]||    |¯\|¯\ /¯\     --
  1267. -- | ] ||_||| ] ||_   | /| /| O | == --
  1268. -- ||   \__||__]|__]  || | \ \_/     --
  1269. ---------------------------------------
  1270. --   /¯]|¯¯]/¯¯\/¯¯\[¯¯]|\ || /¯¯]   --
  1271. --  | [ | ] \_¯\\_¯\ ][ | \ || [¯|   --
  1272. --   \_]|__]\__/\__/[__]|| \| \__|   --
  1273. ---------------------------------------
  1274.  
  1275. fuelSlot = {1,16}
  1276.  
  1277. function getFuelSlot()
  1278.  return fuelSlot[1],fuelSlot[2]
  1279. end --function
  1280.  
  1281. function setFuelSlot(a,b)
  1282.  if a == nil then
  1283.   return false
  1284.  end --if
  1285.  b = b or a
  1286.  if a < b then
  1287.   fuelSlot = {a,b}
  1288.  else
  1289.   fuelSlot = {b,a}
  1290.  end --if/else
  1291.  return true
  1292. end --function
  1293.  
  1294.  
  1295.  
  1296. function refuel(b)
  1297.  b = b or 1
  1298.  b = math.min(b, turtle.getFuelLimit())
  1299.  local a,x,z,slot
  1300.  slot = turtle.getSelectedSlot()
  1301.  a = true
  1302.  
  1303.  while turtle.getFuelLevel() < b do
  1304.   for x=fuelSlot[1],fuelSlot[2] do
  1305.    turtle.select(x)
  1306.    if turtle.refuel(1) then break end
  1307.    if x == fuelSlot[2] and a then
  1308.     if fuelSlot[1] == fuelSlot[2] then
  1309.         flex.send("Waiting for fuel in "..
  1310.           "slot "..tostring(fuelSlot[1])..
  1311.           "...", colors.pink)
  1312.     else
  1313.      flex.send("Waiting for fuel in "..
  1314.        "slots "..tostring(fuelSlot[1])..
  1315.        "-"..tostring(fuelSlot[2])..
  1316.        "...", colors.pink)
  1317.     end --if/else
  1318.     a = false
  1319.    end --if
  1320.   end --for
  1321.  end --while
  1322.  
  1323.  if not a then
  1324.   flex.send("Thanks!",colors.lime)
  1325.  end --if
  1326.  turtle.select(slot)
  1327.  
  1328. end --function
  1329.  
  1330.  
  1331. local fuelvalue = {}
  1332. local fuelfile = "dig_fuel.cfg"
  1333. local file, line
  1334. if fs.exists(fuelfile) then
  1335.  file = fs.open(fuelfile, "r")
  1336.  line = file.readLine()
  1337.  while line ~= nil do
  1338.   fuelvalue[line] = tonumber(file.readLine())
  1339.   line = file.readLine()
  1340.  end --while
  1341.  file.close()
  1342. else
  1343.  file = fs.open(fuelfile, "w")
  1344.  file.close()
  1345. end --if/else
  1346.  
  1347.  
  1348. function checkFuelValue(x)
  1349.  if type(x) ~= "number" then
  1350.   local x = turtle.getSelectedSlot()
  1351.  end --if
  1352.  if turtle.getItemCount(x) == 0 then
  1353.   return 0
  1354.  end --if
  1355.  
  1356.  local a,b,c,d
  1357.  a = turtle.getItemDetail(x)["name"]
  1358.  b = fuelvalue[a]
  1359.  
  1360.  if b == nil then
  1361.   c = turtle.getFuelLevel()
  1362.   turtle.select(x)
  1363.   turtle.refuel(1)
  1364.   d = turtle.getFuelLevel() - c
  1365.   fuelvalue[a] = d
  1366.  
  1367.   file = fs.open(fuelfile, "a")
  1368.   file.writeLine(a)
  1369.   file.writeLine(tostring(d))
  1370.   file.close()
  1371.  
  1372.   if turtle.getItemCount(x) == 0 then
  1373.    return 0
  1374.   else
  1375.    return d
  1376.   end --if
  1377.  end --if
  1378.  
  1379.  return b
  1380. end --function
  1381.  
  1382.  
  1383.  
  1384. blockSlot = 0
  1385. blockStacks = 1
  1386. function getBlockSlot() return blockSlot end
  1387. function setBlockSlot(n) blockSlot = n end
  1388. function getBlockStacks() return blockStacks end
  1389. function setBlockStacks(n) blockStacks = n end
  1390.  
  1391.  
  1392. function checkBlocks()
  1393.  if blockSlot == 0 then return end
  1394.  local x,docondense
  1395.  
  1396.  if not flex.isItem(options["buildingblocks"],blockSlot) then
  1397.   if turtle.getItemCount(blockSlot) > 0 then
  1398.    for x=blockSlot+1,16 do
  1399.     if turtle.getItemCount(x) == 0 then
  1400.      turtle.select(blockSlot)
  1401.      turtle.transferTo(x)
  1402.      break
  1403.     end --if
  1404.    end --for
  1405.   end --if
  1406.  end --if
  1407.  
  1408.  if turtle.getItemCount(blockSlot) == 0 then
  1409.   for x=1,16 do
  1410.    if flex.isItem(options["buildingblocks"],x) then
  1411.     turtle.select(x)
  1412.     turtle.transferTo(blockSlot)
  1413.     turtle.select(blockSlot)
  1414.     break
  1415.    end --if
  1416.   end --for
  1417.  flex.condense(blockSlot)
  1418.  end --if
  1419.  
  1420.  turtle.select(blockSlot)
  1421. end --function
  1422.  
  1423.  
  1424. function blockLava(dir)
  1425.  if not flex.isBlock("lava",dir) then
  1426.   return
  1427.  end --if
  1428.  local slot = turtle.getSelectedSlot()
  1429.  checkBlocks()
  1430.  turtle.select(blockSlot)
  1431.  if dir == "up" then
  1432.   turtle.placeUp()
  1433.  elseif dir == "down" then
  1434.   turtle.placeDown()
  1435.  else
  1436.   turtle.place()
  1437.  end --if/else
  1438.  turtle.select(slot)
  1439. end --function
  1440.  
  1441. function blockLavaUp() blockLava("up") end
  1442. function blockLavaDown() blockLava("down") end
  1443.  
  1444.  
  1445.  
  1446.  
  1447. -- Organize Inventory/Fuel
  1448. --   C
  1449. --  CTQQQ
  1450. --   QQQQ
  1451. -- (Chest/Turtle/Quarry)
  1452.  
  1453. function dropNotFuel()
  1454.  
  1455.  local slot = turtle.getSelectedSlot()
  1456.  local a,b,c,f,x,y,z
  1457.  local crafty,usedbucketalready,blocksPresent
  1458.  turtle.select(1)
  1459.  if turtle.getItemCount(1) > 1 then
  1460.   flex.condense()
  1461.  end --if
  1462.  
  1463.  -- Check there's inventory to place loot
  1464.  a = true
  1465.  while true do
  1466.   if isChest() then
  1467.    break
  1468.   end
  1469.   if a then
  1470.    flex.send("Output inventory not found!",
  1471.      colors.red)
  1472.    a = false
  1473.   end --if
  1474.   sleep(1)
  1475.  end --while
  1476.  if not a then
  1477.   flex.send("Thanks!",colors.lightBlue)
  1478.  end --if
  1479.  
  1480.  
  1481.  -- Drop off what's not fuel
  1482.  blocksPresent = blockStacks
  1483.  for x=1,16 do
  1484.   turtle.select(x)
  1485.   if not turtle.refuel(0) then
  1486.    if flex.isItem(options["buildingblocks"]) then
  1487.     if blocksPresent <= 0 then
  1488.      turtle.drop()
  1489.     else
  1490.      blocksPresent = blocksPresent - 1
  1491.     end --if/else
  1492.    else
  1493.     turtle.drop()
  1494.    end --if/else
  1495.   end --if
  1496.  end --for
  1497.  checkBlocks()
  1498.  turtle.select(1)
  1499.  -- Retrieve surplus fuel from fuel chest
  1500.  while turtle.suckUp() do sleep(0) end
  1501.  if turtle.getItemCount(fuelSlot[1]) > 0 then
  1502.   flex.condense()
  1503.  end --if
  1504.  
  1505.  
  1506.  -- Craft coal into blocks
  1507.  if peripheral.find("workbench") and isChestUp() then
  1508.  
  1509.   z = 0
  1510.   for x=1,16 do
  1511.    if turtle.getItemCount(x) > 0 and
  1512.       turtle.getItemDetail(x)["name"]
  1513.       ~= "minecraft:coal" then
  1514.     turtle.select(x)
  1515.     turtle.dropUp()
  1516.    end --if
  1517.    z = z + turtle.getItemCount(x)
  1518.   end --for
  1519.  
  1520.   if z >= 9 then
  1521.  
  1522.    y = 13
  1523.    for x=1,12 do
  1524.     turtle.select(x)
  1525.     while turtle.getItemCount(x) > 0 do
  1526.      if y <= 16 then
  1527.       turtle.transferTo(y)
  1528.      else
  1529.       z = z - turtle.getItemCount(x)
  1530.       turtle.dropUp()
  1531.      end --if/else
  1532.      if turtle.getItemCount(x) > 0 then
  1533.       y = y + 1
  1534.      end --if
  1535.     end --while
  1536.    end --for
  1537.  
  1538.    y = math.floor(z/9)
  1539.    turtle.select(13)
  1540.    turtle.dropUp(z%9)
  1541.    a = 13
  1542.    b = { 1, 2, 3, 5, 6, 7, 9, 10, 11 }
  1543.    for x=1,#b do
  1544.     if turtle.getItemCount(a) < y then
  1545.      turtle.select(a+1)
  1546.      turtle.transferTo(a,
  1547.        y - turtle.getItemCount(a))
  1548.     end --if
  1549.     turtle.select(a)
  1550.     turtle.transferTo(b[x], y)
  1551.     if turtle.getItemCount(a) == 0 then
  1552.      a = a + 1
  1553.     end --if
  1554.    end --for
  1555.  
  1556.    if peripheral.getType("left") == "workbench" then
  1557.     crafty = peripheral.wrap("left")
  1558.    else
  1559.     crafty = peripheral.wrap("right")
  1560.    end --if/else
  1561.    turtle.select(1)
  1562.    crafty.craft()
  1563.  
  1564.   end --if
  1565.   while turtle.suckUp() do sleep(0) end
  1566.   checkBlocks()
  1567.  
  1568.  end --if
  1569.  
  1570.  
  1571.  --Tally up fuel sources
  1572.  z = {} -- slot #
  1573.  f = {} -- amount of fuel
  1574.  usedbucketalready = false
  1575.  
  1576.  for x=1,16 do
  1577.   turtle.select(x)
  1578.  
  1579.   if turtle.refuel(0) then
  1580.    y = turtle.getItemCount()
  1581.  
  1582.    if flex.isItem("bucket") and y==1 then
  1583.     if not usedbucketalready then
  1584.      -- Only use buckets one at a time
  1585.      turtle.refuel()
  1586.      usedbucketalready = true
  1587.      turtle.drop()
  1588.     end
  1589.  
  1590.    else -- Not a bucket
  1591.     a = checkFuelValue(x)
  1592.     if a > 0 then
  1593.      z[#z+1] = x
  1594.      f[#z] = a*turtle.getItemCount(x)
  1595.     end --if
  1596.  
  1597.    end --if/else (is bucket)
  1598.  
  1599.   end --if (is fuel)
  1600.  end --for (slots)
  1601.  
  1602.  
  1603.  -- Choose best fuel available
  1604.  a = 0
  1605.  for x=1, #f do
  1606.   a = math.max(a,f[x])
  1607.  end --for
  1608.  for x=1, #f do
  1609.   if f[x] == a then
  1610.    if z[x] > 1 then
  1611.     for b=2,16 do
  1612.      if turtle.getItemCount(b) == 0 then
  1613.       turtle.select(1)
  1614.       turtle.transferTo(b)
  1615.       break
  1616.      end --if
  1617.     end --for
  1618.     turtle.select(z[x])
  1619.     turtle.transferTo(1)
  1620.    end --if
  1621.    break
  1622.   end --if
  1623.  end --for
  1624.  
  1625.  
  1626.  -- Deposit surplus fuel
  1627.  for x=fuelSlot[1]+1,16 do
  1628.   if turtle.getItemCount(x) > 0 then
  1629.  
  1630.    turtle.select(x)
  1631.    if turtle.refuel(0) then
  1632.     if isChestUp() then
  1633.      -- Place in fuel chest
  1634.      turtle.dropUp()
  1635.     end --if
  1636.     if turtle.getItemCount() > 0 then
  1637.      -- Fuel chest is full or absent
  1638.      turtle.drop()
  1639.     end --if
  1640.    end --if
  1641.  
  1642.   end --if (more than zero items)
  1643.  end --for (all slots after fuel slot)
  1644.  
  1645.  turtle.select(fuelSlot[1])
  1646.  if not turtle.refuel(0) then
  1647.   for x=fuelSlot[1]+1,16 do
  1648.    turtle.transferTo(x)
  1649.    if turtle.getItemCount() == 0 then
  1650.     break
  1651.    end --if
  1652.   end --for
  1653.  end --if
  1654.  
  1655.  flex.condense(fuelSlot[1]+1)
  1656.  
  1657.  if turtle.getItemCount(16) > 0 then
  1658.   flex.send("Inventory full!",colors.red)
  1659.   turtle.select(16)
  1660.   while not turtle.drop() do sleep(5) end
  1661.   flex.send("Inventory has room!",colors.lightBlue)
  1662.   turtle.select(slot)
  1663.   return dropNotFuel()
  1664.  end --if
  1665.  
  1666.  turtle.select(slot)
  1667.  
  1668. end --function dropNotFuel()
  1669.  
  1670.  
  1671. -- Added function to get full inventory details
  1672. function getInventory()
  1673.     local inventory = {}
  1674.     for slot = 1, 16 do
  1675.         local itemDetail = turtle.getItemDetail(slot)
  1676.         if itemDetail then
  1677.             inventory[slot] = {
  1678.                 name = itemDetail.name,
  1679.                 count = itemDetail.count,
  1680.                 displayName = itemDetail.displayName, -- Include display name
  1681.                 durability = itemDetail.durability, -- Include durability
  1682.             }
  1683.         end
  1684.     end
  1685.     return inventory
  1686. end
Add Comment
Please, Sign In to add comment