Compare commits
8 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3ce2d83c22 | ||
![]() |
36dba7614e | ||
![]() |
bbcc586128 | ||
![]() |
c36fd418ab | ||
![]() |
620c9f48cf | ||
![]() |
ea7785c761 | ||
![]() |
27d9dd6c68 | ||
![]() |
be0781340a |
5 changed files with 69 additions and 9 deletions
|
@ -1,4 +1,25 @@
|
||||||
---------------------------------------------------------------------------------------------------
|
---------------------------------------------------------------------------------------------------
|
||||||
|
Version: 0.0.12
|
||||||
|
Date: 06.04.2025
|
||||||
|
Changes:
|
||||||
|
Add table.count
|
||||||
|
---------------------------------------------------------------------------------------------------
|
||||||
|
Version: 0.0.11
|
||||||
|
Date: 28.03.2025
|
||||||
|
Changes:
|
||||||
|
- Add Technology:setIngredients
|
||||||
|
---------------------------------------------------------------------------------------------------
|
||||||
|
Version: 0.0.10
|
||||||
|
Date: 16.03.2025
|
||||||
|
Changes:
|
||||||
|
- Recipe: addIngredient amount defaults to 1
|
||||||
|
- Functions are chainable
|
||||||
|
---------------------------------------------------------------------------------------------------
|
||||||
|
Version: 0.0.9
|
||||||
|
Date: 28.02.2025
|
||||||
|
Bug Fixes:
|
||||||
|
- Rename `table.concat` to `table.concatTables` to resolve conflict
|
||||||
|
---------------------------------------------------------------------------------------------------
|
||||||
Version: 0.0.8
|
Version: 0.0.8
|
||||||
Date: 28.02.2025
|
Date: 28.02.2025
|
||||||
Changes:
|
Changes:
|
||||||
|
|
|
@ -39,12 +39,14 @@ end
|
||||||
---- @param data table The data to assign
|
---- @param data table The data to assign
|
||||||
function Recipe:assign(data)
|
function Recipe:assign(data)
|
||||||
table.assign(self.prototype, data)
|
table.assign(self.prototype, data)
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds an ingredient to the recipe
|
--- Adds an ingredient to the recipe
|
||||||
--- @param ingredientName string The name of the ingredient
|
--- @param ingredientName string The name of the ingredient
|
||||||
--- @param amount number The amount of the ingredient
|
--- @param amount number The amount of the ingredient (default: 1)
|
||||||
function Recipe:addIngredient(ingredientName, amount)
|
function Recipe:addIngredient(ingredientName, amount)
|
||||||
|
amount = amount or 1
|
||||||
local ingredientType = data.raw.item[ingredientName] and "item"
|
local ingredientType = data.raw.item[ingredientName] and "item"
|
||||||
or data.raw.module[ingredientName] and "item"
|
or data.raw.module[ingredientName] and "item"
|
||||||
or data.raw.fluid[ingredientName] and "fluid"
|
or data.raw.fluid[ingredientName] and "fluid"
|
||||||
|
@ -64,6 +66,7 @@ function Recipe:addIngredient(ingredientName, amount)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.insert(self.prototype.ingredients, { name = ingredientName, amount = amount, type = ingredientType })
|
table.insert(self.prototype.ingredients, { name = ingredientName, amount = amount, type = ingredientType })
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Replaces an existing ingredient by name with a new ingredient or adjusts the amount
|
--- Replaces an existing ingredient by name with a new ingredient or adjusts the amount
|
||||||
|
@ -80,10 +83,11 @@ function Recipe:replaceIngredient(old, new, amount)
|
||||||
if result.name == old then
|
if result.name == old then
|
||||||
result.name = new
|
result.name = new
|
||||||
result.amount = amount or result.amount
|
result.amount = amount or result.amount
|
||||||
return
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Removes an existing ingredient by name
|
--- Removes an existing ingredient by name
|
||||||
|
@ -93,10 +97,11 @@ function Recipe:removeIngredient(ingredientName)
|
||||||
for i, result in pairs(self.prototype.ingredients) do
|
for i, result in pairs(self.prototype.ingredients) do
|
||||||
if result.name == ingredientName then
|
if result.name == ingredientName then
|
||||||
self.prototype.ingredients[i] = nil
|
self.prototype.ingredients[i] = nil
|
||||||
return
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Replaces an existing result by name with a new result
|
--- Replaces an existing result by name with a new result
|
||||||
|
@ -121,6 +126,7 @@ function Recipe:replaceResult(old, new, amount, expensiveAmount)
|
||||||
table.main_product = new
|
table.main_product = new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds the recipe to a technology
|
--- Adds the recipe to a technology
|
||||||
|
@ -135,10 +141,11 @@ function Recipe:unlockedByTechnology(technology)
|
||||||
end
|
end
|
||||||
for _, effect in pairs(technology.effects) do
|
for _, effect in pairs(technology.effects) do
|
||||||
if effect.type == "unlock-recipe" and effect.recipe == self.prototype.name then
|
if effect.type == "unlock-recipe" and effect.recipe == self.prototype.name then
|
||||||
return
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.insert(technology.effects, { type = "unlock-recipe", recipe = self.prototype.name })
|
table.insert(technology.effects, { type = "unlock-recipe", recipe = self.prototype.name })
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
---- Clones the recipe
|
---- Clones the recipe
|
||||||
|
|
|
@ -47,12 +47,14 @@ end
|
||||||
--- @param data table The data to assign
|
--- @param data table The data to assign
|
||||||
function Technology:assign(data)
|
function Technology:assign(data)
|
||||||
table.assign(self.prototype, data)
|
table.assign(self.prototype, data)
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Sets the prerequisite for the technology
|
--- Sets the prerequisite for the technology
|
||||||
--- @param prerequisites table The names of the prerequisites
|
--- @param prerequisites table The names of the prerequisites
|
||||||
function Technology:setPrerequisites(prerequisites)
|
function Technology:setPrerequisites(prerequisites)
|
||||||
self.prototype.prerequisites = prerequisites
|
self.prototype.prerequisites = prerequisites
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds a prerequisite to the technology
|
--- Adds a prerequisite to the technology
|
||||||
|
@ -63,6 +65,7 @@ function Technology:addPrerequisite(prerequisite)
|
||||||
if (not table.contains(self.prototype.prerequisites, _prerequisite)) then
|
if (not table.contains(self.prototype.prerequisites, _prerequisite)) then
|
||||||
table.insert(self.prototype.prerequisites, _prerequisite)
|
table.insert(self.prototype.prerequisites, _prerequisite)
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds multiple prerequisites to the technology
|
--- Adds multiple prerequisites to the technology
|
||||||
|
@ -75,6 +78,7 @@ function Technology:addPrerequisites(prerequisites)
|
||||||
table.insert(self.prototype.prerequisites, _prerequisite)
|
table.insert(self.prototype.prerequisites, _prerequisite)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Replaces a prerequisite in a technology
|
--- Replaces a prerequisite in a technology
|
||||||
|
@ -88,6 +92,7 @@ function Technology:replacePrerequisite(old, new)
|
||||||
self.prototype.prerequisites[i] = _new
|
self.prototype.prerequisites[i] = _new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Removes a prerequisite from the technology
|
--- Removes a prerequisite from the technology
|
||||||
|
@ -97,9 +102,10 @@ function Technology:removePrerequisite(prerequisite)
|
||||||
for i, techPrerequisite in pairs(self.prototype.prerequisites) do
|
for i, techPrerequisite in pairs(self.prototype.prerequisites) do
|
||||||
if techPrerequisite == _prerequisite then
|
if techPrerequisite == _prerequisite then
|
||||||
table.remove(self.prototype.prerequisites, i)
|
table.remove(self.prototype.prerequisites, i)
|
||||||
return
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds an ingredient to the technology
|
--- Adds an ingredient to the technology
|
||||||
|
@ -109,6 +115,7 @@ function Technology:addIngredient(ingredientName, amount)
|
||||||
self.prototype.unit = self.prototype.unit or { ingredients = {} }
|
self.prototype.unit = self.prototype.unit or { ingredients = {} }
|
||||||
self.prototype.unit.ingredients = self.prototype.unit.ingredients or {}
|
self.prototype.unit.ingredients = self.prototype.unit.ingredients or {}
|
||||||
table.insert(self.prototype.unit.ingredients, { ingredientName, amount or 1 })
|
table.insert(self.prototype.unit.ingredients, { ingredientName, amount or 1 })
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds a list of ingredients to the technology with the default amount of 1
|
--- Adds a list of ingredients to the technology with the default amount of 1
|
||||||
|
@ -117,6 +124,7 @@ function Technology:addIngredients(ingredientNames)
|
||||||
for _, ingredientName in pairs(ingredientNames) do
|
for _, ingredientName in pairs(ingredientNames) do
|
||||||
self:addIngredient(ingredientName)
|
self:addIngredient(ingredientName)
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Removes an existing ingredient by name
|
--- Removes an existing ingredient by name
|
||||||
|
@ -129,6 +137,16 @@ function Technology:removeIngredient(ingredientName)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Sets the ingredients for the technology
|
||||||
|
--- @param ingredientNames table The names of the ingredients
|
||||||
|
function Technology:setIngredients(ingredients)
|
||||||
|
self.prototype.unit = self.prototype.unit or {}
|
||||||
|
self.prototype.unit.ingredients = {}
|
||||||
|
self:addIngredients(ingredients)
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds a recipe unlock to the technology
|
--- Adds a recipe unlock to the technology
|
||||||
|
@ -136,16 +154,18 @@ end
|
||||||
function Technology:addRecipe(recipeName)
|
function Technology:addRecipe(recipeName)
|
||||||
for _, effect in pairs(self.prototype.effects) do
|
for _, effect in pairs(self.prototype.effects) do
|
||||||
if effect.type == "unlock-recipe" and effect.recipe == recipeName then
|
if effect.type == "unlock-recipe" and effect.recipe == recipeName then
|
||||||
return
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.insert(self.prototype.effects, { type = "unlock-recipe", recipe = recipeName })
|
table.insert(self.prototype.effects, { type = "unlock-recipe", recipe = recipeName })
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds an effect to the technology
|
--- Adds an effect to the technology
|
||||||
--- @param effect table The effect to add
|
--- @param effect table The effect to add
|
||||||
function Technology:addEffect(effect)
|
function Technology:addEffect(effect)
|
||||||
table.insert(self.prototype.effects, effect)
|
table.insert(self.prototype.effects, effect)
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Removes a recipe unlock from the technology
|
--- Removes a recipe unlock from the technology
|
||||||
|
@ -154,9 +174,10 @@ function Technology:removeRecipe(recipeName)
|
||||||
for i, effect in pairs(self.prototype.effects) do
|
for i, effect in pairs(self.prototype.effects) do
|
||||||
if effect.type == "unlock-recipe" and effect.recipe == recipeName then
|
if effect.type == "unlock-recipe" and effect.recipe == recipeName then
|
||||||
table.remove(self.prototype.effects, i)
|
table.remove(self.prototype.effects, i)
|
||||||
return
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Clones the technology
|
--- Clones the technology
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "cf-lib",
|
"name": "cf-lib",
|
||||||
"version": "0.0.8",
|
"version": "0.0.12",
|
||||||
"title": "cackling fiends library",
|
"title": "cackling fiends library",
|
||||||
"description": "Because I'd like to have my own library :-)",
|
"description": "Because I'd like to have my own library :-)",
|
||||||
"author": "cackling fiend",
|
"author": "cackling fiend",
|
||||||
|
|
|
@ -74,7 +74,7 @@ end
|
||||||
--- @param table1 table The first table
|
--- @param table1 table The first table
|
||||||
--- @param table2 table The second table
|
--- @param table2 table The second table
|
||||||
--- @return table The concatenated table
|
--- @return table The concatenated table
|
||||||
function table.concat(table1, table2)
|
function table.concatTables(table1, table2)
|
||||||
local result = {}
|
local result = {}
|
||||||
for _, v in pairs(table1) do
|
for _, v in pairs(table1) do
|
||||||
table.insert(result, v)
|
table.insert(result, v)
|
||||||
|
@ -92,4 +92,15 @@ function table.trim(target)
|
||||||
return table.filter(target, function(v)
|
return table.filter(target, function(v)
|
||||||
return v ~= nil
|
return v ~= nil
|
||||||
end)
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Counts the entries of given table.
|
||||||
|
--- @param target table Table to count
|
||||||
|
--- @return number Amount of entries
|
||||||
|
function table.count(target)
|
||||||
|
local count = 0
|
||||||
|
for _ in pairs(target) do
|
||||||
|
count = count + 1
|
||||||
|
end
|
||||||
|
return count
|
||||||
end
|
end
|
Loading…
Add table
Reference in a new issue