
#include "MW4Headers.hpp"

#include "MWDebugHelper.hpp"

#include "MWPlayer.hpp"
#include "Adept\NameTable.hpp"
#include "VehicleInterface.hpp"
#include "Vehicle.hpp"
#include "MWDamageObject.hpp"
#include "MWPlayer.hpp"
#include "AI_DebugRenderer.hpp"
#include "AI_FireStyle.hpp"
#include "AI_CombatTacticInterface.hpp"
#include "ablprofiler.hpp"
#include "rail_move.hpp"
#include "ai.hpp"
#include "moverai.hpp"
#include "combatai.hpp"
#include "mwmission.hpp"
#include "CameraShip.hpp"
#include "MWApplication.hpp"
#include "DamageDispatch.hpp"
#include "NonCom.hpp"

const unsigned int max_switch_entries = 39;

using namespace MW4AI;
using namespace MechWarrior4;


DebugHelper* DebugHelper::m_Instance = 0;
bool DebugHelper::m_AIOff = false;

DebugHelper::DebugHelper()
	: m_FollowCamera(false)
	, m_PlayerID(0xFFFFFFFF)
	, m_SwitchTo_Page(0)
	, m_BreakInCurrentVehicle(false)
{
	Verify(m_Instance == 0);
	m_Instance = this;

	AddDebuggerMenuItem("Libraries\\AI\\Show AI Stats",
						Check_ShowAIStats,
						Activate_ShowAIStats,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\Show AI Stats (Current Vehicle Only)",
						Check_ShowAIStatsCurrentVehicleOnly,
						Activate_ShowAIStatsCurrentVehicleOnly,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\AI\\Show Damage Info",
						Check_ShowDamageInfo,
						Activate_ShowDamageInfo,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\Disable AI",
						Checked_DisableAI,
						Activate_DisableAI,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\Disable Combat Movement",
						Checked_DisableCombatMovement,
						Activate_DisableCombatMovement,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\Disable Combat Firing",
						Checked_DisableCombatFiring,
						Activate_DisableCombatFiring,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\AI\\Show Num Path Requests",
						Check_ShowNumPathRequests,
						Activate_ShowNumPathRequests,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\AI\\Show Movement Lines",
						Check_ShowMovementLines,
						Activate_ShowMovementLines,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\Show Movement Paths",
						Check_ShowMovementPaths,
						Activate_ShowMovementPaths,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\AI\\Show Dead Reckoning (0.5s)",
						Check_ShowDeadReckoning05s,
						Activate_ShowDeadReckoning05s,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\Show Dead Reckoning (1s)",
						Check_ShowDeadReckoning1s,
						Activate_ShowDeadReckoning1s,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\Show Dead Reckoning (2s)",
						Check_ShowDeadReckoning2s,
						Activate_ShowDeadReckoning2s,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\Hide Dead Reckoning",
						Check_HideDeadReckoning,
						Activate_HideDeadReckoning,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\AI\\Global Invulnerability",
						Check_InvulnerabilityGlobal,
						Activate_InvulnerabilityGlobal,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\AI\\Ignore Current Vehicle",
						Check_IgnoreCurrentVehicle,
						Activate_IgnoreCurrentVehicle,
						0);

	AddDebuggerMenuItem("Libraries\\AI\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\AI\\Enable AI Heat Management",
						Check_EnableAIHeatManagement,
						Activate_EnableAIHeatManagement,
						0);


	AddDebuggerMenuItem("Libraries\\AI\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\AI\\Break In Current Vehicle",
						Checked_BreakInCurrentVehicle,
						Activate_BreakInCurrentVehicle,
						0);

	AddDebuggerMenuItem("Libraries\\ABL\\Profile",
						Check_ABLProfile,
						Activate_ABLProfile,
						0);

	AddDebuggerMenuItem("Libraries\\Camera\\In Mech",
						Check_CameraInMech,
						Activate_CameraInMech,
						0);

	AddDebuggerMenuItem("Libraries\\Camera\\External",
						Check_CameraExternal,
						Activate_CameraExternal,
						0);

	AddDebuggerMenuItem("Libraries\\Camera\\Top Down",
						Check_CameraTopDown,
						Activate_CameraTopDown,
						0);

	AddDebuggerMenuItem("Libraries\\Camera\\Top Down (Follow)",
						Check_CameraTopDownFollow,
						Activate_CameraTopDownFollow,
						0);

	AddDebuggerMenuItem("Libraries\\Camera\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\Camera\\Attach to Next",
						Check_CameraAttachToNext,
						Activate_CameraAttachToNext,
						0);

	AddDebuggerMenuItem("Libraries\\Switch To >>\\  NEXT PAGE -->",0,0,0,Activate_SwitchToNextPage);

	{for (int i = 0;
		  i < max_switch_entries;
		  ++i)
	{
		std::string s = GetMenuItemName(i);
		AddDebuggerMenuItem((char*)s.c_str(),0,0,0,SwitchCallback);
	}}

//	AddDebuggerMenuItem("Libraries\\Mech\\Pushup Off",
//						Check_TestPushupOff,
//						Activate_TestPushupOff,
//						0);

	
	AddDebuggerMenuItem("Libraries\\Mech\\Reset",0,
						Activate_TestReset,0);

	AddDebuggerMenuItem("Libraries\\Mech\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\Mech\\Arm Disabled",
						Check_TestArmActivation,
						Activate_TestArmActivation,
						0);

	AddDebuggerMenuItem("Libraries\\Mech\\ ",0,0,0);


	AddDebuggerMenuItem("Libraries\\Mech\\Left Gimp",
						Check_TestLeftGimp,
						Activate_TestLeftGimp,
						0);

	AddDebuggerMenuItem("Libraries\\Mech\\Right Gimp",
						Check_TestRightGimp,
						Activate_TestRightGimp,
						0);

	AddDebuggerMenuItem("Libraries\\Mech\\ ",0,0,0);


	AddDebuggerMenuItem("Libraries\\Mech\\Fall Forward",0,
						Activate_TestFallForward,0);

	AddDebuggerMenuItem("Libraries\\Mech\\Fall Backward",0,
						Activate_TestFallBackward,0);

	AddDebuggerMenuItem("Libraries\\Mech\\Fall Left",0,
						Activate_TestFallLeft,0);

	AddDebuggerMenuItem("Libraries\\Mech\\Fall Right",0,
						Activate_TestFallRight,0);


	AddDebuggerMenuItem("Libraries\\Mech\\ ",0,0,0);

	AddDebuggerMenuItem("Libraries\\Mech\\SpringHit1",0,
						Activate_TestHit1,0);
	AddDebuggerMenuItem("Libraries\\Mech\\SpringHit2",0,
						Activate_TestHit2,0);
	AddDebuggerMenuItem("Libraries\\Mech\\SpringHit3",0,
						Activate_TestHit3,0);
	AddDebuggerMenuItem("Libraries\\Mech\\SpringHit4",0,
						Activate_TestHit4,0);


	AddDebuggerMenuItem("Libraries\\Mech\\ ",0,0,0);
	
	AddDebuggerMenuItem("Libraries\\Mech\\Fast Stand Transition",Check_TestFastTransition,
						Activate_TestFastTransition,0);

	AddDebuggerMenuItem("Libraries\\Mech\\No Blend",Check_NoBlend,
						Activate_NoBlend,0);


	AddDebuggerMenuItem("Libraries\\Mech\\ ",0,0,0);
	
	AddDebuggerMenuItem("Libraries\\Mech\\Leave Torso Alone",Check_AlwaysLeaveTorsoAlone,
						Activate_AlwaysLeaveTorsoAlone,0);
	AddDebuggerMenuItem("Libraries\\Mech\\Always Center Torso",Check_AlwaysCenterTorsoToLegs,
						Activate_AlwaysCenterTorsoToLegs,0);
	AddDebuggerMenuItem("Libraries\\Mech\\Always Center Legs To Torso",Check_AlwaysCenterLegsToTorso,
						Activate_AlwaysCenterLegsToTorso,0);

	AddDebuggerMenuItem("Libraries\\InputTrainer\\Throttle Enabled",Check_ThrottleEnabled,Activate_ThrottleEnabled,0);
	AddDebuggerMenuItem("Libraries\\InputTrainer\\Turn Enabled",Check_TurnEnabled,Activate_TurnEnabled,0);
	AddDebuggerMenuItem("Libraries\\InputTrainer\\Torso Enabled",Check_TorsoEnabled,Activate_TorsoEnabled,0);
	AddDebuggerMenuItem("Libraries\\InputTrainer\\Firing Enabled",Check_FiringEnabled,Activate_FiringEnabled,0);
	AddDebuggerMenuItem("Libraries\\InputTrainer\\View Modes Enabled",Check_ViewModesEnabled,Activate_ViewModesEnabled,0);
	AddDebuggerMenuItem("Libraries\\InputTrainer\\Target Selection Enabled",Check_TargetSelectionEnabled,Activate_TargetSelectionEnabled,0);
	AddDebuggerMenuItem("Libraries\\InputTrainer\\Zoom Enabled",Check_ZoomEnabled,Activate_ZoomEnabled,0);
	AddDebuggerMenuItem("Libraries\\InputTrainer\\Heat Enabled",Check_HeatEnabled,Activate_HeatEnabled,0);
	AddDebuggerMenuItem("Libraries\\InputTrainer\\Nav Switching Enabled",Check_NavSwitchingEnabled,Activate_NavSwitchingEnabled,0);
	AddDebuggerMenuItem("Libraries\\InputTrainer\\Misc Enabled",Check_MiscEnabled,Activate_MiscEnabled,0);
}

