From 64b6ed774bf3b6cbdc23b27c85a0f7e3b8b3f532 Mon Sep 17 00:00:00 2001 From: Restart <45609733+RestartFU@users.noreply.github.com> Date: Fri, 15 Mar 2024 17:50:01 -0400 Subject: [PATCH] world/chunk: Added function to compare chunks (#855) * push * simplify --- server/world/chunk/chunk.go | 20 ++++++++++++++++++++ server/world/chunk/sub_chunk.go | 15 +++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/server/world/chunk/chunk.go b/server/world/chunk/chunk.go index c3412f5c7..9adf4b092 100644 --- a/server/world/chunk/chunk.go +++ b/server/world/chunk/chunk.go @@ -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 @@ -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 diff --git a/server/world/chunk/sub_chunk.go b/server/world/chunk/sub_chunk.go index aee76ae11..42db56b93 100644 --- a/server/world/chunk/sub_chunk.go +++ b/server/world/chunk/sub_chunk.go @@ -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}