Posted 06 January 2013 - 07:42 PM
Hello I'm on my 5th day of coding ever so please be kind, any advice is welcome. Anyways I'm trying to
program a turtle to mine "d" block lengths, return, and then move to the rigth "b" amount of branches reapting the dig length each time. Everything runs fine until I reach the function that I named nextbranch(), the problem is that the turtle will move one extra branch over and then stop is there a way I can stop this? I've tried to put if thens, if then elseif's, for do, while do and I just can't seem to get to not move one extra branch over.
As an example I would input d = 5, b = 3 so it would mine 5 blocks deep and 3 branches it works just fine except on the final 3rd branch it moves one more branch over but doesn't mine it.
program a turtle to mine "d" block lengths, return, and then move to the rigth "b" amount of branches reapting the dig length each time. Everything runs fine until I reach the function that I named nextbranch(), the problem is that the turtle will move one extra branch over and then stop is there a way I can stop this? I've tried to put if thens, if then elseif's, for do, while do and I just can't seem to get to not move one extra branch over.
As an example I would input d = 5, b = 3 so it would mine 5 blocks deep and 3 branches it works just fine except on the final 3rd branch it moves one more branch over but doesn't mine it.
term.clear()
term.setCursorPos(1,1)
repeat
write("How deep should I dig? ")
d = tonumber(read(d))
while d < 1 do
write("Input must be a number, how deep to dig? ")
end
write("How many branches should I dig? ")
b = tonumber(read(B)/>/>/>/>)
while d <1 do
write("Input must be a number, how many branches to dig?")
end
print("Acknowledged will dig " ..d.. " blocks deep, and " ..b.. " branches.")
write("Is this correct? [y/n] ")
correct = io.read()
term.clear()
term.setCursorPos(1,1)
until correct == "y"
term.clear()
term.setCursorPos(1,1)
while true do
print("Please place torches in slot top left and cobblestone in top middle.")
print("Press any key when ready to begin.")
event = os.pullEvent() -- any key will continue process --
break
end
print("Beginning operations.")
--[[Start Functions]]--
function safefwd()
success = false
while not success do
success = turtle.forward()
if not success then
turtle.dig()
sleep(0.5)
end
end
end
function dig() -- begining in the middle of a 3x3 hallway it will dig 5 blocks into a wall and place a torch --
turtle.select(1)
move = d + 1
n = 0
for i = 1,move do
n=n+1
if not turtle.detectDown() then
turtle.select(2)
turtle.placeDown()
end
safefwd()
if not turtle.detectDown() then
turtle.select(2)
turtle.placeDown()
end
while turtle.detectUp() do
turtle.digUp()
sleep(0.5)
end
if n==6 then
turtle.turnLeft()
turtle.up()
turtle.dig()
turtle.select(1)
turtle.place()
turtle.down()
turtle.turnRight()
n=0
end
end
end
function done() -- turns around and returns to start --
move = d + 1
turtle.turnLeft()
turtle.turnLeft()
for i = 1,move do
safefwd()
end
end
function dropinv()
local firstItem,lastItem = 3,16 -- defines slots 3 through 16 on turtle --
for i = firstItem, lastItem do -- drops item into chest in middle of hallway below it --
turtle.select(i)
turtle.dropDown()
end
turtle.select(1)
end
function nextbranch() -- moves the turtle over to the next branch --
turtle.turnRight()
for i = 1,4 do
safefwd()
end
turtle.turnRight()
end
--[[End Functions]]--
m = 0
repeat
dig()
done()
dropinv()
if m==b then -- I'm having difficulties here with preventing one extra iteration of nextbranch()--
error()
nextbranch()
end
m = m + 1
until m == b