//////////////////////////////////////////////////////
//	Author: Drammel									//
//	Date: April Fool's Day 2009						//
//	Title: feat_wr_defense							//
//	Description: While in a White Raven stance if at//
//	least one ally is within 5 feet, gain +1 AC.	//
//	Additionally, while you wield a White Raven		//
//	weapon, they gain +1 AC.						//
//////////////////////////////////////////////////////

#include "bot9s_inc_constants"
#include "bot9s_inc_variables"

void WhiteRavenDefense()
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);

	SetLocalInt(oToB, "WRDefenseActive", 1); // Loop status check;
	
	int nStance = GetLocalInt(oToB, "Stance");
	int nStance2 = GetLocalInt(oToB, "Stance2");
	
	if (nStance == STANCE_BOLSTERING_VOICE || nStance2 == STANCE_BOLSTERING_VOICE
	|| nStance == STANCE_LEADING_THE_CHARGE || nStance2 == STANCE_LEADING_THE_CHARGE
	|| nStance == STANCE_PRESS_THE_ADVANTAGE || nStance2 == STANCE_PRESS_THE_ADVANTAGE
	|| nStance == STANCE_SWARM_TACTICS || nStance2 == STANCE_SWARM_TACTICS
	|| nStance == STANCE_TACTICS_OF_THE_WOLF || nStance2 == STANCE_TACTICS_OF_THE_WOLF)
	{
		float f8 = FeetToMeters(8.0f);
		float fRange = FeetToMeters(60.0f);
		float fDist;
		location lPC = GetLocation(oPC);
		object oFriend;

		oFriend = GetFirstObjectInShape(SHAPE_SPHERE, fRange, lPC, TRUE);
		
		while (GetIsObjectValid(oFriend))
		{
			fDist = GetDistanceBetween(oPC, oFriend);

			if ((fDist <= f8) && (!GetIsReactionTypeHostile(oFriend, oPC)) && (oFriend != oPC))
			{
				object oWeapon = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC);
				
				if (GetIsWhiteRavenWeapon(oWeapon) == TRUE)
				{
					effect eBuddyAC = EffectACIncrease(1);
					eBuddyAC = ExtraordinaryEffect(eBuddyAC);
					ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eBuddyAC, oFriend, 6.0f);
				}
				
				if (GetLocalInt(oToB, "WhiteRavenDefense") == 0) //Needed because there's often more than one party member.
				{
					SetLocalInt(oToB, "WhiteRavenDefense", 1);
					DelayCommand(5.99f, SetLocalInt(oToB, "WhiteRavenDefense", 0));
					
					effect eAC = EffectACIncrease(1);
					eAC = ExtraordinaryEffect(eAC);
					ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eAC, oPC, 6.0f);
				}
			}
			oFriend = GetNextObjectInShape(SHAPE_SPHERE, fRange, lPC, TRUE);
		}
	}
	DelayCommand(6.0f, WhiteRavenDefense());
}

void main()
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);

	if (GetLocalInt(oToB, "WRDefenseActive") == 1)
	{
		return; // Prevents duplicate checks.
	}
	else if (GetLocalInt(oToB, "WRDefenseActive") == 0)
	{
		WhiteRavenDefense();
	}
}