Skip to content

Commit

Permalink
move AI DTR into shared DTR class
Browse files Browse the repository at this point in the history
  • Loading branch information
xanunderscore committed Aug 11, 2024
1 parent 1386144 commit 63fe46f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 66 deletions.
64 changes: 18 additions & 46 deletions BossMod/AI/AIManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Dalamud.Game.Gui.Dtr;
using BossMod.Autorotation;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
Expand All @@ -16,12 +15,12 @@ sealed class AIManager : IDisposable
private readonly AIConfig _config;
private int MasterSlot => (int)_config.FollowSlot; // non-zero means corresponding player is master
private Positional DesiredPositional => _config.DesiredPositional;
private AIBehaviour? _beh;
private Preset? _aiPreset;
private readonly UISimpleWindow _ui;
private readonly IDtrBarEntry _dtrBarEntry = Service.DtrBar.Get("Bossmod");
private WorldState WorldState => _autorot.Bossmods.WorldState;
public float ForceMovementIn => _beh?.ForceMovementIn ?? float.MaxValue;
public float ForceMovementIn => Behaviour?.ForceMovementIn ?? float.MaxValue;

public AIBehaviour? Behaviour { get; private set; }

public AIManager(RotationModuleManager autorot, ActionManagerEx amex)
{
Expand All @@ -37,48 +36,23 @@ public void Dispose()
{
SwitchToIdle();
_ui.Dispose();
_dtrBarEntry.Remove();
Service.ChatGui.ChatMessage -= OnChatMessage;
Service.CommandManager.RemoveHandler("/vbmai");
}

public void DtrUpdate(AIBehaviour? behaviour)
{
_dtrBarEntry.Shown = _config.ShowDTR;
if (_dtrBarEntry.Shown)
{
var status = behaviour != null ? "On" : "Off";
_dtrBarEntry.Text = "AI: " + status;
_dtrBarEntry.OnClick = () =>
{
if (behaviour != null)
SwitchToIdle();
else
{
if (!_config.Enabled)
{
_config.Enabled = true;
_config.Modified.Fire();
}
SwitchToFollow((int)_config.FollowSlot);
}
};
}
}

public void Update()
{
if (!WorldState.Party.Members[MasterSlot].IsValid())
SwitchToIdle();

if (!_config.Enabled && _beh != null)
if (!_config.Enabled && Behaviour != null)
SwitchToIdle();

var player = WorldState.Party.Player();
var master = WorldState.Party[MasterSlot];
if (_beh != null && player != null && master != null)
if (Behaviour != null && player != null && master != null)
{
_beh.Execute(player, master);
Behaviour.Execute(player, master);
}
else
{
Expand All @@ -87,21 +61,19 @@ public void Update()
_controller.Update(player);

_ui.IsOpen = _config.Enabled && player != null && _config.DrawUI;

DtrUpdate(_beh);
}

private void DrawOverlay()
{
ImGui.TextUnformatted($"AI: {(_beh != null ? "on" : "off")}, master={WorldState.Party[MasterSlot]?.Name}");
ImGui.TextUnformatted($"AI: {(Behaviour != null ? "on" : "off")}, master={WorldState.Party[MasterSlot]?.Name}");
ImGui.TextUnformatted($"Navi={_controller.NaviTargetPos} / {_controller.NaviTargetRot}{(_controller.ForceFacing ? " forced" : "")}");
_beh?.DrawDebug();
Behaviour?.DrawDebug();

using (var leaderCombo = ImRaii.Combo("Follow", _beh == null ? "<idle>" : (_config.FollowTarget ? "<target>" : WorldState.Party[MasterSlot]?.Name ?? "<unknown>")))
using (var leaderCombo = ImRaii.Combo("Follow", Behaviour == null ? "<idle>" : (_config.FollowTarget ? "<target>" : WorldState.Party[MasterSlot]?.Name ?? "<unknown>")))
{
if (leaderCombo)
{
if (ImGui.Selectable("<idle>", _beh == null))
if (ImGui.Selectable("<idle>", Behaviour == null))
{
SwitchToIdle();
}
Expand Down Expand Up @@ -149,30 +121,30 @@ private void DrawOverlay()
if (ImGui.Selectable(p.Name, p == _aiPreset))
{
_aiPreset = p;
if (_beh != null)
_beh.AIPreset = p;
if (Behaviour != null)
Behaviour.AIPreset = p;
}
}
}
}
}

private void SwitchToIdle()
public void SwitchToIdle()
{
_beh?.Dispose();
_beh = null;
Behaviour?.Dispose();
Behaviour = null;

_config.FollowSlot = PartyState.PlayerSlot;
_config.Modified.Fire();
_controller.Clear();
}

private void SwitchToFollow(int masterSlot)
public void SwitchToFollow(int masterSlot)
{
SwitchToIdle();
_config.FollowSlot = (AIConfig.Slot)masterSlot;
_config.Modified.Fire();
_beh = new AIBehaviour(_controller, _autorot, _aiPreset);
Behaviour = new AIBehaviour(_controller, _autorot, _aiPreset);
}

private unsafe int FindPartyMemberSlotFromSender(SeString sender)
Expand Down Expand Up @@ -233,7 +205,7 @@ private void OnCommand(string cmd, string message)
SwitchToIdle();
break;
case "toggle":
if (_beh == null)
if (Behaviour == null)
SwitchToFollow((int)_config.FollowSlot);
else
SwitchToIdle();
Expand Down
60 changes: 42 additions & 18 deletions BossMod/Autorotation/DTR.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,74 @@
using Dalamud.Game.Gui.Dtr;
using BossMod.AI;
using Dalamud.Game.Gui.Dtr;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Interface.Utility.Raii;
using ImGuiNET;

namespace BossMod.Autorotation;
internal class DTR : IDisposable
internal sealed class DTR : IDisposable
{
private readonly RotationModuleManager _mgr;
private readonly IDtrBarEntry _dtrBarEntry = Service.DtrBar.Get("vbm");
private readonly AutorotationConfig _config = Service.Config.Get<AutorotationConfig>();
private readonly AIManager _ai;
private readonly IDtrBarEntry _autorotationEntry = Service.DtrBar.Get("vbm-autorotation");
private readonly IDtrBarEntry _aiEntry = Service.DtrBar.Get("vbm-ai");
private readonly AutorotationConfig _arConfig = Service.Config.Get<AutorotationConfig>();
private readonly AIConfig _aiConfig = Service.Config.Get<AIConfig>();
private readonly DTRPopup _popup;

public DTR(RotationModuleManager manager)
public DTR(RotationModuleManager manager, AIManager ai)
{
_mgr = manager;
_ai = ai;
_popup = new(_mgr) { IsOpen = true };

_dtrBarEntry.OnClick = () => _popup.Show();
_autorotationEntry.OnClick = () => _popup.Show();
_aiEntry.OnClick = () =>
{
if (_ai.Behaviour == null)
{
if (!_aiConfig.Enabled)
{
_aiConfig.Enabled = true;
_aiConfig.Modified.Fire();
}
_ai.SwitchToFollow((int)_aiConfig.FollowSlot);
}
else
_ai.SwitchToIdle();
};
}

public void Dispose()
{
_dtrBarEntry.Remove();
_autorotationEntry.Remove();
_aiEntry.Remove();
_popup.Dispose();
}

public void Update()
{
_dtrBarEntry.Shown = _config.ShowDTR != AutorotationConfig.DtrStatus.None;
_autorotationEntry.Shown = _arConfig.ShowDTR != AutorotationConfig.DtrStatus.None;
var (icon, current) = _mgr.Preset?.Name switch
{
"" => (BitmapFontIcon.SwordSheathed, "Disabled"),
null => (BitmapFontIcon.SwordSheathed, "Idle"),
var x => (BitmapFontIcon.SwordUnsheathed, x)
};
Payload prefix = _config.ShowDTR == AutorotationConfig.DtrStatus.TextOnly ? new TextPayload("vbm: ") : new IconPayload(icon);
_dtrBarEntry.Text = new SeString(prefix, new TextPayload(current));
Payload prefix = _arConfig.ShowDTR == AutorotationConfig.DtrStatus.TextOnly ? new TextPayload("vbm: ") : new IconPayload(icon);
_autorotationEntry.Text = new SeString(prefix, new TextPayload(current));

_aiEntry.Shown = _aiConfig.ShowDTR;
_aiEntry.Text = "AI: " + (_ai.Behaviour == null ? "Off" : "On");
}
}

internal class DTRPopup(RotationModuleManager _mgr) : UIWindow("###vbm_dtr", false, new(100, 100), ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoMouseInputs | ImGuiWindowFlags.NoResize)
internal class DTRPopup(RotationModuleManager manager) : UIWindow("###vbm_dtr", false, new(100, 100), ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoMouseInputs | ImGuiWindowFlags.NoResize)
{
private bool _open;
public void Show()
{
if (_mgr.Player == null)
if (manager.Player == null)
return;

_open = true;
Expand All @@ -66,24 +90,24 @@ public override void Draw()

void DrawSelector()
{
if (_mgr.Player == null)
if (manager.Player == null)
return;

using (ImRaii.PushColor(ImGuiCol.Button, 0xff000080, _mgr.Preset == RotationModuleManager.ForceDisable))
using (ImRaii.PushColor(ImGuiCol.Button, 0xff000080, manager.Preset == RotationModuleManager.ForceDisable))
{
if (ImGui.Button("X"))
{
_mgr.Preset = _mgr.Preset == RotationModuleManager.ForceDisable ? null : RotationModuleManager.ForceDisable;
manager.Preset = manager.Preset == RotationModuleManager.ForceDisable ? null : RotationModuleManager.ForceDisable;
}
}

foreach (var p in _mgr.Database.Presets.PresetsForClass(_mgr.Player.Class))
foreach (var p in manager.Database.Presets.PresetsForClass(manager.Player.Class))
{
ImGui.SameLine();
using var col = ImRaii.PushColor(ImGuiCol.Button, 0xff008080, _mgr.Preset == p);
using var col = ImRaii.PushColor(ImGuiCol.Button, 0xff008080, manager.Preset == p);
if (ImGui.Button(p.Name))
{
_mgr.Preset = _mgr.Preset == p ? null : p;
manager.Preset = manager.Preset == p ? null : p;
ImGui.CloseCurrentPopup();
}
}
Expand Down
2 changes: 1 addition & 1 deletion BossMod/Framework/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public unsafe Plugin(IDalamudPluginInterface dalamud, ICommandManager commandMan
_ai = new(_rotation, _amex);
_broadcast = new();
_ipc = new(_rotation, _amex);
_dtr = new(_rotation);
_dtr = new(_rotation, _ai);

Service.Framework.Update += OnUpdate;

Expand Down
1 change: 0 additions & 1 deletion BossMod/Framework/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public sealed class Service
[PluginService] public static IObjectTable ObjectTable { get; private set; }
[PluginService] public static ITargetManager TargetManager { get; private set; }
[PluginService] public static IKeyState KeyState { get; private set; }
[PluginService] public static IDtrBar DtrBar { get; private set; }
#pragma warning restore CS8618

#pragma warning disable CA2211
Expand Down

0 comments on commit 63fe46f

Please sign in to comment.