//////////////////////////////////////////////////////
//	Author: Drammel									//
//	Date: 3/31/2009									//
//	Title: feat_dvt_bulwark							//
//	Description: If damaged with a melee attack,	//
//	gain +1 AC for 6 seconds.						//
//////////////////////////////////////////////////////

void DevotedBulwark()
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);
	object oAttacker = GetLastDamager();
	object oAttacked = GetAttackTarget(oAttacker);

	SetLocalInt(oToB, "DvtBulwarkActive", 1); //Loop status check.

	if ((oPC == oAttacked) && (GetDistanceBetween(oPC, oAttacker) <= FeetToMeters(8.0f)))
	{
		int nMyHp = GetLocalInt(oToB, "DevotedBulwarkHP");
		int nMyHp2 = GetLocalInt(oToB, "DevotedBulwarkHP2");

		if (nMyHp > nMyHp2)
		{
			effect eBulwark = EffectACIncrease(1);
			eBulwark = SupernaturalEffect(eBulwark);
			ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eBulwark, oPC, 6.0f);
		}
	}
	DelayCommand(6.0f, DevotedBulwark());
}

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

	SetLocalInt(oToB, "DevotedBulwarkHP", GetCurrentHitPoints());
	DelayCommand(5.7f, SetLocalInt(oToB, "DevotedBulwarkHP2", GetCurrentHitPoints()));
	// Window of .2 seconds to determine if the PC has lost Hit points in the round.
	DelayCommand(6.0f, RunDBHealth());
}

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

	if (GetLocalInt(oToB, "DvtBulwarkActive") == 1)
	{
		return; // Prevents duplicate checks.
	}
	else if (GetLocalInt(oToB, "DvtBulwarkActive") == 0)
	{
		RunDBHealth();
		DelayCommand(5.8f, DevotedBulwark());
	}
}