From faaa556ad05a6fc920e576264e8a60f36ec8b79d Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Thu, 11 Jun 2020 20:42:59 +0100 Subject: [PATCH] Move the Atlas code into it's own package --- .vscode/settings.json | 5 +++++ cmd/rove-server/internal/routes_test.go | 19 ++++++++-------- pkg/{game => atlas}/atlas.go | 2 +- pkg/{game => atlas}/atlas_test.go | 2 +- pkg/{game => atlas}/tile.go | 2 +- pkg/game/world.go | 29 +++++++++++++------------ pkg/game/world_test.go | 15 +++++++------ pkg/rove/api.go | 5 +++-- 8 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 .vscode/settings.json rename pkg/{game => atlas}/atlas.go (99%) rename pkg/{game => atlas}/atlas_test.go (99%) rename pkg/{game => atlas}/tile.go (91%) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b3be4a5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "gopls": { + "buildFlags": ["-tags=integration"] + }, +} \ No newline at end of file diff --git a/cmd/rove-server/internal/routes_test.go b/cmd/rove-server/internal/routes_test.go index dd0f2fe..e34b983 100644 --- a/cmd/rove-server/internal/routes_test.go +++ b/cmd/rove-server/internal/routes_test.go @@ -13,6 +13,7 @@ import ( "github.com/google/uuid" "github.com/mdiluz/rove/pkg/accounts" + "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/game" "github.com/mdiluz/rove/pkg/rove" "github.com/mdiluz/rove/pkg/vector" @@ -144,10 +145,10 @@ func TestHandleRadar(t *testing.T) { wallPos2 := vector.Vector{X: 1, Y: 1} rockPos := vector.Vector{X: 1, Y: 3} emptyPos := vector.Vector{X: -2, Y: -3} - assert.NoError(t, s.world.Atlas.SetTile(wallPos1, game.TileWall)) - assert.NoError(t, s.world.Atlas.SetTile(wallPos2, game.TileWall)) - assert.NoError(t, s.world.Atlas.SetTile(rockPos, game.TileRock)) - assert.NoError(t, s.world.Atlas.SetTile(emptyPos, game.TileEmpty)) + assert.NoError(t, s.world.Atlas.SetTile(wallPos1, atlas.TileWall)) + assert.NoError(t, s.world.Atlas.SetTile(wallPos2, atlas.TileWall)) + assert.NoError(t, s.world.Atlas.SetTile(rockPos, atlas.TileRock)) + assert.NoError(t, s.world.Atlas.SetTile(emptyPos, atlas.TileEmpty)) request, _ := http.NewRequest(http.MethodGet, path.Join("/", name, "/radar"), nil) response := httptest.NewRecorder() @@ -166,17 +167,17 @@ func TestHandleRadar(t *testing.T) { radarOrigin := vector.Vector{X: -attrib.Range, Y: -attrib.Range} // Make sure the rover tile is correct - assert.Equal(t, game.TileRover, status.Tiles[len(status.Tiles)/2]) + assert.Equal(t, atlas.TileRover, status.Tiles[len(status.Tiles)/2]) // Check our other tiles wallPos1.Add(radarOrigin.Negated()) wallPos2.Add(radarOrigin.Negated()) rockPos.Add(radarOrigin.Negated()) emptyPos.Add(radarOrigin.Negated()) - assert.Equal(t, game.TileWall, status.Tiles[wallPos1.X+wallPos1.Y*scope]) - assert.Equal(t, game.TileWall, status.Tiles[wallPos2.X+wallPos2.Y*scope]) - assert.Equal(t, game.TileRock, status.Tiles[rockPos.X+rockPos.Y*scope]) - assert.Equal(t, game.TileEmpty, status.Tiles[emptyPos.X+emptyPos.Y*scope]) + assert.Equal(t, atlas.TileWall, status.Tiles[wallPos1.X+wallPos1.Y*scope]) + assert.Equal(t, atlas.TileWall, status.Tiles[wallPos2.X+wallPos2.Y*scope]) + assert.Equal(t, atlas.TileRock, status.Tiles[rockPos.X+rockPos.Y*scope]) + assert.Equal(t, atlas.TileEmpty, status.Tiles[emptyPos.X+emptyPos.Y*scope]) } diff --git a/pkg/game/atlas.go b/pkg/atlas/atlas.go similarity index 99% rename from pkg/game/atlas.go rename to pkg/atlas/atlas.go index 9030069..66a2baa 100644 --- a/pkg/game/atlas.go +++ b/pkg/atlas/atlas.go @@ -1,4 +1,4 @@ -package game +package atlas import ( "fmt" diff --git a/pkg/game/atlas_test.go b/pkg/atlas/atlas_test.go similarity index 99% rename from pkg/game/atlas_test.go rename to pkg/atlas/atlas_test.go index 5423617..a9c7021 100644 --- a/pkg/game/atlas_test.go +++ b/pkg/atlas/atlas_test.go @@ -1,4 +1,4 @@ -package game +package atlas import ( "testing" diff --git a/pkg/game/tile.go b/pkg/atlas/tile.go similarity index 91% rename from pkg/game/tile.go rename to pkg/atlas/tile.go index 954f571..af44e87 100644 --- a/pkg/game/tile.go +++ b/pkg/atlas/tile.go @@ -1,4 +1,4 @@ -package game +package atlas // Tile represents the type of a tile on the map type Tile byte diff --git a/pkg/game/world.go b/pkg/game/world.go index 43bc6c1..29948ad 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -9,6 +9,7 @@ import ( "sync" "github.com/google/uuid" + "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/bearing" "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/pkg/vector" @@ -21,7 +22,7 @@ type World struct { Rovers map[uuid.UUID]Rover `json:"rovers"` // Atlas represends the world map of chunks and tiles - Atlas Atlas `json:"atlas"` + Atlas atlas.Atlas `json:"atlas"` // Mutex to lock around all world operations worldMutex sync.RWMutex @@ -42,7 +43,7 @@ func NewWorld(size, chunkSize int) *World { Rovers: make(map[uuid.UUID]Rover), CommandQueue: make(map[uuid.UUID]CommandStream), Incoming: make(map[uuid.UUID]CommandStream), - Atlas: NewAtlas(size, chunkSize), + Atlas: atlas.NewAtlas(size, chunkSize), } } @@ -88,7 +89,7 @@ func (w *World) SpawnRover() (uuid.UUID, error) { if tile, err := w.Atlas.GetTile(rover.Attributes.Pos); err != nil { return uuid.Nil, err } else { - if tile == TileEmpty { + if tile == atlas.TileEmpty { break } else { // Try and spawn to the east of the blockage @@ -98,7 +99,7 @@ func (w *World) SpawnRover() (uuid.UUID, error) { } // Set the world tile to a rover - if err := w.Atlas.SetTile(rover.Attributes.Pos, TileRover); err != nil { + if err := w.Atlas.SetTile(rover.Attributes.Pos, atlas.TileRover); err != nil { return uuid.Nil, err } @@ -117,7 +118,7 @@ func (w *World) DestroyRover(id uuid.UUID) error { if i, ok := w.Rovers[id]; ok { // Clear the tile - if err := w.Atlas.SetTile(i.Attributes.Pos, TileEmpty); err != nil { + if err := w.Atlas.SetTile(i.Attributes.Pos, atlas.TileEmpty); err != nil { return fmt.Errorf("coudln't clear old rover tile: %s", err) } delete(w.Rovers, id) @@ -167,11 +168,11 @@ func (w *World) WarpRover(id uuid.UUID, pos vector.Vector) error { // Update the world tile if tile, err := w.Atlas.GetTile(pos); err != nil { return fmt.Errorf("coudln't get state of destination rover tile: %s", err) - } else if tile == TileRover { + } else if tile == atlas.TileRover { return fmt.Errorf("can't warp rover to occupied tile, check before warping") - } else if err := w.Atlas.SetTile(pos, TileRover); err != nil { + } else if err := w.Atlas.SetTile(pos, atlas.TileRover); err != nil { return fmt.Errorf("coudln't set rover tile: %s", err) - } else if err := w.Atlas.SetTile(i.Attributes.Pos, TileEmpty); err != nil { + } else if err := w.Atlas.SetTile(i.Attributes.Pos, atlas.TileEmpty); err != nil { return fmt.Errorf("coudln't clear old rover tile: %s", err) } @@ -201,11 +202,11 @@ func (w *World) MoveRover(id uuid.UUID, b bearing.Bearing) (RoverAttributes, err // Get the tile and verify it's empty if tile, err := w.Atlas.GetTile(newPos); err != nil { return i.Attributes, fmt.Errorf("couldn't get tile for new position: %s", err) - } else if tile == TileEmpty { + } else if tile == atlas.TileEmpty { // Set the world tiles - if err := w.Atlas.SetTile(newPos, TileRover); err != nil { + if err := w.Atlas.SetTile(newPos, atlas.TileRover); err != nil { return i.Attributes, fmt.Errorf("coudln't set rover tile: %s", err) - } else if err := w.Atlas.SetTile(i.Attributes.Pos, TileEmpty); err != nil { + } else if err := w.Atlas.SetTile(i.Attributes.Pos, atlas.TileEmpty); err != nil { return i.Attributes, fmt.Errorf("coudln't clear old rover tile: %s", err) } @@ -221,7 +222,7 @@ func (w *World) MoveRover(id uuid.UUID, b bearing.Bearing) (RoverAttributes, err } // RadarFromRover can be used to query what a rover can currently see -func (w *World) RadarFromRover(id uuid.UUID) ([]Tile, error) { +func (w *World) RadarFromRover(id uuid.UUID) ([]atlas.Tile, error) { w.worldMutex.RLock() defer w.worldMutex.RUnlock() @@ -252,7 +253,7 @@ func (w *World) RadarFromRover(id uuid.UUID) ([]Tile, error) { } // Gather up all tiles within the range - var radar = make([]Tile, radarSpan*radarSpan) + var radar = make([]atlas.Tile, radarSpan*radarSpan) for j := scanMin.Y; j <= scanMax.Y; j++ { for i := scanMin.X; i <= scanMax.X; i++ { q := vector.Vector{X: i, Y: j} @@ -377,7 +378,7 @@ func (w *World) ExecuteCommand(c *Command, rover uuid.UUID) (finished bool, err } // PrintTiles simply prints the input tiles directly for debug -func PrintTiles(tiles []Tile) { +func PrintTiles(tiles []atlas.Tile) { num := int(math.Sqrt(float64(len(tiles)))) for j := num - 1; j >= 0; j-- { for i := 0; i < num; i++ { diff --git a/pkg/game/world_test.go b/pkg/game/world_test.go index 186ee1e..c002442 100644 --- a/pkg/game/world_test.go +++ b/pkg/game/world_test.go @@ -3,6 +3,7 @@ package game import ( "testing" + "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/bearing" "github.com/mdiluz/rove/pkg/vector" "github.com/stretchr/testify/assert" @@ -87,7 +88,7 @@ func TestWorld_GetSetMovePosition(t *testing.T) { assert.Equal(t, pos, newAttribs.Pos, "Failed to correctly move position for rover") // Place a tile in front of the rover - assert.NoError(t, world.Atlas.SetTile(vector.Vector{X: 0, Y: 2}, TileWall)) + assert.NoError(t, world.Atlas.SetTile(vector.Vector{X: 0, Y: 2}, atlas.TileWall)) newAttribs, err = world.MoveRover(a, b) assert.Equal(t, pos, newAttribs.Pos, "Failed to correctly not move position for rover into wall") } @@ -134,12 +135,12 @@ func TestWorld_RadarFromRover(t *testing.T) { PrintTiles(radar) // Test all expected values - assert.Equal(t, TileRover, radar[1+fullRange]) - assert.Equal(t, TileRover, radar[4+4*fullRange]) + assert.Equal(t, atlas.TileRover, radar[1+fullRange]) + assert.Equal(t, atlas.TileRover, radar[4+4*fullRange]) for i := 0; i < 8; i++ { - assert.Equal(t, TileWall, radar[i]) - assert.Equal(t, TileWall, radar[i+(7*9)]) - assert.Equal(t, TileWall, radar[i*9]) - assert.Equal(t, TileWall, radar[(i*9)+7]) + assert.Equal(t, atlas.TileWall, radar[i]) + assert.Equal(t, atlas.TileWall, radar[i+(7*9)]) + assert.Equal(t, atlas.TileWall, radar[i*9]) + assert.Equal(t, atlas.TileWall, radar[(i*9)+7]) } } diff --git a/pkg/rove/api.go b/pkg/rove/api.go index 8ecb108..007b8d1 100644 --- a/pkg/rove/api.go +++ b/pkg/rove/api.go @@ -3,6 +3,7 @@ package rove import ( "path" + "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/game" ) @@ -78,8 +79,8 @@ type RadarResponse struct { Error string `json:"error,omitempty"` // The set of positions for nearby non-empty tiles - Range int `json:"range"` - Tiles []game.Tile `json:"tiles"` + Range int `json:"range"` + Tiles []atlas.Tile `json:"tiles"` } // ================