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

fix: coordinates calc for negative positions #182

Merged
merged 2 commits into from
Nov 30, 2023
Merged
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
13 changes: 13 additions & 0 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions test/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/sh
set -e
set -x

minetestserver --config /minetest.conf

Expand Down
15 changes: 10 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"math"

"github.com/minetest-go/areasparser"
)
Expand All @@ -10,22 +11,26 @@ 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
return
}

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) {
Expand Down
42 changes: 42 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading