Convert Atlas to infinite lazy growth

The atlas will now expand as needed for any query, but only initialise the chunk tile memory when requested

	While this may still be a pre-mature optimisation, it does simplify some code and ensures that our memory footprint stays small, for the most part
This commit is contained in:
Marc Di Luzio 2020-06-27 14:48:21 +01:00
parent 2556c0d049
commit b116cdf291
6 changed files with 186 additions and 337 deletions

View file

@ -8,7 +8,7 @@ import (
)
func TestCommand_Move(t *testing.T) {
world := NewWorld(2, 8)
world := NewWorld(8)
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{

View file

@ -12,7 +12,6 @@ import (
"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/objects"
"github.com/mdiluz/rove/pkg/vector"
)
@ -44,7 +43,7 @@ type World struct {
var wordsFile = os.Getenv("WORDS_FILE")
// NewWorld creates a new world object
func NewWorld(size, chunkSize int) *World {
func NewWorld(chunkSize int) *World {
// Try and load the words file
var lines []string
@ -65,23 +64,11 @@ func NewWorld(size, chunkSize int) *World {
Rovers: make(map[string]Rover),
CommandQueue: make(map[string]CommandStream),
Incoming: make(map[string]CommandStream),
Atlas: atlas.NewAtlas(size, chunkSize),
Atlas: atlas.NewAtlas(chunkSize),
words: lines,
}
}
// SpawnWorld spawns a border at the edge of the world atlas
func (w *World) SpawnWorld(fillWorld bool) error {
w.worldMutex.Lock()
defer w.worldMutex.Unlock()
if fillWorld {
if err := w.Atlas.SpawnRocks(); err != nil {
return err
}
}
return w.Atlas.SpawnWalls()
}
// SpawnRover adds an rover to the game
func (w *World) SpawnRover() (string, error) {
w.worldMutex.Lock()
@ -114,16 +101,14 @@ func (w *World) SpawnRover() (string, error) {
// Seach until we error (run out of world)
for {
if tile, err := w.Atlas.GetTile(rover.Pos); err != nil {
return "", err
tile := w.Atlas.GetTile(rover.Pos)
if !objects.IsBlocking(tile) {
break
} else {
if !objects.IsBlocking(tile) {
break
} else {
// Try and spawn to the east of the blockage
rover.Pos.Add(vector.Vector{X: 1, Y: 0})
}
// Try and spawn to the east of the blockage
rover.Pos.Add(vector.Vector{X: 1, Y: 0})
}
}
log.Printf("Spawned rover at %+v\n", rover.Pos)
@ -153,9 +138,7 @@ func (w *World) DestroyRover(rover string) error {
if i, ok := w.Rovers[rover]; ok {
// Clear the tile
if err := w.Atlas.SetTile(i.Pos, objects.Empty); err != nil {
return fmt.Errorf("coudln't clear old rover tile: %s", err)
}
w.Atlas.SetTile(i.Pos, objects.Empty)
delete(w.Rovers, rover)
} else {
return fmt.Errorf("no rover matching id")
@ -213,9 +196,8 @@ func (w *World) WarpRover(rover string, pos vector.Vector) error {
}
// Check the tile is not blocked
if tile, err := w.Atlas.GetTile(pos); err != nil {
return fmt.Errorf("coudln't get state of destination rover tile: %s", err)
} else if objects.IsBlocking(tile) {
tile := w.Atlas.GetTile(pos)
if objects.IsBlocking(tile) {
return fmt.Errorf("can't warp rover to occupied tile, check before warping")
}
@ -237,9 +219,8 @@ func (w *World) MoveRover(rover string, b bearing.Bearing) (vector.Vector, error
newPos := i.Pos.Added(b.Vector())
// Get the tile and verify it's empty
if tile, err := w.Atlas.GetTile(newPos); err != nil {
return vector.Vector{}, fmt.Errorf("couldn't get tile for new position: %s", err)
} else if !objects.IsBlocking(tile) {
tile := w.Atlas.GetTile(newPos)
if !objects.IsBlocking(tile) {
// Perform the move
i.Pos = newPos
w.Rovers[rover] = i
@ -265,18 +246,12 @@ func (w *World) RoverStash(rover string) (byte, error) {
defer w.worldMutex.Unlock()
if r, ok := w.Rovers[rover]; ok {
if tile, err := w.Atlas.GetTile(r.Pos); err != nil {
return objects.Empty, err
} else {
if objects.IsStashable(tile) {
r.Inventory = append(r.Inventory, tile)
w.Rovers[rover] = r
if err := w.Atlas.SetTile(r.Pos, objects.Empty); err != nil {
return objects.Empty, err
} else {
return tile, nil
}
}
tile := w.Atlas.GetTile(r.Pos)
if objects.IsStashable(tile) {
r.Inventory = append(r.Inventory, tile)
w.Rovers[rover] = r
w.Atlas.SetTile(r.Pos, objects.Empty)
return tile, nil
}
} else {
@ -306,32 +281,19 @@ func (w *World) RadarFromRover(rover string) ([]byte, error) {
Y: roverPos.Y + r.Range,
}
// Make sure we only query within the actual world
worldMin, worldMax := w.Atlas.GetWorldExtents()
scanMin := vector.Vector{
X: maths.Max(radarMin.X, worldMin.X),
Y: maths.Max(radarMin.Y, worldMin.Y),
}
scanMax := vector.Vector{
X: maths.Min(radarMax.X, worldMax.X),
Y: maths.Min(radarMax.Y, worldMax.Y),
}
// Gather up all tiles within the range
var radar = make([]byte, radarSpan*radarSpan)
for j := scanMin.Y; j <= scanMax.Y; j++ {
for i := scanMin.X; i <= scanMax.X; i++ {
for j := radarMin.Y; j <= radarMax.Y; j++ {
for i := radarMin.X; i <= radarMax.X; i++ {
q := vector.Vector{X: i, Y: j}
if tile, err := w.Atlas.GetTile(q); err != nil {
return nil, fmt.Errorf("failed to query tile: %s", err)
tile := w.Atlas.GetTile(q)
// Get the position relative to the bottom left of the radar
relative := q.Added(radarMin.Negated())
index := relative.X + relative.Y*radarSpan
radar[index] = tile
} else {
// Get the position relative to the bottom left of the radar
relative := q.Added(radarMin.Negated())
index := relative.X + relative.Y*radarSpan
radar[index] = tile
}
}
}

View file

@ -11,14 +11,14 @@ import (
func TestNewWorld(t *testing.T) {
// Very basic for now, nothing to verify
world := NewWorld(4, 4)
world := NewWorld(4)
if world == nil {
t.Error("Failed to create world")
}
}
func TestWorld_CreateRover(t *testing.T) {
world := NewWorld(2, 8)
world := NewWorld(8)
a, err := world.SpawnRover()
assert.NoError(t, err)
b, err := world.SpawnRover()
@ -33,7 +33,7 @@ func TestWorld_CreateRover(t *testing.T) {
}
func TestWorld_GetRover(t *testing.T) {
world := NewWorld(2, 4)
world := NewWorld(4)
a, err := world.SpawnRover()
assert.NoError(t, err)
@ -43,7 +43,7 @@ func TestWorld_GetRover(t *testing.T) {
}
func TestWorld_DestroyRover(t *testing.T) {
world := NewWorld(4, 1)
world := NewWorld(1)
a, err := world.SpawnRover()
assert.NoError(t, err)
b, err := world.SpawnRover()
@ -61,7 +61,7 @@ func TestWorld_DestroyRover(t *testing.T) {
}
func TestWorld_GetSetMovePosition(t *testing.T) {
world := NewWorld(4, 4)
world := NewWorld(4)
a, err := world.SpawnRover()
assert.NoError(t, err)
@ -84,14 +84,14 @@ func TestWorld_GetSetMovePosition(t *testing.T) {
assert.Equal(t, pos, newPos, "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}, objects.LargeRock))
world.Atlas.SetTile(vector.Vector{X: 0, Y: 2}, objects.LargeRock)
newPos, err = world.MoveRover(a, b)
assert.Equal(t, pos, newPos, "Failed to correctly not move position for rover into wall")
}
func TestWorld_RadarFromRover(t *testing.T) {
// Create world that should have visible walls on the radar
world := NewWorld(4, 2)
world := NewWorld(2)
a, err := world.SpawnRover()
assert.NoError(t, err)
b, err := world.SpawnRover()
@ -102,40 +102,18 @@ func TestWorld_RadarFromRover(t *testing.T) {
assert.NoError(t, world.WarpRover(b, bpos), "Failed to warp rover")
assert.NoError(t, world.WarpRover(a, vector.Vector{X: 0, Y: 0}), "Failed to warp rover")
// Spawn the world wall
err = world.Atlas.SpawnWalls()
assert.NoError(t, err)
radar, err := world.RadarFromRover(a)
assert.NoError(t, err, "Failed to get radar from rover")
fullRange := 4 + 4 + 1
assert.Equal(t, fullRange*fullRange, len(radar), "Radar returned wrong length")
// It should look like:
// ---------
// OOOOOOOO-
// O------O-
// O------O-
// O---R--O-
// O------O-
// O------O-
// OR-----O-
// OOOOOOOO-
PrintTiles(radar)
// Test all expected values
// Test the expected values
assert.Equal(t, objects.Rover, radar[1+fullRange])
assert.Equal(t, objects.Rover, radar[4+4*fullRange])
for i := 0; i < 8; i++ {
assert.Equal(t, objects.LargeRock, radar[i])
assert.Equal(t, objects.LargeRock, radar[i+(7*9)])
assert.Equal(t, objects.LargeRock, radar[i*9])
assert.Equal(t, objects.LargeRock, radar[(i*9)+7])
}
}
func TestWorld_RoverStash(t *testing.T) {
world := NewWorld(2, 2)
world := NewWorld(2)
a, err := world.SpawnRover()
assert.NoError(t, err)
@ -147,15 +125,13 @@ func TestWorld_RoverStash(t *testing.T) {
err = world.WarpRover(a, pos)
assert.NoError(t, err, "Failed to set position for rover")
err = world.Atlas.SetTile(pos, objects.SmallRock)
assert.NoError(t, err, "Failed to set tile to rock")
world.Atlas.SetTile(pos, objects.SmallRock)
o, err := world.RoverStash(a)
assert.NoError(t, err, "Failed to stash")
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
tile, err := world.Atlas.GetTile(pos)
assert.NoError(t, err, "Failed to get tile")
tile := world.Atlas.GetTile(pos)
assert.Equal(t, objects.Empty, tile, "Stash failed to remove object from atlas")
inv, err := world.RoverInventory(a)
@ -164,7 +140,7 @@ func TestWorld_RoverStash(t *testing.T) {
}
func TestWorld_RoverDamage(t *testing.T) {
world := NewWorld(2, 2)
world := NewWorld(2)
a, err := world.SpawnRover()
assert.NoError(t, err)
@ -179,8 +155,7 @@ func TestWorld_RoverDamage(t *testing.T) {
info, err := world.GetRover(a)
assert.NoError(t, err, "couldn't get rover info")
err = world.Atlas.SetTile(vector.Vector{X: 0.0, Y: 1.0}, objects.LargeRock)
assert.NoError(t, err, "Failed to set tile to rock")
world.Atlas.SetTile(vector.Vector{X: 0.0, Y: 1.0}, objects.LargeRock)
vec, err := world.MoveRover(a, bearing.North)
assert.NoError(t, err, "Failed to move rover")
@ -192,7 +167,7 @@ func TestWorld_RoverDamage(t *testing.T) {
}
func TestWorld_RoverRepair(t *testing.T) {
world := NewWorld(2, 2)
world := NewWorld(2)
a, err := world.SpawnRover()
assert.NoError(t, err)
@ -207,15 +182,13 @@ func TestWorld_RoverRepair(t *testing.T) {
originalInfo, err := world.GetRover(a)
assert.NoError(t, err, "couldn't get rover info")
err = world.Atlas.SetTile(pos, objects.SmallRock)
assert.NoError(t, err, "Failed to set tile to rock")
world.Atlas.SetTile(pos, objects.SmallRock)
o, err := world.RoverStash(a)
assert.NoError(t, err, "Failed to stash")
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
err = world.Atlas.SetTile(vector.Vector{X: 0.0, Y: 1.0}, objects.LargeRock)
assert.NoError(t, err, "Failed to set tile to rock")
world.Atlas.SetTile(vector.Vector{X: 0.0, Y: 1.0}, objects.LargeRock)
vec, err := world.MoveRover(a, bearing.North)
assert.NoError(t, err, "Failed to move rover")