Page 1 of 1

Access to the "formula" calculator for infinite technologies

Posted: Thu Feb 21, 2019 12:50 pm
by totobest
Hi devs,

For my mod TechnologyMarket, I would like to calculate the "cost" of a technology. I need the "count" and the ingredients.
For infinite technologies, there is no "count" properties but a "formula" property which allows to get the "count" based on the current level of the technology.

What I need is access to the internal formula parser (as stated here https://www.factorio.com/blog/post/fff-161) from Lua.
Maybe something like :

Code: Select all

formula = "100(L-6)"
engine.formula_calc(formula, 3)

Re: Access to the "formula" calculator for infinite technologies

Posted: Thu Feb 21, 2019 8:15 pm
by quyxkh
Looks like you can just sub in a `*` between any digit-or-letter-and-`(` pairs then use lua's evaluator,

Code: Select all

/c
local resblock = {}
function rescount(tech)
    local f = resblock[tech.name]
    if f == nil then
        local n = tech.research_unit_count_formula
	local _ = n and 'return function(L) return ' .. n:gsub( '(%w)(%()', '%1*%2') .. '\nend'
        f = n and load(_)() or false
        resblock[tech.name] = f
        end
    return f and f(tech.level) or tech.research_unit_count
    end
then e.g. `/c game.print(rescount(game.player.force.technologies['mining-productivity-16']))` works.

Re: Access to the "formula" calculator for infinite technologies

Posted: Sat Feb 23, 2019 8:39 pm
by totobest
Thank you for your reply. I have already implemented the parser using gsub and load.
Thus it might not cover the case.
Hence my question.

Re: Access to the "formula" calculator for infinite technologies

Posted: Thu Jun 06, 2019 6:05 am
by Boodals
I have implemented the function game.evaluate_expression(...) for this purpose (I have source access). It will be in 0.17.46.
For the future, you can make an API request in the modding interface requests board, where it'll get seen much quicker (at least by me).

Re: Access to the "formula" calculator for infinite technologies

Posted: Fri Jun 07, 2019 5:24 pm
by totobest
Boodals wrote: Thu Jun 06, 2019 6:05 am I have implemented the function game.evaluate_expression(...) for this purpose (I have source access). It will be in 0.17.46.
For the future, you can make an API request in the modding interface requests board, where it'll get seen much quicker (at least by me).
Wow that's awesome. Thank you.