=begin #=============================================================================== Title: Troop Surprise Condition Author: Hime Date: Nov 10, 2013 -------------------------------------------------------------------------------- ** Change log Nov 10, 2013 - added support for encounter checking for all battle encounters Oct 10, 2013 - Initial release -------------------------------------------------------------------------------- ** Terms of Use * Free to use in non-commercial projects * Contact me for commercial use * No real support. The script is provided as-is * Will do bug fixes, but no compatibility patches * Features may be requested but no guarantees, especially if it is non-trivial * Credits to Hime Works in your project * Preserve this header -------------------------------------------------------------------------------- ** Description This script allows you to specify custom troop surprise conditions. The condition must evaluate to true or false, but you are not limited to using probability-based conditions. The condition is specified as a ruby formula, which allows you to create any kind of condition you want once you figure out how to write it. -------------------------------------------------------------------------------- ** Installation In the script editor, place this script below Materials and above Main -------------------------------------------------------------------------------- ** Usage In the first page of a troop event, create a comment of the form Where `formula` is a valid ruby statement that returns a number. Four variables are available in your formula p - current game party t - current troop v - game variables s - game switches In the configuration, there is a global surprise condition which you can set. It will be used if the troop doesn't have its own condition. #=============================================================================== =end $imported = {} if $imported.nil? $imported["TH_TroopSurpriseCondition"] = true #=============================================================================== # ** Configuration #=============================================================================== module TH module Troop_Surprise_Condition # Default surprise condition if none specified. # This is what the default project uses. It is evaluated in the context of # the BattleManager. Global_Condition = "rand < rate_surprise && !@preemptive" Regex = //i end end module RPG class Troop def surprise_condition parse_surprise_condition unless @surprise_condition return @surprise_condition end def parse_surprise_condition @surprise_condition = "" @pages[0].list.each do |cmd| if cmd.code == 108 && cmd.parameters[0] =~ TH::Troop_Surprise_Condition::Regex @surprise_condition = $1 break end end end end end module BattleManager class << self alias :th_troop_surprise_condition_setup :setup alias :th_troop_surprise_condition_on_encounter :on_encounter alias :th_troop_surprise_condition_battle_start :battle_start end #----------------------------------------------------------------------------- # The assumption here is the encounter is checked after setup. #----------------------------------------------------------------------------- def self.setup(*args) @encounter_checked = false th_troop_surprise_condition_setup(*args) end def self.on_encounter th_troop_surprise_condition_on_encounter evaluate_surprise_flag @encounter_checked = true end def self.evaluate_surprise_flag if $game_troop.surprise_condition.empty? @surprise = eval_surprise_condition($game_system.surprise_condition) else @surprise = eval_surprise_condition($game_troop.surprise_condition) end end def self.eval_surprise_condition(formula, p=$game_party, t=$game_troop, v=$game_variables, s=$game_switches) eval(formula) end def self.encounter_checked @encounter_checked end #----------------------------------------------------------------------------- # Run the encounter processing for events as well. Don't do it again if # it's already been checked #----------------------------------------------------------------------------- def self.battle_start on_encounter unless self.encounter_checked th_troop_surprise_condition_battle_start end end class Game_System attr_accessor :surprise_condition alias :th_troop_surprise_condition_intialize :initialize def initialize th_troop_surprise_condition_intialize @surprise_condition = TH::Troop_Surprise_Condition::Global_Condition end end class Game_Troop < Game_Unit def surprise_condition troop.surprise_condition end end