diff --git a/db/coords.go b/coords/convert.go similarity index 50% rename from db/coords.go rename to coords/convert.go index 7ee926e..04380cc 100644 --- a/db/coords.go +++ b/coords/convert.go @@ -1,4 +1,4 @@ -package db +package coords //https://bitbucket.org/s_l_teichmann/mtsatellite/src/e1bf980a2b278c570b3f44f9452c9c087558acb3/common/coords.go?at=default&fileviewer=file-view-default const ( @@ -10,17 +10,17 @@ const ( ) -func CoordToPlain(x, y, z int) int64 { - return int64(z)<<(2*numBitsPerComponent) + - int64(y)<> numBitsPerComponent - y := unsignedToSigned(pythonModulo(int16(i))) - i = (i - int64(x)) >> numBitsPerComponent - z := unsignedToSigned(pythonModulo(int16(i))) - return int(x), int(y), int(z) +func PlainToCoord(i int64) MapBlockCoords { + c := MapBlockCoords{} + c.X = unsignedToSigned(pythonModulo(int16(i))) + i = (i - int64(c.X)) >> numBitsPerComponent + c.Y = unsignedToSigned(pythonModulo(int16(i))) + i = (i - int64(c.Y)) >> numBitsPerComponent + c.Z = unsignedToSigned(pythonModulo(int16(i))) + return c } diff --git a/coords/convert_test.go b/coords/convert_test.go new file mode 100644 index 0000000..fe766a4 --- /dev/null +++ b/coords/convert_test.go @@ -0,0 +1,42 @@ +package coords + +import ( + "testing" + "github.com/sirupsen/logrus" +) + +var log *logrus.Entry +func init(){ + log = logrus.WithFields(logrus.Fields{"prefix": "coords/convert_test"}) +} + +func testCoordConvert(t *testing.T, mb MapBlockCoords){ + log.WithFields(logrus.Fields{"coords":mb}).Info("MapblockCoords") + + p := CoordToPlain(mb) + log.WithFields(logrus.Fields{"plain":p}).Info("MapblockCoords") + + mb2 := PlainToCoord(p) + log.WithFields(logrus.Fields{"coords2":mb2}).Info("MapblockCoords") + + + if mb.X != mb2.X { + t.Fatal("X mismatch") + } + + if mb.Y != mb2.Y { + t.Fatal("Y mismatch") + } + + if mb.Z != mb2.Z { + t.Fatal("Z mismatch") + } + +} + +func TestConvertPlainMapBlock(t *testing.T){ + testCoordConvert(t, NewMapBlockCoords(10, 0, -10)) + testCoordConvert(t, NewMapBlockCoords(-2048, 2047, -10)) + testCoordConvert(t, NewMapBlockCoords(-3, 0, 2047)) //0...2047 + +} diff --git a/coords/mapblockcoords.go b/coords/mapblockcoords.go new file mode 100644 index 0000000..3db8d38 --- /dev/null +++ b/coords/mapblockcoords.go @@ -0,0 +1,9 @@ +package coords + +type MapBlockCoords struct { + X,Y,Z int +} + +func NewMapBlockCoords(x,y,z int) MapBlockCoords { + return MapBlockCoords{X:x, Y:y, Z:z} +}