Simulator window changes
This commit is contained in:
@@ -13,7 +13,7 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
{
|
{
|
||||||
private const ImGuiWindowFlags WindowFlags = ImGuiWindowFlags.AlwaysAutoResize;
|
private const ImGuiWindowFlags WindowFlags = ImGuiWindowFlags.AlwaysAutoResize;
|
||||||
|
|
||||||
private static Configuration Configuration => Service.Configuration;
|
private static Configuration Config => Service.Configuration;
|
||||||
|
|
||||||
private Item Item { get; }
|
private Item Item { get; }
|
||||||
private bool IsExpert { get; }
|
private bool IsExpert { get; }
|
||||||
@@ -39,7 +39,7 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
Input = input;
|
Input = input;
|
||||||
ClassJob = classJob;
|
ClassJob = classJob;
|
||||||
Macro = macro;
|
Macro = macro;
|
||||||
MacroName = Macro?.Name ?? $"Macro {Configuration.Macros.Count + 1}";
|
MacroName = Macro?.Name ?? $"Macro {Config.Macros.Count + 1}";
|
||||||
Actions = new();
|
Actions = new();
|
||||||
ResetSimulator();
|
ResetSimulator();
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
|
|
||||||
private void ResetSimulator()
|
private void ResetSimulator()
|
||||||
{
|
{
|
||||||
Sim = Configuration.CreateSimulator(LatestState);
|
Sim = Config.CreateSimulator(LatestState);
|
||||||
ReexecuteAllActions();
|
ReexecuteAllActions();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using Craftimizer.Simulator;
|
|
||||||
using Craftimizer.Simulator.Actions;
|
using Craftimizer.Simulator.Actions;
|
||||||
using Dalamud.Interface.Windowing;
|
using Dalamud.Interface.Windowing;
|
||||||
using System;
|
using System;
|
||||||
|
|||||||
@@ -62,11 +62,11 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
|
|
||||||
private void DrawActions()
|
private void DrawActions()
|
||||||
{
|
{
|
||||||
var hideUnlearnedActions = Configuration.HideUnlearnedActions;
|
var hideUnlearnedActions = Config.HideUnlearnedActions;
|
||||||
if (ImGui.Checkbox("Show only learned actions", ref hideUnlearnedActions))
|
if (ImGui.Checkbox("Show only learned actions", ref hideUnlearnedActions))
|
||||||
{
|
{
|
||||||
Configuration.HideUnlearnedActions = hideUnlearnedActions;
|
Config.HideUnlearnedActions = hideUnlearnedActions;
|
||||||
Configuration.Save();
|
Config.Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
Sim.SetState(LatestState);
|
Sim.SetState(LatestState);
|
||||||
@@ -86,7 +86,7 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
var baseAction = action.Base();
|
var baseAction = action.Base();
|
||||||
|
|
||||||
var cannotUse = action.Level() > Input.Stats.Level || (action == ActionType.Manipulation && !Input.Stats.CanUseManipulation);
|
var cannotUse = action.Level() > Input.Stats.Level || (action == ActionType.Manipulation && !Input.Stats.CanUseManipulation);
|
||||||
if (cannotUse && Configuration.HideUnlearnedActions)
|
if (cannotUse && Config.HideUnlearnedActions)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var shouldNotUse = !baseAction.CanUse(Sim) || Sim.IsComplete;
|
var shouldNotUse = !baseAction.CanUse(Sim) || Sim.IsComplete;
|
||||||
@@ -313,12 +313,12 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
var quarterButtonSize = new Vector2(quarterWidth, ImGui.CalcTextSize("A").Y + ImGui.GetStyle().FramePadding.Y * 2);
|
var quarterButtonSize = new Vector2(quarterWidth, ImGui.CalcTextSize("A").Y + ImGui.GetStyle().FramePadding.Y * 2);
|
||||||
|
|
||||||
var conditionRandomnessText = "Condition Randomness";
|
var conditionRandomnessText = "Condition Randomness";
|
||||||
var conditionRandomness = Configuration.ConditionRandomness;
|
var conditionRandomness = Config.ConditionRandomness;
|
||||||
ImGui.BeginDisabled(!CanModifyActions);
|
ImGui.BeginDisabled(!CanModifyActions);
|
||||||
if (ImGui.Checkbox(conditionRandomnessText, ref conditionRandomness))
|
if (ImGui.Checkbox(conditionRandomnessText, ref conditionRandomness))
|
||||||
{
|
{
|
||||||
Configuration.ConditionRandomness = conditionRandomness;
|
Config.ConditionRandomness = conditionRandomness;
|
||||||
Configuration.Save();
|
Config.Save();
|
||||||
ResetSimulator();
|
ResetSimulator();
|
||||||
}
|
}
|
||||||
ImGui.EndDisabled();
|
ImGui.EndDisabled();
|
||||||
@@ -355,15 +355,15 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
{
|
{
|
||||||
Macro.Name = MacroName;
|
Macro.Name = MacroName;
|
||||||
Macro.Actions = Actions.Select(a => a.Action).ToList();
|
Macro.Actions = Actions.Select(a => a.Action).ToList();
|
||||||
Configuration.Save();
|
Config.Save();
|
||||||
}
|
}
|
||||||
ImGui.SameLine();
|
ImGui.SameLine();
|
||||||
}
|
}
|
||||||
if (ImGui.Button("Save New", Macro == null ? halfButtonSize : quarterButtonSize))
|
if (ImGui.Button("Save New", Macro == null ? halfButtonSize : quarterButtonSize))
|
||||||
{
|
{
|
||||||
Macro = new() { Name = MacroName, Actions = Actions.Select(a => a.Action).ToList() };
|
Macro = new() { Name = MacroName, Actions = Actions.Select(a => a.Action).ToList() };
|
||||||
Configuration.Macros.Add(Macro);
|
Config.Macros.Add(Macro);
|
||||||
Configuration.Save();
|
Config.Save();
|
||||||
}
|
}
|
||||||
ImGui.SameLine();
|
ImGui.SameLine();
|
||||||
if (ImGui.Button("Reset", halfButtonSize))
|
if (ImGui.Button("Reset", halfButtonSize))
|
||||||
@@ -377,9 +377,11 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
string buttonText;
|
string buttonText;
|
||||||
string tooltipText;
|
string tooltipText;
|
||||||
bool isEnabled;
|
bool isEnabled;
|
||||||
if (!SolverTask.IsCompleted)
|
var taskCompleted = SolverTask?.IsCompleted ?? true;
|
||||||
|
var taskCancelled = SolverTaskToken?.IsCancellationRequested ?? false;
|
||||||
|
if (!taskCompleted)
|
||||||
{
|
{
|
||||||
if (SolverTaskToken.IsCancellationRequested)
|
if (taskCancelled)
|
||||||
{
|
{
|
||||||
buttonText = "Cancelling...";
|
buttonText = "Cancelling...";
|
||||||
tooltipText = "Cancelling macro generation. This shouldn't take long.";
|
tooltipText = "Cancelling macro generation. This shouldn't take long.";
|
||||||
@@ -412,22 +414,18 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
ImGui.BeginDisabled(!isEnabled);
|
ImGui.BeginDisabled(!isEnabled);
|
||||||
if (ImGui.Button(buttonText, buttonSize))
|
if (ImGui.Button(buttonText, buttonSize))
|
||||||
{
|
{
|
||||||
if (!SolverTask.IsCompleted)
|
if (!taskCompleted)
|
||||||
{
|
{
|
||||||
if (!SolverTaskToken.IsCancellationRequested)
|
if (!taskCancelled)
|
||||||
{
|
SolverTaskToken?.Cancel();
|
||||||
SolverTaskToken.Cancel();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (SolverActionsChanged)
|
if (SolverActionsChanged)
|
||||||
{
|
{
|
||||||
if (state.HasValue)
|
if (state.HasValue)
|
||||||
{
|
|
||||||
SolveMacro(state.Value);
|
SolveMacro(state.Value);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Actions.RemoveRange(SolverInitialActionCount, Actions.Count - SolverInitialActionCount);
|
Actions.RemoveRange(SolverInitialActionCount, Actions.Count - SolverInitialActionCount);
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
using Craftimizer.Simulator;
|
using Craftimizer.Simulator;
|
||||||
using Craftimizer.Simulator.Actions;
|
using Craftimizer.Simulator.Actions;
|
||||||
using Craftimizer.Solver.Crafty;
|
|
||||||
using Dalamud.Interface.Windowing;
|
using Dalamud.Interface.Windowing;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -12,13 +10,13 @@ namespace Craftimizer.Plugin.Windows;
|
|||||||
|
|
||||||
public sealed partial class Simulator : Window, IDisposable
|
public sealed partial class Simulator : Window, IDisposable
|
||||||
{
|
{
|
||||||
private Task SolverTask { get; set; } = Task.CompletedTask;
|
private Task? SolverTask { get; set; }
|
||||||
private CancellationTokenSource SolverTaskToken { get; set; } = new();
|
private CancellationTokenSource? SolverTaskToken { get; set; }
|
||||||
private ConcurrentQueue<ActionType> SolverActionQueue { get; } = new();
|
private ConcurrentQueue<ActionType> SolverActionQueue { get; } = new();
|
||||||
private int SolverInitialActionCount { get; set; }
|
private int SolverInitialActionCount { get; set; }
|
||||||
private bool SolverActionsChanged { get; set; } = true;
|
private bool SolverActionsChanged { get; set; } = true;
|
||||||
|
|
||||||
private bool CanModifyActions => SolverTask.IsCompleted;
|
private bool CanModifyActions => SolverTask?.IsCompleted ?? true;
|
||||||
|
|
||||||
private void OnActionsChanged()
|
private void OnActionsChanged()
|
||||||
{
|
{
|
||||||
@@ -49,44 +47,50 @@ public sealed partial class Simulator : Window, IDisposable
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SolveMacro(SimulationState solverState)
|
private void StopSolveMacro()
|
||||||
{
|
{
|
||||||
|
if (SolverTask == null || SolverTaskToken == null)
|
||||||
|
return;
|
||||||
|
|
||||||
if (!SolverTask.IsCompleted)
|
if (!SolverTask.IsCompleted)
|
||||||
{
|
|
||||||
SolverTaskToken.Cancel();
|
SolverTaskToken.Cancel();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SolverTaskToken.Dispose();
|
||||||
|
SolverTask.Dispose();
|
||||||
|
|
||||||
|
SolverTask = null;
|
||||||
|
SolverTaskToken = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevents the quality bar from being unfair between solves
|
private void SolveMacro(SimulationState solverState)
|
||||||
if (Configuration.ConditionRandomness)
|
|
||||||
{
|
{
|
||||||
Configuration.ConditionRandomness = false;
|
StopSolveMacro();
|
||||||
Configuration.Save();
|
|
||||||
|
// Prevents the quality bar from being unfair between solves
|
||||||
|
if (Config.ConditionRandomness)
|
||||||
|
{
|
||||||
|
Config.ConditionRandomness = false;
|
||||||
|
Config.Save();
|
||||||
|
|
||||||
ResetSimulator();
|
ResetSimulator();
|
||||||
}
|
}
|
||||||
|
|
||||||
SolverActionsChanged = false;
|
SolverActionsChanged = false;
|
||||||
|
|
||||||
SolverTaskToken.Dispose();
|
|
||||||
SolverTask.Dispose();
|
|
||||||
SolverActionQueue.Clear();
|
SolverActionQueue.Clear();
|
||||||
|
|
||||||
SolverInitialActionCount = Actions.Count;
|
SolverInitialActionCount = Actions.Count;
|
||||||
SolverTaskToken = new();
|
SolverTaskToken = new();
|
||||||
Func<SolverConfig, SimulationState, Action<ActionType>?, CancellationToken, SolverSolution> solverMethod = Configuration.SolverAlgorithm switch
|
SolverTask = Task.Run(() => Config.SolverAlgorithm.Invoke(Config.SolverConfig, solverState, SolverActionQueue.Enqueue, SolverTaskToken.Token));
|
||||||
{
|
|
||||||
SolverAlgorithm.Oneshot => Solver.Crafty.Solver.SearchOneshot,
|
|
||||||
SolverAlgorithm.OneshotForked => Solver.Crafty.Solver.SearchOneshotForked,
|
|
||||||
SolverAlgorithm.Stepwise => Solver.Crafty.Solver.SearchStepwise,
|
|
||||||
SolverAlgorithm.StepwiseForked => Solver.Crafty.Solver.SearchStepwiseForked,
|
|
||||||
SolverAlgorithm.StepwiseFurcated or _ => Solver.Crafty.Solver.SearchStepwiseFurcated,
|
|
||||||
};
|
|
||||||
SolverTask = Task.Run(() => solverMethod(Configuration.SolverConfig, solverState, SolverActionQueue.Enqueue, SolverTaskToken.Token));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
SolverTask.Dispose();
|
StopSolveMacro();
|
||||||
SolverTaskToken.Dispose();
|
SolverTask?.Wait();
|
||||||
|
SolverTask?.Dispose();
|
||||||
|
SolverTaskToken?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user