Skip to content

Commit

Permalink
Optimizations (from @wolfcomp)
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Mar 15, 2024
1 parent ea963f9 commit 3223bdc
Show file tree
Hide file tree
Showing 43 changed files with 669 additions and 332 deletions.
3 changes: 2 additions & 1 deletion Craftimizer/Windows/MacroEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,7 @@ private void DrawActionHotbars()
using var _color3 = ImRaii.PushColor(ImGuiCol.ButtonHovered, Vector4.Zero);
using var _color2 = ImRaii.PushColor(ImGuiCol.ButtonActive, Vector4.Zero);
using var _alpha = ImRaii.PushStyle(ImGuiStyleVar.DisabledAlpha, ImGui.GetStyle().DisabledAlpha * .5f);
var cost = 0;
foreach (var category in Enum.GetValues<ActionCategory>())
{
if (category == ActionCategory.Combo)
Expand All @@ -952,7 +953,7 @@ private void DrawActionHotbars()
if (i < itemCount)
{
var actionBase = actions[i].Base();
var canUse = actionBase.CanUse(sim);
var canUse = actionBase.CanUse(sim, ref cost);
if (ImGui.ImageButton(actions[i].GetIcon(RecipeData!.ClassJob).ImGuiHandle, new(imageSize), default, Vector2.One, 0, default, !canUse ? new(1, 1, 1, ImGui.GetStyle().DisabledAlpha) : Vector4.One))
AddStep(actions[i]);
if (!canUse &&
Expand Down
26 changes: 20 additions & 6 deletions Simulator/Actions/AdvancedTouch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ namespace Craftimizer.Simulator.Actions;

internal sealed class AdvancedTouch : BaseAction
{
public override ActionCategory Category => ActionCategory.Quality;
public override int Level => 84;
public override uint ActionId => 100411;
public int CostDefault = 46;
public int CostOptimal = 18;
public int EfficiencyDefault = 150;

public override bool IncreasesQuality => true;
public AdvancedTouch()
{
Category = ActionCategory.Quality;
Level = 84;
ActionId = 100411;
IncreasesQuality = true;
}

public override int CPCost(Simulator s) => s.ActionStates.TouchComboIdx == 2 ? 18 : 46;
public override int Efficiency(Simulator s) => 150;

public override void CPCost(Simulator s, ref int cost)
{
cost = s.ActionStates.TouchComboIdx == 2 ? CostOptimal : CostDefault;
}

public override void Efficiency(Simulator s, ref int eff)
{
eff = EfficiencyDefault;
}
}
2 changes: 0 additions & 2 deletions Simulator/Actions/AdvancedTouchCombo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ internal sealed class AdvancedTouchCombo : BaseComboAction<StandardTouchCombo, A
{
public override ActionType ActionTypeA => ActionType.StandardTouchCombo;
public override ActionType ActionTypeB => ActionType.AdvancedTouch;

public override int CPCost(Simulator s) => 18 * 3;
}
83 changes: 52 additions & 31 deletions Simulator/Actions/BaseAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,34 @@ public abstract class BaseAction
// Non-instanced properties

// Metadata
public abstract ActionCategory Category { get; }
public abstract int Level { get; }
public ActionCategory Category;

public int Level;
// Doesn't matter from which class, we'll use the sheet to extrapolate the rest
public abstract uint ActionId { get; }
public uint ActionId;
// Seconds
public virtual int MacroWaitTime => 3;
public int MacroWaitTime = 3;

// Action properties
public virtual bool IncreasesProgress => false;
public virtual bool IncreasesQuality => false;
public virtual int DurabilityCost => 10;
public virtual bool IncreasesStepCount => true;
public bool IncreasesProgress;
public bool IncreasesQuality;
public int DurabilityCost = 10;
public bool IncreasesStepCount = true;
public int EfficiencyFactor;
public float SuccessRateFactor = 1;

// Instanced properties
public abstract int CPCost(Simulator s);
public virtual int Efficiency(Simulator s) => 0;
public virtual float SuccessRate(Simulator s) => 1f;
public abstract void CPCost(Simulator s, ref int cost);

public virtual void Efficiency(Simulator s, ref int eff)
{
eff = EfficiencyFactor;
}

public virtual void SuccessRate(Simulator s, ref float success)
{
success = SuccessRateFactor;
}

// Return true if it can be in the action pool now or in the future
// e.g. if Heart and Soul is already used, it is impossible to use it again
Expand All @@ -33,18 +44,23 @@ public virtual bool IsPossible(Simulator s) =>

// Return true if it can be used now
// This already assumes that IsPossible returns true *at some point before*
public virtual bool CouldUse(Simulator s) =>
s.CP >= CPCost(s);
public virtual bool CouldUse(Simulator s, ref int cost)
{
CPCost(s, ref cost);
return s.CP >= cost;
}

public bool CanUse(Simulator s) =>
IsPossible(s) && CouldUse(s);
public bool CanUse(Simulator s, ref int cost) =>
IsPossible(s) && CouldUse(s, ref cost);

public virtual void Use(Simulator s)
public virtual void Use(Simulator s, ref int cost, ref float success, ref int eff)
{
if (s.RollSuccess(SuccessRate(s)))
UseSuccess(s);
SuccessRate(s, ref success);
if (s.RollSuccess(success))
UseSuccess(s, ref eff);
CPCost(s, ref cost);

s.ReduceCP(CPCost(s));
s.ReduceCP(cost);
s.ReduceDurability(DurabilityCost);

if (s.Durability > 0)
Expand All @@ -63,38 +79,43 @@ public virtual void Use(Simulator s)
s.ActiveEffects.DecrementDuration();
}

public virtual void UseSuccess(Simulator s)
public virtual void UseSuccess(Simulator s, ref int eff)
{
if (Efficiency(s) != 0f)
Efficiency(s, ref eff);
if (eff != 0)
{
if (IncreasesProgress)
s.IncreaseProgress(Efficiency(s));
s.IncreaseProgress(eff);
if (IncreasesQuality)
s.IncreaseQuality(Efficiency(s));
s.IncreaseQuality(eff);
}
}

public virtual string GetTooltip(Simulator s, bool addUsability)
{
var builder = new StringBuilder();
if (addUsability && !CanUse(s))
int cost = 0;
float success = 1f;
if (addUsability && !CanUse(s, ref cost))
builder.AppendLine($"Cannot Use");
builder.AppendLine($"Level {Level}");
if (CPCost(s) != 0)
builder.AppendLine($"-{s.CalculateCPCost(CPCost(s))} CP");
if (cost != 0)
builder.AppendLine($"-{s.CalculateCPCost(cost)} CP");
if (DurabilityCost != 0)
builder.AppendLine($"-{s.CalculateDurabilityCost(DurabilityCost)} Durability");
if (Efficiency(s) != 0)
Efficiency(s, ref cost);
if (cost != 0)
{
if (IncreasesProgress)
builder.AppendLine($"+{s.CalculateProgressGain(Efficiency(s))} Progress");
builder.AppendLine($"+{s.CalculateProgressGain(cost)} Progress");
if (IncreasesQuality)
builder.AppendLine($"+{s.CalculateQualityGain(Efficiency(s))} Quality");
builder.AppendLine($"+{s.CalculateQualityGain(cost)} Quality");
}
if (!IncreasesStepCount)
builder.AppendLine($"Does Not Increase Step Count");
if (SuccessRate(s) != 1f)
builder.AppendLine($"{s.CalculateSuccessRate(SuccessRate(s)) * 100:##}% Success Rate");
SuccessRate(s, ref success);
if (Math.Abs(success - 1f) > float.Epsilon)
builder.AppendLine($"{s.CalculateSuccessRate(success) * 100:##}% Success Rate");
return builder.ToString();
}
}
15 changes: 9 additions & 6 deletions Simulator/Actions/BaseBuffAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ namespace Craftimizer.Simulator.Actions;

internal abstract class BaseBuffAction : BaseAction
{
// Non-instanced properties
public abstract EffectType Effect { get; }
public virtual byte Duration => 1;
public override int MacroWaitTime => 2;
public BaseBuffAction()

Check warning on line 7 in Simulator/Actions/BaseBuffAction.cs

View workflow job for this annotation

GitHub Actions / build

Abstract types should not have public or internal constructors (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0017.md)

Check warning on line 7 in Simulator/Actions/BaseBuffAction.cs

View workflow job for this annotation

GitHub Actions / build

Abstract types should not have public or internal constructors (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0017.md)

Check warning on line 7 in Simulator/Actions/BaseBuffAction.cs

View workflow job for this annotation

GitHub Actions / bench

Abstract types should not have public or internal constructors (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0017.md)
{
MacroWaitTime = 2;
DurabilityCost = 0;
}

public sealed override int DurabilityCost => 0;
// Non-instanced properties
public EffectType Effect;
public int Duration = 1;

public override void UseSuccess(Simulator s) =>
public override void UseSuccess(Simulator s, ref int eff) =>
s.AddEffect(Effect, Duration);

public override string GetTooltip(Simulator s, bool addUsability)
Expand Down
9 changes: 6 additions & 3 deletions Simulator/Actions/BaseComboAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ public abstract class BaseComboAction : BaseAction
public abstract ActionType ActionTypeA { get; }
public abstract ActionType ActionTypeB { get; }

public sealed override ActionCategory Category => ActionCategory.Combo;
public BaseComboAction()

Check warning on line 8 in Simulator/Actions/BaseComboAction.cs

View workflow job for this annotation

GitHub Actions / build

Abstract types should not have public or internal constructors (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0017.md)

Check warning on line 8 in Simulator/Actions/BaseComboAction.cs

View workflow job for this annotation

GitHub Actions / build

Abstract types should not have public or internal constructors (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0017.md)

Check warning on line 8 in Simulator/Actions/BaseComboAction.cs

View workflow job for this annotation

GitHub Actions / bench

Abstract types should not have public or internal constructors (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0017.md)
{
Category = ActionCategory.Combo;
}

protected bool BaseCouldUse(Simulator s) =>
base.CouldUse(s);
protected bool BaseCouldUse(Simulator s, ref int cost) =>
base.CouldUse(s, ref cost);

private static bool VerifyDurability2(int durabilityA, int durability, in Effects effects)
{
Expand Down
31 changes: 20 additions & 11 deletions Simulator/Actions/BaseComboActionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ namespace Craftimizer.Simulator.Actions;
protected static readonly A ActionA = new();
protected static readonly B ActionB = new();

public override int Level => ActionB.Level;
public override uint ActionId => ActionB.ActionId;

public override bool IncreasesProgress => ActionA.IncreasesProgress || ActionB.IncreasesProgress;
public override bool IncreasesQuality => ActionA.IncreasesQuality || ActionB.IncreasesQuality;
protected BaseComboAction()
{
Level = ActionB.Level;
ActionId = ActionB.ActionId;
IncreasesProgress = ActionA.IncreasesProgress || ActionB.IncreasesProgress;
IncreasesQuality = ActionA.IncreasesQuality || ActionB.IncreasesQuality;
}

public override int CPCost(Simulator s) => ActionA.CPCost(s) + ActionB.CPCost(s);
public override void CPCost(Simulator s, ref int cost)
{
var costTmp = 0;
ActionA.CPCost(s, ref costTmp);
cost += costTmp;
ActionB.CPCost(s, ref costTmp);
cost += costTmp;
}

public override bool IsPossible(Simulator s) => ActionA.IsPossible(s) && ActionB.IsPossible(s);

public override bool CouldUse(Simulator s) =>
BaseCouldUse(s) && VerifyDurability2(s, ActionA.DurabilityCost);
public override bool CouldUse(Simulator s, ref int cost) =>
BaseCouldUse(s, ref cost) && VerifyDurability2(s, ActionA.DurabilityCost);

public override void Use(Simulator s)
public override void Use(Simulator s, ref int cost, ref float success, ref int eff)
{
ActionA.Use(s);
ActionB.Use(s);
ActionA.Use(s, ref cost, ref success, ref eff);
ActionB.Use(s, ref cost, ref success, ref eff);
}

public override string GetTooltip(Simulator s, bool addUsability) =>
Expand Down
24 changes: 18 additions & 6 deletions Simulator/Actions/BasicSynthesis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ namespace Craftimizer.Simulator.Actions;

internal sealed class BasicSynthesis : BaseAction
{
public override ActionCategory Category => ActionCategory.Synthesis;
public override int Level => 1;
public override uint ActionId => 100001;
public int CP;

Check warning on line 5 in Simulator/Actions/BasicSynthesis.cs

View workflow job for this annotation

GitHub Actions / build

Field 'BasicSynthesis.CP' is never assigned to, and will always have its default value 0

Check warning on line 5 in Simulator/Actions/BasicSynthesis.cs

View workflow job for this annotation

GitHub Actions / build

Field 'BasicSynthesis.CP' is never assigned to, and will always have its default value 0

Check warning on line 5 in Simulator/Actions/BasicSynthesis.cs

View workflow job for this annotation

GitHub Actions / bench

Field 'BasicSynthesis.CP' is never assigned to, and will always have its default value 0
public int EfficiencyNormal = 100;
public int EfficiencyGood = 120;

public override bool IncreasesProgress => true;
public BasicSynthesis()
{
Category = ActionCategory.Synthesis;
IncreasesProgress = true;
ActionId = 100001;
Level = 1;
}

public override int CPCost(Simulator s) => 0;
public override void CPCost(Simulator s, ref int cost)
{
cost = CP;
}
// Basic Synthesis Mastery Trait
public override int Efficiency(Simulator s) => s.Input.Stats.Level >= 31 ? 120 : 100;
public override void Efficiency(Simulator s, ref int eff)
{
eff = s.Input.Stats.Level >= 31 ? EfficiencyGood : EfficiencyNormal;
}
}
24 changes: 18 additions & 6 deletions Simulator/Actions/BasicTouch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ namespace Craftimizer.Simulator.Actions;

internal sealed class BasicTouch : BaseAction
{
public override ActionCategory Category => ActionCategory.Quality;
public override int Level => 5;
public override uint ActionId => 100002;
public int CP = 18;
public int eff = 100;

public override bool IncreasesQuality => true;
public BasicTouch()
{
Category = ActionCategory.Quality;
Level = 5;
ActionId = 100002;
IncreasesQuality = true;
}

public override int CPCost(Simulator s) => 18;
public override int Efficiency(Simulator s) => 100;
public override void CPCost(Simulator s, ref int cost)
{
cost = CP;
}

public override void Efficiency(Simulator s, ref int eff)
{
eff = this.eff;
}
}
29 changes: 20 additions & 9 deletions Simulator/Actions/ByregotsBlessing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@ namespace Craftimizer.Simulator.Actions;

internal sealed class ByregotsBlessing : BaseAction
{
public override ActionCategory Category => ActionCategory.Quality;
public override int Level => 50;
public override uint ActionId => 100339;
public int CP = 24;

public override bool IncreasesQuality => true;
public ByregotsBlessing()
{
Category = ActionCategory.Quality;
Level = 50;
ActionId = 100339;
IncreasesQuality = true;
}

public override int CPCost(Simulator s) => 24;
public override int Efficiency(Simulator s) => 100 + (20 * s.GetEffectStrength(EffectType.InnerQuiet));
public override void CPCost(Simulator s, ref int cost)
{
cost = CP;
}

public override void Efficiency(Simulator s, ref int eff)
{
eff = 100 + (20 * s.GetEffectStrength(EffectType.InnerQuiet));
}

public override bool CouldUse(Simulator s) => s.HasEffect(EffectType.InnerQuiet) && base.CouldUse(s);
public override bool CouldUse(Simulator s, ref int cost) => s.HasEffect(EffectType.InnerQuiet) && base.CouldUse(s, ref cost);

public override void UseSuccess(Simulator s)
public override void UseSuccess(Simulator s, ref int eff)
{
base.UseSuccess(s);
base.UseSuccess(s, ref eff);
s.RemoveEffect(EffectType.InnerQuiet);
}
}
Loading

1 comment on commit 3223bdc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 3223bdc Previous: ea963f9 Ratio
Craftimizer.Benchmark.Bench.Solve(State: 8C468532, Config: 99B38877) 1335583026.6666667 ns (± 10451109.65820422)
Craftimizer.Benchmark.Bench.Solve(State: 8C468532, Config: 99B38877) 961882553.8461539 ns (± 4843592.93665618)
Craftimizer.Benchmark.Bench.Solve(State: D472A5FE, Config: 99B38877) 948700664.2857143 ns (± 9059448.406422235)
Craftimizer.Benchmark.Bench.Solve(State: D472A5FE, Config: 99B38877) 708124692.8571428 ns (± 6881848.270348519)

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.