// Modified by Deva Winblood.
//::///////////////////////////////////////////////
//:: x0_s2_fiend
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
Summons the 'fiendish' servant for the player.
This is a modified version of Planar Binding


At Level 5 the Blackguard gets a Succubus

At Level 9 the Blackguard will get a Vrock

Will remain for one hour per level of blackguard
*/
//:://////////////////////////////////////////////
//:: Created By: Brent
//:: Created On: April 2003
//:://////////////////////////////////////////////

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()
{
    int nLevel = GetLevelByClass(CLASS_TYPE_BLACKGUARD, OBJECT_SELF);
    effect eSummon;
    float fDelay = 3.0;
    int nDuration = nLevel;

    if (nLevel < 9)
    {
        eSummon = EffectSummonCreature("sum6ce",VFX_FNF_SUMMON_GATE, fDelay);
    }
    else if (nLevel < 15 )
    {
        eSummon = EffectSummonCreature("sum9ce", VFX_FNF_SUMMON_GATE, fDelay);
    }
    else
    {
       if (GetHasFeat(1003,OBJECT_SELF)) // epic fiend feat
       {
           eSummon = EffectSummonCreature("sum9ceh", VFX_FNF_SUMMON_GATE, fDelay);
       }
       else
       {
        eSummon = EffectSummonCreature("sum9ce", VFX_FNF_SUMMON_GATE, fDelay);
       }
    }

    ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eSummon, GetSpellTargetLocation(), HoursToSeconds(nDuration));
    DelayCommand(3.0,fnSetWatchers(OBJECT_SELF,HoursToSeconds(nDuration)));
}