From 72af4a1d6325ed4cfdff96d01eedd7086d6af087 Mon Sep 17 00:00:00 2001 From: Brevven Date: Fri, 21 Jan 2022 23:10:42 -0800 Subject: [PATCH] lib --- control-util.lua | 23 ++++ data-util.lua | 287 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 293 insertions(+), 17 deletions(-) create mode 100644 control-util.lua diff --git a/control-util.lua b/control-util.lua new file mode 100644 index 0000000..968b924 --- /dev/null +++ b/control-util.lua @@ -0,0 +1,23 @@ +local me = require("me") + +local util = {} +util.me = me + +function decode(data) + if type(data) == "string" then return data end + local str = {} + for i = 2, #data do + str[i-1] = decode(data[i]) + end + return table.concat(str, "") +end + +function util.get_list() + local p = game.item_prototypes[me.name.."-list"] + if p then + data = p.localised_description + return decode(data) + end +end + +return util diff --git a/data-util.lua b/data-util.lua index 03359d2..f1815b6 100644 --- a/data-util.lua +++ b/data-util.lua @@ -8,6 +8,21 @@ local util = {} util.me = me util.get_setting = util.me.get_setting +util.titanium_plate = "" +util.titanium_processing = "" + +if mods["FactorioExtended-Plus-Core"] then + util.titanium_plate = "titanium-alloy" +else + util.titanium_plate = "titanium-plate" +end + +if mods["pyrawores"] then + util.titanium_processing = "titanium-mk01" +else + util.titanium_processing = "titanium-processing" +end + function util.fe_plus(sub) if mods["FactorioExtended-Plus-"..sub] then return true @@ -68,14 +83,33 @@ function util.remove_prerequisite(technology_name, prerequisite) end end + -- Add an effect to a given technology function util.add_effect(technology_name, effect) local technology = data.raw.technology[technology_name] if technology then + if not technology.effects then technology.effects = {} end table.insert(technology.effects, effect) end end +-- remove recipe unlock effect from a given technology +function util.remove_recipe_effect(technology_name, recipe_name) + local technology = data.raw.technology[technology_name] + local index = -1 + if technology then + for i, effect in pairs(technology.effects) do + if effect.type == "unlock-recipe" and effect.recipe == recipe_name then + index = i + break + end + end + if index > -1 then + table.remove(technology.effects, index) + end + end +end + -- Set technology ingredients function util.set_tech_recipe(technology_name, ingredients) local technology = data.raw.technology[technology_name] @@ -84,10 +118,42 @@ function util.set_tech_recipe(technology_name, ingredients) end end +function util.set_enabled(recipe_name, enabled) + if data.raw.recipe[recipe_name] then + if data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].normal.enabled = enabled end + if data.raw.recipe[recipe_name].expensive then data.raw.recipe[recipe_name].expensive.enabled = enabled end + if not data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].enabled = enabled end + end +end + +-- Add a given quantity of ingredient to a given recipe +function util.add_or_add_to_ingredient(recipe_name, ingredient, quantity) + if me.bypass[recipe_name] then return end + if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then + me.add_modified(recipe_name) + add_or_add_to_ingredient(data.raw.recipe[recipe_name], ingredient, quantity) + add_or_add_to_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity) + add_or_add_to_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity) + end +end + +function add_or_add_to_ingredient(recipe, ingredient, quantity) + if recipe ~= nil and recipe.ingredients ~= nil then + for i, existing in pairs(recipe.ingredients) do + if existing[1] == ingredient or existing.name == ingredient then + add_to_ingredient(recipe, ingredient, quantity) + return + end + end + table.insert(recipe.ingredients, {ingredient, quantity}) + end +end + -- Add a given quantity of ingredient to a given recipe function util.add_ingredient(recipe_name, ingredient, quantity) if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then + me.add_modified(recipe_name) add_ingredient(data.raw.recipe[recipe_name], ingredient, quantity) add_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity) add_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity) @@ -106,10 +172,37 @@ function add_ingredient(recipe, ingredient, quantity) end end +-- Set an ingredient to a given quantity +function util.set_ingredient(recipe_name, ingredient, quantity) + if me.bypass[recipe_name] then return end + if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then + me.add_modified(recipe_name) + set_ingredient(data.raw.recipe[recipe_name], ingredient, quantity) + set_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity) + set_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity) + end +end + +function set_ingredient(recipe, ingredient, quantity) + if recipe ~= nil and recipe.ingredients ~= nil then + for i, existing in pairs(recipe.ingredients) do + if existing[1] == ingredient then + existing[2] = quantity + return + elseif existing.name == ingredient then + existing.amount = quantity + existing.amount_min = nil + existing.amount_max = nil + return + end + end + table.insert(recipe.ingredients, {ingredient, quantity}) + end +end -- Add a given quantity of product to a given recipe. -- Only works for recipes with multiple products function util.add_product(recipe_name, product) - if data.raw.recipe[recipe_name] and data.raw.item[product] then + if data.raw.recipe[recipe_name] and (data.raw.item[product[1]] or data.raw.item[product.name]) then add_product(data.raw.recipe[recipe_name], product) add_product(data.raw.recipe[recipe_name].normal, product) add_product(data.raw.recipe[recipe_name].expensive, product) @@ -117,8 +210,15 @@ function util.add_product(recipe_name, product) end function add_product(recipe, product) - if recipe ~= nil and recipe.results ~= nil then - table.insert(recipe.results, product) + if recipe ~= nil then + if not recipe.normal then + if recipe.results == nil then + recipe.results = {{recipe.result, recipe.result_count and recipe.result_count or 1}} + end + recipe.result = nil + recipe.result_count = nil + table.insert(recipe.results, product) + end end end @@ -126,6 +226,7 @@ end function util.replace_ingredient(recipe_name, old, new) if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] and data.raw.item[new] then + me.add_modified(recipe_name) replace_ingredient(data.raw.recipe[recipe_name], old, new) replace_ingredient(data.raw.recipe[recipe_name].normal, old, new) replace_ingredient(data.raw.recipe[recipe_name].expensive, old, new) @@ -151,6 +252,7 @@ end function util.remove_ingredient(recipe_name, old) if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] then + me.add_modified(recipe_name) remove_ingredient(data.raw.recipe[recipe_name], old) remove_ingredient(data.raw.recipe[recipe_name].normal, old) remove_ingredient(data.raw.recipe[recipe_name].expensive, old) @@ -176,6 +278,7 @@ end function util.replace_some_ingredient(recipe_name, old, old_amount, new, new_amount) if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] and data.raw.item[new] then + me.add_modified(recipe_name) replace_some_ingredient(data.raw.recipe[recipe_name], old, old_amount, new, new_amount) replace_some_ingredient(data.raw.recipe[recipe_name].normal, old, old_amount, new, new_amount) replace_some_ingredient(data.raw.recipe[recipe_name].expensive, old, old_amount, new, new_amount) @@ -204,8 +307,9 @@ end -- multiply the cost, energy, and results of a recipe by a multiple function util.multiply_recipe(recipe_name, multiple) - if me.bypass[recipe_name] then return end + me.add_modified(recipe_name) if data.raw.recipe[recipe_name] then + if me.bypass[recipe_name] then return end multiply_recipe(data.raw.recipe[recipe_name], multiple) multiply_recipe(data.raw.recipe[recipe_name].normal, multiple) multiply_recipe(data.raw.recipe[recipe_name].expensive, multiple) @@ -274,6 +378,32 @@ function has_ingredient(recipe, ingredient) return false end +-- Remove a product from a recipe, WILL NOT remove the only product +function util.remove_product(recipe_name, old) + me.add_modified(recipe_name) + if data.raw.recipe[recipe_name] then + if me.bypass[recipe_name] then return end + remove_product(data.raw.recipe[recipe_name], old) + remove_product(data.raw.recipe[recipe_name].normal, old) + remove_product(data.raw.recipe[recipe_name].expensive, old) + end +end + +function remove_product(recipe, old) + index = -1 + if recipe ~= nil and recipe.results ~= nil then + for i, result in pairs(recipe.results) do + if result.name == old or result[1] == old then + index = i + break + end + end + if index > -1 then + table.remove(recipe.results, index) + end + end +end + -- Replace one product with another in a recipe function util.replace_product(recipe_name, old, new) if data.raw.recipe[recipe_name] then @@ -298,6 +428,10 @@ end -- Remove an element of type t and name from data.raw function util.remove_raw(t, name) + if not data.raw[t] then + log(t.." not found in data.raw") + return + end if data.raw[t][name] then for i, elem in pairs(data.raw[t]) do if elem.name == name then @@ -309,9 +443,10 @@ function util.remove_raw(t, name) end -- Multiply energy required -function util.multiply_time(recipe, factor) - if me.bypass[recipe_name] then return end +function util.multiply_time(recipe_name, factor) + me.add_modified(recipe_name) if data.raw.recipe[recipe_name] then + if me.bypass[recipe_name] then return end multiply_time(data.raw.recipe[recipe_name], factor) multiply_time(data.raw.recipe[recipe_name].normal, factor) multiply_time(data.raw.recipe[recipe_name].expensive, factor) @@ -326,20 +461,52 @@ function multiply_time(recipe, factor) end end +-- Add to energy required +function util.add_time(recipe_name, amount) + me.add_modified(recipe_name) + if data.raw.recipe[recipe_name] then + if me.bypass[recipe_name] then return end + add_time(data.raw.recipe[recipe_name], amount) + add_time(data.raw.recipe[recipe_name].normal, amount) + add_time(data.raw.recipe[recipe_name].expensive, amount) + end +end + +function add_time(recipe, amount) + if recipe then + if recipe.energy_required then + recipe.energy_required = recipe.energy_required + amount + end + end +end + -- Set recipe category -function util.set_category(recipe, category) - if me.bypass[recipe_name] then return end - if data.raw.recipe[recipe] then - data.raw.recipe[recipe].category = category - end +function util.set_category(recipe_name, category) + if me.bypass[recipe_name] then return end + if data.raw.recipe[recipe_name] then + me.add_modified(recipe_name) + data.raw.recipe[recipe_name].category = category + end end -- Set recipe subgroup -function util.set_subgroup(recipe, subgroup) - if me.bypass[recipe_name] then return end - if data.raw.recipe[recipe] then - data.raw.recipe[recipe].subgroup = subgroup - end +function util.set_subgroup(recipe_name, subgroup) + if me.bypass[recipe_name] then return end + if data.raw.recipe[recipe_name] then + me.add_modified(recipe_name) + data.raw.recipe[recipe_name].subgroup = subgroup + end +end + +-- Set recipe icons +function util.set_icons(recipe_name, icons) + if me.bypass[recipe_name] then return end + if data.raw.recipe[recipe_name] then + me.add_modified(recipe_name) + data.raw.recipe[recipe_name].icons = icons + data.raw.recipe[recipe_name].icon = nil + data.raw.recipe[recipe_name].icon_size = nil + end end function util.set_to_founding(recipe) @@ -376,7 +543,7 @@ function add_to_ingredient(recipe, it, amount) return end if ingredient[1] == it then - ingredient[2] = ingredients[2] + amount + ingredient[2] = ingredient[2] + amount return end end @@ -410,4 +577,90 @@ function add_to_product(recipe, product, amount) end end +-- Adds a result to a mineable type +function util.add_minable_result(t, name, result) + if data.raw[t] and data.raw[t][name] and data.raw[t][name].minable then + if data.raw[t][name].minable.result and not data.raw[t][name].minable.results then + data.raw[t][name].minable.results = { + {data.raw[t][name].minable.result ,data.raw[t][name].minable.count}} + data.raw[t][name].minable.result = nil + data.raw[t][name].minable.result_count = nil + end + if data.raw[t][name].minable.results then + table.insert(data.raw[t][name].minable.results, result) + end + end +end + + +local function insert(nodes, node, value) + table.insert(node, value) -- store as parameter + if 21 == #node then + node = {""} + table.insert(nodes, node) + end + return node +end + +local function encode(data) + local node = {""} + local root = {node} + local n = string.len(data) + for i = 1,n,200 do + local value = string.sub(data, i, i+199) + node = insert(root, node, value) + end + while #root > 20 do + local nodes,node = {},{""} + for _, value in ipairs(root) do + node = insert(nodes, node, value) + end + root = nodes + end + if #root == 1 then root = root[1] else + table.insert(root, 1, "") -- no locale template + end + return #root < 3 and (root[2] or "") or root +end + +function decode(data) + if type(data) == "string" then return data end + local str = {} + for i = 2, #data do + str[i-1] = decode(data[i]) + end + return table.concat(str, "") +end + +function util.create_list() + if #me.list>0 then + if not data.raw.item[me.name.."-list"] then + data:extend({{ + type="item", + name=me.name.."-list", + localised_description = "", + enabled=false, + icon = "__core__/graphics/empty.png", + icon_size = 1, + stack_size = 1, + flags = {"hidden", "hide-from-bonus-gui"} + }}) + end + + local have = {} + local list = {} + for i, recipe in pairs(me.list) do + if not have[recipe] then + have[recipe] = true + table.insert(list, recipe) + end + end + + if #list>0 then + data.raw.item[me.name.."-list"].localised_description = + encode(decode(data.raw.item[me.name.."-list"].localised_description).."\n"..table.concat(list, "\n")) + end + end +end + return util