Skip to content

Commit

Permalink
Try fixing render distance/Chunk Slice bugs...
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin452 committed Feb 27, 2024
1 parent 7fab2a7 commit 67dad83
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 84 deletions.
51 changes: 22 additions & 29 deletions src/world/chunk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -337,44 +337,34 @@ function NewChunk(x, z)
-- update slices that were flagged in the previous step
for i = 1, WorldHeight / SliceHeight do
if sliceUpdates[i][1] then
self.slices[i]:updateModel()

if sliceUpdates[i][2] then
local chunk = GetChunkRaw(self.x - 1, self.z)
if chunk then
local neighborSlice = chunk.slices[i]
if neighborSlice then
neighborSlice:updateModel()
if self.slices[i] then
self.slices[i]:updateModel()

if sliceUpdates[i][2] then
local chunk = GetChunkRaw(self.x - 1, self.z)
if chunk and chunk.slices[i] then
chunk.slices[i]:updateModel()
end
end
end

if sliceUpdates[i][3] then
local chunk = GetChunkRaw(self.x + 1, self.z)
if chunk then
local neighborSlice = chunk.slices[i]
if neighborSlice then
neighborSlice:updateModel()
if sliceUpdates[i][3] then
local chunk = GetChunkRaw(self.x + 1, self.z)
if chunk and chunk.slices[i] then
chunk.slices[i]:updateModel()
end
end
end

if sliceUpdates[i][4] or sliceUpdates[i][5] then
local chunk = GetChunkRaw(self.x, self.z - 1)
if chunk then
local neighborSlice = chunk.slices[i]
if neighborSlice then
neighborSlice:updateModel()
if sliceUpdates[i][4] or sliceUpdates[i][5] then
local chunk = GetChunkRaw(self.x, self.z - 1)
if chunk and chunk.slices[i] then
chunk.slices[i]:updateModel()
end
end
end

if sliceUpdates[i][4] or sliceUpdates[i][5] then
local chunk = GetChunkRaw(self.x, self.z + 1)
if chunk then
local neighborSlice = chunk.slices[i]
if neighborSlice then
neighborSlice:updateModel()
if sliceUpdates[i][4] or sliceUpdates[i][5] then
local chunk = GetChunkRaw(self.x, self.z + 1)
if chunk and chunk.slices[i] then
chunk.slices[i]:updateModel()
end
end
end
Expand Down Expand Up @@ -403,6 +393,9 @@ function NewChunkSlice(x, y, z, parent)
t:assignModel(compmodel)
t.active = true
t.updateModel = function(self)
if not self or not self.parent or not self.model then
return
end
local model = {}

for i = 1, ChunkSize do
Expand Down
156 changes: 101 additions & 55 deletions src/world/updatelogic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,89 @@ function UpdateGame(dt)

-- Generate and update chunks within render distance
local renderChunks = {}
table.remove(renderChunks)

for i = math.floor(playerX / ChunkSize) - RenderDistance / ChunkSize, math.floor(playerX / ChunkSize) + RenderDistance / ChunkSize do
for j = math.floor(playerZ / ChunkSize) - RenderDistance / ChunkSize, math.floor(playerZ / ChunkSize) + RenderDistance / ChunkSize do
local chunk = ChunkHashTable[ChunkHash(i)] and ChunkHashTable[ChunkHash(i)][ChunkHash(j)]

if not chunk then
chunk = NewChunk(i, j)
ChunkSet[chunk] = true
ChunkHashTable[ChunkHash(i)] = ChunkHashTable[ChunkHash(i)] or {}
ChunkHashTable[ChunkHash(i)][ChunkHash(j)] = chunk
UpdateCaves()
LuaCraftPrintLoggingNormal("Generated chunk with coordinates:", i, j)
end

local mx, y, mz = chunk.x, chunk.y, chunk.z
local dx, dy, dz = playerX - mx, playerY - y, playerZ - mz

-- Calculate Euclidean distance
local distance = math.sqrt(dx * dx + dy * dy + dz * dz)

if distance < RenderDistance then
if not chunk.isInitialLightningInititalized then
chunk:sunlight()
chunk.isInitialLightningInititalized = true
elseif not chunk.isPopulated then
chunk:populate()
chunk.isPopulated = true
elseif not chunk.isInitialized then
chunk.changes = {}
chunk:processRequests()
for i = 1, WorldHeight / SliceHeight do
local sliceY = chunk.y + (i - 1) * SliceHeight + 1
print("Creating slice at Y:", sliceY)
chunk.slices[i] = NewChunkSlice(chunk.x, sliceY, chunk.z, chunk)
end
chunk.isInitialized = true
--table.remove(renderChunks)

