Skills using HP after MP is depleted?

In the game I am making, I wish to let my designated Black Mage sacrifice his own HP to cast spells if he has insufficient MP. Is there a way this can be achieved? I'd assume it would have to use Yanfly's Lunatic Mode to achieve this, I'm just not that familiar with the syntax of RPG Maker.
So...here's a way to do this, but it has a few questions/caveats.

Can your Black Mage always do this, or only once he learns a skill or has a state? I'll just do it off the class below.

This will do the trick, but I expect it will cause the skill to generally display an HP cost of 0.

Maybe the plugin is smarter than I expect and if the cost is 0 it doesn't display. But if it does, and if you badly don't want that, you can ask for help editing the plugin.

This uses Yanfly's Skill Core:
Code:
<Custom MP Cost>
cost=user.currentClass().id==Y ? Math.max(X, user.mp) : X;
</Custom MP Cost>

<Custom HP Cost>
cost=user.currentClass().id==Y ? Math.min(0, (user.mp-X)*-1) : 0;
</Custom HP Cost>

in both cases, replace X with the original MP cost of the spell and Y with the ID of your black mage class (no leading 0s). That should cause it to have the original MP cost, and any of that cost that the mage can't afford will transfer to an HP cost (and if he can't afford to cast it that way, it's just greyed out).
The former, my Black Mage can always do this.
So...here's a way to do this, but it has a few questions/caveats.

Can your Black Mage always do this, or only once he learns a skill or has a state? I'll just do it off the class below.

This will do the trick, but I expect it will cause the skill to generally display an HP cost of 0.

Maybe the plugin is smarter than I expect and if the cost is 0 it doesn't display. But if it does, and if you badly don't want that, you can ask for help editing the plugin.

This uses Yanfly's Skill Core:
Code:
<Custom MP Cost>
cost=user.currentClass().id==Y ? Math.max(X, user.mp) : X;
</Custom MP Cost>

<Custom HP Cost>
cost=user.currentClass().id==Y ? Math.min(0, (user.mp-X)*-1) : 0;
</Custom HP Cost>

in both cases, replace X with the original MP cost of the spell and Y with the ID of your black mage class (no leading 0s). That should cause it to have the original MP cost, and any of that cost that the mage can't afford will transfer to an HP cost (and if he can't afford to cast it that way, it's just greyed out).
Thank you so much for your help. I had to mess around with the code a bit but now it works perfectly! I'll continue to tweak it to match what I want for my game.

Original thread: https://forums.rpgmakerweb.com/threads/skills-using-hp-after-mp-is-depleted.153121/