//////////////////////////////////////////////////
//	Author: Drammel								//
//	Date: 1/13/2009								//
//	Name: launch_martial_menu					//
//	Description: Launch the menu that displays 	//
// 	the icons.  Also runs combat data functions.//
//////////////////////////////////////////////////

#include "bot9s_inc_constants"
#include "bot9s_include"

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

	if (!GetIsObjectValid(oToB)) //Error Prevention
	{
		DelayCommand(0.1f, RunIsEncounterActive());
		return;
	}

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

	if ((GetIsInCombat(oPC)) && (GetLocalInt(oToB, "Encounter") == 0))
	{
		SetLocalInt(oToB, "Encounter", 1);
		DelayCommand(6.0f, SetLocalInt(oToB, "Encounter", 2));
		
		if (GetHasFeat(FEAT_VITAL_RECOVERY, oPC))
		{
			SetLocalInt(oToB, "VitalRecovery", 0);
		}

		if (GetHasFeat(FEAT_DW_ADEPT, oPC))
		{
			string sPhoenix = GetFirstName(oPC) + "sa_phoenix";
			object oPhoenix = GetObjectByTag(sPhoenix);

			if (GetIsObjectValid(oPhoenix))
			{
				SetLocalInt(oToB, "RisingPhoenix", 0);
			}
		}

		DelayCommand(6.0f, RunIsEncounterActive());
	}
	else if ((GetIsInCombat(oPC)) && (GetLocalInt(oToB, "Encounter") == 3))
	{
		SetLocalInt(oToB, "Encounter", 1);
		DelayCommand(6.0f, SetLocalInt(oToB, "Encounter", 2));
		DelayCommand(6.0f, RunIsEncounterActive());
	}
	else if ((!GetIsInCombat(oPC)) && (GetLocalInt(oToB, "Encounter") == 2) && (GetCurrentHitPoints(oPC) < 1))
	{
		DelayCommand(6.0f, RunIsEncounterActive());
	}
	else if ((!GetIsInCombat(oPC)) && (GetLocalInt(oToB, "Encounter") == 2))
	{   // Prevents Hide in Plain Sight style cheese.
		SetLocalInt(oToB, "Encounter", 3);
		DelayCommand(RoundsToSeconds(3), SetLocalInt(oToB, "Encounter", 0));
		DelayCommand(6.0f, RunIsEncounterActive());
		SetLocalInt(oPC, "bot9s_StrikeTotal", 0);
	}
	else DelayCommand(0.1f, RunIsEncounterActive());
}

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

	SetLocalInt(oPC, "bot9s_PreloadPending", 1);

	if (GetIsObjectValid(oToB))
	{
		DeleteLocalInt(oPC, "bot9s_PreloadPending");
		/* Drammel's Note: It seems this prevents Martial Study from correctly loading the variables.
		if (GetLastRestEventType() == REST_EVENTTYPE_REST_FINISHED || GetLastRestEventType() == REST_EVENTTYPE_REST_CANCELLED)
		{
			return; //Only will run on levelup and entering the module.
		}*/

		if (GetLocalInt(oToB, "PreloadDone") == 0)
		{
			ExecuteScript("bot9s_preload", oPC); // Stores 2da data on oToB.
		}
	}
	else DelayCommand(0.1f, DoPreLoad());
}

void main()
{
	object oPC = OBJECT_SELF;
	object oMartialAdept = GetControlledCharacter(oPC); //Checking to prevent unwanted screens from popping on resting.
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);

	int nCrusader = GetLevelByClass(CLASS_TYPE_CRUSADER, oPC);
	int nSaint = GetLevelByClass(CLASS_TYPE_SAINT, oPC);
	int nSwordsage = GetLevelByClass(CLASS_TYPE_SWORDSAGE, oPC);
	int nWarblade = GetLevelByClass(CLASS_TYPE_WARBLADE, oPC);
	int nRest = GetLastRestEventType();

	if ((oPC == oMartialAdept) && (nCrusader > 0) && (GetLocalInt(oToB, "Unfettered") == 0))
	{
		DisplayGuiScreen(oPC, "SCREEN_MARTIAL_MENU_CR", FALSE, "martial_menu_cr.xml");
	}

	if ((nCrusader > 2) && (nRest == REST_EVENTTYPE_REST_FINISHED))
	{
		SetLocalInt(oToB, "ZealousSurgeUse", 1);
	}
	
	if ((oPC == oMartialAdept) && (nSaint > 0) && (GetLocalInt(oToB, "SublimeWay") == 0))
	{
		DisplayGuiScreen(oPC, "SCREEN_MARTIAL_MENU_SA", FALSE, "martial_menu_sa.xml");
	}
	
	if ((oPC == oMartialAdept) && (nSwordsage > 0) && (GetLocalInt(oToB, "DesertWind") == 0))
	{
		DisplayGuiScreen(oPC, "SCREEN_MARTIAL_MENU_SS", FALSE, "martial_menu_ss.xml");
	}

	if ((nSwordsage > 19) && (nRest == REST_EVENTTYPE_REST_FINISHED))
	{
		SetLocalInt(oToB, "DualBoost", 3);
	}

	if ((oPC == oMartialAdept) && (nWarblade > 0) && (GetLocalInt(oToB, "SupernalClarity") == 0))
	{
		DisplayGuiScreen(oPC, "SCREEN_MARTIAL_MENU_WB", FALSE, "martial_menu_wb.xml");
	}
	
	if ((oPC == oMartialAdept) && (GetHasFeat(FEAT_MARTIAL_STUDY, oPC)) && (GetLocalInt(oToB, "UmbralAwn") == 0))
	{
		DisplayGuiScreen(oPC, "SCREEN_MARTIAL_MENU", FALSE, "martial_menu.xml");
	}

	if (oPC == oMartialAdept)
	{
		DisplayGuiScreen(oPC, "SCREEN_SWIFT_ACTION", FALSE, "swift_action.xml");
	}

	DisplayGuiScreen(oPC, "SCREEN_WENDERSNAVEN", FALSE, "wendersnaven.xml"); //Used for debugging purposes.

	if (nRest == REST_EVENTTYPE_REST_FINISHED)
	{
		SetLocalInt(oToB, "Encounter", 0);

		if (GetHasFeat(FEAT_SUDDEN_RECOVERY, oPC))
		{
			SetLocalInt(oToB, "SuddenRecovery", 0);
		}
	}

	if ((GetHasFeat(FEAT_DW_ADEPT, oPC)) && (GetIsObjectValid(oToB)) && (GetLocalInt(oToB, "Encounter") == 0))
	{
		string sPhoenix = GetFirstName(oPC) + "sa_phoenix";
		object oPhoenix = GetObjectByTag(sPhoenix);

		if (GetIsObjectValid(oPhoenix))
		{
			SetLocalInt(oToB, "RisingPhoenix", 0);
		}
	}

	if (GetLocalInt(oPC, "bot9s_PreloadPending") == 0)
	{
		DoPreLoad();
	}

	if (GetLocalInt(oToB, "bot9s_background_data") == 0)
	{
	//	RunPCPositionPerRound(); //Depriciated
	//	RunPCPositionPerSecond();
		RunIsEncounterActive();
	//	RunScreenStatus(); Depriciated like crazy.  The newer, better version of this function is now gui_wendersnaven.
	}
}