//::///////////////////////////////////////////////
//:: Wild Mage Spell - Aerial's Scatterport Friendly
//::///////////////////////////////////////////////
/*

Scatterport

Caster Level(s): Wizard / Sorcerer 1
Innate Level: 1
School: Transmutation
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: Small
Area of Effect / Target: 25 ft. + 5 ft./2 levels
Duration: Instant
Save: Fortitude negates.
Spell Resistance: Yes

This spell transports the designated creature 2d4 + 5 feet/2 
levels away from its original position in a random direction.  
Scatterport is limited to line-of-sight and will not 
teleport the subjects past any barriers.  If an object is 
between the scatterport target creature and the intended 
destination of their fantastic journey they will arrive next 
to the blocking object instead of the at maximum distance.  
Hostile creatures are allowed a Fortitude save vs. spells 
to resist the effect.


2d2f_sp_scatterport_friendly.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 03-09-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"
#include "x0_i0_position"


//:: prototype
//:: angle and distance to vector
vector VectorFromAngleAndDistance(float fAngle, float distance);



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

void mainSpell()
{
	object oCaster = JXGetCaster();
	int nWMLvl = GetLevelByClass(CLASS_WILDMAGE, oCaster);
	if(nWMLvl >= 0)
	{
/* 
  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 oTarget = GetSpellTargetObject();
	int nLvl = JXGetCasterLevel();
	float fRadius = FeetToMeters(25.0) + (FeetToMeters(5.0)*(IntToFloat(nLvl)/2.0));
	location lTargetEnd; // final location
	vector vTargetEnd; // vector to get final location
	vector vRandVector; // used to check that final location is in LOS
	vector vStart = GetPosition(oTarget); //vector of start position
	float x1 = vStart.x;
	float y1 = vStart.y;
	float fRandAngle = IntToFloat(Random(360));
	
	effect eVis = EffectVisualEffect(VFX_DUR_SCATTERPORT);
	
	int nLineOfSight = FALSE;	
	while(nLineOfSight == FALSE)
	{
		// get a vector from the random angle and range. it starts from 0
		vector vRandVector = VectorFromAngleAndDistance(fRandAngle, fRadius);
		
		// add the vector to the vector of the targets current location
		vTargetEnd = Vector(vStart.x + vRandVector.x,vStart.y + vRandVector.y);
		
		// check if in LOS
		nLineOfSight = LineOfSightVector(GetPosition(oTarget),vTargetEnd);
		
		if(nLineOfSight == FALSE && fRadius > 0.0)
		{
			// reduce by 3 feet tell it is in LOS
			fRadius = fRadius - FeetToMeters(3.0);
		}
		else
		{
			nLineOfSight = TRUE;
		}
	}
	
	// get location from vector
	lTargetEnd = Location(GetArea(oTarget),vTargetEnd,0.0f);
	
	SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_SCATTERPORT));
    
	if (spellsIsTarget(oTarget, SPELL_TARGET_ALLALLIES, OBJECT_SELF)) 
	{
		AssignCommand(oTarget, ActionJumpToLocation(lTargetEnd));

		JXApplyEffectAtLocation(DURATION_TYPE_INSTANT,eVis,GetLocation(oTarget));
		JXApplyEffectAtLocation(DURATION_TYPE_INSTANT,eVis,lTargetEnd);
	}
	
	
	}
	else
	{
	SendMessageToPC(oCaster, "You must be a Wild Mage to use this spell");
	}
	
	
	JXPostSpellCastCode();
	return;
}

vector VectorFromAngleAndDistance(float fAngle, float fDistance)
{
	float x = fDistance * cos(fAngle);
	float y = fDistance * sin(fAngle);
	vector vLocation = Vector(x,y);
	return vLocation;
}