MV "Constrained" - How to apply effect when enemy with state is defeated

I recently added a "constrained" status effect to my game. When an enemy successfully applies the "constrained" state on a character, that character cannot move for the duration, and that enemy acquires a different state that causes them to only attack the targeted character for the duration.
If something were to interrupt the states of either the attacker or the target, such as the target dies or the attacker has a conflicting state applied, the other character obviously loses their corresponding state and restriction as well.

The one instance this does not work is when a "constraining" enemy is defeated, their target remains "constrained" until the timer runs out like normal. I'm using Yanfly's Buffs&States Core, but <Custom Remove Effect> <Custom Leave Effect> don't run when a state ends because a battler has been defeated. I've implemented a similar effect before by using <Custom Apply Effect> on State #1 to remove a state linked to a specific character when that character dies, but because <Custom Apply Effect> executes after state 1 is applied, is seems checking .isStateAffected() always return "false" because every other status effect is already removed.
Troop events don't seem to be the solution, since the Enemy HP condition relies on a designated index.

So I'm looking for some way to remove the "constrained" condition from an actor when an enemy with the corresponding "constraining" state is defeated. The way they're currently implemented, the generic state that the actors gain does not contain information on which enemy is their attacker. The enemies gain separate states corresponding to the actor they have "constrained" which end if that actor HP <= 0.


There are two other minor issues with this mechanic that I would appreciate some help with:

1. To prevent this from completely overwhelming the player, I set a condition with Continuum AI Eval for enemies to only use a constraining skill when there isn't already a party member restrained. ( <AI Eval: 1> !$gameParty.members().some(actor => actor.isStateAffected(21)) </AI Eval>) However, it seems the check only happens once at the start of the turn and not when the move actually executes, because if a troop contains 2 snake enemies and they both happen to select that skill from their pattern, there's a possibility they both successfully hit that turn and there are 2 constrained party members.
Right now with how the troops are set up, it's only possible for 2/3 party members to be constrained at most, which is difficult, but rare and still entirely possible for the player to fight their way out of. For that, I don't consider this issue "game breaking", but for consistency and QA I'd prefer to keep the restriction to 1 party member.

2. As an additional effect of the "constrained" condition, I would like to have characters with that state take increased damage, like with the Sp-Parameter‍s Physical Damage and Magical Damage, but I haven't found a way to do the equivalent to Certain Hit damage. Is there a way to increase the rate of Certain Hit damage in the same way, or some way to modify the rate of all three equally? My understanding is the guard modifier only ever adds to the existing defense and can't be used to increase the rate of incoming damage either.
I'm using Yanfly's Buffs&States Core, but <Custom Remove Effect> <Custom Leave Effect> don't run when a state ends because a battler has been defeated
Turan has a tweak for that

However, it seems the check only happens once at the start of the turn and not when the move actually executes, because if a troop contains 2 snake enemies and they both happen to select that skill from their pattern, there's a possibility they both successfully hit that turn and there are 2 constrained party members.
You could make the 2 snakes different enemies and give them mutually exclusive conditions on using the skill. or maybe keep them as the single enemy in the database but see if you can refer to the user's index in the enemy troop to help determine if they should be able to use the skill that turn. I normally use Yanfly's AI plugin, and that one does let enemies choose their actions as their turns come up as opposed to at the start of the turn.

Is there a way to increase the rate of Certain Hit damage in the same way, or some way to modify the rate of all three equally?
there is no certain hit damage rate parameter, but you are already using yanfly's buffs and states plugin. You can make a react effect that increases the incoming damage taken by certain hits (or all hit types) with the notetag and the JS for it. it just wont be at the same step the traits are factored in

My understanding is the guard modifier only ever adds to the existing defense and can't be used to increase the rate of incoming damage either.
Correct on guard effect being unable to increase damage taken, but also keep in mind that it only effects damage taken while guarding and that it doesnt contribute any defence to any formula by itself. its a % reduction applied after the formula is evaluated, but only while guarding.
Turan has a tweak for that
Oh, that's perfect. Adding an if (target.hp > 0) conditional to any existing Remove effects was enough to keep those from being affected.

You could make the 2 snakes different enemies and give them mutually exclusive conditions on using the skill. or maybe keep them as the single enemy in the database but see if you can refer to the user's index in the enemy troop to help determine if they should be able to use the skill that turn. I normally use Yanfly's AI plugin, and that one does let enemies choose their actions as their turns come up as opposed to at the start of the turn.
I've been using Continuum's since it doesn't overwrite the enemy's existing attack pattern, but it's good to know that the option exists if I just need it for one or two mob enemies with these kinds of edge cases (assuming the two don't conflict within the same project). Otherwise adding a restriction for even/odd turns could work as well, so I know I have a couple of options here.

