#==============================================================================
# Dynamic skill rates
#------------------------------------------------------------------------------
# Author: Timtrack
# date: 11/01/2026
#==============================================================================
$imported ||= {}
$imported["TIM-DynamicSkillRates"] = true
#==============================================================================
# Description: allows skills and items to have their rates defined by notetags
# formulas. The rates that can be modified are the hit rate, the dodge rate,
# the crit rate, the counterattack and magic reflection rates
# (never used the last two though)
# You can set in your skill/item the notetag:
# <dynamic X: formula>
# - X is the rated stat (either cnt, mrf, hit, eva or cri)
# - formula, much like damage formula is evaluated with arguments:
# a -> the user of the skill/item
# b -> the target of it
# self -> the skill/item
# v -> the game variables, v[i] being variable i
# default -> the value of the rate if there was no formula
#
# The result of the formula should be a value between
# 0 (0% chance) and 1 (100% chance)
#
# Examples:
# <dynamic hit: 0.1>
# <dynamic cri: default * 2>
# <dynamic eva: 0>
#==============================================================================
# Installation: probably best to put it as low as possible in your load order
#==============================================================================
# Term of use: free for commercial and non-commercial games, if you give credit
#==============================================================================
module TIM
module DynamicSkillRates
REGEXP = /<dynamic\s+(cnt|mrf|hit|eva|cri):\s+(.+)\s*>/i
#list of modified rates (refered to with item_x methods in Game_Battler:
LIST = [:cnt, :mrf, :hit, :eva, :cri]
end #DynamicSkillRates
end #TIM
#==============================================================================
# DataManager
#==============================================================================
module DataManager
#--------------------------------------------------------------------------
# alias method: load_database
#--------------------------------------------------------------------------
class <<self; alias load_database_tim_dsr load_database; end
def self.load_database
load_database_tim_dsr
load_notetags_tim_dsr
end
#--------------------------------------------------------------------------
# new method: load_notetags_tim_dsr
#--------------------------------------------------------------------------
def self.load_notetags_tim_dsr
[$data_skills, $data_items].each{ |g| g.each {|o| o.load_dsr if o} }
end
end #DataManager
#==============================================================================
# RPG::UsableItem
#==============================================================================
class RPG::UsableItem < RPG::BaseItem
#--------------------------------------------------------------------------
# new method: load_dsr
#--------------------------------------------------------------------------
def load_dsr
@dsr_formulas = {}
self.note.split(/[\r\n]+/).each do |line|
if line.match(TIM::DynamicSkillRates::REGEXP)
@dsr_formulas[$1.to_sym] = $2
end
end
end
#--------------------------------------------------------------------------
# new method: dsr_formula?
#--------------------------------------------------------------------------
def dsr_formula?(rate)
@dsr_formulas[rate]
end
#--------------------------------------------------------------------------
# new method: eval_dsr
#--------------------------------------------------------------------------
def eval_dsr(rate, a, b, default, v)
eval(@dsr_formulas[rate])
end
end #RPG::UsableItem
#==============================================================================
# Game_Battler
#==============================================================================
class Game_Battler < Game_BattlerBase
#--------------------------------------------------------------------------
# alias methods: item_cnt, item_mrf, item_hit, item_eva, item_cri
#--------------------------------------------------------------------------
TIM::DynamicSkillRates::LIST.each do |rate|
#method names: item_x and tim_dsr_item_x with x a rate in LIST
method_symb = "item_#{rate}".to_sym
aliased_symb = "tim_dsr_#{method_symb}".to_sym
alias_method(aliased_symb, method_symb)
define_method(method_symb) do |user, item|
default = send(aliased_symb, user, item)
if item.dsr_formula?(rate)
item.eval_dsr(rate, user, self, default, $game_variables)
else
default
end
end #item_rate
end
end #Game_Battler