I'm making a game and had a look at the questions about calculating damage/diminishing returns here and here but I haven't quite been able to come up with a logarithmic function that satisfies this goal - which is probably super obvious to those mathematically minded of you (apologies if it is!).
The goal is:
- deal half of the damage total to the first target
- deal a diminishing amount of damage for each target beyond the first
- do not deal more damage than is input into the function
- bonus: create a linear downward curve
An example (what I've got so far):
fn = (amount, count) => {
let d = amount;
let r = 0.5;
let a = [];
for (i = 0; i < count; i++) {
a.push(d * r);
r -= r/(count-i);
}
a.push(a.reduce((acc, cur) => acc + cur)); //push the total combined damage output as last result of array
return a;
}
You can probably spot the problem! Given 300 damage and 3 targets (fn(300, 3)
), this outputs the following: [150, 100, 50, 300]
- which is great. The last number is just the total amount of damage being dealt, which with these inputs matches the amount fed into the function.
Given 4 targets and 400 damage, the problem is exposed with the output being: [200, 150, 100, 50, 500]
- the damage output is 100 more than the input amount.
This is not a game breaking problem for me and it's also not that important that the downward curve be linear, but if there is a way to achieve that result that would be awesome and I would greatly appreciate any input!
Thanks for your time!