Skip to content

Commit

Permalink
world/chunk: Added function to compare chunks (#855)
Browse files Browse the repository at this point in the history
* push

* simplify
  • Loading branch information
RestartFU authored Mar 15, 2024
1 parent fd85a55 commit 64b6ed7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions server/world/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chunk

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

// Chunk is a segment in the world with a size of 16x16x256 blocks. A chunk contains multiple sub chunks
Expand Down Expand Up @@ -42,6 +43,25 @@ func New(air uint32, r cube.Range) *Chunk {
}
}

// Equals returns if the chunk passed is equal to the current one
func (chunk *Chunk) Equals(c *Chunk) bool {
if !chunk.recalculateHeightMap && !c.recalculateHeightMap && !slices.Equal(c.heightMap, chunk.heightMap) {
return false
}

if c.r != chunk.r || c.air != chunk.air || len(c.sub) != len(chunk.sub) {
return false
}

for i, s := range c.sub {
if !s.Equals(chunk.sub[i]) {
return false
}
}

return true
}

// Range returns the cube.Range of the Chunk as passed to New.
func (chunk *Chunk) Range() cube.Range {
return chunk.r
Expand Down
15 changes: 15 additions & 0 deletions server/world/chunk/sub_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ type SubChunk struct {
skyLight []uint8
}

// Equals returns if the sub chunk passed is equal to the current one.
func (sub *SubChunk) Equals(s *SubChunk) bool {
if s.air != sub.air || len(s.storages) != len(sub.storages) {
return false
}

for i, st := range s.storages {
if !st.Equal(sub.storages[i]) {
return false
}
}

return true
}

// NewSubChunk creates a new sub chunk. All sub chunks should be created through this function
func NewSubChunk(air uint32) *SubChunk {
return &SubChunk{air: air}
Expand Down

0 comments on commit 64b6ed7

Please sign in to comment.