Maths Equation Help

I have 4 stats in my game which should all add up to create the total damage output done by the player, but I can’t figure out a balanced equation. Would be great if one of you could help me with this.

Basically, lets say there are the stats a, b, c and d and the damage output is e. How would I make it so if all numbers are equal, e would be an average of them, but if a or b are higher than c and d, e would be higher than the average, and if c or d are higher than a and b, e would be lower than average? Example:

a - 10
b - 10
c - 10
d - 10

e = 50

a - 15
b - 15
c - 10
d - 10

e = 70

a - 10
b - 10
c - 15
d - 15

e = 30

Would it be possible for c and d to have base values of 0 and subtract them from the damage total instead?

a - 25
b - 25
c + 0
d + 0

e = 50

a - 35
b - 35
c + 0
d + 0

e = 70

a - 25
b - 25
c + 10
d + 10

e = 30

No, I don’t think so, because in the game players would be able to level up each stat independently and after some time players would have stats like:

a = 8217
b = 2598
c = 3459
d = 1250

I just need some kind of equation that would take these 4 stats and add them up into one value which would be the damage they do to another player, but to make it balanced I’d like the damage to go up as a or b go up, but go down if c or d go up.

Your post is somewhat confusing to me.
You got 4 stats. With e being the damage output. So do you want e to be the average of a, b, c and d. Or is there something else you desire?

I want e to be the result of a, b, c and d added together, but it has to be an equation where e would go up if a or b were higher than c and d, and go down if c or d were higher than a and b.

Here’s something I conjured up. There might be a way easier way to do this but if there is, I don’t know it

-- These constants effect how much your damage can fluctuate from the average of a,b,c,d
-- i.e. MAXIMUM_GAIN = 2 means than your damage can increase by 2x it's current value (for a total of 3x the average of a,b,c,d)
-- 		MAXIMUM_LOSS = 1 means that your damage can reduce to 0 if c and d are significantly larger than a and b
-- i.e. if you reduce MAXIMUM_LOSS to 0.5, then your average can only decrease by half at most
local MAXIMUM_LOSS = 1
local MAXIMUM_GAIN = 2
-- This constant effects the rate that your damage value increases or decreases compared to the difference in your values (indirectly proportional to the signedDifference value)
-- i.e. GAIN_PERIOD = 1000 means that you will stop gaining/losing damage when the difference* value is at or above 1000
local GAIN_PERIOD = 1000


local gainPeriodAdjusted = 4/(GAIN_PERIOD)

function calculateDamage(a,b,c,d)
	
	-- Average the values
	local average = (a+b+c+d)/4
	
	-- Essentially just gets the difference between two pairs of numbers. Probably not the ideal way of doing it.
	-- To get a better suited equation you'll need to clarify what you want to happen in cases you didn't describe
	-- i.e. what should happen if there is no pair clearly larger than the other? (a & c large, b & d small)
	-- In that particular scenario, this equation will just weight towards whatever the larger difference is
	local signedDifference = ((math.min(a,b)-math.max(c,d)) + (math.max(a,b)-math.min(c,d)))/2
	
	-- Decide if we're increasing or decreasing
	local coeffecient = (signedDifference < 0 and MAXIMUM_LOSS or MAXIMUM_GAIN)
	
	-- tanh is a kinda nice double-ended exponential function - your damage increase will start linearly proportional to the difference* and eventually approach and rest at (MAXIMUM_GAIN * [current average]) 
	return coeffecient * average * math.tanh(gainPeriodAdjusted*signedDifference) + average

end

*Every time I say difference I mean the signedDifference

Plus a picture to describe what’s going on:

4 Likes

Thanks, seems like it’s working how I want it to, except there’s a slight problem. The higher the values go, the bigger the change in damage is which isn’t exactly what I wanted, I tried messing with the “MAXIMUM_LOSS” and “MAXIMUM_GAIN” but they don’t seem to be used in your equation at all.

Here is what I mean, the change in the values is only 500, but the damage goes all the way up to 15000 and all the way down to 200:

print(calculateDamage(5000,5000,5000,5000))

print(calculateDamage(5500,5000,5000,5000))

print(calculateDamage(5500,5500,5000,5000))

print(calculateDamage(5000,5000,5000,5500))

print(calculateDamage(5000,5000,5500,5500))

5000
12931.340098547
15372.289590796
1221.8299507267
188.85520460196

Where should I put the “MAXIMUM_LOSS” and “MAXIMUM_GAIN” values?

Whoooops sorry, made the classic mistake of messing it all up when you’re done and trying to make it readable :slight_smile:

This line:
local coeffecient = (signedDifference < 0 and 1 or 2)

Should be:
local coeffecient = (signedDifference < 0 and MAXIMUM_LOSS or MAXIMUM_GAIN)

1 Like