#include "MW4Headers.hpp"
#include "hudcomp.hpp"
#include "hudcomp2.hpp"
#include "hudweapon.hpp"
#include "guiweaponmanager.hpp"
#include "weapon.hpp"
#include "mwapplication.hpp"
#include "..\missionlang\resource.h"

using namespace MechWarrior4;

// MSL 5.03 RTX
static float RECOVER_FROM_JAMMING_TIME = 5.0f;

// MSL 5.03 Ammo Bay Fire
static float TIME_AMMO_BAY_FIRE = 7.0f;

//sanghoon begin
#include <windows.h>
#include <ddraw.h>
#include <d3d.h>

#include <GameOS\render.hpp>
extern bool g_bNoWeaponRangeCheck;
//sanghoon end

static void MakeStringCaps(char* buffer)
{
	while(*buffer)
		*buffer++ = (char) toupper (*buffer);
}

HUDWeapon::HUDWeapon() :
		m_WeaponManager (NULL),
		m_Weapons (NULL),
		m_SingleFire (NULL)
{
	int i;

	weaponcolor = MakeColor (0,150,0,200);
	Color (weaponcolor);
	for (i=0;i<MAX_HUD_WEAPONS;i++)
	{
		name_array[i] = new HUDText ();
		name_array[i]->UpdateText ("Weapon Name");
		ammo_text[i] = new HUDText ();
		ammo_text[i]->SetSize (HUDText::SMALL_SIZE);
		ammo_text[i]->UpdateText ("34");
		ammo_text[i]->Justification (HUDText::LEFT_ALIGN);
	}

	m_Text[0] = new HUDText ();
	m_Text[1] = new HUDText ();
	m_Text[2] = new HUDText ();
	m_Text[3] = new HUDText ();
	m_Text[4] = new HUDText ();
	m_ReticleRange = 10000.0f;
	
	AddTexture ("hud\\hud5",20,20,134,26,141);		// 0
	AddTexture ("hud\\hud5",21,27,134,33,141);    // 1
	AddTexture ("hud\\hud5",22,34,134,40,141);    // 2
	AddTexture ("hud\\hud5",23,20,142,26,150);    // 3
	AddTexture ("hud\\hud5",24,27,142,33,150);    // 4
	AddTexture ("hud\\hud5",25,34,142,40,150);    // 5

	MWApplication *app;
	app = MWApplication::GetInstance ();

	DWORD color = MakeColor (0,0,0,255);
	m_Text[0]->Justification (HUDText::LEFT_ALIGN);
	m_Text[0]->Color (color);
	m_Text[0]->UpdateText (app->GetLocString (IDS_WEAPONGROUPHEADER));
	
	m_Text[1]->Justification (HUDText::LEFT_ALIGN);
	m_Text[1]->Color (color);
	m_Text[1]->UpdateText (app->GetLocString (IDS_RELOADHUD));

	m_Text[2]->Justification (HUDText::LEFT_ALIGN);
	m_Text[2]->Color (color);
	m_Text[2]->UpdateText (app->GetLocString (IDS_WEAPONNAMEHEADER));

	m_Text[3]->Justification (HUDText::LEFT_ALIGN);
	m_Text[3]->Color (color);
	m_Text[3]->UpdateText (app->GetLocString (IDS_WEAPONAMMOHEADER));

	m_Text[4]->Justification (HUDText::LEFT_ALIGN);
	m_Text[4]->Color (color);
	m_Text[4]->UpdateText (app->GetLocString (IDS_WEAPONGROUPS));
	m_Text[4]->SetSize (HUDText::SMALL_SIZE);
	
	m_NumWeapons = 0;
	for (i=0;i<6;i++)
		m_CanHit[i] = false;
}
HUDWeapon::~HUDWeapon()
{
	int i;
	for (i=0;i<MAX_HUD_WEAPONS;i++)
	{
		delete name_array[i];
		delete ammo_text[i];
	}
	delete m_Text[0];
	delete m_Text[1];
	delete m_Text[2];
	delete m_Text[3];
	delete m_Text[4];
}
void HUDWeapon::Reset (void)
{
	int i;
	for (i=0;i<6;i++)
		m_CanHit[i] = false;
	m_SingleFire.Remove ();
}

void HUDWeapon::WeaponManager (GUIWeaponManager *value)
{
	m_WeaponManager.Remove ();
	m_WeaponManager.Add (value);
	ChainIteratorOf<GUIWeapon *> iterator(&m_Weapons);
	GUIWeapon *weapon;
	while ((weapon = iterator.ReadAndNext()) != NULL)
	{
		m_Weapons.Remove (weapon);
	}
}