DebugHelper::~DebugHelper()
{
	Verify(m_Instance != 0);
	m_Instance = 0;
}

VehicleInterface* GetVehicleInterface()
{
	if (NameTable::GetInstance() == 0)
	{
		return (0);
	}

	if (Adept::Player::GetInstance() == NULL)
	{
		return (0);
	}

	MWPlayer* p = Cast_Object(MWPlayer*,Adept::Player::GetInstance());
	Check_Object(p);
	
	if (p->GetInterface() == NULL)
	{
		return (0);
	}

	return (p->GetInterface());
}

Vehicle* GetVehicle()
{
	if (GetVehicleInterface() == 0)
	{
		return (0);
	}

	return (GetVehicleInterface()->vehicle);
}

Mech* GetMech()
{
	Vehicle* v = GetVehicle();

	if (v == 0)
	{
		return (0);
	}

	if (v->IsDerivedFrom(Mech::DefaultData) == true)
	{
		return (Cast_Object(Mech*,v));
	}

	return (0);
}

void DebugHelper::Execute()
{
	


	if ((m_FollowCamera == true) &&
		(Check_CameraTopDownFollow() == true) &&
		(GetVehicle() != 0))
	{
		NetMissionParameters::MWNetMissionParameters *params = MWApplication::GetInstance()->GetLocalNetParams();
		if (!params->m_allow3rdPerson)
			return;

		Point3D camera = GetVehicleInterface()->cameraOffset;
		Point3D vehicle = (Point3D)GetVehicle()->GetLocalToWorld();

		camera.x = vehicle.x;
		camera.z = vehicle.z;

		GetVehicleInterface()->SetCameraMode(VehicleInterface::TopDownCameraView,&camera);
	}
}

