New account registration is temporarily disabled.

[RMVX ACE] DAMAGE "VARIANCE"

Posts

Pages: 1
Max McGee
with sorrow down past the fence
9159
Hey, can one of you programming savvy folks tell me what this (default) code actually means in plain english? Possibly with a couple simple examples?

 def apply_variance(damage, variance)

amp = [damage.abs * variance / 100, 0].max.to_i
var = rand(amp + 1) + rand(amp + 1) - amp
damage >= 0 ? damage + var : damage - var
end


I'm trying to figure out how exactly variance is calculated but this seems pretty opaque to me.

because i suck at math/programming
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
Sure, I'll take a crack at this.

The method is obviously to vary damage done by a particular degree, but let's break it down piece by piece:

Two arguments are supplied to the method: the damage that was done, and the variance desired.

The first line declares a variable called amp and gives it a value of either 0 or (variance)% of absolute damage, whichever is higher, as an integer. Let's say we did 20 damage with a variance of 20, amp will be 4, as 4 is 20% of 20. (Interestingly enough, as we're dealing with absolute damage and the variance can't be lower than 0, it's never going to choose 0 as the max because it's not possible for this equation to return a value less than 0.)

Next up we're setting var to a random integer number between 0 and amp (in the case of our example, this will be 0-4) + a random number in the same range, minus amp. Let's say our random numbers are 4 and 3, we end up with 4+3-4 = 3.

The next line is an inline if checking whether damage is greater than or equal to 0: if it is, we return damage + var: in our example, this is 23. If it isn't, we return damage - var: this won't happen in our example as damage is positive, but the point of this is to make variance affect healing the same way as damage by reversing the signs.

TL;DR: It works out by how much the value can vary depending on the variance percentage you set, chooses a random value between -(maximum variance) and +(maximum variance), and adds it to the damage done.

Edit: Removed stupid stuff that wasn't true. Thanks for the reminder LockeZ!
LockeZ
I'd really like to get rid of LockeZ. His play style is way too unpredictable. He's always like this too. If he ran a country, he'd just kill and imprison people at random until crime stopped.
5958
Line by line:


amp = [damage.abs * variance / 100, 0].max.to_i


Make an array of two values. The first value in the array is the damage * variance/100. In other words, if the variance is 30, then the first value in the array is 30% of the damage. The second value in the array is 0.

Then pick whichever of these two values is higher. (Approximately 99.99% of the time, this will be the first value.) This makes it so it's impossible for variance to be a negative number - if it is, it acts like 0 variance. The original damage can actually be a negative number, though, because this line is using damage.abs instead of just damage. Which is good because negative damage is how healing works.

Functionally, this line is really just amp = damage * variance / 100 with some extra sanity checks.



var = rand(amp + 1) + rand(amp + 1) - amp


In the first line, amp was set to the maximum amount of possible variance. So if you have a 1000 damage spell with 30% variance, amp is now set to 300. This line takes that value and actually does the randomness.

It picks two random numbers between 1 and amp, adds them together, and then subtracts amp. This results in a random number between amp and negative amp. So in our example, we now have a random number between 300 and -300.



damage >= 0 ? damage + var : damage - var


A lot of people hate the x?y:z syntax and I can't blame them. This line could be rewritten as:

if damage >=0

return damage + var
else
return damage - var
end


The "return" is implied. In Ruby, if you simply write "x" on a line by itself, it takes this to mean "return x". This is really obnoxious and unreadable, and it's even worse in complex lines like this. I'm not sure why they started doing it in RGSS3; they didn't use this abbreviation in RPG Maker XP. This is my annoyed face.

Ultimately, in our example input of 1000 damage and 30 variance, this function returns 700 + 2d300.
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
I think we're both slightly incorrect in terms of our amp explanations, come to think of it: I forgot rand was non-inclusive of the argument, and you're slightly off on the values chosen.

rand(amp + 1) will generate an integer between 0 and amp. Your example is still correct, but it's not between 1 and amp.
Max McGee
with sorrow down past the fence
9159
So basically, damage will be "Base Damage" (however that's calculated) + between Variance% of the base damage and -Variance$ of the base damage?

So if base damage is 100, and Variance is 80, damage will between 20 and 180?
LockeZ
I'd really like to get rid of LockeZ. His play style is way too unpredictable. He's always like this too. If he ran a country, he'd just kill and imprison people at random until crime stopped.
5958
Yes, correctamundo.
Pages: 1