Critical and Damage scripts

Hi! I'm looking for a couple scripts:

1) I would like to be able to make the damage of one character to increase as his HP decreases.
2) I would like for an animation and sound effect to happen before critical hit damage is applied to the enemy.

For the second one, I found this, but I when I copy-paste the script, it is one continuous line, which seems to confuse RPG Maker.

Thanks ahead of time to anyone who answers.
1) damage is completely determined by the damage formula of the skill.
you should create personal skills for that actor, and use a.hp (user-HP) and a.mhp (user-MaxHP) in the damage formula the same way you're currently using a.atk or a.def etc.

2) years ago a forum update destroyed the line-breaks in code tags, and all older scripts have that problem.
A programmer needs to go through that text to restore the line breaks correctly - can be done, is just a bit of work. Sorry, I'm currently not up to it.
I tried to fix the fix the code the best I could. But I'm not exactly a scripter, so it might still not work properly. But maybe its useful for someone who wants something a little more tidy to start working.

Ruby:
module PC27
    module CrAn
# If this switch is not equal to 0, this switch can be used to turn the
# animation ON or OFF.
Switch = 0
# The two picture id's the script uses to draw the animation. Do not use
# them for other "Show Pictures" events.
ActorPicture = 2
BackPicture = 1
# Number of frames the image needs to slide in/out
SlideInTime = 20
SlideOutTime = 20
# Number of frames the image stays and distance it moves in that time
LingerTime = 60
LingerDistance = 100
# This is the sound played at the begin of the animation and the number
# of frames you want to wait before the picture shows up.
InitSound = "Absorb1"
InitWait = 30
# Those are actor specific sounds. It plays one random sound.
ActorSounds = {
# Actor_ID => [ "sound1", "sound2", ...],
1 => [ "Water4", "Water1", "Water3"],
2 => [ "Absorb1", "Fire1", "Fire2"],
# 3 => [...
}
    end
end

class Scene_Battle
   
    alias pc27_crit_effect_show_animation show_animation
    def show_animation(targets, animation_id)
        item = @subject.current_action.item
        if item.damage.none?
            targets.each { |target| target.set_crit_to_false}
        else
targets.each { |target| target.eval_crit(@subject, item)
        }
        end
        show_critical_animation if actor_crit?(targets)
        pc27_crit_effect_show_animation(targets, animation_id)
    end
       
    def show_critical_animation
        lo = PC27::CrAn::LingerDistance * 0.5 play_initial_sound show_crit_background
        show_crit_actor move_crit_actor(Graphics.width * 0.5 - lo, PC27::CrAn::SlideInTime)
        play_random_actor_sound(@subject)
        move_crit_actor(Graphics.width * 0.5 + lo, PC27::CrAn::LingerTime)
        move_crit_actor(Graphics.width + 50, PC27::CrAn::SlideOutTime)
        erease_crit_actor erease_crit_background
    end
   
    def play_initial_sound
        RPG::SE.new(PC27::CrAn::InitSound).play
        wait(PC27::CrAn::InitWait)
    end
   
    def play_random_actor_sound(subject)
        RPG::SE.new(PC27::CrAn::ActorSounds[subject.id].sample).play
    end
   
    def show_crit_actor
        name = @subject.name + "CritImg"
        y = Graphics.height * 0.5
        $game_troop.screen.pictures[PC27::CrAn::ActorPicture].show(name, 1, -50, y, 100, 100, 255, 0)
    end
   
    def move_crit_actor(x, time)
        y = Graphics.height * 0.5
        $game_troop.screen.pictures[PC27::CrAn::ActorPicture].move(1, x, y, 100, 100, 255, 0, time)
        wait(time)
    end
   
    def erease_crit_actor
        $game_troop.screen.pictures[PC27::CrAn::ActorPicture].erase
    end
   
    def show_crit_background
        x = Graphics.width * 0.5
        y = Graphics.height * 0.5
        $game_troop.screen.pictures[PC27::CrAn::BackPicture].show("CritBG", 1, x, y, 100, 100, 255, 0)
    end
   
    def erease_crit_background
        $game_troop.screen.pictures[PC27::CrAn::BackPicture].erase
    end
   
    def actor_crit?(targets)
        return false unless (PC27::CrAn::Switch == 0 || $game_switches[PC27::CrAn::Switch])
        return false unless @subject.is_a?(Game_Actor)
        targets.each do |target|
            return true if target.result.critical
        end
        return false
    end
end
   
class Game_Battler
   
    def eval_crit(user, item)
        @result.critical = (rand < item_cri(user, item))
    end
   
    def set_crit_to_false
        @result.critical = false
    end
   
    def item_apply(user, item)
        crit = @result.critical
        @result.clear
        @result.used = item_test(user, item)
        @result.missed = (@result.used && rand >= item_hit(user, item))
        @result.evaded = ([email protected] && rand < item_eva(user, item))
        if @result.hit?
            unless item.damage.none?
                @result.critical = crit make_damage_value(user, item)
                execute_damage(user)
            end
            item.effects.each {|effect| item_effect_apply(user, item, effect) }
            item_user_effect(user, item)
        end
    end