DebugHelper* DebugHelper::GetInstance()
{
	return (m_Instance);
}

bool __stdcall DebugHelper::Check_CameraInMech()
{
	if (GetVehicleInterface() == 0)
	{
		return (false);
	}

	return (GetVehicleInterface()->cameraView == VehicleInterface::InMechCameraView);
}

void __stdcall DebugHelper::Activate_CameraInMech()
{
	if (GetVehicleInterface() != 0)
	{
		GetVehicleInterface()->SetCameraMode(VehicleInterface::InMechCameraView);
	}
}

bool __stdcall DebugHelper::Check_CameraExternal()
{
	if (GetVehicleInterface() == 0)
	{
		return (false);
	}

	return (GetVehicleInterface()->cameraView == VehicleInterface::ExternalCameraAttachedView);
}

void __stdcall DebugHelper::Activate_CameraExternal()
{
	if (GetVehicleInterface() != 0)
	{
		NetMissionParameters::MWNetMissionParameters *params = MWApplication::GetInstance()->GetLocalNetParams();
		if (!params->m_allow3rdPerson)
			return;


		GetVehicleInterface()->SetCameraMode(VehicleInterface::ExternalCameraAttachedView);
	}
}

bool __stdcall DebugHelper::Check_CameraTopDown()
{
	if (GetVehicleInterface() == 0)
	{
		return (false);
	}

	if ((GetVehicleInterface()->cameraView == VehicleInterface::TopDownCameraView) &&
		(GetInstance()->m_FollowCamera == false))
	{
		return (true);
	}

	return (false);
}

void __stdcall DebugHelper::Activate_CameraTopDown()
{
	if (GetVehicleInterface() != 0)
	{
		NetMissionParameters::MWNetMissionParameters *params = MWApplication::GetInstance()->GetLocalNetParams();
		if (!params->m_allow3rdPerson)
			return;

		Stuff::Point3D p = (Point3D)GetVehicle()->GetLocalToWorld();
		p.y = 200.0f;
		GetVehicleInterface()->SetCameraMode(VehicleInterface::TopDownCameraView,&p);
		GetInstance()->m_FollowCamera = false;
	}
}

bool __stdcall DebugHelper::Check_CameraTopDownFollow()
{
	if (GetVehicleInterface() == 0)
	{
		return (false);
	}

	if ((GetVehicleInterface()->cameraView == VehicleInterface::TopDownCameraView) &&
		(GetInstance()->m_FollowCamera == true))
	{
		return (true);
	}

	return (false);
}

void __stdcall DebugHelper::Activate_CameraTopDownFollow()
{
	if (GetVehicleInterface() != 0)
	{
		NetMissionParameters::MWNetMissionParameters *params = MWApplication::GetInstance()->GetLocalNetParams();
		if (!params->m_allow3rdPerson)
			return;

		Stuff::Point3D p = (Point3D)GetVehicle()->GetLocalToWorld();
		p.y = 200.0f;
		GetVehicleInterface()->SetCameraMode(VehicleInterface::TopDownCameraView,&p);
		GetInstance()->m_FollowCamera = true;
	}
}

bool __stdcall DebugHelper::Check_InvulnerabilityGlobal()
{
	return (DamageDispatch::GetGlobalInvulnerability());
}

void __stdcall DebugHelper::Activate_InvulnerabilityGlobal()
{
	if (DamageDispatch::GetGlobalInvulnerability())
	{
		DamageDispatch::SetGlobalInvulnerability(false);
	}
	else
	{
		DamageDispatch::SetGlobalInvulnerability(true);
	}
}

bool __stdcall DebugHelper::Check_CameraAttachToNext()
{
	return (false);
}

void __stdcall DebugHelper::Activate_CameraAttachToNext()
{
	if ((GetVehicle() == 0) ||
		(GetVehicleInterface() == 0))
	{
		return;
	}

	if (GetInstance()->m_PlayerID == 0xFFFFFFFF)
	{
		GetInstance()->m_PlayerID = GetVehicle()->objectID;
	}

	Adept::NameTable::NameTableEntryArray& vehicle_array = Adept::NameTable::GetInstance()->nameTableArray[Adept::NameTable::VehicleArray];

	int index = vehicle_array.GetLength();
	{for (int i = 0;
		  i < vehicle_array.GetLength();
		  ++i)
	{
		if (vehicle_array[i].GetObjectID() == GetInstance()->m_PlayerID)
		{
			index = i + 1;

			if (index >= vehicle_array.GetLength())
			{
				index = 0;
			}

			if (index < vehicle_array.GetLength())
			{
				Adept::Entity* entity = vehicle_array[index].dataPointer->GetCurrent();

				while ((index < vehicle_array.GetLength()) &&
					   ((entity == 0) ||
					    (entity->IsDerivedFrom(Vehicle::DefaultData) == false)))
				{
					++index;
					if (index < vehicle_array.GetLength())
					{
						entity = vehicle_array[index].dataPointer->GetCurrent();
					}
					else
					{
						index = 0;
					}

					if (index == i)
					{
						return;
					}
				}
			}

			break;
		}
	}}

	if ((index >= vehicle_array.GetLength()) ||
		(vehicle_array[index].dataPointer->GetCurrent()->IsDerivedFrom(Vehicle::DefaultData) == false))
	{
		return;
	}

	Vehicle* v = Cast_Object(Vehicle*,vehicle_array[index].dataPointer->GetCurrent());
	Check_Object(v);
	SwitchToObject(*v);
}

