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

Looking for advice on manipulating a turtle's inventory...

Started by gronkdamage, 29 June 2013 - 05:52 AM
gronkdamage #1
Posted 29 June 2013 - 07:52 AM
Long story short: I have a mining program that digs everything out in a straight line; and plugs in the walls with cobble. (so no lava spilling everywhere…) I have a second program that drops everything that isn't cobble or torches, and then I manually refill any lost cobble (cobble goes in slots 1-3, torches in 16).

I want to change this (if it's possible), to go through the inventory - and ensure that slots 1-3 have 64 cobble; and dump the rest.

So I guess I start off with turtle.getItemSpace(1) (through 3); and if the result is greater than 0, look for cobble in one of the other slots (4-15), then use turtle.transferTo() to move it around.


Question Time:

Is there possibly an easier way of doing this? Or do I have about the gist of what I need to do? :)/>


TIA :)/>
Senmori #2
Posted 29 June 2013 - 11:10 AM
This is very easy to accomplish, I'd suggest taking a look at the turtle api wiki page, found here.

Though, if your lazy I can put some untested code in a spoiler…
Spoiler

local cStart = 1
local cEnd = 3
local torchSlot = 16

function updateInv()
	--# Fill the required slots
		for i = cStart,cEnd do
			turtle.select(i)
		  
			  -- # If slot has less than 64 items
				if ( turtle.getItemCount(i) < 64 ) then
						-- # Store the amount in a variable
					invAmount = turtle.getItemCount(i)
						for j = 4,torchSlot-1 do
						   --# if the currently selected slot matches to 'i' do stuff
							if turtle.compareTo(i) then
								turtle.select(j)
								turtle.transferTo(i,64-invAmount)
							end
						end
				end
		end
	  
		--  # Inventory was filled thanks to the for loop
		for i = cEnd+1, torchSlot-1 do
			turtle.select(i)
			turtle.drop()
		end
end
gronkdamage #3
Posted 29 June 2013 - 04:45 PM
Laziness is the mother of all invention :)/> And nice, thanks! I'll look over the code and give it a test. Thanks!
gronkdamage #4
Posted 30 June 2013 - 07:55 PM
So that didn't quite work as intended, but I wrote a piece of code that works perfectly :)/> So just in case anyone else comes looking for something similar, I went with this:


local function Refill()
 for i = 1,3 do
  if  turtle.getItemSpace(i) ~= 0 then
   for j = 4,15 do
    turtle.select(j)
    if turtle.compareTo(i) then
     turtle.transferTo(i)
    end
   end
  end
 end
end

local function Unload()
 for i = 4,15 do
  turtle.select(i)
  if turtle.getItemCount(i) > 0 then
   turtle.dropDown()
  end
 end
 turtle.select(1)
end