Merge pull request 'gather' (#9) from gather into main
All checks were successful
Deploy Promiscuity Auth API / deploy (push) Successful in 45s
Deploy Promiscuity Character API / deploy (push) Successful in 1m0s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 58s
k8s smoke test / test (push) Successful in 7s
All checks were successful
Deploy Promiscuity Auth API / deploy (push) Successful in 45s
Deploy Promiscuity Character API / deploy (push) Successful in 1m0s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 58s
k8s smoke test / test (push) Successful in 7s
Reviewed-on: #9
This commit is contained in:
commit
0b15fb02d2
@ -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
|
||||
@ -23,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 := ""
|
||||
@ -31,6 +33,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 +72,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 +112,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), _get_location_biome_key(coord))
|
||||
|
||||
for key in _tile_nodes.keys():
|
||||
if wanted_keys.has(key):
|
||||
@ -115,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)
|
||||
@ -134,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())
|
||||
@ -196,7 +207,12 @@ 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),
|
||||
"biomeKey": "plains",
|
||||
"resources": []
|
||||
}
|
||||
|
||||
|
||||
func _selected_location_name(coord: Vector2i) -> String:
|
||||
@ -263,7 +279,12 @@ 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,
|
||||
"biomeKey": String(location.get("biomeKey", "plains")).strip_edges(),
|
||||
"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 +351,157 @@ 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 _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:
|
||||
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
|
||||
|
||||
|
||||
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)
|
||||
|
||||
@ -46,7 +46,15 @@ Outbound JSON documents
|
||||
"coord": {
|
||||
"x": "number",
|
||||
"y": "number"
|
||||
}
|
||||
},
|
||||
"biomeKey": "plains",
|
||||
"resources": [
|
||||
{
|
||||
"itemKey": "wood",
|
||||
"remainingQuantity": 100,
|
||||
"gatherQuantity": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
@ -15,4 +15,10 @@ public class VisibleLocation
|
||||
|
||||
[BsonElement("coord")]
|
||||
public LocationCoord Coord { get; set; } = new();
|
||||
|
||||
[BsonElement("biomeKey")]
|
||||
public string BiomeKey { get; set; } = "plains";
|
||||
|
||||
[BsonElement("resources")]
|
||||
public List<VisibleLocationResource> Resources { get; set; } = [];
|
||||
}
|
||||
|
||||
16
microservices/CharacterApi/Models/VisibleLocationResource.cs
Normal file
16
microservices/CharacterApi/Models/VisibleLocationResource.cs
Normal file
@ -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;
|
||||
}
|
||||
@ -9,6 +9,89 @@ public class CharacterStore
|
||||
private readonly IMongoCollection<Character> _col;
|
||||
private readonly IMongoCollection<BsonDocument> _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<string, BiomeDefinition> Biomes = new()
|
||||
{
|
||||
["plains"] = new BiomeDefinition(
|
||||
"plains",
|
||||
3.0,
|
||||
new Dictionary<string, double>
|
||||
{
|
||||
["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<string, double>
|
||||
{
|
||||
["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<string, double>
|
||||
{
|
||||
["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<string, double>
|
||||
{
|
||||
["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<string, double>
|
||||
{
|
||||
["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<VisibleLocation> 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<BsonDocument>.Filter.And(
|
||||
Builders<BsonDocument>.Filter.Eq("coord.x", x),
|
||||
Builders<BsonDocument>.Filter.Eq("coord.y", y)
|
||||
);
|
||||
|
||||
var update = Builders<BsonDocument>.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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,10 +200,227 @@ 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<bool> EnsureLocationStateAsync(int x, int y)
|
||||
{
|
||||
var filter = Builders<BsonDocument>.Filter.And(
|
||||
Builders<BsonDocument>.Filter.Eq("coord.x", x),
|
||||
Builders<BsonDocument>.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<BsonDocument>.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<UpdateDefinition<BsonDocument>>();
|
||||
var biomeKey = document.GetValue("biomeKey", BsonNull.Value).IsBsonNull
|
||||
? await DetermineBiomeKeyAsync(x, y)
|
||||
: document.GetValue("biomeKey", "plains").AsString;
|
||||
|
||||
if (!document.Contains("biomeKey"))
|
||||
updates.Add(Builders<BsonDocument>.Update.Set("biomeKey", biomeKey));
|
||||
if (!document.Contains("resources"))
|
||||
updates.Add(Builders<BsonDocument>.Update.Set("resources", BuildResourcesDocument(biomeKey, x, y)));
|
||||
|
||||
if (updates.Count == 0)
|
||||
return;
|
||||
|
||||
var id = document.GetValue("_id").AsObjectId;
|
||||
await _locations.UpdateOneAsync(
|
||||
Builders<BsonDocument>.Filter.Eq("_id", id),
|
||||
Builders<BsonDocument>.Update.Combine(updates));
|
||||
}
|
||||
|
||||
private async Task<string> 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<List<string>> 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<BsonDocument>.Filter.And(
|
||||
Builders<BsonDocument>.Filter.Eq("coord.x", coord.Item1),
|
||||
Builders<BsonDocument>.Filter.Eq("coord.y", coord.Item2)))
|
||||
.ToList();
|
||||
var filter = Builders<BsonDocument>.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<VisibleLocationResource> MapVisibleLocationResources(BsonDocument document)
|
||||
{
|
||||
if (!document.TryGetValue("resources", out var resourcesValue) || resourcesValue.BsonType != BsonType.Array)
|
||||
return [];
|
||||
|
||||
var results = new List<VisibleLocationResource>();
|
||||
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();
|
||||
@ -192,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<string, double> TransitionWeights,
|
||||
IReadOnlyList<ResourceRule> ResourceRules);
|
||||
}
|
||||
|
||||
@ -39,6 +39,7 @@ Stored documents (MongoDB)
|
||||
"x": 0,
|
||||
"y": 0
|
||||
},
|
||||
"biomeKey": "plains",
|
||||
"resources": [
|
||||
{
|
||||
"itemKey": "wood",
|
||||
|
||||
@ -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<LocationResource> Resources { get; set; } = [];
|
||||
|
||||
|
||||
@ -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<Location>.Filter.Eq(l => l.Coord.X, 0),
|
||||
Builders<Location>.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<UpdateDefinition<Location>>();
|
||||
if (string.IsNullOrWhiteSpace(existing.BiomeKey))
|
||||
updates.Add(Builders<Location>.Update.Set(l => l.BiomeKey, "plains"));
|
||||
if (existing.Resources.Count == 0)
|
||||
updates.Add(Builders<Location>.Update.Set(l => l.Resources, new List<LocationResource>
|
||||
{
|
||||
new() { ItemKey = "wood", RemainingQuantity = 100, GatherQuantity = 3 },
|
||||
new() { ItemKey = "grass", RemainingQuantity = 500, GatherQuantity = 10 }
|
||||
}));
|
||||
if (updates.Count > 0)
|
||||
_col.UpdateOne(filter, Builders<Location>.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 },
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user