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

Spell Shape

Caster Level(s): Wizard / Sorcerer 7
Innate Level: 7
School: Abjuration
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: 0
Area of Effect / Target: Caster
Duration: 1d4 + 1 rounds 
Save: Special
Spell Resistance: No

     This spell gives the wild mage the ability to seize 
magical energy directed at him and reshape it as he desires. 
While it is in effect, the spell gives no visible sign of 
its existence. It offers no protection against area effect 
spells.

     If a wild mage is the target of a spell or magical item, this spell automatically allows him a saving throw. If the saving throw is failed, the opponent’s spell has normal effects. If the saving throw is successful, the spell shape absorbs the magical energy of the opponent’s spell. The wild mage can then choose to let the energy dissipate or he can
instantly use it to cast a spell back at the opposing mage. 
The return spell must be of an equal or lesser spell level 
than the original one and must be currently memorized by the 
mage. The act of returning the spell does not cost the wild 
mage any of his memorized spells. Spell energy cannot be 
saved; if not used immediately, it dissipates. 

     For example, Hamos, a wild mage, is protected by spell 
shape and is struck by a finger of death (a 7th level spell. 
He succeeds at his saving throw and is now able to cast a 
spell of 7th level or lower. Hamos currently has feeblemind 
memorized. Since it is only a 5th-level spell, he chooses to 
cast it back at his enemy. He makes his level variation 
check (and doesn‘t get a wild surge) and the feeblemind is 
sent hurtling back at his foe. Hamos still has his original 
feeblemind memorized. The remaining two spell levels are 
lost.

2d2f_sp_spell_shape.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 02-17-2008
//:: Last Update: 03-22-2008
//::///////////////////////////////////////////////

#include "2d2f_includes"
#include "jx_inc_magic"
#include "x2_inc_spellhook" 
#include "jx_inc_magic_info"
#include "nwn2_inc_spells"
#include "nw_i0_spells"



void mainSpell();
void main()
{
	AssignCommand(JXGetCaster(), mainSpell());
}

void mainSpell()
{
	object oCaster = JXGetCaster();
	int nWMLvl = GetLevelByClass(CLASS_WILDMAGE, oCaster);
	if(nWMLvl >= 9)
	{
/* 
  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
        { JXPostSpellCastCode(); return; }
    }
	//:://////////////////////////////////////////
	
	int nDur = d4(1) + 1;
	float fDuration = RoundsToSeconds(nDur);	

	effect eVis = EffectVisualEffect(VFX_IMP_SPELL_SHAPE);
	
	effect eVis2 = EffectVisualEffect(VFX_DUR_LIGHT_PURPLE_5);
	effect eVis3 = EffectEffectIcon(EFFECT_ICON_SPELL_SHAPE);
	effect eScale = EffectSetScale(1.0);
	effect eLink = EffectLinkEffects(eVis2,eVis3);
	eLink = EffectLinkEffects(eLink,eScale);
		
	//Fire cast spell at event for the specified target
    SignalEvent(oCaster, EventSpellCastAt(OBJECT_SELF, SPELL_SPELL_SHAPE, FALSE));
	
	//Check for metamagic extend
    fDuration = ApplyMetamagicDurationMods(fDuration);
    int nDurType = ApplyMetamagicDurationTypeMods(DURATION_TYPE_TEMPORARY);

	JXApplyEffectToObject(nDurType, eVis, oCaster, fDuration);
	JXApplyEffectToObject(nDurType, eLink, oCaster, fDuration);
		
	}
	else
	{
	SendMessageToPC(oCaster, "You must be at least a level 9 Wild Mage to use this spell");
	}
	
	
	JXPostSpellCastCode();
	return;
}