From 19b3acc0e9c64acd81c136186e6939984b01806e Mon Sep 17 00:00:00 2001 From: ethaniccc Date: Tue, 9 Jan 2024 15:59:02 +0000 Subject: [PATCH 1/6] 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 { From 403c0d408a89af386898c79a7ee7157bf9df16d9 Mon Sep 17 00:00:00 2001 From: ethaniccc Date: Wed, 10 Jan 2024 22:17:51 +0000 Subject: [PATCH 2/6] simplify --- server/world/chunk/chunk.go | 19 ++----------------- server/world/chunk/sub_chunk.go | 15 ++------------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/server/world/chunk/chunk.go b/server/world/chunk/chunk.go index 0e249fa0a..c9d9ba484 100644 --- a/server/world/chunk/chunk.go +++ b/server/world/chunk/chunk.go @@ -46,23 +46,8 @@ 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) { + if c.r != chunk.r || c.air != chunk.air || + !slices.Equal(c.heightMap, chunk.heightMap) || len(c.sub) != len(chunk.sub) { return false } diff --git a/server/world/chunk/sub_chunk.go b/server/world/chunk/sub_chunk.go index 3aafab649..9b280cc49 100644 --- a/server/world/chunk/sub_chunk.go +++ b/server/world/chunk/sub_chunk.go @@ -18,19 +18,8 @@ func NewSubChunk(air uint32) *SubChunk { // 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) { + if s.air != sub.air || !slices.Equal(s.blockLight, sub.blockLight) || + !slices.Equal(s.skyLight, sub.skyLight) || len(s.storages) != len(sub.storages) { return false } From 3ac37c17b7b95f05003c342b474921f23dd8ac4c Mon Sep 17 00:00:00 2001 From: ethaniccc <59127626+ethaniccc@users.noreply.github.com> Date: Sun, 11 Feb 2024 14:01:12 -0500 Subject: [PATCH 3/6] Don't compare chunks if height map needs to be re-calculated --- server/world/chunk/chunk.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/world/chunk/chunk.go b/server/world/chunk/chunk.go index c9d9ba484..ad2976942 100644 --- a/server/world/chunk/chunk.go +++ b/server/world/chunk/chunk.go @@ -46,6 +46,10 @@ 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 { + return false + } + if c.r != chunk.r || c.air != chunk.air || !slices.Equal(c.heightMap, chunk.heightMap) || len(c.sub) != len(chunk.sub) { return false From a665196fcb6bf062cc7409e909e5e0a3a721b78f Mon Sep 17 00:00:00 2001 From: ethaniccc Date: Sun, 11 Feb 2024 21:03:37 +0000 Subject: [PATCH 4/6] go fmt --- server/world/chunk/chunk.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/world/chunk/chunk.go b/server/world/chunk/chunk.go index ad2976942..5d5ec0f4e 100644 --- a/server/world/chunk/chunk.go +++ b/server/world/chunk/chunk.go @@ -49,7 +49,7 @@ func (chunk *Chunk) Equals(c *Chunk) bool { if chunk.recalculateHeightMap || c.recalculateHeightMap { return false } - + if c.r != chunk.r || c.air != chunk.air || !slices.Equal(c.heightMap, chunk.heightMap) || len(c.sub) != len(chunk.sub) { return false From 7db49bcb19245aec1edf596071c0c5b159b34271 Mon Sep 17 00:00:00 2001 From: ethaniccc <59127626+ethaniccc@users.noreply.github.com> Date: Wed, 14 Feb 2024 10:23:08 -0500 Subject: [PATCH 5/6] don't compare recalculateHeightMap --- server/world/chunk/chunk.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/server/world/chunk/chunk.go b/server/world/chunk/chunk.go index 5d5ec0f4e..c9d9ba484 100644 --- a/server/world/chunk/chunk.go +++ b/server/world/chunk/chunk.go @@ -46,10 +46,6 @@ 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 { - return false - } - if c.r != chunk.r || c.air != chunk.air || !slices.Equal(c.heightMap, chunk.heightMap) || len(c.sub) != len(chunk.sub) { return false From 4381e5fc257312c856208e7c7edfaf7c371cd110 Mon Sep 17 00:00:00 2001 From: ethaniccc <59127626+ethaniccc@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:57:54 -0500 Subject: [PATCH 6/6] only calculate heightMap if both chunks don't need heightMap recalculation --- server/world/chunk/chunk.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server/world/chunk/chunk.go b/server/world/chunk/chunk.go index c9d9ba484..1a746e843 100644 --- a/server/world/chunk/chunk.go +++ b/server/world/chunk/chunk.go @@ -46,8 +46,11 @@ 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 || c.air != chunk.air || - !slices.Equal(c.heightMap, chunk.heightMap) || len(c.sub) != len(chunk.sub) { + 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 }