chore: refractor
This commit is contained in:
108
ServerScriptService/Actor/ServerChunkManager/TerrainGen/init.lua
Normal file
108
ServerScriptService/Actor/ServerChunkManager/TerrainGen/init.lua
Normal file
@@ -0,0 +1,108 @@
|
||||
--!native
|
||||
--!optimize 2
|
||||
|
||||
local TerrainGen = {}
|
||||
|
||||
local ChunkManager = require(game:GetService("ReplicatedStorage"):WaitForChild("Shared").ChunkManager)
|
||||
local Chunk = require(game:GetService("ReplicatedStorage"):WaitForChild("Shared").ChunkManager.Chunk)
|
||||
|
||||
TerrainGen.ServerChunkCache = {} :: {[typeof("")]: typeof(Chunk.new(0,0,0))}
|
||||
|
||||
local function chunkKeyFromCoords(x: number, y: number, z: number): string
|
||||
return `{x},{y},{z}`
|
||||
end
|
||||
|
||||
function TerrainGen:UnloadAllChunks(): number
|
||||
local count = 0
|
||||
for key in pairs(TerrainGen.ServerChunkCache) do
|
||||
TerrainGen.ServerChunkCache[key] = nil
|
||||
count += 1
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
local function worldToChunkCoord(v: number): number
|
||||
return math.floor((v + 16) / 32)
|
||||
end
|
||||
|
||||
function TerrainGen:PreloadNearPlayers(radius: number, yRadius: number?): number
|
||||
local Players = game:GetService("Players")
|
||||
local r = radius or 5
|
||||
local ry = yRadius or 1
|
||||
local loaded = 0
|
||||
for _, player in ipairs(Players:GetPlayers()) do
|
||||
local character = player.Character
|
||||
local root = character and character:FindFirstChild("HumanoidRootPart")
|
||||
if root then
|
||||
local pos = root.Position
|
||||
local cx = worldToChunkCoord(pos.X)
|
||||
local cy = worldToChunkCoord(pos.Y)
|
||||
local cz = worldToChunkCoord(pos.Z)
|
||||
for y = -ry, ry do
|
||||
for x = -r, r do
|
||||
for z = -r, r do
|
||||
TerrainGen:GetChunk(cx + x, cy + y, cz + z)
|
||||
loaded += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return loaded
|
||||
end
|
||||
|
||||
-- Load a chunk from the DataStore or generate it if not found
|
||||
function TerrainGen:GetChunk(x, y, z)
|
||||
local key = chunkKeyFromCoords(x, y, z)
|
||||
if TerrainGen.ServerChunkCache[key] then
|
||||
return TerrainGen.ServerChunkCache[key]
|
||||
end
|
||||
|
||||
-- Generate a new chunk if it doesn't exist
|
||||
local chunk = Chunk.new(x, y, z)
|
||||
if y == 1 then
|
||||
for cx = 1, 8 do
|
||||
for cz = 1, 8 do
|
||||
--local perlin = math.noise(((x*8)+cx)/100,((z*8)+cz)/100)
|
||||
chunk:CreateBlock(cx, 1, cz, { id = 1, state = {} })
|
||||
--chunk:CreateBlock(x, 2, z, { id = 1, state = {} })
|
||||
end
|
||||
end
|
||||
end
|
||||
if y == 0 then
|
||||
for cx = 1, 8 do
|
||||
for cy = 1, 8 do
|
||||
for cz = 1, 8 do
|
||||
--local perlin = math.noise(((x*8)+cx)/100,((z*8)+cz)/100)
|
||||
chunk:CreateBlock(cx, cy, cz, { id = 2, state = {} })
|
||||
--chunk:CreateBlock(x, 2, z, { id = 1, state = {} })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
TerrainGen.ServerChunkCache[key] = chunk
|
||||
return chunk
|
||||
end
|
||||
|
||||
-- Fake Chunk
|
||||
function TerrainGen:GetFakeChunk(x, y, z)
|
||||
|
||||
-- Generate a new chunk if it doesn't exist
|
||||
local chunk = Chunk.new(x, y, z)
|
||||
for cy = 1,8 do
|
||||
for cx = 1, 8 do
|
||||
for cz = 1, 8 do
|
||||
--local perlin = math.noise(((x*8)+cx)/100,((z*8)+cz)/100)
|
||||
chunk:CreateBlock(cx, cy, cz, { id = -2, state = {} })
|
||||
--chunk:CreateBlock(x, 2, z, { id = 1, state = {} })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return chunk
|
||||
end
|
||||
|
||||
TerrainGen.CM = ChunkManager
|
||||
|
||||
return TerrainGen
|
||||
Reference in New Issue
Block a user