end
Thanks for the swift reply, Andar! That answer to 1 should help. The way I'm hoping to have it is that the character normally can't do that, but he learns it later in the game. So I could make copies of his powers and when he learns it later, his powers get replaced with the copies that have the -HP formula.

Muttsuhura! Thanks! I appreciate the time you must have spent going through that, so thanks a bunch! I'll give it a try in a bit.
I got this error:
1696531497591.png
First, always name the slot of a script aftert you place it - that name becomes part of the error message and is empty here, but it is important to identify the slot where the error is if there are multiple scripts without names.

Second, you might have messed up the copy&paste because in a correct copy that line is 46, not 50. Please check

Third - as the poster above said he didn't know if he got all breaks right. So if the added lines are just empty then you'll have to wait for someone else to crosscheck the script for wrong linebreaks.
I was on my work place so I couldn't test it before. That was not a good idea xd
Here's the code fixed. It didn't crashed in my project so I suppose it work properly. But I'm not sure what should it do so, try it out:

Ruby:
module PC27
    module CrAn
# If this switch is not equal to 0, this switch can be used to turn the
# animation ON or OFF.
Switch = 0
# The two picture id's the script uses to draw the animation. Do not use
# them for other "Show Pictures" events.
ActorPicture = 2
BackPicture = 1
# Number of frames the image needs to slide in/out
SlideInTime = 20
SlideOutTime = 20
# Number of frames the image stays and distance it moves in that time
LingerTime = 60
LingerDistance = 100
# This is the sound played at the begin of the animation and the number
# of frames you want to wait before the picture shows up.
InitSound = "Absorb1"
InitWait = 30
# Those are actor specific sounds. It plays one random sound.
ActorSounds = {
# Actor_ID => [ "sound1", "sound2", ...],
1 => [ "Water4", "Water1", "Water3"],
2 => [ "Absorb1", "Fire1", "Fire2"],
# 3 => [...
}
  end
end

class Scene_Battle
  
  alias pc27_crit_effect_show_animation show_animation
  def show_animation(targets, animation_id)
    item = @subject.current_action.item
    if item.damage.none?
      targets.each { |target| target.set_crit_to_false}
    else
    targets.each { |target| target.eval_crit(@subject, item)}
    end
      show_critical_animation if actor_crit?(targets)
      pc27_crit_effect_show_animation(targets, animation_id)
  end
      
  def show_critical_animation
    lo = PC27::CrAn::LingerDistance * 0.5
    play_initial_sound
    show_crit_background
    show_crit_actor move_crit_actor(Graphics.width * 0.5 - lo, PC27::CrAn::SlideInTime)
    play_random_actor_sound(@subject)
    move_crit_actor(Graphics.width * 0.5 + lo, PC27::CrAn::LingerTime)
    move_crit_actor(Graphics.width + 50, PC27::CrAn::SlideOutTime)
    erease_crit_actor erease_crit_background
  end
  
  def play_initial_sound
    RPG::SE.new(PC27::CrAn::InitSound).play
    wait(PC27::CrAn::InitWait)
  end
  
  def play_random_actor_sound(subject)
    RPG::SE.new(PC27::CrAn::ActorSounds[subject.id].sample).play
  end
  
  def show_crit_actor
    name = @subject.name + "CritImg"
    y = Graphics.height * 0.5
    $game_troop.screen.pictures[PC27::CrAn::ActorPicture].show(name, 1, -50, y, 100, 100, 255, 0)
  end
  
  def move_crit_actor(x, time)
    y = Graphics.height * 0.5
    $game_troop.screen.pictures[PC27::CrAn::ActorPicture].move(1, x, y, 100, 100, 255, 0, time)
    wait(time)
  end
  
  def erease_crit_actor
    $game_troop.screen.pictures[PC27::CrAn::ActorPicture].erase
  end
  
  def show_crit_background
    x = Graphics.width * 0.5
    y = Graphics.height * 0.5
    $game_troop.screen.pictures[PC27::CrAn::BackPicture].show("CritBG", 1, x, y, 100, 100, 255, 0)
  end
  
  def erease_crit_background
    $game_troop.screen.pictures[PC27::CrAn::BackPicture].erase
  end
  
  def actor_crit?(targets)
    return false unless (PC27::CrAn::Switch == 0 || $game_switches[PC27::CrAn::Switch])
    return false unless @subject.is_a?(Game_Actor)
    targets.each do |target|
    return true if target.result.critical
      end
    return false
  end
    
end
  
class Game_Battler
  
  def eval_crit(user, item)
    @result.critical = (rand < item_cri(user, item))
  end
  
  def set_crit_to_false
    @result.critical = false
  end
  
  def item_apply(user, item)
    crit = @result.critical
    @result.clear
    @result.used = item_test(user, item)
    @result.missed = (@result.used && rand >= item_hit(user, item))
    @result.evaded = ([email protected] && rand < item_eva(user, item))
    if @result.hit?
      unless item.damage.none?
      @result.critical = crit
      make_damage_value(user, item)
      execute_damage(user)
    end
    item.effects.each {|effect| item_effect_apply(user, item, effect) }
    item_user_effect(user, item)
    end
  end

