Skip to content

Commit

Permalink
Merge pull request #89
Browse files Browse the repository at this point in the history
salvage_revert_again
  • Loading branch information
Cheackraze committed Jul 26, 2023
2 parents 2636c1c + 775460a commit 91610de
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 14 deletions.
27 changes: 13 additions & 14 deletions Content.Server/Parallax/BiomeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private void LoadChunks(
var startNodeY = rand.Next(lower, upper + 1);
var startNode = new Vector2i(startNodeX, startNodeY);
frontier.Clear();
frontier.Add(startNode);
frontier.Add(startNode + chunk);
while (groupCount > 0 && frontier.Count > 0)
{
Expand All @@ -434,40 +434,39 @@ private void LoadChunks(
continue;
var neighbor = new Vector2i(x + node.X, y + node.Y);
var chunkOffset = neighbor - chunk;
// Check if it's inbounds.
if (neighbor.X < lower ||
neighbor.Y < lower ||
neighbor.X > upper ||
neighbor.Y > upper)
if (chunkOffset.X < lower ||
chunkOffset.Y < lower ||
chunkOffset.X > upper ||
chunkOffset.Y > upper)
{
continue;
}
if (!spawnSet.Add(neighbor))
continue;
frontier.Add(neighbor);
}
}
var actualNode = node + chunk;
if (!spawnSet.Add(actualNode))
continue;
// Check if it's a valid spawn, if so then use it.
var enumerator = grid.GetAnchoredEntitiesEnumerator(actualNode);
var enumerator = grid.GetAnchoredEntitiesEnumerator(node);
if (enumerator.MoveNext(out _))
continue;
// Check if mask matches.
TryGetEntity(actualNode, component.Layers, noiseCopy, grid, out var proto);
TryGetEntity(node, component.Layers, noiseCopy, grid, out var proto);
if (proto != layerProto.EntityMask)
{
continue;
}
var chunkOrigin = SharedMapSystem.GetChunkIndices(actualNode, ChunkSize) * ChunkSize;
var chunkOrigin = SharedMapSystem.GetChunkIndices(node, ChunkSize) * ChunkSize;
if (!pending.TryGetValue(chunkOrigin, out var pendingMarkers))
{
Expand All @@ -482,7 +481,7 @@ private void LoadChunks(
}
// Log.Info($"Added node at {actualNode} for chunk {chunkOrigin}");
layerMarkers.Add(actualNode);
layerMarkers.Add(node);
groupCount--;
}
}
Expand Down
27 changes: 27 additions & 0 deletions Content.Shared/Parallax/Biomes/Layers/BiomeMetaLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Robust.Shared.Noise;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;

namespace Content.Shared.Parallax.Biomes.Layers;

/// <summary>
/// Contains more biome layers recursively via a biome template.
/// Can be used for sub-biomes.
/// </summary>
[Serializable, NetSerializable]
public sealed class BiomeMetaLayer : IBiomeLayer
{
[DataField("noise")]
public FastNoiseLite Noise { get; } = new(0);

/// <inheritdoc/>
[DataField("threshold")]
public float Threshold { get; } = -1f;

/// <inheritdoc/>
[DataField("invert")]
public bool Invert { get; }

[DataField("template", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<BiomeTemplatePrototype>))]
public string Template = string.Empty;
}
44 changes: 44 additions & 0 deletions Content.Shared/Parallax/Biomes/SharedBiomeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ public bool TryGetBiomeTile(Vector2i indices, List<IBiomeLayer> layers, FastNois
{
var layer = layers[i];

// Check if the tile is from meta layer, otherwise fall back to default layers.
if (layer is BiomeMetaLayer meta)
{
SetNoise(noise, oldSeed, layer.Noise);
var found = noise.GetNoise(indices.X, indices.Y);
found *= layer.Invert ? -1 : 1;

if (found > layer.Threshold && TryGetBiomeTile(indices, ProtoManager.Index<BiomeTemplatePrototype>(meta.Template).Layers, noise,
grid, out tile))
{
noise.SetSeed(oldSeed);
return true;
}

continue;
}

if (layer is not BiomeTileLayer tileLayer)
continue;

Expand Down Expand Up @@ -187,6 +204,8 @@ protected bool TryGetEntity(Vector2i indices, List<IBiomeLayer> layers, FastNois
if (!worldLayer.AllowedTiles.Contains(tileId))
continue;

break;
case BiomeMetaLayer:
break;
default:
continue;
Expand All @@ -198,7 +217,16 @@ protected bool TryGetEntity(Vector2i indices, List<IBiomeLayer> layers, FastNois
value = invert ? value * -1 : value;

if (value < layer.Threshold)
continue;

if (layer is BiomeMetaLayer meta)
{
if (TryGetEntity(indices, ProtoManager.Index<BiomeTemplatePrototype>(meta.Template).Layers, noise, grid, out entity))
{
noise.SetSeed(oldSeed);
return true;
}

continue;
}

Expand Down Expand Up @@ -248,6 +276,8 @@ public bool TryGetDecals(Vector2i indices, List<IBiomeLayer> layers, FastNoiseLi
if (!worldLayer.AllowedTiles.Contains(tileId))
continue;

break;
case BiomeMetaLayer:
break;
default:
continue;
Expand All @@ -256,6 +286,20 @@ public bool TryGetDecals(Vector2i indices, List<IBiomeLayer> layers, FastNoiseLi
SetNoise(noise, oldSeed, layer.Noise);
var invert = layer.Invert;

if (layer is BiomeMetaLayer meta)
{
var found = noise.GetNoise(indices.X, indices.Y);
found *= layer.Invert ? -1 : 1;

if (found > layer.Threshold && TryGetDecals(indices, ProtoManager.Index<BiomeTemplatePrototype>(meta.Template).Layers, noise, grid, out decals))
{
noise.SetSeed(oldSeed);
return true;
}

continue;
}

// Check if the other layer should even render, if not then keep going.
if (layer is not BiomeDecalLayer decalLayer)
{
Expand Down
37 changes: 37 additions & 0 deletions Resources/Prototypes/Procedural/biome_templates.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
# Contains several biomes
- type: biomeTemplate
id: Continental
layers:
- !type:BiomeMetaLayer
template: Lava
- !type:BiomeMetaLayer
template: Caves
threshold: -0.5
noise:
frequency: 0.001
noiseType: OpenSimplex2
fractalType: FBm
octaves: 2
lacunarity: 2
gain: 0.5
- !type:BiomeMetaLayer
template: Grasslands
threshold: 0
noise:
frequency: 0.001
noiseType: OpenSimplex2
fractalType: FBm
octaves: 2
lacunarity: 2
gain: 0.5
- !type:BiomeMetaLayer
template: Snow
threshold: 0.5
noise:
frequency: 0.001
noiseType: OpenSimplex2
fractalType: FBm
octaves: 2
lacunarity: 2
gain: 0.5

# Desert
# TODO: Water in desert
- type: biomeTemplate
Expand Down

0 comments on commit 91610de

Please sign in to comment.