#include "NW_I0_GENERIC"
void main()
{
    // Crypt Thing's PC Teleport
    int nEvent = GetUserDefinedEventNumber();
    if (!(GetLocalInt(OBJECT_SELF, "fired") == 1) && nEvent == 1003)
        {
        // Casting
        object oCreature = GetNearestObject(OBJECT_TYPE_CREATURE);
        PlayAnimation(ANIMATION_LOOPING_CONJURE1,1.0,4.0);
        SetLocalInt(OBJECT_SELF, "fired", 1);

        int iNum = 1;
        while (GetIsObjectValid(oCreature) && GetDistanceToObject(oCreature) < 30.0 && iNum<2 &&
                GetIsEnemy(oCreature)) {
            // Saving Throw
            if (WillSave(oCreature, 18, SAVING_THROW_TYPE_SPELL) == 0){

                FloatingTextStringOnCreature(GetName(oCreature)+
                        " has been teleported by the Crypt Thing!", oCreature, FALSE);
                // Failed
                object oDest1 = GetObjectByTag("WP_CRYPTTHING_1");
                location lDest1 = GetLocation(oDest1);
                object oDest2 = GetObjectByTag("WP_CRYPTTHING_2");
                location lDest2 = GetLocation(oDest2);
                object oDest3 = GetObjectByTag("WP_CRYPTTHING_3");
                location lDest3 = GetLocation(oDest3);
                object oDest4 = GetObjectByTag("WP_CRYPTTHING_4");
                location lDest4 = GetLocation(oDest4);
                object oDest5 = GetObjectByTag("WP_CRYPTTHING_5");
                location lDest5 = GetLocation(oDest5);
                object oDest6 = GetObjectByTag("WP_CRYPTTHING_6");
                location lDest6 = GetLocation(oDest6);
                SetLocalInt(OBJECT_SELF, "duration", DURATION_TYPE_TEMPORARY);
                int duration = GetLocalInt(OBJECT_SELF, "duration");
                SetLocalInt(OBJECT_SELF, "vfx", VFX_IMP_AC_BONUS);
                int vfx = GetLocalInt(OBJECT_SELF, "vfx");
                effect eEffect = EffectVisualEffect(vfx);

                int nRoll=d6();

                switch (nRoll)
                {
                    case 1:
                        ApplyEffectToObject(duration, eEffect, oCreature, 2.0);
                        DelayCommand(1.0, AssignCommand(oCreature, JumpToLocation(lDest1)));
                    break;
                    case 2:
                        ApplyEffectToObject(duration, eEffect, oCreature, 2.0);
                        DelayCommand(1.0, AssignCommand(oCreature, JumpToLocation(lDest2)));
                    break;
                    case 3:
                        ApplyEffectToObject(duration, eEffect, oCreature, 2.0);
                        DelayCommand(1.0, AssignCommand(oCreature, JumpToLocation(lDest3)));
                    break;
                    case 4:
                        ApplyEffectToObject(duration, eEffect, oCreature, 2.0);
                        DelayCommand(1.0, AssignCommand(oCreature, JumpToLocation(lDest4)));
                    break;
                    case 5:
                        ApplyEffectToObject(duration, eEffect, oCreature, 2.0);
                        DelayCommand(1.0, AssignCommand(oCreature, JumpToLocation(lDest5)));
                    break;
                    case 6:
                        ApplyEffectToObject(duration, eEffect, oCreature, 2.0);
                        DelayCommand(1.0, AssignCommand(oCreature, JumpToLocation(lDest6)));
                    break;
                }

            } else {
                FloatingTextStringOnCreature(GetName(oCreature)+
                        " resisted the Crypt Thing's spell!", oCreature, FALSE);
            }
            iNum++;
            oCreature = GetNearestObject(OBJECT_TYPE_CREATURE, OBJECT_SELF, iNum);
        }
    }
}


