Merge branch 'main' of https://git.ranaze.com/null/promiscuity into main
All checks were successful
Deploy Promiscuity Auth API / deploy (push) Successful in 46s
Deploy Promiscuity Character API / deploy (push) Successful in 58s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 59s
k8s smoke test / test (push) Successful in 7s
All checks were successful
Deploy Promiscuity Auth API / deploy (push) Successful in 46s
Deploy Promiscuity Character API / deploy (push) Successful in 58s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 59s
k8s smoke test / test (push) Successful in 7s
This commit is contained in:
commit
82e4ae8a81
@ -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 := ""
|
||||
@ -143,6 +144,10 @@ func _spawn_tile(coord: Vector2i, location_data: Dictionary) -> void:
|
||||
var tile := _block.duplicate() as MeshInstance3D
|
||||
tile.name = "TileMesh"
|
||||
tile.visible = true
|
||||
var biome_key := String(location_data.get("biomeKey", "plains")).strip_edges()
|
||||
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())
|
||||
@ -285,6 +290,7 @@ func _ensure_selected_location_exists(coord: Vector2i) -> void:
|
||||
_known_locations[coord] = {
|
||||
"id": "",
|
||||
"name": _selected_location_name(coord),
|
||||
"biomeKey": "plains",
|
||||
"locationObject": {}
|
||||
}
|
||||
|
||||
@ -357,14 +363,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]
|
||||
var location_object := {}
|
||||
var object_variant: Variant = location.get("locationObject", {})
|
||||
if typeof(object_variant) == TYPE_DICTIONARY:
|
||||
location_object = (object_variant as Dictionary).duplicate(true)
|
||||
_known_locations[coord] = {
|
||||
"id": String(location.get("id", "")).strip_edges(),
|
||||
"name": location_name,
|
||||
"locationObject": location_object
|
||||
"biomeKey": String(location.get("biomeKey", "plains")).strip_edges(),
|
||||
"locationObject": _parse_location_object(location.get("locationObject", {}))
|
||||
}
|
||||
loaded_count += 1
|
||||
|
||||
@ -528,3 +531,63 @@ 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 _get_location_data(coord: Vector2i) -> Dictionary:
|
||||
var value: Variant = _known_locations.get(coord, {})
|
||||
if typeof(value) == TYPE_DICTIONARY:
|
||||
return value as Dictionary
|
||||
return {}
|
||||
|
||||
|
||||
func _parse_location_object(value: Variant) -> Dictionary:
|
||||
if typeof(value) != TYPE_DICTIONARY:
|
||||
return {}
|
||||
|
||||
var object_data := value as Dictionary
|
||||
var state_value: Variant = object_data.get("state", {})
|
||||
var state: Dictionary = {}
|
||||
if typeof(state_value) == TYPE_DICTIONARY:
|
||||
var raw_state := state_value as Dictionary
|
||||
state = {
|
||||
"itemKey": String(raw_state.get("itemKey", "")).strip_edges(),
|
||||
"remainingQuantity": int(raw_state.get("remainingQuantity", 0)),
|
||||
"gatherQuantity": int(raw_state.get("gatherQuantity", 1))
|
||||
}
|
||||
|
||||
return {
|
||||
"id": String(object_data.get("id", "")).strip_edges(),
|
||||
"objectType": String(object_data.get("objectType", "")).strip_edges(),
|
||||
"objectKey": String(object_data.get("objectKey", "")).strip_edges(),
|
||||
"name": String(object_data.get("name", "")).strip_edges(),
|
||||
"state": state
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
@ -17,7 +17,7 @@ public class VisibleLocation
|
||||
public LocationCoord Coord { get; set; } = new();
|
||||
|
||||
[BsonElement("biomeKey")]
|
||||
public string BiomeKey { get; set; } = string.Empty;
|
||||
public string BiomeKey { get; set; } = "plains";
|
||||
|
||||
[BsonElement("locationObject")]
|
||||
public VisibleLocationObject? LocationObject { 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,8 +9,49 @@ 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("plains", 3.0, new()
|
||||
{
|
||||
["forest"] = 1.7,
|
||||
["wetlands"] = 0.9,
|
||||
["rocky"] = 0.8,
|
||||
["desert"] = 0.4
|
||||
}),
|
||||
["forest"] = new("forest", 3.4, new()
|
||||
{
|
||||
["plains"] = 1.6,
|
||||
["wetlands"] = 1.3,
|
||||
["rocky"] = 0.5,
|
||||
["desert"] = 0.1
|
||||
}),
|
||||
["wetlands"] = new("wetlands", 3.1, new()
|
||||
{
|
||||
["forest"] = 1.5,
|
||||
["plains"] = 1.1,
|
||||
["rocky"] = 0.2,
|
||||
["desert"] = 0.05
|
||||
}),
|
||||
["rocky"] = new("rocky", 3.0, new()
|
||||
{
|
||||
["plains"] = 1.2,
|
||||
["forest"] = 0.6,
|
||||
["desert"] = 1.1,
|
||||
["wetlands"] = 0.1
|
||||
}),
|
||||
["desert"] = new("desert", 3.2, new()
|
||||
{
|
||||
["rocky"] = 1.4,
|
||||
["plains"] = 0.8,
|
||||
["forest"] = 0.1,
|
||||
["wetlands"] = 0.05
|
||||
})
|
||||
};
|
||||
|
||||
public sealed record VisibleLocationResult(List<VisibleLocation> Locations, int GeneratedCount);
|
||||
private sealed record BiomeDefinition(string Key, double ContinuationWeight, Dictionary<string, double> TransitionWeights);
|
||||
|
||||
public CharacterStore(IConfiguration cfg)
|
||||
{
|
||||
@ -42,10 +83,8 @@ public class CharacterStore
|
||||
return result.ModifiedCount > 0 || result.MatchedCount > 0;
|
||||
}
|
||||
|
||||
public Task<List<VisibleLocation>> GetVisibleLocationsAsync(Character character)
|
||||
{
|
||||
return GetVisibleLocationsInternalAsync(character, ensureGenerated: false);
|
||||
}
|
||||
public Task<List<VisibleLocation>> GetVisibleLocationsAsync(Character character) =>
|
||||
GetVisibleLocationsInternalAsync(character, ensureGenerated: false);
|
||||
|
||||
public async Task<VisibleLocationResult> GetOrCreateVisibleLocationsAsync(Character character)
|
||||
{
|
||||
@ -73,9 +112,10 @@ public class CharacterStore
|
||||
if (ensureGenerated)
|
||||
{
|
||||
foreach (var document in documents)
|
||||
await EnsureLocationObjectAsync(document);
|
||||
await BackfillLocationStateAsync(document);
|
||||
documents = await _locations.Find(filter).ToListAsync();
|
||||
}
|
||||
|
||||
return documents.Select(MapVisibleLocation).ToList();
|
||||
}
|
||||
|
||||
@ -88,101 +128,202 @@ 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("biomeKey", DetermineBiomeKey(x, y))
|
||||
.SetOnInsert("locationObject", CreateLocationObjectValueForBiome(DetermineBiomeKey(x, y), x, y))
|
||||
.SetOnInsert("locationObjectResolved", true)
|
||||
.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;
|
||||
}
|
||||
}
|
||||
|
||||
return generatedCount;
|
||||
}
|
||||
|
||||
private static string DefaultLocationName(int x, int y)
|
||||
private async Task<bool> EnsureLocationStateAsync(int x, int y)
|
||||
{
|
||||
if (x == 0 && y == 0)
|
||||
return "Origin";
|
||||
return $"Location {x},{y}";
|
||||
}
|
||||
var filter = Builders<BsonDocument>.Filter.And(
|
||||
Builders<BsonDocument>.Filter.Eq("coord.x", x),
|
||||
Builders<BsonDocument>.Filter.Eq("coord.y", y)
|
||||
);
|
||||
|
||||
private static VisibleLocation MapVisibleLocation(BsonDocument document)
|
||||
{
|
||||
var coord = document.GetValue("coord", new BsonDocument()).AsBsonDocument;
|
||||
var locationObject = MapVisibleLocationObject(document.GetValue("locationObject", BsonNull.Value));
|
||||
var idValue = document.GetValue("_id", BsonNull.Value);
|
||||
string? id = null;
|
||||
if (!idValue.IsBsonNull)
|
||||
var existing = await _locations.Find(filter).FirstOrDefaultAsync();
|
||||
if (existing is not null)
|
||||
{
|
||||
id = idValue.BsonType == BsonType.ObjectId
|
||||
? idValue.AsObjectId.ToString()
|
||||
: idValue.ToString();
|
||||
await BackfillLocationStateAsync(existing);
|
||||
return false;
|
||||
}
|
||||
|
||||
return new VisibleLocation
|
||||
{
|
||||
Id = id,
|
||||
Name = document.GetValue("name", "").AsString,
|
||||
Coord = new LocationCoord
|
||||
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 = coord.GetValue("x", 0).ToInt32(),
|
||||
Y = coord.GetValue("y", 0).ToInt32()
|
||||
},
|
||||
BiomeKey = document.GetValue("biomeKey", "").AsString,
|
||||
LocationObject = locationObject
|
||||
};
|
||||
{ "x", x },
|
||||
{ "y", y }
|
||||
})
|
||||
.SetOnInsert("biomeKey", biomeKey)
|
||||
.SetOnInsert("locationObject", CreateLocationObjectValueForBiome(biomeKey, x, y))
|
||||
.SetOnInsert("locationObjectResolved", true)
|
||||
.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 EnsureLocationObjectAsync(BsonDocument document)
|
||||
private async Task BackfillLocationStateAsync(BsonDocument document)
|
||||
{
|
||||
var hasBiome = document.TryGetValue("biomeKey", out var biomeValue) &&
|
||||
biomeValue.BsonType == BsonType.String &&
|
||||
!string.IsNullOrWhiteSpace(biomeValue.AsString);
|
||||
var resolved = document.TryGetValue("locationObjectResolved", out var resolvedValue) &&
|
||||
resolvedValue.ToBoolean();
|
||||
if (hasBiome && resolved)
|
||||
return;
|
||||
|
||||
var idValue = document.GetValue("_id", BsonNull.Value);
|
||||
if (idValue.IsBsonNull)
|
||||
return;
|
||||
|
||||
var coord = document.GetValue("coord", new BsonDocument()).AsBsonDocument;
|
||||
var x = coord.GetValue("x", 0).ToInt32();
|
||||
var y = coord.GetValue("y", 0).ToInt32();
|
||||
var biomeKey = hasBiome ? biomeValue.AsString : DetermineBiomeKey(x, y);
|
||||
var locationObject = TryMigrateLegacyResource(document) ?? CreateLocationObjectValueForBiome(biomeKey, x, y);
|
||||
var filter = Builders<BsonDocument>.Filter.And(
|
||||
Builders<BsonDocument>.Filter.Eq("_id", idValue)
|
||||
);
|
||||
var update = Builders<BsonDocument>.Update
|
||||
.Set("biomeKey", biomeKey)
|
||||
.Set("locationObjectResolved", true)
|
||||
.Set("locationObject", locationObject);
|
||||
await _locations.UpdateOneAsync(filter, update);
|
||||
|
||||
var updates = new List<UpdateDefinition<BsonDocument>>();
|
||||
var biomeKey = document.GetValue("biomeKey", BsonNull.Value).IsBsonNull
|
||||
? await DetermineBiomeKeyAsync(x, y)
|
||||
: document.GetValue("biomeKey", "plains").AsString;
|
||||
var objectResolved = document.TryGetValue("locationObjectResolved", out var resolvedValue) &&
|
||||
resolvedValue.ToBoolean();
|
||||
|
||||
if (!document.Contains("biomeKey"))
|
||||
updates.Add(Builders<BsonDocument>.Update.Set("biomeKey", biomeKey));
|
||||
if (!objectResolved)
|
||||
{
|
||||
var locationObject = TryMigrateLegacyResource(document) ?? CreateLocationObjectValueForBiome(biomeKey, x, y);
|
||||
updates.Add(Builders<BsonDocument>.Update.Set("locationObject", locationObject));
|
||||
updates.Add(Builders<BsonDocument>.Update.Set("locationObjectResolved", true));
|
||||
}
|
||||
|
||||
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 BsonValue CreateLocationObjectValueForBiome(string biomeKey, int x, int y)
|
||||
{
|
||||
var roll = StableNoise(x, y, 401);
|
||||
return biomeKey switch
|
||||
{
|
||||
"forest" => roll switch
|
||||
{
|
||||
< 0.35 => BsonNull.Value,
|
||||
< 0.80 => CreateGatherableObjectDocument("wood", 60, 3),
|
||||
< 0.95 => CreateGatherableObjectDocument("grass", 120, 10),
|
||||
_ => CreateGatherableObjectDocument("stone", 40, 2)
|
||||
},
|
||||
"rocky" => roll switch
|
||||
{
|
||||
< 0.60 => BsonNull.Value,
|
||||
< 0.90 => CreateGatherableObjectDocument("stone", 40, 2),
|
||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||
},
|
||||
"wetlands" => roll switch
|
||||
{
|
||||
< 0.40 => BsonNull.Value,
|
||||
< 0.90 => CreateGatherableObjectDocument("grass", 120, 10),
|
||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||
},
|
||||
"desert" => roll switch
|
||||
{
|
||||
< 0.70 => BsonNull.Value,
|
||||
< 0.95 => CreateGatherableObjectDocument("stone", 40, 2),
|
||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||
},
|
||||
_ => roll switch
|
||||
{
|
||||
< 0.50 => BsonNull.Value,
|
||||
< 0.85 => CreateGatherableObjectDocument("grass", 120, 10),
|
||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static BsonDocument? TryMigrateLegacyResource(BsonDocument document)
|
||||
@ -207,6 +348,51 @@ public class CharacterStore
|
||||
return null;
|
||||
}
|
||||
|
||||
private static BsonDocument CreateGatherableObjectDocument(string itemKey, int remainingQuantity, int gatherQuantity)
|
||||
{
|
||||
var normalizedItemKey = NormalizeItemKey(itemKey);
|
||||
return new BsonDocument
|
||||
{
|
||||
{ "id", Guid.NewGuid().ToString("N") },
|
||||
{ "objectType", "gatherable" },
|
||||
{ "objectKey", $"{normalizedItemKey}_node" },
|
||||
{ "name", HumanizeItemKey(normalizedItemKey) },
|
||||
{
|
||||
"state", new BsonDocument
|
||||
{
|
||||
{ "itemKey", normalizedItemKey },
|
||||
{ "remainingQuantity", remainingQuantity },
|
||||
{ "gatherQuantity", gatherQuantity }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static VisibleLocation MapVisibleLocation(BsonDocument document)
|
||||
{
|
||||
var coord = document.GetValue("coord", new BsonDocument()).AsBsonDocument;
|
||||
var locationObject = MapVisibleLocationObject(document.GetValue("locationObject", BsonNull.Value));
|
||||
var idValue = document.GetValue("_id", BsonNull.Value);
|
||||
string? id = null;
|
||||
if (!idValue.IsBsonNull)
|
||||
{
|
||||
id = idValue.BsonType == BsonType.ObjectId ? idValue.AsObjectId.ToString() : idValue.ToString();
|
||||
}
|
||||
|
||||
return new VisibleLocation
|
||||
{
|
||||
Id = id,
|
||||
Name = document.GetValue("name", "").AsString,
|
||||
Coord = new LocationCoord
|
||||
{
|
||||
X = coord.GetValue("x", 0).ToInt32(),
|
||||
Y = coord.GetValue("y", 0).ToInt32()
|
||||
},
|
||||
BiomeKey = document.GetValue("biomeKey", "plains").AsString,
|
||||
LocationObject = locationObject
|
||||
};
|
||||
}
|
||||
|
||||
private static VisibleLocationObject? MapVisibleLocationObject(BsonValue value)
|
||||
{
|
||||
if (value.IsBsonNull || value is not BsonDocument document)
|
||||
@ -228,102 +414,6 @@ public class CharacterStore
|
||||
};
|
||||
}
|
||||
|
||||
private static string DetermineBiomeKey(int x, int y)
|
||||
{
|
||||
if (x == 0 && y == 0)
|
||||
return "plains";
|
||||
|
||||
var regionX = FloorDiv(x, 4);
|
||||
var regionY = FloorDiv(y, 4);
|
||||
var roll = Math.Abs(HashCode.Combine(regionX, regionY, 7919)) % 100;
|
||||
if (roll < 35)
|
||||
return "plains";
|
||||
if (roll < 60)
|
||||
return "forest";
|
||||
if (roll < 80)
|
||||
return "rocky";
|
||||
if (roll < 92)
|
||||
return "wetlands";
|
||||
return "desert";
|
||||
}
|
||||
|
||||
private static BsonValue CreateLocationObjectValueForBiome(string biomeKey, int x, int y)
|
||||
{
|
||||
var roll = Math.Abs(HashCode.Combine(x, y, 1543)) % 100;
|
||||
return biomeKey switch
|
||||
{
|
||||
"forest" => roll switch
|
||||
{
|
||||
< 35 => BsonNull.Value,
|
||||
< 80 => CreateGatherableObjectDocument("wood", 60, 3),
|
||||
< 95 => CreateGatherableObjectDocument("grass", 120, 10),
|
||||
_ => CreateGatherableObjectDocument("stone", 40, 2)
|
||||
},
|
||||
"rocky" => roll switch
|
||||
{
|
||||
< 60 => BsonNull.Value,
|
||||
< 90 => CreateGatherableObjectDocument("stone", 40, 2),
|
||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||
},
|
||||
"wetlands" => roll switch
|
||||
{
|
||||
< 40 => BsonNull.Value,
|
||||
< 90 => CreateGatherableObjectDocument("grass", 120, 10),
|
||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||
},
|
||||
"desert" => roll switch
|
||||
{
|
||||
< 70 => BsonNull.Value,
|
||||
< 95 => CreateGatherableObjectDocument("stone", 40, 2),
|
||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||
},
|
||||
_ => roll switch
|
||||
{
|
||||
< 50 => BsonNull.Value,
|
||||
< 85 => CreateGatherableObjectDocument("grass", 120, 10),
|
||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static BsonDocument CreateGatherableObjectDocument(string itemKey, int remainingQuantity, int gatherQuantity)
|
||||
{
|
||||
var normalizedItemKey = NormalizeItemKey(itemKey);
|
||||
return new BsonDocument
|
||||
{
|
||||
{ "id", Guid.NewGuid().ToString("N") },
|
||||
{ "objectType", "gatherable" },
|
||||
{ "objectKey", $"{normalizedItemKey}_node" },
|
||||
{ "name", HumanizeItemKey(normalizedItemKey) },
|
||||
{
|
||||
"state", new BsonDocument
|
||||
{
|
||||
{ "itemKey", normalizedItemKey },
|
||||
{ "remainingQuantity", remainingQuantity },
|
||||
{ "gatherQuantity", gatherQuantity }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static string NormalizeItemKey(string itemKey) => itemKey.Trim().ToLowerInvariant();
|
||||
|
||||
private static string HumanizeItemKey(string itemKey)
|
||||
{
|
||||
return string.Join(" ", itemKey.Split('_', StringSplitOptions.RemoveEmptyEntries)
|
||||
.Where(part => part.Length > 0)
|
||||
.Select(part => char.ToUpperInvariant(part[0]) + part[1..]));
|
||||
}
|
||||
|
||||
private static int FloorDiv(int value, int divisor)
|
||||
{
|
||||
var quotient = value / divisor;
|
||||
var remainder = value % divisor;
|
||||
if (remainder != 0 && ((remainder < 0) != (divisor < 0)))
|
||||
quotient -= 1;
|
||||
return quotient;
|
||||
}
|
||||
|
||||
private void EnsureLocationCoordIndexes()
|
||||
{
|
||||
var indexes = _locations.Indexes.List().ToList();
|
||||
@ -338,11 +428,7 @@ public class CharacterStore
|
||||
_locations.Indexes.DropOne(name);
|
||||
}
|
||||
|
||||
var coordIndex = new BsonDocument
|
||||
{
|
||||
{ "coord.x", 1 },
|
||||
{ "coord.y", 1 }
|
||||
};
|
||||
var coordIndex = new BsonDocument { { "coord.x", 1 }, { "coord.y", 1 } };
|
||||
var coordIndexOptions = new CreateIndexOptions { Unique = true, Name = CoordIndexName };
|
||||
_locations.Indexes.CreateOne(new CreateIndexModel<BsonDocument>(coordIndex, coordIndexOptions));
|
||||
}
|
||||
@ -365,14 +451,42 @@ public class CharacterStore
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
if (!allowAnyOwner)
|
||||
{
|
||||
filter = Builders<Character>.Filter.And(
|
||||
filter,
|
||||
Builders<Character>.Filter.Eq(c => c.OwnerUserId, ownerUserId)
|
||||
);
|
||||
}
|
||||
filter = Builders<Character>.Filter.And(filter, Builders<Character>.Filter.Eq(c => c.OwnerUserId, ownerUserId));
|
||||
|
||||
var result = await _col.DeleteOneAsync(filter);
|
||||
return result.DeletedCount > 0;
|
||||
}
|
||||
|
||||
private static string DefaultLocationName(int x, int y)
|
||||
{
|
||||
if (x == 0 && y == 0)
|
||||
return "Origin";
|
||||
return $"Location {x},{y}";
|
||||
}
|
||||
|
||||
private static string NormalizeItemKey(string itemKey) => itemKey.Trim().ToLowerInvariant();
|
||||
|
||||
private static string HumanizeItemKey(string itemKey)
|
||||
{
|
||||
return string.Join(" ", itemKey.Split('_', StringSplitOptions.RemoveEmptyEntries)
|
||||
.Where(part => part.Length > 0)
|
||||
.Select(part => char.ToUpperInvariant(part[0]) + part[1..]));
|
||||
}
|
||||
|
||||
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 = 17;
|
||||
foreach (var ch in value)
|
||||
hash = (hash * 31) + ch;
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,6 +39,7 @@ Stored documents (MongoDB)
|
||||
"x": 0,
|
||||
"y": 0
|
||||
},
|
||||
"biomeKey": "plains",
|
||||
"resources": [
|
||||
{
|
||||
"itemKey": "wood",
|
||||
|
||||
@ -15,12 +15,12 @@ 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; } = [];
|
||||
|
||||
[BsonElement("biomeKey")]
|
||||
public string BiomeKey { get; set; } = string.Empty;
|
||||
|
||||
[BsonElement("locationObject")]
|
||||
public LocationObject? LocationObject { 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
|
||||
{
|
||||
@ -76,7 +77,6 @@ public class LocationStore
|
||||
}
|
||||
}
|
||||
},
|
||||
{ "biomeKey", new BsonDocument { { "bsonType", new BsonArray { "string", "null" } } } },
|
||||
{
|
||||
"locationObject", new BsonDocument
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user