Skip to content

Commit

Permalink
Add support for coloring successful positionals
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcintyre committed Jul 26, 2022
1 parent fc27ca2 commit b3337a6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
3 changes: 3 additions & 0 deletions DamageInfoPlugin/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ public bool FocusTargetCastBarColorEnabled
public Vector4 PhysicalBgColor { get; set; } = new(1, 1, 1, 1);
public Vector4 MagicBgColor { get; set; } = new(1, 1, 1, 1);
public Vector4 DarknessBgColor { get; set; } = new(1, 1, 1, 1);

public Vector4 PositionalColor { get; set; } = new(0, 1, 0, 1);

public bool DebugLogEnabled { get; set; }

public bool IncomingColorEnabled { get; set; } = true;
public bool OutgoingColorEnabled { get; set; } = true;
public bool PetColorEnabled { get; set; } = true;
public bool PositionalColorEnabled { get; set; } = true;

public bool SourceTextEnabled { get; set; }
public bool PetSourceTextEnabled { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion DamageInfoPlugin/DamageInfoEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum ActionEffectType : byte
ApplyStatusEffectTarget = 15,
ApplyStatusEffectSource = 16,
StatusNoEffect = 20,
StartActionCombo = 27,
PositionalSucceed = 27,
ComboSucceed = 28,
Knockback = 33,
Mount = 40,
Expand Down
65 changes: 53 additions & 12 deletions DamageInfoPlugin/DamageInfoPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using Dalamud;
using Dalamud.Data;
using Dalamud.Game;
Expand All @@ -14,10 +15,13 @@
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Hooking;
using Dalamud.Interface.Colors;
using Dalamud.IoC;
using Dalamud.Logging;
using Dalamud.Utility;
using FFXIVClientStructs.FFXIV.Component.GUI;
using ImGuiNET;
using Lumina.Excel.GeneratedSheets;
using static DamageInfoPlugin.LogType;
using Action = Lumina.Excel.GeneratedSheets.Action;
using Character = FFXIVClientStructs.FFXIV.Client.Game.Character.Character;
Expand Down Expand Up @@ -61,24 +65,24 @@ private delegate void AddScreenLogDelegate(
int val3,
int val4);

private readonly Hook<AddScreenLogDelegate> _addScreenLogHook;

private delegate void ReceiveActionEffectDelegate(uint sourceId, Character* sourceCharacter, IntPtr pos, EffectHeader* effectHeader, EffectEntry* effectArray, ulong* effectTail);

private readonly Hook<ReceiveActionEffectDelegate> _receiveActionEffectHook;

private delegate void SetCastBarDelegate(IntPtr thisPtr, IntPtr a2, IntPtr a3, IntPtr a4, char a5);

private readonly Hook<AddScreenLogDelegate> _addScreenLogHook;
private readonly Hook<ReceiveActionEffectDelegate> _receiveActionEffectHook;
private readonly Hook<SetCastBarDelegate> _setCastBarHook;
private readonly Hook<SetCastBarDelegate> _setFocusTargetCastBarHook;

private readonly CastbarInfo _nullCastbarInfo;

private Dictionary<uint, DamageType> _actionToDamageTypeDict;
private readonly HashSet<uint> _ignoredCastActions;

private ActionEffectStore _actionStore;

private readonly HashSet<int> _positionalStore = new();
private readonly int[] _positionalFallback = {
56, 66, 79, 88, 2255, 2258, 3554, 3556, 3563, 7481, 7482, 24382, 24383, 25772,
};

public DamageInfoPlugin(
[RequiredVersion("1.0")] GameGui gameGui,
[RequiredVersion("1.0")] FlyTextGui ftGui,
Expand All @@ -99,23 +103,24 @@ public DamageInfoPlugin(

_configuration = LoadConfig(pi);
_ui = new PluginUI(_configuration, this);

_actionToDamageTypeDict = new Dictionary<uint, DamageType>();
_ignoredCastActions = new HashSet<uint>();
_actionStore = new ActionEffectStore(_configuration);

_nullCastbarInfo = new CastbarInfo { unitBase = null, gauge = null, bg = null };

cmdMgr.AddHandler(CommandName, new CommandInfo(OnCommand)
{
HelpMessage = "Display the Damage Info configuration interfae."
});

_nullCastbarInfo = new CastbarInfo { unitBase = null, gauge = null, bg = null };

try
{
var actionSheet = dataMgr.GetExcelSheet<Action>();
var actionTransientSheet = dataMgr.GetExcelSheet<ActionTransient>(ClientLanguage.English);

if (actionSheet == null)
throw new NullReferenceException();

foreach (var row in actionSheet)
{
var tmpType = (DamageType)row.AttackType.Row;
Expand All @@ -130,6 +135,31 @@ public DamageInfoPlugin(
_ignoredCastActions.Add(row.ActionCategory.Row);
}

if (actionTransientSheet != null)
{
foreach (var row in actionTransientSheet)
{
var desc = row.Description.ToDalamudString().TextValue;
if (desc.Contains("target's rear") || desc.Contains("target's flank"))
_positionalStore.Add((int) row.RowId);
}
}
else
{
PluginLog.Debug("Runtime positional data failed, falling back.");
foreach (var id in _positionalFallback)
_positionalStore.Add(id);
}

// Code to update the positional fallback
// var sb = new StringBuilder();
// foreach (var id in _positionalStore)
// {
// sb.Append(id);
// sb.Append(", ");
// }
// PluginLog.Debug(sb.ToString());

var receiveActionEffectFuncPtr = scanner.ScanText("4C 89 44 24 ?? 55 56 57 41 54 41 55 41 56 48 8D 6C 24");
_receiveActionEffectHook = new Hook<ReceiveActionEffectDelegate>(receiveActionEffectFuncPtr, (ReceiveActionEffectDelegate)ReceiveActionEffect);

Expand Down Expand Up @@ -470,7 +500,7 @@ private void ReceiveActionEffect(uint sourceId, Character* sourceCharacter, IntP
try
{
_actionStore.Cleanup();

DebugLog(Effect, $"--- source actor: {sourceCharacter->GameObject.ObjectID}, action id {effectHeader->ActionId}, anim id {effectHeader->AnimationId} numTargets: {effectHeader->TargetCount} ---");

// TODO: Reimplement opcode logging, if it's even useful. Original code follows
Expand All @@ -488,6 +518,12 @@ private void ReceiveActionEffect(uint sourceId, Character* sourceCharacter, IntP
_ => 0
};

var positionalSucceedId = -1;
for (int i = 0; i < entryCount; i++)
if (effectArray[i].type == ActionEffectType.PositionalSucceed)
positionalSucceedId = effectArray[i].value;
var positionalSucceed = _positionalStore.Contains(positionalSucceedId) && positionalSucceedId == effectHeader->ActionId;

for (int i = 0; i < entryCount; i++)
{
if (effectArray[i].type == ActionEffectType.Nothing) continue;
Expand All @@ -508,6 +544,7 @@ private void ReceiveActionEffect(uint sourceId, Character* sourceCharacter, IntP
sourceId = sourceId,
targetId = target,
value = dmg,
positionalSucceed = positionalSucceed,
};

_actionStore.AddEffect(newEffect);
Expand Down Expand Up @@ -602,10 +639,14 @@ private void OnFlyTextCreated(
{
var incomingCheck = !isCharaAction && isCharaTarget && !isHealingAction && _configuration.IncomingColorEnabled;
var outgoingCheck = isCharaAction && !isCharaTarget && !isHealingAction && _configuration.OutgoingColorEnabled;
var posCheck = isCharaAction && info.positionalSucceed && _configuration.PositionalColorEnabled;
var petCheck = !isCharaAction && !isCharaTarget && petIds.Contains(info.sourceId) && !isHealingAction && _configuration.PetColorEnabled;

if (incomingCheck || outgoingCheck || petCheck)
color = GetDamageColor(dmgType);

if (posCheck)
color = ImGui.GetColorU32(_configuration.PositionalColor);
}

if (_configuration.SourceTextEnabled || _configuration.PetSourceTextEnabled || _configuration.HealSourceTextEnabled)
Expand Down
8 changes: 5 additions & 3 deletions DamageInfoPlugin/DamageInfoStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public struct ActionEffectInfo
public uint sourceId;
public ulong targetId;
public uint value;
public bool positionalSucceed;

public override bool Equals(object o)
{
Expand All @@ -62,18 +63,19 @@ o is ActionEffectInfo other
&& other.kind == kind
&& other.sourceId == sourceId
&& other.targetId == targetId
&& other.value == value;
&& other.value == value
&& other.positionalSucceed == positionalSucceed;
}

public override int GetHashCode()
{
return HashCode.Combine(actionId, (int)kind, sourceId, targetId, value);
return HashCode.Combine(actionId, (int)kind, sourceId, targetId, value, positionalSucceed);
}

public override string ToString()
{
return
$"actionId: {actionId} kind: {kind} ({(int)kind}) sourceId: {sourceId} (0x{sourceId:X}) targetId: {targetId} (0x{targetId:X}) value: {value}";
$"actionId: {actionId} kind: {kind} ({(int)kind}) sourceId: {sourceId} (0x{sourceId:X}) targetId: {targetId} (0x{targetId:X}) value: {value} positionalSucceed: {positionalSucceed}";
}
}
}
21 changes: 20 additions & 1 deletion DamageInfoPlugin/PluginUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private void DrawSettingsWindow()
var lPhys = configuration.PhysicalColor;
var lMag = configuration.MagicColor;
var lDark = configuration.DarknessColor;
var lPos = configuration.PositionalColor;
var gPhys = configuration.PhysicalCastColor;
var gMag = configuration.MagicCastColor;
var gDark = configuration.DarknessCastColor;
Expand All @@ -66,6 +67,7 @@ private void DrawSettingsWindow()
var incomingColorConfigValue = configuration.IncomingColorEnabled;
var outgoingColorConfigValue = configuration.OutgoingColorEnabled;
var petColorConfigValue = configuration.PetColorEnabled;
var posColorConfigValue = configuration.PositionalColorEnabled;

var sourceTextConfigValue = configuration.SourceTextEnabled;
var petSourceTextConfigValue = configuration.PetSourceTextEnabled;
Expand All @@ -77,7 +79,7 @@ private void DrawSettingsWindow()
var healAttackTextConfigValue = configuration.HealAttackTextEnabled;

// computed state
var colorAllConfigValue = incomingColorConfigValue && outgoingColorConfigValue && petColorConfigValue;
var colorAllConfigValue = incomingColorConfigValue && outgoingColorConfigValue && petColorConfigValue && posColorConfigValue;
var sourceTextAllConfigValue = sourceTextConfigValue && petSourceTextConfigValue && healSourceTextConfigValue;
var attackTextAllConfigValue = incAttackTextConfigValue && outAttackTextConfigValue && petAttackTextConfigValue && healAttackTextConfigValue;

Expand Down Expand Up @@ -114,6 +116,7 @@ private void DrawSettingsWindow()
configuration.IncomingColorEnabled = colorAllConfigValue;
configuration.OutgoingColorEnabled = colorAllConfigValue;
configuration.PetColorEnabled = colorAllConfigValue;
configuration.PositionalColorEnabled = colorAllConfigValue;
configuration.Save();
}
ImGui.NextColumn();
Expand Down Expand Up @@ -192,6 +195,16 @@ private void DrawSettingsWindow()
configuration.Save();
}
ImGui.NextColumn();
ImGui.Text("Positionals");
ImGui.NextColumn();
if (ImGui.Checkbox("##positionalcolor", ref posColorConfigValue))
{
configuration.PositionalColorEnabled = posColorConfigValue;
configuration.Save();
}
ImGui.NextColumn();
ImGui.NextColumn();
ImGui.NextColumn();
ImGui.Text("Heals");
ImGui.NextColumn();
ImGui.NextColumn();
Expand Down Expand Up @@ -226,6 +239,12 @@ private void DrawSettingsWindow()
configuration.DarknessColor = lDark;
configuration.Save();
}

if (ImGui.ColorEdit4("Positionals##flytext", ref lPos))
{
configuration.PositionalColor = lPos;
configuration.Save();
}
}

if (ImGui.CollapsingHeader("Castbars"))
Expand Down

0 comments on commit b3337a6

Please sign in to comment.