% Damage Calculator for Skills [resolved]

What i want is to have different kind of skills in my game, like in Phantasy Star 4.
1- Magic: just like the default

2- Skills: These ones can be used a certain # of times depending on the level and the most important thing is that the damage they do is a % of the damage done by normal attacks

lets say in the database I add "%120" to the name, this information is not shown in the game but that skill does 120% of the damage dealt with normal attacks. or i add "%100" to the name and make it deal damage in all enemies meaning it would do the same damage as in normal attacks but damaging everyone. The damage would depend on the equipment so if with a beginner sword 100% is 200 damage with an advanced sword 100% damage would be 1000

these skills dont use MP they have a number of times of usage until you go to an inn and every 4 levels another time is added to the skill. But just having the damage calculator is fine
  1. class Game_Battler
  2.  
  3.   def skill_hit?(user, skill)
  4.     if ( [3,4].include?(skill.scope) and self.hp == 0) or
  5.       ( [5,6].include?(skill.scope) and self.hp >= 1)
  6.       return false
  7.     end
  8.     # else
  9.     return true
  10.   end
  11.  
  12.   def attack_hit?(attacker)
  13.     hit_result = (rand(100) < attacker.hit)
  14.     return hit_result
  15.   end
  16.  
  17.   def damage_correct(attacker, damage)
  18.     # critical
  19.     if rand(100) < 4 * attacker.dex / self.agi
  20.       damage *= 2
  21.       self.critical = true
  22.     end
  23.     # Guard
  24.     damage /= 2 if self.guarding?
  25.     return damage
  26.   end
  27.  
  28.  
  29.   def attack_self_damage(attacker)
  30.     self.critical = false
  31.     # basic damage
  32.     atk = [attacker.atk - self.pdef / 2, 0].max
  33.     damage = atk * (20 + attacker.str) / 20
  34.     # Element correction
  35.     damage *= elements_correct(attacker.element_set)
  36.     damage /= 100
  37.     if (damage > 0)
  38.       damage = damage_correct(attacker, damage)
  39.     end
  40.     # Dispersion
  41.     if damage.abs > 0
  42.       amp = [damage.abs * 15 / 100, 1].max
  43.       damage += rand(amp+1) + rand(amp+1) - amp
  44.     end
  45.     return damage
  46.   end
  47.  
  48.   def skill_effect(user, skill)
  49.     data = skill.name.split(/%/)
  50.     skill_rate = data[1].nil? ? 100 : data[1].to_i
  51.     hit_result = attack_hit?(user)
  52.     # If hit occurs
  53.     if hit_result == true
  54.       damage = attack_self_damage(user)
  55.       self.damage = damage * skill_rate / 100
  56.       # State Removed by Shock
  57.       remove_states_shock
  58.       # Substract damage from HP
  59.       self.hp -= self.damage
  60.       # State change
  61.       @state_changed = false
  62.       states_plus(user.plus_state_set)
  63.       states_minus(user.minus_state_set)
  64.     # When missing
  65.     else
  66.       # Set damage to "Miss"
  67.       self.damage = "Miss"
  68.       # Clear critical flag
  69.       self.critical = false
  70.     end
  71.    
  72.   end
  73.  
  74. end
  75.  
  76.  
  77. class Game_Actor < Game_Battler
  78.   attr_accessor  :left_skills
  79.  
  80.   alias actor_init initialize
  81.   def initialize(actor_id)
  82.     actor_init(actor_id)
  83.     @left_skills = Hash.new
  84.     n = max_skill_uses
  85.     @skills.each { |id| @left_skills[id] = n }
  86.   end
  87.  
  88.   def max_skill_uses
  89.     return [@level/4, 1].max
  90.   end
  91.  
  92.   #--------------------------------------------------------------------------
  93.   # * Recover All
  94.   #--------------------------------------------------------------------------
  95.   def recover_all
  96.     @hp = maxhp
  97.     @sp = maxsp
  98.     for i in @states.clone
  99.       remove_state(i)
  100.     end
  101.     @skills.each { |id| @left_skills[id] = max_skill_uses }
  102.   end
  103.  
  104.   def skill_can_use?(skill_id)
  105.     if @left_skills[skill_id] <= 0
  106.       return false
  107.     end
  108.     # Unusable if incapacitated
  109.     if dead?
  110.       return false
  111.     end
  112.     # If silent, only physical skills can be used
  113.     if $data_skills[skill_id].atk_f == 0 and self.restriction == 1
  114.       return false
  115.     end
  116.     # Get usable time
  117.     occasion = $data_skills[skill_id].occasion
  118.     # If in battle
  119.     if $game_temp.in_battle
  120.       # Usable with [Normal] and [Only Battle]
  121.       return (occasion == 0 or occasion == 1)
  122.     # If not in battle
  123.     else
  124.       # Usable with [Normal] and [Only Menu]
  125.       return (occasion == 0 or occasion == 2)
  126.     end
  127.   end
  128.  
  129. end
  130.  
  131. class Window_Skill < Window_Selectable
  132.   #--------------------------------------------------------------------------
  133.   # * Draw Item
  134.   #     index : item number
  135.   #--------------------------------------------------------------------------
  136.   def draw_item(index)
  137.     skill = @data[index]
  138.     if @actor.skill_can_use?(skill.id)
  139.       self.contents.font.color = normal_color
  140.     else
  141.       self.contents.font.color = disabled_color
  142.     end
  143.     x = 4 + index % 2 * (288 + 32)
  144.     y = index / 2 * 32
  145.     rect = Rect.new(x, y, self.width / @column_max - 32, 32)
  146.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  147.     bitmap = RPG::Cache.icon(skill.icon_name)
  148.     opacity = self.contents.font.color == normal_color ? 255 : 128
  149.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  150.     # remove the % from the name:
  151.     nameParts = skill.name.split(/%/)
  152.     self.contents.draw_text(x + 28, y, 204, 32, nameParts[0], 0)
  153.     #self.contents.draw_text(x + 232, y, 48, 32, skill.sp_cost.to_s, 2)
  154.     max = @actor.max_skill_uses
  155.     left = @actor.left_skills[skill.id]
  156.     self.contents.draw_text(x + 232, y, 48, 32, "#{left}/#{max}", 2)
  157.   end
  158.  
  159. end
  160.  
  161.  
  162. class Scene_Battle
  163.   #--------------------------------------------------------------------------
  164.   # * Make Skill Action Results
  165.   #--------------------------------------------------------------------------
  166.   def make_skill_action_result
  167.     # Get skill
  168.     @skill = $data_skills[@active_battler.current_action.skill_id]
  169.     # If not a forcing action
  170.     unless @active_battler.current_action.forcing
  171.       # If unable to use due to SP running out
  172.       unless @active_battler.skill_can_use?(@skill.id)
  173.         # Clear battler being forced into action
  174.         $game_temp.forcing_battler = nil
  175.         # Shift to step 1
  176.         @phase4_step = 1
  177.         return
  178.       end
  179.     end
  180.     # ------------------------------------------- #
  181.     # Use up SP -- disabled
  182.     #@active_battler.sp -= @skill.sp_cost
  183.     # Use up 'skill times' instead
  184.     if @active_battler.is_a?(Game_Actor)
  185.       @active_battler.left_skills[@skill.id] -= 1
  186.     end
  187.     # ------------------------------------------- #
  188.     # Refresh status window
  189.     @status_window.refresh
  190.     # Show skill name on help window
  191.     @help_window.set_text(@skill.name, 1)
  192.     # Set animation ID
  193.     @animation1_id = @skill.animation1_id
  194.     @animation2_id = @skill.animation2_id
  195.     # Set command event ID
  196.     @common_event_id = @skill.common_event_id
  197.     # Set target battlers
  198.     set_target_battlers(@skill.scope)
  199.     # Apply skill effect
  200.     for target in @target_battlers
  201.       target.skill_effect(@active_battler, @skill)
  202.     end
  203.   end
  204.  
  205.   def make_skill_action_result
  206.     # Get skill
  207.     @skill = $data_skills[@active_battler.current_action.skill_id]
  208.     # If not a forcing action
  209.     unless @active_battler.current_action.forcing
  210.       # If unable to use due to SP running out
  211.       unless @active_battler.skill_can_use?(@skill.id)
  212.         # Clear battler being forced into action
  213.         $game_temp.forcing_battler = nil
  214.         # Shift to step 1
  215.         @phase4_step = 1
  216.         return
  217.       end
  218.     end
  219.     # Use up SP
  220.     @active_battler.sp -= @skill.sp_cost
  221.     # Refresh status window
  222.     @status_window.refresh
  223.     # -------------------------------------- #
  224.     name_parts = @skill.name.split(/%/)
  225.     # Show skill name on help window
  226.     @help_window.set_text(name_parts[0], 1)
  227.     # -------------------------------------- #
  228.     # Set animation ID
  229.     @animation1_id = @skill.animation1_id
  230.     @animation2_id = @skill.animation2_id
  231.     # Set command event ID
  232.     @common_event_id = @skill.common_event_id
  233.     # Set target battlers
  234.     set_target_battlers(@skill.scope)
  235.     # Apply skill effect
  236.     for target in @target_battlers
  237.       target.skill_effect(@active_battler, @skill)
  238.     end
  239.   end
  240.  
  241. end