bool __stdcall DebugHelper::Check_IgnoreCurrentVehicle()
{
	if ((GetVehicleInterface()) &&
		(GetVehicleInterface()->vehicle))
	{
		return (GetVehicleInterface()->vehicle->GetTargetDesirability() == MWObject::desirability_never);
	}

	return (false);
}

void __stdcall DebugHelper::Activate_IgnoreCurrentVehicle()
{
	if ((GetVehicleInterface()) &&
		(GetVehicleInterface()->vehicle))
	{
		MWObject* v = GetVehicleInterface()->vehicle;

		Stuff::Scalar invuln = v->GetTargetDesirability();

		if (invuln == MWObject::desirability_never)
		{
			v->SetTargetDesirability(MWObject::desirability_invalid);
		}
		else
		{
			v->SetTargetDesirability(MWObject::desirability_never);
		}
	}
}

bool __stdcall DebugHelper::Checked_DisableAI ()
{
	return m_AIOff;
}

void __stdcall DebugHelper::Activate_DisableAI ()
{
	if (!MWMission::GetInstance())
		return;

	if (!m_AIOff)
	{
		int i,j;
		m_AIOff = true;
		int size;
		Check_Object (MWMission::GetInstance());
		MWMission *mission = Cast_Object (MWMission*,MWMission::GetInstance());

		mission->UnloadScript ();

		NameTable *table = NameTable::GetInstance();
		Check_Object (table);
		i = NameTable::AIArray;
		{
			size = table->nameTableArray[i].GetLength ();
			for (j=0;j<size;j++)
			{
				NameTableEntry *data;
				Entity *who;

				data = &table->nameTableArray [i][j];

				AI *ai;
				MoverAI *moverai;
				CombatAI *combatai;

				who = data->dataPointer->GetCurrent ();

				if (!who)
					continue;
				if (who->IsDerivedFrom (AI::DefaultData))
				{
					ai = Cast_Object (AI *,who);
					ai->UnloadScript ();
				}
				if (who->IsDerivedFrom (MoverAI::DefaultData))
				{
					moverai = Cast_Object (MoverAI *,who);
					moverai->ClearMoveOrder ();
				}
				if (who->IsDerivedFrom (CombatAI::DefaultData))
				{
					combatai = Cast_Object (CombatAI *,who);
					combatai->OrderStopAttacking ();
				}
			}
		}
	}
}

bool __stdcall DebugHelper::Check_ShowAIStats()
{
	if (DebugRenderer::GetInstance() == 0)
	{
		return (false);
	}

	return (DebugRenderer::GetInstance()->GetAIStatsRendering() == DebugRenderer::STATS_ALL);
}

void __stdcall DebugHelper::Activate_ShowAIStats()
{
	DebugRenderer* instance = DebugRenderer::GetInstance();

	if (instance == 0)
	{
		return;
	}

	if (instance->GetAIStatsRendering() != DebugRenderer::STATS_ALL)
	{
		instance->SetAIStatsRendering(DebugRenderer::STATS_ALL);
	}
	else
	{
		instance->SetAIStatsRendering(DebugRenderer::STATS_NONE);
	}
}

bool __stdcall DebugHelper::Check_ShowAIStatsCurrentVehicleOnly()
{
	if (DebugRenderer::GetInstance() == 0)
	{
		return (false);
	}

	return (DebugRenderer::GetInstance()->GetAIStatsRendering() == DebugRenderer::STATS_CURRENT_VEHICLE);
}

void __stdcall DebugHelper::Activate_ShowAIStatsCurrentVehicleOnly()
{
	DebugRenderer* instance = DebugRenderer::GetInstance();

	if (instance == 0)
	{
		return;
	}

	if (instance->GetAIStatsRendering() != DebugRenderer::STATS_CURRENT_VEHICLE)
	{
		instance->SetAIStatsRendering(DebugRenderer::STATS_CURRENT_VEHICLE);
	}
	else
	{
		instance->SetAIStatsRendering(DebugRenderer::STATS_NONE);
	}
}

bool __stdcall DebugHelper::Check_ShowDamageInfo()
{
	DebugRenderer* instance = DebugRenderer::GetInstance();

	if (instance == 0)
	{
		return (false);
	}

	return (instance->GetDamageInfoRendering());
}

void __stdcall DebugHelper::Activate_ShowDamageInfo()
{
	DebugRenderer* instance = DebugRenderer::GetInstance();

	if (instance == 0)
	{
		return;
	}

	if (instance->GetDamageInfoRendering() == true)
	{
		instance->SetDamageInfoRendering(false);
	}
	else
	{
		instance->SetDamageInfoRendering(true);
	}
}

