LuaTechnology.level not showing infinite tech level

Place to get help with not working mods / modding interface.
Post Reply
Illiander42
Filter Inserter
Filter Inserter
Posts: 363
Joined: Mon Feb 05, 2018 10:01 am
Contact:

LuaTechnology.level not showing infinite tech level

Post by Illiander42 »

LuaTechnology.level is not showing infinite tech level

Example: Energy Weapons Damage is at level 21, but Lua is reporting the highest level of any tech as 7.

I am trying to determine the number of times any research has been completed in a way that handles mods being added/removed mid-playthrough (which means I can't just keep a tally of on_research_finished events).

(Yes, this is for a better "evolution tied to tech percentage" mod)

Current code in on_research_finished:

Code: Select all

        local player = game.get_player(1)
        local numResearched = 0
        local researches = {}
        local numTechs = 0
        for _, tech in pairs(force.technologies) do
            if tech.enabled then
                numTechs = numTechs + 1
            end
            if tech.researched then
                --numResearched = numResearched + tech.level
                local base_name = string.match(tech.name, "^(%g-)%-%d+$")
                if base_name == nil then base_name = tech.name end
                if researches[base_name] then
                    if researches[base_name] < tech.level then
                        researches[base_name] = tech.level
                    end
                else
                    researches[base_name] = tech.level
                end
            end
        end
        local highest_level = 0
        for _, level in pairs(researches) do
            numResearched = numResearched + level
            if level > highest_level then
                highest_level = level
            end
        end
        player.print("highest level: "..highest_level)
        player.print("num techs: "..numTechs)
        player.print("num techs researched: "..numResearched)
If this is intended behaviour, then please tell me how to get the currently researched level of an infinite tech.

quyxkh
Smart Inserter
Smart Inserter
Posts: 1027
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by quyxkh »

Try writing less rococo code?

Code: Select all

/c t={level=0} for k,v in pairs(game.player.force.technologies) do t=t.level<v.level and v or t end game.print(t.name..":"..t.level)

Illiander42
Filter Inserter
Filter Inserter
Posts: 363
Joined: Mon Feb 05, 2018 10:01 am
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by Illiander42 »

1) What do you mean by "rococo" code? I value clarity over conciseness in code.

2) That doesn't solve the problem that tech.level doesn't report infinite techs correctly.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5148
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by Klonan »

Works for me:
(Using /cheat to research all technologies between each command)
factorio-run_2021-12-15_09-39-46.png
factorio-run_2021-12-15_09-39-46.png (50.63 KiB) Viewed 2549 times
I will move this to modding help

Bilka
Factorio Staff
Factorio Staff
Posts: 3123
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by Bilka »

Illiander42 wrote:
Tue Dec 14, 2021 11:06 pm
LuaTechnology.level is not showing infinite tech level

Example: Energy Weapons Damage is at level 21, but Lua is reporting the highest level of any tech as 7.
This happens because your code skips counting the level of techs that aren't researched. But the weapons damage tech is considered unresearched, because the current available level of the tech is not researched. Example with 24 levels of the tech researched:

Image
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Illiander42
Filter Inserter
Filter Inserter
Posts: 363
Joined: Mon Feb 05, 2018 10:01 am
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by Illiander42 »

Ok, that makes sense now that it's been pointed out to me. Tech.researched isn't "has this tech been researched ever" it's "has this tech been researched fully".

Is tech.level == zero for all unresearched techs, and one for individual techs (eg. construction bots) that have been researched?

Or is there a better way to get the total number of times a tech has finished researching (other than counting "research complete" events)?

quyxkh
Smart Inserter
Smart Inserter
Posts: 1027
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by quyxkh »

Illiander42 wrote:
Wed Dec 15, 2021 8:37 am
1) What do you mean by "rococo" code? I value clarity over conciseness in code.
What I mean is that the code you posted puts so much verbosity and structure into clarifying every little step that it wasn't even doing what you thougt it doing: you reported luatechnology.level not showing infinite tech level, as if it was even checking luatechnology.level for infinite techs. What was happening was, that huge mess of clarity never even got there, it wasn't luatechnology not showing it, it was your code never checking it. And your code was so overwrought with performative syntax and structure in the name of "clarity" that you couldn't see that your own code never did what you wrote it to do at all, because all that code you added for clarity's sake was bug-ridden and slow.

Illiander42
Filter Inserter
Filter Inserter
Posts: 363
Joined: Mon Feb 05, 2018 10:01 am
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by Illiander42 »

So something like this should work in all cases?

Code: Select all

        local numResearched = 0
        local numTechs = 0
        for _, tech in pairs(force.technologies) do
            if tech.enabled then
                numTechs = numTechs + 1
            end
            if tech.researched then
                numResearched = numResearched + 1
            else
                if not (tech.research_unit_count_formula == nil) then
                    local base_number = tonumber(string.match(tech.name, "^%g-%-(%d+)$"))
                    numResearched = numResearched + (tech.level - base_number)
                end
            end
        end
        player.print("num techs: "..numTechs)
        player.print("num techs researched: "..numResearched)

quyxkh
Smart Inserter
Smart Inserter
Posts: 1027
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by quyxkh »

Try

Code: Select all

    numtechs=0 lvlsresearched=0
    for name,tech in pairs(game.forces.player.technologies) do if tech.enabled then
        numtechs = numtechs +1
        baselevel = game.technology_prototypes[name].level
        lvlsresearched  = lvlsresearched + tech.level - baselevel + (tech.researched and 1 or 0)
        end end
    game.player.print( 'Num techs: '..numtechs )
    game.player.print( 'Levels researched: '.. lvlsresearched )

Illiander42
Filter Inserter
Filter Inserter
Posts: 363
Joined: Mon Feb 05, 2018 10:01 am
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by Illiander42 »

From my poking around, tech.level doesn't seem to exist for non-multi-levelled techs?

My lua isn't great, so what happens then?

And how does your code handle multi-level non-infinite tech? I know it's a flaw in mine.

quyxkh
Smart Inserter
Smart Inserter
Posts: 1027
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: LuaTechnology.level not showing infinite tech level

Post by quyxkh »

Don't know where you were looking, `/c game.print(game.player.force.technologies.automation.level)` prints `1`, for instance, and since you can't add nil to a number that loop would crap out if it ever encountered a missing level. There's this trick you can pull if you're running in a terminal, you can print debugging data to it, which I did when boiling the nonsense out of that loop before deciding it was good enough to be done with. I have a consolegoodies.lua with functions for prettyprinting results,

Code: Select all

psd={comment=false,nocode=true}
function psl(p,k) return print(serpent.line(p,k or psd)) end
function psb(p,k) return print(serpent.block(p,k or psd)) end
function gpsl(p,k) return game.print(serpent.line(p,k or psd)) end
and had a line that became `psl{name=name, level=tech.level,baselevel=baselevel,researched=researched}` just before the calc there while writing the code. Nothing in the doc mentions these levels being optional, and they're very good about mentioning that elsewhere, afaik it's always called out.

I can't find any example of a prototype getting the base level wrong, regardless of how many levels there are, finite or not. `tech.level - baselevel + (tech.researched and 1 or 0)` works so long as the underlying data's as expected. What, specifically, are you looking at that led you to think it's ever something else?

Post Reply

Return to “Modding help”