//::///////////////////////////////////////////////
//:: Wild Mage Spell - Vortex heart script
//::///////////////////////////////////////////////
/*
Description modified from AD&D 2nd Edition - "Tome of Magic"

Vortex

Caster Level(s): Wizard / Sorcerer 5
Innate Level: 5
School: Evocation
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: Medium
Area of Effect / Target: 8' diameter circle / caster’s familiar
Duration: 1d4 + 1 round / 2 lvl
Save: Fortitude 1/2
Spell Resistance: Yes

     This spell takes advantage of the magical link between 
a wizard and his familiar.  The wild mage manipulates this 
connection transforming his familiar into a mass of swirling 
raw magical energy.  Once the transformation is complete all 
semblance of personality and loyalty in this once faithful 
companion is lost.  Each round the caster can attempt to 
possess the Vortex temporarily controlling its movement.  
However, his control is not perfect and there is a 50% 
chance of losing control each round.  If loses control, or 
does not take control in the first place, the Vortex will 
move around randomly.   The link between the caster and the 
Vortex is not as strong as it was with his familiar and if 
the Vortex moves beyond 30 meters he will be unable to 
possess it.  At the end of the spell the casters familiar 
reforms but may suffer damage from the transition.

	The vortex is composed of raw magical energy. Nonmagical
creatures within 7.5 feet of the vortex suffer ld4 points of 
damage per level of the caster. Magical creatures and 
spellcasters suffer ld6 points of damage per level of the 
caster. Creatures struck are allowed a Fortitude saving 
throw vs. spells to suffer only half damage. Each time a 
creature is struck, there is a 5% chance that the vortex 
will explode in a wild surge. When this happens a random 
spell between levels 1 and 6 is cast targeting a random 
creature within 7.5 feet of the vortex. This spell is 
guaranteed to cause a wild surge.  The probability of each 
level being cast is as follows.
Level 1 = 10%
Level 2 = 20%
Level 3 = 25%
Level 4 = 20%
Level 5 = 15%
Level 6 = 10%
If the vortex causes a wild surge, the spell ends 
immediately.



2d2f_sp_vortex_heart.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 03-16-08
//:: Last Update: 03-22-08
//::///////////////////////////////////////////////

//#include "2d2f_includes"
//#include "jx_inc_magic"
//#include "x2_inc_spellhook" 
//#include "jx_inc_magic_info"
#include "2d2f_inc_magic_class"
//#include "nwn2_inc_spells"
//#include "nw_i0_spells"
#include "2d2f_inc_magic_wild"


void main()
{
	object oPC = GetFirstPC();
	object oCaster = GetAreaOfEffectCreator();//JXGetAOERealCreator();
	object oFamiliar = GetLocalObject(oPC,"oFamiliar");
	float nRadius = FeetToMeters(2.5);
	object oTarget = GetFirstInPersistentObject();
	float fDistance = GetDistanceBetween(oPC,oFamiliar);
	int nLvl = JXGetCasterLevel(oPC);
	int nDam;
	
	int nDC = 15 + GetAbilityModifier(ABILITY_INTELLIGENCE,oPC);
		
	if(GetHasFeat(FEAT_SPELL_FOCUS_EVOCATION,oPC,TRUE) == TRUE)
	{
		nDC = nDC+1;
	}
	else if(GetHasFeat(FEAT_GREATER_SPELL_FOCUS_EVOCATION,oPC,TRUE) == TRUE)
	{
		nDC = nDC+2;
	}
	else if(GetHasFeat(FEAT_EPIC_SPELL_FOCUS_EVOCATION,oPC,TRUE) == TRUE)
	{
		nDC = nDC+3;
	}
	if(GetHasFeat(FEAT_SPELLCASTING_PRODIGY,oPC,TRUE) == TRUE)
	{
		nDC = nDC+1;
	}
	
	//SendMessageToPC(oPC,"Vortex heart script fired");
	
	if(GetIsDead(oPC) || !GetIsObjectValid(oCaster))
    {
        DestroyObject(OBJECT_SELF, 0.0);
		SetImmortal(oFamiliar, FALSE);
		SetAssociateState(NW_ASC_MODE_STAND_GROUND, FALSE, oTarget);
		SetAssociateState(NW_ASC_MODE_PUPPET, FALSE, oTarget);
    }	
	
	AssignCommand(oFamiliar,ClearAllActions());
	
	SetAssociateState(NW_ASC_MODE_STAND_GROUND, TRUE, oFamiliar);
		
	SetAssociateState(NW_ASC_MODE_PUPPET, TRUE, oFamiliar);
		
	AssignCommand(oFamiliar,ActionRandomWalk());
	DelayCommand(3.0,AssignCommand(oFamiliar,ActionRandomWalk()));
	
	
	int nD2 = d2(1);
	
	if(nD2 == 1 && fDistance < 30.0)
	{
		SendMessageToPC(oPC,"You lose controll of the Vortex!!!");
			
		int i;
		for(i = 1; i <= 3; i++)
		{	
			DelayCommand(IntToFloat((i*2)-1),UnpossessFamiliar(oFamiliar));
		}			
	}
	else if(nD2 == 2 && fDistance < 30.0)
	{
	
		SendMessageToPC(oPC,"You may possess the Vortex.");
			
	}
	else if(fDistance > 30.0)
	{
		SendMessageToPC(oPC,"The Vortex has moved beyond the range of you control.");
		SendMessageToPC(oPC,"You lose controll of the Vortex!!!");
		
		int i;
		for(i = 1; i <= 3; i++)
		{	
			DelayCommand(IntToFloat((i*2)-1),UnpossessFamiliar(oFamiliar));
		}
	}
	
	while(GetIsObjectValid(oTarget) && oTarget != oFamiliar && GetObjectType(oTarget) == OBJECT_TYPE_CREATURE)
	{
	//SendMessageToPC(oPC,"Target heart : " + GetName(oTarget));
		if (!MyResistSpell(oPC, oTarget))
	   	{
		// magical creature
		if(GetRacialType(oTarget) == RACIAL_TYPE_ABERRATION ||
		GetRacialType(oTarget) == RACIAL_TYPE_CONSTRUCT ||
		GetRacialType(oTarget) == RACIAL_TYPE_DRAGON ||
		GetRacialType(oTarget) == RACIAL_TYPE_ELEMENTAL ||
		GetRacialType(oTarget) == RACIAL_TYPE_FEY ||
		GetRacialType(oTarget) == RACIAL_TYPE_INCORPOREAL ||
		GetRacialType(oTarget) == RACIAL_TYPE_MAGICAL_BEAST ||
		GetRacialType(oTarget) == RACIAL_TYPE_OUTSIDER ||
		GetRacialType(oTarget) == RACIAL_TYPE_SHAPECHANGER ||
		GetRacialType(oTarget) == RACIAL_TYPE_UNDEAD ||
		GetLevelByClass(CLASS_TYPE_BARD,oTarget) > 0 ||
		GetLevelByClass(CLASS_TYPE_CLERIC,oTarget) > 0 ||
		GetLevelByClass(CLASS_TYPE_DRUID,oTarget) > 0 ||
		GetLevelByClass(CLASS_TYPE_SORCERER,oTarget) > 0 ||
		GetLevelByClass(CLASS_TYPE_SPIRIT_SHAMAN,oTarget) > 0 ||
		GetLevelByClass(CLASS_TYPE_WIZARD,oTarget) > 0 ||
		GetLevelByClass(CLASS_TYPE_FAVORED_SOUL,oTarget) > 0 ||
		GetLevelByClass(CLASS_TYPE_WARLOCK,oTarget) > 0)
		{
			SignalEvent(oTarget, EventSpellCastAt(oFamiliar,SPELL_VORTEX));
				
			nDam = d6(nLvl);
						
			//Adjust the damage based on the Reflex Save, Evasion and Improved Evasion.
            nDam = GetReflexAdjustedDamage(nDam, oTarget, nDC, SAVING_THROW_TYPE_SPELL);
            			
            if(nDam > 0)
            {
				effect eDam = EffectDamage(nDam);
				JXApplyEffectToObject(DURATION_TYPE_INSTANT,eDam,oTarget);
           	}	
			
			int nD20 = d20(1);
			
			if(nD20 == 20)
			{		
				SendMessageToPC(oPC,"The Vortex explodes causing a wild surge!!!");		
				RemoveSpellEffects(SPELL_VORTEX,oPC,oFamiliar);
				DestroyObject(OBJECT_SELF, 0.0);
				SetAssociateState(NW_ASC_MODE_STAND_GROUND, FALSE, oTarget);
				SetAssociateState(NW_ASC_MODE_PUPPET, FALSE, oTarget);
    			AssignCommand(oFamiliar,ClearAllActions());
				DelayCommand(6.0,SetImmortal(oFamiliar, FALSE));
				
				effect eVis = EffectVisualEffect(VFX_IMP_VORTEX);
				JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eVis,oFamiliar,4.0);
								
				doSurgeNoSpell(oTarget,nRadius);
			}
			
		}
		else // not a magical creature
		{
			SignalEvent(oTarget, EventSpellCastAt(oFamiliar,SPELL_VORTEX));
				
			nDam = d4(nLvl);
						
			//Adjust the damage based on the Reflex Save, Evasion and Improved Evasion.
            nDam = GetReflexAdjustedDamage(nDam, oTarget, nDC, SAVING_THROW_TYPE_SPELL);
            			
            if(nDam > 0)
            {
				effect eDam = EffectDamage(nDam);
				JXApplyEffectToObject(DURATION_TYPE_INSTANT,eDam,oTarget);
           	}						
		
			int nD20 = d20(1);
			
			if(nD20 == 20)
			{		
				SendMessageToPC(oPC,"The Vortex explodes causing a wild surge!!!");		
				RemoveSpellEffects(SPELL_VORTEX,oPC,oFamiliar);
				DestroyObject(OBJECT_SELF, 0.0);
				SetAssociateState(NW_ASC_MODE_STAND_GROUND, FALSE, oTarget);
				SetAssociateState(NW_ASC_MODE_PUPPET, FALSE, oTarget);
    			AssignCommand(oFamiliar,ClearAllActions());
				DelayCommand(6.0,SetImmortal(oFamiliar, FALSE));
				
				effect eVis = EffectVisualEffect(VFX_IMP_VORTEX);
				JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eVis,oFamiliar,4.0);
								
				doSurgeNoSpell(oTarget,nRadius);
			}
		}	
		}//sr	
	
		oTarget = GetNextInPersistentObject();
	}

}
/*
if ( MySavingThrow(SAVING_THROW_FORT, oTarget, nDC,SAVING_THROW_TYPE_SPELL ))
        	{
				nDam = d6(nLvl);
			}
			else
			{
				nDam = d6(nLvl/2);
			}
			
			SignalEvent(oTarget, EventSpellCastAt(oFamiliar,SPELL_VORTEX));
						
			effect eDam = EffectDamage(nDam);
			JXApplyEffectToObject(DURATION_TYPE_INSTANT,eDam,oTarget);
			
*/