bool __stdcall DebugHelper::Check_ShowMovementLines()
{
	DebugRenderer* instance = DebugRenderer::GetInstance();

	if (instance == 0)
	{
		return (false);
	}

	return (instance->GetMovementLineRendering());
}

void __stdcall DebugHelper::Activate_ShowMovementLines()
{
	DebugRenderer* instance = DebugRenderer::GetInstance();

	if (instance == 0)
	{
		return;
	}

	if (instance->GetMovementLineRendering() == true)
	{
		instance->SetMovementLineRendering(false);
	}
	else
	{
		instance->SetMovementLineRendering(true);
	}
}

bool __stdcall DebugHelper::Check_ShowMovementPaths()
{
	DebugRenderer* instance = DebugRenderer::GetInstance();

	if (instance == 0)
	{
		return (false);
	}

	return (instance->GetMovementPathRendering());
}

void __stdcall DebugHelper::Activate_ShowMovementPaths()
{
	DebugRenderer* instance = DebugRenderer::GetInstance();

	if (instance == 0)
	{
		return;
	}

	if (instance->GetMovementPathRendering() == true)
	{
		instance->SetMovementPathRendering(false);
	}
	else
	{
		instance->SetMovementPathRendering(true);
	}
}

DWORD __stdcall DebugHelper::SwitchCallback(char* name, DWORD function)
{
	if ((GetInstance() == 0) ||
		(GetVehicle() == 0))
	{
		return (0);
	}

	gos_PushCurrentHeap(Heap);

	unsigned int index = GetMenuItemIndex(name);

	switch (function)
	{
		case gosMenu_Name:
		{
			GetInstance()->m_PublicString = GetMenuItemName(index,false);
			GetInstance()->m_PublicString += " ";
			GetInstance()->m_PublicString += GetObjectName(index);
			gos_PopCurrentHeap();

			return (DWORD)(GetInstance()->m_PublicString.c_str());		// Override name
		}
		case gosMenu_Activated:
		{
			SwitchToObject(index);

			gos_PopCurrentHeap();
			return 1;			
		}
	}

	gos_PopCurrentHeap();

	return (0);
}

std::string DebugHelper::GetMenuItemName(unsigned int index, bool fIncludePrefix)
{
	Verify(index < 100);

	std::string rv;
	
	if (fIncludePrefix == true)
	{
		rv = GetSwitchToPrefix();
	}

	char s[80];
	if (index < 10)
	{
		sprintf(s,"0%d",index);
	}
	else
	{
		sprintf(s,"%d",index);
	}

	rv += s;
	return (rv);
}

unsigned int DebugHelper::GetMenuItemIndex(const std::string& name)
{
	Verify(name.size() >= GetSwitchToPrefix().size() + 2);

	std::string n = name;
	n.erase(0,GetSwitchToPrefix().size());

	return (10 * (n[0] - '0')) + (n[1] - '0');
}

std::string DebugHelper::GetSwitchToPrefix()
{
	return ("Libraries\\Switch To >>\\");
}

unsigned int DebugHelper::GetNumObjectNames()
{
	Adept::NameTable::NameTableEntryArray& vehicle_array = Adept::NameTable::GetInstance()->nameTableArray[Adept::NameTable::VehicleArray];

	int count = 0;

	{for (int i = 0;
		  i < vehicle_array.GetLength();
		  ++i)
	{
		Adept::Entity* entity = vehicle_array[i].dataPointer->GetCurrent();

		if ((entity != 0) &&
			(entity->IsDerivedFrom(Vehicle::DefaultData) == true))
		{
			++count;
		}
	}}

	Adept::NameTable::NameTableEntryArray& cultural_array = Adept::NameTable::GetInstance()->nameTableArray[Adept::NameTable::CulturalArray];

	{for (int i = 0;
		  i < cultural_array.GetLength();
		  ++i)
	{
		Adept::Entity* entity = cultural_array[i].dataPointer->GetCurrent();

		if ((entity != 0) &&
			(entity->IsDerivedFrom(NonCom::DefaultData) == true))
		{
			++count;
		}
	}}


	if (count > max_switch_entries)
	{
		return (max_switch_entries);
	}

	return (count);
}

std::string DebugHelper::GetObjectName(unsigned int index)
{
	Adept::NameTable::NameTableEntryArray& vehicle_array = Adept::NameTable::GetInstance()->nameTableArray[Adept::NameTable::VehicleArray];
	Adept::NameTable::NameTableEntryArray& cultural_array = Adept::NameTable::GetInstance()->nameTableArray[Adept::NameTable::CulturalArray];

	int count = 0;
	index += GetInstance()->m_SwitchTo_Page * max_switch_entries;

	if (index >= vehicle_array.GetLength() + cultural_array.GetLength())
	{
		return ("");
	}

	{for (unsigned int i = 0;
		  i < vehicle_array.GetLength();
		  ++i)
	{
		Adept::Entity* entity = vehicle_array[i].dataPointer->GetCurrent();

		if ((entity != 0) &&
			(entity->IsDerivedFrom(Vehicle::DefaultData) == true))
		{
			if (count == index)
			{
				return (entity->instanceName);
			}

			++count;
		}
	}}

	{for (unsigned int i = 0;
		  i < cultural_array.GetLength();
		  ++i)
	{
		Adept::Entity* entity = cultural_array[i].dataPointer->GetCurrent();

		if ((entity != 0) &&
			(entity->IsDerivedFrom(NonCom::DefaultData) == true))
		{
			if (count == index)
			{
				return (entity->instanceName);
			}

			++count;
		}
	}}

	return ("");
}

