Merge remote-tracking branch 'WD-core/master' into upstream-core

This commit is contained in:
BIGZi0348
2025-01-01 19:40:47 +03:00
31 changed files with 298 additions and 54 deletions

View File

@@ -30,7 +30,7 @@ public sealed partial class ArtifactSystem
_usedNodeIds.Clear();
var uninitializedNodes = new List<ArtifactNode> { new(){ Id = GetValidNodeId() } };
var uninitializedNodes = new List<ArtifactNode> { new() { Id = GetValidNodeId() } };
var createdNodes = 1;
while (uninitializedNodes.Count > 0)
@@ -50,7 +50,7 @@ public sealed partial class ArtifactSystem
break;
}
var child = new ArtifactNode {Id = GetValidNodeId(), Depth = node.Depth + 1};
var child = new ArtifactNode { Id = GetValidNodeId(), Depth = node.Depth + 1 };
node.Edges.Add(child.Id);
child.Edges.Add(node.Id);
@@ -240,4 +240,73 @@ public sealed partial class ArtifactSystem
{
return nodes.First(x => x.Id == id);
}
/// <summary>
/// WD.
/// Generate an Artifact tree with fully developed nodes.
/// </summary>
private void GenerateSafeArtifactNodeTree(EntityUid artifact, ref List<ArtifactNode> allNodes, int nodesToCreate)
{
if (nodesToCreate < 1)
{
Log.Error($"nodesToCreate {nodesToCreate} is less than 1. Aborting artifact tree generation.");
return;
}
_usedNodeIds.Clear();
var uninitializedNodes = new List<ArtifactNode> { new() { Id = GetValidNodeId() } };
var createdNodes = 1;
while (uninitializedNodes.Count > 0)
{
var node = uninitializedNodes[0];
uninitializedNodes.Remove(node);
node.Trigger = GetRandomTrigger(artifact, ref node);
node.Effect = GetRandomEffect(artifact, ref node);
var maxChildren = _random.Next(1, MaxEdgesPerNode - 1);
for (var i = 0; i < maxChildren; i++)
{
if (nodesToCreate <= createdNodes)
{
break;
}
var child = new ArtifactNode { Id = GetValidNodeId(), Depth = node.Depth + 1 };
node.Edges.Add(child.Id);
child.Edges.Add(node.Id);
uninitializedNodes.Add(child);
createdNodes++;
}
allNodes.Add(node);
}
foreach (var item in allNodes)
{
if (item.Depth <= 3)
item.Effect = GetSafeRandomEffect(artifact, item.Depth);
}
}
/// <summary>
/// WD.
/// </summary>
private string GetSafeRandomEffect(EntityUid artifact, int nodeDepth)
{
var allEffects = _prototype.EnumeratePrototypes<ArtifactEffectPrototype>()
.Where(x => (x.Whitelist?.IsValid(artifact, EntityManager) ?? true) && (!x.Blacklist?.IsValid(artifact, EntityManager) ?? true) && x.Safe).ToList();
var validDepth = allEffects.Select(x => x.TargetDepth).Distinct().ToList();
var weights = GetDepthWeights(validDepth, nodeDepth);
var selectedRandomTargetDepth = GetRandomTargetDepth(weights);
var targetEffects = allEffects
.Where(x => x.TargetDepth == selectedRandomTargetDepth).ToList();
return _random.Pick(targetEffects).ID;
}
}

View File

@@ -296,4 +296,17 @@ public sealed partial class ArtifactSystem : EntitySystem
{
return allNodes.First(n => n.Depth == 0);
}
/// <summary>
/// WD. Randomize a given artifact.
/// </summary>
[PublicAPI]
public void SafeRandomizeArtifact(EntityUid uid, ref ArtifactComponent component)
{
var nodeAmount = _random.Next(component.NodesMin, component.NodesMax);
GenerateSafeArtifactNodeTree(uid, ref component.NodeTree, nodeAmount);
var firstNode = GetRootNode(component.NodeTree);
EnterNode(uid, ref firstNode, component);
}
}