Skip to content

Commit

Permalink
Remove edge and instead just have keys locked to a certain type
Browse files Browse the repository at this point in the history
  • Loading branch information
IntelOrca committed Sep 22, 2024
1 parent 4d3e1d7 commit 532e723
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 176 deletions.
144 changes: 86 additions & 58 deletions IntelOrca.Biohazard.BioRand.Tests/TestRouting.cs

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions IntelOrca.Biohazard.BioRand/Routing/Edge.cs

This file was deleted.

11 changes: 0 additions & 11 deletions IntelOrca.Biohazard.BioRand/Routing/EdgeFlags.cs

This file was deleted.

26 changes: 13 additions & 13 deletions IntelOrca.Biohazard.BioRand/Routing/Graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,17 @@ public ImmutableArray<Node> GetEdges(Node node)

private string[] GetKeys(Node node)
{
var edges = node.Requires
.Where(x => x.Node.Kind == NodeKind.Key)
.ToArray();
return edges
.Select(e => string.Join(" ", GetIcon(e), $"K<sub>{e.Node.Id}</sub>"))
return node.Requires
.Where(x => x.IsKey)
.Select(e => string.Join(" ", GetIcon(e), $"K<sub>{e.Id}</sub>"))
.ToArray();
}

private static string GetIcon(Edge edge)
private static string GetIcon(Node node)
{
if ((edge.Flags & EdgeFlags.Consume) != 0)
if (node.Kind == NodeKind.ConsumableKey)
return "fa:fa-triangle-exclamation";
if ((edge.Flags & EdgeFlags.Removable) != 0)
if (node.Kind == NodeKind.RemovableKey)
return "fa:fa-circle";
return "";
}
Expand Down Expand Up @@ -90,8 +88,8 @@ private ImmutableArray<ImmutableArray<Node>> GetSubgraphs()
}
}

private static bool IsStartNode(Node node) => RequiresNothingOrKeys(node) && node.Kind != NodeKind.Key;
private static bool RequiresNothingOrKeys(Node node) => node.Requires.All(x => x.Node.Kind == NodeKind.Key);
private static bool IsStartNode(Node node) => RequiresNothingOrKeys(node) && !node.IsKey;
private static bool RequiresNothingOrKeys(Node node) => node.Requires.All(x => x.IsKey);