local playerChunkX = math.floor(playerX / ChunkSize)
local playerChunkZ = math.floor(playerZ / ChunkSize)

for distance = 0, RenderDistance / ChunkSize do
for i = -distance, distance do
for j = -distance, distance do
local chunkX = playerChunkX + i
local chunkZ = playerChunkZ + j

local chunk = ChunkHashTable[ChunkHash(chunkX)]
and ChunkHashTable[ChunkHash(chunkX)][ChunkHash(chunkZ)]

if not chunk then
chunk = NewChunk(chunkX, chunkZ)
ChunkSet[chunk] = true
ChunkHashTable[ChunkHash(chunkX)] = ChunkHashTable[ChunkHash(chunkX)] or {}
ChunkHashTable[ChunkHash(chunkX)][ChunkHash(chunkZ)] = chunk
UpdateCaves()
LuaCraftPrintLoggingNormal("Generated chunk with coordinates:", chunkX, chunkZ)
end
chunk.active = true
for _, chunkSlice in ipairs(chunk.slices) do
chunkSlice.active = true

local mx, y, mz = chunk.x, chunk.y, chunk.z
local dx, dy, dz = playerX - mx, playerY - y, playerZ - mz

-- Calculate Euclidean distance
local chunkDistance = math.sqrt(dx * dx + dy * dy + dz * dz)

if chunkDistance < RenderDistance then
if not chunk.isInitialLightningInititalized then
chunk:sunlight()
chunk.isInitialLightningInititalized = true
elseif not chunk.isPopulated then
chunk:populate()
chunk.isPopulated = true
elseif not chunk.isInitialized then
chunk.changes = {}

-- Populate the chunk before initializing slices
for sliceIndex = 1, WorldHeight / SliceHeight do
if not chunk.slices[sliceIndex] then
local sliceY = chunk.y + (sliceIndex - 1) * SliceHeight + 1
chunk.slices[sliceIndex] = NewChunkSlice(chunk.x, sliceY, chunk.z, chunk)
end
end

chunk:processRequests()
for _, chunkSlice in ipairs(chunk.slices) do
renderChunkSlice(chunkSlice, ThePlayer.x, ThePlayer.y, ThePlayer.z)
end
chunk.isInitialized = true
end

chunk.active = true
for i = 1, #chunk.slices do
-- LuaCraftPrintLoggingNormal("test2")
local chunkSlice = chunk.slices[i]
chunkSlice.active = true
end
else
chunk.active = false
for i = 1, #chunk.slices do
--LuaCraftPrintLoggingNormal("test2")
local chunkSlice = chunk.slices[i]
chunkSlice.active = false
end

--it seem that table.remove hre is doesn't work
for j = #renderChunks, 1, -1 do
if not renderChunks[j].active then
table.remove(renderChunks, j)
end
end


-- LuaCraftPrintLoggingNormal("Chunk at coordinates (", chunk.x, ",", chunk.z, ") is inactive")
end
else
chunk.active = false
for _, chunkSlice in ipairs(chunk.slices) do
chunkSlice.active = false

if not isInTable(renderChunks, chunk) then
table.insert(renderChunks, chunk)
end
end

table.insert(renderChunks, chunk)
end
end

Expand All @@ -78,19 +111,25 @@ function UpdateGame(dt)
if #chunk.changes > 0 then
chunk:updateModel()
end

for _, chunkSlice in ipairs(chunk.slices) do
if chunkSlice.active then
if not chunkSlice.alreadyrendered then
-- renderChunkSlice(chunkSlice, playerX, playerY, playerZ)
end
else
if chunkSlice.active == false then
-- Remove the inactive ChunkSlice
LuaCraftPrintLoggingNormal(
"Removed chunkSlice at coordinates: "
.. chunkSlice.x
.. ", "
.. chunkSlice.y
.. ", "
.. chunkSlice.z
)
table.remove(chunk.slices, i)
chunkSlice.alreadyrendered = false
end
end
end
end

LogicAccumulator = LogicAccumulator + dt

-- update all things in ThingList update queue
Expand All @@ -112,9 +151,16 @@ function UpdateGame(dt)
end
end
end

function isInTable(tbl, value)
for _, v in ipairs(tbl) do
if v == value then
return true
end
end
return false
end
function renderChunkSlice(chunkSlice, playerX, playerY, playerZ)
print("Rendering chunk slice:", chunkSlice.x, chunkSlice.y, chunkSlice.z)
--LuaCraftPrintLoggingNormal("Rendering chunk slice:", chunkSlice.x, chunkSlice.y, chunkSlice.z)
local model = {}

for i = 1, ChunkSize do
Expand Down

0 comments on commit 67dad83

Please sign in to comment.