Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: Simon Greedwell on March 01, 2016, 01:19:48 pm

Title: [Resolved]Help with a HP Sacrifice script.
Post by: Simon Greedwell on March 01, 2016, 01:19:48 pm
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.
Title: Re: Help with a HP Sacrifice script.
Post by: Blizzard on March 01, 2016, 04:56:43 pm
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.
Title: Re: Help with a HP Sacrifice script.
Post by: Simon Greedwell on March 01, 2016, 05:46:41 pm
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.
Title: Re: Help with a HP Sacrifice script.
Post by: Blizzard on March 02, 2016, 01:43:28 am
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.
Title: Re: Help with a HP Sacrifice script.
Post by: KK20 on March 02, 2016, 02:36:44 am
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
Title: Re: Help with a HP Sacrifice script.
Post by: Simon Greedwell on March 02, 2016, 07:07:37 am
On the script, where should that code go?
Title: Re: Help with a HP Sacrifice script.
Post by: KK20 on March 02, 2016, 12:02:06 pm
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.
Title: Re: Help with a HP Sacrifice script.
Post by: Simon Greedwell on March 02, 2016, 01:13:44 pm
I placed the fix below the HP Sacrifice script and this happened as soon as a battle start:

(http://i.imgur.com/zFKWtQV.png)

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

(http://i.imgur.com/55CBYQw.png)
Title: Re: Help with a HP Sacrifice script.
Post by: KK20 on March 02, 2016, 01:49:40 pm
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
Title: Re: Help with a HP Sacrifice script.
Post by: Simon Greedwell on March 02, 2016, 02:16:13 pm
That seemed to do the trick.

A thousand thanks.  :)