science-extra-trigger-techs/data-updates.lua

Wed, 27 Aug 2025 10:26:12 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 27 Aug 2025 10:26:12 +0300
changeset 19
adbc5c74f279
permissions
-rw-r--r--

Lots of stuff

---@type table<string, int>
local min_science_requirements = {}

-- Extract tech tier from the name
-- e.g. "military-3" => 3
---@param name string
local function tech_level(name)
	local a, b = string.find(name, "-%d+$")
	if a == nil
	then
		return nil
	else
		return tonumber(string.sub(name, a+1, b))
	end
end

for _, technology in pairs(data.raw["technology"])
do
	if technology.unit
	then
		local prerequisites = {}
		for _, prereq in pairs(technology.prerequisites or {})
		do
			prerequisites[prereq] = 1
		end
		local unit_count = 50
		if technology.unit.count
		then
			unit_count = technology.unit.count
		elseif technology.unit.count_formula
		then
			local level = tech_level(technology.name) or 1
			unit_count = math.max(1, helpers.evaluate_expression(technology.unit.count_formula, {l = level, L = level}))
		end
		for _, ing in pairs(technology.unit.ingredients or {})
		do
			if prerequisites[ing[1]]
			then
				local count = math.min(500, ing[2] * unit_count / 5)
				if not min_science_requirements[ing[1]]
				then
					min_science_requirements[ing[1]] = count
				else
					min_science_requirements[ing[1]] = math.min(min_science_requirements[ing[1]], count)
				end
			end
		end
	end
end

for _, technology in pairs(data.raw["technology"])
do
	local science_pack = nil
	for _, effect in pairs(technology.effects or {})
	do
		if effect.type == "unlock-recipe" and min_science_requirements[effect.recipe]
		then
			science_pack = effect.recipe
			break
		end
	end
	if science_pack ~= nil
	then
		---@type data.TechnologyPrototype
		local trigger_tech = {
			type = "technology",
			name = technology.name.."-2",
			research_trigger = {
				type = "craft-item",
				item = science_pack,
				count = math.min(500, min_science_requirements[science_pack]),
			},
			icon = technology.icon,
			icons = technology.icons,
			icon_size = technology.icon_size,
			prerequisites = {technology.name},
			upgrade = true,
		}
		technology["_science_extra_trigger_technology"] = technology.name.."-2"
		data:extend{trigger_tech}
	end
end

mercurial