void HUDWeapon::SetupWeapons (void)
{
	int i;
	if (m_WeaponManager.GetCurrent ())
	{
		GUIWeapon *weap;

		for (i=0;i<MAX_HUD_WEAPONS;i++)
		{
			weap = m_WeaponManager.GetCurrent ()->GetWeapon (i);
			if (weap)
				m_Weapons.Add (weap);
		}
	}
}

const int group_loc[6] = {4,11,18,25,32,39};

void HUDWeapon::DrawImplementation(void)
{
if(!hsh_initialized){
	int i;
	for (i=0;i<6;i++)
		m_CanHit[i] = false;
	if (m_Weapons.GetSize () == 0)
		return;

	m_NumWeapons = m_Weapons.GetSize ();

	Point3D loc,size;
	loc.x = 800.0f - 225.0f;
	loc.y = 0;
	loc.z = 0.9f;
	DWORD color;
	Stuff::Scalar reload;
	int w;
	bool canhit;

	color = MakeColor (0,0,0,150);

	int totalheight=0,oneheight=0;
	ChainIteratorOf<GUIWeapon *> iterator2(&m_Weapons);
	GUIWeapon *weapon;
	w = 0;
	while ((weapon = iterator2.ReadAndNext()) != NULL)
	{
		DWORD a,b;
		char buffer[256];
// MSL 5.05 Rear Firing Weapons
		if (weapon->m_weapon->GetWeaponFacing() == 1)
		{
			std::string s;
			s = weapon->weaponName;
			s += " (Rear)";
			strcpy (buffer, (char*)s.c_str());
		}
		else
		{
			strcpy (buffer, weapon->weaponName);
		}

		for (a=0;a<strlen (buffer);a++)
		{
			buffer[a] = (char) toupper (buffer[a]);
		}
//		name_array[w]->UpdateText (weapon->weaponName);
		name_array[w]->UpdateText (buffer);
		name_array[w]->SetSize (HUDText::SMALL_SIZE);
		name_array[w]->DrawSize (a,b);
//		totalheight += b+1;
		if (b != 0)
			totalheight += (b-3);
		oneheight = b;
		w++;
	}

	// background boxes
	LDrawRect (loc,0,0,52,totalheight + 20,color);				// group
	LDrawRect (loc,53,0,225,totalheight + 20,color);			//weapon
//	LDrawRect (loc,0,0,52,((m_NumWeapons + 1)* 15) + 3,color);				// group
//	LDrawRect (loc,53,0,225,((m_NumWeapons + 1)* 15) + 3,color);			//weapon

	// frames and header boxes
	color = MakeColor (0,175,0,150);
//	LDrawFrame (loc,0,0,51,((m_NumWeapons + 1)* 15) + 3,color);
//	LDrawSpecFrame (loc,0,0,225,totalheight + 20,color);
	LDrawRect (loc,3,3,48,17,color);
//	LDrawFrame (loc,51, 0 , 225, ((m_NumWeapons + 1)* 15) + 3,color);
//	LDrawFrame (loc,51, 0 , 225, totalheight + 20,color);

	LDrawRect (loc,50, 3 , 68, 17,color);
	LDrawRect (loc,70, 3 , 192, 17,color);
	LDrawRect (loc,194, 3 , 223, 17,color);
	LDrawSpecFrame (loc,0,0,225,totalheight + 20,color);

	int weapony = 18;

	m_Text[0]->TopLeft (loc.x,loc.y);
	m_Text[0]->BottomRight (loc.x+195,loc.y+20);
	m_Text[1]->TopLeft (loc.x,loc.y);
	m_Text[1]->BottomRight (loc.x+195,loc.y+20);
	m_Text[2]->TopLeft (loc.x,loc.y);
	m_Text[2]->BottomRight (loc.x+195,loc.y+20);

	m_Text[0]->Draw (Point3D (loc.x+6,loc.y+3,0.9f));			// group
	m_Text[1]->Draw (Point3D (loc.x+54,loc.y+3,0.9f));		// status
	m_Text[2]->Draw (Point3D (loc.x+75,loc.y+3,0.9f));		// weapon

	
	m_Text[3]->TopLeft (loc.x,loc.y);
	m_Text[3]->BottomRight (loc.x+223,loc.y+20);
	DWORD textw,texth;
	m_Text[3]->DrawSize (textw,texth);
	m_Text[3]->Draw (Point3D (loc.x+223-textw,loc.y+3,0.9f));

	color = MakeColor (0,50,0,150);

	Verify (m_NumWeapons);
	LDrawRect (loc,4, weapony + 1 , 46, (weapony+totalheight-2),color);
//	LDrawRect (loc,4, weapony - 1 , 46, (weapony+(15*(m_NumWeapons-1)) + 11),color);

	ChainIteratorOf<GUIWeapon *> iterator(&m_Weapons);
	w = 0;
	while ((weapon = iterator.ReadAndNext()) != NULL)
	{
		DWORD namewidth,nameheight;
		canhit = false;
		const Weapon::GameModel *weapon_model = weapon->m_weapon->GetGameModel();
		Check_Object(weapon_model);

		char text[10];

//		name_array[w]->UpdateText (weapon->weaponName);
		name_array[w]->DrawSize (namewidth,nameheight);

		itoa (DECRYPT(*weapon->ammoCount),text,10);
// Added ammocount for ammocount check against canhit check
// MSL 5.00
        int ammocount=DECRYPT(*weapon->ammoCount);
		ammo_text[w]->UpdateText (text);
		color = MakeColor (0,50,0,150);

		DWORD weaponcolor=MakeColor (0,150,0,200);
		DWORD selectedcolor=MakeColor (0,0,175,255);
		const Scalar scurr_time = (Scalar)gos_GetElapsedTime();
		int icurr_time = (int)scurr_time;
		DWORD a;
		char buffer[256];

		switch (weapon->m_status)
		{
			case 0:// ready
// MSL 5.00 Added ammocount check to canhit check
// MSL 5.05 Weapon Facing Check
				if ((weapon->m_Range >= m_ReticleRange) && (!(ammocount == 0)))
				{
					canhit = weapon->m_weapon->CanFireFromCurrentArm();
					//canhit = true;
				}
				else
				{
                    canhit = false;
				}
				if (canhit)
				{
					color = MakeColor (0,255,0,255);
					selectedcolor = MakeColor (0,0,255,255);
				}
				else
				{
					color = MakeColor (0,175,0,255);
					selectedcolor = MakeColor (0,0,175,255);
				}
				weaponcolor = MakeColor (0,175,0,255);
				LDrawRect (loc, 52, weapony+1  ,(int) (66), weapony + nameheight-4,weaponcolor);
			break;
			case 1:// reloading
				if (weapon->m_Range >= m_ReticleRange)
					color = MakeColor (255,227,0,125);
				else
					color = MakeColor (186,166,0,125);
				weaponcolor = MakeColor (225,200,0,125);
	
				reload = (14.0f * weapon->weaponCount) / weapon_model->reloadTime;
				LDrawRect (loc, 52, weapony+1  ,(int) (66 - reload), weapony + nameheight-4,color);
//				reload = (122.0f * weapon->weaponCount) / weapon_model->reloadTime;
//				LDrawRect (loc, 70, weapony + 1 ,(int) (192 - reload), weapony + 9,color);
				break;
			case 2:// destroyed
				color = MakeColor (0,0,0,150);
				weaponcolor = MakeColor (0,0,0,200);
				break;
// MSL 5.03 Weapon Jammed
			case 3:// jammed
				color = MakeColor (185,50,50,150);
				weaponcolor = MakeColor (230,50,50,150);
				reload = (14.0f * weapon->weaponJamCount) / RECOVER_FROM_JAMMING_TIME; //add in weapon->jamtime later

				LDrawRect (loc, 52, weapony+1  ,(int) (66 - reload), weapony + nameheight-4,color);
				if (icurr_time%2)
				{
					color = MakeColor (0,0,0,0);
					weaponcolor = MakeColor (250,240,225,150);
					color = MakeColor (230,50,50,150);

					strcpy (buffer,MWApplication::GetInstance()->GetLocString(IDS_WEAPON_JAMMED));
					for (a=0;a<strlen (buffer);a++)
					{
						buffer[a] = (char) toupper (buffer[a]);
					}
					name_array[w]->Color(250,240,225,150);
					name_array[w]->UpdateText (buffer);
					int txtSize = strlen((const char *)buffer);
					LDrawRect (loc, 70, weapony+1, (int)(70 + (txtSize*9) + 5), weapony+nameheight-4, color);
				}
				break;
			case 4:// Ammo Bay Fire
				color = MakeColor (185,50,50,150);
				weaponcolor = MakeColor (230,50,50,150);
				reload = (14.0f * weapon->weaponFireCount) / TIME_AMMO_BAY_FIRE; //add in weapon->jamtime later
				LDrawRect (loc, 52, weapony+1  ,(int) (66 - reload), weapony + nameheight-4,color);
				if (icurr_time%2)
				{
					color = MakeColor (0,0,0,0);
					weaponcolor = MakeColor (250,240,225,150);
					color = MakeColor (230,50,50,150);

					strcpy (buffer,MWApplication::GetInstance()->GetLocString(IDS_AMMO_BAY_FIRE));
					for (a=0;a<strlen (buffer);a++)
					{
						buffer[a] = (char) toupper (buffer[a]);
					}
					name_array[w]->Color(250,240,225,150);
					name_array[w]->UpdateText (buffer);
					int txtSize = strlen((const char *)buffer);
					LDrawRect (loc, 70, weapony+1, (int)(70 + (txtSize*9) + 5), weapony+nameheight-4, color);
				}
				break;
		}

		if (weapon->m_weapon->IsInWeaponGroup1 ())												// groups
		{
			if (canhit)
				m_CanHit[0] = true;
			if ((weapon->m_status == 0) && (m_SelectedWeaponGroup == 1))
				LDrawRect (loc, 5, weapony + 1 , 11, weapony + nameheight-4,selectedcolor);
			else
				LDrawRect (loc, 5, weapony + 1 , 11, weapony + nameheight-4,color);
		}
		if (weapon->m_weapon->IsInWeaponGroup2 ())
		{
			if (canhit)
				m_CanHit[1] = true;
			if ((weapon->m_status == 0) && (m_SelectedWeaponGroup == 2))
				LDrawRect (loc, 12, weapony + 1 , 18, weapony + nameheight-4,selectedcolor);
			else
				LDrawRect (loc, 12, weapony + 1 , 18, weapony + nameheight-4,color);
		}
		if (weapon->m_weapon->IsInWeaponGroup3 ())
		{
			if (canhit)
				m_CanHit[2] = true;
			if ((weapon->m_status == 0) && (m_SelectedWeaponGroup == 3))
				LDrawRect (loc, 19, weapony + 1 , 25, weapony + nameheight-4,selectedcolor);
			else
				LDrawRect (loc, 19, weapony + 1 , 25, weapony + nameheight-4,color);
		}
		if (weapon->m_weapon->IsInWeaponGroup4 ())
		{
			if (canhit)
				m_CanHit[3] = true;
			if ((weapon->m_status == 0) && (m_SelectedWeaponGroup == 4))
				LDrawRect (loc, 26, weapony + 1 , 32, weapony + nameheight-4,selectedcolor);
			else
				LDrawRect (loc, 26, weapony + 1 , 32, weapony + nameheight-4,color);
		}
		if (weapon->m_weapon->IsInWeaponGroup5 ())
		{
			if (canhit)
				m_CanHit[4] = true;
			if ((weapon->m_status == 0) && (m_SelectedWeaponGroup == 5))
				LDrawRect (loc, 33, weapony + 1 , 39, weapony + nameheight-4,selectedcolor);
			else
				LDrawRect (loc, 33, weapony + 1 , 39, weapony + nameheight-4,color);
		}
		if (weapon->m_weapon->IsInWeaponGroup6 ())
		{
			if (canhit)
				m_CanHit[5] = true;
			if ((weapon->m_status == 0) && (m_SelectedWeaponGroup == 6))
				LDrawRect (loc, 40, weapony + 1 , 46, weapony + nameheight-4,selectedcolor);
			else
				LDrawRect (loc, 40, weapony + 1 , 46, weapony + nameheight-4,color);
		}

		Point3D numsize = m_Textures[20]->Size ();
		for (i=0;i<6;i++)
		{
			m_Textures[20+i]->Draw (Point3D (loc.x+group_loc[i]+1,loc.y+weapony+1,0.9f),numsize,MakeColor (0,0,0,255));
		}
//		m_Text[4]->TopLeft (loc.x,loc.y);
//		m_Text[4]->BottomRight (loc.x+195,loc.y + ((m_NumWeapons + 1)* 15));
//		m_Text[4]->BottomRight (loc.x+195,loc.y + totalheight);
//		m_Text[4]->Draw (Point3D (loc.x+5,loc.y+weapony,0.9f));
		
		Verify (name_array[w]);
		name_array[w]->Color (weaponcolor);
		name_array[w]->TopLeft (loc.x,loc.y);
//		name_array[w]->BottomRight (loc.x+192, loc.y + ((m_NumWeapons + 1)* 15) + 2);
		name_array[w]->BottomRight (loc.x+192, loc.y + totalheight + 2);

// MSL 5.03 OUT OF AMMO
//		name_array[w]->Draw (Point3D (loc.x+75,loc.y+weapony,0.9f));
		if (weapon->m_weapon == m_SingleFire.GetCurrent ())
		{
			DWORD color = MakeColor (0,0,255,255);
			DrawFrame ((int) (loc.x+72),(int) (loc.y+weapony),(int) (loc.x+190),(int) (loc.y+weapony+nameheight-5),color);
		}
		
		if ((DECRYPT(*weapon->ammoCount) > -1) && (weapon->m_status != 2))
		{
			if (DECRYPT(*weapon->ammoCount) == 0)
			{
				ammo_text[w]->Color (MakeColor (0xaa,0,0,255));
				DWORD a;
				char buffer[256];
				const Scalar scurr_time = (Scalar)gos_GetElapsedTime();
				int icurr_time = (int)scurr_time;
				if (icurr_time%2)
				{
					color = MakeColor (0,0,0,0);
					weaponcolor = MakeColor (250,240,225,150);
					color = MakeColor (230,50,50,150);
					//LDrawRect (loc, 70, weapony+1, (int)(70 + 100), weapony+nameheight-4, color);

					strcpy (buffer,MWApplication::GetInstance()->GetLocString(IDS_OUT_OF_AMMO));
					for (a=0;a<strlen (buffer);a++)
					{
						buffer[a] = (char) toupper (buffer[a]);
					}
					name_array[w]->Color(250,240,225,150);
					name_array[w]->UpdateText (buffer);
					int txtSize = strlen((const char *)buffer);
					LDrawRect (loc, 70, weapony+1, (int)(70 + (txtSize*9) + 5), weapony+nameheight-4, color);
				}
			}
			else
			{
				ammo_text[w]->Color (weaponcolor);
			}
			ammo_text[w]->TopLeft (loc.x,loc.y);
			ammo_text[w]->BottomRight (loc.x+225,(Scalar) (weapony + totalheight));
//			ammo_text[w]->BottomRight (loc.x+223,loc.y + ((m_NumWeapons + 1)* 15));
			DWORD textw,texth;
			ammo_text[w]->DrawSize (textw,texth);
			ammo_text[w]->Draw (Point3D (loc.x+225-textw,loc.y+weapony-2,0.9f));
		}
		else if (DECRYPT(*weapon->ammoCount) == -1)
		{
			LDrawLine (loc,204,weapony+(nameheight/2),213,weapony+(nameheight/2),weaponcolor);
		}

		name_array[w]->Draw (Point3D (loc.x+75,loc.y+weapony,0.9f));

//		weapony += (nameheight+1);
		weapony += (nameheight-3);
		w++;
	}
}
else
if(mfd_device.BeginChannel(4))
{
	extern BOOL g_baChainFireMode[];

	mfd_device.pD3DDevice->SetTexture(0,0);
	mfd_device.pD3DDevice->SetRenderState( D3DRENDERSTATE_ALPHABLENDENABLE, FALSE );

    static int s_naStates[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };

    static char mfd_text_weapon[8][32]={
	    "GROUP FIRE","CHAINFIRE","NEXTWEAPON","PREVWEAPON",
	    "MODE","ASSIGNGROUP1","ASSIGNGROUP2","ASSIGNGROUP3"
    };

    switch(GetFiringMode())
    {
    case 0: // mixed
        s_naStates[0] = s_naStates[1] = 1;
        break;
    case 1: // group
        s_naStates[0] = 2;
        s_naStates[1] = 1;
        break;
    case 2: // chain
        s_naStates[0] = 1;
        s_naStates[1] = 2;
        break;
    }

	mfd_device.DrawMFDBackText(mfd_text_weapon, s_naStates);

	const int lm=10;//left margin +lm
	const int tm=45;//top margin +tm

	const DWORD normal_color=0xFFFFFFFF;
	const DWORD load_color=0xFFA0A0A0;
	const DWORD out_color=0xFF808080;
	const DWORD break_color=0xFF606060;
	const DWORD nogroup_color=0xFF606060;

	for (int i=0;i<3;i++) m_CanHit[i] = false;

	if (m_Weapons.GetSize () == 0)return;

	m_NumWeapons = m_Weapons.GetSize ();

	Stuff::Scalar reload;

	GUIWeapon *weapon;
	int totalheight=0;
	int totalcount=0;
	ChainIteratorOf<GUIWeapon *> iterator2(&m_Weapons);
	while ((weapon = iterator2.ReadAndNext()) != NULL){
		totalheight+=30;
		totalcount++;
	}


	int rh=25;//row_height
	//Assumes up to 16 rows (including header row).
	// background boxes

	mfd_device.DrawQuad(0+lm,0+tm,96+lm,(totalcount+1)*rh+10+tm,0xFF404040);
	mfd_device.DrawQuad(98+lm,0+tm,620+lm,(totalcount+1)*rh+10+tm,0xFF404040);

	// frames and header boxes
	mfd_device.DrawQuad(  5+lm,5+tm, 95+lm,rh+5+tm,0xFFFFFFFF);
	mfd_device.DrawQuad(100+lm,5+tm,150+lm,rh+5+tm,0xFFFFFFFF);
	mfd_device.DrawQuad(155+lm,5+tm,545+lm,rh+5+tm,0xFFFFFFFF);
	mfd_device.DrawQuad(550+lm,5+tm,615+lm,rh+5+tm,0xFFFFFFFF);
	mfd_device.DrawFrame(0+lm,0+tm,620+lm,(totalcount+1)*rh+10+tm,0xFFFFFFFF);

	// Titles
	mfd_device.pFont[0].DrawText((  5+ 95)/2+lm,5+rh/2+tm,0xFF000000,"GROUP",TEXTALIGN_ORG);
	mfd_device.pFont[0].DrawText((100+150)/2+lm,5+rh/2+tm,0xFF000000,"R",TEXTALIGN_ORG);
	mfd_device.pFont[0].DrawText((155+545)/2+lm,5+rh/2+tm,0xFF000000,"WEAPON",TEXTALIGN_ORG);
	mfd_device.pFont[0].DrawText((550+615)/2+lm,5+rh/2+tm,0xFF000000,"A",TEXTALIGN_ORG);

	Verify (m_NumWeapons);

	ChainIteratorOf<GUIWeapon *> iterator(&m_Weapons);
	int weapony = 0;
	bool canhit;

	while ((weapon = iterator.ReadAndNext()) != NULL)
	{
		canhit = false;
		const Weapon::GameModel *weapon_model = weapon->m_weapon->GetGameModel();
		Check_Object(weapon_model);

		char text[10];

		int ammocount=DECRYPT(*weapon->ammoCount);
		itoa (ammocount,text,10);

		char weapon_name[256];
// MSL 5.05 Rear Firing Weapons
		if (weapon->m_weapon->GetWeaponFacing() == 1)
		{
			std::string s;
			s = weapon->weaponName;
			s += " (Rear)";
			strcpy (weapon_name, (char*)s.c_str());
		}
		else
		{
			strcpy (weapon_name,weapon->weaponName);
		}

		MakeStringCaps(weapon_name);

		DWORD hsh_color1=0,hsh_color2=0;
		
		//Weapon fire-ready status: define exact color for each state.

		//Normal (target in range)(can fire)	0xFFFFFFFF
		//Normal (target out of range)(can fire)    0xFFFFFFFF
		//Reloading (cannot fire)				0xFFA0A0A0
		//Out of ammo (cannot fire)		0xFF808080
		//Destroyed (cannot fire)				0xFF606060
		DWORD nameheight=30;
		int top=5+(weapony+1)*rh;
		int bottom = top+rh;
		int center=top+rh/2;
		DWORD lcolor=break_color;
		switch (weapon->m_status)
		{
			case 0://normal case
// MSL 5.00 Added ammocount check against canhit check
// MSL 5.05 Weapon Facing Check
				if ((weapon->m_Range >= m_ReticleRange) && (!(ammocount == 0)))
				{
					canhit = weapon->m_weapon->CanFireFromCurrentArm();
				}
				else
					canhit = false;
                // canhit = weapon->m_Range >= m_ReticleRange; // replaced by above
// MSL 5.00 Added for Ammocount check

				if (ammocount == 0)
					lcolor=out_color;
				else
					lcolor=normal_color;
				mfd_device.DrawQuad(100+lm,top+2+tm,150+lm, bottom-2+tm,lcolor); // Changed to lcolor from normal_color
				break;
			case 1://reloading
// MSL 5.00 Added for Ammocount check
				if (ammocount == 0)
					lcolor=out_color;
				else
				lcolor=load_color;
				reload = (50.0f * weapon->weaponCount) / weapon_model->reloadTime;
				mfd_device.DrawQuad(100+lm,top+2+tm,150-reload+lm, bottom-2+tm,lcolor); // Changed to lcolor from load_color
				break;
			case 2://destroyed... break out
				lcolor=break_color;
				break;
			case 3://jammed... break out
				{
//					DWORD a;
//					char buffer[256];
					const Scalar scurr_time = (Scalar)gos_GetElapsedTime();
					int icurr_time = (int)scurr_time;
					if (icurr_time%2)
					{
						lcolor=normal_color;
						reload = (50.0f * weapon->weaponJamCount) / RECOVER_FROM_JAMMING_TIME; //add in weapon->jamtime later
						mfd_device.DrawQuad(100+lm,top+2+tm,150-reload+lm, bottom-2+tm,lcolor);
					}
					else
					{
						lcolor=load_color;
						reload = (50.0f * weapon->weaponJamCount) / RECOVER_FROM_JAMMING_TIME; //add in weapon->jamtime later
						mfd_device.DrawQuad(100+lm,top+2+tm,150-reload+lm, bottom-2+tm,lcolor);
					}
				}
				break;
			case 4://Ammo Bay Fire... break out
				lcolor=load_color;
				reload = (50.0f * weapon->weaponFireCount) / TIME_AMMO_BAY_FIRE; //add in weapon->jamtime later
				mfd_device.DrawQuad(100+lm,top+2+tm,150-reload+lm, bottom-2+tm,lcolor);
				break;
		}
		if(ammocount== 0){
			hsh_color1=0x8800;
			hsh_color2=0xFF808080;
		}
		
		bool weapon_flag[3]={0,};

		if(weapon->m_status!=2){
			if (weapon->m_weapon->IsInWeaponGroup1 ()){weapon_flag[0]=true;if (canhit)m_CanHit[0] = true;}
			if (weapon->m_weapon->IsInWeaponGroup2 ()){weapon_flag[1]=true;if (canhit)m_CanHit[1] = true;}
			if (weapon->m_weapon->IsInWeaponGroup3 ()){weapon_flag[2]=true;if (canhit)m_CanHit[2] = true;}
		}
		char *weapon_string[3]={"1","2","3"};
		

		//Display the status of the weapon group.
		for(int i=0;i<3;i++){
			DWORD weapon_color=weapon_flag[i]?normal_color:nogroup_color;
// MSL 5.00 Added Ammocount check, if = 0 then weapon name same as ammo nogroup_color
			if (ammocount == 0)
				weapon_color = nogroup_color;
			mfd_device.DrawQuad(5+30*i+lm,top+2+tm , 5+26+30*i+lm, bottom-2+tm,weapon_color);
			mfd_device.pFont[0].DrawText(5+26/2+30*i+lm,center+tm,0xFF000000,weapon_string[i],TEXTALIGN_ORG);
		}


		Point3D numsize = m_Textures[20]->Size ();
		
		//Display weapon name.
// MSL 5.00 Added Ammocount check, if = 0 then weapon name same as ammo out_color
		switch (weapon->m_status)
		{
			case 0://normal case
				if (ammocount == 0)
				{
//					const Scalar scurr_time = (Scalar)gos_GetElapsedTime();
//					int icurr_time = (int)scurr_time;
//					if (icurr_time%2)
//					{
						mfd_device.DrawQuad(155+lm,top+1+tm , 545+lm, bottom-2+tm,0xFF404040);
						mfd_device.pFont[0].DrawText(155+5+lm,center+tm,out_color,"OUT OF AMMO",TEXTALIGN_VCENTER);
//					}
//					else
//					{
//						mfd_device.DrawQuad(155+lm,top+1+tm , 545+lm, bottom-2+tm,0xFF404040);
//						mfd_device.pFont[0].DrawText(155+5+lm,center+tm,out_color,weapon_name,TEXTALIGN_VCENTER);
//					}
				}
				else
				{
					mfd_device.pFont[0].DrawText(155+5+lm,center+tm,lcolor,weapon_name,TEXTALIGN_VCENTER);
				}
				break;
			case 2:
				{
//					const Scalar scurr_time = (Scalar)gos_GetElapsedTime();
//					int icurr_time = (int)scurr_time;
//					if (icurr_time%2)
//					{
						mfd_device.DrawQuad(155+lm,top+1+tm , 545+lm, bottom-2+tm,0xFF404040);
						mfd_device.pFont[0].DrawText(155+5+lm,center+tm,out_color,"WEAPON DESTROYED",TEXTALIGN_VCENTER);
//					}
//					else
//					{
//						mfd_device.DrawQuad(155+lm,top+1+tm , 545+lm, bottom-2+tm,0xFF404040);
//						mfd_device.pFont[0].DrawText(155+5+lm,center+tm,out_color,weapon_name,TEXTALIGN_VCENTER);
//					}
				}
				break;
			case 3:
				{
					const Scalar scurr_time = (Scalar)gos_GetElapsedTime();
					int icurr_time = (int)scurr_time;
					if (icurr_time%2)
					{
						mfd_device.DrawQuad(155+lm,top+1+tm , 545+lm, bottom-2+tm,out_color);
						mfd_device.pFont[0].DrawText(155+5+lm,center+tm,normal_color,"WEAPON JAMMED",TEXTALIGN_VCENTER);
					}
					else
					{
						mfd_device.DrawQuad(155+lm,top+1+tm , 545+lm, bottom-2+tm,0xFF404040);
						mfd_device.pFont[0].DrawText(155+5+lm,center+tm,out_color,weapon_name,TEXTALIGN_VCENTER);
					}
				}
				break;
			case 4:
				{
					const Scalar scurr_time = (Scalar)gos_GetElapsedTime();
					int icurr_time = (int)scurr_time;
					if (icurr_time%2)
					{
						mfd_device.DrawQuad(155+lm,top+1+tm , 545+lm, bottom-2+tm,normal_color);
						mfd_device.pFont[0].DrawText(155+5+lm,center+tm,out_color,"AMMO BAY FIRE",TEXTALIGN_VCENTER);
					}
					else
					{
						mfd_device.DrawQuad(155+lm,top+1+tm , 545+lm, bottom-2+tm,0xFF404040);
						mfd_device.pFont[0].DrawText(155+5+lm,center+tm,out_color,weapon_name,TEXTALIGN_VCENTER);
					}
				}
				break;
			default:
				{
					mfd_device.pFont[0].DrawText(155+5+lm,center+tm,lcolor,weapon_name,TEXTALIGN_VCENTER);
				}
		}

		//Display frame for the selected weapon currently fired with Enter.
		if (weapon->m_weapon == m_SingleFire.GetCurrent ()){
			mfd_device.DrawFrame(155+lm,top+1+tm,545+lm,bottom-1+tm,0xFFFFFFFF);
		}
		
		DWORD ammocount_color= ammocount == 0 ? out_color:normal_color;
		if ((ammocount > -1) && (weapon->m_status != 2)){
			mfd_device.pFont[0].DrawText(615-5+lm,center+tm,ammocount_color,text,TEXTALIGN_VCENTER|TEXTALIGN_RIGHT);
		}else if (ammocount == -1){
			//Display '-' for weapon types with unlimited ammo.
			mfd_device.pFont[0].DrawText(615-5+lm,center+tm,ammocount_color,"-",TEXTALIGN_VCENTER|TEXTALIGN_RIGHT);
		}
		weapony ++;
	}

	mfd_device.EndChannel();
}
else
{
	for (int i=0;i<3;i++) m_CanHit[i] = false;

	if (m_Weapons.GetSize () == 0)return;

	m_NumWeapons = m_Weapons.GetSize ();

	GUIWeapon *weapon;
	ChainIteratorOf<GUIWeapon *> iterator2(&m_Weapons);

	Verify (m_NumWeapons);

	ChainIteratorOf<GUIWeapon *> iterator(&m_Weapons);
	bool canhit;

	while ((weapon = iterator.ReadAndNext()) != NULL)
	{
		canhit = false;
		const Weapon::GameModel *weapon_model = weapon->m_weapon->GetGameModel();
		Check_Object(weapon_model);

		int ammocount=DECRYPT(*weapon->ammoCount);

		switch (weapon->m_status)
		{
			case 0://normal case
// Added ammocount check against canhit check
// MSL 5.00
					if (g_bNoWeaponRangeCheck)
					{
						// mw4.exe -nowrc // no weapon range check...
						// enable this if no WeaponRange check.... - jcem only... for light-green-WeaponGroupReady Box
						canhit = (ammocount != 0);
					}
					else
					{
						// canhit = weapon->m_Range >= m_ReticleRange; // MSL - replaced by above
// MSL 5.05 Weapon Facing Check
						if ((weapon->m_Range >= m_ReticleRange) && (!(ammocount == 0)))
						{
							canhit = weapon->m_weapon->CanFireFromCurrentArm();

						}
						else
						{
							canhit = false;
						}
					}
                // canhit = weapon->m_Range >= m_ReticleRange; // replaced by above

// Added for Ammocount check
// MSL 5.00
				break;
			case 1://reloading
// Added for Ammocount check
// MSL 5.00
				break;
			case 2://destroyed... break out
				break;
			case 3://jammed
				break;
			}
			if(weapon->m_status!=2)
			{
				if (weapon->m_weapon->IsInWeaponGroup1 ())
				{
					if (canhit)m_CanHit[0] = true;
				}
				if (weapon->m_weapon->IsInWeaponGroup2 ())
				{
					if (canhit)m_CanHit[1] = true;
				}
				if (weapon->m_weapon->IsInWeaponGroup3 ())
				{
					if (canhit)m_CanHit[2] = true;
				}
			}
		}
	}
}

void HUDWeapon::SingleFire (Weapon *weap)			// -1 means no single fire mode
{
	m_SingleFire.Remove ();
	if (weap)
		m_SingleFire.Add (weap);
}


void HUDWeapon::SelectedWeaponGroup (int selected)
{
	m_SelectedWeaponGroup = selected;
}
