[Ace] Lunatic States Punishment HP Drain Problem

Spoiler
I'm having a completely bizarre issue with Yanfly's Lunatic States Punishment script, with the Drain HP function and trying to get to the bottom of it. In short, ALL States that are on any battler that has an HP drain state active are counting as HP drain states of the same amount. Background information below:

I'm building a character that has several functions built around draining HP; they can apply HP drain states to enemies, force friendly targets to apply them to themselves (which allows those targets to then be healed when they take damage) and apply HP drain states on friends so when enemies hit friendly targets they are healed. All of these effects are working fine and that's not the issue I'm having.

The calculation of these effects appears to be very off. At first, it seemed to be always doubling the HP drain effect, as I noticed that in order to have the character be healed 10% of damage dealt to an enemy, the state needed to be marked to drain 5%. The effect to drain from other targets was doubled as well. I later then noticed that when I did the eventing for the ability that forces targeted allies to cast an HP drain on the character, the effect was tripled (a 25% drain was returning 75%).

This ability made use of several extra/hidden states for the sake of the eventing, and on a whim I decided to look at the calculations for the other two states after applying any state to the battler that is to have their HP drained. I found that additional states, even those not tagged to drain HP, applied to the battler, increased the HP drain effect by the same amount as the HP drain tag.

This explained why I was seeing the 75% effect from a 25% drain tag; each additional state, even those that are not an HP drain state, that are on a battler with an active HP drain, are adding an additional HP drain
amount. This is NOT occurring with RPG Maker VX Ace's latent stacking stat buffs, just custom states that don't need to have HP drain in them even. I'd like to find a way for this not to occur, and am unsure what I must change/edit in the script to do so. I made one minor edit to Yanfly's script when I realized that battlers that were inflicted with a Death state were able to come back to life when the drained HP, but I've otherwise left it intact. The Drain HP effect is below, and the full script at the top of this post.

Thank you very much for any insight or help fixing this issue!

