diff --git a/pkg/game/atlas.go b/pkg/game/atlas.go index 88bc73e..8f1bfcd 100644 --- a/pkg/game/atlas.go +++ b/pkg/game/atlas.go @@ -46,6 +46,26 @@ func NewAtlas(size int, chunkSize int) Atlas { return a } +// SpawnAtlasBorder surrounds the current atlas in a border wall +func (a *Atlas) SpawnAtlasBorder() error { + extent := a.ChunkSize * (a.Size / 2) + // Surround the atlas in walls + for i := -extent; i < extent; i++ { + + if err := a.SetTile(Vector{i, extent - 1}, TileWall); err != nil { + return err + } else if a.SetTile(Vector{extent - 1, i}, TileWall); err != nil { + return err + } else if a.SetTile(Vector{-extent, i}, TileWall); err != nil { + return err + } else if a.SetTile(Vector{i, extent - 1}, TileWall); err != nil { + return err + } + } + + return nil +} + // SetTile sets an individual tile's kind func (a *Atlas) SetTile(v Vector, tile Tile) error { chunk := a.ToChunk(v) diff --git a/pkg/game/atlas_test.go b/pkg/game/atlas_test.go index 99fe5f4..9571f8a 100644 --- a/pkg/game/atlas_test.go +++ b/pkg/game/atlas_test.go @@ -134,3 +134,27 @@ func TestAtlas_Grown(t *testing.T) { assert.NoError(t, err) assert.Equal(t, Tile(3), tile) } + +func TestAtlas_SpawnAtlasBorder(t *testing.T) { + // Start with a small example + a := NewAtlas(2, 2) + assert.NotNil(t, a) + assert.Equal(t, 4, len(a.Chunks)) + + assert.NoError(t, a.SpawnAtlasBorder()) + tile, err := a.GetTile(Vector{0, 0}) + assert.NoError(t, err) + assert.Equal(t, TileEmpty, tile) + + tile, err = a.GetTile(Vector{1, 1}) + assert.NoError(t, err) + assert.Equal(t, TileWall, tile) + + tile, err = a.GetTile(Vector{-1, -1}) + assert.NoError(t, err) + assert.Equal(t, TileEmpty, tile) + + tile, err = a.GetTile(Vector{-2, -2}) + assert.NoError(t, err) + assert.Equal(t, TileWall, tile) +} diff --git a/pkg/game/tile.go b/pkg/game/tile.go index 9500cfe..41cf648 100644 --- a/pkg/game/tile.go +++ b/pkg/game/tile.go @@ -4,7 +4,7 @@ package game type Tile byte const ( - TileEmpty = '_' + TileEmpty = Tile(0) - TileRock = 'o' + TileWall = Tile(1) ) diff --git a/pkg/game/world.go b/pkg/game/world.go index 27ea969..1710a54 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -32,10 +32,15 @@ func NewWorld() *World { return &World{ Rovers: make(map[uuid.UUID]Rover), CommandQueue: make(map[uuid.UUID]CommandStream), - Atlas: NewAtlas(2, 8), // TODO: Choose an appropriate world size + Atlas: NewAtlas(4, 8), // TODO: Choose an appropriate world size } } +// SpawnWorldBorder spawns a border at the edge of the world atlas +func (w *World) SpawnWorldBorder() error { + return w.Atlas.SpawnAtlasBorder() +} + // SpawnRover adds an rover to the game func (w *World) SpawnRover() uuid.UUID { w.worldMutex.Lock() @@ -56,8 +61,8 @@ func (w *World) SpawnRover() uuid.UUID { // Spawn in a random place near the origin rover.Attributes.Pos = Vector{ - 10 - (rand.Int() % 20), - 10 - (rand.Int() % 20), + w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)), + w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)), } // TODO: Verify no blockages in this area diff --git a/pkg/server/server.go b/pkg/server/server.go index fb040d5..d045983 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -109,6 +109,11 @@ func (s *Server) Initialise() (err error) { // Add to our sync s.sync.Add(1) + // Spawn a border on the default world + if err := s.world.SpawnWorldBorder(); err != nil { + return err + } + // Load the accounts if requested if err := s.LoadAll(); err != nil { return err