Additive Element Res. Stacking- Madness After a Year

For the last year, I've been convinced that the element resistances in my game stack additively. I know the multiplicative way is the norm in the engine, but I found a snippet to change the system and it all seemed to work suddenly. I tested many different pieces of gear with elemental resistances, and they stacked the resistance additively as planned.

Now, after about a year of using the system... it suddenly doesn't seem to work on one occasion.

I'd rather explain the entire mess with images, so here goes.


This character here has got 2 perks increasing Air Defence. 5% and 15%.
Gran1_zpscb1myjcm.png


In this image, you can see how it correctly adds up to 20%.
Gran2_zpsywvrfgku.png


This character also has 2 perks for Air Defence- 65% and 5%.
Zera2_zps1fpow4ac.png


And this is the godforsaken result.
Zera1_zpsvnnxlt51.png


65 +15 = 71, according to this system.
Where does the 1 come from? It wouldn't make sense even in the default system. The character doesn't have any initial resistance changes on her, and it clearly says on the states descriptions, to increase the rates for 95% and 35% (which reverses as 5% and 65%.

This is the snippet I used to change the multiplicative stacking to additive. It has worked before this, although I haven't tested it extensively.

class Game_BattlerBase
def element_rate(element_id)
features_with_id(FEATURE_ELEMENT_RATE, element_id).inject(1.0) {|r, ft| r += ft.value - 1 }
end
end

Any help would be appreciated enormously. This is the one part I'm afraid might greatly dumb down the system in my game if I can't find a solution.
There's two things I can think of:

1: You have a trait somewhere that is giving another 1% of resistance. Check all equips, actor traits and passive skills.

2: Rounding error. I've seen this happen with other scripts where the resistance is off by +/- 1% for some reason I never figured out (for example, in my game, for some reason 200% - 200% = -1%. Figure that out if you can). Best I can figure is this error occurs due to how decimals are added in the computer, and there is little that can be done if that is indeed the case.
Weird... Your code should be working... Have you tried using the built-in Game_BattlerBase method features_sum?

Code:
Game_BattlerBase
  def element_rate(element_id)
    features_sum(FEATURE_ELEMENT_RATE, element_id)
  end 
end
I think it must be some sort of error. I just changed the 65% to 70% and the 5% to 10%, and voila, it gives me a correct number 80%. Maybe I'll just try to avoid using the number 5...

This is a bit out of subject, but apparently it's possible to make the states stack as well, by copying the same snippet and changing all the "element" words to "state" words.

Stacking State Resistances:

class Game_BattlerBase
def state_rate(state_id)
features_with_id(FEATURE_STATE_RATE, state_id).inject(1.0) {|r, ft| r += ft.value - 1 }
end
end
class Game_BattlerBase
def element_rate(element_id)
features_with_id(FEATURE_ELEMENT_RATE, element_id).inject(1.0) {|r, ft| r += ft.value - 1 }
end
end

There's two things going on here. First, the snippet you provided isn't doing what you think it is. Second, you're encountering some kind of rounding error. Let me walk you through what your code is doing.

First, it gathers up all the appropriate features with the correct element ID (in your example 5 and 65). It then does this:
1. Start with a value of 1 [ inject(1.0) ]
2. Add to the value of 1 the first element rate minus 1 [ {|r, ft| r += ft.value - 1 } ]
- so 1 + (65 - 1) = 65
3. Now take that value and add the second element rate minus 1
- so 65 + ( 5 - 1 ) = 69
4. Return that value.

This process would continue for more numbers. If you had 5 or 6 features, you could expect the end value to diverge from what you expect. @Kyo Panda is correct. You should use features_sum to get the functionality you want.

As for the rounding error, if you are still getting it after changing to features_sum, let us know. There may be a fix for that as well.
Element rate features are measured with float numbers, with 1.0 being 100% (normal damage), which is why it starts with 1.0.

Element rate feature values lower than 100% in the database means that it will reduce the damage taken from the element.
Element rate feature values higher than 100% in the database means that it will increase the damage taken from the element.

And that is why it subtracts 1.0 from the original feature value.
For example, if you would use 80% element rate in the database, that would become:
0.8 - 1.0 = -0.2 (aka -20%)
And that added to the base resistance (which is always 100%, that 1.0 it starts with in the code) would be:
1.0 - 0.2 = 0.8 (aka 80%)
Which means that the incoming damage from the element is reduced by 20%, like it should be (or in other words, only 80% damage will be taken).
Add another feature on top of these which is 155% in the database, and it will become:
Feature value: 1.55 - 1.0 = 0.55 (aka 55%)
End value: 1.0 - 0.2 + 0.55 = 1.35 (aka 135%)
So, at the end, the damage taken would be increased by 35% (or in other words, 135% damage will be taken).

So, no, the little snippet used here should work just as expected (it does, actually).

Rounding errors happen sometimes. But still, it shouldn't display it that way, it is a giant error, and the display method should never round it that way.
Which led me to check the display method, and surprise, that is the one causing the giant rounding error.
Add this patch below the advanced status script you are using (which is most probably Blackmorning's, right?):
Spoiler
Code:
module BMCHART
 
  def graph_values(chart,id)
    n = 0
    case chart
    when :element
      n    = eval("(@actor.#{chart}_rate(#{id})*100)")
      if $imported["YEA-ElementReflect"] && @actor.element_reflect_rate(id) != 0
        n = -(@actor.element_reflect_rate(id) * 100)
      elsif $imported["Elemental_Modifiers"]
        n  = (@actor.element_resist_rate(id) * 100)
      end
    when :state
      n    = eval("(@actor.#{chart}_rate(id)*100)")
      n    = 0 if @actor.state_resist_set.include?(id)
    when :param
      n    = (param_ratio(@actor, id) * 100)
    end
    return n
  end

  def element_number(id)
    value1 = graph_values(:element,id)
    if $imported["YEA-ElementReflect"] && @actor.element_reflect_rate(id) != 0
      value1 = @actor.element_reflect_rate(id) * 100
      color1 = text_color(22)
      text1 = sprintf("#{Vocab.xparam_a(5)} %d%", value1.to_i)      #shows mrf
    else
      color1 = percent_color((value1 - 100) * -1)
      if value1 == 0; text1 = 'Immune'
      elsif value1 < 0; text1 = 'Absorb'
      else
        value1 = ((value1 - 100) * -1) if BM::STATUS::NUMBER_STYLE[:element] == 1
        text1 = sprintf("%d%", value1.to_i)
      end
    end       
    value2 = (@actor.atk_element_rate(id) * 100)
    value2 = (@actor.element_attack_rate(id) * 100) if $imported["Elemental_Modifiers"]
    color2 = percent_color(value2)
    text2 = sprintf("%d%", value2.to_i)
    return [Vocab.element(id), text1, color1, text2, color2]
  end

  def state_number(id)
    value1 = graph_values(:state,id)
    color1 = percent_color((value1 - 100) * -1)
    if value1 == 0; text1 = 'Immune'
    else
      value1 = (value1 - 100) * -1 if BM::STATUS::NUMBER_STYLE[:state] == 1
      text1 = sprintf("%d%", value1.to_i)
    end
    value2 = @actor.atk_states_rate(id) * 100
    color2 = percent_color(value2)
    text2 = sprintf("%d%", value2.to_i) 
    return [Vocab.state(id), text1, color1, text2, color2]
  end

end
This should fix the display error you get.
Well, at least if you are using the status menu script I think you use, that is.
Thank you so much for the help, everyone! With Sixth's code, the system seems to work perfectly 🙂

Original thread: https://forums.rpgmakerweb.com/threads/additive-element-res-stacking-madness-after-a-year.79256/