#include "prc_x2_itemprop"
#include "x2_inc_switches"
#include "x2_inc_spellhook"
#include "nw_i0_spells"
#include "inc_misc_tools"
void main()
    {
    if (!X2PreSpellCastCode())
        {
        // If code within the PreSpellCastHook (i.e. UMD) reports FALSE, do not run this spell
            return;
        }
    object oCaster = GetLastSpellCaster();
    if (!GetIsObjectValid(oCaster))
        oCaster = GetSpellCastItem();
    int nSpell = GetSpellId();
    int nSpellDC = GetSpellSaveDC();
    int nCastLevel = GetCasterLevel(OBJECT_SELF);
    object oTarget = GetSpellTargetObject();
    int nDuration = GetCasterLevel(OBJECT_SELF);
    int nMetaMagic = GetMetaMagicFeat();
    effect eVis,eDur,eRemove;
    effect eAC1, eAC2, eAC3, eAC4;
    int nCnt,i,iPassed;
    itemproperty ipItem;
    object oTrap;
    string sTag;
    switch (nSpell)
        {
        case SPELL_FIND_TRAPS:
            {
            eVis = EffectVisualEffect(VFX_IMP_KNOCK);
            nCnt = 1;
            oTrap = GetNearestObject(OBJECT_TYPE_TRIGGER | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, OBJECT_SELF, nCnt);
            while(GetIsObjectValid(oTrap) && GetDistanceToObject(oTrap) <= 30.0)
                {
                if(GetIsTrapped(oTrap))
                    {
                    SetTrapDetectedBy(oTrap, OBJECT_SELF);
                    ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eVis, GetLocation(oTrap));
                    }
                nCnt++;
                oTrap = GetNearestObject(OBJECT_TYPE_TRIGGER | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, OBJECT_SELF, nCnt);
                }
            SetModuleOverrideSpellScriptFinished();
            }
        case SPELL_MAGE_ARMOR:
            {
            //Declare major variables
            nDuration = GetCasterLevel(OBJECT_SELF);
            nMetaMagic = GetMetaMagicFeat();
            eVis = EffectVisualEffect(VFX_IMP_AC_BONUS);
            //Fire cast spell at event for the specified target
            SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_MAGE_ARMOR, FALSE));
            //Check for metamagic extend
            if (nMetaMagic == METAMAGIC_EXTEND) //Duration is +100%
                 nDuration = nDuration * 2;
            //Set the four unique armor bonuses
            int i = GetItemACValue(GetItemInSlot(INVENTORY_SLOT_CHEST,oTarget)) + GetItemACValue(GetItemInSlot(INVENTORY_SLOT_LEFTHAND,oTarget));
            if (i >= 4)return;
            else i = 4 - i;
            eAC1 = EffectACIncrease(i, AC_ARMOUR_ENCHANTMENT_BONUS);
            eDur = EffectVisualEffect(VFX_DUR_CESSATE_POSITIVE);
            RemoveEffectsFromSpell(oTarget, SPELL_MAGE_ARMOR);
            //Apply the armor bonuses and the VFX impact
            ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eAC1, oTarget, HoursToSeconds(nDuration));
            ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oTarget);
            SetModuleOverrideSpellScriptFinished();
            }
        case SPELL_CONTINUAL_FLAME:
            {
            if (!GetIsObjectValid(GetSpellCastItem()))
                {
                if (!GetHasGem(2,OBJECT_SELF))
                    {
                    if (GetIsPC(OBJECT_SELF))
                        SendMessageToPC(OBJECT_SELF,"You require a gem of at least 50 gold value to cast this spell.");
                    SetModuleOverrideSpellScriptFinished();
                    }
                return;
                }
            return;
            }
        case SPELL_IDENTIFY:
            {
            if (!GetIsObjectValid(GetSpellCastItem()))
                {
                if (!GetHasGem(3,OBJECT_SELF))
                    {
                    if (GetIsPC(OBJECT_SELF))
                        SendMessageToPC(OBJECT_SELF,"You require a gem of at least 100 gold value to cast this spell.");
                    SetModuleOverrideSpellScriptFinished();
                    return;
                    }
                }
            return;
            }
        case SPELL_REMOVE_CURSE:
            {
            eRemove = GetFirstEffect(oTarget);
            eVis = EffectVisualEffect(VFX_IMP_REMOVE_CONDITION);
            //Fire cast spell at event for the specified target
            SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_REMOVE_CURSE, FALSE));
            //Get the first effect on the target
            while(GetIsEffectValid(eRemove))
                {
                //Check if the current effect is of correct type
                if (GetEffectType(eRemove) == EFFECT_TYPE_CURSE)
                    {
                    //Remove the effect and apply VFX impact
                    RemoveEffect(oTarget, eRemove);
                    ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oTarget);
                    iPassed = TRUE;
                    }
                //Get the next effect on the target
                GetNextEffect(oTarget);
                }
            if (!iPassed)
                {
                for (i = 0; i<= 14; i++)
                    {
                    if (GetItemCursedFlag(GetItemInSlot(i,oTarget)))
                        {
                        SetItemCursedFlag(GetItemInSlot(i,oTarget),FALSE);
                        break;
                        }
                    }
                }
            SetModuleOverrideSpellScriptFinished();
            return;
            }
        };
    }
