Jump to content
Trvc

Hit Processing Options

Recommended Posts

TRVC: HIT PROCESSING OPTIONS

 

I put together two scripts to add more flexibility to the default hit processing methods for my own project.

 

Figured I would share them to see if they are useful to anyone:

 

The first allows the user to specify their own formulas for every part of the hit processing procedure.

 

Custom formulas for Evade, Magic Evade, Counter Attacks, Magic Reflect, Hit Rate and Crit Chance.

 

 

The second script allows for the user to specify (via skill note tags) modified hit processing for that skill

 

Evade, Counter Chance, Hit Chance, Crit Chance and Reflect Chance can either be set to a value or modified by an arithmetic operator (or disabled completely)

 

Feel free to use these scripts if your own projects (commercial or free) Under these conditions

     1) The script header must be included with the script

     2) You must give credit to me (Trvc) somewhere in the project

 

Other then that you are free to use or modify either script as you see fit.

 

Hit Processing Formulas:

 

 

=begin
==============================================================================
================================= Preamble ===================================
==============================================================================
Name:         Custom Hit Processing Formulas (CHPF)

Author:       Trevyn (Trvc)

Purpose:      Allows the use of custom formulas in place of the default
              Hit processing formulas.

Change Log:
    5/22/14:  1.0 Initial release
    
Terms of Use:
    1) Include this header along with the script
    2) Give credit to Trvc somewhere in your project
  Otherwise you are free to use and or modify this script for any commercial
  or non-commercial project.
==============================================================================    
================================= compatibility: =============================
==============================================================================
  overwriten methods
    game battler:
        item_hit
        item_eva
        item_cri
        item_mrf
        item_cnt
        
  If you use this script with my "Hit Processing By Skill" script then place
  this script above it.
  
  If used with other scripts that modify hit processing it is probably a good
  idea to place this sript above them
==============================================================================
================================= Instructions: ==============================
==============================================================================
Modify the formulas below to your liking.
      For your conveniance I have included the fallowing values that can be used
      of the formulas
           a = attacker
           b = defender
           v[x] = variable x
           s[x] = switch x
           p[x] = party member x
           actors[x] = actor x
           
    
=end
module TRVC
  module CHPF
    # ========================================================================
    # ========================== Customization: ==============================
    # ========================================================================
    # Modifies how evade chance is calculated
    CUSTOM_EVADE_FORMULA   = "b.eva - (a.hit > 1 ? a.hit - 1 : 0)"
    # If hit is above 100% then decrease evade by the amount of hit over
    # 100%
    
    # Modifies how crit chance is calculated
    CUSTOM_CRIT_FORMULA    = "a.cri - b.cev + (a.hit > 1 ? (a.hit - 1) / 10 : 0)"
    # If hit is above 100% then increase crit by 1% for each 10 percent extra
    # hit
    
    # Modifies how counter chance is claculated
    CUSTOM_COUNTER_FORMULA = "b.cnt - (a.hit > 1.1 ? a.hit - 1.1 : 0)"
    # if hit is above 110% then reduce counter chance by 1% for each extra hit
    
    # Modifies how reflect chance is calculated
    CUSTOM_REFLECT_FORMULA = "b.mrf"
    # Default Engine Calculation
    
    # Modifies how magic evasion is calculated
    CUSTOM_M_EVADE_FORMULA = "b.mev"
    # Default Engine Calculation
    
    # Modifies how hit rate is calculatd
    CUSTOM_HIT_FORMULA     = "a.hit"
    # Default Engine Calculation
    
    # Adds a calculation for magic hit rate use as you will
    CUSTOM_M_HIT_FORMULA   = "1.0"
    # Default Engine Calculation
    
    
    # =======================================================================
    # ========================== End Customization: =========================
    # =======================================================================
  end
end

