Posted 21 March 2013 - 03:20 PM
Title: Buggy Mining/Printing code.
So, I’m completely new to code and have been working on a code to mine the ‘length and width’ chosen; however, it seems to be full of bugs. I’ve come to the point where I’m just scratching my head wondering why it’s not working. If I could get some help on this and any pointers you feel like sharing that would be great.
The bugs that I know about are:
o Printing amount of blocks stays at “Mined 1 block” when I was trying to get it to count and print the total blocks mined to that point.
o When mining a straight line turtle continues forever. (I believe this is because finish and length aren’t being compared correctly)
o When mining a length of 3 and width of 3 the turtle goes in a square like this:
>>>>
^__ v ← Start, forward being down.
<<<<
It's supposed to look like this:
__v ← Start, forward being down.
v<<
>>v
v<< ← And then come back to the starting position.
Anyway, here’s the code:
Again, any help or guidance would be much appreciated.
So, I’m completely new to code and have been working on a code to mine the ‘length and width’ chosen; however, it seems to be full of bugs. I’ve come to the point where I’m just scratching my head wondering why it’s not working. If I could get some help on this and any pointers you feel like sharing that would be great.
The bugs that I know about are:
o Printing amount of blocks stays at “Mined 1 block” when I was trying to get it to count and print the total blocks mined to that point.
o When mining a straight line turtle continues forever. (I believe this is because finish and length aren’t being compared correctly)
o When mining a length of 3 and width of 3 the turtle goes in a square like this:
>>>>
^__ v ← Start, forward being down.
<<<<
It's supposed to look like this:
__v ← Start, forward being down.
v<<
>>v
v<< ← And then come back to the starting position.
Anyway, here’s the code:
--Declare locals.
local tArgs = {...}
local length = tArgs[1]
local width = tArgs[2]
width = width - 1
local face = 1
local blocks = 0
local finish = 0
--Create functions.
function left(face)
if turtle then turtle.turnLeft() end
face = face - 1
if face < 1 then face = face + 4 end
return face
end
function right(face)
if turtle then turtle.turnRight() end
face = face + 1
if face > 4 then face = face - 4 end
return face
end
function addB(blocks)
blocks = blocks + 1
if blocks > 1 then
print (‘Mined ‘..blocks..’ blocks.’)
end
if blocks == 1 then
print (‘Mined ‘..blocks..’ block.’)
end
return blocks
end
function digWidth(width)
For a = 1, width do
while turtle.detect() == true do
turtle.dig()
addB(blocks)
sleep(0.5)
end
turtle.forward()
while turtle.detectUp() == true do
turtle.digUp()
addB(blocks)
sleep(0.5)
end
while turtle.detectDown() == true do
turtle.digDown()
addB(blocks)
sleep(0.5)
end
end
end
function walkWidth(width)
For dNCT = 1, width do
turtle.forward()
end
end
--Execute code.
while finish ~= length do
if face == 1 then
while turtle.detect() == true do
turtle.dig()
addB(blocks)
sleep(0.5)
end
turtle.forward()
while turtle.detectUp() == true do
turtle.digUp()
addB(blocks)
sleep(0.5)
end
while turtle.detectDown() == true do
turtle.digDown()
addB(blocks)
sleep(0.5)
end
finish = finish + 1
if width > 0 then
right(face)
digWidth(width)
if finish == length then
left(face)
left(face)
walkWidth(width)
right(face)
end
end
end
if finish ~= length then
if face == 2 then
left(face)
while turtle.detect() == true do
turtle.dig()
addB(blocks)
sleep(0.5)
end
turtle.forward()
while turtle.detectUp() == true do
turtle.digUp()
addB(blocks)
sleep(0.5)
end
while turtle.detectDown() == true do
turtle.digDown()
addB(blocks)
sleep(0.5)
end
finish = finish + 1
left(face)
digWidth(width)
if finish == length then
right(face)
end
end
end
if finish ~= length then
if face == 4 then
right(face)
while turtle.detect() == true do
turtle.dig()
addB(blocks)
sleep(0.5)
end
turtle.forward()
while turtle.detectUp() == true do
turtle.digUp()
addB(blocks)
sleep(0.5)
end
while turtle.detectDown() == true do
turtle.digDown()
addB(blocks)
sleep(0.5)
end
finish = finish + 1
right(face)
digWidth()
if finish == length then
left(face)
left(face)
walkWidth(width)
right(face)
end
end
end
end
Again, any help or guidance would be much appreciated.