Remove GlobalSpace, domains and extends methods in Mod

This commit is contained in:
Van
2025-12-01 17:08:56 +03:00
parent ace217832b
commit e44420d0ab
19 changed files with 72 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
Api = {} Api = {}
--[[
---@alias Domain table<string,Mod|SubDomain> Global table for accessing public classes provided by mods. ---@alias Domain table<string,Mod|SubDomain> Global table for accessing public classes provided by mods.
---@alias SubDomain table<string,Mod> ---@alias SubDomain table<string,Mod>
@@ -46,7 +46,7 @@ Api.addModToGlobalSpace = function(mod, location, force)
-- TODO: баг: ликвидные имена не определяются шаблоном -- TODO: баг: ликвидные имена не определяются шаблоном
--[[if not location:match('^[%a_][%w_]*([%.][%a_][%w_]*)*$') then --[[if not location:match('^[%a_][%w_]*([%.][%a_][%w_]*)*$') then
error('bad argument #2 to \'Api.addModToGlobalSpace\' (invalid format)') error('bad argument #2 to \'Api.addModToGlobalSpace\' (invalid format)')
end--]] end --] ]
-- Split `location` into fields -- TODO: !!!Make a String function!!! -- Split `location` into fields -- TODO: !!!Make a String function!!!
local fields = {} local fields = {}
@@ -88,4 +88,4 @@ Api.addModToGlobalSpace = function(mod, location, force)
error(string.format('field \'%s\' already exists in \'%s\'', last_field, location)) error(string.format('field \'%s\' already exists in \'%s\'', last_field, location))
end end
current[last_field] = mod current[last_field] = mod
end end --]]

View File

@@ -28,6 +28,7 @@ function Mod.getInfo(modname)
return {name = name, shortName = shortName, path = path, require = require} return {name = name, shortName = shortName, path = path, require = require}
end end
--[[
function Mod:new() function Mod:new()
---@type Mod ---@type Mod
local instance = setmetatable(Mod.getInfo(), { __index = self }) local instance = setmetatable(Mod.getInfo(), { __index = self })
@@ -61,6 +62,6 @@ function Mod:getModClassExtended(ChildClass)
local childClass = Mod:getClassExtended(self.Class, ChildClass or {}) local childClass = Mod:getClassExtended(self.Class, ChildClass or {})
return childClass return childClass
end end --]]
return Mod return Mod

View File

@@ -1,5 +1,5 @@
local Enum = Mod:new() local modInfo = Mod.getInfo('smc__core__enum')
local require = modInfo.require
Enum.Class = Enum.require('Enum') return require('Enum')
Api.addModToGlobalSpace(Enum, 'Core.Enum')

View File

@@ -3,7 +3,7 @@
---@class Enum ---@class Enum
local Enum = {} local Enum = {}
---@param enum table[] ---@param enum string[]
---@return Enum ---@return Enum
function Enum:new(enum) function Enum:new(enum)
local metaEnum = {} local metaEnum = {}

View File

@@ -1,5 +1,4 @@
local Item = Mod:new() local modInfo = Mod.getInfo('smc__core__item')
local require = modInfo.require
Item.Class = Item.require('Item') return require('Item')
Api.addModToGlobalSpace(Item, 'Core.Item')

View File

@@ -1,8 +1,10 @@
local modInfo = Mod.getInfo() local modInfo = Mod.getInfo('smc__core__item')
local require = modInfo.require local require = modInfo.require
local VisualEnum = require('Item.VisualEnum') local VisualEnum = require('Item.VisualEnum')
local ItemFactory = Mod.getInfo('smc__core__item_factory').require('ItemFactory')
---@class Item ---@class Item
---@field itemDef Item.ItemDefinition ---@field itemDef Item.ItemDefinition
---@field luantiDef table ---@field luantiDef table
@@ -61,7 +63,7 @@ local defMerge = function(...)
return table.merge(...) return table.merge(...)
end end
---@param itemDef Item.ItemDefinition ---@param itemDef Item.ItemDefinition|Node.NodeDefinition
---@return Item ---@return Item
function Item:new(itemDef) function Item:new(itemDef)
---Adding default parameters ---Adding default parameters
@@ -103,7 +105,7 @@ end
---@return Item ---@return Item
function Item:getExtended(defaultDef) function Item:getExtended(defaultDef)
---@type Item ---@type Item
local ChildClass = Mod:getClassExtended(self, { local ChildClass = Class.extend(self, {
defaultDef = defaultDef, defaultDef = defaultDef,
}) })
@@ -119,7 +121,7 @@ function Item:getFactory(defaultDef)
end end
---@type ItemFactory ---@type ItemFactory
local instance = Core.ItemFactory():new(self, defaultDef) local instance = ItemFactory:new(self, defaultDef)
return instance return instance
end end

