//::///////////////////////////////////////////////
//:: Cone: Mindflayer Mind Blast
//:: x2_m1_mindblast
//:: Copyright (c) 2002 Bioware Corp.
//:://////////////////////////////////////////////
/*
    Anyone caught in the cone must make a
    Will save (DC 17) or be stunned for 3d4 rounds
*/
//:://////////////////////////////////////////////
//:: Created By: Keith Warner
//:: Created On: Dec 5/02
//:://////////////////////////////////////////////

#include "x2_i0_spells"

void main()
{
   int nSaveDC = 14 +(GetHitDice(OBJECT_SELF)/2) +  GetAbilityModifier(ABILITY_WISDOM,OBJECT_SELF);
   int nDC = (GetHitDice(OBJECT_SELF)-20)+ nSaveDC;
   object oTarget = GetSpellTargetObject();
   object oCaster = OBJECT_SELF;
   float fRange = 15.0f;
   int nDuration = d4(1)+1;
   //DoMindBlast(nSaveDC, d6(10), 15.0f);


if(GetLocalInt(GetArea(OBJECT_SELF), "NOCAST")==2 && !GetIsDM(oCaster))
{

//Make them stop what they are doing instantly!
AssignCommand(oCaster, ClearAllActions());

//Though the player may show animation, nothing happens! :)
//SetModuleOverrideSpellScriptFinished();

FloatingTextStringOnCreature("All spells fizzle in town.", oCaster);

return;
}

///////////////////////////////////////////////////////////////////////////////
//Handle Jail...
//NO ITEMS WORK IN JAIL!!!
if(GetLocalInt(GetArea(oCaster), "JAIL")==1)
{
 AssignCommand(oCaster, ClearAllActions());
 //Though the player may show animation, nothing happens! :)
 //SetModuleOverrideSpellScriptFinished();

 return;
}

///////////////////////////////////////////////////////////////////////////////

   {
    int nStunTime;
    float fDelay;

    location lTargetLocation = GetSpellTargetLocation();
    object oTarget;
    effect eCone;
    effect eVis = EffectVisualEffect(VFX_IMP_SONIC);

    oTarget = GetFirstObjectInShape(SHAPE_SPELLCONE, fRange, lTargetLocation, TRUE);

    while(GetIsObjectValid(oTarget))
    {
        int nApp = GetAppearanceType(oTarget);
        int bImmune = FALSE;
        //----------------------------------------------------------------------
        // Hack to make mind flayers immune to their psionic attacks...
        //----------------------------------------------------------------------
        if (nApp == 413 ||nApp== 414 || nApp == 415)
        {
            bImmune = TRUE;
        }

        if(spellsIsTarget(oTarget,SPELL_TARGET_STANDARDHOSTILE,OBJECT_SELF) && oTarget != OBJECT_SELF && !bImmune )
        {
            SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, GetSpellId()));
            fDelay = GetDistanceBetween(OBJECT_SELF, oTarget)/20;
            // already stunned
            if (GetHasSpellEffect(GetSpellId(),oTarget))
            {
                 // only affects the targeted object
                ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_STUN), oTarget);
                int nDamage;
                if (GetLevelByClass(CLASS_TYPE_SHIFTER,OBJECT_SELF)>0)
                {
                    nDamage = d6(GetLevelByClass(CLASS_TYPE_SHIFTER,OBJECT_SELF)/2);
                }
                else
                {
                    nDamage = d6(GetHitDice(OBJECT_SELF)/2);
                }
                ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectDamage(nDamage), oTarget);
                ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_BIGBYS_FORCEFUL_HAND), oTarget);
            }
            else if (WillSave(oTarget, nDC) < 1)
            {
                //Calculate the length of the stun
                nStunTime = nDuration;
                //Set stunned effect
                eCone = EffectStunned();
                eCone = SupernaturalEffect(eCone); //Hack to make it bypass immunities :)
                //Apply the VFX impact and effects
                DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oTarget));
                DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eCone, oTarget, RoundsToSeconds(nStunTime)));
            }
        }
        //Get next target in spell area
        oTarget = GetNextObjectInShape(SHAPE_SPELLCONE, fRange, lTargetLocation, TRUE);
    }
}

}