From be0781340ae5521d4f17320b7ac9715c3f256061 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Fri, 28 Feb 2025 23:16:17 +0100 Subject: [PATCH 01/12] Rename `table.concat` to `table.concatTables` to resolve conflict --- cf-lib/changelog.txt | 5 +++++ cf-lib/info.json | 2 +- cf-lib/util/table.lua | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cf-lib/changelog.txt b/cf-lib/changelog.txt index 4b31875..b9642fa 100644 --- a/cf-lib/changelog.txt +++ b/cf-lib/changelog.txt @@ -1,4 +1,9 @@ --------------------------------------------------------------------------------------------------- +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: diff --git a/cf-lib/info.json b/cf-lib/info.json index 0eb9de6..652f221 100644 --- a/cf-lib/info.json +++ b/cf-lib/info.json @@ -1,6 +1,6 @@ { "name": "cf-lib", - "version": "0.0.8", + "version": "0.0.9", "title": "cackling fiends library", "description": "Because I'd like to have my own library :-)", "author": "cackling fiend", diff --git a/cf-lib/util/table.lua b/cf-lib/util/table.lua index 0e8f32b..e44b559 100644 --- a/cf-lib/util/table.lua +++ b/cf-lib/util/table.lua @@ -74,7 +74,7 @@ end --- @param table1 table The first table --- @param table2 table The second table --- @return table The concatenated table -function table.concat(table1, table2) +function table.concatTables(table1, table2) local result = {} for _, v in pairs(table1) do table.insert(result, v) From 27d9dd6c68abb3f69dea7aaec250b45ae096e4b6 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Fri, 14 Mar 2025 18:13:37 +0100 Subject: [PATCH 02/12] Recipe: addIngredient amount defaults to 1 --- cf-lib/data/Recipe.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cf-lib/data/Recipe.lua b/cf-lib/data/Recipe.lua index 52e453b..4bbd0f1 100644 --- a/cf-lib/data/Recipe.lua +++ b/cf-lib/data/Recipe.lua @@ -43,8 +43,9 @@ end --- Adds an ingredient to the recipe --- @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) + 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" From ea7785c761d0cdc7efa03590626423274e203b3d Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Sun, 16 Mar 2025 18:40:40 +0100 Subject: [PATCH 03/12] Functions are chainable --- cf-lib/data/Recipe.lua | 12 +++++++++--- cf-lib/data/Technology.lua | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cf-lib/data/Recipe.lua b/cf-lib/data/Recipe.lua index 4bbd0f1..1a5c67e 100644 --- a/cf-lib/data/Recipe.lua +++ b/cf-lib/data/Recipe.lua @@ -39,6 +39,7 @@ end ---- @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 @@ -65,6 +66,7 @@ function Recipe:addIngredient(ingredientName, amount) 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 @@ -81,10 +83,11 @@ function Recipe:replaceIngredient(old, new, amount) if result.name == old then result.name = new result.amount = amount or result.amount - return + return self end end end + return self end --- Removes an existing ingredient by name @@ -94,10 +97,11 @@ function Recipe:removeIngredient(ingredientName) for i, result in pairs(self.prototype.ingredients) do if result.name == ingredientName then self.prototype.ingredients[i] = nil - return + return self end end end + return self end --- Replaces an existing result by name with a new result @@ -122,6 +126,7 @@ function Recipe:replaceResult(old, new, amount, expensiveAmount) table.main_product = new end end + return self end --- Adds the recipe to a technology @@ -136,10 +141,11 @@ function Recipe:unlockedByTechnology(technology) end for _, effect in pairs(technology.effects) do if effect.type == "unlock-recipe" and effect.recipe == self.prototype.name then - return + return self end end table.insert(technology.effects, { type = "unlock-recipe", recipe = self.prototype.name }) + return self end ---- Clones the recipe diff --git a/cf-lib/data/Technology.lua b/cf-lib/data/Technology.lua index 2c641d5..f30d739 100644 --- a/cf-lib/data/Technology.lua +++ b/cf-lib/data/Technology.lua @@ -47,12 +47,14 @@ 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 @@ -63,6 +65,7 @@ function Technology:addPrerequisite(prerequisite) 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 @@ -75,6 +78,7 @@ function Technology:addPrerequisites(prerequisites) table.insert(self.prototype.prerequisites, _prerequisite) end end + return self end --- Replaces a prerequisite in a technology @@ -88,6 +92,7 @@ function Technology:replacePrerequisite(old, new) self.prototype.prerequisites[i] = _new end end + return self end --- Removes a prerequisite from the technology @@ -97,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 @@ -109,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 @@ -117,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 @@ -129,6 +137,7 @@ function Technology:removeIngredient(ingredientName) end end end + return self end --- Adds a recipe unlock to the technology @@ -136,16 +145,18 @@ end function Technology:addRecipe(recipeName) for _, effect in pairs(self.prototype.effects) do if effect.type == "unlock-recipe" and effect.recipe == recipeName then - return + 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 @@ -154,9 +165,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 From 620c9f48cfe455f3a655f64837224297c8711489 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Sun, 16 Mar 2025 18:42:02 +0100 Subject: [PATCH 04/12] 0.0.10 --- cf-lib/changelog.txt | 6 ++++++ cf-lib/info.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cf-lib/changelog.txt b/cf-lib/changelog.txt index b9642fa..2fc199a 100644 --- a/cf-lib/changelog.txt +++ b/cf-lib/changelog.txt @@ -1,4 +1,10 @@ --------------------------------------------------------------------------------------------------- +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: diff --git a/cf-lib/info.json b/cf-lib/info.json index 652f221..eeda71c 100644 --- a/cf-lib/info.json +++ b/cf-lib/info.json @@ -1,6 +1,6 @@ { "name": "cf-lib", - "version": "0.0.9", + "version": "0.0.10", "title": "cackling fiends library", "description": "Because I'd like to have my own library :-)", "author": "cackling fiend", From c36fd418abadc389ce2808a8b5ecdedd127783e8 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Fri, 28 Mar 2025 18:54:28 +0100 Subject: [PATCH 05/12] Add Technology:setIngredients --- cf-lib/data/Technology.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cf-lib/data/Technology.lua b/cf-lib/data/Technology.lua index f30d739..bf950c8 100644 --- a/cf-lib/data/Technology.lua +++ b/cf-lib/data/Technology.lua @@ -140,6 +140,15 @@ function Technology:removeIngredient(ingredientName) 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) From bbcc58612897530178664eed9e87661f44995709 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Fri, 28 Mar 2025 18:55:11 +0100 Subject: [PATCH 06/12] 0.0.11 --- cf-lib/changelog.txt | 5 +++++ cf-lib/info.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cf-lib/changelog.txt b/cf-lib/changelog.txt index 2fc199a..ac1dd2f 100644 --- a/cf-lib/changelog.txt +++ b/cf-lib/changelog.txt @@ -1,4 +1,9 @@ --------------------------------------------------------------------------------------------------- +Version: 0.0.11 +Date: 28.03.2025 + Changes: + - Add Technology:setIngredients +--------------------------------------------------------------------------------------------------- Version: 0.0.10 Date: 16.03.2025 Changes: diff --git a/cf-lib/info.json b/cf-lib/info.json index eeda71c..b6047b0 100644 --- a/cf-lib/info.json +++ b/cf-lib/info.json @@ -1,6 +1,6 @@ { "name": "cf-lib", - "version": "0.0.10", + "version": "0.0.11", "title": "cackling fiends library", "description": "Because I'd like to have my own library :-)", "author": "cackling fiend", From 36dba7614eef7b2d845931db58eabeaef4b950b9 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Sun, 6 Apr 2025 16:43:21 +0200 Subject: [PATCH 07/12] Add table.count --- cf-lib/util/table.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cf-lib/util/table.lua b/cf-lib/util/table.lua index e44b559..865d683 100644 --- a/cf-lib/util/table.lua +++ b/cf-lib/util/table.lua @@ -92,4 +92,15 @@ 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 \ No newline at end of file From 3ce2d83c228fa9d1759d427aaed5af2e77dcc5c9 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Sun, 6 Apr 2025 16:43:54 +0200 Subject: [PATCH 08/12] 0.0.12 --- cf-lib/changelog.txt | 5 +++++ cf-lib/info.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cf-lib/changelog.txt b/cf-lib/changelog.txt index ac1dd2f..bb6d348 100644 --- a/cf-lib/changelog.txt +++ b/cf-lib/changelog.txt @@ -1,4 +1,9 @@ --------------------------------------------------------------------------------------------------- +Version: 0.0.12 +Date: 06.04.2025 + Changes: + Add table.count +--------------------------------------------------------------------------------------------------- Version: 0.0.11 Date: 28.03.2025 Changes: diff --git a/cf-lib/info.json b/cf-lib/info.json index b6047b0..7691c7a 100644 --- a/cf-lib/info.json +++ b/cf-lib/info.json @@ -1,6 +1,6 @@ { "name": "cf-lib", - "version": "0.0.11", + "version": "0.0.12", "title": "cackling fiends library", "description": "Because I'd like to have my own library :-)", "author": "cackling fiend", From 8e637400a57e723b43f65b02092bfbe46fedcbfd Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Fri, 23 May 2025 21:03:59 +0200 Subject: [PATCH 09/12] Add class Item --- cf-lib/data/Item.lua | 52 ++++++++++++++++++++++++++++++++++++++++++ cf-lib/data/Recipe.lua | 20 ++++++++-------- 2 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 cf-lib/data/Item.lua diff --git a/cf-lib/data/Item.lua b/cf-lib/data/Item.lua new file mode 100644 index 0000000..e7cc877 --- /dev/null +++ b/cf-lib/data/Item.lua @@ -0,0 +1,52 @@ +--- Utility class for a single item +--- @class Item +local Item = {} + +--- Pass an item name or an item table to get a Item object +--- @param value string|table The name of the item or the item table +function Item:new(value) + local name + local item + + if type(value) == "string" then + name = value + item = data.raw.item[name] + if not item then + log("Item not found: " .. name) + return nil + end + elseif type(value) == "table" then + name = value.name + item = value + end + + local obj = { + prototype = item + } + + setmetatable(obj, self) + self.__index = self + return obj +end + +--- Applies the item to the game +function Item:apply() + data:extend({ self.prototype }) +end + +--- Assigns data to the item +--- Shorthand for table.assign(item.prototype, data) +--- @param data table The data to assign +function Item:assign(data) + table.assign(self.prototype, data) + return self +end + +--- Sets the weight of the item calculated from given count per rocket. +--- @param count number The amount of items that fit into a rocket +function Item:itemsPerRocket(count) + self.prototype.weight = (1000 / count) * kg + return self +end + +return Item \ No newline at end of file diff --git a/cf-lib/data/Recipe.lua b/cf-lib/data/Recipe.lua index 1a5c67e..6ca5192 100644 --- a/cf-lib/data/Recipe.lua +++ b/cf-lib/data/Recipe.lua @@ -1,9 +1,9 @@ ----- Utility class for a single recipe ----- @class Recipe +--- 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 +--- 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 @@ -29,14 +29,14 @@ function Recipe:new(value) return obj end ----- Applies the recipe to the game +--- 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 +--- 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 @@ -148,8 +148,8 @@ function Recipe:unlockedByTechnology(technology) return self end ----- Clones the recipe ----- @param name string The name of the new recipe +--- 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 From a9457f7c386156b3221fb661010c47db5fed2550 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Fri, 23 May 2025 21:04:51 +0200 Subject: [PATCH 10/12] 0.0.13 --- cf-lib/changelog.txt | 5 +++++ cf-lib/info.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cf-lib/changelog.txt b/cf-lib/changelog.txt index bb6d348..9955581 100644 --- a/cf-lib/changelog.txt +++ b/cf-lib/changelog.txt @@ -1,4 +1,9 @@ --------------------------------------------------------------------------------------------------- +Version: 0.0.13 +Date: 23.05.2025 + Changes: + Add Item class +--------------------------------------------------------------------------------------------------- Version: 0.0.12 Date: 06.04.2025 Changes: diff --git a/cf-lib/info.json b/cf-lib/info.json index 7691c7a..d87420a 100644 --- a/cf-lib/info.json +++ b/cf-lib/info.json @@ -1,6 +1,6 @@ { "name": "cf-lib", - "version": "0.0.12", + "version": "0.0.13", "title": "cackling fiends library", "description": "Because I'd like to have my own library :-)", "author": "cackling fiend", From 9878331df58188b5a46b08dc58cec6ac22c9f743 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Sat, 31 May 2025 22:55:18 +0200 Subject: [PATCH 11/12] Add :merge --- cf-lib/data/Item.lua | 8 ++++++++ cf-lib/data/Recipe.lua | 10 +++++++++- cf-lib/data/Technology.lua | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cf-lib/data/Item.lua b/cf-lib/data/Item.lua index e7cc877..1cfffcf 100644 --- a/cf-lib/data/Item.lua +++ b/cf-lib/data/Item.lua @@ -42,6 +42,14 @@ function Item:assign(data) return self end +--- Deeply merges data with the item +--- Shorthand for table.merge(item.prototype, data) +--- @param data table The data to merge +function Item:merge(data) + table.merge(self.prototype, data) + return self +end + --- Sets the weight of the item calculated from given count per rocket. --- @param count number The amount of items that fit into a rocket function Item:itemsPerRocket(count) diff --git a/cf-lib/data/Recipe.lua b/cf-lib/data/Recipe.lua index 6ca5192..6eca2b6 100644 --- a/cf-lib/data/Recipe.lua +++ b/cf-lib/data/Recipe.lua @@ -42,6 +42,14 @@ function Recipe:assign(data) return self end +--- Deeply merges data with the recipe +--- Shorthand for table.merge(recipe.prototype, data) +--- @param data table The data to merge +function Recipe:merge(data) + table.merge(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) @@ -62,7 +70,7 @@ function Recipe:addIngredient(ingredientName, amount) ingredient.amount_min = nil ingredient.amount_max = nil ingredient.probability = nil - return + return self end end table.insert(self.prototype.ingredients, { name = ingredientName, amount = amount, type = ingredientType }) diff --git a/cf-lib/data/Technology.lua b/cf-lib/data/Technology.lua index bf950c8..13a1dd9 100644 --- a/cf-lib/data/Technology.lua +++ b/cf-lib/data/Technology.lua @@ -50,6 +50,14 @@ function Technology:assign(data) return self end +--- Deeply merges data with the technology +--- Shorthand for table.merge(technology.prototype, data) +--- @param data table The data to merge +function Technology:merge(data) + table.merge(self.prototype, data) + return self +end + --- Sets the prerequisite for the technology --- @param prerequisites table The names of the prerequisites function Technology:setPrerequisites(prerequisites) From 8ded39211b544387704167a614b8fec8fef5fe95 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Tue, 10 Jun 2025 19:47:34 +0200 Subject: [PATCH 12/12] 0.0.14 --- cf-lib/changelog.txt | 5 +++++ cf-lib/info.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cf-lib/changelog.txt b/cf-lib/changelog.txt index 9955581..ae8b83d 100644 --- a/cf-lib/changelog.txt +++ b/cf-lib/changelog.txt @@ -1,4 +1,9 @@ --------------------------------------------------------------------------------------------------- +Version: 0.0.14 +Date: 10.06.2025 + Changes: + Add :merge +--------------------------------------------------------------------------------------------------- Version: 0.0.13 Date: 23.05.2025 Changes: diff --git a/cf-lib/info.json b/cf-lib/info.json index d87420a..c22dbfa 100644 --- a/cf-lib/info.json +++ b/cf-lib/info.json @@ -1,6 +1,6 @@ { "name": "cf-lib", - "version": "0.0.13", + "version": "0.0.14", "title": "cackling fiends library", "description": "Because I'd like to have my own library :-)", "author": "cackling fiend",