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

Loop Repeats Itself

Started by Nicolay, 01 October 2013 - 08:01 PM
Nicolay #1
Posted 01 October 2013 - 10:01 PM
Hello, I am new to this forum and I recently have been working on a Tree Farm. I am not sure if I need to go into too much details on how it works, but the problems that I am having is after the turtle is supposed to have completed the loop (even though it's a while loop) it still doesn't wanna sleep, it's like the check() function is being called as soon as all the other functions are completed. I tried to make it print at the while loop at the button just to make a confirmation that the loop does not sleep.

And I dont actually know if there is a difference for "check()" or "return check()" if it actually has something to say…

Code removed due to the problem was fixed.
Lyqyd #2
Posted 02 October 2013 - 12:43 PM
Split into new topic.
Sora Firestorm #3
Posted 02 October 2013 - 07:51 PM
By placing return fw() in the fw() function, you're causing it to loop itself. Strip that out, it's not necessary. Come back once you've done that and run another test.
Nicolay #4
Posted 02 October 2013 - 08:04 PM
By placing return fw() in the fw() function, you're causing it to loop itself. Strip that out, it's not necessary. Come back once you've done that and run another test.

Thanks for your comment. Although that's not what the problem is. The turtle is doing the whole script perfectly, but if you look at the button of the script I tell it to sleep, it does not wanna do that. So therefore it seems like the the "function check()" is being repeated before can sleep.
campicus #5
Posted 02 October 2013 - 08:34 PM
Make sure to format your code, makes debugging lots easier. Also use [.spoiler] tags (without the ".")

I don't know why check would loop forever… what exactly happens? Does it ever print "checking done"?

Spoiler

local BlocksBetweenTree = 5 --"make your variables local"
local TreesInLine = 1			 --"as a convention, capitals are not used at the beggining"
local TreesTaken = 1
local Lines = 1
local LinesTaken = 1
local Side = "Left"
local Replant = 1
local Spotted = 0
local Cutting = 0


function fw()
  repeat  --"I prefer the repeat loop in this case, just personal preference"
	turtle.attack()
	sleep(0)
  until turtle.forward()
end

function sapling()
  turtle.turnRight()
  turtle.turnRight()
  for a = 1, 4 do
	fw()
  end
  Total = 0
  while Total < 128 do
	turtle.select(1)
	turtle.suck()
	Total = 0

	for slot = 1, 16 do
	  Total = Total + turtle.getItemCount(slot)
	end
  end
  turtle.turnLeft()
  turtle.turnLeft()
  for a = 1, 4 do
	fw()
  end
end

function place()
  turtle.select(1)
  if turtle.getItemCount(1) == 0 then
	turtle.select(2)
  end
  if turtle.getItemCount(2) == 0 then
	turtle.select(3)
  end
end

function check()
  Spotted = 0
  if turtle.detect() then
	turtle.digUp()
	turtle.up()
	Spotted = 1
	if turtle.detect() then
	  Spotted = 0
	  Cutting = 1
	return cut()
  end
else
  Spotted = 0
end
return next()
end

function cut()
  turtle.down()
  turtle.select(2)
  turtle.dig()
  fw()
  while turtle.detectUp() do
	turtle.digUp()
	turtle.up()
  end
  while not turtle.detectDown() do
	turtle.down()
  end
  return next()
end

function unload()
  for slot = 1, 16 do
	turtle.select(slot)
	turtle.drop()
  end
end

function next()
  if Cutting == 1 and Replant == 0 then
	fw()
  end
  if Cutting == 1 and Replant == 1 then
	turtle.back()
	place()
	turtle.place()
	turtle.up()
	fw()
	fw()
	turtle.down()
  end

  if Spotted == 0 and Replant == 1 and Cutting == 0 then
	place()
	turtle.place()
	turtle.up()
	fw()
	fw()
	turtle.down()
  end
  if Spotted == 0 and Replant == 0 and Cutting == 0 then
	fw()
	fw()
  end
  if Spotted == 1 and Cutting == 0 then
	fw()
	fw()
	turtle.down()
  end
  Spotted = 0
  Cutting = 0
  return go()
end

function go()
  if TreesTaken < TreesInLine then
	BlocksMoved = 1
	TreesTaken = TreesTaken + 1
	while BlocksMoved < BlocksBetweenTree do
	  fw()
	  BlocksMoved = BlocksMoved + 1
	end
  else
	rotate()
  end
  return check()
end

function rotate()
  turtle.down()
  if Side == "Left" then
	turtle.turnLeft()
	BlocksMoved = -1
	while BlocksMoved < BlocksBetweenTree do
	  fw()
	  BlocksMoved = BlocksMoved + 1
	  Side = "Right"
	end
	turtle.turnLeft()
	BlocksMoved = 1
	TreesTaken = 1
	return check()
  elseif Side == "Right" then
	if LinesTaken < Lines then
	  turtle.turnRight()
	  BlocksMoved = -1
	  while BlocksMoved < BlocksBetweenTree do
		fw()
		BlocksMoved = BlocksMoved + 1
		Side = "Left"
	  end
	  turtle.turnRight()
	  BlocksMoved = 1
	  TreesTaken = 1
	  LinesTaken = LinesTaken + 1
	  return check()
	else
	  return park()
	end
  end
end

function park()
  Moved = 0
  turtle.turnLeft()
  if Lines == 1 then
	ParkLines = Lines * 6
  else
	ParkLines = (Lines * 6) + (6*(Lines - 1))
  end
  while Moved < ParkLines do
	fw()
	Moved = Moved + 1
  end
  turtle.turnRight()
  for a = 1, 13 do
	fw()
  end
  turtle.turnRight()
  fw()
  fw()
  turtle.turnLeft()
  fw()
  unload()
  turtle.turnLeft()
  turtle.turnLeft()
  fw()
  turtle.turnRight()
  fw()
  fw()
  turtle.turnLeft()
  fw()
  fw()
  fw()
end

function prepear()
  for x = 1,10 do --"use the for loop here"
	fw()
  end
end

while true do
  sapling()
  prepear()
  print("OH")
  check()
  print("Checked done")
  sleep(300)
  print("Sleep")
end
Nicolay #6
Posted 02 October 2013 - 08:47 PM
Thanks again for your comment. Apparently for some reasons the format (the lines) where changed what I pasted it into the forums, probably I did something wrong. I tested your code and it still working exactly the same as it did as I had it before. The turtle is a tree farmer, it's going to cut the trees, and replant saplings if "Replant" is set to true, when it have done all the trees, it will go back to his originally position which is the function "Park", it goes goes through some chest to drop the items off and then goes to park.. That is where he is actually suppose to start sleeping, but instead he goes up one block, two blocks forward, one block down, turn left, and goes a few blocks forward and just messes around. Although for some reasons, even though it messes around, it's still able to do the same function park perfectly normal, drop item in chest (even though there's nothing), and park again… and then it does this over and over again. I have a feeling that when the function park is called, the script is some how continue running another function in the background, so when function park is done, the function in the background is continuing, if that's possible?

Thanks for your help.
campicus #7
Posted 02 October 2013 - 09:13 PM
All your functions make it really hard to tell what happens in what situation lol All I can think of is that your programs has a reoccurring loop in this sequence:

check() –> next() –> go() –> rotate() –> check() … and so on, forever.

Check this perhaps?
Nicolay #8
Posted 02 October 2013 - 09:54 PM
All your functions make it really hard to tell what happens in what situation lol All I can think of is that your programs has a reoccurring loop in this sequence:

check() –> next() –> go() –> rotate() –> check() … and so on, forever.

Check this perhaps?

The check function is to check if there is a tree there or just a sapling, and depending on the check it either cut the tree and replant sapling (if Replant is set to true), and if there is just a sapling there it will simply move to the next tree in the line. One line = Back and forth one time. When the turtle have checked all the trees in a line, it does the rotate function, meaning it goes to the next line (going back). If you want, you can join my server to take a look at how it actually works? Anyway, I am just thinking of aborting the script once it's parked, but I would really like to have this actually work as I want.
Lyqyd #9
Posted 02 October 2013 - 10:25 PM
No, he's got it exactly. After rotate calls park, and park does its thing and returns, the function that called rotate, which is go, tail-calls check.

You really need to re-structure this to not bounce around between your function so much. They aren't gotos, so please don't (ab)use them like gotos.
Nicolay #10
Posted 02 October 2013 - 10:38 PM
Thanks a lot. Now it seems like it's working very good again!