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