Split Atlas chunks into tiles and objects
This commit is contained in:
parent
74dcae6542
commit
062f9cfec8
11 changed files with 284 additions and 169 deletions
|
@ -1,6 +1,7 @@
|
|||
package game
|
||||
|
||||
import (
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
"github.com/mdiluz/rove/pkg/vector"
|
||||
)
|
||||
|
||||
|
@ -16,7 +17,7 @@ type Rover struct {
|
|||
Range int `json:"range"`
|
||||
|
||||
// Inventory represents any items the rover is carrying
|
||||
Inventory []byte `json:"inventory"`
|
||||
Inventory []objects.Object `json:"inventory"`
|
||||
|
||||
// Integrity represents current rover health
|
||||
Integrity int `json:"integrity"`
|
||||
|
|
|
@ -101,8 +101,8 @@ func (w *World) SpawnRover() (string, error) {
|
|||
|
||||
// Seach until we error (run out of world)
|
||||
for {
|
||||
tile := w.Atlas.GetTile(rover.Pos)
|
||||
if !objects.IsBlocking(tile) {
|
||||
_, obj := w.Atlas.QueryPosition(rover.Pos)
|
||||
if !obj.IsBlocking() {
|
||||
break
|
||||
} else {
|
||||
// Try and spawn to the east of the blockage
|
||||
|
@ -136,13 +136,11 @@ func (w *World) DestroyRover(rover string) error {
|
|||
w.worldMutex.Lock()
|
||||
defer w.worldMutex.Unlock()
|
||||
|
||||
i, ok := w.Rovers[rover]
|
||||
_, ok := w.Rovers[rover]
|
||||
if !ok {
|
||||
return fmt.Errorf("no rover matching id")
|
||||
}
|
||||
|
||||
// Clear the tile
|
||||
w.Atlas.SetTile(i.Pos, objects.Empty)
|
||||
delete(w.Rovers, rover)
|
||||
return nil
|
||||
}
|
||||
|
@ -175,7 +173,7 @@ func (w *World) SetRoverPosition(rover string, pos vector.Vector) error {
|
|||
}
|
||||
|
||||
// RoverInventory returns the inventory of a requested rover
|
||||
func (w *World) RoverInventory(rover string) ([]byte, error) {
|
||||
func (w *World) RoverInventory(rover string) ([]objects.Object, error) {
|
||||
w.worldMutex.RLock()
|
||||
defer w.worldMutex.RUnlock()
|
||||
|
||||
|
@ -201,8 +199,8 @@ func (w *World) WarpRover(rover string, pos vector.Vector) error {
|
|||
}
|
||||
|
||||
// Check the tile is not blocked
|
||||
tile := w.Atlas.GetTile(pos)
|
||||
if objects.IsBlocking(tile) {
|
||||
_, obj := w.Atlas.QueryPosition(pos)
|
||||
if obj.IsBlocking() {
|
||||
return fmt.Errorf("can't warp rover to occupied tile, check before warping")
|
||||
}
|
||||
|
||||
|
@ -224,8 +222,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
|
||||
tile := w.Atlas.GetTile(newPos)
|
||||
if !objects.IsBlocking(tile) {
|
||||
_, obj := w.Atlas.QueryPosition(newPos)
|
||||
if !obj.IsBlocking() {
|
||||
// Perform the move
|
||||
i.Pos = newPos
|
||||
w.Rovers[rover] = i
|
||||
|
@ -243,34 +241,35 @@ func (w *World) MoveRover(rover string, b bearing.Bearing) (vector.Vector, error
|
|||
}
|
||||
|
||||
// RoverStash will stash an item at the current rovers position
|
||||
func (w *World) RoverStash(rover string) (byte, error) {
|
||||
func (w *World) RoverStash(rover string) (objects.Type, error) {
|
||||
w.worldMutex.Lock()
|
||||
defer w.worldMutex.Unlock()
|
||||
|
||||
r, ok := w.Rovers[rover]
|
||||
if !ok {
|
||||
return objects.Empty, fmt.Errorf("no rover matching id")
|
||||
return objects.None, fmt.Errorf("no rover matching id")
|
||||
}
|
||||
|
||||
tile := w.Atlas.GetTile(r.Pos)
|
||||
if !objects.IsStashable(tile) {
|
||||
return objects.Empty, nil
|
||||
_, obj := w.Atlas.QueryPosition(r.Pos)
|
||||
if !obj.IsStashable() {
|
||||
return objects.None, nil
|
||||
}
|
||||
|
||||
r.Inventory = append(r.Inventory, tile)
|
||||
r.Inventory = append(r.Inventory, obj)
|
||||
w.Rovers[rover] = r
|
||||
w.Atlas.SetTile(r.Pos, objects.Empty)
|
||||
return tile, nil
|
||||
w.Atlas.SetObject(r.Pos, objects.Object{Type: objects.None})
|
||||
return obj.Type, nil
|
||||
}
|
||||
|
||||
// RadarFromRover can be used to query what a rover can currently see
|
||||
func (w *World) RadarFromRover(rover string) ([]byte, error) {
|
||||
func (w *World) RadarFromRover(rover string) (radar []byte, objs []byte, err error) {
|
||||
w.worldMutex.RLock()
|
||||
defer w.worldMutex.RUnlock()
|
||||
|
||||
r, ok := w.Rovers[rover]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no rover matching id")
|
||||
err = fmt.Errorf("no rover matching id")
|
||||
return
|
||||
}
|
||||
|
||||
// The radar should span in range direction on each axis, plus the row/column the rover is currently on
|
||||
|
@ -288,18 +287,19 @@ func (w *World) RadarFromRover(rover string) ([]byte, error) {
|
|||
}
|
||||
|
||||
// Gather up all tiles within the range
|
||||
var radar = make([]byte, radarSpan*radarSpan)
|
||||
radar = make([]byte, radarSpan*radarSpan)
|
||||
objs = make([]byte, radarSpan*radarSpan)
|
||||
for j := radarMin.Y; j <= radarMax.Y; j++ {
|
||||
for i := radarMin.X; i <= radarMax.X; i++ {
|
||||
q := vector.Vector{X: i, Y: j}
|
||||
|
||||
tile := w.Atlas.GetTile(q)
|
||||
tile, obj := w.Atlas.QueryPosition(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
|
||||
|
||||
objs[index] = byte(obj.Type)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -312,14 +312,11 @@ func (w *World) RadarFromRover(rover string) ([]byte, error) {
|
|||
if dist.X <= r.Range && dist.Y <= r.Range {
|
||||
relative := r.Pos.Added(radarMin.Negated())
|
||||
index := relative.X + relative.Y*radarSpan
|
||||
radar[index] = objects.Rover
|
||||
objs[index] = byte(objects.Rover)
|
||||
}
|
||||
}
|
||||
|
||||
// Add this rover
|
||||
radar[len(radar)/2] = objects.Rover
|
||||
|
||||
return radar, nil
|
||||
return radar, objs, nil
|
||||
}
|
||||
|
||||
// Enqueue will queue the commands given
|
||||
|
@ -433,7 +430,14 @@ func PrintTiles(tiles []byte) {
|
|||
num := int(math.Sqrt(float64(len(tiles))))
|
||||
for j := num - 1; j >= 0; j-- {
|
||||
for i := 0; i < num; i++ {
|
||||
fmt.Printf("%c", tiles[i+num*j])
|
||||
|
||||
t := tiles[i+num*j]
|
||||
if t != 0 {
|
||||
fmt.Printf("%c", t)
|
||||
} else {
|
||||
fmt.Printf(" ")
|
||||
}
|
||||
|
||||
}
|
||||
fmt.Print("\n")
|
||||
}
|
||||
|
|
|
@ -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/objects"
|
||||
"github.com/mdiluz/rove/pkg/vector"
|
||||
|
@ -84,7 +85,7 @@ 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
|
||||
world.Atlas.SetTile(vector.Vector{X: 0, Y: 2}, objects.LargeRock)
|
||||
world.Atlas.SetObject(vector.Vector{X: 0, Y: 2}, objects.Object{Type: objects.LargeRock})
|
||||
newPos, err = world.MoveRover(a, b)
|
||||
assert.Equal(t, pos, newPos, "Failed to correctly not move position for rover into wall")
|
||||
}
|
||||
|
@ -102,14 +103,15 @@ 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")
|
||||
|
||||
radar, err := world.RadarFromRover(a)
|
||||
radar, objs, 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")
|
||||
assert.Equal(t, fullRange*fullRange, len(objs), "Radar returned wrong length")
|
||||
|
||||
// Test the expected values
|
||||
assert.Equal(t, objects.Rover, radar[1+fullRange])
|
||||
assert.Equal(t, objects.Rover, radar[4+4*fullRange])
|
||||
assert.Equal(t, byte(objects.Rover), objs[1+fullRange])
|
||||
assert.Equal(t, byte(objects.Rover), objs[4+4*fullRange])
|
||||
}
|
||||
|
||||
func TestWorld_RoverStash(t *testing.T) {
|
||||
|
@ -125,18 +127,20 @@ func TestWorld_RoverStash(t *testing.T) {
|
|||
err = world.WarpRover(a, pos)
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
|
||||
world.Atlas.SetTile(pos, objects.SmallRock)
|
||||
// Set to a traversible tile
|
||||
world.Atlas.SetTile(pos, atlas.TileRock)
|
||||
world.Atlas.SetObject(pos, objects.Object{Type: 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 := world.Atlas.GetTile(pos)
|
||||
assert.Equal(t, objects.Empty, tile, "Stash failed to remove object from atlas")
|
||||
_, obj := world.Atlas.QueryPosition(pos)
|
||||
assert.Equal(t, objects.None, obj.Type, "Stash failed to remove object from atlas")
|
||||
|
||||
inv, err := world.RoverInventory(a)
|
||||
assert.NoError(t, err, "Failed to get inventory")
|
||||
assert.Equal(t, objects.SmallRock, inv[0])
|
||||
assert.Equal(t, objects.Object{Type: objects.SmallRock}, inv[0])
|
||||
}
|
||||
|
||||
func TestWorld_RoverDamage(t *testing.T) {
|
||||
|
@ -155,7 +159,7 @@ func TestWorld_RoverDamage(t *testing.T) {
|
|||
info, err := world.GetRover(a)
|
||||
assert.NoError(t, err, "couldn't get rover info")
|
||||
|
||||
world.Atlas.SetTile(vector.Vector{X: 0.0, Y: 1.0}, objects.LargeRock)
|
||||
world.Atlas.SetObject(vector.Vector{X: 0.0, Y: 1.0}, objects.Object{Type: objects.LargeRock})
|
||||
|
||||
vec, err := world.MoveRover(a, bearing.North)
|
||||
assert.NoError(t, err, "Failed to move rover")
|
||||
|
@ -176,19 +180,22 @@ func TestWorld_RoverRepair(t *testing.T) {
|
|||
Y: 0.0,
|
||||
}
|
||||
|
||||
world.Atlas.SetTile(pos, atlas.TileNone)
|
||||
world.Atlas.SetObject(pos, objects.Object{Type: objects.None})
|
||||
|
||||
err = world.WarpRover(a, pos)
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
|
||||
originalInfo, err := world.GetRover(a)
|
||||
assert.NoError(t, err, "couldn't get rover info")
|
||||
|
||||
world.Atlas.SetTile(pos, objects.SmallRock)
|
||||
world.Atlas.SetObject(pos, objects.Object{Type: 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")
|
||||
|
||||
world.Atlas.SetTile(vector.Vector{X: 0.0, Y: 1.0}, objects.LargeRock)
|
||||
world.Atlas.SetObject(vector.Vector{X: 0.0, Y: 1.0}, objects.Object{Type: objects.LargeRock})
|
||||
|
||||
vec, err := world.MoveRover(a, bearing.North)
|
||||
assert.NoError(t, err, "Failed to move rover")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue