Decompiled source of Damage Preview v1.0.1

plugins/FtkDamagePreview.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using GridEditor;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace FtkDamagePreview;

internal sealed class RollRules
{
	internal int Slots;

	internal int Focus;

	internal float Skill;

	internal bool Illuminated;

	internal bool Darkness;

	internal bool Shocked;

	internal float Critical;

	internal float CalledShot;

	internal float Justice;

	internal float BlackHole;

	internal double Encourage;

	internal double Distract;

	internal double RoyalDroll;
}
internal sealed class TargetRules
{
	internal int Health;

	internal int WeaponDamage;

	internal int Defense;

	internal bool Physical;

	internal float FrozenMultiplier = 1f;

	internal double Evasion;

	internal bool DamageBlocked;

	internal bool Reflecting;

	internal double? EffectChance;
}
internal sealed class AttackRules
{
	internal float Multiplier = 1f;

	internal float CriticalBonus;

	internal bool IgnoreDefense;

	internal bool RequiresPerfect;

	internal bool Harmless;

	internal bool Petrify;

	internal bool Secondary;

	internal bool SplashOnPerfect;

	internal bool JusticeSplash;
}
internal struct RollOutcome
{
	internal int Successes;

	internal double Probability;

	internal bool Critical;

	internal bool Justice;

	internal bool BlackHole;
}
internal sealed class DamageDistribution
{
	internal readonly SortedDictionary<int, double> Damage = new SortedDictionary<int, double>();

	internal double ExpectedDamage { get; private set; }

	internal double KillChance { get; private set; }

	internal double PerfectChance { get; private set; }

	internal double EffectChance { get; private set; }

	internal double ProbabilityAtLeast(long threshold)
	{
		double num = 0.0;
		foreach (KeyValuePair<int, double> item in Damage)
		{
			if (item.Key >= threshold)
			{
				num += item.Value;
			}
		}
		return ClampProbability(num);
	}

	internal static List<RollOutcome> Roll(RollRules rules)
	{
		List<RollOutcome> list = new List<RollOutcome>();
		double num = ClampProbability(rules.Critical);
		double num2 = (1.0 - num) * ClampProbability(rules.CalledShot);
		double num3 = (1.0 - num - num2) * ClampProbability(rules.Justice);
		double num4 = 1.0 - num - num2 - num3;
		double num5 = ClampProbability(rules.BlackHole);
		double num6 = (num + num4) * num5;
		if (num6 > 0.0)
		{
			list.Add(new RollOutcome
			{
				BlackHole = true,
				Probability = num6
			});
		}
		AddSlotOutcomes(list, rules, num4 * (1.0 - num5), critical: false, justice: false);
		AddSlotOutcomes(list, rules, num * (1.0 - num5) + num2, critical: true, justice: false);
		AddSlotOutcomes(list, rules, num3, critical: false, justice: true);
		return list;
	}

	private static void AddSlotOutcomes(List<RollOutcome> outcomes, RollRules rules, double probability, bool critical, bool justice)
	{
		if (probability <= 0.0)
		{
			return;
		}
		double[,] array = new double[rules.Slots + 1, 8];
		double[,] array2 = new double[rules.Slots + 1, 8];
		array[0, 0] = probability;
		double num = SkillRollChance(rules.Skill);
		for (int i = 0; i < rules.Slots; i++)
		{
			bool flag = i < rules.Focus || rules.Illuminated;
			bool flag2 = rules.Darkness || (i == 0 && rules.Shocked);
			for (int j = 0; j <= i; j++)
			{
				for (int k = 0; k < 8; k++)
				{
					double num2 = array[j, k];
					if (num2 != 0.0)
					{
						if (flag || (!flag2 && (critical || justice)))
						{
							array2[j + 1, k] += num2;
							continue;
						}
						if (flag2)
						{
							array2[j, k] += num2;
							continue;
						}
						double num3 = num2 * num;
						double num4 = num2 - num3;
						double num5 = (((k & 1) == 0) ? (num3 * rules.Distract) : 0.0);
						double num6 = (((k & 4) == 0) ? ((num3 - num5) * rules.RoyalDroll) : 0.0);
						double num7 = (((k & 2) == 0) ? (num4 * rules.Encourage) : 0.0);
						array2[j, k | 1] += num5;
						array2[j, k | 4] += num6;
						array2[j + 1, k] += num3 - num5 - num6;
						array2[j + 1, k | 2] += num7;
						array2[j, k] += num4 - num7;
					}
				}
			}
			double[,] array3 = array;
			array = array2;
			array2 = array3;
			Array.Clear(array2, 0, array2.Length);
		}
		for (int l = 0; l <= rules.Slots; l++)
		{
			double num8 = 0.0;
			for (int m = 0; m < 8; m++)
			{
				num8 += array[l, m];
			}
			if (num8 > 0.0)
			{
				outcomes.Add(new RollOutcome
				{
					Successes = l,
					Probability = num8,
					Critical = critical,
					Justice = justice
				});
			}
		}
	}