public string ToMermaid()
{
Expand All @@ -105,7 +103,7 @@ public string ToMermaid()
mb.BeginSubgraph($"G<sub>{gIndex}</sub>");
foreach (var node in g)
{
if (node.Kind == NodeKind.Key && !keysAsNodes)
if (node.IsKey && !keysAsNodes)
continue;

var (letter, shape) = GetNodeLabel(node);
Expand All @@ -122,7 +120,7 @@ public string ToMermaid()
foreach (var edge in Edges)
{
var a = edge.Key;
if (a.Kind == NodeKind.Key && !keysAsNodes)
if (a.IsKey && !keysAsNodes)
continue;

var sourceName = GetNodeName(a);
Expand Down Expand Up @@ -153,7 +151,9 @@ static string GetNodeName(Node node)
return node.Kind switch
{
NodeKind.Item => ('I', MermaidShape.Square),
NodeKind.Key => ('K', MermaidShape.Hexagon),
NodeKind.ReusuableKey => ('K', MermaidShape.Hexagon),
NodeKind.ConsumableKey => ('K', MermaidShape.Hexagon),
NodeKind.RemovableKey => ('K', MermaidShape.Hexagon),
_ => ('R', MermaidShape.Circle),
};
}
Expand Down
33 changes: 19 additions & 14 deletions IntelOrca.Biohazard.BioRand/Routing/GraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,42 @@ private int GetNextId()
return ++_id;
}

public Node Key(int group, string? label)
public Node ReusuableKey(int group, string? label)
{
var node = new Node(GetNextId(), group, NodeKind.Key, label, new Edge[0]);
var node = new Node(GetNextId(), group, NodeKind.ReusuableKey, label, new Node[0]);
_nodes.Add(node);
return node;
}

public Node Item(int group, string? label, params Node[] requires)
public Node ConsumableKey(int group, string? label)
{
var node = new Node(GetNextId(), group, NodeKind.Item, label, requires.Select(x => new Edge(x)).ToArray());
var node = new Node(GetNextId(), group, NodeKind.ConsumableKey, label, new Node[0]);
_nodes.Add(node);
return node;
}

public Node AndGate(string? label)
public Node RemovableKey(int group, string? label)
{
var node = new Node(GetNextId(), 0, NodeKind.AndGate, label, new Edge[0]);
var node = new Node(GetNextId(), group, NodeKind.RemovableKey, label, new Node[0]);
_nodes.Add(node);
return node;
}

public Node AndGate(string? label, params Node[] requires)
public Node Item(int group, string? label, params Node[] requires)
{
var node = new Node(GetNextId(), group, NodeKind.Item, label, requires.ToArray());
_nodes.Add(node);
return node;
}

public Node AndGate(string? label)
{
var node = new Node(GetNextId(), 0, NodeKind.AndGate, label, requires.Select(x => new Edge(x)).ToArray());
var node = new Node(GetNextId(), 0, NodeKind.AndGate, label, new Node[0]);
_nodes.Add(node);
return node;
}

public Node AndGate(string? label, params Edge[] requires)
public Node AndGate(string? label, params Node[] requires)
{
var node = new Node(GetNextId(), 0, NodeKind.AndGate, label, requires.ToArray());
_nodes.Add(node);
Expand All @@ -51,26 +58,24 @@ public Node AndGate(string? label, params Edge[] requires)

public Node OrGate(string? label, params Node[] requires)
{
var node = new Node(GetNextId(), 0, NodeKind.OrGate, label, requires.Select(x => new Edge(x)).ToArray());
var node = new Node(GetNextId(), 0, NodeKind.OrGate, label, requires.ToArray());
_nodes.Add(node);
return node;
}

public Node OneWay(string? label, params Edge[] requires)
public Node OneWay(string? label, params Node[] requires)
{
var node = new Node(GetNextId(), 0, NodeKind.OneWay, label, requires.ToArray());
_nodes.Add(node);
return node;
}

public Node OneWay(string? label, params Node[] requires) => OneWay(label, requires.Select(x => new Edge(x)).ToArray());

public Graph Build()
{
var edges = new Dictionary<Node, List<Node>>();
foreach (var c in _nodes)
{
foreach (var r in c.Requires.Select(x => x.Node))
foreach (var r in c.Requires)
{
if (!edges.TryGetValue(r, out var list))
{
Expand Down
12 changes: 9 additions & 3 deletions IntelOrca.Biohazard.BioRand/Routing/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ namespace IntelOrca.Biohazard.BioRand.Routing
public int Group { get; }
public NodeKind Kind { get; }
public string? Label { get; }
public Edge[] Requires { get; }
public Node[] Requires { get; }

public Node(int id, int group, NodeKind flags, string? label, Edge[] requires)
public Node(int id, int group, NodeKind kind, string? label, Node[] requires)
{
Id = id;
Group = group;
Kind = flags;
Kind = kind;
Label = label;
Requires = requires;
}

public bool IsItem => Kind == NodeKind.Item;
public bool IsKey =>
Kind == NodeKind.ReusuableKey ||
Kind == NodeKind.ConsumableKey ||
Kind == NodeKind.RemovableKey;

public bool Equals(Node other) => Id == other.Id;
public override bool Equals(object obj) => obj is Node node && Equals(node);
public override int GetHashCode() => Id.GetHashCode();
Expand Down
4 changes: 3 additions & 1 deletion IntelOrca.Biohazard.BioRand/Routing/NodeKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public enum NodeKind : byte
OrGate,
OneWay,
Item,
Key,
ReusuableKey,
ConsumableKey,
RemovableKey
}
}
12 changes: 7 additions & 5 deletions IntelOrca.Biohazard.BioRand/Routing/Route.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void Visit(Node n)
if (!visited.Add(n))
return;

if (keysAsNodes || n.Kind != NodeKind.Key)
if (keysAsNodes || n.IsKey)
{
var label = n.Label;
if (n.Kind == NodeKind.Item && !keysAsNodes)
Expand All @@ -60,14 +60,16 @@ void Visit(Node n)
mb.Node($"N{n.Id}", label,
n.Kind switch
{
NodeKind.Key => MermaidShape.Hexagon,
NodeKind.ReusuableKey => MermaidShape.Hexagon,
NodeKind.ConsumableKey => MermaidShape.Hexagon,
NodeKind.RemovableKey => MermaidShape.Hexagon,
NodeKind.Item => keysAsNodes
? MermaidShape.Square
: MermaidShape.DoubleSquare,
_ => MermaidShape.Circle,
});
}
if (n.Kind == NodeKind.Key)
if (n.IsKey)
{
var items = ItemToKey.GetKeysContainingValue(n);
foreach (var item in items)
Expand All @@ -80,10 +82,10 @@ void Visit(Node n)
}
else
{
foreach (var r in n.Requires.Select(x => x.Node))
foreach (var r in n.Requires)
{
Visit(r);
if (r.Kind == NodeKind.Key && !keysAsNodes)
if (r.IsKey && !keysAsNodes)
{
var items = ItemToKey.GetKeysContainingValue(n);
foreach (var item in items)
Expand Down
Loading

0 comments on commit 532e723

Please sign in to comment.