State that consumes MP on attack for a critical hit

Hello

I'm trying to figure out a way that while affected by a state, the standard attack will consume MP and inflict a critical hit always. Also if the user has less than 20 MP, no MP is consumed, and the attack functions as it normally would with criticals being a random chance. This way if the MP is too low, they can still use attack, which is why I'm not doing this through VisuStella skill costs.

It seemed like an easy thing to accomplish when I had the idea, but now trying to implement I've realized I don't really know how to do this.
JavaScript:
(() => {
    const runeBladeStateId = 10; // the auto-crit state
    const runeBladeMpCost = 20; // the mp cost
    const runeBladeState_itemCri = Game_Action.prototype.itemCri;
    Game_Action.prototype.itemCri = function(target) {
        const attack = this.isAttack() && this.item().damage.critical;
        const subject = BattleManager._subject;
        if (attack && subject.isStateAffected(runeBladeStateId) && subject.mp >= runeBladeMpCost) {
            subject.gainMp(-runeBladeMpCost);
            return 1;
        } else {
            return runeBladeState_itemCri.call(this, target);
        }
    };
})();

nw_t7eiXBozpi.gif


Not sure if it will be compatible with VS. You also easily do this with VS notetags, but I don't know how to use their framework, so maybe someone else can provide you with an alternative if you'd rather do it that way.
This way if the MP is too low, they can still use attack, which is why I'm not doing this through VisuStella skill costs.

Code:
<JS MP Cost>
 cost = user.mp >= 20 ? 20 : 0;
</JS type Cost>
@Kenen thank you, that works!

Code:
<JS MP Cost>
 cost = user.mp >= 20 ? 20 : 0;
</JS type Cost>

I actually thought of this after I posted. I can't believe I didn't think of it beforehand. It still has the problem of accounting for the attack being standard, not 100% critical, if the actor can't pay the MP cost. There's probably a simple solution to that but I just haven't been able to figure it out.

Doesn't matter I guess, since the above method works for me, but thanks for the assist regardless!
I actually thought of this after I posted. I can't believe I didn't think of it beforehand. It still has the problem of accounting for the attack being standard, not 100% critical, if the actor can't pay the MP cost. There's probably a simple solution to that but I just haven't been able to figure it out.
Someone wanting to do this via notetags should be able to stick a little boolean into the skill cost itself.

Code:
<JS MP Cost>
if (user.mp >= 20)
{
    user.paidMp = true;
    cost = 20;
}
else
{
    user.paidMp = false;
    cost = 0;
}
</JS MP Cost>

<JS Crtical Rate>
rate = user.paidMp ? 1 : .2;
</JS Critical Rate>

Original thread: https://forums.rpgmakerweb.com/threads/state-that-consumes-mp-on-attack-for-a-critical-hit.168283/