//::///////////////////////////////////////////////
//:: Wild Mage Spell - Erratic Essence Exchanger
//::///////////////////////////////////////////////
/*
Erratic Essence Exchanger

Caster Level(s): Wizard / Sorcerer 6
Innate Level: 6
School: Necromancy
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: Small
Area of Effect / Target: 80' diameter circle
Duration: 1 round / level
Save: None
Spell Resistance: No
!!! This is a Wild Magic Spell and can only be cast by a Wild Mage !!!

     Erratic Essence Exchanger destabilizes the life-force of any creatures caught 
within its area of effect.  Random connections are then made between these creatures 
similar to how a lake is connected to a sea allowing health to flow between them like 
a river.  This connection seeks balance, and the flow can not stop until both side of 
the connection are equalized.
     Everyone within 40 feet of where Erratic Essence Exchanger is centered 
randomly selects a creature with which they form a connection.  At this time, if 
the selected creature is not themselves, their Hit Point total is changed to that of the 
random creature.  If the hit points are greater than their normal maximum they are 
added as temporary hit points that can not be healed and will disappear when the 
spell�s duration expires.  If the hit points are less than what they currently have they 
take an amount of irresistible damage equal to the difference between totals.  These 
hit points will be returned when the spell expires.

2d2f_sp_essence_exchanger.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 4-7-2008
//:: Last Update: 4-7-2008
//::///////////////////////////////////////////////

// 2d2f - redundant includes caused major problems
//#include "2d2f_includes"
//#include "jx_inc_magic"
#include "x2_inc_spellhook" 
//#include "jx_inc_magic_info"
//#include "nwn2_inc_spells"
//#include "nw_i0_spells"
#include "2d2f_inc_magic_wild"

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

void mainSpell()
{
	object oCaster = JXGetCaster();
	int nWMLvl = GetLevelByClass(CLASS_WILDMAGE, oCaster);
	if((nWMLvl >= 9) || (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; }
    }
	//:://////////////////////////////////////////
	
	location lTarget = GetSpellTargetLocation();
	int nLvl = JXGetCasterLevel();
	int nDur = nLvl;
	float fDuration = RoundsToSeconds(nDur);
	object oLocationMarker = CreateObject(OBJECT_TYPE_PLACEABLE, "plc_ipoint ", lTarget, FALSE, "jx_ipoint_caster");
	
	//Check for metamagic extend
    fDuration = ApplyMetamagicDurationMods(fDuration);
    int nDurType = ApplyMetamagicDurationTypeMods(DURATION_TYPE_TEMPORARY);
	
	//do aoe inpact effect
	effect eVis = EffectVisualEffect(VFX_DUR_ESSENCE_EXCHANGER);
	JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eVis,oLocationMarker,RoundsToSeconds(1));
	
	object oTarget = GetFirstObjectInShape(SHAPE_SPHERE,RADIUS_SIZE_TREMENDOUS,lTarget,TRUE,OBJECT_TYPE_CREATURE);
	
	while(GetIsObjectValid(oTarget))
	{
		object oRandom = GetRandomCreatureInArea(oLocationMarker,oTarget,RADIUS_SIZE_TREMENDOUS, TRUE, TRUE);
		int nTargetHP = GetCurrentHitPoints(oTarget);
		int nRandomHP = GetCurrentHitPoints(oRandom);
		int nHPDiff = nRandomHP - nTargetHP;
		effect eTempHP;
		effect eDam;
		effect eHeal;
				
		if(oTarget != oRandom)
		{
			effect eBeam = EffectBeam(VFX_BEAM_ESSENCE_EXCHANGER,oRandom,BODY_NODE_CHEST,FALSE);
			JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eBeam,oTarget,4.2);
			
			if(nHPDiff > 0) // target gains hp
			{
				eTempHP = EffectTemporaryHitpoints(nHPDiff);
				JXApplyEffectToObject(nDurType,eTempHP,oTarget,fDuration);			
			}
			else if(nHPDiff < 0) // target loses hp
			{
				int nDam = nHPDiff * -1;
				eDam = EffectDamage(nDam,DAMAGE_TYPE_MAGICAL,DAMAGE_POWER_ENERGY,TRUE);
				eHeal = EffectHeal(nHPDiff);
				JXApplyEffectToObject(DURATION_TYPE_INSTANT,eDam,oTarget);
				DelayCommand(fDuration,	JXApplyEffectToObject(DURATION_TYPE_INSTANT,eHeal,oTarget));
			}
		
		}
	
	
	
	oTarget = GetNextObjectInShape(SHAPE_SPHERE,RADIUS_SIZE_TREMENDOUS,lTarget,TRUE,OBJECT_TYPE_CREATURE);
	}
	
	DestroyObject(oLocationMarker,fDuration);
	
	}
	else
	{
	SendMessageToPC(oCaster, "You must be at least a level 9 Wild Mage to use this spell");
	}
	
	
	JXPostSpellCastCode();
	return;
}