[Resolved]Help with a HP Sacrifice script.

Started by Simon Greedwell, March 01, 2016, 01:19:48 pm

Previous topic - Next topic

Simon Greedwell

March 01, 2016, 01:19:48 pm Last Edit: March 02, 2016, 02:17:53 pm by Simon Greedwell
This is LockeZ's "HP Sacrifice" script. It allows the creation of skill that consume HP instead of SP.

#==============================================================================
# HP Sacrifice
# By LockeZ
#==============================================================================
# This script allows you to create skills and items that cost HP when used.
#
# Compatible with the default battle system.
# Compatible with Tankentai.
#
# Contact me at ff3lockez@yahoo.com with questions.
#==============================================================================

module LockeZ
  # Do not remove this line
  HP_Sacrifice_Action = {'Skill' => {}, 'Item' => {}}
  # Do not remove this line
 
  # Below here are the lines you can change to control which actions
  #   cost HP, and how much.
 
  # Format for each line:
  # ---------------------
  # HP_Sacrifice_Action[action_type][id] = [chance, formula, amount, anim_id, pop, can_kill]
 
 
  # And here is what each of those things means:
 
  # action_type = Should be set to 'Skill' or 'Item'
  # id          = ID number of the skill/item that costs HP
  #
  # chance  = chance of damaging self
  # formula = How the amount lost is calculated.  Must be 'integer' or 'percent'.
  # amount  = Amount of HP lost, based on formula.  If formula is set to
  #           integer, this is the actual amount lost.  If it's set to percent,
  #           this is the percent lost.
  # anim_id = ID of the animation shown over the user; leave nil or 0 for
  #           no animation
  # pop     = true or false, whether the sacrificed amount pops up visually
  #           over the user's head like damage.
  # can_kill = true or false.  If false the skill will not reduce the user below
  #            1 HP.  If true it can kill the user.
 
 
  # Example Skills
  # The "Darkness" spell (spell #28) now costs 250 HP to cast.  It won't
  #   reduce the caster below 1 HP.
 
  HP_Sacrifice_Action['Skill'][86] = [100, 'percent', 5, nil, false, false]
  # The "Mass Darkness" spell (spell #30) now costs 500 HP to cast.  It can
  #   kill the caster.
 
  HP_Sacrifice_Action['Skill'][86] = [100, 'percent', 5, nil, false, false]
  HP_Sacrifice_Action['Skill'][90] = [100, 'percent', 10, nil, false, false]
  HP_Sacrifice_Action['Skill'][91] = [100, 'percent', 12, nil, false, false]
  HP_Sacrifice_Action['Skill'][93] = [100, 'percent', 5, nil, false, false]
  HP_Sacrifice_Action['Skill'][94] = [100, 'percent', 7, nil, false, false]
  HP_Sacrifice_Action['Skill'][100] = [100, 'percent', 5, nil, false, false]
  HP_Sacrifice_Action['Skill'][99] = [100, 'percent', 5, nil, false, false]
  HP_Sacrifice_Action['Skill'][101] = [100, 'percent', 10, nil, false, false]
  HP_Sacrifice_Action['Skill'][103] = [100, 'percent', 7, nil, false, false] 
  HP_Sacrifice_Action['Skill'][114] = [100, 'percent', 7, nil, false, false]
  HP_Sacrifice_Action['Skill'][115] = [100, 'percent', 12, nil, false, false]
  HP_Sacrifice_Action['Skill'][116] = [100, 'percent', 15, nil, false, false]
  HP_Sacrifice_Action['Skill'][125] = [100, 'percent', 15, nil, false, false]
  HP_Sacrifice_Action['Skill'][200] = [100, 'percent', 18, nil, false, false]
  HP_Sacrifice_Action['Skill'][130] = [100, 'percent', 7, nil, false, false]
  HP_Sacrifice_Action['Skill'][131] = [100, 'percent', 10, nil, false, false]
  HP_Sacrifice_Action['Skill'][126] = [100, 'percent', 15, nil, false, false]
  HP_Sacrifice_Action['Skill'][88] = [100, 'percent', 5, nil, false, false]
  HP_Sacrifice_Action['Skill'][401] = [100, 'percent', 1, nil, false, false]
  HP_Sacrifice_Action['Skill'][117] = [100, 'percent', 10, nil, false, false]
  HP_Sacrifice_Action['Skill'][404] = [100, 'integer', 500, nil, true, false]
  HP_Sacrifice_Action['Skill'][407] = [100, 'percent', 75, nil, false, false]
  HP_Sacrifice_Action['Skill'][104] = [100, 'percent', 10, nil, false, false]
  HP_Sacrifice_Action['Skill'][408] = [100, 'percent', 100, 111, true, true]
  HP_Sacrifice_Action['Skill'][409] = [100, 'percent', 100, 111, true, true]
  end

#==============================================================================
# ** Don't modify anything below this point.
#==============================================================================


