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

How to check fail-proof a turtle's inventory status ?

Started by Tai, 17 August 2013 - 09:20 PM
Tai #1
Posted 17 August 2013 - 11:20 PM
How to check fail-proof a turtle's inventory status ?

I am writing a turtle program for excavating tunnels, rooms or quarries. I would like the turtle to check its inventory before every dig order. This should prevent any precious minerals dropping to ground or in worst case burnt in lava.

I could compare every slot of the turtle with the block in front of it and check if there is at least space for 5 items (e.g. Redstone dropping up to 5 pieces of dust). But this get very slow after several slots of the turtle contains at least 1 item. The function turtle.select( … ) is so slow.

If I try to keep the inventory status in a table, I am running into other problems. A player could manually empty some slots of the active turtle and confuse the turtle this way. Or some minerals have a varying amount of drops.

The fastest and secure way I have found so far is to check if there is at least one completely free slot left. This leads to a turtle that returns with a not optimized filled inventory.
Bubba #2
Posted 18 August 2013 - 12:27 AM
I would actually go ahead and use a table to keep track of things. In order to make sure the turtle only recounts when necessary, you can use the parallel API to listen for key events and have the player hit a key whenever they empty the turtle.

For example:

local needsRecount

local function listen()
  term.clear() term.setCursorPos(1,1)
  term.write("If you are emptying the turtle of items, please press any key to prompt a recount.")
  while true do
	local e = {os.pullEvent("key")}
	needsRecount = true
  end
end

local function mine()
  --# Your stuff goes here
  if needsRecount then
	--#Do the recount
  end
end

parallel.waitForAny(listen, mine)

This being said, it may still be a good idea to go with the secure way and check to make sure there is at least one open slot. This way, the turtle cannot be filled completely with a certain type of item, and have any other precious items burn up in lava.
Tai #3
Posted 18 August 2013 - 01:20 AM
Many thanks for this fast reply.

Okay. I will continue to use the check for at least one empty slot. The hint to the parallel API is nonetheless quite useful. I will use it for another check: The turtle will use a whitelist for blocks it is allowed to mine. If a player empties accidently a slot that is used for the whitelist, he can rebuild the whitelist without need to restart the program,