	private static double SkillRollChance(float skill)
	{
		int num = 0;
		for (int i = 0; i < 100; i++)
		{
			if ((float)i / 100f <= skill)
			{
				num++;
			}
		}
		return (double)num / 100.0;
	}

	internal static DamageDistribution Calculate(int slots, IList<RollOutcome> outcomes, AttackRules attack, TargetRules target)
	{
		DamageDistribution damageDistribution = new DamageDistribution();
		foreach (RollOutcome outcome in outcomes)
		{
			if (outcome.BlackHole)
			{
				bool flag = !attack.Secondary && !target.DamageBlocked;
				damageDistribution.Add(flag ? target.Health : 0, outcome.Probability, target.Health);
				continue;
			}
			bool flag2 = outcome.Successes == slots;
			if (flag2)
			{
				damageDistribution.PerfectChance += outcome.Probability;
			}
			if (target.DamageBlocked || (attack.Secondary && ((attack.SplashOnPerfect && !flag2) || (attack.JusticeSplash && !outcome.Justice))))
			{
				damageDistribution.Add(0, outcome.Probability, target.Health);
				continue;
			}
			double num = (flag2 ? 0.0 : target.Evasion);
			double num2 = outcome.Probability * (1.0 - num);
			int damage = GetDamage(outcome, slots, attack, target);
			damageDistribution.Add(0, outcome.Probability * num, target.Health);
			double num3 = 0.0;
			if ((!attack.RequiresPerfect || flag2) && (damage > 0 || attack.Harmless))
			{
				num3 = num2 * target.EffectChance.GetValueOrDefault();
			}
			if (target.Reflecting && damage > 0)
			{
				if (attack.Petrify && damage < target.Health)
				{
					damageDistribution.Add(damage, num3, target.Health);
					damageDistribution.Add(0, num2 - num3, target.Health);
					damageDistribution.EffectChance += num3;
				}
				else
				{
					damageDistribution.Add(0, num2, target.Health);
				}
			}
			else
			{
				damageDistribution.Add(damage, num2, target.Health);
				if (damage < target.Health)
				{
					damageDistribution.EffectChance += num3;
				}
			}
		}
		return damageDistribution;
	}

	private static int GetDamage(RollOutcome outcome, int slots, AttackRules attack, TargetRules target)
	{
		int num = RoundDamage((float)outcome.Successes / (float)slots * (float)target.WeaponDamage * attack.Multiplier);
		if (num <= 0)
		{
			return 0;
		}
		int num2 = (outcome.Critical ? RoundDamage((float)num * attack.CriticalBonus) : 0);
		int num3 = ((attack.IgnoreDefense && (!attack.RequiresPerfect || outcome.Successes == slots)) ? Math.Min(0, target.Defense) : target.Defense);
		int val = RoundDamage((float)num * target.FrozenMultiplier) + num2 - num3;
		return Math.Max(0, Math.Min(target.Physical ? 1000 : int.MaxValue, val));
	}

	private void Add(int damage, double probability, int health)
	{
		if (!(probability <= 0.0))
		{
			Damage.TryGetValue(damage, out var value);
			Damage[damage] = value + probability;
			ExpectedDamage += (double)damage * probability;
			if (damage >= health)
			{
				KillChance += probability;
			}
		}
	}

	internal static double ClampProbability(double probability)
	{
		return Math.Max(0.0, Math.Min(1.0, probability));
	}

	private static int RoundDamage(float value)
	{
		return (int)(value + ((value > 0f) ? 0.5f : (-0.5f)));
	}
}
internal sealed class TargetPreview
{
	internal string Name;

