MP Regeneration only when HP isn't low

Hello. I need to do two things: make an actor regenerate MP (in and out of battle) by the amount of his current HP divided by 4, and limit the max MP he will be able to regenerate if he's hurt, to the same percentage his current HP is at from his max HP. What I mean is, for example: if the actor has 300 HP (max) and 150 MP (max), he would regenerate 75 MP per turn in battle; if he has 200 HP he'd regenerate 50 MP, and so on. If the actor was at 300 HP, then his MP would recover fully and at its fastest pace, but if he was at 200 HP (66% of his max HP), then his MP would regenerate slower and the regeneration would stop at the same percentage (99 MP of 150, which is also 66%).


I also want to apply the same regeneration mechanics outside of battle. And every actor would have the same regeneration mechanics from the start of the game, so they aren't supposed to be triggered by skills. I have no idea how to do any of these things, so any help would be appreciated.  
Could have probably just pulled this off with events, or wrote a better script, but I didn't have enough time, so here is something in between instead.


First, install this script...

Spoiler
#===============================================================================
# ** Module: Turn Manager
#===============================================================================
module Turn_Manager
#-----------------------------------------------------------------------------
# * New Method: proc_effects
#-----------------------------------------------------------------------------
def self.proc_effects
#==========================================================================#
#= Actor Proc Effects Below =#
#==========================================================================#
$game_party.members.each do |actor|
#==========================================================================#
#-------------------------------------------------------------------------
# Party MP Regeneration (Always)
#-------------------------------------------------------------------------
dmg = [actor.hp * 0.25, actor.mmp].min.round.to_i
if $imported["YEA-BattleEngine"] && dmg > 0
Sound.play_recovery
text = sprintf(YEA::BATTLE:😛OPUP_SETTINGS[:mp_heal], dmg.group)
actor.create_popup(text, "MP_HEAL")
end
actor.mp += dmg
#==========================================================================#
end # $game_party.member.each do |a|
#==========================================================================#
#= Enemy Proc Effects Below =#
#==========================================================================#
return unless SceneManager.scene == Scene_Battle
$game_troop.members.each do |enemy|
#-------------------------------------------------------------------------
# Enemy MP Regeneration (Always)
#-------------------------------------------------------------------------
dmg = [enemy.hp * 0.25, enemy.mmp].min.round.to_i
if $imported["YEA-BattleEngine"] && dmg > 0
Sound.play_recovery
text = sprintf(YEA::BATTLE:😛OPUP_SETTINGS[:mp_heal], dmg.group)
enemy.create_popup(text, "MP_HEAL")
end
enemy.mp += dmg
#==========================================================================#
end
end # proc_effects
end # Turn_Manager

Then call the method at the end of each turn.

Spoiler
turn_manager_01.PNG

That's it for battles, you'd have to duplicate that script call for each individual troop. Kinda inefficient.


As for maps, you can call the same method within a common event. Flip the switch whenever you want your actors to regenerate MP.

Spoiler
turn_manager_02.PNG

Adjust the number of steps taken before the party regenerates MP to your liking. It is set to 20 in the example above.
Thanks so much for taking your time making that script, it works perfectly!
Sorry for not testing everything before, but I've found that the actors will always eventually regenerate their full MP, even if they are hurt. What changes is the pace at which they do, but the regeneration doesn't stop at the same percentage of their current HP.
That's what I get for being in a hurry. I've updated a few things.


Use this script instead, throw old one out.

Spoiler
#===============================================================================
# ** Module: Turn Manager
#===============================================================================
module Turn_Manager
#-----------------------------------------------------------------------------
# * New Method: proc_effects
#-----------------------------------------------------------------------------
def self.proc_effects
#==========================================================================#
#= Actor Proc Effects Below =#
#==========================================================================#
$game_party.members.each do |actor|
#==========================================================================#
#-------------------------------------------------------------------------
# Party MP Regeneration (MP Rate is less than HP Rate)
#-------------------------------------------------------------------------
unless actor.mp_rate >= 1.0 || actor.mp_rate >= actor.hp_rate
dmg = [actor.hp * 0.25, actor.mmp].min.round.to_i
if $imported["YEA-BattleEngine"] && dmg > 0
Sound.play_recovery
text = sprintf(YEA::BATTLE:😛OPUP_SETTINGS[:mp_heal], dmg.group)
actor.create_popup(text, "MP_HEAL")
end
actor.mp = [actor.mp + dmg, actor.mmp * actor.hp_rate].min.round.to_i
end # unless
#==========================================================================#
end # $game_party.member.each do |a|
#==========================================================================#
#= Enemy Proc Effects Below =#
#==========================================================================#
return unless SceneManager.scene == Scene_Battle
$game_troop.members.each do |enemy|
#-------------------------------------------------------------------------
# Enemy MP Regeneration (Always)
#-------------------------------------------------------------------------
unless enemy.mp_rate >= 1.0 || enemy.mp_rate >= enemy.hp_rate
dmg = [enemy.hp * 0.25, enemy.mmp].min.round.to_i
if $imported["YEA-BattleEngine"] && dmg > 0
Sound.play_recovery
text = sprintf(YEA::BATTLE:😛OPUP_SETTINGS[:mp_heal], dmg.group)
enemy.create_popup(text, "MP_HEAL")
end
enemy.mp = [enemy.mp + dmg, enemy.mmp * enemy.hp_rate].min.round.to_i
end # unless
#==========================================================================#
end
end # proc_effects
end # Turn_Manager



As for the common event that regenerates mp on the map...

Spoiler
turn_manager_02_update.PNG

A 60 frame wait time allowed the method to be skipped entirely, so I lowered it. Also made sure the player couldn't exploit regeneration by moving 20 steps and stopping so that they would keep regenerating every x frames until they moved again... Hopefully I didn't miss anything this time.
It all works well now! I can't thank you enough for all your work, you have been very helpful.
I really need to learn to test everything before talking... this is embarrassing.


I'm aware you may have gotten tired of this topic (which is completely understandable as you've already helped me a ton), but I'm afraid I've found another issue: the enemies are unaffected by the script. They don't regenerate MP at all.  I know I only asked for actor's MP regeneration in the OP, but it never ocurred to me that the enemies also needed the regeneration, as it's crucial to make the gameplay balanced. It was a great addition on your part, and I'd really like to see if it's possible to introduce it in the game as well.
Enemy MP regeneration was already implemented, I just made a small error on one line which caused the script to skip that part of the code indefinitely. It's an easy fix.


Locate this line.


#==========================================================================#
#= Enemy Proc Effects Below =#
#==========================================================================#
return unless SceneManager.scene == Scene_Battle


Replace it with this

Code:
    #==========================================================================#
    #=                      Enemy Proc Effects Below                          =#
    #==========================================================================#
    return unless SceneManager.scene_is?(Scene_Battle)
Everything is working fine now. Again, you have my gratitude.

Original thread: https://forums.rpgmakerweb.com/threads/mp-regeneration-only-when-hp-isnt-low.64755/