void DebugHelper::SwitchToObject(unsigned int index)
{
	Adept::NameTable::NameTableEntryArray& vehicle_array = Adept::NameTable::GetInstance()->nameTableArray[Adept::NameTable::VehicleArray];
	Adept::NameTable::NameTableEntryArray& cultural_array = Adept::NameTable::GetInstance()->nameTableArray[Adept::NameTable::CulturalArray];

	int count = 0;
	index += GetInstance()->m_SwitchTo_Page * max_switch_entries;

	if (index >= vehicle_array.GetLength() + cultural_array.GetLength())
	{
		return;
	}

	{for (unsigned int i = 0;
		  i < vehicle_array.GetLength();
		  ++i)
	{
		Adept::Entity* entity = vehicle_array[i].dataPointer->GetCurrent();

		if ((entity != 0) &&
			(entity->IsDerivedFrom(Vehicle::DefaultData) == true))
		{
			if (count == index)
			{
				Vehicle* v = Cast_Object(Vehicle*,entity);

				SwitchToObject(*v);
				return;
			}

			++count;
		}
	}}

	{for (unsigned int i = 0;
		  i < cultural_array.GetLength();
		  ++i)
	{
		Adept::Entity* entity = cultural_array[i].dataPointer->GetCurrent();

		if ((entity != 0) &&
			(entity->IsDerivedFrom(NonCom::DefaultData) == true))
		{
			if (count == index)
			{
				MWObject* v = Cast_Object(MWObject*,entity);

				SwitchToObject(*v);
				return;
			}

			++count;
		}
	}}
}

void DebugHelper::SwitchToObject(MWObject& v)
{
	Check_Object(&v);

	// no one is allowed to switch unless they are the server
	if ((!Network::GetInstance()->AmIServer()) ||
		(CameraShipManager::Instance == 0))
		return;

	NetMissionParameters::MWNetMissionParameters *params = MWApplication::GetInstance()->GetLocalNetParams();
	if (!params->m_allow3rdPerson)
		return;

	GetInstance()->m_PlayerID = v.objectID;

	Stuff::UnitVector3D unit_backward;
	v.GetLocalToWorld().GetLocalBackwardInWorld(&unit_backward);

	Stuff::Point3D backward(unit_backward);
	backward *= 16;

	Stuff::LinearMatrix4D matrix(v.GetLocalToWorld());
	backward += (Stuff::Point3D)matrix;
	matrix.BuildTranslation(backward);

	GetVehicleInterface()->SetCameraMode(VehicleInterface::ExternalCameraDetachedView);
	GetVehicleInterface()->cameraOffset = matrix;
	GetVehicleInterface()->cameraOffset.y += 5;
	GetVehicleInterface()->cameraRotation = YawPitchRoll(matrix);
}

DWORD __stdcall DebugHelper::Activate_SwitchToNextPage(char* name, DWORD function)
{
	switch (function)
	{
		case gosMenu_Activated:
		{
			const unsigned int max_pages = GetNumObjectNames() / max_switch_entries;

			if (GetInstance()->m_SwitchTo_Page >= max_pages)
			{
				GetInstance()->m_SwitchTo_Page = 0;
			}
			else
			{
				++(GetInstance()->m_SwitchTo_Page);
			}

			return 1;			
		}
	}

	return 0;
}

bool __stdcall DebugHelper::Check_ShowDeadReckoning05s()
{
	if (DebugRenderer::GetInstance() == 0)
	{
		return (false);
	}

	return (DebugRenderer::GetInstance()->GetDeadReckoningRenderTime() == 0.5f);
}

void __stdcall DebugHelper::Activate_ShowDeadReckoning05s()
{
	if (DebugRenderer::GetInstance() != 0)
	{
		DebugRenderer::GetInstance()->SetDeadReckoningRenderTime(0.5f);
	}
}

bool __stdcall DebugHelper::Check_ShowDeadReckoning1s()
{
	if (DebugRenderer::GetInstance() == 0)
	{
		return (false);
	}

	return (DebugRenderer::GetInstance()->GetDeadReckoningRenderTime() == 1);
}

void __stdcall DebugHelper::Activate_ShowDeadReckoning1s()
{
	if (DebugRenderer::GetInstance() != 0)
	{
		DebugRenderer::GetInstance()->SetDeadReckoningRenderTime(1);
	}
}

bool __stdcall DebugHelper::Check_ShowDeadReckoning2s()
{
	if (DebugRenderer::GetInstance() == 0)
	{
		return (false);
	}

	return (DebugRenderer::GetInstance()->GetDeadReckoningRenderTime() == 2);
}

void __stdcall DebugHelper::Activate_ShowDeadReckoning2s()
{
	if (DebugRenderer::GetInstance() != 0)
	{
		DebugRenderer::GetInstance()->SetDeadReckoningRenderTime(2);
	}
}

bool __stdcall DebugHelper::Check_HideDeadReckoning()
{
	if (DebugRenderer::GetInstance() == 0)
	{
		return (false);
	}

	return (DebugRenderer::GetInstance()->GetDeadReckoningRenderTime() == 0);
}

void __stdcall DebugHelper::Activate_HideDeadReckoning()
{
	if (DebugRenderer::GetInstance() != 0)
	{
		DebugRenderer::GetInstance()->SetDeadReckoningRenderTime(0);
	}
}

