/***
*
*	Copyright (c) 1996-2001, Valve LLC. All rights reserved.
*	
*	This product contains software technology licensed from Id 
*	Software, Inc. ("Id Technology").  Id Technology (c) 1996 Id Software, Inc. 
*	All Rights Reserved.
*
*   Use, distribution, and modification of this source code and/or resulting
*   object code is restricted to non-commercial enhancements to products from
*   Valve LLC.  All other use, distribution, or modification is prohibited
*   without written permission from Valve LLC.
*
****/
//
// Health.cpp
//
// implementation of CHudHealth class
// Redone by harSens, original file is health.bak
//

#include "STDIO.H"
#include "STDLIB.H"
#include "MATH.H"

#include "hud.h"
#include "cl_util.h"
#include "parsemsg.h"
#include <string.h>

#define BAR_OFFSET640 4
#define BAR_OFFSET320 2

DECLARE_MESSAGE(m_Health, Health )
DECLARE_MESSAGE(m_Health, MaxHealth)

int CHudHealth::Init(void)
{	HOOK_MESSAGE(Health);	
	HOOK_MESSAGE(MaxHealth);
	m_iMaxHealth = 100;
	
	m_iHealth = 100;
	gHUD.AddHudElem(this);
	m_iFlags |= HUD_ACTIVE;
	return 1;
}

int CHudHealth::VidInit(void)
{	m_HUD_mainbar = gHUD.GetSpriteIndex( "mainbar" );
	m_HUD_healthline = gHUD.GetSpriteIndex("healthline");
	m_HUD_lffront = gHUD.GetSpriteIndex("lffront");
	return 1;
}

int CHudHealth:: MsgFunc_MaxHealth(const char *pszName,  int iSize, void *pbuf )
{	BEGIN_READ( pbuf, iSize );
	m_iMaxHealth = READ_BYTE();
	return 1;
}

int CHudHealth:: MsgFunc_Health(const char *pszName,  int iSize, void *pbuf )
{	BEGIN_READ( pbuf, iSize );
	m_iHealth = READ_BYTE();
	return 1;
}

int CHudHealth::Draw(float flTime)
{	if (!(gHUD.m_iWeaponBits & (1<<(WEAPON_SUIT)) ))
		return 1;

	if ( (gHUD.m_iHideHUDDisplay & ( HIDEHUD_WEAPONS | HIDEHUD_ALL )) )
		return 1;

	int r,g,b,x,y;
	UnpackRGB(r,g,b, RGB_WHITEISH);
	
	int iWidth = gHUD.GetSpriteRect(m_HUD_lffront).right - gHUD.GetSpriteRect(m_HUD_lffront).left;
	int iHeigth = gHUD.GetSpriteRect(m_HUD_lffront).bottom - gHUD.GetSpriteRect(m_HUD_lffront).top;
	
	y = ScreenHeight - iHeigth; //assume KiHeigth==HealthHeigth==PowerLevelHeigth
	x = 0;
	SPR_Set(gHUD.GetSprite(m_HUD_lffront), r, g, b);
	SPR_Draw(0, x, y, &gHUD.GetSpriteRect(m_HUD_lffront));

	x += iWidth;
	iWidth = gHUD.GetSpriteRect(m_HUD_mainbar).right - gHUD.GetSpriteRect(m_HUD_mainbar).left;			
	
	SPR_Set(gHUD.GetSprite(m_HUD_mainbar), r, g, b);
	SPR_Draw(0, x, y, &gHUD.GetSpriteRect(m_HUD_mainbar));

	// draw line
	if (ScreenWidth<640)
	{	iWidth = iWidth - 2 * BAR_OFFSET320;
		x += BAR_OFFSET320;
	}
	else
	{	iWidth = iWidth - 2 * BAR_OFFSET640;
		x += BAR_OFFSET640;
	}
	int iBarFill = iWidth * m_iHealth / m_iMaxHealth;
	for (int i = 0;i<iBarFill;i++)
	{	SPR_Set(gHUD.GetSprite(m_HUD_healthline), r, g, b);
		SPR_DrawHoles(0, x + i, y, &gHUD.GetSpriteRect(m_HUD_healthline));
	}

	// draw numbers	
	int iNumberHeigth = gHUD.GetSpriteRect(gHUD.m_HUD_comma).bottom - gHUD.GetSpriteRect(gHUD.m_HUD_comma).top;
	int iNumberWidth = gHUD.GetNumWidth( m_iHealth, DHN_KI );
	
	//check if number fits in bar		
	if (iNumberHeigth<iHeigth && iNumberWidth < iWidth)
	{	int iNumberX = x + (iWidth - iNumberWidth) / 2;		
		int iNumberY = y + (iHeigth - iNumberHeigth) / 2;
		gHUD.DrawHudNumber(iNumberX, iNumberY, DHN_KI, m_iHealth, r, g, b);
	}	
	return 1;
}