Move bearing into maths

This commit is contained in:
Marc Di Luzio 2020-07-10 18:24:54 +01:00
parent 5b1fe61097
commit f40f7123d4
5 changed files with 17 additions and 22 deletions

View file

@ -11,7 +11,7 @@ import (
"time" "time"
"github.com/mdiluz/rove/pkg/atlas" "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/objects"
"github.com/mdiluz/rove/pkg/roveapi" "github.com/mdiluz/rove/pkg/roveapi"
"github.com/mdiluz/rove/pkg/version" "github.com/mdiluz/rove/pkg/version"
@ -216,7 +216,7 @@ func InnerMain(command string, args ...string) error {
i++ i++
if len(args) == i { if len(args) == i {
return fmt.Errorf("move command must be passed bearing") return fmt.Errorf("move command must be passed bearing")
} else if _, err := bearing.FromString(args[i]); err != nil { } else if _, err := maths.FromString(args[i]); err != nil {
return err return err
} }
commands = append(commands, commands = append(commands,

View file

@ -1,10 +1,8 @@
package bearing package maths
import ( import (
"fmt" "fmt"
"strings" "strings"
"github.com/mdiluz/rove/pkg/maths"
) )
// Bearing describes a compass direction // Bearing describes a compass direction
@ -67,7 +65,7 @@ func FromString(s string) (Bearing, error) {
return -1, fmt.Errorf("unknown bearing: %s", s) return -1, fmt.Errorf("unknown bearing: %s", s)
} }
var bearingVectors = []maths.Vector{ var bearingVectors = []Vector{
{X: 0, Y: 1}, // N {X: 0, Y: 1}, // N
{X: 1, Y: 1}, // NE {X: 1, Y: 1}, // NE
{X: 1, Y: 0}, // E {X: 1, Y: 0}, // E
@ -79,6 +77,6 @@ var bearingVectors = []maths.Vector{
} }
// Vector converts a Direction to a Vector // Vector converts a Direction to a Vector
func (d Bearing) Vector() maths.Vector { func (d Bearing) Vector() Vector {
return bearingVectors[d] return bearingVectors[d]
} }

View file

@ -1,9 +1,8 @@
package bearing package maths
import ( import (
"testing" "testing"
"github.com/mdiluz/rove/pkg/maths"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -12,7 +11,7 @@ func TestDirection(t *testing.T) {
assert.Equal(t, "North", dir.String()) assert.Equal(t, "North", dir.String())
assert.Equal(t, "N", dir.ShortString()) assert.Equal(t, "N", dir.ShortString())
assert.Equal(t, maths.Vector{X: 0, Y: 1}, dir.Vector()) assert.Equal(t, Vector{X: 0, Y: 1}, dir.Vector())
dir, err := FromString("N") dir, err := FromString("N")
assert.NoError(t, err) assert.NoError(t, err)

View file

@ -10,7 +10,6 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/atlas"
"github.com/mdiluz/rove/pkg/bearing"
"github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects" "github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/roveapi" "github.com/mdiluz/rove/pkg/roveapi"
@ -279,7 +278,7 @@ func (w *World) WarpRover(rover string, pos maths.Vector) error {
} }
// MoveRover attempts to move a rover in a specific direction // MoveRover attempts to move a rover in a specific direction
func (w *World) MoveRover(rover string, b bearing.Bearing) (maths.Vector, error) { func (w *World) MoveRover(rover string, b maths.Bearing) (maths.Vector, error) {
w.worldMutex.Lock() w.worldMutex.Lock()
defer w.worldMutex.Unlock() defer w.worldMutex.Unlock()
@ -428,7 +427,7 @@ func (w *World) Enqueue(rover string, commands ...Command) error {
for _, c := range commands { for _, c := range commands {
switch c.Command { switch c.Command {
case roveapi.CommandType_move: case roveapi.CommandType_move:
if _, err := bearing.FromString(c.Bearing); err != nil { if _, err := maths.FromString(c.Bearing); err != nil {
return fmt.Errorf("unknown bearing: %s", c.Bearing) return fmt.Errorf("unknown bearing: %s", c.Bearing)
} }
case roveapi.CommandType_broadcast: case roveapi.CommandType_broadcast:
@ -507,7 +506,7 @@ func (w *World) ExecuteCommand(c *Command, rover string) (err error) {
switch c.Command { switch c.Command {
case roveapi.CommandType_move: case roveapi.CommandType_move:
if dir, err := bearing.FromString(c.Bearing); err != nil { if dir, err := maths.FromString(c.Bearing); err != nil {
return err return err
} else if _, err := w.MoveRover(rover, dir); err != nil { } else if _, err := w.MoveRover(rover, dir); err != nil {
return err return err

View file

@ -4,7 +4,6 @@ import (
"testing" "testing"
"github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/atlas"
"github.com/mdiluz/rove/pkg/bearing"
"github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects" "github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/roveapi" "github.com/mdiluz/rove/pkg/roveapi"
@ -80,7 +79,7 @@ func TestWorld_GetSetMovePosition(t *testing.T) {
assert.NoError(t, err, "Failed to set position for rover") assert.NoError(t, err, "Failed to set position for rover")
assert.Equal(t, pos, newPos, "Failed to correctly set position for rover") assert.Equal(t, pos, newPos, "Failed to correctly set position for rover")
b := bearing.North b := maths.North
newPos, err = world.MoveRover(a, b) newPos, err = world.MoveRover(a, b)
assert.NoError(t, err, "Failed to set position for rover") assert.NoError(t, err, "Failed to set position for rover")
pos.Add(maths.Vector{X: 0, Y: 1}) pos.Add(maths.Vector{X: 0, Y: 1})
@ -228,7 +227,7 @@ func TestWorld_RoverDamage(t *testing.T) {
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}, objects.Object{Type: objects.LargeRock})
vec, err := world.MoveRover(a, bearing.North) vec, err := world.MoveRover(a, maths.North)
assert.NoError(t, err, "Failed to move rover") assert.NoError(t, err, "Failed to move rover")
assert.Equal(t, pos, vec, "Rover managed to move into large rock") assert.Equal(t, pos, vec, "Rover managed to move into large rock")
@ -266,7 +265,7 @@ func TestWorld_RoverRepair(t *testing.T) {
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}, objects.Object{Type: objects.LargeRock})
// Try and bump into the rock // Try and bump into the rock
vec, err := world.MoveRover(a, bearing.North) vec, err := world.MoveRover(a, maths.North)
assert.NoError(t, err, "Failed to move rover") assert.NoError(t, err, "Failed to move rover")
assert.Equal(t, pos, vec, "Rover managed to move into large rock") assert.Equal(t, pos, vec, "Rover managed to move into large rock")
@ -313,13 +312,13 @@ func TestWorld_Charge(t *testing.T) {
assert.NoError(t, err, "Failed to get position for rover") assert.NoError(t, err, "Failed to get position for rover")
// Ensure the path ahead is empty // Ensure the path ahead is empty
world.Atlas.SetTile(initialPos.Added(bearing.North.Vector()), atlas.TileRock) world.Atlas.SetTile(initialPos.Added(maths.North.Vector()), atlas.TileRock)
world.Atlas.SetObject(initialPos.Added(bearing.North.Vector()), objects.Object{Type: objects.None}) world.Atlas.SetObject(initialPos.Added(maths.North.Vector()), objects.Object{Type: objects.None})
// Try and move north (along unblocked path) // Try and move north (along unblocked path)
newPos, err := world.MoveRover(a, bearing.North) newPos, err := world.MoveRover(a, maths.North)
assert.NoError(t, err, "Failed to set position for rover") assert.NoError(t, err, "Failed to set position for rover")
assert.Equal(t, initialPos.Added(bearing.North.Vector()), newPos, "Failed to correctly move position for rover") assert.Equal(t, initialPos.Added(maths.North.Vector()), newPos, "Failed to correctly move position for rover")
// Ensure rover lost charge // Ensure rover lost charge
rover, err := world.GetRover(a) rover, err := world.GetRover(a)