	internal int Health;

	internal DamageDistribution Distribution;

	internal bool ShowEffect;
}
internal static class GamePreview
{
	private const ID NoProficiency = (ID)(-1);

	internal static List<TargetPreview> Read(uiBattleStanceButtons panel, uiBattleButton button)
	{
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Invalid comparison between Unknown and I4
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Invalid comparison between Unknown and I4
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Invalid comparison between Unknown and I4
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Invalid comparison between Unknown and I4
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Invalid comparison between Unknown and I4
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Invalid comparison between Unknown and I4
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_011b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: Invalid comparison between Unknown and I4
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c9: Invalid comparison between Unknown and I4
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d8: Invalid comparison between Unknown and I4
		//IL_0212: Unknown result type (might be due to invalid IL or missing references)
		//IL_021a: Unknown result type (might be due to invalid IL or missing references)
		//IL_021c: Unknown result type (might be due to invalid IL or missing references)
		//IL_023b: Unknown result type (might be due to invalid IL or missing references)
		//IL_023e: Invalid comparison between Unknown and I4
		//IL_027a: Unknown result type (might be due to invalid IL or missing references)
		//IL_027d: Invalid comparison between Unknown and I4
		//IL_0278: Unknown result type (might be due to invalid IL or missing references)
		//IL_0294: Unknown result type (might be due to invalid IL or missing references)
		EncounterSession instance = EncounterSession.Instance;
		CharacterOverworld combatCow = panel.CombatCow;
		if ((Object)(object)instance == (Object)null || (Object)(object)combatCow == (Object)null || (Object)(object)combatCow.m_CurrentDummy == (Object)null)
		{
			return null;
		}
		EnemyDummy currentEnemy = instance.GetCurrentEnemy();
		if ((Object)(object)currentEnemy == (Object)null || ((CharacterDummy)currentEnemy).GetCurrentHealth() <= 0 || ((EnemyInfo)currentEnemy).m_EnemyCombat == null)
		{
			return null;
		}
		FTK_weaponStats2 entry = FTK_weaponStats2DB.GetDB().GetEntry(combatCow.m_WeaponID);
		ID val = (ID)(((int)button.m_ButtonType != 4) ? (-1) : ((int)panel.GetCurrentSelectedProf()));
		if ((int)val == -1 && (int)button.m_ButtonType != 2)
		{
			return null;
		}
		FTK_proficiencyTable val2 = (((int)val == -1) ? null : FTK_proficiencyTableDB.Get(val));
		if (val2 != null && val2.m_TargetFriendly)
		{
			return null;
		}
		DamageType val3 = ((val2 != null && (int)val2.m_DmgTypeOverride != -1) ? val2.m_DmgTypeOverride : entry._dmgtype);
		if ((int)val3 == -1 && (val2 == null || !val2.m_Harmless))
		{
			return null;
		}
		RollRules rollRules = ReadRollRules(instance, combatCow, currentEnemy, entry, val, val2);
		if (rollRules.Slots <= 0)
		{
			return null;
		}
		List<RollOutcome> outcomes = DamageDistribution.Roll(rollRules);
		GameFlow instance2 = GameFlow.Instance;
		ProficiencyBase val4 = ((val2 == null || (Object)(object)ProficiencyManager.Instance == (Object)null) ? null : ProficiencyManager.Instance.Get(val));
		if ((Object)(object)val4 != (Object)null && (int)val4.m_Category == 12)
		{
			val4 = null;
		}
		AttackRules attackRules = new AttackRules
		{
			Multiplier = ((val2 == null) ? 1f : (val2.m_Harmless ? 0f : val2.m_DmgMultiplier)),
			CriticalBonus = instance2.m_CritDmgPercent,
			IgnoreDefense = (val2?.m_IgnoresArmor ?? false),
			RequiresPerfect = (val2?.m_FullSlots ?? false),
			Harmless = (val2?.m_Harmless ?? false),
			Petrify = ((Object)(object)val4 != (Object)null && (int)val4.m_Category == 36)
		};
		bool physical = (int)val3 == 0;
		float frozenDmgPercent = instance2.m_FrozenDmgPercent;
		List<TargetPreview> list = new List<TargetPreview> { PreviewTarget(currentEnemy, combatCow.m_CharacterStats, rollRules, outcomes, attackRules, physical, frozenDmgPercent, val4, val2) };
		TargetType val5 = (TargetType)((val2 != null) ? ((int)val2.m_Target) : 0);
		if ((int)val5 != 0 || rollRules.Justice > 0f)
		{
			attackRules.Secondary = true;
			attackRules.JusticeSplash = (int)val5 == 0;
			attackRules.SplashOnPerfect = !attackRules.JusticeSplash && attackRules.RequiresPerfect && (Object)(object)val2.m_ProficiencyPrefab == (Object)null;
			if (attackRules.JusticeSplash)
			{
				val5 = (TargetType)1;
			}
			if ((int)val5 == 1)
			{
				attackRules.Multiplier *= 0.5f;
			}
			foreach (CharacterDummy aoeTarget in instance.GetAoeTargets((CharacterDummy)(object)currentEnemy, val5, (List<DefendMarkerType>)null))
			{
				EnemyDummy val6 = (EnemyDummy)(object)((aoeTarget is EnemyDummy) ? aoeTarget : null);
				if ((Object)(object)val6 != (Object)null && ((CharacterDummy)val6).GetCurrentHealth() > 0)
				{
					list.Add(PreviewTarget(val6, combatCow.m_CharacterStats, rollRules, outcomes, attackRules, physical, frozenDmgPercent, val4, val2));
				}
			}
		}
		return list;
	}

