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

[Turtles] [Lua] How do I make a sorting system?

Started by mrSLIMEguy, 26 October 2012 - 08:19 PM
mrSLIMEguy #1
Posted 26 October 2012 - 10:19 PM
So, i know the things for putting things in chests, but how do i get the turtle to only put certain blocks in a chest? Like one for cobblestone, sand etc

I think you can do something like

turtle.compare()

but, how do i do items?
and blocks?

Can i also use the item method on the blocks so i dont need to place them under a chest?

Thanks
-mrSLIMEguy

EDIT:
Here is my layout:
https://twitter.com/mrslimeguy/status/262032608601460736 (first thing i thought of)
(once i get the code fixed, i can expand)
Edited on 27 October 2012 - 02:29 AM
ChunLing #2
Posted 26 October 2012 - 10:50 PM
turtle.compareTo([slot]). If you use a loop to identify the stack sizes in each of your slots, then turtle.suck() an item from the chest, then recheck the slots, you can be sure which slot has the item from the chest. Then you go through and compareTo each of the other slots to the slot that got the item from the chest to see if they match (and then I presume you drop all the matching slots into the chest). Use a table to store the different slot stack sizes, and the change, and whether the slot matched the stack with the item from the chest.
mrSLIMEguy #3
Posted 27 October 2012 - 04:20 AM
OK, i tried that…. but i failed :D/>/>

Code: http://pastebin.com/xbUeCEmd

Image of my layout:

(at the top)
ChunLing #4
Posted 27 October 2012 - 05:12 AM
You're not storing the returns or checking to see which slot got the item. When you suck up an item, it just goes into the next available slot (and it doesn't necessarily consider a stack of that item type to be available, oddly). I'll see if I can't work up a sample code that's a better illustration.
ChunLing #5
Posted 27 October 2012 - 05:42 AM
Okay, here's a simple function that you can adapt to your needs.
local function matchchestitem()
	local t_stackinfo = {}
	for i = 1,16 do t_stackinfo[i] = turtle.getItemCount(i) end
--there are now 16 sequential table entries, each containing a before value
	if turtle.suck() then -- we can skip the rest if we can't get an item
		local index = 0
		repeat index = index+1
		until turtle.getItemCount(index) > t_stackinfo[index]
--we now know where the sucked items went, at least some of them
		turtle.select(index)
		for i = 1,16 do
			if turtle.compareTo(i) then --the items in slot i match those taken from the chest
			-- do what you will with them, but make sure to
				turtle.select(index)
			end -- before the next loop
		end
		return index --so we know which slot was the match
	else print("there was nothing in the chest or your turtle was full, sorry") return nil
	end
end
No guarantees this structure will suit your needs. Right now the latter bit doesn't really do anything, so a simplified version looks like this
local function matchchestitem()
	local t_stackinfo = {}
	for i = 1,16 do t_stackinfo[i] = turtle.getItemCount(i) end
	if turtle.suck() then
		local index = 0
		repeat index = index+1
		until turtle.getItemCount(index) > t_stackinfo[index]
		return index
	else return nil end
end
This function just checks the chest in front to see if it can suck up anything and returns the first slot that got something or nil if the turtle didn't pick up anything.

Oops, extra table bits removed.
Edited on 27 October 2012 - 03:44 AM
ChunLing #6
Posted 27 October 2012 - 05:53 AM
Really, an easier way of doing this is to have dedicated inventory turtles that have 16 one item stacks of various item types. The turtles (or pipes or chestcarts or whatever) that bring in unidentified items dump them all in some dump chests, then the inventory turtles go over and suck each of the chests, go to their section of the inventory, and then dump all but one of the appropriate stack into the appropriate chest. Then they loop around and do it again. As long as the inventory turtles are the only ones putting things into the chests, and as long as they always keep a single item for matching with other items, the chests will always have only the items that were initially placed in their inventory turtles slot.

Of course, this takes a fair number of turtles to work, and the movement has to be fail-proof and all so they don't get off course, but it is much easier if you've got the turtles to work it. But since you probably don't, at least for now, the above solution has value in the interim.
bbqroast #7
Posted 28 October 2012 - 12:52 AM
To explain what the others are saying:
In order to find out what type of item is in a slot you must use the turtle.compareTo(slotID) function.
So here is some code that would face a chest and deposit all items that match what that chest has in it (not tested):

slotCounts = {} - Create a empty table called slotCounts
for i = 1, 16, 1 do
   slotCounts[i] = turtle.getItemCount(i)
end
turtle.suck()
selectedSlot = -1
for i = 1, 16, 1 do
   if slotCounts[i] ~= turtle.getItemCount(i) then
	   selectedSlot = i
   end
end
if selectedSlot == -1 then
   return
end
turtle.select(selectedSlot)
for i = 1, 16, 1 do
   if turtle.compareTo(i) then
	   turtle.drop()
   end
end
Basically the code creates a table, storing the amount of items in each slot of the turtles inventory. Then it sucks a item from the chest. It then checks each slot seeing if the item count has changed, if it has it knows that slot has the item from the chest in it. Finally the turtle selects that slot, and checks all the other slots. If they contain the same item it drops them into the chest.
ChunLing #8
Posted 28 October 2012 - 01:52 AM
You should make sure, when dropping all the items, to not drop everything out of the slot that you're using to compareTo. And you want to make it so that you select the other slots and drop their contents.

The above code still works, but it only certainly drops all of the stack that got the item (and won't drop anything from stacks after that item). You could rewrite the last bit like so:
for i = selectedSlot,selectedSlot+15, 1 do
   turtle.select(math.fmod(i,16)+1)
   if turtle.compareTo(selectedSlot) then turtle.drop() end
end
This makes the loop select the next slot after the selected slot, and then iterates from there, looping around to one when i goes over 15 (we're selecting i+1 before then, afterwards we're basically talking i-15).

That modification allows you to go through and drop all the slots that match selectedSlot before you drop selectedSlot.