core: fix building

This commit is contained in:
2026-01-08 19:30:49 +02:00
committed by Tangled
parent 165913ca51
commit b34830a493
9 changed files with 594 additions and 83 deletions

View File

@@ -31,6 +31,16 @@ local FORCELOAD_CHUNKS = {
local unloadingChunks = {}
local pendingChunkRequests = {}
local lastChunkKey: string? = nil
local lastHeavyTick = 0
local HEAVY_TICK_INTERVAL = 1.5
local lastUnloadSweep = 0
local UNLOAD_SWEEP_INTERVAL = 1.5
local function worldToChunkCoord(v: number): number
return math.floor((v + 16) / 32)
end
local CHUNK_OFFSETS = {}
do
for y = -CHUNK_RADIUS, CHUNK_RADIUS do
@@ -45,6 +55,22 @@ do
end)
end
function ChunkManager:UnloadAllNow()
for key, chunk in pairs(Chunk.AllChunks) do
unloadingChunks[key] = true
pcall(function()
if chunk.loaded then
chunk:UnloadImmediate()
end
end)
pcall(function()
chunk:Destroy()
end)
Chunk.AllChunks[key] = nil
unloadingChunks[key] = nil
end
end
local function Swait(l)
for _ = 1, l do
RunService.Stepped:Wait()
@@ -232,26 +258,40 @@ function ChunkManager:Tick()
local pos = player.Character:GetPivot().Position
local chunkPos = {
x = math.round(pos.X / 32),
y = math.round(pos.Y / 32),
z = math.round(pos.Z / 32)
x = worldToChunkCoord(pos.X),
y = worldToChunkCoord(pos.Y),
z = worldToChunkCoord(pos.Z)
}
local ck = `{chunkPos.x},{chunkPos.y},{chunkPos.z}`
local now = tick()
local shouldHeavyTick = (ck ~= lastChunkKey) or (now - lastHeavyTick >= HEAVY_TICK_INTERVAL)
lastChunkKey = ck
if shouldHeavyTick then
lastHeavyTick = now
end
task.defer(function()
local processed = 0
for _, offset in ipairs(CHUNK_OFFSETS) do
local cx, cy, cz = chunkPos.x + offset[1], chunkPos.y + offset[2], chunkPos.z + offset[3]
local chunk = ChunkManager:GetChunk(cx, cy, cz)
chunk.inhabitedTime = tick()
if not chunk.loaded then
ChunkManager:LoadChunk(cx, cy, cz)
processed += 1
if processed % LOAD_BATCH == 0 then
Swait(1)
if shouldHeavyTick then
task.defer(function()
local processed = 0
for _, offset in ipairs(CHUNK_OFFSETS) do
local cx, cy, cz = chunkPos.x + offset[1], chunkPos.y + offset[2], chunkPos.z + offset[3]
local chunk = ChunkManager:GetChunk(cx, cy, cz)
chunk.inhabitedTime = now
if not chunk.loaded then
ChunkManager:LoadChunk(cx, cy, cz)
processed += 1
if processed % LOAD_BATCH == 0 then
Swait(1)
end
end
end
end)
else
local current = Chunk.AllChunks[ck]
if current then
current.inhabitedTime = now
end
end)
end
--[[
task.defer(function()
@@ -275,15 +315,18 @@ function ChunkManager:Tick()
end)
--]]
for key, loadedChunk in pairs(Chunk.AllChunks) do
if tick() - loadedChunk.inhabitedTime > 15 and not unloadingChunks[key] then
unloadingChunks[key] = true
task.defer(function()
loadedChunk:Unload()
loadedChunk:Destroy()
Chunk.AllChunks[key] = nil
unloadingChunks[key] = nil
end)
if now - lastUnloadSweep >= UNLOAD_SWEEP_INTERVAL then
lastUnloadSweep = now
for key, loadedChunk in pairs(Chunk.AllChunks) do
if now - loadedChunk.inhabitedTime > 15 and not unloadingChunks[key] then
unloadingChunks[key] = true
task.defer(function()
loadedChunk:Unload()
loadedChunk:Destroy()
Chunk.AllChunks[key] = nil
unloadingChunks[key] = nil
end)
end
end
end
end
@@ -295,9 +338,9 @@ function ChunkManager:ResyncAroundPlayer(radius: number)
end
local pos = player.Character:GetPivot().Position
local chunkPos = {
x = math.round(pos.X / 32),
y = math.round(pos.Y / 32),
z = math.round(pos.Z / 32)
x = worldToChunkCoord(pos.X),
y = worldToChunkCoord(pos.Y),
z = worldToChunkCoord(pos.Z)
}
for y = -radius, radius do
for x = -radius, radius do
@@ -327,6 +370,12 @@ function ChunkManager:Init()
ChunkFolder.Parent = game:GetService("Workspace")
ChunkManager:ForceTick()
tickremote.OnClientEvent:Connect(function(m)
if m == "U_ALL" then
ChunkManager:UnloadAllNow()
end
end)
task.defer(function()
while true do
wait(2)