Haunted dungeon template (#23768)
* haunted dungeon * Initial work Still needs prefab gen work to make it interesting. * ime a worm * weh * Work * Slight tweaks --------- Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
53
Content.Server/Procedural/DungeonJob.CorridorClutterPost.cs
Normal file
53
Content.Server/Procedural/DungeonJob.CorridorClutterPost.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Procedural;
|
||||
using Content.Shared.Procedural.PostGeneration;
|
||||
using Content.Shared.Storage;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Physics.Components;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Procedural;
|
||||
|
||||
public sealed partial class DungeonJob
|
||||
{
|
||||
private async Task PostGen(CorridorClutterPostGen gen, Dungeon dungeon, EntityUid gridUid, MapGridComponent grid,
|
||||
Random random)
|
||||
{
|
||||
var physicsQuery = _entManager.GetEntityQuery<PhysicsComponent>();
|
||||
var count = (int) Math.Ceiling(dungeon.CorridorTiles.Count * gen.Chance);
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
var tile = random.Pick(dungeon.CorridorTiles);
|
||||
|
||||
var enumerator = _maps.GetAnchoredEntitiesEnumerator(_gridUid, _grid, tile);
|
||||
var blocked = false;
|
||||
|
||||
while (enumerator.MoveNext(out var ent))
|
||||
{
|
||||
if (!physicsQuery.TryGetComponent(ent, out var physics) ||
|
||||
!physics.CanCollide ||
|
||||
!physics.Hard)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
blocked = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (blocked)
|
||||
continue;
|
||||
|
||||
count--;
|
||||
|
||||
var protos = EntitySpawnCollection.GetSpawns(gen.Contents, random);
|
||||
var coords = _maps.ToCenterCoordinates(_gridUid, tile, _grid);
|
||||
_entManager.SpawnEntities(coords, protos);
|
||||
await SuspendIfOutOfTime();
|
||||
|
||||
if (!ValidateResume())
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ public sealed partial class DungeonJob
|
||||
|
||||
private bool HasWall(MapGridComponent grid, Vector2i tile)
|
||||
{
|
||||
var anchored = grid.GetAnchoredEntitiesEnumerator(tile);
|
||||
var anchored = _maps.GetAnchoredEntitiesEnumerator(_gridUid, _grid, tile);
|
||||
|
||||
while (anchored.MoveNext(out var uid))
|
||||
{
|
||||
@@ -52,7 +52,7 @@ public sealed partial class DungeonJob
|
||||
// Gather existing nodes
|
||||
foreach (var tile in allTiles)
|
||||
{
|
||||
var anchored = grid.GetAnchoredEntitiesEnumerator(tile);
|
||||
var anchored = _maps.GetAnchoredEntitiesEnumerator(_gridUid, _grid, tile);
|
||||
|
||||
while (anchored.MoveNext(out var anc))
|
||||
{
|
||||
@@ -186,7 +186,9 @@ public sealed partial class DungeonJob
|
||||
// - Tiles first
|
||||
foreach (var neighbor in dungeon.RoomExteriorTiles)
|
||||
{
|
||||
if (dungeon.RoomTiles.Contains(neighbor))
|
||||
DebugTools.Assert(!dungeon.RoomTiles.Contains(neighbor));
|
||||
|
||||
if (dungeon.Entrances.Contains(neighbor))
|
||||
continue;
|
||||
|
||||
if (!_anchorable.TileFree(grid, neighbor, DungeonSystem.CollisionLayer, DungeonSystem.CollisionMask))
|
||||
@@ -265,7 +267,6 @@ public sealed partial class DungeonJob
|
||||
Random random)
|
||||
{
|
||||
var physicsQuery = _entManager.GetEntityQuery<PhysicsComponent>();
|
||||
var tagQuery = _entManager.GetEntityQuery<TagComponent>();
|
||||
|
||||
foreach (var tile in dungeon.CorridorTiles)
|
||||
{
|
||||
@@ -771,7 +772,7 @@ public sealed partial class DungeonJob
|
||||
{
|
||||
for (var y = -expansion; y <= expansion; y++)
|
||||
{
|
||||
var neighbor = new Vector2i(tile.X + x, tile.Y + y);
|
||||
var neighbor = new Vector2(tile.X + x, tile.Y + y).Floored();
|
||||
|
||||
if (dungeon.RoomTiles.Contains(neighbor) ||
|
||||
dungeon.RoomExteriorTiles.Contains(neighbor) ||
|
||||
@@ -817,6 +818,52 @@ public sealed partial class DungeonJob
|
||||
return mod;
|
||||
});
|
||||
|
||||
WidenCorridor(dungeon, gen.Width, corridorTiles);
|
||||
|
||||
var setTiles = new List<(Vector2i, Tile)>();
|
||||
var tileDef = _prototype.Index(gen.Tile);
|
||||
|
||||
foreach (var tile in corridorTiles)
|
||||
{
|
||||
setTiles.Add((tile, _tile.GetVariantTile(tileDef, random)));
|
||||
}
|
||||
|
||||
grid.SetTiles(setTiles);
|
||||
dungeon.CorridorTiles.UnionWith(corridorTiles);
|
||||
BuildCorridorExterior(dungeon);
|
||||
}
|
||||
|
||||
private void BuildCorridorExterior(Dungeon dungeon)
|
||||
{
|
||||
var exterior = dungeon.CorridorExteriorTiles;
|
||||
|
||||
// Just ignore entrances or whatever for now.
|
||||
foreach (var tile in dungeon.CorridorTiles)
|
||||
{
|
||||
for (var x = -1; x <= 1; x++)
|
||||
{
|
||||
for (var y = -1; y <= 1; y++)
|
||||
{
|
||||
var neighbor = new Vector2i(tile.X + x, tile.Y + y);
|
||||
|
||||
if (dungeon.CorridorTiles.Contains(neighbor) ||
|
||||
dungeon.RoomExteriorTiles.Contains(neighbor) ||
|
||||
dungeon.RoomTiles.Contains(neighbor) ||
|
||||
dungeon.Entrances.Contains(neighbor))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
exterior.Add(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void WidenCorridor(Dungeon dungeon, float width, ICollection<Vector2i> corridorTiles)
|
||||
{
|
||||
var expansion = width - 2;
|
||||
|
||||
// Widen the path
|
||||
if (expansion >= 1)
|
||||
{
|
||||
@@ -831,7 +878,7 @@ public sealed partial class DungeonJob
|
||||
{
|
||||
for (var y = -expansion; y <= expansion; y++)
|
||||
{
|
||||
var neighbor = new Vector2i(node.X + x, node.Y + y);
|
||||
var neighbor = new Vector2(node.X + x, node.Y + y).Floored();
|
||||
|
||||
// Diagonals still matter here.
|
||||
if (dungeon.RoomTiles.Contains(neighbor) ||
|
||||
@@ -852,36 +899,6 @@ public sealed partial class DungeonJob
|
||||
corridorTiles.Add(node);
|
||||
}
|
||||
}
|
||||
|
||||
var setTiles = new List<(Vector2i, Tile)>();
|
||||
var tileDef = _prototype.Index(gen.Tile);
|
||||
|
||||
foreach (var tile in corridorTiles)
|
||||
{
|
||||
setTiles.Add((tile, _tile.GetVariantTile(tileDef, random)));
|
||||
}
|
||||
|
||||
grid.SetTiles(setTiles);
|
||||
dungeon.CorridorTiles.UnionWith(corridorTiles);
|
||||
|
||||
var exterior = dungeon.CorridorExteriorTiles;
|
||||
|
||||
// Just ignore entrances or whatever for now.
|
||||
foreach (var tile in dungeon.CorridorTiles)
|
||||
{
|
||||
for (var x = -1; x <= 1; x++)
|
||||
{
|
||||
for (var y = -1; y <= 1; y++)
|
||||
{
|
||||
var neighbor = new Vector2i(tile.X + x, tile.Y + y);
|
||||
|
||||
if (dungeon.CorridorTiles.Contains(neighbor))
|
||||
continue;
|
||||
|
||||
exterior.Add(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PostGen(EntranceFlankPostGen gen, Dungeon dungeon, EntityUid gridUid, MapGridComponent grid,
|
||||
|
||||
@@ -21,7 +21,6 @@ public sealed partial class DungeonJob
|
||||
var dungeonRotation = _dungeon.GetDungeonRotation(seed);
|
||||
var dungeonTransform = Matrix3.CreateTransform(_position, dungeonRotation);
|
||||
var roomPackProtos = new Dictionary<Vector2i, List<DungeonRoomPackPrototype>>();
|
||||
var fallbackTile = new Tile(_tileDefManager[prefab.Tile].TileId);
|
||||
|
||||
foreach (var pack in _prototype.EnumeratePrototypes<DungeonRoomPackPrototype>())
|
||||
{
|
||||
@@ -325,6 +324,7 @@ public sealed partial class DungeonJob
|
||||
}
|
||||
|
||||
room.Entrances.Add(entrancePos);
|
||||
dungeon.Entrances.Add(entrancePos);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
193
Content.Server/Procedural/DungeonJob.WormPost.cs
Normal file
193
Content.Server/Procedural/DungeonJob.WormPost.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Procedural;
|
||||
using Content.Shared.Procedural.PostGeneration;
|
||||
using Robust.Shared.Collections;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Procedural;
|
||||
|
||||
public sealed partial class DungeonJob
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to connect rooms via worm-like corridors.
|
||||
/// </summary>
|
||||
private async Task PostGen(WormCorridorPostGen gen, Dungeon dungeon, EntityUid gridUid, MapGridComponent grid, Random random)
|
||||
{
|
||||
var networks = new List<(Vector2i Start, HashSet<Vector2i> Network)>();
|
||||
|
||||
// List of places to start from.
|
||||
var worm = new ValueList<Vector2i>();
|
||||
var startAngles = new Dictionary<Vector2i, Angle>();
|
||||
|
||||
foreach (var room in dungeon.Rooms)
|
||||
{
|
||||
foreach (var entrance in room.Entrances)
|
||||
{
|
||||
var network = new HashSet<Vector2i> { entrance };
|
||||
networks.Add((entrance, network));
|
||||
|
||||
// Point away from the room to start with.
|
||||
startAngles.Add(entrance, (entrance + grid.TileSizeHalfVector - room.Center).ToAngle());
|
||||
}
|
||||
}
|
||||
|
||||
// There's a lot of ways to handle this, e.g. pathfinding towards each room
|
||||
// For simplicity we'll go through each entrance randomly and generate worms from it
|
||||
// then as a final step we will connect all of their networks.
|
||||
random.Shuffle(networks);
|
||||
|
||||
for (var i = 0; i < gen.Count; i++)
|
||||
{
|
||||
// Find a random network to worm from.
|
||||
var startIndex = (i % networks.Count);
|
||||
var startPos = networks[startIndex].Start;
|
||||
var position = startPos + grid.TileSizeHalfVector;
|
||||
|
||||
var remainingLength = gen.Length;
|
||||
worm.Clear();
|
||||
var angle = startAngles[startPos];
|
||||
|
||||
for (var x = remainingLength; x >= 0; x--)
|
||||
{
|
||||
position += angle.ToVec();
|
||||
angle += random.NextAngle(-gen.MaxAngleChange, gen.MaxAngleChange);
|
||||
var roundedPos = position.Floored();
|
||||
|
||||
// Check if the tile doesn't overlap something it shouldn't
|
||||
if (dungeon.RoomTiles.Contains(roundedPos) ||
|
||||
dungeon.RoomExteriorTiles.Contains(roundedPos))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
worm.Add(roundedPos);
|
||||
}
|
||||
|
||||
// Uhh yeah.
|
||||
if (worm.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find a random part on the existing worm to start.
|
||||
var value = random.Pick(worm);
|
||||
networks[startIndex].Network.UnionWith(worm);
|
||||
startAngles[value] = random.NextAngle();
|
||||
}
|
||||
|
||||
// Now to ensure they all connect we'll pathfind each network to one another
|
||||
// Simple BFS pathfinder
|
||||
var main = networks[0];
|
||||
|
||||
var frontier = new PriorityQueue<Vector2i, float>();
|
||||
var cameFrom = new Dictionary<Vector2i, Vector2i>();
|
||||
var costSoFar = new Dictionary<Vector2i, float>();
|
||||
|
||||
// How many times we try to patch the networks together
|
||||
var attempts = 3;
|
||||
|
||||
for (var attempt = 0; attempt < attempts; attempt++)
|
||||
{
|
||||
// Skip index 0
|
||||
for (var i = networks.Count - 1; i > 0; i--)
|
||||
{
|
||||
cameFrom.Clear();
|
||||
frontier.Clear();
|
||||
costSoFar.Clear();
|
||||
|
||||
var targetNode = random.Pick(main.Network);
|
||||
|
||||
var other = networks[i];
|
||||
var startNode = other.Network.First();
|
||||
frontier.Enqueue(startNode, 0f);
|
||||
costSoFar[startNode] = 0f;
|
||||
var count = 0;
|
||||
|
||||
await SuspendIfOutOfTime();
|
||||
if (!ValidateResume())
|
||||
return;
|
||||
|
||||
while (frontier.TryDequeue(out var node, out _) && count < gen.PathLimit)
|
||||
{
|
||||
count++;
|
||||
|
||||
// Found
|
||||
if (main.Network.Contains(node))
|
||||
{
|
||||
// found, rebuild
|
||||
frontier.Clear();
|
||||
main.Network.Add(node);
|
||||
main.Network.UnionWith(other.Network);
|
||||
var target = node;
|
||||
|
||||
// Rebuild
|
||||
while (cameFrom.TryGetValue(target, out var source))
|
||||
{
|
||||
target = source;
|
||||
main.Network.Add(target);
|
||||
}
|
||||
|
||||
networks.RemoveSwap(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (var x = -1; x <= 1; x++)
|
||||
{
|
||||
for (var y = -1; y <= 1; y++)
|
||||
{
|
||||
if (x == 0 && y == 0)
|
||||
continue;
|
||||
|
||||
var neighbor = node + new Vector2i(x, y);
|
||||
|
||||
// Exclude room tiles.
|
||||
if (dungeon.RoomTiles.Contains(neighbor) ||
|
||||
dungeon.RoomExteriorTiles.Contains(neighbor))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var tileCost = (neighbor - node).Length;
|
||||
var gScore = costSoFar[node] + tileCost;
|
||||
|
||||
if (costSoFar.TryGetValue(neighbor, out var nextValue) && gScore >= nextValue)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
cameFrom[neighbor] = node;
|
||||
costSoFar[neighbor] = gScore;
|
||||
var hScore = (targetNode - neighbor).Length + gScore;
|
||||
|
||||
frontier.Enqueue(neighbor, hScore);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WidenCorridor(dungeon, gen.Width, main.Network);
|
||||
dungeon.CorridorTiles.UnionWith(main.Network);
|
||||
BuildCorridorExterior(dungeon);
|
||||
|
||||
var tiles = new List<(Vector2i Index, Tile Tile)>();
|
||||
var tileDef = _prototype.Index(gen.Tile);
|
||||
|
||||
foreach (var tile in dungeon.CorridorTiles)
|
||||
{
|
||||
tiles.Add((tile, _tile.GetVariantTile(tileDef, random)));
|
||||
}
|
||||
|
||||
foreach (var tile in dungeon.CorridorExteriorTiles)
|
||||
{
|
||||
tiles.Add((tile, _tile.GetVariantTile(tileDef, random)));
|
||||
}
|
||||
|
||||
_maps.SetTiles(_gridUid, _grid, tiles);
|
||||
}
|
||||
}
|
||||
@@ -126,6 +126,9 @@ public sealed partial class DungeonJob : Job<Dungeon>
|
||||
case CornerClutterPostGen clutter:
|
||||
await PostGen(clutter, dungeon, _gridUid, _grid, random);
|
||||
break;
|
||||
case CorridorClutterPostGen corClutter:
|
||||
await PostGen(corClutter, dungeon, _gridUid, _grid, random);
|
||||
break;
|
||||
case CorridorPostGen cordor:
|
||||
await PostGen(cordor, dungeon, _gridUid, _grid, random);
|
||||
break;
|
||||
@@ -159,6 +162,9 @@ public sealed partial class DungeonJob : Job<Dungeon>
|
||||
case WallMountPostGen wall:
|
||||
await PostGen(wall, dungeon, _gridUid, _grid, random);
|
||||
break;
|
||||
case WormCorridorPostGen worm:
|
||||
await PostGen(worm, dungeon, _gridUid, _grid, random);
|
||||
break;
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user