lib updates

This commit is contained in:
Brevven 2021-12-31 01:37:04 -08:00
parent fc3db6df54
commit 56b5ee5fc1
2 changed files with 65 additions and 6 deletions

View file

@ -1,4 +1,6 @@
-- WARNING - this file will be overwritten, edit bzlib/data-util.lua
-- WARNING WARNING WARNING
-- This file will be overwritten in mod zipfiles, edit bzlib/data-util.lua
-- WARNING WARNING WARNING
local me = require("me")
local util = {}
@ -20,6 +22,13 @@ function util.get_stack_size(default)
return default
end
function util.k2assets()
if mods["Krastorio2Assets"] then
return "__Krastorio2Assets__"
end
return "__Krastorio2__/graphics"
end
-- check if a table contains a sought value
function util.contains(table, sought)
for i, value in pairs(table) do
@ -32,7 +41,7 @@ end
-- Add a prerequisite to a given technology
function util.add_prerequisite(technology_name, prerequisite)
technology = data.raw.technology[technology_name]
local technology = data.raw.technology[technology_name]
if technology and data.raw.technology[prerequisite] then
if technology.prerequisites then
table.insert(technology.prerequisites, prerequisite)
@ -44,7 +53,7 @@ end
-- Remove a prerequisite from a given technology
function util.remove_prerequisite(technology_name, prerequisite)
technology = data.raw.technology[technology_name]
local technology = data.raw.technology[technology_name]
local index = -1
if technology and data.raw.technology[prerequisite] then
for i, prereq in pairs(technology.prerequisites) do
@ -61,7 +70,7 @@ end
-- Add an effect to a given technology
function util.add_effect(technology_name, effect)
technology = data.raw.technology[technology_name]
local technology = data.raw.technology[technology_name]
if technology then
table.insert(technology.effects, effect)
end
@ -69,7 +78,7 @@ end
-- Set technology ingredients
function util.set_tech_recipe(technology_name, ingredients)
technology = data.raw.technology[technology_name]
local technology = data.raw.technology[technology_name]
if technology then
technology.unit.ingredients = ingredients
end
@ -351,4 +360,54 @@ function util.add_crafting_category(entity_type, entity, category)
end
end
function util.add_to_ingredient(recipe, ingredient, amount)
if data.raw.recipe[recipe] then
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
function add_to_ingredient(recipe, it, amount)
if recipe ~= nil and recipe.ingredients ~= nil then
for i, ingredient in pairs(recipe.ingredients) do
if ingredient.name == it then
ingredient.amount = ingredient.amount + amount
return
end
if ingredient[1] == it then
ingredient[2] = ingredients[2] + amount
return
end
end
end
end
function util.add_to_product(recipe, product, amount)
if data.raw.recipe[recipe] then
add_to_product(data.raw.recipe[recipe], product, amount)
add_to_product(data.raw.recipe[recipe].normal, product, amount)
add_to_product(data.raw.recipe[recipe].expensive, product, amount)
end
end
function add_to_product(recipe, product, amount)
if recipe ~= nil and recipe.results ~= nil then
if recipe.result == product then
recipe.result_count = recipe.result_count + amount
return
end
for i, result in pairs(recipe.results) do
if result.name == product then
result.amount = result.amount + amount
return
end
if result[1] == product then
result[2] = result[2] + amount
return
end
end
end
end
return util