From 19b3acc0e9c64acd81c136186e6939984b01806e Mon Sep 17 00:00:00 2001 From: ethaniccc Date: Tue, 9 Jan 2024 15:59:02 +0000 Subject: [PATCH] chunk.go: add Equals() --- server/world/chunk/chunk.go | 36 +++++++++++++++++++++++++++++++++ server/world/chunk/sub_chunk.go | 32 +++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/server/world/chunk/chunk.go b/server/world/chunk/chunk.go index c3412f5c7..0e249fa0a 100644 --- a/server/world/chunk/chunk.go +++ b/server/world/chunk/chunk.go @@ -1,6 +1,8 @@ package chunk import ( + "slices" + "github.com/df-mc/dragonfly/server/block/cube" ) @@ -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 diff --git a/server/world/chunk/sub_chunk.go b/server/world/chunk/sub_chunk.go index aee76ae11..3aafab649 100644 --- a/server/world/chunk/sub_chunk.go +++ b/server/world/chunk/sub_chunk.go @@ -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 { @@ -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 {