class Game_Battler < Game_BattlerBase

  #--------------------------------------------------------------------------
  # overwrit method item_cnt
  #--------------------------------------------------------------------------
  def item_cnt(user, item)
    return 0 unless item.physical?
    return trvc_evaluate(user,item, TRVC::CHPF::CUSTOM_COUNTER_FORMULA)
  end
  #--------------------------------------------------------------------------
  # overwrite method item_mrf
  #--------------------------------------------------------------------------
  def item_mrf(user, item)
    return 0 unless item.magical?
    return trvc_evaluate(user, item, TRVC::CHPF::CUSTOM_REFLECT_FORMULA)
  end
  #--------------------------------------------------------------------------
  # overwrite method item_eva
  #--------------------------------------------------------------------------
  def item_eva(user, item)
    return trvc_evaluate(user, item, TRVC::CHPF::CUSTOM_EVADE_FORMULA) if item.physical?
    return trvc_evaluate(user, item, TRVC::CHPF::CUSTOM_M_EVADE_FORMULA) if item.magical?
    return 0
  end
  #--------------------------------------------------------------------------
  # overwrite method item_cri
  #--------------------------------------------------------------------------
  def item_cri(user, item)
    return 0 unless item.damage.critical
    return trvc_evaluate(user, item, TRVC::CHPF::CUSTOM_CRIT_FORMULA)
  end
  #--------------------------------------------------------------------------
  # overwrite method item_hit
  #--------------------------------------------------------------------------
  def item_hit(user, item)
    rate = item.success_rate * 0.01
    rate *= trvc_evaluate(user, item, TRVC::CHPF::CUSTOM_HIT_FORMULA) if item.physical? 
    rate *= trvc_evaluate(user, item, TRVC::CHPF::CUSTOM_M_HIT_FORMULA) if item.magical? 

    return rate
  end
  #--------------------------------------------------------------------------
  # new method: chpf_evaluate
  #--------------------------------------------------------------------------
  def trvc_evaluate(user, item, formula)
    a = user
    b = self
    v = $game_variables
    p = $game_party
    actors = $game_actors
    result = eval(formula)
    return result unless result.nil?
    return 0
  end
  
end
    
    

 

 

 

Hit Processing By Skill

 

 

=begin
===============================================================================
====================== Preamble ===============================================
===============================================================================
Name:       Skill Specific Hit Processing (sshp)
Author:     Trevyn (Trvc)

ChangeLog:
    5/21/14 Version 1.0 (initial release)

Purpose:    Allows setting or modification of Hit, Evade, Counter, Reflect or
            Crit rates via skill notetags
 
Terms of Use:
    1) Include this header along with the script
    2) Give credit to Trvc somewhere in your project
  Otherwise you are free to use and or modify this script for any commercial
  or non-commercial project.            
===============================================================================
====================== Compatibility ==========================================
===============================================================================
Compatibility:
  Overwrites Methods:
          none
          
  Alias methods:
      Game_Battler:
          item_cnt
          item_eva
          item_mrf
          item_cri
          item_hit
If used with my "Hit processing Formulas" Script then place this sript below it
          
===============================================================================          
===================== Instructions: ===========================================
===============================================================================
General:
    As always place this script below Materials and above Main.

    
Notetags:
   Use these notetags for either usable items or skills
   
<set counter: x> sets counter chance to x

<mod counter: x y> modifies counter chance using operation x and
                   value y

<no counter>

<set evade: x>
<mod evade: x y> or
<no evade>

<set reflect: x>
<mod reflect: x y>
<no reflect>

<set hit: x>
<mod hit: x y>

<set crit: x>
<mod crit: x y>
      
          
       
===============================================================================
==================== Examples: ================================================
===============================================================================
  <set evade: 0.3> skill has a static 30% chance to be dodged
  <no counter> skill can not be countered
  <mod reflect: - 0.2> skill has a 20% less chance to be reflected
    etc
=end
module TRVC
  module SSHP
    module REGEX
      MOD_COUNTER = /<mod[ \-_]?counter:\s*([\+\/\-\*])\s*(\d*\.?\d+)\s*>/i
      SET_COUNTER = /<set[ \-_]?counter:\s*(\d*\.?\d+)\s*>/i
      NO_COUNTER = /<no[ \-_]?counter>/i
      MOD_EVADE = /<mod[ \-_]?evade:\s*([\+\/\-\*])\s*(\d*\.?\d+)\s*>/i
      SET_EVADE = /<set[ \-_]?evade:\s*(\d*\.?\d*)\s*>/i
      NO_EVADE = /<no[ \-_]?evade>/i
      MOD_REFLECT = /<mod[ \-_]?reflect:\s*([\+\/\-\*])\s*(\d*\.?\d+)\s*>/i
      SET_REFLECT = /<set[ \-_]?reflect:\s*(\d*\.?\d+)\s*>/i
      NO_REFLECT = /<no[ \-_]?reflect>/i
      MOD_HIT = /<mod[ \-_]?hit:\s*([\+\/\-\*])\s*(\d*\.?\d+)\s*>/i
      SET_HIT = /<set[ \-_]?hit:\s*(\d*\.?\d+)\s*>/i
      MOD_CRIT = /<mod[ \-_]?crit:\s*([\+\/\-\*])\s*(\d*\.?\d+)\s*>/i
      SET_CRIT = /<set[ \-_]?crit:\s*(\d*\.?\d+)\s*>/i
    end
  end