there is no certain hit damage rate parameter, but you are already using yanfly's buffs and states plugin. You can make a react effect that increases the incoming damage taken by certain hits (or all hit types) with the notetag and the JS for it. it just wont be at the same step the traits are factored in
So this would be <Custom React Effect>? The help description is very confusing, it states
This effect will run before damage calculation occurs and will return the
'value' variable as a damage modifier.
But from my testing it is not a modifier, it is the value of the damage dealt and the default is the outcome of the damage formula.
So if I wanted to increase all incoming damage by 25% It wouldn't be value = 1.25 or value = 125, it would be <Custom React Effect> value = (value * 1.25) </Custom React Effect>

When you say "it just wont be at the same step the traits are factored in," do you mean the damage formula of the skill itself won't be modified since the add state trait applies after damage is dealt?

Correct on guard effect being unable to increase damage taken, but also keep in mind that it only effects damage taken while guarding and that it doesnt contribute any defence to any formula by itself. its a % reduction applied after the formula is evaluated, but only while guarding.
Yeah, I was originally thinking since the guard effect worked by applying a consistent modifier to the outcome of the damage formula like I wanted it to, I could use Special Flag: Guard and set the Guard Effect to -50 or something to increase the rate of damage, but it can't go lower than 0.
But from my testing it is not a modifier, it is the value of the damage dealt and the default is the outcome of the damage formula...

...When you say "it just wont be at the same step the traits are factored in," do you mean the damage formula of the skill itself won't be modified since the add state trait applies after damage is dealt?
Your observation is correct. the value variable is the damage that is to be dealt. during <react>, its after its been calculated (so not only after the formula is evaluated, but also after damage/element/guard rates are applied), but before its been executed on the target.

Remember to always use Math.round(), ceil(), or floor() to round value if you did something that can make it not an integer. the damage dealt is rounded by the engine end of its evaluation, but <react> effects happen after that point and can allow decimal damage to be dealt. Theres a few similar checks and safeguards that these states ignore.

So if I wanted to increase all incoming damage by 25% It wouldn't be value = 1.25 or value = 125, it would be <Custom React Effect> value = (value * 1.25) </Custom React Effect>
yes, but youll need to be a bit more precise. that would affect healing taken as well.
Code:
<Custom React Effect>
if (this.isDamage() && value > 0)
  value = Math.ceil(value * 1.25);
</Custom React Effect>
this increases damage provided that the action is meant to deal damage (so not set as hp/mp recovery) and did more than 0 damage at this point (the attack wasnt absorbed or a similar react effect didnt make the damage negative and cause it to heal). The tags are evaluated within a Game_Action, so action functions can be used
Remember to always use Math.round(), ceil(), or floor() to round value if you did something that can make it not an integer. the damage dealt is rounded by the engine end of its evaluation, but <react> effects happen after that point and can allow decimal damage to be dealt. Theres a few similar checks and safeguards that these states ignore.
Thanks, understanding how the systems interact under the surface always makes future problem solving easier.

Speaking of, I don't know how well documented this is but I found basically no information when I was looking into Guard Effect, and it turns out my harebrained scheme to use it as a damage multiplier almost works?

The database just says something like "Guard Effect will decrease if below 100%". I assumed if "f" is the damage formula, the final result would look something like f/(2^(x/100))where a 0% would mean no reduction in damage, a 50% would be half as effective at reducing damage, and 200% would be twice as good at reducing damage.

It turns out the ACTUAL formula is something like f * (50/x) so Guard Effect 50% is actually no effect, Guard Effect 25% DEALS twice as much damage, and Guard Effect 0%, uhhh...

Screenshot (423).png

I didn't know that could happen.

So it turns out you CAN use the Sp-Parameter: Guard Effect and Special Flag: Guard traits together to apply a damage modifier to a battler. I can't use it for Constrain, because the Cannot Move restriction negates the guard flag.
It turns out the ACTUAL formula is something like f * (50/x) so Guard Effect 50% is actually no effect, Guard Effect 25% DEALS twice as much damage, and Guard Effect 0%, uhhh...
I see now. i misremembered the guard formula as being damage / (grd+1).its actually damage / (grd*2), so having reduced guard effect to 0 would result in that even in a vanilla project. good catch

Original thread: https://forums.rpgmakerweb.com/threads/constrained-how-to-apply-effect-when-enemy-with-state-is-defeated.183416/