This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
okok99haha's profile picture

Turtle Ignore the last move in the loop

Started by okok99haha, 07 August 2013 - 07:47 PM
okok99haha #1
Posted 07 August 2013 - 09:47 PM
Title: Turtle Ignore the last move in the loop

So, I was making a program that would mine the certain area and I have problem that because of the loop, turtle goes one block further than it should go.
This is the code:
local tArgs = {...}

term.clear()
term.setCursorPos(1,1)
print("Digging:")
print("  "..tArgs[1].." blocks Down")
print("  "..tArgs[2].." blocks Forward")
print("  "..tArgs[3].." blocks Side")
term.setCursorPos(18, 9)
textutils.slowPrint("made by okok99haha")

function down()
for i = 1, tArgs[1] do
  while not turtle.down()
	do turtle.digDown()
end
end
end

function up()
for i = 1, tArgs[1] do
  while not turtle.up()
	do turtle.digUp()
end
end
end

function forward()
  while not turtle.forward()
	do turtle.dig()
end
end

function line()
  for aa = 1, tArgs[2] do
	down()
	up()
	forward()
end
end

function turn()
turtle.turnRight()
turtle.turnRight()
end

function back()
for ab = 1, tArgs[2] do
turtle.forward()
end
end

function moveLine()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
end

for i = 1, tArgs[3] do
  line()
  turn()
  back()
  moveLine()
end

term.setCursorPos(1,11)
print("Done!")
print("Print Any Key to Exit")
os.pullEvent("key")
term.clear()
term.setCursorPos(1,1)

I know where the problem is, it is the line function, but I couldn't find a way to solve it.
Simply, I want it to make ignore the last function, forward() on the last loop of the line function.
Please help!
Lyqyd #2
Posted 08 August 2013 - 08:24 PM
Split into new topic.

You could check if the current iteration is less than the maximum and only call the function if it is. A simple if statement around the function call in question will solve this nicely.
okok99haha #3
Posted 09 August 2013 - 10:23 PM
Split into new topic.

You could check if the current iteration is less than the maximum and only call the function if it is. A simple if statement around the function call in question will solve this nicely.

Could you please write me an example, I tried but failed.
This is what I tried:


function line()
for aa = 1, tArgs[2] do
down()
up()
if aa < tArgs[2] then
forward()
end
end
end



I get

cleararea:47: attempt to compare string with number expected, got string

where line 47 is if aa < tArgs[2] then
and I get this when it comes to the forward() function
reububble #4
Posted 10 August 2013 - 12:28 AM

function line()
  for aa = 2, tArgs[2] do
    down()
    up()
    forward()
  end
  down()
  up()
end

Should do the trick but only if args[2] is never less than 2. Your problem could be because when you use arguments they are entered in string form, so you might need to either change args[2] to a number by using
args[2] = tonumber(args[2])
or by using tonumber(args[2]) in the line you check it.
okok99haha #5
Posted 10 August 2013 - 01:29 AM

function line()
  for aa = 2, tArgs[2] do
	down()
	up()
	forward()
  end
  down()
  up()
end

Should do the trick but only if args[2] is never less than 2. Your problem could be because when you use arguments they are entered in string form, so you might need to either change args[2] to a number by using
args[2] = tonumber(args[2])
or by using tonumber(args[2]) in the line you check it.

Thanks, I used tArgs[2] = tonumber(tArgs[2]) (not sure if I did it right) and modified other parts and it works!
Another question, is there a way to make turtle to stop and until it gets fuel, and then continue the move?
I tried, but I couldn't find a way to do that.
Thanks!
reububble #6
Posted 10 August 2013 - 06:23 AM
There exists a function called turtle.getFuelLevel
Obviously, it returns the turtle's fuel level…
You could create a part of your turtle's routine to check this function.
A suitable snippet of code could be

while true do
  -- Lots of turtley things
  -- including digging
  -- and moving
  -- then running out of fuel
  if turtle.getFuelLevel() < 10 then
    turtle.select(whatever slot the coal should be in)
    turtle.refuel(1)
  end
end

Do be careful though, I used an if statement here, which means that it won't repeat the refueling process if it didn't work the first time.
That means that if there is no fuel in the slot, the turtle will continue to roll around in a paraplegic heap on the ground. No offense to any paraplegic persons, it's a serious condition.

That should be the most of it you need to know.
okok99haha #7
Posted 11 August 2013 - 02:43 AM
There exists a function called turtle.getFuelLevel Obviously, it returns the turtle's fuel level… You could create a part of your turtle's routine to check this function. A suitable snippet of code could be
 while true do -- Lots of turtley things -- including digging -- and moving -- then running out of fuel if turtle.getFuelLevel() < 10 then turtle.select(whatever slot the coal should be in) turtle.refuel(1) end end 
Do be careful though, I used an if statement here, which means that it won't repeat the refueling process if it didn't work the first time. That means that if there is no fuel in the slot, the turtle will continue to roll around in a paraplegic heap on the ground. No offense to any paraplegic persons, it's a serious condition. That should be the most of it you need to know.

Well, I want it to repeat the refuelling process if it fails.
Is there any way to do that?
reububble #8
Posted 11 August 2013 - 07:54 AM
Yes just change the if statement to a while statement.

while turtle.getFuelLevel() < 10 do
okok99haha #9
Posted 18 August 2013 - 03:14 AM
Yes just change the if statement to a while statement.

while turtle.getFuelLevel() < 10 do

Thanks!