//::///////////////////////////////////////////////
//:: Wall of Force: On Enter
//:: shaz_sp_wall_of_forcea.nss
//:://////////////////////////////////////////////
/*
    Block creatures from passing thru the wall by "bouncing" them back away from it
*/
//:://////////////////////////////////////////////
//:: Created By: Ken Cockerham
//:: Created On: 4/30/08
//:://////////////////////////////////////////////

#include "jx_inc_magic"
#include "X0_I0_SPELLS"
#include "x2_inc_spellhook"

void main()
{

    //Declare major variables
    int nCasterLvl = JXGetCasterLevel(OBJECT_SELF);
    int nMetaMagic = JXGetMetaMagicFeat();
    int nDamage;
    effect eDam;
    object oEnterer;
    //Declare and assign personal impact visual effect.
    effect eVis = EffectVisualEffect(VFX_HIT_SPELL_EVOCATION);
    //Capture the first target object in the shape.
    oEnterer = GetEnteringObject();
    
	//JXApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oEnterer);
	
	ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oEnterer);
	
	SendMessageToPC(oEnterer, "You smack into an invisible wall!");
	
	// bounce them back
	// first we have to generate a vector for a direction to send them
	// one of the vectors perpendicular to the wall will be appropriate
	// but to find out which, we'll have to do a dot product
	object oWall = GetNearestObjectByTag("ShazForceWall");
	location lWallLeft = GetLocalLocation(oWall, "LeftEnd");
	location lWallRight = GetLocalLocation(oWall, "RightEnd");
	vector vWallLeft = GetPositionFromLocation(lWallLeft);
	vector vWallRight = GetPositionFromLocation(lWallRight);
	vector vWallLR;
	vector vWallPerp1;
	vector vWallPerp2;
	vWallLR.x = vWallRight.x - vWallLeft.x;
	vWallLR.y = vWallRight.y - vWallLeft.y;
	
	vWallPerp1.x = -vWallLR.y;
	vWallPerp1.y = vWallLR.x;
	vWallPerp2.x = vWallLR.y;
	vWallPerp2.y = -vWallLR.x;
	
	vector vEnterer = GetPosition(oEnterer);
	vector vWallLeftToEnterer;
	
	vWallLeftToEnterer.x = vEnterer.x - vWallLeft.x;
	vWallLeftToEnterer.y = vEnterer.y - vWallLeft.y;
	
	float fDot;
	
	fDot = vWallLeftToEnterer.x * vWallPerp1.x + vWallLeftToEnterer.y * vWallPerp1.y;
	
	if(fDot > 0.0f) {
		// Perp1 faces towards enterer
		vEnterer.x = vEnterer.x + (vWallPerp1.x / 4.0f);
		vEnterer.y = vEnterer.y + (vWallPerp1.y / 4.0f);
	} else {
		// Perp2 faces towards enterer
		vEnterer.x = vEnterer.x + (vWallPerp2.x / 4.0f);
		vEnterer.y = vEnterer.y + (vWallPerp2.y / 4.0f);
	}
	
	location vNewLoc = Location(GetArea(oEnterer), vEnterer, GetFacing(oEnterer));
	
	AssignCommand(oEnterer, ClearAllActions());
	AssignCommand(oEnterer, ActionJumpToLocation(vNewLoc));
	
//	if (!GetIsObjectValid(JXGetAOERealCreator()))
//    {
//        DestroyObject(OBJECT_SELF);
//      return;
//  }
//	// Here is an attempt to let the AI know they've been messed with. Hopefully since I'm masking the wall
//	// as an entangle spell, they will realize they can't progress and do something else
//	// (probably shooting the wall instead, but its better than walking into it over and over) ;-)
	// This doesn't work, first of all they keep moving, second of all they walk right thru the wall!
	// I think it wipes out their command stack or something, thus destroying the "jumps"... oh well...
	//SignalEvent(oEnterer, EventSpellCastAt(JXGetAOERealCreator(), SPELL_ENTANGLE));
}