I have an epic idea for a mod that involves how the presents work, but how can i make an item that has a different chance of giving a different item? Like how the presents give candy blocks at a high chance and how a snow globe drop rare. If anyone could tell me how to do this i would be very grateful!
Figure out 1) how many different items you want to produce, and 2) the chance you want each different item to drop (n out of M). Convert all the chance fractions to have the same denominator, and make sure that the resulting fractions add up to < 1 (if less than 1, then there will be chances to produce nothing). Let's try re-doing the Present one: say 1/25 chance to drop a Snow Globe, 2/5 chance to drop a Green Candy Cane Block, 2/5 chance to drop a Red Candy Cane Block, rest of the time, drop a Teddy Bear (or nothing) Refactoring into a common denominator of 25, you get 1/25, 10/25, 10/25, and 4/25, respectively, which all add up to 25/25, or 1. Then you can think of them as a numbered range beginning with 0: 0 is Snow Globe, 1-10 is Green Candy Cane Block, 11-20 is Red Candy Cane Block, 21-24 is Teddy Bear (or nothing). In your code, use Main.rand.Next(24), to generate a random number between 0 and 24, inclusive. Then test the result against your numbered range and do whatever. Code: int num = Main.rand.Next(24); if (num == 0) // do something with Snow Globe else if (num <= 10) // do something with Green Candy Cane Blocks else if (num <= 20) // do something with Red Candy Cane Blocks else // do something with Teddy Bear, or do nothing
For Code: //do something with Snow Globe I suggest changing the item directly. Code: if (num == 0) { item.setDefaults(snowGlobeType); // I think that's the method you want. // maybe you need to redraw that inventory square. // I dunno how to do that. }
Well, I assumed that OP knew how or could figure out what to do, I was mainly describing how to change the chances in broad terms. The actual source code for changing the Present to, say, Red Candy Cane Blocks, is Code: Main.player[Main.myPlayer].inventory[num81].SetDefaults(586, false, true); Main.player[Main.myPlayer].inventory[num81].stack = Main.rand.Next(20, 50); // how many blocks Then the update loop takes care of redrawing it.
What i was thinking of is there would be 5 things per different present. Example: 5/20=Dirt 5/20=Stone 4/20=Mushroom 3/20=Copper Ore 3/20=Silver Coin Im no good at C# or C++ (i forgot which one was the terraria one) so if i could get a good example of each line of code telling me which each line does and how to make it work, then it would work. Id want this to work with custom mod items aswell so if someone could explain that aswell..