#==============================================================================
# ** Scene_Battle
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  include LockeZ
 
  #--------------------------------------------------------------------------
  # These 2 methods makes the script compatible with the default battle system.
  #--------------------------------------------------------------------------
  unless respond_to?('step4_part3')
    alias make_skill_action_result_hp_sacrifice make_skill_action_result
    def make_skill_action_result
      make_skill_action_result_hp_sacrifice
      action = @active_battler.current_action
      if action != nil and HP_Sacrifice_Action['Skill'].include?(action.skill_id)
        sacrifice_hp(@active_battler, HP_Sacrifice_Action['Skill'][action.skill_id].dup)
      end
    end
 
    alias make_item_action_result_hp_sacrifice make_item_action_result
    def make_item_action_result
      make_item_action_result_hp_sacrifice
      action = @active_battler.current_action
      if action != nil and HP_Sacrifice_Action['Item'].include?(action.item_id)
        sacrifice_hp(@active_battler, HP_Sacrifice_Action['Item'][action.item_id].dup)
      end
    end
  end
 
  #--------------------------------------------------------------------------
  # Sacrifice HP
  #     battler : active battler
  #     action  : action
  #--------------------------------------------------------------------------
  def sacrifice_hp(battler, action)
    if action[0] >= rand(100)
      if action[1] == 'integer'
        hp_sacrificed = action[2].to_i
      elsif action[1] == 'percent'
        hp_sacrificed = (battler.maxhp * action[2] / 100).to_i
      end
      if action[5] == false
        hp_sacrificed = [battler.hp - 1, hp_sacrificed].min
      end
      if hp_sacrificed != 0
        # This line happens only in the DBS
        # Atoa's CBS does this automatically for all damage
        battler.hp -= hp_sacrificed

        battler.damage = hp_sacrificed
        battler.animation_id = action[3].nil? ? 0 : action[3]
        battler.damage_pop = action[4]
        @status_window.refresh
      end
    end
  end
end


The problem I detected is that you can cast the skills regardless of the HP you currently have. For example, the very first skill in the script consume 5% of the character's current HP and yet it can still be used if the character only have 1 HP, as long as you haven't configured the skill to kill the caster if it eats more HP than they have. This also happens if you've set up an ability that uses a flat number.
Bury with my...money!

Blizzard

You can try using Tons of Add-ons. It has game_guy's HP Skill in there which does the same thing as this script and it doesn't have that issue.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Simon Greedwell

Sadly, Tons of Addons is incompatible with some of the dozen or so scripts I'm currently using.

Other similar scripts, such as DerWulfman's, lack what this script offers: the possibility of setting the HP to be consumed by the ability as a percentage of the caster's Max Hp instead of an integer.
Bury with my...money!

Blizzard

Ah, I see. Well, adding that code shouldn't be much work as only an alias for Game_Actor#skill_can_use? needs to be added.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

KK20

March 02, 2016, 02:36:44 am #4 Last Edit: March 02, 2016, 02:39:08 am by KK20
Something like this

class Game_Battler
 
  alias skill_can_use_hp_consuming_skills_later? skill_can_use?
  def skill_can_use?(id)
    skill = HP_Sacrifice_Action['Skill'][id]
    # If can kill self when using (or if no skill configured), then skip to alias
    unless skill.nil? || skill[5]
      # If percent usage
      if skill[1] == 'percent'
        return false if self.hp < self.maxhp * skill[2] / 100
      else # Integer usage
        return false if self.hp < skill[2]
      end
    end
    return skill_can_use_hp_consuming_skills_later?(id)
  end
 
end

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Simon Greedwell

On the script, where should that code go?
Bury with my...money!

KK20

Literally anywhere as long as it's below Game_Battler.

Trust me, if you put the script in a wrong place (or really any script for that matter), you'll know immediately it didn't work.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Simon Greedwell

I placed the fix below the HP Sacrifice script and this happened as soon as a battle start:



So I placed the fix within the HP Sacrifice script and this error popped out:

Bury with my...money!

KK20

Oh, I didn't see everything was inside a module.



class Game_Battler
 
  alias skill_can_use_hp_consuming_skills_later? skill_can_use?
  def skill_can_use?(id)
    skill = LockeZ::HP_Sacrifice_Action['Skill'][id]
    # If can kill self when using (or if no skill configured), then skip to alias
    unless skill.nil? || skill[5]
      # If percent usage
      if skill[1] == 'percent'
        return false if self.hp < self.maxhp * skill[2] / 100
      else # Integer usage
        return false if self.hp < skill[2]
      end
    end
    return skill_can_use_hp_consuming_skills_later?(id)
  end
 
end

That should address the first error.

The second error is due to improper placement. You were putting a class within a class when they should be separated from each other.

# Should be like this
class Scene_Battle
...
end

class Game_Battler
...
end

# Should NOT be like this
class Scene_Battle
  class Game_Battler
  end
end

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Simon Greedwell

That seemed to do the trick.

A thousand thanks.  :)
Bury with my...money!