	private static RollRules ReadRollRules(EncounterSession session, CharacterOverworld player, EnemyDummy target, FTK_weaponStats2 weapon, ID id, FTK_proficiencyTable proficiency)
	{
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		CharacterStats characterStats = player.m_CharacterStats;
		CharacterDummy currentDummy = player.m_CurrentDummy;
		int spentFocus = characterStats.SpentFocus;
		int num = ((proficiency != null && proficiency.m_SlotOverride > 0) ? proficiency.m_SlotOverride : weapon._slots);
		RollRules rollRules = new RollRules
		{
			Slots = num,
			Focus = spentFocus,
			Skill = characterStats.GetSkillValue(weapon._skilltest, true, proficiency?.m_PerSlotSkillRoll ?? 0f),
			Illuminated = currentDummy.Illuminated,
			Darkness = currentDummy.Darkness,
			Shocked = currentDummy.Shocked,
			Critical = (currentDummy.Shocked ? 0f : (characterStats.ChanceToCrit + 0.05f * (float)spentFocus))
		};
		ReadAttackPassives(session, player, target, weapon, id, proficiency, rollRules);
		if (spentFocus >= num || rollRules.Illuminated || rollRules.Darkness)
		{
			return rollRules;
		}
		double num2 = 1.0;
		foreach (CharacterDummy otherCombatPlayerMember in session.GetOtherCombatPlayerMembers(currentDummy))
		{
			if ((Object)(object)otherCombatPlayerMember != (Object)null && otherCombatPlayerMember.CanEncourage())
			{
				num2 *= 0.6499999761581421;
			}
		}
		rollRules.Encourage = 1.0 - num2;
		if (spentFocus == 0)
		{
			double num3 = 0.15f * Mathf.Pow(0.8f, (float)num);
			double num4 = 1.0;
			foreach (CharacterDummy otherCombatEnemyMember in session.GetOtherCombatEnemyMembers(target))
			{
				if ((Object)(object)otherCombatEnemyMember != (Object)null && otherCombatEnemyMember.CanDistract())
				{
					num4 *= 1.0 - num3;
				}
			}
			rollRules.Distract = 1.0 - num4;
		}
		if (HauntManager.IsScourgeActive((Scourge)5))
		{
			MiniHexHaunt haunt = HauntManager.GetHaunt((Scourge)5);
			if ((Object)(object)haunt != (Object)null && haunt.IsScourgeCooledDown())
			{
				rollRules.RoyalDroll = DamageDistribution.ClampProbability(haunt.GetHauntDBEntry().m_AbilityChance);
			}
		}
		return rollRules;
	}

