Guest User

dig.lua

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