Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[VX] Regeneration

Regeneration
by Dargor
Version 1.0


Introduction

This script will let actors regenerate HP/MP in 3 occasions. Regeneration can occur on map, when defending or at the end of a battle.
Also, this system is highly customizable. You can specify any regeneration formula you want, for any actors in any of the 3 occasions, and much more!

Features
  • Can regenerate HP/MP on map, when defending or at the end of a battle
  • Regeneration formulas are highly customizable; you can set any formula you want for any actors for any of the 3 occasions
  • Can turn On/Off any of the 3 Regen occasions
  • You can specify the Regen delay on map
  • You can make Anti-Regen States
  • A Super Guard multiplier can be applied on defense Regen formulas

Script

Code:
 

#==============================================================================

# ** Regeneration

#------------------------------------------------------------------------------

#  © Dargor, 2008

#  04/11/08

#  Version 1.0

#  Requested by shc0023

#------------------------------------------------------------------------------

#  VERSION HISTORY:

#   - 1.0 (01/11/08), Initial release

#------------------------------------------------------------------------------

#  INTRODUCTION:

#     This script will let actors regenerate HP/MP in 3 occasions.

#     Regeneration can occur on map, when defending or at the end of 

#     a battle. This system is highly customizable. You can specify any 

#     regeneration formula you want, for any actors in any of the 3

#     occasions!

#------------------------------------------------------------------------------

#  INSTRUCTIONS:

#   1) Paste the script above main

#   2) Edit the constants in the Regeneration module

#==============================================================================

 

#==============================================================================

#  ** Regeneration Configuration Module

#==============================================================================

 

module Regeneration

  # Regenerate after battles?

  Battle_Regen = true

  # Defense Regen

  Defense_Regen = true

  # Regenerate on map?

  Map_Regen = true

  # On Map Regeneration delay (in frames)

  Map_Regen_Delay = 240

  # End battle results allowed (0: Victory, 1: Escape, 2: Defeated)

  Battle_End = [0]

  # States that prevents from regenerating after battles

  NoRegen_States = [1]

  # Play recovery SE on map?

  Map_Recovery_SE = true

  # Super Guard Regeneration Multiplier

  Super_Guard_Multiplier = 3

  # Battle Regeneration rate formula for HP/MP

  # SYNTAX: {actor_id => "formula"}

  Battle_HP_Formulas = {}

  Battle_MP_Formulas = {}

  # Default formula if none specified

  Battle_HP_Formulas.default = "(self.maxhp / 100) * 6" # 6% of maxhp

  Battle_MP_Formulas.default = "(self.maxmp / 100) * 3" # 3% of maxmp

  # Defense Regeneration rate formula for HP/MP

  # SYNTAX: {actor_id => "formula"}

  Defense_HP_Formulas = {}

  Defense_MP_Formulas = {}

  # Default formula if none specified

  Defense_HP_Formulas.default = "(self.maxhp / 100) * 2" # 2% of maxhp

  Defense_MP_Formulas.default = "(self.maxmp / 100) * 1" # 1% of maxmp

  # Map Regeneration rate formula for HP/MP

  # SYNTAX: {actor_id => "formula"}

  Map_HP_Formulas = {}

  Map_MP_Formulas = {}

  # Default formula if none specified

  Map_HP_Formulas.default = "(self.maxhp / 100) * 2" # 2% of maxhp

  Map_MP_Formulas.default = "(self.maxmp / 100) * 1" # 1% of maxmp

end

 

#==============================================================================

# ** Game_System

#------------------------------------------------------------------------------

#  This class handles system-related data. Also manages vehicles and BGM, etc.

# The instance of this class is referenced by $game_system.

#==============================================================================

 

class Game_System

  #--------------------------------------------------------------------------

  # * Public Instance Variables

  #--------------------------------------------------------------------------

  attr_accessor  :map_regen

  attr_accessor  :battle_regen

  attr_accessor  :defense_regen

  #--------------------------------------------------------------------------

  # * Alias Listing

  #--------------------------------------------------------------------------

  alias dargor_vx_battle_regen_system_initialize initialize

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  def initialize

    # The usual

    dargor_vx_battle_regen_system_initialize

    # Map/Battle Regeneration flags

    @battle_regen = Regeneration::Battle_Regen

    @defense_regen = Regeneration::Defense_Regen

    @map_regen = Regeneration::Map_Regen

  end

