Skip to content

Commit

Permalink
Fix crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed May 6, 2024
1 parent 869a27b commit b5ed17d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Craftimizer/Windows/SynthHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ private void DrawMacroActions()
}

if (ImGui.Button("Open in Macro Editor", new(-1, 0)))
Service.Plugin.OpenMacroEditor(CharacterStats!, RecipeData!, new(Service.ClientState.LocalPlayer!.StatusList), Enumerable.Empty<ActionType>(), null);
Service.Plugin.OpenMacroEditor(CharacterStats!, RecipeData!, new(Service.ClientState.LocalPlayer!.StatusList), [], null);
}

private void OnStartCrafting(ushort recipeId)
Expand Down
4 changes: 2 additions & 2 deletions Simulator/SimulationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public record struct SimulationState
public ActionStates ActionStates;

// https://github.com/ffxiv-teamcraft/simulator/blob/0682dfa76043ff4ccb38832c184d046ceaff0733/src/model/tables.ts#L2
private static ReadOnlySpan<int> HQPercentTable => new[] {
private static ReadOnlySpan<int> HQPercentTable => [
1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8,
9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17,
17, 18, 18, 18, 19, 19, 20, 20, 21, 22, 23, 24, 26, 28, 31, 34, 38, 42, 47, 52, 58, 64, 68, 71,
74, 76, 78, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 94, 96, 98, 100
};
];
public readonly int HQPercent => HQPercentTable[(int)Math.Clamp((float)Quality / Input.Recipe.MaxQuality * 100, 0, 100)];
public readonly int Collectability => Math.Max(Quality / 10, 1);
public readonly int MaxCollectability => Math.Max(Input.Recipe.MaxQuality / 10, 1);
Expand Down
11 changes: 6 additions & 5 deletions Solver/MCTS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ private Node Select()
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
[SkipLocalsInit]
private (Node ExpandedNode, float Score) ExpandAndRollout(Random random, Simulator simulator, Node initialNode)
private (Node ExpandedNode, float Score) ExpandAndRollout(Random random, Simulator simulator, Node initialNode, Span<ActionType> actionBuffer)
{
ref var initialState = ref initialNode.State;
// expand once
Expand All @@ -194,7 +193,7 @@ private Node Select()
var currentActions = expandedNode.State.AvailableActions;

byte actionCount = 0;
Span<ActionType> actions = stackalloc ActionType[Math.Min(config.MaxStepCount - currentState.ActionCount, config.MaxRolloutStepCount)];
var actions = actionBuffer[..Math.Min(config.MaxStepCount - currentState.ActionCount, config.MaxRolloutStepCount)];
while (SimulationNode.GetCompletionState(currentCompletionState, currentActions) == CompletionState.Incomplete &&
actionCount < actions.Length)
{
Expand Down Expand Up @@ -260,16 +259,18 @@ static bool NodesIncomplete(Node node, Stack<Node> path)
return !NodesIncomplete(rootNode, new());
}

public void Search(int iterations, ref int progress, CancellationToken token)
[SkipLocalsInit]
public unsafe void Search(int iterations, ref int progress, CancellationToken token)
{
var simulator = new Simulator(config.ActionPool, config.MaxStepCount, rootNode.State.State);
var random = rootNode.State.State.Input.Random;
var staleCounter = 0;
var i = 0;
Span<ActionType> actionBuffer = stackalloc ActionType[Math.Min(config.MaxStepCount, config.MaxRolloutStepCount)];
for (; i < iterations || MaxScore == 0; i++)
{
var selectedNode = Select();
var (endNode, score) = ExpandAndRollout(random, simulator, selectedNode);
var (endNode, score) = ExpandAndRollout(random, simulator, selectedNode, actionBuffer);
if (MaxScore == 0)
{
if (endNode == selectedNode)
Expand Down
2 changes: 1 addition & 1 deletion Solver/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Simulator(ActionType[] actionPool, int maxStepCount, SimulationState? fil
State = state;
pool = pool.Where(x => x.Item1.IsPossible(this));
}
actionPoolObjects = pool.OrderBy(x => x.x).ToArray();
actionPoolObjects = [.. pool.OrderBy(x => x.x)];
this.maxStepCount = maxStepCount;
}

Expand Down
2 changes: 1 addition & 1 deletion Solver/SolverConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public SolverConfig()
}

public static ActionType[] OptimizeActionPool(IEnumerable<ActionType> actions) =>
actions.Order().ToArray();
[.. actions.Order()];

public static readonly ActionType[] DeterministicActionPool = OptimizeActionPool(new[]
{
Expand Down

0 comments on commit b5ed17d

Please sign in to comment.