#==============================================================================
#  @> Spirit Link Tech ~ Karin's Soulkeeper, 2015
#------------------------------------------------------------------------------
#   v1.0.1 - May 21: Added "Cleave"
#   v1.0 - May 18 : Started & finished.
#------------------------------------------------------------------------------
#  * Description:
#    The way this works, if the target has the 'Spirit Link' state,
#    the damage will be spread out and divided amongst all of
#    the target's allies that also have the 'Spirit Link' state,
#    effectively reducing the damage that the target takes.
#
#    This script can also produce a related negative state that will deal
#    a portion of the damage the target took to its allies. This portion will
#    then be divided amongst the target's allies equally, thereby increasing
#    the total damage taken by the troop/party.
#
#    You may also opt to not divide the damage and deal it directly to the
#    target's allies by including the state id in the CLEAVES list.
#
#    This script allows for multiple 'Spirit Link' states, each with their
#    own share percentage rates. Declare the state id's and their corresponding
#    share rates below.
#------------------------------------------------------------------------------
#  * To Use:
#    Put this script below Materials and above Main.
#------------------------------------------------------------------------------
#  * Compatibility:
#    This script overwrites Game_Battler.execute_damage
#------------------------------------------------------------------------------
#  * Terms:
#    Free to use in any kind of project, with or without credit. I wouldn't
#    really mind. Though I'd appreciate it! (^w^)/
#    Just don't, you know, claim that you made this here script yourself,
#    Because that's just mean (._.)
#------------------------------------------------------------------------------
#  * "Fun" Fact:
#    I made this script after a freakish, half-metre-or-so long millipede started
#    charging at me all of a sudden while I was taking a walk. For reasons
#    unknown, that same millipede made me think of making a damage-distribution
#    skill for my game. Since this was impossible in the default setup, I made
#    this here script. That is great and all but honestly, it would have been
#    better if that millipede didn't show up... *cringes*
#==============================================================================

#CUSTOMIZATION OPTIONS
module KS
  module SpiritLink
  #--------------------------------------------------------------------------
  # Set the state id's that will be considered as 'Spirit Link' states
  #--------------------------------------------------------------------------
  # - These could be any state at all, really.
  #--------------------------------------------------------------------------
  STATES = [41, 42, 43]
  
  #--------------------------------------------------------------------------
  # Set the percentage of the incoming damage to be shared with the allies;
  #--------------------------------------------------------------------------
  # - Input numbers above 0 to avoid possible loss of hair and/or scapula.
  # - Rates here correspond directly to the state id's declared above
  #--------------------------------------------------------------------------
  # Inputting 1.0 will cause ALL of the damage to be shared equally with allies.
  #
  # Inputting another number, like say 0.8, only 80% of the damage will be
  # shared and split. The targeted will take 20% of the full blow plus
  # her share of the 80% that was split amongst allies. So if she had 3 other
  # linked allies, they would take 20% of the damage each, while she would take
  # 20% (un-split damage) + 20% (her share of the split damage).
  #
  # Inputting a number greater than 1.0 will turn 'Spirit Link' into a negative
  # state that inflicts extra damage to the target's allies. Example: inputting
  # a rate of 1.25 will cause 25% of the damage to be split amongst the target's
  # allies (the target itself will be excluded from this splitting).
  #--------------------------------------------------------------------------
  SHARE_RATE = [1.0, 0.6, 1.25]
  
  #--------------------------------------------------------------------------
  # Sets the 'Spirit Link' states that will 'Cleave'.
  #--------------------------------------------------------------------------
  # IMPORTANT: The id's you put here MUST also be in the STATES array above.
  #
  # NOTE: This ONLY applies to the 'Spirit Link's with SHARE_RATES greater
  #       than 1.0.
  #--------------------------------------------------------------------------
  # Adding the ID of a state here will cause the entirety of the damage to
  # be dealt to the target's allies, instead of distributing it amongst them.
  # The original target is still excluded from the cleave damage. It would
  # be too OP if the target wasn't, so...
  #
  # Example: If you have a SHARE_RATE of 1.25, 25% of the damage will be
  # dealt directly to each the target's allies.
  #--------------------------------------------------------------------------
  CLEAVES = [43]
  
  end
end
# END OF CUSTOMIZATION OPTIONS

#==============================================================================
# ** Modified Class: Game_Battler
#==============================================================================
class Game_Battler < Game_BattlerBase
  #--------------------------------------------------------------------------
  # * Overwrite: Damage Processing
  #--------------------------------------------------------------------------
  def execute_damage(user)
    on_damage(@result.hp_damage) if @result.hp_damage > 0
    self.mp -= @result.mp_damage
    user.hp += @result.hp_drain
    user.mp += @result.mp_drain
    estado = SpiritLink?

    if estado >= 0 && @result.hp_damage > 0
      damage = @result.hp_damage
      print "Main: #{damage}; "
      pasen = KS::SpiritLink::SHARE_RATE[KS::SpiritLink::STATES.index(estado)]
      linked = []
      for act in self.friends_unit.alive_members
        linked.push(act) if act.state?(estado)
      end
      
      if pasen <= 1 #spirit link (positive)
        share = damage * (1.0 - pasen)
        self.hp -= share
        damage -= share
        print "Shared: #{damage}; "
        damage /= linked.size
        print "Split: #{damage.to_i};\n"
        for act in linked
          act.hp = [(act.hp - damage).to_i, 0].max
          act.perform_collapse_effect if act.hp == 0
        end
        
      else #fatal bonds (negative)
        self.hp -= damage
        self.add_state(1) if self.hp == 0
        linked.delete(self)
        damage *= (pasen - 1.0)
        print "Shared: #{damage}; "
        damage /= [linked.size-1, 1].max unless KS::SpiritLink::CLEAVES.include(estado)
        print "Split: #{damage.to_i};\n"
        for act in linked
          act.hp = [(act.hp - damage).to_i, 0].max
          act.perform_collapse_effect if act.hp == 0
        end
      end
      
    else
      self.hp -= @result.hp_damage
    end
  end

  #--------------------------------------------------------------------------
  # * New: Self (target) has Spirit Link State?
  #--------------------------------------------------------------------------
  def SpiritLink?
    for id in KS::SpiritLink::STATES
      return id if self.state?(id)
    end
    return -1
  end
end