end

 

#==============================================================================

# ** Game_Actor

#------------------------------------------------------------------------------

#  This class handles actors. It's used within the Game_Actors class

# ($game_actors) and referenced by the Game_Party class ($game_party).

#==============================================================================

 

class Game_Actor < Game_Battler

  #--------------------------------------------------------------------------

  # * Get Map HP/MP Regeneration Formulas

  #--------------------------------------------------------------------------

  def map_regen_formulas

    hp_formula = nil

    mp_formula = nil

    # Get actor's map hp regen formula

    if Regeneration::Map_HP_Formulas[@actor_id].nil?

      hp_formula = eval(Regeneration::Map_HP_Formulas.default)

    else

      hp_formula = eval(Regeneration::Map_HP_Formulas[@actor_id])

    end

    # Get actor's map mp regen formula

    if Regeneration::Map_MP_Formulas[@actor_id].nil?

      mp_formula = eval(Regeneration::Map_MP_Formulas.default)

    else

      mp_formula = eval(Regeneration::Map_MP_Formulas[@actor_id])

    end

    hp_formula = 1 if hp_formula == 0

    mp_formula = 1 if mp_formula == 0

    return [hp_formula, mp_formula]

  end

  #--------------------------------------------------------------------------

  # * Regenerate on Map

  #--------------------------------------------------------------------------

  def map_regenerate

    # Check if map regeneration is allowed

    if $game_system.map_regen

      # Make sure the actor is not in a NoRegen state

      for state_id in Regeneration::NoRegen_States

        if state?(state_id)

          # Do not regenerate

          return

        end

      end

      # Regenerate

      self.hp += map_regen_formulas[0]

      self.mp += map_regen_formulas[1]

    end

  end

  #--------------------------------------------------------------------------

  # * Get defense HP/MP Regeneration Formulas

  #--------------------------------------------------------------------------

  def defense_regen_formulas

    hp_formula = nil

    mp_formula = nil

    # Get actor's battle hp regen formula

    if Regeneration::Defense_HP_Formulas[@actor_id].nil?

      hp_formula = eval(Regeneration::Defense_HP_Formulas.default)

    else

      hp_formula = eval(Regeneration::Defense_HP_Formulas[@actor_id])

    end

    # Get actor's battle mp regen formula

    if Regeneration::Defense_HP_Formulas[@actor_id].nil?

      mp_formula = eval(Regeneration::Defense_MP_Formulas.default)

    else

      mp_formula = eval(Regeneration::Defense_MP_Formulas[@actor_id])

    end

    hp_formula = 1 if hp_formula == 0

    mp_formula = 1 if mp_formula == 0

    if super_guard

      hp_formula *= Regeneration::Super_Guard_Multiplier

      mp_formula *= Regeneration::Super_Guard_Multiplier

    end

    return [hp_formula, mp_formula]

  end

  #--------------------------------------------------------------------------

  # * Regenerate After Battles

  #--------------------------------------------------------------------------

  def defense_regenerate

    # Check if battle regeneration la allowed

    if $game_system.defense_regen

     # Make sure the actor is not in a NoRegen state

     for state_id in Regeneration::NoRegen_States

       if state?(state_id)

         # Do not regenerate

         return

       end

      end

     end

    @hp_damage = -defense_regen_formulas[0]

    @mp_damage = -defense_regen_formulas[1]

    self.hp -= @hp_damage

    self.mp -= @mp_damage

  end

  #--------------------------------------------------------------------------

  # * Get Battle HP/MP Regeneration Formulas

  #--------------------------------------------------------------------------

  def battle_regen_formulas

    hp_formula = nil

    mp_formula = nil

    # Get actor's battle hp regen formula

    if Regeneration::Battle_HP_Formulas[@actor_id].nil?

      hp_formula = eval(Regeneration::Battle_HP_Formulas.default)

    else

      hp_formula = eval(Regeneration::Battle_HP_Formulas[@actor_id])

    end

    # Get actor's battle mp regen formula

    if Regeneration::Battle_MP_Formulas[@actor_id].nil?

      mp_formula = eval(Regeneration::Battle_MP_Formulas.default)

    else

      mp_formula = eval(Regeneration::Battle_MP_Formulas[@actor_id])

    end

    hp_formula = 1 if hp_formula == 0

    mp_formula = 1 if mp_formula == 0

    return [hp_formula, mp_formula]

  end

  #--------------------------------------------------------------------------

  # * Regenerate After Battles

  #--------------------------------------------------------------------------

  def battle_regenerate

    # Check if battle regeneration is allowed

    if $game_system.battle_regen

      # Make sure the actor is not in a NoRegen state

      for state_id in Regeneration::NoRegen_States

        if state?(state_id)

          # Do not regenerate

          return

        end

      end

    end

    @hp_damage = -battle_regen_formulas[0]

    @mp_damage = -battle_regen_formulas[1]

    self.hp -= @hp_damage

    self.mp -= @mp_damage

  end

