using CalamityMod.Items.Accessories;
using CalamityMod.World;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod
{
#region Weighted Item Stack Struct
// TODO -- DropHelper will need to be fully retooled in 1.4 to utilize this struct for all functions.
public struct WeightedItemStack
{
public const float DefaultWeight = 1f;
public const float MinisiculeWeight = 1E-6f;
internal int itemID;
internal float weight;
internal int minQuantity;
internal int maxQuantity;
internal WeightedItemStack(int id, float w)
{
itemID = id;
weight = w;
minQuantity = 1;
maxQuantity = 1;
}
internal WeightedItemStack(int id, float w, int quantity)
{
itemID = id;
weight = w;
minQuantity = quantity;
maxQuantity = quantity;
}
internal WeightedItemStack(int id, float w, int min, int max)
{
itemID = id;
weight = w;
minQuantity = min;
maxQuantity = max;
}
internal int ChooseQuantity() => Main.rand.Next(minQuantity, maxQuantity + 1);
}
#endregion
public static class DropHelper
{
#region Global Drop Chances
///
/// Weapons in Normal Mode typically have a 1 in X chance of dropping, where X is this variable.
///
public const int NormalWeaponDropRateInt = 4;
///
/// Weapons in Normal Mode typically have this chance to drop (decimal number out of 1.0).
///
public const float NormalWeaponDropRateFloat = 0.25f;
///
/// Weapons in Expert Mode typically have a 1 in X chance of dropping, where X is this variable.
///
public const int BagWeaponDropRateInt = 3;
///
/// Weapons in Expert Mode typically have this chance to drop (decimal number out of 1.0).
///
public const float BagWeaponDropRateFloat = 0.3333333f;
#endregion
#region Block Drops
///
/// Adds the specified items to TML's blockLoot list. Items on the list cannot spawn in the world via any means.
/// This is used to prevent vanilla loot code from spawning certain items.
/// You should only use this function inside NPCLoot or bag opening code. TML will clear the list for you when the loot event is over.
///
/// The item IDs to prevent from spawning.
public static void BlockDrops(params int[] itemIDs)
{
foreach (int itemID in itemIDs)
NPCLoader.blockLoot.Add(itemID);
}
#endregion
#region Weighted Item Sets
// int itemID --> WeightedItemStack
public static WeightedItemStack WeightStack(this int itemID) => WeightStack(itemID, WeightedItemStack.DefaultWeight);
public static WeightedItemStack WeightStack(this int itemID, float weight) => new WeightedItemStack(itemID, weight);
public static WeightedItemStack WeightStack(this int itemID, int quantity) => WeightStack(itemID, WeightedItemStack.DefaultWeight, quantity);
public static WeightedItemStack WeightStack(this int itemID, float weight, int quantity) => new WeightedItemStack(itemID, weight, quantity);
public static WeightedItemStack WeightStack(this int itemID, int min, int max) => WeightStack(itemID, WeightedItemStack.DefaultWeight, min, max);
public static WeightedItemStack WeightStack(this int itemID, float weight, int min, int max) => new WeightedItemStack(itemID, weight, min, max);
// ModItem generic parameter --> WeightedItemStack
public static WeightedItemStack WeightStack() where T : ModItem => WeightStack(WeightedItemStack.DefaultWeight);
public static WeightedItemStack WeightStack(float weight) where T : ModItem => WeightStack(ModContent.ItemType(), weight);
public static WeightedItemStack WeightStack(int quantity) where T : ModItem => WeightStack(WeightedItemStack.DefaultWeight, quantity);
public static WeightedItemStack WeightStack(float weight, int quantity) where T : ModItem => WeightStack(ModContent.ItemType(), weight, quantity);
public static WeightedItemStack WeightStack(int min, int max) where T : ModItem => WeightStack(WeightedItemStack.DefaultWeight, min, max);
public static WeightedItemStack WeightStack(float weight, int min, int max) where T : ModItem => WeightStack(ModContent.ItemType(), weight, min, max);
// Separated implementation used so weighted random code isn't duplicated in two places.
public static WeightedItemStack RollWeightedRandom(WeightedItemStack[] stacks)
{
int i;
float[] breakpoints = new float[stacks.Length];
float totalWeight = 0f;
// Assign breakpoints based on the cumulative sum of weights thus far.
// Error check invalid weights by giving them an unbelievably small drop chance.
for (i = 0; i < stacks.Length; ++i)
{
float w = stacks[i].weight;
if (w <= 0f || float.IsNaN(w) || float.IsInfinity(w))
w = WeightedItemStack.MinisiculeWeight;
breakpoints[i] = totalWeight += w;
}
// Iterate through the breakpoints until you find the first one that is surpassed. Drop that item.
float needle = Main.rand.NextFloat(totalWeight);
i = 0;
while (needle > breakpoints[i])
++i;
return stacks[i];
}
///
/// Chooses an item (or stack of items) from an array of drop definitions and drops it from the given NPC.
/// Each item is given a certain weight to spawn. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item(s).
/// Whether the drop should be "instanced" (each player gets their own copy).
/// The array of drop definitions to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropItemFromWeightedSet(NPC npc, bool dropPerPlayer, params WeightedItemStack[] stacks)
{
// Can't choose anything from an empty array.
if (stacks is null || stacks.Length == 0)
return 0;
WeightedItemStack stk = RollWeightedRandom(stacks);
return DropItem(npc, stk.itemID, dropPerPlayer, stk.minQuantity, stk.maxQuantity);
}
///
/// Chooses an item (or stack of items) from an array of drop definitions and drops it from the given NPC.
/// Each item is given a certain weight to spawn.
///
/// The NPC which should drop the item(s).
/// The array of drop definitions to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropItemFromWeightedSet(NPC npc, params WeightedItemStack[] stacks)
{
return DropItemFromWeightedSet(npc, false, stacks);
}
///
/// Chooses an item (or stack of items) from an array of drop definitions and spawns it for the given player.
/// Each item is given a certain weight to spawn.
///
/// The player which should receive the item(s).
/// The array of drop definitions to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropItemFromWeightedSet(Player p, params WeightedItemStack[] stacks)
{
// Can't choose anything from an empty array.
if (stacks is null || stacks.Length == 0)
return 0;
WeightedItemStack stk = RollWeightedRandom(stacks);
return DropItem(p, stk.itemID, stk.minQuantity, stk.maxQuantity);
}
///
/// Rolls for each item (or stack of items) in an array of drop definitions to drop at their defined chances.
/// Always drops at least one of the defined stacks. Optionally spawns one copy of these drops per player.
///
/// The NPC which should drop the items.
/// Whether the drops should be "instanced" (each player gets their own copy).
/// The array of drop definitions to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropEntireWeightedSet(NPC npc, bool dropPerPlayer, params WeightedItemStack[] stacks)
{
int numDrops = 0;
// Can't choose anything from an empty array.
if (stacks is null || stacks.Length == 0)
return numDrops;
for (int i = 0; i < stacks.Length; ++i)
{
WeightedItemStack stk = stacks[i];
numDrops += DropItemChance(npc, stk.itemID, dropPerPlayer, stk.weight, stk.minQuantity, stk.maxQuantity);
}
// If nothing at all was dropped, drop one thing at (weighted) random.
if (numDrops <= 0)
numDrops += DropItemFromWeightedSet(npc, dropPerPlayer, stacks);
return numDrops;
}
///
/// Rolls for each item (or stack of items) in an array of drop definitions to drop at their defined chances.
/// Always drops at least one of the defined stacks.
///
/// The NPC which should drop the items.
/// The array of drop definitions to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropEntireWeightedSet(NPC npc, params WeightedItemStack[] stacks)
{
return DropEntireWeightedSet(npc, false, stacks);
}
///
/// Rolls for each item (or stack of items) in an array of drop definitions to drop at their defined chances.
/// Always drops at least one of the defined stacks.
///
/// The player which should receive the item(s).
/// The array of drop definitions to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropEntireWeightedSet(Player p, params WeightedItemStack[] stacks)
{
int numDrops = 0;
// Can't choose anything from an empty array.
if (stacks is null || stacks.Length == 0)
return numDrops;
for (int i = 0; i < stacks.Length; ++i)
{
WeightedItemStack stk = stacks[i];
numDrops += DropItemChance(p, stk.itemID, stk.weight, stk.minQuantity, stk.maxQuantity);
}
// If nothing at all was dropped, drop one thing at (weighted) random.
if (numDrops <= 0)
numDrops += DropItemFromWeightedSet(p, stacks);
return numDrops;
}
#endregion
#region Boss Bag Drop Helpers
///
/// Automatically drops the correct number of boss bags for each difficulty based on constants kept in DropHelper.
///
/// The NPC to drop boss bags for.
/// The number of boss bags dropped.
public static int DropBags(NPC theBoss)
{
int bagsDropped = 0;
// Don't drop any bags for an invalid NPC.
if (theBoss is null)
return bagsDropped;
// If the difficulty isn't Expert+, no more bags are dropped.
if (!Main.expertMode)
return bagsDropped;
// Drop the 1 vanilla Expert Mode boss bag.
theBoss.DropBossBags();
bagsDropped++;
return bagsDropped;
}
#endregion
#region Specific Drop Helpers
// Code copied from Player.QuickSpawnClonedItem, which was added by TML.
///
/// Clones the given item and spawns it into the world at the given position. You can also customize stack count as necessary.
/// The default stack count of -1 makes it copy the stack count of the given item.
///
/// The item to clone and spawn.
/// Where the item should be spawned.
/// The stack count to use. Leave at -1 to use the stack of the item parameter.
/// The spawned clone of the item. NEVER equal to the input item.
public static Item DropItemClone(Item item, Vector2 position, int stack = -1)
{
int index = Item.NewItem(position, item.type, stack, false, -1, false, false);
Item theClone = Main.item[index] = item.Clone();
theClone.whoAmI = index;
theClone.position = position;
if (stack != -1)
theClone.stack = stack;
// If in multiplayer, broadcast that this item was spawned.
if (Main.netMode == NetmodeID.MultiplayerClient)
NetMessage.SendData(MessageID.SyncItem, -1, -1, null, index, 1f);
return theClone;
}
///
/// Finds the worm segment nearest to an NPC's target by combing the NPC array for the closest NPC that is one of the specified types.
/// Return the specified NPC's index if no matching worm segment was found.
///
/// The NPC whose target is used for distance comparisons.
/// An array (or multiple parameters) of NPC IDs which are the worm segments to look for.
/// An index in the NPC array of the closest worm segment, or the specified NPC's index.
public static int FindClosestWormSegment(NPC wormHead, params int[] wormSegmentIDs)
{
List idsToCheck = new List(wormSegmentIDs);
Vector2 playerPos = Main.player[wormHead.target].Center;
int r = wormHead.whoAmI;
float minDist = 1E+06f;
for (int i = 0; i < Main.npc.Length; ++i)
{
NPC n = Main.npc[i];
if (n != null && n.active && idsToCheck.Contains(n.type))
{
float dist = (n.Center - playerPos).Length();
if (dist < minDist)
{
minDist = dist;
r = i;
}
}
}
return r;
}
public static bool DropRevBagAccessories(Player p)
{
return DropItemFromSetCondition(p, CalamityWorld.revenge, 0.05f, ModContent.ItemType(), ModContent.ItemType(), ModContent.ItemType());
}
///
/// Randomly peppers stacks of 1 of the specified item all across the given NPC's hitbox.
/// Makes it appear as though the NPC "explodes" into a cloud of many identical items. Best used with floating items such as Souls.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items to place in each separate stack.
/// The number of items dropped. Not always equal to quantity if stack size isn't 1.
public static int DropItemSpray(NPC npc, int itemID, int minQuantity = 1, int maxQuantity = 0, int stackSize = 1)
{
int quantity;
// If they're equal (or for some reason max is less??) then just drop the minimum amount.
if (maxQuantity <= minQuantity)
quantity = minQuantity;
// Otherwise pick a random amount to drop, inclusive.
else
quantity = Main.rand.Next(minQuantity, maxQuantity + 1);
int dropped = 0;
Vector2 pos = Vector2.Zero;
for (int i = 0; i < quantity; i += stackSize)
{
pos.X = Main.rand.NextFloat(npc.Hitbox.Left, npc.Hitbox.Right);
pos.Y = Main.rand.NextFloat(npc.Hitbox.Top, npc.Hitbox.Bottom);
Item.NewItem(pos, itemID, stackSize);
dropped += stackSize;
}
return dropped;
}
#endregion
#region NPC Item Drops 100% Chance
///
/// Drops a stack of one or more items from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItem(NPC npc, int itemID, bool dropPerPlayer, int minQuantity = 1, int maxQuantity = 0)
{
int quantity;
// If they're equal (or for some reason max is less??) then just drop the minimum amount.
if (maxQuantity <= minQuantity)
quantity = minQuantity;
// Otherwise pick a random amount to drop, inclusive.
else
quantity = Main.rand.Next(minQuantity, maxQuantity + 1);
// If the final quantity is 0 or less, don't bother.
if (quantity <= 0)
return 0;
// If the drop is supposed to be instanced, drop it as such.
if (dropPerPlayer)
{
npc.DropItemInstanced(npc.position, npc.Size, itemID, quantity, true);
}
else
{
Item.NewItem(npc.Hitbox, itemID, quantity);
}
return quantity;
}
///
/// Drops a stack of one or more items from the given NPC.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItem(NPC npc, int itemID, int minQuantity = 1, int maxQuantity = 0)
{
return DropItem(npc, itemID, false, minQuantity, maxQuantity);
}
#endregion
#region NPC Item Drops Float Chance
///
/// At a chance, drops a stack of one or more items from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// The chance that the items will drop. A decimal number <= 1.0.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemChance(NPC npc, int itemID, bool dropPerPlayer, float chance, int minQuantity = 1, int maxQuantity = 0)
{
// If you fail the roll to get the drop, stop immediately.
if (Main.rand.NextFloat() > chance)
return 0;
return DropItem(npc, itemID, dropPerPlayer, minQuantity, maxQuantity);
}
///
/// At a chance, drops a stack of one or more items from the given NPC.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// The chance that the items will drop. A decimal number <= 1.0.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemChance(NPC npc, int itemID, float chance, int minQuantity = 1, int maxQuantity = 0)
{
return DropItemChance(npc, itemID, false, chance, minQuantity, maxQuantity);
}
#endregion
#region NPC Item Drops Int Chance
///
/// At a chance, drops a stack of one or more items from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// The chance that the items will drop is 1 in this number. For example, 5 gives a 1 in 5 chance.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemChance(NPC npc, int itemID, bool dropPerPlayer, int oneInXChance, int minQuantity = 1, int maxQuantity = 0)
{
// If you fail the roll to get the drop, stop immediately.
if (Main.rand.Next(oneInXChance) != 0)
return 0;
return DropItem(npc, itemID, dropPerPlayer, minQuantity, maxQuantity);
}
///
/// At a chance, drops a stack of one or more items from the given NPC.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// The chance that the items will drop is 1 in this number. For example, 5 gives a 1 in 5 chance.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemChance(NPC npc, int itemID, int oneInXChance, int minQuantity = 1, int maxQuantity = 0)
{
return DropItemChance(npc, itemID, false, oneInXChance, minQuantity, maxQuantity);
}
#endregion
#region NPC Item Drops Conditional
///
/// With a condition, drops a stack of one or more items from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemCondition(NPC npc, int itemID, bool dropPerPlayer, bool condition, int minQuantity = 1, int maxQuantity = 0)
{
return condition ? DropItem(npc, itemID, dropPerPlayer, minQuantity, maxQuantity) : 0;
}
///
/// With a condition, drops a stack of one or more items from the given NPC.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemCondition(NPC npc, int itemID, bool condition, int minQuantity = 1, int maxQuantity = 0)
{
return condition ? DropItem(npc, itemID, false, minQuantity, maxQuantity) : 0;
}
///
/// With a condition and at a chance, drops a stack of one or more items from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The chance that the items will drop. A decimal number <= 1.0.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemCondition(NPC npc, int itemID, bool dropPerPlayer, bool condition, float chance, int minQuantity = 1, int maxQuantity = 0)
{
return condition ? DropItemChance(npc, itemID, dropPerPlayer, chance, minQuantity, maxQuantity) : 0;
}
///
/// With a condition and at a chance, drops a stack of one or more items from the given NPC.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The chance that the items will drop. A decimal number <= 1.0.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemCondition(NPC npc, int itemID, bool condition, float chance, int minQuantity = 1, int maxQuantity = 0)
{
return condition ? DropItemChance(npc, itemID, false, chance, minQuantity, maxQuantity) : 0;
}
///
/// With a condition and at a chance, drops a stack of one or more items from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The chance that the items will drop is 1 in this number. For example, 5 gives a 1 in 5 chance.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemCondition(NPC npc, int itemID, bool dropPerPlayer, bool condition, int oneInXChance, int minQuantity = 1, int maxQuantity = 0)
{
return condition ? DropItemChance(npc, itemID, dropPerPlayer, oneInXChance, minQuantity, maxQuantity) : 0;
}
///
/// With a condition and at a chance, drops a stack of one or more items from the given NPC.
///
/// The NPC which should drop the item(s).
/// The ID of the item(s) to drop.
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The chance that the items will drop is 1 in this number. For example, 5 gives a 1 in 5 chance.
/// The minimum number of items to drop. Defaults to 1.
/// The maximum number of items to drop. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items dropped.
public static int DropItemCondition(NPC npc, int itemID, bool condition, int oneInXChance, int minQuantity = 1, int maxQuantity = 0)
{
return condition ? DropItemChance(npc, itemID, false, oneInXChance, minQuantity, maxQuantity) : 0;
}
#endregion
#region NPC Item Set Drops
///
/// Chooses an item from an array and drops it from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// Whether an item was dropped.
public static bool DropItemFromSet(NPC npc, bool dropPerPlayer, params int[] itemIDs)
{
// Can't choose anything from an empty array.
if (itemIDs is null || itemIDs.Length == 0)
return false;
// Choose which item to drop.
int itemID = Main.rand.Next(itemIDs);
// If the drop is supposed to be instanced, drop it as such.
if (dropPerPlayer)
{
npc.DropItemInstanced(npc.position, npc.Size, itemID, 1, true);
}
else
{
Item.NewItem(npc.Hitbox, itemID);
}
return true;
}
///
/// Chooses an item from an array and drops it from the given NPC.
///
/// The NPC which should drop the item.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// Whether an item was dropped.
public static bool DropItemFromSet(NPC npc, params int[] itemIDs)
{
return DropItemFromSet(npc, false, itemIDs);
}
///
/// At a chance, chooses an item from an array and drops it from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// The chance that the item will drop. A decimal number <= 1.0.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// Whether an item was dropped.
public static bool DropItemFromSetChance(NPC npc, bool dropPerPlayer, float chance, params int[] itemIDs)
{
// If you fail the roll to get the drop, stop immediately.
if (Main.rand.NextFloat() > chance)
return false;
return DropItemFromSet(npc, dropPerPlayer, itemIDs);
}
///
/// At a chance, chooses an item from an array and drops it from the given NPC.
///
/// The NPC which should drop the item.
/// The chance that the item will drop. A decimal number <= 1.0.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// Whether an item was dropped.
public static bool DropItemFromSetChance(NPC npc, float chance, params int[] itemIDs)
{
return DropItemFromSetChance(npc, false, chance, itemIDs);
}
#endregion
#region NPC Item Set Drops Conditional
///
/// With a condition, chooses an item from an array and drops it from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
public static bool DropItemFromSetCondition(NPC npc, bool dropPerPlayer, bool condition, params int[] itemIDs)
{
return condition ? DropItemFromSet(npc, dropPerPlayer, itemIDs) : false;
}
///
/// With a condition, chooses an item from an array and drops it from the given NPC.
///
/// The NPC which should drop the item.
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
public static bool DropItemFromSetCondition(NPC npc, bool condition, params int[] itemIDs)
{
return condition ? DropItemFromSet(npc, false, itemIDs) : false;
}
///
/// With a condition, chooses an item from an array and drops it from the given NPC. Optionally spawns one copy of this drop per player.
///
/// The NPC which should drop the item.
/// Whether the drop should be "instanced" (each player gets their own copy).
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The chance that the item will drop. A decimal number <= 1.0.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
public static bool DropItemFromSetCondition(NPC npc, bool dropPerPlayer, bool condition, float chance, params int[] itemIDs)
{
return condition ? DropItemFromSetChance(npc, dropPerPlayer, chance, itemIDs) : false;
}
///
/// With a condition, chooses an item from an array and drops it from the given NPC.
///
/// The NPC which should drop the item.
/// Any arbitrary Boolean condition to gate this drop. If false, nothing is dropped.
/// The chance that the item will drop. A decimal number <= 1.0.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
public static bool DropItemFromSetCondition(NPC npc, bool condition, float chance, params int[] itemIDs)
{
return condition ? DropItemFromSetChance(npc, false, chance, itemIDs) : false;
}
#endregion
#region NPC Entire Set Drops
///
/// Rolls for each item in an array to drop at a given chance. Always drops at least one item.
/// Optionally spawns one copy of these drops per player.
///
/// The NPC which should drop the items.
/// Whether the drops should be "instanced" (each player gets their own copy).
/// The chance that an item will drop. A decimal number <= 1.0.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropEntireSet(NPC npc, bool dropPerPlayer, float chance, params int[] itemIDs)
{
int numDrops = 0;
// Can't choose anything from an empty array.
if (itemIDs is null || itemIDs.Length == 0)
return numDrops;
// Tally the total number of items dropped as the drop set is iterated through.
for (int i = 0; i < itemIDs.Length; ++i)
numDrops += DropItemChance(npc, itemIDs[i], dropPerPlayer, chance);
// If nothing at all was dropped, drop one thing at random.
numDrops += DropItemFromSetCondition(npc, dropPerPlayer, numDrops <= 0, itemIDs) ? 1 : 0;
return numDrops;
}
///
/// Rolls for each item in an array to drop at a given chance. Always drops at least one item.
/// Optionally spawns one copy of these drops per player.
///
/// The NPC which should drop the items.
/// Whether the drops should be "instanced" (each player gets their own copy).
/// The chance that the items will spawn is 1 in this number. For example, 5 gives a 1 in 5 chance.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropEntireSet(NPC npc, bool dropPerPlayer, int oneInXChance, params int[] itemIDs)
{
int numDrops = 0;
// Can't choose anything from an empty array.
if (itemIDs is null || itemIDs.Length == 0)
return numDrops;
// Tally the total number of items dropped as the drop set is iterated through.
for (int i = 0; i < itemIDs.Length; ++i)
numDrops += DropItemChance(npc, itemIDs[i], dropPerPlayer, oneInXChance);
// If nothing at all was dropped, drop one thing at random.
numDrops += DropItemFromSetCondition(npc, dropPerPlayer, numDrops <= 0, itemIDs) ? 1 : 0;
return numDrops;
}
///
/// Rolls for each item in an array to drop at a given chance. Always drops at least one item.
///
/// The NPC which should drop the items.
/// The chance that an item will drop. A decimal number <= 1.0.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropEntireSet(NPC npc, float chance, params int[] itemIDs)
{
return DropEntireSet(npc, false, chance, itemIDs);
}
///
/// Rolls for each item in an array to drop at a given chance. Always drops at least one item.
///
/// The NPC which should drop the items.
/// The chance that the items will spawn is 1 in this number. For example, 5 gives a 1 in 5 chance.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropEntireSet(NPC npc, int oneInXChance, params int[] itemIDs)
{
return DropEntireSet(npc, false, oneInXChance, itemIDs);
}
#endregion
#region Player Item Spawns
///
/// Spawns a stack of one or more items for the given player.
///
/// The player which should receive the item(s).
/// The ID of the item(s) to spawn.
/// The minimum number of items to spawn. Defaults to 1.
/// The maximum number of items to spawn. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items spawned.
public static int DropItem(Player p, int itemID, int minQuantity = 1, int maxQuantity = 0)
{
int quantity;
// If they're equal (or for some reason max is less??) then just drop the minimum amount.
if (maxQuantity <= minQuantity)
quantity = minQuantity;
// Otherwise pick a random amount to drop, inclusive.
else
quantity = Main.rand.Next(minQuantity, maxQuantity + 1);
// If the final quantity is 0 or less, don't bother.
if (quantity <= 0)
return 0;
p.QuickSpawnItem(itemID, quantity);
return quantity;
}
///
/// At a chance, spawns a stack of one or more items for the given player.
///
/// The player which should receive the item(s).
/// The ID of the item(s) to spawn.
/// The chance that the items will spawn. A decimal number <= 1.0.
/// The minimum number of items to spawn. Defaults to 1.
/// The maximum number of items to spawn. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items spawned.
public static int DropItemChance(Player p, int itemID, float chance, int minQuantity = 1, int maxQuantity = 0)
{
// If you fail the roll to get the drop, stop immediately.
if (Main.rand.NextFloat() > chance)
return 0;
return DropItem(p, itemID, minQuantity, maxQuantity);
}
///
/// At a chance, spawns a stack of one or more items for the given player.
///
/// The player which should receive the item(s).
/// The ID of the item(s) to spawn.
/// The chance that the items will spawn is 1 in this number. For example, 5 gives a 1 in 5 chance.
/// The minimum number of items to spawn. Defaults to 1.
/// The maximum number of items to spawn. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items spawned.
public static int DropItemChance(Player p, int itemID, int oneInXChance, int minQuantity = 1, int maxQuantity = 0)
{
// If you fail the roll to get the drop, stop immediately.
if (Main.rand.Next(oneInXChance) != 0)
return 0;
return DropItem(p, itemID, minQuantity, maxQuantity);
}
#endregion
#region Player Item Spawns Conditional
///
/// With a condition, spawns a stack of one or more items for the given player.
///
/// The player which should receive the item(s).
/// The ID of the item(s) to spawn.
/// Any arbitrary Boolean condition to gate this spawn. If false, nothing is spawned.
/// The minimum number of items to spawn. Defaults to 1.
/// The maximum number of items to spawn. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items spawned.
public static int DropItemCondition(Player p, int itemID, bool condition, int minQuantity = 1, int maxQuantity = 0)
{
return condition ? DropItem(p, itemID, minQuantity, maxQuantity) : 0;
}
///
/// With a condition and at a chance, spawns a stack of one or more items for the given player.
///
/// The player which should receive the item(s).
/// The ID of the item(s) to spawn.
/// Any arbitrary Boolean condition to gate this spawn. If false, nothing is spawned.
/// The chance that the items will spawn. A decimal number <= 1.0.
/// The minimum number of items to spawn. Defaults to 1.
/// The maximum number of items to spawn. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items spawned.
public static int DropItemCondition(Player p, int itemID, bool condition, float chance, int minQuantity = 1, int maxQuantity = 0)
{
return condition ? DropItemChance(p, itemID, chance, minQuantity, maxQuantity) : 0;
}
///
/// With a condition and at a chance, spawns a stack of one or more items for the given player.
///
/// The player which should receive the item(s).
/// The ID of the item(s) to spawn.
/// Any arbitrary Boolean condition to gate this spawn. If false, nothing is spawned.
/// The chance that the items will spawn is 1 in this number. For example, 5 gives a 1 in 5 chance.
/// The minimum number of items to spawn. Defaults to 1.
/// The maximum number of items to spawn. Defaults to 0, meaning the minimum quantity is always used.
/// The number of items spawned.
public static int DropItemCondition(Player p, int itemID, bool condition, int oneInXChance, int minQuantity = 1, int maxQuantity = 0)
{
return condition ? DropItemChance(p, itemID, oneInXChance, minQuantity, maxQuantity) : 0;
}
#endregion
#region Player Item Set Spawns
///
/// Chooses an item from an array and spawns it for the given player.
///
/// The player which should receive the item.
/// The array of items to choose from. If it's null or empty, nothing will be spawned.
/// Whether an item was spawned.
public static bool DropItemFromSet(Player p, params int[] itemIDs)
{
// Can't choose anything from an empty array.
if (itemIDs is null || itemIDs.Length == 0)
return false;
// Choose which item to drop.
int itemID = Main.rand.Next(itemIDs);
p.QuickSpawnItem(itemID);
return true;
}
///
/// At a chance, chooses an item from an array and spawns it for the given player.
///
/// The player which should receive the item.
/// The chance that the item will spawn. A decimal number <= 1.0.
/// The array of items to choose from. If it's null or empty, nothing will be spawned.
/// Whether an item was spawned.
public static bool DropItemFromSetChance(Player p, float chance, params int[] itemIDs)
{
// If you fail the roll to get the drop, stop immediately.
if (Main.rand.NextFloat() > chance)
return false;
return DropItemFromSet(p, itemIDs);
}
#endregion
#region Player Item Set Spawns Conditional
///
/// With a condition, chooses an item from an array and spawns it for the given player.
///
/// The player which should receive the item.
/// Any arbitrary Boolean condition to gate this spawn. If false, nothing is spawned.
/// The array of items to choose from. If it's null or empty, nothing will be spawned.
/// Whether an item was spawned.
public static bool DropItemFromSetCondition(Player p, bool condition, params int[] itemIDs)
{
return condition ? DropItemFromSet(p, itemIDs) : false;
}
///
/// With a condition and at a chance, chooses an item from an array and spawns it for the given player.
///
/// The player which should receive the item.
/// Any arbitrary Boolean condition to gate this spawn. If false, nothing is spawned.
/// The chance that the items will spawn. A decimal number <= 1.0.
/// The array of items to choose from. If it's null or empty, nothing will be spawned.
/// Whether an item was spawned.
public static bool DropItemFromSetCondition(Player p, bool condition, float chance, params int[] itemIDs)
{
return condition ? DropItemFromSetChance(p, chance, itemIDs) : false;
}
#endregion
#region Player Entire Set Spawns
///
/// Rolls for each item in an array to drop at a given chance. Always drops at least one item.
///
/// The player which should receive the items.
/// The chance that an item will drop. A decimal number <= 1.0.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropEntireSet(Player p, float chance, params int[] itemIDs)
{
int numDrops = 0;
// Can't choose anything from an empty array.
if (itemIDs is null || itemIDs.Length == 0)
return numDrops;
// Tally the total number of items dropped as the drop set is iterated through.
for (int i = 0; i < itemIDs.Length; ++i)
numDrops += DropItemChance(p, itemIDs[i], chance);
// If nothing at all was dropped, drop one thing at random.
numDrops += DropItemFromSetCondition(p, numDrops <= 0, itemIDs) ? 1 : 0;
return numDrops;
}
///
/// Rolls for each item in an array to drop at a given chance. Always drops at least one item.
///
/// The player which should receive the items.
/// The chance that the items will spawn is 1 in this number. For example, 5 gives a 1 in 5 chance.
/// The array of items to choose from. If it's null or empty, nothing will be dropped.
/// The number of items dropped.
public static int DropEntireSet(Player p, int oneInXChance, params int[] itemIDs)
{
int numDrops = 0;
// Can't choose anything from an empty array.
if (itemIDs is null || itemIDs.Length == 0)
return numDrops;
// Tally the total number of items dropped as the drop set is iterated through.
for (int i = 0; i < itemIDs.Length; ++i)
numDrops += DropItemChance(p, itemIDs[i], oneInXChance);
// If nothing at all was dropped, drop one thing at random.
numDrops += DropItemFromSetCondition(p, numDrops <= 0, itemIDs) ? 1 : 0;
return numDrops;
}
#endregion
}
}