#include "MW4Headers.hpp"

//sanghoon begin
#include "MWVideoRenderer.hpp"
//sanghoon end
#include "MWGUIManager.hpp"
#include "GUIWeaponManager.hpp"
#include "GUIRadarManager.hpp"
#include "GUILightAmp.hpp"
#include "GUIStaticView.hpp"
#include "Mech.hpp"
#include "Torso.hpp"
#include "HeatManager.hpp"
#include "Objective.hpp"
#include "MWApplication.hpp"
#include "ShellScriptHeaders.hpp"
#include "HUDScriptHeaders.hpp"
#include "VehicleInterface.hpp"
#include "jumpjet.hpp"
#include "flag.hpp"
#include "bucket.hpp"
#include "MWMission.hpp"

#include "hudcomp.hpp"
#include "hudcomp2.hpp"
#include "huddamage.hpp"
#include "hudweapon.hpp"
#include "hudtarg.hpp"
#include "hudchat.hpp"
#include "hudcomm.hpp"
#include "hudhelp.hpp"
#include "hudmap.hpp"
#include "hudobj.hpp"
#include "hudscore.hpp"
#include "hudtimer.hpp"
#include "huddebug.hpp"
#include "hudhelparrow.hpp"
#include "hudcamera.hpp"
#include "aiutils.hpp"

#include <Adept\NameTable.hpp>
#include <Adept\Application.hpp>
#include <Adept\GUIObject.hpp>
#include <Adept\CameraComponent.hpp>

#include <ElementRenderer\CameraElement.hpp>

#include <MLR\MLRClipper.hpp>

// jcem - begin
#include "ctcl.h"
extern bool g_bCOOP; // COin OPeration
// jcem - end

//#######################################################################
//###########################    MWGUIManager   #########################
//#######################################################################
	
