From 50d44e83876cde1d1cb2aae26c3368c43c5e0e7e Mon Sep 17 00:00:00 2001 From: Zeeshaun Date: Thu, 19 Mar 2026 12:38:45 -0500 Subject: [PATCH 1/2] Allowing player to gather resources --- game/scenes/Levels/location_level.gd | 141 +++++++++++++++++- microservices/CharacterApi/DOCUMENTS.md | 9 +- .../CharacterApi/Models/VisibleLocation.cs | 3 + .../CharacterApi/Services/CharacterStore.cs | 26 +++- 4 files changed, 174 insertions(+), 5 deletions(-) diff --git a/game/scenes/Levels/location_level.gd b/game/scenes/Levels/location_level.gd index 48d01b2..05ef64d 100644 --- a/game/scenes/Levels/location_level.gd +++ b/game/scenes/Levels/location_level.gd @@ -1,6 +1,7 @@ extends Node3D const CHARACTER_API_URL := "https://pchar.ranaze.com/api/Characters" +const LOCATION_API_URL := "https://ploc.ranaze.com/api/Locations" @export var tile_size := 16.0 @export var block_height := 1.0 @@ -31,6 +32,7 @@ var _coord_sync_in_flight := false var _queued_coord_sync: Variant = null var _locations_refresh_in_flight := false var _queued_locations_refresh := false +var _gather_in_flight := false func _ready() -> void: @@ -69,6 +71,11 @@ func _process(_delta: float) -> void: _queue_locations_refresh() +func _unhandled_input(event: InputEvent) -> void: + if event.is_action_pressed("interact"): + _try_gather_current_tile() + + func _get_stream_position() -> Vector3: if _tracked_node: return _tracked_node.global_position @@ -104,7 +111,7 @@ func _rebuild_tiles(center: Vector2i) -> void: wanted_keys[coord] = true if _tile_nodes.has(coord): continue - _spawn_tile(coord, String(_known_locations[coord])) + _spawn_tile(coord, _get_location_name(coord)) for key in _tile_nodes.keys(): if wanted_keys.has(key): @@ -196,7 +203,11 @@ func _create_tile_label(location_name: String) -> Label3D: func _ensure_selected_location_exists(coord: Vector2i) -> void: if _known_locations.has(coord): return - _known_locations[coord] = _selected_location_name(coord) + _known_locations[coord] = { + "id": "", + "name": _selected_location_name(coord), + "resources": [] + } func _selected_location_name(coord: Vector2i) -> String: @@ -263,7 +274,11 @@ func _load_existing_locations() -> void: var location_name := String(location.get("name", "")).strip_edges() if location_name.is_empty(): location_name = "Location %d,%d" % [coord.x, coord.y] - _known_locations[coord] = location_name + _known_locations[coord] = { + "id": String(location.get("id", "")).strip_edges(), + "name": location_name, + "resources": _parse_location_resources(location.get("resources", [])) + } loaded_count += 1 print("LocationLevel loaded %d visible locations for character %s." % [loaded_count, _character_id]) @@ -330,3 +345,123 @@ func _sync_character_coord_async(coord: Vector2i) -> void: if _queued_coord_sync != null and _queued_coord_sync is Vector2i and _queued_coord_sync != _persisted_coord: var queued_coord: Vector2i = _queued_coord_sync _sync_character_coord(queued_coord) + + +func _try_gather_current_tile() -> void: + if _gather_in_flight: + return + var location_data := _get_location_data(_center_coord) + if location_data.is_empty(): + push_warning("No location data available for current tile.") + return + var location_id := String(location_data.get("id", "")).strip_edges() + if location_id.is_empty(): + push_warning("Current tile has no location id; cannot gather.") + return + var resources: Array = location_data.get("resources", []) + if resources.is_empty(): + push_warning("No gatherable resources remain at this location.") + return + var resource: Dictionary = resources[0] + var resource_key := String(resource.get("itemKey", "")).strip_edges() + if resource_key.is_empty(): + push_warning("Current location resource is missing an itemKey.") + return + _gather_current_tile(location_id, resource_key) + + +func _gather_current_tile(location_id: String, resource_key: String) -> void: + _gather_in_flight = true + _gather_current_tile_async(location_id, resource_key) + + +func _gather_current_tile_async(location_id: String, resource_key: String) -> void: + var request := HTTPRequest.new() + add_child(request) + + var headers := PackedStringArray() + if not AuthState.access_token.is_empty(): + headers.append("Authorization: Bearer %s" % AuthState.access_token) + headers.append("Content-Type: application/json") + + var body := JSON.stringify({ + "characterId": _character_id, + "resourceKey": resource_key + }) + + var err := request.request("%s/%s/gather" % [LOCATION_API_URL, location_id], headers, HTTPClient.METHOD_POST, body) + if err != OK: + push_warning("Failed to request gather action: %s" % err) + request.queue_free() + _gather_in_flight = false + return + + var result: Array = await request.request_completed + request.queue_free() + + var result_code: int = result[0] + var response_code: int = result[1] + var response_body: String = result[3].get_string_from_utf8() + if result_code != HTTPRequest.RESULT_SUCCESS or response_code < 200 or response_code >= 300: + push_warning("Failed to gather resource (%s/%s): %s" % [result_code, response_code, response_body]) + _gather_in_flight = false + return + + var parsed: Variant = JSON.parse_string(response_body) + if typeof(parsed) != TYPE_DICTIONARY: + push_warning("Gather response was not a dictionary.") + _gather_in_flight = false + return + + var gather_response := parsed as Dictionary + var remaining_quantity := int(gather_response.get("remainingQuantity", 0)) + var quantity_granted := int(gather_response.get("quantityGranted", 0)) + _update_location_resource(_center_coord, resource_key, remaining_quantity) + print("Gathered %s x%s at %s." % [resource_key, quantity_granted, _center_coord]) + _gather_in_flight = false + + +func _get_location_data(coord: Vector2i) -> Dictionary: + var value: Variant = _known_locations.get(coord, {}) + if typeof(value) == TYPE_DICTIONARY: + return value as Dictionary + return {} + + +func _get_location_name(coord: Vector2i) -> String: + var location_data := _get_location_data(coord) + return String(location_data.get("name", "Location %d,%d" % [coord.x, coord.y])) + + +func _parse_location_resources(resources_value: Variant) -> Array: + var results: Array = [] + if typeof(resources_value) != TYPE_ARRAY: + return results + for entry in resources_value: + if typeof(entry) != TYPE_DICTIONARY: + continue + var resource := entry as Dictionary + results.append({ + "itemKey": String(resource.get("itemKey", "")).strip_edges(), + "remainingQuantity": int(resource.get("remainingQuantity", 0)), + "gatherQuantity": int(resource.get("gatherQuantity", 1)) + }) + return results + + +func _update_location_resource(coord: Vector2i, resource_key: String, remaining_quantity: int) -> void: + var location_data := _get_location_data(coord) + if location_data.is_empty(): + return + var resources: Array = location_data.get("resources", []) + var updated_resources: Array = [] + for entry in resources: + if typeof(entry) != TYPE_DICTIONARY: + continue + var resource := (entry as Dictionary).duplicate() + if String(resource.get("itemKey", "")).strip_edges() == resource_key: + resource["remainingQuantity"] = remaining_quantity + if int(resource.get("remainingQuantity", 0)) > 0: + updated_resources.append(resource) + location_data["resources"] = updated_resources + _known_locations[coord] = location_data diff --git a/microservices/CharacterApi/DOCUMENTS.md b/microservices/CharacterApi/DOCUMENTS.md index cf4733e..1c314b6 100644 --- a/microservices/CharacterApi/DOCUMENTS.md +++ b/microservices/CharacterApi/DOCUMENTS.md @@ -46,7 +46,14 @@ Outbound JSON documents "coord": { "x": "number", "y": "number" - } + }, + "resources": [ + { + "itemKey": "wood", + "remainingQuantity": 100, + "gatherQuantity": 3 + } + ] } ] ``` diff --git a/microservices/CharacterApi/Models/VisibleLocation.cs b/microservices/CharacterApi/Models/VisibleLocation.cs index 5aff51d..c06e6b9 100644 --- a/microservices/CharacterApi/Models/VisibleLocation.cs +++ b/microservices/CharacterApi/Models/VisibleLocation.cs @@ -15,4 +15,7 @@ public class VisibleLocation [BsonElement("coord")] public LocationCoord Coord { get; set; } = new(); + + [BsonElement("resources")] + public List Resources { get; set; } = []; } diff --git a/microservices/CharacterApi/Services/CharacterStore.cs b/microservices/CharacterApi/Services/CharacterStore.cs index 4705e03..0380d67 100644 --- a/microservices/CharacterApi/Services/CharacterStore.cs +++ b/microservices/CharacterApi/Services/CharacterStore.cs @@ -140,10 +140,34 @@ public class CharacterStore { X = coord.GetValue("x", 0).ToInt32(), Y = coord.GetValue("y", 0).ToInt32() - } + }, + Resources = MapVisibleLocationResources(document) }; } + private static List MapVisibleLocationResources(BsonDocument document) + { + if (!document.TryGetValue("resources", out var resourcesValue) || resourcesValue.BsonType != BsonType.Array) + return []; + + var results = new List(); + foreach (var value in resourcesValue.AsBsonArray) + { + if (value.BsonType != BsonType.Document) + continue; + + var resource = value.AsBsonDocument; + results.Add(new VisibleLocationResource + { + ItemKey = resource.GetValue("itemKey", "").AsString, + RemainingQuantity = resource.GetValue("remainingQuantity", 0).ToInt32(), + GatherQuantity = resource.GetValue("gatherQuantity", 1).ToInt32() + }); + } + + return results; + } + private void EnsureLocationCoordIndexes() { var indexes = _locations.Indexes.List().ToList(); From 6d97e5324c7639158d36e536e029a7ea240b414f Mon Sep 17 00:00:00 2001 From: Zeeshaun Date: Thu, 19 Mar 2026 13:45:33 -0500 Subject: [PATCH 2/2] Biome generation --- game/scenes/Levels/location_level.gd | 44 ++- microservices/CharacterApi/DOCUMENTS.md | 1 + .../CharacterApi/Models/VisibleLocation.cs | 3 + .../Models/VisibleLocationResource.cs | 16 + .../CharacterApi/Services/CharacterStore.cs | 319 ++++++++++++++++-- microservices/LocationsApi/DOCUMENTS.md | 1 + microservices/LocationsApi/Models/Location.cs | 3 + .../LocationsApi/Services/LocationStore.cs | 25 +- 8 files changed, 376 insertions(+), 36 deletions(-) create mode 100644 microservices/CharacterApi/Models/VisibleLocationResource.cs 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 },