Add ability to set initialized sim state if needed

This commit is contained in:
Asriel Camora
2023-11-13 23:15:18 -08:00
parent b9c871f2b2
commit 980613970c
5 changed files with 28 additions and 24 deletions
+5 -3
View File
@@ -1018,7 +1018,7 @@ public sealed class MacroEditor : Window, IDisposable
private void DrawActionHotbars() private void DrawActionHotbars()
{ {
var sim = CreateSim(); var sim = CreateSim(State);
var imageSize = ImGui.GetFrameHeight() * 2; var imageSize = ImGui.GetFrameHeight() * 2;
var spacing = ImGui.GetStyle().ItemSpacing.Y; var spacing = ImGui.GetStyle().ItemSpacing.Y;
@@ -1283,8 +1283,7 @@ public sealed class MacroEditor : Window, IDisposable
} }
if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled))
{ {
var sim = CreateSim(); ImGui.SetTooltip($"{action.GetName(RecipeData!.ClassJob)}\n{actionBase.GetTooltip(CreateSim(lastState), true)}");
ImGui.SetTooltip($"{action.GetName(RecipeData!.ClassJob)}\n{actionBase.GetTooltip(sim, true)}");
} }
lastState = state; lastState = state;
} }
@@ -1653,6 +1652,9 @@ public sealed class MacroEditor : Window, IDisposable
private static Sim CreateSim() => private static Sim CreateSim() =>
Service.Configuration.ConditionRandomness ? new Sim() : new SimNoRandom(); Service.Configuration.ConditionRandomness ? new Sim() : new SimNoRandom();
private static Sim CreateSim(in SimulationState state) =>
Service.Configuration.ConditionRandomness ? new Sim() { State = state } : new SimNoRandom() { State = state };
private void AddStep(ActionType action, int index = -1, bool isSolver = false) private void AddStep(ActionType action, int index = -1, bool isSolver = false)
{ {
if (index < -1 || index >= Macro.Count) if (index < -1 || index >= Macro.Count)
+19 -17
View File
@@ -6,20 +6,22 @@ namespace Craftimizer.Simulator;
public class Simulator public class Simulator
{ {
protected SimulationState State; public SimulationState State { init => state = value; }
public SimulationInput Input => State.Input; private SimulationState state;
public ref int ActionCount => ref State.ActionCount;
public ref int StepCount => ref State.StepCount;
public ref int Progress => ref State.Progress;
public ref int Quality => ref State.Quality;
public ref int Durability => ref State.Durability;
public ref int CP => ref State.CP;
public ref Condition Condition => ref State.Condition;
public ref Effects ActiveEffects => ref State.ActiveEffects;
public ref ActionStates ActionStates => ref State.ActionStates;
public bool IsFirstStep => State.StepCount == 0; public SimulationInput Input => state.Input;
public ref int ActionCount => ref state.ActionCount;
public ref int StepCount => ref state.StepCount;
public ref int Progress => ref state.Progress;
public ref int Quality => ref state.Quality;
public ref int Durability => ref state.Durability;
public ref int CP => ref state.CP;
public ref Condition Condition => ref state.Condition;
public ref Effects ActiveEffects => ref state.ActiveEffects;
public ref ActionStates ActionStates => ref state.ActionStates;
public bool IsFirstStep => state.StepCount == 0;
public virtual CompletionState CompletionState { public virtual CompletionState CompletionState {
get get
@@ -37,8 +39,8 @@ public class Simulator
public (ActionResponse Response, SimulationState NewState) Execute(in SimulationState state, ActionType action) public (ActionResponse Response, SimulationState NewState) Execute(in SimulationState state, ActionType action)
{ {
State = state; this.state = state;
return (Execute(action), State); return (Execute(action), this.state);
} }
private ActionResponse Execute(ActionType action) private ActionResponse Execute(ActionType action)
@@ -67,16 +69,16 @@ public class Simulator
public (ActionResponse Response, SimulationState NewState, int FailedActionIdx) ExecuteMultiple(in SimulationState state, IEnumerable<ActionType> actions) public (ActionResponse Response, SimulationState NewState, int FailedActionIdx) ExecuteMultiple(in SimulationState state, IEnumerable<ActionType> actions)
{ {
State = state; this.state = state;
var i = 0; var i = 0;
foreach(var action in actions) foreach(var action in actions)
{ {
var resp = Execute(action); var resp = Execute(action);
if (resp != ActionResponse.UsedAction) if (resp != ActionResponse.UsedAction)
return (resp, State, i); return (resp, this.state, i);
i++; i++;
} }
return (ActionResponse.UsedAction, State, -1); return (ActionResponse.UsedAction, this.state, -1);
} }
[Pure] [Pure]
+1 -1
View File
@@ -20,7 +20,7 @@ public sealed class MCTS
public MCTS(in MCTSConfig config, in SimulationState state) public MCTS(in MCTSConfig config, in SimulationState state)
{ {
this.config = config; this.config = config;
var sim = new Simulator(config.MaxStepCount); var sim = new Simulator(config.MaxStepCount) { State = state };
rootNode = new(new( rootNode = new(new(
state, state,
null, null,
+2 -2
View File
@@ -240,7 +240,7 @@ public sealed class Solver : IDisposable
{ {
var actions = new List<ActionType>(); var actions = new List<ActionType>();
var state = State; var state = State;
var sim = new Simulator(Config.MaxStepCount); var sim = new Simulator(Config.MaxStepCount) { State = state };
while (true) while (true)
{ {
Token.ThrowIfCancellationRequested(); Token.ThrowIfCancellationRequested();
@@ -303,7 +303,7 @@ public sealed class Solver : IDisposable
{ {
var actions = new List<ActionType>(); var actions = new List<ActionType>();
var state = State; var state = State;
var sim = new Simulator(Config.MaxStepCount); var sim = new Simulator(Config.MaxStepCount) { State = state };
while (true) while (true)
{ {
Token.ThrowIfCancellationRequested(); Token.ThrowIfCancellationRequested();
+1 -1
View File
@@ -170,7 +170,7 @@ public class SimulatorTests
}, },
0, 4064, 15, 332); 0, 4064, 15, 332);
Assert.AreEqual(10, state.ActiveEffects.InnerQuiet); Assert.AreEqual(10, state.ActiveEffects.InnerQuiet);
Assert.IsTrue(ActionType.TrainedFinesse.Base().CanUse(new SimulatorNoRandom())); Assert.IsTrue(ActionType.TrainedFinesse.Base().CanUse(new SimulatorNoRandom() { State = state }));
} }
[TestMethod] [TestMethod]