//////////////////////////////////////////////
//	Author: Drammel							//
//	Date: 2/9/2009							//
//	Title: ct_ctrcharge						//
//	Description: Immeadiate Action; Redirect//
//	an oppenent who makes a charge at you.	//
//////////////////////////////////////////////

#include "bot9s_include"
#include "bot9s_inc_variables"
#include "x0_i0_enemy"
#include "bot9s_inc_maneuvers"

void CounterCharge()
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);
	
	object oFoe = IntToObject(GetLocalInt(oToB, "CounterCharge"));
	location lFoe = GetLocation(oFoe);
	location lFoeStart = GetLastLocationOfCreature(oFoe);
	float fRange = GetMeleeRange(oPC);
	float fThrow = FeetToMeters(10.0f);
	float fCharge = GetDistanceBetweenLocations(lFoeStart, lFoe);
	
	int nMySize = GetCreatureSize(oPC);
	int nFoeSize = GetCreatureSize(oFoe);
	int nMyStr = GetAbilityModifier(ABILITY_STRENGTH, oPC);
	int nMyDex = GetAbilityModifier(ABILITY_DEXTERITY, oPC);
	int nFoeStr = GetAbilityModifier(ABILITY_STRENGTH, oFoe);
	int nFoeDex = GetAbilityModifier(ABILITY_DEXTERITY, oFoe);
	
	if (nMySize > nFoeSize)
	{
		nMyStr = nMyStr + 4;
	}
	else if (nMySize < nFoeSize)
	{
		 nMyDex =  nMyDex + 4;
	}
	
	effect eFoeAttack = EffectAttackIncrease(4, EFFECT_TYPE_ENEMY_ATTACK_BONUS);
		
	if ((GetLocalInt(oToB, "Swift") == 0) && (GetCurrentHitPoints(oPC) > 0))
	{
		DelayCommand(0.1f, CounterCharge());
		
		if ((fCharge >= fThrow) && (GetDistanceToObject(oFoe) <= fRange))
		{
			if (nMyStr >= nFoeStr)
			{
				if ((nMyStr + d20(1)) >= (nFoeStr+ d20(1)))
				{
					ThrowTarget(oFoe, fThrow);
					DelayCommand(0.1f, FloatingTextStringOnCreature("<color=cyan>*Counter Charge!*</color>", oPC, TRUE, 5.0f, COLOR_CYAN, COLOR_BLUE_DARK));
					RunSwiftAction(209, "C");
				}
				else
				{
					ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eFoeAttack, oFoe, 6.0f);
					DelayCommand(0.1f, FloatingTextStringOnCreature("<color=cyan>*Counter Charge Failed!*</color>", oPC, TRUE, 5.0f, COLOR_CYAN, COLOR_BLUE_DARK));
					RunSwiftAction(209, "C");
				}
			}
			else if (nMyDex >= nFoeDex)
			{
				if ((nMyDex + d20(1)) >= (nFoeDex + d20(1)))
				{
					ThrowTarget(oFoe, fThrow);
					DelayCommand(0.1f, FloatingTextStringOnCreature("<color=cyan>*Counter Charge!*</color>", oPC, TRUE, 5.0f, COLOR_CYAN, COLOR_BLUE_DARK));
					RunSwiftAction(209, "C");
				}
				else
				{
					ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eFoeAttack, oFoe, 6.0f);
					DelayCommand(0.1f, FloatingTextStringOnCreature("<color=cyan>*Counter Charge Failed!*</color>", oPC, TRUE, 5.0f, COLOR_CYAN, COLOR_BLUE_DARK));
					RunSwiftAction(209, "C");
				}
			}
		}				
	}
}

void main()
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);
	object oTarget = IntToObject(GetLocalInt(oToB, "Target"));
	
	if (NotMyFoe("Target") == TRUE)
	{
		return;
	}
	
	int nTarget = GetLocalInt(oToB, "Target");

	location lTarget = GetLocation(oTarget);
	
	SetLocalInt(oToB, "CounterCharge", nTarget);
	SetLastLocationOfCreature(oTarget, lTarget, TRUE, 6.0f); //If it can't get to me in one round, by definition, it isn't a charge.
	ExpendManeuver(209, "C");
	CounterCharge();
}