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

Turtle dropoff function

Started by grand_mind1, 23 March 2013 - 07:39 PM
grand_mind1 #1
Posted 23 March 2013 - 08:39 PM
I am starting on a new quarry program to improve my previous one that had some kinks. I first want to refine the dropoff function in which the turtle places down an enderchest, dumps it's contents into the chest(which goes to my sorting system), and if the turtle's inventory is empty it will continue with what it was doing, but if it is not(like if my sorting system is backed up or if too many turtles are dumping into it at the same time) then it will wait five seconds and then try again. Most of it works fine except for the part where it tries to figure out if it's inventory is empty. I have it printing out the total number of items in it's inventory, for debugging purposes, before dumping into a chest or deciding that it's inventory is empty. When it prints out how much it has, it doesn't update after dumping items into the chest. So basically, the outcome is it thinking that it still has items in its inventory even when it doesn't.

Code:

--Very repetitive way to find out if there are any items in the inventory
local inv = turtle.getItemCount(1)+turtle.getItemCount(2)+turtle.getItemCount(3)+turtle.getItemCount(4)+turtle.getItemCount(5)+turtle.getItemCount(6)+turtle.getItemCount(7)+turtle.getItemCount(8)+turtle.getItemCount(9)+turtle.getItemCount(10)+turtle.getItemCount(11)+turtle.getItemCount(12)+turtle.getItemCount(13)+turtle.getItemCount(14)+turtle.getItemCount(15)+turtle.getItemCount(16)
local function forward(forw)
  curr = 0
  for i = 1,forw do
    while curr ~= forw do
	  if not turtle.forward() then
	    turtle.dig()
	    turtle.attack()
	    turtle.forward()
	  end
    curr = curr+1
    end
  end
end
local function empty() --Dropoff starts here
  turtle.select(16)
  turtle.placeUp()
  turtle.select(1)
  select = 1
  for i = 1,16 do
    turtle.dropUp()
    turtle.select(select)
    select = select+1
  end
  sleep(5)
  if inv == 0 then
    print("Inventory is empty!")
    print(inv)
    turtle.select(1)
    turtle.digUp()
    turtle.transferTo(16)
  else
    print("Inventory didn't empty!")
    print(inv)
    sleep(5)
    empty()
  end
end  --Ends here
print(inv)
empty()
By the way, if anyone knows a better way to find out how many items are in it's inventory please tell me! I am almost certain that their is a better way.
Also, there is some extra stuff besides the dropoff function for the quarry.

Help is appreciated!
Thanks! :D/>

P.S. Sorry, if this is hard to follow. I typed it at like 4 in the morning. :P/>
Viproz #2
Posted 23 March 2013 - 09:47 PM

local function numbItemsInInv()
  local items = 0
  for i = 1, 16 do -- Scan every slot of the turtle
    items = items + turtle.getItemCount(i)
  end
  return items
end
local function forward(forw)
  for i = 1,forw do -- it will execute the code forw times
    while not turtle.forward() do -- it will not leave while the turtle don''t move
	  turtle.dig()
	  turtle.attack()
    end
  end
end
local function empty() --Dropoff starts here
  turtle.select(16)
  turtle.placeUp()
  items = numbItemsInInv()
  while items ~= 0 do
    print("Try to empty the turtle's inventory")
    for i = 1,16 do -- Maybe it's to 17, i don't remember
	  turtle.select(i)
	  turtle.dropUp()
    end
    items = numbItemsInInv()
    if items ~= 0 then sleep(5) end
  end
  -- Here the inventory is obligatory empty
  print("Inventory is empty!")
  turtle.select(16)
  turtle.digUp()
end  --Ends here
inv = numbItemsInInv()
print(inv)
empty()

I didn't test it, I commented the code for the changes