mehmattski

snakemine

Dec 14th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1. -- Dig the snake-like branch mine four times.
  2. -- Returns frequently to drop off supplies.
  3. -- Places torches reasonably when they are placed in slot 16.
  4.  
  5. function turnAround()
  6.     for i=1,2 do
  7.        turtle.turnLeft()
  8.     end
  9. end
  10.  
  11. function smartForward()
  12.     --Dig a 2 high, 1 wide tunnel; gravel and sand friendly.
  13.     while not turtle.forward() do
  14.         turtle.dig()
  15.     end
  16.     while turtle.detectUp() do
  17.         turtle.digUp()
  18.         sleep(0.5)    
  19.     end
  20. end
  21.  
  22. function snakeRight()
  23.     -- Make a right turn in the snake.
  24.     turtle.turnRight()
  25.     for i=1,3 do
  26.         smartForward()
  27.     end
  28.     turtle.select(16)
  29.     turtle.placeUp()
  30.     turtle.turnRight()
  31. end
  32.  
  33. function snakeLeft()
  34.     -- Make a left turn in the snake.
  35.     turtle.turnLeft()
  36.     for i=1,3 do
  37.         smartForward()
  38.     end
  39.     turtle.select(16)
  40.     turtle.placeUp()
  41.     sleep(0.25)
  42.     turtle.turnLeft()
  43. end
  44.  
  45. function snakeSegment(l)
  46.     -- Complete one segment of the snake of length l
  47.     -- Turtle starts facing the wall for the first run
  48.     for i=1,l do
  49.         smartForward()
  50.         if i % 10 == 0 then
  51.             torchPlace()
  52.         end
  53.     end
  54.     snakeRight()
  55.     for i=1,l*2 do
  56.         smartForward()
  57.         if i % 10 == 0 then
  58.             torchPlace()
  59.         end
  60.     end
  61.     snakeLeft()
  62.     for i=1,l do
  63.         smartForward()
  64.         if i % 10 == 0 then
  65.             torchPlace()
  66.         end
  67.     end
  68. end
  69.  
  70. function torchPlace()
  71.     turtle.select(16)
  72.     turtle.turnRight()
  73.     sleep(0.25)
  74.     turtle.placeUp()
  75.     turtle.turnLeft()
  76. end
  77.  
  78.  
  79. function goHome(l)
  80.     -- After a segment of length l, return to base
  81.     -- Drop off items
  82.     turtle.turnLeft()
  83.     for i=1,l+2 do
  84.         smartForward()
  85.     end
  86.     for j = 1,15 do
  87.         turtle.select(j)
  88.         turtle.drop()
  89.     end
  90. end
  91.  
  92. function resume(l)
  93.     for i =1,l+2 do
  94.         turtle.forward()
  95.     end
  96.     turtle.turnLeft()
  97. end
  98.  
  99. local args = {...}
  100. if table.getn(args) < 1 then
  101.     print("Usage: snakemine maxlength")
  102.     print("maxlength is the length of the largest side branch. Must be multiple of 6")
  103.     print("Make sure torches are in slot 16!")
  104.     return
  105. end
  106. -- first arg is the maximum length of a tunnel off of
  107. local maxLength = tonumber(args[1])
  108. if maxLength % 6 ~= 0 then
  109.     print("maxLength of " .. maxLength .. " is not a multiple of 6! Try again!")
  110.     return
  111. end
  112.  
  113. for quad = 1,4 do
  114.     for i = 1,3 do smartForward() end
  115.     turtle.turnLeft()
  116.     for seg = 6,maxLength,6 do
  117.         snakeSegment(seg)
  118.         goHome(seg)
  119.         if seg < maxLength then
  120.             turnAround()
  121.             resume(seg)
  122.         else
  123.             for i=1,4 do
  124.                 turtle.forward()
  125.             end
  126.             turtle.turnLeft()
  127.         end    
  128.     end
  129.  
  130. end
Advertisement
Add Comment
Please, Sign In to add comment