This commit is contained in:
Brevven 2024-12-27 22:56:26 -08:00
parent 6e4fadb699
commit cb0b405a73
2 changed files with 310 additions and 146 deletions

View file

@ -12,6 +12,20 @@ function decode(data)
return table.concat(str, "") return table.concat(str, "")
end end
function util.check_fluid_mining()
if me.fluid_mining then
for i, force in pairs(game.forces) do
if (
(force.technologies["uranium-processing"] and force.technologies["uranium-processing"].researched) or
(force.technologies["titanium-processing"] and force.technologies["titanium-processing"].researched) or
false
) then
force.technologies["fluid-mining"].researched = true
end
end
end
end
function util.get_list() function util.get_list()
local p = game.item_prototypes[me.name.."-list"] local p = game.item_prototypes[me.name.."-list"]
if p then if p then
@ -26,4 +40,153 @@ function util.force_enable_recipe(event, recipe_name)
end end
end end
function util.warptorio2_expansion_helper()
if script.active_mods["warptorio2_expansion"] then
function check_container_for_items(container,items)
local has_all =true
for k=1,#items do
if container.get_item_count(items[k].name)<items[k].count then has_all=false break end
end
return has_all
end
function remove_items_from_container(container,items)
for k=1,#items do
container.remove_item(items[k])
end
end
script.on_nth_tick(60, function (event)
if global.done then return end
local fix_items={
{name='iron-plate',count=100},
{name='iron-gear-wheel',count=100},
{name='repair-pack',count=50},
}
local entities = {}
for i=1,100 do
if game.surfaces[i] then
local lentities= game.surfaces[i].find_entities_filtered{area = {{-100, -100}, {100, 100}}, name = "wpe_broken_lab"}
for j, entity in pairs(lentities) do
table.insert(entities, entity)
end
end
end
if #entities == 0 then
if global.checking then
-- The lab has already been fixed
global.done = true
else
-- Check that the lab doesn't reappear due to a warp
global.checking = true
end
return
end
if check_container_for_items(entities[1],fix_items) then
remove_items_from_container(entities[1],fix_items)
local lab = entities[1].surface.create_entity({name='wpe_repaired_lab', position=entities[1].position, force = game.forces.player})
lab.destructible=false
lab.minable=false
entities[1].destroy()
global.done = true
end
end)
end
end
function util.ore_fix()
for _, resource in pairs(me.resources) do
local map_gen_settings = game.surfaces.nauvis.map_gen_settings
if map_gen_settings.autoplace_controls[resource] == nil then
map_gen_settings.autoplace_controls[resource] = {}
end
if map_gen_settings.autoplace_settings.entity.settings[resource] == nil then
map_gen_settings.autoplace_settings.entity.settings[resource] = {}
end
game.surfaces.nauvis.map_gen_settings = map_gen_settings
end
end
-- A workaround for generating ores until this bug is fixed:
-- https://forums.factorio.com/viewtopic.php?f=7&t=124996&p=655013#p655013
function util.ore_workaround(event)
for i, ore in pairs(util.me.ores_for_workaround) do
if (
event.surface and
event.surface.map_gen_settings and
event.surface.map_gen_settings.autoplace_controls and
event.surface.map_gen_settings.autoplace_controls["titanium-ore"]
) then
return
end
if event.surface.name ~= "nauvis" then return end
if math.random() < settings.global[util.me.name.."-ore-workaround-probability"].value then
util.generate_ore(event, ore.name, ore.amount, ore.tiles)
end
end
end
-- The majority of this function was written by Eradicator, see https://forums.factorio.com/viewtopic.php?t=72723
function util.generate_ore(event, name, amount, tiles)
local biases = {[0] = {[0] = 1}}
local t = 1
repeat
t = t + util.grow(biases,t,tiles)
until t >= tiles
local pos = {x=event.position.x*32, y=event.position.y*32}
local multiplier = math.max(math.abs(event.position.x), math.abs(event.position.y))
if multiplier < 10 then return end -- don't generate too close to start
local total_bias = 0
for x,_ in pairs(biases) do for y,bias in pairs(_) do
total_bias = total_bias + bias
end end
for x,_ in pairs(biases) do for y,bias in pairs(_) do
local entity = {
name = name,
amount = amount * (bias/total_bias) * multiplier,
force = 'neutral',
position = {pos.x+x,pos.y+y},
}
if event.surface.can_place_entity(entity) then
event.surface.create_entity(entity)
end
end end
end
-- The majority of this function was written by Eradicator, see https://forums.factorio.com/viewtopic.php?t=72723
function util.grow(grid,t,tiles)
local w_max = 256
local h_max = 256
local abs = math.abs
local old = {}
local new_count = 0
for x,_ in pairs(grid) do for y,__ in pairs(_) do
table.insert(old,{x,y})
end end
for _,pos in pairs(old) do
local x,y = pos[1],pos[2]
local bias = grid[x][y]
for dx=-1,1,1 do for dy=-1,1,1 do
local a,b = x+dx, y+dy
if (math.random() > 0.9) and (abs(a) < w_max) and (abs(b) < h_max) then
grid[a] = grid[a] or {}
if not grid[a][b] then
grid[a][b] = 1 - (t/tiles)
new_count = new_count + 1
if (new_count+t) == tiles then return new_count end
end
end
end end
end
return new_count
end
return util return util

