Guaranteed Critical if Affected with State

I want to add a guaranteed critical hit if a player/battler is affected by state.

I figure it will be in the damage formula for the Attack skill, and I have a rough idea of how it should look. Would anyone mind correcting the below? I imagine it would look something like this, but I know I am missing parts of the syntax.

If b.state = 11 then critical hit = 100% | a.atk * 4 - b.def *2

Thanks!!
b.state would mean the target of the attack has the state, not the user. For the user to have the state in the calculation, use a.state.
I'm at work and can't test but something like this perhaps?

c = a.atk * 4 - b.def *2; a.isStateAffected(11) ? c*2 : c

edit:
a.isStateAffected(11) if you want to check the user of the skill for state 11
b.isStateAffected(11) if you want to check the target of the skill for state 11
@JtheDuelist That's correct. Essentially, if something has the sleep state on them, I want a physical attack to do a critical hit.

@Traveling Bard I'll check and let you know. Thanks!

Edit. Yeah, I corrected the target part to represent the enemy. It does extra damage, but it is not a critical hit.
b.isStateAffected(11) ? (a.atk * 4 - b.def *2 )*2 : a.atk * 4 - b.def *2


I've had to play around with damage formula logic a bit to get things you'd think should work to work. Likely my inexperience haha anywho, try the above. It's essentially the same thing just written out more. If that doesn't work, I'll give it a few spins when I get home in a few hours.
Yeah, that was essentially what I changed it to earlier. It does do the extra damage, it just doesn't register as a critical hit (changing the color of the text, etc.), so the player would see the additional damage, but not make the connection that it is a crit.
Oh, gotcha. Instead of what I have showed you so far just leverage this plugin from yanfly:

http://yanfly.moe/2015/11/08/yep-26-critical-control/

Put the appropriate notetag under the sleep state and just crank up the critical rate for your use cases up to 100%. You'll need his damage core plugin as well but that should get you what you are looking for here.
I looked at that, but I think it works as a buff and not a debuff. Meaning, if I set a notetag on sleep to set the critical rate at say 999% to ensure a critical, then I would need to be under the effect of asleep to get the change in critical rate.

I'll give it a try and see if I can adjust it.
I would avoid @Traveling Bard's approach because, while it's clever, it will either make it impossible to land a "normal" Crit (if you choose "Critical: No") or make it possible to land a "double Crit" that deals 600% damage (if you choose "Critical: Yes" and the normal Crit calculation also succeeds).

I believe that the "Critical" property of an action can actually be changed at any point before the attack's results are finalized, meaning that you could just set the Critical property to true as part of the damage formula if the user (a) or target (b) is affected by the given State. I've never played around with Action Result properties in MV before so I don't know the syntax - maybe someone can help provide this?

Alternatively, a relatively simple change to the code base to always score the Crit. In the function Game_Action.prototype.itemCri (rpg_objects.js) the chance of a skill landing a Crit is determined (by default this is the same for any skill that can crit). You can change this method to automatically return a 1 (or even higher, like 9999) if the target is asleep and the skill is something that will crit whenever the target is asleep.
Thanks, @Wavelength! You gave me an idea with that critical property being set in the damage formula part.

I'm now emotionally invested in solving this with a damage formula!

Try this: if (b.isStateAffected(11)) b.result().critical = true; a.atk * 4 - b.def *2
@Traveling Bard's new formula looks good to me - and wouldn't require any changes to the code. @Shinma try it out and see if it works in your game! 😀
We are so close!

I get the critical hit in regards to it changing the color, but it only does normal damage.

Edit*

I understand the basic concepts of programming, so I understand that ?: is the equivalent of an if/then statement. I think that ; ends a statement and starts a new one?

I am not 100% sure of where the critical gets it's value. I know that the critical damage is * 3.

Maybe it derives it's value from the dmg formula so we should set it first?

Sidenote, in case anyone is testing it. The state is actually 10, I incorrectly set it as 11 in the first post.
Ok... this is going to sound crazy... but try this:

c = (a.atk * 4 - b.def *2); if (b.isStateAffected(10)){ b.result().critical = true; c*=3;} c

That should mimic a critical which I believe is 300% normal damage. Just change what c is multiplying by if you want to tweak that...

@Wavelength, would doing this give the possibility for double critical too? I guess it can be tested by setting a piece of equipment to 100% critical rate and swinging a few times.

You know, couldn't you also just set the sleep state to have a negative critical evasion parameter/trait?
That did indeed work!

I did think about the negative critical evasion, but I think that skill is set to dodge critical hits.

Edit* I tested it by setting CEV to - 999% on the Sleep state and every hit was still a normal hit.

BUT that gave me an idea. I went back and used your formula that was providing the color change but still doing base damage, and added 300% physical damage to the Sleep state. By doing so I was able to get the red text, deal 300% damage and ensure as not to get double criticals!

I really appreciate both of you helping me solve this issue!!
If you have the action sequence and critical control plugins from Yanfly (the latter requires the Damage Core), there's an action sequence command to "force critical". Using "if target.isStateAffected(10)" (10 is the default ID for Sleep) before it will check for your target to have the sleep state then guarantee a critical hit.
Thanks!
Sorry for the bump, but I was just wrestling w/ this problem myself today and figured out a simple solution that also resolves the double critical issue.
It requires Yanfly's Critical Control plugin, which of course also requires his Damage Core plugin. Here's the code:

<Critical Rate Formula>
if (target.isStateAffected(73)) {
rate = 1;
}
</Critical Rate Formula>

My state ID was 73, you should obviously change it to w/e suits your needs.

Original thread: https://forums.rpgmakerweb.com/threads/guaranteed-critical-if-affected-with-state.95929/