end

 

#==============================================================================

# ** Game_Party

#------------------------------------------------------------------------------

#  This class handles the party. It includes information on amount of gold

# and items. The instance of this class is referenced by $game_party.

#==============================================================================

 

class Game_Party

  #--------------------------------------------------------------------------

  # * Regenerate Party

  #--------------------------------------------------------------------------

  def regenerate

    if Graphics.frame_count % Regeneration::Map_Regen_Delay == 0

      for member in members

        total_hp = member.hp

        total_maxhp = member.maxhp

        member.map_regenerate

      end

      Sound.play_recovery unless total_hp == total_maxhp or !Regeneration::Map_Recovery_SE

    end

  end

end

 

#==============================================================================

# ** Game_Player

#------------------------------------------------------------------------------

#  This class handles maps. It includes event starting determinants and map

# scrolling functions. The instance of this class is referenced by $game_map.

#==============================================================================

 

class Game_Player < Game_Character

  #--------------------------------------------------------------------------

  # * Alias Listing

  #--------------------------------------------------------------------------

  alias dargor_vx_battle_regen_player_update update

  #--------------------------------------------------------------------------

  # * Frame Update

  #--------------------------------------------------------------------------

  def update

    # If player is free to move

    if movable?

      # Regenerate

      $game_party.regenerate

    end 

    # The Usual

    dargor_vx_battle_regen_player_update

  end

end

   

#==============================================================================

# ** Scene_Battle

#------------------------------------------------------------------------------

#  This class performs battle screen processing.

#==============================================================================

 

