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

[Lua][Question] Help with comparing multiple values.

Started by fusiomax, 29 September 2012 - 08:41 AM
fusiomax #1
Posted 29 September 2012 - 10:41 AM
So I'm making a cobble miner and I just want him to mine cobble when he detects some infront of him, and when all 16 slots are full dropping his inventory into my storage system.

Now I have no problem with the detecting and then digging, but to find if the inventory is full is the bit I'm having trouble with.

I can see 2 ways to do this:
1, store the number of times turtle has mined in a variable, and once equal to 1024 then dumping inventory.
2, nesting if statements checking all slots and if all slots are full then running the dump procedure.

But these are both quite complicated awkward ways of doing it, is there an easier way?

Thanks in advance :P/>/>

EDIT: I nested the if statements just to see how silly it would look:
Spoiler

local function invDump()
for i = 1, 16 do
  turtle.select(i)
  turtle.dropDown()
end
end
while true do
if turtle.detect() then
  turtle.dig()
end
if turtle.getItemCount(1) == 64 then
  if turtle.getItemCount(2) == 64 then
   if turtle.getItemCount(3) == 64 then
	if turtle.getItemCount(4) == 64 then
	 if turtle.getItemCount(5) == 64 then
	  if turtle.getItemCount(6) == 64 then
	   if turtle.getItemCount(7) == 64 then
		if turtle.getItemCount(8) == 64 then
		 if turtle.getItemCount(9) == 64 then
		  if turtle.getItemCount(10) == 64 then
		   if turtle.getItemCount(11) == 64 then
			if turtle.getItemCount(12) == 64 then
			 if turtle.getItemCount(13) == 64 then
			  if turtle.getItemCount(14) == 64 then
			   if turtle.getItemCount(15) == 64 then
				if turtle.getItemCount(16) == 64 then
				 invDump()
				end
			   end
			  end
			 end
			end
		   end
		  end
		 end
		end
	   end
	  end
	 end
	end
   end
  end
end
end

It formats it a bit strange when I copy it in to here
Maome #2
Posted 29 September 2012 - 11:22 AM
The same code could be written like this:


local zcount = false

for i 1,16 do
  if turtle.getItemCount(i) != 64 then
    zcount = true
  end
end

if zcount == false then
  invDump()
end

But if you don't think you'll be canceling it half way through or starting it with some inventory already filled a counter that increments on every cobble mined would be easier.

EDIT: Ok that wasn't lua, this is probably closer. It may not run but you should get the idea of how to do it.
Doyle3694 #3
Posted 29 September 2012 - 11:34 AM
or you just check slot 16 and make sure you select slot 1 afterwards, like:
if turtle.getItemCount(16) = 64 then
invDump()
end
turtle.select(1)
fusiomax #4
Posted 29 September 2012 - 11:43 AM
The same code could be written like this:


local zcount = false

for i 1,16 do
  if turtle.getItemCount(i) != 64 then
	zcount = true
  end
end

if zcount == false then
  invDump()
end

But if you don't think you'll be canceling it half way through or starting it with some inventory already filled a counter that increments on every cobble mined would be easier.

EDIT: Ok that wasn't lua, this is probably closer. It may not run but you should get the idea of how to do it.

That comes in handy for something else I'm working on thanks :P/>/>


or you just check slot 16 and make sure you select slot 1 afterwards, like:
if turtle.getItemCount(16) = 64 then
invDump()
end
turtle.select(1)

That is really simple, makes me feel derpy :D/>/> cheers man

EDIT: for anyone interested here is my final code:
Spoiler

local function invDump()
for i = 1, 16 do
  turtle.select(i)
  turtle.dropDown()
end
turtle.select(1)
end
while true do
turtle.select(1)
if turtle.getItemCount(16) == 64 then
  invDump()
end
if turtle.detect() then
  turtle.dig()
end
end