Handy code for costing science

Place to get help with not working mods / modding interface.
Post Reply
blazespinnaker
Filter Inserter
Filter Inserter
Posts: 665
Joined: Wed Sep 16, 2020 12:45 pm
Contact:

Handy code for costing science

Post by blazespinnaker »

Is there a good place that lua code is being shared?


Code: Select all

/c 
function cost_tech(techCosted)
    techCosted = techCosted or "rocket-silo"
    tech={}
    raw={iron=1, copper=2, coal=3, stone=4, oil=5, steel=6, plastic=7, sulfur=8, gears=9, coil=10, greenc=11, inserter=12, belts=13, engines=14, pipes=15, redc=16}
    rawc={iron=0, copper=0, coal=0, stone=0, oil=0, steel=0, plastic=0, sulfur=0, gears=0, coil=0, greenc=0, inserter=0, belts=0, engines=0, pipes=0, redc=0}
    a=0 l=0 c=0 p=0 u=0 m=0 table.insert(tech,game.player.force.technologies[techCosted])
    dc={}

    rc={a={2,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
    l={5.5,1.5,0,0,0,0,0,0,1.5,3,1,1,1,0,0,0},
    c={12,7.5,1.5,0,38.462,1,3,0.5,1,15,3,0,0,1,2,1.5},
    p={52.5,19.167,3.333,11.667,68.376,8.333,6.667,0,0,38.333,8.33,0,0,0,0,3.333},
    u={33.333,49.833,3.833,0,106.838,2.667,7.667,1.667,0.333,58.333,17.667,0,0,0.333, 0.667, 1.333},
    m={7,2.5,5,10,0,0,0,0,0,0,0,0,0,0,0,0}
    }
    a=0 l=0 c=0 p=0 u=0 m=0 te=0
    while next(tech)~=nil do
        head = table.remove(tech,1)
        tn = head['name']
        if (dc[tn] == nil) then
            dc[tn]='found'
            uc=head['research_unit_count']
            en=head['research_unit_energy']
            aa=0 la=0 pa=0 ua=0 ma=0 ca=0
            for k,v in pairs(head['research_unit_ingredients']) do
            n=v['name']
            if (n=='automation-science-pack') then 
                aa=uc
            elseif (n=='logistic-science-pack') then 
                la=uc
            elseif (n=='production-science-pack') then 
                pa=uc
            elseif (n=='utility-science-pack') then 
                ua=uc
            elseif (n=='chemical-science-pack') then 
                ca=uc
            elseif (n=='military-science-pack') then 
                ma=uc
            end
            end
            game.player.print(tn .. " (en:" .. tostring(en) .. ") Automation(Red) - " .. tostring(aa) .. " Logistic(Green) - " .. tostring(la) .. " Chemical(Blue) - " .. tostring(ca) .. " Production(Purple) - " .. tostring(pa) .. " Utility(Yellow) - " .. tostring(ua) .." Military(Black) - " .. tostring(ma))
            a=a+aa
            l=l+la
            p=p+pa
            u=u+ua
            m=m+ma
            c=c+ca
            te = te + uc * (en/60)
            for k,v in pairs(head['prerequisites']) do
            table.insert(tech, v) 
            end
        end
    end
    out2 = "\nTotal:" .. " (en:" .. tostring(te/60) .. ") Automation(Red) - " .. tostring(a) .. " Logistic(Green) - " .. tostring(l) .. " Chemical(Blue) - " .. tostring(c) .. " Production(Purple) - " .. tostring(p) .. " Utility(Yellow) - " .. tostring(u)  .. " Military(Black) - " .. tostring(m) 
    game.player.print(out2)
    cos={a=a, l=l, p=p, u=u, m=m, c=c}
    rcos={}
    for k,v in pairs(cos) do
        for ki,vi in pairs(rawc) do
            rawc[ki]=rawc[ki]+rc[k][raw[ki]] * cos[k]
        end
    end
    out="Total Resources: "
    for ki,vi in pairs(rawc) do
        out=out .. ki .. ":" .. tostring(vi) .. " "
    end
    game.player.print(out)
    game.player.print(out2)
    game.write_file("techs.lua",  "\n\n" .. techCosted .. "\n" .. out .. out2, true)
end
OptimaUPS Mod, pm for info.

Post Reply

Return to “Modding help”