Skip to content

Commit

Permalink
Move accepted actions to ActionSet
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Nov 4, 2023
1 parent ac109ae commit 2f727f7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 51 deletions.
6 changes: 3 additions & 3 deletions Craftimizer.Test/Solver/ActionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class ActionSetTests
[TestMethod]
public void TestAcceptedActions()
{
var actions = Craftimizer.Solver.Simulator.AcceptedActions;
var lut = Craftimizer.Solver.Simulator.AcceptedActionsLUT;
var actions = ActionSet.AcceptedActions;
var lut = ActionSet.AcceptedActionsLUT;

Assert.IsTrue(actions.Length <= 32);
foreach (var i in Enum.GetValues<ActionType>())
Expand Down Expand Up @@ -123,7 +123,7 @@ public void TestRandomIndex()
{
var action = set.SelectRandom(rng);

Assert.IsTrue(actions.Contains(action));
CollectionAssert.Contains(actions, action);

counts[action] = counts.GetValueOrDefault(action) + 1;
}
Expand Down
2 changes: 1 addition & 1 deletion Craftimizer/Windows/RecipeNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ private void CalculateBestMacrosTask(CancellationToken token)
var state = new SimulationState(input);
var config = Service.Configuration.SimulatorSolverConfig;
var mctsConfig = new MCTSConfig(config);
var simulator = new Solver.Simulator(state, mctsConfig.MaxStepCount);
var simulator = new SimulatorNoRandom(state);
List<Macro> macros = new(Service.Configuration.Macros);

token.ThrowIfCancellationRequested();
Expand Down
44 changes: 43 additions & 1 deletion Solver/ActionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ public struct ActionSet
{
private uint bits;

public static readonly ActionType[] AcceptedActions = new[]
{
ActionType.StandardTouchCombo,
ActionType.AdvancedTouchCombo,
ActionType.FocusedTouchCombo,
ActionType.FocusedSynthesisCombo,
ActionType.TrainedFinesse,
ActionType.PrudentSynthesis,
ActionType.Groundwork,
ActionType.AdvancedTouch,
ActionType.CarefulSynthesis,
ActionType.TrainedEye,
ActionType.DelicateSynthesis,
ActionType.PreparatoryTouch,
ActionType.Reflect,
ActionType.FocusedTouch,
ActionType.FocusedSynthesis,
ActionType.PrudentTouch,
ActionType.Manipulation,
ActionType.MuscleMemory,
ActionType.ByregotsBlessing,
ActionType.WasteNot2,
ActionType.BasicSynthesis,
ActionType.Innovation,
ActionType.GreatStrides,
ActionType.StandardTouch,
ActionType.Veneration,
ActionType.WasteNot,
ActionType.Observe,
ActionType.MastersMend,
ActionType.BasicTouch,
};

public static readonly int[] AcceptedActionsLUT;

static ActionSet()
{
AcceptedActionsLUT = new int[Enum.GetValues<ActionType>().Length];
for (var i = 0; i < AcceptedActions.Length; i++)
AcceptedActionsLUT[(byte)AcceptedActions[i]] = i;
}

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int FromAction(ActionType action)
Expand All @@ -22,7 +64,7 @@ private static int FromAction(ActionType action)
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ActionType ToAction(int index) => Simulator.AcceptedActions[index];
private static ActionType ToAction(int index) => AcceptedActions[index];
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint ToMask(ActionType action) => 1u << (FromAction(action) + 1);
Expand Down
48 changes: 2 additions & 46 deletions Solver/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Craftimizer.Solver;

public sealed class Simulator : SimulatorNoRandom
internal sealed class Simulator : SimulatorNoRandom
{
private readonly int maxStepCount;

Expand All @@ -25,50 +25,6 @@ public Simulator(SimulationState state, int maxStepCount) : base(state)
this.maxStepCount = maxStepCount;
}

public static readonly ActionType[] AcceptedActions = new[]
{
ActionType.StandardTouchCombo,
ActionType.AdvancedTouchCombo,
ActionType.FocusedTouchCombo,
ActionType.FocusedSynthesisCombo,
ActionType.TrainedFinesse,
ActionType.PrudentSynthesis,
ActionType.Groundwork,
ActionType.AdvancedTouch,
ActionType.CarefulSynthesis,
ActionType.TrainedEye,
ActionType.DelicateSynthesis,
ActionType.PreparatoryTouch,
ActionType.Reflect,
ActionType.FocusedTouch,
ActionType.FocusedSynthesis,
ActionType.PrudentTouch,
ActionType.Manipulation,
ActionType.MuscleMemory,
ActionType.ByregotsBlessing,
ActionType.WasteNot2,
ActionType.BasicSynthesis,
ActionType.Innovation,
ActionType.GreatStrides,
ActionType.StandardTouch,
ActionType.Veneration,
ActionType.WasteNot,
ActionType.Observe,
ActionType.MastersMend,
ActionType.BasicTouch,
};

public static readonly int[] AcceptedActionsLUT;

static Simulator()
{
AcceptedActionsLUT = new int[Enum.GetValues<ActionType>().Length];
for (var i = 0; i < AcceptedActionsLUT.Length; i++)
AcceptedActionsLUT[i] = -1;
for (var i = 0; i < AcceptedActions.Length; i++)
AcceptedActionsLUT[(byte)AcceptedActions[i]] = i;
}

// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L146
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -191,7 +147,7 @@ public ActionSet AvailableActionsHeuristic(bool strict)
return new();

var ret = new ActionSet();
foreach (var action in AcceptedActions)
foreach (var action in ActionSet.AcceptedActions)
if (CanUseAction(action, strict))
ret.AddAction(action);
return ret;
Expand Down

0 comments on commit 2f727f7

Please sign in to comment.