Advertisement
Guest User

LegendaryItemAffixPlugin

a guest
Dec 15th, 2020
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.73 KB | None | 0 0
  1. using Turbo.Plugins.Default;
  2. using System.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6.  
  7. namespace Turbo.Plugins.Miqui
  8. {
  9.  
  10.     public enum AffixType
  11.     {
  12.         PERCENTAGE,
  13.         SECONDS,
  14.         NO_UNIT,
  15.         USELESS
  16.     }
  17.  
  18.     public class ItemAffixInfo
  19.     {
  20.         public uint Sno { get; }
  21.         public AffixType AffixType { get; }
  22.         public string AssociatedCode { get; }
  23.  
  24.         private ItemAffixInfo(uint sno, AffixType affixType) : this(sno, affixType, "Item_Power_Passive") { }
  25.  
  26.         public ItemAffixInfo(uint sno, AffixType affixType, string associatedCode)
  27.         {
  28.             Sno = sno;
  29.             AffixType = affixType;
  30.             AssociatedCode = associatedCode;
  31.         }
  32.  
  33.         public static ItemAffixInfo PercentageFromSno(uint sno)
  34.         {
  35.             return new ItemAffixInfo(sno, AffixType.PERCENTAGE);
  36.         }
  37.  
  38.         public static ItemAffixInfo SecondFromSno(uint sno)
  39.         {
  40.             return new ItemAffixInfo(sno, AffixType.SECONDS);
  41.         }
  42.  
  43.         public static ItemAffixInfo NoUnitFromSno(uint sno)
  44.         {
  45.             return new ItemAffixInfo(sno, AffixType.NO_UNIT);
  46.         }
  47.     }
  48.  
  49.     public class LegendaryItemAffixPlugin : BasePlugin, IInGameTopPainter, ICustomizer
  50.     {
  51.  
  52.         private bool debug = false;
  53.         private bool LoggedStats = false;
  54.         private long LastLog { get; set; }
  55.  
  56.         public IFont LegendaryAffixFont { get; set; }
  57.  
  58.         public bool ShowOnUnidentifiedOnly { get; set; } = false;
  59.  
  60.         public Dictionary<uint, ItemAffixInfo> ItemAffixInfos { get; set; }
  61.  
  62.         public LegendaryItemAffixPlugin()
  63.         {
  64.             Enabled = true;
  65.         }
  66.  
  67.         public override void Load(IController hud)
  68.         {
  69.             base.Load(hud);
  70.  
  71.             LegendaryAffixFont = Hud.Render.CreateFont("arial", 7, 255, 0, 0, 0, true, false, false);
  72.             LegendaryAffixFont.SetShadowBrush(128, 111, 134, 252, true);
  73.         }
  74.  
  75.         public void PaintTopInGame(ClipState clipState)
  76.         {
  77.             if (clipState != ClipState.Inventory)
  78.                 return;
  79.  
  80.             int stashPage, stashTab, stashTabAbs;
  81.  
  82.             stashTab = Hud.Inventory.SelectedStashTabIndex;
  83.             stashPage = Hud.Inventory.SelectedStashPageIndex;
  84.             stashTabAbs = stashTab + (stashPage * Hud.Inventory.MaxStashTabCountPerPage);
  85.  
  86.             var items = Hud.Game.Items.Where(x => x.Location == ItemLocation.Inventory || x.Location == ItemLocation.Stash || x.Location.IsEquipped());
  87.             foreach (var item in items)
  88.             {
  89.                 if (ShowOnUnidentifiedOnly && !item.Unidentified)
  90.                     continue;
  91.  
  92.                 if (item.SocketedInto != null)
  93.                     continue;
  94.  
  95.                 if (item.Location == ItemLocation.Stash)
  96.                 {
  97.                     var tabIndex = item.InventoryY / 10;
  98.                     if (tabIndex != stashTabAbs)
  99.                         continue;
  100.                 }
  101.  
  102.                 if ((item.InventoryX < 0) || (item.InventoryY < 0))
  103.                     continue;
  104.  
  105.                 Log(" **************************************** " + item.FullNameEnglish + " (sno:" + item.SnoActor.Sno + ")");
  106.  
  107.                 if (isItemLegendaryGem(item))
  108.                     continue;
  109.  
  110.                 ItemAffixInfo legendaryItemAffixInfo;
  111.                 bool wasFound = ItemAffixInfos.TryGetValue((uint)item.SnoActor.Sno, out legendaryItemAffixInfo);
  112.                 if (!wasFound)
  113.                     continue;
  114.  
  115.                 IItem[] gems = item.ItemsInSocket;
  116.                 // The list evolves afterwards : once a stat has been removed, we don't remove it again
  117.                 // The goal of that is : if a leg. gem has the same affix value as the legendary affix of the ring/amulet, the legendary affix of the ring/amulet is still shown
  118.                 List<IItemStat> gemStatsToRemove = getGemStatsSameCode(gems, legendaryItemAffixInfo);
  119.  
  120.                 if (legendaryItemAffixInfo.AffixType != AffixType.USELESS && legendaryItemAffixInfo.AssociatedCode != null)
  121.                     foreach (var stat in item.StatList)
  122.                     {
  123.                         bool drawn = DrawLegendaryAffix(item, stat, legendaryItemAffixInfo, gemStatsToRemove);
  124.                         if (drawn)
  125.                             break;
  126.                     }
  127.                 Log("****************************************");
  128.             }
  129.             LoggedStats = true;
  130.         }
  131.  
  132.         private List<IItemStat> getGemStatsSameCode(IItem[] gems, ItemAffixInfo legendaryItemAffixInfo)
  133.         {
  134.             List<IItemStat> stats = new List<IItemStat>();
  135.             if (gems != null && gems.Count() == 1)
  136.             {
  137.                 foreach (var gemStat in gems[0].StatList)
  138.                 {
  139.                     if (gemStat.Attribute != null && gemStat.Attribute.Code != null && gemStat.Attribute.Code.Equals(legendaryItemAffixInfo.AssociatedCode))
  140.                     {
  141.                         stats.Add(gemStat);
  142.                         Log(gemStat.Attribute.Code + " " + gemStat.DoubleValue);
  143.                     }
  144.                 }
  145.             }
  146.             return stats;
  147.         }
  148.  
  149.         private bool isItemLegendaryGem(IItem item)
  150.         {
  151.             bool isLegendaryGem = false;
  152.             foreach (var stat in item.StatList)
  153.             {
  154.                 if (stat.Attribute != null && stat.Attribute.Code != null && stat.Attribute.Code.Equals("Jewel_Rank"))
  155.                     isLegendaryGem = true;
  156.             }
  157.             return isLegendaryGem;
  158.         }
  159.  
  160.         private bool DrawLegendaryAffix(IItem item, IItemStat stat, ItemAffixInfo legendaryItemAffixInfo, List<IItemStat> gemStatsToRemove)
  161.         {
  162.             if (stat.Attribute != null && stat.Attribute.Code != null)
  163.             {
  164.                 Log("" + stat.Attribute.Code + " : " + stat.DoubleValue + (stat.IntegerValue != null ? ("(" + stat.IntegerValue + ")") : ""));
  165.                 if (legendaryItemAffixInfo.AssociatedCode != null && stat.Attribute.Code.Equals(legendaryItemAffixInfo.AssociatedCode))
  166.                 {
  167.                     if (gemStatsToRemove.Count() > 0)
  168.                     {
  169.                         for (int i = 0; i < gemStatsToRemove.Count(); i++)
  170.                         {
  171.                             if (gemStatsToRemove[i].DoubleValue.Equals(stat.DoubleValue))
  172.                             {
  173.                                 // Remove in the gem stat List to make sure we still show the stat if the legendary gem & item have the same stat value
  174.                                 Log("Removed : " + gemStatsToRemove[i].Attribute.Code + " : " + gemStatsToRemove[i].DoubleValue);
  175.                                 gemStatsToRemove.RemoveAt(i);
  176.                                 return false;
  177.                             }
  178.                         }
  179.                     }
  180.                     try
  181.                     {
  182.                         double multiplyBy = legendaryItemAffixInfo.AffixType == AffixType.PERCENTAGE ? 100d : 1d;
  183.                         string unit;
  184.                         if (legendaryItemAffixInfo.AffixType == AffixType.PERCENTAGE)
  185.                             unit = "%";
  186.                         else if (legendaryItemAffixInfo.AffixType == AffixType.SECONDS)
  187.                             unit = "s";
  188.                         else
  189.                             unit = "";
  190.  
  191.                         if (!handleSpecialCases(legendaryItemAffixInfo, stat))
  192.                             return false;
  193.  
  194.                         double val = stat.DoubleValue * multiplyBy;
  195.  
  196.                         // Leoric's crown is annoying with rounding so we round ourselves
  197.                         if (legendaryItemAffixInfo.Sno == (uint)ActorSnoEnum._helm_norm_unique_01)
  198.                             val = Convert.ToInt32(val);
  199.  
  200.                         var rect = Hud.Inventory.GetItemRect(item);
  201.                         var text = val.ToString("#.##", CultureInfo.CreateSpecificCulture("en-GB")) + unit;
  202.                         var layout = LegendaryAffixFont.GetTextLayout(text);
  203.                         LegendaryAffixFont.DrawText(layout, rect.X, rect.Y);
  204.                         return true; // Set to false if you want to dislay all info of items in debug
  205.                     }
  206.                     catch (OverflowException)
  207.                     {
  208.                         // Don't do this
  209.                     }
  210.                 }
  211.             }
  212.             return false;
  213.         }
  214.  
  215.         private bool handleSpecialCases(ItemAffixInfo legendaryItemAffixInfo, IItemStat stat)
  216.         {
  217.             // Witching hour
  218.             if (legendaryItemAffixInfo.Sno == (uint)ActorSnoEnum._belt_norm_unique_07)
  219.                 if (stat.DoubleValue > 1)
  220.                     return false;
  221.  
  222.             // Tzo krin
  223.             if (legendaryItemAffixInfo.Sno == (uint)ActorSnoEnum._spiritstone_norm_unique_12)
  224.                 if (stat.IntegerValue == 0)
  225.                     return false;
  226.  
  227.             return true;
  228.         }
  229.  
  230.         private void Log(string toLog)
  231.         {
  232.             if (!debug)
  233.                 return;
  234.  
  235.             if (toLog == null || toLog.Equals(""))
  236.                 return;
  237.  
  238.             if (Hud.Game.CurrentRealTimeMilliseconds - LastLog > 10 * 1000)
  239.                 LoggedStats = false;
  240.  
  241.             if (!LoggedStats)
  242.             {
  243.                 Hud.TextLog.Log("itemLog", toLog);
  244.                 LastLog = Hud.Game.CurrentRealTimeMilliseconds;
  245.             }
  246.         }
  247.  
  248.         public void Customize()
  249.         {
  250.             if (!ShowOnUnidentifiedOnly)
  251.                 Hud.RunOnPlugin<InventoryAndStashPlugin>(plugin => plugin.SocketedLegendaryGemRankEnabled = false);
  252.         }
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement