#==============================================================================
# 
# ▼ Craze's Script Asylum - Elemental Merger v1.01
# -- Last Updated: 2012.01.05
# -- Level: Normal, Hard
# -- Requires: n/a
# 
#==============================================================================

$imported = {} if $imported.nil?
$imported["CRZ-ElementalMerger"] = true

#==============================================================================
# ▼ Updates
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# 2012.01.06 - Minor update for compatability with any absorption script
# 2012.01.05 - Finished Script
# 2012.01.04 - Started Script
# 
#==============================================================================
# ▼ Introduction
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# The RM* series has always had a particularly awful method of determining
# damage done when multiple elements are applied - all it does is take the best
# rate and apply it, even if the other elements are nullified!
#
# With this script, elemental damage will be merged proportionally, so that
# using a Soldier's hypothetical attack skills will also use that Soldier's
# inherent elements (from, say, his Fire Spear or an Infuse Ice status effect)
# and apply them ALL against a foe. If a foe is weak to both Piercing and Fire,
# they'll take even more damage than by default - but they'll take blended damage
# if they take double Piercing and half Fire damage.
#
# If you don't get it, know this: just plugging this script in will make
# your elemental equipment much more useful.
# 
#==============================================================================
# ▼ Instructions
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# To install this script, open up your script editor and copy/paste this script
# to an open slot below ▼ Materials/素材 but above ▼ Main. Remember to save.
# 
# Edit the settings in the module below as you see fit.
# 
#==============================================================================
# ▼ Compatibility
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# This script is made strictly for RPG Maker VX Ace. It is highly unlikely that
# it will run with RPG Maker VX without adjusting.
# 
# This script works with YEA Battle Engine Add-On: Elemental Popups without
# any adjustment on either end. The most damaging element's color will be used.
# There are also no issues with YEA Element Absorb.
#
# Overwritten methods:
#   Game_Battler
#      item_element_rate
#
#==============================================================================

module CRZ
  module ELE_MERGE
    
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # - Weapon Element Settings -
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # Use these settings to dictate how the elements made inherent by actors,
    # classes, weapons, equipment, states blends with your skills. Anything
    # set to "true" below will add any inherent elements to that type of 
    # action (determined by "Hit Type" according to Esrever's translation)
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # Any action tagged as "Physical" will include user's attack elements
    PHYS_ATK_ELEMENTS = true
    # Any action tagged as "Magical" will include user's attack elements
    MAGIC_ATK_ELEMENTS = false
    # Any action tagged as "Guaranteed Hit" will include user's attack elements.
    # This is normally used for buffs and healing, but can be used for any
    # skill you want to completely penetrate all defenses.
    CERT_ATK_ELEMENTS = true
    
    # For some games, you might have elemental healing, or attack elements
    # might be inherited simply due to CERT_ATK_ELEMENTS being true.
    # If you do not want any healing ability to inherit elements, set to true.
    IGNORE_RECOVERY = true 
    
    
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # - Elemental Merging [Hard Mode] -
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # Pick the type of elemental merging or blending you wish for your game
    # to use. This will take all potential elements and, instead of using the
    # default mechanic of picking only the best rate, will merge the rates.
    # Ignore this section if you want this happen, but don't dare touch
    # the math.
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # 0: Default RMAce. Will find the best rate out of all potential elements
    # 1: Addititive. If results are 100%, 150%, and 80%, the total will be 130%
    #      Ex.  100 + (100 - 100) + (150 - 100) + (80 - 100) = 130%
    #      Ex.  100 + (0 - 100) + (150 - 100) = 50%
    # 2: Multiplicative. If results are 100%, 150%, and 80%, 
    #      the total will be 120%
    #      Ex.  100 * (100 / 100) * (150 / 100) * (80 / 100) = 120%
    #      Ex.  100 * (0 / 100) * (150 / 100) = 0% 
    # 3: Multiplicative Psuedo-Null. If results are 100%, 150%, and 80%, 
    #      the total will be 120%. Nulled (fully resisted) elements are
    #      used to divide overall damage done by 10 (RECOMMENDED)
    #      Ex.  100 * (100 / 100) * (150 / 100) * (80 / 100) = 120%
    #      Ex.  100 * ([0 + 10] / 100) * (150 / 100) = 15% (Default)
    MERGE_TYPE = 3
    
    # If you use MERGE_TYPE 3, nulled elements will divide overall damage done
    # by this amount. Default is 10, but examples for other amounts are given
    # below. Note that attacks with a single element, which is nulled, will
    # deal 0 damage.
    #      Ex.  150% / [10] = 15% (Default)
    #      Ex.  150% / [20] = 7.5% 
    #      Ex.  150% / [5] = 30% 
    #      Ex.  150% / [3] = 50% 
    PSUEDO_NULL_AMT = 10 
    
    # This applies to all elemental merge types. When the final rate of damage
    # is determined, it is checked against this percentage. If below this
    # constant, the damage becomes 0. A value from 10-20 is recommended.
    #      Ex.  100% *  10% = [10% is <= 10%] = 0% (Default)
    # If you do not wish to use this feature, set this constant to 0.
    AUTO_NULL_PER = 10
    
  end # ELE_MERGE
end # CRZ

#==============================================================================
# ▼ Editting anything past this point may potentially result in causing
# computer damage, incontinence, explosion of user's head, coma, death, and/or
# halitosis so edit at your own risk.
#==============================================================================


#==============================================================================
# ■ Game_Battler
#==============================================================================

class Game_Battler
  
  #--------------------------------------------------------------------------
  # overwrite method: item_element_rate
  #--------------------------------------------------------------------------
  def item_element_rate(user, item)
    return 1.0 if item.damage.recover? and CRZ::ELE_MERGE::IGNORE_RECOVERY
    array = []
    if !user.atk_elements.empty?
      array += user.atk_elements if item.physical? and CRZ::ELE_MERGE::PHYS_ATK_ELEMENTS
      array += user.atk_elements if item.magical? and CRZ::ELE_MERGE::MAGIC_ATK_ELEMENTS
      array += user.atk_elements if item.certain? and CRZ::ELE_MERGE::CERT_ATK_ELEMENTS
    end
      
    if item.damage.element_id >= 0
      array.push(item.damage.element_id)
    end
    
    return 1.0 if array.empty?
    
    case CRZ::ELE_MERGE::MERGE_TYPE
    when 1 # Additive
      rate = 1.0
      for i in array
        rate += element_rate(i) - 1.0
      end
    when 2 # Multiplicative
      rate = 1.0
      for i in array
        rate *= element_rate(i)
      end
    when 3 # Multiplicative Psuedo-Null
      rate = 1.0
      for i in array
        rate *= [element_rate(i),(CRZ::ELE_MERGE::PSUEDO_NULL_AMT / 100.0)].max
      end
    else # Default (0)
      rate = elements_max_rate(user.array)
    end
    rate = 0 if rate <= (CRZ::ELE_MERGE::AUTO_NULL_PER / 100.0) and rate > 0
    rate
  end
  
end # Game_Battler

#==============================================================================
# 
# ▼ End of File
# 
#==============================================================================