Advertisement
drpepper240

buildplane

Dec 31st, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.76 KB | None | 0 0
  1.     local tArgs = { ... }
  2.     if #tArgs < 3 then
  3.             print( "Usage: buildplane <n up> <n right> <n forward>" )
  4.             return
  5.     end
  6.      
  7.     -- Mine in a quarry pattern until we hit something we can't dig
  8.     --local length = tonumber( tArgs[1] )
  9.     local rDn = tonumber( tArgs[1] )
  10.     local rRt = tonumber( tArgs[2] )
  11.     local rFd = tonumber( tArgs[3] )
  12.      
  13.     -- if length < 1 then
  14.             -- print( "Tunnel length must be positive" )
  15.             -- return
  16.     -- end
  17.            
  18.     local cDn = 0
  19.     local cFd = 0
  20.     local cRt = 0
  21.     local hFd = 1
  22.     local hRt = 0
  23.     local collected = 0
  24.    
  25.     local slotsAvailable = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
  26.  
  27.      
  28.     local function collect()
  29.             collected = collected + 1
  30.             if math.fmod(collected, 25) == 0 then
  31.                     print( "Mined "..collected.." items." )
  32.             end
  33.     end
  34.      
  35.     local function selectLastAvailableSlot()
  36.         local cSlot = 1
  37.         slotsAvailable = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
  38.         repeat
  39.             if (turtle.getItemCount(1) < 2) then
  40.                 slotsAvailable[1] = false
  41.                 print("out of Blocks (1-12)")
  42.                 io.read()
  43.             else
  44.                 slotsAvailable[1] = true
  45.                 for cSlot=12,2,-1 do
  46.                     if (turtle.getItemCount(cSlot)>0) then
  47.                         if (slotsAvailable[cSlot] == true) then
  48.                             turtle.select(cSlot)
  49.                             if (turtle.compareTo(1)) then
  50.                                 turtle.select(cSlot)
  51.                                 return
  52.                             else
  53.                                 slotsAvailable[cSlot] = false
  54.                             end
  55.                         end
  56.                     else
  57.                         slotsAvailable[cSlot] = false
  58.                     end
  59.                 end
  60.                 turtle.select(1)
  61.                 return
  62.             end
  63.         until ( slotsAvailable[1] == true )
  64.     end
  65.  
  66.     --placing block underneath
  67.     local function placeBlockDown()
  68.         if not (turtle.detectDown()) then
  69.             selectLastAvailableSlot()
  70.             turtle.placeDown()
  71.         end
  72.     end
  73.    
  74.     --placing block in front of
  75.     local function placeBlock()
  76.         selectLastAvailableSlot()
  77.         turtle.place()
  78.     end
  79.  
  80.     local function tryDig()
  81.             while turtle.dig() do
  82.                 collect()
  83.             end
  84.             return true
  85.     end
  86.    
  87.     local function tryDigUp()
  88.             while turtle.digUp() do
  89.                 collect()
  90.                 --sleep(0.5)
  91.             end
  92.             return true
  93.     end
  94.      
  95.     local function refuel()
  96.             local fuelLevel = turtle.getFuelLevel()
  97.             if fuelLevel == "unlimited" or fuelLevel > 0 then
  98.                     return
  99.             end
  100.            
  101.             local function tryRefuel()
  102.                     for n=1,16 do
  103.                             if turtle.getItemCount(n) > 0 then
  104.                                     turtle.select(n)
  105.                                     if turtle.refuel(1) then
  106.                                             turtle.select(1)
  107.                                             return true
  108.                                     end
  109.                             end
  110.                     end
  111.                     turtle.select(1)
  112.                     return false
  113.             end
  114.            
  115.             if not tryRefuel() then
  116.                     print( "Add more fuel to continue." )
  117.                     while not tryRefuel() do
  118.                             sleep(1)
  119.                     end
  120.                     print( "Resuming Tunnel." )
  121.             end
  122.     end
  123.      
  124.     local function tryDown()
  125.             refuel()
  126.             while not turtle.down() do
  127.                     if turtle.detectDown() then
  128.                             if not turtle.digDown() then
  129.                                     return false
  130.                             end
  131.                     elseif turtle.attackDown() then
  132.                             collect()
  133.                     else
  134.                             sleep( 0.5 )
  135.                     end
  136.             end
  137.             return true
  138.     end
  139.      
  140.     local function tryForward()
  141.             refuel()
  142.             while not turtle.forward() do
  143.                     if turtle.detect() then
  144.                             if not tryDig() then
  145.                                     return false
  146.                             end
  147.                     elseif turtle.attack() then
  148.                             collect()
  149.                     else
  150.                             sleep( 0.5 )
  151.                     end
  152.             end
  153.             return true
  154.     end
  155.    
  156.     local function tryUp()
  157.         refuel()
  158.         while not turtle.up() do
  159.             if turtle.detectUp() then
  160.                 if not tryDigUp() then
  161.                     return false
  162.                 end
  163.             elseif turtle.attackUp() then
  164.                 collect()
  165.             else
  166.                 sleep( 0.5 )
  167.             end
  168.         end
  169.         return true
  170.     end
  171.    
  172.     local lastTurnR = 0
  173.      
  174.     print( "Building..." )
  175.    
  176.     if (rFd==0 and rRt == 0) then
  177.         --up only
  178.         for n=0,rDn do
  179.             if tryUp() then
  180.                 --if (n % 2==0) then
  181.                     placeBlockDown()
  182.                 --end
  183.             else
  184.                 print("cannot dig up")
  185.             end
  186.         end
  187.     else
  188.    
  189.         for n=0,rDn do
  190.                 while cRt <= rRt do
  191.                         while cFd < rFd do
  192.                                 if tryForward() then
  193.                                         cFd = cFd + 1
  194.                                         --put block
  195.                                         placeBlockDown()
  196.                                 else
  197.                                         print("cannot dig fd")
  198.                                 end
  199.                         end
  200.                        
  201.                         if cRt ~= rRt then
  202.                                 if lastTurnR == 1 then
  203.                                         turtle.turnLeft()
  204.                                         tryForward()
  205.                                         --put block
  206.                                         placeBlockDown()
  207.                                         turtle.turnLeft()
  208.                                         lastTurnR = 0
  209.                                 elseif lastTurnR == 0 then
  210.                                         turtle.turnRight()
  211.                                         tryForward()
  212.                                         --put block
  213.                                         placeBlockDown()
  214.                                         turtle.turnRight()
  215.                                         lastTurnR = 1
  216.                                 end
  217.                         else
  218.                         turtle.turnRight()
  219.                         turtle.turnRight()
  220.                         end
  221.                        
  222.                         cFd=0
  223.                         cRt= cRt + 1
  224.                 end
  225.                 cRt=0
  226.                 if n~=rDn then
  227.                         if not tryUp() then
  228.                                 print("cannot dig dn")
  229.                         else
  230.                                 --put block
  231.                                 placeBlockDown()
  232.                         end
  233.                 end
  234.         end
  235.     end      
  236.      
  237.     print( "Construction complete." )
  238.     print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement