Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: all options can be add some conditions!
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiDog1998 committed Oct 21, 2023
1 parent 37ac0d3 commit 5cbafc8
Show file tree
Hide file tree
Showing 32 changed files with 553 additions and 411 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RotationSolver.ActionSequencer;
namespace RotationSolver.Basic.Configuration.Conditions;

internal class ActionCondition : DelayCondition
{
Expand Down
4 changes: 2 additions & 2 deletions RotationSolver.Basic/Configuration/Conditions/ConditionSet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RotationSolver.ActionSequencer;
namespace RotationSolver.Basic.Configuration.Conditions;

internal class ConditionSet : DelayCondition
{
Expand All @@ -21,7 +21,7 @@ public override bool IsTrueInside(ICustomRotation rotation)
}
}

internal enum LogicalType: byte
internal enum LogicalType : byte
{
And,
Or,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ECommons.GameHelpers;

namespace RotationSolver.ActionSequencer;
namespace RotationSolver.Basic.Configuration.Conditions;

internal abstract class DelayCondition : ICondition
{
Expand All @@ -11,7 +11,7 @@ internal abstract class DelayCondition : ICondition

public bool IsTrue(ICustomRotation rotation)
{
if(_delay.GetRange == null)
if (_delay.GetRange == null)
{
_delay = new(() => (DelayMin, DelayMax));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RotationSolver.ActionSequencer;
namespace RotationSolver.Basic.Configuration.Conditions;

internal interface ICondition
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json.Linq;

namespace RotationSolver.ActionSequencer;
namespace RotationSolver.Basic.Configuration.Conditions;

internal class IConditionConverter : JsonCreationConverter<ICondition>
{
Expand Down Expand Up @@ -38,7 +38,7 @@ private static bool FieldExists(string fieldName, JObject jObject)
}
}

public abstract class JsonCreationConverter<T> : JsonConverter
internal abstract class JsonCreationConverter<T> : JsonConverter
{
protected abstract T Create(JObject jObject);

Expand Down
123 changes: 123 additions & 0 deletions RotationSolver.Basic/Configuration/Conditions/MajorConditionSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using ECommons.DalamudServices;
using ECommons.ExcelServices;

namespace RotationSolver.Basic.Configuration.Conditions;

internal class MajorConditionSet
{
const string conditionName = "Unnamed";

[JsonIgnore]
public bool IsUnnamed => Name == conditionName;

/// <summary>
/// Key for action id.
/// </summary>
public Dictionary<Job, Dictionary<uint, ConditionSet>> Conditions { get; } = new();

[JsonIgnore]
public Dictionary<uint, ConditionSet> ConditionDict
{
get
{
if (!Conditions.TryGetValue(DataCenter.Job, out var dict))
{
dict = Conditions[DataCenter.Job] = new();
}
return dict;
}
}

public Dictionary<Job, Dictionary<uint, ConditionSet>> DisabledConditions { get; } = new();

[JsonIgnore]
public Dictionary<uint, ConditionSet> DisableConditionDict
{
get
{
if (!DisabledConditions.TryGetValue(DataCenter.Job, out var dict))
{
dict = DisabledConditions[DataCenter.Job] = new();
}
return dict;
}
}

public Dictionary<PluginConfigBool, ConditionSet> ForceEnableConditions { get; private set; }
= new();

public Dictionary<PluginConfigBool, ConditionSet> ForceDisableConditions { get; private set; }
= new();

public string Name;

public ConditionSet GetCondition(uint id)
{
if (!ConditionDict.TryGetValue(id, out var conditionSet))
{
conditionSet = ConditionDict[id] = new ConditionSet();
}
return conditionSet;
}

public ConditionSet GetDisabledCondition(uint id)
{
if (!DisableConditionDict.TryGetValue(id, out var conditionSet))
{
conditionSet = DisableConditionDict[id] = new ConditionSet();
}
return conditionSet;
}

public ConditionSet GetEnableCondition(PluginConfigBool config)
{
if (!ForceEnableConditions.TryGetValue(config, out var conditionSet))
{
conditionSet = ForceEnableConditions[config] = new ConditionSet();
}
return conditionSet;
}

public ConditionSet GetDisableCondition(PluginConfigBool config)
{
if (!ForceDisableConditions.TryGetValue(config, out var conditionSet))
{
conditionSet = ForceDisableConditions[config] = new ConditionSet();
}
return conditionSet;
}

public MajorConditionSet(string name = conditionName)
{
Name = name;
}

public void Save(string folder)
{
if (!Directory.Exists(folder)) return;
var path = Path.Combine(folder, Name + ".json");

var str = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(path, str);
}

public static MajorConditionSet[] Read(string folder)
{
if (!Directory.Exists(folder)) return Array.Empty<MajorConditionSet>();

return Directory.EnumerateFiles(folder, "*.json").Select(p =>
{
var str = File.ReadAllText(p);

try
{
return JsonConvert.DeserializeObject<MajorConditionSet>(str, new IConditionConverter());
}
catch
{
Svc.Chat.Print($"Failed to load the conditionSet from {p}");
return null;
}
}).Where(set => set != null && !string.IsNullOrEmpty(set.Name)).ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RotationSolver.ActionSequencer;
namespace RotationSolver.Basic.Configuration.Conditions;

internal class RotationCondition : DelayCondition
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using ECommons.GameHelpers;
using Lumina.Excel.GeneratedSheets;

namespace RotationSolver.ActionSequencer;
namespace RotationSolver.Basic.Configuration.Conditions;

internal class TargetCondition : DelayCondition
{
Expand All @@ -27,7 +27,7 @@ public override bool IsTrueInside(ICustomRotation rotation)
if (_action != null)
{
_action.CanUse(out _, CanUseOption.EmptyOrSkipCombo | CanUseOption.MustUse
| CanUseOption.IgnoreTarget);
| CanUseOption.IgnoreTarget);
tar = _action.Target;
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ECommons.GameHelpers;
using RotationSolver.Basic.Traits;

namespace RotationSolver.ActionSequencer;
namespace RotationSolver.Basic.Configuration.Conditions;

internal class TraitCondition : DelayCondition
{
Expand Down
60 changes: 53 additions & 7 deletions RotationSolver.Basic/Configuration/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using Dalamud.Utility;
using ECommons.DalamudServices;
using ECommons.ExcelServices;
using FFXIVClientStructs.STD;
using RotationSolver.Basic.Configuration.Conditions;
using System;

namespace RotationSolver.Basic.Configuration;

Expand All @@ -20,7 +23,7 @@ public static PluginConfig Create()
}

[JsonProperty]
private Dictionary<Job, JobConfig> _jobsConfig = new();
private readonly Dictionary<Job, JobConfig> _jobsConfig = new();
public GlobalConfig GlobalConfig { get; private set; } = new();
public int Version { get; set; } = 7;

Expand All @@ -34,8 +37,26 @@ public int GetValue(PluginConfigInt config)
=> GlobalConfig.Ints.GetValue(config);

public bool GetValue(PluginConfigBool config)
{
var rotation = DataCenter.RightNowRotation;
var set = DataCenter.RightSet;
if (rotation != null && set != null)
{
if (GetEnableBoolRaw(config) && set.GetEnableCondition(config).IsTrue(rotation)) return true;
if (GetDisableBoolRaw(config) && set.GetDisableCondition(config).IsTrue(rotation)) return false;
}

return GetBoolRaw(config);
}

public bool GetBoolRaw(PluginConfigBool config)
=> GlobalConfig.Bools.GetValue(config);

public bool GetDisableBoolRaw(PluginConfigBool config)
=> GlobalConfig.ForcedDisableBools.GetValue(config);
public bool GetEnableBoolRaw(PluginConfigBool config)
=> GlobalConfig.ForcedEnableBools.GetValue(config);

public float GetValue(PluginConfigFloat config)
=> GlobalConfig.Floats.GetValue(config);

Expand All @@ -51,7 +72,7 @@ public float GetDefault(Job job, JobConfigFloat config)
public int GetDefault(PluginConfigInt config)
=> GlobalConfig.Ints.GetDefault(config);

public bool GetDefault(PluginConfigBool config)
public bool GetBoolRawDefault(PluginConfigBool config)
=> GlobalConfig.Bools.GetDefault(config);

public float GetDefault(PluginConfigFloat config)
Expand Down Expand Up @@ -101,9 +122,12 @@ public void SetValue(PluginConfigInt config, int value)
}
GlobalConfig.Ints.SetValue(config, value);
}
public void SetValue(PluginConfigBool config, bool value)
public void SetBoolRaw(PluginConfigBool config, bool value)
=> GlobalConfig.Bools.SetValue(config, value);

public void SetDisableBoolRaw(PluginConfigBool config, bool value)
=> GlobalConfig.ForcedDisableBools.SetValue(config, value);
public void SetEnableBoolRaw(PluginConfigBool config, bool value)
=> GlobalConfig.ForcedEnableBools.SetValue(config, value);
public void SetValue(PluginConfigFloat config, float value)
{
var attr = config.GetAttribute<DefaultAttribute>();
Expand Down Expand Up @@ -177,6 +201,9 @@ [Serializable] public class GlobalConfig
{
public DictionConfig<PluginConfigInt, int> Ints { get; private set; } = new();
public DictionConfig<PluginConfigBool, bool> Bools { get; private set; } = new();
public DictionConfig<PluginConfigBool, bool> ForcedEnableBools { get; private set; } = new(defaultGetter: cmd => false);
public DictionConfig<PluginConfigBool, bool> ForcedDisableBools { get; private set; } = new(defaultGetter: cmd => false);

public DictionConfig<PluginConfigFloat, float> Floats { get; private set; } = new();
public DictionConfig<PluginConfigVector4, Vector4> Vectors { get; private set; } = new(new()
{
Expand Down Expand Up @@ -456,13 +483,19 @@ public DefaultAttribute(object @default, object min = null, object max = null)
[Serializable] public class DictionConfig<TConfig, TValue> where TConfig : struct, Enum
{
[JsonProperty]
private Dictionary<TConfig, TValue> configs = new ();
private readonly Dictionary<TConfig, TValue> configs = new ();

private readonly SortedList<TConfig, TValue> _defaults;

public DictionConfig(SortedList<TConfig, TValue> @default = null)
private readonly Func<TConfig, TValue> _defaultGetter;

public DictionConfig(SortedList<TConfig, TValue> @default = null, Func<TConfig, TValue> defaultGetter = null)
{
_defaults = @default;
_defaultGetter = defaultGetter ?? ((command) =>
{
return (TValue)command.GetAttribute<DefaultAttribute>()?.Default ?? default;
});
}

public TValue GetValue(TConfig command)
Expand All @@ -471,7 +504,20 @@ public TValue GetValue(TConfig command)

public TValue GetDefault(TConfig command)
=> _defaults?.TryGetValue(command, out var value) ?? false ? value
: (TValue)command.GetAttribute<DefaultAttribute>()?.Default ?? default;
: Get_Default(command);

private TValue Get_Default(TConfig command)
{
try
{
return _defaultGetter(command);
}
catch (Exception ex)
{
Svc.Log.Warning(ex, "Failed to load the default value.");
return default;
}
}

public void SetValue(TConfig command, TValue value)
=> configs[command] = value;
Expand Down
30 changes: 28 additions & 2 deletions RotationSolver.Basic/DataCenter.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.ClientState.Objects.SubKinds;
using Dalamud.Logging;
using ECommons.DalamudServices;
using ECommons.ExcelServices;
using ECommons.GameHelpers;
using FFXIVClientStructs.FFXIV.Client.Game;
using FFXIVClientStructs.FFXIV.Client.Game.Fate;
using RotationSolver.Basic.Configuration;
using RotationSolver.Basic.Configuration.Conditions;
using Action = Lumina.Excel.GeneratedSheets.Action;
using CharacterManager = FFXIVClientStructs.FFXIV.Client.Game.Character.CharacterManager;

namespace RotationSolver.Basic;

internal static class DataCenter
{
public static MajorConditionSet RightSet
{
get
{
if (ConditionSets == null || !ConditionSets.Any())
{
ConditionSets = new MajorConditionSet[] { new MajorConditionSet() };
}

var index = Service.Config.GetValue(PluginConfigInt.ActionSequencerIndex);
if(index < 0 || index >= ConditionSets.Count())
{
index = 0;
Service.Config.SetValue(PluginConfigInt.ActionSequencerIndex, index);
}

return ConditionSets.ElementAt(index);
}
}

internal static IEnumerable<MajorConditionSet> ConditionSets { get; set; } = Array.Empty<MajorConditionSet>();

/// <summary>
/// Only recorded 15s hps.
/// </summary>
public const int HP_RECORD_TIME = 240;
internal static Queue<(DateTime time, SortedList<uint, float> hpRatios)> RecordedHP { get; } = new(HP_RECORD_TIME + 1);

public static ICustomRotation RightNowRotation { get; internal set; }


internal static bool NoPoslock => Svc.Condition[ConditionFlag.OccupiedInEvent]
|| !Service.Config.GetValue(Configuration.PluginConfigBool.PoslockCasting)
|| !Service.Config.GetValue(PluginConfigBool.PoslockCasting)
//Key cancel.
|| Svc.KeyState[ConfigurationHelper.Keys[Service.Config.GetValue(Configuration.PluginConfigInt.PoslockModifier) % ConfigurationHelper.Keys.Length]]
//Gamepad cancel.
Expand Down
Loading

0 comments on commit 5cbafc8

Please sign in to comment.