Code:
#----------------------------------------------------------------------    # Punish Effect No.7: Drain HP    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    # Best used with shock effect. Whenever user takes HP damage, the    # original caster of the state will heal HP based on HP damage dealt.    #     # Recommended notetag:    #   <shock effect: drain hp x%>    #----------------------------------------------------------------------    when /DRAIN HP[ ](\d+)([%%])/i      return unless @result.hp_damage > 0      dmg = (@result.hp_damage * $1.to_i * 0.01).to_i      if $imported["YEA-BattleEngine"] && dmg > 0                text = sprintf(YEA::BATTLE::POPUP_SETTINGS[:hp_heal], dmg.group)              end      if         state_origin.hp > 0        then      state_origin.hp += dmg      end
Code:
#==============================================================================# # ▼ Yanfly Engine Ace - Lunatic States Package - Punishment v1.01# -- Last Updated: 2011.12.15# -- Level: Lunatic# -- Requires: YEA - Lunatic States v1.00+# #==============================================================================$imported = {} if $imported.nil?$imported["YEA-LSP-Punishment"] = true#==============================================================================# ▼ Updates# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# 2011.12.31 - Bug Fixed: Error with battle popups not showing.# 2011.12.15 - Started Script and Finished.# #==============================================================================# ▼ Introduction# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# This is a script for Lunatic States Package Effects with punishment themed# effects. Included in it are effects that make battlers undead (take damage# whenever they are healed), make battlers whenever they execute physical or# magical attacks, make battlers take damage based on the original caster of# the state's stats, and an effect that heals the original caster of the state# whenever the battler takes HP or MP damage.# #==============================================================================# ▼ Instructions# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# To install this script, open up your script editor and copy/paste this script# to an open slot below ▼ Materials/素材 but above ▼ Main. Remember to save.# # Install this script under YEA - Lunatic States. Then, proceed to use the# proper effects notetags to apply the proper LSP Punishment item desired.# Look within the script for more instructions on how to use each effect.# #==============================================================================# ▼ Compatibility# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# This script is made strictly for RPG Maker VX Ace. It is highly unlikely that# it will run with RPG Maker VX without adjusting.# # This script requires YEA - Lunatic States v1.00+ to work. It must be placed# under YEA - Lunatic States v1.00+ in the script listing.# #==============================================================================if $imported["YEA-LunaticStates"]class Game_BattlerBase    #--------------------------------------------------------------------------  # ● Lunatic States Package Effects - Punishment  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  # These effects are centered around the theme of punishment. These effects  # punish users for doing various aspects that may benefit them by harming  # them in different ways.  #--------------------------------------------------------------------------  alias lunatic_state_extension_lsp1 lunatic_state_extension  def lunatic_state_extension(effect, state, user, state_origin, log_window)    case effect.upcase    #----------------------------------------------------------------------    # Punish Effect No.1: Undead HP    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    # Best used with react effect. This causes any HP healing done to become    # reversed and deal HP damage to the healed target.    #     # Recommended notetag:    #   <react effect: undead hp>    #----------------------------------------------------------------------    when /UNDEAD HP/i      return unless @result.hp_damage < 0      @result.hp_damage *= -1      @result.hp_drain *= -1          #----------------------------------------------------------------------    # Punish Effect No.2: Undead MP    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    # Best used with react effect. This causes any MP healing done to become    # reversed and deal MP damage to the healed target.    #     # Recommended notetag:    #   <react effect: undead mp>    #----------------------------------------------------------------------    when /UNDEAD MP/i      return unless @result.mp_damage < 0      @result.mp_damage *= -1      @result.mp_drain *= -1          #----------------------------------------------------------------------    # Punish Effect No.3: Physical Backfire    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    # Best used with while effect. Whenever the affected battler uses a    # physical attack, that battler will take HP damage equal to its own    # stats after finishing the current action. Battler cannot die from    # this effect.    #     # Recommended notetag:    #   <while effect: physical backfire stat x%>    #     # Replace "stat" with MAXHP, MAXMP, ATK, DEF, MAT, MDF, AGI, or LUK.    # Replace x with the stat multiplier to affect damage dealt.    #----------------------------------------------------------------------    when /PHYSICAL BACKFIRE[ ](.*)[ ](\d+)([%%])/i      return if user.current_action.nil?      return unless user.current_action.item.physical?      case $1.upcase      when "MAXHP"; dmg = user.mhp      when "MAXMP"; dmg = user.mmp      when "ATK";   dmg = user.atk      when "DEF";   dmg = user.def      when "MAT";   dmg = user.mat      when "MDF";   dmg = user.mdf      when "AGI";   dmg = user.agi      when "LUK";   dmg = user.luk      else; return      end      dmg = (dmg * $2.to_i * 0.01).to_i      if $imported["YEA-BattleEngine"] && dmg > 0        text = sprintf(YEA::BATTLE::POPUP_SETTINGS[:hp_dmg], dmg.group)        user.create_popup(text, "HP_DMG")      end      user.perform_damage_effect      user.hp = [user.hp - dmg, 1].max          #----------------------------------------------------------------------    # Punish Effect No.4: Magical Backfire    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    # Best used with while effect. Whenever the affected battler uses a    # magical attack, that battler will take HP damage equal to its own    # stats after finishing the current action. Battler cannot die from    # this effect.    #     # Recommended notetag:    #   <while effect: magical backfire stat x%>    #     # Replace "stat" with MAXHP, MAXMP, ATK, DEF, MAT, MDF, AGI, or LUK.    # Replace x with the stat multiplier to affect damage dealt.    #----------------------------------------------------------------------    when /MAGICAL BACKFIRE[ ](.*)[ ](\d+)([%%])/i      return if user.current_action.nil?      return unless user.current_action.item.magical?      case $1.upcase      when "MAXHP"; dmg = user.mhp      when "MAXMP"; dmg = user.mmp      when "ATK";   dmg = user.atk      when "DEF";   dmg = user.def      when "MAT";   dmg = user.mat      when "MDF";   dmg = user.mdf      when "AGI";   dmg = user.agi      when "LUK";   dmg = user.luk      else; return      end      dmg = (dmg * $2.to_i * 0.01).to_i      if $imported["YEA-BattleEngine"] && dmg > 0        text = sprintf(YEA::BATTLE::POPUP_SETTINGS[:hp_dmg], dmg.group)        user.create_popup(text, "HP_DMG")      end      user.perform_damage_effect      user.hp = [user.hp - dmg, 1].max          #----------------------------------------------------------------------    # Punish Effect No.5: Stat Slip Damage    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    # Best used with close effect. At the end of the turn, the affected    # battler will take HP slip damage based on the stat of the of one who    # casted the status effect onto the battler. Battler cannot die from    # this effect.    #     # Recommended notetag:    #   <close effect: stat slip damage x%>    #     # Replace "stat" with MAXHP, MAXMP, ATK, DEF, MAT, MDF, AGI, or LUK.    # Replace x with the stat multiplier to affect damage dealt.    #----------------------------------------------------------------------    when /(.*)[ ]SLIP DAMAGE[ ](\d+)([%%])/i      case $1.upcase      when "MAXHP"; dmg = state_origin.mhp      when "MAXMP"; dmg = state_origin.mmp      when "ATK";   dmg = state_origin.atk      when "DEF";   dmg = state_origin.def      when "MAT";   dmg = state_origin.mat      when "MDF";   dmg = state_origin.mdf      when "AGI";   dmg = state_origin.agi      when "LUK";   dmg = state_origin.luk      else; return      end      dmg = (dmg * $2.to_i * 0.01).to_i      if $imported["YEA-BattleEngine"] && dmg > 0        text = sprintf(YEA::BATTLE::POPUP_SETTINGS[:hp_dmg], dmg.group)        user.create_popup(text, "HP_DMG")      end      user.perform_damage_effect      user.hp = [user.hp - dmg, 1].max          #----------------------------------------------------------------------    # Punish Effect No.6: Stat Slip Heal    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    # Best used with close effect. At the end of the turn, the affected    # battler will heal HP  based on the stat of the of one who casted the    # status effect onto the battler.    #     # Recommended notetag:    #   <close effect: stat slip heal x%>    #     # Replace "stat" with MAXHP, MAXMP, ATK, DEF, MAT, MDF, AGI, or LUK.    # Replace x with the stat multiplier to affect damage dealt.    #----------------------------------------------------------------------    when /(.*)[ ]SLIP HEAL[ ](\d+)([%%])/i      case $1.upcase      when "MAXHP"; dmg = state_origin.mhp      when "MAXMP"; dmg = state_origin.mmp      when "ATK";   dmg = state_origin.atk      when "DEF";   dmg = state_origin.def      when "MAT";   dmg = state_origin.mat      when "MDF";   dmg = state_origin.mdf      when "AGI";   dmg = state_origin.agi      when "LUK";   dmg = state_origin.luk      else; return      end      dmg = (dmg * $2.to_i * 0.01).to_i      if $imported["YEA-BattleEngine"] && dmg > 0        text = sprintf(YEA::BATTLE::POPUP_SETTINGS[:hp_heal], dmg.group)        user.create_popup(text, "HP_HEAL")      end      user.perform_damage_effect      user.hp += dmg          #----------------------------------------------------------------------    # Punish Effect No.7: Drain HP    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    # Best used with shock effect. Whenever user takes HP damage, the    # original caster of the state will heal HP based on HP damage dealt.    #     # Recommended notetag:    #   <shock effect: drain hp x%>    #----------------------------------------------------------------------    when /DRAIN HP[ ](\d+)([%%])/i      return unless @result.hp_damage > 0      dmg = (@result.hp_damage * $1.to_i * 0.01).to_i      if $imported["YEA-BattleEngine"] && dmg > 0                text = sprintf(YEA::BATTLE::POPUP_SETTINGS[:hp_heal], dmg.group)              end      if         state_origin.hp > 0        then      state_origin.hp += dmg      end          #----------------------------------------------------------------------    # Punish Effect No.8: Drain MP    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    # Best used with shock effect. Whenever user takes MP damage, the    # original caster of the state will heal MP based on MP damage dealt.    #     # Recommended notetag:    #   <shock effect: drain mp x%>    #----------------------------------------------------------------------    when /DRAIN MP[ ](\d+)([%%])/i      return unless @result.mp_damage > 0      dmg = (@result.mp_damage * $1.to_i * 0.01).to_i      if $imported["YEA-BattleEngine"] && dmg > 0        Sound.play_recovery        text = sprintf(YEA::BATTLE::POPUP_SETTINGS[:mp_heal], dmg.group)        user.create_popup(text, "MP_HEAL")      end      state_origin.mp += dmg          #----------------------------------------------------------------------    # Stop editting past this point.    #----------------------------------------------------------------------    else      so = state_origin      lw = log_window      lunatic_state_extension_lsp1(effect, state, user, so, lw)    end  end  end # Game_BattlerBaseend # $imported["YEA-LunaticStates"]#==============================================================================# # ▼ End of File# #==============================================================================
That error is occurring because Yanfly has a bug in the script. He called for each in states do, then inside that called for each in states do again. What it means is it loops through all your states n^2 times, where n is the number of states you really have in effect, and it is then running the lunatic state 1 time on each loop of n times (so it is effectively running n times, where n is the number of states you have).

I have a version of it with the bug fixed, but I work in 3 minutes. I'll PM you it after work or early tomorrow.

Edit: PM'd you the script. The bug is in the Lunatic States, not the Punishment script. I sent you the correction.
Wow, thank you so much, Bgillisp! It sounds like you have it all figured out! I'd be extremely appreciative and please PM that to me or post it here when you can!
@bgillisp - I do think you should post that up as a fix on the RGSS3 forums... That would help a lot of people.
I didn't know I was allowed to, since I modified the base script to fix that, and it is still Yanfly's script except for me deleting one for loop.
If you go to the section, there are threads that are basically edited versions of the original, mostly fixes for bugs and so on... It will still depend on the author of the base script course. And of course, you will put it clear that it's a fix for his script, and not your own full script.


You could even just post the part where the edit was, instead of uploading the whole script, so it will be like a patch instead of a full script. Then just add a link to the original script. So no problems.
Hmmm...ok, I'll try to post it up tomorrow or Saturday, I'm fatigued right now after 12 hours of writing cutscenes.

Original thread: https://forums.rpgmakerweb.com/threads/ace-lunatic-states-punishment-hp-drain-problem.36844/