Stop spawning rovers outside the chunks or warping into other rovers
This commit is contained in:
parent
ae2cb6598a
commit
520f78b5c3
3 changed files with 24 additions and 14 deletions
|
@ -5,7 +5,6 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
@ -175,13 +174,7 @@ func InnerMain(command string) error {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Print out the radar
|
// Print out the radar
|
||||||
num := int(math.Sqrt(float64(len(response.Tiles))))
|
game.PrintTiles(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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case "rover":
|
case "rover":
|
||||||
|
|
|
@ -2,6 +2,7 @@ package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -68,8 +69,8 @@ func (w *World) SpawnRover() (uuid.UUID, error) {
|
||||||
|
|
||||||
// Spawn in a random place near the origin
|
// Spawn in a random place near the origin
|
||||||
rover.Attributes.Pos = Vector{
|
rover.Attributes.Pos = Vector{
|
||||||
w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)),
|
w.Atlas.ChunkSize/2 - rand.Intn(w.Atlas.ChunkSize),
|
||||||
w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)),
|
w.Atlas.ChunkSize/2 - rand.Intn(w.Atlas.ChunkSize),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seach until we error (run out of world)
|
// Seach until we error (run out of world)
|
||||||
|
@ -91,6 +92,8 @@ func (w *World) SpawnRover() (uuid.UUID, error) {
|
||||||
return uuid.Nil, err
|
return uuid.Nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Spawned rover at %+v\n", rover.Attributes.Pos)
|
||||||
|
|
||||||
// Append the rover to the list
|
// Append the rover to the list
|
||||||
w.Rovers[rover.Id] = rover
|
w.Rovers[rover.Id] = rover
|
||||||
|
|
||||||
|
@ -152,9 +155,11 @@ func (w *World) WarpRover(id uuid.UUID, pos Vector) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the world tile
|
// Update the world tile
|
||||||
// TODO: Make this (and other things) transactional
|
if tile, err := w.Atlas.GetTile(pos); err != nil {
|
||||||
// TODO: Check this worldtile is free
|
return fmt.Errorf("coudln't get state of destination rover tile: %s", err)
|
||||||
if err := w.Atlas.SetTile(pos, TileRover); err != nil {
|
} 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)
|
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, TileEmpty); err != nil {
|
||||||
return fmt.Errorf("coudln't clear old rover tile: %s", err)
|
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
|
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
|
// RLock read locks the world
|
||||||
func (w *World) RLock() {
|
func (w *World) RLock() {
|
||||||
w.worldMutex.RLock()
|
w.worldMutex.RLock()
|
||||||
|
|
|
@ -107,8 +107,8 @@ func TestWorld_RadarFromRover(t *testing.T) {
|
||||||
|
|
||||||
// Warp the rovers into position
|
// Warp the rovers into position
|
||||||
bpos := Vector{-3, -3}
|
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(b, bpos), "Failed to warp rover")
|
||||||
|
assert.NoError(t, world.WarpRover(a, Vector{0, 0}), "Failed to warp rover")
|
||||||
|
|
||||||
// Spawn the world wall
|
// Spawn the world wall
|
||||||
err = world.Atlas.SpawnWalls()
|
err = world.Atlas.SpawnWalls()
|
||||||
|
@ -129,6 +129,7 @@ func TestWorld_RadarFromRover(t *testing.T) {
|
||||||
// O------O-
|
// O------O-
|
||||||
// OR-----O-
|
// OR-----O-
|
||||||
// OOOOOOOO-
|
// OOOOOOOO-
|
||||||
|
PrintTiles(radar)
|
||||||
|
|
||||||
// Test all expected values
|
// Test all expected values
|
||||||
assert.Equal(t, TileRover, radar[1+fullRange])
|
assert.Equal(t, TileRover, radar[1+fullRange])
|
||||||
|
|
Loading…
Add table
Reference in a new issue