Biome generation

This commit is contained in:
2026-03-19 13:45:33 -05:00
parent fedb426cfc
commit 6d97e5324c
8 changed files with 376 additions and 36 deletions
@@ -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;
}
}
@@ -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<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)
@@ -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<string, double> TransitionWeights,
IReadOnlyList<ResourceRule> ResourceRules);
}