Access to the "formula" calculator for infinite technologies

totobest
Inserter
Inserter
Posts: 27
Joined: Mon Dec 26, 2016 12:46 am
Contact:

Access to the "formula" calculator for infinite technologies

Post 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)
quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

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

Post 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.
totobest
Inserter
Inserter
Posts: 27
Joined: Mon Dec 26, 2016 12:46 am
Contact:

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

Post 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.
Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

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

Post 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).
totobest
Inserter
Inserter
Posts: 27
Joined: Mon Dec 26, 2016 12:46 am
Contact:

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

Post 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.
Post Reply

Return to “Implemented mod requests”