//::///////////////////////////////////////////////
//:: Wild Mage Spell - Organic Un-wounding
//::///////////////////////////////////////////////
/*

Organic Un-Wounding

Caster Level(s): Wizard / Sorcerer 2
Innate Level: 2
School: Necromancy
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: Touch
Area of Effect / Target: One Creature
Duration: 7 rounds
Save: None
Spell Resistance: No

!!! This is a Wild Magic Spell and can only be cast by a 
Wild Mage !!!

     Organic Un-Wounding appears to accomplish the 
impossible; an arcane spell that heals.  However, this is 
far from what it actually does.  This spell simply reveals 
the impression that a living creature had previously made on 
the weave and then forces that creature’s body to return to 
that time before it had been injured.  It is not healing if 
the person had never been injured in the first place.
     While ultimately beneficial, this process is extremely 
stressful on the recipient’s body.  Organic Un-Wounding 
causes its target to fall down in a paralytic state for 7 
rounds during which they are completely vulnerable.  This 
state is critical to the un-wounding process and creatures 
immune to paralysis will not be affected by this spell.  
Additionally, the recipient looses 1 point of constitution 
for 1 hour after they regain use of their body.  After the 
first round, the un-wounding process takes 6 additional 
rounds.  Damage is removed each round according to the 
following table.

Round 1 - 1 point
Round 2 - 1d4 points
Round 3 - 1d4 points + 1 point / 4 caster levels (up to + 5)
Round 4 - 1d4 points + 1 point / 4 caster levels (up to + 5)
Round 5 - 1d4 points
Round 6 - 1 point

2d2f_sp_un_wounding.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 03-28-2008
//:: Last Update: 03-28-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 >= 1) || (GetObjectType(oCaster) == OBJECT_TYPE_PLACEABLE))
	{
/* 
  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; }
    }
	//:://////////////////////////////////////////
	object oPC = GetFirstPC();
	object oTarget = JXGetSpellTargetObject();
	int nUnwoundMe = 0;
	
			
	// check if immune to paralysis
	if(GetIsImmune(oTarget,IMMUNITY_TYPE_PARALYSIS) != TRUE)
	{	
		if(spellsIsTarget(oTarget,SPELL_TARGET_SELECTIVEHOSTILE,oCaster))
		{
			int nTouch = TouchAttackMelee(oTarget);
			if (nTouch > 0)
			{
				SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_ORGANIC_UNWOUNDING));
	
				if (!MyResistSpell(OBJECT_SELF, oTarget))
	   			{
					//Make Fort save
            		if (!/*Fort Save*/ MySavingThrow(SAVING_THROW_FORT, oTarget, JXGetSpellSaveDC(),SAVING_THROW_TYPE_SPELL ))
            		{
						nUnwoundMe = 1;
					}
				}
			}
		}
		else
		{
			SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_ORGANIC_UNWOUNDING,FALSE));
	
			nUnwoundMe = 1;
		}
		
		
		if(nUnwoundMe == 1)
		{
			int nLvl = JXGetCasterLevel();
			float fDur = RoundsToSeconds(7);
			SetLocalObject(oCaster, "oUnWoundingTarget", oTarget);
			DelayCommand(fDur,DeleteLocalObject(oCaster, "oUnWoundingTarget"));
				
			effect ePar = EffectParalyze(99,SAVING_THROW_WILL,FALSE);
			effect eVis = EffectVisualEffect(VFX_DUR_BLUR);
			effect eVis2 = EffectVisualEffect(VFX_DUR_UN_WOUNDING_STEAM);
			effect eLink = EffectLinkEffects(ePar,eVis);
			eLink = EffectLinkEffects(eLink,eVis2);
			eLink = SupernaturalEffect(eLink);
			eLink = SetEffectSpellId(eLink,SPELL_ORGANIC_UNWOUNDING);
			
			AssignCommand(oTarget, ClearAllActions());
	        AssignCommand(oTarget, ActionPlayAnimation(ANIMATION_LOOPING_DEAD_BACK,	0.5, fDur));
			DelayCommand(2.5,JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eLink,oTarget,fDur));
			DelayCommand(2.5,JXApplyAreaEffectToObject(AOE_PER_CUSTOM_AOE,oTarget,fDur,"","2d2f_sp_un_wounding_heart",""));	
		}
	}
	else
	{
		SendMessageToPC(oPC, "Organic Un-Wounding will not work if the target is immune to paralysis");
	}	
	
	}
	else
	{
	SendMessageToPC(oCaster, "You must be at least a level 1 Wild Mage to use this spell");
	}
	
	
	JXPostSpellCastCode();
	return;
}