
Untitled
By: a guest on
Jul 20th, 2012 | syntax:
Lua | size: 2.26 KB | hits: 13 | expires: Never
local tArgs = { ... }
if #tArgs ~= 1 then
print( "Usage: stairs <deepth>" )
return
end
local deepth = tArgs[1];
-- left: 1
-- forward: 2
-- right: 3
local direction = 2;
local xPos = -1;
local yPos = 0;
local zPos = 0;
local function turnLeft()
if (direction == 1) then
return false
end
turtle.turnLeft();
direction = direction - 1;
return true;
end
local function turnRight()
if (direction == 3) then
return false
end
turtle.turnRight();
direction = direction + 1;
return true;
end
local function perforate()
local result = turtle.dig();
turtle.drop();
os.sleep(0.8);
return result;
end
local function perforateDown()
local result = turtle.digDown();
turtle.drop();
return result;
end
local function perforateUp()
local result = turtle.digUp();
turtle.drop();
return result;
end
local function advance()
if (turtle.detect()) then
if (not perforate()) then
return false;
end
end
turtle.forward();
if (direction == 2) then
xPos = xPos + 1;
elseif (direction == 1) then
yPos = yPos - 1;
elseif (direction == 3) then
yPos = yPos + 1;
end
return true;
end
local function advanceUp()
if (turtle.detectUp()) then
if (not perforateUp()) then
return false;
end
end
turtle.up();
zPos = zPos + 1;
return true;
end
local function advanceDown()
if (turtle.detectDown()) then
if (not perforateDown()) then
return false;
end
end
turtle.down();
zPos = zPos - 1;
return true;
end
local isDown = true;
local done = false;
turnRight();
local movement = {}
movement[0] = advance;
movement[1] = advanceUp;
movement[2] = advanceDown;
movement[3] = turnLeft;
movement[4] = turnRight;
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};
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};
while (not done) do
local block;
if (isDown) then
block = blockUp;
else
block = blockDown;
end
for n = 1,#block do
local mov = block[n];
if (not movement[mov]()) then
print("failed at ",n,isDown);
done = true;
break;
end
end
isDown = not isDown;
deepth = deepth - 1;
if (deepth == 0) then
break;
end
end
print("Ended with no errors!");