Skip to content

Commit

Permalink
Merge pull request #412 from xanunderscore/dtr
Browse files Browse the repository at this point in the history
dtr bar display
  • Loading branch information
awgil authored Aug 11, 2024
2 parents f103faf + 63fe46f commit 65d8dd0
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 47 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
13 changes: 13 additions & 0 deletions BossMod/Autorotation/AutorotationConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ public sealed class AutorotationConfig : ConfigNode
[PropertyDisplay("Show in-game UI")]
public bool ShowUI = false;

public enum DtrStatus
{
[PropertyDisplay("Disabled")]
None,
[PropertyDisplay("Text only")]
TextOnly,
[PropertyDisplay("With icon")]
Icon
}

[PropertyDisplay("Preset display in DTR bar")]
public DtrStatus ShowDTR = DtrStatus.None;

[PropertyDisplay("Show positional hints in world")]
public bool ShowPositionals = false;

Expand Down
115 changes: 115 additions & 0 deletions BossMod/Autorotation/DTR.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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 sealed class DTR : IDisposable
{
private readonly RotationModuleManager _mgr;
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, AIManager ai)
{
_mgr = manager;
_ai = ai;
_popup = new(_mgr) { IsOpen = true };

_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()
{
_autorotationEntry.Remove();
_aiEntry.Remove();
_popup.Dispose();
}

public void Update()
{
_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 = _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 manager) : UIWindow("###vbm_dtr", false, new(100, 100), ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoMouseInputs | ImGuiWindowFlags.NoResize)
{
private bool _open;
public void Show()
{
if (manager.Player == null)
return;

_open = true;
}

public override void Draw()
{
if (ImGui.BeginPopup("vbm_dtr_menu"))
{
DrawSelector();
ImGui.EndPopup();
}
if (_open)
{
_open = false;
ImGui.OpenPopup("vbm_dtr_menu");
}
}

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

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

foreach (var p in manager.Database.Presets.PresetsForClass(manager.Player.Class))
{
ImGui.SameLine();
using var col = ImRaii.PushColor(ImGuiCol.Button, 0xff008080, manager.Preset == p);
if (ImGui.Button(p.Name))
{
manager.Preset = manager.Preset == p ? null : p;
ImGui.CloseCurrentPopup();
}
}
}
}
2 changes: 2 additions & 0 deletions BossMod/Autorotation/PresetDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ public void Save()
Service.Log($"Failed to write database to '{_dbPath.FullName}': {ex}");
}
}

public IEnumerable<Preset> PresetsForClass(Class c) => Presets.Where(p => p.Modules.Any(m => RotationModuleRegistry.Modules[m.Key].Definition.Classes[(int)c]));
}
2 changes: 1 addition & 1 deletion BossMod/Autorotation/UIRotationWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override void Draw()
}
}

foreach (var p in _mgr.Database.Presets.Presets.Where(p => p.Modules.Any(m => RotationModuleRegistry.Modules[m.Key].Definition.Classes[(int)player.Class])))
foreach (var p in _mgr.Database.Presets.PresetsForClass(player.Class))
{
ImGui.SameLine();
using var col = ImRaii.PushColor(ImGuiCol.Button, 0xff008080, _mgr.Preset == p);
Expand Down
11 changes: 11 additions & 0 deletions BossMod/Framework/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public sealed class Plugin : IDalamudPlugin
private readonly AI.Broadcast _broadcast;
private readonly IPCProvider _ipc;
private TimeSpan _prevUpdateTime;
private readonly DTR _dtr;

// windows
private readonly ConfigUI _configUI; // TODO: should be a proper window!
Expand Down Expand Up @@ -81,6 +82,9 @@ public unsafe Plugin(IDalamudPluginInterface dalamud, ICommandManager commandMan
_ai = new(_rotation, _amex);
_broadcast = new();
_ipc = new(_rotation, _amex);
_dtr = new(_rotation, _ai);

Service.Framework.Update += OnUpdate;

_configUI = new(Service.Config, _ws, _rotationDB);
_wndBossmod = new(_bossmod);
Expand All @@ -97,6 +101,7 @@ public unsafe Plugin(IDalamudPluginInterface dalamud, ICommandManager commandMan
public void Dispose()
{
Service.Condition.ConditionChange -= OnConditionChanged;
Service.Framework.Update -= OnUpdate;
_wndDebug.Dispose();
_wndRotation.Dispose();
_wndReplay.Dispose();
Expand All @@ -110,10 +115,16 @@ public void Dispose()
_amex.Dispose();
_hintsBuilder.Dispose();
_bossmod.Dispose();
_dtr.Dispose();
ActionDefinitions.Instance.Dispose();
CommandManager.RemoveHandler("/vbm");
}

private void OnUpdate(IFramework fw)
{
_dtr.Update();
}

private void OnCommand(string cmd, string args)
{
Service.Log($"OnCommand: {cmd} {args}");
Expand Down

0 comments on commit 65d8dd0

Please sign in to comment.