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

Multiple layer table searching

Started by sjonky, 10 February 2014 - 11:17 AM
sjonky #1
Posted 10 February 2014 - 12:17 PM
I've gotten stuck on this problem a few times now when trying to make some contraptions with computercraft, And i just can't wrap my head around a good way to solve it.

This is a simple paint drawing of the problem:

The thingy to the left is a drawing of a multiple layered table, and the drawing on the right is a container with the colors i got. I want to get the green color, but i dont have one in my chest, So ill have to mix a blue and yellow color to get the green, i dont have thoose either so i need to go to the next layer until i get a color i already got in my container. Then ill go back up again through the colors until i have my green. Could anyone explain and easy and efficient way to do this in code? preferable a dynamic way. So this colored table could be even deeper, or if one color needs more colors to mix than 2.
Lyqyd #2
Posted 10 February 2014 - 01:07 PM
So, I'm guessing that this isn't the problem you're actually trying to solve. You're probably trying to set up an auto-crafting system, similar to how AE works, correct? A system that, given enough recipe information, can craft a complex item from basic materials. An easier way to do this is to create a "flat" list of recipes, rather than a tree to a specific item. You might have this for your recipe list:


local recipes = {
  ["oak_planks"] = {
    creates = 4,
    pattern = {
      {"oak_log"},
      {},
      {},
    },
  },
  ["wood_door"] = {
    creates = 1,
    pattern = {
      {"oak_planks", "oak_planks"},
      {"oak_planks", "oak_planks"},
      {"oak_planks", "oak_planks"},
    },
  },
  ["chest"] = {
    creates = 1,
    pattern = {
      {"oak_planks", "oak_planks", "oak_planks"},
      {"oak_planks", nil, "oak_planks"},
      {"oak_planks", "oak_planks", "oak_planks"},
    },
  },
}

Then you'd want to recurse along the table, either using items from storage or using the recipe to make the materials you need.
sjonky #3
Posted 10 February 2014 - 01:40 PM
Yeah first time i hit this problem was trying to make a good easy to use AE-like autocrafting. Right now im trying with breeding bees to get the desired species.
sjonky #4
Posted 10 February 2014 - 02:14 PM
Woot, i think i solved my bee breeding problem atleast ^^ was not as hard as i thought.


function wantSpecies(species)
	local needSpecies = species
	while true do
		local parents = getParents(needSpecies)
		print(needSpecies)
		print(parents.parent1.." x "..parents.parent2)
		parent1 = findSpecies(parents.parent1)
		parent2 = findSpecies(parents.parent2)
		if not parent1 then
			needSpecies = parents.parent1
		elseif not parent2 then
			needSpecies = parents.parent2
		else
			return parent1, parent2, needSpecies
		end
	end
end

I was stuck with thinking that i needed to find all bees i needed at one function run. When i actually only need to find one possible "row" anyway to get the bee. Atleast this function looks to be doing what i want it to do so far
Edited on 10 February 2014 - 01:17 PM