//::///////////////////////////////////////////////
//:: Invisibility Sphere
//:: NW_S0_InvSph.nss
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    All allies within 15ft are rendered invisible.
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: Jan 7, 2002
//:://////////////////////////////////////////////

#include "jx_inc_magic"
#include "x2_inc_spellhook" 
#include "nw_i0_spells"
#include "x0_i0_SPELLS"



void mainSpell();
void main()
{
	AssignCommand(JXGetCaster(), mainSpell());
}

void mainSpell()
{

/* 
  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
        { JXPostSpellCastCode(); return; }
    }

// End of Spell Cast Hook


    //Declare major variables including Area of Effect Object
    int nDuration = JXGetCasterLevel(OBJECT_SELF);
    int nMetaMagic = JXGetMetaMagicFeat();
    //Make sure duration does no equal 0
    if (nDuration < 1)
    {
        nDuration = 1;
    }
    //Check Extend metamagic feat.
    if (nMetaMagic & METAMAGIC_EXTEND)
    {
	   nDuration = nDuration *2;	//Duration is +100%
    }
	effect eInvis = EffectInvisibility(INVISIBILITY_TYPE_NORMAL);
	effect eVis = EffectVisualEffect(VFX_DUR_INVISIBILITY);
	effect eLink = EffectLinkEffects( eInvis, eVis );
    //Create an instance of the AOE Object using the Apply Effect function
    JXApplyAreaEffectToObject(AOE_PER_INVIS_SPHERE, OBJECT_SELF, TurnsToSeconds(nDuration));

	//Declare major variables
	location lCenter = GetLocation(OBJECT_SELF);
    object oTarget = GetFirstObjectInShape( SHAPE_SPHERE, RADIUS_SIZE_LARGE, lCenter );
	object oCaster = OBJECT_SELF;
    
	while(GetIsObjectValid(oTarget))
	{
		if ( spellsIsTarget(oTarget, SPELL_TARGET_ALLALLIES, oCaster) )
		{ 	
      		SignalEvent(oTarget, EventSpellCastAt(oCaster, SPELL_INVISIBILITY_SPHERE, FALSE));
      		JXApplyEffectToObject( DURATION_TYPE_PERMANENT, eLink, oTarget );		
		}
		oTarget = GetNextObjectInShape( SHAPE_SPHERE, RADIUS_SIZE_LARGE, lCenter );
	}

	JXPostSpellCastCode();
}