View File

@@ -1,7 +1,10 @@
---@type Enum
local Enum = Mod.getInfo('smc__core__enum').require('Enum')
---@class Item.VisualEnum : Enum ---@class Item.VisualEnum : Enum
---@field ITEM_2D Enum.EnumKey ---@field ITEM_2D Enum.EnumKey
---@field ITEM_3D Enum.EnumKey ---@field ITEM_3D Enum.EnumKey
local VisualEnum = Core.Enum:getModClassInstance({ local VisualEnum = Enum:new({
'ITEM_2D', 'ITEM_2D',
'ITEM_3D', 'ITEM_3D',
}) })

View File

@@ -1,5 +1,4 @@
local ItemFactory = Mod:new() local modInfo = Mod.getInfo('smc__core__item_factory')
local require = modInfo.require
ItemFactory.Class = ItemFactory.require('ItemFactory') return require('ItemFactory')
Api.addModToGlobalSpace(ItemFactory, 'Core.ItemFactory')

View File

@@ -1,5 +1,4 @@
local MapGen = Mod:new() local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require
MapGen.Class = MapGen.require('MapGen') return require('MapGen')
Api.addModToGlobalSpace(MapGen, 'Core.MapGen')

View File

@@ -664,8 +664,6 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
-- local param2_data = voxelManip:get_param2_data() -- local param2_data = voxelManip:get_param2_data()
local area = VoxelArea:new({MinEdge = eMin, MaxEdge = eMax}) local area = VoxelArea:new({MinEdge = eMin, MaxEdge = eMax})
data[area:index(0,50,0)] = core.get_content_id('rocks:malachite')
local index local index
-- Initial generation: landscape -- Initial generation: landscape
for z = minPos.z, maxPos.z do for z = minPos.z, maxPos.z do

View File

@@ -10,7 +10,7 @@ local Peak = require('MapGen.Peak')
---@class MapGen.Triangulation.FakePeak : MapGen.Peak ---@class MapGen.Triangulation.FakePeak : MapGen.Peak
---@field id number ---@field id number
---@field getPeakPos fun():vector ---@field getPeakPos fun():vector
local FakePeak = Mod:getClassExtended(Peak, {}) local FakePeak = Class.extend(Peak, {})
---Returns a fake `MapGen.Peak` that has no noise parameters. ---Returns a fake `MapGen.Peak` that has no noise parameters.
---Note that the initialized peak should not be used in generation. ---Note that the initialized peak should not be used in generation.

View File

@@ -1,5 +1,4 @@
local Node = Mod:new() local modInfo = Mod.getInfo('smc__core__node')
local require = modInfo.require
Node.Class = Node.require('Node') return require('Node')
Api.addModToGlobalSpace(Node, 'Core.Node')

View File

@@ -1,9 +1,17 @@
local itemRequire = Mod.getInfo('smc__core__item').require
---@type Item
local Item = itemRequire('Item')
---@type Item.VisualEnum ---@type Item.VisualEnum
local VisualEnum = Core.Item.require('Item.VisualEnum') local VisualEnum = itemRequire('Item.VisualEnum')
---@type ItemFactory
local ItemFactory = Mod.getInfo('smc__core__item_factory').require('ItemFactory')
---@class Node : Item ---@class Node : Item
---@field defaultDef Node.NodeDefinition ---@field defaultDef Node.NodeDefinition
local Node = Core.Item:getModClassExtended({}) local Node = Class.extend(Item, {})
---@param nodeDef Node.NodeDefinition ---@param nodeDef Node.NodeDefinition
---@return Node ---@return Node
@@ -11,7 +19,7 @@ function Node:new(nodeDef)
nodeDef.settings.visual = VisualEnum.ITEM_3D nodeDef.settings.visual = VisualEnum.ITEM_3D
---@type Node ---@type Node
local instance = Core.Item:getModClassInstance(nodeDef) local instance = Item:new(nodeDef)
return instance return instance
end end
@@ -23,7 +31,7 @@ end
---@return Node ---@return Node
function Node:getExtended(defaultDef) function Node:getExtended(defaultDef)
---@type Node ---@type Node
local ChildClass = Mod:getClassExtended(self, { local ChildClass = Class.extend(self, {
defaultDef = defaultDef, defaultDef = defaultDef,
}) })
@@ -39,7 +47,7 @@ function Node:getFactory(defaultDef)
end end
---@type ItemFactory ---@type ItemFactory
local instance = Core.ItemFactory:getModClassInstance(self, defaultDef) local instance = ItemFactory:new(self, defaultDef)
return instance return instance
end end

