diff --git a/Bio_Industries_2/libs/category-functions.lua b/Bio_Industries_2/libs/category-functions.lua index c26ce71..4ced9cf 100644 --- a/Bio_Industries_2/libs/category-functions.lua +++ b/Bio_Industries_2/libs/category-functions.lua @@ -1,130 +1,130 @@ -local BioInd = require('common')('Bio_Industries_2') - -if not thxbob.lib.machine then - thxbob.lib.machine = {} -end - -function thxbob.lib.machine.has_category(machine, category_in) - local hasit = false - if machine and machine.crafting_categories then - for i, category in pairs(machine.crafting_categories) do - if category == category_in then - hasit = true - end - end - end - return hasit -end - -function thxbob.lib.machine.add_category(machine, category) - if machine and data.raw["recipe-category"][category] then - if not machine.crafting_categories then - machine.crafting_categories = { category } - elseif not thxbob.lib.machine.has_category(machine, category) then - table.insert(machine.crafting_categories, category) - end - else - if not data.raw["recipe-category"][category] then - log("Crafting category " .. category .. " does not exist.") - end - end -end - -function thxbob.lib.machine.if_add_category(machine, category, category_to_add) - if machine and data.raw["recipe-category"][category] and data.raw["recipe-category"][category_to_add] then - if thxbob.lib.machine.has_category(machine, category) then - thxbob.lib.machine.add_category(machine, category_to_add) - end - else - if not data.raw["recipe-category"][category] then - log("Crafting category " .. category .. " does not exist.") - end - if not data.raw["recipe-category"][category_to_add] then - log("Crafting category " .. category_to_add .. " does not exist.") - end - end -end - -function thxbob.lib.machine.type_if_add_category(machine_type, category, category_to_add) - if data.raw["recipe-category"][category] and data.raw["recipe-category"][category_to_add] then - for i, machine in pairs(data.raw[machine_type]) do - thxbob.lib.machine.if_add_category(machine, category, category_to_add) - end - else - if not data.raw["recipe-category"][category] then - log("Crafting category " .. category .. " does not exist.") - end - if not data.raw["recipe-category"][category_to_add] then - log("Crafting category " .. category_to_add .. " does not exist.") - end - end -end - -function thxbob.lib.machine.has_resource_category(machine, category_in) - local hasit = false - if machine and machine.resource_categories then - for i, category in pairs(machine.resource_categories) do - if category == category_in then - hasit = true - end - end - end - return hasit -end - -function thxbob.lib.machine.add_resource_category(machine, category) - if machine and data.raw["resource-category"][category] then - if not machine.resource_categories then - machine.resource_categories = { category } - elseif not thxbob.lib.machine.has_resource_category(machine, category) then - table.insert(machine.resource_categories, category) - end - else - if not data.raw["resource-category"][category] then - log("Resource category " .. category .. " does not exist.") - end - end -end - -function thxbob.lib.machine.if_add_resource_category(machine, category, category_to_add) - if machine and data.raw["resource-category"][category] and data.raw["resource-category"][category_to_add] then - if thxbob.lib.machine.has_resource_category(machine, category) then - thxbob.lib.machine.add_resource_category(machine, category_to_add) - end - else - if not data.raw["resource-category"][category] then - log("Resource category " .. category .. " does not exist.") - end - if not data.raw["resource-category"][category_to_add] then - log("Resource category " .. category_to_add .. " does not exist.") - end - end -end - -function thxbob.lib.machine.type_if_add_resource_category(machine_type, category, category_to_add) - if data.raw["resource-category"][category] and data.raw["resource-category"][category_to_add] then - for i, machine in pairs(data.raw[machine_type]) do - thxbob.lib.machine.if_add_resource_category(machine, category, category_to_add) - end - else - if not data.raw["resource-category"][category] then - log(debug.traceback()) - log("Resource category " .. category .. " does not exist.") - end - if not data.raw["resource-category"][category_to_add] then - log(debug.traceback()) - log("Resource category " .. category_to_add .. " does not exist.") - end - end -end - -function thxbob.lib.machine.copy_categories_from(machine_type, from_name, to_name) - local from_machine = data.raw[machine_type][from_name] - local to_machine = data.raw[machine_type][to_name] - - if from_machine and to_machine then - for _, category in pairs(from_machine.crafting_categories) do - thxbob.lib.machine.add_category(to_machine, category) - end - end -end +local BioInd = require('common')('Bio_Industries_2') + +if not thxbob.lib.machine then + thxbob.lib.machine = {} +end + +function thxbob.lib.machine.has_category(machine, category_in) + local hasit = false + if machine and machine.crafting_categories then + for i, category in pairs(machine.crafting_categories) do + if category == category_in then + hasit = true + end + end + end + return hasit +end + +function thxbob.lib.machine.add_category(machine, category) + if machine and data.raw["recipe-category"][category] then + if not machine.crafting_categories then + machine.crafting_categories = { category } + elseif not thxbob.lib.machine.has_category(machine, category) then + table.insert(machine.crafting_categories, category) + end + else + if not data.raw["recipe-category"][category] then + log("Crafting category " .. category .. " does not exist.") + end + end +end + +function thxbob.lib.machine.if_add_category(machine, category, category_to_add) + if machine and data.raw["recipe-category"][category] and data.raw["recipe-category"][category_to_add] then + if thxbob.lib.machine.has_category(machine, category) then + thxbob.lib.machine.add_category(machine, category_to_add) + end + else + if not data.raw["recipe-category"][category] then + log("Crafting category " .. category .. " does not exist.") + end + if not data.raw["recipe-category"][category_to_add] then + log("Crafting category " .. category_to_add .. " does not exist.") + end + end +end + +function thxbob.lib.machine.type_if_add_category(machine_type, category, category_to_add) + if data.raw["recipe-category"][category] and data.raw["recipe-category"][category_to_add] then + for i, machine in pairs(data.raw[machine_type]) do + thxbob.lib.machine.if_add_category(machine, category, category_to_add) + end + else + if not data.raw["recipe-category"][category] then + log("Crafting category " .. category .. " does not exist.") + end + if not data.raw["recipe-category"][category_to_add] then + log("Crafting category " .. category_to_add .. " does not exist.") + end + end +end + +function thxbob.lib.machine.has_resource_category(machine, category_in) + local hasit = false + if machine and machine.resource_categories then + for i, category in pairs(machine.resource_categories) do + if category == category_in then + hasit = true + end + end + end + return hasit +end + +function thxbob.lib.machine.add_resource_category(machine, category) + if machine and data.raw["resource-category"][category] then + if not machine.resource_categories then + machine.resource_categories = { category } + elseif not thxbob.lib.machine.has_resource_category(machine, category) then + table.insert(machine.resource_categories, category) + end + else + if not data.raw["resource-category"][category] then + log("Resource category " .. category .. " does not exist.") + end + end +end + +function thxbob.lib.machine.if_add_resource_category(machine, category, category_to_add) + if machine and data.raw["resource-category"][category] and data.raw["resource-category"][category_to_add] then + if thxbob.lib.machine.has_resource_category(machine, category) then + thxbob.lib.machine.add_resource_category(machine, category_to_add) + end + else + if not data.raw["resource-category"][category] then + log("Resource category " .. category .. " does not exist.") + end + if not data.raw["resource-category"][category_to_add] then + log("Resource category " .. category_to_add .. " does not exist.") + end + end +end + +function thxbob.lib.machine.type_if_add_resource_category(machine_type, category, category_to_add) + if data.raw["resource-category"][category] and data.raw["resource-category"][category_to_add] then + for i, machine in pairs(data.raw[machine_type]) do + thxbob.lib.machine.if_add_resource_category(machine, category, category_to_add) + end + else + if not data.raw["resource-category"][category] then + log(debug.traceback()) + log("Resource category " .. category .. " does not exist.") + end + if not data.raw["resource-category"][category_to_add] then + log(debug.traceback()) + log("Resource category " .. category_to_add .. " does not exist.") + end + end +end + +function thxbob.lib.machine.copy_categories_from(machine_type, from_name, to_name) + local from_machine = data.raw[machine_type][from_name] + local to_machine = data.raw[machine_type][to_name] + + if from_machine and to_machine then + for _, category in pairs(from_machine.crafting_categories) do + thxbob.lib.machine.add_category(to_machine, category) + end + end +end diff --git a/Bio_Industries_2/libs/error-functions.lua b/Bio_Industries_2/libs/error-functions.lua index fa8259a..889c6b5 100644 --- a/Bio_Industries_2/libs/error-functions.lua +++ b/Bio_Industries_2/libs/error-functions.lua @@ -1,154 +1,154 @@ -local BioInd = require('common')('Bio_Industries_2') - -if not thxbob.lib.error then - thxbob.lib.error = {} -end - -function thxbob.lib.error.technology(technology, name, desc) - if name == nil then - name = "Technology" - end - if desc == nil then - desc = name - end - if technology == nil then - log(name .. " variable not passed") - elseif type(technology) == "table" then - log(name .. " variable is a table.") - elseif not (type(technology) == "string") then - log(name .. " variable not a string.") - elseif not data.raw.technology[technology] then - log(desc .. " " .. technology .. " does not exist.") - end -end - -function thxbob.lib.error.recipe(recipe, name, desc) - if name == nil then - name = "Recipe" - end - if desc == nil then - desc = name - end - if recipe == nil then - log(name .. " variable not passed") - elseif type(recipe) == "table" then - log(name .. " variable is a table.") - elseif not (type(recipe) == "string") then - log(name .. " variable not a string.") - elseif not data.raw.recipe[recipe] then - log(desc .. " " .. recipe .. " does not exist.") - end -end - -function thxbob.lib.error.resource(resource, name, desc) - if name == nil then - name = "Resource" - end - if desc == nil then - desc = name - end - if resource == nil then - log(name .. " variable not passed") - elseif type(resource) == "table" then - log(name .. " variable is a table.") - elseif not (type(resource) == "string") then - log(name .. " variable not a string.") - elseif not data.raw.resource[resource] then - log(desc .. " " .. resource .. " does not exist.") - end -end - -function thxbob.lib.error.item(item, name) - if name == nil then - name = "Item" - end - if item == nil then - log(name .. " variable not passed") - elseif type(item) == "table" then - log(name .. " variable is a table.") - elseif not (type(item) == "string") then - log(name .. " variable not a string.") - else - local item_type = thxbob.lib.item.get_type(item) - if not item_type then - log(name .. " " .. item .. " not a valid item of any type.") - end - end -end - -function thxbob.lib.error.item_of_type(item, item_type_in, name) - if name == nil then - name = "Item" - end - if item == nil then - log(name .. " variable not passed") - elseif type(item) == "table" then - log(name .. " variable is a table.") - elseif not (type(item) == "string") then - log(name .. " variable not a string.") - else - local item_type = thxbob.lib.item.get_type(item) - if not item_type then - log(name .. " " .. item .. " does not exist.") - elseif item_type ~= item_type_in then - log(name .. " " .. item .. " is of type: " .. item_type .. ", expected: " .. item_type_in) - end - end -end - -function thxbob.lib.error.ingredient(ingredient, name) - if name == nil then - name = "Ingredient" - end - if ingredient == nil then - log(name .. " variable not passed") - elseif not (type(ingredient == "table")) then - log(name .. " variable not a table") - elseif not (type(ingredient.name) == "string") then - log(name .. ".name variable not a string.") - elseif thxbob.lib.item.get_type(ingredient.name) == nil then - log(name .. ".name not a valid item of any type.") - end - if not (type(ingredient.amount) == "number") then - log(name .. ".amount variable not a number.") - end - if not (ingredient.type == "item" or ingredient.type == "fluid") then - log(name .. ".type not a valid ingredient item type") - end -end - -function thxbob.lib.error.result(result, name) - if name == nil then - name = "Result" - end - if result == nil then - log(name .. " variable not passed") - return - elseif not (type(result == "table")) then - log(name .. " variable not a table") - elseif not (type(result.name) == "string") then - log(name .. ".name variable not a string.") - elseif thxbob.lib.item.get_type(result.name) == nil then - log(name .. ".name not a valid item of any type.") - end - if result.amount then - if type(result.amount) == "number" then - log(name .. ".amount variable not a number.") - end - elseif result.amount_min and result.amount_max then - if not (type(result.amount_min) == "number") then - log(name .. ".amount_min variable not a number.") - end - if not (type(result.amount_max) == "number") then - log(name .. ".amount_max variable not a number.") - end - end - if result.probability then - if not (type(result.probability) == "number") then - log(name .. ".probability not a number") - end - end - if not (result.type == "item" or result.type == "fluid") then - log(name .. ".type not a valid result item type") - end -end +local BioInd = require('common')('Bio_Industries_2') + +if not thxbob.lib.error then + thxbob.lib.error = {} +end + +function thxbob.lib.error.technology(technology, name, desc) + if name == nil then + name = "Technology" + end + if desc == nil then + desc = name + end + if technology == nil then + log(name .. " variable not passed") + elseif type(technology) == "table" then + log(name .. " variable is a table.") + elseif not (type(technology) == "string") then + log(name .. " variable not a string.") + elseif not data.raw.technology[technology] then + log(desc .. " " .. technology .. " does not exist.") + end +end + +function thxbob.lib.error.recipe(recipe, name, desc) + if name == nil then + name = "Recipe" + end + if desc == nil then + desc = name + end + if recipe == nil then + log(name .. " variable not passed") + elseif type(recipe) == "table" then + log(name .. " variable is a table.") + elseif not (type(recipe) == "string") then + log(name .. " variable not a string.") + elseif not data.raw.recipe[recipe] then + log(desc .. " " .. recipe .. " does not exist.") + end +end + +function thxbob.lib.error.resource(resource, name, desc) + if name == nil then + name = "Resource" + end + if desc == nil then + desc = name + end + if resource == nil then + log(name .. " variable not passed") + elseif type(resource) == "table" then + log(name .. " variable is a table.") + elseif not (type(resource) == "string") then + log(name .. " variable not a string.") + elseif not data.raw.resource[resource] then + log(desc .. " " .. resource .. " does not exist.") + end +end + +function thxbob.lib.error.item(item, name) + if name == nil then + name = "Item" + end + if item == nil then + log(name .. " variable not passed") + elseif type(item) == "table" then + log(name .. " variable is a table.") + elseif not (type(item) == "string") then + log(name .. " variable not a string.") + else + local item_type = thxbob.lib.item.get_type(item) + if not item_type then + log(name .. " " .. item .. " not a valid item of any type.") + end + end +end + +function thxbob.lib.error.item_of_type(item, item_type_in, name) + if name == nil then + name = "Item" + end + if item == nil then + log(name .. " variable not passed") + elseif type(item) == "table" then + log(name .. " variable is a table.") + elseif not (type(item) == "string") then + log(name .. " variable not a string.") + else + local item_type = thxbob.lib.item.get_type(item) + if not item_type then + log(name .. " " .. item .. " does not exist.") + elseif item_type ~= item_type_in then + log(name .. " " .. item .. " is of type: " .. item_type .. ", expected: " .. item_type_in) + end + end +end + +function thxbob.lib.error.ingredient(ingredient, name) + if name == nil then + name = "Ingredient" + end + if ingredient == nil then + log(name .. " variable not passed") + elseif not (type(ingredient == "table")) then + log(name .. " variable not a table") + elseif not (type(ingredient.name) == "string") then + log(name .. ".name variable not a string.") + elseif thxbob.lib.item.get_type(ingredient.name) == nil then + log(name .. ".name not a valid item of any type.") + end + if not (type(ingredient.amount) == "number") then + log(name .. ".amount variable not a number.") + end + if not (ingredient.type == "item" or ingredient.type == "fluid") then + log(name .. ".type not a valid ingredient item type") + end +end + +function thxbob.lib.error.result(result, name) + if name == nil then + name = "Result" + end + if result == nil then + log(name .. " variable not passed") + return + elseif not (type(result == "table")) then + log(name .. " variable not a table") + elseif not (type(result.name) == "string") then + log(name .. ".name variable not a string.") + elseif thxbob.lib.item.get_type(result.name) == nil then + log(name .. ".name not a valid item of any type.") + end + if result.amount then + if type(result.amount) == "number" then + log(name .. ".amount variable not a number.") + end + elseif result.amount_min and result.amount_max then + if not (type(result.amount_min) == "number") then + log(name .. ".amount_min variable not a number.") + end + if not (type(result.amount_max) == "number") then + log(name .. ".amount_max variable not a number.") + end + end + if result.probability then + if not (type(result.probability) == "number") then + log(name .. ".probability not a number") + end + end + if not (result.type == "item" or result.type == "fluid") then + log(name .. ".type not a valid result item type") + end +end diff --git a/Bio_Industries_2/libs/functions.lua b/Bio_Industries_2/libs/functions.lua index c94998c..9c39f42 100644 --- a/Bio_Industries_2/libs/functions.lua +++ b/Bio_Industries_2/libs/functions.lua @@ -1,80 +1,80 @@ -local BioInd = require('common')('Bio_Industries_2') - --- Merges table2's contents into table1. -function thxbob.lib.table_merge(table1, table2) - for index, value in pairs(table2) do - if type(value) == "table" then - if type(table1[index]) == "table" then - thxbob.lib.table_merge(table1[index], table2[index]) - else - table1[index] = util.table.deepcopy(table2[index]) - end - else - table1[index] = value - end - end -end - -function thxbob.lib.minable_result_check(object) - if object then - if object.results == nil then - object.results = {} - end - - if object.result then - local item = thxbob.lib.item.ingredient({ type = "item", name = object.result, amount = 1 }) - if object.count then - item.amount = object.count - object.count = nil - end - thxbob.lib.item.add_new(object.results, item) - - object.result = nil - end - else - log(object .. " does not exist.") - end -end - -function thxbob.lib.belt_speed_ips(ips) - return ips * 1 / 480 -end - ---Inserts the new item into the table only if it doesn't already exist. (Index optional. Designed to insert strings only.) -function thxbob.lib.safe_insert(array, new_item, index) - local addit = true - for i, item in pairs(array) do - if item == new_item then - addit = false - end - end - if addit then - if index then - table.insert(array, index, new_item) - else - table.insert(array, new_item) - end - end -end - ---takes an item/fluid/entity(maybe even recipe) and returns a complete icons array. ---if it has no icons= tag, it builds one from icon and icon_size. ---Example use thxbob.lib.icons_from_item(data.raw.item.wood) -function thxbob.lib.icons_from_item(item) - if item and type(item) == "table" then - local icons = {} - if item.icons then - icons = item.icons - elseif item.icon then - icons = { { icon = item.icon, icon_size = item.icon_size or 64 } } - else - icons = nil - log(debug.traceback()) - log(item.name .. " has no valid icons.") - end - return icons - end - log(debug.traceback()) - log("object does not exist.") - return nil -end +local BioInd = require('common')('Bio_Industries_2') + +-- Merges table2's contents into table1. +function thxbob.lib.table_merge(table1, table2) + for index, value in pairs(table2) do + if type(value) == "table" then + if type(table1[index]) == "table" then + thxbob.lib.table_merge(table1[index], table2[index]) + else + table1[index] = util.table.deepcopy(table2[index]) + end + else + table1[index] = value + end + end +end + +function thxbob.lib.minable_result_check(object) + if object then + if object.results == nil then + object.results = {} + end + + if object.result then + local item = thxbob.lib.item.ingredient({ type = "item", name = object.result, amount = 1 }) + if object.count then + item.amount = object.count + object.count = nil + end + thxbob.lib.item.add_new(object.results, item) + + object.result = nil + end + else + log(object .. " does not exist.") + end +end + +function thxbob.lib.belt_speed_ips(ips) + return ips * 1 / 480 +end + +--Inserts the new item into the table only if it doesn't already exist. (Index optional. Designed to insert strings only.) +function thxbob.lib.safe_insert(array, new_item, index) + local addit = true + for i, item in pairs(array) do + if item == new_item then + addit = false + end + end + if addit then + if index then + table.insert(array, index, new_item) + else + table.insert(array, new_item) + end + end +end + +--takes an item/fluid/entity(maybe even recipe) and returns a complete icons array. +--if it has no icons= tag, it builds one from icon and icon_size. +--Example use thxbob.lib.icons_from_item(data.raw.item.wood) +function thxbob.lib.icons_from_item(item) + if item and type(item) == "table" then + local icons = {} + if item.icons then + icons = item.icons + elseif item.icon then + icons = { { icon = item.icon, icon_size = item.icon_size or 64 } } + else + icons = nil + log(debug.traceback()) + log(item.name .. " has no valid icons.") + end + return icons + end + log(debug.traceback()) + log("object does not exist.") + return nil +end diff --git a/Bio_Industries_2/libs/item-functions.lua b/Bio_Industries_2/libs/item-functions.lua index 27ac39a..d3983c4 100644 --- a/Bio_Industries_2/libs/item-functions.lua +++ b/Bio_Industries_2/libs/item-functions.lua @@ -1,363 +1,363 @@ -local BioInd = require('common')('Bio_Industries_2') - - -if not thxbob.lib.item then - thxbob.lib.item = {} -end - -function thxbob.lib.item.get_type(name) --returns actual item type - local item_type = nil - if type(name) == "string" then - for type_name, _ in pairs(defines.prototypes["item"]) do - if data.raw[type_name] and data.raw[type_name][name] then - item_type = type_name - end - end - if data.raw.fluid and data.raw.fluid[name] then - item_type = "fluid" - end - else - log("Item name is not a string") - end - return item_type -end - -function thxbob.lib.item.get_basic_type(name) --returns fluid for fluid, item for all other types. - local item_type = thxbob.lib.item.get_type(name) - if not (item_type == "fluid" or item_type == nil) then - item_type = "item" - end - return item_type -end - -function thxbob.lib.item.get_basic_type_simple(name) --assumes type is item, even if the item doesn't exist - local item_type = "item" - if data.raw.fluid[name] then - item_type = "fluid" - end - return item_type -end - -function thxbob.lib.item.ingredient_simple(inputs) --doesn't care if the item actually exists or not, returns if a valid ingredient structure can be determined. - local item = {} - - if type(inputs) == "table" then - if inputs.name and type(inputs.name) == "string" then - item.name = inputs.name - else - log(debug.traceback()) - log("Unable to determine an ingredient name") - return nil - end - - if inputs.amount and type(inputs.amount) == "number" then - item.amount = inputs.amount - else - log(debug.traceback()) - log("Unable to determine an ingredient amount") - return nil - end - - if inputs.type then - item.type = inputs.type - else - log(debug.traceback()) - log("Unable to determine an ingredient type") - return nil - end - - if item.type == "item" then - if type(item.amount) ~= "number" or item.amount < 1 then - item.amount = 1 - else - item.amount = math.floor(item.amount) - end - end - if item.type == "fluid" then - item.temperature = inputs.temperature - item.minimum_temperature = inputs.minimum_temperature - item.maximum_temperature = inputs.maximum_temperature - item.fluidbox_index = inputs.fluidbox_index - item.fluidbox_multiplier = inputs.fluidbox_multiplier - end - - item.ignored_by_stats = inputs.ignored_by_stats - - return item - else - log(debug.traceback()) - thxbob.lib.error.ingredient(item) - return nil - end -end - -function thxbob.lib.item.ingredient(inputs) --returns a valid ingredient only if the item exists. - local item = thxbob.lib.item.ingredient_simple(inputs) - if item then - return item - else - if inputs and inputs.name then - log(inputs.name) - end - log(debug.traceback()) - thxbob.lib.error.ingredient(inputs) - return nil - end -end - ---Same as ingredient, but has support for amount_min, amount_max and probability -function thxbob.lib.item.result_simple(inputs) - local item = {} - - if type(inputs) == "table" then - if inputs.name and type(inputs.name) == "string" then - item.name = inputs.name - else - log(debug.traceback()) - log("Unable to determine a result name") - return nil - end - - if inputs.amount and type(inputs.amount) == "number" then - item.amount = inputs.amount - elseif inputs.amount_min and inputs.amount_max then - item.amount_min = inputs.amount_min - item.amount_max = inputs.amount_max - else - log(debug.traceback()) - log("Unable to determine a result amount") - return nil - end - - if inputs.probability then - item.probability = inputs.probability - end - - if inputs.type then - item.type = inputs.type - else - item.type = thxbob.lib.item.get_basic_type_simple(item.name) - end - - if item.type == "item" then - if item.amount then - if type(item.amount) ~= "number" or item.amount < 1 then - item.amount = 1 - else - item.amount = math.floor(item.amount) - end - end - if item.amount_min then - if type(item.amount_min) ~= "number" or item.amount_min < 1 then - item.amount_min = 0 - else - item.amount_min = math.floor(item.amount_min) - end - end - if item.amount_max then - if type(item.amount_max) ~= "number" or item.amount_max < 1 then - item.amount_max = 1 - else - item.amount_max = math.ceil(item.amount_max) - end - end - end - if item.type == "fluid" then - item.fluidbox_index = inputs.fluidbox_index - item.temperature = inputs.temperature - else - item.extra_count_fraction = inputs.extra_count_fraction - item.percent_spoiled = inputs.percent_spoiled - end - - item.ignored_by_stats = inputs.ignored_by_stats - item.ignored_by_productivity = inputs.ignored_by_productivity - item.show_details_in_recipe_tooltip = inputs.show_details_in_recipe_tooltip - end - - if - type(item.name) == "string" - and (type(item.amount) == "number" or (type(item.amount_min) == "number" and type(item.amount_max) == "number")) - and (item.probability == nil or type(item.probability) == "number") - and (item.type == "item" or item.type == "fluid") - then - return item - else - log(debug.traceback()) - thxbob.lib.error.result(item) - return nil - end -end - -function thxbob.lib.item.result(inputs) --returns a valid result only if the item exists. - local item = thxbob.lib.item.result_simple(inputs) - if item then - return item - else - if inputs and inputs.name then - log(inputs.name) - end - log(debug.traceback()) - thxbob.lib.error.result(inputs) - return nil - end -end - -function thxbob.lib.item.combine(item1_in, item2_in) - local item = {} - local item1 = thxbob.lib.item.result(item1_in) - local item2 = thxbob.lib.item.result(item2_in) - - if item1 and item2 then - item.name = item1.name - item.type = item1.type - - if item1.amount and item2.amount then - item.amount = item1.amount + item2.amount - elseif item1.amount_min and item1.amount_max and item2.amount_min and item2.amount_max then - item.amount_min = item1.amount_min + item2.amount_min - item.amount_max = item1.amount_max + item2.amount_max - elseif item1.amount_min and item1.amount_max and item2.amount then - item.amount_min = item1.amount_min + item2.amount - item.amount_max = item1.amount_max + item2.amount - elseif item1.amount and item2.amount_min and item2.amount_max then - item.amount_min = item1.amount + item2.amount_min - item.amount_max = item1.amount + item2.amount_max - end - - if item1.probability and item2.probability then - item.probability = (item1.probability + item2.probability) / 2 - elseif item1.probability then - item.probability = (item1.probability + 1) / 2 - elseif item2.probability then - item.probability = (item2.probability + 1) / 2 - end - - if item1.ignored_by_productivity and item2.ignored_by_productivity then - item.ignored_by_productivity = item1.ignored_by_productivity + item2.ignored_by_productivity - elseif item1.ignored_by_productivity then - item.ignored_by_productivity = item1.ignored_by_productivity - elseif item2.ignored_by_productivity then - item.ignored_by_productivity = item2.ignored_by_productivity - end - - if item1.ignored_by_stats and item2.ignored_by_stats then - item.ignored_by_stats = item1.ignored_by_stats + item2.ignored_by_stats - elseif item1.ignored_by_stats then - item.ignored_by_stats = item1.ignored_by_stats - elseif item2.ignored_by_stats then - item.ignored_by_stats = item2.ignored_by_stats - end - - item.fluidbox_index = item1.fluidbox_index or item2.fluidbox_index - - return item - else - return nil - end -end - -function thxbob.lib.item.add(list, item_in) --increments amount if exists - local item = thxbob.lib.item.result(item_in) - if type(list) == "table" and item then - local addit = true - for i, object in pairs(list) do - if object.name == item.name then - addit = false - list[i] = thxbob.lib.item.combine(object, item) - end - end - if addit then - table.insert(list, item) - end - end -end - -function thxbob.lib.item.add_new(list, item_in) --ignores if exists - local item = thxbob.lib.item.result(item_in) - if type(list) == "table" and item then - local addit = true - for i, object in pairs(list) do - local basic_object = thxbob.lib.item.result(object) - if basic_object and item.name == basic_object.name then - addit = false - end - end - if addit then - table.insert(list, item) - end - end -end - -function thxbob.lib.item.remove(list, item) - if type(list) == "table" and type(item) == "string" then - for i, object in ipairs(list) do - if object.name == item then - table.remove(list, i) - end - end - else - log(debug.traceback()) - thxbob.lib.error.item(item) - end -end - -function thxbob.lib.item.set(list, item_in) - local item = thxbob.lib.item.result(item_in) - if type(list) == "table" and item then - local addit = true - for i, object in pairs(list) do - if object.name == item.name then - list[i] = item - addit = false - end - end - if addit then - table.insert(list, item) - end - end -end - -function thxbob.lib.item.hide(item_name) - if type(item_name) == "string" then - local item = data.raw.item[item_name] or data.raw.fluid[item_name] - if item then - item.hidden = true - end - else - log(debug.traceback()) - thxbob.lib.error.item(item_name) - end -end - -function thxbob.lib.item.hide_entity(type_name, entity_name) - if type(type_name) == "string" and type(entity_name) == "string" then - local entities = data.raw[type_name] - if entities then - local entity = entities[entity_name] - if entity then - entity.hidden = true - end - end - else - log(debug.traceback()) - thxbob.lib.error.item(entity_name) - end -end - -function thxbob.lib.item.set_subgroup(item_name, subgroup) - if type(item_name) == "string" and type(subgroup) == "string" then - local item = data.raw.item[item_name] - if item then - item.subgroup = subgroup - else - item = data.raw.fluid[item_name] - if item then - item.subgroup = subgroup - end - end - else - log(debug.traceback()) - thxbob.lib.error.item(item_name) - end -end +local BioInd = require('common')('Bio_Industries_2') + + +if not thxbob.lib.item then + thxbob.lib.item = {} +end + +function thxbob.lib.item.get_type(name) --returns actual item type + local item_type = nil + if type(name) == "string" then + for type_name, _ in pairs(defines.prototypes["item"]) do + if data.raw[type_name] and data.raw[type_name][name] then + item_type = type_name + end + end + if data.raw.fluid and data.raw.fluid[name] then + item_type = "fluid" + end + else + log("Item name is not a string") + end + return item_type +end + +function thxbob.lib.item.get_basic_type(name) --returns fluid for fluid, item for all other types. + local item_type = thxbob.lib.item.get_type(name) + if not (item_type == "fluid" or item_type == nil) then + item_type = "item" + end + return item_type +end + +function thxbob.lib.item.get_basic_type_simple(name) --assumes type is item, even if the item doesn't exist + local item_type = "item" + if data.raw.fluid[name] then + item_type = "fluid" + end + return item_type +end + +function thxbob.lib.item.ingredient_simple(inputs) --doesn't care if the item actually exists or not, returns if a valid ingredient structure can be determined. + local item = {} + + if type(inputs) == "table" then + if inputs.name and type(inputs.name) == "string" then + item.name = inputs.name + else + log(debug.traceback()) + log("Unable to determine an ingredient name") + return nil + end + + if inputs.amount and type(inputs.amount) == "number" then + item.amount = inputs.amount + else + log(debug.traceback()) + log("Unable to determine an ingredient amount") + return nil + end + + if inputs.type then + item.type = inputs.type + else + log(debug.traceback()) + log("Unable to determine an ingredient type") + return nil + end + + if item.type == "item" then + if type(item.amount) ~= "number" or item.amount < 1 then + item.amount = 1 + else + item.amount = math.floor(item.amount) + end + end + if item.type == "fluid" then + item.temperature = inputs.temperature + item.minimum_temperature = inputs.minimum_temperature + item.maximum_temperature = inputs.maximum_temperature + item.fluidbox_index = inputs.fluidbox_index + item.fluidbox_multiplier = inputs.fluidbox_multiplier + end + + item.ignored_by_stats = inputs.ignored_by_stats + + return item + else + log(debug.traceback()) + thxbob.lib.error.ingredient(item) + return nil + end +end + +function thxbob.lib.item.ingredient(inputs) --returns a valid ingredient only if the item exists. + local item = thxbob.lib.item.ingredient_simple(inputs) + if item then + return item + else + if inputs and inputs.name then + log(inputs.name) + end + log(debug.traceback()) + thxbob.lib.error.ingredient(inputs) + return nil + end +end + +--Same as ingredient, but has support for amount_min, amount_max and probability +function thxbob.lib.item.result_simple(inputs) + local item = {} + + if type(inputs) == "table" then + if inputs.name and type(inputs.name) == "string" then + item.name = inputs.name + else + log(debug.traceback()) + log("Unable to determine a result name") + return nil + end + + if inputs.amount and type(inputs.amount) == "number" then + item.amount = inputs.amount + elseif inputs.amount_min and inputs.amount_max then + item.amount_min = inputs.amount_min + item.amount_max = inputs.amount_max + else + log(debug.traceback()) + log("Unable to determine a result amount") + return nil + end + + if inputs.probability then + item.probability = inputs.probability + end + + if inputs.type then + item.type = inputs.type + else + item.type = thxbob.lib.item.get_basic_type_simple(item.name) + end + + if item.type == "item" then + if item.amount then + if type(item.amount) ~= "number" or item.amount < 1 then + item.amount = 1 + else + item.amount = math.floor(item.amount) + end + end + if item.amount_min then + if type(item.amount_min) ~= "number" or item.amount_min < 1 then + item.amount_min = 0 + else + item.amount_min = math.floor(item.amount_min) + end + end + if item.amount_max then + if type(item.amount_max) ~= "number" or item.amount_max < 1 then + item.amount_max = 1 + else + item.amount_max = math.ceil(item.amount_max) + end + end + end + if item.type == "fluid" then + item.fluidbox_index = inputs.fluidbox_index + item.temperature = inputs.temperature + else + item.extra_count_fraction = inputs.extra_count_fraction + item.percent_spoiled = inputs.percent_spoiled + end + + item.ignored_by_stats = inputs.ignored_by_stats + item.ignored_by_productivity = inputs.ignored_by_productivity + item.show_details_in_recipe_tooltip = inputs.show_details_in_recipe_tooltip + end + + if + type(item.name) == "string" + and (type(item.amount) == "number" or (type(item.amount_min) == "number" and type(item.amount_max) == "number")) + and (item.probability == nil or type(item.probability) == "number") + and (item.type == "item" or item.type == "fluid") + then + return item + else + log(debug.traceback()) + thxbob.lib.error.result(item) + return nil + end +end + +function thxbob.lib.item.result(inputs) --returns a valid result only if the item exists. + local item = thxbob.lib.item.result_simple(inputs) + if item then + return item + else + if inputs and inputs.name then + log(inputs.name) + end + log(debug.traceback()) + thxbob.lib.error.result(inputs) + return nil + end +end + +function thxbob.lib.item.combine(item1_in, item2_in) + local item = {} + local item1 = thxbob.lib.item.result(item1_in) + local item2 = thxbob.lib.item.result(item2_in) + + if item1 and item2 then + item.name = item1.name + item.type = item1.type + + if item1.amount and item2.amount then + item.amount = item1.amount + item2.amount + elseif item1.amount_min and item1.amount_max and item2.amount_min and item2.amount_max then + item.amount_min = item1.amount_min + item2.amount_min + item.amount_max = item1.amount_max + item2.amount_max + elseif item1.amount_min and item1.amount_max and item2.amount then + item.amount_min = item1.amount_min + item2.amount + item.amount_max = item1.amount_max + item2.amount + elseif item1.amount and item2.amount_min and item2.amount_max then + item.amount_min = item1.amount + item2.amount_min + item.amount_max = item1.amount + item2.amount_max + end + + if item1.probability and item2.probability then + item.probability = (item1.probability + item2.probability) / 2 + elseif item1.probability then + item.probability = (item1.probability + 1) / 2 + elseif item2.probability then + item.probability = (item2.probability + 1) / 2 + end + + if item1.ignored_by_productivity and item2.ignored_by_productivity then + item.ignored_by_productivity = item1.ignored_by_productivity + item2.ignored_by_productivity + elseif item1.ignored_by_productivity then + item.ignored_by_productivity = item1.ignored_by_productivity + elseif item2.ignored_by_productivity then + item.ignored_by_productivity = item2.ignored_by_productivity + end + + if item1.ignored_by_stats and item2.ignored_by_stats then + item.ignored_by_stats = item1.ignored_by_stats + item2.ignored_by_stats + elseif item1.ignored_by_stats then + item.ignored_by_stats = item1.ignored_by_stats + elseif item2.ignored_by_stats then + item.ignored_by_stats = item2.ignored_by_stats + end + + item.fluidbox_index = item1.fluidbox_index or item2.fluidbox_index + + return item + else + return nil + end +end + +function thxbob.lib.item.add(list, item_in) --increments amount if exists + local item = thxbob.lib.item.result(item_in) + if type(list) == "table" and item then + local addit = true + for i, object in pairs(list) do + if object.name == item.name then + addit = false + list[i] = thxbob.lib.item.combine(object, item) + end + end + if addit then + table.insert(list, item) + end + end +end + +function thxbob.lib.item.add_new(list, item_in) --ignores if exists + local item = thxbob.lib.item.result(item_in) + if type(list) == "table" and item then + local addit = true + for i, object in pairs(list) do + local basic_object = thxbob.lib.item.result(object) + if basic_object and item.name == basic_object.name then + addit = false + end + end + if addit then + table.insert(list, item) + end + end +end + +function thxbob.lib.item.remove(list, item) + if type(list) == "table" and type(item) == "string" then + for i, object in ipairs(list) do + if object.name == item then + table.remove(list, i) + end + end + else + log(debug.traceback()) + thxbob.lib.error.item(item) + end +end + +function thxbob.lib.item.set(list, item_in) + local item = thxbob.lib.item.result(item_in) + if type(list) == "table" and item then + local addit = true + for i, object in pairs(list) do + if object.name == item.name then + list[i] = item + addit = false + end + end + if addit then + table.insert(list, item) + end + end +end + +function thxbob.lib.item.hide(item_name) + if type(item_name) == "string" then + local item = data.raw.item[item_name] or data.raw.fluid[item_name] + if item then + item.hidden = true + end + else + log(debug.traceback()) + thxbob.lib.error.item(item_name) + end +end + +function thxbob.lib.item.hide_entity(type_name, entity_name) + if type(type_name) == "string" and type(entity_name) == "string" then + local entities = data.raw[type_name] + if entities then + local entity = entities[entity_name] + if entity then + entity.hidden = true + end + end + else + log(debug.traceback()) + thxbob.lib.error.item(entity_name) + end +end + +function thxbob.lib.item.set_subgroup(item_name, subgroup) + if type(item_name) == "string" and type(subgroup) == "string" then + local item = data.raw.item[item_name] + if item then + item.subgroup = subgroup + else + item = data.raw.fluid[item_name] + if item then + item.subgroup = subgroup + end + end + else + log(debug.traceback()) + thxbob.lib.error.item(item_name) + end +end diff --git a/Bio_Industries_2/libs/recipe-functions.lua b/Bio_Industries_2/libs/recipe-functions.lua index 4a0593c..4825276 100644 --- a/Bio_Industries_2/libs/recipe-functions.lua +++ b/Bio_Industries_2/libs/recipe-functions.lua @@ -1,560 +1,560 @@ -local BioInd = require('common')('Bio_Industries_2') - -if not thxbob.lib.recipe then - thxbob.lib.recipe = {} -end - -local function quantity_convertion(amount, old, new) - if thxbob.lib.item.get_type(old) == "fluid" and thxbob.lib.item.get_type(new) == "item" then - amount = math.ceil(amount / 10) - end - if thxbob.lib.item.get_type(old) == "item" and thxbob.lib.item.get_type(new) == "fluid" then - amount = amount * 10 - end - return amount -end - -local function get_old_quantity(ingredients, old) - local amount = 0 - for i, ingredient in pairs(ingredients) do - local item = thxbob.lib.item.ingredient_simple(ingredient) - if item then - if item.name == old then - amount = item.amount + amount - end - else - log("recipe contains an invalid ingredient") - end - end - return amount -end - -local function replace_ingredient(ingredients, old, new, new_type) - local amount = get_old_quantity(ingredients, old) - if amount > 0 then - amount = quantity_convertion(amount, old, new) - thxbob.lib.item.remove(ingredients, old) - thxbob.lib.item.add(ingredients, { type = new_type, name = new, amount = amount }) - return true - end - return false -end - -function thxbob.lib.recipe.replace_ingredient(recipe, old, new) - if type(recipe) == "string" and type(old) == "string" and type(new) == "string" and data.raw.recipe[recipe] then - local retval = false - local new_type = thxbob.lib.item.get_type(new) - - if new_type and data.raw.recipe[recipe].ingredients then - new_type = new_type == "fluid" and "fluid" or "item" - if replace_ingredient(data.raw.recipe[recipe].ingredients, old, new, new_type) then - retval = true - end - end - - return retval - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - thxbob.lib.error.item(old) - thxbob.lib.error.item(new) - return false - end -end - -function thxbob.lib.recipe.replace_ingredient_in_all(old, new) - if type(old) == "string" and type(new) == "string" and thxbob.lib.item.get_type(new) then - for i, recipe in pairs(data.raw.recipe) do - thxbob.lib.recipe.replace_ingredient(recipe.name, old, new) - end - else - log(debug.traceback()) - thxbob.lib.error.item(old) - thxbob.lib.error.item(new) - end -end - -function thxbob.lib.recipe.remove_ingredient(recipe, item) - if type(recipe) == "string" and type(item) == "string" and data.raw.recipe[recipe] then - if data.raw.recipe[recipe].ingredients then - thxbob.lib.item.remove(data.raw.recipe[recipe].ingredients, item) - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - thxbob.lib.error.item(item) - end -end - -function thxbob.lib.recipe.clear_ingredients(recipe) - if type(recipe) == "string" and data.raw.recipe[recipe] then - if data.raw.recipe[recipe].ingredients then - data.raw.recipe[recipe].ingredients = {} - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end -end - -function thxbob.lib.recipe.add_new_ingredient(recipe, item_in) - local item = thxbob.lib.item.ingredient(item_in) - if - type(recipe) == "string" - and data.raw.recipe[recipe] - and item - and type(item) == "table" - and thxbob.lib.item.get_type(item.name) - then - if data.raw.recipe[recipe].ingredients then - thxbob.lib.item.add_new(data.raw.recipe[recipe].ingredients, item) - end - else - if not (type(recipe) == "string" and data.raw.recipe[recipe]) then - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end - end -end - -function thxbob.lib.recipe.add_ingredient(recipe, item_in) - local item = thxbob.lib.item.ingredient(item_in) - if - type(recipe) == "string" - and data.raw.recipe[recipe] - and item - and type(item) == "table" - and thxbob.lib.item.get_type(item.name) - then - if data.raw.recipe[recipe].ingredients then - thxbob.lib.item.add(data.raw.recipe[recipe].ingredients, item) - end - else - if not (type(recipe) == "string" and data.raw.recipe[recipe]) then - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end - end -end - -function thxbob.lib.recipe.add_ingredients(recipe, ingredients) - if type(recipe) == "string" and data.raw.recipe[recipe] and type(ingredients) == "table" then - for i, ingredient in pairs(ingredients) do - thxbob.lib.recipe.add_ingredient(recipe, ingredient) - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end -end - -function thxbob.lib.recipe.set_ingredient(recipe, item_in) - local item = thxbob.lib.item.ingredient(item_in) - if - type(recipe) == "string" - and data.raw.recipe[recipe] - and item - and type(item) == "table" - and thxbob.lib.item.get_type(item.name) - then - if data.raw.recipe[recipe].ingredients then - thxbob.lib.item.set(data.raw.recipe[recipe].ingredients, item) - end - else - if not (type(recipe) == "string" and data.raw.recipe[recipe]) then - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end - end -end - -function thxbob.lib.recipe.set_ingredients(recipe, ingredients) - if type(recipe) == "string" and data.raw.recipe[recipe] and type(ingredients) == "table" then - thxbob.lib.recipe.clear_ingredients(recipe) - thxbob.lib.recipe.add_ingredients(recipe, ingredients) - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end -end - -function thxbob.lib.recipe.add_result(recipe, item_in) - local item = thxbob.lib.item.result(item_in) - if - type(recipe) == "string" - and data.raw.recipe[recipe] - and item - and type(item) == "table" - and thxbob.lib.item.get_type(item.name) - then - if data.raw.recipe[recipe].results then - thxbob.lib.item.add(data.raw.recipe[recipe].results, item) - end - else - if not (type(recipe) == "string" and data.raw.recipe[recipe]) then - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end - end -end - -function thxbob.lib.recipe.set_result(recipe, item_in) - local item = thxbob.lib.item.result(item_in) - if - type(recipe) == "string" - and data.raw.recipe[recipe] - and item - and type(item) == "table" - and thxbob.lib.item.get_type(item.name) - then - if data.raw.recipe[recipe].results then - thxbob.lib.item.set(data.raw.recipe[recipe].results, item) - end - else - if not (type(recipe) == "string" and data.raw.recipe[recipe]) then - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end - end -end - -function thxbob.lib.recipe.remove_result(recipe, item) - if type(recipe) == "string" and type(item) == "string" and data.raw.recipe[recipe] then - if data.raw.recipe[recipe].results then - thxbob.lib.item.remove(data.raw.recipe[recipe].results, item) - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - thxbob.lib.error.item(item) - end -end - -function thxbob.lib.recipe.enabled(recipe, bool) - if type(recipe) == "string" and type(bool) == "boolean" and data.raw.recipe[recipe] then - data.raw.recipe[recipe].enabled = bool or false - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - if not (type(bool) == "boolean") then - log("Variable Bool is missing or not of type Boolean") - end - end -end - -function thxbob.lib.recipe.hide(recipe) - if type(recipe) == "string" and data.raw.recipe[recipe] then - local prototype = data.raw.recipe[recipe] - prototype.hidden = true - prototype.enabled = false - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end -end - -function thxbob.lib.recipe.set_energy_required(recipe, time) - if type(recipe) == "string" and type(time) == "number" and data.raw.recipe[recipe] then - data.raw.recipe[recipe].energy_required = time - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - if not (type(time) == "number") then - log("Variable Time is missing or not of type Number") - end - end -end - -local function duplicate_ingredient_check(recipe_name, ingredients) - local new_ingredients = {} - local items = {} - local rebuild = false - for i, ingredient in ipairs(ingredients) do - local item = thxbob.lib.item.ingredient(ingredient) - if item then -- duplicate value - if items[item.name] then - rebuild = true - log("Duplicate item " .. item.name .. " found on recipe " .. recipe_name .. ".") - else - items[item.name] = true - thxbob.lib.item.add(new_ingredients, ingredient) - end - else --invalid value - rebuild = true - log("Invalid item found on recipe " .. recipe_name .. ".") - end - end - if rebuild == true then - return new_ingredients - end -end - -local function duplicate_ingredient_check_full(recipe) - if type(recipe) == "string" and data.raw.recipe[recipe] then - if data.raw.recipe[recipe].ingredients then - local ingredients = duplicate_ingredient_check(recipe, data.raw.recipe[recipe].ingredients) - if ingredients then - data.raw.recipe[recipe].ingredients = ingredients - end - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe) - end -end - -function thxbob.lib.recipe.ingredients_cleanup() - log("Running recipe ingredients cleanup...") - for recipe_name, recipe in pairs(data.raw.recipe) do - duplicate_ingredient_check_full(recipe_name) - end -end - -function thxbob.lib.recipe.set_subgroup(recipe_name, subgroup) - if type(recipe_name) == "string" and type(subgroup) == "string" then - local recipe = data.raw.recipe[recipe_name] - if recipe then - recipe.subgroup = subgroup - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe_name) - end -end - -function thxbob.lib.recipe.set_category(recipe_name, category) - if type(recipe_name) == "string" and type(category) == "string" then - local recipe = data.raw.recipe[recipe_name] - if recipe then - recipe.category = category - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe_name) - end -end - -function thxbob.lib.recipe.allow_productivity(recipe_name) - if type(recipe_name) == "string" then - local recipe = data.raw.recipe[recipe_name] - if recipe then - recipe.allow_productivity = true - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe_name) - end -end - -function thxbob.lib.recipe.disallow_productivity(recipe_name) - if type(recipe_name) == "string" then - local recipe = data.raw.recipe[recipe_name] - if recipe then - recipe.allow_productivity = false - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe_name) - end -end - -function thxbob.lib.recipe.add_additional_category(recipe_name, category_name) - local recipe = data.raw.recipe[recipe_name] - local category = data.raw["recipe-category"][category_name] - if recipe and category then - recipe.additional_categories = recipe.additional_categories or {} - thxbob.lib.safe_insert(recipe.additional_categories, category_name) - end -end - -if mods["quality"] then - function thxbob.lib.recipe.update_recycling_recipe_icon(recipe_name, icon_name, size) - --Does not handle multiple icons - local target_recipe = data.raw.recipe[recipe_name] - local image_size = size or 64 - if type(icon_name) == "string" then - if target_recipe then - target_recipe.icons[2].icon = icon_name - target_recipe.icons[2].icon_size = image_size - target_recipe.icons[2].scale = 0.4 / (image_size / 64) - else - log(debug.traceback()) - log("Recycling recipe " .. recipe_name .. " not found") - end - else - log(debug.traceback()) - log("Invalid icon input") - end - end - - function thxbob.lib.recipe.update_recycling_recipe_single(recipe_name, replace_icon) - --Requires that the target recycling recipe's prefix is the same as the source used for recipe_name - if type(recipe_name) == "string" then - local target_recipe_name = recipe_name .. "-recycling" - local source_recipe = data.raw.recipe[recipe_name] - local target_recipe = data.raw.recipe[target_recipe_name] - if source_recipe then - if target_recipe then - local new_time = source_recipe.energy_required or 0.5 - target_recipe.energy_required = new_time / 16 - target_recipe.results = {} - local source_output_amount = 1 - for i, source_results in pairs(source_recipe.results) do - if source_results.name == recipe_name then - source_output_amount = source_results.amount - end - end - for i, outputs in pairs(source_recipe.ingredients) do - if source_recipe.ingredients[i].type == "item" then - table.insert(target_recipe.results, { - type = "item", - name = source_recipe.ingredients[i].name, - amount = source_recipe.ingredients[i].amount / source_output_amount / 4, - extra_count_fraction = source_recipe.ingredients[i].amount / source_output_amount % 4 / 4, - }) - end - end - - --Tries to find replacement icon if item of same name exists. If not, icon replacement will have do be done manually if desired. - if replace_icon == true then - local item_type = thxbob.lib.item.get_type(recipe_name) - local recipe_icon = target_recipe.icons[2].icon - local recipe_icon_size = target_recipe.icons[2].icon_size or 64 - if item_type and data.raw[item_type][recipe_name] then - if data.raw[item_type][recipe_name].icon then - recipe_icon = data.raw[item_type][recipe_name].icon - recipe_icon_size = data.raw[item_type][recipe_name].icon_size or 64 - thxbob.lib.recipe.update_recycling_recipe_icon(target_recipe_name, recipe_icon, recipe_icon_size) - elseif data.raw[item_type][recipe_name].icons then - target_recipe.icons = { { icon = "__quality__/graphics/icons/recycling.png" } } - for i, icon_replacement in pairs(data.raw[item_type][recipe_name].icons) do - local image_size = icon_replacement.icon_size or 64 - local image_scale = icon_replacement.scale or 1 - table.insert(target_recipe.icons, { - icon = icon_replacement.icon, - icon_size = image_size, - scale = 0.4 * image_scale / (image_size / 64), - shift = util.mul_shift(icon_replacement.shift, 0.8), - tint = icon_replacement.tint, - }) - end - table.insert(target_recipe.icons, { icon = "__quality__/graphics/icons/recycling-top.png" }) - end - end - end - else - log(debug.traceback()) - log("Recycling recipe " .. target_recipe_name .. " not found") - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe_name) - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recipe_name) - end - end - - function thxbob.lib.recipe.update_recycling_recipe(recipe_name) - if type(recipe_name) == "string" then - thxbob.lib.recipe.update_recycling_recipe_single(recipe_name, true) - end - if type(recipe_name) == "table" then - for i, single_recipe in pairs(recipe_name) do - thxbob.lib.recipe.update_recycling_recipe_single(single_recipe, true) - end - end - end - - function thxbob.lib.recipe.update_recycling_recipe_from_recipe(recycling_recipe, desired_recipe, replace_icon) - if - type(recycling_recipe) == "string" - and data.raw.recipe[recycling_recipe] - and string.sub(data.raw.recipe[recycling_recipe].name, -10) == "-recycling" - then - if type(desired_recipe) == "string" and data.raw.recipe[desired_recipe] then - local item_name = string.sub(recycling_recipe, 1, -11) - local target_recipe = data.raw.recipe[recycling_recipe] - local source_recipe = data.raw.recipe[desired_recipe] - local new_time = source_recipe.energy_required or 0.5 - target_recipe.energy_required = new_time / 16 - target_recipe.results = {} - local source_output_amount = 1 - for i, source_results in pairs(source_recipe.results) do - if source_results.name == item_name then - source_output_amount = source_results.amount - end - end - for i, outputs in pairs(source_recipe.ingredients) do - if source_recipe.ingredients[i].type == "item" then - table.insert(target_recipe.results, { - type = "item", - name = source_recipe.ingredients[i].name, - amount = source_recipe.ingredients[i].amount / source_output_amount / 4, - extra_count_fraction = source_recipe.ingredients[i].amount / source_output_amount % 4 / 4, - }) - end - end - - if replace_icon == true then - local item_type = thxbob.lib.item.get_type(item_name) - local recipe_icon = target_recipe.icons[2].icon - local recipe_icon_size = target_recipe.icons[2].icon_size or 64 - if item_type and data.raw[item_type][item_name] then - if data.raw[item_type][item_name].icon then - recipe_icon = data.raw[item_type][item_name].icon - recipe_icon_size = data.raw[item_type][item_name].icon_size or 64 - thxbob.lib.recipe.update_recycling_recipe_icon(recycling_recipe, recipe_icon, recipe_icon_size) - elseif data.raw[item_type][item_name].icons then - target_recipe.icons = { { icon = "__quality__/graphics/icons/recycling.png" } } - for i, icon_replacement in pairs(data.raw[item_type][item_name].icons) do - local image_size = icon_replacement.icon_size or 64 - local image_scale = icon_replacement.scale or 1 - table.insert(target_recipe.icons, { - icon = icon_replacement.icon, - icon_size = image_size, - scale = 0.4 * image_scale / (image_size / 64), - shift = util.mul_shift(icon_replacement.shift, 0.8), - tint = icon_replacement.tint, - }) - end - table.insert(target_recipe.icons, { icon = "__quality__/graphics/icons/recycling-top.png" }) - end - end - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(desired_recipe) - end - else - log(debug.traceback()) - thxbob.lib.error.recipe(recycling_recipe) - end - end -else - function thxbob.lib.recipe.update_recycling_recipe_icon() - log(debug.traceback()) - log("Improper function call. Cannot update recycling without Quality mod.") - end - - function thxbob.lib.recipe.update_recycling_recipe_single() - log(debug.traceback()) - log("Improper function call. Cannot update recycling without Quality mod.") - end - - function thxbob.lib.recipe.update_recycling_recipe() - log(debug.traceback()) - log("Improper function call. Cannot update recycling without Quality mod.") - end - - function thxbob.lib.recipe.update_recycling_recipe_to_self_recipe() - log(debug.traceback()) - log("Improper function call. Cannot update recycling without Quality mod.") - end - - function thxbob.lib.recipe.update_recycling_recipe_from_recipe() - log(debug.traceback()) - log("Improper function call. Cannot update recycling without Quality mod.") - end -end +local BioInd = require('common')('Bio_Industries_2') + +if not thxbob.lib.recipe then + thxbob.lib.recipe = {} +end + +local function quantity_convertion(amount, old, new) + if thxbob.lib.item.get_type(old) == "fluid" and thxbob.lib.item.get_type(new) == "item" then + amount = math.ceil(amount / 10) + end + if thxbob.lib.item.get_type(old) == "item" and thxbob.lib.item.get_type(new) == "fluid" then + amount = amount * 10 + end + return amount +end + +local function get_old_quantity(ingredients, old) + local amount = 0 + for i, ingredient in pairs(ingredients) do + local item = thxbob.lib.item.ingredient_simple(ingredient) + if item then + if item.name == old then + amount = item.amount + amount + end + else + log("recipe contains an invalid ingredient") + end + end + return amount +end + +local function replace_ingredient(ingredients, old, new, new_type) + local amount = get_old_quantity(ingredients, old) + if amount > 0 then + amount = quantity_convertion(amount, old, new) + thxbob.lib.item.remove(ingredients, old) + thxbob.lib.item.add(ingredients, { type = new_type, name = new, amount = amount }) + return true + end + return false +end + +function thxbob.lib.recipe.replace_ingredient(recipe, old, new) + if type(recipe) == "string" and type(old) == "string" and type(new) == "string" and data.raw.recipe[recipe] then + local retval = false + local new_type = thxbob.lib.item.get_type(new) + + if new_type and data.raw.recipe[recipe].ingredients then + new_type = new_type == "fluid" and "fluid" or "item" + if replace_ingredient(data.raw.recipe[recipe].ingredients, old, new, new_type) then + retval = true + end + end + + return retval + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + thxbob.lib.error.item(old) + thxbob.lib.error.item(new) + return false + end +end + +function thxbob.lib.recipe.replace_ingredient_in_all(old, new) + if type(old) == "string" and type(new) == "string" and thxbob.lib.item.get_type(new) then + for i, recipe in pairs(data.raw.recipe) do + thxbob.lib.recipe.replace_ingredient(recipe.name, old, new) + end + else + log(debug.traceback()) + thxbob.lib.error.item(old) + thxbob.lib.error.item(new) + end +end + +function thxbob.lib.recipe.remove_ingredient(recipe, item) + if type(recipe) == "string" and type(item) == "string" and data.raw.recipe[recipe] then + if data.raw.recipe[recipe].ingredients then + thxbob.lib.item.remove(data.raw.recipe[recipe].ingredients, item) + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + thxbob.lib.error.item(item) + end +end + +function thxbob.lib.recipe.clear_ingredients(recipe) + if type(recipe) == "string" and data.raw.recipe[recipe] then + if data.raw.recipe[recipe].ingredients then + data.raw.recipe[recipe].ingredients = {} + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end +end + +function thxbob.lib.recipe.add_new_ingredient(recipe, item_in) + local item = thxbob.lib.item.ingredient(item_in) + if + type(recipe) == "string" + and data.raw.recipe[recipe] + and item + and type(item) == "table" + and thxbob.lib.item.get_type(item.name) + then + if data.raw.recipe[recipe].ingredients then + thxbob.lib.item.add_new(data.raw.recipe[recipe].ingredients, item) + end + else + if not (type(recipe) == "string" and data.raw.recipe[recipe]) then + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end + end +end + +function thxbob.lib.recipe.add_ingredient(recipe, item_in) + local item = thxbob.lib.item.ingredient(item_in) + if + type(recipe) == "string" + and data.raw.recipe[recipe] + and item + and type(item) == "table" + and thxbob.lib.item.get_type(item.name) + then + if data.raw.recipe[recipe].ingredients then + thxbob.lib.item.add(data.raw.recipe[recipe].ingredients, item) + end + else + if not (type(recipe) == "string" and data.raw.recipe[recipe]) then + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end + end +end + +function thxbob.lib.recipe.add_ingredients(recipe, ingredients) + if type(recipe) == "string" and data.raw.recipe[recipe] and type(ingredients) == "table" then + for i, ingredient in pairs(ingredients) do + thxbob.lib.recipe.add_ingredient(recipe, ingredient) + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end +end + +function thxbob.lib.recipe.set_ingredient(recipe, item_in) + local item = thxbob.lib.item.ingredient(item_in) + if + type(recipe) == "string" + and data.raw.recipe[recipe] + and item + and type(item) == "table" + and thxbob.lib.item.get_type(item.name) + then + if data.raw.recipe[recipe].ingredients then + thxbob.lib.item.set(data.raw.recipe[recipe].ingredients, item) + end + else + if not (type(recipe) == "string" and data.raw.recipe[recipe]) then + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end + end +end + +function thxbob.lib.recipe.set_ingredients(recipe, ingredients) + if type(recipe) == "string" and data.raw.recipe[recipe] and type(ingredients) == "table" then + thxbob.lib.recipe.clear_ingredients(recipe) + thxbob.lib.recipe.add_ingredients(recipe, ingredients) + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end +end + +function thxbob.lib.recipe.add_result(recipe, item_in) + local item = thxbob.lib.item.result(item_in) + if + type(recipe) == "string" + and data.raw.recipe[recipe] + and item + and type(item) == "table" + and thxbob.lib.item.get_type(item.name) + then + if data.raw.recipe[recipe].results then + thxbob.lib.item.add(data.raw.recipe[recipe].results, item) + end + else + if not (type(recipe) == "string" and data.raw.recipe[recipe]) then + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end + end +end + +function thxbob.lib.recipe.set_result(recipe, item_in) + local item = thxbob.lib.item.result(item_in) + if + type(recipe) == "string" + and data.raw.recipe[recipe] + and item + and type(item) == "table" + and thxbob.lib.item.get_type(item.name) + then + if data.raw.recipe[recipe].results then + thxbob.lib.item.set(data.raw.recipe[recipe].results, item) + end + else + if not (type(recipe) == "string" and data.raw.recipe[recipe]) then + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end + end +end + +function thxbob.lib.recipe.remove_result(recipe, item) + if type(recipe) == "string" and type(item) == "string" and data.raw.recipe[recipe] then + if data.raw.recipe[recipe].results then + thxbob.lib.item.remove(data.raw.recipe[recipe].results, item) + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + thxbob.lib.error.item(item) + end +end + +function thxbob.lib.recipe.enabled(recipe, bool) + if type(recipe) == "string" and type(bool) == "boolean" and data.raw.recipe[recipe] then + data.raw.recipe[recipe].enabled = bool or false + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + if not (type(bool) == "boolean") then + log("Variable Bool is missing or not of type Boolean") + end + end +end + +function thxbob.lib.recipe.hide(recipe) + if type(recipe) == "string" and data.raw.recipe[recipe] then + local prototype = data.raw.recipe[recipe] + prototype.hidden = true + prototype.enabled = false + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end +end + +function thxbob.lib.recipe.set_energy_required(recipe, time) + if type(recipe) == "string" and type(time) == "number" and data.raw.recipe[recipe] then + data.raw.recipe[recipe].energy_required = time + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + if not (type(time) == "number") then + log("Variable Time is missing or not of type Number") + end + end +end + +local function duplicate_ingredient_check(recipe_name, ingredients) + local new_ingredients = {} + local items = {} + local rebuild = false + for i, ingredient in ipairs(ingredients) do + local item = thxbob.lib.item.ingredient(ingredient) + if item then -- duplicate value + if items[item.name] then + rebuild = true + log("Duplicate item " .. item.name .. " found on recipe " .. recipe_name .. ".") + else + items[item.name] = true + thxbob.lib.item.add(new_ingredients, ingredient) + end + else --invalid value + rebuild = true + log("Invalid item found on recipe " .. recipe_name .. ".") + end + end + if rebuild == true then + return new_ingredients + end +end + +local function duplicate_ingredient_check_full(recipe) + if type(recipe) == "string" and data.raw.recipe[recipe] then + if data.raw.recipe[recipe].ingredients then + local ingredients = duplicate_ingredient_check(recipe, data.raw.recipe[recipe].ingredients) + if ingredients then + data.raw.recipe[recipe].ingredients = ingredients + end + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe) + end +end + +function thxbob.lib.recipe.ingredients_cleanup() + log("Running recipe ingredients cleanup...") + for recipe_name, recipe in pairs(data.raw.recipe) do + duplicate_ingredient_check_full(recipe_name) + end +end + +function thxbob.lib.recipe.set_subgroup(recipe_name, subgroup) + if type(recipe_name) == "string" and type(subgroup) == "string" then + local recipe = data.raw.recipe[recipe_name] + if recipe then + recipe.subgroup = subgroup + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe_name) + end +end + +function thxbob.lib.recipe.set_category(recipe_name, category) + if type(recipe_name) == "string" and type(category) == "string" then + local recipe = data.raw.recipe[recipe_name] + if recipe then + recipe.category = category + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe_name) + end +end + +function thxbob.lib.recipe.allow_productivity(recipe_name) + if type(recipe_name) == "string" then + local recipe = data.raw.recipe[recipe_name] + if recipe then + recipe.allow_productivity = true + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe_name) + end +end + +function thxbob.lib.recipe.disallow_productivity(recipe_name) + if type(recipe_name) == "string" then + local recipe = data.raw.recipe[recipe_name] + if recipe then + recipe.allow_productivity = false + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe_name) + end +end + +function thxbob.lib.recipe.add_additional_category(recipe_name, category_name) + local recipe = data.raw.recipe[recipe_name] + local category = data.raw["recipe-category"][category_name] + if recipe and category then + recipe.additional_categories = recipe.additional_categories or {} + thxbob.lib.safe_insert(recipe.additional_categories, category_name) + end +end + +if mods["quality"] then + function thxbob.lib.recipe.update_recycling_recipe_icon(recipe_name, icon_name, size) + --Does not handle multiple icons + local target_recipe = data.raw.recipe[recipe_name] + local image_size = size or 64 + if type(icon_name) == "string" then + if target_recipe then + target_recipe.icons[2].icon = icon_name + target_recipe.icons[2].icon_size = image_size + target_recipe.icons[2].scale = 0.4 / (image_size / 64) + else + log(debug.traceback()) + log("Recycling recipe " .. recipe_name .. " not found") + end + else + log(debug.traceback()) + log("Invalid icon input") + end + end + + function thxbob.lib.recipe.update_recycling_recipe_single(recipe_name, replace_icon) + --Requires that the target recycling recipe's prefix is the same as the source used for recipe_name + if type(recipe_name) == "string" then + local target_recipe_name = recipe_name .. "-recycling" + local source_recipe = data.raw.recipe[recipe_name] + local target_recipe = data.raw.recipe[target_recipe_name] + if source_recipe then + if target_recipe then + local new_time = source_recipe.energy_required or 0.5 + target_recipe.energy_required = new_time / 16 + target_recipe.results = {} + local source_output_amount = 1 + for i, source_results in pairs(source_recipe.results) do + if source_results.name == recipe_name then + source_output_amount = source_results.amount + end + end + for i, outputs in pairs(source_recipe.ingredients) do + if source_recipe.ingredients[i].type == "item" then + table.insert(target_recipe.results, { + type = "item", + name = source_recipe.ingredients[i].name, + amount = source_recipe.ingredients[i].amount / source_output_amount / 4, + extra_count_fraction = source_recipe.ingredients[i].amount / source_output_amount % 4 / 4, + }) + end + end + + --Tries to find replacement icon if item of same name exists. If not, icon replacement will have do be done manually if desired. + if replace_icon == true then + local item_type = thxbob.lib.item.get_type(recipe_name) + local recipe_icon = target_recipe.icons[2].icon + local recipe_icon_size = target_recipe.icons[2].icon_size or 64 + if item_type and data.raw[item_type][recipe_name] then + if data.raw[item_type][recipe_name].icon then + recipe_icon = data.raw[item_type][recipe_name].icon + recipe_icon_size = data.raw[item_type][recipe_name].icon_size or 64 + thxbob.lib.recipe.update_recycling_recipe_icon(target_recipe_name, recipe_icon, recipe_icon_size) + elseif data.raw[item_type][recipe_name].icons then + target_recipe.icons = { { icon = "__quality__/graphics/icons/recycling.png" } } + for i, icon_replacement in pairs(data.raw[item_type][recipe_name].icons) do + local image_size = icon_replacement.icon_size or 64 + local image_scale = icon_replacement.scale or 1 + table.insert(target_recipe.icons, { + icon = icon_replacement.icon, + icon_size = image_size, + scale = 0.4 * image_scale / (image_size / 64), + shift = util.mul_shift(icon_replacement.shift, 0.8), + tint = icon_replacement.tint, + }) + end + table.insert(target_recipe.icons, { icon = "__quality__/graphics/icons/recycling-top.png" }) + end + end + end + else + log(debug.traceback()) + log("Recycling recipe " .. target_recipe_name .. " not found") + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe_name) + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recipe_name) + end + end + + function thxbob.lib.recipe.update_recycling_recipe(recipe_name) + if type(recipe_name) == "string" then + thxbob.lib.recipe.update_recycling_recipe_single(recipe_name, true) + end + if type(recipe_name) == "table" then + for i, single_recipe in pairs(recipe_name) do + thxbob.lib.recipe.update_recycling_recipe_single(single_recipe, true) + end + end + end + + function thxbob.lib.recipe.update_recycling_recipe_from_recipe(recycling_recipe, desired_recipe, replace_icon) + if + type(recycling_recipe) == "string" + and data.raw.recipe[recycling_recipe] + and string.sub(data.raw.recipe[recycling_recipe].name, -10) == "-recycling" + then + if type(desired_recipe) == "string" and data.raw.recipe[desired_recipe] then + local item_name = string.sub(recycling_recipe, 1, -11) + local target_recipe = data.raw.recipe[recycling_recipe] + local source_recipe = data.raw.recipe[desired_recipe] + local new_time = source_recipe.energy_required or 0.5 + target_recipe.energy_required = new_time / 16 + target_recipe.results = {} + local source_output_amount = 1 + for i, source_results in pairs(source_recipe.results) do + if source_results.name == item_name then + source_output_amount = source_results.amount + end + end + for i, outputs in pairs(source_recipe.ingredients) do + if source_recipe.ingredients[i].type == "item" then + table.insert(target_recipe.results, { + type = "item", + name = source_recipe.ingredients[i].name, + amount = source_recipe.ingredients[i].amount / source_output_amount / 4, + extra_count_fraction = source_recipe.ingredients[i].amount / source_output_amount % 4 / 4, + }) + end + end + + if replace_icon == true then + local item_type = thxbob.lib.item.get_type(item_name) + local recipe_icon = target_recipe.icons[2].icon + local recipe_icon_size = target_recipe.icons[2].icon_size or 64 + if item_type and data.raw[item_type][item_name] then + if data.raw[item_type][item_name].icon then + recipe_icon = data.raw[item_type][item_name].icon + recipe_icon_size = data.raw[item_type][item_name].icon_size or 64 + thxbob.lib.recipe.update_recycling_recipe_icon(recycling_recipe, recipe_icon, recipe_icon_size) + elseif data.raw[item_type][item_name].icons then + target_recipe.icons = { { icon = "__quality__/graphics/icons/recycling.png" } } + for i, icon_replacement in pairs(data.raw[item_type][item_name].icons) do + local image_size = icon_replacement.icon_size or 64 + local image_scale = icon_replacement.scale or 1 + table.insert(target_recipe.icons, { + icon = icon_replacement.icon, + icon_size = image_size, + scale = 0.4 * image_scale / (image_size / 64), + shift = util.mul_shift(icon_replacement.shift, 0.8), + tint = icon_replacement.tint, + }) + end + table.insert(target_recipe.icons, { icon = "__quality__/graphics/icons/recycling-top.png" }) + end + end + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(desired_recipe) + end + else + log(debug.traceback()) + thxbob.lib.error.recipe(recycling_recipe) + end + end +else + function thxbob.lib.recipe.update_recycling_recipe_icon() + log(debug.traceback()) + log("Improper function call. Cannot update recycling without Quality mod.") + end + + function thxbob.lib.recipe.update_recycling_recipe_single() + log(debug.traceback()) + log("Improper function call. Cannot update recycling without Quality mod.") + end + + function thxbob.lib.recipe.update_recycling_recipe() + log(debug.traceback()) + log("Improper function call. Cannot update recycling without Quality mod.") + end + + function thxbob.lib.recipe.update_recycling_recipe_to_self_recipe() + log(debug.traceback()) + log("Improper function call. Cannot update recycling without Quality mod.") + end + + function thxbob.lib.recipe.update_recycling_recipe_from_recipe() + log(debug.traceback()) + log("Improper function call. Cannot update recycling without Quality mod.") + end +end diff --git a/Bio_Industries_2/libs/technology-functions.lua b/Bio_Industries_2/libs/technology-functions.lua index 6503489..1534cf1 100644 --- a/Bio_Industries_2/libs/technology-functions.lua +++ b/Bio_Industries_2/libs/technology-functions.lua @@ -1,419 +1,419 @@ -local BioInd = require('common')('Bio_Industries_2') - -if not thxbob.lib.tech then - thxbob.lib.tech = {} -end - -local function add_new_science_pack(technology, pack, amount) - if technology.unit and technology.unit.ingredients then - local addit = true - for i, ingredient in pairs(technology.unit.ingredients) do - if ingredient[1] == pack then - addit = false - end - end - if addit then - table.insert(technology.unit.ingredients, { pack, amount }) - end - end -end - -local function add_science_pack(technology, pack, amount) - if technology.unit and technology.unit.ingredients then - local addit = true - for i, ingredient in pairs(technology.unit.ingredients) do - if ingredient[1] == pack then - addit = false - ingredient[2] = ingredient[2] + amount - end - end - if addit then - table.insert(technology.unit.ingredients, { pack, amount }) - end - end -end - -local function remove_science_pack(technology, pack) - if technology.unit and technology.unit.ingredients then - for i, ingredient in pairs(technology.unit.ingredients) do - if ingredient[1] == pack then - table.remove(technology.unit.ingredients, i) - end - end - end -end - -local function replace_science_pack(technology, old, new) - if technology.unit and technology.unit.ingredients then - local doit = false - local amount = 0 - for i, ingredient in pairs(technology.unit.ingredients) do - if ingredient[1] == old then - doit = true - amount = ingredient[2] + amount - end - end - if doit then - remove_science_pack(technology, old) - add_science_pack(technology, new, amount) - end - end -end - -function thxbob.lib.tech.replace_science_pack(technology, old, new) - if - type(technology) == "string" - and type(old) == "string" - and type(new) == "string" - and data.raw.technology[technology] - -- data.raw.tool[old] and - and data.raw.tool[new] - then - if data.raw.technology[technology].unit then - replace_science_pack(data.raw.technology[technology], old, new) - end - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - thxbob.lib.error.item_of_type(old, "tool", "Old science pack") - thxbob.lib.error.item_of_type(new, "tool", "New science pack") - end -end - -function thxbob.lib.tech.add_new_science_pack(technology, pack, amount) - if - type(technology) == "string" - and type(pack) == "string" - and type(amount) == "number" - and data.raw.technology[technology] - and data.raw.tool[pack] - then - if data.raw.technology[technology].unit then - add_new_science_pack(data.raw.technology[technology], pack, amount) - end - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - thxbob.lib.error.item_of_type(pack, "tool", "Science pack") - end -end - -function thxbob.lib.tech.add_science_pack(technology, pack, amount) - if - type(technology) == "string" - and type(pack) == "string" - and type(amount) == "number" - and data.raw.technology[technology] - and data.raw.tool[pack] - then - if data.raw.technology[technology].unit then - add_science_pack(data.raw.technology[technology], pack, amount) - end - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - thxbob.lib.error.item_of_type(pack, "tool", "Science pack") - end -end - -function thxbob.lib.tech.add_science_packs(technology, science_packs) - if type(technology) == "string" and type(science_packs) == "table" then - for i, science_pack in pairs(science_packs) do - if - type(science_pack) == "table" - and type(science_pack[1]) == "string" - and data.raw.tool[science_pack[1]] - and type(science_pack[2]) == "number" - then - thxbob.lib.tech.add_science_pack(technology, science_pack[1], science_pack[2]) - end - end - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - end -end - -function thxbob.lib.tech.remove_science_pack(technology, pack) - if type(technology) == "string" and type(pack) == "string" and data.raw.technology[technology] then - if data.raw.technology[technology].unit then - remove_science_pack(data.raw.technology[technology], pack) - end - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - end -end - -function thxbob.lib.tech.clear_science_packs(technology) - if type(technology) == "string" and data.raw.technology[technology] then - if data.raw.technology[technology].unit then - data.raw.technology[technology].unit.ingredients = {} - end - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - end -end - -function thxbob.lib.tech.set_science_packs(technology, science_packs) - if type(technology) == "string" and data.raw.technology[technology] and type(science_packs) == "table" then - thxbob.lib.tech.clear_science_packs(technology) - thxbob.lib.tech.add_science_packs(technology, science_packs) - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - end -end - -function thxbob.lib.tech.set_science_pack_count(technology, count) - if type(technology) == "string" and data.raw.technology[technology] then - local prototype = data.raw.technology[technology] - if prototype.unit then - prototype.unit.count = count - end - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - end -end - -local function has_recipe_unlock(technology, recipe) - if technology.effects then - for i, effect in pairs(technology.effects) do - if effect.type == "unlock-recipe" and effect.recipe == recipe then - return true - end - end - end - return false -end - -local function add_recipe_unlock(technology, recipe) - local addit = true - if not technology.effects then - technology.effects = {} - end - for i, effect in pairs(technology.effects) do - if effect.type == "unlock-recipe" and effect.recipe == recipe then - addit = false - end - end - if addit then - table.insert(technology.effects, { type = "unlock-recipe", recipe = recipe }) - end -end - -local function remove_recipe_unlock(technology, recipe) - if technology.effects then - for i, effect in pairs(technology.effects) do - if effect.type == "unlock-recipe" and effect.recipe == recipe then - table.remove(technology.effects, i) - end - end - end -end - -function thxbob.lib.tech.has_recipe_unlock(technology, recipe) - if - type(technology) == "string" - and type(recipe) == "string" - and data.raw.technology[technology] - and data.raw.recipe[recipe] - then - local hasit = false - hasit = has_recipe_unlock(data.raw.technology[technology], recipe) - - return hasit - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - thxbob.lib.error.recipe(recipe) - return false - end -end - -function thxbob.lib.tech.add_recipe_unlock(technology, recipe) - if - type(technology) == "string" - and type(recipe) == "string" - and data.raw.technology[technology] - and data.raw.recipe[recipe] - then - add_recipe_unlock(data.raw.technology[technology], recipe) - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - thxbob.lib.error.recipe(recipe) - end -end - -function thxbob.lib.tech.remove_recipe_unlock(technology, recipe) - if - type(technology) == "string" - and type(recipe) == "string" - and data.raw.technology[technology] - -- data.raw.recipe[recipe] --don't check to see if something we're removing exists. - then - remove_recipe_unlock(data.raw.technology[technology], recipe) - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - thxbob.lib.error.recipe(recipe) - end -end - -local function add_prerequisite(technology, prerequisite) - local addit = true - if technology.prerequisites then - for i, check in ipairs(technology.prerequisites) do - if check == prerequisite then - addit = false - end - end - else - technology.prerequisites = {} - end - if addit then - table.insert(technology.prerequisites, prerequisite) - end -end - -local function remove_prerequisite(technology, prerequisite) - if technology.prerequisites then - for i, check in ipairs(technology.prerequisites) do - if check == prerequisite then - table.remove(technology.prerequisites, i) - end - end - end -end - -local function replace_prerequisite(technology, old, new) - if technology.prerequisites then - for i, prerequisite in ipairs(technology.prerequisites) do - if prerequisite == old then - remove_prerequisite(technology, old) - add_prerequisite(technology, new) - end - end - end -end - -function thxbob.lib.tech.replace_prerequisite(technology, old, new) - if - type(technology) == "string" - and type(old) == "string" - and type(new) == "string" - and data.raw.technology[technology] - -- data.raw.technology[old] and - and data.raw.technology[new] - then - replace_prerequisite(data.raw.technology[technology], old, new) - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - thxbob.lib.error.technology(old, "Old prerequisite", "Old prerequisite technology") - thxbob.lib.error.technology(new, "New prerequisite", "New prerequisite technology") - end -end - -function thxbob.lib.tech.add_prerequisite(technology, prerequisite) - if - type(technology) == "string" - and type(prerequisite) == "string" - and data.raw.technology[technology] - and data.raw.technology[prerequisite] - then - add_prerequisite(data.raw.technology[technology], prerequisite) - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - thxbob.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology") - end -end - -function thxbob.lib.tech.remove_prerequisite(technology, prerequisite) - if - type(technology) == "string" - and type(prerequisite) == "string" - and data.raw.technology[technology] - -- data.raw.technology[prerequisite] - then - remove_prerequisite(data.raw.technology[technology], prerequisite) - else - log(debug.traceback()) - thxbob.lib.error.technology(technology) - thxbob.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology") - end -end - -function thxbob.lib.tech.hide(technology_name) - if type(technology_name) == "string" and data.raw.technology[technology_name] then - local technology = data.raw.technology[technology_name] - technology.hidden = true - technology.enabled = false - else - log(debug.traceback()) - thxbob.lib.error.technology(technology_name) - end -end - -function thxbob.lib.tech.ignore_tech_cost_multiplier(technology_name, ignore) - if type(technology_name) == "string" and type(ignore) == "boolean" then - local technology = data.raw.technology[technology_name] - if technology then - technology.ignore_tech_cost_multiplier = ignore - end - else - log(debug.traceback()) - thxbob.lib.error.technology(technology_name) - end -end - -function thxbob.lib.tech.technology_icon_constant(technology_icon, constant_icon, x, y) - local scale = 0.5 - local xshift = x or 64 - local yshift = y or 64 - if type(technology_icon) == "table" and technology_icon.icon and technology_icon.icon_size then - local icons = { - technology_icon, - { - icon = constant_icon, - icon_size = 128, - scale = scale, - shift = { xshift * scale, yshift * scale }, - }, - } - return icons - else - log(debug.traceback()) - log(technology_icon .. " not given in required table format") - end -end - -function thxbob.lib.tech.technology_line_icon_constant(technology_line, first, last, technology_icon, constant_icon) - local scale = 0.5 - if type(technology_icon) == "table" and technology_icon.icon and technology_icon.icon_size then - for i = first, last do - local tech_name = technology_line .. "-" .. i - if data.raw.technology[tech_name] then - data.raw.technology[tech_name].icons = { - technology_icon, - { - icon = constant_icon, - icon_size = 128, - scale = scale, - shift = { 64 * scale, 64 * scale }, - }, - } - else - log(debug.traceback()) - thxbob.lib.error.technology(tech_name) - end - end - else - log(debug.traceback()) - log(technology_icon .. " not given in required table format") - end -end +local BioInd = require('common')('Bio_Industries_2') + +if not thxbob.lib.tech then + thxbob.lib.tech = {} +end + +local function add_new_science_pack(technology, pack, amount) + if technology.unit and technology.unit.ingredients then + local addit = true + for i, ingredient in pairs(technology.unit.ingredients) do + if ingredient[1] == pack then + addit = false + end + end + if addit then + table.insert(technology.unit.ingredients, { pack, amount }) + end + end +end + +local function add_science_pack(technology, pack, amount) + if technology.unit and technology.unit.ingredients then + local addit = true + for i, ingredient in pairs(technology.unit.ingredients) do + if ingredient[1] == pack then + addit = false + ingredient[2] = ingredient[2] + amount + end + end + if addit then + table.insert(technology.unit.ingredients, { pack, amount }) + end + end +end + +local function remove_science_pack(technology, pack) + if technology.unit and technology.unit.ingredients then + for i, ingredient in pairs(technology.unit.ingredients) do + if ingredient[1] == pack then + table.remove(technology.unit.ingredients, i) + end + end + end +end + +local function replace_science_pack(technology, old, new) + if technology.unit and technology.unit.ingredients then + local doit = false + local amount = 0 + for i, ingredient in pairs(technology.unit.ingredients) do + if ingredient[1] == old then + doit = true + amount = ingredient[2] + amount + end + end + if doit then + remove_science_pack(technology, old) + add_science_pack(technology, new, amount) + end + end +end + +function thxbob.lib.tech.replace_science_pack(technology, old, new) + if + type(technology) == "string" + and type(old) == "string" + and type(new) == "string" + and data.raw.technology[technology] + -- data.raw.tool[old] and + and data.raw.tool[new] + then + if data.raw.technology[technology].unit then + replace_science_pack(data.raw.technology[technology], old, new) + end + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + thxbob.lib.error.item_of_type(old, "tool", "Old science pack") + thxbob.lib.error.item_of_type(new, "tool", "New science pack") + end +end + +function thxbob.lib.tech.add_new_science_pack(technology, pack, amount) + if + type(technology) == "string" + and type(pack) == "string" + and type(amount) == "number" + and data.raw.technology[technology] + and data.raw.tool[pack] + then + if data.raw.technology[technology].unit then + add_new_science_pack(data.raw.technology[technology], pack, amount) + end + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + thxbob.lib.error.item_of_type(pack, "tool", "Science pack") + end +end + +function thxbob.lib.tech.add_science_pack(technology, pack, amount) + if + type(technology) == "string" + and type(pack) == "string" + and type(amount) == "number" + and data.raw.technology[technology] + and data.raw.tool[pack] + then + if data.raw.technology[technology].unit then + add_science_pack(data.raw.technology[technology], pack, amount) + end + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + thxbob.lib.error.item_of_type(pack, "tool", "Science pack") + end +end + +function thxbob.lib.tech.add_science_packs(technology, science_packs) + if type(technology) == "string" and type(science_packs) == "table" then + for i, science_pack in pairs(science_packs) do + if + type(science_pack) == "table" + and type(science_pack[1]) == "string" + and data.raw.tool[science_pack[1]] + and type(science_pack[2]) == "number" + then + thxbob.lib.tech.add_science_pack(technology, science_pack[1], science_pack[2]) + end + end + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + end +end + +function thxbob.lib.tech.remove_science_pack(technology, pack) + if type(technology) == "string" and type(pack) == "string" and data.raw.technology[technology] then + if data.raw.technology[technology].unit then + remove_science_pack(data.raw.technology[technology], pack) + end + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + end +end + +function thxbob.lib.tech.clear_science_packs(technology) + if type(technology) == "string" and data.raw.technology[technology] then + if data.raw.technology[technology].unit then + data.raw.technology[technology].unit.ingredients = {} + end + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + end +end + +function thxbob.lib.tech.set_science_packs(technology, science_packs) + if type(technology) == "string" and data.raw.technology[technology] and type(science_packs) == "table" then + thxbob.lib.tech.clear_science_packs(technology) + thxbob.lib.tech.add_science_packs(technology, science_packs) + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + end +end + +function thxbob.lib.tech.set_science_pack_count(technology, count) + if type(technology) == "string" and data.raw.technology[technology] then + local prototype = data.raw.technology[technology] + if prototype.unit then + prototype.unit.count = count + end + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + end +end + +local function has_recipe_unlock(technology, recipe) + if technology.effects then + for i, effect in pairs(technology.effects) do + if effect.type == "unlock-recipe" and effect.recipe == recipe then + return true + end + end + end + return false +end + +local function add_recipe_unlock(technology, recipe) + local addit = true + if not technology.effects then + technology.effects = {} + end + for i, effect in pairs(technology.effects) do + if effect.type == "unlock-recipe" and effect.recipe == recipe then + addit = false + end + end + if addit then + table.insert(technology.effects, { type = "unlock-recipe", recipe = recipe }) + end +end + +local function remove_recipe_unlock(technology, recipe) + if technology.effects then + for i, effect in pairs(technology.effects) do + if effect.type == "unlock-recipe" and effect.recipe == recipe then + table.remove(technology.effects, i) + end + end + end +end + +function thxbob.lib.tech.has_recipe_unlock(technology, recipe) + if + type(technology) == "string" + and type(recipe) == "string" + and data.raw.technology[technology] + and data.raw.recipe[recipe] + then + local hasit = false + hasit = has_recipe_unlock(data.raw.technology[technology], recipe) + + return hasit + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + thxbob.lib.error.recipe(recipe) + return false + end +end + +function thxbob.lib.tech.add_recipe_unlock(technology, recipe) + if + type(technology) == "string" + and type(recipe) == "string" + and data.raw.technology[technology] + and data.raw.recipe[recipe] + then + add_recipe_unlock(data.raw.technology[technology], recipe) + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + thxbob.lib.error.recipe(recipe) + end +end + +function thxbob.lib.tech.remove_recipe_unlock(technology, recipe) + if + type(technology) == "string" + and type(recipe) == "string" + and data.raw.technology[technology] + -- data.raw.recipe[recipe] --don't check to see if something we're removing exists. + then + remove_recipe_unlock(data.raw.technology[technology], recipe) + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + thxbob.lib.error.recipe(recipe) + end +end + +local function add_prerequisite(technology, prerequisite) + local addit = true + if technology.prerequisites then + for i, check in ipairs(technology.prerequisites) do + if check == prerequisite then + addit = false + end + end + else + technology.prerequisites = {} + end + if addit then + table.insert(technology.prerequisites, prerequisite) + end +end + +local function remove_prerequisite(technology, prerequisite) + if technology.prerequisites then + for i, check in ipairs(technology.prerequisites) do + if check == prerequisite then + table.remove(technology.prerequisites, i) + end + end + end +end + +local function replace_prerequisite(technology, old, new) + if technology.prerequisites then + for i, prerequisite in ipairs(technology.prerequisites) do + if prerequisite == old then + remove_prerequisite(technology, old) + add_prerequisite(technology, new) + end + end + end +end + +function thxbob.lib.tech.replace_prerequisite(technology, old, new) + if + type(technology) == "string" + and type(old) == "string" + and type(new) == "string" + and data.raw.technology[technology] + -- data.raw.technology[old] and + and data.raw.technology[new] + then + replace_prerequisite(data.raw.technology[technology], old, new) + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + thxbob.lib.error.technology(old, "Old prerequisite", "Old prerequisite technology") + thxbob.lib.error.technology(new, "New prerequisite", "New prerequisite technology") + end +end + +function thxbob.lib.tech.add_prerequisite(technology, prerequisite) + if + type(technology) == "string" + and type(prerequisite) == "string" + and data.raw.technology[technology] + and data.raw.technology[prerequisite] + then + add_prerequisite(data.raw.technology[technology], prerequisite) + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + thxbob.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology") + end +end + +function thxbob.lib.tech.remove_prerequisite(technology, prerequisite) + if + type(technology) == "string" + and type(prerequisite) == "string" + and data.raw.technology[technology] + -- data.raw.technology[prerequisite] + then + remove_prerequisite(data.raw.technology[technology], prerequisite) + else + log(debug.traceback()) + thxbob.lib.error.technology(technology) + thxbob.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology") + end +end + +function thxbob.lib.tech.hide(technology_name) + if type(technology_name) == "string" and data.raw.technology[technology_name] then + local technology = data.raw.technology[technology_name] + technology.hidden = true + technology.enabled = false + else + log(debug.traceback()) + thxbob.lib.error.technology(technology_name) + end +end + +function thxbob.lib.tech.ignore_tech_cost_multiplier(technology_name, ignore) + if type(technology_name) == "string" and type(ignore) == "boolean" then + local technology = data.raw.technology[technology_name] + if technology then + technology.ignore_tech_cost_multiplier = ignore + end + else + log(debug.traceback()) + thxbob.lib.error.technology(technology_name) + end +end + +function thxbob.lib.tech.technology_icon_constant(technology_icon, constant_icon, x, y) + local scale = 0.5 + local xshift = x or 64 + local yshift = y or 64 + if type(technology_icon) == "table" and technology_icon.icon and technology_icon.icon_size then + local icons = { + technology_icon, + { + icon = constant_icon, + icon_size = 128, + scale = scale, + shift = { xshift * scale, yshift * scale }, + }, + } + return icons + else + log(debug.traceback()) + log(technology_icon .. " not given in required table format") + end +end + +function thxbob.lib.tech.technology_line_icon_constant(technology_line, first, last, technology_icon, constant_icon) + local scale = 0.5 + if type(technology_icon) == "table" and technology_icon.icon and technology_icon.icon_size then + for i = first, last do + local tech_name = technology_line .. "-" .. i + if data.raw.technology[tech_name] then + data.raw.technology[tech_name].icons = { + technology_icon, + { + icon = constant_icon, + icon_size = 128, + scale = scale, + shift = { 64 * scale, 64 * scale }, + }, + } + else + log(debug.traceback()) + thxbob.lib.error.technology(tech_name) + end + end + else + log(debug.traceback()) + log(technology_icon .. " not given in required table format") + end +end