Skip to content

Commit

Permalink
chunk.go: add Equals()
Browse files Browse the repository at this point in the history
  • Loading branch information
ethaniccc committed Jan 9, 2024
1 parent 21eb18f commit 19b3acc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
36 changes: 36 additions & 0 deletions server/world/chunk/chunk.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package chunk

import (
"slices"

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

Expand Down Expand Up @@ -42,6 +44,40 @@ 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 c.r != chunk.r {
return false
}

if c.air != chunk.air {
return false
}

if c.recalculateHeightMap != chunk.recalculateHeightMap {
return false
}

if !slices.Equal(c.heightMap, chunk.heightMap) {
return false
}

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

for i := 0; i < len(c.sub); i++ {
c1 := c.sub[i]
c2 := chunk.sub[i]

if !c1.Equals(c2) {
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
32 changes: 32 additions & 0 deletions server/world/chunk/sub_chunk.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package chunk

import "slices"

// SubChunk is a cube of blocks located in a chunk. It has a size of 16x16x16 blocks and forms part of a stack
// that forms a Chunk.
type SubChunk struct {
Expand All @@ -14,6 +16,36 @@ func NewSubChunk(air uint32) *SubChunk {
return &SubChunk{air: air}
}

// 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 {
return false
}

if !slices.Equal(s.blockLight, sub.blockLight) {
return false
}

if !slices.Equal(s.skyLight, sub.skyLight) {
return false
}

if len(s.storages) != len(sub.storages) {
return false
}

for i := 0; i < len(s.storages); i++ {
s1 := s.storages[i]
s2 := sub.storages[i]

if !s1.Equal(s2) {
return false
}
}

return true
}

// Empty checks if the SubChunk is considered empty. This is the case if the SubChunk has 0 block storages or if it has
// a single one that is completely filled with air.
func (sub *SubChunk) Empty() bool {
Expand Down

0 comments on commit 19b3acc

Please sign in to comment.