Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: Lua  |  size: 2.26 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. local tArgs = { ... }
  2. if #tArgs ~= 1 then
  3.         print( "Usage: stairs <deepth>" )
  4.         return
  5. end
  6.  
  7. local deepth = tArgs[1];
  8.  
  9. -- left: 1
  10. -- forward: 2
  11. -- right: 3
  12.  
  13. local direction = 2;
  14. local xPos = -1;
  15. local yPos = 0;
  16. local zPos = 0;
  17.  
  18. local function turnLeft()
  19.         if (direction == 1) then
  20.                 return false
  21.         end
  22.         turtle.turnLeft();
  23.         direction = direction - 1;     
  24.         return true;
  25. end
  26.  
  27. local function turnRight()
  28.         if (direction == 3) then
  29.                 return false
  30.         end
  31.         turtle.turnRight();
  32.         direction = direction + 1;
  33.         return true;
  34. end
  35.  
  36. local function perforate()
  37.         local result = turtle.dig();
  38.         turtle.drop();
  39.         os.sleep(0.8);
  40.         return result;
  41. end
  42.  
  43. local function perforateDown()
  44.         local result = turtle.digDown();
  45.         turtle.drop();
  46.         return result;
  47. end
  48.  
  49. local function perforateUp()
  50.         local result = turtle.digUp();
  51.         turtle.drop();
  52.         return result;
  53. end
  54.  
  55.  
  56. local function advance()
  57.         if (turtle.detect()) then
  58.                 if (not perforate()) then
  59.                         return false;
  60.                 end            
  61.         end
  62.         turtle.forward();
  63.         if (direction == 2) then
  64.                 xPos = xPos + 1;
  65.         elseif (direction == 1) then
  66.                 yPos = yPos - 1;
  67.         elseif (direction == 3) then
  68.                 yPos = yPos + 1;
  69.         end            
  70.         return true;
  71. end
  72.  
  73. local function advanceUp()
  74.         if (turtle.detectUp()) then
  75.                 if (not perforateUp()) then
  76.                         return false;
  77.                 end            
  78.         end
  79.  
  80.         turtle.up();
  81.         zPos = zPos + 1;
  82.         return true;
  83. end
  84.  
  85. local function advanceDown()
  86.         if (turtle.detectDown()) then
  87.                 if (not perforateDown()) then
  88.                         return false;
  89.                 end            
  90.         end
  91.  
  92.         turtle.down();
  93.         zPos = zPos - 1;
  94.         return true;
  95. end
  96.  
  97. local isDown = true;
  98. local done     = false;
  99.  
  100. turnRight();
  101.  
  102.  
  103. local movement = {}
  104. movement[0] = advance;
  105. movement[1] = advanceUp;
  106. movement[2] = advanceDown;
  107. movement[3] = turnLeft;
  108. movement[4] = turnRight;
  109.  
  110. local blockUp = {3,0,2,3,0,0,0,4,4,1,0,0,0,3,3,1,0,0,0,4,4,1,0,0,0};
  111. local blockDown = {3,2,0,3,0,0,0,2,4,4,0,0,0,2,3,3,0,0,0,2,4,4,0,0,0};
  112.  
  113. while (not done) do
  114.         local block;
  115.         if (isDown) then
  116.                 block = blockUp;
  117.         else
  118.                 block = blockDown;
  119.         end
  120.        
  121.         for n = 1,#block do
  122.                 local mov = block[n];
  123.                 if (not movement[mov]()) then
  124.                         print("failed at ",n,isDown);
  125.                         done = true;
  126.                         break;
  127.                 end
  128.         end
  129.         isDown = not isDown;
  130.         deepth = deepth - 1;
  131.         if (deepth == 0) then
  132.                 break;
  133.         end
  134. end
  135.  
  136. print("Ended with no errors!");