Robear9992

quarry

Jul 2nd, 2022 (edited)
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.33 KB | None | 0 0
  1. --cc:Tweaked: Quarry Program for Turtle
  2. --pastebin get UfndbHaX quarry.lua
  3. --quarry 10 10 10
  4.  
  5. local sides = require("sides")
  6. local nav = require("navigation")
  7. local inventory = require("inventory")
  8. local timer = require("timer")
  9.  
  10. local clearEverything = false
  11. local OK_EMPTY_SLOTS = 4 --The number of empty slots after dropping crap necessary to continue mining
  12.  
  13. local STONE =           { name = "minecraft:stone" }
  14. local DIRT =            { name = "minecraft:dirt" }
  15. local GRASS =           { name = "minecraft:grass_block"}
  16. local COBBLE =          { name = "minecraft:cobblestone" }
  17. local GRAVEL =          { name = "minecraft:gravel" }
  18. local SAND =            { name = "minecraft:sand" }
  19. local SANDSTONE =       { name = "minecraft:sandstone" }
  20. local NETHERRACK =      { name = "minecraft:netherrack" }
  21. local DIORITE =         { name = "minecraft:diorite" }
  22. local ANDESITE =        { name = "minecraft:andesite" }
  23. local GRANITE =         { name = "minecraft:granite" }
  24. local COAL =            { name = "minecraft:coal" }
  25. local GRANITE_C =       { name = "create:granite_cobblestone" }
  26. local ANDESITE_C =      { name = "create:andesite_cobblestone" }
  27. local DIORITE_C =       { name = "create:diorite_cobblestone" }
  28. local STELLA_ARC =      { name = "forbidden_arcanus:stella_arcanum" }
  29. local BEDROCK =         { name = "minecraft:bedrock" }
  30. local COARSE_DIRT =     { name = "minecraft:coarse_dirt" }
  31. local TUFF =            { name = "minecraft:tuff" }
  32. local DEEPSLATE_C =     { name = "minecraft:cobbled_deepslate" }
  33. local DEEPSLATE =       { name = "minecraft:deepslate" }
  34. local LIMESTONE =       { name = "create:limestone" }
  35. local CALCITE =         { name = "minecraft:calcite" }
  36. local SMOOTH_BASALT =   { name = "minecraft:smooth_basalt" }
  37. local RED_SAND =        { name = "minecraft:red_sand" }
  38. local YELLOW_TC =       { name = "minecraft:yellow_terracotta" }
  39.  
  40.  
  41. local crap = {
  42.     STONE,
  43.     DIRT,
  44.     GRASS,
  45.     COBBLE,
  46.     GRAVEL,
  47.     SAND,
  48.     SANDSTONE,
  49.     NETHERRACK,
  50.     DIORITE,
  51.     ANDESITE,
  52.     GRANITE,
  53.     GRANITE_C,
  54.     ANDESITE_C,
  55.     DIORITE_C,
  56.     STELLA_ARC,
  57.     BEDROCK,
  58.     COARSE_DIRT,
  59.     TUFF,
  60.     DEEPSLATE_C,
  61.     DEEPSLATE,
  62.     LIMESTONE,
  63.     CALCITE,
  64.     SMOOTH_BASALT,
  65.     RED_SAND,
  66.     YELLOW_TC
  67. }
  68.    
  69. local danger = { STELLA_ARC, BEDROCK }
  70.  
  71.  
  72. local function showFinalMessage()
  73.     print("completed in " .. timer.format() .. " with " .. turtle.getFuelLevel() .. " fuel remaining")
  74. end
  75.  
  76. local function abort(reason)
  77.     nav.turnTo(sides.front)
  78.     showFinalMessage()
  79.     error(reason)
  80. end
  81.  
  82. local function isCrap(item)
  83.     for _, icrap in pairs(crap) do
  84.         if stacksEqual(icrap, item) then
  85.             return true
  86.         end
  87.     end
  88.     return false
  89. end
  90.  
  91. local function isDangerous(item)
  92.     for _, idanger in pairs(danger) do
  93.         if stacksEqual(idanger, item) then
  94.             return true, item
  95.         end
  96.     end
  97.     return false
  98. end
  99.  
  100. local function dropCrap()
  101.     local dropped = false
  102.     for n=1,16 do
  103.         if turtle.getItemCount(n) > 0 then
  104.             if isCrap(turtle.getItemDetail(n, true)) then
  105.                 turtle.select(n)
  106.                 turtle.dropUp()
  107.                 dropped = true
  108.             end
  109.         end
  110.     end
  111.     return dropped
  112. end
  113.  
  114. local function unload()
  115.     local facing = nav.getDirection()
  116.     local position = nav.getPosition()
  117.     moveTo({x=0,y=0})
  118.     moveTo({z=0})
  119.     nav.turnTo(sides.left)
  120.     dropCrap() --HACK: Can pick up crap en route back
  121.     if not turtle.items.depositAll(sides.front) then abort("Inventory full") end
  122.     moveTo(position)
  123.     nav.turnTo(facing)
  124. end
  125.  
  126. local function checkInventory()
  127.     if turtle.items.isFull() then
  128.         dropCrap()
  129.     turtle.items.compact()
  130.        
  131.         if turtle.items.count() >= turtle.items.size() - OK_EMPTY_SLOTS then
  132.             unload()
  133.         end
  134.     end
  135. end
  136.  
  137. local function digUp()
  138.     local exists,item = turtle.inspectUp()
  139.     if(exists and (clearEverything or not isCrap(item))) then
  140.         turtle.digUp()
  141.     end
  142. end
  143.  
  144. local function digDown()
  145.     local exists,item = turtle.inspectDown()
  146.     if(exists and (clearEverything or not isCrap(item))) then
  147.         turtle.digDown()
  148.     end
  149. end
  150.  
  151. local function getRequiredFuelLevel(x,y,z)
  152.     return x * y * (z / 3) + 200
  153. end
  154.  
  155. local function flee(reason)
  156.     if nav.getPosition().y == 0 then
  157.         nav.up()
  158.         nav.up()
  159.         nav.up()
  160.     end
  161.     nav.moveTo({x=0})
  162.     nav.moveTo({y=0})
  163.     nav.moveTo({z=0})
  164.     dropCrap()
  165.     unload()
  166.     abort(reason)
  167. end
  168.  
  169. local function fleeBlock(block)
  170.     flee("Encountered " .. block.name .. " at level " .. nav.getPosition().z)
  171. end
  172.  
  173. local function isDangerAhead()
  174.     local exists,item = turtle.inspect()
  175.     if exists then
  176.         return isDangerous(item)
  177.     end
  178.     return false
  179. end
  180.  
  181. local function forward()
  182.     local danger, item = isDangerAhead()
  183.     if danger then fleeBlock(item) end
  184.     nav.forward()
  185. end
  186.  
  187. local function isDangerDown()
  188.     local exists,item = turtle.inspectDown()
  189.     if exists then
  190.         return isDangerous(item)
  191.     end
  192.     return false
  193. end
  194.  
  195. local function down()
  196.     local danger, item = isDangerDown()
  197.     if isDangerDown() then fleeBlock(item) end
  198.     nav.down()
  199. end
  200.  
  201. local function isDangerUp()
  202.     local exists,item = turtle.inspectUp()
  203.     if exists then
  204.         return isDangerous(item)
  205.     end
  206.     return false
  207. end
  208.  
  209. local function up()
  210.     local danger, item = isDangerUp()
  211.     if danger then fleeBlock(item) end
  212.     nav.up()
  213. end
  214.  
  215.  
  216. function moveTo(position)
  217.     local navOffset = nav.getPosition()
  218.     position = navOffset.union(position)
  219.     local delta = position.subtract(navOffset)
  220.    
  221.     if delta.z > 0 then
  222.         for i=1,delta.z do up() end
  223.     elseif delta.z < 0 then
  224.         for i=1,-delta.z do down() end
  225.     end
  226.  
  227.     if delta.x > 0 then
  228.         nav.turnTo(sides.right)
  229.         for i=1,delta.x do forward() end
  230.     elseif delta.x < 0 then
  231.         nav.turnTo(sides.left)
  232.         for i=1,-delta.x do forward() end
  233.     end
  234.    
  235.     if delta.y > 0 then
  236.         nav.turnTo(sides.front)
  237.         for i=1,delta.y do forward() end
  238.     elseif delta.y <0 then
  239.         nav.turnTo(sides.back)
  240.         for i=1,-delta.y do forward() end
  241.     end
  242. end
  243.  
  244. ------------------------------------  MAIN ROUTINES
  245.  
  246. local tArgs = { ... }
  247.  
  248. if #tArgs ~= 3 then
  249.         print( "Usage: quarry <depth> <distance> <width>" )
  250.         return
  251. end
  252.  
  253. -- Mine in a quarry pattern until we hit something we can't dig
  254. local depth     = tonumber( tArgs[1] )
  255. local distance  = tonumber( tArgs[2] )
  256. local width     = tonumber( tArgs[3] )
  257.  
  258. --Validation
  259. if depth < 1 then
  260.         print( "Quarry depth must be greater than 0" )
  261.         return
  262. end
  263.  
  264. if distance < 1 then
  265.         print( "Quarry distance must be greater than 0" )
  266.         return
  267. end
  268.  
  269. if width < 1 then
  270.         print( "Quarry width must be greater than 0" )
  271.         return
  272. end
  273.  
  274. if width % 2 == 1 then
  275.         print( "Quarry width must be an even number" )
  276.         return
  277. end
  278.  
  279.  
  280. if turtle.getFuelLevel() < getRequiredFuelLevel(width,distance,depth) then
  281.     local fuelRequired = getRequiredFuelLevel(width,distance,depth)
  282.     io.write("Not enough fuel (" .. fuelRequired .. " required)\n")
  283.     return
  284. end
  285.  
  286. if inventory.wrap(sides.left) == nil then
  287.     io.write("Chest required to the left of turtle\n")
  288.     return
  289. end
  290.  
  291. turtle.select(1)
  292. local zs = -math.floor(depth / 3) -- We mine straight, up, and down
  293. local xs = width / 2 -- a zig and a zag
  294. timer.start()
  295.  
  296. for z=1,zs,-1 do
  297.     moveTo({z=(z-1)*3-3})
  298.     for x=1,xs do
  299.         moveTo({x=(x-1)*2})
  300.         nav.turnTo(sides.front)
  301.        
  302.         digUp()
  303.         digDown()
  304.         for y=1,distance do
  305.             forward()
  306.             digUp()
  307.             digDown()
  308.             checkInventory()
  309.         end
  310.  
  311.         moveTo({x=(x-1)*2+1})
  312.         nav.turnTo(sides.back)
  313.         for n=1,distance do
  314.             digUp()
  315.             digDown()
  316.             forward()
  317.             checkInventory()
  318.         end
  319.         digUp()
  320.         digDown()
  321.  
  322.         moveTo({y=0}) --HACK: Ensure we reset
  323.     end
  324. end
  325.  
  326. dropCrap()
  327. nav.moveHome()
  328. unload()
  329. nav.turnTo(sides.front)
  330. showFinalMessage()
  331.  
Add Comment
Please, Sign In to add comment