Fizzgig

surface

Jan 5th, 2013
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.33 KB | None | 0 0
  1. --Surface Quarry v1.0
  2.  --By: Fizzgig
  3.  
  4.  --Tested with: FTB Mod pack - Direwolf20 v3
  5.  
  6.  --Purpose: A turtle program designed to dig and collect blocks from the surface to a specified depth.
  7.  -- Useful for collecting sand from the desert and keeping the terrain generally "untouched".
  8.  -- No ugly quarry holes!
  9.  
  10.  --Usage: surface <Depth> <Length> <Width> [Optional <'left'(Default='right')> <Offset>]
  11.  -- Recommended depths 1 or 2. Greater than 3 is not recommended.
  12.  -- Fuel slots 1 and 2. Split stack or 2 stacks (Put fuel in each slot)
  13.  -- Placement - Turtle ontop of a chest.
  14.  
  15. --Arguments
  16.     local args = {...} --depth length width [Optional <'left'> <Offset>]
  17.     if #args < 3 then
  18.     print("Usage: surface <Depth> <Length> <Width> [Optional <'left'(Default='right')> <Offset>]")
  19.     return
  20.     end
  21.  
  22.     if tonumber(args[1]) < 1 then args[1] = "1" end
  23.     if tonumber(args[2]) < 2 then args[2] = "2" end
  24.     if tonumber(args[3]) < 2 then args[3] = "2" end
  25.  
  26.     local depth = tonumber(args[1])
  27.     local length = tonumber(args[2])
  28.     local width = tonumber(args[3])
  29.    
  30.     local quarry = "right"
  31.     if #args >= 4 then
  32.     if args[4] == "left" or args[4] == "Left" or args[4] == "LEFT" then quarry = "left" end
  33.     end
  34.  
  35.     local offset = 0
  36.     if #args == 5 then
  37.     if tonumber(args[5]) < 0 then args[5] = "0" end
  38.     offset = tonumber(args[5])
  39.     end
  40.    
  41. --Declarations
  42.     local xpos = -2
  43.     local ypos = 0
  44.     local zpos = 1
  45.     local dir = 1
  46.     local safe = false
  47.  
  48.    
  49. --Functions
  50.     function organize(num)
  51.             --Sort the inv
  52.             for i=num,16 do
  53.                 turtle.select(i)
  54.                 if turtle.getItemCount(i) > 0 then --same as below. that is intended
  55.                     for j=1,4 do
  56.                         if turtle.getItemCount(i) > 0 then --double condition is intended
  57.                             if turtle.getItemSpace(j)>turtle.getItemCount(i) then
  58.                                 turtle.transferTo(j,turtle.getItemCount(i))
  59.                                 else
  60.                                 turtle.transferTo(j,turtle.getItemSpace(j))
  61.                             end
  62.                         end
  63.                     end
  64.                 end
  65.             end
  66.            
  67.             local athome = (xpos == -2 and ypos == 0 and zpos == 1)
  68.            
  69.             --Drop to chest
  70.             if athome then
  71.                 for i=num,16 do
  72.                     turtle.select(i)
  73.                     if turtle.getItemCount(i) > 0 then
  74.                     turtle.dropDown(turtle.getItemCount(i))
  75.                     end
  76.                 end
  77.             end
  78.         end
  79.        
  80.     function setdir(ndir)
  81.             if ndir == 4 and dir == 1 then
  82.                 turtle.turnLeft()
  83.                 dir = 4
  84.             end
  85.            
  86.             if ndir == 1 and dir == 4 then
  87.                 turtle.turnRight()
  88.                 dir = 1
  89.             end
  90.  
  91.             if ndir == 4 and dir == 2 then
  92.                 turtle.turnLeft()
  93.                 turtle.turnLeft()
  94.                 dir = 4
  95.             end
  96.  
  97.             if ndir == 2 and dir == 4 then
  98.                 turtle.turnRight()
  99.                 turtle.turnRight()
  100.                 dir= 2
  101.             end
  102.  
  103.             if ndir > dir then
  104.                 while dir ~= ndir do
  105.                     turtle.turnRight()
  106.                     dir = dir+1
  107.                 end
  108.             end
  109.  
  110.             if ndir < dir then
  111.                 while dir ~= ndir do
  112.                     turtle.turnLeft()
  113.                     dir = dir-1
  114.                 end
  115.             end
  116.         end
  117.     function forward(num)
  118.             if num == nil then num=1 end
  119.             local modifyer
  120.             if dir <= 2 then modifyer = 1 else modifyer = -1 end
  121.  
  122.             if dir == 1 or dir == 3 then
  123.                 while num > 0 do
  124.                     if turtle.forward() then
  125.                         xpos=xpos+modifyer
  126.                         num = num-1
  127.                     elseif safe then
  128.                         if turtle.getFuelLevel() == 0 then fuel(1,2)
  129.                         elseif turtle.up() then zpos=zpos+1
  130.                         elseif turtle.down() then zpos=zpos-1 end
  131.                     else
  132.                         turtle.dig()
  133.                         fuel(1,2)
  134.                     end
  135.                 end
  136.             end
  137.  
  138.             if dir == 2 or dir == 4 then
  139.                 while num > 0 do
  140.                     if turtle.forward() then
  141.                         ypos=ypos+modifyer
  142.                         num = num-1
  143.                     elseif safe then
  144.                         if turtle.getFuelLevel() == 0 then fuel(1,2)
  145.                         elseif turtle.up() then zpos=zpos+1
  146.                         elseif turtle.down() then zpos=zpos-1 end
  147.                     else
  148.                         turtle.dig()
  149.                         fuel(1,2)
  150.                     end
  151.                 end
  152.             end
  153.         end
  154.     function up(num)
  155.             if num == nil then num = 1 end
  156.             while num > 0 do
  157.                 if turtle.up() then
  158.                     zpos=zpos+1
  159.                     num = num-1
  160.                 else
  161.                     turtle.digUp()
  162.                     fuel(1,2)
  163.                 end
  164.             end
  165.         end
  166.     function down(num)
  167.             if num == nil then num = 1 end
  168.             while num > 0 do
  169.                 if turtle.down() then
  170.                     zpos=zpos-1
  171.                     num = num-1
  172.                 else
  173.                     turtle.digDown()
  174.                     fuel(1,2)
  175.                 end
  176.             end
  177.         end
  178.     function home()
  179.             safe = true
  180.            
  181.             go(0,ypos)
  182.             go(xpos,0)
  183.             if zpos > 1 then down(zpos-1) end
  184.             if zpos < 1 then up(1-zpos) end
  185.             go(-2,0)
  186.             setdir(1)
  187.            
  188.             safe = false
  189.            
  190.         end
  191.     function start()
  192.             go(0,ypos)
  193.         end
  194.     function go(x,y)
  195.             if y ~= ypos then
  196.                     if y < ypos then
  197.                             setdir(4)
  198.                             forward(ypos-y)
  199.                         end
  200.                     if y > ypos then
  201.                             setdir(2)
  202.                             forward(y-ypos)
  203.                         end
  204.                 end
  205.             if x ~= xpos then
  206.                     if x < xpos then
  207.                             setdir(3)
  208.                             forward(xpos-x)
  209.                         end
  210.                     if x > xpos then
  211.                             setdir(1)
  212.                             forward(x-xpos)
  213.                         end
  214.                 end
  215.         end
  216.     function fuel(num1,num2)
  217.             if num1 == nil then num1 = 1 end
  218.             if num2 == nil then num2 = num1 end
  219.            
  220.             if turtle.getFuelLevel() == 0 then
  221.                 while turtle.getFuelLevel() == 0 do
  222.                     if turtle.getItemCount(num1)-1 > 0 then
  223.                         turtle.select(num1)
  224.                         turtle.refuel(1)
  225.                     elseif turtle.getItemCount(num2)-1 > 0 then
  226.                         turtle.select(num2)
  227.                         turtle.refuel(1)
  228.                     end
  229.  
  230.                     if turtle.getFuelLevel() == 0 then
  231.                         term.clear()
  232.                         term.setCursorPos(1,1)
  233.                         if num1 ~= num2 then print("Waiting for fuel - Slot "..num1.." or "..num2.."...")
  234.                         else print("Waiting for fuel - Slot "..num1.."...") end
  235.                     end
  236.                    
  237.                     sleep(0.05)
  238.                 end
  239.                 term.clear()
  240.                 term.setCursorPos(1,1)
  241.             end
  242.         end
  243.  
  244. --Main
  245.  
  246.     start()
  247.     if quarry == "left" then go(xpos,ypos-(offset+1)) else go(xpos,ypos+offset) end
  248.     setdir(1)
  249.  
  250.  
  251.     for i=1,width-offset do
  252.         for j=1,length do
  253.             while turtle.detectDown() == false do down() end
  254.            
  255.             if depth > 1 then down(depth-1) end
  256.             turtle.digDown()
  257.            
  258.             while turtle.detect() do up() end
  259.  
  260.             if j ~= length then forward() end
  261.         end
  262.  
  263.         if i ~= width-offset then
  264.  
  265.             while turtle.detect() do up() end
  266.            
  267.             if xpos==0 then
  268.                 xstore=xpos
  269.                 ystore= ypos
  270.                 zstore=zpos
  271.  
  272.                 home()
  273.                 organize(3)
  274.  
  275.                 start()
  276.                
  277.                 safe = true
  278.                
  279.                 go(xstore,ypos)
  280.                 go(xpos,ystore)
  281.                 if zstore > zpos then up(zstore-zpos) end
  282.                 if zstore < zpos then down(zpos-zstore) end
  283.                
  284.                 safe = false
  285.             end
  286.            
  287.             if quarry == "left" then setdir(4) else setdir(2) end
  288.             forward()
  289.             if xpos==0 then setdir(1) else setdir(3) end
  290.         end
  291.     end
  292.  
  293.     home()
  294.     organize(3)
Advertisement
Add Comment
Please, Sign In to add comment