ascobol

Modified EnderDig

May 16th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.93 KB | None | 0 0
  1. --[[
  2.  
  3. Modified Digging Program based on EnderDig/dignow by worshiprick
  4.  
  5. Original URL: http://turtlescripts.com/project/gjdh2g-EnderDig
  6.  
  7. Modifications:
  8. - EnderChest with fuel in Slot 16
  9. - EnderChest with torches in Slot 15 (Limits usable slots for items to 2 - 13)
  10. - More than one branch/tunnel possible
  11. - Start with arguments disables user input
  12.  
  13. --]]
  14.  
  15. local arg = (...)
  16.  
  17. local xSize
  18. local ySize
  19. local zSize
  20. local nRuns
  21. local nDistBetweenRuns = 0
  22.  
  23. print "Enderchest in slot 1,Enderchest with Torches in slot 15 (optional), EnderChest with fuel in slot 16 (optional, allows turtle to eat found fuel as a last resort)."
  24. if(arg == nil or #arg < 4) then
  25.     print "How far is a branch?(x)"
  26.     xSize = tonumber(io.read())
  27.     print "How high is a branch? (y)"
  28.     ySize = tonumber(io.read())
  29.     print "How wide (to the left) is a branch? (z) (even numbers are more efficient)"
  30.     zSize = tonumber(io.read())
  31.     print "How many branches to dig? (n)"
  32.     nRuns = tonumber(io.read())
  33.     if(nRuns < 1) then
  34.         nRuns = 1
  35.     end
  36.     if(nRuns ~= 1) then
  37.         print "How far many blocks are between the branches (to the right)? (d)"
  38.         nDistBetweenRuns = tonumber(io.read())         
  39.     end
  40. else
  41.     xSize = tonumber(arg[0])
  42.     ySize = tonumber(arg[1])
  43.     zSize = tonumber(arg[2])
  44.     nRuns = tonumber(arg[3])
  45.     if(nRuns < 1) then
  46.         nRuns = 1
  47.     end
  48.     if(#arg == 5) then
  49.         nDistBetweenRuns = tonumber(arg[4])
  50.     else
  51.         nDistBetweenRuns = zSize
  52.     end
  53.    
  54. end
  55.  
  56. while turtle.getItemCount(1) < 1 do
  57.   print "Please put an ENDER CHEST in slot 1. Hit any key when done."
  58.   os.pullEvent("char")
  59. end
  60.  
  61. -- 0 = South, 1 = West, 2 = North, 3 = East
  62. -- These are relative values, not necessarily the minecraft facing dir (which you need wireless for)
  63. -- If the player isn't actually facing south, x and z will be inverted or reversed. That's OK.
  64. local iDirection = 0
  65.  
  66. local xMovedFromOrigin = 0
  67. local yMovedFromOrigin = 0
  68. local zMovedFromOrigin = 0
  69.  
  70. local bHasTorches = turtle.getItemCount(15) > 0
  71. local bHasFuel = turtle.getItemCount(16) > 0
  72.  
  73. function CheckFuel()
  74.   if(bHasFuel == false) then
  75.     return true
  76.   end
  77.  
  78.   if turtle.getFuelLevel() <= 10 then
  79.     turtle.select(16)
  80.     --turtle.refuel(1)
  81.     RefuelFromSlot16()
  82.     if turtle.getFuelLevel() <= 10 then
  83.       -- phase 2: low fuel, fueling failed, return home
  84.     end
  85.   end
  86.  
  87.   if turtle.getFuelLevel() <= 1 then
  88.     print "OOF: Out Of Fuel :("
  89.     print "I'm going to eat your coal"
  90.     shell.run("refuel", "all") 
  91.     if turtle.getFuelLevel() <= 1 then
  92.       print "I am STILL out of fuel...turtle starving..."
  93.       print "going...dark..."
  94.       print "...so...cold...sleepy..."
  95.       turtle.sleep(120)
  96.       return false
  97.     end
  98.   end
  99.  
  100.   return true  
  101. end
  102.  
  103. function MoveForward()
  104.   CheckFuel()
  105.  
  106.   if(turtle.forward()) then
  107.     if iDirection == 0 then
  108.       zMovedFromOrigin = zMovedFromOrigin + 1
  109.     elseif iDirection == 1 then
  110.       xMovedFromOrigin = xMovedFromOrigin - 1
  111.     elseif iDirection == 2 then
  112.       zMovedFromOrigin = zMovedFromOrigin - 1
  113.     elseif iDirection == 3 then
  114.       xMovedFromOrigin = xMovedFromOrigin + 1
  115.     end
  116.     return true
  117.   else
  118.     return false
  119.   end
  120. end
  121.  
  122. function TurnLeft()
  123.   iDirection = iDirection - 1
  124.   if(iDirection < 0) then
  125.     iDirection = 3
  126.   end
  127.   turtle.turnLeft()
  128. end
  129.  
  130. function TurnRight()
  131.   iDirection = iDirection + 1
  132.   if(iDirection > 3) then
  133.     iDirection = 0
  134.   end
  135.   turtle.turnRight()
  136. end
  137.  
  138. function MoveUp()
  139.   CheckFuel()
  140.   if(turtle.up()) then
  141.     yMovedFromOrigin = yMovedFromOrigin + 1
  142.     return true
  143.   end
  144.   return false
  145. end
  146.  
  147. function MoveDown()
  148.   CheckFuel()
  149.   if(turtle.down()) then
  150.     yMovedFromOrigin = yMovedFromOrigin - 1
  151.     return true
  152.   end
  153.   return false
  154. end
  155.  
  156. function PlaceTorch()
  157.     if(bHasTorches == false) then
  158.         return
  159.     end
  160.  
  161.     if turtle.getItemCount(14) < 1 then
  162.         RefillTorchesFromSlot15()
  163.     end
  164.    
  165.     if turtle.getItemCount(14) >= 1 then
  166.         local iSaveDirection = iDirection
  167.         while(iDirection ~= 2) do
  168.             TurnRight()
  169.         end
  170.  
  171.         turtle.select(14)
  172.         turtle.place()
  173.  
  174.         while(iDirection ~= iSaveDirection) do
  175.             TurnLeft()
  176.         end
  177.     end
  178. end
  179.  
  180. function DigUp(iBlocks)
  181.     if(iBlocks < 1) then
  182.         return
  183.     end
  184.     for y = 1, iBlocks - 1 do
  185.         while(MoveUp() == false) do
  186.             turtle.digUp()
  187.             turtle.suckUp()
  188.         end -- end while
  189.     end -- end up/down
  190. end
  191.  
  192. function DigDown(iBlocks)
  193.     if(iBlocks < 1) then
  194.         return
  195.     end
  196.     for y = 1, iBlocks - 1 do
  197.         while(MoveDown() == false) do
  198.             turtle.digDown()
  199.             turtle.suckDown()
  200.         end -- end while
  201.     end -- end up/down
  202. end
  203.  
  204. function DigForward()
  205.     while(MoveForward() == false) do
  206.         turtle.dig()
  207.         turtle.suck()
  208.     end
  209. end
  210.  
  211.  
  212. function GoHome()
  213.  
  214.     DigDown(yMovedFromOrigin)
  215.  
  216.     if iDirection == 0 then
  217.         TurnRight()
  218.         TurnRight()
  219.     elseif iDirection == 1 then
  220.         TurnRight()
  221.     elseif iDirection == 3 then
  222.         TurnLeft()
  223.     end
  224.  
  225.     for i = 1, zMovedFromOrigin do
  226.         DigForward()
  227.     end
  228.     TurnLeft()
  229.  
  230.     for i = 1, xMovedFromOrigin do
  231.         DigForward()
  232.     end
  233.  
  234. end
  235.  
  236. function DumpInventoryToSlot1()
  237.     while turtle.detectUp() do
  238.         turtle.digUp()
  239.     end
  240.     turtle.select(1)
  241.     while turtle.placeUp() == false do
  242.         turtle.digUp()
  243.     end
  244.  
  245.     local iLastInventorySlot = 16
  246.     if(bHasTorches) then
  247.         iLastInventorySlot = 13
  248.     elseif(bHasFuel) then
  249.         iLastInventorySlot = 15
  250.     end
  251.  
  252.     for i = 2, iLastInventorySlot do
  253.         turtle.select(i)
  254.         turtle.dropUp()
  255.     end
  256.     turtle.select(1)
  257.     turtle.digUp()
  258. end
  259.  
  260. function RefuelFromSlot16()
  261.   local result = false
  262.   while turtle.detectUp() do
  263.     turtle.digUp()
  264.   end
  265.   turtle.select(16)
  266.   while turtle.placeUp() == false do
  267.     turtle.digUp()
  268.   end
  269.  
  270.   local refilled = turtle.suckUp()
  271.   if(not refilled) then
  272.     print("Fuel chest is empty!")
  273.   else
  274.     local refueled = turtle.refuel()
  275.     if(not refueled) then
  276.         print("No fuel in fuel chest found!")
  277.         turtle.dropUp()
  278.     else
  279.         result = true
  280.     end
  281.   end
  282.  
  283.   turtle.select(16)
  284.   turtle.digUp()
  285.   return result
  286. end
  287.  
  288. function RefillTorchesFromSlot15()
  289.   local result = false
  290.   while turtle.detectUp() do
  291.     turtle.digUp()
  292.   end
  293.   turtle.select(15)
  294.   while turtle.placeUp() == false do
  295.     turtle.digUp()
  296.   end
  297.   turtle.select(14)
  298.   local refilled = turtle.suckUp()
  299.   if(not refilled) then
  300.     print("Torch chest is empty!")
  301.   else
  302.     print("Torches refilled (" .. turtle.getItemCount(14) .. ")!") 
  303.     result = true  
  304.   end
  305.  
  306.   turtle.select(15)
  307.   turtle.digUp()
  308.   return result
  309. end
  310.  
  311. function ResetDirections()
  312.     iDirection = 0
  313.  
  314.     xMovedFromOrigin = 0
  315.     yMovedFromOrigin = 0
  316.     zMovedFromOrigin = 0   
  317. end
  318.  
  319. function Main()
  320.   for n = 1, nRuns do
  321.       ResetDirections()
  322.            
  323.       print("Mining " .. xSize .. ", " .. ySize .. ", " .. zSize .. ", Branch: " .. n)
  324.      
  325.       TurnLeft()
  326.       local zMax = math.floor(zSize / 2)
  327.      
  328.       for x = 1, xSize do
  329.  
  330.         for z = 1, zMax do
  331.           DigUp(ySize)
  332.           DigForward()
  333.           DigDown(ySize)
  334.  
  335.           if z < zMax then
  336.             DigForward()
  337.           end
  338.           -- if we filled up almost all of the inventory (1 is ender chest so 2-10), dump inventory to chest
  339.           if turtle.getItemCount(10) >= 1 then
  340.             print("~" .. (x / xSize * 100) .. "% complete of branch " .. n .. ", " .. turtle.getFuelLevel()  .. " fuel remaining.")
  341.             DumpInventoryToSlot1()
  342.           end
  343.         end -- end z axis
  344.  
  345.         -- if the face size wasn't even, we still have a single column to clear
  346.         if(zSize % 2 ~= 0) then
  347.           -- print("adjusting for odd face size")
  348.           DigForward()
  349.           DigUp(ySize)
  350.           DigDown(ySize)
  351.         end
  352.  
  353.         -- if we need to change the orientation of the cleared area, change right to left here
  354.         if(x % 2 ~= 0) then
  355.           TurnRight()
  356.           DigForward()
  357.           TurnRight()
  358.         else
  359.           TurnLeft()
  360.           DigForward()
  361.           TurnLeft()
  362.         end
  363.  
  364.         if(x % 8 == 7) then
  365.           PlaceTorch()
  366.         end
  367.  
  368.       end -- end x axis
  369.      
  370.       print "Returning to beginning of branch now!"
  371.  
  372.       GoHome()
  373.       DumpInventoryToSlot1()
  374.      
  375.       if((nRuns ~= 1) and (n ~= nRuns)) then
  376.         for go = 1, (nDistBetweenRuns + zSize) do
  377.             DigForward()
  378.         end
  379.         TurnLeft()
  380.       end    
  381.   end
  382.     print("Mining done!")
  383. end
  384.  
  385. Main() -- !
Advertisement
Add Comment
Please, Sign In to add comment