From 3aa2dbef3d9b7f459ff652e13cde2c3aee6b6b85 Mon Sep 17 00:00:00 2001 From: Ronoaldo Pereira Date: Thu, 30 Nov 2023 03:19:22 -0300 Subject: [PATCH] fix: coordinates calc for negative positions (#182) * fix: coordinates calc for negative positions This also removes the offset for mapchunks, was also causing issues when exporting. Fixes #181 * ci: added 'set -x' to test script entrypoint.sh --- export.go | 13 +++++++++++++ test/entrypoint.sh | 1 + util.go | 15 ++++++++++----- util_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 util_test.go diff --git a/export.go b/export.go index f6cd0bf..8211564 100644 --- a/export.go +++ b/export.go @@ -4,15 +4,28 @@ import ( "fmt" "github.com/minetest-go/mtdb/block" + "github.com/sirupsen/logrus" ) func ExportChunk(src, dst block.BlockRepository, x, y, z int) error { x1, y1, z1, x2, y2, z2 := GetMapblockBoundsFromChunk(x, y, z) + logrus.WithFields(logrus.Fields{ + "x": x, + "y": y, + "z": z, + "x1": x1, + "y1": y1, + "z1": z1, + "x2": x2, + "y2": y2, + "z2": z2, + }).Debug("exporting blocks for chunk") for mbx := x1; mbx <= x2; mbx++ { for mby := y1; mby <= y2; mby++ { for mbz := z1; mbz <= z2; mbz++ { block, err := src.GetByPos(mbx, mby, mbz) + logrus.Debugf("Block %v,%v,%v", block.PosX, block.PosY, block.PosZ) if err != nil { return fmt.Errorf("error in src-mapblock %d/%d/%d: %v", mbx, mby, mbz, err) } diff --git a/test/entrypoint.sh b/test/entrypoint.sh index 6d67fb1..c58bd87 100755 --- a/test/entrypoint.sh +++ b/test/entrypoint.sh @@ -1,5 +1,6 @@ #!/bin/sh set -e +set -x minetestserver --config /minetest.conf diff --git a/util.go b/util.go index 3d29134..9ee3247 100644 --- a/util.go +++ b/util.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "math" "github.com/minetest-go/areasparser" ) @@ -10,14 +11,18 @@ func GetChunkKey(x, y, z int) string { return fmt.Sprintf("%d/%d/%d", x, y, z) } +func floorOfDivision(n, q int) int { + return int(math.Floor(float64(n) / float64(q))) +} + func GetMapblockPosFromNode(x, y, z int) (int, int, int) { - return int(x / 16), int(y / 16), int(z / 16) + return floorOfDivision(x, 16), floorOfDivision(y, 16), floorOfDivision(z, 16) } func GetMapblockBoundsFromChunk(x, y, z int) (x1, y1, z1, x2, y2, z2 int) { - x1 = (x * 5) - 2 - y1 = (y * 5) - 2 - z1 = (z * 5) - 2 + x1 = (x * 5) + y1 = (y * 5) + z1 = (z * 5) x2 = x1 + 4 y2 = y1 + 4 z2 = z1 + 4 @@ -25,7 +30,7 @@ func GetMapblockBoundsFromChunk(x, y, z int) (x1, y1, z1, x2, y2, z2 int) { } func GetChunkPosFromMapblock(x, y, z int) (int, int, int) { - return int((x + 2) / 5), int((y + 2) / 5), int((z + 2) / 5) + return floorOfDivision((x), 5), floorOfDivision((y), 5), floorOfDivision((z), 5) } func GetChunkPosFromNode(x, y, z int) (int, int, int) { diff --git a/util_test.go b/util_test.go new file mode 100644 index 0000000..89489f1 --- /dev/null +++ b/util_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "testing" +) + +func TestGetMapblockPosFromNode(t *testing.T) { + type args struct { + x int + y int + z int + } + tests := []struct { + name string + args args + wantx int + wanty int + wantz int + }{ + {"coords for node 0,0,0", args{0, 0, 0}, 0, 0, 0}, + {"coords for node 15,15,15", args{15, 15, 15}, 0, 0, 0}, + {"coords for node 16,16,16", args{16, 16, 16}, 1, 1, 1}, + {"coords for node 31,16,16", args{31, 16, 16}, 1, 1, 1}, + {"coords for node -15,-15,-15", args{-15, -15, -15}, -1, -1, -1}, + {"coords for node -1565,4,-386", args{-1565, 4, -386}, -98, 0, -25}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + x, y, z := GetMapblockPosFromNode(tt.args.x, tt.args.y, tt.args.z) + t.Logf("GetMapblockPosFromNode %v: %v, %v, %v", tt.args, x, y, z) + if x != tt.wantx { + t.Errorf("GetMapblockPosFromNode() x = %v, want %v", x, tt.wantx) + } + if y != tt.wanty { + t.Errorf("GetMapblockPosFromNode() y = %v, want %v", y, tt.wanty) + } + if z != tt.wantz { + t.Errorf("GetMapblockPosFromNode() z = %v, want %v", z, tt.wantz) + } + }) + } +}