This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Question]Turtles resource drop-offs
Started by MatazaNz, 04 October 2012 - 09:33 AMPosted 04 October 2012 - 11:33 AM
Alright, I know how to use turtles, I have made a few complex programs with the turtles, but they all involved building but not mining. How would I code into the turtle that when it's inventory is full, it will return to a chest and drop it all into it? Any help is appreciated. I only need to know what kind of commands I need to issue, the rest I can work out.
Posted 04 October 2012 - 11:59 AM
There is no default command for checking if the turtle's inventory is full, however, you can write a function that does that. Here's how I would write one:
It's up to you though to design the returning home system as there are many ways to do that (redstone signalling, pathfollowing, gps networks)
Dropping items off are as simple as creating a for loop to iterate over your inventory and then turtle.drop() the itemstack when facing a chest.
function inventoryFull()
for i = 1, 16 do
if turtle.getItemCount(i) == 0 then
return false
end
end
return true
end
It's up to you though to design the returning home system as there are many ways to do that (redstone signalling, pathfollowing, gps networks)
Dropping items off are as simple as creating a for loop to iterate over your inventory and then turtle.drop() the itemstack when facing a chest.
Posted 29 January 2013 - 07:35 PM
There is no default command for checking if the turtle's inventory is full, however, you can write a function that does that. Here's how I would write one:function inventoryFull() for i = 1, 16 do if turtle.getItemCount(i) == 0 then return false end end return true end
It's up to you though to design the returning home system as there are many ways to do that (redstone signalling, pathfollowing, gps networks)
Dropping items off are as simple as creating a for loop to iterate over your inventory and then turtle.drop() the itemstack when facing a chest.
I'm sorry for the *incredibly* late reply. Thank you, that really helped. I haven't been on the CCforums for a while, that's why I didn't reply sooner.
Posted 29 January 2013 - 07:50 PM
Thank you, that really helped. I haven't been on the CCforums for a while, that's why I didn't reply sooner.
That would actually tell the code that the turtle is full when the first slot fills up. You would want to do the inverse of this so I would be more inclined to do this.Here's how I would write one:function inventoryFull() for i = 1, 16 do if turtle.getItemCount(i) == 0 then return false end end return true end
local function inventoryFull()
for i = 1, 16 do -- if your using a Tekkit computer craft (version 1.3) the 16 should be 9
if turtle.getItemSpace(i) ~= 0 then -- meaning there is space
return false -- its not full
end
end
return true -- its full because we went through the entire inventory without finding space
end
Posted 29 January 2013 - 09:14 PM
That code is close but wouldn't factor in different blocks. So If you had 16 slots and they all had dirt in and you got a diamond, you'd loose the diamond as there is no spare inventory slot for it.That would actually tell the code that the turtle is full when the first slot fills up. You would want to do the inverse of this so I would be more inclined to do this.
local function inventoryFull()
for i = 1, 16 do – if your using a Tekkit computer craft (version 1.3) the 16 should be 9
if turtle.getItemSpace(i) ~= 0 then – meaning there is space
return false – its not full
end
end
return true – its full because we went through the entire inventory without finding space
end
Your best bet with inventory is this:
local function inventoryHasSpace()
local iHaveSpace = false
for i = 1, 16 do
if turtle.getItemSpace(i) == 0 then
return true – at least one spare inventory slot
end
end
return iHaveSpace
end
Posted 29 January 2013 - 09:17 PM
All you did was rename the function and add an iHaveSpace variable and make sure its empty, what if they have a 12 diamond in a slot and then your like "no you cant put that diamond in a diamond stack!"… also use [code][/code] around codeThat code is close but wouldn't factor in different blocks. So If you had 16 slots and they all had dirt in and you got a diamond, you'd loose the diamond as there is no spare inventory slot for it.
Your best bet with inventory is this:
local function inventoryHasSpace()
local iHaveSpace = false
for i = 1, 16 do
if turtle.getItemSpace(i) == 0 then
return true – at least one spare inventory slot
end
end
return iHaveSpace
end
The best you can do to factor in different items/blocks is by doing this before digging it
local function spaceForItem()
for i = 1, 16 do
turtle.select(i)
if turtle.compare() and turtle.getItemSpace(i) >= 7 then
return true
end
end
return false
end
where the number 7 is the MAX drops that you can ever get from a block, i don't think 7 is right, but I cant remember the actual number.Posted 29 January 2013 - 09:40 PM
well aside from the inventory sensing here is a basic example of how to head back to the chest when you need to. call activate() to start tracking your movements and call deactivate to stop tracking. call retrace to go back to the chest
oldT=turtle or {}
local str=""
local function activate()
str=""
local tDirs={up="d",down="u",forward="f",back="b",turnRight="l",turnLeft="r"}
local tMoves={}
for k,v in pairs(tDirs) do tMoves[v]=k end
turtle=setmetatable({},{__index=function(self,index)
if tDirs[index] then
return function() if oldT[index]() then str=tDirs[index]..str return true end return false end
elseif oldT[index] then
return oldT[index]
end
end})
end
local function deactivate()
turtle=oldT
end
local function retrace()
for i=1,#str do
while not oldT[tMoves[string.sub(str,i,i)]]() do end
end
end
Posted 30 January 2013 - 11:48 AM
I always just check if slot 16 (or whatever slot I've reserved for the purpose) has anything in it. As long as the only way that slot would have anything would be for all the other slots to be full, it's more efficient than checking all the slots.