Move object into atlas

This commit is contained in:
Marc Di Luzio 2020-07-10 18:39:33 +01:00
parent f40f7123d4
commit f0ab2abf6e
8 changed files with 66 additions and 72 deletions

View file

@ -12,7 +12,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/mdiluz/rove/pkg/version"
"golang.org/x/net/context"
@ -288,7 +287,7 @@ func InnerMain(command string, args ...string) error {
for i := 0; i < num; i++ {
t := response.Tiles[i+num*j]
o := response.Objects[i+num*j]
if o != byte(objects.None) {
if o != byte(atlas.ObjectNone) {
fmt.Printf("%c", o)
} else if t != byte(atlas.TileNone) {
fmt.Printf("%c", t)

View file

@ -2,7 +2,6 @@ package atlas
import (
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
)
// Tile describes the type of terrain
@ -28,8 +27,8 @@ type Atlas interface {
SetTile(v maths.Vector, tile Tile)
// SetObject will set a location on the Atlas to contain an object
SetObject(v maths.Vector, obj objects.Object)
SetObject(v maths.Vector, obj Object)
// QueryPosition queries a position on the atlas
QueryPosition(v maths.Vector) (byte, objects.Object)
QueryPosition(v maths.Vector) (byte, Object)
}

View file

@ -5,7 +5,6 @@ import (
"testing"
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/stretchr/testify/assert"
)
@ -186,14 +185,14 @@ func TestAtlas_GetSetObject(t *testing.T) {
assert.NotNil(t, a)
// Set the origin tile to 1 and test it
a.SetObject(maths.Vector{X: 0, Y: 0}, objects.Object{Type: objects.LargeRock})
a.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: ObjectLargeRock})
_, obj := a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, objects.Object{Type: objects.LargeRock}, obj)
assert.Equal(t, Object{Type: ObjectLargeRock}, obj)
// Set another tile to 1 and test it
a.SetObject(maths.Vector{X: 5, Y: -2}, objects.Object{Type: objects.SmallRock})
a.SetObject(maths.Vector{X: 5, Y: -2}, Object{Type: ObjectSmallRock})
_, obj = a.QueryPosition(maths.Vector{X: 5, Y: -2})
assert.Equal(t, objects.Object{Type: objects.SmallRock}, obj)
assert.Equal(t, Object{Type: ObjectSmallRock}, obj)
}
func TestAtlas_Grown(t *testing.T) {
@ -239,11 +238,11 @@ func TestAtlas_GetSetCorrect(t *testing.T) {
pos := maths.Vector{X: x, Y: y}
a.SetTile(pos, TileRock)
a.SetObject(pos, objects.Object{Type: objects.LargeRock})
a.SetObject(pos, Object{Type: ObjectLargeRock})
tile, obj := a.QueryPosition(pos)
assert.Equal(t, TileRock, Tile(tile))
assert.Equal(t, objects.Object{Type: objects.LargeRock}, obj)
assert.Equal(t, Object{Type: ObjectLargeRock}, obj)
}
}
@ -260,7 +259,7 @@ func TestAtlas_WorldGen(t *testing.T) {
for j := num - 1; j >= 0; j-- {
for i := 0; i < num; i++ {
t, o := a.QueryPosition(maths.Vector{X: i, Y: j})
if o.Type != objects.None {
if o.Type != ObjectNone {
fmt.Printf("%c", o.Type)
} else if t != byte(TileNone) {
fmt.Printf("%c", t)

View file

@ -5,7 +5,6 @@ import (
"math/rand"
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/ojrac/opensimplex-go"
)
@ -16,7 +15,7 @@ type chunk struct {
// Objects represents the objects within the chunk
// only one possible object per tile for now
Objects map[int]objects.Object `json:"objects"`
Objects map[int]Object `json:"objects"`
}
// chunkBasedAtlas represents a grid of Chunks
@ -71,14 +70,14 @@ func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile Tile) {
}
// SetObject sets the object on a tile
func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj objects.Object) {
func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj 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 maths.Vector) (byte, objects.Object) {
func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (byte, Object) {
c := a.worldSpaceToChunkWithGrow(v)
local := a.worldSpaceToChunkLocal(v)
a.populate(c)
@ -100,7 +99,7 @@ func (a *chunkBasedAtlas) populate(chunk int) {
}
c.Tiles = make([]byte, a.ChunkSize*a.ChunkSize)
c.Objects = make(map[int]objects.Object)
c.Objects = make(map[int]Object)
origin := a.chunkOriginInWorldSpace(chunk)
for i := 0; i < a.ChunkSize; i++ {
@ -121,15 +120,15 @@ func (a *chunkBasedAtlas) populate(chunk int) {
// Get the object noise value for this location
o := a.objectNoise.Eval2(float64(origin.X+i)/objectNoiseScale, float64(origin.Y+j)/objectNoiseScale)
var obj = objects.None
var obj = ObjectNone
switch {
case o > 0.6:
obj = objects.LargeRock
obj = ObjectLargeRock
case o > 0.5:
obj = objects.SmallRock
obj = ObjectSmallRock
}
if obj != objects.None {
c.Objects[j*a.ChunkSize+i] = objects.Object{Type: obj}
if obj != ObjectNone {
c.Objects[j*a.ChunkSize+i] = Object{Type: obj}
}
}
}
@ -137,9 +136,9 @@ func (a *chunkBasedAtlas) populate(chunk int) {
// Set up any objects
for i := 0; i < len(c.Tiles); i++ {
if rand.Intn(16) == 0 {
c.Objects[i] = objects.Object{Type: objects.LargeRock}
c.Objects[i] = Object{Type: ObjectLargeRock}
} else if rand.Intn(32) == 0 {
c.Objects[i] = objects.Object{Type: objects.SmallRock}
c.Objects[i] = Object{Type: ObjectSmallRock}
}
}
@ -155,12 +154,12 @@ func (a *chunkBasedAtlas) setTile(chunk int, local maths.Vector, tile byte) {
}
// setObject sets an object in a specific chunk
func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object objects.Object) {
func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object Object) {
a.populate(chunk)
c := a.Chunks[chunk]
i := a.chunkTileIndex(local)
if object.Type != objects.None {
if object.Type != ObjectNone {
c.Objects[i] = object
} else {
delete(c.Objects, i)

View file

@ -1,21 +1,21 @@
package objects
package atlas
// Type represents an object type
type Type byte
// Types of objects
const (
// None represents no object at all
None = Type(0)
// ObjectNone represents no object at all
ObjectNone = Type(0)
// Rover represents a live rover
Rover = Type('R')
// ObjectRover represents a live rover
ObjectRover = Type('R')
// SmallRock is a small stashable rock
SmallRock = Type('o')
// ObjectSmallRock is a small stashable rock
ObjectSmallRock = Type('o')
// LargeRock is a large blocking rock
LargeRock = Type('O')
// ObjectLargeRock is a large blocking rock
ObjectLargeRock = Type('O')
)
// Object represents an object in the world
@ -26,8 +26,8 @@ type Object struct {
// IsBlocking checks if an object is a blocking object
func (o *Object) IsBlocking() bool {
var blocking = [...]Type{
Rover,
LargeRock,
ObjectRover,
ObjectLargeRock,
}
for _, t := range blocking {
@ -41,7 +41,7 @@ func (o *Object) IsBlocking() bool {
// IsStashable checks if an object is stashable
func (o *Object) IsStashable() bool {
var stashable = [...]Type{
SmallRock,
ObjectSmallRock,
}
for _, t := range stashable {

View file

@ -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"`

View file

@ -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)
}
}

View file

@ -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