diff --git a/cf-lib/changelog.txt b/cf-lib/changelog.txt index ae8b83d..4b31875 100644 --- a/cf-lib/changelog.txt +++ b/cf-lib/changelog.txt @@ -1,35 +1,4 @@ --------------------------------------------------------------------------------------------------- -Version: 0.0.14 -Date: 10.06.2025 - Changes: - Add :merge ---------------------------------------------------------------------------------------------------- -Version: 0.0.13 -Date: 23.05.2025 - Changes: - Add Item 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: diff --git a/cf-lib/data/Item.lua b/cf-lib/data/Item.lua deleted file mode 100644 index 1cfffcf..0000000 --- a/cf-lib/data/Item.lua +++ /dev/null @@ -1,60 +0,0 @@ ---- 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 - ---- 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) - 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 6eca2b6..52e453b 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,32 +29,22 @@ 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 -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) +--- @param amount number The amount of the ingredient 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" @@ -70,11 +60,10 @@ function Recipe:addIngredient(ingredientName, amount) ingredient.amount_min = nil ingredient.amount_max = nil ingredient.probability = nil - return self + 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 @@ -91,11 +80,10 @@ function Recipe:replaceIngredient(old, new, amount) if result.name == old then result.name = new result.amount = amount or result.amount - return self + return end end end - return self end --- Removes an existing ingredient by name @@ -105,11 +93,10 @@ function Recipe:removeIngredient(ingredientName) for i, result in pairs(self.prototype.ingredients) do if result.name == ingredientName then self.prototype.ingredients[i] = nil - return self + return end end end - return self end --- Replaces an existing result by name with a new result @@ -134,7 +121,6 @@ function Recipe:replaceResult(old, new, amount, expensiveAmount) table.main_product = new end end - return self end --- Adds the recipe to a technology @@ -149,15 +135,14 @@ 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 self + return 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 +---- 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 diff --git a/cf-lib/data/Technology.lua b/cf-lib/data/Technology.lua index 13a1dd9..2c641d5 100644 --- a/cf-lib/data/Technology.lua +++ b/cf-lib/data/Technology.lua @@ -47,22 +47,12 @@ end --- @param data table The data to assign function Technology:assign(data) table.assign(self.prototype, 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) self.prototype.prerequisites = prerequisites - return self end --- Adds a prerequisite to the technology @@ -73,7 +63,6 @@ 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 @@ -86,7 +75,6 @@ function Technology:addPrerequisites(prerequisites) table.insert(self.prototype.prerequisites, _prerequisite) end end - return self end --- Replaces a prerequisite in a technology @@ -100,7 +88,6 @@ function Technology:replacePrerequisite(old, new) self.prototype.prerequisites[i] = _new end end - return self end --- Removes a prerequisite from the technology @@ -110,10 +97,9 @@ function Technology:removePrerequisite(prerequisite) for i, techPrerequisite in pairs(self.prototype.prerequisites) do if techPrerequisite == _prerequisite then table.remove(self.prototype.prerequisites, i) - return self + return end end - return self end --- Adds an ingredient to the technology @@ -123,7 +109,6 @@ 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 @@ -132,7 +117,6 @@ function Technology:addIngredients(ingredientNames) for _, ingredientName in pairs(ingredientNames) do self:addIngredient(ingredientName) end - return self end --- Removes an existing ingredient by name @@ -145,16 +129,6 @@ 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 @@ -162,18 +136,16 @@ end function Technology:addRecipe(recipeName) for _, effect in pairs(self.prototype.effects) do if effect.type == "unlock-recipe" and effect.recipe == recipeName then - return self + return 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 @@ -182,10 +154,9 @@ 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 self + return end end - return self end --- Clones the technology diff --git a/cf-lib/info.json b/cf-lib/info.json index c22dbfa..0eb9de6 100644 --- a/cf-lib/info.json +++ b/cf-lib/info.json @@ -1,6 +1,6 @@ { "name": "cf-lib", - "version": "0.0.14", + "version": "0.0.8", "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 865d683..0e8f32b 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.concatTables(table1, table2) +function table.concat(table1, table2) local result = {} for _, v in pairs(table1) do table.insert(result, v) @@ -92,15 +92,4 @@ 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