//::///////////////////////////////////////////////
//:: Wall of Force
//:: shaz_sp_wall_of_force.nss
//:://////////////////////////////////////////////
/*
    Creates an invisible wall of force that prevents
	all movement and spells from passing thru it, except
	teleport and dimenson door, which don't exist in NWN2.
	It cannot be destroyed by any means other than 
	mordekien's disjunction or disintigrate.
*/
//:://////////////////////////////////////////////
//:: Created By: Ken Cockerham
//:: Created On: 4/30/08
//:://////////////////////////////////////////////

#include "ginc_debug"
#include "ginc_math"
#include "jx_inc_magic"
#include "x2_inc_spellhook" 
#include "shaz_inc_forcewall"

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

void mainSpell()
{

/* 
  Spellcast Hook Code 
  Added 2003-06-20 by Georg
  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; }
    }

// End of Spell Cast Hook


    //Declare Area of Effect object using the appropriate constant
    //Get the location where the wall is to be placed.
    //location lTarget = JXGetSpellTargetLocation();
	location lTarget = GetSpellTargetLocation();	// shaz: made it use this to fix a bizzare bug, where an aoe spell after a wild surge would go to the place the surge had been
    int nDuration = JXGetCasterLevel(OBJECT_SELF);// / 2;	// Shaz made this last a lot longer (it lasts quite a bit longer in the PHB than fire wall)
    if(nDuration == 0)
    {
        nDuration = 1;
    }
    int nMetaMagic = JXGetMetaMagicFeat();

        //Check fort metamagic
        if (nMetaMagic & METAMAGIC_EXTEND)
		{
			nDuration = nDuration *2;	//Duration is +100%
		}
    //Create the Area of Effect Object declared above.
    JXApplyAreaEffectAtLocation(AOE_PER_WALL_OF_FORCE, lTarget, RoundsToSeconds(nDuration), "shaz_sp_wall_of_forcea", "shaz_sp_wall_of_forcec", "****");
	
	// Set a global so that the spell hook knows force walls are around
	// this is turned off when the spell interruption code doesn't find any walls
	SetGlobalInt("ShazForceWall", 1);
	
	// Create objects representing the endpoints
	// create a vector from caster to target
	vector vCaster = GetPosition(OBJECT_SELF);
	//SendMessageToPC(OBJECT_SELF, "CastPos: " + VectorToString(vCaster));
	vector vTarget = GetPositionFromLocation(lTarget);
	//SendMessageToPC(OBJECT_SELF, "TargetPos: " + VectorToString(vTarget));
	vector vCastToTarg;
	vector vNormCastTarg;
	vector vLeftEnd;	// might actually be the right end in reality, it doesn't really matter
	vector vRightEnd;
	vCastToTarg.x = vTarget.x - vCaster.x;
	vCastToTarg.y = vTarget.y - vCaster.y;
	vNormCastTarg = VectorNormalize(vCastToTarg);
	//SendMessageToPC(OBJECT_SELF, "vNormalized: " + VectorToString(vNormCastTarg));
	// generate perpendiculars, mult them by 5 (1/2 width of wall), add them to center of wall
	vLeftEnd.x = -vNormCastTarg.y * 5.0 + vTarget.x;	// assuming hard-coded wall length of 10
	vLeftEnd.y = vNormCastTarg.x * 5.0 + vTarget.y;
	vRightEnd.x = vNormCastTarg.y * 5.0 + vTarget.x;
	vRightEnd.y = -vNormCastTarg.x * 5.0 + vTarget.y;
	//SendMessageToPC(OBJECT_SELF, "vLeftEnd: " + VectorToString(vLeftEnd));
	//SendMessageToPC(OBJECT_SELF, "vRightEnd: " + VectorToString(vRightEnd));
	location lLeftEnd = Location(GetArea(OBJECT_SELF), vLeftEnd, 0.0);
	location lRightEnd = Location(GetArea(OBJECT_SELF), vRightEnd, 0.0);
	
	// create an object at the wall's target location that has 
	// the endpoints in it so we can script against them later
	object oWall = CreateObject(OBJECT_TYPE_PLACEABLE, "plc_ipoint ", lTarget, FALSE, "ShazForceWall");
	SetLocalLocation(oWall, "LeftEnd", lLeftEnd);
	SetLocalLocation(oWall, "RightEnd", lRightEnd);
	DestroyObject(oWall, RoundsToSeconds(nDuration) + 1.0f);
	
	effect eVis = EffectVisualEffect(VFX_DUR_SPELL_DISPLACEMENT);
	JXApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eVis, lLeftEnd, RoundsToSeconds(nDuration));
	JXApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eVis, lRightEnd, RoundsToSeconds(nDuration));
	
	// finally, do an arrow immunity check
	ShazForceWallDoArrowImmunity();
	
	JXPostSpellCastCode();
}