Ore + entitytable fixes (#37675)
* Ore + entitytable fixes Iterate every dungeon not just last. * Big shot * Fixes
This commit is contained in:
@@ -15,131 +15,136 @@ public sealed partial class DungeonJob
|
||||
/// </summary>
|
||||
private async Task PostGen(
|
||||
OreDunGen gen,
|
||||
Dungeon dungeon,
|
||||
List<Dungeon> dungeons,
|
||||
HashSet<Vector2i> reservedTiles,
|
||||
Random random)
|
||||
{
|
||||
// Doesn't use dungeon data because layers and we don't need top-down support at the moment.
|
||||
|
||||
var emptyTiles = false;
|
||||
var replaceEntities = new Dictionary<Vector2i, EntityUid>();
|
||||
var availableTiles = new List<Vector2i>();
|
||||
|
||||
foreach (var node in dungeon.AllTiles)
|
||||
foreach (var dungeon in dungeons)
|
||||
{
|
||||
// Empty tile, skip if relevant.
|
||||
if (!emptyTiles && (!_maps.TryGetTile(_grid, node, out var tile) || tile.IsEmpty))
|
||||
continue;
|
||||
var emptyTiles = false;
|
||||
var replaceEntities = new Dictionary<Vector2i, EntityUid>();
|
||||
var availableTiles = new List<Vector2i>();
|
||||
|
||||
// Check if it's a valid spawn, if so then use it.
|
||||
var enumerator = _maps.GetAnchoredEntitiesEnumerator(_gridUid, _grid, node);
|
||||
var found = false;
|
||||
|
||||
// We use existing entities as a mark to spawn in place
|
||||
// OR
|
||||
// We check for any existing entities to see if we can spawn there.
|
||||
while (enumerator.MoveNext(out var uid))
|
||||
foreach (var node in dungeon.AllTiles)
|
||||
{
|
||||
// We can't replace so just stop here.
|
||||
if (gen.Replacement == null)
|
||||
break;
|
||||
if (reservedTiles.Contains(node))
|
||||
continue;
|
||||
|
||||
var prototype = _entManager.GetComponent<MetaDataComponent>(uid.Value).EntityPrototype;
|
||||
// Empty tile, skip if relevant.
|
||||
if (!emptyTiles && (!_maps.TryGetTile(_grid, node, out var tile) || tile.IsEmpty))
|
||||
continue;
|
||||
|
||||
if (prototype?.ID == gen.Replacement)
|
||||
// Check if it's a valid spawn, if so then use it.
|
||||
var enumerator = _maps.GetAnchoredEntitiesEnumerator(_gridUid, _grid, node);
|
||||
var found = false;
|
||||
|
||||
// We use existing entities as a mark to spawn in place
|
||||
// OR
|
||||
// We check for any existing entities to see if we can spawn there.
|
||||
while (enumerator.MoveNext(out var uid))
|
||||
{
|
||||
replaceEntities[node] = uid.Value;
|
||||
found = true;
|
||||
break;
|
||||
// We can't replace so just stop here.
|
||||
if (gen.Replacement == null)
|
||||
break;
|
||||
|
||||
var prototype = _entManager.GetComponent<MetaDataComponent>(uid.Value).EntityPrototype;
|
||||
|
||||
if (prototype?.ID == gen.Replacement)
|
||||
{
|
||||
replaceEntities[node] = uid.Value;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
continue;
|
||||
|
||||
// Add it to valid nodes.
|
||||
availableTiles.Add(node);
|
||||
|
||||
await SuspendDungeon();
|
||||
|
||||
if (!ValidateResume())
|
||||
return;
|
||||
}
|
||||
|
||||
if (!found)
|
||||
continue;
|
||||
var remapping = new Dictionary<EntProtoId, EntProtoId>();
|
||||
|
||||
// Add it to valid nodes.
|
||||
availableTiles.Add(node);
|
||||
|
||||
await SuspendDungeon();
|
||||
|
||||
if (!ValidateResume())
|
||||
return;
|
||||
}
|
||||
|
||||
var remapping = new Dictionary<EntProtoId, EntProtoId>();
|
||||
|
||||
// TODO: Move this to engine
|
||||
if (_prototype.TryIndex(gen.Entity, out var proto) &&
|
||||
proto.Components.TryGetComponent("EntityRemap", out var comps))
|
||||
{
|
||||
var remappingComp = (EntityRemapComponent) comps;
|
||||
remapping = remappingComp.Mask;
|
||||
}
|
||||
|
||||
var frontier = new ValueList<Vector2i>(32);
|
||||
|
||||
// Iterate the group counts and pathfind out each group.
|
||||
for (var i = 0; i < gen.Count; i++)
|
||||
{
|
||||
await SuspendDungeon();
|
||||
|
||||
if (!ValidateResume())
|
||||
return;
|
||||
|
||||
var groupSize = random.Next(gen.MinGroupSize, gen.MaxGroupSize + 1);
|
||||
|
||||
// While we have remaining tiles keep iterating
|
||||
while (groupSize > 0 && availableTiles.Count > 0)
|
||||
// TODO: Move this to engine
|
||||
if (_prototype.TryIndex(gen.Entity, out var proto) &&
|
||||
proto.Components.TryGetComponent("EntityRemap", out var comps))
|
||||
{
|
||||
var startNode = random.PickAndTake(availableTiles);
|
||||
frontier.Clear();
|
||||
frontier.Add(startNode);
|
||||
|
||||
// This essentially may lead to a vein being split in multiple areas but the count matters more than position.
|
||||
while (frontier.Count > 0 && groupSize > 0)
|
||||
{
|
||||
// Need to pick a random index so we don't just get straight lines of ores.
|
||||
var frontierIndex = random.Next(frontier.Count);
|
||||
var node = frontier[frontierIndex];
|
||||
frontier.RemoveSwap(frontierIndex);
|
||||
availableTiles.Remove(node);
|
||||
|
||||
// Add neighbors if they're valid, worst case we add no more and pick another random seed tile.
|
||||
for (var x = -1; x <= 1; x++)
|
||||
{
|
||||
for (var y = -1; y <= 1; y++)
|
||||
{
|
||||
var neighbor = new Vector2i(node.X + x, node.Y + y);
|
||||
|
||||
if (frontier.Contains(neighbor) || !availableTiles.Contains(neighbor))
|
||||
continue;
|
||||
|
||||
frontier.Add(neighbor);
|
||||
}
|
||||
}
|
||||
|
||||
var prototype = gen.Entity;
|
||||
|
||||
if (replaceEntities.TryGetValue(node, out var existingEnt))
|
||||
{
|
||||
var existingProto = _entManager.GetComponent<MetaDataComponent>(existingEnt).EntityPrototype;
|
||||
_entManager.DeleteEntity(existingEnt);
|
||||
|
||||
if (existingProto != null && remapping.TryGetValue(existingProto.ID, out var remapped))
|
||||
{
|
||||
prototype = remapped;
|
||||
}
|
||||
}
|
||||
|
||||
// Tile valid salad so add it.
|
||||
_entManager.SpawnAtPosition(prototype, _maps.GridTileToLocal(_gridUid, _grid, node));
|
||||
|
||||
groupSize--;
|
||||
}
|
||||
var remappingComp = (EntityRemapComponent) comps;
|
||||
remapping = remappingComp.Mask;
|
||||
}
|
||||
|
||||
if (groupSize > 0)
|
||||
var frontier = new ValueList<Vector2i>(32);
|
||||
|
||||
// Iterate the group counts and pathfind out each group.
|
||||
for (var i = 0; i < gen.Count; i++)
|
||||
{
|
||||
_sawmill.Warning($"Found remaining group size for ore veins of {gen.Replacement ?? "null"}!");
|
||||
await SuspendDungeon();
|
||||
|
||||
if (!ValidateResume())
|
||||
return;
|
||||
|
||||
var groupSize = random.Next(gen.MinGroupSize, gen.MaxGroupSize + 1);
|
||||
|
||||
// While we have remaining tiles keep iterating
|
||||
while (groupSize > 0 && availableTiles.Count > 0)
|
||||
{
|
||||
var startNode = random.PickAndTake(availableTiles);
|
||||
frontier.Clear();
|
||||
frontier.Add(startNode);
|
||||
|
||||
// This essentially may lead to a vein being split in multiple areas but the count matters more than position.
|
||||
while (frontier.Count > 0 && groupSize > 0)
|
||||
{
|
||||
// Need to pick a random index so we don't just get straight lines of ores.
|
||||
var frontierIndex = random.Next(frontier.Count);
|
||||
var node = frontier[frontierIndex];
|
||||
frontier.RemoveSwap(frontierIndex);
|
||||
availableTiles.Remove(node);
|
||||
|
||||
// Add neighbors if they're valid, worst case we add no more and pick another random seed tile.
|
||||
for (var x = -1; x <= 1; x++)
|
||||
{
|
||||
for (var y = -1; y <= 1; y++)
|
||||
{
|
||||
var neighbor = new Vector2i(node.X + x, node.Y + y);
|
||||
|
||||
if (frontier.Contains(neighbor) || !availableTiles.Contains(neighbor))
|
||||
continue;
|
||||
|
||||
frontier.Add(neighbor);
|
||||
}
|
||||
}
|
||||
|
||||
var prototype = gen.Entity;
|
||||
|
||||
if (replaceEntities.TryGetValue(node, out var existingEnt))
|
||||
{
|
||||
var existingProto = _entManager.GetComponent<MetaDataComponent>(existingEnt).EntityPrototype;
|
||||
_entManager.DeleteEntity(existingEnt);
|
||||
|
||||
if (existingProto != null && remapping.TryGetValue(existingProto.ID, out var remapped))
|
||||
{
|
||||
prototype = remapped;
|
||||
}
|
||||
}
|
||||
|
||||
// Tile valid salad so add it.
|
||||
_entManager.SpawnAtPosition(prototype, _maps.GridTileToLocal(_gridUid, _grid, node));
|
||||
|
||||
groupSize--;
|
||||
}
|
||||
}
|
||||
|
||||
if (groupSize > 0)
|
||||
{
|
||||
_sawmill.Warning($"Found remaining group size for ore veins of {gen.Replacement ?? "null"}!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user