From ef72cf2594a9b78309bb3ee4312101ea44d13788 Mon Sep 17 00:00:00 2001 From: Brevven Date: Sat, 24 Jul 2021 22:26:48 -0700 Subject: [PATCH] refactor --- advanced-carbon-furnace.lua | 2 +- changelog.txt | 5 ++ data-util.lua | 119 +++++++++++++++++++++--------------- info.json | 2 +- me.lua | 38 ++++++++++++ tungsten-ore.lua | 2 +- tungsten-recipe-5d.lua | 4 +- tungsten-recipe-se.lua | 6 +- tungsten-recipe-updates.lua | 20 +++--- tungsten-recipe.lua | 22 +++---- 10 files changed, 142 insertions(+), 78 deletions(-) create mode 100644 me.lua diff --git a/advanced-carbon-furnace.lua b/advanced-carbon-furnace.lua index 6a36c96..49ad861 100644 --- a/advanced-carbon-furnace.lua +++ b/advanced-carbon-furnace.lua @@ -1,7 +1,7 @@ local futil = require("util"); local util = require("__bztungsten__.data-util"); -if util.get_setting("bztungsten-advanced-carbon-furnace") == "yes" and not mods["Krastorio2"] then +if util.me.get_setting("bztungsten-advanced-carbon-furnace") == "yes" and not mods["Krastorio2"] then log("Using advanced carbon furnace") data:extend({ { diff --git a/changelog.txt b/changelog.txt index f5dd493..e61d113 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,9 @@ --------------------------------------------------------------------------------------------------- +Version: 0.3.6 +Date: 2021-07-26 + Changes: + - Minor refactor +--------------------------------------------------------------------------------------------------- Version: 0.3.5 Date: 2021-07-25 Features: diff --git a/data-util.lua b/data-util.lua index ec88730..df8a65a 100644 --- a/data-util.lua +++ b/data-util.lua @@ -1,15 +1,8 @@ +local me = require("me") local util = {} -util.name = "bztungsten" -util.tungsten_ore = "tungsten-ore" -util.tungsten_plate = "tungsten-plate" -util.tungsten_carbide_recipe = "tungsten-carbide" - -util.tungsten_processing = "tungsten-processing" -if mods.bobplates then - util.tungsten_processing = "bz-tungsten-processing" - util.tungsten_carbide_recipe = "bz-tungsten-carbide" -end +util.me = me +util.get_setting = util.me.get_setting function util.fe_plus(sub) if mods["FactorioExtended-Plus-"..sub] then @@ -17,30 +10,6 @@ function util.fe_plus(sub) end end -function util.fluid_amount() - if mods["Krastorio2"] then - local amt = util.get_setting("bztungsten-mining-fluid-amount-k2") - return amt and amt or 1 - end - local amt = util.get_setting("bztungsten-mining-fluid-amount") - return amt and amt or 10 -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 - function util.get_stack_size(default) if mods["Krastorio2"] then size = tonumber(krastorio.general.getSafeSettingValue("kr-stack-size")) @@ -106,8 +75,8 @@ 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 data.raw.recipe[recipe_name] then + 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) add_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity) @@ -129,7 +98,7 @@ 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] then + if data.raw.recipe[recipe_name] and data.raw.item[product] 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) @@ -144,8 +113,8 @@ 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 data.raw.recipe[recipe_name] then + 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) replace_ingredient(data.raw.recipe[recipe_name].expensive, old, new) @@ -169,7 +138,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) @@ -195,8 +164,8 @@ 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 data.raw.recipe[recipe_name] then + 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) replace_some_ingredient(data.raw.recipe[recipe_name].expensive, old, old_amount, new, new_amount) @@ -212,11 +181,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 @@ -227,7 +194,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) @@ -281,12 +248,66 @@ 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 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) + multiply_time(data.raw.recipe[recipe_name].expensive, factor) + end +end + +function multiply_time(recipe, factor) + if recipe then + if recipe.energy_required then + recipe.energy_required = recipe.energy_required * factor + 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 +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 +end + +function util.set_to_founding(recipe) + util.set_category(recipe, "founding") + 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/info.json b/info.json index 8f44093..6b516e7 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "bztungsten", - "version": "0.3.5", + "version": "0.3.6", "factorio_version": "1.1", "title": "Tungsten", "author": "Brevven", diff --git a/me.lua b/me.lua new file mode 100644 index 0000000..82e240b --- /dev/null +++ b/me.lua @@ -0,0 +1,38 @@ +local me = {} + +me.name = "bztungsten" +me.tungsten_ore = "tungsten-ore" +me.tungsten_plate = "tungsten-plate" +me.tungsten_carbide_recipe = "tungsten-carbide" + +me.tungsten_processing = "tungsten-processing" +if mods.bobplates then + me.tungsten_processing = "bz-tungsten-processing" + me.tungsten_carbide_recipe = "bz-tungsten-carbide" +end + +function me.fluid_amount() + if mods["Krastorio2"] then + local amt = me.get_setting("bztungsten-mining-fluid-amount-k2") + return amt and amt or 1 + end + local amt = me.get_setting("bztungsten-mining-fluid-amount") + return amt and amt or 10 +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/tungsten-ore.lua b/tungsten-ore.lua index 823f2b1..9d15ea7 100644 --- a/tungsten-ore.lua +++ b/tungsten-ore.lua @@ -28,7 +28,7 @@ data:extend({ hardness = 1, mining_particle = "tungsten-ore-particle", mining_time = 2, - fluid_amount = util.fluid_amount(), + fluid_amount = util.me.fluid_amount(), required_fluid=(mods["Krastorio2"] and "mineral-water" or "water"), result = "tungsten-ore" }, diff --git a/tungsten-recipe-5d.lua b/tungsten-recipe-5d.lua index c7f9b8c..2366674 100644 --- a/tungsten-recipe-5d.lua +++ b/tungsten-recipe-5d.lua @@ -81,7 +81,7 @@ if mods["5dim_core"] then enabled = true, energy_required = 50, ingredients = {{"tungsten-dust", 85}}, - result = util.tungsten_plate, + result = util.me.tungsten_plate, result_count = 50, }, { @@ -95,7 +95,7 @@ if mods["5dim_core"] then enabled = true, energy_required = 500, ingredients = {{"tungsten-plate", 85}}, - result = util.tungsten_plate, + result = util.me.tungsten_plate, result_count = 20, } }) diff --git a/tungsten-recipe-se.lua b/tungsten-recipe-se.lua index 2ef66e7..53d6234 100644 --- a/tungsten-recipe-se.lua +++ b/tungsten-recipe-se.lua @@ -21,7 +21,7 @@ if mods["space-exploration"] then {name = "se-vulcanite-block", amount = 1}, }, results = { - {name = util.tungsten_plate, amount = 8}, + {name = util.me.tungsten_plate, amount = 8}, }, icons = { @@ -50,7 +50,7 @@ if mods["space-exploration"] then {name = "se-vulcanite-block", amount = 1}, }, results = { - {name = util.tungsten_plate, amount = 12}, + {name = util.me.tungsten_plate, amount = 12}, }, icons = { @@ -89,5 +89,5 @@ if mods["aai-industry"] and not mods["Krastorio2"] and not mods.bzfoundry then }, } }) - util.add_effect(util.tungsten_processing, {type = "unlock-recipe", recipe= "tungsten-carbide-coal"}) + util.add_effect(util.me.tungsten_processing, {type = "unlock-recipe", recipe= "tungsten-carbide-coal"}) end diff --git a/tungsten-recipe-updates.lua b/tungsten-recipe-updates.lua index e6b8105..af83aa7 100644 --- a/tungsten-recipe-updates.lua +++ b/tungsten-recipe-updates.lua @@ -1,7 +1,7 @@ local util = require("__bztungsten__.data-util"); -- Lamps -util.add_prerequisite("optics", util.tungsten_processing) +util.add_prerequisite("optics", util.me.tungsten_processing) util.set_tech_recipe("optics", {{"automation-science-pack", 1}, {"logistic-science-pack", 1}}) util.add_ingredient("small-lamp", "tungsten-plate", 1) @@ -11,14 +11,14 @@ util.add_ingredient("rail-signal", "small-lamp", 1) util.add_ingredient("rail-chain-signal", "small-lamp", 1) -- Ammunition -util.add_prerequisite("military-2", util.tungsten_processing) +util.add_prerequisite("military-2", util.me.tungsten_processing) util.replace_ingredient("piercing-rounds-magazine", "copper-plate", "tungsten-plate") util.replace_ingredient("piercing-shotgun-shell", "copper-plate", "tungsten-plate") util.replace_ingredient("cannon-shell", "copper-plate", "tungsten-plate") -- Tooling -util.add_prerequisite("automation-2", util.tungsten_processing) +util.add_prerequisite("automation-2", util.me.tungsten_processing) util.replace_some_ingredient("assembling-machine-2", "steel-plate", 1, "tungsten-carbide", 1) util.add_ingredient("assembling-machine-3", "tungsten-carbide", 4) util.add_ingredient("chemical-plant", "tungsten-carbide", 2) @@ -28,12 +28,12 @@ util.add_ingredient("electric-furnace", "tungsten-carbide", 1) util.replace_ingredient("accumulator", "iron-plate", "tungsten-plate") util.add_ingredient("oil-refinery", "tungsten-plate", 1) util.add_ingredient("chemical-plant", "tungsten-plate", 1) -util.add_prerequisite("engine", util.tungsten_processing) +util.add_prerequisite("engine", util.me.tungsten_processing) util.add_ingredient("engine-unit", "tungsten-plate", 1) -- Hot stuff -util.add_prerequisite("advanced-materials-processing-2", util.tungsten_processing) +util.add_prerequisite("advanced-materials-processing-2", util.me.tungsten_processing) util.replace_some_ingredient("electric-furnace", "steel-plate", 5, "tungsten-plate", 5) util.replace_some_ingredient("heat-exchanger", "copper-plate", 50, "tungsten-plate", 50) util.replace_some_ingredient("heat-pipe", "copper-plate", 10, "tungsten-plate", 10) @@ -45,7 +45,7 @@ util.add_ingredient("nuclear-fuel", "tungsten-plate", 1) -- Rocket engine nozzle techs util.add_effect("rocketry", { type = "unlock-recipe", recipe = "rocket-engine-nozzle" }) util.add_prerequisite("rocket-silo", "rocketry") -if util.get_setting("bztungsten-avoid-military") == "yes" then +if util.me.get_setting("bztungsten-avoid-military") == "yes" then util.set_tech_recipe("rocketry", {{"automation-science-pack", 1}, {"logistic-science-pack", 1}}) end @@ -93,13 +93,13 @@ if mods["Krastorio2"] then util.set_tech_recipe("optics", {{"basic-tech-card", 1}, {"automation-science-pack", 1}, {"logistic-science-pack", 1}}) util.set_tech_recipe("oil-processing", {{"basic-tech-card", 1}, {"automation-science-pack", 1}, {"logistic-science-pack", 1}}) util.set_tech_recipe("plastics", {{"basic-tech-card", 1}, {"automation-science-pack", 1}, {"logistic-science-pack", 1}}) - if util.get_setting("bztungsten-avoid-military") == "yes" then + if util.me.get_setting("bztungsten-avoid-military") == "yes" then util.set_tech_recipe("rocketry", {{"basic-tech-card", 1}, {"automation-science-pack", 1}, {"logistic-science-pack", 1}}) end util.remove_prerequisite("kr-sentinel", "optics") - util.add_prerequisite(util.tungsten_processing, "kr-mineral-water-gathering") + util.add_prerequisite(util.me.tungsten_processing, "kr-mineral-water-gathering") util.remove_prerequisite("kr-mineral-water-gathering", "fluid-handling") -- engines come later with Tungsten. @@ -237,7 +237,7 @@ util.add_ingredient("vtk-deepcore-mining-drill-advanced", "tungsten-carbide", 50 -- Advanced Mining Drills util.add_ingredient("advanced-mining-drill", "tungsten-carbide", 1) -util.add_prerequisite("advanced-mining-drill",util.tungsten_processing) +util.add_prerequisite("advanced-mining-drill",util.me.tungsten_processing) util.add_ingredient("elite-mining-drill", "tungsten-carbide", 5) util.add_ingredient("ultimate-mining-drill", "tungsten-carbide", 10) @@ -304,7 +304,7 @@ util.add_ingredient("mega-furnace", "tungsten-plate", 5) -- Mundas util.add_ingredient("munda_big_machine", "tungsten-carbide", 32) -util.add_prerequisite("munda_big_machine", util.tungsten_processing) +util.add_prerequisite("munda_big_machine", util.me.tungsten_processing) -- Realistic reactors diff --git a/tungsten-recipe.lua b/tungsten-recipe.lua index f316796..8c984ee 100644 --- a/tungsten-recipe.lua +++ b/tungsten-recipe.lua @@ -19,7 +19,7 @@ data:extend({ }, { type = "item", - name = util.tungsten_plate, + name = util.me.tungsten_plate, icon = "__bztungsten__/graphics/icons/tungsten-plate.png", icon_size = 128, icon_mipmaps = 3, subgroup = "raw-material", @@ -32,7 +32,7 @@ end data:extend({ { type = "recipe", - name = util.tungsten_plate, + name = util.me.tungsten_plate, category = "smelting", subgroup = "raw-material", order = "d[tungsten-plate]", @@ -50,26 +50,26 @@ data:extend({ enabled = false, energy_required = 24, ingredients = {{"tungsten-ore", 15}}, - result = util.tungsten_plate, + result = util.me.tungsten_plate, result_count = 5, } or { enabled = false, energy_required = 32/15, ingredients = {{"tungsten-ore", mods.bobplates and 4 or 2}}, - result = util.tungsten_plate, + result = util.me.tungsten_plate, }), expensive = { enabled = false, energy_required = 9.6, ingredients = {{"tungsten-ore", 3}}, - result = util.tungsten_plate + result = util.me.tungsten_plate } }, { type = "technology", - name = util.tungsten_processing, + name = util.me.tungsten_processing, icons = {{ icon = mods.bobplates and "__bobplates__/graphics/icons/technology/tungsten-processing.png" @@ -80,11 +80,11 @@ data:extend({ { { type = "unlock-recipe", - recipe = util.tungsten_plate + recipe = util.me.tungsten_plate }, { type = "unlock-recipe", - recipe = util.tungsten_carbide_recipe, + recipe = util.me.tungsten_carbide_recipe, }, mods["TheBigFurnace"] and { type = "unlock-recipe", @@ -117,7 +117,7 @@ data:extend({ energy_required = 6, --TODO ingredients = {{"tungsten-ore", 100}}, results = { - {type="item", name = util.tungsten_plate, amount=50}, + {type="item", name = util.me.tungsten_plate, amount=50}, } }, expensive = @@ -126,7 +126,7 @@ data:extend({ energy_required = 15, --TODO ingredients = {{"tungsten-ore", 99}}, results = { - {type="item", name = util.tungsten_plate, amount=33}, + {type="item", name = util.me.tungsten_plate, amount=33}, } } } or nil, @@ -135,7 +135,7 @@ data:extend({ data:extend({ { type = "recipe", - name = util.tungsten_carbide_recipe, + name = util.me.tungsten_carbide_recipe, category = (mods["Krastorio2"] and "smelting" or "smelting-carbon"), subgroup = "raw-material", order = "z[tungsten-carbide]",