Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to compare chunks #841

Closed
wants to merge 7 commits into from
Closed
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
24 changes: 24 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,28 @@ 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 := 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
21 changes: 21 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,25 @@ 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 || !slices.Equal(s.blockLight, sub.blockLight) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if s.air != sub.air || !slices.Equal(s.blockLight, sub.blockLight) ||
if s.air != sub.air ||

!slices.Equal(s.skyLight, sub.skyLight) || len(s.storages) != len(sub.storages) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
!slices.Equal(s.skyLight, sub.skyLight) || len(s.storages) != len(sub.storages) {
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
Loading