6 posts
Location
Pocatello, ID
Posted 18 July 2012 - 03:00 AM
I'm working on a lua script that will enable my turtle to know how many times it has done a certain function.
My program is to clear, or flatten a small piece of land (20x20).
I've built the following setup of code.
while turtle.detectDown() do
if turtle.detect() then
turtle.dig() else
turtle.forward(1)
end
end
I was hoping to insert a loop with a counting function before the first end.
Something to the effect of :
if turtle.forward(1) then
"#oftimes turtle.forward(1)" = nblocks
if nblocks==20 then
turtle.turnLeft()
(( In here, I want to insert a loop to run the whole thing over again, if possible at all. ))
else turtle.forward(1)
end
else (( Can i put nothing in here? else print(nblocks) ))
end
end
end
I don't want anyone to write the whole thing themselves, just looking for help with the counting function ( if there is one), which counts the number of times I've used a certain function, and the loop in the second block of code after turtle.turnLeft().
Thanks in advance!
504 posts
Location
Seattle, WA
Posted 18 July 2012 - 04:44 AM
I'm not entirely sure I understand the problem completely, and or what you wish to achieve, however I will lend what help I can.
I suggest that after every time you call 'turtle.forward()' or 'turtle.dig()' you could have a variable holding the amount of times called. Once 20 is reached, the variable resets to 0.
Something like this:
turtie.forward(1); nTimesMoved = nTimesMoved+1
-- You can attach multiple statements to a single line if they are
-- followed by semi colon ';'
Hope I helped :P/>/>
8543 posts
Posted 18 July 2012 - 04:54 AM
This may or may not run right out of the box, but it should give you something to play around with and the comment should help you understand what's going on.
if turtle.detect() then turtle.dig() end
--enter the 20x20 square.
turtle.forward()
--repeat the row-pair-digging loop ten times (20 rows total)
for i=1,10 do
--we are in the first block, so we only need to go forward 19 times.
for i=1,19 do
--dig out the first row of the pair.
if turtle.detect() then turtle.dig() end
turtle.forward()
end
--turn and move forward to start the second row of the pair.
turtle.turnLeft()
if turtle.detect() then turtle.dig() end
turtle.forward()
turtle.turnLeft()
--we are one block into the second row of the pair, so we only need to go forward 19 times.
for i=1,19 do
if turtle.detect() then turtle.dig() end
turtle.forward()
end
turtle.turnRight()
--as long as we are not on the last row, move over to get into position for the next row pair.
if i < 10 then
if turtle.detect() then turtle.dig() end
turtle.forward()
turtle.turnRight()
end
end
6 posts
Location
Pocatello, ID
Posted 18 July 2012 - 06:17 AM
@PaymentOption
I see what you mean. Declare a var of nblocks at 0, and then add 1 every subsequent time, until it hits 20.
Thanks. I guess I was just confused or something.
@Lyqyd
Looks good, but I was wondering if it would be easier to do a spiral.
So forward 20, turn left, forward 19, turn left, forward 18, etc. until 1.
That's what I was thinking, but this gives me a good foothold. Thanks!
515 posts
Location
Australia
Posted 18 July 2012 - 02:45 PM
@PaymentOption
I see what you mean. Declare a var of nblocks at 0, and then add 1 every subsequent time, until it hits 20.
Thanks. I guess I was just confused or something.
@Lyqyd
Looks good, but I was wondering if it would be easier to do a spiral.
So forward 20, turn left, forward 19, turn left, forward 18, etc. until 1.
That's what I was thinking, but this gives me a good foothold. Thanks!
If you just want to repeat something 20 times, you can use a for loop
for i = 1, 20 do
-- what you want to do
end
Edit: @Lyqyd Whoops was on my mobile so I just skim read the stuff. Missed your post :-/
8543 posts
Posted 19 July 2012 - 01:28 AM
@PaymentOption
I see what you mean. Declare a var of nblocks at 0, and then add 1 every subsequent time, until it hits 20.
Thanks. I guess I was just confused or something.
@Lyqyd
Looks good, but I was wondering if it would be easier to do a spiral.
So forward 20, turn left, forward 19, turn left, forward 18, etc. until 1.
That's what I was thinking, but this gives me a good foothold. Thanks!
No problem. While a spiral may be the
same number of turns, I don't think that writing the loop for it would be easier at all. The row-pairs loop is quite straightforward and easily adapted to support varying dimensions.
@PaymentOption
I see what you mean. Declare a var of nblocks at 0, and then add 1 every subsequent time, until it hits 20.
Thanks. I guess I was just confused or something.
@Lyqyd
Looks good, but I was wondering if it would be easier to do a spiral.
So forward 20, turn left, forward 19, turn left, forward 18, etc. until 1.
That's what I was thinking, but this gives me a good foothold. Thanks!
If you just want to repeat something 20 times, you can use a for loop
for i = 1, 20 do
-- what you want to do
end
Yeah, I kind of already posted exactly that.