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

[programming] [help] item counter(slotnum)

Started by Goof, 05 October 2012 - 12:16 PM
Goof #1
Posted 05 October 2012 - 02:16 PM
Hey!

i'm trying to make a turtle, that can do the following:


[qoute]
when the pressure plate in front of it, is powered by 64 wood(an example) then it have to suck the 64 wood, up.
i have 3 comparing items in the last 3 slots:

Wood,
Sapling,
Cobblestone

when the turtle, did suck the wood, it will set a variable (turtleW) to "turtle.getItemCount(1)"
when all things above is happened for the Wood, Sapling, and the cobblestone, it has to send a rednet message to a specific turtle, that would do another program.
[/qoute]

Now to the problem..
When i drop 1 stack of wood on the pressure plate , the turtle sucks them up, but i have to drop them within 5 seconds, before it says"too long without yielding"….
but when i did put all the things, that it needs, in it, it dosent set the variables (the variables would be printed) to 64 at wood, saplings, and cobblestone. why?
i cannot post my code, because it will be used for a thing, in the nearest future.

can anyone make a little program that can count 3 specific items? and if there is too much of the item, it will drop them down.


Please help me… i cannot solve it.. i tried to solve it around 20 times, but still dosent work…

Thanks!
OmegaVest #2
Posted 05 October 2012 - 02:33 PM

tMax = {}
tMax["wood"] = 20
tMax["cobble"] = 30
tMax["saplings"] = 5

iSlot = 1

for k, v in pairs(tMax) do
   turtle.select(iSlot)
   iCount = turtle.getItemCount()
   if iCount > v then
      turtle.drop(iCount-v)
      print("Dropping ", iCount-v, " of ", k, ".")
   end
end


That should count them, and drop too many. You can change how many you want to have in the turtle at the top, and add/remove items. Just, put the items in the order they will be in the turtle's inventory. There's no way to know what's in it otherwise.
Goof #3
Posted 05 October 2012 - 02:42 PM
ehhm.. i'd tried it out, but… is the ["wood"] = 20 knowing, that it has to count the "wood" ??? or did i still have to place 1 of all the blocks, and then compare to 1,2,3 ?
OmegaVest #4
Posted 05 October 2012 - 02:49 PM
Ah, no. My code is seriously just to drop anything over a specific amount of items. So, if you have 40 wood in slot two, it will still drop 10 wood, even though the index is cobble. The names were just a placeholder because I sometimes forget what I'm doing when I run a program. And it lest you have that little line at the end.

If you want to compare() then you will have to have a block in that slot, but you don't have to.
Goof #5
Posted 05 October 2012 - 03:02 PM
okay… ehm i will post my code, but when i'd customize it a bit, then it still says "too long without yielding."

Spoiler


MaxW = 64
MinW = 64
MinC = 64
MaxC = 64
MaxS = 64
MinS = 64
WC = 0
CC = 0
SC = 0
xx = false
x = false

function countItems()
x = true
while x do
turtle.select(14)
if turtle.compareTo(1) then
if turtle.getItemCount(1) > MaxW then
if turtle.getItemCount(1) < MinW then
WC = turtle.getItemCount(1)
print(WC)
end
end
end
if turtle.compareTo(2) then
if turtle.getItemCount(2) > MaxW then
if turtle.getItemCount(2) < MinW then
WC = turtle.getItemCount(2)
end
end
end
if turtle.compareTo(3) then
if turtle.getItemCount(3) > MaxW then
if turtle.getItemCount(3) < MinW then
WC = turtle.getItemCount(3)
end
end
end
turtle.select(15)
if turtle.compareTo(1) then
if turtle.getItemCount(1) > MaxC then
if turtle.getItemCount(1) < MinC then
CC = turtle.getItemCount(1)
end
end
end
if turtle.compareTo(2) then
if turtle.getItemCount(2) > MaxC then
if turtle.getItemCount(2) < MinC then
CC = turtle.getItemCount(2)
end
end
end
if turtle.compareTo(3) then
if turtle.getItemCount(3) > MaxC then
if turtle.getItemCount(3) < MinC then
CC = turtle.getItemCount(3)
print(CC)
end
end
end
turtle.select(16)
if turtle.compareTo(1) then
if turtle.getItemCount(1) > MaxS then
if turtle.getItemCount(1) < MinS then
SC = turtle.getItemCount(1)
end
end
end
if turtle.compareTo(2) then
if turtle.getItemCount(2) > MaxS then
if turtle.getItemCount(2) < MinS then
SC = turtle.getItemCount(2)
end
end
end
if turtle.compareTo(3) then
if turtle.getItemCount(3) > MaxS then
if turtle.getItemCount(3) < MinS then
SC = turtle.getItemCount(3)
print(SC)
end
end
end

xx = true
while xx do
if WC == 64 then
if CC == 64 then
if SC == 64 then
print("ALL IS CORRECT!")

end
end
end

end

x = false
xx = false
end
end


tMax = {}
tMax["wood"] = 64
tMax["cobble"] = 64
tMax["saplings"] = 64

iSlot = 1

for k, v in pairs(tMax) do
   turtle.select(iSlot)
   iCount = turtle.getItemCount()
   if iCount > v then
      turtle.drop(iCount-v)
      print("Dropping ", iCount-v, " of ", k, ".")?
   end
end


























OmegaVest #6
Posted 05 October 2012 - 03:46 PM
GAHHH! I have spent the last half an hour puzzling over how to optimize your code for what you want. And I've determined, I don't think I can. But I can give you a few. . . pointers.

First: Nowhere in here does it drop anything. If that is the main goal of the program, then you need to rethink.

Second: Your most recent error comes from not ending your while loop. . . ever, really. x never becomes false again, so it never gets past it. Also, you need a sleep call in there somewhere.

Third: I intended my code to be used in conjunction, not apart from your own. A basic breakdown of what it does is this: Create a table storing resource names and maximums, initialize a slot, check slot for amount, compare against maximum. If over maximum, drop until meets maximum.
Ponder over that for how it fits (if it fits) into your purpose.

Fourth: For loops. They can be your friend. You can shorten most of your itemCount function by using two nested for loops

for x = 14, 16 do
   turtle.select(x)
   for y = 1, 3 do
      if turtle.compareTo(y) then
         iCount = turtle.getItemCount(x)
         if iCount > min and iCount < max then
             --Set variable
         end
      end
   end
end
You will need to learn tables too.
Doyle3694 #7
Posted 05 October 2012 - 04:20 PM
How nice, unformated code <3