	private static void ReadAttackPassives(EncounterSession session, CharacterOverworld player, EnemyDummy target, FTK_weaponStats2 weapon, ID id, FTK_proficiencyTable proficiency, RollRules rules)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Invalid comparison between Unknown and I4
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Invalid comparison between Unknown and I4
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Invalid comparison between Unknown and I4
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Invalid comparison between Unknown and I4
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Invalid comparison between Unknown and I4
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Invalid comparison between Unknown and I4
		CharacterSkills characterSkills = player.m_CharacterStats.m_CharacterSkills;
		CharacterDummy currentDummy = player.m_CurrentDummy;
		if ((int)currentDummy.m_SpecialAttack != 0 || (proficiency != null && (int)proficiency.m_Target != 0))
		{
			return;
		}
		if ((Object)(object)currentDummy.m_EventListener != (Object)null && (characterSkills.m_CalledShot || characterSkills.m_Justice))
		{
			WeaponType bestWeaponType = currentDummy.m_EventListener.GetBestWeaponType(id);
			if (characterSkills.m_CalledShot && rules.Focus == 0 && !weapon.m_NoFocus && ((int)bestWeaponType == 3 || (int)bestWeaponType == 11) && currentDummy.CanUseAbility())
			{
				rules.CalledShot = 0.1f;
			}
			if (characterSkills.m_Justice && !rules.Shocked && (int)((FTK_itembase)weapon).m_ObjectSlot == 4 && ((int)bestWeaponType == 7 || (int)bestWeaponType == 0 || (int)bestWeaponType == 1 || (int)bestWeaponType == 4) && session.GetAoeTargets((CharacterDummy)(object)target, (TargetType)1, (List<DefendMarkerType>)null).Count > 0)
			{
				rules.Justice = 0.15f + 0.03f * (float)rules.Focus;
			}
		}
		if (characterSkills.m_BlackHole && !((EnemyInfo)target).m_EnemyCombat.m_IsBoss && !((EnemyInfo)target).m_EnemyCombat.m_IsScourge)
		{
			int totalPartyItems = FTKHub.Instance.GetTotalPartyItems((ID)414);
			rules.BlackHole = (0.06f + 0.02f * (float)rules.Focus) * Mathf.Pow(0.5f, (float)totalPartyItems);
		}
	}

	private static TargetPreview PreviewTarget(EnemyDummy enemy, CharacterStats stats, RollRules rules, List<RollOutcome> outcomes, AttackRules attack, bool physical, float frozen, ProficiencyBase effect, FTK_proficiencyTable proficiency)
	{
		double num = (((CharacterDummy)enemy).CanDodge() ? ((EnemyInfo)enemy).GetEVDRating() : 0f);
		if (!((EnemyInfo)enemy).IsEvasive())
		{
			num *= Math.Pow(0.5, rules.Focus);
		}
		TargetRules targetRules = new TargetRules
		{
			Health = ((CharacterDummy)enemy).GetCurrentHealth(),
			WeaponDamage = stats.GetWeaponMaxDamage(((EnemyInfo)enemy).m_EnemyCombat.m_RaceTypes),
			Defense = (physical ? ((EnemyInfo)enemy).GetArmor() : ((EnemyInfo)enemy).GetResist()),
			Physical = physical,
			FrozenMultiplier = (((CharacterDummy)enemy).Frozen ? frozen : 1f),
			Evasion = DamageDistribution.ClampProbability(num),
			DamageBlocked = (((CharacterDummy)enemy).Petrified || ((CharacterDummy)enemy).Protected || ((CharacterDummy)enemy).Shielded),
			Reflecting = ((CharacterDummy)enemy).Reflecting,
			EffectChance = (((Object)(object)effect == (Object)null) ? ((double?)null) : new double?(effect.IsImmune((CharacterDummy)(object)enemy) ? 0.0 : DamageDistribution.ClampProbability(proficiency.m_ChanceToAffect)))
		};
		return new TargetPreview
		{
			Name = ((EnemyInfo)enemy).m_EnemyCombat.GetEnemyDisplay(),
			Health = targetRules.Health,
			Distribution = DamageDistribution.Calculate(rules.Slots, outcomes, attack, targetRules),
			ShowEffect = targetRules.EffectChance.HasValue
		};
	}
}
[BepInPlugin("ftk.damagepreview", "For The King Damage Preview", "1.0.1")]
public sealed class Plugin : BaseUnityPlugin
{
	private const string Id = "ftk.damagepreview";

	private static Plugin instance;

