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

[LUA]Stacking with a turtle...[Solved](sort of)

Started by Lost Ninja, 28 March 2013 - 12:18 AM
Lost Ninja #1
Posted 28 March 2013 - 01:18 AM
Basically trying to make my turtle re-stack all all of the items it carries to make the dumping into an ender chest faster, I have the rest of the code working but no matter what I have tried with the actual stacker function it either does nothing or gets into a never ending loop.
function stacker()
	for i = 4,16 do
		if turtle.getItemCount(i) < 64 then
			for b = 4,16 do
				sel(B)/>/>/>
				if not (b == i) then
					if turtle.compareTo(i) then
						turtle.transferTo(i)
					end
				end
			end
		end
	end
end
http://pastebin.com/P6yEgTY8
((sel(x) is just a turtle.select(slot) function))

I figure I'm doing something stupid, but can't for the life of me work out what. I get no errors on running so I figure the code is doing what it should just not what I want… :/

Code should work like this:
Check slot 4 to see if it contains less than 64 items then select slot 4 if this slot is the same slot as you already have ignore it and select slot 5. If slot 5 contains the same item move as much of this item as possible into slot 4.

Just realised I need to repeat the check of slot 4 to check it now has 64 items… but I can probably work that out, (probably just call stacker twice…) :D/>
Bubba #2
Posted 28 March 2013 - 01:28 AM
Edit: You aren't only trying to restack things into slot 4 are you? Because if so then the rest of this post doesn't really apply.
Edit 2: Our code is essentially the same now that I look again except that I put the turtle.select in a different place (first for loop rather than second, which would actually make a pretty serious speed difference when you consider that selecting takes longer than comparing or transfering) and I added in a few speed improvements. Also, you are trying to do sel(B ) rather than sel(b ), but since you got no errors I'm assuming that you made a typo transfering to the forum.
Edit 3: I did some speed tests, and this code:

for i=1,16 do
  if turtle.getItemCount(i) > 1 then
	turtle.select(i)
	turtle.drop()
  end
end
is faster (by 5 seconds actually), even when I combine the drop into the function as I do below. So that would probably be your best bet here.

Oops! Didn't really mean to post this cause I wasn't finished - just accidentally hit enter and it submitted. But anyway, this is my method of restacking. To be honest though, I feel like it would just be faster to dump directly into an ender chest because this way you have to cycle through the turtle's inventory twice as opposed to the once that you would do with a regular dump.

local function restack()
  for x=1,16 do
	if turtle.getItemCount(x) < 64 then
	  turtle.select(x)
	  for y=x+1,16 do --Avoids testing the same slot
		turtle.transferTo(y) --It's actually not necessary to compare items - if it's able to transfer it will do so. This cuts down on the time of checking that they are the same and then transfering.
	  end
	else
	  turtle.select(x)
	  turtle.drop() --Incorporates the drop into the restack function so that it's faster.
	end
  end
end

Also, is the turtle digging blocks itself or are items be inserted by users? Because if the former, then there would be no need for a restack.
Lost Ninja #3
Posted 28 March 2013 - 02:21 AM
Its supposed to check slot 4 fill it from the rest of the inventory if its below 64 (and there is more of that item in the inventory). Then when 4 has 64 (or nothing else to stack into it) it should do the same for slot five.

Having read your post however I realise that I have made a basic error in my logic anyway, so I'm going to try and get that working on paper then try and code it again. But the code snippet you have will help with that, so thanks. :D/>
theoriginalbit #4
Posted 28 March 2013 - 02:48 AM
Ok just curious here… its obviously a miner, so why don't you have it sort as it mines?
LordIkol #5
Posted 28 March 2013 - 03:25 AM
Ok just curious here… its obviously a miner, so why don't you have it sort as it mines?

Im more curious about the question why you wanna sort an inventory " to make the dumping into an ender chest faster"

for sorting you have to iterate multiple times through your inventory for putting things into chest only one time or do i get sth wrong?
Bubba #6
Posted 28 March 2013 - 03:27 AM
Ok just curious here… its obviously a miner, so why don't you have it sort as it mines?

Err… I didn't see that he said it was a miner? But yeah, if it is a miner, then just selecting the first slot pretty much automatically sorts it for you :)/>