--Stairs Down Simple Script v1
--Turtle Program Computer Craft
--by SemlerPDX(July2016)
--COMMENT SYNTAX EXPLAINATION
--torches must be in slot 1 (one for every 3rd layer dug)
--stairs can be in slot 2 (equal to or greater than number of stairs requested)
--StairsDown 20 (number of stairs down -- will use stairs from slot 2)
--example: StairsDown 20 (makes stairs 20 deep w/ torches and stair blocks)
-- Variables --
local args = {...}
local d = args[1] or 0
local r = 0 + d
local t = turtle
local lit = 2
if d == 0 then
print("<error421: missing length>")
print("<ex: Stairs 20 (starts digging stairs 20 blocks deep with stair blocks in slot 2)>")
return
end
-- Functions --
local function ForceForward()
while not t.forward() do
t.dig()
end
end
--dig down, decrement lit variable
local function ForceDown()
t.digDown()
t.down()
lit = lit - 1
end
--shave 4 blocks and place torch if needed
local function Shave()
for v=1,4 do
if v == 3 then
if lit == 0 then
t.placeUp()
lit = lit + 2
ForceForward()
else
ForceForward()
end
else
ForceForward()
end
end
end
--move to next shaving position
local function ResetRun()
for v=1,3 do
t.back()
end
end
--place stairs while returning home
local function StairsHome()
t.turnLeft()
t.turnLeft()
t.select(2)
while r ~=0 do
t.place()
t.up()
t.forward()
r = r -1
end
end
--return home
local function ForceHome()
while r ~=0 do
t.up()
t.back()
r = r -1
end
end
--clear display screen
local function ClearScr()
term.clear()
term.setCursorPos(1,1)
end
-- Pre-fire scripts --
ClearScr()
print("Building staircase "..tostring(d).." blocks down...")
t.digDown()
t.down()
sleep(2)
---- Main Loop ----
while d ~= 0 do
local function ForceCancel()
local event, key = os.pullEvent("key")
end
local function Main()
Shave()
ResetRun()
ForceDown()
d = d - 1
end
FunctionEnabled = parallel.waitForAny(ForceCancel,Main)
if FunctionEnabled == 1 then
textutils.slowPrint("Cancelling Build...", 15)
sleep(1.65)
break
end
end
-- Evaluate Ending --
if FunctionEnabled == 1 then --(1 = functions cancelled, 2 = standard function looped)
ClearScr()
print("Build Cancelled by user")
print("...returning to start in 5 seconds - move out of path")
sleep(6)
ForceHome()
t.select(1)
else
ClearScr()
print("Build Completed")
print("Building stairs and returning home...")
t.up()
StairsHome()
t.select(1)
--ClearScr()
print("...program complete")
end
Comments