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

Turtle programming question

Started by AgentRenamon, 11 February 2014 - 10:07 AM
AgentRenamon #1
Posted 11 February 2014 - 11:07 AM
I love turtles; but their programming code is a mystery to me, despite having taken programming classes a while back. Here's what I need a turtle to do for me:

1: To detect what a given item is in slot 1.
2: To dump everything else into an inventory below it.
3: If the item in slot 1 isn't there, then retrieve it from an inventory in front of it (an AE Interface if that matters)

I've managed to cobble a couple of idiot-level programs that does stuff for me; but I would really love some help in completing these 3 task within 1 program. Thank you in advance…
sjonky #2
Posted 11 February 2014 - 11:16 AM
1: A turtle can't detect what and item is, it is blind. There only way to do this is to have one block in it's inventory which you already know what is. Then comparing slot 1 to that item with turtle.compareTo(slot)
2: This code will drop everything from slot 2 to slot 16, change thoose numbers if you want to limit the dropping even more.

function dropAll()
   for i = 2, 16 do
	   if turtle.getItemCount(i) > 0 then
		   turtle.select(i)
		   turtle.dropDown()
	   end
	end
end

3: Im not sure this one will work, but i would think an AE interface which exports the item you want in slot 1, it should work with turtle.suck()

function pickUpItem()
	if turtle.getItemCount(1) == 0 then
		turtle.forward()
		turtle.select(1)
		turtle.suck()
		turtle.back()
	end
end
Edited on 11 February 2014 - 10:24 AM
awsmazinggenius #3
Posted 11 February 2014 - 11:16 AM
I may have understood you wrong, but you cannot currently detect block/item IDs in ComputerCraft without an additional mod.

EDIT: Damn ninja :ph34r:/>
Edited on 11 February 2014 - 10:16 AM
AgentRenamon #4
Posted 11 February 2014 - 11:36 AM
1: A turtle can't detect what and item is, it is blind. There only way to do this is to have one block in it's inventory which you already know what is. Then comparing slot 1 to that item with turtle.compareTo(slot)
2: This code will drop everything from slot 2 to slot 16, change thoose numbers if you want to limit the dropping even more.

function dropAll()
   for i = 2, 16 do
	   if turtle.getItemCount(i) > 0 then
		   turtle.select(i)
		   turtle.dropDown()
	   end
	end
end

3: Im not sure this one will work, but i would think an AE interface which exports the item you want in slot 1, it should work with turtle.suck()

function pickUpItem()
	if turtle.getItemCount(1) == 0 then
		turtle.forward()
		turtle.select(1)
		turtle.suck()
		turtle.back()
	end
end

1: That's gonna put a serious wrinkle in my plans, since the project I'm working on causes all of the given item in the same inventory to become damaged over time. That's why I need the turtle to somehow recognize what an item is, because when the detected item breaks, it must be able to obtain a replacement.

2: That should work beautifully, since I was actually hoping for a complete purge of everything except slot 1 anyway.

3: This is why I'm sorry that the MiscPerpherals mod isn't being updated any more, because it had a block that allowed a Turtle to directly access the AE network even to the point of letting it make auto-crafting requests. But I do have the OpenPeripherals Mod, which allows CC interaction with a wider range of machines and blocks than usual.

Thanks for the help.
CometWolf #5
Posted 11 February 2014 - 12:15 PM
Though i've never done it myself, i do believe openP allowes AE interaction. Also, with openP you can use a chest or any other inventory as a peripheral which can get the name, id and i think meta id(damage value). Just dump the items from the turtle into a chest or something, then dump them back once identified.
AgentRenamon #6
Posted 11 February 2014 - 03:34 PM
Found an acceptable work-around finally…

Since the item I'm replacing is slowly damaged over time anyway, I'm setting up a super-simple time-delay script. It will cause the turtle to sleep until the item breaks, and then dumps everything down into an ME interface before sucking a new item back into slot 1. Turns out the basic suck command works perfectly it.

Here's what I came up with:


-- Turtle sits on an AE Interface, which is set up to provide the desired object, just for clarity's sake...
shell.run("clear")

function pickup()
print("re-arming")
if turtle.getItemCount(1) == 0 then
turtle.select(1)
turtle.suckDown()
end
end

function dumpall()
print("dumping")
for i = 1,16 do
if turtle.getItemCount(i) > 0 then
turtle.select(i)
turtle.dropDown()
end
end
end

while true do
dumpall()
pickup()
turtle.select(1)
print("sleeping")
sleep(600)
print("waking")
end

The print commands is purely for debugging purposes; they can be removed or remarked out….

This is my first semi-major program; so I'm positive somebody out there can clean this up.

BTW, this is so my turtle can be the "chest" needed to use the Peaceful Table from the Extra Utilities Mod. The Peaceful Table acts like a slow-functioning Mob Trap while allowing you to play on Peaceful Difficulty. Check out the Mod; It has tons of goodies no matter what difficulty you play on, as well as cheap early-game piping system.

Later …. Peace ….
Edited on 11 February 2014 - 08:17 PM
sjonky #7
Posted 11 February 2014 - 04:42 PM
Though i've never done it myself, i do believe openP allowes AE interaction. Also, with openP you can use a chest or any other inventory as a peripheral which can get the name, id and i think meta id(damage value). Just dump the items from the turtle into a chest or something, then dump them back once identified.

This is certainly possible, If you put a chest beside the turtle/computer do chest = peripheral.wrap(side), and then do chest.getStackInSlot(slot) you will get a table in return with: name, id, rawName, Quantity, damage value. You can also talk directly to your AE system, crafting stuff, extracting stuff, or just check how much of a certain item you got in your AE system.

And AgentRenamon the thing you should change with your code is using identation. Makes the code alot easier to read. Although this code is short and easy to understand right now, when you get into long and advanced functions its good to already being used to use identation.
AgentRenamon #8
Posted 11 February 2014 - 09:16 PM
Though i've never done it myself, i do believe openP allowes AE interaction. Also, with openP you can use a chest or any other inventory as a peripheral which can get the name, id and i think meta id(damage value). Just dump the items from the turtle into a chest or something, then dump them back once identified.

This is certainly possible, If you put a chest beside the turtle/computer do chest = peripheral.wrap(side), and then do chest.getStackInSlot(slot) you will get a table in return with: name, id, rawName, Quantity, damage value. You can also talk directly to your AE system, crafting stuff, extracting stuff, or just check how much of a certain item you got in your AE system.

And AgentRenamon the thing you should change with your code is using identation. Makes the code alot easier to read. Although this code is short and easy to understand right now, when you get into long and advanced functions its good to already being used to use identation.

Thanks for the info; I'll look into it later since this method seems to be working as good as I want it to for now. Also, the reason for the lack of indentation is because this the actual file from my map save, not my work-save which is auto-indented. I use remarks myself if my work's gonna be on the lengthy side, so I'll know which areas of code controls what stage of its progress. While not trainned in assembler-like code such as Lua, I did take courses for Visual Cobol "back in the day"; so I know the drill here…

Later…. Peace….