Expand to see the code.


You didn't give much detail, hopefully this is what you meant.

- if you forget to add %x to the skill name, it does 100% damage.
- all skills have the same # of uses: level/4.
- each skill has a separate number. a level 4 actor with x skills can use each skill once, rather than any skill 4x times.
- skill menu shows the number of skills you have left (1/3 etc).
- Recover All will heal the # of skill uses too.
Weapons already effect the damage dealt by skills, so I didn't have to change that.
The thing is that when there is a change of weapon the skill ends up doing less damage so what i want is that those skills marked in a way are directly calculated by normal attacks if a normal attack does 500dmg the skill with 100% does 500dmg and with 120% does 600dmg because theyre supposed to be skills and not magic.

so basically what im saying is that there are 2 kind of skills

Magic: Done with MP and infinite if there is mp to cast them
Skills: Damage based on the normal attacks and limited by #

the 2 of them are available so i want to mark the skills by atribute "skill"

if marked with attribute "skill": it is limited and 1 is added every 4 lvls

If not its a normal Spell and it uses mp like regular spells


now about the number of uses of the "Skills" i want the count to start on the level learnt so if the skill is leant in level 10 it has only 1 use until lvl 14

And thank you very much for helping me :grin:
I don't get what you mean with the weapons.
In rmxp, Attack does damage based on ATK. Equipping a weapon changes actor's states, including ATK. So weapons effect both Attack damage and Skill damage, (or non- if your weapon doesn't raise ATK). You shouldn't have a problem, as long as your non-magical skills are physical (100% Atk-f) .
  1.  
  2. class RPG::Skill
  3.  
  4.   Non_Magic_Skill = 17        # element ID (of the 'skill' element)
  5.  
  6.   def non_magic_skill?
  7.     return @element_set.include?(Non_Magic_Skill)
  8.   end
  9.  
  10.   def fixed_name
  11.     nameParts = @name.split(/%/)
  12.     return nameParts[0]
  13.   end
  14.  
  15.   def get_rate_from_name
  16.     data = @name.split(/%/)
  17.     skill_rate = data[1].nil? ? 100 : data[1].to_i
  18.     return skill_rate
  19.   end
  20.  
  21. end
  22.  
  23.  
  24. class Game_Battler
  25.  
  26.   def skill_hit?(user, skill)
  27.     if ( [3,4].include?(skill.scope) and self.hp == 0) or
  28.       ( [5,6].include?(skill.scope) and self.hp >= 1)
  29.       return false
  30.     end
  31.     # else
  32.     return true
  33.   end
  34.  
  35.   def damage_correct(attacker, damage)
  36.     # critical
  37.     if rand(100) < 4 * attacker.dex / self.agi
  38.       damage *= 2
  39.       self.critical = true
  40.     end
  41.     # Guard
  42.     damage /= 2 if self.guarding?
  43.     return damage
  44.   end
  45.  
  46.  
  47.   def attack_self_damage(attacker)
  48.     self.critical = false
  49.     # basic damage
  50.     atk = [attacker.atk - self.pdef / 2, 0].max
  51.     damage = atk * (20 + attacker.str) / 20
  52.     # Element correction
  53.     damage *= elements_correct(attacker.element_set)
  54.     damage /= 100
  55.     if (damage > 0)
  56.       damage = damage_correct(attacker, damage)
  57.     end
  58.     # Dispersion
  59.     if damage.abs > 0
  60.       amp = [damage.abs * 15 / 100, 1].max
  61.       damage += rand(amp+1) + rand(amp+1) - amp
  62.     end
  63.     return damage
  64.   end
  65.  
  66.   def non_magic_skill_effect(user, skill)
  67.     # Clear critical flag
  68.     self.critical = false
  69.     return false unless skill_hit?(user, skill)
  70.     # Clear effective flag
  71.     effective = false
  72.     # Set effective flag if common ID is effective
  73.     effective |= skill.common_event_id > 0
  74.     skill_rate = skill.get_rate_from_name
  75.     #hit_result = attack_hit?(user)
  76.     # First hit detection
  77.     hit = skill.hit
  78.     if skill.atk_f > 0
  79.       hit *= user.hit / 100
  80.     end
  81.     hit_result = (rand(100) < hit)
  82.     # Set effective flag if skill is uncertain
  83.     effective |= hit < 100
  84.     # If hit occurs
  85.     if hit_result == true
  86.       damage = attack_self_damage(user)
  87.       self.damage = damage * skill_rate / 100
  88.       # Element correction
  89.       self.damage *= elements_correct(skill.element_set)
  90.       self.damage /= 100
  91.       # State Removed by Shock
  92.       remove_states_shock
  93.       # Substract damage from HP
  94.       self.hp -= self.damage
  95.       # State change
  96.       @state_changed = false
  97.       effective |= states_plus(skill.plus_state_set)
  98.       effective |= states_minus(skill.minus_state_set)
  99.     # When missing
  100.     else
  101.       # Set damage to "Miss"
  102.       self.damage = "Miss"
  103.     end
  104.     # If not in battle
  105.     unless $game_temp.in_battle
  106.       # Set damage to nil
  107.       self.damage = nil
  108.     end
  109.     # End Method
  110.     return effective
  111.   end
  112.  
  113.   # new
  114.   alias default_skill_effect skill_effect
  115.  
  116.   def skill_effect(user, skill)
  117.     if skill.non_magic_skill?
  118.       non_magic_skill_effect(user, skill)
  119.     else
  120.       default_skill_effect(user, skill)
  121.     end
  122.   end
  123.  
  124. end
  125.  
  126.  
  127. class Game_Actor < Game_Battler
  128.   attr_accessor  :left_skills
  129.  
  130.   alias actor_init initialize
  131.   def initialize(actor_id)
  132.     #actor_init(actor_id)
  133.     @left_skills = Hash.new
  134.     @skill_level_learnt = Hash.new
  135.     actor_init(actor_id)
  136.     # fix level_learnt
  137.     for i in 1..@level
  138.       for j in $data_classes[@class_id].learnings
  139.         if j.level == i
  140.           #learn_skill(j.skill_id)
  141.           @skill_level_learnt[j.skill_id] = i
  142.         end
  143.       end
  144.     end
  145.     update_left_skills
  146.   end
  147.  
  148.   def max_skill_uses(id)
  149.     lev_learnt = @skill_level_learnt[id]
  150.     max = (@level-lev_learnt)/4
  151.     max += 1
  152.     return max
  153.   end
  154.  
  155.   def learn_skill(skill_id)
  156.     if skill_id > 0 and not skill_learn?(skill_id)
  157.       @skills.push(skill_id)
  158.       @skills.sort!
  159.     end
  160.     @left_skills[skill_id] = 1
  161.     @skill_level_learnt[skill_id] = @level
  162.   end
  163.  
  164.   def update_left_skills
  165.     # update the number of skill uses. will return uses to max.
  166.     for id in @skills
  167.       lev_learnt = @skill_level_learnt[id]
  168.       @left_skills[id] = 1+((@level-lev_learnt)/4)
  169.     end  
  170.  
  171.   end
  172.  
  173.   #--------------------------------------------------------------------------
  174.   # * Change Level
  175.   #     level : new level
  176.   #--------------------------------------------------------------------------
  177.   def level=(level)
  178.     # Check up and down limits
  179.     level = [[level, $data_actors[@actor_id].final_level].min, 1].max
  180.     # Change EXP
  181.     self.exp = @exp_list[level]
  182.     # edit: update skill uses to fit the new level.
  183.     update_left_skills
  184.   end
  185.  
  186.   # new
  187.   #--------------------------------------------------------------------------
  188.   # * Change EXP
  189.   #     exp : new EXP
  190.   #--------------------------------------------------------------------------
  191.   def exp=(exp)
  192.     @exp = [[exp, 9999999].min, 0].max
  193.     # Level up
  194.     while @exp >= @exp_list[@level+1] and @exp_list[@level+1] > 0
  195.       @level += 1
  196.       # Learn skill
  197.       for j in $data_classes[@class_id].learnings
  198.         if j.level == @level
  199.           learn_skill(j.skill_id)
  200.         end
  201.       end
  202.       update_left_skills          # edit
  203.     end
  204.     # Level down
  205.     while @exp < @exp_list[@level]
  206.       @level -= 1
  207.       update_left_skills          # edit
  208.     end
  209.     # Correction if exceeding current max HP and max SP
  210.     @hp = [@hp, self.maxhp].min
  211.     @sp = [@sp, self.maxsp].min
  212.    
  213.   end
  214.  
  215.   #--------------------------------------------------------------------------
  216.   # * Recover All
  217.   #--------------------------------------------------------------------------
  218.   def recover_all
  219.     @hp = maxhp
  220.     @sp = maxsp
  221.     for i in @states.clone
  222.       remove_state(i)
  223.     end
  224.     @skills.each { |id| @left_skills[id] = max_skill_uses(id) }
  225.   end
  226.  
  227.   def skill_can_use?(skill_id)
  228.     # --------------- edit -------------- #
  229.     not_magical = $data_skills[skill_id].non_magic_skill?
  230.     if not_magical
  231.       # If there's not enough 'uses', the skill cannot be used.
  232.       if @left_skills[skill_id] <= 0
  233.         return false
  234.       end
  235.     else # skills use SP
  236.       # If there's not enough SP, the skill cannot be used.
  237.       if $data_skills[skill_id].sp_cost > self.sp
  238.         return false
  239.       end
  240.     end
  241.     # ----------------------------------- #
  242.     if not skill_learn?(skill_id)
  243.       return false
  244.     end
  245.     # Unusable if incapacitated
  246.     if dead?
  247.       return false
  248.     end
  249.     # If silent, only physical skills can be used
  250.     if $data_skills[skill_id].atk_f == 0 and self.restriction == 1
  251.       return false
  252.     end
  253.     # Get usable time
  254.     occasion = $data_skills[skill_id].occasion
  255.     # If in battle
  256.     if $game_temp.in_battle
  257.       # Usable with [Normal] and [Only Battle]
  258.       return (occasion == 0 or occasion == 1)
  259.     # If not in battle
  260.     else
  261.       # Usable with [Normal] and [Only Menu]
  262.       return (occasion == 0 or occasion == 2)
  263.     end
  264.   end
  265.  
  266. end
  267.  
  268. class Window_Skill < Window_Selectable
  269.   #--------------------------------------------------------------------------
  270.   # * Draw Item
  271.   #     index : item number
  272.   #--------------------------------------------------------------------------
  273.   def draw_item(index)
  274.     skill = @data[index]
  275.     if @actor.skill_can_use?(skill.id)
  276.       self.contents.font.color = normal_color
  277.     else
  278.       self.contents.font.color = disabled_color
  279.     end
  280.     x = 4 + index % 2 * (288 + 32)
  281.     y = index / 2 * 32
  282.     rect = Rect.new(x, y, self.width / @column_max - 32, 32)
  283.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  284.     bitmap = RPG::Cache.icon(skill.icon_name)
  285.     opacity = self.contents.font.color == normal_color ? 255 : 128
  286.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  287.     # edit:
  288.     if skill.non_magic_skill?
  289.       # use skill.fixed_name, so the %x isn't shown on screen.
  290.       # replace the sp cost with: left/max uses
  291.       self.contents.draw_text(x + 28, y, 204, 32, skill.fixed_name, 0)
  292.       max = @actor.max_skill_uses(skill.id)
  293.       left = @actor.left_skills[skill.id]
  294.       self.contents.draw_text(x + 232, y, 48, 32, "#{left}/#{max}", 2)
  295.     else
  296.       self.contents.draw_text(x + 28, y, 204, 32, skill.name, 0)
  297.       self.contents.draw_text(x + 232, y, 48, 32, skill.sp_cost.to_s, 2)  
  298.     end
  299.    
  300.   end
  301.  
  302. end
  303.  
  304.  
  305. class Scene_Battle
  306.   #--------------------------------------------------------------------------
  307.   # * Make Skill Action Results
  308.   #--------------------------------------------------------------------------
  309.   def make_skill_action_result
  310.     # Get skill
  311.     @skill = $data_skills[@active_battler.current_action.skill_id]
  312.     # If not a forcing action
  313.     unless @active_battler.current_action.forcing
  314.       # If unable to use due to SP running out
  315.       unless @active_battler.skill_can_use?(@skill.id)
  316.         # Clear battler being forced into action
  317.         $game_temp.forcing_battler = nil
  318.         # Shift to step 1
  319.         @phase4_step = 1
  320.         return
  321.       end
  322.     end
  323.     # ----------------- edit ------------------- #
  324.     unless @skill.non_magic_skill?
  325.       # Use up SP
  326.       @active_battler.sp -= @skill.sp_cost
  327.     end
  328.     # Use up 'skill times' instead
  329.     if @active_battler.is_a?(Game_Actor) and @skill.non_magic_skill?
  330.       @active_battler.left_skills[@skill.id] -= 1
  331.     end
  332.     # ------------------------------------------- #
  333.     # Refresh status window
  334.     @status_window.refresh
  335.     # Show skill name on help window
  336.     # edit: use skill.fixed_name
  337.     @help_window.set_text(@skill.fixed_name, 1)
  338.     # Set animation ID
  339.     @animation1_id = @skill.animation1_id
  340.     @animation2_id = @skill.animation2_id
  341.     # Set command event ID
  342.     @common_event_id = @skill.common_event_id
  343.     # Set target battlers
  344.     set_target_battlers(@skill.scope)
  345.     # Apply skill effect
  346.     for target in @target_battlers
  347.       target.skill_effect(@active_battler, @skill)
  348.     end
  349.   end
  350.  
  351. end
