factoriolib / flib

A set of high-quality, commonly-used utilities for creating Factorio mods.
https://mods.factorio.com/mod/flib
MIT License
67 stars 15 forks source link

feat: Method to check if a technology is researchable #66

Open EspadaV8 opened 6 months ago

EspadaV8 commented 6 months ago

Only just found out about this mod and was wondering if it'd possible to add something like flib_technology.is_researchable(technology). I have a very small mod and had to "implement" (I copied it from another mod) this, however, it only kind of works, since some technologies don't list all of their prerequisites (for example, the weapon damage and speed improvements don't list them all so the graph doesn't get cluttered) it's possible that this returns true when really the technology cannot be researched.

local flib_technology.is_researchable(technology)
    if (not technology.enabled) or technology.researched then
        return false
    end

    for _, prerequisite in pairs(technology.prerequisites) do
        if not prerequisite.researched then
            return false
        end
    end

    return true
end

I'm sure this could be improved, however, I don't have enough understanding of the Factorio data structures to know how to make those improvements.