This commit is contained in:
Brevven 2022-08-01 22:20:38 -07:00
parent 88a35dae23
commit e63d66ffae

View file

@ -111,11 +111,16 @@ function util.add_effect(technology_name, effect)
end end
end end
-- Add an effect to a given technology to unlock recipe
function util.add_unlock(technology_name, recipe)
util.add_effect(technology_name, {type="unlock-recipe", recipe=recipe})
end
-- remove recipe unlock effect from a given technology -- remove recipe unlock effect from a given technology
function util.remove_recipe_effect(technology_name, recipe_name) function util.remove_recipe_effect(technology_name, recipe_name)
local technology = data.raw.technology[technology_name] local technology = data.raw.technology[technology_name]
local index = -1 local index = -1
if technology then if technology and technology.effects then
for i, effect in pairs(technology.effects) do for i, effect in pairs(technology.effects) do
if effect.type == "unlock-recipe" and effect.recipe == recipe_name then if effect.type == "unlock-recipe" and effect.recipe == recipe_name then
index = i index = i
@ -144,6 +149,14 @@ function util.set_enabled(recipe_name, enabled)
end end
end end
function util.set_hidden(recipe_name)
if data.raw.recipe[recipe_name] then
if data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].normal.hidden = true end
if data.raw.recipe[recipe_name].expensive then data.raw.recipe[recipe_name].expensive.hidden = true end
if not data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].hidden = true end
end
end
-- Add a given quantity of ingredient to a given recipe -- Add a given quantity of ingredient to a given recipe
function util.add_or_add_to_ingredient(recipe_name, ingredient, quantity) function util.add_or_add_to_ingredient(recipe_name, ingredient, quantity)
if me.bypass[recipe_name] then return end if me.bypass[recipe_name] then return end
@ -183,7 +196,6 @@ function add_ingredient(recipe, ingredient, quantity, is_fluid)
if recipe ~= nil and recipe.ingredients ~= nil then if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do for i, existing in pairs(recipe.ingredients) do
if existing[1] == ingredient or existing.name == ingredient then if existing[1] == ingredient or existing.name == ingredient then
log("Not adding "..ingredient.." -- duplicate")
return return
end end
end end
@ -270,28 +282,78 @@ function add_product(recipe, product)
end end
end end
-- Get the amount of the ingredient, will check base/normal not expensive
function util.get_ingredient_amount(recipe_name, ingredient_name)
local recipe = data.raw.recipe[recipe_name]
if recipe then
if recipe.normal and recipe.normal.ingredients then
for i, ingredient in pairs(recipe.normal.ingredients) do
if ingredient[1] == ingredient_name then return ingredient[2] end
if ingredient.name == ingredient_name then return ingredient.amount end
end
elseif recipe.ingredients then
for i, ingredient in pairs(recipe.ingredients) do
if ingredient[1] == ingredient_name then return ingredient[2] end
if ingredient.name == ingredient_name then return ingredient.amount end
end
end
return 1
end
return 0
end
-- Get the amount of the result, will check base/normal not expensive
function util.get_amount(recipe_name, product)
if not product then product = recipe_name end
local recipe = data.raw.recipe[recipe_name]
if recipe then
if recipe.normal and recipe.normal.results then
for i, result in pairs(recipe.normal.results) do
if result[1] == product then return result[2] end
if result.name == product then return result.amount end
end
elseif recipe.normal and recipe.normal.result_count then
return recipe.normal.result_count
elseif recipe.results then
for i, result in pairs(recipe.results) do
if result[1] == product then return result[2] end
if result.name == product then return result.amount end
end
elseif recipe.result_count then
return recipe.result_count
end
return 1
end
return 0
end
-- Replace one ingredient with another in a recipe -- Replace one ingredient with another in a recipe
function util.replace_ingredient(recipe_name, old, new) function util.replace_ingredient(recipe_name, old, new, amount)
if me.bypass[recipe_name] then return end if me.bypass[recipe_name] then return end
if data.raw.recipe[recipe_name] and (data.raw.item[new] or data.raw.fluid[new]) then if data.raw.recipe[recipe_name] and (data.raw.item[new] or data.raw.fluid[new]) then
me.add_modified(recipe_name) me.add_modified(recipe_name)
replace_ingredient(data.raw.recipe[recipe_name], old, new) replace_ingredient(data.raw.recipe[recipe_name], old, new, amount)
replace_ingredient(data.raw.recipe[recipe_name].normal, old, new) replace_ingredient(data.raw.recipe[recipe_name].normal, old, new, amount)
replace_ingredient(data.raw.recipe[recipe_name].expensive, old, new) replace_ingredient(data.raw.recipe[recipe_name].expensive, old, new, amount)
end end
end end
function replace_ingredient(recipe, old, new) function replace_ingredient(recipe, old, new, amount)
if recipe ~= nil and recipe.ingredients ~= nil then if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do for i, existing in pairs(recipe.ingredients) do
if existing[1] == new or existing.name == new then if existing[1] == new or existing.name == new then
log("Not adding "..new.." -- duplicate")
return return
end end
end end
for i, ingredient in pairs(recipe.ingredients) do for i, ingredient in pairs(recipe.ingredients) do
if ingredient.name == old then ingredient.name = new end if ingredient.name == old then
if ingredient[1] == old then ingredient[1] = new end ingredient.name = new
if amount then ingredient.amount = amount end
end
if ingredient[1] == old then
ingredient[1] = new
if amount then ingredient[2] = amount end
end
end end
end end
end end
@ -338,7 +400,6 @@ function replace_some_ingredient(recipe, old, old_amount, new, new_amount, is_fl
if recipe ~= nil and recipe.ingredients ~= nil then if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do for i, existing in pairs(recipe.ingredients) do
if existing[1] == new or existing.name == new then if existing[1] == new or existing.name == new then
log("Not adding "..new.." -- duplicate")
return return
end end
end end
@ -354,6 +415,46 @@ function replace_some_ingredient(recipe, old, old_amount, new, new_amount, is_fl
end end
end end
-- set the amount of a product.
function util.set_product_amount(recipe_name, product, amount)
me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then
if me.bypass[recipe_name] then return end
set_product_amount(data.raw.recipe[recipe_name], product, amount)
set_product_amount(data.raw.recipe[recipe_name].normal, product, amount)
set_product_amount(data.raw.recipe[recipe_name].expensive, product, amount)
end
end
function set_product_amount(recipe, product, amount)
if recipe then
if recipe.result_count then
recipe.result_count = amount
end
if recipe.results then
for i, result in pairs(recipe.results) do
if result.name == product then
if result.amount then
result.amount = amount
end
if result.amount_min ~= nil then
result.amount_min = nil
result.amount_max = nil
result.amount = amount
end
end
if result[1] == product then
result[2] = amount
end
end
end
if not recipe.results and not recipe.result_count then
-- implicit one item result
recipe.result_count = amount
end
end
end
-- multiply the cost, energy, and results of a recipe by a multiple -- multiply the cost, energy, and results of a recipe by a multiple
function util.multiply_recipe(recipe_name, multiple) function util.multiply_recipe(recipe_name, multiple)
me.add_modified(recipe_name) me.add_modified(recipe_name)
@ -510,6 +611,25 @@ function util.remove_raw(t, name)
end end
end end
-- Set energy required
function util.set_recipe_time(recipe_name, time)
me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then
if me.bypass[recipe_name] then return end
set_recipe_time(data.raw.recipe[recipe_name], time)
set_recipe_time(data.raw.recipe[recipe_name].normal, time)
set_recipe_time(data.raw.recipe[recipe_name].expensive, time)
end
end
function set_recipe_time(recipe, time)
if recipe then
if recipe.energy_required then
recipe.energy_required = time
end
end
end
-- Multiply energy required -- Multiply energy required
function util.multiply_time(recipe_name, factor) function util.multiply_time(recipe_name, factor)
me.add_modified(recipe_name) me.add_modified(recipe_name)
@ -551,7 +671,7 @@ end
-- Set recipe category -- Set recipe category
function util.set_category(recipe_name, category) function util.set_category(recipe_name, category)
if me.bypass[recipe_name] then return end if me.bypass[recipe_name] then return end
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] and data.raw["recipe-category"][category] then
me.add_modified(recipe_name) me.add_modified(recipe_name)
data.raw.recipe[recipe_name].category = category data.raw.recipe[recipe_name].category = category
end end
@ -601,10 +721,9 @@ end
-- Add crafting category to an entity -- Add crafting category to an entity
function util.add_crafting_category(entity_type, entity, category) function util.add_crafting_category(entity_type, entity, category)
if data.raw[entity_type][entity] then if data.raw[entity_type][entity] and data.raw["recipe-category"][category] then
for i, existing in pairs(data.raw[entity_type][entity].crafting_categories) do for i, existing in pairs(data.raw[entity_type][entity].crafting_categories) do
if existing == category then if existing == category then
log(entity.." not adding "..category.." -- duplicate")
return return
end end
end end