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

Need help with a sorting turtle program.

Started by DangerDave, 25 August 2013 - 03:31 AM
DangerDave #1
Posted 25 August 2013 - 05:31 AM
Title: Need help with a sorting turtle program.

So I've just gotten into this and I'm writing a program to have my turtles sort items. I know this code is basic and redundant, but I only know a few things, so this is what I've got:


function positiveDrop()
  turtle.turnLeft()
  turtle.drop()
  turtle.turnRight()
  itemWait()
  end

function negativeDrop()
  turtle.turnRight()
  turtle.turnRight()
  turtle.drop()
  turtle.turnLeft()
  turtle.turnLeft()
  itemWait()
  end

function itemCompare()
  if turtle.compareTo(2) then
  positiveDrop()
  elseif turtle.compareTo(3) then
  positiveDrop()
  elseif turtle.compareTo(4) then
  positiveDrop()
  elseif turtle.compareTo(5) then
  positiveDrop()
  elseif turtle.compareTo(6) then
  positiveDrop()
  elseif turtle.compareTo(7) then
  positiveDrop()
  elseif turtle.compareTo(8) then
  positiveDrop()
  elseif turtle.compareTo(9) then
  positiveDrop()
  elseif turtle.compareTo(10) then
  positiveDrop()
  elseif turtle.compareTo(11) then
  positiveDrop()
  elseif turtle.compareTo(12) then
  positiveDrop()
  elseif turtle.compareTo(13) then
  positiveDrop()
  elseif turtle.compareTo(14) then
  positiveDrop()
  elseif turtle.compareTo(15) then
  positiveDrop()
  elseif turtle.compareTo(16) then
  positiveDrop()
  elseif turtle.getItemCount(1) == 0 then
  itemWait()
  else negativeDrop()
  end
end

function itemWait()
  turtle.select(1)
  while turtle.getItemCount(1) <= 0 do
  itemCompare()
  end
end

itemWait()

Ok, so this robot is designed to accept an item from the robot in front of it (who has rejected it as one of his items) and either A) put it into the box to his left (positiveDrop) or B) hand it to the chest behind it (negativeDrop). I've managed to make this happen with the robot in front by using a loop almost identical to the while loop at the end only for sucking items out of chest rather than being handed to it. The problem I'm having is that for some reason on this turtle the loop won't run forever. So long as the turtle in front is actively handing it items then it works as intended, but if it sits for more than a second or two the program ends. There's no error message, it just ends as if the program completed successfully. What am I missing?

Thanks in advance!
(Also, I know this should be in pastebin or something rather then just Copy/Pasted, but I haven't found how to do that yet.)

In this image the turtle in question is the one on the bottom.

Edited by
DangerDave #2
Posted 25 August 2013 - 05:12 PM
Edited by Bubba, Today, 05:04 AM.
Added code tags + split into new topic.

Thanks Bubba!
jay5476 #3
Posted 25 August 2013 - 05:19 PM
Try putting your itemwait() function at the top of the code because when your functions try and call itemwait() it doesn't exist yet
jay5476 #4
Posted 25 August 2013 - 05:22 PM
Sorry for double post but your also going deeper into the stack by calling functions that call your function try a tail call eg.

return function() -- instead of
function()
DangerDave #5
Posted 25 August 2013 - 05:37 PM
Sorry for double post but your also going deeper into the stack by calling functions that call your function try a tail call eg.

return function() -- instead of
function()
Ok, so I moved the itemWait() as per your suggestion, but I don't understand this second part. I know OF the return command, but I can't seem to wrap my head around what it does. I do that function within a function thing a lot probably because I don't understand return. :P/>
jay5476 #6
Posted 25 August 2013 - 05:47 PM
The stack overflows (kinda big amount of info)
Spoilerfunction asd()
asd()
end

asd()
This will cause the error; since you will be going deeper into the stack with every call of asd.

You can also do this by using multiple functions:

function a()
b()
end
function b()
c()
end


function c()
a()
end

a()

Possible solutions are:

Tail calls:

You can read more about this here: Programming in Lua c. 6.3


function asd()
return asd()
end

Lua uses tail calls; what this means is that, if you call a function right after the return statement, the stack level will be re-used. I got this from the codex of error slaying -2 in the tutorials section
return is very simple eg.

function a()
return "hello"
end
text = a()
print(text) -- or print(a()) would work too
-- outputs hello
term.getSize() does it that's how you ghet the width and height variables
DangerDave #7
Posted 25 August 2013 - 05:50 PM
The stack overflows (<number> will be 256)


function asd()
asd()
end

asd()
This will cause the error; since you will be going deeper into the stack with every call of asd.

You can also do this by using multiple functions:

function a()
b()
end
function b()
c()
end
function c()
a()
end

a()

Possible solutions are:

Tail calls:

You can read more about this here: Programming in Lua c. 6.3


function asd()
return asd()
end
Lua uses tail calls; what this means is that, if you call a function right after the return statement, the stack level will be re-used. I got this from the codex of error slaying -2 in the tutorials section

Alrighty. Looks like i've got some reading to then. Thanks for the guiding lights! =D
jay5476 #8
Posted 25 August 2013 - 05:53 PM
the PiL is very useful for learning how to program, heck im still using it :P/>