ceribia

Excavate

May 27th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.16 KB | None | 0 0
  1. local depth = 0
  2. local unloaded = 0
  3. local collected = 0
  4.  
  5. local xPos,zPos = 0,0
  6. local xDir,zDir = 0,1
  7.  
  8. local goTo -- Filled in further down
  9. local refuel -- Filled in further down
  10.  
  11. local function unload()
  12.     refuel()
  13.     print( "Unloading items..." )
  14.     for n=1,16 do
  15.         turtle.select(n)           
  16.         turtle.drop()
  17.     end
  18. end
  19.  
  20. local function returnLoot()
  21.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  22.     print( "Returning to surface..." )
  23.     goTo(0,0,0,0,-1)
  24.     unload()
  25.    
  26.     local roundTripFuel = (x + y + z) * 2
  27.     if refuel(roundTripFuel) then
  28.         print( "Resuming mining..." )
  29.         goTo(x,y,z,xd,zd)
  30.     else
  31.         error( "Insufficient fuel to make it back")
  32.     end
  33. end
  34.  
  35. local function isFull()
  36.     for n=1,16 do
  37.         if turtle.getItemCount(n) == 0 then        
  38.             --Empty slots
  39.             return false;
  40.         end
  41.     end
  42.     print( "No empty slots left." )
  43.     return true;
  44. end
  45.  
  46. function refuel( amount )
  47.     local fuelLevel = turtle.getFuelLevel()
  48.     if fuelLevel == "unlimited" then
  49.         return true
  50.     end
  51.    
  52.     if amount == nil or fuelLevel < amount then
  53.         for n=1,16 do
  54.             turtle.select(n)
  55.             turtle.refuel()
  56.                        
  57.             if amount ~= nil and turtle.getFuelLevel() >= amount then          
  58.                 return true
  59.             end
  60.         end
  61.        
  62.         if amount ~= nil then
  63.             -- Not enough fuel
  64.             print("Insufficient fuel")
  65.             return false
  66.         else
  67.             return true
  68.         end    
  69.     else
  70.         return true
  71.     end
  72. end
  73.  
  74. movements = {}
  75. movements["forward"] = {}
  76. movements["forward"]["detect"] = turtle.detect
  77. movements["forward"]["dig"] = turtle.dig   
  78. movements["forward"]["move"] = turtle.forward
  79. movements["forward"]["attack"] = turtle.attack
  80.  
  81. movements["up"] = {}
  82. movements["up"]["detect"] = turtle.detectUp
  83. movements["up"]["dig"] = turtle.digUp
  84. movements["up"]["move"] = turtle.up
  85. movements["up"]["attack"] = turtle.attackUp
  86.  
  87. movements["down"] = {}
  88. movements["down"]["detect"] = turtle.detectDown
  89. movements["down"]["dig"] = turtle.digDown
  90. movements["down"]["move"] = turtle.down
  91. movements["down"]["attack"] = turtle.attackDown
  92.                    
  93. local function move(direction)
  94.     --print("Attempting to move with " .. direction)
  95.     while not movements[direction].move() do
  96.         if movements[direction].detect() then
  97.             if movements[direction].dig() then
  98.                 if isFull() then
  99.                     returnLoot()
  100.                 end            
  101.             else
  102.                 error("Failed to dig block")               
  103.             end
  104.         elseif movements[direction].attack() then
  105.             -- Do nothing, next loop after attacking
  106.         else
  107.             sleep( 0.5 )
  108.         end
  109.     end
  110.    
  111.     if direction == "forward" then
  112.         xPos = xPos + xDir
  113.         zPos = zPos + zDir
  114.     elseif direction == "up" then
  115.         depth = depth - 1
  116.     elseif direction == "down" then
  117.         depth = depth + 1
  118.     end
  119. end
  120.  
  121. local function turnLeft()
  122.     turtle.turnLeft()
  123.     xDir, zDir = -zDir, xDir
  124. end
  125.  
  126. local function turnRight()
  127.     turtle.turnRight()
  128.     xDir, zDir = zDir, -xDir
  129. end
  130.  
  131. function alignXDir( xGoalDir )
  132.     if xGoalDir == xDir then
  133.         return
  134.     elseif xGoalDir == zDir then
  135.         turnRight()
  136.     elseif xGoalDir == -zDir then
  137.         turnLeft()
  138.     else
  139.         turnRight()
  140.         turnRight()
  141.     end
  142.    
  143.     if xGoalDir ~= xDir then
  144.         error("Failed to fix direction")
  145.     end
  146. end    
  147.  
  148. function alignZDir( zGoalDir )
  149.     if zGoalDir == zDir then
  150.         return
  151.     elseif zGoalDir == xDir then
  152.         turnLeft()
  153.     elseif zGoalDir == -xDir then
  154.         turnRight()
  155.     else
  156.         turnRight()
  157.         turnRight()
  158.     end
  159.    
  160.     if zGoalDir ~= zDir then
  161.         error("Failed to fix direction")
  162.     end
  163. end    
  164.  
  165. function goTo( x, y, z, xd, zd, atEachSqurare)
  166.     --print( "GoTo: " .. x .. "," .. y .. "," .. z .. " From: " .. xPos .. "," .. depth .. "," .. zPos)
  167.    
  168.     local targetDistanceToHome = x + y + z
  169.     if not refuel(targetDistanceToHome) then
  170.         print("We can't go there and make it home.")
  171.        
  172.         local distanceHome = xPos + zPos + depth
  173.         if not refuel(distanceHome) then
  174.             error("Not enough fuel to get home!")
  175.         else
  176.             goTo( 0,0,0,0,-1 )
  177.             unload()
  178.             error("Didn't have enough fuel to finish")
  179.         end
  180.     end    
  181.    
  182.     while depth ~= y do
  183.         -- print("Depth = " .. depth .. " | y = " .. y)
  184.         if depth > y then
  185.             move("up")
  186.         elseif depth < y then
  187.             move("down")
  188.         end
  189.        
  190.         if atEachSqurare then
  191.             atEachSqurare()
  192.         end
  193.     end
  194.  
  195.     local xGoalDir = (x - xPos) / math.abs(x - xPos)
  196.     while xPos ~= x do 
  197.         alignXDir(xGoalDir)
  198.         move("forward")
  199.         if atEachSqurare then
  200.             atEachSqurare()
  201.         end    
  202.     end
  203.        
  204.     local zGoalDir = (z - zPos) / math.abs(z - zPos)
  205.     while zPos ~= z do 
  206.         alignZDir(zGoalDir)
  207.         move("forward")
  208.         if atEachSqurare then
  209.             atEachSqurare()
  210.         end    
  211.     end
  212.        
  213.     --Fix alignment
  214.     if xd ~= -1 then
  215.         alignXDir(xd)
  216.     end
  217.    
  218.     if zd ~= -1 then
  219.         alignZDir(zd)
  220.     end
  221. end
  222.  
  223. function mineAboveAndBelow ()
  224.     turtle.digUp()
  225.     turtle.digDown()
  226. end
  227.  
  228. function mineLevel(maxX, maxZ, yDepth, upAndDown)
  229.     local nextColumnDirection = turnRight
  230.  
  231.     local initalZ = zPos
  232.     local targetZ = maxZ
  233.     local incrememnt = 1
  234.    
  235.     if zPos == maxZ then
  236.         targetZ = 0
  237.         incrememnt = -1
  238.     end
  239.    
  240.     local atEachSqurare = nil
  241.     if upAndDown then
  242.         atEachSqurare = mineAboveAndBelow
  243.     end
  244.    
  245.     for z=initalZ, targetZ, incrememnt do
  246.         --Move to start of the column
  247.         goTo(xPos,yDepth,z,-1,-1, atEachSqurare)
  248.    
  249.         --Sweep the column
  250.         if xPos == 0 then
  251.             goTo(maxX,depth,zPos,-1,-1, atEachSqurare)
  252.         else
  253.             goTo(0,depth,zPos,-1,-1, atEachSqurare)
  254.         end        
  255.     end
  256. end
  257.  
  258. -----------------------------------------------------------------------------
  259. --Main Program
  260. -----------------------------------------------------------------------------
  261.  
  262. local tArgs = { ... }
  263. if #tArgs ~= 3 then
  264.     print( "My Version Usage: excavate xLength zLength depth" )
  265.     return
  266. end
  267.  
  268. local xLength = tonumber( tArgs[1] ) - 1
  269. local zLength = tonumber( tArgs[2] ) - 1
  270. local yLength = tonumber( tArgs[3] ) - 1
  271.  
  272. if xLength < 0 or zLength < 0 or yLength < 0 then
  273.     print( "All arguments must be positive" )
  274.     return
  275. end
  276.  
  277. print( "Quarrying..." )
  278. if yLength >= 2 then
  279.     for y=1, (yLength-1), 3 do
  280.         print("Mining level " .. y)
  281.         mineLevel(xLength, zLength, y, true)
  282.     end
  283. end
  284.  
  285. print( "Done updown levels. Depth " .. depth .. "/" .. yLength)
  286.  
  287. if depth < (yLength-1) then
  288.     for y=depth+2, yLength do
  289.         print("Finishing level "  .. y )
  290.         mineLevel(xLength, zLength, y)
  291.     end
  292. end
  293.    
  294. print( "Returning to entance..." )
  295. -- Return to where we started
  296. goTo( 0,0,0,0,-1 )
  297. unload()
  298. goTo( 0,0,0,0,1)
  299.  
  300. print("Done")
Add Comment
Please, Sign In to add comment