	private Harmony harmony;

	private uiBattleStanceButtons panel;

	private uiBattleButton button;

	private List<TargetPreview> targets;

	private float nextRefresh;

	private bool loggedPreviewError;

	private readonly PreviewPanel previewPanel = new PreviewPanel();

	private void Awake()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Expected O, but got Unknown
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Expected O, but got Unknown
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Expected O, but got Unknown
		instance = this;
		harmony = new Harmony("ftk.damagepreview");
		harmony.Patch((MethodBase)AccessTools.Method(typeof(uiBattleStanceButtons), "DisplayBattleActionInfo", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "OnAction", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(uiBattleStanceButtons), "SelectEnemyDummy", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "OnTarget", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(uiBattleStanceButtons), "BattleButtonsOff", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "OnClose", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Damage preview hooks installed.");
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
		instance = null;
	}

	private static void OnAction(uiBattleStanceButtons __instance, uiBattleButton __0, bool __1)
	{
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Invalid comparison between Unknown and I4
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Invalid comparison between Unknown and I4
		if ((Object)(object)instance == (Object)null)
		{
			return;
		}
		if (!__1)
		{
			if ((Object)(object)instance.panel == (Object)(object)__instance && (Object)(object)instance.button == (Object)(object)__0)
			{
				instance.Clear();
			}
		}
		else if ((Object)(object)__0 == (Object)null || ((int)__0.m_ButtonType != 2 && (int)__0.m_ButtonType != 4))
		{
			instance.Clear();
		}
		else
		{
			instance.panel = __instance;
			instance.button = __0;
			instance.nextRefresh = 0f;
		}
	}

	private static void OnTarget(uiBattleStanceButtons __instance)
	{
		if ((Object)(object)instance != (Object)null && (Object)(object)instance.panel == (Object)(object)__instance)
		{
			instance.nextRefresh = 0f;
		}
	}

	private static void OnClose(uiBattleStanceButtons __instance)
	{
		if ((Object)(object)instance != (Object)null && (Object)(object)instance.panel == (Object)(object)__instance)
		{
			instance.Clear();
		}
	}

	private void Clear()
	{
		panel = null;
		button = null;
		targets = null;
	}

	private bool IsVisible()
	{
		if ((Object)(object)panel != (Object)null && panel.m_Initialized && !panel.m_Hidden && ((Behaviour)panel).isActiveAndEnabled && (Object)(object)button != (Object)null && ((Behaviour)button).isActiveAndEnabled && button.m_CanUse && (Object)(object)panel.m_InfoPanel != (Object)null)
		{
			return ((Component)panel.m_InfoPanel).gameObject.activeInHierarchy;
		}
		return false;
	}

	private void LateUpdate()
	{
		if (!IsVisible())
		{
			targets = null;
			nextRefresh = 0f;
			return;
		}
		float realtimeSinceStartup = Time.realtimeSinceStartup;
		if (realtimeSinceStartup < nextRefresh)
		{
			return;
		}
		nextRefresh = realtimeSinceStartup + 0.25f;
		try
		{
			targets = GamePreview.Read(panel, button);
			loggedPreviewError = false;
		}
		catch (Exception ex)
		{
			targets = null;
			if (!loggedPreviewError)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Couldn't read this attack's preview: " + ex));
			}
			loggedPreviewError = true;
		}
	}

	private void OnGUI()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Invalid comparison between Unknown and I4
		if ((int)Event.current.type == 7 && targets != null && IsVisible())
		{
			PreviewPanel obj = previewPanel;
			Transform transform = ((Component)panel.m_InfoPanel).transform;
			obj.Draw((RectTransform)(object)((transform is RectTransform) ? transform : null), targets);
		}
	}
}
internal sealed class PreviewPanel
{
	private const int Width = 326;

	private const int Row = 20;

	private static readonly Color Background = Color32.op_Implicit(new Color32((byte)33, (byte)31, (byte)30, byte.MaxValue));

	private static readonly Color FrameShadow = Color32.op_Implicit(new Color32((byte)18, (byte)16, (byte)15, byte.MaxValue));

	private static readonly Color Border = Color32.op_Implicit(new Color32((byte)189, (byte)182, (byte)157, byte.MaxValue));