end


class RPG::UsableItem
  # ===========================================================================
  #  new method counter_rate 
  # ===========================================================================
  def counter_rate
    return @sshp_counter_rate unless @sshp.nil?
    load_sshp_notetags
    return @sshp_counter_rate
  end
  # ===========================================================================
  #  new method counter_mod_type
  # ===========================================================================
  def counter_mod_type
    return @sshp_counter_type unless @sshp.nil?
    load_sshp_notetags
    return @sshp_counter_type
  end
  # ===========================================================================
  #  new method evade_rate 
  # ===========================================================================
  def evade_rate
    return @sshp_evade_rate unless @sshp.nil?
    load_sshp_notetags
    return @sshp_evade_rate
  end
  # ===========================================================================
  #  new method evade_mod_type
  # ===========================================================================
  def evade_mod_type
    return @sshp_evade_type unless @sshp.nil?
    load_sshp_notetags
    return @sshp_evade_type
  end
  # ===========================================================================
  #  new method reflect_rate 
  # ===========================================================================
  def reflect_rate
    return @sshp_reflect_rate unless @sshp.nil?
    load_sshp_notetags
    return @sshp_reflect_rate
  end
  # ===========================================================================
  #  new method reflect_mod_type
  # ===========================================================================
  def reflect_mod_type
    return @sshp_reflect_type unless @sshp.nil?
    load_sshp_notetags
    return @sshp_reflect_type
  end
  # ===========================================================================
  #  new method hit_rate 
  # ===========================================================================
  def hit_rate
    return @sshp_hit_rate unless @sshp.nil?
    load_sshp_notetags
    return @sshp_hit_rate
  end
  # ===========================================================================
  #  new method hit_mod_type
  # ===========================================================================
  def hit_mod_type
    return @sshp_hit_type unless @sshp.nil?
    load_sshp_notetags
    return @sshp_hit_type
  end
  # ===========================================================================
  #  new method crit_rate 
  # ===========================================================================
  def crit_rate
    return @sshp_crit_rate unless @sshp.nil?
    load_sshp_notetags
    return @sshp_crit_rate
  end
  # ===========================================================================
  #  new method crit_mod_type
  # ===========================================================================
  def crit_mod_type
    return @sshp_crit_type unless @sshp.nil?
    load_sshp_notetags
    return @sshp_crit_type
  end
  # ===========================================================================
  #  new method load_sshp_notetags
  # ===========================================================================
  def load_sshp_notetags
    load_sshp_counter_notetags
    load_sshp_evade_notetags
    load_sshp_reflect_notetags
    load_sshp_crit_notetags
    load_sshp_hit_notetags
    @sshp = true
  end
  # ===========================================================================
  #  new method load_sshp_counter_notetags
  # ===========================================================================
  def load_sshp_counter_notetags
    if self.note.match(TRVC::SSHP::REGEX::NO_COUNTER)
      @sshp_counter_type = "="
      @sshp_counter_rate = 0
    elsif self.note.match(TRVC::SSHP::REGEX::MOD_COUNTER)
      @sshp_counter_type = $1
      @sshp_counter_rate = $2.to_f
    elsif self.note.match(TRVC::SSHP::REGEX::SET_COUNTER)
      @sshp_counter_rate = $1.to_f
      @sshp_counter_type = "="
    end
  end
  # ===========================================================================
  #  new method load_sshp_evade_notetags
  # ===========================================================================
  def load_sshp_evade_notetags
    if self.note.match(TRVC::SSHP::REGEX::NO_EVADE)
      @sshp_evade_type = "="
      @sshp_evade_rate = 0
    elsif self.note.match(TRVC::SSHP::REGEX::MOD_EVADE)
      @sshp_evade_type = $1
      @sshp_evade_rate = $2.to_f
    elsif self.note.match(TRVC::SSHP::REGEX::SET_EVADE)
      @sshp_evade_type = "="
      @sshp_evade_rate = $1.to_f
    end
  end
  # ===========================================================================
  #  new method load_sshp_reflect_notetags
  # ===========================================================================
  def load_sshp_reflect_notetags
    if self.note.match(TRVC::SSHP::REGEX::NO_REFLECT)
      @sshp_reflect_type = "="
      @sshp_reflect_rate = 0
    elsif self.note.match(TRVC::SSHP::REGEX::MOD_REFLECT)
      @sshp_reflect_type = $1
      @sshp_reflect_rate = $2.to_f
    elsif self.note.match(TRVC::SSHP::REGEX::SET_REFLECT)
      @sshp_reflect_type = "="
      @sshp_reflect_rate = $1.to_f
    end
  end
  # ===========================================================================
  #  new method load_sshp_hit_notetags
  # ===========================================================================
  def load_sshp_hit_notetags
    if self.note.match(TRVC::SSHP::REGEX::MOD_HIT)
      @sshp_hit_type = $1
      @sshp_hit_rate = $2.to_f
    elsif self.note.match(TRVC::SSHP::REGEX::SET_HIT)
      @sshp_hit_type = "="
      @sshp_hit_rate = $1.to_f
    end
  end
  # ===========================================================================
  #  new method load_sshp_crit_notetags
  # ===========================================================================
  def load_sshp_crit_notetags
    if self.note.match(TRVC::SSHP::REGEX::MOD_CRIT)
      @sshp_crit_type = $1
      @sshp_crit_rate = $2.to_f
    elsif self.note.match(TRVC::SSHP::REGEX::SET_CRIT)
      @sshp_crit_type = "="
      @sshp_crit_rate = $1.to_f
    end
  end