class Scene_Battle < Scene_Base

  #--------------------------------------------------------------------------

  # * Alias Listing

  #--------------------------------------------------------------------------

  alias dargor_vx_battle_regen_battle_end battle_end

  alias dargor_vx_battle_regen_execute_action_guard execute_action_guard

  #-----------------------------------------------------------------------------

  # Execute Action_Guard

  #-----------------------------------------------------------------------------

  def execute_action_guard

    # The Usual

    dargor_vx_battle_regen_execute_action_guard

    if $game_system.defense_regen

      # Regenerate actor

      @active_battler.defense_regenerate

      # Display regeneration text

      display_hp_damage(@active_battler)

      wait(45)

      @message_window.back_one

      display_mp_damage(@active_battler)

      wait(45)

      @message_window.clear

    end

  end

  #--------------------------------------------------------------------------

  # * End Battle

  #     result : Results (0: win, 1: escape, 2:lose)

  #--------------------------------------------------------------------------

  def battle_end(result)

    # If regenerating is allowed with this battle end result

    if Regeneration::Battle_End.include?(result)

      for member in $game_party.members

        # Regenerate actor

        member.battle_regenerate

        # Display regeneration text

        Sound.play_recovery

        display_hp_regeneration(member)

        display_mp_regeneration(member)

        wait_for_message

        @message_window.clear

      end

    end

    # The usual

    dargor_vx_battle_regen_battle_end(result)

  end

  #--------------------------------------------------------------------------

  # * Show HP Regeneration

  #     target : Target

  #     obj    : Skill or item

  #--------------------------------------------------------------------------

  def display_hp_regeneration(target)

    if target.hp_damage == 0                # No damage

      fmt = target.actor? ? Vocab::ActorNoDamage : Vocab::EnemyNoDamage

      text = sprintf(fmt, target.name)

    elsif target.absorbed                   # Absorb

      fmt = target.actor? ? Vocab::ActorDrain : Vocab::EnemyDrain

      text = sprintf(fmt, target.name, Vocab::hp, target.hp_damage)

    elsif target.hp_damage > 0              # Damage

      if target.actor?

        text = sprintf(Vocab::ActorDamage, target.name, target.hp_damage)

        Sound.play_actor_damage

        $game_troop.screen.start_shake(5, 5, 10)

      else

        text = sprintf(Vocab::EnemyDamage, target.name, target.hp_damage)

        Sound.play_enemy_damage

        target.blink = true

      end

    else                                    # Recovery

      fmt = target.actor? ? Vocab::ActorRecovery : Vocab::EnemyRecovery

      text = sprintf(fmt, target.name, Vocab::hp, -target.hp_damage)

    end

    $game_message.texts.push(text)

  end

  #--------------------------------------------------------------------------

  # * Show MP Regeneration

  #     target : Target

  #     obj    : Skill or item

  #--------------------------------------------------------------------------

  def display_mp_regeneration(target)

    return if target.dead?

    return if target.mp_damage == 0

    if target.absorbed                      # Absorb

      fmt = target.actor? ? Vocab::ActorDrain : Vocab::EnemyDrain

      text = sprintf(fmt, target.name, Vocab::mp, target.mp_damage)

    elsif target.mp_damage > 0              # Damage

      fmt = target.actor? ? Vocab::ActorLoss : Vocab::EnemyLoss

      text = sprintf(fmt, target.name, Vocab::mp, target.mp_damage)

    else                                    # Recovery

      fmt = target.actor? ? Vocab::ActorRecovery : Vocab::EnemyRecovery

      text = sprintf(fmt, target.name, Vocab::mp, -target.mp_damage)

    end

    $game_message.texts.push(text)

  end 

end

 

Credits and Thanks

Thanks to shc0023 for requesting the script.

Hope you like it!
-Dargor
 
I don't think you should have put in those eval()'s. I know you want to make it more user friendly, but why more user friendly if it costs your script's speed a lot?
 
Being able to set different formulas for each actors was part of the original request and it's the most user friendly method I found. I highly doubt there will be any noticeable speed change unless your computer is really slow.

If you know of another better/faster method, please feel free to share it!
Take care!
-Dargor
 
Dargor":2olzo3ut said:
Being able to set different formulas for each actors was part of the original request and it's the most user friendly method I found. I highly doubt there will be any noticeable speed change unless your computer is really slow.

If you know of another better/faster method, please feel free to share it!
Take care!
-Dargor

Altough a case/swtich is not as nice as this, it sure is quicker - and you don't have that many possibilities.. Mainly, regeneration takes place with the maxstats as their base. So the ONLY thing you need to define is the rate, and perhaps an addition. This only allows easy formulas, but you could extend this function later.

For example:

Battle_HP_Formulas.default = ['liniear', 0.06, 1] would result in ax+b where a = 0.06, b = 1
Battle_HP_Formulas.default = ['quad', 0.02, 0.04, 0.5] would result in ax*x+ bx + c where a = 0.02, b = 0.04, c = 0.5

etc. You could even take it further by just doing: [0.06, 1] then use an size check to determine what formula you use.

Code:
whateverdef(base, args*)

result = 0; args.reverse!
for i in 0...args.size
  result += args[i] * base ** i
end
return result;

ex.

whateverdef(self.max_hp, [4,3,2]) # axx + bx + c

=>

result += 2 * self.max_hp ** 0 (2 * 1 = c)
result += 3 * self.max_hp ** 1 (bx)
result += 4 * sefl.max_hp ** 2 (axx)

etc...

All liniear, kwad, and so on from the from ax^n + bx^n-1...+ cx^0 are then defined.
I think it's better then using a mod eval.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top