Cwackers

quarry.lua

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