VX - SCRIPT PROBLEM...

Posts

Pages: first 12 next last
So I've been implementing a new damage algorithm into RMVX and I seem to be having some problems, in particular it's to do with the outputted damage, it's just not coming out as predicted if the numbers put in are different.
The sections of the code I've edited and added are as follows:

Damage Algorithm:
def make_attack_damage_value(attacker)
damage = Math.sqrt((attacker.atk / self.def) * attacker.pow)
damage *= 5
damage *= make_level_mod(attacker) # base calculation
#damage = 0 if damage < 0 # if negative, make 0
#damage *= elements_max_rate(attacker.element_set) # elemental adjustment
#damage /= 100
if damage == 0 # if damage is 0,
damage = 1 # half of the time, 1 dmg
elsif damage > 0 # a positive number?
@critical = (rand(100) < attacker.cri) # critical hit?
@critical = false if prevent_critical # criticals prevented?
damage *= 3 if @critical # critical adjustment
end
#damage = apply_variance(damage, 5) # variance
#damage = apply_guard(damage) # guard adjustment
@hp_damage = damage.round # damage HP
end


(New) Level Modifier
def make_level_mod(attacker)
if attacker.level > self.level
leveldiff = attacker.level - self.level
levelmod = Math.sqrt(Math.sqrt(Math.sqrt(1+leveldiff)))
elsif attacker.level < self.level
leveldiff = self.level - attacker.level
levelmod = 2 - Math.sqrt(Math.sqrt(Math.sqrt(1+leveldiff)))
elsif attacker.level == self.level
levelmod = 1
end
if levelmod < 0.4
levelmod = 0.4
elsif levelmod > 1.6
levelmod = 1.6
end
return levelmod


Now for the problem: if the character's Atk stat is reduced to be lower than the enemy's Def, then apparently outputted damage from the first section of the algorithm becomes zero, giving an output of 1 damage.
Having put the same algorithm and stats of the test character and monster into Excel, I've gotten numbers that are only slightly smaller than if their Atk is equal/greater than the monster's Def, meaning something stupid is happening in RPGMaker, but I just can't pinpoint it at all.

Any scripters think they might have a clue as to why it's not working right? Thanks in advance to anyone able to help.
Craze
why would i heal when i could equip a morningstar
15170
Yeah, RGSS2 hates decimals. Try:

damage = Math.sqrt(((attacker.atk * 100) / self.def) * attacker.pow / 100)
LouisCyphre
can't make a bad game if you don't finish any games
4523
post=143354
Is RGSS as unable to handle fractions as normal RPGMaker events are? If so, then you might want to scale your numbers up (x100), then do your algorithm, then scale them back down (divide by 100).
Actually, almost all programming languages will do integer division, given integers to work with.

25 / 100 = 0      # Integer division. Results in 0.25, then the decimal is axed.
25.0 / 100 = 0.25 # Real division. 25.0 is not an integer (it has a decimal), so the code has to
# use real division instead.

In order for the code to do real division, one of the operands has to be a real number! Just multiply one of them by 1.0, or append a .0 to the end of any constants you're using.
Thanks you lot. Didn't know that programming languages do integer division as standard, probably 'cause I've never really used division much in scripting.

Funny how it always seems to turn out to be something simple and basic that's the problem though, ahwell, at least it's an easy fix, thanks again.
Or append a .to_f to cast the value into a floating decimal point number
LouisCyphre
can't make a bad game if you don't finish any games
4523
post=143368
Or append a .to_f to cast the value into a floating decimal point number
You can't to 100.to_f, though! 100 isn't a variable. You can do stuff like damage.to_f, though.
Also you'd want to .to_i or round the final variable, so you don't end up doing 20.563467623 damage or something like that.

Edit: Disregard this, I should try reading the script in the first post and not just respond based on comments.
post=143369
post=143368
Or append a .to_f to cast the value into a floating decimal point number
You can't to 100.to_f, though! 100 isn't a variable. You can do stuff like damage.to_f, though.


You can convert a numeric value into float point by putting it in parenthesis (100).to_f.
LouisCyphre
can't make a bad game if you don't finish any games
4523
There's not much point or advantage over "100.0" though =p

Just trying to steer him in the right direction for clean code!
My advice is 100% valid since you'll never do math to nothing but constants and converting the variable to float means you can change any part of the equation without a change in behaviour, like accidentally erasing a 1.0 to force float operations. Also "Just multiply one of them by 1.0" is terrible advice when making clean code!

Cleaner version of make_level_mod code while I'm at it:

def make_level_mod(attacker)

MAX_MOD = 1.6
MIN_MOD = 0.4

leveldiff = attacker.level - self.level

return 1 if leveldiff == 0

levelmod = (1 + Math.abs(leveldiff) ) ** 0.125
levelmod = 2 - levelmod if leveldiff < 0

return [ [levelmod, MAX_MOD].min, MIN_MOD].max
end

(untested)

*edit*
Fixed a bug because Ruby always handles 'if's weird as though they're always followed by blocks
Craze
why would i heal when i could equip a morningstar
15170
Why didn't you tell me this a year ago? >=(
Thanks GRS. That's definitely a lot cleaner than my initial version, although the ** confused me for awhile (Used to using ^ in RGSS). forgot about the whole Min, Max thing as well... I have a lot to learn about RGSS it seems...
............

Ruby supports using ^?



LouisCyphre
can't make a bad game if you don't finish any games
4523
post=143392
............

Ruby supports using ^?



Indeed it does, I knew it accepted ^=, so I was guessing it accepted it on it's own too, and I just replaced the ** and it was all fine so yeah.
I thought the exponential operator was ** ?
I appears to be both ** and ^ !
I'd say that both work fine, just different ways of displaying the same thing. I just use ^ more 'cause it's quicker to type and easier to read (Espec when doing maths revision using wolfram to act as a calculator... urgh exams...)
Craze
why would i heal when i could equip a morningstar
15170
post=143393
post=143392
............

Ruby supports using ^?



Pages: first 12 next last