Skaruts

ComputerCraft Shafter Program

Dec 10th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. -- Variables ----------------------------------------------------------
  2.  
  3.     local tArgs = { ... }
  4.  
  5.     local tFuel = tonumber (turtle.getFuelLevel())
  6.     local tSlot = 1
  7.  
  8.     if #tArgs < 1 then
  9.         term.clear()
  10.         term.setCursorPos(1,1)
  11.         print("Usage: shafter <length> <height> <width> <Direction>");
  12.         print("Type shafter and a '?' or 'help' for more info.")
  13.         return
  14.     elseif (tArgs[1] == '?' or tArgs[1] == 'help') then
  15.         term.clear()
  16.         term.setCursorPos(1,1)
  17.         print("shafter <length> <height> <width> <Direction>");
  18.         print("Examples:")
  19.         print("shafter 20 2 5 1")
  20.         print("Creates a 20 long, 5 wide, 2 high room from right to left.")
  21.         print("If details are omitted,")
  22.         print("default height is 2,")
  23.         print("default width is 1,")
  24.         print("default direction is 2 (left to right).")
  25.         print("Shaft length is mandatory.")
  26.         return
  27.     end
  28.    
  29.     local sLength = tonumber ( tArgs[1] )
  30.    
  31.    
  32.    
  33.    
  34.     local sWidth = tonumber (tArgs [3] )
  35.     local sHeight = tonumber (tArgs [2] )
  36.     local sDirection = tonumber (tArgs [4] )
  37.    
  38.     if sWidth == nil then
  39.         sWidth = 1
  40.     end
  41.  
  42.     if sHeight == nil then
  43.         sHeight = 2
  44.     end
  45.     if sDirection == nil then
  46.         sDirection = 2
  47.     end
  48.  
  49. --  local turns = sWidth - 1
  50.     local aSize = (sLength * sWidth) * sHeight
  51.     local sRows = sWidth
  52.    
  53.     hasTurned = false
  54. -- Functions ------------------------------------------------------------
  55.    
  56.     local function digShaft(length, height)
  57.         if hasTurned == true then
  58.             length = length - 1
  59.         end
  60.  
  61.         for i = 1, length do
  62.             turtle.dig()
  63.  
  64.             for j = 1, height - 1 do
  65.                 turtle.up()
  66.                 turtle.dig()
  67.             end
  68.  
  69.             if (turtle.detectUp() == false) then
  70.                 turtle.select(2)
  71.                 turtle.placeUp()
  72.             end
  73.  
  74.             for j = 1, height - 1 do
  75.                 turtle.down()
  76.             end
  77.             turtle.forward()
  78.  
  79.             if (turtle.detectDown() == false) then
  80.                 turtle.select(2)
  81.                 turtle.placeDown()
  82.             end
  83.         end
  84.         sRows = sRows - 1
  85.     end
  86.  
  87.     local function swapTurn(direction)
  88.         if direction == 1 then
  89.             sDirection = 2
  90.         elseif direction == 2 then
  91.             sDirection = 1
  92.         end
  93.     end
  94.  
  95.     local function turnTurtle(direction)
  96.         if direction == 1 then
  97.             turtle.turnLeft()
  98.         elseif direction == 2 then
  99.             turtle.turnRight()
  100.         end
  101.     end
  102.    
  103.     local function makeTurn(height)
  104.            
  105.         turnTurtle(sDirection)
  106.         turtle.dig()
  107.        
  108.         for u = 1, height - 1 do
  109.             turtle.up()
  110.             turtle.dig()
  111.         end
  112.        
  113.         if (turtle.detectUp() == false) then
  114.             turtle.select(2)
  115.             turtle.placeUp()
  116.         end
  117.        
  118.         for d = 1, height - 1 do
  119.             turtle.down()
  120.         end
  121.        
  122.         turtle.forward()  
  123.        
  124.         if (turtle.detectDown() == false) then
  125.             turtle.select(2)
  126.             turtle.placeDown()
  127.         end
  128.    
  129.         turnTurtle(sDirection)
  130.         swapTurn(sDirection)
  131.         hasTurned = true
  132.     end
  133.    
  134.    
  135.             -- Check fuel ----------------------------
  136.             -------------------------------------------
  137.            
  138.     local function checkFuel(size)
  139.         fuel = turtle.getFuelLevel()
  140.         trips = (size / 2) * 3 -- wrong!
  141.         fuelNeeded = trips - fuel
  142.        
  143.         if fuelNeeded < 0 then
  144.             fuelNeeded = 0
  145.         end
  146.        
  147.         print("Remaining fuel: "..fuel)
  148.         print("Area to dig: "..size)
  149.         print("Fuel needed: "..fuelNeeded)
  150.        
  151.         --- fuel = size + length if turtle finishes job away from starting point
  152.  
  153.         if fuelNeeded > 0 then
  154.             print("Attempting to get enough fuel for the job...")
  155.             while fuel < trips and tSlot < 17 do
  156.                 turtle.select(tSlot)
  157.                
  158.                 if  turtle.refuel(1) == false then
  159.                     tSlot = tSlot + 1
  160.                 end
  161.                
  162.                 if tSlot == 17 then
  163.                     print ("Not enough accessible tFuel. Aborting!")
  164.                     return false
  165.                 end
  166.                
  167.                 if tSlot < 17 and fuelNeeded == 0 then
  168.                     print ("Done refueling. Commencing job!")
  169.                     turtle.select(2)
  170.                 end
  171.                
  172.                 fuel = turtle.getFuelLevel()
  173.                
  174.             end
  175.             print ("fuel: "..fuel)
  176.         end
  177.         return true
  178.     end
  179.  
  180. -- Temporary function to prevent getting stuck if there's a block above at start   
  181. local function checkSpace()
  182.     while turtle.detectUp() do
  183.         turtle.back()
  184.     end
  185. end
  186.  
  187. local function doJob()
  188.     while sRows ~= 0 do
  189.         digShaft(sLength, sHeight)
  190.         if sRows ~= 0 then
  191.             makeTurn(sHeight)
  192.         end
  193.     end
  194. end
  195.    
  196. -- Do the job ------------------------------------------------------
  197. --------------------------------------------------------------------
  198.  
  199. -- Debug stuff
  200. print ("sLength: "..sLength.." \\ sHeight: "..sHeight.." \\ sWidth: "..sWidth)
  201. print ("tFuel: "..tFuel.. " \\ aSize: "..aSize)
  202. print ("sDirection: "..sDirection.." \\ sRows: "..sRows)
  203.  
  204. -- actual actions
  205. if checkFuel(aSize) then
  206.     checkSpace()
  207.     doJob()
  208. end
Advertisement
Add Comment
Please, Sign In to add comment