diff --git a/cmd/rove/main.go b/cmd/rove/main.go index 674428e..5d5100f 100644 --- a/cmd/rove/main.go +++ b/cmd/rove/main.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "io/ioutil" - "math" "os" "path" @@ -175,13 +174,7 @@ func InnerMain(command string) error { } else { // Print out the radar - num := int(math.Sqrt(float64(len(response.Tiles)))) - for j := num - 1; j >= 0; j-- { - for i := 0; i < num; i++ { - fmt.Printf("%d", response.Tiles[i+num*j]) - } - fmt.Print("\n") - } + game.PrintTiles(response.Tiles) } case "rover": diff --git a/pkg/game/world.go b/pkg/game/world.go index a0a24b7..a4d1912 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -2,6 +2,7 @@ package game import ( "fmt" + "math" "math/rand" "strings" "sync" @@ -68,8 +69,8 @@ func (w *World) SpawnRover() (uuid.UUID, error) { // Spawn in a random place near the origin rover.Attributes.Pos = Vector{ - w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)), - w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)), + w.Atlas.ChunkSize/2 - rand.Intn(w.Atlas.ChunkSize), + w.Atlas.ChunkSize/2 - rand.Intn(w.Atlas.ChunkSize), } // Seach until we error (run out of world) @@ -91,6 +92,8 @@ func (w *World) SpawnRover() (uuid.UUID, error) { return uuid.Nil, err } + fmt.Printf("Spawned rover at %+v\n", rover.Attributes.Pos) + // Append the rover to the list w.Rovers[rover.Id] = rover @@ -152,9 +155,11 @@ func (w *World) WarpRover(id uuid.UUID, pos Vector) error { } // Update the world tile - // TODO: Make this (and other things) transactional - // TODO: Check this worldtile is free - if err := w.Atlas.SetTile(pos, TileRover); err != nil { + 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 { + return fmt.Errorf("Can't warp rover to occupied tile, check before warping") + } else if err := w.Atlas.SetTile(pos, 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 { return fmt.Errorf("coudln't clear old rover tile: %s", err) @@ -345,6 +350,17 @@ func (w *World) ExecuteCommand(c *Command, rover uuid.UUID) (finished bool, err return } +// PrintTiles simply prints the input tiles directly for debug +func PrintTiles(tiles []Tile) { + num := int(math.Sqrt(float64(len(tiles)))) + for j := num - 1; j >= 0; j-- { + for i := 0; i < num; i++ { + fmt.Printf("%d", tiles[i+num*j]) + } + fmt.Print("\n") + } +} + // RLock read locks the world func (w *World) RLock() { w.worldMutex.RLock() diff --git a/pkg/game/world_test.go b/pkg/game/world_test.go index da19b40..d478b2c 100644 --- a/pkg/game/world_test.go +++ b/pkg/game/world_test.go @@ -107,8 +107,8 @@ func TestWorld_RadarFromRover(t *testing.T) { // Warp the rovers into position bpos := Vector{-3, -3} - assert.NoError(t, world.WarpRover(a, Vector{0, 0}), "Failed to warp rover") assert.NoError(t, world.WarpRover(b, bpos), "Failed to warp rover") + assert.NoError(t, world.WarpRover(a, Vector{0, 0}), "Failed to warp rover") // Spawn the world wall err = world.Atlas.SpawnWalls() @@ -129,6 +129,7 @@ func TestWorld_RadarFromRover(t *testing.T) { // O------O- // OR-----O- // OOOOOOOO- + PrintTiles(radar) // Test all expected values assert.Equal(t, TileRover, radar[1+fullRange])