#define UTIL_DAMAGEFRAME_H	aaa
//----------------------------------------------------------------------------
//  @brief
//        в данном файле сделана работа с рамкой, которая отображает
//        урон, наносимый герою

//  @ingroup
    
//  2004Sep10 created. Zmanovsky Peter.
//  2007Sep11 перенесено из AS2 в FrankenShooter.
//  (C) 2004-2007    Sigma-Team
//-----------------------------------------------------------------------------

//нормальная гамма для рамки
#define DEFAULT_ALPHA_FOR_HPFRAME                       0x7F
//максимальная мигнутая гамма для рамки
#define BLINK_ALPHA_FOR_HPFRAME                         0x00
//минимальная мигнутая гамма для рамки
#define BLINK_MIN_ALPHA_FOR_HPFRAME                     0x6F

//минимальная мигнутая гамма для глаза
#define BLINK_MIN_ALPHA_FOR_HPFRAME2                    DEFAULT_ALPHA_FOR_HPFRAME

//-----------------------------------------------------------------
//время начала мигания
static int aStartHpFrameBlinkTime[2];
//альфа, к которой нужно стремиться при мигании. высчитывается через проценты урона
static int gaBeginAlpha[2];
static int gaBlinkAlpha[2];
static int gaFramePrevAlpha[2] = {999999, 999999};
static int FrameSprite = 0;
static bool gbPlayerWasDead = false;

//-----------------------------------------------------------------
DecrHeroSpeed();

//-----------------------------------------------------------------
SetAlphaForHpSprites(int iType, int iGamma)
{
	int iVid = HPFRAME_SPRITE_VID;
	if(iType == 1)
		iVid = 962;
	//
	gaFramePrevAlpha[iType] = iGamma;
	//
	//if(Z_DEBUG2)
	//	Log3("iGamma = " + itoa(iGamma));
	//
	iGamma = iGamma << 24;
	SetVidData(iVid, VID_GAMMA, iGamma );
	SetVidData(iVid, VID_GAMMA+1, iGamma );
	SetVidData(iVid, VID_GAMMA+2, iGamma );
	SetVidData(iVid, VID_GAMMA+3, iGamma );
}


//-----------------------------------------------------------------
InitHpFrameSprite()
{
    iff(1)
    {
		aStartHpFrameBlinkTime[0] = 0;
		aStartHpFrameBlinkTime[1] = 0;
		gaBlinkAlpha[0] = DEFAULT_ALPHA_FOR_HPFRAME;
		gaBlinkAlpha[1] = DEFAULT_ALPHA_FOR_HPFRAME;
		gaBeginAlpha[0] = DEFAULT_ALPHA_FOR_HPFRAME;
		gaBeginAlpha[1] = DEFAULT_ALPHA_FOR_HPFRAME;
    }
    //
    SetAlphaForHpSprites(0, DEFAULT_ALPHA_FOR_HPFRAME);
    SetAlphaForHpSprites(1, DEFAULT_ALPHA_FOR_HPFRAME);
}


