From ec614e6080b99dbb2e9aadba10411361c484acd0 Mon Sep 17 00:00:00 2001 From: Vanic Date: Tue, 20 Jan 2026 19:06:53 +0300 Subject: [PATCH] Class: add `getInstance()`. MapGen: fixes `:new()` methods. #18 --- mods/CORE/MapGen/src/MapGen.lua | 28 ++++++++++--------- mods/CORE/MapGen/src/MapGen/Biome.lua | 6 ++-- mods/CORE/MapGen/src/MapGen/Cavern.lua | 6 ++-- mods/CORE/MapGen/src/MapGen/Decoration.lua | 6 ++-- mods/CORE/MapGen/src/MapGen/Layer.lua | 6 ++-- mods/CORE/MapGen/src/MapGen/Peak.lua | 5 ++-- mods/CORE/MapGen/src/MapGen/StaticLayer.lua | 6 ++-- mods/CORE/MapGen/src/MapGen/Tetrahedron.lua | 8 ++++-- mods/CORE/MapGen/src/MapGen/Triangle.lua | 5 ++-- .../MapGen/src/MapGen/Triangulation/Edge.lua | 5 ++-- mods/SYSTEM/Class/init.lua | 24 +++++++++++++++- 11 files changed, 72 insertions(+), 33 deletions(-) diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index 99b6251..b625c68 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -1,6 +1,8 @@ local mathAbs, mathRound, mathMin, mathMax, id = math.abs, math.round, math.min, math.max, core.get_content_id +local getInstance = Class.getInstance + local modInfo = Mod.getInfo('smc__core__map_gen') local require = modInfo.require @@ -34,14 +36,14 @@ local Cavern = require('MapGen.Cavern') local ff2luat = require('MapGen.ff2luat') ---@class MapGen ----@field layersByName table ----@field layersList MapGen.Layer[] ----@field staticsByName table ----@field staticsList MapGen.StaticLayer[] ----@field isLayersNoisesInitialized boolean Is the fractal noise of the layer initialized? This is necessary for a one-time noise initialization. ----@field isCavernsNoiseInitialized boolean Is the fractal noise of the caverns initialized? This is necessary for a one-time noise initialization. ----@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work. ----@field nodeIDs table +---@field layersByName table +---@field layersList MapGen.Layer[] +---@field staticsByName table +---@field staticsList MapGen.StaticLayer[] +---@field isLayersNoisesInitialized boolean Is the fractal noise of the layer initialized? This is necessary for a one-time noise initialization. +---@field isCavernsNoiseInitialized boolean Is the fractal noise of the caverns initialized? This is necessary for a one-time noise initialization. +---@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work. +---@field nodeIDs table local MapGen = { layersByName = {}, layersList = {}, @@ -56,7 +58,7 @@ local MapGen = { --- ---**Only for EmmyLua.** ---@class MapGenDef ----@field nodes table +---@field nodes table ---@param def MapGenDef ---@return MapGen @@ -68,10 +70,10 @@ function MapGen:new(def) end ---@type MapGen - local instance = setmetatable({ - nodes = def.nodes, - nodeIDs = nodesIDs, - }, {__index = self}) + local instance = getInstance(self, { + nodes = def.nodes, + nodeIDs = nodesIDs, + }) return instance end diff --git a/mods/CORE/MapGen/src/MapGen/Biome.lua b/mods/CORE/MapGen/src/MapGen/Biome.lua index 263e755..b0e812c 100644 --- a/mods/CORE/MapGen/src/MapGen/Biome.lua +++ b/mods/CORE/MapGen/src/MapGen/Biome.lua @@ -1,5 +1,7 @@ local id = core.get_content_id +local getInstance = Class.getInstance + ---@class MapGen.Biome ---@field name string ---@field tempPoint number @@ -165,7 +167,7 @@ function Biome:new(name, def) end ---@type MapGen.Biome - local instance = setmetatable({ + local instance = getInstance(self, { name = name, tempPoint = def.tempPoint, humidityPoint = def.humidityPoint, @@ -181,7 +183,7 @@ function Biome:new(name, def) generateSoil = def.generateSoil, generateTurf = def.generateTurf, generateBottom = def.generateBottom, - }, {__index = self}) + }) return instance end diff --git a/mods/CORE/MapGen/src/MapGen/Cavern.lua b/mods/CORE/MapGen/src/MapGen/Cavern.lua index b5a90bc..9643696 100644 --- a/mods/CORE/MapGen/src/MapGen/Cavern.lua +++ b/mods/CORE/MapGen/src/MapGen/Cavern.lua @@ -1,6 +1,8 @@ local mathMin, mathMax, mathAbs = math.min, math.max, math.abs +local getInstance = Class.getInstance + ---@class MapGen.Cavern ---@field name string ---@field minY number @@ -50,13 +52,13 @@ function Cavern:new(name, def) end ---@type MapGen.Cavern - local instance = setmetatable({ + local instance = getInstance(self, { name = name, minY = def.minY, maxY = def.maxY, smoothDistance = def.smoothDistance, threshold = def.threshold, - }, {__index = self}) + }) function instance:getNoise() return _noise diff --git a/mods/CORE/MapGen/src/MapGen/Decoration.lua b/mods/CORE/MapGen/src/MapGen/Decoration.lua index 15d644d..55a4d52 100644 --- a/mods/CORE/MapGen/src/MapGen/Decoration.lua +++ b/mods/CORE/MapGen/src/MapGen/Decoration.lua @@ -1,3 +1,5 @@ +local getInstance = Class.getInstance + ---**NOT IMPLEMENTED** ---@class MapGen.Decoration local Decoration = {} @@ -5,9 +7,9 @@ local Decoration = {} ---@return MapGen.Decoration function Decoration:new() ---@type MapGen.Decoration - local instance = setmetatable({ + local instance = getInstance(self, { - }, {__index = self}) + }) return instance end diff --git a/mods/CORE/MapGen/src/MapGen/Layer.lua b/mods/CORE/MapGen/src/MapGen/Layer.lua index 1f60b38..24b7843 100644 --- a/mods/CORE/MapGen/src/MapGen/Layer.lua +++ b/mods/CORE/MapGen/src/MapGen/Layer.lua @@ -1,3 +1,5 @@ +local getInstance = Class.getInstance + ---@class MapGen.Layer ---@field name string ---@field minY integer @@ -89,7 +91,7 @@ function Layer:new(name, def) end ---@type MapGen.Layer - local instance = setmetatable({ + local instance = getInstance(self, { name = name, minY = def.minY, maxY = def.maxY, @@ -102,7 +104,7 @@ function Layer:new(name, def) calcHumidity = def.calcHumidity, biomesGorizontalScattering = def.biomesGorizontalScattering or 0, biomesVerticalScattering = def.biomesVerticalScattering or 0, - }, {__index = self}) + }) return instance end diff --git a/mods/CORE/MapGen/src/MapGen/Peak.lua b/mods/CORE/MapGen/src/MapGen/Peak.lua index 3ad4294..a40b52c 100644 --- a/mods/CORE/MapGen/src/MapGen/Peak.lua +++ b/mods/CORE/MapGen/src/MapGen/Peak.lua @@ -1,3 +1,5 @@ +local getInstance = Class.getInstance + ---@class MapGen.Peak ---@field id number ---@field getPeakPos fun(self):vector @@ -32,9 +34,8 @@ function Peak:new(def) end ---@type MapGen.Peak - local instance = setmetatable({}, + local instance = getInstance(self,{}, { - __index = self, __tostring = self.toString, __eq = self.eq }) diff --git a/mods/CORE/MapGen/src/MapGen/StaticLayer.lua b/mods/CORE/MapGen/src/MapGen/StaticLayer.lua index 9f94f00..57e66f1 100644 --- a/mods/CORE/MapGen/src/MapGen/StaticLayer.lua +++ b/mods/CORE/MapGen/src/MapGen/StaticLayer.lua @@ -1,5 +1,7 @@ local id = core.get_content_id +local getInstance = Class.getInstance + ---@class MapGen.StaticLayer ---@field name string ---@field minY number @@ -43,14 +45,14 @@ function StaticLayer:new(name, def) local staticNodeID = id(def.staticNode) ---@type MapGen.StaticLayer - local instance = setmetatable({ + local instance = getInstance(self, { name = name, minY = def.minY, maxY = def.maxY, staticNode = def.staticNode, staticNodeID = staticNodeID, generateStaticNode = def.generateStaticNode - }, {__index = self}) + }) return instance end diff --git a/mods/CORE/MapGen/src/MapGen/Tetrahedron.lua b/mods/CORE/MapGen/src/MapGen/Tetrahedron.lua index b723bb4..c8457aa 100644 --- a/mods/CORE/MapGen/src/MapGen/Tetrahedron.lua +++ b/mods/CORE/MapGen/src/MapGen/Tetrahedron.lua @@ -1,5 +1,7 @@ local mathAbs = math.abs +local getInstance = Class.getInstance + local modInfo = Mod.getInfo('smc__core__map_gen') local require = modInfo.require @@ -31,7 +33,7 @@ local Tetrahedron = {} ---@param p4 MapGen.Peak ---@return MapGen.Tetrahedron function Tetrahedron:new(p1, p2, p3, p4) - local instance = setmetatable({ + local instance = getInstance(self, { p1 = p1, p2 = p2, p3 = p3, @@ -44,8 +46,8 @@ function Tetrahedron:new(p1, p2, p3, p4) f2 = Triangle:new(p1, p2, p4), f3 = Triangle:new(p1, p3, p4), f4 = Triangle:new(p2, p3, p4), - }, { - __index = self, + }, + { __tostring = self.toString }) diff --git a/mods/CORE/MapGen/src/MapGen/Triangle.lua b/mods/CORE/MapGen/src/MapGen/Triangle.lua index 5386a14..9614a2b 100644 --- a/mods/CORE/MapGen/src/MapGen/Triangle.lua +++ b/mods/CORE/MapGen/src/MapGen/Triangle.lua @@ -1,6 +1,8 @@ local mathSqrt, mathAbs = math.sqrt, math.abs +local getInstance = Class.getInstance + local modInfo = Mod.getInfo('smc__core__map_gen') local require = modInfo.require @@ -93,7 +95,7 @@ function Triangle:new(p1, p2, p3) color = c1 end - local instance = setmetatable({ + local instance = getInstance(self, { p1 = p1, p2 = p2, p3 = p3, @@ -106,7 +108,6 @@ function Triangle:new(p1, p2, p3) color = color }, { - __index = self, __tostring = self.toString, __eq = self.eq }) diff --git a/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua b/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua index af08b88..a8d5101 100644 --- a/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua +++ b/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua @@ -1,3 +1,5 @@ +local getInstance = Class.getInstance + ---@class MapGen.Triangulation.Edge ---@field p1 MapGen.Peak ---@field p2 MapGen.Peak @@ -8,12 +10,11 @@ local Edge = {} ---@return MapGen.Triangulation.Edge function Edge:new( p1, p2 ) ---@type MapGen.Triangulation.Edge - local instance = setmetatable({ + local instance = getInstance(self, { p1 = p1, p2 = p2 }, { - __index = self, __tostring = self.toString, __eq = self.eq }) diff --git a/mods/SYSTEM/Class/init.lua b/mods/SYSTEM/Class/init.lua index ddaa8f0..0317df8 100644 --- a/mods/SYSTEM/Class/init.lua +++ b/mods/SYSTEM/Class/init.lua @@ -1,3 +1,5 @@ +local setMetaTable, TableCopy = setmetatable, table.copy_with_metatables + ---@class Class Class = {} @@ -5,5 +7,25 @@ Class = {} ---@param parent table ---@param child table function Class.extend(parent ,child) - return setmetatable(child or {}, { __index = parent }) + return setmetatable(child or {}, { __index = TableCopy(parent) }) +end + +---Intended for class constructors. +--- +---Correctly creates an object of the class using `setmetatable()`. +---@generic T:table +---@param self T Parent class. +---@param params table Child object parameters. +---@param metatable table? The table that will become the metatable for the object. Note: The `__index` field will be overwritten by a copy of `self`. +---@return T +function Class.getInstance(self, params, metatable) + if metatable == nil then + metatable = {} + end + + metatable.__index = TableCopy(self) + + local instance = setMetaTable(params, metatable) + + return instance end \ No newline at end of file