__int64	tHUDTime,tHUDTextureTime,tHUDTextTime,tHUDFrameTime,tHUDLineTime,tHUDRectTime;
namespace MechWarrior4
{
	extern Scalar g_HUDHeatLevel,g_HUDPPCLevel;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MWGUIManager::MWGUIManager() :
	GUIManager(),
	m_ZoomVisible(false),
	weaponManager (NULL),
	lightAmp (NULL),
//	zoomLightAmp (NULL),
	staticViewManager (NULL),
	vehicleInterface (NULL),
	m_CurrentTarget (NULL),
  m_targetElement (NULL)
{
#if !defined(NO_TIMERS)
	AddStatistic( "Hud Time", "%", gos_timedata, (void*)&tHUDTime, 0 );
	AddStatistic( " +-- Texture Time", "%", gos_timedata, (void*)&tHUDTextureTime, 0 );
	AddStatistic( " +-- Text Time", "%", gos_timedata, (void*)&tHUDTextTime, 0 );
	AddStatistic( " +-- Frame Time", "%", gos_timedata, (void*)&tHUDFrameTime, 0 );
	AddStatistic( " +-- Line Time", "%", gos_timedata, (void*)&tHUDLineTime, 0 );
	AddStatistic( " +-- Rect Time", "%", gos_timedata, (void*)&tHUDRectTime, 0 );
#endif

	m_Vehicle = NULL;
	InterfaceZoomMode = false;
	gametype[0] = 0;

	weaponManager = new GUIWeaponManager();
	Register_Object(weaponManager);
	m_ShutdownMode = false;
	radarManager = NULL;
	m_ShutdownMode = false;
	hudnav = NULL;
	hudTargetArrow = NULL;
	hudTorsoBar = NULL;
	hudZoom = NULL;
	hudHeat = NULL;
	hudJump = NULL;
	hudCoolant = NULL;
	hudSpeed = NULL;
	hudDamage = NULL;
	hudTargetDamage = NULL;
	hudWeapon = NULL;
	hudReticle = NULL;
	hudChat = NULL;
	hudComm = NULL;
	hudHelp = NULL;
	hudMap = NULL;
	hudObjective = NULL;
	hudScore = NULL;
	hudTimer = NULL;
	hudHelpArrow = NULL;
	hudDebug = NULL;
	hudmp = NULL;
	hudCamera = NULL;

	lightAmp = new GUILightAmp();
//	zoomLightAmp = new GUILightAmp();
	staticViewManager = new GUIStaticView();
	
	m_TimerShowing = false;
	m_DebugShowing = false;
	m_HelpArrowShowing = false;

	m_currentWidthRes = Environment.screenWidth;

	weaponManager->guiManager = this;
//	targetReticule = NULL;
//	lockedTargetReticule = NULL;
	currentSpeedValue = 0.0f;
	torsoPitchValue = 0;
	torsoTwistValue = 0;
	m_isShown = SHOW_ALL;
	m_LightAmpShowing = false;
	m_torsoRotation = 0;
	m_targetWindowAlignment = 0;
//	m_zoomOn = 0;
//	font = gos_LoadFont("Assets\\Graphics\\arial8.tga", 0);

	m_targetXLocation = -1.0f;
	m_targetYLocation = -1.0f;
	m_targetHeight = 0.0f;
	m_targetWidth = 0.0f;
	m_playerFacing = 0;
	m_navPointFacing = 0;
	m_navPointRange = -1.0f;
	m_navPointName = (char *)gos_Malloc(MaxStringLength);
	Str_Copy(m_navPointName, "No Point Selected", MaxStringLength);

	reticleName = (char *)gos_Malloc(MaxStringLength);
	Str_Copy(reticleName, "No Target", MaxStringLength);
// MSL 5.02 SubSystems
	IFF_Jammer = false; //iff jammer d/n exist by default
	reticleAlignment = 0;
	reticleDistance = 1000.0f;
//	hudReticle->ReticleName (reticleName);
//	hudReticle->ReticleRange (reticleDistance);
//	hudWeapon->ReticleRange (reticleDistance);

//	isHeatDirty = NULL;
//	isCoolantDirty = NULL;
	damageArray = NULL;
	armorArray = NULL;

	m_targetDamageArray = NULL;
	m_targetArmorArray = NULL;
	m_targetTonage = -1.0f;
	m_targetWindowName = (char *)gos_Malloc(MaxStringLength);
	Str_Copy(m_targetWindowName, "No Name", MaxStringLength);

	ClearTargetIndicator();
	Check_Object(Application::GetInstance());

	heatValue = NULL;
	coolantValue = NULL;
	jumpValue = NULL;
	maxJumpValue = 0;

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MWGUIManager::~MWGUIManager()
{
	Unregister_Object(weaponManager);
	delete weaponManager;

	m_Components.clear ();
// end new components

	delete lightAmp;
//	delete zoomLightAmp;
	delete staticViewManager;

	gos_Free(reticleName);
	gos_Free(m_navPointName);
	gos_Free(m_targetWindowName);

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::Execute()
{
	Check_Object(this);
	Check_Object(weaponManager);
	Check_Object(radarManager);
	if (VehicleInterface::GetInstance()->brbMode)
		return;

	GUIManager::Execute();

	lightAmp->ChangeResolution();
	if(m_currentWidthRes != Environment.screenWidth)
	{
		m_currentWidthRes = Environment.screenWidth;
//		zoomLightAmp->ChangeResolution();
		staticViewManager->ChangeResolution();
	}

	if(m_isShown)
	{
	//	weaponManager->Draw();
//		radarManager->Draw();
		Check_Object(ObjectiveRenderer::Instance);
		ObjectiveRenderer::Instance->Execute();
	}
	else
	{
		ClearTargetIndicator();
	}
#ifdef _ICECAP
	StartCAPAll ();
#endif
	UpdateComponents ();
	RenderComponents ();
#ifdef _ICECAP
	StopCAPAll ();
#endif
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::SetWeaponMode(int weapon_mode,Weapon* weap)
{
	Check_Object(this);
	Check_Object(weaponManager);

	weaponManager->drawMode = weapon_mode;
	hudWeapon->SingleFire (weap);
}

void MWGUIManager::CreateComponents (void)
{
	Verify (radarManager == NULL);
	Verify (hudnav == NULL);
	Verify (hudTargetArrow == NULL);
	Verify (hudTorsoBar == NULL);
	Verify (hudZoom == NULL);
	Verify (hudHeat == NULL);
	Verify (hudJump == NULL);
	Verify (hudCoolant == NULL);
	Verify (hudSpeed == NULL);
	Verify (hudDamage == NULL);
	Verify (hudTargetDamage == NULL);
	Verify (hudWeapon == NULL);
	Verify (hudReticle == NULL);
	Verify (hudChat == NULL);
	Verify (hudComm == NULL);
	Verify (hudHelp == NULL);
	Verify (hudMap == NULL);
	Verify (hudObjective == NULL);
	Verify (hudScore == NULL);
	Verify (hudTimer == NULL);
	Verify (hudHelpArrow == NULL);
	Verify (hudDebug == NULL);
	Verify (hudmp == NULL);
	Verify (hudCamera == NULL);

// new hud components
	radarManager = new GUIRadarManager();
	Register_Object(radarManager);
	AddComponent (radarManager);

	hudnav = new HUDNav ();
	Register_Object(hudnav);
	AddComponent (hudnav);

	hudTargetArrow = new HUDTargetArrow ();
	Register_Object(hudTargetArrow);
	AddComponent (hudTargetArrow);

	hudTorsoBar = new HUDTorsoBar ();
	Register_Object(hudTorsoBar);
	AddComponent (hudTorsoBar);
	
	hudZoom = new HUDZoom ();
	Register_Object(hudZoom);
	AddComponent (hudZoom);
	hudZoom->Hide ();

	hudHeat = new HUDHeat ();
	Register_Object (hudHeat);
	AddComponent (hudHeat);

	hudJump = new HUDJump ();
	Register_Object (hudJump);
	AddComponent (hudJump);

	hudCoolant = new HUDCoolant ();
	Register_Object (hudCoolant);
	AddComponent (hudCoolant);
	
	hudSpeed = new HUDSpeed ();
	Register_Object (hudSpeed);
	AddComponent (hudSpeed);

	hudDamage = new HUDDamage ();
	Register_Object (hudDamage);
	AddComponent (hudDamage);

	hudTargetDamage = new HUDTargetDamage ();
	Register_Object (hudTargetDamage);
	AddComponent (hudTargetDamage);

	hudWeapon = new HUDWeapon ();
	Register_Object (hudWeapon);
	AddComponent (hudWeapon);
	hudWeapon->WeaponManager (weaponManager);

	hudComm = new HUDComm();
	Register_Object (hudComm);
	AddComponent (hudComm);

	hudReticle = new HUDReticle();
	Register_Object (hudReticle);
	AddComponent (hudReticle);

	hudChat = new HUDChat();
	Register_Object (hudChat);
	AddComponent (hudChat);

	hudHelp = new HUDHelp();
	Register_Object (hudHelp);
	AddComponent (hudHelp);

	hudMap = new HUDMap();
	Register_Object (hudMap);
	AddComponent (hudMap);

	hudObjective = new HUDObjective();
	Register_Object (hudObjective);
	AddComponent (hudObjective);
	
	hudScore = new HUDScore();
	Register_Object (hudScore);
	AddComponent (hudScore);
	
	hudTimer = new HUDTimer();
	Register_Object (hudTimer);
	AddComponent (hudTimer);
	
	hudHelpArrow = new HUDHelpArrow();
	Register_Object (hudHelpArrow);
	AddComponent (hudHelpArrow);
	
	hudDebug = new HUDDebug();
	Register_Object (hudDebug);
	AddComponent (hudDebug);
	
	hudmp = new HUDMP();
	Register_Object (hudmp);
	AddComponent (hudmp);

	hudCamera = new HUDCamera ();
	Register_Object (hudCamera);
	AddComponent (hudCamera);
}

void MWGUIManager::ClearComponents (void)
{
// new components
	if (radarManager)
	{
		Unregister_Object(radarManager);
		delete radarManager;
	}

	if (hudnav)
	{
		Unregister_Object(hudnav);
		delete hudnav;
	}

	if (hudTargetArrow)
	{
		Unregister_Object(hudTargetArrow);
		delete hudTargetArrow;
	}

	if (hudTorsoBar)
	{
		Unregister_Object(hudTorsoBar);
		delete hudTorsoBar;
	}

	if (hudZoom)
	{
		Unregister_Object(hudZoom);
		delete hudZoom;
	}

	if (hudHeat)
	{
		Unregister_Object(hudHeat);
		delete hudHeat;
	}

	if (hudJump)
	{
		Unregister_Object (hudJump);
		delete hudJump;
	}

	if (hudCoolant)
	{
		Unregister_Object (hudCoolant);
		delete hudCoolant;
	}

	if (hudSpeed)
	{
		Unregister_Object (hudSpeed);
		delete hudSpeed;
	}

	if (hudDamage)
	{
		Unregister_Object (hudDamage);
		delete hudDamage;
	}

	if (hudTargetDamage)
	{
		Unregister_Object (hudTargetDamage);
		delete hudTargetDamage;
	}

	if (hudWeapon)
	{
		Unregister_Object (hudWeapon);
		delete hudWeapon;
	}

	if (hudReticle)
	{
		Unregister_Object (hudReticle);
		delete hudReticle;
	}

	if (hudChat)
	{
		Unregister_Object (hudChat);
		delete hudChat;
	}

	if (hudComm)
	{
		Unregister_Object (hudComm);
		delete hudComm;
	}

	if (hudHelp)
	{
		Unregister_Object (hudHelp);
		delete hudHelp;
	}

	if (hudMap)
	{
		Unregister_Object (hudMap);
		delete hudMap;
	}

	if (hudObjective)
	{
		Unregister_Object (hudObjective);
		delete hudObjective;
	}

	if (hudScore)
	{
		Unregister_Object (hudScore);
		delete hudScore;
	}

	if (hudTimer)
	{
		Unregister_Object (hudTimer);
		delete hudTimer;
	}

	if (hudHelpArrow)
	{
		Unregister_Object (hudHelpArrow);
		delete hudHelpArrow;
	}

	if (hudDebug)
	{
		Unregister_Object (hudDebug);
		delete hudDebug;
	}

	if (hudmp)
	{
		Unregister_Object (hudmp);
		delete hudmp;
	}
	if (hudCamera)
	{
		Unregister_Object (hudCamera);
		delete hudCamera;
	}
// new components
	radarManager = NULL;
	hudnav = NULL;
	hudTargetArrow = NULL;
	hudTorsoBar = NULL;
	hudZoom = NULL;
	hudHeat = NULL;
	hudJump = NULL;
	hudCoolant = NULL;
	hudSpeed = NULL;
	hudDamage = NULL;
	hudTargetDamage = NULL;
	hudWeapon = NULL;
	hudReticle = NULL;
	hudChat = NULL;
	hudComm = NULL;
	hudHelp = NULL;
	hudMap = NULL;
	hudObjective = NULL;
	hudScore = NULL;
	hudTimer = NULL;
	hudHelpArrow = NULL;
	hudDebug = NULL;
	hudmp = NULL;
	hudCamera = NULL;
	m_Components.clear ();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::InitializeNewVehicle(Vehicle *vehicle)
{
	CreateComponents ();

	if (lightAmp)
		lightAmp->NewVehicle();
	m_Vehicle = vehicle;
	m_ShutdownMode = false;
	if (!vehicle)
	{
		maxSpeed = 0;
		Hide ();
//		isHeatDirty = NULL;
//		isCoolantDirty = NULL;
		jumpValue = NULL;
		maxJumpValue = 0;
		heatValue = NULL;
		coolantValue = NULL;
		damageArray = NULL;
		armorArray = NULL;
		Reset();
		return;
	}
	Check_Object(this);
	const Vehicle__GameModel *model = vehicle->GetGameModel();
	Check_Object(model);

	maxSpeed = model->maxSpeed;
	
	m_MapShowing = false;
	Torso *torso = vehicle->GetTorso();
	if(torso)
	{
		Check_Object(torso);
		const Torso__GameModel *torso_model = torso->GetGameModel();
		Check_Object(torso_model);

		maxTorsoTwist = torso_model->twistRadius;
		maxTorsoPitch = torso_model->pitchRadius;
	}
	
	jumpValue = NULL;
	maxJumpValue = 0;
	if(vehicle->IsDerivedFrom(Mech::DefaultData))
	{
		Mech *mech = Cast_Object(Mech *, vehicle);
		damageArray = mech->damageArray;
		armorArray = mech->armorArray;
		if (armorArray)
			hudDamage->ArmorValue (armorArray,true);
		if (mech->GetJumpJet ())
		{
			JumpJet *jet;
			jet = mech->GetJumpJet ();
			jumpValue = jet->CurrentChargePtr ();
			maxJumpValue = jet->GetMaxCharge ();
		}
	}

	m_targetDamageArray = NULL;
	m_targetArmorArray = NULL;
	m_targetTonage = -1.0f;

	ClearTargetIndicator();
	coolantValue = &vehicle->m_heatManager->coolantPercentage;
	heatValue = &vehicle->m_heatManager->heatPercentage;
//	isHeatDirty = &vehicle->m_heatManager->isHeatDirty;
//	isCoolantDirty = &vehicle->m_heatManager->isCoolantDirty;

// MSL 5.05 Merc Fix?
	// initialize this so no crash after respawn
	hudnav->PlayerFacing (0);

	m_isShown = SHOW_ALL;
	m_ShowLevel = m_isShown;
	m_drawScoring = false;
	Reset ();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::InitializeTargetSystem()
{
	Check_Object(this);
	HideHudComponent (HUD_RADAR);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::DisplayTargetSystem(bool locked)
{
	Check_Object(this);

	isReticleLocked = (int)locked;
	hudReticle->IsReticleLocked (isReticleLocked);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::DisplayTargetSystemLRM(bool locked)
{
	Check_Object(this);

	isReticleLockedLRM = (int)locked;
	hudReticle->IsReticleLockedLRM (isReticleLockedLRM);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::DisplayTargetSystemMRM(bool locked)
{
	Check_Object(this);

	isReticleLockedMRM = (int)locked;
	hudReticle->IsReticleLockedMRM (isReticleLockedMRM);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::DisplayTargetSystemSRM(bool locked)
{
	Check_Object(this);

	isReticleLockedSRM = (int)locked;
	hudReticle->IsReticleLockedSRM (isReticleLockedSRM);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::DisplayTargetSystemSSRM(bool locked)
{
	Check_Object(this);

	isReticleLockedSSRM = (int)locked;
	hudReticle->IsReticleLockedSSRM (isReticleLockedSSRM);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::ClearGUIChain()
{
	Check_Object(this);

	GUIManager::ClearGUIChain();
	if(weaponManager)
	{
		Check_Object(weaponManager);
		weaponManager->ClearWeaponsManager();
	}
	ClearComponents ();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::SetSpeedBar(Stuff::Scalar value,Stuff::Scalar minForward,Stuff::Scalar minReverse,Stuff::Scalar maxForward,Stuff::Scalar maxReverse)
{
	Check_Object(this);

	currentSpeedValue = value;
	hudSpeed->Speed (currentSpeedValue,minForward,minReverse,maxForward,maxReverse);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::SetTorsoPitch(Scalar value)
{
	Check_Object(this);

	torsoPitchValue = (int)((value / maxTorsoPitch) * 100);
	hudTorsoBar->TorsoPitch ((Scalar) torsoPitchValue);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::SetTorsoTwist(Scalar value)
{
	Check_Object(this);

	m_torsoRotation = (int)(value * Degrees_Per_Radian);
	radarManager->TorsoTwist (value);
	hudMap->TorsoTwist (value);
	torsoTwistValue = (int)((value / maxTorsoTwist) * 100);
	hudTorsoBar->TorsoTwist ((Scalar) torsoTwistValue);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::SetReticuleTarget (Entity *target_entity)
{
	hudTargetDamage->ReticuleTarget (target_entity);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::SetReticleName(const char *object_name)
{
	Check_Object(this);

	if (object_name)
		Str_Copy(reticleName, object_name, MaxStringLength);
	else
		Str_Copy(reticleName, "No Target", MaxStringLength);

	hudReticle->ReticleName (reticleName);
	hudReticle->ReticleRange (reticleDistance);
	hudWeapon->ReticleRange (reticleDistance);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::Reset()
{
	Check_Object(this);

	m_isShown = SHOW_ALL;
	m_drawScoring = false;
	m_ZoomVisible = false;
	m_HelpArrowShowing = false;
	m_TimerShowing = false;
	m_DebugShowing = false;

	stlport::vector <HUDComponent *>::iterator iter;
	
	for (iter = m_Components.begin ();iter != m_Components.end ();iter++)
	{
		(*iter)->SetVehicle (m_Vehicle);
		(*iter)->Reset ();
	}

	Show ();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::Hide()
{
	Check_Object(this);
	if (!m_Components.size ())
		return;

	if (VehicleInterface::GetInstance()->m_targetCamera)
	{
		ElementRenderer::Element *fred;
		if (m_targetElement.GetCurrent ())
			m_targetElement.Remove ();
		fred = VehicleInterface::GetInstance()->m_targetCamera->GetScene();
		if (fred)
			m_targetElement.Add (fred);
		VehicleInterface::GetInstance()->m_targetCamera->SetScene(NULL);
	}

	m_isShown = HIDE_ALL;
	stlport::vector <HUDComponent *>::iterator iter;
	for (iter = m_Components.begin ();iter != m_Components.end ();iter++)
	{
		(*iter)->Hide ();
	}
	ShowZoom (m_ZoomVisible);
	ShowMap (m_MapShowing);
	ShowHelpArrow (m_HelpArrowShowing);
	ShowTimer (m_TimerShowing);
	ShowDebug (m_DebugShowing);
}

void MWGUIManager::ReshowHud (void)
{
	switch (m_ShowLevel)
	{
		case HIDE_ALL:
			Hide ();
		break;
		case SHOW_ALL:
			Show ();
		break;
		case SHOW_RETICLE:
			ShowReticle ();
		break;
	}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::Show()
{
	Check_Object(this);
	
	if (!m_Components.size ())
		return;

	if(VehicleInterface::GetInstance() && VehicleInterface::GetInstance()->m_targetCamera && m_targetElement.GetCurrent ())
	{
		VehicleInterface::GetInstance()->m_targetCamera->SetScene(m_targetElement.GetCurrent ());
		m_targetElement.Remove ();
	}

	stlport::vector <HUDComponent *>::iterator iter;
	for (iter = m_Components.begin ();iter != m_Components.end ();iter++)
	{
		(*iter)->Show ();
	}
	if(!MWApplication::GetInstance()->networkingFlag)
		hudChat->Hide ();

	m_isShown = SHOW_ALL;
	ShowZoom (m_ZoomVisible);
	ShowMap (m_MapShowing);
	ShowHelpArrow (m_HelpArrowShowing);
	ShowTimer (m_TimerShowing);
	ShowDebug (m_DebugShowing);

	/*	
	if (m_Vehicle)
		hudCamera->Hide ();
	else
		hudCamera->Show ();
		*/

	if (m_drawScoring)
		ShowScoring ();
	else
		HideScoring ();

	if (m_Vehicle && !VehicleInterface::GetInstance()->IsObserving())
		hudCamera->Hide ();
	else
		hudCamera->Show ();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::ShowReticle()
{
	Check_Object(this);

	if (VehicleInterface::GetInstance()->m_targetCamera)
	{
		ElementRenderer::Element *fred;
		if (m_targetElement.GetCurrent ())
			m_targetElement.Remove ();
		fred = VehicleInterface::GetInstance()->m_targetCamera->GetScene();
		if (fred)
			m_targetElement.Add (fred);
		VehicleInterface::GetInstance()->m_targetCamera->SetScene(NULL);
	}

	m_isShown = SHOW_RETICLE;
	stlport::vector <HUDComponent *>::iterator iter;
	for (iter = m_Components.begin ();iter != m_Components.end ();iter++)
	{
		(*iter)->Hide ();
	}
	hudTargetArrow->Show ();
	hudReticle->Show ();
	hudTorsoBar->Show ();
	ShowZoom (m_ZoomVisible);
	ShowMap (m_MapShowing);
	ShowHelpArrow (m_HelpArrowShowing);
	ShowTimer (m_TimerShowing);
	ShowDebug (m_DebugShowing);
	if (m_Vehicle)
		hudCamera->Hide ();
	else
		hudCamera->Show ();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::Toggle()
{
	Check_Object(this);

	if (!m_Vehicle)
	{
		switch (m_isShown)
		{
			case HIDE_ALL:
				ShowReticle ();
				m_ShowLevel = m_isShown;
			break;
			case SHOW_RETICLE:
				Show ();
				m_ShowLevel = m_isShown;
			break;
			case SHOW_ALL:
				Hide ();
				m_ShowLevel = m_isShown;
			break;
		}
	}
	else
	{
		switch (m_isShown)
		{
			case HIDE_ALL:
				ShowReticle ();
				m_ShowLevel = m_isShown;
			break;
			case SHOW_RETICLE:
				Show ();
				m_ShowLevel = m_isShown;
			break;
			case SHOW_ALL:
				Hide ();
				m_ShowLevel = m_isShown;
			break;
		}
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::ToggleScoring()
{
	Check_Object(this);

	if(m_drawScoring)
		m_drawScoring = false;
	else
		m_drawScoring = true;

	if (m_drawScoring)
		ShowScoring ();
	else
		HideScoring ();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::ShowScoring()
{
	Check_Object(this);

	m_drawScoring = true;
	hudScore->Show ();
	if (VehicleInterface::GetInstance ())
	{
		InterfaceZoomMode = VehicleInterface::GetInstance ()->reticuleZoomShowing;
		VehicleInterface::GetInstance ()->requestedZoomChange = false;
		VehicleInterface::GetInstance ()->userRequestForreticuleZoomChange = true;
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::HideScoring()
{
	Check_Object(this);

	m_drawScoring = false;
	hudScore->Hide ();
	if (VehicleInterface::GetInstance ())
		if (InterfaceZoomMode != VehicleInterface::GetInstance ()->reticuleZoomShowing)
		{
			VehicleInterface::GetInstance ()->requestedZoomChange = InterfaceZoomMode;
			VehicleInterface::GetInstance ()->userRequestForreticuleZoomChange = true;
		}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::ShowLightAmplification()
{
	Check_Object(this);
	Check_Object(lightAmp);
//	Check_Object (zoomLightAmp);

	if (m_Vehicle)
	{
		if(m_Vehicle->DoesHaveLightAmp())
		{
			lightAmp->Show();
//			zoomLightAmp->Show ();
			m_LightAmpShowing = true;
		}
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::HideLightAmplification()
{
	Check_Object(this);
	Check_Object(lightAmp);
//	Check_Object (zoomLightAmp);

	lightAmp->Hide();
//	zoomLightAmp->Hide ();
	m_LightAmpShowing = false;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::ShowStaticView()
{
	Check_Object(this);
	Check_Object(staticViewManager);

	staticViewManager->Show();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::AnimateStaticView()
{
	Check_Object(this);
	Check_Object(staticViewManager);


	staticViewManager->Animate();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::HideStaticView()
{
	Check_Object(this);
	Check_Object(staticViewManager);

	staticViewManager->Hide();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::SetNavSystem(Stuff::Scalar facing_angle,Stuff::Scalar nav_point_angle,Stuff::Scalar range,int nav_point_id)
{
	Check_Object(this);

	if(facing_angle < 0.0f)
		facing_angle += Two_Pi;

	facing_angle = Two_Pi - facing_angle;

	m_playerFacing = (int)(facing_angle * Degrees_Per_Radian);
	hudnav->PlayerFacing (m_playerFacing);

//	if(nav_point_angle < 0.0f)
//		nav_point_angle += Two_Pi;

	m_navPointFacing = (int)(nav_point_angle * Degrees_Per_Radian);
	m_navPointRange = range;
	hudnav->NavPointFacing (m_navPointFacing);
	hudnav->NavPointRange ((int) m_navPointRange);
	
	if(nav_point_id != NameTable::NullObjectID)
	{
		Check_Object(NameTable::GetInstance());
		NavPoint *nav;
		Entity *ent;
		ent = NameTable::GetInstance()->FindData (nav_point_id);
		Verify (ent->IsDerivedFrom (NavPoint::DefaultData));
		nav = Cast_Object (NavPoint *,ent);
		Str_Copy(m_navPointName,nav->GetObjectName (), MaxStringLength);
	}
	else
		Str_Copy(m_navPointName, "No Point Selected", MaxStringLength);

	hudnav->NavPointName (m_navPointName);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::SetTargetPosition(CameraComponent *camera, const Point3D& target_world, const Point3D& target_size,
																		 Stuff::Scalar cameraWidth, Stuff::Scalar cameraHeight,
																		 Stuff::Scalar cameraOffsetX, Stuff::Scalar cameraOffsetY)
{
	Check_Object(this);

	if (target_world.y < -1000)
	{
		hudTargetArrow->TargetLoc (-1,-1,-1,-1);
		return;
	}

	MidLevelRenderer::MLRClipper *clipper;
	camera->GetElement()->Sync();
	clipper = camera->GetElement()->GetClipper();

	Check_Object(clipper);
	clipper->UpdateClipperCameraData(
				camera->GetElement()->GetLocalToWorld(),
				camera->GetElement()->GetCameraToClip()
			);
	
	
	Vector4D target_in_clipper;

	target_in_clipper.Multiply(target_world, clipper->GetWorldToClipMatrix());

	m_targetXLocation = (target_in_clipper.x / target_in_clipper.w);
	Clamp(m_targetXLocation, 0.0f, 1.0f);
	m_targetXLocation *= -cameraWidth;
	m_targetXLocation += cameraWidth + cameraOffsetX;
	m_targetYLocation = (target_in_clipper.y / target_in_clipper.w);
	Clamp(m_targetYLocation, 0.0f, 1.0f);
	m_targetYLocation *= -cameraHeight;
	m_targetYLocation += cameraHeight + cameraOffsetY;
	
	if (!Small_Enough(target_size))
	{
		FindTargetBoxSize(clipper, target_world, target_size, m_targetWidth, m_targetHeight);
		m_targetHeight *= -cameraHeight;
		m_targetWidth *= -cameraWidth;
	}
	else
	{
		m_targetWidth = 0.0f;
		m_targetHeight = 0.0f;
	}


	hudTargetArrow->TargetLoc (m_targetXLocation,m_targetYLocation, m_targetWidth, m_targetHeight);
}

void MWGUIManager::FindTargetBoxSize(MidLevelRenderer::MLRClipper *clipper, const Point3D & target_world, const Point3D & target_size, Stuff::Scalar & targetWidth, Stuff::Scalar & targetHeight)
{
	Stuff::Point3D box_corners[8];

	box_corners[0].x =
	box_corners[3].x =
	box_corners[4].x =
	box_corners[7].x =
		target_world.x - target_size.x;
	box_corners[1].x =
	box_corners[2].x =
	box_corners[5].x =
	box_corners[6].x =
		target_world.x + target_size.x;

	box_corners[2].y =
	box_corners[3].y =
	box_corners[7].y =
	box_corners[6].y =
		target_world.y - target_size.y;
	box_corners[0].y =
	box_corners[1].y =
	box_corners[4].y =
	box_corners[5].y =
		target_world.y + target_size.y;

	box_corners[0].z =
	box_corners[1].z =
	box_corners[2].z =
	box_corners[3].z =
		target_world.z - target_size.z;
	box_corners[4].z =
	box_corners[5].z =
	box_corners[6].z =
	box_corners[7].z =
		target_world.z + target_size.z;

	//
	// Set to values that will be overwritten on the first test since they are out of range
	//
	Stuff::Scalar min_corner_XLocation = -1.0;
	Stuff::Scalar min_corner_YLocation = -1.0;
	Stuff::Scalar max_corner_XLocation = 2.0;
	Stuff::Scalar max_corner_YLocation = 2.0;
	//
	// Look for the bounding rectangle in the 2d view by mapping to 2d and finding min/max x and y
	//
	for (int i = 0; i < 8; i++)
	{
		Vector4D target_in_clipper;
		Stuff::Scalar corner_XLocation;
		Stuff::Scalar corner_YLocation;

		target_in_clipper.Multiply(box_corners[i], clipper->GetWorldToClipMatrix());

		corner_XLocation = (target_in_clipper.x / target_in_clipper.w);
		Clamp(corner_XLocation, 0.0f, 1.0f);
		corner_YLocation = (target_in_clipper.y / target_in_clipper.w);
		Clamp(corner_YLocation, 0.0f, 1.0f);
		//
		// For the purpose of these tests min and max are reverse meaning since they are going to be multiplied by a
		// negative number down below when we want the real value.
		//
		min_corner_XLocation = (min_corner_XLocation >= corner_XLocation ? min_corner_XLocation : corner_XLocation);
		max_corner_XLocation = (max_corner_XLocation <= corner_XLocation ? max_corner_XLocation : corner_XLocation);
		min_corner_YLocation = (min_corner_YLocation >= corner_YLocation ? min_corner_YLocation : corner_YLocation);
		max_corner_YLocation = (max_corner_YLocation <= corner_YLocation ? max_corner_YLocation : corner_YLocation);
	}
	targetHeight = max_corner_YLocation - min_corner_YLocation;
	targetWidth = max_corner_XLocation - min_corner_XLocation;
	//
	// Don't let the minimum, size go below some fixed size vs. the screen.
	//		(We're still using negative coordinates here)
	Max_Clamp(targetHeight, -0.050f);
	Max_Clamp(targetWidth,  -0.050f);
}
		
void MWGUIManager::SetLeftTargetIndicator()
{
	Check_Object(this);
	Check_Object (hudTargetArrow);
	m_leftTargetIndicator = 1;
	hudTargetArrow->LeftArrow ();
}

void MWGUIManager::SetRightTargetIndicator()
{
	Check_Object(this);
	Check_Object (hudTargetArrow);
	m_rightTargetIndicator = 1;
	hudTargetArrow->RightArrow ();
}

		
void MWGUIManager::SetTopTargetIndicator()
{
	Check_Object(this);
	Check_Object (hudTargetArrow);
	m_topTargetIndicator = 1;
	hudTargetArrow->TopArrow ();
}

void MWGUIManager::SetBottomTargetIndicator()
{
	Check_Object(this);
	Check_Object (hudTargetArrow);
	m_bottomTargetIndicator = 1;
	hudTargetArrow->BottomArrow ();
}

void MWGUIManager::SetTargetIndicatorInZoom()
{
	Check_Object(this);
	Check_Object (hudTargetArrow);
	m_targetIndicatorInZoom = 1;
	hudTargetArrow->IndicatorInZoom (true);
}

void MWGUIManager::ClearTargetIndicator()
{
	Check_Object(this);
	m_leftTargetIndicator = 0;
	m_rightTargetIndicator = 0;
	m_topTargetIndicator = 0;
	m_bottomTargetIndicator = 0;
	m_targetIndicatorInZoom = 0;
	if (hudTargetArrow)
	{
		hudTargetArrow->IndicatorInZoom(false);
		hudTargetArrow->TargetLoc (-1.0f,-1.0f, 0.0f, 0.0f);
	}
	m_targetXLocation = -1;
	m_targetYLocation = -1;
	m_targetHeight = 0.0f;
	m_targetWidth = 0.0f;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void MWGUIManager::SetNewTargetWindowEntity(Entity *new_target)
{
	Check_Object(this);

	if (m_CurrentTarget.GetCurrent ())
		m_CurrentTarget.Remove ();
	hudTargetDamage->Target (new_target);
	if(new_target)
	{
		m_CurrentTarget.Add (new_target);
		radarManager->CurrentTarget (new_target);
		hudMap->CurrentTarget (new_target);

		if (m_Vehicle != 0)
			m_targetWindowAlignment = (m_Vehicle->GetRelativeAlignment(new_target->GetAlignment()));
		else
			m_targetWindowAlignment = (new_target->GetAlignment());

		hudTargetArrow->TargetAlignment (m_targetWindowAlignment);
		hudTargetDamage->TargetAlignment(m_targetWindowAlignment);
		
/*
		MString target_name = new_target->GetGameModelResourceName();
		target_name.StripDirectory();
		target_name.StripExtension();
*/
		if (new_target->GetObjectName())
			Str_Copy(m_targetWindowName, (char *)new_target->GetObjectName(), MaxStringLength);
		else
			m_targetWindowName[0] = 0;

		if (new_target->IsDerivedFrom (Vehicle::DefaultData))
		{
			Vehicle *veh;
			veh = Cast_Object (Vehicle *,new_target);
			m_targetArmorArray = veh->armorArray;
		}
		if(new_target->IsDerivedFrom(Mech::DefaultData))
		{
			Mech *mech = Cast_Object(Mech *, new_target);
			m_targetDamageArray = mech->damageArray;

			m_targetTonage = mech->GetTonage();

			if(mech->isPlayerVehicle)
			{
				Str_Copy(m_targetWindowName, (char *)mech->instanceName, MaxStringLength);
			}
		}
		else
		{
			m_targetDamageArray = NULL;
			m_targetTonage = -1;
		}
	}
	else
	{
		m_targetTonage = 0;
		m_targetWindowName[0] = 0;
		radarManager->CurrentTarget (NULL);
		hudMap->CurrentTarget (NULL);
	}

	hudTargetDamage->TargetName (m_targetWindowName);
    //hudTargetArrow->TargetName (m_targetWindowName);
	hudTargetDamage->TargetTonnage (m_targetTonage);
}



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// New HUD system
//

void MWGUIManager::AddComponent (HUDComponent *comp)
{
	m_Components.push_back (comp);
}

	bool VehicleInsideObjectiveArea(MechWarrior4::Vehicle& vehicle)
	{
		MWMission* mission = Cast_Object(MWMission*,MWMission::GetInstance());
		CBucketManager* bucketman = mission->m_BucketManager;

		if (bucketman != 0)
		{
			const stlport::vector<Vehicle*>& v = bucketman->GetVehiclesInsideObjectiveArea();

			{for (stlport::vector<Vehicle*>::const_iterator i = v.begin();
				  i != v.end();
				  ++i)
			{
				if ((*i) == &vehicle)
				{
					return (true);
				}
			}}
		}

		return (false);
	}

void MWGUIManager::UpdateComponents (void)
{
	if (hudScore)
		hudScore->GameType (gametype);
	if(heatValue)
		hudHeat->Heat (*heatValue);
	if(coolantValue)
		hudCoolant->Coolant (*coolantValue);
	if (jumpValue)
		hudJump->Jump (DECRYPT(*jumpValue),maxJumpValue);
	else
		hudJump->Jump (-1.0f,0);

	if (damageArray)
		hudDamage->DamageValue (damageArray);
	if (armorArray)
		hudDamage->ArmorValue (armorArray,false);

// MSL 5.02 SubSystems
	Scalar dist = 1000;

	if ((m_CurrentTarget.GetCurrent ()) && (m_Vehicle))
	{
		Point3D loc1 (m_Vehicle->GetLocalToWorld ());
		Point3D loc2 (m_CurrentTarget.GetCurrent ()->GetLocalToWorld ());
		Point3D loc3;
		loc3.Subtract (loc1,loc2);
		Scalar dist = loc3.GetLength ();
		hudTargetDamage->TargetRange (dist); // hudTargetArrow->TargetRange (dist);
	}

// MSL 5.02 SubSystems
	if (m_Vehicle != 0)
	{
		if (   (GetReticleIFFJammer())
//			&& (m_Vehicle->GetRelativeAlignment(reticleAlignment) == Entity::Enemy)
			&& (hudReticle->GetReticleRange() >= 200)
			)
		{
			hudReticle->ReticleAlignment (Entity::DefaultAlignment);
		}
		else
			hudReticle->ReticleAlignment (m_Vehicle->GetRelativeAlignment(reticleAlignment));
	}
	else
	{
		hudReticle->ReticleAlignment (reticleAlignment);
	}


	if (MWApplication::GetInstance()->networkingFlag)
	{
		if (m_Vehicle != 0)
		{
			ShowFlag(Flag::AnyFlagsAttachedTo(*m_Vehicle));
			ShowKOTF(VehicleInsideObjectiveArea(*m_Vehicle));
		}
		else
		{
			ShowKOTF(false);
		}

		MWMission* mission = Cast_Object(MWMission*,MWMission::GetInstance());
		if (mission != 0)
		{
			//if (MWApplication::GetInstance()->serverFlag)
			//{
				MPTimeLeft(mission->GetEndMissionTime());
			//}
			//else
			//{
			//	MPTimeLeft(-1.0f);
			//}
			
		}		
	}

//	if (VehicleInterface::GetInstance())
//		hudChat->SetCurrentChat (VehicleInterface::GetInstance()->m_CurrentChatMessage);

	Stuff::Time current;
	current = gos_GetElapsedTime ();
	stlport::vector <HUDComponent *>::iterator iter;
	for (iter = m_Components.begin ();iter != m_Components.end ();iter++)
	{
		(*iter)->Update (current);
	}
}

void MWGUIManager::HeatLevel (Scalar level)
{
	g_HUDHeatLevel = level;
}

void MWGUIManager::AddPPCLevel (Scalar level)
{
	g_HUDPPCLevel += level;
}


void MWGUIManager::RenderComponents (void)
{
#if !defined(NO_TIMERS)
	MW4AI::my_AutoTimer fred ((void *) &tHUDTime);
#endif

	if (VehicleInterface::GetInstance()->cinemaPlaying)
	{
		if (VehicleInterface::GetInstance()->m_targetCamera)
		{
			ElementRenderer::Element *fred;
			fred = VehicleInterface::GetInstance()->m_targetCamera->GetScene();
			if (fred)
			{
				if (m_targetElement.GetCurrent ())
					m_targetElement.Remove ();
				m_targetElement.Add (fred);
				VehicleInterface::GetInstance()->m_targetCamera->SetScene(NULL);
			}
		}
		return;
	}
	
	gos_PushRenderStates ();
	HUDTexture::StartTexturePass ();
	stlport::vector <HUDComponent *>::iterator iter;

	if (!m_drawScoring && !VehicleInterface::GetInstance()->IsObserving())
	{
		if ((m_Vehicle) )
		{
			if(!m_Vehicle->vehicleShutDown){
				for (iter = m_Components.begin ();iter != m_Components.end ();iter++)
				{
                    // jcem - no Lancemate...
#if !defined(_DEBUG)
                    if (hudComm == (*iter)) {
                        if (g_bCOOP)
                            continue;
                    }
#endif // !defined(_DEBUG)
					if (hudHelpArrow == (*iter))
						continue;
					if ((hudChat == (*iter)) || (hudScore == (*iter)) || (hudDebug == (*iter)) || (hudCamera == (*iter)))
						(*iter)->Draw (true);			
					else
						(*iter)->Draw ();
				}
			}else{
				//sanghoon: while shutting down..
				for (iter = m_Components.begin ();iter != m_Components.end ();iter++)
				{
					//Main		hudZoom,//hudReticle,hudObjective,hudTorsoBar,hudHelp,hudScore
					//Secondary	hudHeat,hudJump,hudCoolant,hudSpeed,//radarManager
					//Aux		hudChat(1),hudNav(2),hudWeapon(3),hudDamage(5),//hudTargetDamage(4)
                    // jcem - no Lancemate...
#if !defined(_DEBUG)
                    if (hudComm == (*iter)) {
                        if (g_bCOOP)
                            continue;
                    }
#endif // !defined(_DEBUG)
					if (hudHelpArrow == (*iter))
						continue;
					if ( hudChat == (*iter) )
						(*iter)->Draw(true);
					if(
						hudZoom		== (*iter)||
						hudHeat		== (*iter)||
						hudJump		== (*iter)||
						hudCoolant	== (*iter)||
						hudSpeed	== (*iter)||
						hudChat		== (*iter)||
						hudnav		== (*iter)||
						hudWeapon	== (*iter)||
						hudDamage	== (*iter)
						)
						(*iter)->Draw();
				}
			}
		}
		else
		{
			hudChat->Draw (true);
			hudCamera->Draw (true);
// MSL 5.05 Merc Fix?
			// want to draw the objectives in an objective-based Multiplayer game if you're dead
			hudObjective->Draw(true);
		}
	}
	else
	{
		//sanghoon begin
        extern CamerashipParams g_CamerashipParams;
		if (CTCL_IsConsoleX() || g_CamerashipParams.m_bAllowChatDisplay)
		    hudChat->Draw (true);
		//sanghoon end
		hudScore->Draw (true);
		hudCamera->Draw(true);
	}
	HUDTexture::EndTexturePass ();
    if (!VehicleInterface::GetInstance()->IsObserving())
	hudHelpArrow->Draw ();						// want it to always be on top
	hudDebug->Draw ();
	gos_PopRenderStates ();


//sanghoon begin
	MWApplication* app = MWApplication::GetInstance();
	if (app->networkingFlag)
	{
		MWMission *mwmiss= Cast_Object(MWMission*, MWMission::GetInstance());
		Verify (mwmiss);
// MSL 5.02 Black Screen Fix
		Stuff::Scalar current_time=mwmiss->GetMissionTime();
		#define TIME_END_MISSION_FADEOUT 3.0f
		#define TIME_END_MISSION_FADEOUT2 0.1f
		if (current_time < TIME_END_MISSION_FADEOUT)
		{
		    MWVideoRenderer *vid_rend = Cast_Object(MWVideoRenderer *, VideoRenderer::Instance);
			Stuff::RGBColor color;
		    color = RGBColor(0.0f, 0.0f, 0.0f);
			Scalar fadelevel=0;
			vid_rend->SetFadeLevel(fadelevel, color);
		}
		if (current_time >= TIME_END_MISSION_FADEOUT)
		{
			Stuff::Scalar scalar_time=mwmiss->CheckEndMissionTime();
			if (scalar_time<TIME_END_MISSION_FADEOUT)
			{
			    MWVideoRenderer *vid_rend = Cast_Object(MWVideoRenderer *, VideoRenderer::Instance);
				Stuff::RGBColor color;
			    color = RGBColor(0.0f, 0.0f, 0.0f);
			    Scalar fadelevel=0;

				if (scalar_time<TIME_END_MISSION_FADEOUT2)
	                fadelevel=255;
	            else
				    fadelevel=(TIME_END_MISSION_FADEOUT-scalar_time)/TIME_END_MISSION_FADEOUT;//0~1
			    vid_rend->SetFadeLevel(fadelevel, color);
			}
		}
	}
//sanghoon end
}

void MWGUIManager::ShowHelpArrow (bool value)
{
	m_HelpArrowShowing = value;
	Check_Object (hudHelpArrow);
	if (!m_isShown)
		hudHelpArrow->Hide ();
	if (value)
		hudHelpArrow->Show ();
	else
		hudHelpArrow->Hide ();
}

void MWGUIManager::ShowTimer (bool value)
{
	m_TimerShowing = value;
	Check_Object (hudTimer);
	if (!m_isShown)
		hudTimer->Hide ();
	if (value)
		hudTimer->Show ();
	else
		hudTimer->Hide ();
}

void MWGUIManager::ShowDebug (bool value)
{
	m_DebugShowing = value;
	Check_Object (hudTimer);
	if (value)
		hudDebug->Show ();
	else
		hudDebug->Hide ();
}

void MWGUIManager::ShowZoom (bool value)
{
	m_ZoomVisible = value;
	Check_Object (hudZoom);
	if (!m_isShown)
		hudZoom->Hide ();
	if (value)
	{
// MSL 5.02
		if (m_LightAmpShowing)
			lightAmp->Hide();
		//			zoomLightAmp->Show ();
		hudZoom->Show ();
	}
	else
	{
//		zoomLightAmp->Hide ();
		hudZoom->Hide ();
	}
}

void MWGUIManager::ShowMap (bool value)
{
	m_MapShowing = value;
	Check_Object (hudMap);
	Check_Object (radarManager);
	if ((m_isShown == HIDE_ALL) || (m_isShown == SHOW_RETICLE))
	{
		hudMap->Hide();
		radarManager->Hide ();
		hudHeat->Hide ();
		hudSpeed->Hide ();
	}
	else if (value)
	{
		hudMap->Show ();
		radarManager->Hide ();
		hudHeat->Hide ();
		hudSpeed->Hide ();
	}
	else
	{
		hudMap->Hide ();
		radarManager->Show ();
		hudHeat->Show ();
		hudSpeed->Show ();
	}
}

void MWGUIManager::SetZoomDisplayLevel (int level)
{
	Check_Object (hudZoom);
	hudZoom->SetZoomLevel(level);
}

void MWGUIManager::MPName (const char *name)
{
	Str_Copy (gametype,name,256);
	if (hudScore)
		hudScore->GameType (gametype);
}

void MWGUIManager::MPTimeLeft (Stuff::Time value)
{
	if (hudScore)
		hudScore->TimeLeft (value);
}

// MSL 5.03 Ammo Bay Fire Indicator
void MWGUIManager::ShowFIRE (bool value)
{
	if (hudmp)
		hudmp->ShowFIRE (value);
}

// MSL 5.03 Jam Indicator
void MWGUIManager::ShowJAM (bool value)
{
	if (hudmp)
		hudmp->ShowJAM (value);
}

void MWGUIManager::ShowFlag (bool value)
{
	if (hudmp)
		hudmp->ShowFlag (value);
}

void MWGUIManager::ShowKOTF (bool value)
{
	if (hudmp)
		hudmp->ShowKOTF (value);
}

void MWGUIManager::ShowHudComponent (HUDELEMENTS which)
{
	switch (which)
	{
		case HUD_NAV:
			hudnav->Show ();
			break;
		case HUD_TARGETARROW:
			hudTargetArrow->Show ();
			break;
		case HUD_TORSOBAR:
			hudTorsoBar->Show ();
			break;
		case HUD_ZOOM:
			hudZoom->Show ();
			break;
		case HUD_HEAT:
			hudHeat->Show ();
			break;
		case HUD_COOLANT:
			hudCoolant->Show ();
			break;
		case HUD_JUMP:
			hudJump->Show ();
			break;
		case HUD_SPEED:
			hudSpeed->Show ();
			break;
		case HUD_DAMAGE:
			hudDamage->Show ();
			break;
		case HUD_TARGETDAMAGE:
			hudTargetDamage->Show ();
			break;
		case HUD_WEAPON:
			hudWeapon->Show ();
			break;
		case HUD_RETICLE:
			hudReticle->Show ();
			break;
		case HUD_RADAR:
			radarManager->Show ();
			break;
		case HUD_CHAT:
			hudChat->Show ();
			break;
		case HUD_COMM:
			hudComm->Show ();
			break;
		case HUD_HELP:
			hudHelp->Show ();
			break;
		case HUD_MAP:
			hudMap->Show ();
			break;
		case HUD_OBJECTIVE:
			hudObjective->Show ();
			break;
		case HUD_SCORE:
			hudScore->Show ();
			break;
		case HUD_TIMER:
			hudTimer->Show ();
			break;
		case HUD_HELPARROW:
			hudHelpArrow->Show ();
			break;
		case HUD_DEBUG:
			hudDebug->Show ();
			break;
		case HUD_MP:
			hudmp->Show ();
			break;
		case HUD_CAMERA:
			hudCamera->Show ();
			break;
	}
}

void MWGUIManager::HideHudComponent (HUDELEMENTS which)
{
	switch (which)
	{
		case HUD_NAV:
			hudnav->Hide ();
			break;
		case HUD_TARGETARROW:
			hudTargetArrow->Hide ();
			break;
		case HUD_TORSOBAR:
			hudTorsoBar->Hide ();
			break;
		case HUD_ZOOM:
			hudZoom->Hide ();
			break;
		case HUD_HEAT:
			hudHeat->Hide ();
			break;
		case HUD_COOLANT:
			hudCoolant->Hide ();
			break;
		case HUD_JUMP:
			hudJump->Hide ();
			break;
		case HUD_SPEED:
			hudSpeed->Hide ();
			break;
		case HUD_DAMAGE:
			hudDamage->Hide ();
			break;
		case HUD_TARGETDAMAGE:
			hudTargetDamage->Hide ();
			break;
		case HUD_WEAPON:
			hudWeapon->Hide ();
			break;
		case HUD_RETICLE:
			hudReticle->Hide ();
			break;
		case HUD_RADAR:
			radarManager->Hide ();
			break;
		case HUD_CHAT:
			hudChat->Hide ();
			break;
		case HUD_COMM:
			hudComm->Hide ();
			break;
		case HUD_HELP:
			hudHelp->Hide ();
			break;
		case HUD_MAP:
			hudMap->Hide ();
			break;
		case HUD_OBJECTIVE:
			hudObjective->Hide ();
			break;
		case HUD_SCORE:
			hudScore->Hide ();
			break;
		case HUD_TIMER:
			hudTimer->Hide ();
			break;
		case HUD_HELPARROW:
			hudHelpArrow->Hide ();
			break;
		case HUD_DEBUG:
			hudDebug->Hide ();
			break;
		case HUD_MP:
			hudmp->Hide ();
			break;
		case HUD_CAMERA:
			hudCamera->Hide ();
			break;
	}
}

bool MWGUIManager::HudComponentVisible (HUDELEMENTS which)
{
	HUDComponent* component = Component(which);

	if ((component != 0) &&
		(component->Visible() == true))
	{
		return (true);
	}

	return (false);
}

HUDComponent *MWGUIManager::Component (HUDELEMENTS which)
{
	switch (which)
	{
		case HUD_NAV:
			return hudnav;
			break;
		case HUD_TARGETARROW:
			return hudTargetArrow;
			break;
		case HUD_TORSOBAR:
			return hudTorsoBar;
			break;
		case HUD_ZOOM:
			return hudZoom;
			break;
		case HUD_HEAT:
			return hudHeat;
			break;
		case HUD_COOLANT:
			return hudCoolant;
			break;
		case HUD_JUMP:
			return hudJump;
			break;
		case HUD_SPEED:
			return hudSpeed;
			break;
		case HUD_DAMAGE:
			return hudDamage;
			break;
		case HUD_TARGETDAMAGE:
			return hudTargetDamage;
			break;
		case HUD_WEAPON:
			return hudWeapon;
			break;
		case HUD_RETICLE:
			return hudReticle;
			break;
		case HUD_RADAR:
			return radarManager;
			break;
		case HUD_CHAT:
			return hudChat;
			break;
		case HUD_COMM:
			return hudComm;
			break;
		case HUD_HELP:
			return hudHelp;
			break;
		case HUD_MAP:
			return hudMap;
			break;
		case HUD_OBJECTIVE:
			return hudObjective;
			break;
		case HUD_SCORE:
			return hudScore;
			break;
		case HUD_TIMER:
			return hudTimer;
			break;
		case HUD_HELPARROW:
			return hudHelpArrow;
			break;
		case HUD_DEBUG:
			return hudDebug;
			break;
		case HUD_MP:
			return hudmp;
			break;
		case HUD_CAMERA:
			return hudCamera;
			break;
		default:
			STOP (("bad value passed into MWGUIManager::Component"));
			break;
	}
	return NULL;
}

