MV YEP Buffs & States: State that decreases Max HP per battler turn.

Hey all! I'm needing some assistance with this one.

I'm wanting a state that decreases the afflicted character's max HP per battler turn in battle by 5% of their original max HP. When the state is removed (either by magic or item that heals it, or by death) their max HP returns to what it was originally. Their current HP would remain as it is. Like if they had 30 max HP originally, then it was decreased over some turns to 15 max HP, and they took 5HP damage so they'd have 10/15HP and then they get healed of the state, they would have 10/30HP.

I have SOME of this figured out from scrounging around, but don't quite have the whole thing. I have a state that is applied to all characters and battlers that just monitors their turn. In that state, I have the code below. It works excellent for a couple other afflictions I've got.

Here's what I've got so far, from frankensteining code pieces together the best I could:
Code:
<Custom Action End Effect>
$gameParty.aliveMembers().forEach(battler => {
    if (battler.isStateAffected(16)) {
        $gameParty.members().forEach(function(m){
         m.addParam(0, -(battler.mhp * 0.05));
            });
        if (battler.hp <= 0) {
            battler.performCollapse();
            }
        }
    }
</Custom Action End Effect>

Now the above code works at decreasing max HP for each turn made in battle, but when one is afflicted, it applies to all party members. I know that's because of $gameParty.members().forEach(function(m){m.addParam(0, -(battler.mhp * 0.05));}); but when I cut it down to addParam(0, -(battler.mhp * 0.05)) it doesn't work. Even tried battler.addParam(0, -(battler.mhp * 0.05)) with no luck. It also decreases the max HP based on the current max HP rather than the original max HP. Using any healing item that removes the state also doesn't return the max HP back to its original either.

Any and all help is welcome! Thank you!
but when I cut it down to addParam(0, -(battler.mhp * 0.05)) it doesn't work. Even tried battler.addParam(0, -(battler.mhp * 0.05)) with no luck
thats because battler isnt a reference to anything by itself in that tag. it works in your forEach() because you created that reference. if you want to get the battler the state is active on, you can use "user" and run the functions directly on them.

It also decreases the max HP based on the current max HP rather than the original max HP.
yes. battler.mhp will get you that battler's max hp after all calculations are done in order to determine its value. that includes changes done to it by addParam(). you'd need to separate the values. you can have the battler's "true" max hp be stored on the state's initial application and keep using addParam() based on that value.

Using any healing item that removes the state also doesn't return the max HP back to its original either.
you dont have it say anywhere in the state to give back the max hp lost through the state. to do that, you could also store the value of the hp lost every time the state activates and then give it back on removal

EDIT: also, unless you changed it, max hp has a minimum value of 1. that state cant cause a battler's hp to hit 0 by itself, so theres no need to check if theyve been killed by it
Here's what I've got so far
This is a little confusing, because it doesn't appear to be doing what you described you want to do.

And I see you recognize that in your description after the notetag, I'm just not sure why looking at all aliveMembers seemed like a necessary thing to do at all 😉

Yanfly's notetags provide a user variable that is the battler with the state on it. So there's no need for you to go through some convoluted searching of the party to check whether they have this state, the state already knows.

The other issue is that you're not doing anything to track the original value of the max hit points. So you won't know how much to give back when the state goes away to restore them to where they started (and that's, logically, why you're decreasing on the current max HP...that's what mhp means).

Code:
<Custom Apply Effect>
// Store the Max HP
user.origHp = user.mhp;
user.lostHp = 0;
</Custom Apply Effect>

<Custom Action End Effect>
// Lose 5% of the original Max HP
let loss = Math.round(user.origHp * .05);
user.lostHp += loss;
user.addParam(0, -loss);
</Custom Action End Effect>

<Custom Remove Effect>
// Give back the HP and remove the temporary properties
user.addParam(0, user.lostHp);
delete user.origHp;
delete user.lostHp;
</Custom Remove Effect>
This is a little confusing, because it doesn't appear to be doing what you described you want to do.

And I see you recognize that in your description after the notetag, I'm just not sure why looking at all aliveMembers seemed like a necessary thing to do at all 😉

Yanfly's notetags provide a user variable that is the battler with the state on it. So there's no need for you to go through some convoluted searching of the party to check whether they have this state, the state already knows.

The other issue is that you're not doing anything to track the original value of the max hit points. So you won't know how much to give back when the state goes away to restore them to where they started (and that's, logically, why you're decreasing on the current max HP...that's what mhp means).

Code:
<Custom Apply Effect>
// Store the Max HP
user.origHp = user.mhp;
user.lostHp = 0;
</Custom Apply Effect>

<Custom Action End Effect>
// Lose 5% of the original Max HP
let loss = Math.round(user.origHp * .05);
user.lostHp += loss;
user.addParam(0, -loss);
</Custom Action End Effect>

<Custom Remove Effect>
// Give back the HP and remove the temporary properties
user.addParam(0, user.lostHp);
delete user.origHp;
delete user.lostHp;
</Custom Remove Effect>

Thanks for the quick responses!

Haha it looks like I forgot to give some information again when trying to make sure I'm giving all the info I need to.

The notetag code there definitely works! However it only works at the end of the afflicted character's turn where I'm looking to have it decrease every turn made, including the enemies.

This is the whole notetag code that I have, which contains effects for multiple states that I require a calculation on each turn (unadjusted with any of the new coding) which I have on all players and enemies. I had found another thread where a person wanted their party to each get a max hp buff, so I had plugged that into here, but made it a debuff. Obviously it didn't work, so I was in the process of trying to figure out how to make it apply to only the affected person, but couldn't figure it out. Totally forgot to ask how to store the original max hp, figure out the difference over time, and reapply the difference dealt when the state is removed, haha:

Code:
<Custom Action End Effect>

$gameTroop.aliveMembers().forEach(battler => {

// Decrease Max HP effect
    if (battler.isStateAffected(16)) {
        $gameTroop.members().forEach(function(m){
 m.addParam(0, -(battler.mhp * 0.05));
});
        if (battler.hp <= 0) {
            battler.performCollapse();
    }
// Petrify effect
    } else if (battler.isStateAffected(14)) {
     battler.addStateCounter(14, -1);
     if (battler.getStateCounter(14) == 0) {
        battler.addState(15);     
        battler.removeState(14);
    }
// Toxic effect   
    } else if (battler.isStateAffected(13)) {
        battler.gainHp(-(Math.floor(Math.random()*(battler.mhp * 0.1)+(battler.mhp * 0.05))));
        battler.startDamagePopup();
        if (battler.hp <= 0) {
            battler.performCollapse();
    }
    }

});

$gameParty.aliveMembers().forEach(battler => {

// Decrease Max HP effect
    if (battler.isStateAffected(16)) {
        battler.addParam(0, -(battler.mhp * 0.5));
});
        if (battler.isDead()) {
            battler.performCollapse();
    }
// Petrify effect
    } else if (battler.isStateAffected(14)) {
     battler.addStateCounter(14, -1);
     if (battler.getStateCounter(14) == 0) {
        battler.addState(15);     
        battler.removeState(14);
    }             
// Toxic effect   
    } else if (battler.isStateAffected(13)) {
        battler.gainHp(-(Math.floor(Math.random()*(battler.mhp * 0.1)+(battler.mhp * 0.05))));
        battler.startDamagePopup();
        if (battler.hp <= 0) {
            battler.performCollapse();
    }
    }

});

</Custom Action End Effect>

So as you can see, I'd like to have the max hp decrease when each battler, party member or enemy, takes their turn, in this format if possible.

Would it be just a matter of adding
// Lose 5% of the original Max HP
let loss = Math.round(user.origHp * .05);
user.lostHp += loss;
user.addParam(0, -loss);
here in this multi-state state and then having the Custom Apply and Custom Remove in the actual state (16) itself?

EDIT: Nope! That doesn't work. Probably because of the declared variables are in the 'every turn' state, and not in the actual state?
I'm looking to have it decrease every turn made, including the enemies.
...okay. So then you put the apply and remove effects in state 16, and reference the battler.origHp and battler.lostHp in the same way I did in your calculations.
GOT IT. It wasn't functioning at first, but it was just some left over parentheses/brackets from trying previous things out that got overlooked!

Thanks for the help, @ATT_Turan !

BIG OL' STINKY EDIT:

Just noticed that if the battler dies, their HP doesn't revert back. I've tried a few things so far, like a custom conclude effect, but nada on the working front yet.
The thread in my signature has an edit to make Custom Remove effects work on death.
The thread in my signature has an edit to make Custom Remove effects work on death.
I tested out your plugin, and It looks like it works in its own way; when the afflicted character dies, their max HP is no longer at the decreased amount. But what it does is it changes the display format of the HP ( I have Current HP/Max HP, but the plugin changes it to just displaying the current HP, which on death is 0), and it changes the character's max HP to 999. So I'm not sure what's happening there. I placed it above all YEP plugins, and for testing, every where else, and it always returned the same.

What I've done that does seem to SORT OF work too is a Custom Conclude in state 16:

Code:
<Custom Apply Effect>
// Store the Max HP
user.origHp = user.mhp;
user.lostHp = 0;
</Custom Apply Effect>

<Custom Remove Effect>
// Give back the HP and remove the temporary properties
user.addParam(0, user.lostHp);
delete user.origHp;
delete user.lostHp;
</Custom Remove Effect>

<Custom Conclude Effect>
// Check if the user is dead.
    if (user.hp <= 0) {
      user.addParam(0, user.lostHp);
      delete user.origHp;
      delete user.lostHp;
    }
</Custom Conclude Effect>

However the Custom Conclude only works if the afflicted character brings his own HP to 0/kills himself. When that happens, his max HP reverts to normal. If anyone else kills him (enemy or ally) his max HP stays at its decreased amount. I've tried a number of things out, to the point of not remembering all that I've done, and this is the closest thing I've got to working. I just need it so that the Custom Conclude applies regardless of who lands the killing blow.

EDIT:
I realized that Custom Conclude pertains to the the person who has the state with Custom Conclude, not the target. So instead I plopped it onto the Every Turn state and changed 'user' to 'target' and that seems to work! Now to troubleshoot stacking those states... But that's a whole other matter now, haha.

Original thread: https://forums.rpgmakerweb.com/threads/yep-buffs-states-state-that-decreases-max-hp-per-battler-turn.169708/