Move object into atlas
This commit is contained in:
parent
f40f7123d4
commit
f0ab2abf6e
8 changed files with 66 additions and 72 deletions
|
@ -5,8 +5,8 @@ import (
|
|||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/atlas"
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
)
|
||||
|
||||
// RoverLogEntry describes a single log entry for the rover
|
||||
|
@ -30,7 +30,7 @@ type Rover struct {
|
|||
Range int `json:"range"`
|
||||
|
||||
// Inventory represents any items the rover is carrying
|
||||
Inventory []objects.Object `json:"inventory"`
|
||||
Inventory []atlas.Object `json:"inventory"`
|
||||
|
||||
// Capacity is the maximum number of inventory items
|
||||
Capacity int `json:"capacity"`
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/google/uuid"
|
||||
"github.com/mdiluz/rove/pkg/atlas"
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
"github.com/mdiluz/rove/pkg/roveapi"
|
||||
)
|
||||
|
||||
|
@ -241,7 +240,7 @@ func (w *World) SetRoverPosition(rover string, pos maths.Vector) error {
|
|||
}
|
||||
|
||||
// RoverInventory returns the inventory of a requested rover
|
||||
func (w *World) RoverInventory(rover string) ([]objects.Object, error) {
|
||||
func (w *World) RoverInventory(rover string) ([]atlas.Object, error) {
|
||||
w.worldMutex.RLock()
|
||||
defer w.worldMutex.RUnlock()
|
||||
|
||||
|
@ -319,35 +318,35 @@ func (w *World) MoveRover(rover string, b maths.Bearing) (maths.Vector, error) {
|
|||
}
|
||||
|
||||
// RoverStash will stash an item at the current rovers position
|
||||
func (w *World) RoverStash(rover string) (objects.Type, error) {
|
||||
func (w *World) RoverStash(rover string) (atlas.Type, error) {
|
||||
w.worldMutex.Lock()
|
||||
defer w.worldMutex.Unlock()
|
||||
|
||||
r, ok := w.Rovers[rover]
|
||||
if !ok {
|
||||
return objects.None, fmt.Errorf("no rover matching id")
|
||||
return atlas.ObjectNone, fmt.Errorf("no rover matching id")
|
||||
}
|
||||
|
||||
// Can't pick up when full
|
||||
if len(r.Inventory) >= r.Capacity {
|
||||
return objects.None, nil
|
||||
return atlas.ObjectNone, nil
|
||||
}
|
||||
|
||||
// Ensure the rover has energy
|
||||
if r.Charge <= 0 {
|
||||
return objects.None, nil
|
||||
return atlas.ObjectNone, nil
|
||||
}
|
||||
r.Charge--
|
||||
|
||||
_, obj := w.Atlas.QueryPosition(r.Pos)
|
||||
if !obj.IsStashable() {
|
||||
return objects.None, nil
|
||||
return atlas.ObjectNone, nil
|
||||
}
|
||||
|
||||
r.AddLogEntryf("stashed %c", obj.Type)
|
||||
r.Inventory = append(r.Inventory, obj)
|
||||
w.Rovers[rover] = r
|
||||
w.Atlas.SetObject(r.Pos, objects.Object{Type: objects.None})
|
||||
w.Atlas.SetObject(r.Pos, atlas.Object{Type: atlas.ObjectNone})
|
||||
return obj.Type, nil
|
||||
}
|
||||
|
||||
|
@ -402,7 +401,7 @@ func (w *World) RadarFromRover(rover string) (radar []byte, objs []byte, err err
|
|||
if dist.X <= r.Range && dist.Y <= r.Range {
|
||||
relative := r.Pos.Added(radarMin.Negated())
|
||||
index := relative.X + relative.Y*radarSpan
|
||||
objs[index] = byte(objects.Rover)
|
||||
objs[index] = byte(atlas.ObjectRover)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
|
||||
"github.com/mdiluz/rove/pkg/atlas"
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
"github.com/mdiluz/rove/pkg/roveapi"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -91,7 +90,7 @@ func TestWorld_GetSetMovePosition(t *testing.T) {
|
|||
assert.Contains(t, rover.Logs[len(rover.Logs)-1].Text, "moved", "Rover logs should contain the move")
|
||||
|
||||
// Place a tile in front of the rover
|
||||
world.Atlas.SetObject(maths.Vector{X: 0, Y: 2}, objects.Object{Type: objects.LargeRock})
|
||||
world.Atlas.SetObject(maths.Vector{X: 0, Y: 2}, atlas.Object{Type: atlas.ObjectLargeRock})
|
||||
newPos, err = world.MoveRover(a, b)
|
||||
assert.NoError(t, err, "Failed to move rover")
|
||||
assert.Equal(t, pos, newPos, "Failed to correctly not move position for rover into wall")
|
||||
|
@ -121,8 +120,8 @@ func TestWorld_RadarFromRover(t *testing.T) {
|
|||
assert.Equal(t, fullRange*fullRange, len(objs), "Radar returned wrong length")
|
||||
|
||||
// Test the expected values
|
||||
assert.Equal(t, byte(objects.Rover), objs[1+fullRange])
|
||||
assert.Equal(t, byte(objects.Rover), objs[4+4*fullRange])
|
||||
assert.Equal(t, byte(atlas.ObjectRover), objs[1+fullRange])
|
||||
assert.Equal(t, byte(atlas.ObjectRover), objs[4+4*fullRange])
|
||||
|
||||
// Check the radar results are stable
|
||||
radar1, objs1, err := world.RadarFromRover(a)
|
||||
|
@ -143,7 +142,7 @@ func TestWorld_RoverStash(t *testing.T) {
|
|||
Y: 0.0,
|
||||
}
|
||||
|
||||
world.Atlas.SetObject(pos, objects.Object{Type: objects.None})
|
||||
world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectNone})
|
||||
err = world.WarpRover(a, pos)
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
|
||||
|
@ -155,22 +154,22 @@ func TestWorld_RoverStash(t *testing.T) {
|
|||
|
||||
for i := 0; i < rover.Capacity; i++ {
|
||||
// Place an object
|
||||
world.Atlas.SetObject(pos, objects.Object{Type: objects.SmallRock})
|
||||
world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectSmallRock})
|
||||
|
||||
// Pick it up
|
||||
o, err := world.RoverStash(a)
|
||||
assert.NoError(t, err, "Failed to stash")
|
||||
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
|
||||
assert.Equal(t, atlas.ObjectSmallRock, o, "Failed to get correct object")
|
||||
|
||||
// Check it's gone
|
||||
_, obj := world.Atlas.QueryPosition(pos)
|
||||
assert.Equal(t, objects.None, obj.Type, "Stash failed to remove object from atlas")
|
||||
assert.Equal(t, atlas.ObjectNone, obj.Type, "Stash failed to remove object from atlas")
|
||||
|
||||
// Check we have it
|
||||
inv, err := world.RoverInventory(a)
|
||||
assert.NoError(t, err, "Failed to get inventory")
|
||||
assert.Equal(t, i+1, len(inv))
|
||||
assert.Equal(t, objects.Object{Type: objects.SmallRock}, inv[i])
|
||||
assert.Equal(t, atlas.Object{Type: atlas.ObjectSmallRock}, inv[i])
|
||||
|
||||
// Check that this did reduce the charge
|
||||
info, err := world.GetRover(a)
|
||||
|
@ -187,16 +186,16 @@ func TestWorld_RoverStash(t *testing.T) {
|
|||
}
|
||||
|
||||
// Place an object
|
||||
world.Atlas.SetObject(pos, objects.Object{Type: objects.SmallRock})
|
||||
world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectSmallRock})
|
||||
|
||||
// Try to pick it up
|
||||
o, err := world.RoverStash(a)
|
||||
assert.NoError(t, err, "Failed to stash")
|
||||
assert.Equal(t, objects.None, o, "Failed to get correct object")
|
||||
assert.Equal(t, atlas.ObjectNone, o, "Failed to get correct object")
|
||||
|
||||
// Check it's still there
|
||||
_, obj := world.Atlas.QueryPosition(pos)
|
||||
assert.Equal(t, objects.SmallRock, obj.Type, "Stash failed to remove object from atlas")
|
||||
assert.Equal(t, atlas.ObjectSmallRock, obj.Type, "Stash failed to remove object from atlas")
|
||||
|
||||
// Check we don't have it
|
||||
inv, err := world.RoverInventory(a)
|
||||
|
@ -225,7 +224,7 @@ func TestWorld_RoverDamage(t *testing.T) {
|
|||
info, err := world.GetRover(a)
|
||||
assert.NoError(t, err, "couldn't get rover info")
|
||||
|
||||
world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, objects.Object{Type: objects.LargeRock})
|
||||
world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, atlas.Object{Type: atlas.ObjectLargeRock})
|
||||
|
||||
vec, err := world.MoveRover(a, maths.North)
|
||||
assert.NoError(t, err, "Failed to move rover")
|
||||
|
@ -248,7 +247,7 @@ func TestWorld_RoverRepair(t *testing.T) {
|
|||
}
|
||||
|
||||
world.Atlas.SetTile(pos, atlas.TileNone)
|
||||
world.Atlas.SetObject(pos, objects.Object{Type: objects.None})
|
||||
world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectNone})
|
||||
|
||||
err = world.WarpRover(a, pos)
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
|
@ -257,12 +256,12 @@ func TestWorld_RoverRepair(t *testing.T) {
|
|||
assert.NoError(t, err, "couldn't get rover info")
|
||||
|
||||
// Pick up something to repair with
|
||||
world.Atlas.SetObject(pos, objects.Object{Type: objects.SmallRock})
|
||||
world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectSmallRock})
|
||||
o, err := world.RoverStash(a)
|
||||
assert.NoError(t, err, "Failed to stash")
|
||||
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
|
||||
assert.Equal(t, atlas.ObjectSmallRock, o, "Failed to get correct object")
|
||||
|
||||
world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, objects.Object{Type: objects.LargeRock})
|
||||
world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, atlas.Object{Type: atlas.ObjectLargeRock})
|
||||
|
||||
// Try and bump into the rock
|
||||
vec, err := world.MoveRover(a, maths.North)
|
||||
|
@ -282,10 +281,10 @@ func TestWorld_RoverRepair(t *testing.T) {
|
|||
assert.Contains(t, newinfo.Logs[len(newinfo.Logs)-1].Text, "repair", "Rover logs should contain the repair")
|
||||
|
||||
// Check again that it can't repair past the max
|
||||
world.Atlas.SetObject(pos, objects.Object{Type: objects.SmallRock})
|
||||
world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectSmallRock})
|
||||
o, err = world.RoverStash(a)
|
||||
assert.NoError(t, err, "Failed to stash")
|
||||
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
|
||||
assert.Equal(t, atlas.ObjectSmallRock, o, "Failed to get correct object")
|
||||
|
||||
err = world.ExecuteCommand(&Command{Command: roveapi.CommandType_repair}, a)
|
||||
assert.NoError(t, err, "Failed to repair rover")
|
||||
|
@ -313,7 +312,7 @@ func TestWorld_Charge(t *testing.T) {
|
|||
|
||||
// Ensure the path ahead is empty
|
||||
world.Atlas.SetTile(initialPos.Added(maths.North.Vector()), atlas.TileRock)
|
||||
world.Atlas.SetObject(initialPos.Added(maths.North.Vector()), objects.Object{Type: objects.None})
|
||||
world.Atlas.SetObject(initialPos.Added(maths.North.Vector()), atlas.Object{Type: atlas.ObjectNone})
|
||||
|
||||
// Try and move north (along unblocked path)
|
||||
newPos, err := world.MoveRover(a, maths.North)
|
||||
|
@ -395,7 +394,7 @@ func TestWorld_Broadcast(t *testing.T) {
|
|||
assert.Contains(t, rb.Logs[len(rb.Logs)-1].Text, "ABC", "Rover A should have logged it's broadcast")
|
||||
|
||||
// Warp B outside of the range of A
|
||||
world.Atlas.SetObject(maths.Vector{X: ra.Range, Y: 0}, objects.Object{Type: objects.None})
|
||||
world.Atlas.SetObject(maths.Vector{X: ra.Range, Y: 0}, atlas.Object{Type: atlas.ObjectNone})
|
||||
assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range, Y: 0}))
|
||||
|
||||
// Broadcast from a again
|
||||
|
@ -412,7 +411,7 @@ func TestWorld_Broadcast(t *testing.T) {
|
|||
assert.Contains(t, rb.Logs[len(rb.Logs)-1].Text, "XYZ", "Rover A should have logged it's broadcast")
|
||||
|
||||
// Warp B outside of the range of A
|
||||
world.Atlas.SetObject(maths.Vector{X: ra.Range + 1, Y: 0}, objects.Object{Type: objects.None})
|
||||
world.Atlas.SetObject(maths.Vector{X: ra.Range + 1, Y: 0}, atlas.Object{Type: atlas.ObjectNone})
|
||||
assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range + 1, Y: 0}))
|
||||
|
||||
// Broadcast from a again
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue