Compare commits
21 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3ce2d83c22 | ||
![]() |
36dba7614e | ||
![]() |
bbcc586128 | ||
![]() |
c36fd418ab | ||
![]() |
620c9f48cf | ||
![]() |
ea7785c761 | ||
![]() |
27d9dd6c68 | ||
![]() |
be0781340a | ||
![]() |
bcd8c95388 | ||
![]() |
0390af3d42 | ||
![]() |
cd9886bb34 | ||
![]() |
0a394221e5 | ||
![]() |
acd86bfe2b | ||
![]() |
f8c4b94165 | ||
![]() |
39dca1c3b2 | ||
![]() |
77a4f3d401 | ||
![]() |
c829e856f7 | ||
![]() |
b84902ce6a | ||
![]() |
22c5f9ddc7 | ||
![]() |
b9b7b515cc | ||
![]() |
dca2bd9129 |
5 changed files with 300 additions and 19 deletions
|
@ -1,5 +1,58 @@
|
|||
---------------------------------------------------------------------------------------------------
|
||||
Version: 0.0.2
|
||||
Date: 07.01.2025
|
||||
Changes:
|
||||
- Add Technology class
|
||||
---------------------------------------------------------------------------------------------------
|
||||
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
|
||||
Date: 28.02.2025
|
||||
Changes:
|
||||
- Add table.trim
|
||||
---------------------------------------------------------------------------------------------------
|
||||
Version: 0.0.7
|
||||
Date: 26.02.2025
|
||||
Changes:
|
||||
- Add table.concat
|
||||
Bug Fixes:
|
||||
- Bugfix in Recipe
|
||||
---------------------------------------------------------------------------------------------------
|
||||
Version: 0.0.6
|
||||
Date: 15.02.2025
|
||||
Bug Fixes:
|
||||
- Bugfix in table.removeValue
|
||||
---------------------------------------------------------------------------------------------------
|
||||
Version: 0.0.5
|
||||
Date: 15.02.2025
|
||||
Changes:
|
||||
- Add Recipe class
|
||||
---------------------------------------------------------------------------------------------------
|
||||
Version: 0.0.4
|
||||
Date: 13.02.2025
|
||||
Changes:
|
||||
- Add Technology:addEffect
|
||||
---------------------------------------------------------------------------------------------------
|
||||
Version: 0.0.3
|
||||
Date: 02.02.2025
|
||||
Changes:
|
||||
- Prevent duplicate entries when adding a recipe to a technology
|
||||
---------------------------------------------------------------------------------------------------
|
||||
Version: 0.0.2
|
||||
Date: 07.01.2025
|
||||
Changes:
|
||||
- Add Technology class
|
159
cf-lib/data/Recipe.lua
Normal file
159
cf-lib/data/Recipe.lua
Normal file
|
@ -0,0 +1,159 @@
|
|||
---- Utility class for a single recipe
|
||||
---- @class Recipe
|
||||
local Recipe = {}
|
||||
|
||||
---- Pass a recipe name or a recipe table to get a Recipe object
|
||||
---- @param value string|table The name of the recipe or the recipe table
|
||||
function Recipe:new(value)
|
||||
local name
|
||||
local recipe
|
||||
|
||||
if type(value) == "string" then
|
||||
name = value
|
||||
recipe = data.raw.recipe[name]
|
||||
if not recipe then
|
||||
log("Recipe not found: " .. name)
|
||||
return nil
|
||||
end
|
||||
elseif type(value) == "table" then
|
||||
name = value.name
|
||||
recipe = value
|
||||
end
|
||||
|
||||
local obj = {
|
||||
prototype = recipe
|
||||
}
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
---- Applies the recipe to the game
|
||||
function Recipe:apply()
|
||||
data:extend({ self.prototype })
|
||||
end
|
||||
|
||||
---- Assigns data to the recipe
|
||||
---- Shorthand for table.assign(recipe.prototype, data)
|
||||
---- @param data table The data to assign
|
||||
function Recipe:assign(data)
|
||||
table.assign(self.prototype, data)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds an ingredient to the recipe
|
||||
--- @param ingredientName string The name of the ingredient
|
||||
--- @param amount number The amount of the ingredient (default: 1)
|
||||
function Recipe:addIngredient(ingredientName, amount)
|
||||
amount = amount or 1
|
||||
local ingredientType = data.raw.item[ingredientName] and "item"
|
||||
or data.raw.module[ingredientName] and "item"
|
||||
or data.raw.fluid[ingredientName] and "fluid"
|
||||
or nil
|
||||
if not ingredientType then
|
||||
log("Unknown ingredient: " .. ingredientName)
|
||||
return
|
||||
end
|
||||
self.prototype.ingredients = self.prototype.ingredients or {}
|
||||
for _, ingredient in pairs(self.prototype.ingredients) do
|
||||
if ingredient.name == ingredientName then
|
||||
ingredient.amount = amount
|
||||
ingredient.amount_min = nil
|
||||
ingredient.amount_max = nil
|
||||
ingredient.probability = nil
|
||||
return
|
||||
end
|
||||
end
|
||||
table.insert(self.prototype.ingredients, { name = ingredientName, amount = amount, type = ingredientType })
|
||||
return self
|
||||
end
|
||||
|
||||
--- Replaces an existing ingredient by name with a new ingredient or adjusts the amount
|
||||
--- @param old string The name of the existing ingredient
|
||||
--- @param new? string The name of the new ingredient
|
||||
--- @param amount? number The amount of the new ingredient
|
||||
function Recipe:replaceIngredient(old, new, amount)
|
||||
if type(new) == "number" then
|
||||
amount = new
|
||||
new = old
|
||||
end
|
||||
if self.prototype.ingredients then
|
||||
for _, result in pairs(self.prototype.ingredients) do
|
||||
if result.name == old then
|
||||
result.name = new
|
||||
result.amount = amount or result.amount
|
||||
return self
|
||||
end
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Removes an existing ingredient by name
|
||||
--- @param ingredientName string The name of the ingredient
|
||||
function Recipe:removeIngredient(ingredientName)
|
||||
if self.prototype.ingredients then
|
||||
for i, result in pairs(self.prototype.ingredients) do
|
||||
if result.name == ingredientName then
|
||||
self.prototype.ingredients[i] = nil
|
||||
return self
|
||||
end
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Replaces an existing result by name with a new result
|
||||
--- @param old string The name of the existing result
|
||||
--- @param new? string The name of the new result
|
||||
--- @param amount? number The amount of the new result (keeps the old value if not set)
|
||||
--- @param expensiveAmount? number The amount of the new result for the expensive recipe (uses amount if not set)
|
||||
function Recipe:replaceResult(old, new, amount, expensiveAmount)
|
||||
if type(new) == "number" then
|
||||
expensiveAmount = amount
|
||||
amount = new
|
||||
new = old
|
||||
end
|
||||
if self.prototype.results then
|
||||
for _, result in pairs(self.prototype.results) do
|
||||
if result.name == old then
|
||||
result.name = new
|
||||
result.amount = amount or result.amount
|
||||
end
|
||||
end
|
||||
if table.main_product == old then
|
||||
table.main_product = new
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds the recipe to a technology
|
||||
--- @param technology string|table The name of the technology or the technology table
|
||||
function Recipe:unlockedByTechnology(technology)
|
||||
if not technology then
|
||||
return
|
||||
end
|
||||
technology = type(technology) == "table" and technology or data.raw.technology[technology]
|
||||
if (not technology.effects) then
|
||||
technology.effects = {}
|
||||
end
|
||||
for _, effect in pairs(technology.effects) do
|
||||
if effect.type == "unlock-recipe" and effect.recipe == self.prototype.name then
|
||||
return self
|
||||
end
|
||||
end
|
||||
table.insert(technology.effects, { type = "unlock-recipe", recipe = self.prototype.name })
|
||||
return self
|
||||
end
|
||||
|
||||
---- Clones the recipe
|
||||
---- @param name string The name of the new recipe
|
||||
function Recipe:clone(name)
|
||||
local clone = table.deepcopy(self.prototype)
|
||||
clone.name = name
|
||||
return Recipe:new(clone)
|
||||
end
|
||||
|
||||
return Recipe
|
|
@ -47,32 +47,38 @@ end
|
|||
--- @param data table The data to assign
|
||||
function Technology:assign(data)
|
||||
table.assign(self.prototype, data)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the prerequisite for the technology
|
||||
--- @param prerequisites table The names of the prerequisites
|
||||
function Technology:setPrerequisites(prerequisites)
|
||||
self.prototype.prerequisites = prerequisites
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds a prerequisite to the technology
|
||||
--- @param prerequisite string|table The name of the prerequisite or the prerequisite Technology instance
|
||||
function Technology:addPrerequisite(prerequisite)
|
||||
local _prerequisite = prerequisiteName(prerequisite)
|
||||
self.prototype.prerequisites = self.prototype.prerequisites or {}
|
||||
if (not table.contains(self.prototype.prerequisites, _prerequisite)) then
|
||||
table.insert(self.prototype.prerequisites, _prerequisite)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds multiple prerequisites to the technology
|
||||
--- @param prerequisites table The names of the prerequisite or the prerequisite Technology instances
|
||||
function Technology:addPrerequisites(prerequisites)
|
||||
self.prototype.prerequisites = self.prototype.prerequisites or {}
|
||||
for _, prerequisite in pairs(prerequisites) do
|
||||
local _prerequisite = prerequisiteName(prerequisite)
|
||||
if (not table.contains(self.prototype.prerequisites, _prerequisite)) then
|
||||
table.insert(self.prototype.prerequisites, _prerequisite)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Replaces a prerequisite in a technology
|
||||
|
@ -86,6 +92,7 @@ function Technology:replacePrerequisite(old, new)
|
|||
self.prototype.prerequisites[i] = _new
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Removes a prerequisite from the technology
|
||||
|
@ -95,9 +102,10 @@ function Technology:removePrerequisite(prerequisite)
|
|||
for i, techPrerequisite in pairs(self.prototype.prerequisites) do
|
||||
if techPrerequisite == _prerequisite then
|
||||
table.remove(self.prototype.prerequisites, i)
|
||||
return
|
||||
return self
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds an ingredient to the technology
|
||||
|
@ -107,6 +115,7 @@ function Technology:addIngredient(ingredientName, amount)
|
|||
self.prototype.unit = self.prototype.unit or { ingredients = {} }
|
||||
self.prototype.unit.ingredients = self.prototype.unit.ingredients or {}
|
||||
table.insert(self.prototype.unit.ingredients, { ingredientName, amount or 1 })
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds a list of ingredients to the technology with the default amount of 1
|
||||
|
@ -115,6 +124,7 @@ function Technology:addIngredients(ingredientNames)
|
|||
for _, ingredientName in pairs(ingredientNames) do
|
||||
self:addIngredient(ingredientName)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Removes an existing ingredient by name
|
||||
|
@ -127,12 +137,35 @@ function Technology:removeIngredient(ingredientName)
|
|||
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
|
||||
|
||||
--- Adds a recipe unlock to the technology
|
||||
--- @param recipeName string The name of the recipe to unlock
|
||||
function Technology:addRecipe(recipeName)
|
||||
for _, effect in pairs(self.prototype.effects) do
|
||||
if effect.type == "unlock-recipe" and effect.recipe == recipeName then
|
||||
return self
|
||||
end
|
||||
end
|
||||
table.insert(self.prototype.effects, { type = "unlock-recipe", recipe = recipeName })
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds an effect to the technology
|
||||
--- @param effect table The effect to add
|
||||
function Technology:addEffect(effect)
|
||||
table.insert(self.prototype.effects, effect)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Removes a recipe unlock from the technology
|
||||
|
@ -141,9 +174,10 @@ function Technology:removeRecipe(recipeName)
|
|||
for i, effect in pairs(self.prototype.effects) do
|
||||
if effect.type == "unlock-recipe" and effect.recipe == recipeName then
|
||||
table.remove(self.prototype.effects, i)
|
||||
return
|
||||
return self
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Clones the technology
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "cf-lib",
|
||||
"version": "0.0.2",
|
||||
"title": "cackling fiends library",
|
||||
"description": "Because I'd like to have my own library :-)",
|
||||
"author": "cackling fiend",
|
||||
"homepage": "",
|
||||
"factorio_version": "2.0",
|
||||
"dependencies": [
|
||||
"base"
|
||||
]
|
||||
{
|
||||
"name": "cf-lib",
|
||||
"version": "0.0.12",
|
||||
"title": "cackling fiends library",
|
||||
"description": "Because I'd like to have my own library :-)",
|
||||
"author": "cackling fiend",
|
||||
"homepage": "",
|
||||
"factorio_version": "2.0",
|
||||
"dependencies": [
|
||||
"base"
|
||||
]
|
||||
}
|
|
@ -46,7 +46,7 @@ end
|
|||
table.removeValue = function(target, value)
|
||||
for i, v in ipairs(target) do
|
||||
if v == value then
|
||||
target.remove(target, i)
|
||||
table.remove(target, i)
|
||||
return target
|
||||
end
|
||||
end
|
||||
|
@ -68,4 +68,39 @@ function table.filter(target, predicate)
|
|||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
--- Concatenates two tables into a new table.
|
||||
--- @param table1 table The first table
|
||||
--- @param table2 table The second table
|
||||
--- @return table The concatenated table
|
||||
function table.concatTables(table1, table2)
|
||||
local result = {}
|
||||
for _, v in pairs(table1) do
|
||||
table.insert(result, v)
|
||||
end
|
||||
for _, v in pairs(table2) do
|
||||
table.insert(result, v)
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
--- Trims nil values from a table.
|
||||
--- @param target table The table to trim
|
||||
--- @return table The trimmed table
|
||||
function table.trim(target)
|
||||
return table.filter(target, function(v)
|
||||
return v ~= nil
|
||||
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
|
Loading…
Add table
Reference in a new issue