diff --git a/pkg/atlas/atlas.go b/pkg/atlas/atlas.go index 0793dc9..d4054db 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/atlas/atlas.go @@ -1,13 +1,8 @@ package atlas import ( - "log" - "math/rand" - - "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/pkg/objects" "github.com/mdiluz/rove/pkg/vector" - "github.com/ojrac/opensimplex-go" ) // Tile describes the type of terrain @@ -38,258 +33,3 @@ type Atlas interface { // QueryPosition queries a position on the atlas QueryPosition(v vector.Vector) (byte, objects.Object) } - -// chunk represents a fixed square grid of tiles -type chunk struct { - // Tiles represents the tiles within the chunk - Tiles []byte `json:"tiles"` - - // Objects represents the objects within the chunk - // only one possible object per tile for now - Objects map[int]objects.Object `json:"objects"` -} - -// ChunkBasedAtlas represents a grid of Chunks -type ChunkBasedAtlas struct { - // Chunks represents all chunks in the world - // This is intentionally not a 2D array so it can be expanded in all directions - Chunks []chunk `json:"chunks"` - - // LowerBound is the origin of the bottom left corner of the current chunks in world space (current chunks cover >= this value) - LowerBound vector.Vector `json:"lowerBound"` - - // UpperBound is the top left corner of the current chunks (curent chunks cover < this value) - UpperBound vector.Vector `json:"upperBound"` - - // ChunkSize is the x/y dimensions of each square chunk - ChunkSize int `json:"chunksize"` - - // terrainNoise describes the noise function for the terrain - terrainNoise opensimplex.Noise - - // terrainNoise describes the noise function for the terrain - objectNoise opensimplex.Noise -} - -const ( - noiseSeed = 1024 - terrainNoiseScale = 6 - objectNoiseScale = 3 -) - -// NewAtlas creates a new empty atlas -func NewAtlas(chunkSize int) Atlas { - // Start up with one chunk - a := ChunkBasedAtlas{ - ChunkSize: chunkSize, - Chunks: make([]chunk, 1), - LowerBound: vector.Vector{X: 0, Y: 0}, - UpperBound: vector.Vector{X: chunkSize, Y: chunkSize}, - terrainNoise: opensimplex.New(noiseSeed), - objectNoise: opensimplex.New(noiseSeed), - } - // Initialise the first chunk - a.populate(0) - return &a -} - -// SetTile sets an individual tile's kind -func (a *ChunkBasedAtlas) SetTile(v vector.Vector, tile Tile) { - c := a.worldSpaceToChunkWithGrow(v) - local := a.worldSpaceToChunkLocal(v) - a.setTile(c, local, byte(tile)) -} - -// SetObject sets the object on a tile -func (a *ChunkBasedAtlas) SetObject(v vector.Vector, obj objects.Object) { - c := a.worldSpaceToChunkWithGrow(v) - local := a.worldSpaceToChunkLocal(v) - a.setObject(c, local, obj) -} - -// QueryPosition will return information for a specific position -func (a *ChunkBasedAtlas) QueryPosition(v vector.Vector) (byte, objects.Object) { - c := a.worldSpaceToChunkWithGrow(v) - local := a.worldSpaceToChunkLocal(v) - a.populate(c) - chunk := a.Chunks[c] - i := a.chunkTileIndex(local) - return chunk.Tiles[i], chunk.Objects[i] -} - -// chunkTileID returns the tile index within a chunk -func (a *ChunkBasedAtlas) chunkTileIndex(local vector.Vector) int { - return local.X + local.Y*a.ChunkSize -} - -// populate will fill a chunk with data -func (a *ChunkBasedAtlas) populate(chunk int) { - c := a.Chunks[chunk] - if c.Tiles != nil { - return - } - - c.Tiles = make([]byte, a.ChunkSize*a.ChunkSize) - c.Objects = make(map[int]objects.Object) - - origin := a.chunkOriginInWorldSpace(chunk) - for i := 0; i < a.ChunkSize; i++ { - for j := 0; j < a.ChunkSize; j++ { - - // Get the terrain noise value for this location - t := a.terrainNoise.Eval2(float64(origin.X+i)/terrainNoiseScale, float64(origin.Y+j)/terrainNoiseScale) - var tile Tile - switch { - case t > 0.5: - tile = TileGravel - case t > 0.05: - tile = TileSand - default: - tile = TileRock - } - c.Tiles[j*a.ChunkSize+i] = byte(tile) - - // Get the object noise value for this location - o := a.objectNoise.Eval2(float64(origin.X+i)/objectNoiseScale, float64(origin.Y+j)/objectNoiseScale) - var obj = objects.None - switch { - case o > 0.6: - obj = objects.LargeRock - case o > 0.5: - obj = objects.SmallRock - } - if obj != objects.None { - c.Objects[j*a.ChunkSize+i] = objects.Object{Type: obj} - } - } - } - - // Set up any objects - for i := 0; i < len(c.Tiles); i++ { - if rand.Intn(16) == 0 { - c.Objects[i] = objects.Object{Type: objects.LargeRock} - } else if rand.Intn(32) == 0 { - c.Objects[i] = objects.Object{Type: objects.SmallRock} - } - } - - a.Chunks[chunk] = c -} - -// setTile sets a tile in a specific chunk -func (a *ChunkBasedAtlas) setTile(chunk int, local vector.Vector, tile byte) { - a.populate(chunk) - c := a.Chunks[chunk] - c.Tiles[a.chunkTileIndex(local)] = tile - a.Chunks[chunk] = c -} - -// setObject sets an object in a specific chunk -func (a *ChunkBasedAtlas) setObject(chunk int, local vector.Vector, object objects.Object) { - a.populate(chunk) - - c := a.Chunks[chunk] - i := a.chunkTileIndex(local) - if object.Type != objects.None { - c.Objects[i] = object - } else { - delete(c.Objects, i) - } - a.Chunks[chunk] = c -} - -// worldSpaceToChunkLocal gets a chunk local coordinate for a tile -func (a *ChunkBasedAtlas) worldSpaceToChunkLocal(v vector.Vector) vector.Vector { - return vector.Vector{X: maths.Pmod(v.X, a.ChunkSize), Y: maths.Pmod(v.Y, a.ChunkSize)} -} - -// worldSpaceToChunkID gets the current chunk ID for a position in the world -func (a *ChunkBasedAtlas) worldSpaceToChunkIndex(v vector.Vector) int { - // Shift the vector by our current min - v = v.Added(a.LowerBound.Negated()) - - // Divide by the current size and floor, to get chunk-scaled vector from the lower bound - v = v.DividedFloor(a.ChunkSize) - - // Calculate the width - width := a.UpperBound.X - a.LowerBound.X - widthInChunks := width / a.ChunkSize - - // Along the corridor and up the stairs - return (v.Y * widthInChunks) + v.X -} - -// chunkOriginInWorldSpace returns the origin of the chunk in world space -func (a *ChunkBasedAtlas) chunkOriginInWorldSpace(chunk int) vector.Vector { - // Calculate the width - width := a.UpperBound.X - a.LowerBound.X - widthInChunks := width / a.ChunkSize - - // Reverse the along the corridor and up the stairs - v := vector.Vector{ - X: chunk % widthInChunks, - Y: chunk / widthInChunks, - } - // Multiply up to world scale - v = v.Multiplied(a.ChunkSize) - // Shift by the lower bound - return v.Added(a.LowerBound) -} - -// getNewBounds gets new lower and upper bounds for the world space given a vector -func (a *ChunkBasedAtlas) getNewBounds(v vector.Vector) (lower vector.Vector, upper vector.Vector) { - lower = vector.Min(v, a.LowerBound) - upper = vector.Max(v.Added(vector.Vector{X: 1, Y: 1}), a.UpperBound) - - lower = vector.Vector{ - X: maths.RoundDown(lower.X, a.ChunkSize), - Y: maths.RoundDown(lower.Y, a.ChunkSize), - } - upper = vector.Vector{ - X: maths.RoundUp(upper.X, a.ChunkSize), - Y: maths.RoundUp(upper.Y, a.ChunkSize), - } - return -} - -// worldSpaceToTrunkWithGrow will expand the current atlas for a given world space position if needed -func (a *ChunkBasedAtlas) worldSpaceToChunkWithGrow(v vector.Vector) int { - // If we're within bounds, just return the current chunk - if v.X >= a.LowerBound.X && v.Y >= a.LowerBound.Y && v.X < a.UpperBound.X && v.Y < a.UpperBound.Y { - return a.worldSpaceToChunkIndex(v) - } - - // Calculate the new bounds - lower, upper := a.getNewBounds(v) - size := upper.Added(lower.Negated()) - size = size.Divided(a.ChunkSize) - - // Create the new empty atlas - newAtlas := ChunkBasedAtlas{ - ChunkSize: a.ChunkSize, - LowerBound: lower, - UpperBound: upper, - Chunks: make([]chunk, size.X*size.Y), - terrainNoise: a.terrainNoise, - objectNoise: a.objectNoise, - } - - // Log that we're resizing - log.Printf("Re-allocating world, old: %+v,%+v new: %+v,%+v\n", a.LowerBound, a.UpperBound, newAtlas.LowerBound, newAtlas.UpperBound) - - // Copy all old chunks into the new atlas - for chunk, chunkData := range a.Chunks { - - // Calculate the chunk ID in the new atlas - origin := a.chunkOriginInWorldSpace(chunk) - newChunk := newAtlas.worldSpaceToChunkIndex(origin) - - // Copy over the old chunk to the new atlas - newAtlas.Chunks[newChunk] = chunkData - } - - // Overwrite the old atlas with this one - *a = newAtlas - - return a.worldSpaceToChunkIndex(v) -} diff --git a/pkg/atlas/atlas_test.go b/pkg/atlas/atlas_test.go index 1803bea..1805d66 100644 --- a/pkg/atlas/atlas_test.go +++ b/pkg/atlas/atlas_test.go @@ -10,14 +10,14 @@ import ( ) func TestAtlas_NewAtlas(t *testing.T) { - a := NewAtlas(1).(*ChunkBasedAtlas) + a := NewChunkAtlas(1).(*ChunkBasedAtlas) assert.NotNil(t, a) assert.Equal(t, 1, a.ChunkSize) assert.Equal(t, 1, len(a.Chunks)) // Should start empty } func TestAtlas_toChunk(t *testing.T) { - a := NewAtlas(1).(*ChunkBasedAtlas) + a := NewChunkAtlas(1).(*ChunkBasedAtlas) assert.NotNil(t, a) // Get a tile to spawn the chunks @@ -38,7 +38,7 @@ func TestAtlas_toChunk(t *testing.T) { chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -1, Y: 0}) assert.Equal(t, 2, chunkID) - a = NewAtlas(2).(*ChunkBasedAtlas) + a = NewChunkAtlas(2).(*ChunkBasedAtlas) assert.NotNil(t, a) // Get a tile to spawn the chunks a.QueryPosition(vector.Vector{X: -2, Y: -2}) @@ -58,7 +58,7 @@ func TestAtlas_toChunk(t *testing.T) { chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -2, Y: 1}) assert.Equal(t, 2, chunkID) - a = NewAtlas(2).(*ChunkBasedAtlas) + a = NewChunkAtlas(2).(*ChunkBasedAtlas) assert.NotNil(t, a) // Get a tile to spawn a 4x4 grid of chunks a.QueryPosition(vector.Vector{X: 3, Y: 3}) @@ -83,7 +83,7 @@ func TestAtlas_toChunk(t *testing.T) { chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -2, Y: 2}) assert.Equal(t, 13, chunkID) - a = NewAtlas(3).(*ChunkBasedAtlas) + a = NewChunkAtlas(3).(*ChunkBasedAtlas) assert.NotNil(t, a) // Get a tile to spawn a 4x4 grid of chunks a.QueryPosition(vector.Vector{X: 3, Y: 3}) @@ -105,7 +105,7 @@ func TestAtlas_toChunk(t *testing.T) { } func TestAtlas_toWorld(t *testing.T) { - a := NewAtlas(1).(*ChunkBasedAtlas) + a := NewChunkAtlas(1).(*ChunkBasedAtlas) assert.NotNil(t, a) // Get a tile to spawn some chunks @@ -119,7 +119,7 @@ func TestAtlas_toWorld(t *testing.T) { assert.Equal(t, vector.Vector{X: -1, Y: -1}, a.chunkOriginInWorldSpace(0)) assert.Equal(t, vector.Vector{X: 0, Y: -1}, a.chunkOriginInWorldSpace(1)) - a = NewAtlas(2).(*ChunkBasedAtlas) + a = NewChunkAtlas(2).(*ChunkBasedAtlas) assert.NotNil(t, a) // Get a tile to spawn the chunks a.QueryPosition(vector.Vector{X: -2, Y: -2}) @@ -133,7 +133,7 @@ func TestAtlas_toWorld(t *testing.T) { assert.Equal(t, vector.Vector{X: -2, Y: -2}, a.chunkOriginInWorldSpace(0)) assert.Equal(t, vector.Vector{X: -2, Y: 0}, a.chunkOriginInWorldSpace(2)) - a = NewAtlas(2).(*ChunkBasedAtlas) + a = NewChunkAtlas(2).(*ChunkBasedAtlas) assert.NotNil(t, a) // Get a tile to spawn a 4x4 grid of chunks a.QueryPosition(vector.Vector{X: 3, Y: 3}) @@ -152,7 +152,7 @@ func TestAtlas_toWorld(t *testing.T) { assert.Equal(t, vector.Vector{X: -4, Y: -4}, a.chunkOriginInWorldSpace(0)) assert.Equal(t, vector.Vector{X: 2, Y: -2}, a.chunkOriginInWorldSpace(7)) - a = NewAtlas(3).(*ChunkBasedAtlas) + a = NewChunkAtlas(3).(*ChunkBasedAtlas) assert.NotNil(t, a) // Get a tile to spawn a 4x4 grid of chunks a.QueryPosition(vector.Vector{X: 3, Y: 3}) @@ -167,7 +167,7 @@ func TestAtlas_toWorld(t *testing.T) { } func TestAtlas_GetSetTile(t *testing.T) { - a := NewAtlas(10) + a := NewChunkAtlas(10) assert.NotNil(t, a) // Set the origin tile to 1 and test it @@ -182,7 +182,7 @@ func TestAtlas_GetSetTile(t *testing.T) { } func TestAtlas_GetSetObject(t *testing.T) { - a := NewAtlas(10) + a := NewChunkAtlas(10) assert.NotNil(t, a) // Set the origin tile to 1 and test it @@ -198,7 +198,7 @@ func TestAtlas_GetSetObject(t *testing.T) { func TestAtlas_Grown(t *testing.T) { // Start with a small example - a := NewAtlas(2).(*ChunkBasedAtlas) + a := NewChunkAtlas(2).(*ChunkBasedAtlas) assert.NotNil(t, a) assert.Equal(t, 1, len(a.Chunks)) @@ -233,7 +233,7 @@ func TestAtlas_GetSetCorrect(t *testing.T) { for x := -i * 2; x < i*2; x++ { for y := -i * 2; y < i*2; y++ { - a := NewAtlas(i).(*ChunkBasedAtlas) + a := NewChunkAtlas(i).(*ChunkBasedAtlas) assert.NotNil(t, a) assert.Equal(t, 1, len(a.Chunks)) @@ -251,7 +251,7 @@ func TestAtlas_GetSetCorrect(t *testing.T) { } func TestAtlas_WorldGen(t *testing.T) { - a := NewAtlas(8) + a := NewChunkAtlas(8) // Spawn a large world _, _ = a.QueryPosition(vector.Vector{X: 20, Y: 20}) diff --git a/pkg/atlas/chunkAtlas.go b/pkg/atlas/chunkAtlas.go new file mode 100644 index 0000000..00231af --- /dev/null +++ b/pkg/atlas/chunkAtlas.go @@ -0,0 +1,266 @@ +package atlas + +import ( + "log" + "math/rand" + + "github.com/mdiluz/rove/pkg/maths" + "github.com/mdiluz/rove/pkg/objects" + "github.com/mdiluz/rove/pkg/vector" + "github.com/ojrac/opensimplex-go" +) + +// chunk represents a fixed square grid of tiles +type chunk struct { + // Tiles represents the tiles within the chunk + Tiles []byte `json:"tiles"` + + // Objects represents the objects within the chunk + // only one possible object per tile for now + Objects map[int]objects.Object `json:"objects"` +} + +// ChunkBasedAtlas represents a grid of Chunks +type ChunkBasedAtlas struct { + // Chunks represents all chunks in the world + // This is intentionally not a 2D array so it can be expanded in all directions + Chunks []chunk `json:"chunks"` + + // LowerBound is the origin of the bottom left corner of the current chunks in world space (current chunks cover >= this value) + LowerBound vector.Vector `json:"lowerBound"` + + // UpperBound is the top left corner of the current chunks (curent chunks cover < this value) + UpperBound vector.Vector `json:"upperBound"` + + // ChunkSize is the x/y dimensions of each square chunk + ChunkSize int `json:"chunksize"` + + // terrainNoise describes the noise function for the terrain + terrainNoise opensimplex.Noise + + // terrainNoise describes the noise function for the terrain + objectNoise opensimplex.Noise +} + +const ( + noiseSeed = 1024 + terrainNoiseScale = 6 + objectNoiseScale = 3 +) + +// NewChunkAtlas creates a new empty atlas +func NewChunkAtlas(chunkSize int) Atlas { + // Start up with one chunk + a := ChunkBasedAtlas{ + ChunkSize: chunkSize, + Chunks: make([]chunk, 1), + LowerBound: vector.Vector{X: 0, Y: 0}, + UpperBound: vector.Vector{X: chunkSize, Y: chunkSize}, + terrainNoise: opensimplex.New(noiseSeed), + objectNoise: opensimplex.New(noiseSeed), + } + // Initialise the first chunk + a.populate(0) + return &a +} + +// SetTile sets an individual tile's kind +func (a *ChunkBasedAtlas) SetTile(v vector.Vector, tile Tile) { + c := a.worldSpaceToChunkWithGrow(v) + local := a.worldSpaceToChunkLocal(v) + a.setTile(c, local, byte(tile)) +} + +// SetObject sets the object on a tile +func (a *ChunkBasedAtlas) SetObject(v vector.Vector, obj objects.Object) { + c := a.worldSpaceToChunkWithGrow(v) + local := a.worldSpaceToChunkLocal(v) + a.setObject(c, local, obj) +} + +// QueryPosition will return information for a specific position +func (a *ChunkBasedAtlas) QueryPosition(v vector.Vector) (byte, objects.Object) { + c := a.worldSpaceToChunkWithGrow(v) + local := a.worldSpaceToChunkLocal(v) + a.populate(c) + chunk := a.Chunks[c] + i := a.chunkTileIndex(local) + return chunk.Tiles[i], chunk.Objects[i] +} + +// chunkTileID returns the tile index within a chunk +func (a *ChunkBasedAtlas) chunkTileIndex(local vector.Vector) int { + return local.X + local.Y*a.ChunkSize +} + +// populate will fill a chunk with data +func (a *ChunkBasedAtlas) populate(chunk int) { + c := a.Chunks[chunk] + if c.Tiles != nil { + return + } + + c.Tiles = make([]byte, a.ChunkSize*a.ChunkSize) + c.Objects = make(map[int]objects.Object) + + origin := a.chunkOriginInWorldSpace(chunk) + for i := 0; i < a.ChunkSize; i++ { + for j := 0; j < a.ChunkSize; j++ { + + // Get the terrain noise value for this location + t := a.terrainNoise.Eval2(float64(origin.X+i)/terrainNoiseScale, float64(origin.Y+j)/terrainNoiseScale) + var tile Tile + switch { + case t > 0.5: + tile = TileGravel + case t > 0.05: + tile = TileSand + default: + tile = TileRock + } + c.Tiles[j*a.ChunkSize+i] = byte(tile) + + // Get the object noise value for this location + o := a.objectNoise.Eval2(float64(origin.X+i)/objectNoiseScale, float64(origin.Y+j)/objectNoiseScale) + var obj = objects.None + switch { + case o > 0.6: + obj = objects.LargeRock + case o > 0.5: + obj = objects.SmallRock + } + if obj != objects.None { + c.Objects[j*a.ChunkSize+i] = objects.Object{Type: obj} + } + } + } + + // Set up any objects + for i := 0; i < len(c.Tiles); i++ { + if rand.Intn(16) == 0 { + c.Objects[i] = objects.Object{Type: objects.LargeRock} + } else if rand.Intn(32) == 0 { + c.Objects[i] = objects.Object{Type: objects.SmallRock} + } + } + + a.Chunks[chunk] = c +} + +// setTile sets a tile in a specific chunk +func (a *ChunkBasedAtlas) setTile(chunk int, local vector.Vector, tile byte) { + a.populate(chunk) + c := a.Chunks[chunk] + c.Tiles[a.chunkTileIndex(local)] = tile + a.Chunks[chunk] = c +} + +// setObject sets an object in a specific chunk +func (a *ChunkBasedAtlas) setObject(chunk int, local vector.Vector, object objects.Object) { + a.populate(chunk) + + c := a.Chunks[chunk] + i := a.chunkTileIndex(local) + if object.Type != objects.None { + c.Objects[i] = object + } else { + delete(c.Objects, i) + } + a.Chunks[chunk] = c +} + +// worldSpaceToChunkLocal gets a chunk local coordinate for a tile +func (a *ChunkBasedAtlas) worldSpaceToChunkLocal(v vector.Vector) vector.Vector { + return vector.Vector{X: maths.Pmod(v.X, a.ChunkSize), Y: maths.Pmod(v.Y, a.ChunkSize)} +} + +// worldSpaceToChunkID gets the current chunk ID for a position in the world +func (a *ChunkBasedAtlas) worldSpaceToChunkIndex(v vector.Vector) int { + // Shift the vector by our current min + v = v.Added(a.LowerBound.Negated()) + + // Divide by the current size and floor, to get chunk-scaled vector from the lower bound + v = v.DividedFloor(a.ChunkSize) + + // Calculate the width + width := a.UpperBound.X - a.LowerBound.X + widthInChunks := width / a.ChunkSize + + // Along the corridor and up the stairs + return (v.Y * widthInChunks) + v.X +} + +// chunkOriginInWorldSpace returns the origin of the chunk in world space +func (a *ChunkBasedAtlas) chunkOriginInWorldSpace(chunk int) vector.Vector { + // Calculate the width + width := a.UpperBound.X - a.LowerBound.X + widthInChunks := width / a.ChunkSize + + // Reverse the along the corridor and up the stairs + v := vector.Vector{ + X: chunk % widthInChunks, + Y: chunk / widthInChunks, + } + // Multiply up to world scale + v = v.Multiplied(a.ChunkSize) + // Shift by the lower bound + return v.Added(a.LowerBound) +} + +// getNewBounds gets new lower and upper bounds for the world space given a vector +func (a *ChunkBasedAtlas) getNewBounds(v vector.Vector) (lower vector.Vector, upper vector.Vector) { + lower = vector.Min(v, a.LowerBound) + upper = vector.Max(v.Added(vector.Vector{X: 1, Y: 1}), a.UpperBound) + + lower = vector.Vector{ + X: maths.RoundDown(lower.X, a.ChunkSize), + Y: maths.RoundDown(lower.Y, a.ChunkSize), + } + upper = vector.Vector{ + X: maths.RoundUp(upper.X, a.ChunkSize), + Y: maths.RoundUp(upper.Y, a.ChunkSize), + } + return +} + +// worldSpaceToTrunkWithGrow will expand the current atlas for a given world space position if needed +func (a *ChunkBasedAtlas) worldSpaceToChunkWithGrow(v vector.Vector) int { + // If we're within bounds, just return the current chunk + if v.X >= a.LowerBound.X && v.Y >= a.LowerBound.Y && v.X < a.UpperBound.X && v.Y < a.UpperBound.Y { + return a.worldSpaceToChunkIndex(v) + } + + // Calculate the new bounds + lower, upper := a.getNewBounds(v) + size := upper.Added(lower.Negated()) + size = size.Divided(a.ChunkSize) + + // Create the new empty atlas + newAtlas := ChunkBasedAtlas{ + ChunkSize: a.ChunkSize, + LowerBound: lower, + UpperBound: upper, + Chunks: make([]chunk, size.X*size.Y), + terrainNoise: a.terrainNoise, + objectNoise: a.objectNoise, + } + + // Log that we're resizing + log.Printf("Re-allocating world, old: %+v,%+v new: %+v,%+v\n", a.LowerBound, a.UpperBound, newAtlas.LowerBound, newAtlas.UpperBound) + + // Copy all old chunks into the new atlas + for chunk, chunkData := range a.Chunks { + + // Calculate the chunk ID in the new atlas + origin := a.chunkOriginInWorldSpace(chunk) + newChunk := newAtlas.worldSpaceToChunkIndex(origin) + + // Copy over the old chunk to the new atlas + newAtlas.Chunks[newChunk] = chunkData + } + + // Overwrite the old atlas with this one + *a = newAtlas + + return a.worldSpaceToChunkIndex(v) +} diff --git a/pkg/game/world.go b/pkg/game/world.go index 89a70e5..f7476fb 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -70,7 +70,7 @@ func NewWorld(chunkSize int) *World { Rovers: make(map[string]Rover), CommandQueue: make(map[string]CommandStream), CommandIncoming: make(map[string]CommandStream), - Atlas: atlas.NewAtlas(chunkSize), + Atlas: atlas.NewChunkAtlas(chunkSize), words: lines, TicksPerDay: 24, CurrentTicks: 0,