// Modified by Deva Winblood.
//::///////////////////////////////////////////////
//:: Planar Binding
//:: NW_S0_Planar.nss
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    Summons an outsider dependant on alignment, or
    holds an outsider if the creature fails a save.
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: April 12, 2001
//:://////////////////////////////////////////////
#include "NW_I0_SPELLS"

#include "x2_inc_spellhook"

void fnWatch(object oMaster)
{ // PURPOSE: For summone critter to know when to despawn
  effect eUnsum=EffectVisualEffect(VFX_FNF_SUMMON_MONSTER_1);
  object oMe=OBJECT_SELF;
  SetLocalInt(oMe,"bWatcher",TRUE);
  //SendMessageToPC(oMaster,"DEBUG: '"+GetName(oMe)+"' Watching");
  if (GetIsObjectValid(oMaster)==TRUE)
  { // valid master
    if (GetIsDead(oMaster)||GetCurrentAction(oMaster)==ACTION_REST||GetLocalInt(oMe,"bUnsummon"))
    { // despawn
      ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY,eUnsum,GetLocation(oMe),4.0);
      RemoveHenchman(oMaster,oMe);
      SetIsDestroyable(TRUE,FALSE,FALSE);
      DelayCommand(1.0,DestroyObject(oMe));
    } // despawn
    DelayCommand(6.0,fnWatch(oMaster));
  } // valid master
  else
  { // destroy self
    ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY,eUnsum,GetLocation(oMe),4.0);
    RemoveHenchman(oMaster,oMe);
    SetIsDestroyable(TRUE,FALSE,FALSE);
    DelayCommand(1.0,DestroyObject(oMe));
    DelayCommand(6.0,fnWatch(oMaster));
  } // destroy self
} // fnWatch()

void fnSetWatchers(object oMaster,float fDur)
{ // PURPOSE: Fire the watcher routines
  int nN=1;
  object oSum;
  oSum=GetAssociate(ASSOCIATE_TYPE_SUMMONED,oMaster,nN);
  while(GetIsObjectValid(oSum))
  { // check for watch
    //SendMessageToPC(oMaster,"DEBUG: Summoned '"+GetName(oSum)+"'");
    if (GetLocalInt(oSum,"bWatcher")!=TRUE)
    { // fire watch
      AssignCommand(oSum,fnWatch(oMaster));
      DelayCommand(fDur,SetLocalInt(oSum,"bUnsummon",TRUE));
    } // fire watch
    nN++;
    oSum=GetAssociate(ASSOCIATE_TYPE_SUMMONED,oMaster,nN);
  } // check for watch
} // fnSetWatchers()

void main()
{

/*
  Spellcast Hook Code
  Added 2003-06-23 by GeorgZ
  If you want to make changes to all spells,
  check x2_inc_spellhook.nss to find out more

*/

    if (!X2PreSpellCastCode())
    {
    // If code within the PreSpellCastHook (i.e. UMD) reports FALSE, do not run this spell
        return;
    }

// End of Spell Cast Hook


    //Declare major variables
    object oTarget = GetSpellTargetObject();
    int nMetaMagic = GetMetaMagicFeat();
    int nCasterLevel = GetCasterLevel(OBJECT_SELF);
    int nDuration = GetCasterLevel(OBJECT_SELF);
    effect eSummon;
    effect eGate;
    effect eDur = EffectVisualEffect(VFX_DUR_CESSATE_NEGATIVE);
    effect eDur2 = EffectVisualEffect(VFX_DUR_PARALYZED);
    effect eDur3 = EffectVisualEffect(VFX_DUR_PARALYZE_HOLD);
    effect eLink = EffectLinkEffects(eDur, EffectParalyze());
    eLink = EffectLinkEffects(eLink, eDur2);
    eLink = EffectLinkEffects(eLink, eDur3);

    int nRacial = GetRacialType(oTarget);
    int nAlign = GetAlignmentGoodEvil(OBJECT_SELF);
    if(nDuration == 0)
    {
        nDuration == 1;
    }
    //Check for metamagic extend
    if (nMetaMagic == METAMAGIC_EXTEND)
    {
        nDuration = nDuration *2;   //Duration is +100%
    }
    if (nDuration>24) nDuration=24;
    //Check to make sure a target was selected
    if (GetIsObjectValid(oTarget))
    {
        //Check the racial type of the target
        if(nRacial == RACIAL_TYPE_OUTSIDER)
        {
            if(!GetIsReactionTypeFriendly(oTarget))
            {
                //Fire cast spell at event for the specified target
                SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_PLANAR_BINDING));
                //Make a Will save
                if(!MySavingThrow(SAVING_THROW_WILL, oTarget, GetSpellSaveDC()+2))
                {
                    //Apply the linked effect
                    ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLink, oTarget, RoundsToSeconds(nDuration/2));
                }
            }
        }
    }
    else
    {
        //Set the summon effect based on the alignment of the caster
        float fDelay = 3.0;
        switch (nAlign)
        {
            case ALIGNMENT_EVIL:
                eSummon = EffectSummonCreature("sum6ce",VFX_FNF_SUMMON_GATE, fDelay);
                //eGate = EffectVisualEffect(VFX_FNF_SUMMON_GATE);
            break;
            case ALIGNMENT_GOOD:
                eSummon = EffectSummonCreature("sum5lg", VFX_FNF_SUMMON_CELESTIAL, fDelay);
                //eGate = EffectVisualEffect(219);
            break;
            case ALIGNMENT_NEUTRAL:
                eSummon = EffectSummonCreature("sumgreenslaad",VFX_FNF_SUMMON_MONSTER_3, 1.0);
                //eGate = EffectVisualEffect(VFX_FNF_SUMMON_MONSTER_3);
                //fDelay = 1.0;
            break;
        }
        //Apply the summon effect and VFX impact
        //ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eGate, GetSpellTargetLocation());
        ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eSummon, GetSpellTargetLocation(), HoursToSeconds(nDuration));
    }
    DelayCommand(3.0,fnSetWatchers(OBJECT_SELF,HoursToSeconds(nDuration)));
}