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

Turtle - checking total of a certain ID in any slot?

Started by treaddy, 22 March 2014 - 12:30 AM
treaddy #1
Posted 22 March 2014 - 01:30 AM
Hello,

I'm currently playing the ftb direwolf20 pack and experimenting with turtles. I've made programs for normal cComputers but never turtles.

Basically, in the pack you can get an item called an Ender Quarry. It's ideal for servers because it replaces blocks with dirt as opposed to leaving a massive hole.

The quarry uses a square fence boundary when setting up and only loads the chunk it's mining in.

I'm making a program for the turtle to place this fence for me.

So it ask's for the size and multiplies it by 4 (for square) and I wanted it to detect how many fences it needed as a result.

I can't find anything in the API relating to checking the inventory for certain ID's

Eg. user enters a boundary of 64, turtle needs 256 fence blocks.
Turtle checks inventory, if blocks are there, proceed.
Else, alert user and pause/cancel.

Code attached for context. It does need commenting and cleaning up I am aware. Plus I need to play with the boundary calculation so it doesn't place excess.


Any help would be greatly appreciated, thank you.


print("How big you want yer quarry?")
local size = read()
size = size * 4
local count = size
cFence(count)
function cFence()
while true do
  print(count)
  turtle.dig()
  turtle.digDown()
  turtle.select(2)
  turtle.placeDown()
  if count == 0 then
   break
  else
   count = count - 1
  end
end
end
Bomb Bloke #2
Posted 22 March 2014 - 03:30 AM
ComputerCraft turtles are unable to determine what sort of blocks are in their inventory. Even OpenPeripheral only allows them to determine the sorts of blocks in a different inventory.

In this case I would think it easiest to have the code assume that anything in the turtle's inventory is suitable for building the fence with. You could have the turtle print out that it's making this assumption when the script starts, and ask the user if they're ok with that before proceeding (they should obviously take this time to load up the turtle with their material of choice). If you need to also have the turtle carry fuel items, then you might furthermore specify that a certain slot be used to carry these.

Regarding the quantities, if you wanted to surround a given square area, you'd need (sidelength + 1) * 4 fence blocks. If the fence is going to be within the area, then you'd need (sidelength - 1) * 4 fence blocks.

You might use a function like this to check for items:

local curSlot = 1
turtle.select(curSlot)

local function getNextSlot()
	while true do           -- Start a loop that runs indefinitely.
		for i=1,16 do   -- Alter the range if you want to reserve certain inventory slots for other items.
			if turtle.getItemCount(i) ~= 0 then
				turtle.select(i)
				curSlot = i
				return  -- Exit the function (and hence the "while" loop), we've found more items to build with.
			end
		end
		
		print("Out of items to build with. Please insert more.")
		os.pullEvent("turtle_inventory")  -- Halt until the turtle's inventory changes.
	end
end	

if turtle.getItemCount(curSlot) == 0 then getNextSlot() end  -- Run a copy of this line before every attempt to place a block.
treaddy #3
Posted 22 March 2014 - 05:46 AM
Awesome, thank you for the quick and detailed reply.

ComputerCraft turtles are unable to determine what sort of blocks are in their inventory. Even OpenPeripheral only allows them to determine the sorts of blocks in a different inventory.

That's a shame. You would think given its capabilities to read chests and other storage, it would be able to read its own.

In this case I would think it easiest to have the code assume that anything in the turtle's inventory is suitable for building the fence with. You could have the turtle print out that it's making this assumption when the script starts, and ask the user if they're ok with that before proceeding (they should obviously take this time to load up the turtle with their material of choice). If you need to also have the turtle carry fuel items, then you might furthermore specify that a certain slot be used to carry these.

I thought this would have to be the case. Having a slot for fuel and the rest dedicated towards fencing will work just fine.

Regarding the quantities, if you wanted to surround a given square area, you'd need (sidelength + 1) * 4 fence blocks. If the fence is going to be within the area, then you'd need (sidelength - 1) * 4 fence blocks.

You might use a function like this to check for items:

..snip...

That is brilliant. Will definitely use. Thank you once again :)/>
Lyqyd #4
Posted 22 March 2014 - 08:51 AM
I hate to stick up for OpenPeripheral, but they do add a narcissistic turtle that can examine its own inventory. Check their documentation for more information.
Bomb Bloke #5
Posted 22 March 2014 - 09:14 AM
Ah, I'd thought that was from a different mod.
CometWolf #6
Posted 22 March 2014 - 09:35 AM
I hate to stick up for OpenPeripheral, but they do add a narcissistic turtle that can examine its own inventory. Check their documentation for more information.
Why the hate? lol anyways, i don't believe the narcissistic turtle has a crafting recipe. Atleast i've never been able to find it. You can still spawn it in, but im guessing the op is playing survival.
Lyqyd #7
Posted 22 March 2014 - 10:02 AM
Philosophical disagreements. :P/> You have to craft the turtle with something called a "duck antenna" if I recall correctly. It has some weird name like that.