From a0b811a659c81dca13b11ce5cc6122da52b9491c Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 11:46:37 +0100 Subject: [PATCH 01/12] Move glyph definitions into a central type --- pkg/atlas/atlas.go | 8 ++++---- pkg/atlas/glyph.go | 27 +++++++++++++++++++++++++++ pkg/atlas/objects.go | 6 +++--- 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 pkg/atlas/glyph.go diff --git a/pkg/atlas/atlas.go b/pkg/atlas/atlas.go index 1a2f3fb..73ea341 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/atlas/atlas.go @@ -9,16 +9,16 @@ type Tile byte const ( // TileNone is a keyword for nothing - TileNone = Tile(0) + TileNone = Tile(GlyphNone) // TileRock is solid rock ground - TileRock = Tile('-') + TileRock = Tile(GlyphRock) // TileGravel is loose rocks - TileGravel = Tile(':') + TileGravel = Tile(GlyphGravel) // TileSand is sand - TileSand = Tile('~') + TileSand = Tile(GlyphSand) ) // Atlas represents a 2D world atlas of tiles and objects diff --git a/pkg/atlas/glyph.go b/pkg/atlas/glyph.go new file mode 100644 index 0000000..c39f1be --- /dev/null +++ b/pkg/atlas/glyph.go @@ -0,0 +1,27 @@ +package atlas + +// Glyph represents the text representation of something in the game +type Glyph byte + +const ( + // GlyphNone is a keyword for nothing + GlyphNone = Glyph(0) + + // GlyphRock is solid rock ground + GlyphRock = Glyph('-') + + // GlyphGravel is loose rocks + GlyphGravel = Glyph(':') + + // GlyphSand is sand + GlyphSand = Glyph('~') + + // GlyphRover represents a live rover + GlyphRover = Glyph('R') + + // GlyphSmallRock is a small stashable rock + GlyphSmallRock = Glyph('o') + + // GlyphLargeRock is a large blocking rock + GlyphLargeRock = Glyph('O') +) diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index 68b4ef8..c397ec1 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -6,13 +6,13 @@ type Type byte // Types of objects const ( // ObjectNone represents no object at all - ObjectNone = Type(0) + ObjectNone = Type(GlyphNone) // ObjectRover represents a live rover - ObjectRover = Type('R') + ObjectRover = Type(GlyphRover) // ObjectSmallRock is a small stashable rock - ObjectSmallRock = Type('o') + ObjectSmallRock = Type(GlyphSmallRock) // ObjectLargeRock is a large blocking rock ObjectLargeRock = Type('O') From 53d6ad08d965ca434de5f67b79e961942354de8f Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 11:47:19 +0100 Subject: [PATCH 02/12] Rename Type to ObjectType --- pkg/atlas/objects.go | 19 ++++++++++--------- pkg/rove/world.go | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index c397ec1..cb71229 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -1,31 +1,32 @@ package atlas -// Type represents an object type -type Type byte +// ObjectType represents an object type +type ObjectType byte // Types of objects const ( // ObjectNone represents no object at all - ObjectNone = Type(GlyphNone) + ObjectNone = ObjectType(GlyphNone) // ObjectRover represents a live rover - ObjectRover = Type(GlyphRover) + ObjectRover = ObjectType(GlyphRover) // ObjectSmallRock is a small stashable rock - ObjectSmallRock = Type(GlyphSmallRock) + ObjectSmallRock = ObjectType(GlyphSmallRock) // ObjectLargeRock is a large blocking rock - ObjectLargeRock = Type('O') + ObjectLargeRock = ObjectType(GlyphLargeRock) ) // Object represents an object in the world type Object struct { - Type Type `json:"type"` + // The type of the object + Type ObjectType `json:"type"` } // IsBlocking checks if an object is a blocking object func (o *Object) IsBlocking() bool { - var blocking = [...]Type{ + var blocking = [...]ObjectType{ ObjectRover, ObjectLargeRock, } @@ -40,7 +41,7 @@ func (o *Object) IsBlocking() bool { // IsStashable checks if an object is stashable func (o *Object) IsStashable() bool { - var stashable = [...]Type{ + var stashable = [...]ObjectType{ ObjectSmallRock, } diff --git a/pkg/rove/world.go b/pkg/rove/world.go index 04cad81..177dbba 100644 --- a/pkg/rove/world.go +++ b/pkg/rove/world.go @@ -318,7 +318,7 @@ 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) (atlas.Type, error) { +func (w *World) RoverStash(rover string) (atlas.ObjectType, error) { w.worldMutex.Lock() defer w.worldMutex.Unlock() From acdd01909315c4a6f6dbae7064540f3a21b36008 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 11:50:19 +0100 Subject: [PATCH 03/12] Add Glyph methods to convert to a glyph --- pkg/atlas/atlas.go | 19 +++++++++++++++++++ pkg/atlas/objects.go | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/pkg/atlas/atlas.go b/pkg/atlas/atlas.go index 73ea341..aaa7fd8 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/atlas/atlas.go @@ -1,6 +1,8 @@ package atlas import ( + "log" + "github.com/mdiluz/rove/pkg/maths" ) @@ -21,6 +23,23 @@ const ( TileSand = Tile(GlyphSand) ) +// Glyph returns the glyph for this tile type +func (t Tile) Glyph() Glyph { + switch t { + case TileNone: + return GlyphNone + case TileRock: + return GlyphRock + case TileGravel: + return GlyphGravel + case TileSand: + return GlyphSand + } + + log.Fatalf("Unknown tile type: %c", t) + return GlyphNone +} + // 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 diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index cb71229..1b9eb87 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -1,5 +1,7 @@ package atlas +import "log" + // ObjectType represents an object type type ObjectType byte @@ -18,6 +20,23 @@ const ( ObjectLargeRock = ObjectType(GlyphLargeRock) ) +// Glyph returns the glyph for this object type +func (o ObjectType) Glyph() Glyph { + switch o { + case ObjectNone: + return GlyphNone + case ObjectRover: + return GlyphRover + case ObjectSmallRock: + return GlyphSmallRock + case ObjectLargeRock: + return GlyphLargeRock + } + + log.Fatalf("Unknown object type: %c", o) + return GlyphNone +} + // Object represents an object in the world type Object struct { // The type of the object From f6654360077cf4926a00915af7c3b59ae65a0e6f Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 11:54:11 +0100 Subject: [PATCH 04/12] Convert objects and tiles to base ints --- pkg/atlas/atlas.go | 8 ++++---- pkg/atlas/chunkAtlas.go | 2 +- pkg/atlas/objects.go | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/atlas/atlas.go b/pkg/atlas/atlas.go index aaa7fd8..e681ff2 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/atlas/atlas.go @@ -11,16 +11,16 @@ type Tile byte const ( // TileNone is a keyword for nothing - TileNone = Tile(GlyphNone) + TileNone = iota // TileRock is solid rock ground - TileRock = Tile(GlyphRock) + TileRock // TileGravel is loose rocks - TileGravel = Tile(GlyphGravel) + TileGravel // TileSand is sand - TileSand = Tile(GlyphSand) + TileSand ) // Glyph returns the glyph for this tile type diff --git a/pkg/atlas/chunkAtlas.go b/pkg/atlas/chunkAtlas.go index daee6e6..1d21741 100644 --- a/pkg/atlas/chunkAtlas.go +++ b/pkg/atlas/chunkAtlas.go @@ -128,7 +128,7 @@ func (a *chunkBasedAtlas) populate(chunk int) { obj = ObjectSmallRock } if obj != ObjectNone { - c.Objects[j*a.ChunkSize+i] = Object{Type: obj} + c.Objects[j*a.ChunkSize+i] = Object{Type: ObjectType(obj)} } } } diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index 1b9eb87..ac847fe 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -8,16 +8,16 @@ type ObjectType byte // Types of objects const ( // ObjectNone represents no object at all - ObjectNone = ObjectType(GlyphNone) + ObjectNone = iota // ObjectRover represents a live rover - ObjectRover = ObjectType(GlyphRover) + ObjectRover // ObjectSmallRock is a small stashable rock - ObjectSmallRock = ObjectType(GlyphSmallRock) + ObjectSmallRock // ObjectLargeRock is a large blocking rock - ObjectLargeRock = ObjectType(GlyphLargeRock) + ObjectLargeRock ) // Glyph returns the glyph for this object type From 7e41ac00286e5acafa2da750f05681d863fb3273 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 11:56:05 +0100 Subject: [PATCH 05/12] Rename the glyphs --- pkg/atlas/atlas.go | 6 +++--- pkg/atlas/atlas_test.go | 12 ++++++------ pkg/atlas/chunkAtlas.go | 8 ++++---- pkg/atlas/glyph.go | 24 ++++++++++++------------ pkg/atlas/objects.go | 24 ++++++++++++------------ pkg/rove/world.go | 2 +- pkg/rove/world_test.go | 28 ++++++++++++++-------------- 7 files changed, 52 insertions(+), 52 deletions(-) diff --git a/pkg/atlas/atlas.go b/pkg/atlas/atlas.go index e681ff2..305c355 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/atlas/atlas.go @@ -29,11 +29,11 @@ func (t Tile) Glyph() Glyph { case TileNone: return GlyphNone case TileRock: - return GlyphRock + return GlyphGroundRock case TileGravel: - return GlyphGravel + return GlyphGroundGravel case TileSand: - return GlyphSand + return GlyphGroundSand } log.Fatalf("Unknown tile type: %c", t) diff --git a/pkg/atlas/atlas_test.go b/pkg/atlas/atlas_test.go index 9305b07..5141371 100644 --- a/pkg/atlas/atlas_test.go +++ b/pkg/atlas/atlas_test.go @@ -185,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}, Object{Type: ObjectLargeRock}) + a.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: ObjectRockLarge}) _, obj := a.QueryPosition(maths.Vector{X: 0, Y: 0}) - assert.Equal(t, Object{Type: ObjectLargeRock}, obj) + assert.Equal(t, Object{Type: ObjectRockLarge}, obj) // Set another tile to 1 and test it - a.SetObject(maths.Vector{X: 5, Y: -2}, Object{Type: ObjectSmallRock}) + a.SetObject(maths.Vector{X: 5, Y: -2}, Object{Type: ObjectRockSmall}) _, obj = a.QueryPosition(maths.Vector{X: 5, Y: -2}) - assert.Equal(t, Object{Type: ObjectSmallRock}, obj) + assert.Equal(t, Object{Type: ObjectRockSmall}, obj) } func TestAtlas_Grown(t *testing.T) { @@ -238,11 +238,11 @@ func TestAtlas_GetSetCorrect(t *testing.T) { pos := maths.Vector{X: x, Y: y} a.SetTile(pos, TileRock) - a.SetObject(pos, Object{Type: ObjectLargeRock}) + a.SetObject(pos, Object{Type: ObjectRockLarge}) tile, obj := a.QueryPosition(pos) assert.Equal(t, TileRock, Tile(tile)) - assert.Equal(t, Object{Type: ObjectLargeRock}, obj) + assert.Equal(t, Object{Type: ObjectRockLarge}, obj) } } diff --git a/pkg/atlas/chunkAtlas.go b/pkg/atlas/chunkAtlas.go index 1d21741..99d4668 100644 --- a/pkg/atlas/chunkAtlas.go +++ b/pkg/atlas/chunkAtlas.go @@ -123,9 +123,9 @@ func (a *chunkBasedAtlas) populate(chunk int) { var obj = ObjectNone switch { case o > 0.6: - obj = ObjectLargeRock + obj = ObjectRockLarge case o > 0.5: - obj = ObjectSmallRock + obj = ObjectRockSmall } if obj != ObjectNone { c.Objects[j*a.ChunkSize+i] = Object{Type: ObjectType(obj)} @@ -136,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] = Object{Type: ObjectLargeRock} + c.Objects[i] = Object{Type: ObjectRockLarge} } else if rand.Intn(32) == 0 { - c.Objects[i] = Object{Type: ObjectSmallRock} + c.Objects[i] = Object{Type: ObjectRockSmall} } } diff --git a/pkg/atlas/glyph.go b/pkg/atlas/glyph.go index c39f1be..c5c0ad7 100644 --- a/pkg/atlas/glyph.go +++ b/pkg/atlas/glyph.go @@ -7,21 +7,21 @@ const ( // GlyphNone is a keyword for nothing GlyphNone = Glyph(0) - // GlyphRock is solid rock ground - GlyphRock = Glyph('-') + // GlyphGroundRock is solid rock ground + GlyphGroundRock = Glyph('-') - // GlyphGravel is loose rocks - GlyphGravel = Glyph(':') + // GlyphGroundGravel is loose rocks + GlyphGroundGravel = Glyph(':') - // GlyphSand is sand - GlyphSand = Glyph('~') + // GlyphGroundSand is sand + GlyphGroundSand = Glyph('~') - // GlyphRover represents a live rover - GlyphRover = Glyph('R') + // GlyphRoverLive represents a live rover + GlyphRoverLive = Glyph('R') - // GlyphSmallRock is a small stashable rock - GlyphSmallRock = Glyph('o') + // GlyphRockSmall is a small stashable rock + GlyphRockSmall = Glyph('o') - // GlyphLargeRock is a large blocking rock - GlyphLargeRock = Glyph('O') + // GlyphRockLarge is a large blocking rock + GlyphRockLarge = Glyph('O') ) diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index ac847fe..032ca10 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -11,13 +11,13 @@ const ( ObjectNone = iota // ObjectRover represents a live rover - ObjectRover + ObjectRoverLive // ObjectSmallRock is a small stashable rock - ObjectSmallRock + ObjectRockSmall // ObjectLargeRock is a large blocking rock - ObjectLargeRock + ObjectRockLarge ) // Glyph returns the glyph for this object type @@ -25,12 +25,12 @@ func (o ObjectType) Glyph() Glyph { switch o { case ObjectNone: return GlyphNone - case ObjectRover: - return GlyphRover - case ObjectSmallRock: - return GlyphSmallRock - case ObjectLargeRock: - return GlyphLargeRock + case ObjectRoverLive: + return GlyphRoverLive + case ObjectRockSmall: + return GlyphRockSmall + case ObjectRockLarge: + return GlyphRockLarge } log.Fatalf("Unknown object type: %c", o) @@ -46,8 +46,8 @@ type Object struct { // IsBlocking checks if an object is a blocking object func (o *Object) IsBlocking() bool { var blocking = [...]ObjectType{ - ObjectRover, - ObjectLargeRock, + ObjectRoverLive, + ObjectRockLarge, } for _, t := range blocking { @@ -61,7 +61,7 @@ func (o *Object) IsBlocking() bool { // IsStashable checks if an object is stashable func (o *Object) IsStashable() bool { var stashable = [...]ObjectType{ - ObjectSmallRock, + ObjectRockSmall, } for _, t := range stashable { diff --git a/pkg/rove/world.go b/pkg/rove/world.go index 177dbba..7375df1 100644 --- a/pkg/rove/world.go +++ b/pkg/rove/world.go @@ -401,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(atlas.ObjectRover) + objs[index] = byte(atlas.ObjectRoverLive) } } diff --git a/pkg/rove/world_test.go b/pkg/rove/world_test.go index f3be5c0..5a3e34f 100644 --- a/pkg/rove/world_test.go +++ b/pkg/rove/world_test.go @@ -90,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}, atlas.Object{Type: atlas.ObjectLargeRock}) + world.Atlas.SetObject(maths.Vector{X: 0, Y: 2}, atlas.Object{Type: atlas.ObjectRockLarge}) 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") @@ -120,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(atlas.ObjectRover), objs[1+fullRange]) - assert.Equal(t, byte(atlas.ObjectRover), objs[4+4*fullRange]) + assert.Equal(t, byte(atlas.ObjectRoverLive), objs[1+fullRange]) + assert.Equal(t, byte(atlas.ObjectRoverLive), objs[4+4*fullRange]) // Check the radar results are stable radar1, objs1, err := world.RadarFromRover(a) @@ -154,12 +154,12 @@ func TestWorld_RoverStash(t *testing.T) { for i := 0; i < rover.Capacity; i++ { // Place an object - world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectSmallRock}) + world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectRockSmall}) // Pick it up o, err := world.RoverStash(a) assert.NoError(t, err, "Failed to stash") - assert.Equal(t, atlas.ObjectSmallRock, o, "Failed to get correct object") + assert.Equal(t, atlas.ObjectRockSmall, o, "Failed to get correct object") // Check it's gone _, obj := world.Atlas.QueryPosition(pos) @@ -169,7 +169,7 @@ func TestWorld_RoverStash(t *testing.T) { inv, err := world.RoverInventory(a) assert.NoError(t, err, "Failed to get inventory") assert.Equal(t, i+1, len(inv)) - assert.Equal(t, atlas.Object{Type: atlas.ObjectSmallRock}, inv[i]) + assert.Equal(t, atlas.Object{Type: atlas.ObjectRockSmall}, inv[i]) // Check that this did reduce the charge info, err := world.GetRover(a) @@ -186,7 +186,7 @@ func TestWorld_RoverStash(t *testing.T) { } // Place an object - world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectSmallRock}) + world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectRockSmall}) // Try to pick it up o, err := world.RoverStash(a) @@ -195,7 +195,7 @@ func TestWorld_RoverStash(t *testing.T) { // Check it's still there _, obj := world.Atlas.QueryPosition(pos) - assert.Equal(t, atlas.ObjectSmallRock, obj.Type, "Stash failed to remove object from atlas") + assert.Equal(t, atlas.ObjectRockSmall, obj.Type, "Stash failed to remove object from atlas") // Check we don't have it inv, err := world.RoverInventory(a) @@ -224,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}, atlas.Object{Type: atlas.ObjectLargeRock}) + world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, atlas.Object{Type: atlas.ObjectRockLarge}) vec, err := world.MoveRover(a, maths.North) assert.NoError(t, err, "Failed to move rover") @@ -256,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, atlas.Object{Type: atlas.ObjectSmallRock}) + world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectRockSmall}) o, err := world.RoverStash(a) assert.NoError(t, err, "Failed to stash") - assert.Equal(t, atlas.ObjectSmallRock, o, "Failed to get correct object") + assert.Equal(t, atlas.ObjectRockSmall, o, "Failed to get correct object") - world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, atlas.Object{Type: atlas.ObjectLargeRock}) + world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, atlas.Object{Type: atlas.ObjectRockLarge}) // Try and bump into the rock vec, err := world.MoveRover(a, maths.North) @@ -281,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, atlas.Object{Type: atlas.ObjectSmallRock}) + world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectRockSmall}) o, err = world.RoverStash(a) assert.NoError(t, err, "Failed to stash") - assert.Equal(t, atlas.ObjectSmallRock, o, "Failed to get correct object") + assert.Equal(t, atlas.ObjectRockSmall, o, "Failed to get correct object") err = world.ExecuteCommand(&Command{Command: roveapi.CommandType_repair}, a) assert.NoError(t, err, "Failed to repair rover") From 24d4fe92734992b906aa5b19bb0a075ed06dd83b Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 11:59:14 +0100 Subject: [PATCH 06/12] Convert tiles and object types to typed consts --- pkg/atlas/atlas.go | 8 ++++---- pkg/atlas/objects.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/atlas/atlas.go b/pkg/atlas/atlas.go index 305c355..700be09 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/atlas/atlas.go @@ -11,16 +11,16 @@ type Tile byte const ( // TileNone is a keyword for nothing - TileNone = iota + TileNone = Tile(0) // TileRock is solid rock ground - TileRock + TileRock = Tile(1) // TileGravel is loose rocks - TileGravel + TileGravel = Tile(2) // TileSand is sand - TileSand + TileSand = Tile(3) ) // Glyph returns the glyph for this tile type diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index 032ca10..52d88df 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -8,16 +8,16 @@ type ObjectType byte // Types of objects const ( // ObjectNone represents no object at all - ObjectNone = iota + ObjectNone = ObjectType(0) // ObjectRover represents a live rover - ObjectRoverLive + ObjectRoverLive = ObjectType(1) // ObjectSmallRock is a small stashable rock - ObjectRockSmall + ObjectRockSmall = ObjectType(2) // ObjectLargeRock is a large blocking rock - ObjectRockLarge + ObjectRockLarge = ObjectType(3) ) // Glyph returns the glyph for this object type From 305f64ec38ecaa06c121f0fa33a3e43f890f8bdb Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 12:26:57 +0100 Subject: [PATCH 07/12] Large refactor, move object and tile types out into the proto --- cmd/rove/main.go | 8 +- pkg/atlas/atlas.go | 34 ++--- pkg/atlas/atlas_test.go | 55 ++++---- pkg/atlas/chunkAtlas.go | 31 ++-- pkg/atlas/objects.go | 44 ++---- pkg/rove/world.go | 22 +-- pkg/rove/world_test.go | 48 +++---- proto/roveapi/roveapi.pb.go | 273 ++++++++++++++++++++++++++---------- proto/roveapi/roveapi.proto | 33 ++++- 9 files changed, 338 insertions(+), 210 deletions(-) diff --git a/cmd/rove/main.go b/cmd/rove/main.go index 8a19e68..8e4f740 100644 --- a/cmd/rove/main.go +++ b/cmd/rove/main.go @@ -287,10 +287,10 @@ 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(atlas.ObjectNone) { - fmt.Printf("%c", o) - } else if t != byte(atlas.TileNone) { - fmt.Printf("%c", t) + if o != roveapi.Object_ObjectNone { + fmt.Printf("%c", atlas.ObjectGlyph(o)) + } else if t != roveapi.Tile_TileNone { + fmt.Printf("%c", atlas.TileGlyph(t)) } else { fmt.Printf(" ") } diff --git a/pkg/atlas/atlas.go b/pkg/atlas/atlas.go index 700be09..de83642 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/atlas/atlas.go @@ -4,35 +4,19 @@ import ( "log" "github.com/mdiluz/rove/pkg/maths" + "github.com/mdiluz/rove/proto/roveapi" ) -// Tile describes the type of terrain -type Tile byte - -const ( - // TileNone is a keyword for nothing - TileNone = Tile(0) - - // TileRock is solid rock ground - TileRock = Tile(1) - - // TileGravel is loose rocks - TileGravel = Tile(2) - - // TileSand is sand - TileSand = Tile(3) -) - -// Glyph returns the glyph for this tile type -func (t Tile) Glyph() Glyph { +// TileGlyph returns the glyph for this tile type +func TileGlyph(t roveapi.Tile) Glyph { switch t { - case TileNone: + case roveapi.Tile_TileNone: return GlyphNone - case TileRock: + case roveapi.Tile_Rock: return GlyphGroundRock - case TileGravel: + case roveapi.Tile_Gravel: return GlyphGroundGravel - case TileSand: + case roveapi.Tile_Sand: return GlyphGroundSand } @@ -43,11 +27,11 @@ func (t Tile) Glyph() Glyph { // 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 maths.Vector, tile Tile) + SetTile(v maths.Vector, tile roveapi.Tile) // SetObject will set a location on the Atlas to contain an object SetObject(v maths.Vector, obj Object) // QueryPosition queries a position on the atlas - QueryPosition(v maths.Vector) (byte, Object) + QueryPosition(v maths.Vector) (roveapi.Tile, Object) } diff --git a/pkg/atlas/atlas_test.go b/pkg/atlas/atlas_test.go index 5141371..de8b7b9 100644 --- a/pkg/atlas/atlas_test.go +++ b/pkg/atlas/atlas_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/mdiluz/rove/pkg/maths" + "github.com/mdiluz/rove/proto/roveapi" "github.com/stretchr/testify/assert" ) @@ -169,15 +170,15 @@ func TestAtlas_GetSetTile(t *testing.T) { a := NewChunkAtlas(10) assert.NotNil(t, a) - // Set the origin tile to 1 and test it - a.SetTile(maths.Vector{X: 0, Y: 0}, 1) + // Set the origin tile and test it + a.SetTile(maths.Vector{X: 0, Y: 0}, roveapi.Tile_Gravel) tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0}) - assert.Equal(t, byte(1), tile) + assert.Equal(t, roveapi.Tile_Gravel, tile) - // Set another tile to 1 and test it - a.SetTile(maths.Vector{X: 5, Y: -2}, 2) + // Set another tile and test it + a.SetTile(maths.Vector{X: 5, Y: -2}, roveapi.Tile_Rock) tile, _ = a.QueryPosition(maths.Vector{X: 5, Y: -2}) - assert.Equal(t, byte(2), tile) + assert.Equal(t, roveapi.Tile_Rock, tile) } func TestAtlas_GetSetObject(t *testing.T) { @@ -185,14 +186,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}, Object{Type: ObjectRockLarge}) + a.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: roveapi.Object_RockLarge}) _, obj := a.QueryPosition(maths.Vector{X: 0, Y: 0}) - assert.Equal(t, Object{Type: ObjectRockLarge}, obj) + assert.Equal(t, Object{Type: roveapi.Object_RockLarge}, obj) // Set another tile to 1 and test it - a.SetObject(maths.Vector{X: 5, Y: -2}, Object{Type: ObjectRockSmall}) + a.SetObject(maths.Vector{X: 5, Y: -2}, Object{Type: roveapi.Object_RockSmall}) _, obj = a.QueryPosition(maths.Vector{X: 5, Y: -2}) - assert.Equal(t, Object{Type: ObjectRockSmall}, obj) + assert.Equal(t, Object{Type: roveapi.Object_RockSmall}, obj) } func TestAtlas_Grown(t *testing.T) { @@ -202,28 +203,28 @@ func TestAtlas_Grown(t *testing.T) { assert.Equal(t, 1, len(a.Chunks)) // Set a few tiles to values - 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) + a.SetTile(maths.Vector{X: 0, Y: 0}, roveapi.Tile_Gravel) + a.SetTile(maths.Vector{X: -1, Y: -1}, roveapi.Tile_Rock) + a.SetTile(maths.Vector{X: 1, Y: -2}, roveapi.Tile_Sand) // Check tile values tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0}) - assert.Equal(t, byte(1), tile) + assert.Equal(t, roveapi.Tile_Gravel, tile) tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1}) - assert.Equal(t, byte(2), tile) + assert.Equal(t, roveapi.Tile_Rock, tile) tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2}) - assert.Equal(t, byte(3), tile) + assert.Equal(t, roveapi.Tile_Sand, tile) tile, _ = a.QueryPosition(maths.Vector{X: 0, Y: 0}) - assert.Equal(t, byte(1), tile) + assert.Equal(t, roveapi.Tile_Gravel, tile) tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1}) - assert.Equal(t, byte(2), tile) + assert.Equal(t, roveapi.Tile_Rock, tile) tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2}) - assert.Equal(t, byte(3), tile) + assert.Equal(t, roveapi.Tile_Sand, tile) } func TestAtlas_GetSetCorrect(t *testing.T) { @@ -237,12 +238,12 @@ func TestAtlas_GetSetCorrect(t *testing.T) { assert.Equal(t, 1, len(a.Chunks)) pos := maths.Vector{X: x, Y: y} - a.SetTile(pos, TileRock) - a.SetObject(pos, Object{Type: ObjectRockLarge}) + a.SetTile(pos, roveapi.Tile_Rock) + a.SetObject(pos, Object{Type: roveapi.Object_RockLarge}) tile, obj := a.QueryPosition(pos) - assert.Equal(t, TileRock, Tile(tile)) - assert.Equal(t, Object{Type: ObjectRockLarge}, obj) + assert.Equal(t, roveapi.Tile_Rock, roveapi.Tile(tile)) + assert.Equal(t, Object{Type: roveapi.Object_RockLarge}, obj) } } @@ -259,10 +260,10 @@ 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 != ObjectNone { - fmt.Printf("%c", o.Type) - } else if t != byte(TileNone) { - fmt.Printf("%c", t) + if o.Type != roveapi.Object_ObjectNone { + fmt.Printf("%c", ObjectGlyph(o.Type)) + } else if t != roveapi.Tile_TileNone { + fmt.Printf("%c", TileGlyph(t)) } else { fmt.Printf(" ") } diff --git a/pkg/atlas/chunkAtlas.go b/pkg/atlas/chunkAtlas.go index 99d4668..43ca3af 100644 --- a/pkg/atlas/chunkAtlas.go +++ b/pkg/atlas/chunkAtlas.go @@ -5,6 +5,7 @@ import ( "math/rand" "github.com/mdiluz/rove/pkg/maths" + "github.com/mdiluz/rove/proto/roveapi" "github.com/ojrac/opensimplex-go" ) @@ -63,7 +64,7 @@ func NewChunkAtlas(chunkSize int) Atlas { } // SetTile sets an individual tile's kind -func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile Tile) { +func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile roveapi.Tile) { c := a.worldSpaceToChunkWithGrow(v) local := a.worldSpaceToChunkLocal(v) a.setTile(c, local, byte(tile)) @@ -77,13 +78,13 @@ func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj Object) { } // QueryPosition will return information for a specific position -func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (byte, Object) { +func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (roveapi.Tile, Object) { c := a.worldSpaceToChunkWithGrow(v) local := a.worldSpaceToChunkLocal(v) a.populate(c) chunk := a.Chunks[c] i := a.chunkTileIndex(local) - return chunk.Tiles[i], chunk.Objects[i] + return roveapi.Tile(chunk.Tiles[i]), chunk.Objects[i] } // chunkTileID returns the tile index within a chunk @@ -107,28 +108,28 @@ func (a *chunkBasedAtlas) populate(chunk int) { // Get the terrain noise value for this location t := a.terrainNoise.Eval2(float64(origin.X+i)/terrainNoiseScale, float64(origin.Y+j)/terrainNoiseScale) - var tile Tile + var tile roveapi.Tile switch { case t > 0.5: - tile = TileGravel + tile = roveapi.Tile_Gravel case t > 0.05: - tile = TileSand + tile = roveapi.Tile_Sand default: - tile = TileRock + tile = roveapi.Tile_Rock } c.Tiles[j*a.ChunkSize+i] = byte(tile) // Get the object noise value for this location o := a.objectNoise.Eval2(float64(origin.X+i)/objectNoiseScale, float64(origin.Y+j)/objectNoiseScale) - var obj = ObjectNone + var obj = roveapi.Object_ObjectNone switch { case o > 0.6: - obj = ObjectRockLarge + obj = roveapi.Object_RockLarge case o > 0.5: - obj = ObjectRockSmall + obj = roveapi.Object_RockSmall } - if obj != ObjectNone { - c.Objects[j*a.ChunkSize+i] = Object{Type: ObjectType(obj)} + if obj != roveapi.Object_ObjectNone { + c.Objects[j*a.ChunkSize+i] = Object{Type: roveapi.Object(obj)} } } } @@ -136,9 +137,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] = Object{Type: ObjectRockLarge} + c.Objects[i] = Object{Type: roveapi.Object_RockLarge} } else if rand.Intn(32) == 0 { - c.Objects[i] = Object{Type: ObjectRockSmall} + c.Objects[i] = Object{Type: roveapi.Object_RockSmall} } } @@ -159,7 +160,7 @@ func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object Object c := a.Chunks[chunk] i := a.chunkTileIndex(local) - if object.Type != ObjectNone { + if object.Type != roveapi.Object_ObjectNone { c.Objects[i] = object } else { delete(c.Objects, i) diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index 52d88df..ccbb69b 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -1,35 +1,21 @@ package atlas -import "log" +import ( + "log" -// ObjectType represents an object type -type ObjectType byte - -// Types of objects -const ( - // ObjectNone represents no object at all - ObjectNone = ObjectType(0) - - // ObjectRover represents a live rover - ObjectRoverLive = ObjectType(1) - - // ObjectSmallRock is a small stashable rock - ObjectRockSmall = ObjectType(2) - - // ObjectLargeRock is a large blocking rock - ObjectRockLarge = ObjectType(3) + "github.com/mdiluz/rove/proto/roveapi" ) -// Glyph returns the glyph for this object type -func (o ObjectType) Glyph() Glyph { +// ObjectGlyph returns the glyph for this object type +func ObjectGlyph(o roveapi.Object) Glyph { switch o { - case ObjectNone: + case roveapi.Object_ObjectNone: return GlyphNone - case ObjectRoverLive: + case roveapi.Object_RoverLive: return GlyphRoverLive - case ObjectRockSmall: + case roveapi.Object_RockSmall: return GlyphRockSmall - case ObjectRockLarge: + case roveapi.Object_RockLarge: return GlyphRockLarge } @@ -40,14 +26,14 @@ func (o ObjectType) Glyph() Glyph { // Object represents an object in the world type Object struct { // The type of the object - Type ObjectType `json:"type"` + Type roveapi.Object `json:"type"` } // IsBlocking checks if an object is a blocking object func (o *Object) IsBlocking() bool { - var blocking = [...]ObjectType{ - ObjectRoverLive, - ObjectRockLarge, + var blocking = [...]roveapi.Object{ + roveapi.Object_RoverLive, + roveapi.Object_RockLarge, } for _, t := range blocking { @@ -60,8 +46,8 @@ func (o *Object) IsBlocking() bool { // IsStashable checks if an object is stashable func (o *Object) IsStashable() bool { - var stashable = [...]ObjectType{ - ObjectRockSmall, + var stashable = [...]roveapi.Object{ + roveapi.Object_RockSmall, } for _, t := range stashable { diff --git a/pkg/rove/world.go b/pkg/rove/world.go index 7375df1..51b81ef 100644 --- a/pkg/rove/world.go +++ b/pkg/rove/world.go @@ -318,40 +318,40 @@ 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) (atlas.ObjectType, error) { +func (w *World) RoverStash(rover string) (roveapi.Object, error) { w.worldMutex.Lock() defer w.worldMutex.Unlock() r, ok := w.Rovers[rover] if !ok { - return atlas.ObjectNone, fmt.Errorf("no rover matching id") + return roveapi.Object_ObjectNone, fmt.Errorf("no rover matching id") } // Can't pick up when full if len(r.Inventory) >= r.Capacity { - return atlas.ObjectNone, nil + return roveapi.Object_ObjectNone, nil } // Ensure the rover has energy if r.Charge <= 0 { - return atlas.ObjectNone, nil + return roveapi.Object_ObjectNone, nil } r.Charge-- _, obj := w.Atlas.QueryPosition(r.Pos) if !obj.IsStashable() { - return atlas.ObjectNone, nil + return roveapi.Object_ObjectNone, nil } r.AddLogEntryf("stashed %c", obj.Type) r.Inventory = append(r.Inventory, obj) w.Rovers[rover] = r - w.Atlas.SetObject(r.Pos, atlas.Object{Type: atlas.ObjectNone}) + w.Atlas.SetObject(r.Pos, atlas.Object{Type: roveapi.Object_ObjectNone}) return obj.Type, nil } // RadarFromRover can be used to query what a rover can currently see -func (w *World) RadarFromRover(rover string) (radar []byte, objs []byte, err error) { +func (w *World) RadarFromRover(rover string) (radar []roveapi.Tile, objs []roveapi.Object, err error) { w.worldMutex.RLock() defer w.worldMutex.RUnlock() @@ -376,8 +376,8 @@ func (w *World) RadarFromRover(rover string) (radar []byte, objs []byte, err err } // Gather up all tiles within the range - radar = make([]byte, radarSpan*radarSpan) - objs = make([]byte, radarSpan*radarSpan) + radar = make([]roveapi.Tile, radarSpan*radarSpan) + objs = make([]roveapi.Object, radarSpan*radarSpan) for j := radarMin.Y; j <= radarMax.Y; j++ { for i := radarMin.X; i <= radarMax.X; i++ { q := maths.Vector{X: i, Y: j} @@ -388,7 +388,7 @@ func (w *World) RadarFromRover(rover string) (radar []byte, objs []byte, err err relative := q.Added(radarMin.Negated()) index := relative.X + relative.Y*radarSpan radar[index] = tile - objs[index] = byte(obj.Type) + objs[index] = obj.Type } } @@ -401,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(atlas.ObjectRoverLive) + objs[index] = roveapi.Object_RoverLive } } diff --git a/pkg/rove/world_test.go b/pkg/rove/world_test.go index 5a3e34f..8142858 100644 --- a/pkg/rove/world_test.go +++ b/pkg/rove/world_test.go @@ -90,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}, atlas.Object{Type: atlas.ObjectRockLarge}) + world.Atlas.SetObject(maths.Vector{X: 0, Y: 2}, atlas.Object{Type: roveapi.Object_RockLarge}) 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") @@ -120,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(atlas.ObjectRoverLive), objs[1+fullRange]) - assert.Equal(t, byte(atlas.ObjectRoverLive), objs[4+4*fullRange]) + assert.Equal(t, roveapi.Object_RoverLive, objs[1+fullRange]) + assert.Equal(t, roveapi.Object_RoverLive, objs[4+4*fullRange]) // Check the radar results are stable radar1, objs1, err := world.RadarFromRover(a) @@ -142,34 +142,34 @@ func TestWorld_RoverStash(t *testing.T) { Y: 0.0, } - world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectNone}) + world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_ObjectNone}) err = world.WarpRover(a, pos) assert.NoError(t, err, "Failed to set position for rover") // Set to a traversible tile - world.Atlas.SetTile(pos, atlas.TileRock) + world.Atlas.SetTile(pos, roveapi.Tile_TileNone) rover, err := world.GetRover(a) assert.NoError(t, err, "Failed to get rover") for i := 0; i < rover.Capacity; i++ { // Place an object - world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectRockSmall}) + world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_RockSmall}) // Pick it up o, err := world.RoverStash(a) assert.NoError(t, err, "Failed to stash") - assert.Equal(t, atlas.ObjectRockSmall, o, "Failed to get correct object") + assert.Equal(t, roveapi.Object_RockSmall, o, "Failed to get correct object") // Check it's gone _, obj := world.Atlas.QueryPosition(pos) - assert.Equal(t, atlas.ObjectNone, obj.Type, "Stash failed to remove object from atlas") + assert.Equal(t, roveapi.Object_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, atlas.Object{Type: atlas.ObjectRockSmall}, inv[i]) + assert.Equal(t, atlas.Object{Type: roveapi.Object_RockSmall}, inv[i]) // Check that this did reduce the charge info, err := world.GetRover(a) @@ -186,16 +186,16 @@ func TestWorld_RoverStash(t *testing.T) { } // Place an object - world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectRockSmall}) + world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_RockSmall}) // Try to pick it up o, err := world.RoverStash(a) assert.NoError(t, err, "Failed to stash") - assert.Equal(t, atlas.ObjectNone, o, "Failed to get correct object") + assert.Equal(t, roveapi.Object_ObjectNone, o, "Failed to get correct object") // Check it's still there _, obj := world.Atlas.QueryPosition(pos) - assert.Equal(t, atlas.ObjectRockSmall, obj.Type, "Stash failed to remove object from atlas") + assert.Equal(t, roveapi.Object_RockSmall, obj.Type, "Stash failed to remove object from atlas") // Check we don't have it inv, err := world.RoverInventory(a) @@ -224,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}, atlas.Object{Type: atlas.ObjectRockLarge}) + world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, atlas.Object{Type: roveapi.Object_RockLarge}) vec, err := world.MoveRover(a, maths.North) assert.NoError(t, err, "Failed to move rover") @@ -246,8 +246,8 @@ func TestWorld_RoverRepair(t *testing.T) { Y: 0.0, } - world.Atlas.SetTile(pos, atlas.TileNone) - world.Atlas.SetObject(pos, atlas.Object{Type: atlas.ObjectNone}) + world.Atlas.SetTile(pos, roveapi.Tile_TileNone) + world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_ObjectNone}) err = world.WarpRover(a, pos) assert.NoError(t, err, "Failed to set position for rover") @@ -256,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, atlas.Object{Type: atlas.ObjectRockSmall}) + world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_RockSmall}) o, err := world.RoverStash(a) assert.NoError(t, err, "Failed to stash") - assert.Equal(t, atlas.ObjectRockSmall, o, "Failed to get correct object") + assert.Equal(t, roveapi.Object_RockSmall, o, "Failed to get correct object") - world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, atlas.Object{Type: atlas.ObjectRockLarge}) + world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, atlas.Object{Type: roveapi.Object_RockLarge}) // Try and bump into the rock vec, err := world.MoveRover(a, maths.North) @@ -281,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, atlas.Object{Type: atlas.ObjectRockSmall}) + world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_RockSmall}) o, err = world.RoverStash(a) assert.NoError(t, err, "Failed to stash") - assert.Equal(t, atlas.ObjectRockSmall, o, "Failed to get correct object") + assert.Equal(t, roveapi.Object_RockSmall, o, "Failed to get correct object") err = world.ExecuteCommand(&Command{Command: roveapi.CommandType_repair}, a) assert.NoError(t, err, "Failed to repair rover") @@ -311,8 +311,8 @@ func TestWorld_Charge(t *testing.T) { assert.NoError(t, err, "Failed to get position for rover") // Ensure the path ahead is empty - world.Atlas.SetTile(initialPos.Added(maths.North.Vector()), atlas.TileRock) - world.Atlas.SetObject(initialPos.Added(maths.North.Vector()), atlas.Object{Type: atlas.ObjectNone}) + world.Atlas.SetTile(initialPos.Added(maths.North.Vector()), roveapi.Tile_Rock) + world.Atlas.SetObject(initialPos.Added(maths.North.Vector()), atlas.Object{Type: roveapi.Object_ObjectNone}) // Try and move north (along unblocked path) newPos, err := world.MoveRover(a, maths.North) @@ -394,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}, atlas.Object{Type: atlas.ObjectNone}) + world.Atlas.SetObject(maths.Vector{X: ra.Range, Y: 0}, atlas.Object{Type: roveapi.Object_ObjectNone}) assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range, Y: 0})) // Broadcast from a again @@ -411,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}, atlas.Object{Type: atlas.ObjectNone}) + world.Atlas.SetObject(maths.Vector{X: ra.Range + 1, Y: 0}, atlas.Object{Type: roveapi.Object_ObjectNone}) assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range + 1, Y: 0})) // Broadcast from a again diff --git a/proto/roveapi/roveapi.pb.go b/proto/roveapi/roveapi.pb.go index 1eeeabd..842d574 100644 --- a/proto/roveapi/roveapi.pb.go +++ b/proto/roveapi/roveapi.pb.go @@ -98,6 +98,119 @@ func (CommandType) EnumDescriptor() ([]byte, []int) { return file_roveapi_roveapi_proto_rawDescGZIP(), []int{0} } +// Types of objects +type Object int32 + +const ( + // ObjectNone represents no object at all + Object_ObjectNone Object = 0 + // RoverLive represents a live rover + Object_RoverLive Object = 1 + // RockSmall is a small stashable rock + Object_RockSmall Object = 2 + // RockLarge is a large blocking rock + Object_RockLarge Object = 3 +) + +// Enum value maps for Object. +var ( + Object_name = map[int32]string{ + 0: "ObjectNone", + 1: "RoverLive", + 2: "RockSmall", + 3: "RockLarge", + } + Object_value = map[string]int32{ + "ObjectNone": 0, + "RoverLive": 1, + "RockSmall": 2, + "RockLarge": 3, + } +) + +func (x Object) Enum() *Object { + p := new(Object) + *p = x + return p +} + +func (x Object) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (Object) Descriptor() protoreflect.EnumDescriptor { + return file_roveapi_roveapi_proto_enumTypes[1].Descriptor() +} + +func (Object) Type() protoreflect.EnumType { + return &file_roveapi_roveapi_proto_enumTypes[1] +} + +func (x Object) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use Object.Descriptor instead. +func (Object) EnumDescriptor() ([]byte, []int) { + return file_roveapi_roveapi_proto_rawDescGZIP(), []int{1} +} + +type Tile int32 + +const ( + // TileNone is a keyword for nothing + Tile_TileNone Tile = 0 + // Rock is solid rock ground + Tile_Rock Tile = 1 + // Gravel is loose rocks + Tile_Gravel Tile = 2 + // Sand is sand + Tile_Sand Tile = 3 +) + +// Enum value maps for Tile. +var ( + Tile_name = map[int32]string{ + 0: "TileNone", + 1: "Rock", + 2: "Gravel", + 3: "Sand", + } + Tile_value = map[string]int32{ + "TileNone": 0, + "Rock": 1, + "Gravel": 2, + "Sand": 3, + } +) + +func (x Tile) Enum() *Tile { + p := new(Tile) + *p = x + return p +} + +func (x Tile) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (Tile) Descriptor() protoreflect.EnumDescriptor { + return file_roveapi_roveapi_proto_enumTypes[2].Descriptor() +} + +func (Tile) Type() protoreflect.EnumType { + return &file_roveapi_roveapi_proto_enumTypes[2] +} + +func (x Tile) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use Tile.Descriptor instead. +func (Tile) EnumDescriptor() ([]byte, []int) { + return file_roveapi_roveapi_proto_rawDescGZIP(), []int{2} +} + // ServerStatusRequest is an empty placeholder type ServerStatusRequest struct { state protoimpl.MessageState @@ -629,9 +742,9 @@ type RadarResponse struct { Range int32 `protobuf:"varint,1,opt,name=range,proto3" json:"range,omitempty"` // A 1D array representing range*2 + 1 squared set of tiles, origin bottom // left and in row->column order - Tiles []byte `protobuf:"bytes,2,opt,name=tiles,proto3" json:"tiles,omitempty"` + Tiles []Tile `protobuf:"varint,2,rep,packed,name=tiles,proto3,enum=roveapi.Tile" json:"tiles,omitempty"` // A similar array to the tile array, but containing objects - Objects []byte `protobuf:"bytes,3,opt,name=objects,proto3" json:"objects,omitempty"` + Objects []Object `protobuf:"varint,3,rep,packed,name=objects,proto3,enum=roveapi.Object" json:"objects,omitempty"` } func (x *RadarResponse) Reset() { @@ -673,14 +786,14 @@ func (x *RadarResponse) GetRange() int32 { return 0 } -func (x *RadarResponse) GetTiles() []byte { +func (x *RadarResponse) GetTiles() []Tile { if x != nil { return x.Tiles } return nil } -func (x *RadarResponse) GetObjects() []byte { +func (x *RadarResponse) GetObjects() []Object { if x != nil { return x.Objects } @@ -1044,11 +1157,13 @@ var file_roveapi_roveapi_proto_rawDesc = []byte{ 0x65, 0x73, 0x74, 0x12, 0x2a, 0x0a, 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x52, 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, - 0x55, 0x0a, 0x0d, 0x52, 0x61, 0x64, 0x61, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x75, 0x0a, 0x0d, 0x52, 0x61, 0x64, 0x61, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, - 0x05, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x69, 0x6c, 0x65, 0x73, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x74, 0x69, 0x6c, 0x65, 0x73, 0x12, 0x18, 0x0a, 0x07, - 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x6f, + 0x05, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x12, 0x23, 0x0a, 0x05, 0x74, 0x69, 0x6c, 0x65, 0x73, 0x18, + 0x02, 0x20, 0x03, 0x28, 0x0e, 0x32, 0x0d, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, + 0x54, 0x69, 0x6c, 0x65, 0x52, 0x05, 0x74, 0x69, 0x6c, 0x65, 0x73, 0x12, 0x29, 0x0a, 0x07, 0x6f, + 0x62, 0x6a, 0x65, 0x63, 0x74, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0e, 0x32, 0x0f, 0x2e, 0x72, + 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x52, 0x07, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x73, 0x22, 0x3b, 0x0a, 0x0d, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x2a, 0x0a, 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, @@ -1092,31 +1207,39 @@ var file_roveapi_roveapi_proto_rawDesc = []byte{ 0x01, 0x12, 0x09, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x73, 0x68, 0x10, 0x02, 0x12, 0x0a, 0x0a, 0x06, 0x72, 0x65, 0x70, 0x61, 0x69, 0x72, 0x10, 0x03, 0x12, 0x0c, 0x0a, 0x08, 0x72, 0x65, 0x63, 0x68, 0x61, 0x72, 0x67, 0x65, 0x10, 0x04, 0x12, 0x0d, 0x0a, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, - 0x61, 0x73, 0x74, 0x10, 0x05, 0x32, 0xcf, 0x02, 0x0a, 0x04, 0x52, 0x6f, 0x76, 0x65, 0x12, 0x4d, - 0x0a, 0x0c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1c, - 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, - 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x72, - 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x61, - 0x74, 0x75, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x41, 0x0a, - 0x08, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x12, 0x18, 0x2e, 0x72, 0x6f, 0x76, 0x65, - 0x61, 0x70, 0x69, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x65, - 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, - 0x12, 0x3e, 0x0a, 0x07, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x17, 0x2e, 0x72, 0x6f, - 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x43, - 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, - 0x12, 0x38, 0x0a, 0x05, 0x52, 0x61, 0x64, 0x61, 0x72, 0x12, 0x15, 0x2e, 0x72, 0x6f, 0x76, 0x65, - 0x61, 0x70, 0x69, 0x2e, 0x52, 0x61, 0x64, 0x61, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x16, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x61, 0x64, 0x61, 0x72, - 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3b, 0x0a, 0x06, 0x53, 0x74, - 0x61, 0x74, 0x75, 0x73, 0x12, 0x16, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, - 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x72, - 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x26, 0x5a, 0x24, 0x67, 0x69, 0x74, 0x68, 0x75, - 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d, 0x64, 0x69, 0x6c, 0x75, 0x7a, 0x2f, 0x72, 0x6f, 0x76, - 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x62, - 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x61, 0x73, 0x74, 0x10, 0x05, 0x2a, 0x45, 0x0a, 0x06, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x12, + 0x0e, 0x0a, 0x0a, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x4e, 0x6f, 0x6e, 0x65, 0x10, 0x00, 0x12, + 0x0d, 0x0a, 0x09, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x4c, 0x69, 0x76, 0x65, 0x10, 0x01, 0x12, 0x0d, + 0x0a, 0x09, 0x52, 0x6f, 0x63, 0x6b, 0x53, 0x6d, 0x61, 0x6c, 0x6c, 0x10, 0x02, 0x12, 0x0d, 0x0a, + 0x09, 0x52, 0x6f, 0x63, 0x6b, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x10, 0x03, 0x2a, 0x34, 0x0a, 0x04, + 0x54, 0x69, 0x6c, 0x65, 0x12, 0x0c, 0x0a, 0x08, 0x54, 0x69, 0x6c, 0x65, 0x4e, 0x6f, 0x6e, 0x65, + 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x52, 0x6f, 0x63, 0x6b, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, + 0x47, 0x72, 0x61, 0x76, 0x65, 0x6c, 0x10, 0x02, 0x12, 0x08, 0x0a, 0x04, 0x53, 0x61, 0x6e, 0x64, + 0x10, 0x03, 0x32, 0xcf, 0x02, 0x0a, 0x04, 0x52, 0x6f, 0x76, 0x65, 0x12, 0x4d, 0x0a, 0x0c, 0x53, + 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1c, 0x2e, 0x72, 0x6f, + 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, + 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x72, 0x6f, 0x76, 0x65, + 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x41, 0x0a, 0x08, 0x52, 0x65, + 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x12, 0x18, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, + 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x19, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, + 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3e, 0x0a, + 0x07, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x17, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, + 0x70, 0x69, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x18, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, + 0x61, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x38, 0x0a, + 0x05, 0x52, 0x61, 0x64, 0x61, 0x72, 0x12, 0x15, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, + 0x2e, 0x52, 0x61, 0x64, 0x61, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, + 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x61, 0x64, 0x61, 0x72, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3b, 0x0a, 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, + 0x73, 0x12, 0x16, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x74, 0x61, 0x74, + 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x72, 0x6f, 0x76, 0x65, + 0x61, 0x70, 0x69, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x22, 0x00, 0x42, 0x26, 0x5a, 0x24, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x6d, 0x64, 0x69, 0x6c, 0x75, 0x7a, 0x2f, 0x72, 0x6f, 0x76, 0x65, 0x2f, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -1131,51 +1254,55 @@ func file_roveapi_roveapi_proto_rawDescGZIP() []byte { return file_roveapi_roveapi_proto_rawDescData } -var file_roveapi_roveapi_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_roveapi_roveapi_proto_enumTypes = make([]protoimpl.EnumInfo, 3) var file_roveapi_roveapi_proto_msgTypes = make([]protoimpl.MessageInfo, 14) var file_roveapi_roveapi_proto_goTypes = []interface{}{ (CommandType)(0), // 0: roveapi.CommandType - (*ServerStatusRequest)(nil), // 1: roveapi.ServerStatusRequest - (*ServerStatusResponse)(nil), // 2: roveapi.ServerStatusResponse - (*RegisterRequest)(nil), // 3: roveapi.RegisterRequest - (*Account)(nil), // 4: roveapi.Account - (*RegisterResponse)(nil), // 5: roveapi.RegisterResponse - (*Command)(nil), // 6: roveapi.Command - (*CommandRequest)(nil), // 7: roveapi.CommandRequest - (*CommandResponse)(nil), // 8: roveapi.CommandResponse - (*RadarRequest)(nil), // 9: roveapi.RadarRequest - (*RadarResponse)(nil), // 10: roveapi.RadarResponse - (*StatusRequest)(nil), // 11: roveapi.StatusRequest - (*Log)(nil), // 12: roveapi.Log - (*Vector)(nil), // 13: roveapi.Vector - (*StatusResponse)(nil), // 14: roveapi.StatusResponse + (Object)(0), // 1: roveapi.Object + (Tile)(0), // 2: roveapi.Tile + (*ServerStatusRequest)(nil), // 3: roveapi.ServerStatusRequest + (*ServerStatusResponse)(nil), // 4: roveapi.ServerStatusResponse + (*RegisterRequest)(nil), // 5: roveapi.RegisterRequest + (*Account)(nil), // 6: roveapi.Account + (*RegisterResponse)(nil), // 7: roveapi.RegisterResponse + (*Command)(nil), // 8: roveapi.Command + (*CommandRequest)(nil), // 9: roveapi.CommandRequest + (*CommandResponse)(nil), // 10: roveapi.CommandResponse + (*RadarRequest)(nil), // 11: roveapi.RadarRequest + (*RadarResponse)(nil), // 12: roveapi.RadarResponse + (*StatusRequest)(nil), // 13: roveapi.StatusRequest + (*Log)(nil), // 14: roveapi.Log + (*Vector)(nil), // 15: roveapi.Vector + (*StatusResponse)(nil), // 16: roveapi.StatusResponse } var file_roveapi_roveapi_proto_depIdxs = []int32{ - 4, // 0: roveapi.RegisterResponse.account:type_name -> roveapi.Account + 6, // 0: roveapi.RegisterResponse.account:type_name -> roveapi.Account 0, // 1: roveapi.Command.command:type_name -> roveapi.CommandType - 4, // 2: roveapi.CommandRequest.account:type_name -> roveapi.Account - 6, // 3: roveapi.CommandRequest.commands:type_name -> roveapi.Command - 4, // 4: roveapi.RadarRequest.account:type_name -> roveapi.Account - 4, // 5: roveapi.StatusRequest.account:type_name -> roveapi.Account - 13, // 6: roveapi.StatusResponse.position:type_name -> roveapi.Vector - 6, // 7: roveapi.StatusResponse.incomingCommands:type_name -> roveapi.Command - 6, // 8: roveapi.StatusResponse.queuedCommands:type_name -> roveapi.Command - 12, // 9: roveapi.StatusResponse.logs:type_name -> roveapi.Log - 1, // 10: roveapi.Rove.ServerStatus:input_type -> roveapi.ServerStatusRequest - 3, // 11: roveapi.Rove.Register:input_type -> roveapi.RegisterRequest - 7, // 12: roveapi.Rove.Command:input_type -> roveapi.CommandRequest - 9, // 13: roveapi.Rove.Radar:input_type -> roveapi.RadarRequest - 11, // 14: roveapi.Rove.Status:input_type -> roveapi.StatusRequest - 2, // 15: roveapi.Rove.ServerStatus:output_type -> roveapi.ServerStatusResponse - 5, // 16: roveapi.Rove.Register:output_type -> roveapi.RegisterResponse - 8, // 17: roveapi.Rove.Command:output_type -> roveapi.CommandResponse - 10, // 18: roveapi.Rove.Radar:output_type -> roveapi.RadarResponse - 14, // 19: roveapi.Rove.Status:output_type -> roveapi.StatusResponse - 15, // [15:20] is the sub-list for method output_type - 10, // [10:15] is the sub-list for method input_type - 10, // [10:10] is the sub-list for extension type_name - 10, // [10:10] is the sub-list for extension extendee - 0, // [0:10] is the sub-list for field type_name + 6, // 2: roveapi.CommandRequest.account:type_name -> roveapi.Account + 8, // 3: roveapi.CommandRequest.commands:type_name -> roveapi.Command + 6, // 4: roveapi.RadarRequest.account:type_name -> roveapi.Account + 2, // 5: roveapi.RadarResponse.tiles:type_name -> roveapi.Tile + 1, // 6: roveapi.RadarResponse.objects:type_name -> roveapi.Object + 6, // 7: roveapi.StatusRequest.account:type_name -> roveapi.Account + 15, // 8: roveapi.StatusResponse.position:type_name -> roveapi.Vector + 8, // 9: roveapi.StatusResponse.incomingCommands:type_name -> roveapi.Command + 8, // 10: roveapi.StatusResponse.queuedCommands:type_name -> roveapi.Command + 14, // 11: roveapi.StatusResponse.logs:type_name -> roveapi.Log + 3, // 12: roveapi.Rove.ServerStatus:input_type -> roveapi.ServerStatusRequest + 5, // 13: roveapi.Rove.Register:input_type -> roveapi.RegisterRequest + 9, // 14: roveapi.Rove.Command:input_type -> roveapi.CommandRequest + 11, // 15: roveapi.Rove.Radar:input_type -> roveapi.RadarRequest + 13, // 16: roveapi.Rove.Status:input_type -> roveapi.StatusRequest + 4, // 17: roveapi.Rove.ServerStatus:output_type -> roveapi.ServerStatusResponse + 7, // 18: roveapi.Rove.Register:output_type -> roveapi.RegisterResponse + 10, // 19: roveapi.Rove.Command:output_type -> roveapi.CommandResponse + 12, // 20: roveapi.Rove.Radar:output_type -> roveapi.RadarResponse + 16, // 21: roveapi.Rove.Status:output_type -> roveapi.StatusResponse + 17, // [17:22] is the sub-list for method output_type + 12, // [12:17] is the sub-list for method input_type + 12, // [12:12] is the sub-list for extension type_name + 12, // [12:12] is the sub-list for extension extendee + 0, // [0:12] is the sub-list for field type_name } func init() { file_roveapi_roveapi_proto_init() } @@ -1362,7 +1489,7 @@ func file_roveapi_roveapi_proto_init() { File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_roveapi_roveapi_proto_rawDesc, - NumEnums: 1, + NumEnums: 3, NumMessages: 14, NumExtensions: 0, NumServices: 1, diff --git a/proto/roveapi/roveapi.proto b/proto/roveapi/roveapi.proto index 9dd2577..62dbc5a 100644 --- a/proto/roveapi/roveapi.proto +++ b/proto/roveapi/roveapi.proto @@ -134,6 +134,35 @@ message CommandResponse {} // Radar // +// Types of objects +enum Object { + // ObjectNone represents no object at all + ObjectNone = 0; + + // RoverLive represents a live rover + RoverLive = 1; + + // RockSmall is a small stashable rock + RockSmall = 2; + + // RockLarge is a large blocking rock + RockLarge = 3; +} + +enum Tile { + // TileNone is a keyword for nothing + TileNone = 0; + + // Rock is solid rock ground + Rock = 1; + + // Gravel is loose rocks + Gravel = 2; + + // Sand is sand + Sand = 3; +} + // RadarRequest is the data needed to request the radar for a rover message RadarRequest { // The account for this request @@ -147,10 +176,10 @@ message RadarResponse { // A 1D array representing range*2 + 1 squared set of tiles, origin bottom // left and in row->column order - bytes tiles = 2; + repeated Tile tiles = 2; // A similar array to the tile array, but containing objects - bytes objects = 3; + repeated Object objects = 3; } // From 7bdfa44fb60eda92eea042449af17a34fe2105be Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 12:33:11 +0100 Subject: [PATCH 08/12] Fix up the concept of "None" tiles and objects Replace with "Unknown" which is effectively an invalid value --- cmd/rove/main.go | 6 +-- pkg/atlas/atlas.go | 4 +- pkg/atlas/atlas_test.go | 6 +-- pkg/atlas/chunkAtlas.go | 6 +-- pkg/atlas/glyph.go | 3 -- pkg/atlas/objects.go | 4 +- pkg/rove/world.go | 10 ++-- pkg/rove/world_test.go | 18 +++---- proto/roveapi/roveapi.pb.go | 94 ++++++++++++++++++------------------- proto/roveapi/roveapi.proto | 8 ++-- 10 files changed, 72 insertions(+), 87 deletions(-) diff --git a/cmd/rove/main.go b/cmd/rove/main.go index 8e4f740..c782850 100644 --- a/cmd/rove/main.go +++ b/cmd/rove/main.go @@ -287,12 +287,10 @@ 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 != roveapi.Object_ObjectNone { + if o != roveapi.Object_ObjectUnknown { fmt.Printf("%c", atlas.ObjectGlyph(o)) - } else if t != roveapi.Tile_TileNone { - fmt.Printf("%c", atlas.TileGlyph(t)) } else { - fmt.Printf(" ") + fmt.Printf("%c", atlas.TileGlyph(t)) } } diff --git a/pkg/atlas/atlas.go b/pkg/atlas/atlas.go index de83642..dfdcb74 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/atlas/atlas.go @@ -10,8 +10,6 @@ import ( // TileGlyph returns the glyph for this tile type func TileGlyph(t roveapi.Tile) Glyph { switch t { - case roveapi.Tile_TileNone: - return GlyphNone case roveapi.Tile_Rock: return GlyphGroundRock case roveapi.Tile_Gravel: @@ -21,7 +19,7 @@ func TileGlyph(t roveapi.Tile) Glyph { } log.Fatalf("Unknown tile type: %c", t) - return GlyphNone + return 0 } // Atlas represents a 2D world atlas of tiles and objects diff --git a/pkg/atlas/atlas_test.go b/pkg/atlas/atlas_test.go index de8b7b9..bc0467a 100644 --- a/pkg/atlas/atlas_test.go +++ b/pkg/atlas/atlas_test.go @@ -260,12 +260,10 @@ 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 != roveapi.Object_ObjectNone { + if o.Type != roveapi.Object_ObjectUnknown { fmt.Printf("%c", ObjectGlyph(o.Type)) - } else if t != roveapi.Tile_TileNone { - fmt.Printf("%c", TileGlyph(t)) } else { - fmt.Printf(" ") + fmt.Printf("%c", TileGlyph(t)) } } diff --git a/pkg/atlas/chunkAtlas.go b/pkg/atlas/chunkAtlas.go index 43ca3af..3f19b07 100644 --- a/pkg/atlas/chunkAtlas.go +++ b/pkg/atlas/chunkAtlas.go @@ -121,14 +121,14 @@ 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 = roveapi.Object_ObjectNone + var obj = roveapi.Object_ObjectUnknown switch { case o > 0.6: obj = roveapi.Object_RockLarge case o > 0.5: obj = roveapi.Object_RockSmall } - if obj != roveapi.Object_ObjectNone { + if obj != roveapi.Object_ObjectUnknown { c.Objects[j*a.ChunkSize+i] = Object{Type: roveapi.Object(obj)} } } @@ -160,7 +160,7 @@ func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object Object c := a.Chunks[chunk] i := a.chunkTileIndex(local) - if object.Type != roveapi.Object_ObjectNone { + if object.Type != roveapi.Object_ObjectUnknown { c.Objects[i] = object } else { delete(c.Objects, i) diff --git a/pkg/atlas/glyph.go b/pkg/atlas/glyph.go index c5c0ad7..ff186e3 100644 --- a/pkg/atlas/glyph.go +++ b/pkg/atlas/glyph.go @@ -4,9 +4,6 @@ package atlas type Glyph byte const ( - // GlyphNone is a keyword for nothing - GlyphNone = Glyph(0) - // GlyphGroundRock is solid rock ground GlyphGroundRock = Glyph('-') diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index ccbb69b..269beb4 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -9,8 +9,6 @@ import ( // ObjectGlyph returns the glyph for this object type func ObjectGlyph(o roveapi.Object) Glyph { switch o { - case roveapi.Object_ObjectNone: - return GlyphNone case roveapi.Object_RoverLive: return GlyphRoverLive case roveapi.Object_RockSmall: @@ -20,7 +18,7 @@ func ObjectGlyph(o roveapi.Object) Glyph { } log.Fatalf("Unknown object type: %c", o) - return GlyphNone + return 0 } // Object represents an object in the world diff --git a/pkg/rove/world.go b/pkg/rove/world.go index 51b81ef..b3b15a0 100644 --- a/pkg/rove/world.go +++ b/pkg/rove/world.go @@ -324,29 +324,29 @@ func (w *World) RoverStash(rover string) (roveapi.Object, error) { r, ok := w.Rovers[rover] if !ok { - return roveapi.Object_ObjectNone, fmt.Errorf("no rover matching id") + return roveapi.Object_ObjectUnknown, fmt.Errorf("no rover matching id") } // Can't pick up when full if len(r.Inventory) >= r.Capacity { - return roveapi.Object_ObjectNone, nil + return roveapi.Object_ObjectUnknown, nil } // Ensure the rover has energy if r.Charge <= 0 { - return roveapi.Object_ObjectNone, nil + return roveapi.Object_ObjectUnknown, nil } r.Charge-- _, obj := w.Atlas.QueryPosition(r.Pos) if !obj.IsStashable() { - return roveapi.Object_ObjectNone, nil + return roveapi.Object_ObjectUnknown, nil } r.AddLogEntryf("stashed %c", obj.Type) r.Inventory = append(r.Inventory, obj) w.Rovers[rover] = r - w.Atlas.SetObject(r.Pos, atlas.Object{Type: roveapi.Object_ObjectNone}) + w.Atlas.SetObject(r.Pos, atlas.Object{Type: roveapi.Object_ObjectUnknown}) return obj.Type, nil } diff --git a/pkg/rove/world_test.go b/pkg/rove/world_test.go index 8142858..6534d89 100644 --- a/pkg/rove/world_test.go +++ b/pkg/rove/world_test.go @@ -142,13 +142,10 @@ func TestWorld_RoverStash(t *testing.T) { Y: 0.0, } - world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_ObjectNone}) + world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_ObjectUnknown}) err = world.WarpRover(a, pos) assert.NoError(t, err, "Failed to set position for rover") - // Set to a traversible tile - world.Atlas.SetTile(pos, roveapi.Tile_TileNone) - rover, err := world.GetRover(a) assert.NoError(t, err, "Failed to get rover") @@ -163,7 +160,7 @@ func TestWorld_RoverStash(t *testing.T) { // Check it's gone _, obj := world.Atlas.QueryPosition(pos) - assert.Equal(t, roveapi.Object_ObjectNone, obj.Type, "Stash failed to remove object from atlas") + assert.Equal(t, roveapi.Object_ObjectUnknown, obj.Type, "Stash failed to remove object from atlas") // Check we have it inv, err := world.RoverInventory(a) @@ -191,7 +188,7 @@ func TestWorld_RoverStash(t *testing.T) { // Try to pick it up o, err := world.RoverStash(a) assert.NoError(t, err, "Failed to stash") - assert.Equal(t, roveapi.Object_ObjectNone, o, "Failed to get correct object") + assert.Equal(t, roveapi.Object_ObjectUnknown, o, "Failed to get correct object") // Check it's still there _, obj := world.Atlas.QueryPosition(pos) @@ -246,8 +243,7 @@ func TestWorld_RoverRepair(t *testing.T) { Y: 0.0, } - world.Atlas.SetTile(pos, roveapi.Tile_TileNone) - world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_ObjectNone}) + world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_ObjectUnknown}) err = world.WarpRover(a, pos) assert.NoError(t, err, "Failed to set position for rover") @@ -312,7 +308,7 @@ func TestWorld_Charge(t *testing.T) { // Ensure the path ahead is empty world.Atlas.SetTile(initialPos.Added(maths.North.Vector()), roveapi.Tile_Rock) - world.Atlas.SetObject(initialPos.Added(maths.North.Vector()), atlas.Object{Type: roveapi.Object_ObjectNone}) + world.Atlas.SetObject(initialPos.Added(maths.North.Vector()), atlas.Object{Type: roveapi.Object_ObjectUnknown}) // Try and move north (along unblocked path) newPos, err := world.MoveRover(a, maths.North) @@ -394,7 +390,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}, atlas.Object{Type: roveapi.Object_ObjectNone}) + world.Atlas.SetObject(maths.Vector{X: ra.Range, Y: 0}, atlas.Object{Type: roveapi.Object_ObjectUnknown}) assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range, Y: 0})) // Broadcast from a again @@ -411,7 +407,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}, atlas.Object{Type: roveapi.Object_ObjectNone}) + world.Atlas.SetObject(maths.Vector{X: ra.Range + 1, Y: 0}, atlas.Object{Type: roveapi.Object_ObjectUnknown}) assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range + 1, Y: 0})) // Broadcast from a again diff --git a/proto/roveapi/roveapi.pb.go b/proto/roveapi/roveapi.pb.go index 842d574..dd47ca9 100644 --- a/proto/roveapi/roveapi.pb.go +++ b/proto/roveapi/roveapi.pb.go @@ -102,8 +102,8 @@ func (CommandType) EnumDescriptor() ([]byte, []int) { type Object int32 const ( - // ObjectNone represents no object at all - Object_ObjectNone Object = 0 + // ObjectUnknown represents no object at all + Object_ObjectUnknown Object = 0 // RoverLive represents a live rover Object_RoverLive Object = 1 // RockSmall is a small stashable rock @@ -115,16 +115,16 @@ const ( // Enum value maps for Object. var ( Object_name = map[int32]string{ - 0: "ObjectNone", + 0: "ObjectUnknown", 1: "RoverLive", 2: "RockSmall", 3: "RockLarge", } Object_value = map[string]int32{ - "ObjectNone": 0, - "RoverLive": 1, - "RockSmall": 2, - "RockLarge": 3, + "ObjectUnknown": 0, + "RoverLive": 1, + "RockSmall": 2, + "RockLarge": 3, } ) @@ -158,8 +158,8 @@ func (Object) EnumDescriptor() ([]byte, []int) { type Tile int32 const ( - // TileNone is a keyword for nothing - Tile_TileNone Tile = 0 + // TileUnknown is a keyword for nothing + Tile_TileUnknown Tile = 0 // Rock is solid rock ground Tile_Rock Tile = 1 // Gravel is loose rocks @@ -171,16 +171,16 @@ const ( // Enum value maps for Tile. var ( Tile_name = map[int32]string{ - 0: "TileNone", + 0: "TileUnknown", 1: "Rock", 2: "Gravel", 3: "Sand", } Tile_value = map[string]int32{ - "TileNone": 0, - "Rock": 1, - "Gravel": 2, - "Sand": 3, + "TileUnknown": 0, + "Rock": 1, + "Gravel": 2, + "Sand": 3, } ) @@ -1207,39 +1207,39 @@ var file_roveapi_roveapi_proto_rawDesc = []byte{ 0x01, 0x12, 0x09, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x73, 0x68, 0x10, 0x02, 0x12, 0x0a, 0x0a, 0x06, 0x72, 0x65, 0x70, 0x61, 0x69, 0x72, 0x10, 0x03, 0x12, 0x0c, 0x0a, 0x08, 0x72, 0x65, 0x63, 0x68, 0x61, 0x72, 0x67, 0x65, 0x10, 0x04, 0x12, 0x0d, 0x0a, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, - 0x61, 0x73, 0x74, 0x10, 0x05, 0x2a, 0x45, 0x0a, 0x06, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x12, - 0x0e, 0x0a, 0x0a, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x4e, 0x6f, 0x6e, 0x65, 0x10, 0x00, 0x12, - 0x0d, 0x0a, 0x09, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x4c, 0x69, 0x76, 0x65, 0x10, 0x01, 0x12, 0x0d, - 0x0a, 0x09, 0x52, 0x6f, 0x63, 0x6b, 0x53, 0x6d, 0x61, 0x6c, 0x6c, 0x10, 0x02, 0x12, 0x0d, 0x0a, - 0x09, 0x52, 0x6f, 0x63, 0x6b, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x10, 0x03, 0x2a, 0x34, 0x0a, 0x04, - 0x54, 0x69, 0x6c, 0x65, 0x12, 0x0c, 0x0a, 0x08, 0x54, 0x69, 0x6c, 0x65, 0x4e, 0x6f, 0x6e, 0x65, - 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x52, 0x6f, 0x63, 0x6b, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, - 0x47, 0x72, 0x61, 0x76, 0x65, 0x6c, 0x10, 0x02, 0x12, 0x08, 0x0a, 0x04, 0x53, 0x61, 0x6e, 0x64, - 0x10, 0x03, 0x32, 0xcf, 0x02, 0x0a, 0x04, 0x52, 0x6f, 0x76, 0x65, 0x12, 0x4d, 0x0a, 0x0c, 0x53, - 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1c, 0x2e, 0x72, 0x6f, - 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, - 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x72, 0x6f, 0x76, 0x65, - 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, - 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x41, 0x0a, 0x08, 0x52, 0x65, - 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x12, 0x18, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, - 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x19, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, - 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3e, 0x0a, - 0x07, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x17, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, - 0x70, 0x69, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x1a, 0x18, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, - 0x61, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x38, 0x0a, - 0x05, 0x52, 0x61, 0x64, 0x61, 0x72, 0x12, 0x15, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, - 0x2e, 0x52, 0x61, 0x64, 0x61, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, - 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x61, 0x64, 0x61, 0x72, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3b, 0x0a, 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, - 0x73, 0x12, 0x16, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x74, 0x61, 0x74, - 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x72, 0x6f, 0x76, 0x65, - 0x61, 0x70, 0x69, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x22, 0x00, 0x42, 0x26, 0x5a, 0x24, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x6d, 0x64, 0x69, 0x6c, 0x75, 0x7a, 0x2f, 0x72, 0x6f, 0x76, 0x65, 0x2f, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x61, 0x73, 0x74, 0x10, 0x05, 0x2a, 0x48, 0x0a, 0x06, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x12, + 0x11, 0x0a, 0x0d, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, + 0x10, 0x00, 0x12, 0x0d, 0x0a, 0x09, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x4c, 0x69, 0x76, 0x65, 0x10, + 0x01, 0x12, 0x0d, 0x0a, 0x09, 0x52, 0x6f, 0x63, 0x6b, 0x53, 0x6d, 0x61, 0x6c, 0x6c, 0x10, 0x02, + 0x12, 0x0d, 0x0a, 0x09, 0x52, 0x6f, 0x63, 0x6b, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x10, 0x03, 0x2a, + 0x37, 0x0a, 0x04, 0x54, 0x69, 0x6c, 0x65, 0x12, 0x0f, 0x0a, 0x0b, 0x54, 0x69, 0x6c, 0x65, 0x55, + 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x52, 0x6f, 0x63, 0x6b, + 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x47, 0x72, 0x61, 0x76, 0x65, 0x6c, 0x10, 0x02, 0x12, 0x08, + 0x0a, 0x04, 0x53, 0x61, 0x6e, 0x64, 0x10, 0x03, 0x32, 0xcf, 0x02, 0x0a, 0x04, 0x52, 0x6f, 0x76, + 0x65, 0x12, 0x4d, 0x0a, 0x0c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, + 0x73, 0x12, 0x1c, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x72, 0x76, + 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x1d, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, + 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, + 0x12, 0x41, 0x0a, 0x08, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x12, 0x18, 0x2e, 0x72, + 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, + 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x22, 0x00, 0x12, 0x3e, 0x0a, 0x07, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x17, + 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, + 0x69, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x22, 0x00, 0x12, 0x38, 0x0a, 0x05, 0x52, 0x61, 0x64, 0x61, 0x72, 0x12, 0x15, 0x2e, 0x72, + 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x61, 0x64, 0x61, 0x72, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x61, + 0x64, 0x61, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3b, 0x0a, + 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x16, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, + 0x69, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x17, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x26, 0x5a, 0x24, 0x67, 0x69, + 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d, 0x64, 0x69, 0x6c, 0x75, 0x7a, 0x2f, + 0x72, 0x6f, 0x76, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x72, 0x6f, 0x76, 0x65, 0x61, + 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/proto/roveapi/roveapi.proto b/proto/roveapi/roveapi.proto index 62dbc5a..0813533 100644 --- a/proto/roveapi/roveapi.proto +++ b/proto/roveapi/roveapi.proto @@ -136,8 +136,8 @@ message CommandResponse {} // Types of objects enum Object { - // ObjectNone represents no object at all - ObjectNone = 0; + // ObjectUnknown represents no object at all + ObjectUnknown = 0; // RoverLive represents a live rover RoverLive = 1; @@ -150,8 +150,8 @@ enum Object { } enum Tile { - // TileNone is a keyword for nothing - TileNone = 0; + // TileUnknown is a keyword for nothing + TileUnknown = 0; // Rock is solid rock ground Rock = 1; From 4a89cb9d6e76eb1a4a36dd84ec98d49343ab9c4d Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 12:34:54 +0100 Subject: [PATCH 09/12] Move glyph functions out to the glyph file --- pkg/atlas/atlas.go | 17 ----------------- pkg/atlas/glyph.go | 36 ++++++++++++++++++++++++++++++++++++ pkg/atlas/objects.go | 17 ----------------- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/pkg/atlas/atlas.go b/pkg/atlas/atlas.go index dfdcb74..05012aa 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/atlas/atlas.go @@ -1,27 +1,10 @@ package atlas import ( - "log" - "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/proto/roveapi" ) -// TileGlyph returns the glyph for this tile type -func TileGlyph(t roveapi.Tile) Glyph { - switch t { - case roveapi.Tile_Rock: - return GlyphGroundRock - case roveapi.Tile_Gravel: - return GlyphGroundGravel - case roveapi.Tile_Sand: - return GlyphGroundSand - } - - log.Fatalf("Unknown tile type: %c", t) - return 0 -} - // 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 diff --git a/pkg/atlas/glyph.go b/pkg/atlas/glyph.go index ff186e3..183f460 100644 --- a/pkg/atlas/glyph.go +++ b/pkg/atlas/glyph.go @@ -1,5 +1,11 @@ package atlas +import ( + "log" + + "github.com/mdiluz/rove/proto/roveapi" +) + // Glyph represents the text representation of something in the game type Glyph byte @@ -22,3 +28,33 @@ const ( // GlyphRockLarge is a large blocking rock GlyphRockLarge = Glyph('O') ) + +// TileGlyph returns the glyph for this tile type +func TileGlyph(t roveapi.Tile) Glyph { + switch t { + case roveapi.Tile_Rock: + return GlyphGroundRock + case roveapi.Tile_Gravel: + return GlyphGroundGravel + case roveapi.Tile_Sand: + return GlyphGroundSand + } + + log.Fatalf("Unknown tile type: %c", t) + return 0 +} + +// ObjectGlyph returns the glyph for this object type +func ObjectGlyph(o roveapi.Object) Glyph { + switch o { + case roveapi.Object_RoverLive: + return GlyphRoverLive + case roveapi.Object_RockSmall: + return GlyphRockSmall + case roveapi.Object_RockLarge: + return GlyphRockLarge + } + + log.Fatalf("Unknown object type: %c", o) + return 0 +} diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index 269beb4..e1b6730 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -1,26 +1,9 @@ package atlas import ( - "log" - "github.com/mdiluz/rove/proto/roveapi" ) -// ObjectGlyph returns the glyph for this object type -func ObjectGlyph(o roveapi.Object) Glyph { - switch o { - case roveapi.Object_RoverLive: - return GlyphRoverLive - case roveapi.Object_RockSmall: - return GlyphRockSmall - case roveapi.Object_RockLarge: - return GlyphRockLarge - } - - log.Fatalf("Unknown object type: %c", o) - return 0 -} - // Object represents an object in the world type Object struct { // The type of the object From da91d316491e9bf3ce063775cbb53631a3231083 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 12:36:48 +0100 Subject: [PATCH 10/12] MOve glyph code into client --- {pkg/atlas => cmd/rove}/glyph.go | 2 +- cmd/rove/main.go | 5 ++--- pkg/atlas/atlas_test.go | 17 +---------------- 3 files changed, 4 insertions(+), 20 deletions(-) rename {pkg/atlas => cmd/rove}/glyph.go (98%) diff --git a/pkg/atlas/glyph.go b/cmd/rove/glyph.go similarity index 98% rename from pkg/atlas/glyph.go rename to cmd/rove/glyph.go index 183f460..57f556d 100644 --- a/pkg/atlas/glyph.go +++ b/cmd/rove/glyph.go @@ -1,4 +1,4 @@ -package atlas +package main import ( "log" diff --git a/cmd/rove/main.go b/cmd/rove/main.go index c782850..e00fd9f 100644 --- a/cmd/rove/main.go +++ b/cmd/rove/main.go @@ -10,7 +10,6 @@ import ( "path/filepath" "time" - "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/pkg/version" "github.com/mdiluz/rove/proto/roveapi" @@ -288,9 +287,9 @@ func InnerMain(command string, args ...string) error { t := response.Tiles[i+num*j] o := response.Objects[i+num*j] if o != roveapi.Object_ObjectUnknown { - fmt.Printf("%c", atlas.ObjectGlyph(o)) + fmt.Printf("%c", ObjectGlyph(o)) } else { - fmt.Printf("%c", atlas.TileGlyph(t)) + fmt.Printf("%c", TileGlyph(t)) } } diff --git a/pkg/atlas/atlas_test.go b/pkg/atlas/atlas_test.go index bc0467a..3d715f1 100644 --- a/pkg/atlas/atlas_test.go +++ b/pkg/atlas/atlas_test.go @@ -1,7 +1,6 @@ package atlas import ( - "fmt" "testing" "github.com/mdiluz/rove/pkg/maths" @@ -252,21 +251,7 @@ func TestAtlas_GetSetCorrect(t *testing.T) { func TestAtlas_WorldGen(t *testing.T) { a := NewChunkAtlas(8) + // Spawn a large world _, _ = 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(maths.Vector{X: i, Y: j}) - if o.Type != roveapi.Object_ObjectUnknown { - fmt.Printf("%c", ObjectGlyph(o.Type)) - } else { - fmt.Printf("%c", TileGlyph(t)) - } - - } - fmt.Print("\n") - } } From e9188dbbf654bfa30a7f6611251b0d5b7bf13a94 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 12:37:36 +0100 Subject: [PATCH 11/12] Auto-format proto file --- proto/roveapi/roveapi.proto | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/proto/roveapi/roveapi.proto b/proto/roveapi/roveapi.proto index 0813533..680aa9a 100644 --- a/proto/roveapi/roveapi.proto +++ b/proto/roveapi/roveapi.proto @@ -136,31 +136,31 @@ message CommandResponse {} // Types of objects enum Object { - // ObjectUnknown represents no object at all - ObjectUnknown = 0; + // ObjectUnknown represents no object at all + ObjectUnknown = 0; - // RoverLive represents a live rover - RoverLive = 1; + // RoverLive represents a live rover + RoverLive = 1; - // RockSmall is a small stashable rock - RockSmall = 2; + // RockSmall is a small stashable rock + RockSmall = 2; - // RockLarge is a large blocking rock - RockLarge = 3; + // RockLarge is a large blocking rock + RockLarge = 3; } enum Tile { - // TileUnknown is a keyword for nothing - TileUnknown = 0; + // TileUnknown is a keyword for nothing + TileUnknown = 0; - // Rock is solid rock ground - Rock = 1; + // Rock is solid rock ground + Rock = 1; - // Gravel is loose rocks - Gravel = 2; + // Gravel is loose rocks + Gravel = 2; - // Sand is sand - Sand = 3; + // Sand is sand + Sand = 3; } // RadarRequest is the data needed to request the radar for a rover From cd6a275bb97a6800a774c859ddec1c30a50c4cda Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 12:59:36 +0100 Subject: [PATCH 12/12] Move code to internal cmd/main --- cmd/rove/{ => internal}/glyph.go | 2 +- cmd/rove/main.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) rename cmd/rove/{ => internal}/glyph.go (98%) diff --git a/cmd/rove/glyph.go b/cmd/rove/internal/glyph.go similarity index 98% rename from cmd/rove/glyph.go rename to cmd/rove/internal/glyph.go index 57f556d..1a26f5d 100644 --- a/cmd/rove/glyph.go +++ b/cmd/rove/internal/glyph.go @@ -1,4 +1,4 @@ -package main +package internal import ( "log" diff --git a/cmd/rove/main.go b/cmd/rove/main.go index 17d8dd4..4e3a1d2 100644 --- a/cmd/rove/main.go +++ b/cmd/rove/main.go @@ -10,6 +10,7 @@ import ( "path/filepath" "time" + "github.com/mdiluz/rove/cmd/rove/internal" "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/pkg/version" "github.com/mdiluz/rove/proto/roveapi" @@ -287,9 +288,9 @@ func InnerMain(command string, args ...string) error { t := response.Tiles[i+num*j] o := response.Objects[i+num*j] if o != roveapi.Object_ObjectUnknown { - fmt.Printf("%c", ObjectGlyph(o)) + fmt.Printf("%c", internal.ObjectGlyph(o)) } else { - fmt.Printf("%c", TileGlyph(t)) + fmt.Printf("%c", internal.TileGlyph(t)) } }