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

Balancing The Numbers Of Items In Specific Turtle Slots

Started by CCJJSax, 22 August 2013 - 02:35 PM
CCJJSax #1
Posted 22 August 2013 - 04:35 PM
I'm working on an Equivalent Exchange 3 program that has a minium stone in slot one and then pulls items to transmute from the above chest and deposit the crafted item into the forward chest.

So there is probably a really easy way to use a table to make this doable. But I'm very inexperienced with tables (plus major lack of sleep, but I digress.) So I want it to accept a tArgs[1] to know what is in the above chest to craft and an optional tArgs[2] for a backwards option.

The part I'm getting stuck at is the balancing part. I was able to balance it when there was only 2 items needed for next level of transmution. but then it got to 4, and it will be needlessly complex with my inefficient method. It needs to have the following characteristics.

1: Balance items in all needed slots.



slot1 = turtle.getItemCount(2)  -- lets say it has 3 items
slot2 = turtle.getItemCount(3)  -- lets say it has 11 items
balance = (slot1 + slot2)/2
--<here should be the method to transfer 4 items from slot2 to slot1>


2: deposit the remainder of items into the above chest.



-- keeping the above code variables in mind
-- slot1 has 3
-- slot2 has 10
-- slot1 would end up with six and slot2 would have 7.  I want to dump out that extra from slot2.



3: the ability to put in a number or a word into tArgs[1]
i.e. it takes 2 wood logs in the recipe to make 1 obsidian. Where it takes 4 obsidian to make 1 iron ingot.


I don't mind putting in these items individually.
McLeopold #2
Posted 22 August 2013 - 08:21 PM
Here is my method of the top of my head:

– get total items
– calculate extra and items for each slot, use modulo
– drop extra items from slots with too much
– loop through slots
– pull items from slots with too much
– push items to slots with not enough

And here's the lua. Don't look if you want to try and code it yourself.

Spoiler

local slots = 2
local total = 0

-- get total items
for i = 2, slots do
  total = total + turtle.getItemCount(i)
end
-- calculate extra and items for each slot
local extra = total % slots
local each = (total - extra) / slots

-- drop extra items from slots with too much
for i = 2, slots do
  while extra > 0 and turtle.getItemCount(i) > each do
    turtle.dropUp(1)
    extra = extra - 1
  end
end

-- loop through slots
for i = 2, slots do
  if turtle.getItemCount(i) < each then
    -- pull items from slots with too much
    for j = 2, slots do
      if j ~= i and turtle.getItemCount(j) > each then
        turtle.select(j)
        turtle.transferTo(i, 1)
      end
    end
  elseif turtle.getItemCount(i) < each then
    -- push items to slots with not enough
    turtle.select(i)
    for j = 2, slots do
      if j ~= i and turtle.getItemCount(i) < each then
        turtle.transferTo(j, 1)
      end
    end
  end
end
jay5476 #3
Posted 23 August 2013 - 03:03 AM

local function even(solt1, slot2)
  local total = turtle.getItemCount(slot1) + turtle.getItemCount(slot2)
  local stack = math.floor(total/2) -- rounded awnser
  local rem = total%2 -- get the remander
 turtle.select(slot1)
  turtle.transfer(16)
  turtle.select(slot2)
  turtle.transfer(16) -- transfer slot1 and 2 to 16
  turtle.select(16)
  turtle.dropUp(rem)
  turtle.transfer(slot1, stack)
  turtle.transfer(slot1, stack)
end
it would be simpler then what ^ posted I used a similar code in one of my programs but limit it to 2 slots