From c0de09d5976cec74b6e93be01ab336951dd60e36 Mon Sep 17 00:00:00 2001 From: Doloment Date: Sun, 22 Jun 2025 19:20:06 +0700 Subject: [PATCH] Update from 22.06.2025: add MapGen mod outline, fix some require() issues --- .emmyrc.json | 115 +++++++-- README.md | 1 + mods/API/Ensure/init.lua | 51 ++-- mods/API/Math/init.lua | 9 + mods/API/Math/mod.conf | 1 + mods/API/Mod/init.lua | 26 +- mods/API/Mod/mod.conf | 3 +- mods/API/ModTable/init.lua | 40 ---- mods/API/ModTable/mod.conf | 1 - mods/API/Path/init.lua | 6 - mods/API/Path/mod.conf | 1 - mods/API/Require/init.lua | 20 +- mods/API/String/init.lua | 2 +- mods/API/String/mod.conf | 1 - mods/API/System/init.lua | 75 ++++++ mods/API/System/mod.conf | 3 +- mods/API/Table/init.lua | 2 +- mods/CORE/DynamicRecipe/mod.conf | 1 - .../{DynamicRecipe => EntityModular}/init.lua | 0 mods/CORE/EntityModular/mod.conf | 1 + mods/CORE/Item/init.lua | 5 + mods/CORE/Item/mod.conf | 1 + mods/CORE/Item/src/Item.lua | 41 ++++ mods/CORE/ModularEntity/mod.conf | 1 - mods/CORE/ModularStructure/mod.conf | 1 - mods/CORE/Node/mod.conf | 1 + mods/CORE/NodeFactory/mod.conf | 1 + .../{ModularEntity => RecipeDynamic}/init.lua | 0 mods/CORE/RecipeDynamic/mod.conf | 1 + .../init.lua | 0 mods/CORE/RecipeStatic/mod.conf | 1 + mods/CORE/StaticRecipe/mod.conf | 1 - .../init.lua | 0 mods/CORE/StructureModular/mod.conf | 1 + mods/ENVIROMENT/MapGen/init.lua | 0 mods/ENVIROMENT/modpack.conf | 1 - mods/ENVIRONMENT/MapGen/init.lua | 5 + .../MapGen/mod.conf | 1 + mods/ENVIRONMENT/MapGen/src/Layer.lua | 22 ++ mods/ENVIRONMENT/MapGen/src/MapGen.lua | 223 ++++++++++++++++++ mods/ENVIRONMENT/MapGen/src/MapGenUtils.lua | 20 ++ mods/ENVIRONMENT/MapGen/src/Region.lua | 15 ++ mods/ENVIRONMENT/MapGen/src/Region2D.lua | 19 ++ mods/ENVIRONMENT/MapGen/src/Region3D.lua | 19 ++ mods/ENVIRONMENT/modpack.conf | 1 + 45 files changed, 621 insertions(+), 119 deletions(-) create mode 100644 README.md create mode 100644 mods/API/Math/init.lua create mode 100644 mods/API/Math/mod.conf delete mode 100644 mods/API/ModTable/init.lua delete mode 100644 mods/API/ModTable/mod.conf delete mode 100644 mods/API/Path/init.lua delete mode 100644 mods/API/Path/mod.conf delete mode 100644 mods/CORE/DynamicRecipe/mod.conf rename mods/CORE/{DynamicRecipe => EntityModular}/init.lua (100%) create mode 100644 mods/CORE/EntityModular/mod.conf create mode 100644 mods/CORE/Item/src/Item.lua delete mode 100644 mods/CORE/ModularEntity/mod.conf delete mode 100644 mods/CORE/ModularStructure/mod.conf rename mods/CORE/{ModularEntity => RecipeDynamic}/init.lua (100%) create mode 100644 mods/CORE/RecipeDynamic/mod.conf rename mods/CORE/{ModularStructure => RecipeStatic}/init.lua (100%) create mode 100644 mods/CORE/RecipeStatic/mod.conf delete mode 100644 mods/CORE/StaticRecipe/mod.conf rename mods/CORE/{StaticRecipe => StructureModular}/init.lua (100%) create mode 100644 mods/CORE/StructureModular/mod.conf delete mode 100644 mods/ENVIROMENT/MapGen/init.lua delete mode 100644 mods/ENVIROMENT/modpack.conf create mode 100644 mods/ENVIRONMENT/MapGen/init.lua rename mods/{ENVIROMENT => ENVIRONMENT}/MapGen/mod.conf (50%) create mode 100644 mods/ENVIRONMENT/MapGen/src/Layer.lua create mode 100644 mods/ENVIRONMENT/MapGen/src/MapGen.lua create mode 100644 mods/ENVIRONMENT/MapGen/src/MapGenUtils.lua create mode 100644 mods/ENVIRONMENT/MapGen/src/Region.lua create mode 100644 mods/ENVIRONMENT/MapGen/src/Region2D.lua create mode 100644 mods/ENVIRONMENT/MapGen/src/Region3D.lua create mode 100644 mods/ENVIRONMENT/modpack.conf diff --git a/.emmyrc.json b/.emmyrc.json index 91e104f..eb2673e 100644 --- a/.emmyrc.json +++ b/.emmyrc.json @@ -1,27 +1,92 @@ { - "completion": { - "enable": true, - "autoRequire": false, - "callSnippet": false, - "postfix": "@" - }, - "diagnostics": { - "globals": [ - "core", - "table", - "Ensure", - "string", - "string_utf8" - ] - }, - "workspace": { - "ignoreSubmodules": true - }, - "runtime": { - "version": "Lua5.1", - "requireLikeFunction": [], - "frameworkVersions": [], - "extensions": [], - "requirePattern": [] - } + "completion": { + "enable": true, + "autoRequire": false, + "autoRequireFunction": "require", + "autoRequireNamingConvention": "keep", + "autoRequireSeparator": ".", + "callSnippet": false, + "postfix": "@" + }, + "diagnostics": { + "disable": [ + "unnecessary-if" + ], + "enable": true, + "globals": [ + "core", + "table", + "VoxelArea", + "Ensure", + "string", + "string_utf8" + ], + "globalsRegex": [], + "severity": {}, + "enables": [], + "diagnosticInterval": null + }, + "signature": { + "detailSignatureHelper": true + }, + "hint": { + "enable": true, + "paramHint": true, + "indexHint": true, + "localHint": true, + "overrideHint": true + }, + "runtime": { + "version": "LuaJIT", + "requireLikeFunction": [], + "frameworkVersions": [], + "extensions": [], + "requirePattern": [], + "classDefaultCall": { + "functionName": "", + "forceNonColon": false, + "forceReturnSelf": false + } + }, + "workspace": { + "ignoreDir": [], + "ignoreGlobs": [], + "library": [], + "workspaceRoots": [], + "preloadFileSize": 0, + "encoding": "utf-8", + "moduleMap": [], + "reindexDuration": 5000, + "enableReindex": false + }, + "resource": { + "paths": [] + }, + "codeLens": { + "enable": true + }, + "strict": { + "requirePath": false, + "typeCall": false, + "arrayIndex": true, + "metaOverrideFileDefine": true, + "docBaseConstMatchBaseType": true + }, + "semanticTokens": { + "enable": true + }, + "references": { + "enable": true, + "fuzzySearch": true, + "shortStringSearch": false + }, + "hover": { + "enable": true + }, + "documentColor": { + "enable": true + }, + "codeAction": { + "insertSpace": false + } } \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7515be9 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Simple Makes Complex is a Luanti engine-based game focused on the multiplayer gameplay \ No newline at end of file diff --git a/mods/API/Ensure/init.lua b/mods/API/Ensure/init.lua index dc84c05..1dd4a0e 100644 --- a/mods/API/Ensure/init.lua +++ b/mods/API/Ensure/init.lua @@ -3,40 +3,51 @@ local tostring = tostring Ensure = {} --- Validates a function argument's type and throws a descriptive error on mismatch. ---- @param arg any The argument value to type-check ---- @param expectedType string Expected Lua type name (e.g., "number", "table", "string") ---- @param n number Argument position in function call (1-based index) ---- @param function_name string Name of the parent function for error reporting -function Ensure.argType(arg, expectedType, n, function_name) +--- @param arg any The argument value to type-check +--- @param expectedType string Expected Lua type name (e.g., "number", "table", "string") +--- @param n number Argument position in function call (1-based index) +--- @param functionName string Name of the parent function for error reporting +function Ensure.argType(arg, expectedType, n, functionName) local actualType = type(arg) assert(actualType == expectedType, string.format( 'bad argument #%s to \'%s\' (%s expected, got %s)', tostring(n), - function_name, + functionName, expectedType, actualType)) end local ensureArgType = Ensure.argType ---- @param arg string The string to check ---- @param n number Argument position in function call (1-based index) ---- @param function_name string Name of the parent function for error reporting -function Ensure.stringArgNotEmpty(arg, n, function_name) +--- @param arg any The argument value to type-check +--- @param allowedOptions table Lookup table where keys represent valid options (with true values) +--- @param n number Argument position in function call (1-based index) +--- @param functionName string Name of the parent function for error reporting +function Ensure.argIsAllowed(arg, allowedOptions , n, functionName) + assert(allowedOptions[arg] == true, string.format( + 'bad argument #%s to \'%s\' (the value is not allowed)', + tostring(n), + functionName)) +end + +--- @param arg string The string to check +--- @param n number Argument position in function call (1-based index) +--- @param functionName string Name of the parent function for error reporting +function Ensure.stringArgNotEmpty(arg, n, functionName) ensureArgType(arg, 'string', 1, 'Ensure.stringArgNotEmpty') - assert(#arg ~= 0, string.format('bad argument #%s to \'%s\' (string cannot be empty)', n, function_name)) + assert(#arg ~= 0, string.format('bad argument #%s to \'%s\' (string cannot be empty)', n, functionName)) end ---- @param arg any The argument value to check ---- @param n number Argument position in function call (1-based index) ---- @param function_name string Name of the parent function for error reporting -function Ensure.argNotNil(arg, n, function_name) - assert(arg ~= nil, string.format('bad argument #%s to \'%s\' (key cannot be nil)', n, function_name)) +--- @param arg any The argument value to check +--- @param n number Argument position in function call (1-based index) +--- @param functionName string Name of the parent function for error reporting +function Ensure.argNotNil(arg, n, functionName) + assert(arg ~= nil, string.format('bad argument #%s to \'%s\' (key cannot be nil)', n, functionName)) end ---- @param divider number The number to check ---- @param function_name string Name of the parent function for error reporting -function Ensure.zeroDivision(divider, function_name) - assert(divider ~= 0, string.format('Division by zero in \'%s\'', function_name)) +--- @param divider number The number to check +--- @param functionName string Name of the parent function for error reporting +function Ensure.zeroDivision(divider, functionName) + assert(divider ~= 0, string.format('Division by zero in \'%s\'', functionName)) end diff --git a/mods/API/Math/init.lua b/mods/API/Math/init.lua new file mode 100644 index 0000000..eb1fd3d --- /dev/null +++ b/mods/API/Math/init.lua @@ -0,0 +1,9 @@ +local math = math + +math.lerp = function (a, b, t) + return a + (b - a) * t +end + +math.clamp = function(value, min, max) + return math.min(math.max(value, min), max) +end diff --git a/mods/API/Math/mod.conf b/mods/API/Math/mod.conf new file mode 100644 index 0000000..f8ab11c --- /dev/null +++ b/mods/API/Math/mod.conf @@ -0,0 +1 @@ +name = api__math diff --git a/mods/API/Mod/init.lua b/mods/API/Mod/init.lua index 3ec75d0..cb69137 100644 --- a/mods/API/Mod/init.lua +++ b/mods/API/Mod/init.lua @@ -1,12 +1,18 @@ -_G.Mod = {} +Mod = {} -function Mod:new(addToGlobal) - for key, value in pairs(addToGlobal) do - _G[key] = value - end - local modName = core.get_current_modname() - local modPath = core.get_modpath(modName) - - local modTable = ModTable:new(modName, modPath) - modTable.require = ModTable:getRequire() +function Mod.getInfo() + local name = core.get_current_modname() + local path = core.get_modpath(name) + local require = Require.getModRequire(name, path) + + return {name = name, path = path, require = require} +end + +function Mod:new() + local mod = Mod.getInfo() + + setmetatable(mod, self) + self.__index = self + + return mod end diff --git a/mods/API/Mod/mod.conf b/mods/API/Mod/mod.conf index ca3a457..be9b7e8 100644 --- a/mods/API/Mod/mod.conf +++ b/mods/API/Mod/mod.conf @@ -1,2 +1 @@ -name = api__mod -depends = api__mod_table, api__require, +name = api__mod_table diff --git a/mods/API/ModTable/init.lua b/mods/API/ModTable/init.lua deleted file mode 100644 index 662654d..0000000 --- a/mods/API/ModTable/init.lua +++ /dev/null @@ -1,40 +0,0 @@ -ModTable = {} - -local modLoggers = {} -local loadedModules = {} -local DS = '/' - -function ModTable:getRequire() - return function(fileName) - local modName = self.name - local modPath = self.path - local fullName = modName .. '..' .. fileName - - local module = loadedModules[fullName] - if module ~= nil then - return module - end - - local result = dofile(modPath .. DS .. 'src' .. DS .. fileName:gsub('%.', DS) .. '.lua') - - if result ~= nil then - loadedModules[fullName] = result - else - loadedModules[fullName] = true - end - - return loadedModules[fullName] - end -end - -function ModTable:new(name, path) - local modTable = { - name = name, - path = path, - } - - setmetatable(modTable, self) - self.__index = self - - return modTable -end diff --git a/mods/API/ModTable/mod.conf b/mods/API/ModTable/mod.conf deleted file mode 100644 index be9b7e8..0000000 --- a/mods/API/ModTable/mod.conf +++ /dev/null @@ -1 +0,0 @@ -name = api__mod_table diff --git a/mods/API/Path/init.lua b/mods/API/Path/init.lua deleted file mode 100644 index 513755c..0000000 --- a/mods/API/Path/init.lua +++ /dev/null @@ -1,6 +0,0 @@ -Path = {} - - -function Path:new() - return -end diff --git a/mods/API/Path/mod.conf b/mods/API/Path/mod.conf deleted file mode 100644 index 5ed9af9..0000000 --- a/mods/API/Path/mod.conf +++ /dev/null @@ -1 +0,0 @@ -name = api__path diff --git a/mods/API/Require/init.lua b/mods/API/Require/init.lua index 81cb60d..c14549b 100644 --- a/mods/API/Require/init.lua +++ b/mods/API/Require/init.lua @@ -1,11 +1,19 @@ -_G.Require = {} +Require = {} local loadedModules = {} local DS = '/' -function Require.getModRequire() - return function(fileName) - local modName = self.name - local modPath = self.path +--- Creates a scoped require function for a specific module +--- This custom require handles module loading relative to a specific module path, +--- prevents duplicate loading, and caches results appropriately. +--- @param modName string Namespace identifier for the module (prevents global collisions) +--- @param modPath string Absolute base path to the module's directory +--- @return function require Custom require function scoped to the specified module +function Require.getModRequire(modName, modPath) + --- Custom require function for module-specific loading + --- Resolves files relative to modPath/src, converts dot-notation to path separators + --- @param fileName string Module path in dot-notation (e.g. "lib.utils") + --- @return any loadedModule The loaded module's return value or cached result + local function require(fileName) local fullName = modName .. '..' .. fileName local module = loadedModules[fullName] @@ -23,4 +31,6 @@ function Require.getModRequire() return loadedModules[fullName] end + + return require end diff --git a/mods/API/String/init.lua b/mods/API/String/init.lua index 194d849..a5cf6fa 100644 --- a/mods/API/String/init.lua +++ b/mods/API/String/init.lua @@ -3,7 +3,7 @@ local sub, upper, ensureArgType --- Writes a string with a capital letter. --- @param str string Input string. ---- @return string +--- @return string? function string.capitalize(str) ensureArgType(str, "string", 1, "string.capitalize") diff --git a/mods/API/String/mod.conf b/mods/API/String/mod.conf index 81bf59f..072209c 100644 --- a/mods/API/String/mod.conf +++ b/mods/API/String/mod.conf @@ -1,2 +1 @@ name = api__string -depends = api__table diff --git a/mods/API/System/init.lua b/mods/API/System/init.lua index e69de29..d170e85 100644 --- a/mods/API/System/init.lua +++ b/mods/API/System/init.lua @@ -0,0 +1,75 @@ +Api = {} +Core = {} +Environment = {} +Game = {} +Game.Items = {} +Game.Mobs = {} +Game.Structures = {} +Ui = {} + +local allowedDomains = { + Api = true, + Core = true, + Environment = true, + Game = true, + Ui = true +} + +--- Adds a mod to the global space under specified domain structure +---@param mod table The mod table containing functionality to expose -- TODO: !!!Make a Mod class!!! +---@param location string Dot-separated path within domain structure (e.g. "Items.Log") +---@param force boolean? Whether to overwrite the target location. +Api.addModToGlobalSpace = function(mod, location, force) + -- Ensure `mod` type and that it's not empty + Ensure.argType(mod, 'table', 1, 'Api.addModToGlobalSpace') + + -- Ensure `location` type and that it's not empty + Ensure.argType(location, 'string', 2, 'Api.addModToGlobalSpace') + Ensure.stringArgNotEmpty(location, 2, 'Api.addModToGlobalSpace') + + -- Ensure `location` matches this format: 'field.subfield.subsubfield.<...>' + if not location:match('^[%a_][%w_]*([%.][%a_][%w_]*)*$') then + error('bad argument #2 to \'Api.addModToGlobalSpace\' (invalid format)') + end + + -- Split `location` into fields -- TODO: !!!Make a String function!!! + local fields = {} + local domain = nil + for field in string.gmatch(location, '[^.]+') do + if domain ~= nil then + table.insert(fields, field) + else + domain = field + end + end + + -- Ensure the domain is allowed + Ensure.argIsAllowed(domain, allowedDomains , 2, 'Api.addModToGlobalSpace') + + -- Handle case where we're modifying the domain root directly + if #fields == 0 then + error('Not allowed to directly write into the \''.. domain ..'\' domain') + end + + -- Start traversing from the domain's table + local current = _G[domain] + assert(type(current) == 'table', 'The global domain is not a table') + + -- Traverse each field of the location + local num_fields = #fields + for i = 1, num_fields - 1 do + local field = fields[i] + if current[field] == nil then + current[field] = {} + end + current = current[field] + assert(type(current) == 'table', 'intermediate field is not a table') + end + + -- Assign mod to the final field + local last_field = fields[num_fields] + if force and current[last_field] ~= nil then + error(string.format('field \'%s\' already exists in \'%s\'', last_field, location)) + end + current[last_field] = mod +end diff --git a/mods/API/System/mod.conf b/mods/API/System/mod.conf index 49293b2..9612b38 100644 --- a/mods/API/System/mod.conf +++ b/mods/API/System/mod.conf @@ -1,2 +1,3 @@ name = api__system -depends = api__mod, api__string, +description = Include this mod in your `depends` to ensure all necessary APIs are loaded for your mod +depends = api__ensure, api__math, api__mod, api__require, api__table, api__string, api__utf8, diff --git a/mods/API/Table/init.lua b/mods/API/Table/init.lua index fa086ba..9a93e52 100644 --- a/mods/API/Table/init.lua +++ b/mods/API/Table/init.lua @@ -22,7 +22,7 @@ end --- Returns all keys from the table `t` as an array. --- @param t table Table to check ---- @return any[] +--- @return string[] | number[] function table.get_keys(t) ensureArgType(t, 'table', 1, 'table.get_keys') diff --git a/mods/CORE/DynamicRecipe/mod.conf b/mods/CORE/DynamicRecipe/mod.conf deleted file mode 100644 index 71a9cc3..0000000 --- a/mods/CORE/DynamicRecipe/mod.conf +++ /dev/null @@ -1 +0,0 @@ -name = core__dynamic_recipe diff --git a/mods/CORE/DynamicRecipe/init.lua b/mods/CORE/EntityModular/init.lua similarity index 100% rename from mods/CORE/DynamicRecipe/init.lua rename to mods/CORE/EntityModular/init.lua diff --git a/mods/CORE/EntityModular/mod.conf b/mods/CORE/EntityModular/mod.conf new file mode 100644 index 0000000..596f094 --- /dev/null +++ b/mods/CORE/EntityModular/mod.conf @@ -0,0 +1 @@ +name = core__entity_modular diff --git a/mods/CORE/Item/init.lua b/mods/CORE/Item/init.lua index e69de29..c9afe9f 100644 --- a/mods/CORE/Item/init.lua +++ b/mods/CORE/Item/init.lua @@ -0,0 +1,5 @@ +local Item = Mod:new() + +Item.Class = Item.require('Item') + +Api.addModToGlobalSpace(Item, 'Core.Item') diff --git a/mods/CORE/Item/mod.conf b/mods/CORE/Item/mod.conf index 92829c2..94c527d 100644 --- a/mods/CORE/Item/mod.conf +++ b/mods/CORE/Item/mod.conf @@ -1 +1,2 @@ name = core__item +depends = api__system diff --git a/mods/CORE/Item/src/Item.lua b/mods/CORE/Item/src/Item.lua new file mode 100644 index 0000000..352338e --- /dev/null +++ b/mods/CORE/Item/src/Item.lua @@ -0,0 +1,41 @@ +local Item = {} + +--- @class ItemDefinition +--- @field settings table +local ItemDefinition = { + settings = { + name = '', + title = '', + description = '', + visual = '', -- '3d' | 'flat' + }, + callbacks = { + ... + }, +} + +--- @param itemDef ItemDefinition +local function itemDefToLuantiDef(itemDef) + local s = itemDef.settings + local c = itemDef.callbacks + local luantiDef = table.copy(c) + + return luantiDef +end + +local function register3dItem(luantiDef) + local ld = luantiDef + core.register_node(ld.name, { + description = ld.description + }) +end + +local function registerFlatItem(luantiDef) + +end + +function Item:new(itemDef) + local item = {} + + +end diff --git a/mods/CORE/ModularEntity/mod.conf b/mods/CORE/ModularEntity/mod.conf deleted file mode 100644 index 43d5d71..0000000 --- a/mods/CORE/ModularEntity/mod.conf +++ /dev/null @@ -1 +0,0 @@ -name = core__modular_entity diff --git a/mods/CORE/ModularStructure/mod.conf b/mods/CORE/ModularStructure/mod.conf deleted file mode 100644 index 90d5ebb..0000000 --- a/mods/CORE/ModularStructure/mod.conf +++ /dev/null @@ -1 +0,0 @@ -name = core__modular_structure diff --git a/mods/CORE/Node/mod.conf b/mods/CORE/Node/mod.conf index 094848b..76b1bd4 100644 --- a/mods/CORE/Node/mod.conf +++ b/mods/CORE/Node/mod.conf @@ -1 +1,2 @@ name = core__node +depends = core__item diff --git a/mods/CORE/NodeFactory/mod.conf b/mods/CORE/NodeFactory/mod.conf index 8b69171..c03e061 100644 --- a/mods/CORE/NodeFactory/mod.conf +++ b/mods/CORE/NodeFactory/mod.conf @@ -1 +1,2 @@ name = core__node_factory +depends = core__item_factory diff --git a/mods/CORE/ModularEntity/init.lua b/mods/CORE/RecipeDynamic/init.lua similarity index 100% rename from mods/CORE/ModularEntity/init.lua rename to mods/CORE/RecipeDynamic/init.lua diff --git a/mods/CORE/RecipeDynamic/mod.conf b/mods/CORE/RecipeDynamic/mod.conf new file mode 100644 index 0000000..0eb05ca --- /dev/null +++ b/mods/CORE/RecipeDynamic/mod.conf @@ -0,0 +1 @@ +name = core__recipe_dynamic diff --git a/mods/CORE/ModularStructure/init.lua b/mods/CORE/RecipeStatic/init.lua similarity index 100% rename from mods/CORE/ModularStructure/init.lua rename to mods/CORE/RecipeStatic/init.lua diff --git a/mods/CORE/RecipeStatic/mod.conf b/mods/CORE/RecipeStatic/mod.conf new file mode 100644 index 0000000..2eb4490 --- /dev/null +++ b/mods/CORE/RecipeStatic/mod.conf @@ -0,0 +1 @@ +name = core__recipe_static diff --git a/mods/CORE/StaticRecipe/mod.conf b/mods/CORE/StaticRecipe/mod.conf deleted file mode 100644 index df21aee..0000000 --- a/mods/CORE/StaticRecipe/mod.conf +++ /dev/null @@ -1 +0,0 @@ -name = core__static_recipe diff --git a/mods/CORE/StaticRecipe/init.lua b/mods/CORE/StructureModular/init.lua similarity index 100% rename from mods/CORE/StaticRecipe/init.lua rename to mods/CORE/StructureModular/init.lua diff --git a/mods/CORE/StructureModular/mod.conf b/mods/CORE/StructureModular/mod.conf new file mode 100644 index 0000000..318ca54 --- /dev/null +++ b/mods/CORE/StructureModular/mod.conf @@ -0,0 +1 @@ +name = core__structure_modular diff --git a/mods/ENVIROMENT/MapGen/init.lua b/mods/ENVIROMENT/MapGen/init.lua deleted file mode 100644 index e69de29..0000000 diff --git a/mods/ENVIROMENT/modpack.conf b/mods/ENVIROMENT/modpack.conf deleted file mode 100644 index 38454a8..0000000 --- a/mods/ENVIROMENT/modpack.conf +++ /dev/null @@ -1 +0,0 @@ -name = smc__enviroment diff --git a/mods/ENVIRONMENT/MapGen/init.lua b/mods/ENVIRONMENT/MapGen/init.lua new file mode 100644 index 0000000..847ec48 --- /dev/null +++ b/mods/ENVIRONMENT/MapGen/init.lua @@ -0,0 +1,5 @@ +local MapGen = Mod:new() + +MapGen.Class = MapGen.require('MapGen') + +Api.addModToGlobalSpace(MapGen, 'Core.MapGen') diff --git a/mods/ENVIROMENT/MapGen/mod.conf b/mods/ENVIRONMENT/MapGen/mod.conf similarity index 50% rename from mods/ENVIROMENT/MapGen/mod.conf rename to mods/ENVIRONMENT/MapGen/mod.conf index d14535a..24e1762 100644 --- a/mods/ENVIROMENT/MapGen/mod.conf +++ b/mods/ENVIRONMENT/MapGen/mod.conf @@ -1 +1,2 @@ name = core__map_gen +depends = api__system \ No newline at end of file diff --git a/mods/ENVIRONMENT/MapGen/src/Layer.lua b/mods/ENVIRONMENT/MapGen/src/Layer.lua new file mode 100644 index 0000000..c472e34 --- /dev/null +++ b/mods/ENVIRONMENT/MapGen/src/Layer.lua @@ -0,0 +1,22 @@ +local table, setmetatable = table, setmetatable + +local Layer = {} +Layer.__index = Layer + +function Layer:new(name, minY, maxY) + return setmetatable({ + name = name, + minY = minY, + maxY = maxY, + regions2D = {}, + regions3D = {} + }, self) +end + +function Layer:add2DRegion(region) + table.insert(self.regions2D, region) +end + +function Layer:add3DRegion(region) + table.insert(self.regions3D, region) +end diff --git a/mods/ENVIRONMENT/MapGen/src/MapGen.lua b/mods/ENVIRONMENT/MapGen/src/MapGen.lua new file mode 100644 index 0000000..3a9e0fc --- /dev/null +++ b/mods/ENVIRONMENT/MapGen/src/MapGen.lua @@ -0,0 +1,223 @@ +local modInfo = Mod.getInfo() +local require = modInfo.require +local MapGenUtils = require("MapGenUtils") +local Layer = require("Layer") +local Region2D = require("Region2D") +local Region3D = require("Region3D") + +local core, table, math = core, table, math +local pairs, ipairs = pairs, ipairs + + +-- Main MapGenerator class +local MapGenerator = {} +MapGenerator.__index = MapGenerator + +function MapGenerator:new() + return setmetatable({ + layersByName = {}, + layersList = {}, + registeredRegions = {}, + materials = { + impassableWater = core.get_content_id("impassable_water:water"), + impassableSeabed = core.get_content_id("impassable_water:seabed"), + air = core.get_content_id("air") + } + }, self) +end + +function MapGenerator:registerLayer(name, minY, maxY) + local layer = Layer:new(name, minY, maxY) + self.layersByName[name] = layer + table.insert(self.layersList, layer) + return layer +end + +function MapGenerator:register2DRegion(params) + local region = Region2D:new(params) + local layer = self.layersByName[params.layerName] + if not layer then error("Invalid layer: " .. params.layerName) end + layer:add2DRegion(region) +end + +function MapGenerator:register3DRegion(params) + local region = Region3D:new(params) + local layer = self.layersByName[params.layerName] + if not layer then error("Invalid layer: " .. params.layerName) end + layer:add3DRegion(region) +end + +function MapGenerator:enable() + core.register_on_generated(function(...) + self:onMapGenerated(...) + end) +end + +-- Main generation logic +function MapGenerator:onMapGenerated(minp, maxp, seed) + local vm, emin, emax = core.get_mapgen_object("voxelmanip") + local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax} + local data = vm:get_data() + + -- Sort layers by maxY descending for processing priority + table.sort(self.layersList, function(a, b) + return a.maxY > b.maxY + end) + + local terrainCache = self:process2DRegions(minp, maxp) + self:process3DRegions(minp, maxp, data, area, terrainCache) + + vm:set_data(data) + vm:calc_lighting() + vm:write_to_map() +end + +function MapGenerator:process2DRegions(minp, maxp) + local cache = {} + local width = maxp.x - minp.x + 1 + local zSize = maxp.z - minp.z + 1 + + -- Precompute noise for all 2D regions + local regionNoiseCache = {} + for _, layer in ipairs(self.layersList) do + for _, region in ipairs(layer.regions2D) do + local noiseCache = { + height = {}, + temp = {}, + humid = {}, + } + for x = minp.x, maxp.x do + noiseCache.temp[x] = {} + noiseCache.humid[x] = {} + noiseCache.height[x] = {} + for z = minp.z, maxp.z do + noiseCache.temp[x][z] = region.noiseTemp:get2d({x=x, y=z}) + noiseCache.humid[x][z] = region.noiseHumid:get2d({x=x, y=z}) + noiseCache.height[x][z] = region.noiseHeight:get2d({x=x, y=z}) + end + end + regionNoiseCache[region] = noiseCache + end + end + + for x = minp.x, maxp.x do + cache[x] = {} + for z = minp.z, maxp.z do + cache[x][z] = {} + + for _, layer in ipairs(self.layersList) do + local bestHeight, bestWeight = nil, 0.0 + + for _, region in ipairs(layer.regions2D) do + local noiseCache = regionNoiseCache[region] + local temp = noiseCache.temp[x][z] + local humid = noiseCache.humid[x][z] + + if MapGenUtils.isInRange(temp, region.tempRange) and MapGenUtils.isInRange(humid, region.humidRange) then + local tempWeight = MapGenUtils.calculateWeight(temp, region.tempRange.min, region.tempRange.max) + local heightWeight = MapGenUtils.calculateWeight(humid, region.humidRange.min, region.humidRange.max) + local weight = tempWeight * heightWeight + + if weight > bestWeight then + local heightNoise = noiseCache.height[x][z] + bestHeight = math.lerp( + region.heightRange.min, + region.heightRange.max, + heightNoise + ) + bestWeight = weight + end + end + end + + if bestHeight then + cache[x][z][layer] = math.floor(math.clamp(bestHeight, layer.minY, layer.maxY)) + end + end + end + end + + return cache +end + +function MapGenerator:process3DRegions(minp, maxp, data, area, terrainCache) + -- Precompute 3D noise for regions + local region3DNoise = {} + for _, layer in ipairs(self.layersList) do + for _, region in ipairs(layer.regions3D) do + region3DNoise[region] = { + temp = {}, + humid = {}, + cave = {} + } + for y = minp.y, maxp.y do + region3DNoise[region].temp[y] = {} + region3DNoise[region].humid[y] = {} + region3DNoise[region].cave[y] = {} + for x = minp.x, maxp.x do + region3DNoise[region].temp[y][x] = {} + region3DNoise[region].humid[y][x] = {} + region3DNoise[region].cave[y][x] = {} + for z = minp.z, maxp.z do + region3DNoise[region].temp[y][x][z] = region.noiseTemp:get3d({x=x, y=y, z=z}) + region3DNoise[region].humid[y][x][z] = region.noiseHumid:get3d({x=x, y=y, z=z}) + region3DNoise[region].cave[y][x][z] = region.noiseCave:get3d({x=x, y=y, z=z}) + end + end + end + end + end + + for x = minp.x, maxp.x do + for z = minp.z, maxp.z do + local columnCache = terrainCache[x][z] + local baseIndex = area:index(x, minp.y, z) + local yStride = area.ystride + + for y = minp.y, maxp.y do + local idx = baseIndex + (y - minp.y) * yStride + local processed = false + + for _, layer in ipairs(self.layersList) do + if y >= layer.minY and y <= layer.maxY then + local surfaceY = columnCache[layer] + + if not surfaceY then + data[idx] = y == layer.minY and + self.materials.impassableSeabed or + self.materials.impassableWater + else + -- Set default based on position relative to surface + local default = (y <= surfaceY) and + self.materials.impassableSeabed or + self.materials.air + data[idx] = default + + -- Apply 3D regions (caves/structures) + for _, region in ipairs(layer.regions3D) do + local temp = region3DNoise[region].temp[y][x][z] + local humid = region3DNoise[region].humid[y][x][z] + local cave = region3DNoise[region].cave[y][x][z] + + if MapGenUtils.isInRange(temp, region.tempRange) and + MapGenUtils.isInRange(humid, region.humidRange) and cave > region.caveThreshold then + data[idx] = self.materials.air + break -- Only first matching region applies + end + end + end + + processed = true + break -- Only process one layer per Y position + end + end + + if not processed then + data[idx] = self.materials.air + end + end + end + end +end + +return MapGenerator diff --git a/mods/ENVIRONMENT/MapGen/src/MapGenUtils.lua b/mods/ENVIRONMENT/MapGen/src/MapGenUtils.lua new file mode 100644 index 0000000..e35d8d2 --- /dev/null +++ b/mods/ENVIRONMENT/MapGen/src/MapGenUtils.lua @@ -0,0 +1,20 @@ +local math = math + +-- Utility class with static methods +local MapGenUtils = { + calculateWeight = function(value, min, max) + local center = (min + max) / 2 + local span = (max - min) / 2 + return 1 - math.abs((value - center) / span) + end, + + ---@return boolean + isInRange = function(value, range) + if value >= range.min and value <= range.max then + return true + end + return false + end +} + +return MapGenUtils diff --git a/mods/ENVIRONMENT/MapGen/src/Region.lua b/mods/ENVIRONMENT/MapGen/src/Region.lua new file mode 100644 index 0000000..670681c --- /dev/null +++ b/mods/ENVIRONMENT/MapGen/src/Region.lua @@ -0,0 +1,15 @@ +local Region = {} +Region.__index = Region + +local pairs, setmetatable = pairs, setmetatable + +function Region:new(params) + local instance = setmetatable({}, self) + for k, v in pairs(params) do + instance[k] = v + end + instance:initNoises() + return instance +end + +return Region diff --git a/mods/ENVIRONMENT/MapGen/src/Region2D.lua b/mods/ENVIRONMENT/MapGen/src/Region2D.lua new file mode 100644 index 0000000..61df074 --- /dev/null +++ b/mods/ENVIRONMENT/MapGen/src/Region2D.lua @@ -0,0 +1,19 @@ +local modInfo = Mod.getInfo() +local Region = modInfo.require()("Region") + +local core, setmetatable = core, setmetatable + +-- 2D Region implementation +local Region2D = setmetatable({}, {__index = Region}) + +function Region2D:new(params) + local instance = Region.new(self, params) + instance.type = "2d" + return setmetatable(instance, {__index = Region2D}) +end + +function Region2D:initNoises() + self.noiseHeight = core.get_value_noise(self.noiseParamsHeight) + self.noiseTemp = core.get_value_noise(self.noiseParamsTemp) + self.noiseHumid = core.get_value_noise(self.noiseParamsHumid) +end diff --git a/mods/ENVIRONMENT/MapGen/src/Region3D.lua b/mods/ENVIRONMENT/MapGen/src/Region3D.lua new file mode 100644 index 0000000..9c96379 --- /dev/null +++ b/mods/ENVIRONMENT/MapGen/src/Region3D.lua @@ -0,0 +1,19 @@ +local modInfo = Mod.getInfo() +local Region = modInfo.require()("Region") + +local core, setmetatable = core, setmetatable + +-- 3D Region implementation +local Region3D = setmetatable({}, {__index = Region}) + +function Region3D:new(params) + local instance = Region.new(self, params) + instance.type = "3d" + return setmetatable(instance, {__index = Region3D}) +end + +function Region3D:initNoises() + self.noiseCave = core.get_value_noise(self.noiseParamsCave) + self.noiseTemp = core.get_value_noise(self.noiseParamsTemp) + self.noiseHumid = core.get_value_noise(self.noiseParamsHumid) +end diff --git a/mods/ENVIRONMENT/modpack.conf b/mods/ENVIRONMENT/modpack.conf new file mode 100644 index 0000000..29c80f6 --- /dev/null +++ b/mods/ENVIRONMENT/modpack.conf @@ -0,0 +1 @@ +name = smc__environment