end

  
    


class Game_Battler < Game_BattlerBase
  #--------------------------------------------------------------------------
  # alias method item_cnt
  #--------------------------------------------------------------------------
  alias sshp_item_cnt item_cnt
  def item_cnt(user, item)
    case item.counter_mod_type
    when "+" 
      return (sshp_item_cnt(user, item) + item.counter_rate)
    when "*"
      return (sshp_item_cnt(user, item) * item.counter_rate)
    when "/"
      return (sshp_item_cnt(user, item) / item.counter_rate)
    when "-" 
      return (sshp_item_cnt(user, item) - item.counter_rate)
    when "="
      return (item.counter_rate)
    when nil 
    end
    return sshp_item_cnt(user, item)
  end
  #--------------------------------------------------------------------------
  # alias method item_mrf
  #--------------------------------------------------------------------------
  alias sshp_item_mrf item_mrf
  def item_mrf(user, item)
    case item.reflect_mod_type
    when "+" 
      return (sshp_item_mrf(user, item) + item.reflect_rate)
    when "*" 
      return (sshp_item_mrf(user, item) * item.reflect_rate)
    when "/" 
      return (sshp_item_mrf(user, item) / item.reflect_rate)
    when "-" 
      return (sshp_item_mrf(user, item) - item.reflect_rate)
    when "=" 
      return (item.reflect_rate)
    when nil
    end 
    return sshp_item_mrf(user, item)
  end
  #--------------------------------------------------------------------------
  # alias method item_eva
  #--------------------------------------------------------------------------
  alias sshp_item_eva item_eva
  def item_eva(user, item)
    case item.evade_mod_type
    when "+" 
      return (sshp_item_eva(user, item) + item.evade_rate)
    when "*" 
      return (sshp_item_eva(user, item) * item.evade_rate)
    when "/" 
      return (sshp_item_eva(user, item) / item.evade_rate)
    when "-" 
      return (sshp_item_eva(user, item) - item.evade_rate)
    when "=" 
      return (item.evade_rate)
    when nil 
    end
    return sshp_item_eva(user, item)
  end
  #--------------------------------------------------------------------------
  # alias method item_cri
  #--------------------------------------------------------------------------
  alias sshp_item_cri item_cri
  def item_cri(user, item)
    case item.crit_mod_type
    when "+" 
      return (sshp_item_cri(user, item) + item.crit_rate)
    when "*" 
      return (sshp_item_cri(user, item) * item.crit_rate)
    when "/" 
      return (sshp_item_eva(user, item) / item.crit_rate)
    when "-" 
      return (sshp_item_eva(user, item) - item.crit_rate)
    when "=" 
      return (item.crit_rate)
    when nil 
    end
    return sshp_item_cri(user, item)
  end
  #--------------------------------------------------------------------------
  # alias method item_hit
  #--------------------------------------------------------------------------
  alias sshp_item_hit item_hit
  def item_hit(user, item)
    case item.hit_mod_type
    when "+" 
      return (sshp_item_hit(user, item) + item.hit_rate)
    when "*" 
      return (sshp_item_hit(user, item) * item.hit_rate)
    when "/" 
      return (sshp_item_hit(user, item) / item.hit_rate)
    when "-" 
      return (sshp_item_hit(user, item) - item.hit_rate)
    when "=" 
      return (item.hit_rate)
    when nil 
    end
    return sshp_item_hit(user, item)
  end
