From dd5770a2e68675fc3c34a035835d12c21563143a Mon Sep 17 00:00:00 2001 From: Brevven Date: Sat, 24 Jul 2021 22:05:10 -0700 Subject: [PATCH] fixes, refactor --- changelog.txt | 13 +++++ data-util.lua | 79 ++++++++++---------------- foundry-updates.lua | 12 +++- info.json | 2 +- locale/en/foundry.cfg | 4 +- me.lua | 47 +++++++++++++++ prototypes/coke.lua | 5 +- prototypes/entity/electric-foundry.lua | 5 +- prototypes/entity/foundry.lua | 4 +- prototypes/foundry.lua | 2 +- settings.lua | 6 ++ 11 files changed, 117 insertions(+), 62 deletions(-) create mode 100644 me.lua diff --git a/changelog.txt b/changelog.txt index 2ab0f1a..d611317 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,17 @@ --------------------------------------------------------------------------------------------------- +Version: 0.0.5 +Date: 2021-07-21 + Changes: + - Minor refactor +--------------------------------------------------------------------------------------------------- +Version: 0.0.4 +Date: 2021-07-21 + Changes: + - Both foundry items are more expensive + - Both foundry entities use more energy: burner doubled, electric quadrupled + Features: + - Setting to enable other "assembling machine" entities to do foundry tasks. +--------------------------------------------------------------------------------------------------- Version: 0.0.3 Date: 2021-07-19 Features: diff --git a/data-util.lua b/data-util.lua index 5512c84..74c9e35 100644 --- a/data-util.lua +++ b/data-util.lua @@ -1,39 +1,7 @@ +local me = require("me") local util = {} -util.name = "bzfoundry" - -function util.smelt() - return util.get_setting("bzfoundry-smelt") -end - -function util.carbon() - return util.get_setting("bzfoundry-hydrocarbon") -end - -function util.carbonrecipe() - local carbon = util.carbon() - if carbon == "coke" then - return "coke" - elseif carbon == "solid-fuel" then - return "solid-fuel-from-coal" - end - return nil -end - -function util.get_setting(name) - if settings.startup[name] == nil then - return nil - end - return settings.startup[name].value -end - - -local bypass = {} -if util.get_setting(util.name.."-recipe-bypass") then - for recipe in string.gmatch(util.get_setting(util.name.."-recipe-bypass"), '[^",%s]+') do - bypass[recipe] = true - end -end +util.me = me function util.get_stack_size(default) if mods["Krastorio2"] then @@ -100,7 +68,7 @@ end -- Add a given quantity of ingredient to a given recipe function util.add_ingredient(recipe_name, ingredient, quantity) - if bypass[recipe_name] then return end + if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then add_ingredient(data.raw.recipe[recipe_name], ingredient, quantity) add_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity) @@ -138,7 +106,7 @@ end -- Replace one ingredient with another in a recipe function util.replace_ingredient(recipe_name, old, new) - if bypass[recipe_name] then return end + if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] and data.raw.item[new] then replace_ingredient(data.raw.recipe[recipe_name], old, new) replace_ingredient(data.raw.recipe[recipe_name].normal, old, new) @@ -163,7 +131,7 @@ end -- Remove an ingredient from a recipe function util.remove_ingredient(recipe_name, old) - if bypass[recipe_name] then return end + if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] then remove_ingredient(data.raw.recipe[recipe_name], old) remove_ingredient(data.raw.recipe[recipe_name].normal, old) @@ -189,7 +157,7 @@ end -- Replace an amount of an ingredient in a recipe. Keep at least 1 of old. function util.replace_some_ingredient(recipe_name, old, old_amount, new, new_amount) - if bypass[recipe_name] then return end + if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] and data.raw.item[new] then 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) @@ -206,11 +174,9 @@ function replace_some_ingredient(recipe, old, old_amount, new, new_amount) end end for i, ingredient in pairs(recipe.ingredients) do - -- For final fixes if ingredient.name == old then ingredient.amount = math.max(1, ingredient.amount - old_amount) end - -- For updates if ingredient[1] == old then ingredient[2] = math.max(1, ingredient[2] - old_amount) end @@ -221,7 +187,7 @@ end -- multiply the cost, energy, and results of a recipe by a multiple function util.multiply_recipe(recipe_name, multiple) - if bypass[recipe_name] then return end + if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] then multiply_recipe(data.raw.recipe[recipe_name], multiple) multiply_recipe(data.raw.recipe[recipe_name].normal, multiple) @@ -275,17 +241,19 @@ end -- Remove an element of type t and name from data.raw function util.remove_raw(t, name) - for i, elem in pairs(data.raw[t]) do - if elem.name == name then - data.raw[t][i] = nil - break + if data.raw[t][name] then + for i, elem in pairs(data.raw[t]) do + if elem.name == name then + data.raw[t][i] = nil + break + end end end end -- Multiply energy required function util.multiply_time(recipe, factor) - if bypass[recipe_name] then return end + if me.bypass[recipe_name] then return end if data.raw.recipe[recipe_name] then multiply_time(data.raw.recipe[recipe_name], factor) multiply_time(data.raw.recipe[recipe_name].normal, factor) @@ -303,15 +271,15 @@ end -- Set recipe category function util.set_category(recipe, category) - if bypass[recipe_name] then return end + if me.bypass[recipe_name] then return end if data.raw.recipe[recipe] then data.raw.recipe[recipe].category = category end end --- Set recipe category +-- Set recipe subgroup function util.set_subgroup(recipe, subgroup) - if bypass[recipe_name] then return end + if me.bypass[recipe_name] then return end if data.raw.recipe[recipe] then data.raw.recipe[recipe].subgroup = subgroup end @@ -322,4 +290,17 @@ function util.set_to_founding(recipe) util.set_subgroup(recipe, "foundry-intermediate") end +-- Addc crafting category to an entity +function util.add_crafting_category(entity_type, entity, category) + if data.raw[entity_type][entity] then + for i, existing in pairs(data.raw[entity_type][entity].crafting_categories) do + if existing == category then + log(entity.." not adding "..new.." -- duplicate") + return + end + end + table.insert(data.raw[entity_type][entity].crafting_categories, category) + end +end + return util diff --git a/foundry-updates.lua b/foundry-updates.lua index 9b6d60f..f3cd0d9 100644 --- a/foundry-updates.lua +++ b/foundry-updates.lua @@ -1,14 +1,20 @@ local util = require("data-util") util.set_to_founding("steel-plate") -util.replace_some_ingredient("steel-plate", "iron-plate", 1, util.carbon(), 1) +util.replace_some_ingredient("steel-plate", "iron-plate", 1, util.me.carbon(), 1) util.multiply_time("stee-plate", 4/5) util.add_prerequisite("steel-processing", "foundry") util.set_to_founding("tungsten-carbide") -util.add_ingredient("tungsten-carbide", util.carbon(), 1) +util.add_ingredient("tungsten-carbide", util.me.carbon(), 1) util.set_to_founding("silicon") -util.add_ingredient("silicon", util.carbon(), 1) +util.add_ingredient("silicon", util.me.carbon(), 1) util.set_to_founding("cermet") + +-- K2 +for i, machine in pairs(util.me.get_other_machines()) do + log("BZAdding "..machine) + util.add_crafting_category("assembling-machine", machine, "founding") +end diff --git a/info.json b/info.json index 010a17d..f656da5 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "bzfoundry", - "version": "0.0.3", + "version": "0.0.5", "factorio_version": "1.1", "title": "Foundry", "author": "Brevven", diff --git a/locale/en/foundry.cfg b/locale/en/foundry.cfg index 41cf8df..41083a8 100644 --- a/locale/en/foundry.cfg +++ b/locale/en/foundry.cfg @@ -29,8 +29,10 @@ foundry=Use heat for founding, coking, etc. bzfoundry-recipe-bypass=Bypass recipes bzfoundry-smelt=Foundry can smelt bzfoundry-hydrocarbon=Hydrocarbon for founding +bzfoundry-other-machines=Other machines that can do founding [mod-setting-description] bzfoundry-recipe-bypass=Skip modifying these recipes (comma-separated list). bzfoundry-smelt=If true, the foundry building can also handle raw ore smelting. -bzfoundry-hydrocarbon=Which hydrocarbon to use for founding. The foundry building is also used for coking.\nIf [color=cyan]coke[/color], a coke item and recipe is added, if needed.\nIf [color=cyan]solid fuel[/color], an early but inefficient recipe is added.\nIf [color=cyan]coal[/color], that is used.\nIf [color=cyan]none[/color] no hydrocarbon is used in founding (not recommended). +bzfoundry-hydrocarbon=Which hydrocarbon to use for founding. The foundry building is also used for coking.\nIf [color=cyan]coke[/color], a coke item and recipe is added, if needed.\nIf [color=cyan]solid fuel[/color], an early but inefficient recipe is added.\nIf [color=cyan]coal[/color], that is used.\nIf [color=cyan]none[/color], no hydrocarbon is used in founding (not recommended). +bzfoundry-other-machines=List of other "assembling-machine" entities that can do "founding" recipes. Eg. Krastorio2's "kr-advanced-furnace", or AAII's "industrial-furnace" (comma-separated list). diff --git a/me.lua b/me.lua new file mode 100644 index 0000000..1812d15 --- /dev/null +++ b/me.lua @@ -0,0 +1,47 @@ +local me = {} + +me.name = "bzfoundry" + +function me.smelt() + return me.get_setting("bzfoundry-smelt") +end + +function me.carbon() + return me.get_setting("bzfoundry-hydrocarbon") +end + +function me.carbonrecipe() + local carbon = me.carbon() + if carbon == "coke" then + return "coke" + elseif carbon == "solid-fuel" then + return "solid-fuel-from-coal" + end + return nil +end + +function me.get_other_machines() + local machines = {} + if me.get_setting(me.name.."-other-machines") then + for machine in string.gmatch(me.get_setting(me.name.."-other-machines"), '[^",%s]+') do + table.insert(machines, machine) + end + end + return machines +end + +function me.get_setting(name) + if settings.startup[name] == nil then + return nil + end + return settings.startup[name].value +end + +me.bypass = {} +if me.get_setting(me.name.."-recipe-bypass") then + for recipe in string.gmatch(me.get_setting(me.name.."-recipe-bypass"), '[^",%s]+') do + me.bypass[recipe] = true + end +end + +return me diff --git a/prototypes/coke.lua b/prototypes/coke.lua index 2f4d0a6..f712f09 100644 --- a/prototypes/coke.lua +++ b/prototypes/coke.lua @@ -1,7 +1,6 @@ local util = require("data-util") - -if util.carbon() == "coke" and not data.raw.item["coke"] then +if util.me.carbon() == "coke" and not data.raw.item["coke"] then data:extend({ { type = "item", @@ -32,7 +31,7 @@ data:extend({ enabled=false, }, }) -elseif util.carbon() == "solid-fuel" then +elseif util.me.carbon() == "solid-fuel" then data:extend({ { type = "recipe", diff --git a/prototypes/entity/electric-foundry.lua b/prototypes/entity/electric-foundry.lua index 698ca2d..1b08908 100644 --- a/prototypes/entity/electric-foundry.lua +++ b/prototypes/entity/electric-foundry.lua @@ -26,8 +26,9 @@ data:extend({ }, collision_box = {{-1.7, -1.7}, {1.7, 1.7}}, selection_box = {{-2, -2}, {2, 2}}, - crafting_categories = {"founding", futil.smelt() and "smelting" or nil}, - energy_usage = "90kW", + crafting_categories = {"founding", futil.me.smelt() and "smelting" or nil}, + energy_usage = "360kW", + drain = "12kW", crafting_speed = 4, energy_source = { diff --git a/prototypes/entity/foundry.lua b/prototypes/entity/foundry.lua index ab0bbb2..09537f7 100644 --- a/prototypes/entity/foundry.lua +++ b/prototypes/entity/foundry.lua @@ -26,8 +26,8 @@ data:extend({ }, collision_box = {{-1.7, -1.7}, {1.7, 1.7}}, selection_box = {{-2, -2}, {2, 2}}, - crafting_categories = {"founding", futil.smelt() and "smelting" or nil}, - energy_usage = "90kW", + crafting_categories = {"founding", futil.me.smelt() and "smelting" or nil}, + energy_usage = "180kW", crafting_speed = 4, energy_source = { diff --git a/prototypes/foundry.lua b/prototypes/foundry.lua index 311cc75..e037e9e 100644 --- a/prototypes/foundry.lua +++ b/prototypes/foundry.lua @@ -39,7 +39,7 @@ data:extend({ prerequisites = {"automation"}, effects = { {type = "unlock-recipe", recipe = "foundry"}, - util.carbonrecipe() and {type = "unlock-recipe", recipe = util.carbonrecipe()}, + util.me.carbonrecipe() and {type = "unlock-recipe", recipe = util.me.carbonrecipe()}, }, unit = { count = 25, diff --git a/settings.lua b/settings.lua index 3200b9a..5d4ad4f 100644 --- a/settings.lua +++ b/settings.lua @@ -19,4 +19,10 @@ data:extend({ setting_type = "startup", default_value = false, }, + { + type = "string-setting", + name = "bzfoundry-other-machines", + setting_type = "startup", + default_value = "kr-advanced-furnace", + }, })