Cwackers

dig.lua

May 26th, 2024 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Cloned
  2. -- This is an API for digging and
  3. -- keeping track of motion
  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", "diorite", "granite", "slate" }
  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. -- ||   /¯\  /¯] /\ [¯¯][¯¯] /¯\ |\ || --
  354. -- ||_ | O || [ |  | ||  ][ | O || \ | --
  355. -- |__] \_/  \_]|||| || [__] \_/ || \| --
  356. -----------------------------------------
  357. --||  || /\ |¯\[¯¯] /\ |¯\||  |¯¯]/¯¯\ --
  358. -- \\// |  || / ][ |  || <||_ | ] \_¯\ --
  359. --  \/  ||||| \[__]|||||_/|__]|__]\__/ --
  360. -----------------------------------------
  361.  
  362. xdist = 0
  363. xlast = -1
  364. xmin = 0
  365. xmax = 0
  366.  
  367. ydist = 0
  368. ylast = -1
  369. ymin = 0
  370. ymax = 0
  371.  
  372. zdist = 0
  373. zlast = -1
  374. zmin = 0
  375. zmax = 0
  376.  
  377. rdist = 0
  378. rlast = -1
  379.  
  380. lastmove = "r-"
  381. dugtotal = 0
  382.  
  383.  
  384. function getx() return xdist end
  385. function gety() return ydist end
  386. function getz() return zdist end
  387. function getr() return rdist end
  388.  
  389. function setx(x) xdist = x end
  390. function sety(y) ydist = y end
  391. function setz(z) zdist = z end
  392. function setr(r) rdist = r end
  393.  
  394. function getxmin() return xmin end
  395. function getxmax() return xmax end
  396. function getymin() return ymin end
  397. function getymax() return ymax end
  398. function getzmin() return zmin end
  399. function getzmax() return zmax end
  400.  
  401. function setxmin(x) xmin = x end
  402. function setxmax(x) xmax = x end
  403. function setymin(y) ymin = y end
  404. function setymax(y) ymax = y end
  405. function setzmin(z) zmin = z end
  406. function setzmax(z) zmax = z end
  407.  
  408. function getxlast() return xlast end
  409. function getylast() return ylast end
  410. function getzlast() return zlast end
  411. function getrlast() return rlast end
  412.  
  413. function setxlast(x) xlast = x end
  414. function setylast(y) ylast = y end
  415. function setzlast(z) zlast = z end
  416. function setrlast(r) rlast = r end
  417.  
  418. function getlast() return lastmove end
  419. function setlast(lm) lastmove = lm end
  420.  
  421. function getdug() return dugtotal end
  422. function setdug(d) dugtotal = d end
  423.  
  424.  
  425.  
  426.  
  427. -------------------------------------------
  428. --||   /¯\  /\ |¯\   ///¯¯\ /\ ||  |||¯¯]--
  429. --||_ | O ||  ||  | // \_¯\|  | \\// | ] --
  430. --|__] \_/ |||||_/ //  \__/||||  \/  |__]--
  431. -------------------------------------------
  432.  
  433. function location()
  434.  return
  435.   { xdist, ydist, zdist, rdist,
  436.     xmin, xmax, ymin, ymax, zmin, zmax,
  437.     xlast, ylast, zlast, rlast,
  438.     lastmove, dugtotal }
  439. end
  440.  
  441.  
  442. function saveCoords(loc,save)
  443.  loc = loc or location()
  444.  save = save or savefile
  445.  local file,x
  446.  
  447.  --if fs.exists(save.."_old") then
  448.  -- fs.delete(save.."_old")
  449.  --end
  450.  --if fs.exists(save) then
  451.   --fs.move(save, save.."_old")
  452.  --end
  453.  
  454.  while file == nil do
  455.   file = fs.open(save,"w")
  456.  end --while
  457.  
  458.  for x=1,#loc do
  459.   file.writeLine(tostring(loc[x]))
  460.   if x == 4 then file.flush() end
  461.  end --for
  462.  
  463.  file.close()
  464. end --function
  465.  
  466.  
  467.  
  468. function loadCoords(save)
  469.  save = save or savefile
  470.  local file
  471.  if fs.exists(save) then
  472.   while file == nil do
  473.    file = fs.open(save,"r")
  474.   end --while
  475.   xdist = tonumber(file.readLine() or xdist)
  476.   ydist = tonumber(file.readLine() or ydist)
  477.   zdist = tonumber(file.readLine() or zdist)
  478.   rdist = tonumber(file.readLine() or rdist)
  479.   xmin = tonumber(file.readLine() or xmin)
  480.   xmax = tonumber(file.readLine() or xmax)
  481.   ymin = tonumber(file.readLine() or ymin)
  482.   ymax = tonumber(file.readLine() or ymax)
  483.   zmin = tonumber(file.readLine() or zmin)
  484.   zmax = tonumber(file.readLine() or zmax)
  485.   xlast = tonumber(file.readLine() or xlast)
  486.   ylast = tonumber(file.readLine() or ylast)
  487.   zlast = tonumber(file.readLine() or zlast)
  488.   rlast = tonumber(file.readLine() or rlast)
  489.   lastmove = file.readLine() or lastmove
  490.   dugtotal = tonumber(file.readLine() or dugtotal)
  491.   file.close()
  492.   return true
  493.  
  494.  else
  495.   --if fs.exists(save.."_old") then
  496.   -- loadCoords(save.."_old")
  497.   -- return true
  498.   --end
  499.  
  500.   return false
  501.  end --if/else
  502.  
  503. end -- function
  504.  
  505.  
  506.  
  507. function makeStartup(command, args)
  508.  command = tostring(command)
  509.  args = args or {}
  510.  local x
  511.  for x=1,#args do
  512.   command = command.." "..args[x]
  513.  end --for
  514.  
  515.  local file = fs.open(startfile,"w")
  516.  file.writeLine("print(\"> "..command.."\")")
  517.  file.writeLine("for x=5,1,-1 do")
  518.  file.writeLine(" term.write(tostring(x)..\" \")")
  519.  file.writeLine(" sleep(1)")
  520.  file.writeLine("end --for")
  521.  file.writeLine("print(\" \")")
  522.  file.writeLine("shell.run(\""..command.."\")")
  523.  file.close()
  524. end --function
  525.  
  526.  
  527.  
  528.  
  529. -------------------------------------
  530. --[¯¯]|¯\ /\  /¯]||//[¯¯]|\ || /¯¯]--
  531. -- || | /|  || [ | <  ][ | \ || [¯|--
  532. -- || | \|||| \_]||\\[__]|| \| \__|--
  533. -------------------------------------
  534.  
  535. function update(n)
  536.  
  537.  if n=="fwd" then
  538.  
  539.   if rdist%360 == 0 then -- 12:00
  540.    zdist = zdist + 1
  541.    zlast = 1
  542.    lastmove = "z+"
  543.    
  544.   elseif rdist%360 == 90 then -- 3:00
  545.    xdist = xdist + 1
  546.    xlast = 1
  547.    lastmove = "x+"
  548.    
  549.   elseif rdist%360 == 180 then -- 6:00
  550.    zdist = zdist - 1
  551.    zlast = -1
  552.    lastmove = "z-"
  553.    
  554.   elseif rdist%360 == 270 then -- 9:00
  555.    xdist = xdist - 1
  556.    xlast = -1
  557.    lastmove = "x-"
  558.    
  559.   end --if/else
  560.  
  561.  
  562.  elseif n=="back" then
  563.  
  564.   if rdist%360 == 0 then -- 12:00
  565.    zdist = zdist - 1
  566.    zlast = -1
  567.    lastmove = "z-"
  568.    
  569.   elseif rdist%360 == 90 then -- 3:00
  570.    xdist = xdist - 1
  571.    xlast = -1
  572.    lastmove = "x-"
  573.    
  574.   elseif rdist%360 == 180 then -- 6:00
  575.    zdist = zdist + 1
  576.    zlast = 1
  577.    lastmove = "z+"
  578.    
  579.   elseif rdist%360 == 270 then -- 9:00
  580.    xdist = xdist + 1
  581.    xlast = 1
  582.    lastmove = "x+"
  583.    
  584.   end --if/else
  585.  
  586.  
  587.  elseif n=="up" then
  588.   ydist = ydist + 1
  589.   ylast = 1
  590.   lastmove = "y+"
  591.  
  592.  elseif n=="down" then
  593.   ydist = ydist - 1
  594.   ylast = -1
  595.   lastmove = "y-"
  596.  
  597.  elseif n=="right" then
  598.   rdist = rdist + 90
  599.   rlast = 1
  600.   lastmove = "r+"
  601.   while rdist > 999 do
  602.    rdist = rdist - 360
  603.   end --while
  604.  
  605.  elseif n=="left" then
  606.   rdist = rdist - 90
  607.   rlast = -1
  608.   lastmove = "r-"
  609.   while rdist < -999 do
  610.    rdist = rdist + 360
  611.   end --while
  612.  
  613.  end --if/else
  614.  
  615.  
  616.  if xdist < xmin then
  617.   xmin = xdist
  618.  elseif xdist > xmax then
  619.   xmax = xdist
  620.  end
  621.  
  622.  if ydist < ymin then
  623.   ymin = ydist
  624.  elseif ydist > ymax then
  625.   ymax = ydist
  626.  end
  627.  
  628.  if zdist < zmin then
  629.   zmin = zdist
  630.  elseif zdist > zmax then
  631.   zmax = zdist
  632.  end
  633.  
  634.  saveCoords()
  635.  
  636. end
  637.  
  638.  
  639.  
  640.  
  641. ------------------------------------------
  642. -- |\/| /¯\ ||  |||¯¯]|\/||¯¯]|\ ||[¯¯] --
  643. -- |  || O | \\// | ] |  || ] | \ | ||  --
  644. -- |||| \_/   \/  |__]|||||__]|| \| ||  --
  645. ------------------------------------------
  646.  
  647.  
  648. stuck = false
  649. function isStuck()
  650.  return stuck
  651. end
  652. stuckDir = "none"
  653. function getStuckDir()
  654.  return stuckDir
  655. end
  656.  
  657.  
  658. function left(n)
  659.  n = (n or 1)
  660.  if n < 0 then
  661.   return right(-n)
  662.  end --if
  663.  local x
  664.  for x=1,n do
  665.   turtle.turnLeft()
  666.   update("left")
  667.  end --if
  668.  return true
  669. end --function
  670.  
  671.  
  672. function right(n)
  673.  n = (n or 1)
  674.  if n < 0 then
  675.   return left(-n)
  676.  end --if
  677.  local x
  678.  for x=1,n do
  679.   turtle.turnRight()
  680.   update("right")
  681.  end --if
  682.  return true
  683. end --function
  684.  
  685.  
  686.  
  687. local unbreak = "Unbreakable block detected"
  688. local protect = "Cannot break protected block"
  689.  
  690.  
  691.  
  692. function up(n)
  693.  n = n or 1
  694.  if n < 0 then
  695.   return down(-n)
  696.  end --if
  697.  
  698.  local x,a,b,t
  699.  
  700.  for x=1, n do
  701.   refuel()
  702.  
  703.   if not turtle.up() then
  704.    
  705.    if doblacklist then
  706.     -- Blocks not to mine
  707.     if flex.isBlockUp(options["blacklist"]) then
  708.      if not back() then return false end
  709.      if not up() then return false end
  710.      return true
  711.     end --if
  712.    end --if
  713.    
  714.    t = os.time()
  715.    stuck = false
  716.    while not turtle.up() do
  717.     a,b = turtle.digUp()
  718.     while a do
  719.      dugtotal = dugtotal + 1
  720.      a,b = turtle.digUp()
  721.     end --while
  722.    
  723.     if b == protect then
  724.      if doblacklist then
  725.       options["blacklist"][
  726.          #options["blacklist"]+1]
  727.          = flex.getBlockUp()
  728.       return up(n-x+1)
  729.      else
  730.       b = unbreak
  731.      end --if
  732.     end --if
  733.    
  734.     if b == unbreak then
  735.      flex.send("#1"..b.."#0: {#8"..
  736.        tostring(getx()).."#0,#8"..
  737.        tostring(gety()+1).."#0,#8"..
  738.        tostring(getz()).."#0}")
  739.      knownBedrock[#knownBedrock+1] = {
  740.        getx(), gety()+1, getz() }
  741.      stuck = true
  742.      stuckDir = "up"
  743.      return false
  744.     end --if
  745.     if a then
  746.      dugtotal = dugtotal + 1
  747.     end --if
  748.     if attack then turtle.attackUp() end
  749.    
  750.     if (os.time()-t)/24*20*60 > 20 then
  751.      -- Stuck for at least 20 seconds
  752.      -- Usually caused by height limits
  753.      flex.send("Unable to move up",colors.orange)
  754.      stuck = true
  755.      stuckDir = "up"
  756.      return false
  757.     end --if
  758.    
  759.    end --while
  760.    
  761.   end --if can't move
  762.  
  763.   update("up")
  764.  end --for
  765.  
  766.  return true
  767. end --function
  768.  
  769.  
  770.  
  771. function down(n)
  772.  n = n or 1
  773.  if n < 0 then
  774.   return up(-n)
  775.  end --if
  776.  
  777.  local x,a,b,t
  778.  
  779.  for x=1, n do
  780.   refuel()
  781.  
  782.   if not turtle.down() then
  783.    
  784.    if doblacklist then
  785.     -- Blocks not to mine
  786.     if flex.isBlockDown(options["blacklist"]) then
  787.      if not fwd() then return false end
  788.      if not down() then return false end
  789.      return true
  790.     end --if
  791.    end --if
  792.    
  793.    t = os.time()
  794.    stuck = false
  795.    while not turtle.down() do
  796.     a,b = turtle.digDown()
  797.     while a do
  798.      dugtotal = dugtotal + 1
  799.      a,b = turtle.digDown()
  800.     end --while
  801.    
  802.     if b == protect then
  803.      if doblacklist then
  804.       options["blacklist"][
  805.         #options["blacklist"]+1 ]
  806.         = flex.getBlockDown()
  807.       return down(n-x+1)
  808.      else
  809.       b = unbreak
  810.      end --if
  811.     end --if
  812.    
  813.     if b == unbreak then
  814.      flex.send("#1"..b.."#0: {#8"..
  815.        tostring(getx()).."#0,#8"..
  816.        tostring(gety()-1).."#0,#8"..
  817.        tostring(getz()).."#0}")
  818.      knownBedrock[#knownBedrock+1] = {
  819.        getx(), gety()-1, getz() }
  820.      stuck = true
  821.      stuckDir = "down"
  822.      return false
  823.     end --if
  824.     if a then
  825.      dugtotal = dugtotal + 1
  826.     end --if
  827.     if attack then turtle.attackDown() end
  828.    
  829.     if (os.time()-t)/24*20*60 > 20 then
  830.      -- Stuck for at least 20 seconds
  831.      -- Usually caused by height limits
  832.      flex.send("Unable to move down",colors.orange)
  833.      stuck = true
  834.      stuckDir = "down"
  835.      return false
  836.     end --if
  837.    
  838.    end --while
  839.    
  840.   end --if can't move
  841.  
  842.   update("down")
  843.  end --for
  844.  
  845.  return true
  846. end --function
  847.  
  848.  
  849.  
  850. function fwd(n)
  851.  n = n or 1
  852.  if n < 0 then
  853.   return back(-n)
  854.  end --if
  855.  
  856.  local x,z,a,b,t
  857.  for x=1, n do
  858.   refuel()
  859.  
  860.   if not turtle.forward() then
  861.    
  862.    if doblacklist then
  863.     -- Blocks not to mine
  864.     if flex.isBlock(options["blacklist"]) then
  865.      if flex.isBlockUp(options["blacklist"]) then
  866.       if flex.isBlockDown(options["blacklist"]) then
  867.        
  868.        -- Blocks above, below and in front
  869.        if not back() then return false end
  870.        if not up(2) then return false end
  871.        if not fwd(2) then return false end
  872.        if not down(2) then return false end
  873.        return true
  874.        
  875.       else -- Blocks in front and above
  876.        if not down() then return false end
  877.        x = 0
  878.        while flex.isBlock(options["blacklist"]) do
  879.         x = x + 1
  880.         if not down() then return false end
  881.        end --while
  882.        if not fwd(2) then return false end
  883.        if not up(x) then return false end
  884.        return
  885.       end --if/else
  886.      
  887.      else
  888.       -- Block in front
  889.       if not up() then return false end
  890.       if not fwd(2) then return false end
  891.       if not down() then return false end
  892.       return true
  893.      end --if/else
  894.     end --if
  895.    end --if doblacklist
  896.    
  897.    t = os.time()
  898.    stuck = false
  899.    while not turtle.forward() do
  900.     a,b = turtle.dig()
  901.     while a do
  902.      dugtotal = dugtotal + 1
  903.      a,b = turtle.dig()
  904.     end --while
  905.    
  906.     if b == protect then
  907.      if doblacklist then
  908.       options["blacklist"][
  909.           #options["blacklist"]+1]
  910.           = flex.getBlock()
  911.       return fwd(n-x+1)
  912.      else
  913.       b = unbreak
  914.      end --if
  915.     end --if
  916.    
  917.     if b == unbreak then
  918.      z = { getx(), gety(),
  919.            getz(), getr()%360 }
  920.      
  921.      if z[4] == 0 then
  922.       z[3] = z[3] + 1
  923.      elseif z[4] == 90 then
  924.       z[1] = z[1] + 1
  925.      elseif z[4] == 180 then
  926.       z[3] = z[3] - 1
  927.      elseif z[4] == 270 then
  928.       z[1] = z[1] - 1
  929.      end --if/else
  930.      
  931.      flex.send("#1"..b.."#0: {#8"..
  932.        tostring(z[1]).."#0,#8"..
  933.        tostring(z[2]).."#0,#8"..
  934.        tostring(z[3]).."#0}")
  935.      knownBedrock[#knownBedrock+1] =
  936.        { z[1], z[2], z[3] }
  937.      stuck = true
  938.      stuckDir = "fwd"
  939.      return false
  940.     end --if
  941.    
  942.     if a then
  943.      dugtotal = dugtotal + 1
  944.     end --if
  945.     if attack then turtle.attack() end
  946.    
  947.     --if (os.time()-t)/24*20*60 > 120 then
  948.      -- Stuck for at least 20 seconds
  949.      --flex.send("Unable to move forward",colors.orange)
  950.      --stuck = true
  951.      --stuckDir = "fwd"
  952.      --return false
  953.      --t = os.time()
  954.     --end --if
  955.    
  956.    end --while not turtle.forward()
  957.    
  958.   end --if can't move
  959.  
  960.   update("fwd")
  961.  end --if
  962.  
  963.  return true
  964. end --function
  965.  
  966.  
  967.  
  968. function back(n)
  969.  n = n or 1
  970.  if n < 0 then
  971.   return fwd(-n)
  972.  end --if
  973.  
  974.  local x,turn
  975.  turn = false
  976.  for x=1,n do
  977.   if turn then
  978.    if not fwd() then return false end
  979.   elseif turtle.back() then
  980.    update("back")
  981.   else
  982.    turn = true
  983.    gotor(rdist+180)
  984.    if not fwd() then return false end
  985.   end --if/else
  986.  end --for
  987.  
  988.  if turn then gotor(rdist-180) end
  989.  return true
  990. end --function
  991.  
  992.  
  993.  
  994. function dig(x)
  995.  local a,b
  996.  x = x or "fwd"
  997.  
  998.  if x=="fwd" then
  999.   while turtle.dig() and not stuck do
  1000.    dugtotal = dugtotal + 1
  1001.   end --while
  1002.  
  1003.  elseif x=="up" then
  1004.   while turtle.digUp() and not stuck do
  1005.    dugtotal = dugtotal + 1
  1006.   end --while
  1007.  
  1008.  elseif x=="down" then
  1009.   while turtle.digDown() and not stuck do
  1010.    dugtotal = dugtotal + 1
  1011.   end --while
  1012.  
  1013.  end --if/else
  1014. end --function
  1015.  
  1016. function digUp() dig("up") end
  1017. function digDown() dig("down") end
  1018.  
  1019.  
  1020.  
  1021. -- Agressive placement functions
  1022.  
  1023. function place()
  1024.  local x = flex.getBlock("fwd")
  1025.  local t = os.time()
  1026.  while not turtle.place()
  1027.        and flex.isBlock(options["fluids"]) do
  1028.   if attack then
  1029.    turtle.attackUp()
  1030.    turtle.attack()
  1031.    turtle.attackDown()
  1032.   end --if
  1033.  
  1034.   if (os.time()-t)/24*20*60 > 20 then
  1035.    local z = { getx(), gety(),
  1036.          getz(), getr()%360 }
  1037.    if z[4] == 0 then
  1038.     z[3] = z[3] + 1
  1039.    elseif z[4] == 90 then
  1040.     z[1] = z[1] + 1
  1041.    elseif z[4] == 180 then
  1042.     z[3] = z[3] - 1
  1043.    elseif z[4] == 270 then
  1044.     z[1] = z[1] - 1
  1045.    end --if/else
  1046.    
  1047.    flex.send("#1End of world detected#0: {#8"..
  1048.      tostring(z[1]).."#0,#8"..
  1049.      tostring(z[2]).."#0,#8"..
  1050.      tostring(z[3]).."#0}")
  1051.    knownBedrock[#knownBedrock+1] =
  1052.      { z[1], z[2], z[3] }
  1053.    
  1054.    stuck = true
  1055.    stuckDir = "fwd"
  1056.    return false
  1057.   end --if
  1058.  
  1059.   sleep(0.1)
  1060.   x = flex.getBlock()
  1061.  end --while
  1062.  if turtle.getSelectedSlot() == blockSlot then
  1063.   checkBlocks()
  1064.  end --if
  1065.  return true
  1066. end --function
  1067.  
  1068.  
  1069. function placeUp()
  1070.  local x = flex.getBlockUp()
  1071.  local t = os.time()
  1072.  while not turtle.placeUp()
  1073.        and flex.isBlockUp(options["fluids"]) do
  1074.   if attack then
  1075.    turtle.attack()
  1076.    turtle.attackUp()
  1077.   end --if
  1078.  
  1079.   if (os.time()-t)/24*20*60 > 20 then
  1080.    flex.send("#1Edge of world detected#0: {#8"..
  1081.      tostring(getx()).."#0,#8"..
  1082.      tostring(gety()+1).."#0,#8"..
  1083.      tostring(getz()).."#0}")
  1084.    knownBedrock[#knownBedrock+1] = {
  1085.      getx(), gety()+1, getz() }
  1086.    
  1087.    stuck = true
  1088.    stuckDir = "up"
  1089.    return false
  1090.   end --if
  1091.  
  1092.   sleep(0.1)
  1093.   x = flex.getBlockUp()
  1094.  end --while
  1095.  if turtle.getSelectedSlot == blockSlot then
  1096.   checkBlocks()
  1097.  end --if
  1098.  return true
  1099. end --function
  1100.  
  1101.  
  1102. function placeDown()
  1103.  local x = flex.getBlockDown()
  1104.  local t = os.time()
  1105.  while not turtle.placeDown()
  1106.        and flex.isBlockDown(options["fluids"]) do
  1107.   if attack then
  1108.    turtle.attack()
  1109.    turtle.attackDown()
  1110.   end --if
  1111.  
  1112.   if (os.time()-t)/24*20*60 > 20 then
  1113.    flex.send("#1Edge of world detected#0: {#8"..
  1114.      tostring(getx()).."#0,#8"..
  1115.      tostring(gety()-1).."#0,#8"..
  1116.      tostring(getz()).."#0}")
  1117.    knownBedrock[#knownBedrock+1] = {
  1118.      getx(), gety()-1, getz() }
  1119.    up()
  1120.    placeDown()
  1121.    ymin = gety()
  1122.    
  1123.    stuck = true
  1124.    stuckDir = "down"
  1125.    return false
  1126.   end --if
  1127.  
  1128.   sleep(0.1)
  1129.   x = flex.getBlockDown()
  1130.  end --while
  1131.  if turtle.getSelectedSlot() == blockSlot then
  1132.   checkBlocks()
  1133.  end --if
  1134.  return true
  1135. end --function
  1136.  
  1137.  
  1138.  
  1139.  
  1140. ----------------------------
  1141. --  /¯¯]  /¯\  [¯¯]  /¯\  --
  1142. -- | [¯| | O |  ||  | O | --
  1143. --  \__|  \_/   ||   \_/  --
  1144. ----------------------------
  1145.  
  1146.  
  1147. function gotor(r)
  1148.  if r == nil then
  1149.   error("Number expected, got nil", 2)
  1150.   return
  1151.  end --if
  1152.  local x = (r-rdist)%360
  1153.  -- X is the target direction relative to current rotation
  1154.  
  1155.  if x == 90 then
  1156.   right()
  1157.  elseif x == 180 then
  1158.   left(rlast*2)
  1159.   -- Rotate opposite to last rotation
  1160.  elseif x == 270 then
  1161.   left()
  1162.  elseif x ~= 0 then
  1163.   error("Invalid rotation parameter", 2)
  1164.   return false
  1165.  end --if/else
  1166.  
  1167.  return true
  1168. end --function
  1169.  
  1170.  
  1171.  
  1172. function gotoy(y)
  1173.  if y == nil then
  1174.   error("Number expected, got nil", 2)
  1175.   return
  1176.  end --if
  1177.  
  1178.  while ydist < y do
  1179.   if not up() then return false end
  1180.  end
  1181.  while ydist > y do
  1182.   if not down() then return false end
  1183.  end
  1184.  return true
  1185. end
  1186.  
  1187.  
  1188. function gotox(x)
  1189.  if x == nil then
  1190.   error("Number expected, got nil", 2)
  1191.   return
  1192.  end --if
  1193.  
  1194.  if xdist < x then
  1195.   if rdist == 270 then
  1196.    return back(x-xdist)
  1197.    
  1198.   else
  1199.    gotor(90)
  1200.    return fwd(x-xdist)
  1201.   end
  1202.  end
  1203.  
  1204.  if xdist > x then
  1205.   if rdist == 90 then
  1206.    return back(xdist-x)
  1207.    
  1208.   else
  1209.    gotor(270)
  1210.    return fwd(xdist-x)
  1211.   end
  1212.  end
  1213.  
  1214.  return true
  1215. end
  1216.  
  1217.  
  1218. function gotoz(z)
  1219.  if z == nil then
  1220.   error("Number expected, got nil", 2)
  1221.   return
  1222.  end --if
  1223.  
  1224.  if zdist < z then
  1225.   if rdist == 180 then
  1226.    return back(z-zdist)
  1227.    
  1228.   else
  1229.    gotor(0)
  1230.    return fwd(z-zdist)
  1231.   end
  1232.  end
  1233.  
  1234.  if zdist > z then
  1235.   if rdist == 0 then
  1236.    return back(zdist-z)
  1237.    
  1238.   else
  1239.    gotor(180)
  1240.    return fwd(zdist-z)
  1241.   end
  1242.  end
  1243.  
  1244.  return true
  1245. end
  1246.  
  1247.  
  1248. function goto(x,y,z,r,lm)
  1249.  if type(x) == "table" then
  1250.   if #x < 4 then
  1251.    error("Invalid Goto Table",2)
  1252.   end
  1253.   y = x[2]
  1254.   z = x[3]
  1255.   r = x[4]
  1256.   lm = x[15]
  1257.   x = x[1]
  1258.  end
  1259.  gotox(x or 0)
  1260.  gotoz(z or 0)
  1261.  gotor(r or 0)
  1262.  gotoy(y or 0)
  1263.  if lm~=nil then setlast(lm) end
  1264. end
  1265.  
  1266.  
  1267.  
  1268.  
  1269. ---------------------------------------
  1270. -- |¯¯]|| |||¯¯]||    |¯\|¯\ /¯\     --
  1271. -- | ] ||_||| ] ||_   | /| /| O | == --
  1272. -- ||   \__||__]|__]  || | \ \_/     --
  1273. ---------------------------------------
  1274. --   /¯]|¯¯]/¯¯\/¯¯\[¯¯]|\ || /¯¯]   --
  1275. --  | [ | ] \_¯\\_¯\ ][ | \ || [¯|   --
  1276. --   \_]|__]\__/\__/[__]|| \| \__|   --
  1277. ---------------------------------------
  1278.  
  1279. fuelSlot = {1,16}
  1280.  
  1281. function getFuelSlot()
  1282.  return fuelSlot[1],fuelSlot[2]
  1283. end --function
  1284.  
  1285. function setFuelSlot(a,b)
  1286.  if a == nil then
  1287.   return false
  1288.  end --if
  1289.  b = b or a
  1290.  if a < b then
  1291.   fuelSlot = {a,b}
  1292.  else
  1293.   fuelSlot = {b,a}
  1294.  end --if/else
  1295.  return true
  1296. end --function
  1297.  
  1298.  
  1299.  
  1300. function refuel(b)
  1301.  b = b or 1
  1302.  b = math.min(b, turtle.getFuelLimit())
  1303.  local a,x,z,slot
  1304.  slot = turtle.getSelectedSlot()
  1305.  a = true
  1306.  
  1307.  while turtle.getFuelLevel() < b do
  1308.   for x=fuelSlot[1],fuelSlot[2] do
  1309.    turtle.select(x)
  1310.    if turtle.refuel(1) then break end
  1311.    if x == fuelSlot[2] and a then
  1312.     if fuelSlot[1] == fuelSlot[2] then
  1313.         flex.send("Waiting for fuel in "..
  1314.           "slot "..tostring(fuelSlot[1])..
  1315.           "...", colors.pink)
  1316.     else
  1317.      flex.send("Waiting for fuel in "..
  1318.        "slots "..tostring(fuelSlot[1])..
  1319.        "-"..tostring(fuelSlot[2])..
  1320.        "...", colors.pink)
  1321.     end --if/else
  1322.     a = false
  1323.    end --if
  1324.   end --for
  1325.  end --while
  1326.  
  1327.  if not a then
  1328.   flex.send("Thanks!",colors.lime)
  1329.  end --if
  1330.  turtle.select(slot)
  1331.  
  1332. end --function
  1333.  
  1334.  
  1335.  
  1336.  
  1337. local fuelvalue = {}
  1338. local fuelfile = "dig_fuel.cfg"
  1339. local file, line
  1340. if fs.exists(fuelfile) then
  1341.  file = fs.open(fuelfile, "r")
  1342.  line = file.readLine()
  1343.  while line ~= nil do
  1344.   fuelvalue[line] = tonumber(file.readLine())
  1345.   line = file.readLine()
  1346.  end --while
  1347.  file.close()
  1348. else
  1349.  file = fs.open(fuelfile, "w")
  1350.  file.close()
  1351. end --if/else
  1352.  
  1353.  
  1354. function checkFuelValue(x)
  1355.  if type(x) ~= "number" then
  1356.   local x = turtle.getSelectedSlot()
  1357.  end --if
  1358.  if turtle.getItemCount(x) == 0 then
  1359.   return 0
  1360.  end --if
  1361.  
  1362.  local a,b,c,d
  1363.  a = turtle.getItemDetail(x)["name"]
  1364.  b = fuelvalue[a]
  1365.  
  1366.  if b == nil then
  1367.   c = turtle.getFuelLevel()
  1368.   turtle.select(x)
  1369.   turtle.refuel(1)
  1370.   d = turtle.getFuelLevel() - c
  1371.   fuelvalue[a] = d
  1372.  
  1373.   file = fs.open(fuelfile, "a")
  1374.   file.writeLine(a)
  1375.   file.writeLine(tostring(d))
  1376.   file.close()
  1377.  
  1378.   if turtle.getItemCount(x) == 0 then
  1379.    return 0
  1380.   else
  1381.    return d
  1382.   end --if
  1383.  end --if
  1384.  
  1385.  return b
  1386. end --function
  1387.  
  1388.  
  1389.  
  1390.  
  1391. blockSlot = 0
  1392. blockStacks = 1
  1393. function getBlockSlot() return blockSlot end
  1394. function setBlockSlot(n) blockSlot = n end
  1395. function getBlockStacks() return blockStacks end
  1396. function setBlockStacks(n) blockStacks = n end
  1397.  
  1398.  
  1399. function checkBlocks()
  1400.  if blockSlot == 0 then return end
  1401.  local x,docondense
  1402.  
  1403.  if not flex.isItem(options["buildingblocks"],blockSlot) then
  1404.   if turtle.getItemCount(blockSlot) > 0 then
  1405.    for x=blockSlot+1,16 do
  1406.     if turtle.getItemCount(x) == 0 then
  1407.      turtle.select(blockSlot)
  1408.      turtle.transferTo(x)
  1409.      break
  1410.     end --if
  1411.    end --for
  1412.   end --if
  1413.  end --if
  1414.  
  1415.  if turtle.getItemCount(blockSlot) == 0 then
  1416.   for x=1,16 do
  1417.    if flex.isItem(options["buildingblocks"],x) then
  1418.     turtle.select(x)
  1419.     turtle.transferTo(blockSlot)
  1420.     turtle.select(blockSlot)
  1421.     break
  1422.    end --if
  1423.   end --for
  1424.  flex.condense(blockSlot)
  1425.  end --if
  1426.  
  1427.  turtle.select(blockSlot)
  1428. end --function
  1429.  
  1430.  
  1431.  
  1432. function blockLava(dir)
  1433.  if not flex.isBlock("lava",dir) then
  1434.   return
  1435.  end --if
  1436.  local slot = turtle.getSelectedSlot()
  1437.  checkBlocks()
  1438.  turtle.select(blockSlot)
  1439.  if dir == "up" then
  1440.   turtle.placeUp()
  1441.  elseif dir == "down" then
  1442.   turtle.placeDown()
  1443.  else
  1444.   turtle.place()
  1445.  end --if/else
  1446.  turtle.select(slot)
  1447. end --function
  1448.  
  1449. function blockLavaUp() blockLava("up") end
  1450. function blockLavaDown() blockLava("down") end
  1451.  
  1452.  
  1453.  
  1454.  
  1455. -- Organize Inventory/Fuel
  1456. --   C
  1457. --  CTQQQ
  1458. --   QQQQ
  1459. -- (Chest/Turtle/Quarry)
  1460.  
  1461. function dropNotFuel()
  1462.  
  1463.  local slot = turtle.getSelectedSlot()
  1464.  local a,b,c,f,x,y,z
  1465.  local crafty,usedbucketalready,blocksPresent
  1466.  turtle.select(1)
  1467.  if turtle.getItemCount(1) > 1 then
  1468.   flex.condense()
  1469.  end --if
  1470.  
  1471.  -- Check there's inventory to place loot
  1472.  a = true
  1473.  while true do
  1474.   if isChest() then
  1475.    break
  1476.   end
  1477.   if a then
  1478.    flex.send("Output inventory not found!",
  1479.      colors.red)
  1480.    a = false
  1481.   end --if
  1482.   sleep(1)
  1483.  end --while
  1484.  if not a then
  1485.   flex.send("Thanks!",colors.lightBlue)
  1486.  end --if
  1487.  
  1488.  
  1489.  -- Drop off what's not fuel
  1490.  blocksPresent = blockStacks
  1491.  for x=1,16 do
  1492.   turtle.select(x)
  1493.   if not turtle.refuel(0) then
  1494.    if flex.isItem(options["buildingblocks"]) then
  1495.     if blocksPresent <= 0 then
  1496.      turtle.drop()
  1497.     else
  1498.      blocksPresent = blocksPresent - 1
  1499.     end --if/else
  1500.    else
  1501.     turtle.drop()
  1502.    end --if/else
  1503.   end --if
  1504.  end --for
  1505.  checkBlocks()
  1506.  turtle.select(1)
  1507.  -- Retrieve surplus fuel from fuel chest
  1508.  while turtle.suckUp() do sleep(0) end
  1509.  if turtle.getItemCount(fuelSlot[1]) > 0 then
  1510.   flex.condense()
  1511.  end --if
  1512.  
  1513.  
  1514.  -- Craft coal into blocks
  1515.  if peripheral.find("workbench") and isChestUp() then
  1516.  
  1517.   z = 0
  1518.   for x=1,16 do
  1519.    if turtle.getItemCount(x) > 0 and
  1520.       turtle.getItemDetail(x)["name"]
  1521.       ~= "minecraft:coal" then
  1522.     turtle.select(x)
  1523.     turtle.dropUp()
  1524.    end --if
  1525.    z = z + turtle.getItemCount(x)
  1526.   end --for
  1527.  
  1528.   if z >= 9 then
  1529.    
  1530.    y = 13
  1531.    for x=1,12 do
  1532.     turtle.select(x)
  1533.     while turtle.getItemCount(x) > 0 do
  1534.      if y <= 16 then
  1535.       turtle.transferTo(y)
  1536.      else
  1537.       z = z - turtle.getItemCount(x)
  1538.       turtle.dropUp()
  1539.      end --if/else
  1540.      if turtle.getItemCount(x) > 0 then
  1541.       y = y + 1
  1542.      end --if
  1543.     end --while
  1544.    end --for
  1545.    
  1546.    y = math.floor(z/9)
  1547.    turtle.select(13)
  1548.    turtle.dropUp(z%9)
  1549.    a = 13
  1550.    b = { 1, 2, 3, 5, 6, 7, 9, 10, 11 }
  1551.    for x=1,#b do
  1552.     if turtle.getItemCount(a) < y then
  1553.      turtle.select(a+1)
  1554.      turtle.transferTo(a,
  1555.        y - turtle.getItemCount(a))
  1556.     end --if
  1557.     turtle.select(a)
  1558.     turtle.transferTo(b[x], y)
  1559.     if turtle.getItemCount(a) == 0 then
  1560.      a = a + 1
  1561.     end --if
  1562.    end --for
  1563.    
  1564.    if peripheral.getType("left") == "workbench" then
  1565.     crafty = peripheral.wrap("left")
  1566.    else
  1567.     crafty = peripheral.wrap("right")
  1568.    end --if/else
  1569.    turtle.select(1)
  1570.    crafty.craft()
  1571.    
  1572.   end --if
  1573.   while turtle.suckUp() do sleep(0) end
  1574.   checkBlocks()
  1575.  
  1576.  end --if
  1577.  
  1578.  
  1579.  --Tally up fuel sources
  1580.  z = {} -- slot #
  1581.  f = {} -- amount of fuel
  1582.  usedbucketalready = false
  1583.  
  1584.  for x=1,16 do
  1585.   turtle.select(x)
  1586.  
  1587.   if turtle.refuel(0) then
  1588.    y = turtle.getItemCount()
  1589.    
  1590.    if flex.isItem("bucket") and y==1 then
  1591.     if not usedbucketalready then
  1592.      -- Only use buckets one at a time
  1593.      turtle.refuel()
  1594.      usedbucketalready = true
  1595.      turtle.drop()
  1596.     end
  1597.    
  1598.    else -- Not a bucket
  1599.     a = checkFuelValue(x)
  1600.     if a > 0 then
  1601.      z[#z+1] = x
  1602.      f[#z] = a*turtle.getItemCount(x)
  1603.     end --if
  1604.    
  1605.    end --if/else (is bucket)
  1606.    
  1607.   end --if (is fuel)
  1608.  end --for (slots)
  1609.  
  1610.  
  1611.  -- Choose best fuel available
  1612.  a = 0
  1613.  for x=1, #f do
  1614.   a = math.max(a,f[x])
  1615.  end --for
  1616.  for x=1, #f do
  1617.   if f[x] == a then
  1618.    if z[x] > 1 then
  1619.     for b=2,16 do
  1620.      if turtle.getItemCount(b) == 0 then
  1621.       turtle.select(1)
  1622.       turtle.transferTo(b)
  1623.       break
  1624.      end --if
  1625.     end --for
  1626.     turtle.select(z[x])
  1627.     turtle.transferTo(1)
  1628.    end --if
  1629.    break
  1630.   end --if
  1631.  end --for
  1632.  
  1633.  
  1634.  -- Deposit surplus fuel
  1635.  for x=fuelSlot[1]+1,16 do
  1636.   if turtle.getItemCount(x) > 0 then
  1637.    
  1638.    turtle.select(x)
  1639.    if turtle.refuel(0) then
  1640.     if isChestUp() then
  1641.      -- Place in fuel chest
  1642.      turtle.dropUp()
  1643.     end --if
  1644.     if turtle.getItemCount() > 0 then
  1645.      -- Fuel chest is full or absent
  1646.      turtle.drop()
  1647.     end --if
  1648.    end --if
  1649.    
  1650.   end --if (more than zero items)
  1651.  end --for (all slots after fuel slot)
  1652.  
  1653.  turtle.select(fuelSlot[1])
  1654.  if not turtle.refuel(0) then
  1655.   for x=fuelSlot[1]+1,16 do
  1656.    turtle.transferTo(x)
  1657.    if turtle.getItemCount() == 0 then
  1658.     break
  1659.    end --if
  1660.   end --for
  1661.  end --if
  1662.  
  1663.  flex.condense(fuelSlot[1]+1)
  1664.  
  1665.  if turtle.getItemCount(16) > 0 then
  1666.   flex.send("Inventory full!",colors.red)
  1667.   turtle.select(16)
  1668.   while not turtle.drop() do sleep(5) end
  1669.   flex.send("Inventory has room!",colors.lightBlue)
  1670.   turtle.select(slot)
  1671.   return dropNotFuel()
  1672.  end --if
  1673.  
  1674.  turtle.select(slot)
  1675.  
  1676. end --function dropNotFuel()
  1677.  
  1678.  
  1679.  
Add Comment
Please, Sign In to add comment