core: chatgpt optimizations
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
place.rbxl.lock
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
292
|
|
||||||
RobloxStudioBeta
|
|
||||||
CACHYOS
|
|
||||||
3ad4fa05-1765-4ae8-a484-86d03d176c98
|
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ function Chunk:DoesNeighboringBlockExist(rx, ry, rz, gx, gy, gz, offsetX, offset
|
|||||||
-- Get the neighboring chunk
|
-- Get the neighboring chunk
|
||||||
local neighborChunk = Chunk.AllChunks[`{neighborGX},{neighborGY},{neighborGZ}`]
|
local neighborChunk = Chunk.AllChunks[`{neighborGX},{neighborGY},{neighborGZ}`]
|
||||||
if not neighborChunk then
|
if not neighborChunk then
|
||||||
return true -- Assume block exists in an unloaded chunk
|
return false -- Treat unloaded chunks as empty so edges render
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if the block exists in the neighboring chunk
|
-- Check if the block exists in the neighboring chunk
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ local RunService = game:GetService("RunService")
|
|||||||
|
|
||||||
local ChunkBorderFolder = game:GetService("Workspace"):FindFirstChildOfClass("Terrain")
|
local ChunkBorderFolder = game:GetService("Workspace"):FindFirstChildOfClass("Terrain")
|
||||||
|
|
||||||
|
local NEIGHBOR_OFFSETS = {
|
||||||
|
{-1, 0, 0}, {1, 0, 0},
|
||||||
|
{0, -1, 0}, {0, 1, 0},
|
||||||
|
{0, 0, -1}, {0, 0, 1}
|
||||||
|
}
|
||||||
|
|
||||||
local function placeBorder(a,b,c)
|
local function placeBorder(a,b,c)
|
||||||
local pos = util.ChunkPosToCFrame(Vector3.new(a,b,c),Vector3.new(1,1,1)).Position - Vector3.new(2,2,2)
|
local pos = util.ChunkPosToCFrame(Vector3.new(a,b,c),Vector3.new(1,1,1)).Position - Vector3.new(2,2,2)
|
||||||
local d = objects.ChunkLoading:Clone()
|
local d = objects.ChunkLoading:Clone()
|
||||||
@@ -71,7 +77,7 @@ function ChunkBuilder:BuildChunk(c: typeof(Chunk.new(0,0,0)),parent: Instance?)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
task.synchronize()
|
task.synchronize()
|
||||||
for _, o in pairs({{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}}) do
|
for _, o in pairs(NEIGHBOR_OFFSETS) do
|
||||||
--warn("propogate",o[1],o[2],o[3])
|
--warn("propogate",o[1],o[2],o[3])
|
||||||
-- Adjust for chunk boundaries
|
-- Adjust for chunk boundaries
|
||||||
local b = {x = x + o[1], y = y + o[2], z = z + o[3]}
|
local b = {x = x + o[1], y = y + o[2], z = z + o[3]}
|
||||||
@@ -88,25 +94,27 @@ function ChunkBuilder:BuildChunk(c: typeof(Chunk.new(0,0,0)),parent: Instance?)
|
|||||||
--BlockManager:GetBlock(ch.x)
|
--BlockManager:GetBlock(ch.x)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local blockName = `{x},{y},{z}`
|
||||||
|
local existing = ch:FindFirstChild(blockName)
|
||||||
if d == 0 then
|
if d == 0 then
|
||||||
if ch:FindFirstChild(`{x},{y},{z}`) then
|
if existing then
|
||||||
task.synchronize()
|
task.synchronize()
|
||||||
ch:FindFirstChild(`{x},{y},{z}`):Destroy()
|
existing:Destroy()
|
||||||
task.desynchronize()
|
task.desynchronize()
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if not c:IsBlockRenderable(x, y, z) then
|
if not c:IsBlockRenderable(x, y, z) then
|
||||||
if ch:FindFirstChild(`{x},{y},{z}`) then
|
if existing then
|
||||||
task.synchronize()
|
task.synchronize()
|
||||||
ch:FindFirstChild(`{x},{y},{z}`):Destroy()
|
existing:Destroy()
|
||||||
task.desynchronize()
|
task.desynchronize()
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if ch:FindFirstChild(`{x},{y},{z}`) then
|
if existing then
|
||||||
task.synchronize()
|
task.synchronize()
|
||||||
ch:FindFirstChild(`{x},{y},{z}`):Destroy()
|
existing:Destroy()
|
||||||
task.desynchronize()
|
task.desynchronize()
|
||||||
end
|
end
|
||||||
if not d then return end
|
if not d then return end
|
||||||
@@ -114,7 +122,7 @@ function ChunkBuilder:BuildChunk(c: typeof(Chunk.new(0,0,0)),parent: Instance?)
|
|||||||
local N = util.RotationStringToNormalId(d.state["r"] or "f")
|
local N = util.RotationStringToNormalId(d.state["r"] or "f")
|
||||||
task.synchronize()
|
task.synchronize()
|
||||||
local block = BlockManager:GetBlockRotated(d.id, N, d.state)
|
local block = BlockManager:GetBlockRotated(d.id, N, d.state)
|
||||||
block.Name = `{x},{y},{z}`
|
block.Name = blockName
|
||||||
block:PivotTo(util.ChunkPosToCFrame(c.pos, Vector3.new(x, y, z)))
|
block:PivotTo(util.ChunkPosToCFrame(c.pos, Vector3.new(x, y, z)))
|
||||||
block.Parent = ch
|
block.Parent = ch
|
||||||
task.desynchronize()
|
task.desynchronize()
|
||||||
@@ -180,36 +188,47 @@ function ChunkBuilder:BuildChunk(c: typeof(Chunk.new(0,0,0)),parent: Instance?)
|
|||||||
|
|
||||||
task.defer(function()
|
task.defer(function()
|
||||||
task.synchronize()
|
task.synchronize()
|
||||||
for a,b in pairs(newcache) do
|
for key, data in pairs(newcache) do
|
||||||
for _, o in pairs({{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}}) do
|
local coords = util.BlockPosStringToCoords(key)
|
||||||
|
for _, o in pairs(NEIGHBOR_OFFSETS) do
|
||||||
-- chunks are 8x8x8
|
-- chunks are 8x8x8
|
||||||
local b = {x=x+o[1],y=y+o[2],z=z+o[3]}
|
local nb = {x = coords.X + o[1], y = coords.Y + o[2], z = coords.Z + o[3]}
|
||||||
local ch = {x=c.pos.X,y=c.pos.Y,z=c.pos.Z}
|
local chCoords = {x = c.pos.X, y = c.pos.Y, z = c.pos.Z}
|
||||||
if b.x == 0 then ch.x = c.pos.X - 1 b.x = 8 end
|
if nb.x == 0 then chCoords.x = c.pos.X - 1 nb.x = 8 end
|
||||||
if b.x == 9 then ch.x = c.pos.X + 1 b.x = 1 end
|
if nb.x == 9 then chCoords.x = c.pos.X + 1 nb.x = 1 end
|
||||||
|
|
||||||
if b.y == 0 then ch.y = c.pos.Y - 1 b.y = 8 end
|
if nb.y == 0 then chCoords.y = c.pos.Y - 1 nb.y = 8 end
|
||||||
if b.y == 9 then ch.y = c.pos.Y +1 b.y = 1 end
|
if nb.y == 9 then chCoords.y = c.pos.Y + 1 nb.y = 1 end
|
||||||
|
|
||||||
if b.z == 0 then ch.z = c.pos.Z - 1 b.z = 8 end
|
if nb.z == 0 then chCoords.z = c.pos.Z - 1 nb.z = 8 end
|
||||||
if b.z == 9 then ch.z = c.pos.Z + 1 b.z = 1 end
|
if nb.z == 9 then chCoords.z = c.pos.Z + 1 nb.z = 1 end
|
||||||
|
|
||||||
propogateNeighboringBlockChanges(ch.x,ch.y,ch.z,b.x,b.y,b.z)
|
propogateNeighboringBlockChanges(chCoords.x, chCoords.y, chCoords.z, nb.x, nb.y, nb.z)
|
||||||
end
|
end
|
||||||
local coords = util.BlockPosStringToCoords(a)
|
|
||||||
if not c:IsBlockRenderable(coords.X, coords.Y, coords.Z) then
|
local existing = ch:FindFirstChild(key)
|
||||||
if ch:FindFirstChild(a) then
|
if data == 0 or (data and data.id == 0) then
|
||||||
ch:FindFirstChild(a):Destroy()
|
if existing then
|
||||||
|
existing:Destroy()
|
||||||
end
|
end
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
if ch:FindFirstChild(a) then
|
if not c:IsBlockRenderable(coords.X, coords.Y, coords.Z) then
|
||||||
ch:FindFirstChild(a):Destroy()
|
if existing then
|
||||||
|
existing:Destroy()
|
||||||
|
end
|
||||||
|
continue
|
||||||
end
|
end
|
||||||
local N = util.RotationStringToNormalId(b.state["r"] or "f")
|
if existing then
|
||||||
local block = BlockManager:GetBlockRotated(b.id,N,b.state)
|
existing:Destroy()
|
||||||
block.Name = a
|
end
|
||||||
block:PivotTo(util.ChunkPosToCFrame(c.pos,coords))
|
if not data then
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
local N = util.RotationStringToNormalId(data.state["r"] or "f")
|
||||||
|
local block = BlockManager:GetBlockRotated(data.id, N, data.state)
|
||||||
|
block.Name = key
|
||||||
|
block:PivotTo(util.ChunkPosToCFrame(c.pos, coords))
|
||||||
block.Parent = ch
|
block.Parent = ch
|
||||||
end
|
end
|
||||||
newcache = nil
|
newcache = nil
|
||||||
|
|||||||
@@ -15,11 +15,27 @@ ChunkFolder.Name = "$blockscraft_client"
|
|||||||
ChunkManager.ChunkFolder = ChunkFolder
|
ChunkManager.ChunkFolder = ChunkFolder
|
||||||
|
|
||||||
local CHUNK_RADIUS = 5
|
local CHUNK_RADIUS = 5
|
||||||
|
local LOAD_BATCH = 8
|
||||||
local FORCELOAD_CHUNKS = {
|
local FORCELOAD_CHUNKS = {
|
||||||
{0, 1, 0}
|
{0, 1, 0}
|
||||||
}
|
}
|
||||||
|
|
||||||
local unloadingChunks = {}
|
local unloadingChunks = {}
|
||||||
|
local pendingChunkRequests = {}
|
||||||
|
|
||||||
|
local CHUNK_OFFSETS = {}
|
||||||
|
do
|
||||||
|
for y = -CHUNK_RADIUS, CHUNK_RADIUS do
|
||||||
|
for x = -CHUNK_RADIUS, CHUNK_RADIUS do
|
||||||
|
for z = -CHUNK_RADIUS, CHUNK_RADIUS do
|
||||||
|
table.insert(CHUNK_OFFSETS, {x, y, z, (x * x) + (y * y) + (z * z)})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.sort(CHUNK_OFFSETS, function(a, b)
|
||||||
|
return a[4] < b[4]
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
local function Swait(l)
|
local function Swait(l)
|
||||||
task.synchronize()
|
task.synchronize()
|
||||||
@@ -34,12 +50,26 @@ function ChunkManager:GetChunk(x, y, z)
|
|||||||
return Chunk.AllChunks[key]
|
return Chunk.AllChunks[key]
|
||||||
end
|
end
|
||||||
|
|
||||||
task.synchronize()
|
if pendingChunkRequests[key] then
|
||||||
local data = remote:InvokeServer(x, y, z)
|
task.synchronize()
|
||||||
task.desynchronize()
|
while pendingChunkRequests[key] do
|
||||||
|
task.wait()
|
||||||
|
end
|
||||||
|
return Chunk.AllChunks[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
task.synchronize()
|
||||||
|
pendingChunkRequests[key] = true
|
||||||
|
local ok, data = pcall(function()
|
||||||
|
return remote:InvokeServer(x, y, z)
|
||||||
|
end)
|
||||||
|
if not ok then
|
||||||
|
data = {}
|
||||||
|
end
|
||||||
|
task.synchronize()
|
||||||
local ch = Chunk.from(x, y, z, data)
|
local ch = Chunk.from(x, y, z, data)
|
||||||
Chunk.AllChunks[key] = ch
|
Chunk.AllChunks[key] = ch
|
||||||
|
pendingChunkRequests[key] = nil
|
||||||
return ch
|
return ch
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -116,24 +146,18 @@ function ChunkManager:Tick()
|
|||||||
}
|
}
|
||||||
|
|
||||||
task.defer(function()
|
task.defer(function()
|
||||||
for y = -CHUNK_RADIUS, CHUNK_RADIUS do
|
local processed = 0
|
||||||
task.defer(function()
|
for _, offset in ipairs(CHUNK_OFFSETS) do
|
||||||
for x = -CHUNK_RADIUS, CHUNK_RADIUS do
|
local cx, cy, cz = chunkPos.x + offset[1], chunkPos.y + offset[2], chunkPos.z + offset[3]
|
||||||
task.desynchronize()
|
local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||||
for z = -CHUNK_RADIUS, CHUNK_RADIUS do
|
chunk.inhabitedTime = tick()
|
||||||
local cx, cy, cz = chunkPos.x + x, chunkPos.y + y, chunkPos.z + z
|
if not chunk.loaded then
|
||||||
local key = `{cx},{cy},{cz}`
|
ChunkManager:LoadChunk(cx, cy, cz)
|
||||||
local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
processed += 1
|
||||||
chunk.inhabitedTime = tick()
|
if processed % LOAD_BATCH == 0 then
|
||||||
if not chunk.loaded then
|
Swait(1)
|
||||||
ChunkManager:LoadChunk(cx, cy, cz)
|
|
||||||
Swait(2)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
task.synchronize()
|
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
Swait(10)
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user