end

 

 

 

If you do find any bugs let me know. and I'll see about squashing them :)

 

Also If you find either script useful I would appreciate a reply.

 

EDIT updated the instructions on script 2

Edited by Trvc

Share this post


Link to post
Share on other sites

Nice.

 

Just to ask, would it now be possible to use a d20/D&D type hit processing where you miss if you don't exceed a target number using a random variable and triggering a crit if you manage to roll the maximum number?  (Thus simulating rolling 20 on d20 to trigger a critical)

Share this post


Link to post
Share on other sites

something like this would do it :)

CUSTOM_HIT_FORMULA     = "@dice_temp = rand(20) + 1
                              return (@dice_temp + a.atk) > b.def ? 1 : 0"

CUSTOM_CRIT_FORMULA    = "(@dice_temp + 20 * a.cri) >= 20 ? 1 : 0"

Then simply set Cri to 5% for a 19-20 crit or 10% for a 18-20 crit etc

 

EDIT: Sorry typo... Fixed

Edited by Trvc

Share this post


Link to post
Share on other sites

Hello, can I use this script to make certain attacks have a lower hit rate if the user has certain state? for example, most magical attacks in my project will tend to fail if the caster has the "unfocus" state (id 24)? if I can make it, could you explain me how to make it, please?

Share this post


Link to post
Share on other sites

Hello, can I use this script to make certain attacks have a lower hit rate if the user has certain state? for example, most magical attacks in my project will tend to fail if the caster has the "unfocus" state (id 24)? if I can make it, could you explain me how to make it, please?

This can be done with just the first script

# the form:
# "condition ? x : y" can be used to implement this
# where condition is any valid ruby condition 
# x is the chance if the condition is true (0 = certain miss, 1 = certain hit)
# y is the chance if the condition is false

# so to cause state 24 to cause magic attacks to have a chance to fail the code would be as follows

CUSTOM_M_HIT_FORMULA   = "a.state?(24) ? 0.8 : 1"
 # makes any skill marked as a "Magic Attack" have a 80% chance to miss when afflicted with
 # state 24. otherwise it will have 100%



I will probably create a new script at some point that combines both of the ones above with some new features. 

Including the ability to specify any of the formulas above by skill type or by skill notetag. and maybe the ability to add formula's to state note tags...

 

But that is a project for some other time

Edited by Trvc

Share this post


Link to post
Share on other sites

 

Hello, can I use this script to make certain attacks have a lower hit rate if the user has certain state? for example, most magical attacks in my project will tend to fail if the caster has the "unfocus" state (id 24)? if I can make it, could you explain me how to make it, please?

This can be done with just the first script

# the form:
# "condition ? x : y" can be used to implement this
# where condition is any valid ruby condition 
# x is the chance if the condition is true (0 = certain miss, 1 = certain hit)
# y is the chance if the condition is false

# so to cause state 24 to cause magic attacks to have a chance to fail the code would be as follows

CUSTOM_M_HIT_FORMULA   = "a.state?(24) ? 0.8 : 1"
 # makes any skill marked as a "Magic Attack" have a 80% chance to miss when afflicted with
 # state 24. otherwise it will have 100%



I will probably create a new script at some point that combines both of the ones above with some new features. 

Including the ability to specify any of the formulas above by skill type or by skill notetag. and maybe the ability to add formula's to state note tags...

 

But that is a project for some other time

 

 

Esplendid, this worked! you have no idea how long I was looking for this feature, I REALLY wanted to make a bind equivalente state for magical attacks, thanks a lot! :D

Share this post


Link to post
Share on other sites

This will help all of those people who keep asking about making a dungeon and dragons or pathfinder type combat system, as it looks like you can achieve a close approximation with this script and careful use of formula.

 

Well done!

Share this post


Link to post
Share on other sites

It's me again lol, I had another doubt

 

What if I want to make this:

 

If state 24 is applied, the magic presition will be of 20% (as was the last time I asked) but if it has another state (let's say state 60) it'll be of 40%? I guess it'd be something like:

 

CUSTOM_M_HIT_FORMULA = "a.state?(24) ? 0.2 : (a.state?(40) ? 04 : 1)"

 

But not sure at all

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
Top ArrowTop Arrow Highlighted