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

Detecting Full Stacks & Another question...

Started by ScottCFR, 04 June 2012 - 04:32 AM
ScottCFR #1
Posted 04 June 2012 - 06:32 AM
So first, I'm working on a mining bot. It's pretty decent as it is now. What I'd like to do is have it return to it's home when the bot is full. The problem is, when it picks up different resource (Cobble, Iron, Diamond, Dirt, etc.) the stacks will be different sizes. So, when 8 slots of cobble are filled, the last has 1 diamond in it. I'm not really sure how to detect that like so.


Second question:
How do I use functions in LUA? (I'm sure I can find that elseware, but might aswell ask while I'm here.)


Thanks!
- Scott
Lolgast #2
Posted 04 June 2012 - 08:27 AM
I'm not sure what you mean with the first question, but you can get the amount of items in a certain slot by using turtle.getItemCount(slotNum) (change slotNum to the slot you want to check). For the second question, use:
local function myFirstFunction(args)
-- What you want your function to do
end
myFirstFunction(5) --Call the function, and give the first argument the value 5
This will create a function with the name myFirstFunction, and then run it.
ScottCFR #3
Posted 04 June 2012 - 08:44 AM
The first question I found confusing myself, now that I look at it.

So, the first 8 slots in a turtle are taken up by cobble (completely full). and the last slot has a rare item (ie. diamond), and most likely won't be filled anytime soon. How can I catch that so I can make my turtle return to where it started so it can be unloaded?

Basically, a more advanced way of detecting it being full. Make more sense now?

and thanks for the function thing.
Lolgast #4
Posted 04 June 2012 - 09:00 AM
You could use

for a=1,9
 local amountInSlot = turtle.getItemCount(a)
 if amountInSlot == 64 then
  print("Slot "..a.." is full")
  fullSlots = fullSlots+1
 end
end
fullSlots will now count the amount of slots that are full. You make something like

if a>5 then --change 5 to the maximum amount of full slots
... --your code for returning to base
end
to make it return to your base when full enough.
Lyqyd #5
Posted 04 June 2012 - 09:26 PM
Ideally, you want to detect when you have no more empty slots, and then return to be emptied. This is so if you run into nine different things before diamonds, you don't end up throwing them on the ground because you're waiting to be full.
my_hat_stinks #6
Posted 04 June 2012 - 09:58 PM
Ideally, you want to detect when you have no more empty slots, and then return to be emptied. This is so if you run into nine different things before diamonds, you don't end up throwing them on the ground because you're waiting to be full.

What I would do, is check if we have a free slot, and if we don't then compare the material before digging it, and if we can't hold it then return to the drop-off point
Then you won't go back with 1 diamond when there's another 3 there to pick up

Something along the lines of this:
local function CheckSlots()
   for i=1,9 do
	  turtle.select(i)
	  if (turtle.getItemCount(i) <= 0) or (turtle.compare() and (turtle.getItemSpace(i) >= 1)) then
		 return true
	  end
   end

   return false
end

local function Dig()
   if CheckSlots() then
	  --[[ turtle.dig and any movement etc]]
   else
	  --[[ Return to drop-off ]]
   end
end
Lyqyd #7
Posted 04 June 2012 - 10:17 PM
Well, as soon as you try to mine the next block of stone after you have no empty slots, it would give up. That or redstone, coal, or diamonds.

Empty slot checks are better because once we have no empty slots, there is no way to guarantee that we will be able to properly determine whether a block can be picked up or not without false negatives, so the extra effort to code in block checks isn't really worth it; especially so since the most common block will generate false negatives.
my_hat_stinks #8
Posted 04 June 2012 - 10:28 PM
Well, as soon as you try to mine the next block of stone after you have no empty slots, it would give up. That or redstone, coal, or diamonds.

Empty slot checks are better because once we have no empty slots, there is no way to guarantee that we will be able to properly determine whether a block can be picked up or not without false negatives, so the extra effort to code in block checks isn't really worth it; especially so since the most common block will generate false negatives.

Hm… Good point…
We need a way to get item IDs, then :)/>/>