Skip to content

Commit

Permalink
Fix multiple removable test
Browse files Browse the repository at this point in the history
  • Loading branch information
IntelOrca committed Sep 22, 2024
1 parent 87507fa commit 4d3e1d7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 71 deletions.
2 changes: 1 addition & 1 deletion IntelOrca.Biohazard.BioRand.Tests/TestRouting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public void Removable_MultipleKeysRequired()
AssertItem(route, item0, key0);

var items = route.GetItemsContainingKey(key0);
Assert.Equal(2, items.Count);
Assert.Equal(3, items.Count);

Assert.True(route.AllNodesVisited);
}
Expand Down
2 changes: 2 additions & 0 deletions IntelOrca.Biohazard.BioRand/Routing/Graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ private static string GetIcon(Edge edge)
{
if ((edge.Flags & EdgeFlags.Consume) != 0)
return "fa:fa-triangle-exclamation";
if ((edge.Flags & EdgeFlags.Removable) != 0)
return "fa:fa-circle";
return "";
}

Expand Down
107 changes: 37 additions & 70 deletions IntelOrca.Biohazard.BioRand/Routing/RouteFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using static IntelOrca.Biohazard.BioRand.ScdCondition;

namespace IntelOrca.Biohazard.BioRand.Routing
{
Expand Down Expand Up @@ -93,65 +95,6 @@ private static State Fulfill(State state, Random rng)
}
}
return DoNextSubGraph(bestState, rng);

/*
var checklist = GetChecklist(state);
var requiredKeys = Shuffle(rng, checklist
.SelectMany(x => x.Need)
.Select(x => x.Node)
.Distinct());
var states = new List<State>();
foreach (var key in requiredKeys)
{
var allEdges = checklist
.Where(x => x.Need.All(x => x.Node == key))
.SelectMany(x => x.Need)
.ToArray();
var multipleRequired = allEdges.Any(x => (x.Flags & EdgeFlags.Consume) != 0);
var need = multipleRequired ? allEdges.Length : 1;
var available = Shuffle(rng, state.SpareItems.Where(x => x.Group == key.Group));
if (need == 1)
{
foreach (var a in available)
{
states.Add(state.PlaceKey(a, key));
}
}
else if (available.Length >= need)
{
var newState = state;
for (var i = 0; i < need; i++)
{
newState = newState.PlaceKey(available[i], key);
}
states.Add(newState);
}
}
if (states.Count == 0)
{
var subGraphs = state.OneWay.ToArray();
foreach (var n in subGraphs)
{
state = DoSubgraph(state, new[] { n }, first: false, rng);
}
return state;
}
else
{
State? firstState = null;
foreach (var s in states)
{
var finalState = Fulfill(s, rng);
if (finalState.Next.Count == 0 && finalState.OneWay.Count == 0)
{
return finalState;
}
firstState ??= finalState;
}
return firstState!;
}
*/
}

private static State Expand(State state)
Expand Down Expand Up @@ -180,17 +123,17 @@ private static State Expand(State state)

private static List<Node> GetRequiredKeys2(State state, Node node)
{
var required = GetMissingKeys(state.Keys, node);
var required = GetMissingKeys(state, state.Keys, node);
var newKeys = state.Keys.AddRange(required.Select(x => x.Node));
foreach (var n in state.Next)
{
if (n == node)
continue;

var missingKeys = GetMissingKeys(newKeys, n);
var missingKeys = GetMissingKeys(state, newKeys, n);
if (missingKeys.Count == 0)
{
missingKeys = GetMissingKeys(state.Keys, n);
missingKeys = GetMissingKeys(state, state.Keys, n);
foreach (var k in missingKeys)
{
if ((k.Flags & EdgeFlags.Consume) != 0)
Expand All @@ -204,21 +147,27 @@ private static List<Node> GetRequiredKeys2(State state, Node node)
return required.Select(x => x.Node).ToList();
}

private static List<Edge> GetMissingKeys(ImmutableMultiSet<Node> keys, Node node)
private static List<Edge> GetMissingKeys(State state, ImmutableMultiSet<Node> keys, Node node)
{
var requiredKeys = node.Requires
.Where(x => x.Node.Kind == NodeKind.Key)
.GroupBy(x => x.Node)
.ToArray();

var required = new List<Edge>();
var removable = new List<Node>();
foreach (var g in requiredKeys)
{
var have = keys.GetCount(g.Key);
var need = g.Count() - have;
var flags = CombineFlags(g);
var have = keys.GetCount(g.Key);
var need = (flags & EdgeFlags.Removable) != 0
? GetRemovableKeyCount(state, g.Key, node)
: g.Count();
need -= have;
for (var i = 0; i < need; i++)
{
required.Add(new Edge(g.Key, flags));
}
}

return required;
Expand Down Expand Up @@ -289,6 +238,24 @@ private static (State, Node[]) TakeNextNodes(State state)
return (state, result.ToArray());
}

private static int GetRemovableKeyCount(State state, Node key, Node node)
{
var count = 0;
Recurse(node);
return count;

void Recurse(Node n)
{
foreach (var r in n.Requires)
{
if (r.Node == key)
count++;
else
Recurse(r.Node);
}
}
}

private static HashSet<Node> GetHardDependencies(State state, Node node)
{
var set = new HashSet<Node>();
Expand Down Expand Up @@ -319,11 +286,6 @@ void Recurse(Node node)
}
}

private static ImmutableArray<ChecklistItem> GetChecklist(State state)
{
return state.Next.Select(x => GetChecklistItem(state, x)).ToImmutableArray();
}

private static ChecklistItem GetChecklistItem(State state, Node node)
{
var haveList = new List<Node>();
Expand All @@ -342,6 +304,11 @@ private static ChecklistItem GetChecklistItem(State state, Node node)
var need = edges.Count();
var have = state.Keys.GetCount(key);

if ((flags & EdgeFlags.Removable) != 0)
{
need = GetRemovableKeyCount(state, key, node);
}

var missing = Math.Max(0, need - have);
for (var i = 0; i < missing; i++)
missingList.Add(new Edge(key, flags));
Expand Down

0 comments on commit 4d3e1d7

Please sign in to comment.