end
Here you can get a formatted version that's clean and easy to read.

EDIT 1:

Plus, it has an option that allows you to choose between the default @result.critical setup in a specific method only or that original script's.
true stands for default, false for the custom script's setting.

EDIT 2:

I still missed a line but now it's fixed.

EDIT 3:

I forgot to mention it includes some spelling corrections as well.

Ruby:
module PC27
  module CrAn
    DEFAULT_CRITICAL_CALC = true
    # If this switch is not equal to 0, this switch can be used to turn the
    # animation ON or OFF.
    Switch = 0
    # The two picture id's the script uses to draw the animation. Do not use
    # them for other "Show Pictures" events.
    ActorPicture = 2
    BackPicture = 1
    # Number of frames the image needs to slide in/out
    SlideInTime = 20
    SlideOutTime = 20
    # Number of frames the image stays and distance it moves in that time
    LingerTime = 60
    LingerDistance = 100
    # This is the sound played at the begin of the animation and the number
    # of frames you want to wait before the picture shows up.
    InitSound = "Absorb1"
    InitWait = 30
    # Those are actor specific sounds. It plays one random sound.
    ActorSounds = {
      # Actor_ID => [ "sound1", "sound2", ...],
      1 => [ "Water4", "Water1", "Water3"],
      2 => [ "Absorb1", "Fire1", "Fire2"],
    # 3 => [...
    }
  end
end

class Scene_Battle
  include PC27::CrAn
  alias pc27_crit_effect_show_animation show_animation
  def show_animation(targets, animation_id)
    item = @subject.current_action.item
    if item.damage.none?
      targets.each { |target| target.set_crit_to_false }
    else
      targets.each { |target| target.eval_crit(@subject, item) }
    end
    show_critical_animation if actor_crit?(targets)
    pc27_crit_effect_show_animation(targets, animation_id)
  end
      
  def show_critical_animation
    lo = LingerDistance * 0.5
    play_initial_sound
    show_crit_background
    show_crit_actor
    move_crit_actor(Graphics.width * 0.5 - lo, SlideInTime)
    play_random_actor_sound(@subject)
    move_crit_actor(Graphics.width * 0.5 + lo, LingerTime)
    move_crit_actor(Graphics.width + 50, SlideOutTime)
    erase_crit_actor
    erase_crit_background
  end
  
  def play_initial_sound
    RPG::SE.new(InitSound).play
    wait(InitWait)
  end
  
  def play_random_actor_sound(subject)
    RPG::SE.new(ActorSounds[subject.id].sample).play
  end
  
  def show_crit_actor
    name = @subject.name + "CritImg"
    y = Graphics.height * 0.5
    pic = $game_troop.screen.pictures[ActorPicture]
    pic.show(name, 1, -50, y, 100, 100, 255, 0)
  end
  
  def move_crit_actor(x, time)
    y = Graphics.height * 0.5
    pic = $game_troop.screen.pictures[ActorPicture]
    pic.move(1, x, y, 100, 100, 255, 0, time)
    wait(time)
  end
  
  def erase_crit_actor
    $game_troop.screen.pictures[ActorPicture].erase
  end
  
  def show_crit_background
    x = Graphics.width * 0.5
    y = Graphics.height * 0.5
    pic = $game_troop.screen.pictures[BackPicture]
    pic.show("CritBG", 1, x, y, 100, 100, 255, 0)
  end
  
  def erase_crit_background
    $game_troop.screen.pictures[BackPicture].erase
  end
  
  def actor_crit?(targets)
    return false unless Switch == 0 || $game_switches[Switch]
    return false unless @subject.is_a?(Game_Actor)
    targets.any? {|target| target.result.critical }
  end
end
  
class Game_Battler
  def eval_crit(user, item)
    @result.critical = (rand < item_cri(user, item))
  end

  def set_crit_to_false
    @result.critical = false
  end

  def item_apply(user, item)
    default_crit = PC27::CrAn::DEFAULT_CRITICAL_CALC
    crit = @result.critical unless default_crit
    @result.success = false
    @result.used = item_test(user, item)
    @result.missed = (@result.used && rand >= item_hit(user, item))
    @result.evaded = ([email protected] && rand < item_eva(user, item))
    if @result.hit?
      unless item.damage.none?
        @result.critical = default_crit ? (rand < item_cri(user, item)) : crit
        make_damage_value(user, item)
        execute_damage(user)
      end
      item.effects.each {|effect| item_effect_apply(user, item, effect) }
      item_user_effect(user, item)
    end
  end
end
Wow!

OK so first off, thank you all! Everyone is very helpful and I really appreciated it. It is working perfectly. Problem solved.

Secondly, apologies: Andar was right, of course. I failed to copy the script right. (On top of that, I forgot that I made sounds for the other two characters in the team, thus adding two lines) I'll follow your advise going forward, Adar.
erease_crit_actor erease_crit_background
Line 52 should be two lines
otherwise all good except indenting. well done

Original thread: https://forums.rpgmakerweb.com/threads/critical-and-damage-scripts.161751/