From 44ef09f0dcd384a8827ef8d1e11a9ee9ef62bc79 Mon Sep 17 00:00:00 2001 From: Ozzatron Date: Thu, 7 Apr 2022 12:37:22 -0400 Subject: [PATCH] 1 - Added fractions to DropHelper because float drop rates aren't supported. 2 - Fixed Blighted Eye drops. --- NPCs/NormalNPCs/BlightedEye.cs | 4 +- Utilities/DropHelper.cs | 86 +++++++++++++++++++++++++++++++--- 2 files changed, 81 insertions(+), 9 deletions(-) diff --git a/NPCs/NormalNPCs/BlightedEye.cs b/NPCs/NormalNPCs/BlightedEye.cs index 05375dfca4..3ae6cba72a 100644 --- a/NPCs/NormalNPCs/BlightedEye.cs +++ b/NPCs/NormalNPCs/BlightedEye.cs @@ -64,9 +64,9 @@ namespace CalamityMod.NPCs.NormalNPCs player.AddBuff(BuffID.Weak, 180, true); } - public override void NPCLoot() + public override void ModifyNPCLoot(NPCLoot npcLoot) { - DropHelper.DropItemChance(NPC, ModContent.ItemType(), 0.5f); + npcLoot.Add(ModContent.ItemType(), 2); } } } diff --git a/Utilities/DropHelper.cs b/Utilities/DropHelper.cs index cb5de20501..02c7589009 100644 --- a/Utilities/DropHelper.cs +++ b/Utilities/DropHelper.cs @@ -11,6 +11,20 @@ namespace CalamityMod { public static class DropHelper { + #region Fraction Struct (thanks Yorai) + public struct Fraction + { + internal readonly int numerator = 1; + internal readonly int denominator = 1; + + public Fraction(int n, int d) + { + numerator = n < 0 ? 0 : n; + denominator = d <= 0 ? 1 : d; + } + } + #endregion + #region Global Drop Chances /// @@ -19,9 +33,9 @@ namespace CalamityMod public const int NormalWeaponDropRateInt = 4; /// - /// Weapons in Normal Mode typically have this chance to drop (decimal number out of 1.0). + /// Weapons in Normal Mode typically have this chance to drop (as a DropHelper Fraction). /// - public const float NormalWeaponDropRateFloat = 0.25f; + public static readonly Fraction NormalWeaponDropRateFraction = new(1, NormalWeaponDropRateInt); /// /// Weapons in Expert Mode typically have a 1 in X chance of dropping, where X is this variable. @@ -29,9 +43,9 @@ namespace CalamityMod public const int BagWeaponDropRateInt = 3; /// - /// Weapons in Expert Mode typically have this chance to drop (decimal number out of 1.0). + /// Weapons in Expert Mode typically have this chance to drop (as a DropHelper Fraction). /// - public const float BagWeaponDropRateFloat = 0.3333333f; + public static readonly Fraction BagWeaponDropRateFraction = new(1, BagWeaponDropRateInt); #endregion #region Block Drops @@ -141,8 +155,7 @@ namespace CalamityMod #endregion #region Lambda Drop Rule Condition - - // This class serves as a vanilla drop rule condition that is based on an arbitrary bool. + // This class serves as a vanilla drop rule condition that is based on completely arbitrary code. // Create these using the function DropHelper.If as needed. internal class LambdaDropRuleCondition : IItemDropRuleCondition { @@ -163,6 +176,7 @@ namespace CalamityMod } public static IItemDropRuleCondition If(Func lambda) => new LambdaDropRuleCondition(lambda); + public static IItemDropRuleCondition If(Func lambda, bool ui = true, string desc = "") => new LambdaDropRuleCondition(lambda, ui, desc); #endregion #region Leading Condition Rule Extensions @@ -187,11 +201,26 @@ namespace CalamityMod /// The minimum number of items to drop. Defaults to 1. /// The maximum number of items to drop. Defaults to 1. /// Set to true for this drop to not appear in the Bestiary. - /// + /// The LeadingConditionRule (first parameter). public static IItemDropRule Add(this LeadingConditionRule mainRule, int itemID, int dropRateInt = 1, int minQuantity = 1, int maxQuantity = 1, bool hideLootReport = false) { return mainRule.OnSuccess(ItemDropRule.Common(itemID, dropRateInt, minQuantity, maxQuantity), hideLootReport); } + + /// + /// Shorthand to add a simple drop to the given LeadingConditionRule using a Fraction drop rate. + /// + /// The LeadingConditionRule which should drop this item as one of its chains. + /// The item to drop. + /// The chance that the item will drop as a DropHelper Fraction. + /// The minimum number of items to drop. Defaults to 1. + /// The maximum number of items to drop. Defaults to 1. + /// Set to true for this drop to not appear in the Bestiary. + /// The LeadingConditionRule (first parameter). + public static IItemDropRule Add(this LeadingConditionRule mainRule, int itemID, Fraction dropRate, int minQuantity = 1, int maxQuantity = 1, bool hideLootReport = false) + { + return mainRule.OnSuccess(new CommonDrop(itemID, dropRate.denominator, minQuantity, maxQuantity, dropRate.numerator), hideLootReport); + } #endregion #region NPCLoot Extensions @@ -209,6 +238,20 @@ namespace CalamityMod return npcLoot.Add(ItemDropRule.Common(itemID, dropRateInt, minQuantity, maxQuantity)); } + /// + /// Shorthand to add a simple drop to an NPC using a Fraction drop rate. + /// + /// The NPC's NPCLoot object. + /// The item to drop. + /// The chance that the item will drop as a DropHelper Fraction. + /// The minimum number of items to drop. Defaults to 1. + /// The maximum number of items to drop. Defaults to 1. + /// The item drop rule registered. + public static IItemDropRule Add(this NPCLoot npcLoot, int itemID, Fraction dropRate, int minQuantity = 1, int maxQuantity = 1) + { + return npcLoot.Add(new CommonDrop(itemID, dropRate.denominator, minQuantity, maxQuantity, dropRate.numerator)); + } + /// /// Shorthand to add an arbitrary conditional drop to an NPC. /// @@ -224,6 +267,21 @@ namespace CalamityMod return npcLoot.Add(ItemDropRule.ByCondition(If(lambda), itemID, dropRateInt, minQuantity, maxQuantity)); } + /// + /// Shorthand to add an arbitrary conditional drop to an NPC using a Fraction drop rate. + /// + /// The NPC's NPCLoot object. + /// A lambda which evaluates in real-time to the condition that needs to be checked. + /// The item to drop. + /// The chance that the item will drop as a DropHelper Fraction. + /// The minimum number of items to drop. Defaults to 1. + /// The maximum number of items to drop. Defaults to 1. + /// The item drop rule registered. + public static IItemDropRule AddIf(this NPCLoot npcLoot, Func lambda, int itemID, Fraction dropRate, int minQuantity = 1, int maxQuantity = 1) + { + return npcLoot.Add(ItemDropRule.ByCondition(If(lambda), itemID, dropRate.denominator, minQuantity, maxQuantity, dropRate.numerator)); + } + /// /// Shorthand to add a simple normal-only drop to an NPC. /// @@ -238,6 +296,20 @@ namespace CalamityMod return npcLoot.Add(ItemDropRule.ByCondition(new Conditions.NotExpert(), itemID, dropRateInt, minQuantity, maxQuantity)); } + /// + /// Shorthand to add a simple normal-only drop to an NPC. + /// + /// The NPC's NPCLoot object. + /// The item to drop. + /// The chance that the item will drop as a DropHelper Fraction. + /// The minimum number of items to drop. Defaults to 1. + /// The maximum number of items to drop. Defaults to 1. + /// The item drop rule registered. + public static IItemDropRule AddNormalOnly(this NPCLoot npcLoot, int itemID, Fraction dropRate, int minQuantity = 1, int maxQuantity = 1) + { + return npcLoot.Add(ItemDropRule.ByCondition(new Conditions.NotExpert(), itemID, dropRate.denominator, minQuantity, maxQuantity, dropRate.numerator)); + } + /// /// Registers a LeadingConditionRule for an NPC and returns it so you can add drops to that rule. ///