#==============================================================================
# ** TDS Weapon Unleash
#    Ver: 1.0
#------------------------------------------------------------------------------
#  * Description:
#  A Script that replicates the "Weapon Unleash" system from the game series
#  "Golden sun".
#------------------------------------------------------------------------------
#  * Features: 
#  Gives weapon a chance to "Unleash" a pre set skill during battle. 
#------------------------------------------------------------------------------
#  * Instructions:
#  To use the unleash effect add the following to a weapon's note box.
#
#  UNLEASH: Skill_ID %_Chance
#
#  Skill_ID = The ID of the skill used for the weapon's unleash effect
#  %_Chance = Chance of the unleash effect activating from 0-100%
#
#  Example:
#
#  UNLEASH: 23 100
#------------------------------------------------------------------------------
#  * Notes:
#    None.
#------------------------------------------------------------------------------
#  * New Methods:
#  RPG::Weapon Module
#  - unleash_info
#    ^ Method used to read from the weapon's notebox and get unleash information.
#
#  Game_Battler:
#  - weapon_unleash?
#    ^ Method used to determine if unleash effect is possible and set unleash
#      skill as an action.
#
#  Scene_Battle:
#  - execute_weapon_unleash
#    ^ Method used to execute unleash process.
#------------------------------------------------------------------------------
#  * Aliased Methods:
#  Scene_Battle:
#  - execute_action
#    ^ Aliased to check for weapon unleash effect before original method runs.
#------------------------------------------------------------------------------
# WARNING:
#
# Do not release, distribute or change my work without my expressed written 
# consent, doing so violates the terms of use of this work.
#
# If you really want to share my work please just post a link to the original
# site.
#
# * Not Knowing English or understanding these terms will not excuse you in any
#   way from the consequenses.
#==============================================================================

#==============================================================================
# ** Game_Battler
#------------------------------------------------------------------------------
#  This class deals with battlers. It's used as a superclass of the Game_Actor
# and Game_Enemy classes.
#==============================================================================

class Game_Battler
  #--------------------------------------------------------------------------
  # * Determine and set Weapon Unleash
  #--------------------------------------------------------------------------
  def weapon_unleash?
    # If self is not an actor ir action is not to attack
    return false if !actor? or !@action.attack?
    # Get Main Weapon
    weapon = weapons[0]
    # Return false if main weapon is nil
    return false if weapon == nil  
    # Get Unleash Information
    unleash_info = weapon.unleash_info
    # Return false if unleash info is nil
    return false if unleash_info == nil
    # If Unleash Rate is more than a random number from 0-100%
    if (rand(100) < unleash_info[1])
      # Set Weapon Unleash Skill
      @action.set_skill(unleash_info[0])
      # Return true
      return true  
    end
    # Return false by default
    return false
  end
end

#==============================================================================
# ** Scene_Battle
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle < Scene_Base
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------  
  alias tds_weapon_unleash_scene_battle_execute_action execute_action unless $@
  #--------------------------------------------------------------------------
  # * Execute Battle Actions
  #--------------------------------------------------------------------------
  def execute_action
    # If Active Battler weapon unleash
    if @active_battler.weapon_unleash?
      # Execute Weapon Unleash
      execute_weapon_unleash
      return
    end    
    # Run Original Method
    tds_weapon_unleash_scene_battle_execute_action    
  end
  #--------------------------------------------------------------------------
  # * Execute Battle Action: Weapon Unleash
  #--------------------------------------------------------------------------
  def execute_weapon_unleash    
    # Get Skill
    skill = @active_battler.action.skill
    # Weapon Unleash Text
    text = sprintf("%s's %s let's out a howl! %s!", @active_battler.name,
    @active_battler.weapons[0].name, skill.name)
    # Add Instant Text
    @message_window.add_instant_text(text)
    # Wait 20 frames
    wait(20)
    # Make Action Targets
    targets = @active_battler.action.make_targets
    # Display Animation
    display_animation(targets, skill.animation_id)
    # Run skill common event if it exist
    $game_temp.common_event_id = skill.common_event_id
    # Go Through Targets
    for target in targets
      # Execute Skill Effect
      target.skill_effect(@active_battler, skill)
      # Display Action Effects
      display_action_effects(target, skill)
    end
  end
end

#==============================================================================
# ** RPG Module
#------------------------------------------------------------------------------
# Module for handling information.
#==============================================================================

module RPG 
  #============================================================================
  # ** RPG::Weapon
  #----------------------------------------------------------------------------
  # This module handles Weapon Information
  #============================================================================
  class Weapon < BaseItem
    #-------------------------------------------------------------------------
    # * Get Weapon Unleash Information
    #-------------------------------------------------------------------------
    def unleash_info
      # Check notes for Unleash Skill ID and Chance
      self.note[/UNLEASH: ([-0-9]+) ([-0-9]+)/]      
      # Retunr nil first match is nil
      return nil if $1 == nil 
      # Return Array containing unleash values
      return [$1.to_i, $2.to_i]
    end
  end 
end