github.com/df-mc/dragonfly@v0.9.13/server/world/position.go (about) 1 package world 2 3 import ( 4 "fmt" 5 "github.com/df-mc/dragonfly/server/block/cube" 6 "github.com/go-gl/mathgl/mgl64" 7 "math" 8 ) 9 10 // ChunkPos holds the position of a chunk. The type is provided as a utility struct for keeping track of a 11 // chunk's position. Chunks do not themselves keep track of that. Chunk positions are different from block 12 // positions in the way that increasing the X/Z by one means increasing the absolute value on the X/Z axis in 13 // terms of blocks by 16. 14 type ChunkPos [2]int32 15 16 // String implements fmt.Stringer and returns (x, z). 17 func (p ChunkPos) String() string { 18 return fmt.Sprintf("(%v, %v)", p[0], p[1]) 19 } 20 21 // X returns the X coordinate of the chunk position. 22 func (p ChunkPos) X() int32 { 23 return p[0] 24 } 25 26 // Z returns the Z coordinate of the chunk position. 27 func (p ChunkPos) Z() int32 { 28 return p[1] 29 } 30 31 // SubChunkPos holds the position of a sub-chunk. The type is provided as a utility struct for keeping track of a 32 // sub-chunk's position. Sub-chunks do not themselves keep track of that. Sub-chunk positions are different from 33 // block positions in the way that increasing the X/Y/Z by one means increasing the absolute value on the X/Y/Z axis in 34 // terms of blocks by 16. 35 type SubChunkPos [3]int32 36 37 // String implements fmt.Stringer and returns (x, y, z). 38 func (p SubChunkPos) String() string { 39 return fmt.Sprintf("(%v, %v, %v)", p[0], p[1], p[2]) 40 } 41 42 // X returns the X coordinate of the sub-chunk position. 43 func (p SubChunkPos) X() int32 { 44 return p[0] 45 } 46 47 // Y returns the Y coordinate of the sub-chunk position. 48 func (p SubChunkPos) Y() int32 { 49 return p[1] 50 } 51 52 // Z returns the Z coordinate of the sub-chunk position. 53 func (p SubChunkPos) Z() int32 { 54 return p[2] 55 } 56 57 // chunkPosFromVec3 returns a chunk position from the Vec3 passed. The coordinates of the chunk position are 58 // those of the Vec3 divided by 16, then rounded down. 59 func chunkPosFromVec3(vec3 mgl64.Vec3) ChunkPos { 60 return ChunkPos{ 61 int32(math.Floor(vec3[0])) >> 4, 62 int32(math.Floor(vec3[2])) >> 4, 63 } 64 } 65 66 // chunkPosFromBlockPos returns the ChunkPos of the chunk that a block at a cube.Pos is in. 67 func chunkPosFromBlockPos(p cube.Pos) ChunkPos { 68 return ChunkPos{int32(p[0] >> 4), int32(p[2] >> 4)} 69 }