Guest User

quarry.lua

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