//////////////////////////////////////////////
//	Author: Drammel          	       		//
//	Date: 4/15/2009                    		//
//	Title: clf_ss_acbonus		   			//
//	Description: Grants a wisdom bonus to AC//
//	while the swordsage is wearing light or	//
//	less armor and has no shield equipped.	//
//////////////////////////////////////////////

#include "bot9s_inc_constants"
#include "nw_i0_spells"

void CheckSSAC()
{
	object oPC = OBJECT_SELF;
	int nWisdom = GetAbilityModifier(ABILITY_WISDOM);
	effect eAC = EffectACIncrease(nWisdom, AC_SHIELD_ENCHANTMENT_BONUS); // Valid vs flatfoot and we're not using a shield anyway.
	eAC = SupernaturalEffect(eAC);
	eAC = SetEffectSpellId(eAC, SPELL_FEAT_SSAC);

	effect eTest;
	int nSpell, nTest;

	eTest = GetFirstEffect(oPC);
	nTest = 0;

	while (GetIsEffectValid(eTest))
	{
		nSpell = GetEffectSpellId(eTest);

		if (nSpell == SPELL_FEAT_SSAC)
		{
			nTest = 2;
			RemoveEffect(oPC, eTest);
			eTest = GetFirstEffect(oPC);
		}
		else if (GetEffectType(eTest) == EFFECT_TYPE_ENTANGLE || GetEffectType(eTest) == EFFECT_TYPE_PARALYZE
		|| GetEffectType(eTest) == EFFECT_TYPE_PETRIFY || GetEffectType(eTest) == EFFECT_TYPE_DAZED
		|| GetEffectType(eTest) == EFFECT_TYPE_CUTSCENE_PARALYZE || GetEffectType(eTest) == EFFECT_TYPE_STUNNED
		|| GetEffectType(eTest) == EFFECT_TYPE_TIMESTOP)
		{
			nTest = 1;
			break;
		}
		else nTest = 0;

		if (nTest != 2)
		{
			eTest = GetNextEffect(oPC);
		}
	}

	if (nTest != 1)
	{
		ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eAC, oPC, 5.99f);
	}
}
	
void SwordsageAC()
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);
	object oArmor = GetItemInSlot(INVENTORY_SLOT_CHEST, oPC);
	object oOffhand = GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oPC);
	int nArmor = GetArmorRank(oArmor);
	int nOffhand = GetBaseItemType(oOffhand);

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

	if ((GetHasFeat(FEAT_MONK_AC_BONUS)) && (nArmor == ARMOR_RANK_NONE))
	{
		// Do nothing.
	}
	else if (nOffhand == BASE_ITEM_LARGESHIELD || nOffhand == BASE_ITEM_SMALLSHIELD
	|| nOffhand == BASE_ITEM_TOWERSHIELD)
	{
		// Do more nothing.
	}
	else if (nArmor <= ARMOR_RANK_LIGHT)
	{
		AssignCommand(oPC, CheckSSAC());
	}

	DelayCommand(6.0f, SwordsageAC());
}
	
void main()
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);

	if ((GetLocalInt(oPC, "SSACActive") == 1) || (GetLocalInt(oToB, "SSACActive") == 1))
	{
		return; // Prevents duplicate checks.
	}
	else if ((GetLocalInt(oPC, "SSACActive") == 0) && (GetLocalInt(oToB, "SSACActive") == 0)) //If oToB isn't valid it still returns zero.
	{
		SwordsageAC();
	}
}