View file

@ -11,6 +11,10 @@ util.get_setting = util.me.get_setting
util.titanium_plate = "" util.titanium_plate = ""
util.titanium_processing = "" util.titanium_processing = ""
util.A = {{"automation-science-pack", 1}}
util.AL = {{"automation-science-pack", 1}, {"logistic-science-pack", 1}}
util.ALC = {{"automation-science-pack", 1}, {"logistic-science-pack", 1}, {"chemical-science-pack", 1}}
if mods["FactorioExtended-Plus-Core"] then if mods["FactorioExtended-Plus-Core"] then
util.titanium_plate = "titanium-alloy" util.titanium_plate = "titanium-alloy"
else else
@ -23,6 +27,28 @@ else
util.titanium_processing = "titanium-processing" util.titanium_processing = "titanium-processing"
end end
util.vacuum_icon = { icon="__base__/graphics/icons/fluid/steam.png", tint={r=.1, g=.1, b=.5, a=.5} }
util.vacuum_icon_small = { icon="__base__/graphics/icons/fluid/steam.png", tint={r=.1, g=.1, b=.5, a=.5}, scale=0.25, shift={-8,-8}, }
util.vacuum_vis = {r=.1, g=.1, b=.5}
function util.item(item, quantity, probability)
if not quantity then
quantity = 1
end
if probability then
return {type="item", name=item, amount=quantity, probability=probability}
else
return {type="item", name=item, amount=quantity}
end
end
function util.fluid(fluid, quantity)
if not quantity then
quantity = 10
end
return {type="fluid", name=fluid, amount=quantity}
end
function util.se6() function util.se6()
return mods["space-exploration"] and mods["space-exploration"] >= "0.6" return mods["space-exploration"] and mods["space-exploration"] >= "0.6"
end end
@ -91,6 +117,54 @@ function util.contains(table, sought)
return false return false
end end
-- Replace 'uranium-mining' tech with 'fluid-mining', defaulting to same costs
function util.add_fluid_mining()
if data.raw.technology["fluid-mining"] then return end
util.remove_raw("technology", "uranium-mining")
data:extend({
{
type = "technology",
name = "fluid-mining",
icon = "__"..util.me.name.."__/graphics/technology/fluid-mining.png",
icon_size = 256,
effects =
{
{
type = "mining-with-fluid",
modifier = true
}
},
prerequisites = {"chemical-science-pack", "concrete"},
unit =
{
count = 100,
ingredients =
{
{"automation-science-pack", 1},
{"logistic-science-pack", 1},
{"chemical-science-pack", 1}
},
time = 30,
}
}
})
end
-- Final fix to make sure nothing uses "uranium-mining"
function util.use_fluid_mining_final()
for i, tech in pairs(data.raw.technology) do
if tech.prerequisites then
for j, pre in pairs(tech.prerequisites) do
if pre == "uranium-mining" then
util.add_prerequisite(tech.name, "fluid-mining")
util.remove_prerequisite(tech.name, "uranium-mining")
break
end
end
end
end
end
-- se landfill -- se landfill
-- params: ore, icon_size -- params: ore, icon_size
@ -173,7 +247,7 @@ function util.k2matter(params)
}, },
time = 45, time = 45,
}, },
localised_name = {"technology-name.k2-conversion", {"item-name."..params.k2matter.item_name}}, -- (ignore for now) localised_name = {"technology-name.k2-conversion", {"item-name."..params.k2matter.item_name}},
}, },
}) })
end end
@ -298,15 +372,12 @@ function util.se_matter(params)
end end
end end
-- deprecated
-- Get the normal prototype for a recipe -- either .normal or the recipe itself -- Get the normal prototype for a recipe -- either .normal or the recipe itself
function util.get_normal(recipe_name) function util.get_normal(recipe_name)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
recipe = data.raw.recipe[recipe_name] recipe = data.raw.recipe[recipe_name]
if recipe.normal and recipe.normal.ingredients then return recipe
return recipe.normal
elseif recipe.ingredients then
return recipe
end
end end
end end
@ -421,17 +492,13 @@ end
function util.set_enabled(recipe_name, enabled) function util.set_enabled(recipe_name, enabled)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
if data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].normal.enabled = enabled end data.raw.recipe[recipe_name].enabled = enabled
if data.raw.recipe[recipe_name].expensive then data.raw.recipe[recipe_name].expensive.enabled = enabled end
if not data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].enabled = enabled end
end end
end end
function util.set_hidden(recipe_name) function util.set_hidden(recipe_name)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
if data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].normal.hidden = true end data.raw.recipe[recipe_name].hidden = true
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
end end
@ -441,15 +508,13 @@ function util.add_or_add_to_ingredient(recipe_name, ingredient, quantity, option
if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then
me.add_modified(recipe_name) me.add_modified(recipe_name)
add_or_add_to_ingredient(data.raw.recipe[recipe_name], ingredient, quantity) add_or_add_to_ingredient(data.raw.recipe[recipe_name], ingredient, quantity)
add_or_add_to_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity)
add_or_add_to_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity)
end end
end end
function add_or_add_to_ingredient(recipe, ingredient, quantity) function add_or_add_to_ingredient(recipe, ingredient, quantity)
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.name == ingredient then
add_to_ingredient(recipe, ingredient, quantity) add_to_ingredient(recipe, ingredient, quantity)
return return
end end
@ -465,22 +530,20 @@ function util.add_ingredient(recipe_name, ingredient, quantity, options)
if data.raw.recipe[recipe_name] and (data.raw.item[ingredient] or is_fluid) then if data.raw.recipe[recipe_name] and (data.raw.item[ingredient] or is_fluid) then
me.add_modified(recipe_name) me.add_modified(recipe_name)
add_ingredient(data.raw.recipe[recipe_name], ingredient, quantity, is_fluid) add_ingredient(data.raw.recipe[recipe_name], ingredient, quantity, is_fluid)
add_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity, is_fluid)
add_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity, is_fluid)
end end
end end
function add_ingredient(recipe, ingredient, quantity, is_fluid) 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.name == ingredient then
return return
end end
end end
if is_fluid then if is_fluid then
table.insert(recipe.ingredients, {type="fluid", name=ingredient, amount=quantity}) table.insert(recipe.ingredients, {type="fluid", name=ingredient, amount=quantity})
else else
table.insert(recipe.ingredients, {ingredient, quantity}) table.insert(recipe.ingredients, {type="item", name=ingredient, amount=quantity})
end end
end end
end end
@ -488,21 +551,16 @@ end
-- Add a given ingredient prototype to a given recipe -- Add a given ingredient prototype to a given recipe
function util.add_ingredient_raw(recipe_name, ingredient, options) function util.add_ingredient_raw(recipe_name, ingredient, options)
if not should_force(options) and bypass(recipe_name) then return end if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] and (data.raw.item[ingredient.name] or data.raw.item[ingredient[1]]) then if data.raw.recipe[recipe_name] and data.raw.item[ingredient.name] then
me.add_modified(recipe_name) me.add_modified(recipe_name)
add_ingredient_raw(data.raw.recipe[recipe_name], ingredient) add_ingredient_raw(data.raw.recipe[recipe_name], ingredient)
add_ingredient_raw(data.raw.recipe[recipe_name].normal, ingredient)
add_ingredient_raw(data.raw.recipe[recipe_name].expensive, ingredient)
end end
end end
function add_ingredient_raw(recipe, ingredient) function add_ingredient_raw(recipe, ingredient)
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 ( if existing.name == ingredient.name then
(existing[1] and (existing[1] == ingredient[1] or existing[1] == ingredient.name)) or
(existing.name and (existing.name == ingredient[1] or existing.name == ingredient.name))
) then
return return
end end
end end
@ -516,18 +574,13 @@ function util.set_ingredient(recipe_name, ingredient, quantity, options)
if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then
me.add_modified(recipe_name) me.add_modified(recipe_name)
set_ingredient(data.raw.recipe[recipe_name], ingredient, quantity) set_ingredient(data.raw.recipe[recipe_name], ingredient, quantity)
set_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity)
set_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity)
end end
end end
function set_ingredient(recipe, ingredient, quantity) function set_ingredient(recipe, ingredient, quantity)
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 then if existing.name == ingredient then
existing[2] = quantity
return
elseif existing.name == ingredient then
existing.amount = quantity existing.amount = quantity
existing.amount_min = nil existing.amount_min = nil
existing.amount_max = nil existing.amount_max = nil
@ -542,43 +595,31 @@ end
function util.add_product(recipe_name, product, options) function util.add_product(recipe_name, product, options)
if not should_force(options) and bypass(recipe_name) then return end if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] and if data.raw.recipe[recipe_name] and
(data.raw.item[product[1]] or data.raw.item[product.name] or (data.raw.item[product.name] or data.raw.fluid[product.name]
data.raw.fluid[product[1]] or data.raw.fluid[product.name]
) then ) then
add_product(data.raw.recipe[recipe_name], product) add_product(data.raw.recipe[recipe_name], product)
add_product(data.raw.recipe[recipe_name].normal, product)
add_product(data.raw.recipe[recipe_name].expensive, product)
end end
end end
function add_product(recipe, product) function add_product(recipe, product)
if recipe ~= nil then if recipe ~= nil then
if (product[1] and data.raw.item[product[1]]) or if product.name and data.raw[product.type][product.name] then
(product.name and data.raw[product.type][product.name]) then if recipe.results == nil then
if not recipe.normal then recipe.results = {{recipe.result, recipe.result_count and recipe.result_count or 1}}
if recipe.results == nil then
recipe.results = {{recipe.result, recipe.result_count and recipe.result_count or 1}}
end
recipe.result = nil
recipe.result_count = nil
table.insert(recipe.results, product)
end end
recipe.result = nil
recipe.result_count = nil
table.insert(recipe.results, product)
end end
end end
end end
-- Get the amount of the ingredient, will check base/normal not expensive -- Get the amount of the ingredient
function util.get_ingredient_amount(recipe_name, ingredient_name) function util.get_ingredient_amount(recipe_name, ingredient_name)
local recipe = data.raw.recipe[recipe_name] local recipe = data.raw.recipe[recipe_name]
if recipe then if recipe then
if recipe.normal and recipe.normal.ingredients then if recipe.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 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 if ingredient.name == ingredient_name then return ingredient.amount end
end end
end end
@ -587,27 +628,17 @@ function util.get_ingredient_amount(recipe_name, ingredient_name)
return 0 return 0
end end
-- Get the amount of the result, will check base/normal not expensive -- Get the amount of the result
function util.get_amount(recipe_name, product) function util.get_amount(recipe_name, product)
if not product then product = recipe_name end if not product then product = recipe_name end
local recipe = data.raw.recipe[recipe_name] local recipe = data.raw.recipe[recipe_name]
if recipe then if recipe then
if recipe.normal and recipe.normal.results then if recipe.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 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 if result.name == product then return result.amount end
end end
elseif recipe.result_count then
return recipe.result_count
end end
return 1 return 0
end end
return 0 return 0
end end
@ -617,9 +648,7 @@ function util.get_result_count(recipe_name, product)
if not product then product = recipe_name end if not product then product = recipe_name end
local recipe = data.raw.recipe[recipe_name] local recipe = data.raw.recipe[recipe_name]
if recipe then if recipe then
if recipe.normal and recipe.normal.results then if recipe.results then
return #(recipe.normal.results)
elseif recipe.results then
return #(recipe.results) return #(recipe.results)
end end
return 1 return 1
@ -634,15 +663,13 @@ function util.replace_ingredient(recipe_name, old, new, amount, multiply, option
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, amount, multiply) replace_ingredient(data.raw.recipe[recipe_name], old, new, amount, multiply)
replace_ingredient(data.raw.recipe[recipe_name].normal, old, new, amount, multiply)
replace_ingredient(data.raw.recipe[recipe_name].expensive, old, new, amount, multiply)
end end
end end
function replace_ingredient(recipe, old, new, amount, multiply) function replace_ingredient(recipe, old, new, amount, multiply)
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.name == new then
return return
end end
end end
@ -656,16 +683,6 @@ function replace_ingredient(recipe, old, new, amount, multiply)
ingredient.amount = amount ingredient.amount = amount
end end
end end
end
if ingredient[1] == old then
ingredient[1] = new
if amount then
if multiply then
ingredient[2] = amount * ingredient[2]
else
ingredient[2] = amount
end
end
end end
end end
end end
@ -677,8 +694,6 @@ function util.remove_ingredient(recipe_name, old, options)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
me.add_modified(recipe_name) me.add_modified(recipe_name)
remove_ingredient(data.raw.recipe[recipe_name], old) remove_ingredient(data.raw.recipe[recipe_name], old)
remove_ingredient(data.raw.recipe[recipe_name].normal, old)
remove_ingredient(data.raw.recipe[recipe_name].expensive, old)
end end
end end
@ -686,7 +701,7 @@ function remove_ingredient(recipe, old)
index = -1 index = -1
if recipe ~= nil and recipe.ingredients ~= nil then if recipe ~= nil and recipe.ingredients ~= nil then
for i, ingredient in pairs(recipe.ingredients) do for i, ingredient in pairs(recipe.ingredients) do
if ingredient.name == old or ingredient[1] == old then if ingredient.name == old then
index = i index = i
break break
end end
@ -704,8 +719,6 @@ function util.replace_some_product(recipe_name, old, old_amount, new, new_amount
if data.raw.recipe[recipe_name] and (data.raw.item[new] or is_fluid) then if data.raw.recipe[recipe_name] and (data.raw.item[new] or is_fluid) then
me.add_modified(recipe_name) me.add_modified(recipe_name)
replace_some_product(data.raw.recipe[recipe_name], old, old_amount, new, new_amount, is_fluid) replace_some_product(data.raw.recipe[recipe_name], old, old_amount, new, new_amount, is_fluid)
replace_some_product(data.raw.recipe[recipe_name].normal, old, old_amount, new, new_amount, is_fluid)
replace_some_product(data.raw.recipe[recipe_name].expensive, old, old_amount, new, new_amount, is_fluid)
end end
end end
@ -714,7 +727,7 @@ function replace_some_product(recipe, old, old_amount, new, new_amount)
if recipe.result == new then return end if recipe.result == new then return end
if recipe.results then if recipe.results then
for i, existing in pairs(recipe.results) do for i, existing in pairs(recipe.results) do
if existing[1] == new or existing.name == new then if existing.name == new then
return return
end end
end end
@ -723,9 +736,6 @@ function replace_some_product(recipe, old, old_amount, new, new_amount)
for i, product in pairs(recipe.results) do for i, product in pairs(recipe.results) do
if product.name == old then if product.name == old then
product.amount = math.max(1, product.amount - old_amount) product.amount = math.max(1, product.amount - old_amount)
end
if product[1] == old then
product[2] = math.max(1, product[2] - old_amount)
end end
end end
end end
@ -738,46 +748,57 @@ function util.replace_some_ingredient(recipe_name, old, old_amount, new, new_amo
if data.raw.recipe[recipe_name] and (data.raw.item[new] or is_fluid) then if data.raw.recipe[recipe_name] and (data.raw.item[new] or is_fluid) then
me.add_modified(recipe_name) me.add_modified(recipe_name)
replace_some_ingredient(data.raw.recipe[recipe_name], old, old_amount, new, new_amount, is_fluid) replace_some_ingredient(data.raw.recipe[recipe_name], old, old_amount, new, new_amount, is_fluid)
replace_some_ingredient(data.raw.recipe[recipe_name].normal, old, old_amount, new, new_amount, is_fluid)
replace_some_ingredient(data.raw.recipe[recipe_name].expensive, old, old_amount, new, new_amount, is_fluid)
end end
end end
function replace_some_ingredient(recipe, old, old_amount, new, new_amount, is_fluid) function replace_some_ingredient(recipe, old, old_amount, new, new_amount, 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] == new or existing.name == new then if existing.name == new then
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 if ingredient.name == old then
ingredient.amount = math.max(1, ingredient.amount - old_amount) ingredient.amount = math.max(1, ingredient.amount - old_amount)
end
if ingredient[1] == old then
ingredient[2] = math.max(1, ingredient[2] - old_amount)
end end
end end
add_ingredient(recipe, new, new_amount, is_fluid) add_ingredient(recipe, new, new_amount, is_fluid)
end end
end end
-- set the probability of a product.
function util.set_product_probability(recipe_name, product, probability, options)
if not should_force(options) and bypass(recipe_name) then return end
me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then
set_product_probability(data.raw.recipe[recipe_name], product, probability)
end
end
function set_product_probability(recipe, product, probability)
if recipe then
if recipe.results then
for i, result in pairs(recipe.results) do
if result.name == product then
result.probability = probability
end
end
end
end
end
-- set the amount of a product. -- set the amount of a product.
function util.set_product_amount(recipe_name, product, amount, options) function util.set_product_amount(recipe_name, product, amount, options)
if not should_force(options) and bypass(recipe_name) then return end if not should_force(options) and bypass(recipe_name) then return end
me.add_modified(recipe_name) me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
set_product_amount(data.raw.recipe[recipe_name], product, amount) 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
end end
function set_product_amount(recipe, product, amount) function set_product_amount(recipe, product, amount)
if recipe then if recipe then
if recipe.result_count then
recipe.result_count = amount
end
if recipe.results then if recipe.results then
for i, result in pairs(recipe.results) do for i, result in pairs(recipe.results) do
if result.name == product then if result.name == product then
@ -790,9 +811,6 @@ function set_product_amount(recipe, product, amount)
result.amount = amount result.amount = amount
end end
end end
if result[1] == product then
result[2] = amount
end
end end
end end
if not recipe.results and not recipe.result_count then if not recipe.results and not recipe.result_count then
@ -808,8 +826,6 @@ function util.multiply_recipe(recipe_name, multiple, options)
me.add_modified(recipe_name) me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
multiply_recipe(data.raw.recipe[recipe_name], multiple) multiply_recipe(data.raw.recipe[recipe_name], multiple)
multiply_recipe(data.raw.recipe[recipe_name].normal, multiple)
multiply_recipe(data.raw.recipe[recipe_name].expensive, multiple)
end end
end end
@ -837,9 +853,6 @@ function multiply_recipe(recipe, multiple)
result.catalyst_amount = result.catalyst_amount * multiple result.catalyst_amount = result.catalyst_amount * multiple
end end
end end
if result[1] then
result[2] = result[2] * multiple
end
end end
end end
if not recipe.results and not recipe.result_count then if not recipe.results and not recipe.result_count then
@ -851,9 +864,6 @@ function multiply_recipe(recipe, multiple)
if ingredient.name then if ingredient.name then
ingredient.amount = ingredient.amount * multiple ingredient.amount = ingredient.amount * multiple
end end
if ingredient[1] then
ingredient[2] = ingredient[2] * multiple
end
end end
end end
end end
@ -861,15 +871,14 @@ end
-- Returns true if a recipe has an ingredient -- Returns true if a recipe has an ingredient
function util.has_ingredient(recipe_name, ingredient) function util.has_ingredient(recipe_name, ingredient)
return data.raw.recipe[recipe_name] and ( return data.raw.recipe[recipe_name] and
has_ingredient(data.raw.recipe[recipe_name], ingredient) or has_ingredient(data.raw.recipe[recipe_name], ingredient)
has_ingredient(data.raw.recipe[recipe_name].normal, ingredient))
end end
function has_ingredient(recipe, ingredient) function has_ingredient(recipe, ingredient)
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.name == ingredient then
return true return true
end end
end end
@ -883,8 +892,6 @@ function util.remove_product(recipe_name, old, options)
me.add_modified(recipe_name) me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
remove_product(data.raw.recipe[recipe_name], old) remove_product(data.raw.recipe[recipe_name], old)
remove_product(data.raw.recipe[recipe_name].normal, old)
remove_product(data.raw.recipe[recipe_name].expensive, old)
end end
end end
@ -892,7 +899,7 @@ function remove_product(recipe, old)
index = -1 index = -1
if recipe ~= nil and recipe.results ~= nil then if recipe ~= nil and recipe.results ~= nil then
for i, result in pairs(recipe.results) do for i, result in pairs(recipe.results) do
if result.name == old or result[1] == old then if result.name == old then
index = i index = i
break break
end end
@ -907,8 +914,6 @@ function util.set_main_product(recipe_name, product, options)
if not should_force(options) and bypass(recipe_name) then return end if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
set_main_product(data.raw.recipe[recipe_name], product) set_main_product(data.raw.recipe[recipe_name], product)
set_main_product(data.raw.recipe[recipe_name].normal, product)
set_main_product(data.raw.recipe[recipe_name].expensive, product)
end end
end end
@ -923,8 +928,6 @@ function util.replace_product(recipe_name, old, new, options)
if not should_force(options) and bypass(recipe_name) then return end if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
replace_product(data.raw.recipe[recipe_name], old, new, options) replace_product(data.raw.recipe[recipe_name], old, new, options)
replace_product(data.raw.recipe[recipe_name].normal, old, new, options)
replace_product(data.raw.recipe[recipe_name].expensive, old, new, options)
end end
end end
@ -940,7 +943,6 @@ function replace_product(recipe, old, new, options)
if recipe.results then if recipe.results then
for i, result in pairs(recipe.results) do for i, result in pairs(recipe.results) do
if result.name == old then result.name = new end if result.name == old then result.name = new end
if result[1] == old then result[1] = new end
end end
end end
end end
@ -968,8 +970,6 @@ function util.set_recipe_time(recipe_name, time, options)
me.add_modified(recipe_name) me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
set_recipe_time(data.raw.recipe[recipe_name], time) 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
end end
@ -987,8 +987,6 @@ function util.multiply_time(recipe_name, factor, options)
me.add_modified(recipe_name) me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
multiply_time(data.raw.recipe[recipe_name], factor) multiply_time(data.raw.recipe[recipe_name], factor)
multiply_time(data.raw.recipe[recipe_name].normal, factor)
multiply_time(data.raw.recipe[recipe_name].expensive, factor)
end end
end end
@ -1006,8 +1004,6 @@ function util.add_time(recipe_name, amount, options)
me.add_modified(recipe_name) me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
add_time(data.raw.recipe[recipe_name], amount) add_time(data.raw.recipe[recipe_name], amount)
add_time(data.raw.recipe[recipe_name].normal, amount)
add_time(data.raw.recipe[recipe_name].expensive, amount)
end end
end end
@ -1068,13 +1064,6 @@ function util.add_icon(recipe_name, icon, options)
icon_size=data.raw.item[data.raw.recipe[recipe_name].result].icon_size, icon_size=data.raw.item[data.raw.recipe[recipe_name].result].icon_size,
icon_mipmaps=data.raw.item[data.raw.recipe[recipe_name].result].icon_mipmaps, icon_mipmaps=data.raw.item[data.raw.recipe[recipe_name].result].icon_mipmaps,
}} }}
elseif data.raw.recipe[recipe_name].normal and
data.raw.item[data.raw.recipe[recipe_name].normal.result] then
data.raw.recipe[recipe_name].icons = {{
icon=data.raw.item[data.raw.recipe[recipe_name].normal.result].icon,
icon_size=data.raw.item[data.raw.recipe[recipe_name].normal.result].icon_size,
icon_mipmaps=data.raw.item[data.raw.recipe[recipe_name].normal.result].icon_mipmaps,
}}
end end
data.raw.recipe[recipe_name].icon = nil data.raw.recipe[recipe_name].icon = nil
data.raw.recipe[recipe_name].icon_size = nil data.raw.recipe[recipe_name].icon_size = nil
@ -1094,6 +1083,17 @@ function util.set_icons(recipe_name, icons, options)
end end
end end
-- Set tech icons
function util.set_tech_icons(technology, icons, options)
if not should_force(options) and bypass(technology) then return end
if data.raw.technology[technology] then
me.add_modified(technology)
data.raw.technology[technology].icons = icons
data.raw.technology[technology].icon = nil
data.raw.technology[technology].icon_size = nil
end
end
-- Set recipe icons -- Set recipe icons
function util.set_item_icons(item_name, icons) function util.set_item_icons(item_name, icons)
if data.raw.item[item_name] then if data.raw.item[item_name] then
@ -1137,8 +1137,6 @@ function util.add_to_ingredient(recipe, ingredient, amount, options)
if not should_force(options) and bypass(recipe_name) then return end if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe] then if data.raw.recipe[recipe] then
add_to_ingredient(data.raw.recipe[recipe], ingredient, amount) add_to_ingredient(data.raw.recipe[recipe], ingredient, amount)
add_to_ingredient(data.raw.recipe[recipe].normal, ingredient, amount)
add_to_ingredient(data.raw.recipe[recipe].expensive, ingredient, amount)
end end
end end
@ -1161,8 +1159,6 @@ function util.add_to_product(recipe_name, product, amount, options)
if not should_force(options) and bypass(recipe_name) then return end if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] then if data.raw.recipe[recipe_name] then
add_to_product(data.raw.recipe[recipe_name], product, amount) add_to_product(data.raw.recipe[recipe_name], product, amount)
add_to_product(data.raw.recipe[recipe_name].normal, product, amount)
add_to_product(data.raw.recipe[recipe_name].expensive, product, amount)
end end
end end
@ -1176,10 +1172,6 @@ function add_to_product(recipe, product, amount)
if result.name == product then if result.name == product then
result.amount = result.amount + amount result.amount = result.amount + amount
return return
end
if result[1] == product then
result[2] = result[2] + amount
return
end end
end end
end end
@ -1382,4 +1374,13 @@ function util.set_vtk_dcm_ingredients()
end end
end end
-- Recalculate recycling recipes, since our mod might make updates after quality generates them
function util.redo_recycling()
if mods.quality then
local recycling = require("__quality__.prototypes.recycling")
for _, recipe in pairs(data.raw.recipe) do
recycling.generate_recycling_recipe(recipe)
end
end
end
return util return util