View File

@@ -1,8 +1,8 @@
---@type Item.Callbacks ---@type Item.Callbacks
local ItemCallbacks = Core.Item.require('Item.Callbacks') local ItemCallbacks = Mod.getInfo('smc__core__item').require('Item.Callbacks')
---@class Node.Callbacks : Item.Callbacks ---@class Node.Callbacks : Item.Callbacks
local Callbacks = Mod:getClassExtended(ItemCallbacks, {}) local Callbacks = Class.extend(ItemCallbacks, {})
---Node constructor; called after adding node. ---Node constructor; called after adding node.
---Can set up metadata and stuff like that. ---Can set up metadata and stuff like that.

View File

@@ -1,8 +1,8 @@
---@type Item.Settings ---@type Item.Settings
local ItemSettings = Core.Item.require('Item.Settings') local ItemSettings = Mod.getInfo('smc__core__item').require('Item.Settings')
---@class Node.Settings : Item.Settings ---@class Node.Settings : Item.Settings
local Settings = Mod:getClassExtended(ItemSettings, {}) local Settings = Class.extend(ItemSettings, {})
---See "Node drawtypes" ---See "Node drawtypes"
---@type string? ---@type string?

View File

@@ -1,5 +1,4 @@
local TileSheet = Mod:new() local modInfo = Mod.getInfo('smc__core__tilesheet')
local require = modInfo.require
TileSheet.Class = TileSheet.require('TileSheet') return require('TileSheet')
Api.addModToGlobalSpace(TileSheet, 'Core.TileSheet')

View File

@@ -1,3 +1,6 @@
---@type Node
local Node = Mod.getInfo('smc__core__node').require('Node')
---@type Node.NodeDefinition ---@type Node.NodeDefinition
local defaultLiquidDef = { local defaultLiquidDef = {
settings = { settings = {
@@ -19,7 +22,7 @@ local defaultLiquidDef = {
} }
---@type Node ---@type Node
local defaultLiquid = Core.Node.Class:getExtended(defaultLiquidDef) local defaultLiquid = Node:getExtended(defaultLiquidDef)
local liquidFactory = defaultLiquid:getFactory() local liquidFactory = defaultLiquid:getFactory()

View File

@@ -1,3 +1,9 @@
---@type TileSheet
local TileSheet = Mod.getInfo('smc__core__tilesheet').require('TileSheet')
---@type Node
local Node = Mod.getInfo('smc__core__node').require('Node')
---@type Node.NodeDefinition ---@type Node.NodeDefinition
local defaultRockDef = { local defaultRockDef = {
settings = { settings = {
@@ -11,12 +17,12 @@ local defaultRockDef = {
} }
---@type Node ---@type Node
local defaultRock = Core.Node.Class:getExtended(defaultRockDef) local defaultRock = Node:getExtended(defaultRockDef)
local rockFactory = defaultRock:getFactory() local rockFactory = defaultRock:getFactory()
---@type TileSheet ---@type TileSheet
local ts = Core.TileSheet:getModClassInstance("rocks_sheet.png", 16, 16) local ts = TileSheet:new("rocks_sheet.png", 16, 16)
rockFactory:registerNodesByShortDef({ rockFactory:registerNodesByShortDef({
{"sylite", "Sylite", nil, {ts:t(0, 0)}}, {"sylite", "Sylite", nil, {ts:t(0, 0)}},

View File

@@ -1,3 +1,9 @@
---@type TileSheet
local TileSheet = Mod.getInfo('smc__core__tilesheet').require('TileSheet')
---@type Node
local Node = Mod.getInfo('smc__core__node').require('Node')
---@type Node.NodeDefinition ---@type Node.NodeDefinition
local defaultSoilDef = { local defaultSoilDef = {
settings = { settings = {
@@ -12,12 +18,12 @@ local defaultSoilDef = {
} }
---@type Node ---@type Node
local defaultSoil = Core.Node.Class:getExtended(defaultSoilDef) local defaultSoil = Node:getExtended(defaultSoilDef)
local soilFactory = defaultSoil:getFactory() local soilFactory = defaultSoil:getFactory()
---@type TileSheet ---@type TileSheet
local ts = Core.TileSheet:getModClassInstance("soils_sheet.png", 16, 16) local ts = TileSheet:new("soils_sheet.png", 16, 16)
local clayTiles = { local clayTiles = {
ts:t(0,0) ts:t(0,0)