Expand to see the code.
Oh !!! perfect i feel like a noob!! lol sorry its been awhile since the last time i actually used the maker lately i just sprite lol too much spriting work! and excelent job i feel really happy about this script.

now... can u make it work for Atoa custom battle system?

viewtopic.php?f=11&t=62914

or any sideview animated battle system

It doesnt cause any error it just doesnt work, all the skills are magic no matter what i do
I tried my script with this battle system and it works fine ?
You know you should set the number of the 'skill' element inside the script, right? it's on line 4: Non_Magic_Skill = 17
change the 17 to whatever is the number of your skill element, and make sure your non-magic-skills have this element in the database.
yup, thats what i did. I tested in a new project and worked and then in this demo and didnt work. Can u post upload a project with the demo and ur script working?
Im really thankful for ur help. My game will only have mogs scripts, that battle system and ur script. Ur script is the one giving the fun to my game.

I will test it this weekend. Im posting this from my cellphone. I only have computer on weeknds :p
I didn't do anything special to make it work..
maybe you placed my script in the wrong position.
It should be in a new page above Main and below anything else. Atoa's script has many pages below main, if you put scripts there they'll be ignored and won't do a thing.
ok. Ill work on that this weekend. Thanks for everything 😀
Ok Silver Wind your script works perfectly. I just tested it thanx for everything. credits go for u and u should deffinitely post this script. all the phantasy star 4 fans would love this script :grin: i will lock this post after your reply.
Glad I could help ^^
I'll post the script when I'll have the time.
Topic locked Thx Silver Wind 😀

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=71028