I'm writing a small javascript app which calculates damage per second of a variety of heroes and items in a video game. I'm trying to figure out the formula for critical strike items.
For example, there an item (riftshards) that gives a 20% chance to do 2.4x damage.
.2 * 1.4 = 0.28x extra damage or a damage factor of 1.28.
So my function would look like
getCritMultiplier: function(critMultiplier, critChance) { return critChance * (critMultiplier - 1) + 1; }
This works for when the hero only has 1 chance at a crit. But if the hero has 2 of these items it gets trickier.
I've been told the formula is something like this:
1 - [(1-x)(1-y)...]
Gives the % chance to get a crit where x is your first crit chance item, and y is your second crit chance item. Plugging in .2 for each gives ~36% which seems correct.
However, this is where I'm stuck. If each item has a different crit modifier, I don't know how to calculate it.
For example if you have items that are:
- 20% chance to do 2.4x damage
- 25% chance to do 2.0x damage
- 15% chance to do 3.0x damage
How can I calculate the result multiplier? The only other info I have is that if 2 crits happen at the same time (20% chance to 2.4x and 15% chance to 3.0x both occur) the higher multiplier will fire (3.0x damage). Any help is greatly appreciated.
My function needs to be:
getCritMultiplier: function(critMultipliers) { // critMultipliers is an array of objects with critChance and critMultiplier properties // Returns a single number which is your average DPS increase }
Example calls:
var multipliers = [{critChance: .2, critMultiplier: 2.4}]; getCritMultiplier(multipliers); // Returns 1.28 var multipliers = [{critChance: .2, critMultiplier: 2.4}, {critChance: .15, critMultiplier: 3.0}, {critChance: .25, critMultiplier: 2.0}] getCritMultiplier(multipliers); // Returns ???
Edit: I'll try to explain the problem better in math terms.
Hero does d
damage per attack. He is holding n
number of items. Each item has value p
that is the percent chance that the hero will score a critical strike. Each item may have a distinct value of p
. A critical strike makes the damage for that attack be multiplied by m
, where m
is a distinct value for each item. What is the formula for calculating the average damage per attack?
Example scenario:
- Hero does 100 damage per attack.
d = 100
. - Hero is carrying 3 items.
n = 3
. - Item 1 gives a 20% chance to do 2.4x damage.
p = .2
m = 2.4
- Item 1 gives a 15% chance to do 3.0x damage.
p = .15
m = 3.0
- Item 1 gives a 30% chance to do 2.0x damage.
p = .3
m = 2.0
Edit:
Thanks, got it working, here is the code I used:
getCritMultiplier: function(critMultipliers) { critMultipliers.sort(function(a, b) {return b.CRITICALMODIFIER - a.CRITICALMODIFIER}); var dpsMultiplier = 1; var totalCriticalChance = 0; $.each(critMultipliers, function (i, critMultiplier) { var diminishedChance = (1 - totalCriticalChance) * critMultiplier.CRITICALCHANCE; dpsMultiplier = dpsMultiplier + (diminishedChance * (critMultiplier.CRITICALMODIFIER - 1)); totalCriticalChance += diminishedChance; }); return {dpsMultiplier: dpsMultiplier, totalCriticalChance: totalCriticalChance}; },