Posted 27 February 2013 - 05:33 PM
Title: [QUESTION] Turtle glitching mid-program.
I wrote my first bit of LUA for CC, I have some experience with C++ so it wasn't too rough but I seem to be having a weird run-time error that I can't seem to nail down, so hopefully someone here can help me. So the program digs what I believe is an efficient mining tunnel. It digs a main hallway of 3 high by 2 wide, with branches going out every 3rd row. Now it digs the first bit of hallway and first 2 branches fine but when it starts the 3rd branch it freaks. It spins around then digs up for some reason, I'm not sure why because its basically just looping what it's already done. it looks like it digs up once, then instead of digging down, it turns around twice, thinking its dug both branches, then starts the next hall.
Below is my code, thanks for the help in advance guys.
I wrote my first bit of LUA for CC, I have some experience with C++ so it wasn't too rough but I seem to be having a weird run-time error that I can't seem to nail down, so hopefully someone here can help me. So the program digs what I believe is an efficient mining tunnel. It digs a main hallway of 3 high by 2 wide, with branches going out every 3rd row. Now it digs the first bit of hallway and first 2 branches fine but when it starts the 3rd branch it freaks. It spins around then digs up for some reason, I'm not sure why because its basically just looping what it's already done. it looks like it digs up once, then instead of digging down, it turns around twice, thinking its dug both branches, then starts the next hall.
Below is my code, thanks for the help in advance guys.
-- Variables
turtle.select(1)
local tArgs = { ... }
distance = tonumber(tArgs[1])
branches = tonumber(tArgs[2])
distance = distance or 1
branches = branches or 5
local traveled = 0;
print("Work, work!")
-- Functions --
--Fuels the turtle
function tFuel(amount)
if turtle.getItemCount(1) > 1 then
if turtle.getFuelLevel() < 3 then
turtle.refuel(amount)
end
else
print "Out of fuel!"
return
end
end
--Turn around
function turnAround()
turtle.turnRight()
turtle.turnRight()
end
--Checks if it needs to dig, else it just moves
function tryDig(dir)
-- Forward
if dir == "F" then
if turtle.detect() then
turtle.dig()
sleep(0.25)
turtle.forward()
else
turtle.forward()
end
-- Up
else if dir == "U" then
if turtle.detectUp() then
turtle.digUp()
sleep(0.25)
turtle.up()
else
turtle.up()
end
-- Down
else if dir == "D" then
if turtle.detectDown() then
turtle.digDown()
sleep(0.25)
turtle.down()
else
turtle.down()
end
end
end
end
end
--Digs the hall between branches
function digBranch()
tryDig("F")
tryDig("U")
tryDig("U")
turtle.turnRight()
i = 0
dir = "D"
--Dig branch
while i < branches do
tryDig("F")
tryDig(dir)
tryDig(dir)
--Swap direction
if dir == "D" then
dir = "U"
else
dir = "D"
end
i = i + 1
sleep(0.25)
end
--Bring turtle to bottom
if dir == "D" then
turtle.down()
turtle.down()
dir = "U"
end
turnAround()
--Go back to main hall
i = 0
while i < branches do
tryDig("F")
i = i + 1
sleep(0.25)
end
i = 0
--Dig rest of hall and branch
while i < (branches + 1) do
tryDig("F")
tryDig(dir)
tryDig(dir)
--Swap direction
if dir == "D" then
dir = "U"
else
dir = "D"
end
i = i + 1
end
--Bring turtle to bottom
if dir == "D" then
turtle.down()
turtle.down()
dir = "U"
end
turnAround()
--Go back to main tunnel
i = 0
while i < branches do
turtle.forward()
i = i + 1
end
i = 0
--Prep and Turn to prep for hall dig
turtle.forward()
turtle.turnLeft()
end
--Digs the hall between branches
function digHall()
--Up, one forward
tryDig("F")
turtle.turnLeft()
tryDig("F")
turtle.turnRight()
tryDig("U")
turtle.turnRight()
tryDig("F")
turtle.turnLeft()
tryDig("U")
turtle.turnLeft()
tryDig("F")
turtle.turnRight()
--Down, one forward
tryDig("F")
turtle.turnRight()
tryDig("F")
turtle.turnLeft()
tryDig("D")
turtle.turnLeft()
tryDig("F")
turtle.turnRight()
tryDig("D")
turtle.turnRight()
tryDig("F")
turtle.turnLeft()
end
-- Starting Out --
if turtle.getItemCount(1) >= 1 then
for x = 0, distance do
tFuel(1)
digHall()
traveled = traveled + 2
x = x + 2
digBranch()
traveled = traveled + 1
x = x + 1
end
else
print "No fuel = no dig."
end