Advertisement
IMarvinTPA

Roadie

Dec 31st, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.08 KB | None | 0 0
  1. --pastebin get JWrTxV1z MakeRoom
  2. --pastebin get SpEd82Jp StripLayer
  3. --pastebin get 7JMXTNu5 TallTunnel
  4. --pastebin get Yrsn4Wne Roadie
  5. --Roadie.lua 10 1
  6. --Roadie.lua 128
  7.  
  8. local tArgs = { ... }
  9.  
  10. if #tArgs >= 3 or #tArgs == 0 then
  11.     print( "Usage: Roadie <length> <follow terrain flag> <use anything flag>" )
  12.     print("<length> should be between 1 and 128 or positive number")
  13.     print("<follow terrain flag> (default: 0) 1: if there is an obstruction it will make the road one layer higher.")
  14.     print("   if there is air below the center it will make the road one layer lower.")
  15.     print("   0: the road will be flat and it will tunnel stright through.")
  16.     print("<use anything flag> (default: 0) 1: it will use what it collects to go farther than 128 and use slots 5-16 as building sources.")
  17.     print( "Row 1 should contain fuel (and empty slots)" )
  18.     print( "Row 2 should contain the road edge blocks" )
  19.     print( "Row 3 should contain the road inside blocks" )
  20.     print( "Row for should be inside, inside, empty, empty" )
  21.    
  22.     return
  23. end
  24.  
  25. local length = tonumber( tArgs[1] )
  26.  
  27. local followGround = tonumber( tArgs[2] )
  28. if followGround == nil then
  29.     followGround = 0
  30. end
  31.  
  32. local useAnything = tonumber( tArgs[3] )
  33. if useAnything == nil then
  34.     useAnything = 0
  35. end
  36. local tall = 2
  37.  
  38. --print (useAnything)
  39.  
  40. if useAnything == 0 and length > 128 then
  41.         print( "Road is longer than supplies." )
  42.         return
  43. end
  44.  
  45. if length < 1 then
  46.         print( "Road length must be positive" )
  47.         return
  48. end
  49.        
  50. local depth = 0
  51. local collected = 0
  52. local unloaded = 0
  53. local innerBlocks = {}
  54. local outerBlocks = {}
  55. local innerSource = 1
  56. local outerSource = 1
  57.  
  58. if useAnything == 0 then
  59.    for n=1,4 do
  60.             local nCount = turtle.getItemCount(n + 4)
  61.             outerBlocks[n] = nCount
  62.     end
  63.    for n=1,6 do
  64.             local nCount = turtle.getItemCount(n + 8)
  65.             innerBlocks[n] = nCount
  66.     end
  67. end
  68.  
  69. local function collect()
  70.     collected = collected + 1
  71.     if math.fmod(collected, 25) == 0 then
  72.             print( "Mined "..collected.." items." )
  73.     end
  74. end
  75.  
  76. local function isFull()
  77.     local bFull = true
  78.     local nTotalItems = 0
  79.     for n=1,16 do
  80.             local nCount = turtle.getItemCount(n)
  81.             if nCount == 0 then
  82.                     bFull = false
  83.             end
  84.             nTotalItems = nTotalItems + nCount
  85.     end
  86.          
  87.     if bFull then
  88.             print( "No empty slots left." )
  89.             return true
  90.     end
  91.     return false
  92. end
  93.  
  94. local function unload()
  95.     print( "Unloading items..." )
  96.     turtle.turnRight()
  97.     turtle.turnRight()
  98.     turtle.select(3)
  99.     turtle.place()
  100.     --1: Flooring
  101.     --2: Fuel
  102.     --3: Chests
  103.     --4: Torches
  104.     for n=5,16 do
  105.             unloaded = unloaded + turtle.getItemCount(n)
  106.             turtle.select(n)
  107.             turtle.drop()
  108.     end
  109.     --collected = 0
  110.     turtle.select(1)
  111.     turtle.turnRight()
  112.     turtle.turnRight()     
  113. end
  114.  
  115.  
  116. local function tryDig()
  117.     while turtle.detect() do
  118.             if turtle.dig() then
  119.                     collect()
  120.                     --sleep(0.5)
  121.             else
  122.                     return false
  123.             end
  124.     end
  125.     return true
  126. end
  127.  
  128. local function tryDigUp()
  129.     while turtle.detectUp() do
  130.             if turtle.digUp() then
  131.                     collect()
  132.                     --sleep(0.5)
  133.             else
  134.                     return false
  135.             end
  136.     end
  137.     return true
  138. end
  139.  
  140. local function tryDigDown()
  141.     while turtle.detectDown() do
  142.             if turtle.digDown() then
  143.                     collect()
  144.                     --sleep(0.5)
  145.             else
  146.                     return false
  147.             end
  148.     end
  149.     return true
  150. end
  151.  
  152. local function refuel()
  153.     local fuelLevel = turtle.getFuelLevel()
  154.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  155.             return
  156.     end
  157.    
  158.     local function tryRefuel()
  159.             for n=1,16 do
  160.                 if turtle.getItemCount(n) > 0 then
  161.                     turtle.select(n)
  162.                     if turtle.refuel(1) then
  163.                             turtle.select(1)
  164.                             return true
  165.                     end
  166.                 end
  167.             end
  168.             turtle.select(1)
  169.             return false
  170.     end
  171.    
  172.     if not tryRefuel() then
  173.             print( "Add more fuel to continue." )
  174.             while not tryRefuel() do
  175.                     sleep(1)
  176.             end
  177.             print( "Resuming Road." )
  178.     end
  179. end
  180.  
  181. local function tryUp()
  182.     refuel()
  183.     while not turtle.up() do
  184.             if turtle.detectUp() then
  185.                     if not tryDigUp() then
  186.                             return false
  187.                     end
  188.             elseif turtle.attackUp() then
  189.                     collect()
  190.             else
  191.                     sleep( 0.1 )
  192.             end
  193.     end
  194.     return true
  195. end
  196.  
  197. local function tryDown()
  198.     refuel()
  199.     while not turtle.down() do
  200.             if turtle.detectDown() then
  201.                     if not tryDigDown() then
  202.                             return false
  203.                     end
  204.             elseif turtle.attackDown() then
  205.                     collect()
  206.             else
  207.                     sleep( 0.1 )
  208.             end
  209.     end
  210.     return true
  211. end
  212.  
  213. local function tryForward()
  214.     refuel()
  215.     --if isFull() then
  216.     --  unload()
  217.     --end
  218.     while not turtle.forward() do
  219.             if turtle.detect() then
  220.                     if not tryDig() then
  221.                             return false
  222.                     end
  223.             elseif turtle.attack() then
  224.                     collect()
  225.             else
  226.                     sleep( 0.1 )
  227.             end
  228.     end
  229.     return true
  230. end
  231.  
  232. local function placeAny()
  233.     for n=5,16 do
  234.         if turtle.getItemCount(n) > 0 then
  235.             turtle.select(n)
  236.             if turtle.placeDown() then
  237.                     turtle.select(1)
  238.                     return true
  239.             end
  240.         end
  241.     end
  242.     turtle.select(1)
  243.     return false
  244. end
  245.  
  246. local function placeOuter()
  247.     local cnt = 0
  248.     while not turtle.detectDown() do
  249.         cnt = 0
  250.         for n=1,4 do
  251.             cnt = cnt + outerBlocks[n]
  252.             if outerBlocks[n] > 0 then
  253.                 turtle.select(n + 4)
  254.                 if turtle.placeDown() then
  255.                     outerBlocks[n] = outerBlocks[n] - 1
  256.                     turtle.select(1)
  257.                     return true
  258.                 end
  259.             end
  260.         end
  261.         turtle.select(1)
  262.         if cnt == 0 then
  263.             return false
  264.         end
  265.     end
  266. end
  267.  
  268. local function placeInner()
  269.     local cnt = 0
  270.     while not turtle.detectDown() do
  271.         cnt = 0
  272.         for n=1,6 do
  273.             cnt = cnt + innerBlocks[n]
  274.             if innerBlocks[n] > 0 then
  275.                 turtle.select(n + 8)
  276.                 if turtle.placeDown() then
  277.                     innerBlocks[n] = innerBlocks[n] - 1
  278.                     turtle.select(1)
  279.                     return true
  280.                 end
  281.             end
  282.         end
  283.         turtle.select(1)
  284.         if cnt == 0 then
  285.             return false
  286.         end
  287.     end
  288. end
  289.  
  290. local function turnDir(dir)
  291.     if dir == 1 then
  292.         turtle.turnRight()
  293.     else
  294.         turtle.turnLeft()
  295.     end
  296. end
  297.  
  298. local function terrainTest(dir)
  299.     turnDir(dir)
  300.     --if there is dirt next to the road, we need to go up.
  301.     if turtle.detect() then
  302.         turnDir(-1 * dir)
  303.         return 1       
  304.     end
  305.     if not tryDown() then
  306.         turnDir(-1 * dir)
  307.         return 0
  308.     end
  309.     --If there is air below the road, we need to go down.
  310.     if not turtle.detect() then
  311.         turnDir(-1 * dir)
  312.         tryUp()
  313.         return -1      
  314.     end
  315.     --This should really always work.
  316.     turnDir(-1 * dir)
  317.     tryUp()
  318.     return 0
  319. end
  320.  
  321. local terrainMemory = 0
  322. local function hallway(hallLength)
  323.     for n=1,hallLength do
  324.        
  325.         tryDigDown()
  326.         if followGround ~= 0 and n == math.floor((hallLength + 1) / 2) then
  327.             terrainMemory = terrainTest(dir)
  328.         end
  329.         if useAnything ~= 0 then
  330.             --print("Placing Any")
  331.             placeTest = placeAny()
  332.         elseif n == 1 or n == hallLength then
  333.             --print("Placing Outer")
  334.             placeTest = placeOuter()
  335.         else
  336.             --print("Placing Inner")
  337.             placeTest = placeInner()
  338.         end
  339.         if placeTest == false then
  340.             print("Ran out of supplies!")
  341.             return false
  342.         end
  343.        
  344.         tryDigUp()
  345.            
  346.         if n<hallLength then
  347.             tryDig()
  348.             if not tryForward() then
  349.                 print("Unable to move forward.")
  350.                 return false
  351.             end
  352.         else
  353.             return true
  354.         end
  355.     end
  356. end
  357.  
  358. local function road(roadLength)
  359.     turtle.turnRight()
  360.     tryForward()
  361.     tryForward()
  362.     turtle.turnRight()
  363.     turtle.turnRight()
  364.     dir = 1
  365.     for n=1,roadLength do
  366.         hallTest = hallway(5)
  367.         if not hallTest then
  368.             return false
  369.         end
  370.         if n < roadLength then
  371.             if dir == 1 then
  372.                 turtle.turnRight()
  373.                 stepTest = tryForward()
  374.                 turtle.turnRight()
  375.             else
  376.                 turtle.turnLeft()
  377.                 stepTest = tryForward()
  378.                 turtle.turnLeft()
  379.             end
  380.             dir = -1 * dir
  381.             if terrainMemory > 0 then
  382.                 stepTest = stepTest and tryUp()
  383.             elseif terrainMemory < 0 then
  384.                 stepTest = stepTest and tryDown()
  385.             end
  386.             terrainMemory = 0
  387.             if not stepTest then
  388.                 print("Unable to move forward.")
  389.                 return false
  390.             end
  391.         end
  392.     end --loop
  393.     turtle.turnRight()
  394.     turtle.turnRight()
  395.     tryForward()
  396.     tryForward()
  397.     if dir ~= 1 then
  398.         turtle.turnRight()
  399.     else
  400.         turtle.turnLeft()
  401.     end
  402.    
  403.     return true
  404. end  --road
  405.  
  406.  
  407. print( "Roading..." )
  408.  
  409.  
  410. if road(length) then
  411.     print( "Road complete." )
  412. else
  413.     print( "Road is not complete!")
  414. end
  415. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement