Compare commits
6 Commits
44916bcd3b
...
mapgen-opt
| Author | SHA1 | Date | |
|---|---|---|---|
| ceb35639b8 | |||
| 79df8f2f52 | |||
| e986a4c538 | |||
| 93bda75727 | |||
| 1531abc3e2 | |||
| a34822ed54 |
@@ -24,6 +24,7 @@ function BaseUI:new(name, getFormSpecString, clearContex)
|
||||
instance.getFormSpecString = getFormSpecString
|
||||
|
||||
if clearContex then
|
||||
core.log(name)
|
||||
-- Ensure that when the form is closed, the context is cleared.
|
||||
instance:registerOnFormQuit(function(player)
|
||||
instance:clearContex(player)
|
||||
|
||||
@@ -296,7 +296,7 @@ end
|
||||
---@param w number
|
||||
---@param h number
|
||||
---@param textureName string
|
||||
---@param middle string? Makes the image render in 9-sliced mode and defines the middle rect.
|
||||
---@param middle boolean? Makes the image render in 9-sliced mode and defines the middle rect.
|
||||
---@return Formspec
|
||||
function Formspec:image(x, y, w, h, textureName, middle)
|
||||
local element = ('image[%s,%s;%s,%s;%s;%s]'):format(
|
||||
@@ -441,7 +441,7 @@ end
|
||||
--- * All numbers in middle are integers.
|
||||
---@return Formspec
|
||||
function Formspec:background9(x, y, w, h, textureName, autoClip, middle)
|
||||
local element = ('background9[%s,%s;%s,%s;%s;%s;%s]'):format(
|
||||
local element = ('background9[%s,%s;%s,%s;%s;%s]'):format(
|
||||
x, y, w, h, textureName, autoClip, middle)
|
||||
|
||||
self:addElement(element)
|
||||
|
||||
@@ -4,14 +4,6 @@ local tableCopy, tableMerge, colorize = table.copy, table.merge, core.colorize
|
||||
|
||||
local ItemFactory = Smc.requireItemFactory()
|
||||
|
||||
---Colors the title according to the rarity of the item.
|
||||
---
|
||||
---Note: that if `true`, the `_title_color` will be ignored.
|
||||
local TITLE_RARITY_COLOR = true
|
||||
|
||||
---Shows rarity on a separate line.
|
||||
local SHOW_RARITY = false
|
||||
|
||||
---Determines whether common rarity will be shown in item descriptions or not.
|
||||
local SHOW_COMMON_RARITY = true
|
||||
|
||||
@@ -65,10 +57,6 @@ local Item = {
|
||||
---@param desc string
|
||||
---@return string
|
||||
local function addRarityToDesc(rarity, desc)
|
||||
if not SHOW_RARITY then
|
||||
return desc
|
||||
end
|
||||
|
||||
if not SHOW_COMMON_RARITY and rarity == 'common' then
|
||||
return desc
|
||||
end
|
||||
@@ -110,19 +98,15 @@ local function itemDefToLuantiDef(itemDef)
|
||||
description = string.match(s.name, '%S:(%S+)')..'-desc'
|
||||
end
|
||||
|
||||
assert(s._rarity ~= nil, 'The rarity of an item cannot be empty.')
|
||||
|
||||
if s._title_color ~= nil and s._description_color ~= nil then
|
||||
if TITLE_RARITY_COLOR then
|
||||
title = colorize(RARITY_COLORS[s._rarity], S(title))
|
||||
else
|
||||
title = colorize(s._title_color, S(title))
|
||||
end
|
||||
title = colorize(s._title_color , S(title))
|
||||
description = colorize(s._description_color, S(description))
|
||||
else
|
||||
error('Item description color parameters cannot be empty.')
|
||||
end
|
||||
|
||||
assert(s._rarity ~= nil, 'The rarity of an item cannot be empty.')
|
||||
|
||||
local luantiDescription = addRarityToDesc(s._rarity, title..'\n '..description)
|
||||
|
||||
s._title = title
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
local mathAbs, mathRound, mathMin, mathMax, id, posToString
|
||||
= math.abs, math.round, math.min, math.max, core.get_content_id, core.pos_to_string
|
||||
local mathRound, mathRandom, mathClamp, tableInsert
|
||||
= math.round, math.random, math.clamp, table.insert
|
||||
|
||||
local posToString, getUsTime = core.pos_to_string, core.get_us_time
|
||||
|
||||
local pairs, ipairs, assert = pairs, ipairs, assert
|
||||
|
||||
local v = vector.new
|
||||
local id = core.get_content_id
|
||||
local AIR_ID = core.CONTENT_AIR
|
||||
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
local LOG_PROFILE = false
|
||||
|
||||
local function calculateAverage(t)
|
||||
if #t == 0 then return 0 end -- Защита от деления на ноль
|
||||
local sum = 0
|
||||
for i = 1, #t do
|
||||
sum = sum + t[i]
|
||||
end
|
||||
return sum / #t
|
||||
end
|
||||
local LOG_PROFILE = true
|
||||
local PROFILE_CHUNK = true
|
||||
|
||||
local function mglog(text, ...)
|
||||
core.log((text):format(...))
|
||||
@@ -21,34 +21,30 @@ end
|
||||
local modInfo = Mod.getInfo('smc__core__map_gen')
|
||||
local require = modInfo.require
|
||||
|
||||
---Numeric values defining the surface height and climate for the node.
|
||||
---@alias MapGen.factors {height:number, temp:number, humidity:number}
|
||||
|
||||
---@alias MapGen.PastCalcNoiseValue {triangle:MapGen.Triangle?, x:integer?, z:integer?, noiseValue:number?}
|
||||
---Table of all past generation factor calculations for nodes.
|
||||
---
|
||||
---Contains generation factors by layer name and coordinate.
|
||||
---@type table<string, table<string, MapGen.factors>>
|
||||
local pastNoiseValues = {}
|
||||
|
||||
---@type table<MapGen.Layer.Category, MapGen.PastCalcNoiseValue>
|
||||
local pastNoise2DCalc = {
|
||||
landscape = {},
|
||||
temp = {},
|
||||
humidity = {}
|
||||
}
|
||||
---The triangle in which generation occurred for the previous node.
|
||||
---@type MapGen.Triangle?
|
||||
local pastTriangle
|
||||
|
||||
---@type MapGen.Layer
|
||||
local Layer = require('MapGen.Layer')
|
||||
|
||||
---@type MapGen.StaticLayer
|
||||
local StaticLayer = require('MapGen.StaticLayer')
|
||||
|
||||
---@type MapGen.Biome
|
||||
local Biome = require('MapGen.Biome')
|
||||
|
||||
---@type MapGen.Cavern
|
||||
local Cavern = require('MapGen.Cavern')
|
||||
|
||||
---@type MapGen.Region
|
||||
local Region = require('MapGen.Region')
|
||||
|
||||
local ff2luat = require('MapGen.ff2luat')
|
||||
|
||||
---@class MapGen
|
||||
---@field layersByName table<string, MapGen.Layer>
|
||||
---@field layersList MapGen.Layer[]
|
||||
@@ -57,6 +53,7 @@ local ff2luat = require('MapGen.ff2luat')
|
||||
---@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 nodes table<string, string>
|
||||
---@field nodeIDs table<string, number>
|
||||
local MapGen = {
|
||||
layersByName = {},
|
||||
@@ -83,11 +80,9 @@ function MapGen:new(def)
|
||||
nodesIDs[k] = id(v)
|
||||
end
|
||||
|
||||
---@type MapGen
|
||||
local instance = getInstance(self, {
|
||||
nodes = def.nodes,
|
||||
nodeIDs = nodesIDs,
|
||||
})
|
||||
local instance = getInstance(self, {})
|
||||
instance.nodes = def.nodes
|
||||
instance.nodeIDs = nodesIDs
|
||||
|
||||
return instance
|
||||
end
|
||||
@@ -104,18 +99,16 @@ function MapGen:getLayerByName(layerName)
|
||||
end
|
||||
|
||||
---Determines which layer a node belongs to based on its height coordinate.
|
||||
---@param yPos number
|
||||
---@param y number
|
||||
---@return MapGen.Layer?
|
||||
function MapGen:getLayerByHeight(yPos)
|
||||
function MapGen:getLayerByHeight(y)
|
||||
--- TODO: возможно здесь получится сделать более оптимизированный алгоритм
|
||||
--- учитывая тот факт, что эта функция вызывается для каждой ноды в on_generated
|
||||
--- и может быть даже не один раз
|
||||
--- Можно подумать над тем, чтобы использовать сортированный список
|
||||
|
||||
---@param layer MapGen.Layer
|
||||
for _, layer in ipairs(self.layersList) do
|
||||
|
||||
if yPos >= layer.minY and yPos <= layer.maxY then
|
||||
if y >= layer.minY and y <= layer.maxY then
|
||||
return layer
|
||||
end
|
||||
|
||||
@@ -123,9 +116,9 @@ function MapGen:getLayerByHeight(yPos)
|
||||
end
|
||||
|
||||
---Determines which statics layer a node belongs to based on its height coordinate.
|
||||
---@param yPos number
|
||||
---@param y number
|
||||
---@return MapGen.StaticLayer?
|
||||
function MapGen:getStaticByHeight(yPos)
|
||||
function MapGen:getStaticByHeight(y)
|
||||
--- TODO: возможно здесь получится сделать более оптимизированный алгоритм
|
||||
--- учитывая тот факт, что эта функция вызывается для каждой ноды в on_generated
|
||||
--- и может быть даже не один раз
|
||||
@@ -134,7 +127,7 @@ function MapGen:getStaticByHeight(yPos)
|
||||
---@param static MapGen.StaticLayer
|
||||
for _, static in ipairs(self.staticsList) do
|
||||
|
||||
if yPos >= static.minY and yPos <= static.maxY then
|
||||
if y >= static.minY and y <= static.maxY then
|
||||
return static
|
||||
end
|
||||
|
||||
@@ -143,67 +136,84 @@ end
|
||||
|
||||
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns `nil`.
|
||||
---@param layer string|MapGen.Layer The layer or name of the layer in which the peak is located.
|
||||
---@param category MapGen.Layer.Category The category in which the peak is located.
|
||||
---@param pos MapPosition Coordinate of the desired peak.
|
||||
---@return MapGen.Peak?
|
||||
function MapGen:getPeakByPos(layer, category, pos)
|
||||
function MapGen:getPeakByPos(layer, pos)
|
||||
if type(layer) == 'string' then
|
||||
local layerName = layer
|
||||
layer = self:getLayerByName(layerName)
|
||||
end
|
||||
|
||||
return layer:getPeakByPos(category, pos)
|
||||
return layer:getPeakByPos(pos)
|
||||
end
|
||||
|
||||
---Returns `true` if the point is within the triangle's region.
|
||||
---The barycentric coordinate method is used.
|
||||
---@param px integer
|
||||
---@param pz integer
|
||||
---@param triangle MapGen.Triangle
|
||||
local function pointInTriangle2D(px, pz, triangle)
|
||||
local posP = vector.new(px, 0, pz)
|
||||
local pos1 = triangle.p1.getPeakPos()
|
||||
local pos2 = triangle.p2.getPeakPos()
|
||||
local pos3 = triangle.p3.getPeakPos()
|
||||
|
||||
local v0 = pos3 - pos1
|
||||
local v1 = pos2 - pos1
|
||||
local v2 = posP - pos1
|
||||
|
||||
local dot00 = vector.dot(v0, v0)
|
||||
local dot01 = vector.dot(v0, v1)
|
||||
local dot02 = vector.dot(v0, v2)
|
||||
local dot11 = vector.dot(v1, v1)
|
||||
local dot12 = vector.dot(v1, v2)
|
||||
|
||||
local invDenom = 1 / (dot00 * dot11 - dot01 * dot01)
|
||||
local u = (dot11 * dot02 - dot01 * dot12) * invDenom
|
||||
local v = (dot00 * dot12 - dot01 * dot02) * invDenom
|
||||
|
||||
return (u >= 0) and (v >= 0) and (u + v <= 1)
|
||||
end
|
||||
|
||||
---Returns the `MapGen.Triangle` at the coordinates. If there is no triangle at these coordinates, it returns `nil`.
|
||||
---Checks if a point is inside a triangle using the vector product method.
|
||||
---
|
||||
---**Important:** works only in the XZ plane.
|
||||
---@param layer string|MapGen.Layer The layer or name of the layer the triangle is in.
|
||||
---@param category MapGen.Layer.Category The category in which the triangle is in.
|
||||
---@param pos MapPosition Coordinate of the desired peak.
|
||||
---@return MapGen.Triangle?
|
||||
function MapGen:getTriangleByPos2D(layer, category, pos)
|
||||
if type(layer) == 'string' then
|
||||
local layerName = layer
|
||||
layer = self:getLayerByName(layerName)
|
||||
---If the point is inside the triangle, it will return the triangle itself;
|
||||
---if it is outside, its neighbor in the direction of the point will be returned.
|
||||
---
|
||||
---**Note**: works only with coordinates X-Z.
|
||||
---@param triangle MapGen.Triangle
|
||||
---@param pointPos IntegerVector
|
||||
---@return boolean, MapGen.Triangle?
|
||||
function MapGen:isPointInTriangle(triangle, pointPos)
|
||||
local e1, e2, e3 = triangle.e1, triangle.e2, triangle.e3
|
||||
|
||||
local y1 = e1.vec:cross(pointPos - e1.p1.pos).y
|
||||
if y1 > 0 then
|
||||
return false, triangle.neighbours[3]
|
||||
end
|
||||
|
||||
local y2 = e2.vec:cross(pointPos - e2.p1.pos).y
|
||||
if y2 > 0 then
|
||||
return false, triangle.neighbours[1]
|
||||
end
|
||||
|
||||
local y3 = e3.vec:cross(pointPos - e3.p1.pos).y
|
||||
if y3 > 0 then
|
||||
return false, triangle.neighbours[2]
|
||||
end
|
||||
|
||||
return true, triangle
|
||||
end
|
||||
|
||||
---Finds the triangle containing the point specified by coordinates X & Z.
|
||||
---@param layer MapGen.Layer The layer of the layer the triangle is in.
|
||||
---@param x integer
|
||||
---@param z integer
|
||||
---@param triangle MapGen.Triangle? The triangle from which to begin the search.
|
||||
--- If not specified, the search will be performed
|
||||
--- from the triangle at `id = 1`
|
||||
---@return MapGen.Triangle?
|
||||
function MapGen:findTriangle(layer, x, z, triangle)
|
||||
if triangle == nil then
|
||||
triangle = layer.trianglesList[1]
|
||||
end
|
||||
|
||||
-- Limit the number of iterations to prevent the loop from going into infinity.
|
||||
local iterationCount = 1000000
|
||||
|
||||
local isFound = false
|
||||
while not isFound do
|
||||
if iterationCount < 0 then
|
||||
error('The number of triangle search iterations'..
|
||||
'has significantly exceeded the number of triangles'..
|
||||
'in the list. Most likely, the loop has become infinite')
|
||||
end
|
||||
iterationCount = iterationCount - 1
|
||||
|
||||
if triangle == nil then
|
||||
return nil
|
||||
end
|
||||
|
||||
isFound, triangle = self:isPointInTriangle(triangle, v(x, 0, z))
|
||||
end
|
||||
|
||||
for _, triangle in ipairs(layer.trianglesList[category]) do
|
||||
if pointInTriangle2D(pos.x, pos.z, triangle) then
|
||||
return triangle
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Checks if a point is inside a region.
|
||||
---
|
||||
---If the point is inside the region, it will also return the distance to the nearest border.
|
||||
---@param layer MapGen.Layer The name of the layer in which the region is located.
|
||||
---@param regionName string The name of the region in which the location of the point is checked.
|
||||
@@ -247,13 +257,13 @@ end
|
||||
---@param peak MapGen.Peak
|
||||
---@return string
|
||||
local function vertexKey(peak)
|
||||
local pos = peak:getPeakPos()
|
||||
local pos = peak.pos
|
||||
|
||||
return string.format("%.6f,%.6f", pos.x, pos.z)
|
||||
end
|
||||
|
||||
---Creates a canonical (sorted) key for an edge.
|
||||
---@param edge MapGen.Triangulation.Edge
|
||||
---@param edge MapGen.Edge
|
||||
---@return string
|
||||
local function edgeKey(edge)
|
||||
local k1 = vertexKey(edge.p1)
|
||||
@@ -273,7 +283,7 @@ end
|
||||
function MapGen:getTriangulateBoundaryVertices(triangles)
|
||||
-- Count how many triangles share each edge
|
||||
local edgeCount = {}
|
||||
---@type table<string, MapGen.Triangulation.Edge>
|
||||
---@type table<string, MapGen.Edge>
|
||||
local edgeData = {}
|
||||
|
||||
for _, triangle in ipairs(triangles) do
|
||||
@@ -292,12 +302,12 @@ function MapGen:getTriangulateBoundaryVertices(triangles)
|
||||
end
|
||||
|
||||
-- Boundary edges belong to exactly ONE triangle
|
||||
---@type MapGen.Triangulation.Edge[]
|
||||
---@type MapGen.Edge[]
|
||||
local boundaryEdges = {}
|
||||
|
||||
for key, count in pairs(edgeCount) do
|
||||
if count == 1 then
|
||||
table.insert(boundaryEdges, edgeData[key])
|
||||
tableInsert(boundaryEdges, edgeData[key])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -321,8 +331,8 @@ function MapGen:getTriangulateBoundaryVertices(triangles)
|
||||
adjacency[k1] = adjacency[k1] or {}
|
||||
adjacency[k2] = adjacency[k2] or {}
|
||||
|
||||
table.insert(adjacency[k1], k2)
|
||||
table.insert(adjacency[k2], k1)
|
||||
tableInsert(adjacency[k1], k2)
|
||||
tableInsert(adjacency[k2], k1)
|
||||
end
|
||||
|
||||
-- Walk along boundary edges to create ordered vertex list
|
||||
@@ -340,9 +350,9 @@ function MapGen:getTriangulateBoundaryVertices(triangles)
|
||||
|
||||
while currentKey and not visited[currentKey] do
|
||||
visited[currentKey] = true
|
||||
local pos = vertexData[currentKey]:getPeakPos()
|
||||
local pos = vertexData[currentKey].pos
|
||||
|
||||
table.insert(boundary, {
|
||||
tableInsert(boundary, {
|
||||
x = pos.x,
|
||||
y = 0,
|
||||
z = pos.z
|
||||
@@ -384,7 +394,9 @@ function MapGen:registerLayer(name, def)
|
||||
local layer = Layer:new(name, def)
|
||||
|
||||
self.layersByName[name] = layer
|
||||
table.insert(self.layersList, layer)
|
||||
tableInsert(self.layersList, layer)
|
||||
|
||||
pastNoiseValues[name] = {}
|
||||
|
||||
-- Sort layers by maxY descending for processing priority.
|
||||
table.sort(self.layersList, function(a, b)
|
||||
@@ -419,7 +431,7 @@ function MapGen:registerStaticLayer(name, def)
|
||||
local static = StaticLayer:new(name, def)
|
||||
|
||||
self.staticsByName[name] = static
|
||||
table.insert(self.staticsList, static)
|
||||
tableInsert(self.staticsList, static)
|
||||
|
||||
-- Sort srtatics by maxY descending for processing priority.
|
||||
table.sort(self.staticsList, function(a, b)
|
||||
@@ -484,12 +496,11 @@ end
|
||||
|
||||
---Initialize the boundary region for a layer and category.
|
||||
---@param layer MapGen.Layer
|
||||
---@param category MapGen.Layer.Category
|
||||
function MapGen:initTriangultaionBoundaryRegion(layer, category)
|
||||
local triangles = layer.trianglesList[category]
|
||||
function MapGen:initTriangultaionBoundaryRegion(layer)
|
||||
local triangles = layer.trianglesList
|
||||
local regionVertices = self:getTriangulateBoundaryVertices(triangles)
|
||||
|
||||
local regionName = category..'_boundary'
|
||||
local regionName = 'triangles_boundary'
|
||||
|
||||
local region = Region:new(regionName, {vertices = regionVertices})
|
||||
|
||||
@@ -497,12 +508,10 @@ function MapGen:initTriangultaionBoundaryRegion(layer, category)
|
||||
end
|
||||
|
||||
---Initialize border regions for all layers and all categories.
|
||||
function MapGen:initLayersBoundaryRegions()
|
||||
function MapGen:initLayersBoundaryRegion()
|
||||
---@param layer MapGen.Layer
|
||||
for _, layer in ipairs(self.layersList) do
|
||||
self:initTriangultaionBoundaryRegion(layer, 'landscape')
|
||||
self:initTriangultaionBoundaryRegion(layer, 'temp')
|
||||
self:initTriangultaionBoundaryRegion(layer, 'humidity')
|
||||
self:initTriangultaionBoundaryRegion(layer)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -599,17 +608,17 @@ local function generateNode(mapGenerator, layer, factors, data, index, x, y, z)
|
||||
local scattHeight = y
|
||||
|
||||
if scatteringGorizontal ~= 0 then
|
||||
scattTemp = scattTemp + math.random(scatteringGorizontal * -1, scatteringGorizontal)
|
||||
scattHumidity = scattHumidity + math.random(scatteringGorizontal * -1, scatteringGorizontal)
|
||||
scattTemp = scattTemp + mathRandom(scatteringGorizontal * -1, scatteringGorizontal)
|
||||
scattHumidity = scattHumidity + mathRandom(scatteringGorizontal * -1, scatteringGorizontal)
|
||||
end
|
||||
|
||||
if scatteringVertical ~= 0 then
|
||||
scattHeight = scattHeight + math.random(scatteringVertical * -1, scatteringVertical)
|
||||
scattHeight = scattHeight + mathRandom(scatteringVertical * -1, scatteringVertical)
|
||||
end
|
||||
|
||||
scattTemp = math.clamp(scattTemp, layer.minTemp, layer.maxTemp)
|
||||
scattHumidity = math.clamp(scattHumidity, layer.minHumidity, layer.maxHumidity)
|
||||
scattHeight = math.clamp(scattHeight, layer.minY, layer.maxY)
|
||||
scattTemp = mathClamp(scattTemp, layer.minTemp, layer.maxTemp)
|
||||
scattHumidity = mathClamp(scattHumidity, layer.minHumidity, layer.maxHumidity)
|
||||
scattHeight = mathClamp(scattHeight, layer.minY, layer.maxY)
|
||||
|
||||
scattHeight, scattTemp, scattHumidity = mathRound(scattHeight), mathRound(scattTemp), mathRound(scattHumidity)
|
||||
height, temp, humidity = mathRound(height), mathRound(scattTemp), mathRound(scattHumidity)
|
||||
@@ -679,15 +688,15 @@ end
|
||||
---Calculates a weight for a coordinate using the projection onto
|
||||
---the triangle's height and specifying the distance to the peak
|
||||
---for which the weight is calculated.
|
||||
---@param height MapGen.Triangulation.Edge
|
||||
---@param height MapGen.Edge
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
---@return number
|
||||
local function calcWeight(height, x, y, z)
|
||||
local pos1 = vector.new(x, y, z)
|
||||
local pos2 = height.p1:getPeakPos()
|
||||
local pos3 = height.p2:getPeakPos()
|
||||
local pos1 = v(x, y, z)
|
||||
local pos2 = height.p1.pos
|
||||
local pos3 = height.p2.pos
|
||||
|
||||
local v = pos3 - pos2
|
||||
---@type vector
|
||||
@@ -699,7 +708,7 @@ local function calcWeight(height, x, y, z)
|
||||
|
||||
local posP = pos2 + v * t
|
||||
|
||||
return 1 - vector.distance(pos2, posP) / height:length()
|
||||
return 1 - pos2:distance(posP) / height:length()
|
||||
end
|
||||
|
||||
---Returns the weighted values of 2d noise.
|
||||
@@ -723,9 +732,9 @@ local function calcNoise2dValue(category, layer, triangle, x, y, z)
|
||||
local peak2 = triangle.p2
|
||||
local peak3 = triangle.p3
|
||||
|
||||
local noise1 = layer:getNoiseByColor(category, peak1:getColor())
|
||||
local noise2 = layer:getNoiseByColor(category, peak2:getColor())
|
||||
local noise3 = layer:getNoiseByColor(category, peak3:getColor())
|
||||
local noise1 = layer:getNoiseByColor(category, peak1.color)
|
||||
local noise2 = layer:getNoiseByColor(category, peak2.color)
|
||||
local noise3 = layer:getNoiseByColor(category, peak3.color)
|
||||
|
||||
local noiseValue = 0.0
|
||||
|
||||
@@ -752,138 +761,116 @@ end
|
||||
---Calculates a smoothed 2D noise value based on a preliminary space partitioning.
|
||||
---
|
||||
---Returns `nil` if the point is not included in any triangle.
|
||||
---@param category MapGen.Layer.Category
|
||||
---@param layer MapGen.Layer
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
---@return number?
|
||||
function MapGen:getNoises2dValue(category, layer, x, y, z)
|
||||
---@type MapGen.PastCalcNoiseValue
|
||||
local pastCalc = pastNoise2DCalc[category]
|
||||
---@return number?, number?, number?
|
||||
function MapGen:getFactors(layer, x, y, z)
|
||||
local posString = posToString({x=x,y=0,z=z})
|
||||
|
||||
if (pastCalc.x ~= nil and pastCalc.z ~= nil) and (x == pastCalc.x and z == pastCalc.z) then
|
||||
return pastCalc.noiseValue
|
||||
local factors = pastNoiseValues[layer.name][posString]
|
||||
if factors ~= nil then
|
||||
return factors.height, factors.temp, factors.humidity
|
||||
end
|
||||
|
||||
if pastCalc.triangle ~= nil then
|
||||
---@type number?
|
||||
local noiseValue
|
||||
-- TODO: это сильно тормозит обычные чанки, но ускоряет те, что находятся за границей
|
||||
if not self:pointInRegion2D(layer, 'triangles_boundary', {x=x, y=y, z=z}) then
|
||||
return
|
||||
end
|
||||
|
||||
if pastTriangle ~= nil then
|
||||
local triangles = {
|
||||
pastCalc.triangle,
|
||||
pastCalc.triangle.neighbours[1],
|
||||
pastCalc.triangle.neighbours[2],
|
||||
pastCalc.triangle.neighbours[3],
|
||||
pastTriangle,
|
||||
pastTriangle.neighbours[1],
|
||||
pastTriangle.neighbours[2],
|
||||
pastTriangle.neighbours[3],
|
||||
}
|
||||
|
||||
---@param t MapGen.Triangle
|
||||
for _, t in ipairs(triangles) do
|
||||
if pointInTriangle2D(x, z, t) then
|
||||
noiseValue = calcNoise2dValue(category, layer, t, x, y, z)
|
||||
|
||||
pastCalc.x = x
|
||||
pastCalc.z = z
|
||||
pastCalc.noiseValue = noiseValue
|
||||
|
||||
return noiseValue
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not self:pointInRegion2D(layer, category..'_boundary', {x=x, y=y, z=z}) then
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param triangle MapGen.Triangle
|
||||
for _, triangle in ipairs(layer.trianglesList[category]) do
|
||||
if pointInTriangle2D(x, z, triangle) then
|
||||
local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z)
|
||||
for _, triangle in ipairs(triangles) do
|
||||
if self:isPointInTriangle(triangle, v(x, 0, z)) then
|
||||
local height = calcNoise2dValue('landscape', layer, triangle, x, y, z)
|
||||
local temp = calcNoise2dValue('temp', layer, triangle, x, y, z)
|
||||
local humidity = calcNoise2dValue('humidity', layer, triangle, x, y, z)
|
||||
|
||||
pastCalc.triangle = triangle
|
||||
pastCalc.x = x
|
||||
pastCalc.z = z
|
||||
pastCalc.noiseValue = noiseValue
|
||||
pastNoiseValues[layer.name][posString] = {
|
||||
height = height,
|
||||
temp = temp,
|
||||
humidity = humidity
|
||||
}
|
||||
|
||||
return noiseValue
|
||||
return height, temp, humidity
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- If the point does not fall within the triangle, it is impossible to calculate the height.
|
||||
-- For debugging purposes only, may slow down generation:
|
||||
--core.log('warning', string.format('The point %s %s %s does not fall within any triangle, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z)))
|
||||
local triangle = self:findTriangle(layer, x, z, pastTriangle)
|
||||
if triangle == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local height = calcNoise2dValue('landscape', layer, triangle, x, y, z)
|
||||
local temp = calcNoise2dValue('temp', layer, triangle, x, y, z)
|
||||
local humidity = calcNoise2dValue('humidity', layer, triangle, x, y, z)
|
||||
|
||||
pastTriangle = triangle
|
||||
pastNoiseValues[layer.name][posString] = {
|
||||
height = height,
|
||||
temp = temp,
|
||||
humidity = humidity
|
||||
}
|
||||
|
||||
return height, temp, humidity
|
||||
end
|
||||
|
||||
---Generator handler for specific coordinates.
|
||||
---@param mapGenerator MapGen
|
||||
---@param layer MapGen.Layer?
|
||||
---@param data number[]
|
||||
---@param index number
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
---@return number, number, number
|
||||
local function generatorHandler(mapGenerator, data, index, x, y, z)
|
||||
local startGetLayerTime = core.get_us_time()
|
||||
local function generatorHandler(mapGenerator, layer, data, index, x, y, z)
|
||||
-- If the node belongs to a static layer, then a static node is generated.
|
||||
local static = mapGenerator:getStaticByHeight(y)
|
||||
if static ~= nil then
|
||||
static:generateStaticNode(mapGenerator, data, index, x, y, z)
|
||||
|
||||
return core.get_us_time() - startGetLayerTime, 0, 0
|
||||
return
|
||||
end
|
||||
|
||||
-- If generation occurs outside the layers, then a void is generated.
|
||||
local layer = mapGenerator:getLayerByHeight(y)
|
||||
if layer == nil then
|
||||
data[index] = mapGenerator.nodeIDs.air
|
||||
|
||||
return core.get_us_time() - startGetLayerTime, 0, 0
|
||||
return
|
||||
end
|
||||
|
||||
local endGetLayerTime = core.get_us_time()
|
||||
local endGetLayerTime = getUsTime()
|
||||
local startNoiseCalcTime = endGetLayerTime
|
||||
-- If the value of the noise is `nil`,
|
||||
-- then the point does not fall within any of the triangles.
|
||||
local noiseHeightValue = mapGenerator:getNoises2dValue('landscape', layer, x, y, z)
|
||||
if noiseHeightValue == nil then
|
||||
local height, temp, humidity = mapGenerator:getFactors(layer, x, y, z)
|
||||
if height == nil or temp == nil or humidity == nil then
|
||||
data[index] = core.CONTENT_IGNORE
|
||||
|
||||
return endGetLayerTime - startGetLayerTime, core.get_us_time() - startNoiseCalcTime, 0
|
||||
end
|
||||
|
||||
local noiseTempValue = mapGenerator:getNoises2dValue('temp', layer, x, y, z)
|
||||
if noiseTempValue == nil then
|
||||
data[index] = core.CONTENT_IGNORE
|
||||
|
||||
return endGetLayerTime - startGetLayerTime, core.get_us_time() - startNoiseCalcTime, 0
|
||||
end
|
||||
|
||||
local noiseHumidityValue = mapGenerator:getNoises2dValue('humidity', layer, x, y, z)
|
||||
if noiseHumidityValue == nil then
|
||||
data[index] = core.CONTENT_IGNORE
|
||||
|
||||
return endGetLayerTime - startGetLayerTime, core.get_us_time() - startNoiseCalcTime, 0
|
||||
return
|
||||
end
|
||||
|
||||
-- We recalculate the climate based on altitude.
|
||||
noiseTempValue = layer:calcTemp(noiseTempValue, y)
|
||||
noiseHumidityValue = layer:calcHumidity(noiseHumidityValue, y)
|
||||
temp = layer:calcTemp(temp, y)
|
||||
humidity = layer:calcHumidity(humidity, y)
|
||||
|
||||
---@type MapGen.factors
|
||||
local factors = {
|
||||
height = noiseHeightValue,
|
||||
temp = noiseTempValue,
|
||||
humidity = noiseHumidityValue,
|
||||
height = height,
|
||||
temp = temp,
|
||||
humidity = humidity,
|
||||
}
|
||||
|
||||
local endNoiseCalcTime = core.get_us_time()
|
||||
local startGenNodeTime = endNoiseCalcTime
|
||||
|
||||
generateNode(mapGenerator, layer, factors, data, index, x, y, z)
|
||||
|
||||
local endGenNodeTime = core.get_us_time()
|
||||
|
||||
return endGetLayerTime - startGetLayerTime, endNoiseCalcTime - startNoiseCalcTime, endGenNodeTime - startGenNodeTime
|
||||
end
|
||||
|
||||
---Chunk generation handler.
|
||||
@@ -893,15 +880,15 @@ end
|
||||
---@param blockseed number
|
||||
function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
||||
local minPosStr, maxPosStr = posToString(minPos), posToString(maxPos)
|
||||
local getLayerTime, NoiseCalcTime, GenNodeTime = {}, {}, {}
|
||||
local startTime, endTime = 0.0, 0.0
|
||||
|
||||
if LOG_PROFILE then
|
||||
mglog('Chunk run:\n'..
|
||||
'min_pos: %s\n'..
|
||||
'max_pos: %s\n',
|
||||
minPosStr, maxPosStr)
|
||||
startTime = getUsTime()
|
||||
end
|
||||
local startTime = core.get_us_time()
|
||||
|
||||
local eMin, eMax = voxelManip:get_emerged_area()
|
||||
|
||||
@@ -917,6 +904,11 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
||||
-- local param2_data = voxelManip:get_param2_data()
|
||||
local area = VoxelArea:new({MinEdge = eMin, MaxEdge = eMax})
|
||||
|
||||
-- We obtain a layer for the entire chunk at once for optimization purposes.
|
||||
-- Therefore, if the layer boundaries do not coincide with the chunk boundaries,
|
||||
-- the chunk will be entirely contained within the layer containing its center.
|
||||
local layer = self:getLayerByHeight((minPos.y+maxPos.y)/2)
|
||||
|
||||
local index
|
||||
-- Initial generation: landscape
|
||||
for z = minPos.z, maxPos.z do
|
||||
@@ -924,12 +916,8 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
||||
for y = minPos.y, maxPos.y do
|
||||
index = area:index(x, y, z)
|
||||
|
||||
if data[index] == core.CONTENT_AIR then
|
||||
local t1, t2, t3 = generatorHandler(self, data, index, x, y, z)
|
||||
|
||||
table.insert(getLayerTime, t1)
|
||||
table.insert(NoiseCalcTime, t2)
|
||||
table.insert(GenNodeTime, t3)
|
||||
if data[index] == AIR_ID then
|
||||
generatorHandler(self, layer, data, index, x, y, z)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -962,19 +950,17 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
||||
voxelManip:calc_lighting()
|
||||
voxelManip:update_liquids()
|
||||
|
||||
local endTime = core.get_us_time()
|
||||
if LOG_PROFILE then
|
||||
pastTriangle = nil
|
||||
|
||||
if PROFILE_CHUNK then
|
||||
endTime = getUsTime()
|
||||
mglog('Chunk end:\n'..
|
||||
'min_pos: %s\n'..
|
||||
'max_pos: %s\n'..
|
||||
'time (sec): %.3f\n'..
|
||||
'GenerateHandler time (us): %.3f %.3f %.3f\n'..
|
||||
'---------------',
|
||||
minPosStr, maxPosStr,
|
||||
(endTime - startTime)/1000000,
|
||||
calculateAverage(getLayerTime),
|
||||
calculateAverage(NoiseCalcTime),
|
||||
calculateAverage(GenNodeTime))
|
||||
(endTime - startTime)/1000000)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -998,7 +984,7 @@ function MapGen:run()
|
||||
end)
|
||||
|
||||
self:initBiomesDiagram()
|
||||
self:initLayersBoundaryRegions()
|
||||
self:initLayersBoundaryRegion()
|
||||
|
||||
MapGen.isRunning = true
|
||||
core.log('MapGen run')
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
---@class MapGen.Triangulation.Edge
|
||||
---@class MapGen.Edge
|
||||
---@field p1 MapGen.Peak
|
||||
---@field p2 MapGen.Peak
|
||||
---@field vec vector
|
||||
local Edge = {}
|
||||
|
||||
---@param p1 MapGen.Peak
|
||||
---@param p2 MapGen.Peak
|
||||
---@return MapGen.Triangulation.Edge
|
||||
---@return MapGen.Edge
|
||||
function Edge:new( p1, p2 )
|
||||
---@type MapGen.Triangulation.Edge
|
||||
---@type MapGen.Edge
|
||||
local instance = getInstance(self, {
|
||||
p1 = p1,
|
||||
p2 = p2
|
||||
p2 = p2,
|
||||
vec = p2.pos - p1.pos
|
||||
},
|
||||
{
|
||||
__tostring = self.toString,
|
||||
@@ -29,14 +31,14 @@ function Edge:toString()
|
||||
end
|
||||
|
||||
---Returns true if the edges are equivalent.
|
||||
---@param other MapGen.Triangulation.Edge
|
||||
---@param other MapGen.Edge
|
||||
---@return boolean
|
||||
function Edge:eq(other)
|
||||
return self.p1 == other.p1 and self.p2 == other.p2
|
||||
end
|
||||
|
||||
---Returns true if the edges are similar.
|
||||
---@param other MapGen.Triangulation.Edge
|
||||
---@param other MapGen.Edge
|
||||
---@return boolean
|
||||
function Edge:same(other)
|
||||
return (self.p1 == other.p1 and self.p2 == other.p2)
|
||||
@@ -46,14 +48,14 @@ end
|
||||
---Returns the length of an edge.
|
||||
---@return number
|
||||
function Edge:length()
|
||||
return (self.p1.getPeakPos() - self.p2.getPeakPos()):length()
|
||||
return (self.p1.pos - self.p2.pos):length()
|
||||
end
|
||||
|
||||
---Returns the vector of the point located in the middle of the edge.
|
||||
---@return vector
|
||||
function Edge:getMidPoint()
|
||||
local pos1 = self.p1:getPeakPos()
|
||||
local pos2 = self.p2:getPeakPos()
|
||||
local pos1 = self.p1.pos
|
||||
local pos2 = self.p2.pos
|
||||
|
||||
return (pos1 + pos2 )/2
|
||||
end
|
||||
@@ -7,7 +7,7 @@ local Peak = require('MapGen.Peak')
|
||||
---Allows you to create a `MapGen.Peak` without specifying a noise parameter.
|
||||
---
|
||||
---**WARNING: MUST NOT BE PARTICIPATED IN GENERATION**
|
||||
---@class MapGen.Triangulation.FakePeak : MapGen.Peak
|
||||
---@class MapGen.FakePeak : MapGen.Peak
|
||||
---@field id number
|
||||
---@field getPeakPos fun():vector
|
||||
local FakePeak = Class.extend(Peak, {})
|
||||
@@ -19,8 +19,8 @@ local Triangle = require('MapGen.Triangle')
|
||||
---@field waterLevel integer
|
||||
---@field calcTemp fun(self:MapGen.Layer, value:number, height:number):number
|
||||
---@field calcHumidity fun(self:MapGen.Layer, value:number, height:number):number
|
||||
---@field peaksList table<MapGen.Layer.Category, MapGen.Peak[]>
|
||||
---@field peaksByPos table<MapGen.Layer.Category, table<string, MapGen.Peak>>
|
||||
---@field peaksList MapGen.Peak[]
|
||||
---@field peaksByPos table<string, MapGen.Peak>
|
||||
---@field noisesByColor table<MapGen.Layer.Category, table<ColorString, ValueNoise>>
|
||||
---@field noisesParamsByColor table<MapGen.Layer.Category, table<ColorString, NoiseParams>>
|
||||
---@field biomesByName table<string, MapGen.Biome>
|
||||
@@ -31,18 +31,10 @@ local Triangle = require('MapGen.Triangle')
|
||||
---@field cavernsByName table<string, MapGen.Cavern>
|
||||
---@field cavernsList MapGen.Cavern[]
|
||||
---@field regionsByName table<string, MapGen.Region>
|
||||
---@field trianglesList table<MapGen.Layer.Category, MapGen.Triangle[]>
|
||||
---@field trianglesList MapGen.Triangle[]
|
||||
local Layer = {
|
||||
peaksList = {
|
||||
landscape = {},
|
||||
temp = {},
|
||||
humidity = {},
|
||||
},
|
||||
peaksByPos = {
|
||||
landscape = {},
|
||||
temp = {},
|
||||
humidity = {},
|
||||
},
|
||||
peaksList = {},
|
||||
peaksByPos = {},
|
||||
noisesByColor = {
|
||||
landscape = {},
|
||||
temp = {},
|
||||
@@ -59,11 +51,7 @@ local Layer = {
|
||||
cavernsByName = {},
|
||||
cavernsList = {},
|
||||
regionsByName = {},
|
||||
trianglesList = {
|
||||
landscape = {},
|
||||
temp = {},
|
||||
humidity = {},
|
||||
},
|
||||
trianglesList = {},
|
||||
tetrahedronsList = {},
|
||||
}
|
||||
|
||||
@@ -103,10 +91,9 @@ end
|
||||
---**Note:** If the colors of the new peak and the existing one at the same coordinates
|
||||
---are different, an error will be called.
|
||||
---@param def MapGen.PeakDef
|
||||
---@param category MapGen.Layer.Category
|
||||
---@param scale number
|
||||
---@return MapGen.Peak
|
||||
function Layer:registerPeak(def, category, scale)
|
||||
function Layer:registerPeak(def, scale)
|
||||
assert(def.pos ~= nil)
|
||||
assert(def.pos.x ~= nil)
|
||||
assert(def.pos.z ~= nil)
|
||||
@@ -119,12 +106,12 @@ function Layer:registerPeak(def, category, scale)
|
||||
|
||||
local positionStr = core.pos_to_string(def.pos)
|
||||
|
||||
local peak = self.peaksByPos[category][positionStr]
|
||||
local peak = self.peaksByPos[positionStr]
|
||||
|
||||
if peak == nil then
|
||||
peak = Peak:new(def)
|
||||
self.peaksByPos[category][positionStr] = peak
|
||||
elseif peak.getColor() ~= def.color then
|
||||
self.peaksByPos[positionStr] = peak
|
||||
elseif peak.color ~= def.color then
|
||||
error('There cannot be two peaks with the same coordinates but different colors.')
|
||||
end
|
||||
|
||||
@@ -132,13 +119,13 @@ function Layer:registerPeak(def, category, scale)
|
||||
end
|
||||
|
||||
function Layer:initTrianglesNeighbours()
|
||||
for _, triangles in pairs(self.trianglesList) do
|
||||
for _, triangle in ipairs(triangles) do
|
||||
local triangles = self.trianglesList
|
||||
|
||||
for _, triangle in pairs(triangles) do
|
||||
for _, id in ipairs(triangle.neighboursIDs) do
|
||||
table.insert(triangle.neighbours, triangles[id])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param fileName string
|
||||
@@ -155,9 +142,9 @@ function Layer:getTriaglesFromFile(fileName, category, scale)
|
||||
assert(triangles ~= nil)
|
||||
|
||||
for _, tr in ipairs(triangles) do
|
||||
local p1 = self:registerPeak( tr.vertices[1], category, scale)
|
||||
local p2 = self:registerPeak( tr.vertices[2], category, scale)
|
||||
local p3 = self:registerPeak( tr.vertices[3], category, scale)
|
||||
local p1 = self:registerPeak( tr.vertices[1], scale)
|
||||
local p2 = self:registerPeak( tr.vertices[2], scale)
|
||||
local p3 = self:registerPeak( tr.vertices[3], scale)
|
||||
|
||||
local triangle = Triangle:new(tr.id,p1, p2, p3, tr.adjacent_triangle_ids)
|
||||
|
||||
@@ -219,7 +206,7 @@ end
|
||||
---@param category MapGen.Layer.Category
|
||||
---@param triangle MapGen.Triangle
|
||||
function Layer:addTriangle(category, triangle)
|
||||
local triangles = self.trianglesList[category]
|
||||
local triangles = self.trianglesList
|
||||
if triangles ~= nil then
|
||||
triangles[triangle.id] = triangle
|
||||
else
|
||||
@@ -231,25 +218,16 @@ end
|
||||
---@param category MapGen.Layer.Category
|
||||
---@param peak MapGen.Peak
|
||||
function Layer:addPeak(category, peak)
|
||||
if category == 'landscape' then
|
||||
table.insert(self.peaksList.landscape, peak)
|
||||
elseif category == 'temp' then
|
||||
table.insert(self.peaksList.temp, peak)
|
||||
elseif category == 'humidity' then
|
||||
table.insert(self.peaksList.humidity, peak)
|
||||
else
|
||||
error(('There is no peak category named %s.'):format(category))
|
||||
end
|
||||
table.insert(self.peaksList, peak)
|
||||
end
|
||||
|
||||
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns `nil`.
|
||||
---@param category MapGen.Layer.Category The category in which the peak is located.
|
||||
---@param pos Position Coordinate of the desired peak.
|
||||
---@return MapGen.Peak?
|
||||
function Layer:getPeakByPos(category, pos)
|
||||
function Layer:getPeakByPos(pos)
|
||||
local positionStr = core.pos_to_string(pos)
|
||||
|
||||
local peak = self.peaksByPos[category][positionStr]
|
||||
local peak = self.peaksByPos[positionStr]
|
||||
|
||||
return peak
|
||||
end
|
||||
|
||||
@@ -2,8 +2,8 @@ local getInstance = Class.getInstance
|
||||
|
||||
---@class MapGen.Peak
|
||||
---@field id number
|
||||
---@field getPeakPos fun(self):vector
|
||||
---@field getColor fun(self):ColorString
|
||||
---@field pos vector
|
||||
---@field color ColorString
|
||||
---@field getGroups fun(self):table<string, number>
|
||||
---@field addGroups fun(self, groups:table<string, number>)
|
||||
local Peak = {
|
||||
@@ -23,7 +23,6 @@ local Peak = {
|
||||
---@param def MapGen.PeakDef
|
||||
---@return MapGen.Peak
|
||||
function Peak:new(def)
|
||||
local _pos = vector.new(def.pos)
|
||||
local _color = def.color
|
||||
local _groups
|
||||
|
||||
@@ -39,14 +38,8 @@ function Peak:new(def)
|
||||
__tostring = self.toString,
|
||||
__eq = self.eq
|
||||
})
|
||||
|
||||
function instance:getPeakPos()
|
||||
return _pos:copy()
|
||||
end
|
||||
|
||||
function instance:getColor()
|
||||
return _color
|
||||
end
|
||||
instance.pos = vector.new(def.pos)
|
||||
instance.color = def.color
|
||||
|
||||
function instance:getGroups()
|
||||
return table.copy_with_metatables(_groups)
|
||||
@@ -66,7 +59,7 @@ end
|
||||
---Returns a string describing the object in a readable form.
|
||||
---@return string
|
||||
function Peak:toString()
|
||||
local pos = self:getPeakPos()
|
||||
local pos = self.pos
|
||||
return ('Peak (%s) x: %.2f y: %.2f z: %.2f'):format( self.id, pos.x, pos.y, pos.z)
|
||||
end
|
||||
|
||||
@@ -74,8 +67,8 @@ end
|
||||
---@param other MapGen.Peak
|
||||
---@return boolean
|
||||
function Peak:eq(other)
|
||||
local pos1 = self:getPeakPos()
|
||||
local pos2 = other:getPeakPos()
|
||||
local pos1 = self.pos
|
||||
local pos2 = other.pos
|
||||
|
||||
return pos1.x == pos2.x and pos1.z == pos2.z
|
||||
end
|
||||
|
||||
@@ -6,10 +6,10 @@ local getInstance = Class.getInstance
|
||||
local modInfo = Mod.getInfo('smc__core__map_gen')
|
||||
local require = modInfo.require
|
||||
|
||||
---@type MapGen.Triangulation.Edge
|
||||
local Edge = require('MapGen.Triangulation.Edge')
|
||||
---@type MapGen.Triangulation.FakePeak
|
||||
local FakePeak = require('MapGen.Triangulation.FakePeak')
|
||||
---@type MapGen.Edge
|
||||
local Edge = require('MapGen.Edge')
|
||||
---@type MapGen.FakePeak
|
||||
local FakePeak = require('MapGen.FakePeak')
|
||||
|
||||
-- --- Helpers functions ---
|
||||
|
||||
@@ -29,9 +29,9 @@ end
|
||||
---@param p3 MapGen.Peak
|
||||
---@return boolean
|
||||
local function isFlatAngle(p1, p2, p3)
|
||||
local pos1 = p1:getPeakPos()
|
||||
local pos2 = p2:getPeakPos()
|
||||
local pos3 = p3:getPeakPos()
|
||||
local pos1 = p1.pos
|
||||
local pos2 = p2.pos
|
||||
local pos3 = p3.pos
|
||||
|
||||
local vec1 = pos1 - pos2 -- Vector from p2 to p1
|
||||
local vec2 = pos3 - pos2 -- Vector from p2 to p3
|
||||
@@ -67,12 +67,12 @@ end
|
||||
---@field p1 MapGen.Peak
|
||||
---@field p2 MapGen.Peak
|
||||
---@field p3 MapGen.Peak
|
||||
---@field e1 MapGen.Triangulation.Edge
|
||||
---@field e2 MapGen.Triangulation.Edge
|
||||
---@field e3 MapGen.Triangulation.Edge
|
||||
---@field h1 MapGen.Triangulation.Edge
|
||||
---@field h2 MapGen.Triangulation.Edge
|
||||
---@field h3 MapGen.Triangulation.Edge
|
||||
---@field e1 MapGen.Edge
|
||||
---@field e2 MapGen.Edge
|
||||
---@field e3 MapGen.Edge
|
||||
---@field h1 MapGen.Edge
|
||||
---@field h2 MapGen.Edge
|
||||
---@field h3 MapGen.Edge
|
||||
---@field neighboursIDs integer[]
|
||||
---@field neighbours MapGen.Triangle[]
|
||||
---@field color ColorString?
|
||||
@@ -91,7 +91,7 @@ function Triangle:new(id, p1, p2, p3, neighboursIDs)
|
||||
---@type ColorString?
|
||||
local color = nil
|
||||
|
||||
local c1, c2, c3 = p1.getColor(), p2.getColor(), p3.getColor()
|
||||
local c1, c2, c3 = p1.color, p2.color, p3.color
|
||||
|
||||
if c1 == c2 and c2 == c3 and c3 == c1 then
|
||||
color = c1
|
||||
@@ -123,9 +123,9 @@ end
|
||||
---@return string
|
||||
function Triangle:toString()
|
||||
return ('Triangle: \n %s\n %s\n %s'):format(
|
||||
tostring(self.p1.getPeakPos()),
|
||||
tostring(self.p2.getPeakPos()),
|
||||
tostring(self.p3.getPeakPos())
|
||||
tostring(self.p1.pos),
|
||||
tostring(self.p2.pos),
|
||||
tostring(self.p3.pos)
|
||||
)
|
||||
end
|
||||
|
||||
@@ -148,9 +148,9 @@ end
|
||||
---Returns the coordinates of the triangle's center.
|
||||
---@return vector
|
||||
function Triangle:getCenter()
|
||||
local pos1 = self.p1:getPeakPos()
|
||||
local pos2 = self.p2:getPeakPos()
|
||||
local pos3 = self.p3:getPeakPos()
|
||||
local pos1 = self.p1.pos
|
||||
local pos2 = self.p2.pos
|
||||
local pos3 = self.p3.pos
|
||||
|
||||
return (pos1 + pos2 + pos3) / 3
|
||||
end
|
||||
@@ -167,11 +167,11 @@ end
|
||||
---@param p1 MapGen.Peak
|
||||
---@param p2 MapGen.Peak
|
||||
---@param p3 MapGen.Peak
|
||||
---@return MapGen.Triangulation.Edge
|
||||
---@return MapGen.Edge
|
||||
function Triangle:getHeight(p1, p2, p3)
|
||||
local pos1 = p1:getPeakPos()
|
||||
local pos2 = p2:getPeakPos()
|
||||
local pos3 = p3:getPeakPos()
|
||||
local pos1 = p1.pos
|
||||
local pos2 = p2.pos
|
||||
local pos3 = p3.pos
|
||||
|
||||
local v = pos3 - pos2
|
||||
---@type vector
|
||||
@@ -193,11 +193,11 @@ end
|
||||
---@param p1 MapGen.Peak
|
||||
---@param p2 MapGen.Peak
|
||||
---@param p3 MapGen.Peak
|
||||
---@return MapGen.Triangulation.Edge
|
||||
---@return MapGen.Edge
|
||||
function Triangle:getHeight2D(p1, p2, p3)
|
||||
local pos1 = p1:getPeakPos()
|
||||
local pos2 = p2:getPeakPos()
|
||||
local pos3 = p3:getPeakPos()
|
||||
local pos1 = p1.pos
|
||||
local pos2 = p2.pos
|
||||
local pos3 = p3.pos
|
||||
|
||||
pos1.y = 0
|
||||
pos2.y = 0
|
||||
@@ -228,9 +228,9 @@ end
|
||||
---that is, the calculation is performed on the projection of the triangle XZ.
|
||||
---@return number, number, number
|
||||
function Triangle:getSidesLength2D()
|
||||
local pos1 = self.p1:getPeakPos()
|
||||
local pos2 = self.p2:getPeakPos()
|
||||
local pos3 = self.p3:getPeakPos()
|
||||
local pos1 = self.p1.pos
|
||||
local pos2 = self.p2.pos
|
||||
local pos3 = self.p3.pos
|
||||
|
||||
pos1.y = 0
|
||||
pos2.y = 0
|
||||
@@ -245,9 +245,9 @@ end
|
||||
---that is, the calculation is performed on the projection of the triangle XZ.
|
||||
---@return vector
|
||||
function Triangle:getCircumCenter2D()
|
||||
local pos1 = self.p1:getPeakPos()
|
||||
local pos2 = self.p2:getPeakPos()
|
||||
local pos3 = self.p3:getPeakPos()
|
||||
local pos1 = self.p1.pos
|
||||
local pos2 = self.p2.pos
|
||||
local pos3 = self.p3.pos
|
||||
|
||||
local D = ( pos1.x * (pos2.z - pos3.z) +
|
||||
pos2.x * (pos3.z - pos1.z) +
|
||||
@@ -294,7 +294,7 @@ end
|
||||
---@param p MapGen.Peak
|
||||
---@return boolean
|
||||
function Triangle:inCircumCircle2D(p)
|
||||
local pos = p:getPeakPos()
|
||||
local pos = p.pos
|
||||
|
||||
local c, r = self:getCircumCircle2D()
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
-- World generation occurs in a separate thread (MapGen Environment).
|
||||
-- But for the main thread, the mapgenerator object is also initialized.
|
||||
|
||||
core.set_mapgen_setting('chunksize', '{x=1,y=5,z=1}', true)
|
||||
--core.set_mapgen_setting('chunksize', '{x=2,y=10,z=2}', true)
|
||||
|
||||
local mapGeneneratorRequire = Mod.getInfo('smc__core__map_generator').require
|
||||
|
||||
|
||||
@@ -64,26 +64,3 @@ function Smc.requireFormspec()
|
||||
return class
|
||||
end
|
||||
|
||||
---@return CraftSystem
|
||||
function Smc.requireCraftSystem()
|
||||
---@type CraftSystem
|
||||
local class = Mod.getInfo('smc__core__craft_system').require('CraftSystem')
|
||||
|
||||
return class
|
||||
end
|
||||
|
||||
---@return CraftSystem.PrimitiveCraftHandler
|
||||
function Smc.requireCraftSystemPrimitiveCraftHandler()
|
||||
---@type CraftSystem.PrimitiveCraftHandler
|
||||
local class = Mod.getInfo('smc__core__craft_system').require('CraftSystem.PrimitiveCraftHandler')
|
||||
|
||||
return class
|
||||
end
|
||||
|
||||
---@return CraftSystem.ImmersiveCraftHandler
|
||||
function Smc.requireCraftSystemImmersiveCraftHandler()
|
||||
---@type CraftSystem.ImmersiveCraftHandler
|
||||
local class = Mod.getInfo('smc__core__craft_system').require('CraftSystem.ImmersiveCraftHandler')
|
||||
|
||||
return class
|
||||
end
|
||||
|
||||
@@ -29,7 +29,7 @@ function Profiler:startTimer()
|
||||
end
|
||||
|
||||
---Marks the end time of the timer.
|
||||
function Profiler:endTimer()
|
||||
function Profiler:stopTimer()
|
||||
self.endTime = getUsTime()
|
||||
|
||||
self.deltaTime = self.endTime - self.startTime
|
||||
@@ -50,6 +50,11 @@ function Profiler:getDeltaTime(units)
|
||||
end
|
||||
|
||||
---Displays the time difference in the chat using `core.log`.
|
||||
function Profiler:logDeltaTime()
|
||||
core.log(('DeltaTime[%s]: %.3f'):format(self:getDeltaTime()))
|
||||
---@param min number?
|
||||
function Profiler:logDeltaTime(min)
|
||||
local delta = self:getDeltaTime()
|
||||
|
||||
if delta >= (min or 0) then
|
||||
core.log(('DeltaTime[%s]: %.3f'):format(self.name, delta))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,83 +1,23 @@
|
||||
local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
|
||||
|
||||
local require = Mod.getInfo('smc__ui__player_inventory').require
|
||||
|
||||
---@alias PlayerInventoryUI.PaneType 'left'|'right'
|
||||
---@alias PlayerInventoryUI.getPaneFunc fun(self:PlayerInventoryUI,playerName:string,paneType:PlayerInventoryUI.PaneType):Formspec
|
||||
|
||||
---@alias PlayerInventoryUI.PaneName 'select_pane_menu'|'bag_pane'|'craft_pane'|'equip_pane'|'ref_pane'
|
||||
|
||||
---@type PlayerInventoryUI.getPaneFunc
|
||||
local getSelectPaneMenu = require('PlayerInventoryUI.getSelectPaneMenu')
|
||||
---@type PlayerInventoryUI.getPaneFunc
|
||||
local getBagPane = require('PlayerInventoryUI.getBagPane')
|
||||
---@type PlayerInventoryUI.getPaneFunc
|
||||
local getCraftPane = require('PlayerInventoryUI.getCraftPane')
|
||||
---@type PlayerInventoryUI.getPaneFunc
|
||||
local getEquipPane = require('PlayerInventoryUI.getEquipPane')
|
||||
---@type PlayerInventoryUI.getPaneFunc
|
||||
local getRefPane = require('PlayerInventoryUI.getRefPane')
|
||||
|
||||
---@type table<PlayerInventoryUI.PaneName, PlayerInventoryUI.getPaneFunc>
|
||||
local panes = {
|
||||
['select_pane_menu'] = getSelectPaneMenu,
|
||||
['bag_pane'] = getBagPane,
|
||||
['craft_pane'] = getCraftPane,
|
||||
['equip_pane'] = getEquipPane,
|
||||
['ref_pane'] = getRefPane,
|
||||
}
|
||||
|
||||
---@param paneName PlayerInventoryUI.PaneName
|
||||
local function getPane(paneName)
|
||||
if paneName == nil then
|
||||
---@type PlayerInventoryUI.PaneName
|
||||
paneName = 'select_pane_menu'
|
||||
end
|
||||
|
||||
local pane = panes[paneName]
|
||||
assert(pane ~= nil, ('The pane with the `%s` does not exist.'):format(paneName))
|
||||
|
||||
return pane
|
||||
end
|
||||
|
||||
---@param self PlayerInventoryUI
|
||||
---@param self BaseUI
|
||||
---@param playerName string
|
||||
---@return string
|
||||
local function getFormSpecString(self, playerName)
|
||||
---@type PlayerInventoryUI.PaneName
|
||||
local leftPaneName = self:getContex(playerName, 'left_pane_name') or 'select_pane_menu'
|
||||
---@type PlayerInventoryUI.PaneName
|
||||
local rightPaneName = self:getContex(playerName, 'right_pane_name') or 'select_pane_menu'
|
||||
|
||||
local leftPaneIsBlock = self:getContex(playerName, 'left_pane_is_block')
|
||||
local rightPaneIsBlock = self:getContex(playerName, 'right_pane_is_block')
|
||||
|
||||
local InventoryForm = Formspec:new({
|
||||
size = {x=22, y=12},
|
||||
position = {x=0.5, y=0.5},
|
||||
noPrepend = true,
|
||||
size = {x=24, y=8.5},
|
||||
position = {x=0.5, y=0.6},
|
||||
})
|
||||
|
||||
|
||||
InventoryForm:
|
||||
--bgcolor('#00000000'):
|
||||
listColors('#00000000', '#C4C4C433'):
|
||||
list('current_player', 'main', 5.5,9.5, 9,2):
|
||||
background(0, 9, 22, 3.5, 'player_inventory_middle_background.png', false):
|
||||
container(panes[leftPaneName](self, playerName, 'left'), 0, 0):
|
||||
container(panes[rightPaneName](self, playerName, 'right'), 11, 0):
|
||||
button(10.5, 0, .5, .5, 'close_left_pane', 'x'):
|
||||
button(21.5, 0, .5, .5, 'close_right_pane', 'x')
|
||||
list('current_player', 'main', 12,0.5, 9,2)
|
||||
|
||||
return InventoryForm:toString()
|
||||
end
|
||||
|
||||
---@alias PlayerInventoryUI.ContexKey 'left_pane_name'|'right_pane_name'|'left_pane_is_block'|'right_pane_is_block'
|
||||
|
||||
---@class PlayerInventoryUI : BaseUI
|
||||
---@field getFormSpecString fun(self:PlayerInventoryUI,playerName:string):string
|
||||
---@field showForm fun(self:PlayerInventoryUI,playerName:string):string
|
||||
---@field getContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey):any
|
||||
---@field getFormSpecString fun(self:BaseUI,playerName:string):string
|
||||
---@field showForm fun(self:BaseUI,playerName:string):string
|
||||
local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false)
|
||||
|
||||
--[[
|
||||
@@ -97,41 +37,4 @@ function PlayerInventoryUI:closeForm(playerName)
|
||||
core.close_formspec(playerName, '')
|
||||
end--]]
|
||||
|
||||
PlayerInventoryUI:registerOnFormFieldAction('close_left_pane', function(player, fieldName, fieldValue)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'left_pane_name', 'select_pane_menu')
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('select_left_%S+', function(player, fieldName, fieldValue)
|
||||
local paneName = string.match(fieldName, 'select_%S+_(%S+_%S+)')
|
||||
assert(paneName ~= nil)
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'left_pane_name', paneName)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||
end)
|
||||
|
||||
PlayerInventoryUI:registerOnFormFieldAction('close_right_pane', function(player, fieldName, fieldValue)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'right_pane_name', 'select_pane_menu')
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||
end)
|
||||
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('select_right_%S+', function(player, fieldName, fieldValue)
|
||||
local paneName = string.match(fieldName, 'select_%S+_(%S+_%S+)')
|
||||
assert(paneName ~= nil)
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'right_pane_name', paneName)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||
end)
|
||||
|
||||
return PlayerInventoryUI
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
local Formspec = Smc.requireFormspec()
|
||||
|
||||
local BagPane = Formspec:new()
|
||||
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@param paneType PlayerInventoryUI.PaneType
|
||||
---@return Formspec
|
||||
local function getBagPane(self, playerName, paneType)
|
||||
BagPane:
|
||||
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#BF59EE:120'):
|
||||
label(2,2, 'BAG PANE')
|
||||
|
||||
return BagPane
|
||||
end
|
||||
|
||||
return getBagPane
|
||||
@@ -1,17 +0,0 @@
|
||||
local Formspec = Smc.requireFormspec()
|
||||
|
||||
local CraftPane = Formspec:new()
|
||||
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@param paneType PlayerInventoryUI.PaneType
|
||||
---@return Formspec
|
||||
local function getCraftPane(self, playerName, paneType)
|
||||
CraftPane:
|
||||
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#E7CC35:120'):
|
||||
label(2,2, 'CRAFTING PANE')
|
||||
|
||||
return CraftPane
|
||||
end
|
||||
|
||||
return getCraftPane
|
||||
@@ -1,17 +0,0 @@
|
||||
local Formspec = Smc.requireFormspec()
|
||||
|
||||
local EquipPane = Formspec:new()
|
||||
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@param paneType PlayerInventoryUI.PaneType
|
||||
---@return Formspec
|
||||
local function getEquipPane(self, playerName, paneType)
|
||||
EquipPane:
|
||||
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#72FAC6:120'):
|
||||
label(2,2, 'EQUIPMENT PANE')
|
||||
|
||||
return EquipPane
|
||||
end
|
||||
|
||||
return getEquipPane
|
||||
@@ -1,17 +0,0 @@
|
||||
local Formspec = Smc.requireFormspec()
|
||||
|
||||
local RefPane = Formspec:new()
|
||||
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@param paneType PlayerInventoryUI.PaneType
|
||||
---@return Formspec
|
||||
local function getRefPane(self, playerName, paneType)
|
||||
RefPane:
|
||||
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#8072FA:120'):
|
||||
label(2,2, 'REFERENCE PANE')
|
||||
|
||||
return RefPane
|
||||
end
|
||||
|
||||
return getRefPane
|
||||
@@ -1,21 +0,0 @@
|
||||
local Formspec = Smc.requireFormspec()
|
||||
|
||||
local selectPaneMenu = Formspec:new()
|
||||
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@param paneType PlayerInventoryUI.PaneType
|
||||
---@return Formspec
|
||||
local function getSelectPaneMenu(self, playerName, paneType)
|
||||
|
||||
selectPaneMenu:
|
||||
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#FA8072:120'):
|
||||
button(3.5,2.5,2,2,'select_'..paneType..'_bag_pane', 'bag'):
|
||||
button(5.5,2.5,2,2,'select_'..paneType..'_craft_pane', 'craft'):
|
||||
button(3.5,4.5,2,2,'select_'..paneType..'_equip_pane', 'equip'):
|
||||
button(5.5,4.5,2,2,'select_'..paneType..'_ref_pane', 'ref')
|
||||
|
||||
return selectPaneMenu
|
||||
end
|
||||
|
||||
return getSelectPaneMenu
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 266 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 199 B |
Binary file not shown.
|
Before Width: | Height: | Size: 77 KiB |
Reference in New Issue
Block a user