	private static readonly Color FrameInset = Color32.op_Implicit(new Color32((byte)69, (byte)62, (byte)53, byte.MaxValue));

	private static readonly Color Divider = Color32.op_Implicit(new Color32((byte)96, (byte)87, (byte)72, byte.MaxValue));

	private static readonly Color AlternateRow = Color32.op_Implicit(new Color32((byte)43, (byte)39, (byte)35, byte.MaxValue));

	private static readonly Color Text = Color32.op_Implicit(new Color32((byte)248, (byte)243, (byte)229, byte.MaxValue));

	private static readonly Color Muted = Color32.op_Implicit(new Color32((byte)197, (byte)188, (byte)168, byte.MaxValue));

	private static readonly Color Lethal = Color32.op_Implicit(new Color32((byte)230, (byte)182, (byte)102, byte.MaxValue));

	private readonly Vector3[] corners = (Vector3[])(object)new Vector3[4];

	private GUIStyle label;

	internal void Draw(RectTransform anchor, List<TargetPreview> targets)
	{
		//IL_0310: Unknown result type (might be due to invalid IL or missing references)
		//IL_0317: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Expected O, but got Unknown
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_016a: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_0208: Unknown result type (might be due to invalid IL or missing references)
		//IL_0238: Unknown result type (might be due to invalid IL or missing references)
		//IL_0258: Unknown result type (might be due to invalid IL or missing references)
		//IL_0294: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e9: Unknown result type (might be due to invalid IL or missing references)
		if (targets.Count == 0)
		{
			return;
		}
		if (label == null)
		{
			label = new GUIStyle(GUI.skin.label)
			{
				clipping = (TextClipping)1,
				richText = false
			};
		}
		TargetPreview targetPreview = targets[0];
		DamageDistribution distribution = targetPreview.Distribution;
		int num = 35 + (targetPreview.ShowEffect ? 20 : 0);
		int num2 = num + 20 * (1 + distribution.Damage.Count) + 8;
		int num3 = num2;
		if (targets.Count > 1)
		{
			num3 += 12 + 20 * targets.Count;
		}
		float num4 = Mathf.Min(Mathf.Clamp((float)Screen.height / 900f, 0.75f, 1.25f), Mathf.Min(((float)Screen.width - 24f) / 326f, ((float)Screen.height - 24f) / (float)num3));
		if (num4 <= 0f)
		{
			return;
		}
		Vector2 val = Place(anchor, 326f * num4, (float)num3 * num4);
		Matrix4x4 matrix = GUI.matrix;
		Color color = GUI.color;
		GUI.matrix = Matrix4x4.TRS(new Vector3(val.x, val.y, 0f), Quaternion.identity, new Vector3(num4, num4, 1f));
		try
		{
			DrawFrame(num3);
			Label(12f, 11f, 98f, "Avg " + distribution.ExpectedDamage.ToString("0.0"), 13, Text);
			Label(113f, 11f, 96f, "Kill " + Percent(distribution.KillChance), 13, Lethal);
			Label(218f, 11f, 96f, "Perfect " + Percent(distribution.PerfectChance), 13, Text, right: true);
			if (targetPreview.ShowEffect)
			{
				Label(12f, 31f, 302f, "Effect on target " + Percent(distribution.EffectChance), 13, Text);
			}
			DrawDamageTable(targetPreview, num);
			if (targets.Count > 1)
			{
				Fill(12f, num2, 302f, 1f, Divider);
				Label(12f, num2 + 4, 200f, "Other targets", 11, Muted);
				int num5 = num2 + 4 + 20;
				int num6 = 1;
				while (num6 < targets.Count)
				{
					TargetPreview targetPreview2 = targets[num6];
					Label(12f, num5, 162f, targetPreview2.Name, 12, Text);
					Label(178f, num5, 136f, "Avg " + targetPreview2.Distribution.ExpectedDamage.ToString("0.0") + "   Kill " + Percent(targetPreview2.Distribution.KillChance), 12, Muted, right: true);
					num6++;
					num5 += 20;
				}
			}
		}
		finally
		{
			GUI.matrix = matrix;
			GUI.color = color;
		}
	}