bool __stdcall DebugHelper::Check_ShowNumPathRequests()
{
	if (DebugRenderer::GetInstance() != 0)
	{
		return (DebugRenderer::GetInstance()->GetShowNumPathRequests());
	}

	return (false);
}

void __stdcall DebugHelper::Activate_ShowNumPathRequests()
{
	if (DebugRenderer::GetInstance() != 0)
	{
		if (DebugRenderer::GetInstance()->GetShowNumPathRequests() == true)
		{
			DebugRenderer::GetInstance()->SetShowNumPathRequests(false);
		}
		else
		{
			DebugRenderer::GetInstance()->SetShowNumPathRequests(true);
		}
	}
}

bool __stdcall DebugHelper::Check_EnableAIHeatManagement()
{
	return (MW4AI::FireStyles::FireStyle::gHeatManagementEnabled == true);
}

void __stdcall DebugHelper::Activate_EnableAIHeatManagement()
{
	if (MW4AI::FireStyles::FireStyle::gHeatManagementEnabled == true)
	{
		MW4AI::FireStyles::FireStyle::gHeatManagementEnabled = false;
	}
	else
	{
		MW4AI::FireStyles::FireStyle::gHeatManagementEnabled = true;
	}
}

bool __stdcall DebugHelper::Checked_DisableCombatMovement()
{
	return (MW4AI::CombatTacticInterface::g_DisableMovement);
}

void __stdcall DebugHelper::Activate_DisableCombatMovement()
{
	if (MW4AI::CombatTacticInterface::g_DisableMovement == true)
	{
		MW4AI::CombatTacticInterface::g_DisableMovement = false;
	}
	else
	{
		MW4AI::CombatTacticInterface::g_DisableMovement = true;
	}
}

bool __stdcall DebugHelper::Checked_DisableCombatFiring()
{
	return (MW4AI::CombatTacticInterface::g_DisableFiring);
}

void __stdcall DebugHelper::Activate_DisableCombatFiring()
{
	if (MW4AI::CombatTacticInterface::g_DisableFiring == true)
	{
		MW4AI::CombatTacticInterface::g_DisableFiring = false;
	}
	else
	{
		MW4AI::CombatTacticInterface::g_DisableFiring = true;
	}
}

bool __stdcall DebugHelper::Check_TestArmActivation()
{
	if (GetMech() == 0)
	{
		return (false);
	}

	return (GetMech()->Test_GetArmActivation());
}

void __stdcall DebugHelper::Activate_TestArmActivation()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_ArmActivation();
	}
}

bool __stdcall DebugHelper::Check_TestPushupOff()
{
	if (GetMech() == 0)
	{
		return (false);
	}

	return (GetMech()->Test_GetPushupOff());
}

void __stdcall DebugHelper::Activate_TestPushupOff()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_PushupOff();
	}
}

bool __stdcall DebugHelper::Check_TestLeftGimp()
{
	if (GetMech() == 0)
	{
		return (false);
	}

	return (GetMech()->Test_GetLeftGimp());
}

void __stdcall DebugHelper::Activate_TestLeftGimp()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_LeftGimp();
	}
}

bool __stdcall DebugHelper::Check_TestRightGimp()
{
	if (GetMech() == 0)
	{
		return (false);
	}

	return (GetMech()->Test_GetRightGimp());
}

void __stdcall DebugHelper::Activate_TestRightGimp()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_RightGimp();
	}
}

void __stdcall DebugHelper::Activate_TestLeftArmRecoil()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_LeftArmRecoil();
	}
}

void __stdcall DebugHelper::Activate_TestRightGunRecoil()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_RightGunRecoil();
	}
}

void __stdcall DebugHelper::Activate_TestHit1()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_Hit1();
	}
}

void __stdcall DebugHelper::Activate_TestHit2()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_Hit2();
	}
}

void __stdcall DebugHelper::Activate_TestHit3()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_Hit3();
	}
}

void __stdcall DebugHelper::Activate_TestHit4()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_Hit4();
	}
}

void __stdcall DebugHelper::Activate_TestReset()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_Reset();
	}
}

void __stdcall DebugHelper::Activate_TestFallForward()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_FallForward();
	}
}

void __stdcall DebugHelper::Activate_TestFallBackward()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_FallBackward();
	}
}

void __stdcall DebugHelper::Activate_TestFallLeft()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_FallLeft();
	}
}

void __stdcall DebugHelper::Activate_TestFallRight()
{
	if (GetMech() != 0)
	{
		GetMech()->Test_FallRight();
	}
}

bool __stdcall DebugHelper::Checked_BreakInCurrentVehicle()
{
	if (GetInstance() == 0)
	{
		return (false);
	}

	return (GetInstance()->m_BreakInCurrentVehicle);
}

void __stdcall DebugHelper::Activate_BreakInCurrentVehicle()
{
	if (GetInstance() == 0)
	{
		return;
	}

	if (GetInstance()->m_BreakInCurrentVehicle == true)
	{
		GetInstance()->m_BreakInCurrentVehicle = false;
	}
	else
	{
		GetInstance()->m_BreakInCurrentVehicle = true;
	}
}

bool DebugHelper::ShouldBreakInCurrentVehicle() const
{
	return (m_BreakInCurrentVehicle);
}

bool __stdcall DebugHelper::Check_TestFastTransition()
{
	if (GetMech() == 0)
	{
		return (false);
	}

	return (GetMech()->GetFastTransition());
}

