diff --git a/game/scenes/Levels/location_level.gd b/game/scenes/Levels/location_level.gd index 05ef64d..679212a 100644 --- a/game/scenes/Levels/location_level.gd +++ b/game/scenes/Levels/location_level.gd @@ -24,6 +24,7 @@ var _tracked_node: Node3D var _tile_nodes: Dictionary = {} var _camera_start_offset := Vector3(0.0, 6.0, 10.0) var _border_material: StandardMaterial3D +var _biome_materials: Dictionary = {} var _known_locations: Dictionary = {} var _locations_loaded := false var _character_id := "" @@ -111,7 +112,7 @@ func _rebuild_tiles(center: Vector2i) -> void: wanted_keys[coord] = true if _tile_nodes.has(coord): continue - _spawn_tile(coord, _get_location_name(coord)) + _spawn_tile(coord, _get_location_name(coord), _get_location_biome_key(coord)) for key in _tile_nodes.keys(): if wanted_keys.has(key): @@ -122,7 +123,7 @@ func _rebuild_tiles(center: Vector2i) -> void: _tile_nodes.erase(key) -func _spawn_tile(coord: Vector2i, location_name: String) -> void: +func _spawn_tile(coord: Vector2i, location_name: String, biome_key: String) -> void: var tile_root := Node3D.new() tile_root.name = "Tile_%d_%d" % [coord.x, coord.y] tile_root.position = _coord_to_world(coord) @@ -141,6 +142,9 @@ func _spawn_tile(coord: Vector2i, location_name: String) -> void: var tile := _block.duplicate() as MeshInstance3D tile.name = "TileMesh" tile.visible = true + var biome_material := _get_biome_material(tile, biome_key) + if biome_material: + tile.material_override = biome_material tile_body.add_child(tile) tile.add_child(_create_tile_border()) @@ -206,6 +210,7 @@ func _ensure_selected_location_exists(coord: Vector2i) -> void: _known_locations[coord] = { "id": "", "name": _selected_location_name(coord), + "biomeKey": "plains", "resources": [] } @@ -277,6 +282,7 @@ func _load_existing_locations() -> void: _known_locations[coord] = { "id": String(location.get("id", "")).strip_edges(), "name": location_name, + "biomeKey": String(location.get("biomeKey", "plains")).strip_edges(), "resources": _parse_location_resources(location.get("resources", [])) } loaded_count += 1 @@ -433,6 +439,11 @@ func _get_location_name(coord: Vector2i) -> String: return String(location_data.get("name", "Location %d,%d" % [coord.x, coord.y])) +func _get_location_biome_key(coord: Vector2i) -> String: + var location_data := _get_location_data(coord) + return String(location_data.get("biomeKey", "plains")).strip_edges() + + func _parse_location_resources(resources_value: Variant) -> Array: var results: Array = [] if typeof(resources_value) != TYPE_ARRAY: @@ -465,3 +476,32 @@ func _update_location_resource(coord: Vector2i, resource_key: String, remaining_ updated_resources.append(resource) location_data["resources"] = updated_resources _known_locations[coord] = location_data + + +func _get_biome_material(tile: MeshInstance3D, biome_key: String) -> Material: + var normalized_biome := biome_key if not biome_key.is_empty() else "plains" + if _biome_materials.has(normalized_biome): + return _biome_materials[normalized_biome] + + var source_material := tile.get_active_material(0) + if source_material is StandardMaterial3D: + var material := (source_material as StandardMaterial3D).duplicate() as StandardMaterial3D + material.albedo_color = _get_biome_color(normalized_biome) + _biome_materials[normalized_biome] = material + return material + + return source_material + + +func _get_biome_color(biome_key: String) -> Color: + match biome_key: + "forest": + return Color(0.36, 0.62, 0.34, 1.0) + "wetlands": + return Color(0.28, 0.52, 0.44, 1.0) + "rocky": + return Color(0.52, 0.50, 0.44, 1.0) + "desert": + return Color(0.76, 0.67, 0.38, 1.0) + _: + return Color(0.56, 0.72, 0.38, 1.0) diff --git a/microservices/CharacterApi/DOCUMENTS.md b/microservices/CharacterApi/DOCUMENTS.md index 1c314b6..7e4d5e1 100644 --- a/microservices/CharacterApi/DOCUMENTS.md +++ b/microservices/CharacterApi/DOCUMENTS.md @@ -47,6 +47,7 @@ Outbound JSON documents "x": "number", "y": "number" }, + "biomeKey": "plains", "resources": [ { "itemKey": "wood", diff --git a/microservices/CharacterApi/Models/VisibleLocation.cs b/microservices/CharacterApi/Models/VisibleLocation.cs index c06e6b9..11d4227 100644 --- a/microservices/CharacterApi/Models/VisibleLocation.cs +++ b/microservices/CharacterApi/Models/VisibleLocation.cs @@ -16,6 +16,9 @@ public class VisibleLocation [BsonElement("coord")] public LocationCoord Coord { get; set; } = new(); + [BsonElement("biomeKey")] + public string BiomeKey { get; set; } = "plains"; + [BsonElement("resources")] public List Resources { get; set; } = []; } diff --git a/microservices/CharacterApi/Models/VisibleLocationResource.cs b/microservices/CharacterApi/Models/VisibleLocationResource.cs new file mode 100644 index 0000000..344836e --- /dev/null +++ b/microservices/CharacterApi/Models/VisibleLocationResource.cs @@ -0,0 +1,16 @@ +using MongoDB.Bson.Serialization.Attributes; + +namespace CharacterApi.Models; + +[BsonIgnoreExtraElements] +public class VisibleLocationResource +{ + [BsonElement("itemKey")] + public string ItemKey { get; set; } = string.Empty; + + [BsonElement("remainingQuantity")] + public int RemainingQuantity { get; set; } + + [BsonElement("gatherQuantity")] + public int GatherQuantity { get; set; } = 1; +} diff --git a/microservices/CharacterApi/Services/CharacterStore.cs b/microservices/CharacterApi/Services/CharacterStore.cs index 0380d67..9bd8a05 100644 --- a/microservices/CharacterApi/Services/CharacterStore.cs +++ b/microservices/CharacterApi/Services/CharacterStore.cs @@ -9,6 +9,89 @@ public class CharacterStore private readonly IMongoCollection _col; private readonly IMongoCollection _locations; private const string CoordIndexName = "coord_x_1_coord_y_1"; + private const int WorldSeed = 1729; + private static readonly string[] BiomeOrder = ["plains", "forest", "wetlands", "rocky", "desert"]; + private static readonly Dictionary Biomes = new() + { + ["plains"] = new BiomeDefinition( + "plains", + 3.0, + new Dictionary + { + ["forest"] = 1.7, + ["wetlands"] = 0.9, + ["rocky"] = 0.8, + ["desert"] = 0.4 + }, + new[] + { + new ResourceRule("grass", 180, 420, 12), + new ResourceRule("wood", 25, 90, 3), + new ResourceRule("stone", 10, 40, 2) + }), + ["forest"] = new BiomeDefinition( + "forest", + 3.4, + new Dictionary + { + ["plains"] = 1.6, + ["wetlands"] = 1.3, + ["rocky"] = 0.5, + ["desert"] = 0.1 + }, + new[] + { + new ResourceRule("wood", 220, 520, 6), + new ResourceRule("grass", 90, 220, 8), + new ResourceRule("stone", 15, 45, 2) + }), + ["wetlands"] = new BiomeDefinition( + "wetlands", + 3.1, + new Dictionary + { + ["forest"] = 1.5, + ["plains"] = 1.1, + ["rocky"] = 0.2, + ["desert"] = 0.05 + }, + new[] + { + new ResourceRule("grass", 260, 600, 15), + new ResourceRule("wood", 40, 120, 4) + }), + ["rocky"] = new BiomeDefinition( + "rocky", + 3.0, + new Dictionary + { + ["plains"] = 1.2, + ["forest"] = 0.6, + ["desert"] = 1.1, + ["wetlands"] = 0.1 + }, + new[] + { + new ResourceRule("stone", 220, 540, 8), + new ResourceRule("wood", 10, 40, 2), + new ResourceRule("grass", 20, 70, 4) + }), + ["desert"] = new BiomeDefinition( + "desert", + 3.2, + new Dictionary + { + ["rocky"] = 1.4, + ["plains"] = 0.8, + ["forest"] = 0.1, + ["wetlands"] = 0.05 + }, + new[] + { + new ResourceRule("stone", 80, 220, 5), + new ResourceRule("grass", 5, 25, 2) + }) + }; public sealed record VisibleLocationResult(List Locations, int GeneratedCount); @@ -82,31 +165,8 @@ public class CharacterStore { for (var y = character.Coord.Y - radius; y <= character.Coord.Y + radius; y++) { - var filter = Builders.Filter.And( - Builders.Filter.Eq("coord.x", x), - Builders.Filter.Eq("coord.y", y) - ); - - var update = Builders.Update - .SetOnInsert("_id", ObjectId.GenerateNewId()) - .SetOnInsert("name", DefaultLocationName(x, y)) - .SetOnInsert("coord", new BsonDocument - { - { "x", x }, - { "y", y } - }) - .SetOnInsert("createdUtc", DateTime.UtcNow); - - try - { - var result = await _locations.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true }); - if (result.UpsertedId is not null) - generatedCount += 1; - } - catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) - { - // Another request or service instance created it first. - } + if (await EnsureLocationStateAsync(x, y)) + generatedCount += 1; } } @@ -141,10 +201,203 @@ public class CharacterStore X = coord.GetValue("x", 0).ToInt32(), Y = coord.GetValue("y", 0).ToInt32() }, + BiomeKey = document.GetValue("biomeKey", "plains").AsString, Resources = MapVisibleLocationResources(document) }; } + private async Task EnsureLocationStateAsync(int x, int y) + { + var filter = Builders.Filter.And( + Builders.Filter.Eq("coord.x", x), + Builders.Filter.Eq("coord.y", y) + ); + + var existing = await _locations.Find(filter).FirstOrDefaultAsync(); + if (existing is not null) + { + await BackfillLocationStateAsync(existing); + return false; + } + + var biomeKey = await DetermineBiomeKeyAsync(x, y); + var update = Builders.Update + .SetOnInsert("_id", ObjectId.GenerateNewId()) + .SetOnInsert("name", DefaultLocationName(x, y)) + .SetOnInsert("coord", new BsonDocument + { + { "x", x }, + { "y", y } + }) + .SetOnInsert("biomeKey", biomeKey) + .SetOnInsert("resources", BuildResourcesDocument(biomeKey, x, y)) + .SetOnInsert("createdUtc", DateTime.UtcNow); + + try + { + var result = await _locations.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true }); + return result.UpsertedId is not null; + } + catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) + { + return false; + } + } + + private async Task BackfillLocationStateAsync(BsonDocument document) + { + var coord = document.GetValue("coord", new BsonDocument()).AsBsonDocument; + var x = coord.GetValue("x", 0).ToInt32(); + var y = coord.GetValue("y", 0).ToInt32(); + + var updates = new List>(); + var biomeKey = document.GetValue("biomeKey", BsonNull.Value).IsBsonNull + ? await DetermineBiomeKeyAsync(x, y) + : document.GetValue("biomeKey", "plains").AsString; + + if (!document.Contains("biomeKey")) + updates.Add(Builders.Update.Set("biomeKey", biomeKey)); + if (!document.Contains("resources")) + updates.Add(Builders.Update.Set("resources", BuildResourcesDocument(biomeKey, x, y))); + + if (updates.Count == 0) + return; + + var id = document.GetValue("_id").AsObjectId; + await _locations.UpdateOneAsync( + Builders.Filter.Eq("_id", id), + Builders.Update.Combine(updates)); + } + + private async Task DetermineBiomeKeyAsync(int x, int y) + { + if (x == 0 && y == 0) + return "plains"; + + var neighbors = await LoadNeighborBiomeKeysAsync(x, y); + var baseBiome = DetermineBaseBiomeKey(x, y); + if (neighbors.Count == 0) + return baseBiome; + + var dominantNeighbor = neighbors + .GroupBy(key => key) + .OrderByDescending(group => group.Count()) + .ThenBy(group => group.Key) + .First().Key; + + var bestBiome = baseBiome; + var bestScore = double.NegativeInfinity; + foreach (var candidate in BiomeOrder) + { + var score = candidate == baseBiome ? 2.5 : 0.35; + if (candidate == dominantNeighbor) + score += 1.8; + + foreach (var neighbor in neighbors) + { + if (!Biomes.TryGetValue(neighbor, out var neighborDefinition)) + continue; + + if (candidate == neighbor) + score += neighborDefinition.ContinuationWeight; + else if (neighborDefinition.TransitionWeights.TryGetValue(candidate, out var transitionWeight)) + score += transitionWeight; + } + + score += StableNoise(x, y, StableHash(candidate)) * 0.25; + if (score > bestScore) + { + bestScore = score; + bestBiome = candidate; + } + } + + return bestBiome; + } + + private async Task> LoadNeighborBiomeKeysAsync(int x, int y) + { + var coords = new[] + { + (x - 1, y), + (x + 1, y), + (x, y - 1), + (x, y + 1) + }; + + var filters = coords.Select(coord => + Builders.Filter.And( + Builders.Filter.Eq("coord.x", coord.Item1), + Builders.Filter.Eq("coord.y", coord.Item2))) + .ToList(); + var filter = Builders.Filter.Or(filters); + var neighbors = await _locations.Find(filter).ToListAsync(); + + return neighbors + .Where(doc => doc.Contains("biomeKey")) + .Select(doc => doc.GetValue("biomeKey", "plains").AsString) + .Where(key => !string.IsNullOrWhiteSpace(key)) + .ToList(); + } + + private static string DetermineBaseBiomeKey(int x, int y) + { + var temperature = StableNoise(x, y, 101); + var moisture = StableNoise(x, y, 202); + var ruggedness = StableNoise(x, y, 303); + + if (ruggedness > 0.74) + return "rocky"; + if (moisture > 0.72 && temperature < 0.75) + return "wetlands"; + if (moisture > 0.56) + return "forest"; + if (moisture < 0.22 && temperature > 0.58) + return "desert"; + return "plains"; + } + + private static BsonArray BuildResourcesDocument(string biomeKey, int x, int y) + { + if (!Biomes.TryGetValue(biomeKey, out var biome)) + biome = Biomes["plains"]; + + var resources = new BsonArray(); + foreach (var rule in biome.ResourceRules) + { + var roll = StableNoise(x, y, StableHash(rule.ItemKey)); + var quantity = rule.MinQuantity + (int)Math.Round(roll * (rule.MaxQuantity - rule.MinQuantity)); + if (quantity <= 0) + continue; + + resources.Add(new BsonDocument + { + { "itemKey", rule.ItemKey }, + { "remainingQuantity", quantity }, + { "gatherQuantity", rule.GatherQuantity } + }); + } + + return resources; + } + + private static double StableNoise(int x, int y, int salt) + { + var value = Math.Sin((x * 12.9898) + (y * 78.233) + ((WorldSeed + salt) * 0.1597)) * 43758.5453; + return value - Math.Floor(value); + } + + private static int StableHash(string value) + { + unchecked + { + var hash = 23; + foreach (var c in value) + hash = (hash * 31) + c; + return hash; + } + } + private static List MapVisibleLocationResources(BsonDocument document) { if (!document.TryGetValue("resources", out var resourcesValue) || resourcesValue.BsonType != BsonType.Array) @@ -216,7 +469,15 @@ public class CharacterStore ); } - var result = await _col.DeleteOneAsync(filter); - return result.DeletedCount > 0; - } -} + var result = await _col.DeleteOneAsync(filter); + return result.DeletedCount > 0; + } + + private sealed record ResourceRule(string ItemKey, int MinQuantity, int MaxQuantity, int GatherQuantity); + + private sealed record BiomeDefinition( + string Key, + double ContinuationWeight, + Dictionary TransitionWeights, + IReadOnlyList ResourceRules); +} diff --git a/microservices/LocationsApi/DOCUMENTS.md b/microservices/LocationsApi/DOCUMENTS.md index 9b85cb9..5f08a25 100644 --- a/microservices/LocationsApi/DOCUMENTS.md +++ b/microservices/LocationsApi/DOCUMENTS.md @@ -39,6 +39,7 @@ Stored documents (MongoDB) "x": 0, "y": 0 }, + "biomeKey": "plains", "resources": [ { "itemKey": "wood", diff --git a/microservices/LocationsApi/Models/Location.cs b/microservices/LocationsApi/Models/Location.cs index f477932..fd8b440 100644 --- a/microservices/LocationsApi/Models/Location.cs +++ b/microservices/LocationsApi/Models/Location.cs @@ -15,6 +15,9 @@ public class Location [BsonElement("coord")] public required Coord Coord { get; set; } + [BsonElement("biomeKey")] + public string BiomeKey { get; set; } = "plains"; + [BsonElement("resources")] public List Resources { get; set; } = []; diff --git a/microservices/LocationsApi/Services/LocationStore.cs b/microservices/LocationsApi/Services/LocationStore.cs index 570dbda..92c70b8 100644 --- a/microservices/LocationsApi/Services/LocationStore.cs +++ b/microservices/LocationsApi/Services/LocationStore.cs @@ -36,7 +36,7 @@ public class LocationStore "$jsonSchema", new BsonDocument { { "bsonType", "object" }, - { "required", new BsonArray { "name", "coord", "createdUtc" } }, + { "required", new BsonArray { "name", "coord", "biomeKey", "createdUtc" } }, { "properties", new BsonDocument { @@ -55,6 +55,7 @@ public class LocationStore } } }, + { "biomeKey", new BsonDocument { { "bsonType", "string" } } }, { "resources", new BsonDocument { @@ -219,14 +220,28 @@ public class LocationStore Builders.Filter.Eq(l => l.Coord.X, 0), Builders.Filter.Eq(l => l.Coord.Y, 0) ); - var existing = _col.Find(filter).FirstOrDefault(); - if (existing is not null) - return; - + var existing = _col.Find(filter).FirstOrDefault(); + if (existing is not null) + { + var updates = new List>(); + if (string.IsNullOrWhiteSpace(existing.BiomeKey)) + updates.Add(Builders.Update.Set(l => l.BiomeKey, "plains")); + if (existing.Resources.Count == 0) + updates.Add(Builders.Update.Set(l => l.Resources, new List + { + new() { ItemKey = "wood", RemainingQuantity = 100, GatherQuantity = 3 }, + new() { ItemKey = "grass", RemainingQuantity = 500, GatherQuantity = 10 } + })); + if (updates.Count > 0) + _col.UpdateOne(filter, Builders.Update.Combine(updates)); + return; + } + var origin = new Location { Name = "Origin", Coord = new Coord { X = 0, Y = 0 }, + BiomeKey = "plains", Resources = [ new LocationResource { ItemKey = "wood", RemainingQuantity = 100, GatherQuantity = 3 },