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

get value for a string in io.read() and also some trees

Started by PROdotes, 21 September 2012 - 06:25 PM
PROdotes #1
Posted 21 September 2012 - 08:25 PM
So here's the 2 things i'm working on…i know the basics but i don't know the lua syntax…

first is a item to UU code… it has stuff like:

iron = 5/4
copper = 3/10
rubber = 4/63
copper_cable = copper/2 + rubber

and so on… now what i want to do in my main() is this…

function main()
print("Enter the item you want to craft: ")
item = io.read()
print("That item costs ",getUU(item)," UU to make.")
end

so how would i best write the getUU function?

and second question… trees…
what if i want to define something like this…

iron, base item
copper, base item
redstone, base item
rubber, base item
copper_cable, 1/2 copper, 1 rubber
refined iron, 1 iron
electronic circuit, 1 refined iron, 2 redstone, 6 copper cable

and when i do input = io.read() and input == "electronic circuit"

the output is "1 iron, 2 redstone, 3 copper, 6 rubber"…
i know of ways to do it, but since it's been years since i've worked on a "good code" it would be too messy… the other potential would be a forestry bee breeding program… you enter a type of bee or a product you want and it tells you how to get to the bee you want…
say you enter cultivated and it tells you:
cultivated = common + any natural beed
common = any natural + any natural

so… any clean solutions to my word to variable and find all the members in a tree solution? :)/>/>

thanks in advance ^^


edit:
is it possible to do something like this in lua…
mining_turtle.diamond_pick = 1
mining_turtle.turtle = 1
diamond_pick.diamond = 3
diamond_pick.stick = 2
stick.plank = 1/2
plank.wood = 1/4
turtle.iron = 7
turtle.chest = 1
turtle.computer = 1
chest.plank = 8
computer.stone = 7
computer.redstone = 1
computer.glass_pane = 1
glass_pane.glass = 6/16

and somehow if i ask for the mining turtle mats it tells me 3 diamonds, 8.25 wood, 7 stone, 7 iron, 0.375 glass, 1 redstone… and how…
i really have no clue how lua handles classes…
would be even better if i can tell it that 1 diamond is 9 UU and after giving me that, it tells me the total UU cost…
Lyqyd #2
Posted 22 September 2012 - 01:25 AM
So, first, you're really going to want to use a table for your item values, to make it easier to look it up. Something like:


itemTable = {}
itemTable.iron = "5/4" --use quotes if you want the literal string 5/4 to appear, do not use them if you want it to divide the numbers and store the result (1.25).
itemTable.copper = "3/10"
itemTable.rubber = "4/63"

The second bit is slightly more complex, but not too terribly hard. You use another table, but a slightly more complex one.


itemTable = {}
itemTable[1] = {}
itemTable[1].type = "storage"
itemTable[1].item = "copper_ore"
itemTable[2] = {}
itemTable[2].type = "smelter"
itemTable[2].smelt = {}
itemTable[2].smelt.product = "copper_ingot"
itemTable[2].smelt.ingredient = "copper_ore"
itemTable[3] = {}
itemTable[3].item = "copper_cable"
itemTable[3].type = "craft"
itemTable[3].craft = {}
itemTable[3].craft.quantity = 6
itemTable[3].craft[1] = {}
itemTable[3].craft[1].item = "copper_ingot"
itemTable[3].craft[1].quantity = 3
itemTable[4] = {}
itemTable[4].item = "insulated_copper_cable"
itemTable[4].type = "craft"
itemTable[4].craft = {}
itemTable[4].craft.quantity = 6
itemTable[4].craft[1] = {}
itemTable[4].craft[1].item = "copper_ingot"
itemTable[4].craft[1].quantity = 3
itemTable[4].craft[2] = {}
itemTable[4].craft[2].item = "rubber"
itemTable[4].craft[2].quantity = 6

Then you can iterate through this table using a fairly simple function. I use this one:


function canGet(item)
    for nNum, nInfo in ipairs(nodeTable) do
        if nInfo.type == "craft" then
            if nInfo.item == item then
                for iNum, iInfo in ipairs(nodeTable[nNum].craft) do
                    if not canGet(iInfo.ingredient) then return false end
                end
            end
        elseif nInfo.type == "smelter" then
            for sNum, sInfo in ipairs(nodeTable[nNum].smelt) do
                if sInfo.product == item then
                    if not canGet(sInfo.ingredient) then return false end
                end
            end
        elseif nInfo.type == "storage" or nInfo.type == "supply" then
            if nInfo.item == item then return true end
        end
    end
end

As you can see, it will iterate through the entire list (I generate mine from a specially formatted file) and look for the item requested. If it finds it, it looks for each ingredient. If at any point it can't find one of the components it needs, it will return false. It shouldn't be too hard to add a tracking table that gets passed along as it goes to gather up a list of base ingredients and the quantities needed.
PROdotes #3
Posted 22 September 2012 - 08:08 AM
color me stupid, since i don't really understand LUA… but here's the thing…
i changed nodeTable to itemTable, then added "print(item)" after every == item line…
and then called the entier thing with canGet("insulated_copper_wire") and the result, imho, should be:
insulated_copper_cable
copper_ingot
copper_ore
rubber

instead, it only returns insulated_copper_cable…
shouldn't it recursively run itself and return all the components?
also if i tell it to print(nInfo.quantity) it returns a blank line…

I don't get lua tables at all… except for table.insert and table.remove… and table[n] :P/>/>


fixed it…


function canGet(item, q)
check = false
for nNum, nInfo in ipairs(itemTable) do
if nInfo.type == "craft" then
totalQ = q / nInfo.craft.quantity
if nInfo.item == item then
--print(item,"-",nInfo.craft.quantity)
for iNum, iInfo in ipairs(itemTable[nNum].craft) do
canGet(iInfo.item, totalQ * iInfo.quantity)
check = true
end
end
elseif nInfo.type == "storage" then
if nInfo.item == item then
print(nInfo.item," - ",q)
check = true
end
end
end
if check == false then print("item not found - ",item) end
end

canGet("electronic circuit", 1)

this now, if the items are all set, properly tells me:
redstone - 2
iron ore - 1
copper ore - 3
resin - 4

now only to figure out why iron is not 0.5… anyone see the error? the values seem to be defined properly…
and when i set it to "refined iron" it tells me 0.5… but when it's "electronic circuit" it's 1… weird…
Lyqyd #4
Posted 22 September 2012 - 06:19 PM
I'd probably have to see the table you have set up.

Though realistically, it should tell you the minimum whole number needed, so 0.5 should be more of a problem. For instance, if you wanted twelve glass panes, it should still say that you need 6 glass blocks, rather than telling you that you need only 4.5 glass blocks.
PROdotes #5
Posted 24 September 2012 - 06:10 AM
yea… i've updated the formula now… if the quantity the recursive function requests % needed in recipe > 0 then it's set to requested - requested % needed + needed… since before 7 glass and 2 advanced alloys make 7 reinforced glass… so when i had a recipe that takes 2 reinforced glass it would tell me to get 2 glass… not it properly tells me to get 7… and when crafting i usually have 2-3 leftovers instead of missing 1-2 items :P/>/>
so tyvm for all the help…
but new question… do monitors have a limit? I've added an output to the monitor for the complete shopping list… and just a print to console for just the mats…
and it works fine with every recipe, except "ultimate hybrid solar panel"… then the screen is just blank… I can't really explain it except for hitting some kind of screen limit that wipes it for some reason…