aquilo-start/aqs_util.lua

changeset 19
adbc5c74f279
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aquilo-start/aqs_util.lua	Wed Aug 27 10:26:12 2025 +0300
@@ -0,0 +1,187 @@
+---@class AqsScienceArgs
+---@field count? int
+---@field count_formula? string
+---@field time int
+
+--- @param args AqsScienceArgs
+--- @return data.TechnologyUnit
+function red_science(args)
+	return {
+		count = args.count,
+		count_formula = args.count_formula,
+		time = args.time,
+		ingredients = {
+			{"automation-science-pack", 1},
+		},
+	}
+end
+
+--- @param args AqsScienceArgs
+function green_science(args)
+	local x = red_science(args)
+	table.insert(x.ingredients, {"logistic-science-pack", 1})
+	return x
+end
+
+--- @param args AqsScienceArgs
+function blue_science(args)
+	local x = green_science(args)
+	table.insert(x.ingredients, {"chemical-science-pack", 1})
+	return x
+end
+
+--- @param args AqsScienceArgs
+function cryo_science(args)
+	local x = blue_science(args)
+	table.insert(x.ingredients, {"cryogenic-science-pack", 1})
+	return x
+end
+
+---@return data.ItemIngredientPrototype
+function item(name, amount)
+	return {type="item", name=name, amount=amount or 1}
+end
+
+---@return data.FluidIngredientPrototype
+function fluid(name, amount)
+	return {type = "fluid", name = name, amount = amount}
+end
+
+---@param technology data.TechnologyPrototype
+---@param x string
+function remove_recipe_effect(technology, x)
+	---@type (data.Modifier)[]
+	local list2 = {}
+	for k, v in pairs(technology.effects)
+	do
+		if v.type ~= "unlock-recipe" or v.recipe ~= x
+		then
+			table.insert(list2, v)
+		end
+	end
+	technology.effects = list2
+end
+
+---@param technology data.TechnologyPrototype
+---@param x data.Modifier
+function remove_effect(technology, x)
+	---@type (data.Modifier)[]
+	local list2 = {}
+	for _, technology_effect in pairs(technology.effects or {})
+	do
+		local match = true
+		for key, value in pairs(x)
+		do
+			if technology_effect[key] ~= value
+			then
+				match = false
+				break
+			end
+		end
+		if not match
+		then
+			table.insert(list2, technology_effect)
+		end
+	end
+	technology.effects = list2
+end
+
+---@param recipe_name string
+---@return data.UnlockRecipeModifier
+function unlock_recipe(recipe_name)
+	return {type = "unlock-recipe", recipe = recipe_name}
+end
+
+---@param recipe_name string
+---@return data.ChangeRecipeProductivityModifier
+function recipe_productivity(recipe_name)
+	return {
+		type = "change-recipe-productivity",
+		recipe = recipe_name,
+		change = 0.1,
+	}
+end
+
+---@alias SciencePack "automation-science-pack"|"logistic-science-pack"|"chemical-science-pack"|"production-science-pack"|"utility-science-pack"|"metallurgic-science-pack"|"nuclear-science-pack"|"electromagnetic-science-pack"|"cryogenic-science-pack"|"promethium-science-pack"|"agricultural-science-pack"
+
+---@param technology data.TechnologyPrototype
+---@param x SciencePack
+function is_in_unit(technology, x)
+	print(technology.name)
+	if technology.unit
+	then
+		for k,v in pairs(technology.unit.ingredients)
+		do
+			if v[1] == x
+			then
+				return true
+			end
+		end
+	end
+	return false
+end
+
+---@param technology data.TechnologyPrototype
+---@param x string
+---@param y string
+function replace_in_prerequisites(technology, x, y)
+	if technology.prerequisites ~= nil
+	then
+		for k, v in pairs(technology.prerequisites)
+		do
+			if technology.prerequisites[k] == x
+			then
+				technology.prerequisites[k] = y
+				break
+			end
+		end
+	end
+end
+
+---@param technology data.TechnologyPrototype
+---@param x SciencePack
+---@param y SciencePack
+function replace_in_unit(technology, x, y)
+	if technology.unit
+	then
+		technology.unit = table.deepcopy(technology.unit)
+		for k,v in pairs(technology.unit.ingredients)
+		do
+			if v[1] == x
+			then
+				v[1] = y
+				break
+			end
+		end
+	end
+	replace_in_prerequisites(technology, x, y)
+end
+
+---@param technology data.TechnologyPrototype
+---@param x SciencePack
+function delete_from_unit(technology, x)
+	if technology.unit
+	then
+		local new_ingredients = {}
+		for k,v in pairs(technology.unit.ingredients)
+		do
+			if v[1] ~= x
+			then
+				table.insert(new_ingredients, v)
+			end
+		end
+		technology.unit.ingredients = new_ingredients
+	end
+	if technology.prerequisites ~= nil
+	then
+		local new_prerequisites = {}
+		for k, v in pairs(technology.prerequisites)
+		do
+			if v ~= x
+			then
+				table.insert(new_prerequisites, v)
+			end
+		end
+		technology.prerequisites = new_prerequisites
+	end
+end

mercurial