Class: add getInstance(). MapGen: fixes :new() methods. #18

This commit is contained in:
Van
2026-01-20 19:06:53 +03:00
parent c32d324bfb
commit ec614e6080
11 changed files with 72 additions and 33 deletions

View File

@@ -1,6 +1,8 @@
local mathAbs, mathRound, mathMin, mathMax, id local mathAbs, mathRound, mathMin, mathMax, id
= math.abs, math.round, math.min, math.max, core.get_content_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 modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require local require = modInfo.require
@@ -34,14 +36,14 @@ local Cavern = require('MapGen.Cavern')
local ff2luat = require('MapGen.ff2luat') local ff2luat = require('MapGen.ff2luat')
---@class MapGen ---@class MapGen
---@field layersByName table<string, MapGen.Layer> ---@field layersByName table<string, MapGen.Layer>
---@field layersList MapGen.Layer[] ---@field layersList MapGen.Layer[]
---@field staticsByName table<string, MapGen.StaticLayer> ---@field staticsByName table<string, MapGen.StaticLayer>
---@field staticsList MapGen.StaticLayer[] ---@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 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 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 isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work.
---@field nodeIDs table<string, number> ---@field nodeIDs table<string, number>
local MapGen = { local MapGen = {
layersByName = {}, layersByName = {},
layersList = {}, layersList = {},
@@ -56,7 +58,7 @@ local MapGen = {
--- ---
---**Only for EmmyLua.** ---**Only for EmmyLua.**
---@class MapGenDef ---@class MapGenDef
---@field nodes table<string, string> ---@field nodes table<string, string>
---@param def MapGenDef ---@param def MapGenDef
---@return MapGen ---@return MapGen
@@ -68,10 +70,10 @@ function MapGen:new(def)
end end
---@type MapGen ---@type MapGen
local instance = setmetatable({ local instance = getInstance(self, {
nodes = def.nodes, nodes = def.nodes,
nodeIDs = nodesIDs, nodeIDs = nodesIDs,
}, {__index = self}) })
return instance return instance
end end

View File

@@ -1,5 +1,7 @@
local id = core.get_content_id local id = core.get_content_id
local getInstance = Class.getInstance
---@class MapGen.Biome ---@class MapGen.Biome
---@field name string ---@field name string
---@field tempPoint number ---@field tempPoint number
@@ -165,7 +167,7 @@ function Biome:new(name, def)
end end
---@type MapGen.Biome ---@type MapGen.Biome
local instance = setmetatable({ local instance = getInstance(self, {
name = name, name = name,
tempPoint = def.tempPoint, tempPoint = def.tempPoint,
humidityPoint = def.humidityPoint, humidityPoint = def.humidityPoint,
@@ -181,7 +183,7 @@ function Biome:new(name, def)
generateSoil = def.generateSoil, generateSoil = def.generateSoil,
generateTurf = def.generateTurf, generateTurf = def.generateTurf,
generateBottom = def.generateBottom, generateBottom = def.generateBottom,
}, {__index = self}) })
return instance return instance
end end

View File

@@ -1,6 +1,8 @@
local mathMin, mathMax, mathAbs local mathMin, mathMax, mathAbs
= math.min, math.max, math.abs = math.min, math.max, math.abs
local getInstance = Class.getInstance
---@class MapGen.Cavern ---@class MapGen.Cavern
---@field name string ---@field name string
---@field minY number ---@field minY number
@@ -50,13 +52,13 @@ function Cavern:new(name, def)
end end
---@type MapGen.Cavern ---@type MapGen.Cavern
local instance = setmetatable({ local instance = getInstance(self, {
name = name, name = name,
minY = def.minY, minY = def.minY,
maxY = def.maxY, maxY = def.maxY,
smoothDistance = def.smoothDistance, smoothDistance = def.smoothDistance,
threshold = def.threshold, threshold = def.threshold,
}, {__index = self}) })
function instance:getNoise() function instance:getNoise()
return _noise return _noise

View File

@@ -1,3 +1,5 @@
local getInstance = Class.getInstance
---**NOT IMPLEMENTED** ---**NOT IMPLEMENTED**
---@class MapGen.Decoration ---@class MapGen.Decoration
local Decoration = {} local Decoration = {}
@@ -5,9 +7,9 @@ local Decoration = {}
---@return MapGen.Decoration ---@return MapGen.Decoration
function Decoration:new() function Decoration:new()
---@type MapGen.Decoration ---@type MapGen.Decoration
local instance = setmetatable({ local instance = getInstance(self, {
}, {__index = self}) })
return instance return instance
end end

View File

@@ -1,3 +1,5 @@
local getInstance = Class.getInstance
---@class MapGen.Layer ---@class MapGen.Layer
---@field name string ---@field name string
---@field minY integer ---@field minY integer
@@ -89,7 +91,7 @@ function Layer:new(name, def)
end end
---@type MapGen.Layer ---@type MapGen.Layer
local instance = setmetatable({ local instance = getInstance(self, {
name = name, name = name,
minY = def.minY, minY = def.minY,
maxY = def.maxY, maxY = def.maxY,
@@ -102,7 +104,7 @@ function Layer:new(name, def)
calcHumidity = def.calcHumidity, calcHumidity = def.calcHumidity,
biomesGorizontalScattering = def.biomesGorizontalScattering or 0, biomesGorizontalScattering = def.biomesGorizontalScattering or 0,
biomesVerticalScattering = def.biomesVerticalScattering or 0, biomesVerticalScattering = def.biomesVerticalScattering or 0,
}, {__index = self}) })
return instance return instance
end end

View File

@@ -1,3 +1,5 @@
local getInstance = Class.getInstance
---@class MapGen.Peak ---@class MapGen.Peak
---@field id number ---@field id number
---@field getPeakPos fun(self):vector ---@field getPeakPos fun(self):vector
@@ -32,9 +34,8 @@ function Peak:new(def)
end end
---@type MapGen.Peak ---@type MapGen.Peak
local instance = setmetatable({}, local instance = getInstance(self,{},
{ {
__index = self,
__tostring = self.toString, __tostring = self.toString,
__eq = self.eq __eq = self.eq
}) })

View File

@@ -1,5 +1,7 @@
local id = core.get_content_id local id = core.get_content_id
local getInstance = Class.getInstance
---@class MapGen.StaticLayer ---@class MapGen.StaticLayer
---@field name string ---@field name string
---@field minY number ---@field minY number
@@ -43,14 +45,14 @@ function StaticLayer:new(name, def)
local staticNodeID = id(def.staticNode) local staticNodeID = id(def.staticNode)
---@type MapGen.StaticLayer ---@type MapGen.StaticLayer
local instance = setmetatable({ local instance = getInstance(self, {
name = name, name = name,
minY = def.minY, minY = def.minY,
maxY = def.maxY, maxY = def.maxY,
staticNode = def.staticNode, staticNode = def.staticNode,
staticNodeID = staticNodeID, staticNodeID = staticNodeID,
generateStaticNode = def.generateStaticNode generateStaticNode = def.generateStaticNode
}, {__index = self}) })
return instance return instance
end end

View File

@@ -1,5 +1,7 @@
local mathAbs = math.abs local mathAbs = math.abs
local getInstance = Class.getInstance
local modInfo = Mod.getInfo('smc__core__map_gen') local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require local require = modInfo.require
@@ -31,7 +33,7 @@ local Tetrahedron = {}
---@param p4 MapGen.Peak ---@param p4 MapGen.Peak
---@return MapGen.Tetrahedron ---@return MapGen.Tetrahedron
function Tetrahedron:new(p1, p2, p3, p4) function Tetrahedron:new(p1, p2, p3, p4)
local instance = setmetatable({ local instance = getInstance(self, {
p1 = p1, p1 = p1,
p2 = p2, p2 = p2,
p3 = p3, p3 = p3,
@@ -44,8 +46,8 @@ function Tetrahedron:new(p1, p2, p3, p4)
f2 = Triangle:new(p1, p2, p4), f2 = Triangle:new(p1, p2, p4),
f3 = Triangle:new(p1, p3, p4), f3 = Triangle:new(p1, p3, p4),
f4 = Triangle:new(p2, p3, p4), f4 = Triangle:new(p2, p3, p4),
}, { },
__index = self, {
__tostring = self.toString __tostring = self.toString
}) })

View File

@@ -1,6 +1,8 @@
local mathSqrt, mathAbs local mathSqrt, mathAbs
= math.sqrt, math.abs = math.sqrt, math.abs
local getInstance = Class.getInstance
local modInfo = Mod.getInfo('smc__core__map_gen') local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require local require = modInfo.require
@@ -93,7 +95,7 @@ function Triangle:new(p1, p2, p3)
color = c1 color = c1
end end
local instance = setmetatable({ local instance = getInstance(self, {
p1 = p1, p1 = p1,
p2 = p2, p2 = p2,
p3 = p3, p3 = p3,
@@ -106,7 +108,6 @@ function Triangle:new(p1, p2, p3)
color = color color = color
}, },
{ {
__index = self,
__tostring = self.toString, __tostring = self.toString,
__eq = self.eq __eq = self.eq
}) })

View File

@@ -1,3 +1,5 @@
local getInstance = Class.getInstance
---@class MapGen.Triangulation.Edge ---@class MapGen.Triangulation.Edge
---@field p1 MapGen.Peak ---@field p1 MapGen.Peak
---@field p2 MapGen.Peak ---@field p2 MapGen.Peak
@@ -8,12 +10,11 @@ local Edge = {}
---@return MapGen.Triangulation.Edge ---@return MapGen.Triangulation.Edge
function Edge:new( p1, p2 ) function Edge:new( p1, p2 )
---@type MapGen.Triangulation.Edge ---@type MapGen.Triangulation.Edge
local instance = setmetatable({ local instance = getInstance(self, {
p1 = p1, p1 = p1,
p2 = p2 p2 = p2
}, },
{ {
__index = self,
__tostring = self.toString, __tostring = self.toString,
__eq = self.eq __eq = self.eq
}) })

View File

@@ -1,3 +1,5 @@
local setMetaTable, TableCopy = setmetatable, table.copy_with_metatables
---@class Class ---@class Class
Class = {} Class = {}
@@ -5,5 +7,25 @@ Class = {}
---@param parent table ---@param parent table
---@param child table ---@param child table
function Class.extend(parent ,child) 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 end