From bb8ddfbc5574a7682594fd15178083f5308766e7 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Mon, 12 Aug 2013 15:20:47 +0200 Subject: Getting rid of some magic numbers. --- mcmap/chunk.go | 18 +++++++++--------- mcmap/dims.go | 11 +++++++++++ mcmap/examples/emeraldfinder/main.go | 6 +++--- mcmap/examples/mapper/main.go | 14 +++++++------- mcmap/examples/replace/main.go | 6 +++--- mcmap/prechunk.go | 34 +++++++++++++++++----------------- mcmap/region.go | 20 ++++++++++---------- mcmap/regionfile.go | 20 ++++++++++---------- 8 files changed, 70 insertions(+), 59 deletions(-) create mode 100644 mcmap/dims.go (limited to 'mcmap') diff --git a/mcmap/chunk.go b/mcmap/chunk.go index 496fd0c..3241d82 100644 --- a/mcmap/chunk.go +++ b/mcmap/chunk.go @@ -7,7 +7,7 @@ import ( ) func calcBlockOffset(x, y, z int) int { - if (x < 0) || (y < 0) || (z < 0) || (x >= 16) || (y >= 256) || (z >= 16) { + if (x < 0) || (y < 0) || (z < 0) || (x >= ChunkSizeXZ) || (y >= ChunkSizeY) || (z >= ChunkSizeXZ) { return -1 } @@ -25,15 +25,15 @@ func offsetToPos(off int) (x, y, z int) { func BlockToChunk(bx, bz int) (cx, cz, rbx, rbz int) { cx = bx << 4 cz = bz << 4 - rbx = ((cx % 16) + 16) % 16 - rbz = ((cz % 16) + 16) % 16 + rbx = ((cx % ChunkSizeXZ) + ChunkSizeXZ) % ChunkSizeXZ + rbz = ((cz % ChunkSizeXZ) + ChunkSizeXZ) % ChunkSizeXZ return } // ChunkToBlock calculates the global position of a block, given the chunk position (cx, cz) and the plock position in that chunk (rbx, rbz). func ChunkToBlock(cx, cz, rbx, rbz int) (bx, bz int) { - bx = cx*16 + rbx - bz = cz*16 + rbz + bx = cx*ChunkSizeXZ + rbx + bz = cz*ChunkSizeXZ + rbz return } @@ -79,18 +79,18 @@ func (c *Chunk) Block(x, y, z int) *Block { // // x and z must be in [0, 15]. Height will panic, if this is violated! func (c *Chunk) Height(x, z int) int { - if (x < 0) || (x > 15) || (z < 0) || (z > 15) { + if (x < 0) || (x >= ChunkSizeXZ) || (z < 0) || (z >= ChunkSizeXZ) { panic(errors.New("x or z parameter was out of range")) } - return int(c.heightMap[z*16+x]) + return int(c.heightMap[z*ChunkSizeXZ+x]) } // Biome gets the Biome at x,z. -func (c *Chunk) Biome(x, z int) Biome { return c.biomes[x*16+z] } +func (c *Chunk) Biome(x, z int) Biome { return c.biomes[x*ChunkSizeXZ+z] } // SetBiome sets the biome at x,z. -func (c *Chunk) SetBiome(x, z int, bio Biome) { c.biomes[x*16+z] = bio } +func (c *Chunk) SetBiome(x, z int, bio Biome) { c.biomes[x*ChunkSizeXZ+z] = bio } // MarkUnused marks the chunk as unused. If all chunks of a superchunk are marked as unused, the superchunk will be unloaded and saved (if needed). // diff --git a/mcmap/dims.go b/mcmap/dims.go new file mode 100644 index 0000000..0b2bb7e --- /dev/null +++ b/mcmap/dims.go @@ -0,0 +1,11 @@ +package mcmap + +const ( + ChunkSizeXZ = 16 + ChunkSizeY = 256 + ChunkRectXZ = ChunkSizeXZ * ChunkSizeXZ + ChunkSize = ChunkRectXZ * ChunkSizeY +) + +const superchunkSizeXZ = 32 +const chunkSectionSize = ChunkRectXZ * 16 diff --git a/mcmap/examples/emeraldfinder/main.go b/mcmap/examples/emeraldfinder/main.go index d92ad3c..3249a35 100644 --- a/mcmap/examples/emeraldfinder/main.go +++ b/mcmap/examples/emeraldfinder/main.go @@ -36,9 +36,9 @@ chunkLoop: os.Exit(1) } - for y := 0; y < 256; y++ { - for x := 0; x < 16; x++ { - for z := 0; z < 16; z++ { + for y := 0; y < mcmap.ChunkSizeY; y++ { + for x := 0; x < mcmap.ChunkSizeXZ; x++ { + for z := 0; z < mcmap.ChunkSizeXZ; z++ { blk := chunk.Block(x, y, z) if blk.ID == mcmap.BlkEmeraldOre { absx, absz := mcmap.ChunkToBlock(cx, cz, x, z) diff --git a/mcmap/examples/mapper/main.go b/mcmap/examples/mapper/main.go index 2cbcefb..2b21648 100644 --- a/mcmap/examples/mapper/main.go +++ b/mcmap/examples/mapper/main.go @@ -27,8 +27,8 @@ func main() { } xmin, xmax, zmin, zmax := region.MaxDims() - w := (xmax - xmin) * 16 - h := (zmax - zmin) * 16 + w := (xmax - xmin) * mcmap.ChunkSizeXZ + h := (zmax - zmin) * mcmap.ChunkSizeXZ img := image.NewRGBA(image.Rect(0, 0, w, h)) chunkLoop: @@ -44,19 +44,19 @@ chunkLoop: os.Exit(1) } - for x := 0; x < 16; x++ { + for x := 0; x < mcmap.ChunkSizeXZ; x++ { scanZ: - for z := 0; z < 16; z++ { + for z := 0; z < mcmap.ChunkSizeXZ; z++ { ax, az := mcmap.ChunkToBlock(cx, cz, x, z) - for y := 255; y >= 0; y-- { + for y := mcmap.ChunkSizeY; y >= 0; y-- { blk := chunk.Block(x, y, z) c, ok := colors[blk.ID] if ok { - img.Set(ax-(xmin*16), az-(zmin*16), c) + img.Set(ax-(xmin*mcmap.ChunkSizeXZ), az-(zmin*mcmap.ChunkSizeXZ), c) continue scanZ } } - img.Set(ax-(xmin*16), az-(zmin*16), rgb(0x000000)) + img.Set(ax-(xmin*mcmap.ChunkSizeXZ), az-(zmin*mcmap.ChunkSizeXZ), rgb(0x000000)) } } diff --git a/mcmap/examples/replace/main.go b/mcmap/examples/replace/main.go index e5fff78..87dd677 100644 --- a/mcmap/examples/replace/main.go +++ b/mcmap/examples/replace/main.go @@ -37,9 +37,9 @@ chunkLoop: } modified := false - for y := 0; y < 256; y++ { - for x := 0; x < 16; x++ { - for z := 0; z < 16; z++ { + for y := 0; y < mcmap.ChunkSizeY; y++ { + for x := 0; x < mcmap.ChunkSizeXZ; x++ { + for z := 0; z < mcmap.ChunkSizeXZ; z++ { blk := chunk.Block(x, y, z) if blk.ID == mcmap.BlkBlockOfIron { blk.ID = mcmap.BlkBlockOfDiamond diff --git a/mcmap/prechunk.go b/mcmap/prechunk.go index ae0ea2a..ef6ba33 100644 --- a/mcmap/prechunk.go +++ b/mcmap/prechunk.go @@ -124,7 +124,7 @@ func (pc *preChunk) toChunk(reg *Region) (*Chunk, error) { return nil, fmt.Errorf("Could not read InhabitatedTime tag: %s", err) } - c.biomes = make([]Biome, 256) + c.biomes = make([]Biome, ChunkRectXZ) biomes, err := lvl.GetByteArray("Biomes") switch err { case nil: @@ -132,7 +132,7 @@ func (pc *preChunk) toChunk(reg *Region) (*Chunk, error) { c.biomes[i] = Biome(bio) } case nbt.NotFound: - for i := 0; i < 256; i++ { + for i := 0; i < ChunkRectXZ; i++ { c.biomes[i] = BioUncalculated } default: @@ -162,7 +162,7 @@ func (pc *preChunk) toChunk(reg *Region) (*Chunk, error) { return nil, fmt.Errorf("Could not read Section tag: %s", err) } - c.blocks = make([]Block, 16*16*256) + c.blocks = make([]Block, ChunkSize) for _, _section := range sections.Elems { section := _section.(nbt.TagCompound) @@ -170,17 +170,17 @@ func (pc *preChunk) toChunk(reg *Region) (*Chunk, error) { if err != nil { return nil, fmt.Errorf("Could not read Section -> Y tag: %s", err) } - off := int(y) * 4096 + off := int(y) * chunkSectionSize blocks, err := section.GetByteArray("Blocks") if err != nil { return nil, fmt.Errorf("Could not read Section -> Blocks tag: %s", err) } - blocksAdd := make([]byte, 4096) + blocksAdd := make([]byte, chunkSectionSize) add, err := section.GetByteArray("Add") switch err { case nil: - for i := 0; i < 4096; i++ { + for i := 0; i < chunkSectionSize; i++ { blocksAdd[i] = halfbyte(add, i) } case nbt.NotFound: @@ -201,7 +201,7 @@ func (pc *preChunk) toChunk(reg *Region) (*Chunk, error) { return nil, fmt.Errorf("Could not read Section -> SkyLight tag: %s", err) } - for i := 0; i < 4096; i++ { + for i := 0; i < chunkSectionSize; i++ { c.blocks[off+i] = Block{ ID: BlockID(uint16(blocks[i]) | (uint16(blocksAdd[i]) << 8)), Data: halfbyte(blkData, i), @@ -239,8 +239,8 @@ func (pc *preChunk) toChunk(reg *Region) (*Chunk, error) { _, _, x, z = BlockToChunk(x, z) - x %= 16 - z %= 16 + x %= ChunkSizeXZ + z %= ChunkSizeXZ tick := TileTick{} if tick.i, err = tTick.GetInt("i"); err != nil { @@ -281,7 +281,7 @@ func (c *Chunk) toPreChunk() (*preChunk, error) { } hasBiomes := false - biomes := make([]byte, 16*16) + biomes := make([]byte, ChunkRectXZ) for i, bio := range c.biomes { if bio != BioUncalculated { hasBiomes = true @@ -298,16 +298,16 @@ func (c *Chunk) toPreChunk() (*preChunk, error) { tileTicks := make([]nbt.TagCompound, 0) for subchunk := 0; subchunk < 16; subchunk++ { - off := subchunk * 4096 + off := subchunk * chunkSectionSize - blocks := make([]byte, 4096) - add := make([]byte, 2048) - data := make([]byte, 2048) - blockLight := make([]byte, 2048) - skyLight := make([]byte, 2048) + blocks := make([]byte, chunkSectionSize) + add := make([]byte, chunkSectionSize/2) + data := make([]byte, chunkSectionSize/2) + blockLight := make([]byte, chunkSectionSize/2) + skyLight := make([]byte, chunkSectionSize/2) allAir, addEmpty := true, true - for i := 0; i < 4096; i++ { + for i := 0; i < chunkSectionSize; i++ { blk := c.blocks[i+off] id := blk.ID if id != BlkAir { diff --git a/mcmap/region.go b/mcmap/region.go index 0e569da..8a5fd9f 100644 --- a/mcmap/region.go +++ b/mcmap/region.go @@ -97,24 +97,24 @@ func (reg *Region) MaxDims() (xmin, xmax, zmin, zmax int) { xmax++ zmax++ - xmin *= 32 - xmax *= 32 - zmin *= 32 - zmax *= 32 + xmin *= superchunkSizeXZ + xmax *= superchunkSizeXZ + zmin *= superchunkSizeXZ + zmax *= superchunkSizeXZ return } func chunkToSuperchunk(cx, cz int) (scx, scz, rx, rz int) { scx = cx >> 5 scz = cz >> 5 - rx = ((cx % 32) + 32) % 32 - rz = ((cz % 32) + 32) % 32 + rx = ((cx % superchunkSizeXZ) + superchunkSizeXZ) % superchunkSizeXZ + rz = ((cz % superchunkSizeXZ) + superchunkSizeXZ) % superchunkSizeXZ return } func superchunkToChunk(scx, scz, rx, rz int) (cx, cz int) { - cx = scx*32 + rx - cz = scz*32 + rz + cx = scx*superchunkSizeXZ + rx + cz = scz*superchunkSizeXZ + rz return } @@ -250,8 +250,8 @@ func (reg *Region) AllChunks() <-chan XZPos { go func(ch chan<- XZPos) { for spos, _ := range reg.superchunksAvail { scx, scz := spos.X, spos.Z - for rx := 0; rx < 32; rx++ { - for rz := 0; rz < 32; rz++ { + for rx := 0; rx < superchunkSizeXZ; rx++ { + for rz := 0; rz < superchunkSizeXZ; rz++ { cx, cz := superchunkToChunk(scx, scz, rx, rz) ch <- XZPos{cx, cz} } diff --git a/mcmap/regionfile.go b/mcmap/regionfile.go index ff9794e..746c32d 100644 --- a/mcmap/regionfile.go +++ b/mcmap/regionfile.go @@ -57,8 +57,8 @@ func readRegionFile(r io.ReadSeeker) (map[XZPos]*preChunk, error) { offs := make(map[XZPos]*chunkOffTs) - for z := 0; z < 32; z++ { - for x := 0; x < 32; x++ { + for z := 0; z < superchunkSizeXZ; z++ { + for x := 0; x < superchunkSizeXZ; x++ { var location uint32 if err := binary.Read(r, binary.BigEndian, &location); err != nil { return nil, err @@ -75,8 +75,8 @@ func readRegionFile(r io.ReadSeeker) (map[XZPos]*preChunk, error) { } } - for z := 0; z < 32; z++ { - for x := 0; x < 32; x++ { + for z := 0; z < superchunkSizeXZ; z++ { + for x := 0; x < superchunkSizeXZ; x++ { pos := XZPos{x, z} var ts int32 @@ -119,7 +119,7 @@ func (pc *preChunk) writePreChunk(w io.Writer) error { func writeRegionFile(w io.Writer, pcs map[XZPos]*preChunk) error { offs := make(map[XZPos]chunkOffTs) buf := new(bytes.Buffer) - pw := kagus.NewPaddedWriter(buf, 4096) + pw := kagus.NewPaddedWriter(buf, sectorSize) for pos, pc := range pcs { off := buf.Len() @@ -130,14 +130,14 @@ func writeRegionFile(w io.Writer, pcs map[XZPos]*preChunk) error { return err } offs[pos] = chunkOffTs{ - offset: int64(8192 + off), + offset: int64(2*sectorSize + off), size: int64(buf.Len() - off), ts: pc.ts, } } - for z := 0; z < 32; z++ { - for x := 0; x < 32; x++ { + for z := 0; z < superchunkSizeXZ; z++ { + for x := 0; x < superchunkSizeXZ; x++ { off := uint32(0) if cOff, ok := offs[XZPos{x, z}]; ok { off = cOff.calcLocationEntry() @@ -149,8 +149,8 @@ func writeRegionFile(w io.Writer, pcs map[XZPos]*preChunk) error { } } - for z := 0; z < 32; z++ { - for x := 0; x < 32; x++ { + for z := 0; z < superchunkSizeXZ; z++ { + for x := 0; x < superchunkSizeXZ; x++ { ts := int32(0) if cOff, ok := offs[XZPos{x, z}]; ok { ts = int32(cOff.ts.Unix()) -- cgit v1.2.3-54-g00ecf