//-----------------------------------------------------------------
HpFrameSpriteTact(int iType)
{
	if(iType == 1)		//больше инфекцию тут не трогаем
		return;
	//
    if(aStartHpFrameBlinkTime[iType])
    {
    	bool bEndBlinking = false;
    	//
        int DeltaTime = GetTime() - aStartHpFrameBlinkTime[iType];
		//
		int time1 = BLINK_TIME_SPRITE_1[iType];
		int time2 = BLINK_TIME_SPRITE_2[iType];
		int time3 = BLINK_TIME_SPRITE_3[iType];
		//
		if(gbPlayerWasDead)
		{
			int TIME_ZZZ = 500;
			if(DeltaTime <= TIME_ZZZ)
			{
	       	    int alpha = CountAlpha(gaBlinkAlpha[iType], DEFAULT_ALPHA_FOR_HPFRAME, DeltaTime * 255 / TIME_ZZZ);
    	        SetAlphaForHpSprites(iType, alpha);
			}
			else
				bEndBlinking = true;
		}
		else
        if(DeltaTime <= time1 && time1)
        {
        	int alpha = CountAlpha(gaBeginAlpha[iType], gaBlinkAlpha[iType], DeltaTime * 255 / time1);
        	//проверка, чтобы в ноль не сбрасывалась при повторном ударе - чтобы не моргало
        	//щас по идее уже не надо, но на вский случай оставлю, чтоб никакх глючков не вылазило
			if(alpha <= gaFramePrevAlpha[iType])
            	SetAlphaForHpSprites(iType, alpha);
		}
        else
        if(DeltaTime <= time1+time2 && time2)
   	    {
       		SetAlphaForHpSprites(iType, gaBlinkAlpha[iType]);
        }
   	    else
       	if(DeltaTime <= time1+time2+time3 && time3)
        {
   	        DeltaTime -= (time1+time2);
       	    int alpha = CountAlpha(gaBlinkAlpha[iType], DEFAULT_ALPHA_FOR_HPFRAME, DeltaTime * 255 / time3);
            SetAlphaForHpSprites(iType, alpha);
   	    }
       	else
			bEndBlinking = true;
		//
		if(bEndBlinking)
		{
   	        aStartHpFrameBlinkTime[iType] = 0;
       	    gaBlinkAlpha[iType] = DEFAULT_ALPHA_FOR_HPFRAME;
            SetAlphaForHpSprites(iType, DEFAULT_ALPHA_FOR_HPFRAME);
   	        if(iType == 1)
   	        {
                Destroy(FrameSprite);
                FrameSprite = 0;
			}
			gbPlayerWasDead = false;
		}
    }
}


//-----------------------------------------------------------------
BlinkHpFrameSprite(int iType, int iBlinkPercent)
{
	if(iType == 1)		//больше инфекцию тут не трогаем
		return;
	//
    //сделаем накопительную систему
	aStartHpFrameBlinkTime[iType] = GetTime();
	//
	//новая версия
	int iAddAlpha = (BLINK_ALPHA_FOR_HPFRAME - DEFAULT_ALPHA_FOR_HPFRAME)*iBlinkPercent/100;
	//
	if(gbPlayerWasDead)
	{
		gaBlinkAlpha[iType] = gaFramePrevAlpha[iType] + iAddAlpha;
		gbPlayerWasDead = false;
	}
	else
	if(aStartHpFrameBlinkTime[iType] == 0)
		gaBlinkAlpha[iType] = DEFAULT_ALPHA_FOR_HPFRAME+iAddAlpha;
	else
	if(aStartHpFrameBlinkTime[iType] <= BLINK_TIME_SPRITE_1[iType])
		gaBlinkAlpha[iType] += iAddAlpha;
	else
		gaBlinkAlpha[iType] = gaFramePrevAlpha[iType] + iAddAlpha;
	//
	if(gaBlinkAlpha[iType] < BLINK_ALPHA_FOR_HPFRAME)
		gaBlinkAlpha[iType] = BLINK_ALPHA_FOR_HPFRAME;
	//
	if(gaBlinkAlpha[iType] > BLINK_MIN_ALPHA_FOR_HPFRAME)
		gaBlinkAlpha[iType] = BLINK_MIN_ALPHA_FOR_HPFRAME;
	//
	gaBeginAlpha[iType] = gaFramePrevAlpha[iType];
	if(gaBeginAlpha[iType] < gaBlinkAlpha[iType])
		gaBeginAlpha[iType] = gaBlinkAlpha[iType];
	//
	Create962IfNeeded();
}


//-----------------------------------------------------------------
Create962IfNeeded()
{
	if(/*aStartHpFrameBlinkTime[1]!=0 && */!FrameSprite)
		FrameSprite = CreateSprite(962, ScreenX()/2, ScreenY()/2+450, 450);
}


//-----------------------------------------------------------------
F009_DAMAGE( int unit, int beating_unit, int damage )
{
	if(damage < 0)		//аптечка
		return 0;
	//
	bool bSurvive = false;
	if(LevelNumber < 0)
		bSurvive = true;
	//
	if(!bSurvive)
		damage = damage * giHeroDmgKoef100 / 100;
	//
	int CurHp = Action( unit, ACT_GET_HP );
	if( CurHp > 0)
	{
		int blink = 0;
		int damageP = damage * 100 / CurHp;
		//
		if( damageP >= DMG_FRAME_MAX_DAMAGE )
			blink = 100;
		else
			blink = DMG_FRAME_BLINK_MIN + (100-DMG_FRAME_BLINK_MIN)*damageP / DMG_FRAME_MAX_DAMAGE;
		BlinkHpFrameSprite(0, blink);
		//
		int vid = GetUnitVid(beating_unit);
		/*if(vid==2121 || vid==2100)
			BlinkHpFrameSprite(1, FRAME_962_BLINK_PROCENT);
		else
		if(vid==1768 || vid==1791)
			BlinkHpFrameSprite(1, FRAME_962_BLINK_PROCENT2);
		else
		if(vid==2128)
			BlinkHpFrameSprite(1, FRAME_962_BLINK_2128);
		else*/
		if(vid==2108 && !bSurvive)
			DecrHeroSpeed();
	}
	//
	if(bSurvive)
		return 0;
	//
	return -damage;
}