void __stdcall DebugHelper::Activate_TestFastTransition()
{
	if (GetMech() != 0)
	{
		GetMech()->FastTransition();
	}
}



bool __stdcall DebugHelper::Check_NoBlend()
{
	return Mech::DontLoadBlends;
	
}

void __stdcall DebugHelper::Activate_NoBlend()
{
	Mech::DontLoadBlends = !Mech::DontLoadBlends;
}




bool __stdcall DebugHelper::Check_AlwaysLeaveTorsoAlone()
{
	if (GetMech() == 0)
	{
		return (false);
	}
	if (GetMech()->vehicleInterface == 0)
	{
		return (false);
	}

	return (GetMech()->vehicleInterface->GetPerminateTorsoMode() == VehicleInterface::LeaveTorsoAlone);
}

void __stdcall DebugHelper::Activate_AlwaysLeaveTorsoAlone()
{
	if (GetMech() != 0)
	{
		if (GetMech()->vehicleInterface != 0)
		{
			GetMech()->vehicleInterface->SetPerminateTorsoMode(VehicleInterface::LeaveTorsoAlone);
		}
	}

}
		
bool __stdcall DebugHelper::Check_AlwaysCenterTorsoToLegs()
{
	if (GetMech() == 0)
	{
		return (false);
	}
	if (GetMech()->vehicleInterface == 0)
	{
		return (false);
	}

	return (GetMech()->vehicleInterface->GetPerminateTorsoMode() == VehicleInterface::CenterTorsoToLegs);
}

void __stdcall DebugHelper::Activate_AlwaysCenterTorsoToLegs()
{
	if (GetMech() != 0)
	{
		if (GetMech()->vehicleInterface != 0)
		{
			GetMech()->vehicleInterface->SetPerminateTorsoMode(VehicleInterface::CenterTorsoToLegs);
		}
	}
}
		
bool __stdcall DebugHelper::Check_AlwaysCenterLegsToTorso()
{
	if (GetMech() == 0)
	{
		return (false);
	}
	if (GetMech()->vehicleInterface == 0)
	{
		return (false);
	}

	return (GetMech()->vehicleInterface->GetPerminateTorsoMode() == VehicleInterface::CenterLegsToTorso);
}

void __stdcall DebugHelper::Activate_AlwaysCenterLegsToTorso()
{
	if (GetMech() != 0)
	{
		if (GetMech()->vehicleInterface != 0)
		{
			GetMech()->vehicleInterface->SetPerminateTorsoMode(VehicleInterface::CenterLegsToTorso);
		}
	}
}

bool __stdcall DebugHelper::Check_ABLProfile()
{
	if ((ABL::Profiler::Instance() != 0) &&
		(ABL::Profiler::Instance()->GetActive() == true))
	{
		return (true);
	}

	return (false);
}

void __stdcall DebugHelper::Activate_ABLProfile()
{
	if (ABL::Profiler::Instance() != 0)
	{
		if (ABL::Profiler::Instance()->GetActive() == true)
		{
			ABL::Profiler::Instance()->SetActive(false);
		}
		else
		{
			ABL::Profiler::Instance()->SetActive(true);
		}
	}
}

	void InputTrainer_ToggleEnabled(InputTrainer::InputType t)
	{
		if (InputTrainer::GetInstance()->GetEnabled(t) == true)
		{
			InputTrainer::GetInstance()->SetEnabled(t,false);
		}
		else
		{
			InputTrainer::GetInstance()->SetEnabled(t,true);
		}
	}

bool __stdcall DebugHelper::Check_ThrottleEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::THROTTLE));
}

void __stdcall DebugHelper::Activate_ThrottleEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::THROTTLE);
}

bool __stdcall DebugHelper::Check_TurnEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::TURN));
}

void __stdcall DebugHelper::Activate_TurnEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::TURN);
}

bool __stdcall DebugHelper::Check_TorsoEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::TORSO));
}

void __stdcall DebugHelper::Activate_TorsoEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::TORSO);
}

bool __stdcall DebugHelper::Check_FiringEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::FIRING));
}

void __stdcall DebugHelper::Activate_FiringEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::FIRING);
}

bool __stdcall DebugHelper::Check_ViewModesEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::VIEW_MODES));
}

void __stdcall DebugHelper::Activate_ViewModesEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::VIEW_MODES);
}

bool __stdcall DebugHelper::Check_TargetSelectionEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::TARGET_SELECTION));
}

void __stdcall DebugHelper::Activate_TargetSelectionEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::TARGET_SELECTION);
}

bool __stdcall DebugHelper::Check_ZoomEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::ZOOM));
}

void __stdcall DebugHelper::Activate_ZoomEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::ZOOM);
}

bool __stdcall DebugHelper::Check_HeatEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::HEAT));
}

void __stdcall DebugHelper::Activate_HeatEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::HEAT);
}

bool __stdcall DebugHelper::Check_NavSwitchingEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::NAV_SWITCHING));
}

void __stdcall DebugHelper::Activate_NavSwitchingEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::NAV_SWITCHING);
}

bool __stdcall DebugHelper::Check_MiscEnabled()
{
	return (InputTrainer::GetInstance()->GetEnabled(InputTrainer::MISC));
}

void __stdcall DebugHelper::Activate_MiscEnabled()
{
	InputTrainer_ToggleEnabled(InputTrainer::MISC);
}