	private static void DrawFrame(int height)
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		Fill(0f, 0f, 326f, height, FrameShadow);
		Fill(2f, 2f, 322f, height - 4, Border);
		Fill(3f, 3f, 320f, height - 6, FrameInset);
		Fill(4f, 4f, 318f, height - 8, Background);
		int num = height - 2 - 5;
		Fill(2f, 2f, 5f, 5f, Border);
		Fill(319f, 2f, 5f, 5f, Border);
		Fill(2f, num, 5f, 5f, Border);
		Fill(319f, num, 5f, 5f, Border);
	}

	private void DrawDamageTable(TargetPreview target, int y)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		Label(18f, y, 102f, "Damage", 12, Muted);
		Label(128f, y, 84f, "Exact", 12, Muted, right: true);
		Label(224f, y, 84f, "At least", 12, Muted, right: true);
		y += 20;
		Fill(12f, y - 1, 302f, 1f, Divider);
		DamageDistribution distribution = target.Distribution;
		bool flag = true;
		foreach (KeyValuePair<int, double> item in distribution.Damage)
		{
			if (flag)
			{
				Fill(12f, y, 302f, 20f, AlternateRow);
			}
			Color color = ((item.Key >= target.Health) ? Lethal : Text);
			Label(18f, y, 102f, item.Key.ToString(), 13, color);
			Label(128f, y, 84f, Percent(item.Value), 13, Text, right: true);
			Label(224f, y, 84f, Percent(distribution.ProbabilityAtLeast(item.Key)), 13, Text, right: true);
			y += 20;
			flag = !flag;
		}
	}

	private Vector2 Place(RectTransform anchor, float width, float height)
	{
		//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: Unknown result type (might be due to invalid IL or missing references)
		//IL_016c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0160: Unknown result type (might be due to invalid IL or missing references)
		//IL_0191: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)anchor != (Object)null)
		{
			Canvas componentInParent = ((Component)anchor).GetComponentInParent<Canvas>();
			Camera val = (((Object)(object)componentInParent != (Object)null && (int)componentInParent.renderMode != 0) ? componentInParent.worldCamera : null);
			anchor.GetWorldCorners(corners);
			Vector2 val2 = RectTransformUtility.WorldToScreenPoint(val, corners[0]);
			Vector2 val3 = RectTransformUtility.WorldToScreenPoint(val, corners[2]);
			float num = (float)Screen.height - val3.y;
			Transform obj = ((Transform)anchor).Find("DescriptionBG");
			RectTransform val4 = (RectTransform)(object)((obj is RectTransform) ? obj : null);
			if ((Object)(object)val4 != (Object)null)
			{
				val4.GetWorldCorners(corners);
				num = (float)Screen.height - RectTransformUtility.WorldToScreenPoint(val, corners[2]).y;
			}
			float num2 = Mathf.Clamp(num, 12f, (float)Screen.height - height - 12f);
			if (val3.x + width + 24f <= (float)Screen.width)
			{
				return new Vector2(val3.x + 12f, num2);
			}
			if (val2.x - width - 24f >= 0f)
			{
				return new Vector2(val2.x - width - 12f, num2);
			}
			float num3 = Mathf.Clamp(val2.x, 12f, (float)Screen.width - width - 12f);
			if (num - height >= 24f)
			{
				return new Vector2(num3, num - height - 12f);
			}
			float num4 = (float)Screen.height - val2.y;
			if (num4 + height + 24f <= (float)Screen.height)
			{
				return new Vector2(num3, num4 + 12f);
			}
		}
		return new Vector2((float)Screen.width - width - 12f, 12f);
	}

	private static string Percent(double value)
	{
		if (value > 0.0 && value < 0.001)
		{
			return "<0.1%";
		}
		if (value < 0.999999999999 && value > 0.999)
		{
			return ">99.9%";
		}
		return DamageDistribution.ClampProbability(value).ToString("0.#%");
	}

	private void Label(float x, float y, float width, string value, int fontSize, Color color, bool right = false)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		GUI.color = Color.white;
		label.fontSize = fontSize;
		label.normal.textColor = color;
		label.alignment = (TextAnchor)(right ? 5 : 3);
		GUI.Label(new Rect(x, y, width, 20f), value, label);
	}

	private static void Fill(float x, float y, float width, float height, Color color)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		GUI.color = color;
		GUI.DrawTexture(new Rect(x, y, width, height), (Texture)(object)Texture2D.whiteTexture);
	}
}