//-----------------------------------------------------------------
DecrHeroSpeed()
{
	giHeroSpeedDecr1000 += DS_SPEED_DECREASE * 1000;
	if(giPlayerMaxSpeed0-giHeroSpeedDecr1000/1000 < DS_MIN_SPEED)
		giHeroSpeedDecr1000 = (giPlayerMaxSpeed0-DS_MIN_SPEED)*1000;
	//
	ResetHeroSpeed();
}


//-----------------------------------------------------------------
HeroSpeedTact()
{
	if(giHeroSpeedDecr1000 > 0)
	{
		int prev = giHeroSpeedDecr1000 / 1000;
		//
		giHeroSpeedDecr1000 -= giLastTactTime * DS_SPEED_DECREASE * 1000 / DS_SPEED_INCR_TIME;
		if(giHeroSpeedDecr1000 < 0)
			giHeroSpeedDecr1000 = 0;
		//
		if(prev != giHeroSpeedDecr1000 / 1000)
			ResetHeroSpeed();
	}
}


//-----------------------------------------------------------------
ResetInfection()
{
	giHeroSpeedDecr1000 = 0;

	//при смерти героя должен погасать за пол-секунды
	/*if(aStartHpFrameBlinkTime[1] != 0)
	{
		gaBlinkAlpha[1] = gaFramePrevAlpha[1];
		aStartHpFrameBlinkTime[1] = GetTime();
		gbPlayerWasDead = true;
	}*/
}


//-----------------------------------------------------------------
InfectionFrameTact()
{
    if(FrameSprite && Flagman(0))
        Action(FrameSprite, ACT_CHANGE_COOR, GetX(Flagman(0)), GetY(Flagman(0)) - GetZ(Flagman(0)) + 450,450);
	//
	//autohealing когда hp меньше 30%
	if(Flagman(0) && LevelNumber > 0)
	{
		int hp = Action(Flagman(0), ACT_GET_HP);
		int maxHp = GetVidData(9,VID_MAXHP0);
		//
		int min = maxHp * AUTOHEALING_HP_PERCENT / 100;
		//
		static int hp1000 = 0;	//эмуляция float`а
		int alpha;	//0..255
		if(hp < min)
		{
			hp1000 += 1000 * giLastTactTime / AUTOHEALING_SPEED;
			int d = hp1000 / 1000;
			if(d > 0)
			{
				hp1000 -= d * 1000;
				hp += d;
				if(hp > min)
					hp = min;
				Action(Flagman(0), ACT_SET_HP, hp);
			}
			alpha = hp * 255 / min;
		}
		//
		if(hp >= min)
		{
			hp1000 = 0;
			alpha = 255;
		}
		//
		//if(alpha == 255)
		//	SetAlphaForHpSprites(1, DEFAULT_ALPHA_FOR_HPFRAME);
		//else
		{
			int g1 = GetVidData(962, VID_GAMMA) >> 24;
			int g2 = CountAlpha(BLINK_ALPHA_FOR_HPFRAME, BLINK_MIN_ALPHA_FOR_HPFRAME2, alpha);
			//
			if(g1 != g2)
			{
				int max = g2-g1;
				int d = sgn(max) * Abs(BLINK_ALPHA_FOR_HPFRAME-BLINK_MIN_ALPHA_FOR_HPFRAME2) * giLastTactTime * 2 / 1000;
				if(Abs(d) > Abs(max))
					d = Abs(max) * sgn(d);
				g1 = g1 + d;
				//
				//Log2("g1 = " + itoa(g1) + ". g2 = " + itoa(g2));
				SetAlphaForHpSprites(1, g1);
			}
		}
	}
}

