//::///////////////////////////////////////////////
//:: Doom
//:: NW_S0_Doom.nss
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    If the target fails a save they recieve a -2
    penalty to all saves, attack rolls, damage and
    skill checks for the duration of the spell.

    July 22 2002 (BK): Made it mind affecting.
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: Oct 22, 2001
//:://////////////////////////////////////////////
/*
Patch 1.70

- missing saving throw VFX added
- immunity feedback corrected
- SR check moved before saving throw check
*/

#include "70_inc_spells"
#include "x0_i0_spells"
#include "x2_inc_spellhook"

effect CreateDoomEffectsLink3()
{
//Declare major variables
effect eSaves = EffectSavingThrowDecrease(SAVING_THROW_ALL,3);
effect eAttack = EffectAttackDecrease(3);
effect eDamage = EffectDamageDecrease(3,DAMAGE_TYPE_SLASHING);
effect eSkill = EffectSkillDecrease(SKILL_ALL_SKILLS,3);
effect eDur = EffectVisualEffect(VFX_DUR_CESSATE_NEGATIVE);

effect eLink = EffectLinkEffects(eAttack,eDamage);
eLink = EffectLinkEffects(eLink,eSaves);
eLink = EffectLinkEffects(eLink,eSkill);
return  EffectLinkEffects(eLink,eDur);
}

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
    spellsDeclareMajorVariables();
    effect eVis = EffectVisualEffect(VFX_IMP_DOOM);
    effect eLink = CreateDoomEffectsLink3();

    float fDuration = TurnsToSeconds(spell.Level);
    //Meta-Magic checks
    if(spell.Meta == METAMAGIC_EXTEND)
    {
        fDuration *= 2;
    }
    if(spellsIsTarget(spell.Target, SPELL_TARGET_SINGLETARGET, spell.Caster))
    {
        SignalEvent(spell.Target, EventSpellCastAt(spell.Caster, spell.Id));
        //Spell Resistance check
        if(!MyResistSpell(spell.Caster, spell.Target))
        {
            //will saving throw
            if(!MySavingThrow(SAVING_THROW_WILL, spell.Target, spell.DC, SAVING_THROW_TYPE_MIND_SPELLS, spell.Caster))
            {
                //* Engine workaround for mind affecting spell without mind effect
                if(GetIsImmune(spell.Target, IMMUNITY_TYPE_MIND_SPELLS, spell.Caster))
                {
                    eLink = EffectDazed();//force target to overcome the spell effect and print immunity feedback instead
                    fDuration = 1.0;//for safety
                }
                else
                {
                    //apply doom VFX only if not immune
                    ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, spell.Target);
                }
                ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLink, spell.Target, fDuration);
            }
        }
    }
}