Move vector into maths package

This commit is contained in:
Marc Di Luzio 2020-07-10 18:22:59 +01:00
parent 97d3be000b
commit 5b1fe61097
11 changed files with 131 additions and 134 deletions

View file

@ -1,8 +1,8 @@
package atlas
import (
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/vector"
)
// Tile describes the type of terrain
@ -25,11 +25,11 @@ const (
// Atlas represents a 2D world atlas of tiles and objects
type Atlas interface {
// SetTile sets a location on the Atlas to a type of tile
SetTile(v vector.Vector, tile Tile)
SetTile(v maths.Vector, tile Tile)
// SetObject will set a location on the Atlas to contain an object
SetObject(v vector.Vector, obj objects.Object)
SetObject(v maths.Vector, obj objects.Object)
// QueryPosition queries a position on the atlas
QueryPosition(v vector.Vector) (byte, objects.Object)
QueryPosition(v maths.Vector) (byte, objects.Object)
}

View file

@ -4,8 +4,8 @@ import (
"fmt"
"testing"
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/vector"
"github.com/stretchr/testify/assert"
)
@ -21,49 +21,49 @@ func TestAtlas_toChunk(t *testing.T) {
assert.NotNil(t, a)
// Get a tile to spawn the chunks
a.QueryPosition(vector.Vector{X: -1, Y: -1})
a.QueryPosition(vector.Vector{X: 0, Y: 0})
a.QueryPosition(maths.Vector{X: -1, Y: -1})
a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
// 2 | 3
// -----
// 0 | 1
chunkID := a.worldSpaceToChunkIndex(vector.Vector{X: 0, Y: 0})
chunkID := a.worldSpaceToChunkIndex(maths.Vector{X: 0, Y: 0})
assert.Equal(t, 3, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 0, Y: -1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 0, Y: -1})
assert.Equal(t, 1, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -1, Y: -1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -1, Y: -1})
assert.Equal(t, 0, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -1, Y: 0})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -1, Y: 0})
assert.Equal(t, 2, chunkID)
a = NewChunkAtlas(2).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn the chunks
a.QueryPosition(vector.Vector{X: -2, Y: -2})
a.QueryPosition(maths.Vector{X: -2, Y: -2})
assert.Equal(t, 2*2, len(a.Chunks))
a.QueryPosition(vector.Vector{X: 1, Y: 1})
a.QueryPosition(maths.Vector{X: 1, Y: 1})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
// 2 | 3
// -----
// 0 | 1
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: 1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 1})
assert.Equal(t, 3, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: -2})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: -2})
assert.Equal(t, 1, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -2, Y: -2})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -2, Y: -2})
assert.Equal(t, 0, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -2, Y: 1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -2, Y: 1})
assert.Equal(t, 2, chunkID)
a = NewChunkAtlas(2).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn a 4x4 grid of chunks
a.QueryPosition(vector.Vector{X: 3, Y: 3})
a.QueryPosition(maths.Vector{X: 3, Y: 3})
assert.Equal(t, 2*2, len(a.Chunks))
a.QueryPosition(vector.Vector{X: -3, Y: -3})
a.QueryPosition(maths.Vector{X: -3, Y: -3})
assert.Equal(t, 4*4, len(a.Chunks))
// Chunks should look like:
@ -74,19 +74,19 @@ func TestAtlas_toChunk(t *testing.T) {
// 4 | 5 || 6 | 7
// ----------------
// 0 | 1 || 2 | 3
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: 3})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 3})
assert.Equal(t, 14, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: -3})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: -3})
assert.Equal(t, 2, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -1, Y: -1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -1, Y: -1})
assert.Equal(t, 5, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -2, Y: 2})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -2, Y: 2})
assert.Equal(t, 13, chunkID)
a = NewChunkAtlas(3).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn a 4x4 grid of chunks
a.QueryPosition(vector.Vector{X: 3, Y: 3})
a.QueryPosition(maths.Vector{X: 3, Y: 3})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
@ -94,13 +94,13 @@ func TestAtlas_toChunk(t *testing.T) {
// -------
// || 0| 1
// =======
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: 1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 1})
assert.Equal(t, 0, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 3, Y: 1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 3, Y: 1})
assert.Equal(t, 1, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: 4})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 4})
assert.Equal(t, 2, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 5, Y: 5})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 5, Y: 5})
assert.Equal(t, 3, chunkID)
}
@ -109,36 +109,36 @@ func TestAtlas_toWorld(t *testing.T) {
assert.NotNil(t, a)
// Get a tile to spawn some chunks
a.QueryPosition(vector.Vector{X: -1, Y: -1})
a.QueryPosition(maths.Vector{X: -1, Y: -1})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
// 2 | 3
// -----
// 0 | 1
assert.Equal(t, vector.Vector{X: -1, Y: -1}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, vector.Vector{X: 0, Y: -1}, a.chunkOriginInWorldSpace(1))
assert.Equal(t, maths.Vector{X: -1, Y: -1}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, maths.Vector{X: 0, Y: -1}, a.chunkOriginInWorldSpace(1))
a = NewChunkAtlas(2).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn the chunks
a.QueryPosition(vector.Vector{X: -2, Y: -2})
a.QueryPosition(maths.Vector{X: -2, Y: -2})
assert.Equal(t, 2*2, len(a.Chunks))
a.QueryPosition(vector.Vector{X: 1, Y: 1})
a.QueryPosition(maths.Vector{X: 1, Y: 1})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
// 2 | 3
// -----
// 0 | 1
assert.Equal(t, vector.Vector{X: -2, Y: -2}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, vector.Vector{X: -2, Y: 0}, a.chunkOriginInWorldSpace(2))
assert.Equal(t, maths.Vector{X: -2, Y: -2}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, maths.Vector{X: -2, Y: 0}, a.chunkOriginInWorldSpace(2))
a = NewChunkAtlas(2).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn a 4x4 grid of chunks
a.QueryPosition(vector.Vector{X: 3, Y: 3})
a.QueryPosition(maths.Vector{X: 3, Y: 3})
assert.Equal(t, 2*2, len(a.Chunks))
a.QueryPosition(vector.Vector{X: -3, Y: -3})
a.QueryPosition(maths.Vector{X: -3, Y: -3})
assert.Equal(t, 4*4, len(a.Chunks))
// Chunks should look like:
@ -149,13 +149,13 @@ func TestAtlas_toWorld(t *testing.T) {
// 4 | 5 || 6 | 7
// ----------------
// 0 | 1 || 2 | 3
assert.Equal(t, vector.Vector{X: -4, Y: -4}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, vector.Vector{X: 2, Y: -2}, a.chunkOriginInWorldSpace(7))
assert.Equal(t, maths.Vector{X: -4, Y: -4}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, maths.Vector{X: 2, Y: -2}, a.chunkOriginInWorldSpace(7))
a = NewChunkAtlas(3).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn a 4x4 grid of chunks
a.QueryPosition(vector.Vector{X: 3, Y: 3})
a.QueryPosition(maths.Vector{X: 3, Y: 3})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
@ -163,7 +163,7 @@ func TestAtlas_toWorld(t *testing.T) {
// -------
// || 0| 1
// =======
assert.Equal(t, vector.Vector{X: 0, Y: 0}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, maths.Vector{X: 0, Y: 0}, a.chunkOriginInWorldSpace(0))
}
func TestAtlas_GetSetTile(t *testing.T) {
@ -171,13 +171,13 @@ func TestAtlas_GetSetTile(t *testing.T) {
assert.NotNil(t, a)
// Set the origin tile to 1 and test it
a.SetTile(vector.Vector{X: 0, Y: 0}, 1)
tile, _ := a.QueryPosition(vector.Vector{X: 0, Y: 0})
a.SetTile(maths.Vector{X: 0, Y: 0}, 1)
tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, byte(1), tile)
// Set another tile to 1 and test it
a.SetTile(vector.Vector{X: 5, Y: -2}, 2)
tile, _ = a.QueryPosition(vector.Vector{X: 5, Y: -2})
a.SetTile(maths.Vector{X: 5, Y: -2}, 2)
tile, _ = a.QueryPosition(maths.Vector{X: 5, Y: -2})
assert.Equal(t, byte(2), tile)
}
@ -186,13 +186,13 @@ func TestAtlas_GetSetObject(t *testing.T) {
assert.NotNil(t, a)
// Set the origin tile to 1 and test it
a.SetObject(vector.Vector{X: 0, Y: 0}, objects.Object{Type: objects.LargeRock})
_, obj := a.QueryPosition(vector.Vector{X: 0, Y: 0})
a.SetObject(maths.Vector{X: 0, Y: 0}, objects.Object{Type: objects.LargeRock})
_, obj := a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, objects.Object{Type: objects.LargeRock}, obj)
// Set another tile to 1 and test it
a.SetObject(vector.Vector{X: 5, Y: -2}, objects.Object{Type: objects.SmallRock})
_, obj = a.QueryPosition(vector.Vector{X: 5, Y: -2})
a.SetObject(maths.Vector{X: 5, Y: -2}, objects.Object{Type: objects.SmallRock})
_, obj = a.QueryPosition(maths.Vector{X: 5, Y: -2})
assert.Equal(t, objects.Object{Type: objects.SmallRock}, obj)
}
@ -203,27 +203,27 @@ func TestAtlas_Grown(t *testing.T) {
assert.Equal(t, 1, len(a.Chunks))
// Set a few tiles to values
a.SetTile(vector.Vector{X: 0, Y: 0}, 1)
a.SetTile(vector.Vector{X: -1, Y: -1}, 2)
a.SetTile(vector.Vector{X: 1, Y: -2}, 3)
a.SetTile(maths.Vector{X: 0, Y: 0}, 1)
a.SetTile(maths.Vector{X: -1, Y: -1}, 2)
a.SetTile(maths.Vector{X: 1, Y: -2}, 3)
// Check tile values
tile, _ := a.QueryPosition(vector.Vector{X: 0, Y: 0})
tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, byte(1), tile)
tile, _ = a.QueryPosition(vector.Vector{X: -1, Y: -1})
tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1})
assert.Equal(t, byte(2), tile)
tile, _ = a.QueryPosition(vector.Vector{X: 1, Y: -2})
tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2})
assert.Equal(t, byte(3), tile)
tile, _ = a.QueryPosition(vector.Vector{X: 0, Y: 0})
tile, _ = a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, byte(1), tile)
tile, _ = a.QueryPosition(vector.Vector{X: -1, Y: -1})
tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1})
assert.Equal(t, byte(2), tile)
tile, _ = a.QueryPosition(vector.Vector{X: 1, Y: -2})
tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2})
assert.Equal(t, byte(3), tile)
}
@ -237,7 +237,7 @@ func TestAtlas_GetSetCorrect(t *testing.T) {
assert.NotNil(t, a)
assert.Equal(t, 1, len(a.Chunks))
pos := vector.Vector{X: x, Y: y}
pos := maths.Vector{X: x, Y: y}
a.SetTile(pos, TileRock)
a.SetObject(pos, objects.Object{Type: objects.LargeRock})
tile, obj := a.QueryPosition(pos)
@ -253,13 +253,13 @@ func TestAtlas_GetSetCorrect(t *testing.T) {
func TestAtlas_WorldGen(t *testing.T) {
a := NewChunkAtlas(8)
// Spawn a large world
_, _ = a.QueryPosition(vector.Vector{X: 20, Y: 20})
_, _ = a.QueryPosition(maths.Vector{X: 20, Y: 20})
// Print out the world for manual evaluation
num := 20
for j := num - 1; j >= 0; j-- {
for i := 0; i < num; i++ {
t, o := a.QueryPosition(vector.Vector{X: i, Y: j})
t, o := a.QueryPosition(maths.Vector{X: i, Y: j})
if o.Type != objects.None {
fmt.Printf("%c", o.Type)
} else if t != byte(TileNone) {

View file

@ -6,7 +6,6 @@ import (
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/vector"
"github.com/ojrac/opensimplex-go"
)
@ -27,10 +26,10 @@ type chunkBasedAtlas struct {
Chunks []chunk `json:"chunks"`
// LowerBound is the origin of the bottom left corner of the current chunks in world space (current chunks cover >= this value)
LowerBound vector.Vector `json:"lowerBound"`
LowerBound maths.Vector `json:"lowerBound"`
// UpperBound is the top left corner of the current chunks (curent chunks cover < this value)
UpperBound vector.Vector `json:"upperBound"`
UpperBound maths.Vector `json:"upperBound"`
// ChunkSize is the x/y dimensions of each square chunk
ChunkSize int `json:"chunksize"`
@ -54,8 +53,8 @@ func NewChunkAtlas(chunkSize int) Atlas {
a := chunkBasedAtlas{
ChunkSize: chunkSize,
Chunks: make([]chunk, 1),
LowerBound: vector.Vector{X: 0, Y: 0},
UpperBound: vector.Vector{X: chunkSize, Y: chunkSize},
LowerBound: maths.Vector{X: 0, Y: 0},
UpperBound: maths.Vector{X: chunkSize, Y: chunkSize},
terrainNoise: opensimplex.New(noiseSeed),
objectNoise: opensimplex.New(noiseSeed),
}
@ -65,21 +64,21 @@ func NewChunkAtlas(chunkSize int) Atlas {
}
// SetTile sets an individual tile's kind
func (a *chunkBasedAtlas) SetTile(v vector.Vector, tile Tile) {
func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile Tile) {
c := a.worldSpaceToChunkWithGrow(v)
local := a.worldSpaceToChunkLocal(v)
a.setTile(c, local, byte(tile))
}
// SetObject sets the object on a tile
func (a *chunkBasedAtlas) SetObject(v vector.Vector, obj objects.Object) {
func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj objects.Object) {
c := a.worldSpaceToChunkWithGrow(v)
local := a.worldSpaceToChunkLocal(v)
a.setObject(c, local, obj)
}
// QueryPosition will return information for a specific position
func (a *chunkBasedAtlas) QueryPosition(v vector.Vector) (byte, objects.Object) {
func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (byte, objects.Object) {
c := a.worldSpaceToChunkWithGrow(v)
local := a.worldSpaceToChunkLocal(v)
a.populate(c)
@ -89,7 +88,7 @@ func (a *chunkBasedAtlas) QueryPosition(v vector.Vector) (byte, objects.Object)
}
// chunkTileID returns the tile index within a chunk
func (a *chunkBasedAtlas) chunkTileIndex(local vector.Vector) int {
func (a *chunkBasedAtlas) chunkTileIndex(local maths.Vector) int {
return local.X + local.Y*a.ChunkSize
}
@ -148,7 +147,7 @@ func (a *chunkBasedAtlas) populate(chunk int) {
}
// setTile sets a tile in a specific chunk
func (a *chunkBasedAtlas) setTile(chunk int, local vector.Vector, tile byte) {
func (a *chunkBasedAtlas) setTile(chunk int, local maths.Vector, tile byte) {
a.populate(chunk)
c := a.Chunks[chunk]
c.Tiles[a.chunkTileIndex(local)] = tile
@ -156,7 +155,7 @@ func (a *chunkBasedAtlas) setTile(chunk int, local vector.Vector, tile byte) {
}
// setObject sets an object in a specific chunk
func (a *chunkBasedAtlas) setObject(chunk int, local vector.Vector, object objects.Object) {
func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object objects.Object) {
a.populate(chunk)
c := a.Chunks[chunk]
@ -170,12 +169,12 @@ func (a *chunkBasedAtlas) setObject(chunk int, local vector.Vector, object objec
}
// worldSpaceToChunkLocal gets a chunk local coordinate for a tile
func (a *chunkBasedAtlas) worldSpaceToChunkLocal(v vector.Vector) vector.Vector {
return vector.Vector{X: maths.Pmod(v.X, a.ChunkSize), Y: maths.Pmod(v.Y, a.ChunkSize)}
func (a *chunkBasedAtlas) worldSpaceToChunkLocal(v maths.Vector) maths.Vector {
return maths.Vector{X: maths.Pmod(v.X, a.ChunkSize), Y: maths.Pmod(v.Y, a.ChunkSize)}
}
// worldSpaceToChunkID gets the current chunk ID for a position in the world
func (a *chunkBasedAtlas) worldSpaceToChunkIndex(v vector.Vector) int {
func (a *chunkBasedAtlas) worldSpaceToChunkIndex(v maths.Vector) int {
// Shift the vector by our current min
v = v.Added(a.LowerBound.Negated())
@ -191,13 +190,13 @@ func (a *chunkBasedAtlas) worldSpaceToChunkIndex(v vector.Vector) int {
}
// chunkOriginInWorldSpace returns the origin of the chunk in world space
func (a *chunkBasedAtlas) chunkOriginInWorldSpace(chunk int) vector.Vector {
func (a *chunkBasedAtlas) chunkOriginInWorldSpace(chunk int) maths.Vector {
// Calculate the width
width := a.UpperBound.X - a.LowerBound.X
widthInChunks := width / a.ChunkSize
// Reverse the along the corridor and up the stairs
v := vector.Vector{
v := maths.Vector{
X: chunk % widthInChunks,
Y: chunk / widthInChunks,
}
@ -208,15 +207,15 @@ func (a *chunkBasedAtlas) chunkOriginInWorldSpace(chunk int) vector.Vector {
}
// getNewBounds gets new lower and upper bounds for the world space given a vector
func (a *chunkBasedAtlas) getNewBounds(v vector.Vector) (lower vector.Vector, upper vector.Vector) {
lower = vector.Min(v, a.LowerBound)
upper = vector.Max(v.Added(vector.Vector{X: 1, Y: 1}), a.UpperBound)
func (a *chunkBasedAtlas) getNewBounds(v maths.Vector) (lower maths.Vector, upper maths.Vector) {
lower = maths.Min2(v, a.LowerBound)
upper = maths.Max2(v.Added(maths.Vector{X: 1, Y: 1}), a.UpperBound)
lower = vector.Vector{
lower = maths.Vector{
X: maths.RoundDown(lower.X, a.ChunkSize),
Y: maths.RoundDown(lower.Y, a.ChunkSize),
}
upper = vector.Vector{
upper = maths.Vector{
X: maths.RoundUp(upper.X, a.ChunkSize),
Y: maths.RoundUp(upper.Y, a.ChunkSize),
}
@ -224,7 +223,7 @@ func (a *chunkBasedAtlas) getNewBounds(v vector.Vector) (lower vector.Vector, up
}
// worldSpaceToTrunkWithGrow will expand the current atlas for a given world space position if needed
func (a *chunkBasedAtlas) worldSpaceToChunkWithGrow(v vector.Vector) int {
func (a *chunkBasedAtlas) worldSpaceToChunkWithGrow(v maths.Vector) int {
// If we're within bounds, just return the current chunk
if v.X >= a.LowerBound.X && v.Y >= a.LowerBound.Y && v.X < a.UpperBound.X && v.Y < a.UpperBound.Y {
return a.worldSpaceToChunkIndex(v)

View file

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

View file

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

View file

@ -1,9 +1,7 @@
package vector
package maths
import (
"math"
"github.com/mdiluz/rove/pkg/maths"
)
// Vector desribes a 3D vector
@ -71,15 +69,15 @@ func (v Vector) DividedFloor(val int) Vector {
// Abs returns an absolute version of the vector
func (v Vector) Abs() Vector {
return Vector{maths.Abs(v.X), maths.Abs(v.Y)}
return Vector{Abs(v.X), Abs(v.Y)}
}
// Min returns the minimum values in both vectors
func Min(v1 Vector, v2 Vector) Vector {
return Vector{maths.Min(v1.X, v2.X), maths.Min(v1.Y, v2.Y)}
// Min2 returns the minimum values in both vectors
func Min2(v1 Vector, v2 Vector) Vector {
return Vector{Min(v1.X, v2.X), Min(v1.Y, v2.Y)}
}
// Max returns the max values in both vectors
func Max(v1 Vector, v2 Vector) Vector {
return Vector{maths.Max(v1.X, v2.X), maths.Max(v1.Y, v2.Y)}
// Max2 returns the max values in both vectors
func Max2(v1 Vector, v2 Vector) Vector {
return Vector{Max(v1.X, v2.X), Max(v1.Y, v2.Y)}
}

View file

@ -1,4 +1,4 @@
package vector
package maths
import (
"math"

View file

@ -3,8 +3,8 @@ package rove
import (
"testing"
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/roveapi"
"github.com/mdiluz/rove/pkg/vector"
"github.com/stretchr/testify/assert"
)
@ -12,7 +12,7 @@ func TestCommand_Move(t *testing.T) {
world := NewWorld(8)
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{
pos := maths.Vector{
X: 1.0,
Y: 2.0,
}
@ -30,7 +30,7 @@ func TestCommand_Move(t *testing.T) {
newPos, err := world.RoverPosition(a)
assert.NoError(t, err, "Failed to set position for rover")
pos.Add(vector.Vector{X: 0.0, Y: 1})
pos.Add(maths.Vector{X: 0.0, Y: 1})
assert.Equal(t, pos, newPos, "Failed to correctly set position for rover")
}
@ -38,7 +38,7 @@ func TestCommand_Recharge(t *testing.T) {
world := NewWorld(8)
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{
pos := maths.Vector{
X: 1.0,
Y: 2.0,
}

View file

@ -5,8 +5,8 @@ import (
"log"
"time"
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/vector"
)
// RoverLogEntry describes a single log entry for the rover
@ -24,7 +24,7 @@ type Rover struct {
Name string `json:"name"`
// Pos represents where this rover is in the world
Pos vector.Vector `json:"pos"`
Pos maths.Vector `json:"pos"`
// Range represents the distance the unit's radar can see
Range int `json:"range"`

View file

@ -11,9 +11,9 @@ 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/roveapi"
"github.com/mdiluz/rove/pkg/vector"
)
// World describes a self contained universe and everything in it
@ -103,7 +103,7 @@ func (w *World) SpawnRover() (string, error) {
}
// Spawn in a random place near the origin
rover.Pos = vector.Vector{
rover.Pos = maths.Vector{
X: 10 - rand.Intn(20),
Y: 10 - rand.Intn(20),
}
@ -115,7 +115,7 @@ func (w *World) SpawnRover() (string, error) {
break
} else {
// Try and spawn to the east of the blockage
rover.Pos.Add(vector.Vector{X: 1, Y: 0})
rover.Pos.Add(maths.Vector{X: 1, Y: 0})
}
}
@ -215,19 +215,19 @@ func (w *World) DestroyRover(rover string) error {
}
// RoverPosition returns the position of the rover
func (w *World) RoverPosition(rover string) (vector.Vector, error) {
func (w *World) RoverPosition(rover string) (maths.Vector, error) {
w.worldMutex.RLock()
defer w.worldMutex.RUnlock()
i, ok := w.Rovers[rover]
if !ok {
return vector.Vector{}, fmt.Errorf("no rover matching id")
return maths.Vector{}, fmt.Errorf("no rover matching id")
}
return i.Pos, nil
}
// SetRoverPosition sets the position of the rover
func (w *World) SetRoverPosition(rover string, pos vector.Vector) error {
func (w *World) SetRoverPosition(rover string, pos maths.Vector) error {
w.worldMutex.Lock()
defer w.worldMutex.Unlock()
@ -254,7 +254,7 @@ func (w *World) RoverInventory(rover string) ([]objects.Object, error) {
}
// WarpRover sets an rovers position
func (w *World) WarpRover(rover string, pos vector.Vector) error {
func (w *World) WarpRover(rover string, pos maths.Vector) error {
w.worldMutex.Lock()
defer w.worldMutex.Unlock()
@ -279,13 +279,13 @@ func (w *World) WarpRover(rover string, pos vector.Vector) error {
}
// MoveRover attempts to move a rover in a specific direction
func (w *World) MoveRover(rover string, b bearing.Bearing) (vector.Vector, error) {
func (w *World) MoveRover(rover string, b bearing.Bearing) (maths.Vector, error) {
w.worldMutex.Lock()
defer w.worldMutex.Unlock()
i, ok := w.Rovers[rover]
if !ok {
return vector.Vector{}, fmt.Errorf("no rover matching id")
return maths.Vector{}, fmt.Errorf("no rover matching id")
}
// Ensure the rover has energy
@ -368,11 +368,11 @@ func (w *World) RadarFromRover(rover string) (radar []byte, objs []byte, err err
roverPos := r.Pos
// Get the radar min and max values
radarMin := vector.Vector{
radarMin := maths.Vector{
X: roverPos.X - r.Range,
Y: roverPos.Y - r.Range,
}
radarMax := vector.Vector{
radarMax := maths.Vector{
X: roverPos.X + r.Range,
Y: roverPos.Y + r.Range,
}
@ -382,7 +382,7 @@ func (w *World) RadarFromRover(rover string) (radar []byte, objs []byte, err err
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}
q := maths.Vector{X: i, Y: j}
tile, obj := w.Atlas.QueryPosition(q)

View file

@ -5,9 +5,9 @@ import (
"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/roveapi"
"github.com/mdiluz/rove/pkg/vector"
"github.com/stretchr/testify/assert"
)
@ -68,7 +68,7 @@ func TestWorld_GetSetMovePosition(t *testing.T) {
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{
pos := maths.Vector{
X: 0.0,
Y: 0.0,
}
@ -83,7 +83,7 @@ func TestWorld_GetSetMovePosition(t *testing.T) {
b := bearing.North
newPos, err = world.MoveRover(a, b)
assert.NoError(t, err, "Failed to set position for rover")
pos.Add(vector.Vector{X: 0, Y: 1})
pos.Add(maths.Vector{X: 0, Y: 1})
assert.Equal(t, pos, newPos, "Failed to correctly move position for rover")
rover, err := world.GetRover(a)
@ -92,7 +92,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(vector.Vector{X: 0, Y: 2}, objects.Object{Type: objects.LargeRock})
world.Atlas.SetObject(maths.Vector{X: 0, Y: 2}, objects.Object{Type: objects.LargeRock})
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")
@ -111,9 +111,9 @@ func TestWorld_RadarFromRover(t *testing.T) {
assert.NoError(t, err)
// Warp the rovers into position
bpos := vector.Vector{X: -3, Y: -3}
bpos := maths.Vector{X: -3, Y: -3}
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")
assert.NoError(t, world.WarpRover(a, maths.Vector{X: 0, Y: 0}), "Failed to warp rover")
radar, objs, err := world.RadarFromRover(a)
assert.NoError(t, err, "Failed to get radar from rover")
@ -139,7 +139,7 @@ func TestWorld_RoverStash(t *testing.T) {
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{
pos := maths.Vector{
X: 0.0,
Y: 0.0,
}
@ -215,7 +215,7 @@ func TestWorld_RoverDamage(t *testing.T) {
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{
pos := maths.Vector{
X: 0.0,
Y: 0.0,
}
@ -226,7 +226,7 @@ func TestWorld_RoverDamage(t *testing.T) {
info, err := world.GetRover(a)
assert.NoError(t, err, "couldn't get rover info")
world.Atlas.SetObject(vector.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)
assert.NoError(t, err, "Failed to move rover")
@ -243,7 +243,7 @@ func TestWorld_RoverRepair(t *testing.T) {
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{
pos := maths.Vector{
X: 0.0,
Y: 0.0,
}
@ -263,7 +263,7 @@ func TestWorld_RoverRepair(t *testing.T) {
assert.NoError(t, err, "Failed to stash")
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
world.Atlas.SetObject(vector.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
vec, err := world.MoveRover(a, bearing.North)
@ -378,8 +378,8 @@ func TestWorld_Broadcast(t *testing.T) {
assert.NoError(t, err)
// Warp rovers near to eachother
assert.NoError(t, world.WarpRover(a, vector.Vector{X: 0, Y: 0}))
assert.NoError(t, world.WarpRover(b, vector.Vector{X: 1, Y: 0}))
assert.NoError(t, world.WarpRover(a, maths.Vector{X: 0, Y: 0}))
assert.NoError(t, world.WarpRover(b, maths.Vector{X: 1, Y: 0}))
// Broadcast from a
assert.NoError(t, world.RoverBroadcast(a, []byte{'A', 'B', 'C'}))
@ -396,8 +396,8 @@ 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(vector.Vector{X: ra.Range, Y: 0}, objects.Object{Type: objects.None})
assert.NoError(t, world.WarpRover(b, vector.Vector{X: ra.Range, Y: 0}))
world.Atlas.SetObject(maths.Vector{X: ra.Range, Y: 0}, objects.Object{Type: objects.None})
assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range, Y: 0}))
// Broadcast from a again
assert.NoError(t, world.RoverBroadcast(a, []byte{'X', 'Y', 'Z'}))
@ -413,8 +413,8 @@ 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(vector.Vector{X: ra.Range + 1, Y: 0}, objects.Object{Type: objects.None})
assert.NoError(t, world.WarpRover(b, vector.Vector{X: ra.Range + 1, Y: 0}))
world.Atlas.SetObject(maths.Vector{X: ra.Range + 1, Y: 0}, objects.Object{Type: objects.None})
assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range + 1, Y: 0}))
// Broadcast from a again
assert.NoError(t, world.RoverBroadcast(a, []byte{'H', 'J', 'K'}))