From 2219c34c352feea8464849746b779e8403da65c9 Mon Sep 17 00:00:00 2001 From: Nicholas Novak <34256932+NickyBoy89@users.noreply.github.com> Date: Sun, 10 Dec 2023 19:45:29 -0800 Subject: [PATCH] refactor: Changed some file names and moved some files around --- world/{chunk_data.go => chunk.go} | 44 +++++++++++++++- world/data_format.go | 88 ------------------------------- world/world_position.go | 51 ++++++++++++++++++ 3 files changed, 94 insertions(+), 89 deletions(-) rename world/{chunk_data.go => chunk.go} (62%) delete mode 100644 world/data_format.go create mode 100644 world/world_position.go diff --git a/world/chunk_data.go b/world/chunk.go similarity index 62% rename from world/chunk_data.go rename to world/chunk.go index fa6ca42..943d08a 100644 --- a/world/chunk_data.go +++ b/world/chunk.go @@ -4,13 +4,55 @@ import ( "github.com/Tnze/go-mc/save" ) -const ChunkSectionCount = 16 +const ( + // The number of sections per chunk. This determines the total height of the + // chunk + ChunkSectionCount = 16 + // The number of blocks in a horizontal slice of a chunk + chunkSliceSize = 16 * 16 +) + +// `ChunkData` represents the contents of a "chunk", which is a column of voxels +// in world space type ChunkData struct { Pos ChunkPos `json:"pos"` Sections [ChunkSectionCount]ChunkSection `json:"sections"` } +// `ChunkSection' is a fixed-size cube that stores the data in a chunk +type ChunkSection struct { + // The count of full blocks in the chunk + BlockCount uint `json:"block_count"` + BlockStates [16 * 16 * 16]BlockID `json:"block_states"` +} + +func rem_euclid(a, b int) int { + return (a%b + b) % b +} + +func IndexOfBlock(pos BlockPos) int { + baseX := rem_euclid(pos.X, 16) + baseY := rem_euclid(int(pos.Y), 16) + baseZ := rem_euclid(pos.Z, 16) + + return (baseY * chunkSliceSize) + (baseZ * 16) + baseX +} + +func (cs *ChunkSection) UpdateBlockAtIndex(index int, targetState BlockID) { + // TODO: Keep track of the block count + + cs.BlockStates[index] = targetState +} + +func (cs *ChunkSection) UpdateBlock(pos BlockPos, targetState BlockID) { + cs.BlockStates[IndexOfBlock(pos)] = targetState +} + +func (cs *ChunkSection) FetchBlock(pos BlockPos) BlockID { + return cs.BlockStates[IndexOfBlock(pos)] +} + func (cd *ChunkData) SectionFor(pos BlockPos) *ChunkSection { return &cd.Sections[pos.Y%ChunkSectionCount] } diff --git a/world/data_format.go b/world/data_format.go deleted file mode 100644 index 02af929..0000000 --- a/world/data_format.go +++ /dev/null @@ -1,88 +0,0 @@ -package world - -import ( - "fmt" - "math/rand" -) - -const ( - // Slice size is the total number of blocks in a horizontal slice of a chunk - sliceSize = 16 * 16 -) - -type BlockPos struct { - X int `json:"x"` - Y uint `json:"y"` - Z int `json:"z"` -} - -func (b BlockPos) String() string { - return fmt.Sprintf("BlockPos { X: %v, Y: %v, Z: %v }", b.X, b.Y, b.Z) -} - -func RandomBlockPosWithRange(maxRange float64) BlockPos { - return BlockPos{ - X: int(rand.NormFloat64() * maxRange), - Y: uint(rand.NormFloat64() * maxRange), - Z: int(rand.NormFloat64() * maxRange), - } -} - -func (b BlockPos) ToChunkPos() ChunkPos { - return ChunkPos{ - X: b.X / 16, - Z: b.Z / 16, - } -} - -type ChunkPos struct { - X int `json:"x"` - Z int `json:"z"` -} - -func (cp ChunkPos) ToFileName() string { - return fmt.Sprintf("p.%d.%d.chunk", cp.X, cp.Z) -} - -func (cp ChunkPos) StringCoords() string { - return fmt.Sprintf("%d, %d", cp.X, cp.Z) -} - -type ChunkSection struct { - // The count of full blocks in the chunk - BlockCount uint `json:"block_count"` - BlockStates [16 * 16 * 16]BlockID `json:"block_states"` -} - -func rem_euclid(a, b int) int { - return (a%b + b) % b -} - -func IndexOfBlock(pos BlockPos) int { - baseX := rem_euclid(pos.X, 16) - baseY := rem_euclid(int(pos.Y), 16) - baseZ := rem_euclid(pos.Z, 16) - - return (baseY * sliceSize) + (baseZ * 16) + baseX -} - -func (cs *ChunkSection) UpdateBlockAtIndex(index int, targetState BlockID) { - // TODO: Keep track of the block count - - cs.BlockStates[index] = targetState -} - -func (cs *ChunkSection) UpdateBlock(pos BlockPos, targetState BlockID) { - cs.BlockStates[IndexOfBlock(pos)] = targetState -} - -func (cs *ChunkSection) FetchBlock(pos BlockPos) BlockID { - return cs.BlockStates[IndexOfBlock(pos)] -} - -type BlockID uint8 - -const ( - Empty BlockID = iota - Generic -) diff --git a/world/world_position.go b/world/world_position.go new file mode 100644 index 0000000..f4fe8c5 --- /dev/null +++ b/world/world_position.go @@ -0,0 +1,51 @@ +package world + +import ( + "fmt" + "math/rand" +) + +type BlockPos struct { + X int `json:"x"` + Y uint `json:"y"` + Z int `json:"z"` +} + +func (b BlockPos) String() string { + return fmt.Sprintf("BlockPos { X: %v, Y: %v, Z: %v }", b.X, b.Y, b.Z) +} + +func RandomBlockPosWithRange(maxRange float64) BlockPos { + return BlockPos{ + X: int(rand.NormFloat64() * maxRange), + Y: uint(rand.NormFloat64() * maxRange), + Z: int(rand.NormFloat64() * maxRange), + } +} + +func (b BlockPos) ToChunkPos() ChunkPos { + return ChunkPos{ + X: b.X / 16, + Z: b.Z / 16, + } +} + +type ChunkPos struct { + X int `json:"x"` + Z int `json:"z"` +} + +func (cp ChunkPos) ToFileName() string { + return fmt.Sprintf("p.%d.%d.chunk", cp.X, cp.Z) +} + +func (cp ChunkPos) StringCoords() string { + return fmt.Sprintf("%d, %d", cp.X, cp.Z) +} + +type BlockID uint8 + +const ( + Empty BlockID = iota + Generic +)