From fd0992353d282c1c7b0855afb8121bbf4d1a3b1b Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 13:27:29 +0100 Subject: [PATCH 01/18] Add data to objects --- pkg/atlas/objects.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index e1b6730..69ce560 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -8,6 +8,9 @@ import ( type Object struct { // The type of the object Type roveapi.Object `json:"type"` + + // Data is an internal type used for certain types of object + Data []byte `json:"data"` } // IsBlocking checks if an object is a blocking object From 713699687f0d21f4047fba2f9ad528aa1b8c14f2 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 13:27:38 +0100 Subject: [PATCH 02/18] Add a dormat rover data type --- proto/roveapi/roveapi.pb.go | 67 ++++++++++++++++++++----------------- proto/roveapi/roveapi.proto | 3 ++ 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/proto/roveapi/roveapi.pb.go b/proto/roveapi/roveapi.pb.go index e35c863..2f5e069 100644 --- a/proto/roveapi/roveapi.pb.go +++ b/proto/roveapi/roveapi.pb.go @@ -162,6 +162,8 @@ const ( Object_ObjectUnknown Object = 0 // RoverLive represents a live rover Object_RoverLive Object = 1 + // RoverDormant describes a dormant rover + Object_RoverDormant Object = 4 // RockSmall is a small stashable rock Object_RockSmall Object = 2 // RockLarge is a large blocking rock @@ -173,12 +175,14 @@ var ( Object_name = map[int32]string{ 0: "ObjectUnknown", 1: "RoverLive", + 4: "RoverDormant", 2: "RockSmall", 3: "RockLarge", } Object_value = map[string]int32{ "ObjectUnknown": 0, "RoverLive": 1, + "RoverDormant": 4, "RockSmall": 2, "RockLarge": 3, } @@ -1269,39 +1273,40 @@ var file_roveapi_roveapi_proto_rawDesc = []byte{ 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x4e, 0x6f, 0x72, 0x74, 0x68, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x04, 0x45, 0x61, 0x73, 0x74, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x53, 0x6f, 0x75, 0x74, 0x68, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x57, 0x65, 0x73, 0x74, 0x10, - 0x04, 0x2a, 0x48, 0x0a, 0x06, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x12, 0x11, 0x0a, 0x0d, 0x4f, + 0x04, 0x2a, 0x5a, 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, + 0x0a, 0x09, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x4c, 0x69, 0x76, 0x65, 0x10, 0x01, 0x12, 0x10, 0x0a, + 0x0c, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x44, 0x6f, 0x72, 0x6d, 0x61, 0x6e, 0x74, 0x10, 0x04, 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, 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, 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 b43087b..7d227fb 100644 --- a/proto/roveapi/roveapi.proto +++ b/proto/roveapi/roveapi.proto @@ -151,6 +151,9 @@ enum Object { // RoverLive represents a live rover RoverLive = 1; + // RoverDormant describes a dormant rover + RoverDormant = 4; + // RockSmall is a small stashable rock RockSmall = 2; From 4f1a9c2c2b4b6f4a2de506982afc625927921830 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 13:27:59 +0100 Subject: [PATCH 03/18] Re-order object types --- proto/roveapi/roveapi.pb.go | 24 ++++++++++++------------ proto/roveapi/roveapi.proto | 6 +++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/proto/roveapi/roveapi.pb.go b/proto/roveapi/roveapi.pb.go index 2f5e069..783325f 100644 --- a/proto/roveapi/roveapi.pb.go +++ b/proto/roveapi/roveapi.pb.go @@ -163,11 +163,11 @@ const ( // RoverLive represents a live rover Object_RoverLive Object = 1 // RoverDormant describes a dormant rover - Object_RoverDormant Object = 4 + Object_RoverDormant Object = 2 // RockSmall is a small stashable rock - Object_RockSmall Object = 2 + Object_RockSmall Object = 3 // RockLarge is a large blocking rock - Object_RockLarge Object = 3 + Object_RockLarge Object = 4 ) // Enum value maps for Object. @@ -175,16 +175,16 @@ var ( Object_name = map[int32]string{ 0: "ObjectUnknown", 1: "RoverLive", - 4: "RoverDormant", - 2: "RockSmall", - 3: "RockLarge", + 2: "RoverDormant", + 3: "RockSmall", + 4: "RockLarge", } Object_value = map[string]int32{ "ObjectUnknown": 0, "RoverLive": 1, - "RoverDormant": 4, - "RockSmall": 2, - "RockLarge": 3, + "RoverDormant": 2, + "RockSmall": 3, + "RockLarge": 4, } ) @@ -1276,9 +1276,9 @@ var file_roveapi_roveapi_proto_rawDesc = []byte{ 0x04, 0x2a, 0x5a, 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, 0x10, 0x0a, - 0x0c, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x44, 0x6f, 0x72, 0x6d, 0x61, 0x6e, 0x74, 0x10, 0x04, 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, + 0x0c, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x44, 0x6f, 0x72, 0x6d, 0x61, 0x6e, 0x74, 0x10, 0x02, 0x12, + 0x0d, 0x0a, 0x09, 0x52, 0x6f, 0x63, 0x6b, 0x53, 0x6d, 0x61, 0x6c, 0x6c, 0x10, 0x03, 0x12, 0x0d, + 0x0a, 0x09, 0x52, 0x6f, 0x63, 0x6b, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x10, 0x04, 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, diff --git a/proto/roveapi/roveapi.proto b/proto/roveapi/roveapi.proto index 7d227fb..566bcfd 100644 --- a/proto/roveapi/roveapi.proto +++ b/proto/roveapi/roveapi.proto @@ -152,13 +152,13 @@ enum Object { RoverLive = 1; // RoverDormant describes a dormant rover - RoverDormant = 4; + RoverDormant = 2; // RockSmall is a small stashable rock - RockSmall = 2; + RockSmall = 3; // RockLarge is a large blocking rock - RockLarge = 3; + RockLarge = 4; } enum Tile { From 1eba9a865279f0f4d28b714f7c42978935f8876e Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 13:41:47 +0100 Subject: [PATCH 04/18] Pull world gen out into interface --- pkg/atlas/chunkAtlas.go | 64 +++++++++++++---------------------------- pkg/atlas/worldgen.go | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 44 deletions(-) create mode 100644 pkg/atlas/worldgen.go diff --git a/pkg/atlas/chunkAtlas.go b/pkg/atlas/chunkAtlas.go index 3f19b07..4abb0e8 100644 --- a/pkg/atlas/chunkAtlas.go +++ b/pkg/atlas/chunkAtlas.go @@ -6,7 +6,6 @@ import ( "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/proto/roveapi" - "github.com/ojrac/opensimplex-go" ) // chunk represents a fixed square grid of tiles @@ -34,29 +33,23 @@ type chunkBasedAtlas struct { // ChunkSize is the x/y dimensions of each square chunk ChunkSize int `json:"chunksize"` - // terrainNoise describes the noise function for the terrain - terrainNoise opensimplex.Noise - - // terrainNoise describes the noise function for the terrain - objectNoise opensimplex.Noise + // worldGen is the internal world generator + worldGen WorldGen } const ( - noiseSeed = 1024 - terrainNoiseScale = 6 - objectNoiseScale = 3 + noiseSeed = 1024 ) // NewChunkAtlas creates a new empty atlas func NewChunkAtlas(chunkSize int) Atlas { // Start up with one chunk a := chunkBasedAtlas{ - ChunkSize: chunkSize, - Chunks: make([]chunk, 1), - LowerBound: maths.Vector{X: 0, Y: 0}, - UpperBound: maths.Vector{X: chunkSize, Y: chunkSize}, - terrainNoise: opensimplex.New(noiseSeed), - objectNoise: opensimplex.New(noiseSeed), + ChunkSize: chunkSize, + Chunks: make([]chunk, 1), + LowerBound: maths.Vector{X: 0, Y: 0}, + UpperBound: maths.Vector{X: chunkSize, Y: chunkSize}, + worldGen: NewNoiseWorldGen(noiseSeed), } // Initialise the first chunk a.populate(0) @@ -105,31 +98,15 @@ func (a *chunkBasedAtlas) populate(chunk int) { origin := a.chunkOriginInWorldSpace(chunk) for i := 0; i < a.ChunkSize; i++ { for j := 0; j < a.ChunkSize; j++ { + loc := maths.Vector{X: origin.X + i, Y: origin.Y + j} - // Get the terrain noise value for this location - t := a.terrainNoise.Eval2(float64(origin.X+i)/terrainNoiseScale, float64(origin.Y+j)/terrainNoiseScale) - var tile roveapi.Tile - switch { - case t > 0.5: - tile = roveapi.Tile_Gravel - case t > 0.05: - tile = roveapi.Tile_Sand - default: - tile = roveapi.Tile_Rock - } - c.Tiles[j*a.ChunkSize+i] = byte(tile) + // Set the tile + c.Tiles[j*a.ChunkSize+i] = byte(a.worldGen.GetTile(loc)) - // 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_ObjectUnknown - switch { - case o > 0.6: - obj = roveapi.Object_RockLarge - case o > 0.5: - obj = roveapi.Object_RockSmall - } - if obj != roveapi.Object_ObjectUnknown { - c.Objects[j*a.ChunkSize+i] = Object{Type: roveapi.Object(obj)} + // Set the object + obj := a.worldGen.GetObject(loc) + if obj.Type != roveapi.Object_ObjectUnknown { + c.Objects[j*a.ChunkSize+i] = obj } } } @@ -236,12 +213,11 @@ func (a *chunkBasedAtlas) worldSpaceToChunkWithGrow(v maths.Vector) int { // Create the new empty atlas newAtlas := chunkBasedAtlas{ - ChunkSize: a.ChunkSize, - LowerBound: lower, - UpperBound: upper, - Chunks: make([]chunk, size.X*size.Y), - terrainNoise: a.terrainNoise, - objectNoise: a.objectNoise, + ChunkSize: a.ChunkSize, + LowerBound: lower, + UpperBound: upper, + Chunks: make([]chunk, size.X*size.Y), + worldGen: a.worldGen, } // Log that we're resizing diff --git a/pkg/atlas/worldgen.go b/pkg/atlas/worldgen.go new file mode 100644 index 0000000..38bcd4f --- /dev/null +++ b/pkg/atlas/worldgen.go @@ -0,0 +1,64 @@ +package atlas + +import ( + "github.com/mdiluz/rove/pkg/maths" + "github.com/mdiluz/rove/proto/roveapi" + "github.com/ojrac/opensimplex-go" +) + +// WorldGen describes a world gen algorythm +type WorldGen interface { + // GetTile generates a tile for a location + GetTile(v maths.Vector) roveapi.Tile + + // GetObject generates an object for a location + GetObject(v maths.Vector) Object +} + +// NoiseWorldGen returns a noise based world generator +type NoiseWorldGen struct { + // terrainNoise describes the noise function for the terrain + terrainNoise opensimplex.Noise + + // terrainNoise describes the noise function for the terrain + objectNoise opensimplex.Noise +} + +// NewNoiseWorldGen creates a new noise based world generator +func NewNoiseWorldGen(seed int64) WorldGen { + return &NoiseWorldGen{ + terrainNoise: opensimplex.New(seed), + objectNoise: opensimplex.New(seed), + } +} + +const ( + terrainNoiseScale = 6 + objectNoiseScale = 3 +) + +// GetTile returns the chosen tile at a location +func (g *NoiseWorldGen) GetTile(v maths.Vector) roveapi.Tile { + t := g.terrainNoise.Eval2(float64(v.X)/terrainNoiseScale, float64(v.Y)/terrainNoiseScale) + switch { + case t > 0.5: + return roveapi.Tile_Gravel + case t > 0.05: + return roveapi.Tile_Sand + default: + return roveapi.Tile_Rock + } +} + +// GetObject returns the chosen object at a location +func (g *NoiseWorldGen) GetObject(v maths.Vector) Object { + o := g.objectNoise.Eval2(float64(v.X)/objectNoiseScale, float64(v.Y)/objectNoiseScale) + var obj = roveapi.Object_ObjectUnknown + switch { + case o > 0.6: + obj = roveapi.Object_RockLarge + case o > 0.5: + obj = roveapi.Object_RockSmall + } + return Object{Type: roveapi.Object(obj)} +} From 87a9abcd12d26bce54cf2281b006f764e379cb0f Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 13:49:34 +0100 Subject: [PATCH 05/18] Add a glyph for the dormant rover --- cmd/rove/internal/glyph.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/rove/internal/glyph.go b/cmd/rove/internal/glyph.go index 1a26f5d..eac12c0 100644 --- a/cmd/rove/internal/glyph.go +++ b/cmd/rove/internal/glyph.go @@ -22,6 +22,9 @@ const ( // GlyphRoverLive represents a live rover GlyphRoverLive = Glyph('R') + // GlyphRoverDormant represents a dormant rover + GlyphRoverDormant = Glyph('r') + // GlyphRockSmall is a small stashable rock GlyphRockSmall = Glyph('o') @@ -51,6 +54,8 @@ func ObjectGlyph(o roveapi.Object) Glyph { return GlyphRoverLive case roveapi.Object_RockSmall: return GlyphRockSmall + case roveapi.Object_RoverDormant: + return GlyphRoverDormant case roveapi.Object_RockLarge: return GlyphRockLarge } From c637ed37b93018a7c74e9fba90b7971fe17f3663 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 13:49:43 +0100 Subject: [PATCH 06/18] Make the dormat rover blocking --- pkg/atlas/objects.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/atlas/objects.go b/pkg/atlas/objects.go index 69ce560..cadfa4a 100644 --- a/pkg/atlas/objects.go +++ b/pkg/atlas/objects.go @@ -17,6 +17,7 @@ type Object struct { func (o *Object) IsBlocking() bool { var blocking = [...]roveapi.Object{ roveapi.Object_RoverLive, + roveapi.Object_RoverDormant, roveapi.Object_RockLarge, } From 959cbfa15a036a34a97d0ce55b0ec3b6b6a9fa45 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 13:51:49 +0100 Subject: [PATCH 07/18] Combine the two noise functions, we only need one --- pkg/atlas/worldgen.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/pkg/atlas/worldgen.go b/pkg/atlas/worldgen.go index 38bcd4f..6c5f4b7 100644 --- a/pkg/atlas/worldgen.go +++ b/pkg/atlas/worldgen.go @@ -17,18 +17,14 @@ type WorldGen interface { // NoiseWorldGen returns a noise based world generator type NoiseWorldGen struct { - // terrainNoise describes the noise function for the terrain - terrainNoise opensimplex.Noise - - // terrainNoise describes the noise function for the terrain - objectNoise opensimplex.Noise + // noise describes the noise function + noise opensimplex.Noise } // NewNoiseWorldGen creates a new noise based world generator func NewNoiseWorldGen(seed int64) WorldGen { return &NoiseWorldGen{ - terrainNoise: opensimplex.New(seed), - objectNoise: opensimplex.New(seed), + noise: opensimplex.New(seed), } } @@ -39,7 +35,7 @@ const ( // GetTile returns the chosen tile at a location func (g *NoiseWorldGen) GetTile(v maths.Vector) roveapi.Tile { - t := g.terrainNoise.Eval2(float64(v.X)/terrainNoiseScale, float64(v.Y)/terrainNoiseScale) + t := g.noise.Eval2(float64(v.X)/terrainNoiseScale, float64(v.Y)/terrainNoiseScale) switch { case t > 0.5: return roveapi.Tile_Gravel @@ -52,7 +48,7 @@ func (g *NoiseWorldGen) GetTile(v maths.Vector) roveapi.Tile { // GetObject returns the chosen object at a location func (g *NoiseWorldGen) GetObject(v maths.Vector) Object { - o := g.objectNoise.Eval2(float64(v.X)/objectNoiseScale, float64(v.Y)/objectNoiseScale) + o := g.noise.Eval2(float64(v.X)/objectNoiseScale, float64(v.Y)/objectNoiseScale) var obj = roveapi.Object_ObjectUnknown switch { case o > 0.6: From 37d828c457966ba871f444b0eeb9ea377106c347 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 13:53:38 +0100 Subject: [PATCH 08/18] Rename the rock noise --- pkg/atlas/worldgen.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/atlas/worldgen.go b/pkg/atlas/worldgen.go index 6c5f4b7..590596a 100644 --- a/pkg/atlas/worldgen.go +++ b/pkg/atlas/worldgen.go @@ -30,7 +30,7 @@ func NewNoiseWorldGen(seed int64) WorldGen { const ( terrainNoiseScale = 6 - objectNoiseScale = 3 + rockNoiseScale = 3 ) // GetTile returns the chosen tile at a location @@ -48,7 +48,7 @@ func (g *NoiseWorldGen) GetTile(v maths.Vector) roveapi.Tile { // GetObject returns the chosen object at a location func (g *NoiseWorldGen) GetObject(v maths.Vector) Object { - o := g.noise.Eval2(float64(v.X)/objectNoiseScale, float64(v.Y)/objectNoiseScale) + o := g.noise.Eval2(float64(v.X)/rockNoiseScale, float64(v.Y)/rockNoiseScale) var obj = roveapi.Object_ObjectUnknown switch { case o > 0.6: From faa1271c5a01bb555ceb3fc9d678e333bd5a4691 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 13:57:45 +0100 Subject: [PATCH 09/18] Try and very rarely spawn a dormant rover --- pkg/atlas/worldgen.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/atlas/worldgen.go b/pkg/atlas/worldgen.go index 590596a..8ec8571 100644 --- a/pkg/atlas/worldgen.go +++ b/pkg/atlas/worldgen.go @@ -31,6 +31,7 @@ func NewNoiseWorldGen(seed int64) WorldGen { const ( terrainNoiseScale = 6 rockNoiseScale = 3 + dormantRoverScale = 25 ) // GetTile returns the chosen tile at a location @@ -56,5 +57,14 @@ func (g *NoiseWorldGen) GetObject(v maths.Vector) Object { case o > 0.5: obj = roveapi.Object_RockSmall } + + // Very rarely spawn a dormant rover + if obj == roveapi.Object_ObjectUnknown { + o = g.noise.Eval2(float64(v.X)/dormantRoverScale, float64(v.Y)/dormantRoverScale) + if o > 0.8 { + obj = roveapi.Object_RoverDormant + } + } + return Object{Type: roveapi.Object(obj)} } From ddbbdce1f8f835c9cd68fbe72c697935fdafb675 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:23:11 +0100 Subject: [PATCH 10/18] Move default rover params to function --- pkg/rove/rover.go | 14 ++++++++++++++ pkg/rove/world.go | 11 +---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/rove/rover.go b/pkg/rove/rover.go index 089b44a..ad2d76d 100644 --- a/pkg/rove/rover.go +++ b/pkg/rove/rover.go @@ -5,6 +5,7 @@ import ( "log" "time" + "github.com/google/uuid" "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/maths" ) @@ -51,6 +52,19 @@ type Rover struct { Logs []RoverLogEntry `json:"logs"` } +// DefaultRover returns a default rover object with default settings +func DefaultRover() Rover { + return Rover{ + Range: 4, + Integrity: 10, + MaximumIntegrity: 10, + Capacity: 10, + Charge: 10, + MaximumCharge: 10, + Name: uuid.New().String(), + } +} + // AddLogEntryf adds an entry to the rovers log func (r *Rover) AddLogEntryf(format string, args ...interface{}) { text := fmt.Sprintf(format, args...) diff --git a/pkg/rove/world.go b/pkg/rove/world.go index 7c313ba..2e26c4c 100644 --- a/pkg/rove/world.go +++ b/pkg/rove/world.go @@ -8,7 +8,6 @@ import ( "os" "sync" - "github.com/google/uuid" "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/proto/roveapi" @@ -78,15 +77,7 @@ func (w *World) SpawnRover() (string, error) { defer w.worldMutex.Unlock() // Initialise the rover - rover := Rover{ - Range: 4, - Integrity: 10, - MaximumIntegrity: 10, - Capacity: 10, - Charge: 10, - MaximumCharge: 10, - Name: uuid.New().String(), - } + rover := DefaultRover() // Assign a random name if we have words if len(w.words) > 0 { From c48274eb23281758c5178e5a7e6eab84ba3f35a2 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:27:58 +0100 Subject: [PATCH 11/18] Small refactor in GetObject --- pkg/atlas/worldgen.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pkg/atlas/worldgen.go b/pkg/atlas/worldgen.go index 8ec8571..c2bd05d 100644 --- a/pkg/atlas/worldgen.go +++ b/pkg/atlas/worldgen.go @@ -48,23 +48,22 @@ func (g *NoiseWorldGen) GetTile(v maths.Vector) roveapi.Tile { } // GetObject returns the chosen object at a location -func (g *NoiseWorldGen) GetObject(v maths.Vector) Object { +func (g *NoiseWorldGen) GetObject(v maths.Vector) (obj Object) { o := g.noise.Eval2(float64(v.X)/rockNoiseScale, float64(v.Y)/rockNoiseScale) - var obj = roveapi.Object_ObjectUnknown switch { case o > 0.6: - obj = roveapi.Object_RockLarge + obj.Type = roveapi.Object_RockLarge case o > 0.5: - obj = roveapi.Object_RockSmall + obj.Type = roveapi.Object_RockSmall } // Very rarely spawn a dormant rover - if obj == roveapi.Object_ObjectUnknown { + if obj.Type == roveapi.Object_ObjectUnknown { o = g.noise.Eval2(float64(v.X)/dormantRoverScale, float64(v.Y)/dormantRoverScale) if o > 0.8 { - obj = roveapi.Object_RoverDormant + obj.Type = roveapi.Object_RoverDormant } } - return Object{Type: roveapi.Object(obj)} + return obj } From 9130cf2517722a55d53a362b9bb3ff0042b58c0b Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:30:07 +0100 Subject: [PATCH 12/18] Move atlas package into rove --- pkg/{atlas => rove}/atlas.go | 2 +- pkg/{atlas => rove}/atlas_test.go | 2 +- pkg/{atlas => rove}/chunkAtlas.go | 2 +- pkg/{atlas => rove}/objects.go | 2 +- pkg/rove/rover.go | 3 +-- pkg/rove/world.go | 9 ++++----- pkg/rove/world_test.go | 27 +++++++++++++-------------- pkg/{atlas => rove}/worldgen.go | 2 +- 8 files changed, 23 insertions(+), 26 deletions(-) rename pkg/{atlas => rove}/atlas.go (97%) rename pkg/{atlas => rove}/atlas_test.go (99%) rename pkg/{atlas => rove}/chunkAtlas.go (99%) rename pkg/{atlas => rove}/objects.go (98%) rename pkg/{atlas => rove}/worldgen.go (99%) diff --git a/pkg/atlas/atlas.go b/pkg/rove/atlas.go similarity index 97% rename from pkg/atlas/atlas.go rename to pkg/rove/atlas.go index 05012aa..ab09dba 100644 --- a/pkg/atlas/atlas.go +++ b/pkg/rove/atlas.go @@ -1,4 +1,4 @@ -package atlas +package rove import ( "github.com/mdiluz/rove/pkg/maths" diff --git a/pkg/atlas/atlas_test.go b/pkg/rove/atlas_test.go similarity index 99% rename from pkg/atlas/atlas_test.go rename to pkg/rove/atlas_test.go index 3d715f1..56e5d8d 100644 --- a/pkg/atlas/atlas_test.go +++ b/pkg/rove/atlas_test.go @@ -1,4 +1,4 @@ -package atlas +package rove import ( "testing" diff --git a/pkg/atlas/chunkAtlas.go b/pkg/rove/chunkAtlas.go similarity index 99% rename from pkg/atlas/chunkAtlas.go rename to pkg/rove/chunkAtlas.go index 4abb0e8..f7ea36c 100644 --- a/pkg/atlas/chunkAtlas.go +++ b/pkg/rove/chunkAtlas.go @@ -1,4 +1,4 @@ -package atlas +package rove import ( "log" diff --git a/pkg/atlas/objects.go b/pkg/rove/objects.go similarity index 98% rename from pkg/atlas/objects.go rename to pkg/rove/objects.go index cadfa4a..94595df 100644 --- a/pkg/atlas/objects.go +++ b/pkg/rove/objects.go @@ -1,4 +1,4 @@ -package atlas +package rove import ( "github.com/mdiluz/rove/proto/roveapi" diff --git a/pkg/rove/rover.go b/pkg/rove/rover.go index ad2d76d..fb898eb 100644 --- a/pkg/rove/rover.go +++ b/pkg/rove/rover.go @@ -6,7 +6,6 @@ import ( "time" "github.com/google/uuid" - "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/maths" ) @@ -31,7 +30,7 @@ type Rover struct { Range int `json:"range"` // Inventory represents any items the rover is carrying - Inventory []atlas.Object `json:"inventory"` + Inventory []Object `json:"inventory"` // Capacity is the maximum number of inventory items Capacity int `json:"capacity"` diff --git a/pkg/rove/world.go b/pkg/rove/world.go index 2e26c4c..74d56b7 100644 --- a/pkg/rove/world.go +++ b/pkg/rove/world.go @@ -8,7 +8,6 @@ import ( "os" "sync" - "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/proto/roveapi" ) @@ -25,7 +24,7 @@ type World struct { Rovers map[string]Rover `json:"rovers"` // Atlas represends the world map of chunks and tiles - Atlas atlas.Atlas `json:"atlas"` + Atlas Atlas `json:"atlas"` // Commands is the set of currently executing command streams per rover CommandQueue map[string]CommandStream `json:"commands"` @@ -64,7 +63,7 @@ func NewWorld(chunkSize int) *World { Rovers: make(map[string]Rover), CommandQueue: make(map[string]CommandStream), CommandIncoming: make(map[string]CommandStream), - Atlas: atlas.NewChunkAtlas(chunkSize), + Atlas: NewChunkAtlas(chunkSize), words: lines, TicksPerDay: 24, CurrentTicks: 0, @@ -231,7 +230,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) ([]atlas.Object, error) { +func (w *World) RoverInventory(rover string) ([]Object, error) { w.worldMutex.RLock() defer w.worldMutex.RUnlock() @@ -337,7 +336,7 @@ func (w *World) RoverStash(rover string) (roveapi.Object, error) { 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_ObjectUnknown}) + w.Atlas.SetObject(r.Pos, Object{Type: roveapi.Object_ObjectUnknown}) return obj.Type, nil } diff --git a/pkg/rove/world_test.go b/pkg/rove/world_test.go index 7cf2483..9af411c 100644 --- a/pkg/rove/world_test.go +++ b/pkg/rove/world_test.go @@ -3,7 +3,6 @@ package rove import ( "testing" - "github.com/mdiluz/rove/pkg/atlas" "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/proto/roveapi" "github.com/stretchr/testify/assert" @@ -90,7 +89,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: roveapi.Object_RockLarge}) + world.Atlas.SetObject(maths.Vector{X: 0, Y: 2}, 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") @@ -142,7 +141,7 @@ func TestWorld_RoverStash(t *testing.T) { Y: 0.0, } - world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_ObjectUnknown}) + world.Atlas.SetObject(pos, Object{Type: roveapi.Object_ObjectUnknown}) err = world.WarpRover(a, pos) assert.NoError(t, err, "Failed to set position for rover") @@ -151,7 +150,7 @@ func TestWorld_RoverStash(t *testing.T) { for i := 0; i < rover.Capacity; i++ { // Place an object - world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_RockSmall}) + world.Atlas.SetObject(pos, Object{Type: roveapi.Object_RockSmall}) // Pick it up o, err := world.RoverStash(a) @@ -166,7 +165,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: roveapi.Object_RockSmall}, inv[i]) + assert.Equal(t, Object{Type: roveapi.Object_RockSmall}, inv[i]) // Check that this did reduce the charge info, err := world.GetRover(a) @@ -183,7 +182,7 @@ func TestWorld_RoverStash(t *testing.T) { } // Place an object - world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_RockSmall}) + world.Atlas.SetObject(pos, Object{Type: roveapi.Object_RockSmall}) // Try to pick it up o, err := world.RoverStash(a) @@ -221,7 +220,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: roveapi.Object_RockLarge}) + world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, Object{Type: roveapi.Object_RockLarge}) vec, err := world.MoveRover(a, roveapi.Bearing_North) assert.NoError(t, err, "Failed to move rover") @@ -243,7 +242,7 @@ func TestWorld_RoverRepair(t *testing.T) { Y: 0.0, } - world.Atlas.SetObject(pos, atlas.Object{Type: roveapi.Object_ObjectUnknown}) + world.Atlas.SetObject(pos, Object{Type: roveapi.Object_ObjectUnknown}) err = world.WarpRover(a, pos) assert.NoError(t, err, "Failed to set position for rover") @@ -252,12 +251,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: roveapi.Object_RockSmall}) + world.Atlas.SetObject(pos, Object{Type: roveapi.Object_RockSmall}) o, err := world.RoverStash(a) assert.NoError(t, err, "Failed to stash") 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: roveapi.Object_RockLarge}) + world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, Object{Type: roveapi.Object_RockLarge}) // Try and bump into the rock vec, err := world.MoveRover(a, roveapi.Bearing_North) @@ -277,7 +276,7 @@ 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: roveapi.Object_RockSmall}) + world.Atlas.SetObject(pos, Object{Type: roveapi.Object_RockSmall}) o, err = world.RoverStash(a) assert.NoError(t, err, "Failed to stash") assert.Equal(t, roveapi.Object_RockSmall, o, "Failed to get correct object") @@ -308,7 +307,7 @@ func TestWorld_Charge(t *testing.T) { // Ensure the path ahead is empty world.Atlas.SetTile(initialPos.Added(maths.BearingToVector(roveapi.Bearing_North)), roveapi.Tile_Rock) - world.Atlas.SetObject(initialPos.Added(maths.BearingToVector(roveapi.Bearing_North)), atlas.Object{Type: roveapi.Object_ObjectUnknown}) + world.Atlas.SetObject(initialPos.Added(maths.BearingToVector(roveapi.Bearing_North)), Object{Type: roveapi.Object_ObjectUnknown}) // Try and move north (along unblocked path) newPos, err := world.MoveRover(a, roveapi.Bearing_North) @@ -390,7 +389,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_ObjectUnknown}) + world.Atlas.SetObject(maths.Vector{X: ra.Range, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown}) assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range, Y: 0})) // Broadcast from a again @@ -407,7 +406,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_ObjectUnknown}) + world.Atlas.SetObject(maths.Vector{X: ra.Range + 1, Y: 0}, 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/pkg/atlas/worldgen.go b/pkg/rove/worldgen.go similarity index 99% rename from pkg/atlas/worldgen.go rename to pkg/rove/worldgen.go index c2bd05d..2fe3bad 100644 --- a/pkg/atlas/worldgen.go +++ b/pkg/rove/worldgen.go @@ -1,4 +1,4 @@ -package atlas +package rove import ( "github.com/mdiluz/rove/pkg/maths" From 128171321185106f557ec6e9552291782fefbaa6 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:35:12 +0100 Subject: [PATCH 13/18] Clear locations before warp in tests --- pkg/rove/world_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/rove/world_test.go b/pkg/rove/world_test.go index 9af411c..f393f3d 100644 --- a/pkg/rove/world_test.go +++ b/pkg/rove/world_test.go @@ -109,7 +109,9 @@ func TestWorld_RadarFromRover(t *testing.T) { // Warp the rovers into position bpos := maths.Vector{X: -3, Y: -3} + world.Atlas.SetObject(bpos, Object{Type: roveapi.Object_ObjectUnknown}) assert.NoError(t, world.WarpRover(b, bpos), "Failed to warp rover") + world.Atlas.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown}) assert.NoError(t, world.WarpRover(a, maths.Vector{X: 0, Y: 0}), "Failed to warp rover") radar, objs, err := world.RadarFromRover(a) @@ -371,6 +373,8 @@ func TestWorld_Broadcast(t *testing.T) { assert.NoError(t, err) // Warp rovers near to eachother + world.Atlas.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown}) + world.Atlas.SetObject(maths.Vector{X: 1, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown}) assert.NoError(t, world.WarpRover(a, maths.Vector{X: 0, Y: 0})) assert.NoError(t, world.WarpRover(b, maths.Vector{X: 1, Y: 0})) From d3c480cb049a629167d048b046ea12df974f26c9 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:39:16 +0100 Subject: [PATCH 14/18] Add dormant rover data marshalled into obj data --- pkg/rove/worldgen.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/rove/worldgen.go b/pkg/rove/worldgen.go index 2fe3bad..96ac669 100644 --- a/pkg/rove/worldgen.go +++ b/pkg/rove/worldgen.go @@ -1,6 +1,9 @@ package rove import ( + "encoding/json" + "log" + "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/proto/roveapi" "github.com/ojrac/opensimplex-go" @@ -65,5 +68,21 @@ func (g *NoiseWorldGen) GetObject(v maths.Vector) (obj Object) { } } + // Post process any spawned objects + switch obj.Type { + case roveapi.Object_RoverDormant: + // Create the rover + r := DefaultRover() + + // Set the rover variables + r.Pos = v + + // Marshal the rover data into the object data + obj.Data, err := json.Marshal(r) + if err == nil { + log.Fatalf("couldn't marshal rover, should never fail: %s", err) + } + } + return obj } From 211771121fe91cc7d8a4017279676c41bad9671b Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:47:44 +0100 Subject: [PATCH 15/18] Extract rover naming to rover.go --- pkg/rove/rover.go | 38 +++++++++++++++++++++++++++++++++++++- pkg/rove/world.go | 35 ----------------------------------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/pkg/rove/rover.go b/pkg/rove/rover.go index fb898eb..6968c57 100644 --- a/pkg/rove/rover.go +++ b/pkg/rove/rover.go @@ -1,8 +1,11 @@ package rove import ( + "bufio" "fmt" "log" + "math/rand" + "os" "time" "github.com/google/uuid" @@ -60,7 +63,7 @@ func DefaultRover() Rover { Capacity: 10, Charge: 10, MaximumCharge: 10, - Name: uuid.New().String(), + Name: GenerateRoverName(), } } @@ -75,3 +78,36 @@ func (r *Rover) AddLogEntryf(format string, args ...interface{}) { }, ) } + +var wordsFile = os.Getenv("WORDS_FILE") +var roverWords []string + +// GenerateRoverName generates a new rover name +func GenerateRoverName() string { + + // Try and load the rover words file + if len(roverWords) == 0 { + // Try and load the words file + if file, err := os.Open(wordsFile); err != nil { + log.Printf("Couldn't read words file [%s], running without words: %s\n", wordsFile, err) + } else { + defer file.Close() + scanner := bufio.NewScanner(file) + for scanner.Scan() { + roverWords = append(roverWords, scanner.Text()) + } + if scanner.Err() != nil { + log.Printf("Failure during word file scan: %s\n", scanner.Err()) + } + } + } + + // Assign a random name if we have words + if len(roverWords) > 0 { + // Loop until we find a unique name + return fmt.Sprintf("%s-%s", roverWords[rand.Intn(len(roverWords))], roverWords[rand.Intn(len(roverWords))]) + } + + // Default to a unique string + return uuid.New().String() +} diff --git a/pkg/rove/world.go b/pkg/rove/world.go index 74d56b7..25aa933 100644 --- a/pkg/rove/world.go +++ b/pkg/rove/world.go @@ -1,11 +1,9 @@ package rove import ( - "bufio" "fmt" "log" "math/rand" - "os" "sync" "github.com/mdiluz/rove/pkg/maths" @@ -35,36 +33,15 @@ type World struct { worldMutex sync.RWMutex // Mutex to lock around command operations cmdMutex sync.RWMutex - // Set of possible words to use for names - words []string } -var wordsFile = os.Getenv("WORDS_FILE") - // NewWorld creates a new world object func NewWorld(chunkSize int) *World { - - // Try and load the words file - var lines []string - if file, err := os.Open(wordsFile); err != nil { - log.Printf("Couldn't read words file [%s], running without words: %s\n", wordsFile, err) - } else { - defer file.Close() - scanner := bufio.NewScanner(file) - for scanner.Scan() { - lines = append(lines, scanner.Text()) - } - if scanner.Err() != nil { - log.Printf("Failure during word file scan: %s\n", scanner.Err()) - } - } - return &World{ Rovers: make(map[string]Rover), CommandQueue: make(map[string]CommandStream), CommandIncoming: make(map[string]CommandStream), Atlas: NewChunkAtlas(chunkSize), - words: lines, TicksPerDay: 24, CurrentTicks: 0, } @@ -78,18 +55,6 @@ func (w *World) SpawnRover() (string, error) { // Initialise the rover rover := DefaultRover() - // Assign a random name if we have words - if len(w.words) > 0 { - for { - // Loop until we find a unique name - name := fmt.Sprintf("%s-%s", w.words[rand.Intn(len(w.words))], w.words[rand.Intn(len(w.words))]) - if _, ok := w.Rovers[name]; !ok { - rover.Name = name - break - } - } - } - // Spawn in a random place near the origin rover.Pos = maths.Vector{ X: 10 - rand.Intn(20), From 04d7a5a4ca7a5ea15a370764adf7847e1e09d682 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:47:54 +0100 Subject: [PATCH 16/18] Fill in the dormant rover log --- pkg/rove/worldgen.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/rove/worldgen.go b/pkg/rove/worldgen.go index 96ac669..3561768 100644 --- a/pkg/rove/worldgen.go +++ b/pkg/rove/worldgen.go @@ -77,11 +77,17 @@ func (g *NoiseWorldGen) GetObject(v maths.Vector) (obj Object) { // Set the rover variables r.Pos = v + // For now, mark the log as corrupted + r.AddLogEntryf("log corrupted") + // Marshal the rover data into the object data - obj.Data, err := json.Marshal(r) + b, err := json.Marshal(r) if err == nil { log.Fatalf("couldn't marshal rover, should never fail: %s", err) - } + } + + // Store the bytes + obj.Data = b } return obj From bffad84181974c6c2a0b9e0b7ab8e7af94ff6c31 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:57:12 +0100 Subject: [PATCH 17/18] Don't use noise for rover spawns for now --- pkg/rove/worldgen.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/rove/worldgen.go b/pkg/rove/worldgen.go index 3561768..af7660f 100644 --- a/pkg/rove/worldgen.go +++ b/pkg/rove/worldgen.go @@ -34,7 +34,6 @@ func NewNoiseWorldGen(seed int64) WorldGen { const ( terrainNoiseScale = 6 rockNoiseScale = 3 - dormantRoverScale = 25 ) // GetTile returns the chosen tile at a location @@ -62,8 +61,8 @@ func (g *NoiseWorldGen) GetObject(v maths.Vector) (obj Object) { // Very rarely spawn a dormant rover if obj.Type == roveapi.Object_ObjectUnknown { - o = g.noise.Eval2(float64(v.X)/dormantRoverScale, float64(v.Y)/dormantRoverScale) - if o > 0.8 { + // TODO: Make this better, ideally with noise + if v.X%25 == 0 && v.Y%25 == 0 && v.X != 0 && v.Y != 0 { obj.Type = roveapi.Object_RoverDormant } } From 77212c72584427262d459e96415304d850ff6218 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:57:22 +0100 Subject: [PATCH 18/18] Fix logic for rover marshal test --- pkg/rove/worldgen.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/rove/worldgen.go b/pkg/rove/worldgen.go index af7660f..e36b7b6 100644 --- a/pkg/rove/worldgen.go +++ b/pkg/rove/worldgen.go @@ -81,7 +81,7 @@ func (g *NoiseWorldGen) GetObject(v maths.Vector) (obj Object) { // Marshal the rover data into the object data b, err := json.Marshal(r) - if err == nil { + if err != nil { log.Fatalf("couldn't marshal rover, should never fail: %s", err) }