Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions server/world/chunk/light.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package chunk

import (
"container/list"
"github.com/df-mc/dragonfly/server/block/cube"
)

// insertBlockLightNodes iterates over the chunk and looks for blocks that have a light level of at least 1.
// If one is found, a node is added for it to the node queue.
func (a *lightArea) insertBlockLightNodes(queue *list.List) {
func (a *lightArea) insertBlockLightNodes(queue *lightQueue) {
a.iterSubChunks(anyLightBlocks, func(pos cube.Pos) {
if level := a.highest(pos, LightBlocks); level > 0 {
queue.PushBack(node(pos, level, BlockLight))
queue.push(node(pos, level, BlockLight))
}
})
}
Expand All @@ -29,7 +28,7 @@ func anyLightBlocks(sub *SubChunk) bool {

// insertSkyLightNodes iterates over the chunk and inserts a light node anywhere at the highest block in the
// chunk. In addition, any skylight above those nodes will be set to 15.
func (a *lightArea) insertSkyLightNodes(queue *list.List) {
func (a *lightArea) insertSkyLightNodes(queue *lightQueue) {
a.iterHeightmap(func(x, z int, height, highestNeighbour, highestY, lowestY int) {
pos := cube.Pos{x, height, z}
if height <= a.r.Max() {
Expand All @@ -40,7 +39,7 @@ func (a *lightArea) insertSkyLightNodes(queue *list.List) {
if level := a.highest(pos.Sub(cube.Pos{0, 1}), FilteringBlocks); level != 15 && level != 0 {
// If we hit a block like water or leaves (something that diffuses but does not block light), we
// need a node above this block regardless of the neighbours.
queue.PushBack(node(pos, 15, SkyLight))
queue.push(node(pos, 15, SkyLight))
}
}
}
Expand All @@ -49,7 +48,7 @@ func (a *lightArea) insertSkyLightNodes(queue *list.List) {
// lower than the current one, on the same Y level, or one level higher, because light in
// this column can't spread below that anyway.
if pos[1]++; pos[1] < highestNeighbour {
queue.PushBack(node(pos, 15, SkyLight))
queue.push(node(pos, 15, SkyLight))
continue
}
// Fill the rest with full skylight.
Expand All @@ -60,17 +59,17 @@ func (a *lightArea) insertSkyLightNodes(queue *list.List) {

// insertLightSpreadingNodes inserts light nodes into the node queue passed which, when propagated, will
// spread into the neighbouring chunks.
func (a *lightArea) insertLightSpreadingNodes(queue *list.List, lt light) {
func (a *lightArea) insertLightSpreadingNodes(queue *lightQueue, lt light) {
a.iterEdges(a.nodesNeeded(lt), func(pa, pb cube.Pos) {
la, lb := a.light(pa, lt), a.light(pb, lt)
if la == lb || la-1 == lb || lb-1 == la {
// No chance for this to spread. Don't check for the highest filtering blocks on the side.
return
}
if filter := a.highest(pb, FilteringBlocks) + 1; la > filter && la-filter > lb {
queue.PushBack(node(pb, la-filter, lt))
queue.push(node(pb, la-filter, lt))
} else if filter = a.highest(pa, FilteringBlocks) + 1; lb > filter && lb-filter > la {
queue.PushBack(node(pa, lb-filter, lt))
queue.push(node(pa, lb-filter, lt))
}
})
}
Expand All @@ -91,19 +90,33 @@ func (a *lightArea) nodesNeeded(lt light) func(sa, sb *SubChunk) bool {

// propagate spreads the next light node in the node queue passed through the lightArea a. propagate adds the neighbours
// of the node to the queue for as long as it is able to spread.
func (a *lightArea) propagate(queue *list.List) {
n := queue.Remove(queue.Front()).(lightNode)
func (a *lightArea) propagate(queue *lightQueue) {
n, ok := queue.pop()
if !ok {
return
}
if a.light(n.pos, n.lt) >= n.level {
return
}
a.setLight(n.pos, n.lt, n.level)

for neighbour := range a.neighbours(n) {
filter := a.highest(neighbour.pos, FilteringBlocks) + 1
if n.level > filter && a.light(neighbour.pos, n.lt) < n.level-filter {
neighbour.level = n.level - filter
queue.PushBack(neighbour)
}
x, y, z := n.pos[0], n.pos[1], n.pos[2]
a.propagateNeighbour(queue, n.lt, n.level, x+1, y, z)
a.propagateNeighbour(queue, n.lt, n.level, x-1, y, z)
a.propagateNeighbour(queue, n.lt, n.level, x, y+1, z)
a.propagateNeighbour(queue, n.lt, n.level, x, y-1, z)
a.propagateNeighbour(queue, n.lt, n.level, x, y, z+1)
a.propagateNeighbour(queue, n.lt, n.level, x, y, z-1)
}

func (a *lightArea) propagateNeighbour(queue *lightQueue, lt light, level uint8, x, y, z int) {
if y < a.r.Min() || y > a.r.Max() || x < a.baseX || z < a.baseZ || x >= a.baseX+a.w*16 || z >= a.baseZ+a.w*16 {
return
}
pos := cube.Pos{x, y, z}
filter := a.highest(pos, FilteringBlocks) + 1
if level > filter && a.light(pos, lt) < level-filter {
queue.push(node(pos, level-filter, lt))
}
}

Expand Down
81 changes: 60 additions & 21 deletions server/world/chunk/light_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package chunk

import (
"bytes"
"container/list"
"github.com/df-mc/dragonfly/server/block/cube"
"iter"
"math"
)

Expand All @@ -16,6 +14,62 @@ type lightArea struct {
r cube.Range
}

// lightQueue is a FIFO ring buffer used during light propagation.
type lightQueue struct {
Comment thread
HashimTheArab marked this conversation as resolved.
nodes []lightNode
head int
tail int
size int
}

// newLightQueue creates an empty queue sized to capacity (at least 1).
func newLightQueue(capacity int) *lightQueue {
if capacity < 1 {
capacity = 1
}
return &lightQueue{nodes: make([]lightNode, capacity)}
}

// push appends a node to the tail, growing storage if full.
func (q *lightQueue) push(n lightNode) {
if q.size == len(q.nodes) {
q.grow()
}
q.nodes[q.tail] = n
q.tail = (q.tail + 1) % len(q.nodes)
q.size++
}

// pop removes and returns the oldest queued node.
func (q *lightQueue) pop() (lightNode, bool) {
if q.size == 0 {
return lightNode{}, false
}
n := q.nodes[q.head]
q.head = (q.head + 1) % len(q.nodes)
q.size--
return n, true
}

// empty returns true when no nodes are queued.
func (q *lightQueue) empty() bool {
return q.size == 0
}

// grow expands the ring buffer and reorders elements to start at index 0.
func (q *lightQueue) grow() {
nodes := make([]lightNode, len(q.nodes)<<1)
if q.head < q.tail {
copy(nodes, q.nodes[q.head:q.tail])
} else {
n := copy(nodes, q.nodes[q.head:])
copy(nodes[n:], q.nodes[:q.tail])
}
q.head = 0
q.tail = q.size
q.nodes = nodes
}

// LightArea creates a lightArea with the lower corner of the lightArea at baseX and baseY. The length of the Chunk
// slice must be a square of a number, so 1, 4, 9 etc.
func LightArea(c []*Chunk, baseX, baseY int) *lightArea {
Expand All @@ -30,11 +84,11 @@ func LightArea(c []*Chunk, baseX, baseY int) *lightArea {
// individual chunks within the lightArea itself, without light crossing chunk borders.
func (a *lightArea) Fill() {
a.initialiseLightSlices()
queue := list.New()
queue := newLightQueue(512)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these pre-allocated sizes based on anything? Could we do some tests with a real world to see what a suitable cap might be if that wasn't done yet?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will need @AkmalFairuz to do this

a.insertBlockLightNodes(queue)
a.insertSkyLightNodes(queue)

for queue.Len() != 0 {
for !queue.empty() {
a.propagate(queue)
}
}
Expand All @@ -43,11 +97,11 @@ func (a *lightArea) Fill() {
// neighbouring chunks. The neighbouring chunks must have passed the light 'filling' stage before this
// function is called for an lightArea that includes them.
func (a *lightArea) Spread() {
queue := list.New()
queue := newLightQueue(512)
a.insertLightSpreadingNodes(queue, BlockLight)
a.insertLightSpreadingNodes(queue, SkyLight)

for queue.Len() != 0 {
for !queue.empty() {
a.propagate(queue)
}
}
Expand All @@ -62,21 +116,6 @@ func (a *lightArea) setLight(pos cube.Pos, l light, v uint8) {
l.setLight(a.sub(pos), uint8(pos[0]&0xf), uint8(pos[1]&0xf), uint8(pos[2]&0xf), v)
}

// neighbours returns all neighbour lightNode of the one passed. If one of these nodes would otherwise fall outside the
// lightArea, it is not returned.
func (a *lightArea) neighbours(n lightNode) iter.Seq[lightNode] {
return func(yield func(lightNode) bool) {
for _, f := range cube.Faces() {
nn := lightNode{pos: n.pos.Side(f), lt: n.lt}
if nn.pos[1] <= a.r.Max() && nn.pos[1] >= a.r.Min() && nn.pos[0] >= a.baseX && nn.pos[2] >= a.baseZ && nn.pos[0] < a.baseX+a.w*16 && nn.pos[2] < a.baseZ+a.w*16 {
if !yield(nn) {
return
}
}
}
}
}

// iterSubChunks iterates over all blocks of the lightArea on a per-SubChunk basis. A filter function may be passed to
// specify if a SubChunk should be iterated over. If it returns false, it will not be iterated over.
func (a *lightArea) iterSubChunks(filter func(sub *SubChunk) bool, f func(pos cube.Pos)) {
Expand Down