Advertisement
Cwackers

quarry.lua

May 26th, 2024 (edited)
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Cloned
  2. -- This is a replacement for the
  3. -- 'excavate' program, as it can re-
  4. -- cover from a reboot/unload event.
  5. -- Also avoids destroying spawners!
  6. -- <Flexico64@gmail.com>
  7. -- Please email me if you have any
  8. -- bugs or suggestions!
  9.  
  10. -----------------------------------
  11. -- [¯¯] || || |¯\ [¯¯] ||   |¯¯] --
  12. --  ||  ||_|| | /  ||  ||_  | ]  --
  13. --  ||   \__| | \  ||  |__] |__] --
  14. -----------------------------------
  15. --  /¯\  || ||  /\  |¯\ |¯\ \\// --
  16. -- | O | ||_|| |  | | / | /  \/  --
  17. --  \_\\  \__| |||| | \ | \  ||  --
  18. -----------------------------------
  19.  
  20. os.loadAPI("flex.lua")
  21. os.loadAPI("dig.lua")
  22. dig.doBlacklist() -- Avoid Protected Blocks
  23. dig.doAttack() -- Attack entities that block the way
  24. dig.setFuelSlot(1)
  25. dig.setBlockSlot(2)
  26. local world_height = 384
  27.  
  28.  
  29. local args = {...}
  30. if #args == 0 then
  31.  flex.printColors(
  32.    "quarry <length> [width] [depth]\n"..
  33.    "[skip <layers>] [dump] [nolava] [nether]",
  34.    colors.lightBlue)
  35.  return
  36. end --if
  37.  
  38.  
  39. local reloaded = false
  40. if dig.saveExists() then
  41.  reloaded = true
  42.  dig.loadCoords()
  43. end --if
  44. dig.makeStartup("quarry",args)
  45.  
  46.  
  47. local zmax = tonumber(args[1])
  48. local xmax = tonumber(args[2]) or zmax
  49. local depth = world_height-1
  50. if tonumber(args[2]) ~= nil then
  51.  depth = tonumber(args[3]) or depth
  52. end --if
  53. local ymin = -depth --(1-depth)
  54.  
  55. if xmax == nil or zmax == nil then
  56.  flex.send("Invalid dimensions,",colors.red)
  57.  shell.run("rm startup.lua")
  58.  return
  59. end --if
  60.  
  61.  
  62. local x
  63. local skip = 0
  64. local lava = true
  65. local dodumps = false
  66.  
  67. for x=1,#args do
  68.  
  69.  if args[x] == "dump" then
  70.   dodumps = true
  71.  elseif args[x] == "nolava" then
  72.   lava = false
  73.  elseif args[x] == "nether" then
  74.   dig.setBlockStacks(4)
  75.  end --if
  76.  
  77.  if args[x] == "skip" then
  78.   skip = tonumber(args[x+1])
  79.   if skip == nil then
  80.    flex.printColors("Please specify skip depth",
  81.      colors.red)
  82.    dig.saveClear()
  83.    return
  84.   end --if
  85.   if dig.getymin() > -skip then
  86.    dig.setymin(-skip)
  87.   end --if
  88.  end --if
  89.  
  90. end --for
  91.  
  92.  
  93. if not lava then -- Block lava around edges of quarry
  94.  dig.setBlockSlot(0)
  95.  -- Always keep a stack of blocks
  96. end --if
  97.  
  98.  
  99.  
  100.  
  101. ----------------------------------------------
  102. -- |¯¯]|| |||\ || /¯][¯¯][¯¯] /¯\ |\ ||/¯¯\ --
  103. -- | ] ||_||| \ || [  ||  ][ | O || \ |\_¯\ --
  104. -- ||   \__||| \| \_] || [__] \_/ || \|\__/ --
  105. ----------------------------------------------
  106.  
  107. local location
  108. local function gotoBase()
  109.  local x = dig.getxlast()
  110.  location = dig.location()
  111.  if dig.gety() < -skip then dig.up() end
  112.  dig.gotox(0)
  113.  dig.gotoz(0)
  114.  dig.gotor(180)
  115.  dig.gotoy(0)
  116.  dig.gotox(0)
  117.  dig.setxlast(x)
  118.  dig.gotoz(0)
  119.  dig.gotor(180)
  120.  return location
  121. end --function
  122.  
  123. local function returnFromBase(loc)
  124.  local loc = loc or location
  125.  local x = dig.getxlast()
  126.  dig.gotor(0)
  127.  checkFuel()
  128.  dig.gotoy(math.min(loc[2]+1,-skip))
  129.  checkFuel()
  130.  dig.gotoz(loc[3])
  131.  checkFuel()
  132.  dig.gotox(loc[1])
  133.  dig.setxlast(x) -- Important for restoring
  134.  checkFuel()
  135.  dig.gotor(loc[4])
  136.  checkFuel()
  137.  dig.gotoy(loc[2])
  138. end --function
  139.  
  140.  
  141.  
  142. local function checkHalt()
  143.  if not rs.getInput("top") then
  144.   return
  145.  end --if
  146.  if dig.gety() == 0 then
  147.   return
  148.  end --if
  149.  
  150.  local loc,x
  151.  -- Manual halt; redstone signal from above
  152.  flex.send("Manual halt initiated", colors.orange)
  153.  flex.printColors("Press ENTER to resume mining\n"
  154.    .."or SPACE to return to base",
  155.    colors.pink)
  156.  
  157.  while true do
  158.   x = flex.getKey()
  159.   if x == keys.enter then return end
  160.   if x == keys.space then break end
  161.  end --while
  162.  
  163.  flex.send("Returning to base", colors.yellow)
  164.  loc = gotoBase()
  165.  print(" ")
  166.  flex.printColors("Press ENTER to resume mining",
  167.    colors.pink)
  168.  while flex.getKey() ~= keys.enter do
  169.   sleep(1)
  170.  end --while
  171.  
  172.  if dodumps then dig.doDumpDown() end
  173.  dig.dropNotFuel()
  174.  flex.send("Resuming quarry",colors.yellow)
  175.  returnFromBase(loc)
  176.  
  177. end --function
  178.  
  179.  
  180.  
  181. local function checkInv()
  182.  if turtle.getItemCount(16) > 0 then
  183.  
  184.   if dodumps then
  185.    dig.right(2)
  186.    dig.doDump()
  187.    dig.left(2)
  188.   end --if
  189.  
  190.   if turtle.getItemCount(14) > 0 then
  191.    local loc = gotoBase()
  192.    dig.dropNotFuel()
  193.    returnFromBase(loc)
  194.   end --if
  195.  
  196.  end --if
  197. end --function
  198.  
  199.  
  200.  
  201. function checkFuel()
  202.  local a = turtle.getFuelLevel()
  203.  local b = ( zmax + xmax + depth + 1 )*2
  204.  local c = true
  205.  
  206.  while a < b and c do
  207.   for x=1,16 do
  208.    turtle.select(x)
  209.    if turtle.refuel(1) then
  210.     break
  211.    end --if
  212.    if x == 16 then
  213.     c = false
  214.    end --if
  215.   end --for
  216.   a = turtle.getFuelLevel()
  217.  end --while
  218.  
  219.  if a < b then
  220.   flex.send("Fuel low, returning to surface",
  221.     colors.yellow)
  222.   local loc = gotoBase()
  223.   turtle.select(1)
  224.   if dodumps then dig.doDumpDown() end
  225.   while turtle.suckUp() do sleep(0) end
  226.   dig.dropNotFuel()
  227.   dig.refuel(b)
  228.   flex.send("Fuel aquired!",colors.lightBlue)
  229.   returnFromBase(loc)
  230.  end --if
  231. end --function
  232.  
  233.  
  234.  
  235. local dug = dig.getdug()
  236. local ydeep = dig.getymin()
  237. local function checkProgress()
  238.  a = 1000 --report every <a> blocks dug
  239.  b = 5 --report every <b> meters descended
  240.  if math.floor(dug/a) < math.floor(dig.getdug()/a) then
  241.   flex.send("Dug "..tostring(dig.getdug())..
  242.     " blocks",colors.lightBlue)
  243.  end --if
  244.  if math.floor(-ydeep/b) < math.floor(-dig.gety()/b) then
  245.   flex.send("Descended "..tostring(-dig.gety())..
  246.     "m",colors.green)
  247.  end --if
  248.  dug = dig.getdug()
  249.  ydeep = dig.gety()
  250. end --function
  251.  
  252.  
  253.  
  254. local newlayer = false
  255. function checkNewLayer()
  256.  if newlayer then
  257.   -- This encodes whether or not the turtle has
  258.   --  started a new layer if at the edge
  259.   dig.setr(dig.getr() % 360 + 360)
  260.  else
  261.   dig.setr(dig.getr() % 360)
  262.  end --if
  263. end --function
  264.  
  265.  
  266.  
  267. function lavax()
  268.   if dig.getx() == 0 then
  269.    dig.gotor(270)
  270.    checkNewLayer()
  271.    dig.blockLava()
  272.   elseif dig.getx() == xmax-1 then
  273.    dig.gotor(90)
  274.    checkNewLayer()
  275.    dig.blockLava()
  276.   end --if/else
  277. end --function
  278.  
  279. function lavaz()
  280.   if dig.getz() == 0 then
  281.    dig.gotor(180)
  282.    checkNewLayer()
  283.    dig.blockLava()
  284.   elseif dig.getz() == zmax-1 then
  285.    dig.gotor(0)
  286.    checkNewLayer()
  287.    dig.blockLava()
  288.   end --if/else
  289. end --function
  290.  
  291. function checkLava(n)
  292.  if lava then
  293.   local x
  294.   local r = dig.getr() % 360
  295.  
  296.   if r == 0 or r == 180 then
  297.    lavaz()
  298.    lavax()
  299.   else
  300.    lavax()
  301.    lavaz()
  302.   end --if/else
  303.  
  304.   if dig.gety() == -skip then
  305.    dig.blockLavaUp()
  306.   end --if
  307.  
  308.   if dig.getx() == 0 and dig.getz() == 0
  309.      and dig.gety() > -skip then
  310.    for x=1,4 do
  311.     dig.blockLava()
  312.     dig.left()
  313.     checkNewLayer()
  314.    end --for
  315.   end --if
  316.  
  317.   if n ~= 0 then
  318.    dig.gotor(r)
  319.    checkNewLayer()
  320.   end --if
  321.  
  322.  end --if
  323. end --function
  324.  
  325.  
  326.  
  327. function checkAll(n)
  328.  checkNewLayer()
  329.  checkProgress()
  330.  checkFuel()
  331.  checkInv()
  332.  checkHalt()
  333.  checkLava(n)
  334.  dig.checkBlocks()
  335.  checkNewLayer()
  336. end --function
  337.  
  338.  
  339.  
  340.  
  341. ---------------------------------------
  342. --      |\/|  /\  [¯¯] |\ ||         --
  343. --      |  | |  |  ][  | \ |         --
  344. --      |||| |||| [__] || \|         --
  345. ---------------------------------------
  346. -- |¯\ |¯\  /¯\   /¯¯] |¯\  /\  |\/| --
  347. -- | / | / | O | | [¯| | / |  | |  | --
  348. -- ||  | \  \_/   \__| | \ |||| |||| --
  349. ---------------------------------------
  350.  
  351. local a,b,c,x,y,z,r,loc
  352. local xdir, zdir = 1, 1
  353.  
  354. turtle.select(1)
  355. if reloaded then
  356.  
  357.  flex.send("Resuming "..tostring(zmax).."x"
  358.    ..tostring(xmax).." quarry",colors.yellow)
  359.  
  360.  if dig.gety()==dig.getymin() and dig.gety()~=0 then
  361.   zdir = dig.getzlast()
  362.   if zdir == 0 then zdir = 1 end
  363.   xdir = dig.getxlast()
  364.   if xdir == 0 then xdir = 1 end
  365.  
  366.   if dig.getr() >= 360 then
  367.    -- This encodes whether or not the turtle has
  368.    --  started a new layer if at the edge
  369.    xdir = -xdir
  370.    newlayer = true
  371.   end --if
  372.  
  373.  else
  374.   gotoBase()
  375.   if dodumps then dig.doDumpDown() end
  376.   dig.dropNotFuel()
  377.   dig.gotor(0)
  378.   checkFuel()
  379.   dig.gotoy(dig.getymin())
  380.  end --if
  381.  
  382. else
  383.  
  384.  flex.send("Starting "..tostring(zmax).."x"
  385.    ..tostring(xmax).." quarry",colors.yellow)
  386.  
  387.  if skip > 0 then
  388.   flex.send("Skipping "..tostring(skip)
  389.     .."m", colors.lightGray)
  390.  end --if
  391.  
  392.  if depth < world_height-1 then
  393.   flex.send("Going "..tostring(-ymin)
  394.     .."m deep", colors.lightGray)
  395.  else
  396.   flex.send("To bedrock!",colors.lightGray)
  397.  end --if/else
  398.  
  399. end --if/else
  400.  
  401.  
  402. while dig.gety() > -skip do
  403.  checkFuel()
  404.  dig.down()
  405.  
  406.  if dig.isStuck() then
  407.   flex.send("Co-ordinates lost! Shutting down",
  408.     colors.red)
  409.   --rs.delete("startup.lua")
  410.   return
  411.  end --if
  412.  
  413. end --while
  414.  
  415.  
  416.  
  417. --------------------------
  418. -- |\/|  /\  [¯¯] |\ || --
  419. -- |  | |  |  ][  | \ | --
  420. -- |||| |||| [__] || \| --
  421. --------------------------
  422. -- ||    /¯\   /¯\  |¯\ --
  423. -- ||_  | O | | O | | / --
  424. -- |__]  \_/   \_/  ||  --
  425. --------------------------
  426.  
  427. local done = false
  428. while not done and not dig.isStuck() do
  429.  turtle.select(1)
  430.  
  431.  while not done do
  432.  
  433.   checkAll(0)
  434.   if dig.getz()<=0 and zdir==-1 then break end
  435.   if dig.getz()>=zmax-1 and zdir==1 then break end
  436.  
  437.   if zdir == 1 then dig.gotor(0)
  438.   elseif zdir == -1 then dig.gotor(180)
  439.   end --if/else
  440.   checkNewLayer()
  441.  
  442.   dig.fwd()
  443.  
  444.   if dig.isStuck() then
  445.    done = true
  446.   end --if
  447.  
  448.  end --while (z loop)
  449.  
  450.  if done then break end
  451.  
  452.  zdir = -zdir
  453.  newlayer = false
  454.  
  455.  if dig.getx()<=0 and xdir==-1 then
  456.   newlayer = true
  457.  elseif dig.getx()>=xmax-1 and xdir==1 then
  458.   newlayer = true
  459.  else
  460.   checkAll(0)
  461.   dig.gotox(dig.getx()+xdir)
  462.  end --if/else
  463.  
  464.  if newlayer and not dig.isStuck() then
  465.   xdir = -xdir
  466.   if dig.getymin() <= ymin then break end
  467.   checkAll(0)
  468.   dig.down()
  469.  end --if
  470.  
  471. end --while (cuboid dig loop)
  472.  
  473.  
  474. flex.send("Digging completed, returning to surface",
  475.   colors.yellow)
  476. gotoBase()
  477.  
  478. flex.send("Descended "..tostring(-dig.getymin())..
  479.     "m total",colors.green)
  480. flex.send("Dug "..tostring(dig.getdug())..
  481.     " blocks total",colors.lightBlue)
  482.  
  483. for x=1,16 do
  484.  if dig.isBuildingBlock(x) then
  485.   turtle.select(x)
  486.   dig.placeDown()
  487.   break
  488.  end --if
  489. end --for
  490. turtle.select(1)
  491.  
  492. if dodumps then
  493.  dig.gotor(0)
  494.  dig.doDump()
  495.  dig.gotor(180)
  496. end
  497. dig.dropNotFuel()
  498. dig.gotor(0)
  499.  
  500. dig.clearSave()
  501. flex.modemOff()
  502. os.unloadAPI("dig.lua")
  503. os.unloadAPI("flex.lua")
  504.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement