#define  COMMON_STATISTICS_H aaa
//----------------------------------------------------------------------------
//  @brief
//        статистика после летательных уровней
//        

//  @ingroup
    
//  2006Oct05 created. Zmanovsky Peter.
//  (C) 2006    Sigma-Team
//-----------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
int ssPauseNorm[12+1] = {400,
                       250,		//1. показ кристаллов
                       500,		//2. пауза перед показом медальки
                       500,		//3. показ медальки
                       1500,	//4. полоска bonus points
                       500,		//5. показ медальки
                       1500,	//6. полоска level points
                       500,		//7. показ медальки
                       1500,	//8. полоска time points
                       500,		//9. показ медальки
                       500,		//10.показ ранга
                       500, 1};
//
int ssPauseFly[5+1] =   {400,
                       2500,	//заполнение полоски
                       50,		//пауза перед медалькой
                       1500,	//медалька появилась и ждет
                       500, 1};
                       
int ssPauseEnd[10+1] =   {300,
                       1000,		//1. пробег текста медалек gold
                       400,			//2. пауза
                       1000,		//3. пробег текста медалек silver
                       400,			//4. пауза
                       1000,		//5. пробег текста медалек bronze
                       400,			//6. пауза
                       1000,		//7. показываем scores
                       1000,		//8. показываем ранг
                       500, 1};

//------------------------------------------------------------------------------------------
int BonusCnt = 0;

//------------------------------------------------------------------------------------------
int BonusScreenShowTime = 0;
int giStatShowTime = 0;
int number_of_clicks = 0;
int Time_Scores = 0;
int giStatShown = 0;
int gissLevTime = 0;
int giScoresLevOnStatLoad = 0;
int giLastWavPrd = -1;
int giLastStatSubLev = -1;
int giScoreTotBeforeStat = 0;
int giBtnClickTime = 0;
//
int gSprCrystBar= 0;
bool gbAnotherBar = false;
//
int gSprBar2 = 0;
int gSprBar3 = 0;
int gSprBar4 = 0;
int gSprGold1 = 0;
int gSprGold2 = 0;
int gSprGold3 = 0;
int gSprGold4 = 0;
//
int gTextTotalScore = 0;
int gTextRank = 0;
int giLastCrystDir = 0;
//
int gFlyTextCryst = 0;
int gFlyCrystBar = 0;
int gFlyGold = 0;
int gFlyLife1 = 0;
int gFlyLife2 = 0;
//
int gEndTextGold1 = 0;
int gEndTextGold2 = 0;
int gEndTextGold3 = 0;
int gEndTextPoints = 0;
int gEndTextRank = 0;
//
int gTipEndText1 = 0;
int gTipEndText2 = 0;
int gTipEndText3 = 0;
int gTipEndLink = 0;
int gbTipEndShownAll = false;
int giFlyPoins[10];
bool giFPShowTime[10];

//------------------------------------------------------------------------------------------
ProcessStatistics();

//------------------------------------------------------------------------------------------
GetCrystalCnt()
{
	int ret = 0;
	if(GetVidData( 881, VID_VALIDATE))
		ret += GetVidData( 881, VID_COUNT);
	if(GetVidData( 1220, VID_VALIDATE))
		ret += GetVidData(1220, VID_COUNT);
	if(GetVidData( 1221, VID_VALIDATE))
		ret += GetVidData(1221, VID_COUNT);
	if(GetVidData( 1222, VID_VALIDATE))
		ret += GetVidData(1222, VID_COUNT);
	if(GetVidData( 1230, VID_VALIDATE))
		ret += GetVidData(1230, VID_COUNT);
	return ret;
}


//------------------------------------------------------------------------------------------
GetCrystalOnScreenCnt()
{
	int ret = 0;
	//
	int i;
	i = FirstInBox(0, -ToScreenY(0), 1024, 768-ToScreenY(0));
	while(i)
	{
		if(GetUnitVid(i) == 881 || GetUnitVid(i) == 1222 || GetUnitVid(i) == 1230)
			ret++;
		i = NextInBox();
	}
	return ret;
}


//------------------------------------------------------------------------------------------
InitStatistics()
{
	BonusCnt = GetCrystalCnt();
}


HideStatistics();
//------------------------------------------------------------------------------------------
ShowStatistics(int iType)
{
	int i, spr;
	string str;
	//
	if(giStatShown)
		HideStatistics();
	//
	SetEnvironment(ENV_STOP|ENV_EARTHQUAKE);
	Pause2(1);
	SetCursor(CURSOR_NORMAL);
	giStatShown = iType;
	giLastWavPrd = -1;
	//
	BonusScreenShowTime = GetTime();
	giStatShowTime = GetTime();
	Time_Scores = GetTime() - StartGameTime;	//time we played
	number_of_clicks = 0;
	//
	if(giStatShown == STAT_TYPE_NORM)
		MenuLoad("maps\\stat_sublevel.men");
	else
	if(giStatShown == STAT_TYPE_FLY)
		MenuLoad("maps\\stat_flylevel.men");
	else
	if(giStatShown == STAT_TYPE_END)
		MenuLoad("maps\\stat_endlevel.men");
	else
	if(giStatShown == STAT_TYPE_TIP_END)
		MenuLoad("maps\\tip_end.men");
	//
	if(giStatShown == STAT_TYPE_TIP_END || giStatShown == STAT_TYPE_END)
		HideObjectsForMenu(true);
	else
		HideGameBarForMenu(true);

	for(i = 0; i < 10; i++)
	{
		giFlyPoins[i] = 0;
		giFPShowTime[i] = 0;
	}
	//
	if(giStatShown == STAT_TYPE_NORM)
	{
		gSprCrystBar = MenuFind(743, 6);
		gbAnotherBar = false;
		Action(gSprCrystBar, ACT_CHANGE_DIRECTION, 0);
		//
		gSprBar2 = MenuFind(744, 1);
		gSprBar3 = MenuFind(744, 2);
		gSprBar4 = MenuFind(744, 3);
		gSprGold1 = MenuFind(745, 3);
		gSprGold2 = MenuFind(745, 2);
		gSprGold3 = MenuFind(745, 1);
		gSprGold4 = MenuFind(745, 0);
		//
		gTextTotalScore = MenuFind(3, 11);
		gTextRank = MenuFind(3, 136);
		//
		for( spr = FirstSprite(); spr ;spr = NextSprite() )
		{
			if(GetUnitVid(spr) == 763)
			{
				if(GetDirection(spr) == 0) giFlyPoins[0] = spr;
				if(GetDirection(spr) == 1) giFlyPoins[1] = spr;
				if(GetDirection(spr) == 2) giFlyPoins[2] = spr;
				if(GetDirection(spr) == 3) giFlyPoins[3] = spr;
				if(GetDirection(spr) == 4) giFlyPoins[4] = spr;
				//
				if(GetDirection(spr) == 192) giFlyPoins[5] = spr;
				if(GetDirection(spr) == 128) giFlyPoins[6] = spr;
				if(GetDirection(spr) == 129) giFlyPoins[7] = spr;
				if(GetDirection(spr) == 130) giFlyPoins[8] = spr;
				if(GetDirection(spr) == 131) giFlyPoins[9] = spr;
			}
		}
		//
		Action(gSprBar2, ACT_CHANGE_DIRECTION, 0);
		Action(gSprBar3, ACT_CHANGE_DIRECTION, 0);
		Action(gSprBar4, ACT_CHANGE_DIRECTION, 0);
		Action(gSprGold1, ACT_CHANGE_DIRECTION, 0);
		Action(gSprGold2, ACT_CHANGE_DIRECTION, 0);
		Action(gSprGold3, ACT_CHANGE_DIRECTION, 0);
		Action(gSprGold4, ACT_CHANGE_DIRECTION, 0);
		//Action(gTextTotalScore, ACT_SET_INVISIBLE, 1);
		Action(gTextRank, ACT_SET_INVISIBLE, 1);
		//
		for(i = 0; i < 10; i++)
			Action(giFlyPoins[i], ACT_SET_INVISIBLE, 1);
	}
	else
	if(giStatShown == STAT_TYPE_FLY)
	{
		gFlyTextCryst = MenuFind(3, 159);
		gFlyCrystBar = MenuFind(729);
		gFlyGold = MenuFind(745);
		gFlyLife1 = 0;
		gFlyLife2 = 0;
		for( spr = FirstSprite(); spr ;spr = NextSprite() )
		{
			if(GetUnitVid(spr) == 750 && GetDirection(spr) == 0)
				gFlyLife1 = spr;
			if(GetUnitVid(spr) == 750 && GetDirection(spr) == 1)
				gFlyLife2 = spr;
		}
		//
		Action(gFlyGold, ACT_CHANGE_DIRECTION, 0);
		Action(gFlyCrystBar, ACT_CHANGE_DIRECTION, 0);
	}
	else
	if(giStatShown == STAT_TYPE_END)
	{
		gEndTextGold2 = MenuFind(3, 122);
		gEndTextGold1 = MenuFind(3, 123);
		gEndTextGold3 = MenuFind(3, 124);
		gEndTextPoints = MenuFind(3, 125);
		gEndTextRank = MenuFind(3, 126);
		//
		Action(gEndTextGold1, ACT_SET_INVISIBLE, 1);
		Action(gEndTextGold2, ACT_SET_INVISIBLE, 1);
		Action(gEndTextGold3, ACT_SET_INVISIBLE, 1);
		Action(gEndTextPoints, ACT_SET_INVISIBLE, 1);
		Action(gEndTextRank, ACT_SET_INVISIBLE, 1);
	}
	else
	if(giStatShown == STAT_TYPE_TIP_END)
	{
		gTipEndText1 = MenuFind(30, 229);
		gTipEndText2 = MenuFind(4, 190);
		gTipEndText3 = MenuFind(4, 191);
		gTipEndLink = MenuFind(751);
		//
		Action(gTipEndText1, ACT_SET_TEXT_COUNT, 0);
		Action(gTipEndText2, ACT_SET_INVISIBLE, 1);
		Action(gTipEndText3, ACT_SET_INVISIBLE, 1);
		Action(gTipEndLink, ACT_SET_INVISIBLE, 1);
		//
		gbTipEndShownAll = false;
	}

	//
	gissLevTime = (giWinTime-StartBallLevelTime)/1000;
	giScoresLevOnStatLoad = giScoresLev;
	giScoreTotBeforeStat = gScoreTot;
	giLastCrystDir = 0;
	//
	ProcessStatistics();
}


//------------------------------------------------------------------------------------------
HideStatistics()
{
	if(giStatShown != STAT_TYPE_END)
	{
		Pause2(0);
		SetCursor(CURSOR_OFF);
	}
	else
		HideHoFTexts(0, 0);
	//
	if(giStatShown == STAT_TYPE_NORM)
		MenuRelease("maps\\stat_sublevel.men");
	else
	if(giStatShown == STAT_TYPE_FLY)
		MenuRelease("maps\\stat_flylevel.men");
	else
	if(giStatShown == STAT_TYPE_END)
		MenuRelease("maps\\stat_endlevel.men");
	else
	if(giStatShown == STAT_TYPE_TIP_END)
		MenuRelease("maps\\tip_end.men");
	//
	if(giStatShown == STAT_TYPE_TIP_END || giStatShown == STAT_TYPE_END)
		HideObjectsForMenu(false);
	else
		HideGameBarForMenu(false);
	giStatShown = 0;
	Genocide(728);
	Genocide(755);
	Genocide(756);
	Genocide(757);
	Genocide(763);
	//
	GameBarTact();
}


//------------------------------------------------------------------------------------------
GetTotalHPOnScreen(bool bDebug = false)
{
	int ret = 0;
	//
	if(bDebug)
		Log3("-------------------GetTotalHPOnScreen");
	//
	int i = FirstInBox(0, -ToScreenY(0), 1024, 768-ToScreenY(0));
	while(i)
	{
		if( GetUnitVid(i) != 963 && GetUnitVid(i) != 964		//кнопки
			&& GetUnitVid(i) != 970								//шипы
			&& GetUnitVid(i) != 10
			&& GetUnitVid(i) != 1222
			&& GetUnitVid(i) != 1237
			&& GetUnitVid(i) != 1238
			&& GetUnitVid(i) != 1241
			&& GetUnitVid(i) != 1230
			&& ToScreenY(GetY(i),GetZ(i)) > 0 && ToScreenY(GetY(i),GetZ(i)) < ScreenY() )
		{
			int t = GetVidData(GetUnitVid(i), VID_MAXHP);
			ret += t;
			if(t && bDebug)
			{
				Log3("hp= "+itoa(t)+" vid= "+itoa(GetUnitVid(i))+" sx= " + itoa(ToScreenX(GetX(i)))
							+" sy= " + itoa(ToScreenY(GetY(i), GetZ(i)))
							+" sz= " + itoa(GetZ(i)));
			}
		}
		i = NextInBox();
	}
	//
	if(bDebug)
		Log3("-------------------total hp = " + itoa(ret));
	//
	return ret;
}


//------------------------------------------------------------------------------------------
GetGoldLevTime()
{
	int goldTime = 0;
	int levnum = (LevelNumber-1)*5 + giSubLevelNum;
	if(levnum >= 0 && levnum < 45)
		goldTime = giSubLevTimes[levnum];
	return goldTime;
}


//------------------------------------------------------------------------------------------
GetBonusLevTime()
{
	return GetGoldLevTime() * BONUS_TIME_PERCENT / 100;
}


//------------------------------------------------------------------------------------------
ProcesFlyingPoints()
{
	int i;
	for(i = 0; i < 10; i++)
	{
		if(giFPShowTime[i])
		{
			int x = GetX(giFlyPoins[i]);
			int y = GetY(giFlyPoins[i]);
			int z = GetZ(giFlyPoins[i]);
			//
			int dy = giLastTactTime * FLY_POINTS_SPEED / 1000;
			Action(giFlyPoins[i], ACT_CHANGE_COOR, x, y-dy + z, z);
			//
			int dt = GetTime()-giFPShowTime[i];
			int alpha = 0;
			if(dt > FLY_POINTS_ALPHA_TIME && FLY_POINTS_FLY_TIME - FLY_POINTS_ALPHA_TIME > 0)
				alpha = 255 * (dt-FLY_POINTS_ALPHA_TIME) / (FLY_POINTS_FLY_TIME - FLY_POINTS_ALPHA_TIME);
			if(alpha > 255) alpha = 255;
			if(alpha < 0) alpha = 0;
			Action(giFlyPoins[i], ACT_SET_GAMMA, GammaEncodeToDword(0, 0, 0, -alpha));
			//
			if(dt >= FLY_POINTS_FLY_TIME)
			{
				Destroy(giFlyPoins[i]);
				giFlyPoins[i] = 0;
				giFPShowTime[i] = 0;
			}
		}
	}
}


//------------------------------------------------------------------------------------------
ShowFlyingPoints(int iNum, int iType)
{
	if(iNum < 0 || iNum >= 10 || !giFlyPoins[iNum])
		return;
	//
	Action(giFlyPoins[iNum], ACT_SET_INVISIBLE, 0);
	giFPShowTime[iNum] = GetTime();
	//
	int dir = -1;
	if(iType == 3)
		dir = 128;
	if(iType == 2)
		dir = 64;
	if(iType == 1)
		dir = 0;
	//
	if(dir != -1)
		Action(giFlyPoins[iNum], ACT_CHANGE_DIRECTION, dir);
}


//------------------------------------------------------------------------------------------
CreateGoldSalut(int iType, int x, int y, int iFlyingPointsNum)
{
	int iNum = 0;
	int iVid = 0;
	//
	if(iType == 3)
	{
		iNum = 15;
		iVid = 757;
		PlaySFX(64);
		if(iFlyingPointsNum != -1)
			ShowFlyingPoints(iFlyingPointsNum, iType);
	}
	else
	if(iType == 2)
	{
		iNum = 12;
		iVid = 756;
		PlaySFX(71);
		if(iFlyingPointsNum != -1)
			ShowFlyingPoints(iFlyingPointsNum, iType);
	}
	else
	if(iType == 1)
	{
		iNum = 8;
		iVid = 755;
		PlaySFX(72);
		if(iFlyingPointsNum != -1)
			ShowFlyingPoints(iFlyingPointsNum, iType);
	}
	//
	int i;
	for(i = 0; i < iNum; i++)
		CreateSprite(iVid, ToScreenX(x), ToScreenY(y) + 3000, 3000, Random(255));
}


//------------------------------------------------------------------------------------------
//с какого по какой кристалл. 1-5 - обычные, 6 - большой
GetCrystalsCost(int n1, int n2)
{
	int ret = 0;
	if(n2 == 6)
	{
		ret += SCORES_FOR_CRYSTALL_BIG;
		n2--;
	}
	if(n2 >= n1)
		ret += (SCORES_FOR_CRYSTALL * (n2-n1+1));
	return ret;
}


//------------------------------------------------------------------------------------------
ProcessStatisticsNorm()
{
	int ssCrystals = giCrystSubLevScore2;
	//
	int ssBonusScore = giBonusSubLevScore;
	int ssBonusScoreTotal = giBonusSubLevTotal;
	//
	int ssLevScore = giScoresLevOnStatLoad - ssBonusScore - GetCrystalsCost(1, ssCrystals);
	int ssLevScoreTotal = ssLevScore + GetTotalHPOnScreen();
	//
	int goldTime = GetGoldLevTime();
	//
	int ssTimeScore =  0;
	if(gissLevTime <= goldTime)
		ssTimeScore = 100;
	else
	if(gissLevTime <= goldTime + GetBonusLevTime())
		ssTimeScore = 100 * (goldTime + GetBonusLevTime() - gissLevTime) / GetBonusLevTime();
	//
	//------------------------------------------
	int TotalMedalsAdd = 2;	//всегда добавляем за время и levelScore
	int RankAdd = 0;	//сколько добавка к рэнку за то, что нет кристаллов или бонусов
	//
	if(giCrystOnLev == 0)
		RankAdd = RankAdd+3;
	else
		TotalMedalsAdd++;
	//
	if(giBonusSubLevTotal == 0)
		RankAdd = RankAdd+3;
	else
		TotalMedalsAdd++;
	//------------------------------------------
	//
	int iGold1 = 0;
	if(ssCrystals == 6)
		iGold1 = 3;
	else
	if(ssCrystals >= 4)
		iGold1 = 2;
	else
	if(ssCrystals >= 3)
		iGold1 = 2;
	//
	int p = 0;
	if(ssBonusScoreTotal)
		p = ssBonusScore * 100 / ssBonusScoreTotal;
	int iGold2 = 0;
	if(p >= 100)
		iGold2 = 3;
	else
	if(p >= 50)
		iGold2 = 2;
	else
	if(p >= 1)
		iGold2 = 1;
	//
	p = 0;
	if(ssLevScoreTotal)
		p = ssLevScore * 100 / ssLevScoreTotal;
	int iGold3 = 0;
	if(p >= 100)
		iGold3 = 3;
	else
	if(p >= 50)
		iGold3 = 2;
	else
	if(p >= 1)
		iGold3 = 1;
	//
	int iGold4 = 0;
	if(ssTimeScore == 100)
		iGold4 = 3;
	else
	if(ssTimeScore >= 50)
		iGold4 = 2;
	else
	if(ssTimeScore > 0)
		iGold4 = 1;
	//
	//-----------------
	int aaa[4];
	int i;
	for(i = 0; i < 4; i++)
		aaa[i] = 0;
	//
	aaa[iGold1]++;
	aaa[iGold2]++;
	aaa[iGold3]++;
	aaa[iGold4]++;
	int iScoreMedal1 = SCORES_BY_MEDAL[iGold1];
	int iScoreMedal2 = SCORES_BY_MEDAL[iGold2];
	int iScoreMedal3 = SCORES_BY_MEDAL[iGold3];
	int iScoreMedal4 = SCORES_BY_MEDAL[iGold4];
	//-----------------
	if(giLastStatSubLev != giSubLevelNum)
	{
		giLastStatSubLev = giSubLevelNum;
		//
		giGoldScore1 = giGoldScore1 + aaa[3];
		giGoldScore2 = giGoldScore2 + aaa[2];
		giGoldScore3 = giGoldScore3 + aaa[1];
		giTotalMedals = giTotalMedals + TotalMedalsAdd;
		//
		gScoreTot = gScoreTot + 10*(iScoreMedal1 + iScoreMedal2 + iScoreMedal3 + iScoreMedal4);
	}
	int ssScore =    gScoreTot / 10;
	//
	if(Z_DEBUG2)
	{
		ssScore =    12570;
		//
		ssCrystals = 6;
		ssBonusScore = 200;
		ssBonusScoreTotal = 1000;
		ssLevScore = 500;
		ssLevScoreTotal = 1000;
		ssTimeScore = 100;
		iGold1 = 1;
		iGold2 = 2;
		iGold3 = 1;
		iGold4 = 3;
	}
	if(ssCrystals > 6)
		ssCrystals = 6;
	//
	int rank = iGold1 + iGold2 + iGold3 + iGold4 + RankAdd;
	//
	int ssLevScore2 = 0;
	int ssScore2 =    0;
	int ssCrystals2 = 0;
	int ssBonusScore2 = 0;
	int ssTimeScore2 = 0;
	int ssiGold1 = 0;
	int ssiGold2 = 0;
	int ssiGold3 = 0;
	int ssiGold4 = 0;
	//
	if(!giStatShown)
		return;
	//
	bool bPlayWav = false;
	bool bWasClick = false;
	//
	//--------------------------------------------------------------------------
	if(number_of_clicks >= 10)	Action(gTextRank, ACT_SET_INVISIBLE, 0);
	//--------------------------------------------------------------------------
	if(number_of_clicks >= 0)	ssScore2 = ssScore;
	if(number_of_clicks >= 1)	ssCrystals2 = ssCrystals;
	if(number_of_clicks >= 4)	ssBonusScore2 = ssBonusScore;
	if(number_of_clicks >= 6)	ssLevScore2 = ssLevScore;
	if(number_of_clicks >= 8)	ssTimeScore2 = ssTimeScore;
	//
	if(number_of_clicks >= 3)	ssiGold1 = iGold1;
	if(number_of_clicks >= 5)	ssiGold2 = iGold2;
	if(number_of_clicks >= 7)	ssiGold3 = iGold3;
	if(number_of_clicks >= 9)	ssiGold4 = iGold4;
		
	//--------------------------------------------------------------------------
	int iCurTimeLen = ssPauseNorm[number_of_clicks];
	if(number_of_clicks == 1)	//кристаллы
		iCurTimeLen = iCurTimeLen * ssCrystals;
	if(number_of_clicks == 4)	//полоска
	{
		if(ssBonusScoreTotal)
			iCurTimeLen = iCurTimeLen * ssBonusScore / ssBonusScoreTotal;
	}
	if(number_of_clicks == 6)	//полоска
	{
		if(ssLevScoreTotal)
			iCurTimeLen = iCurTimeLen * ssLevScore / ssLevScoreTotal;
	}
	if(number_of_clicks == 8)	//полоска
		iCurTimeLen = iCurTimeLen * ssTimeScore / 100;
	if(iCurTimeLen == 0)
		iCurTimeLen = 200;
	//
	//--------------------------------------------------------------------------
	int k = 0;
	if(iCurTimeLen)
		k = (GetTime()-BonusScreenShowTime)*1000/iCurTimeLen;
	if(k > 1000)
		k = 1000;
	// 
	if(number_of_clicks == 1)
	{
		ssCrystals2 = ssCrystals*k/1000;
		if(ssCrystals == 0)
			bWasClick = true;
	}
	if(number_of_clicks == 2 || number_of_clicks == 3)
	{
		if(iGold1 == 0)
			bWasClick = true;
	}
	if(number_of_clicks == 4)
	{
		ssBonusScore2 = ssBonusScore*k/1000;
		if(ssBonusScore == 0)
			bWasClick = true;
		else
			bPlayWav = true;
	}
	if(number_of_clicks == 6)
	{
		ssLevScore2 = ssLevScore*k/1000;
		if(ssLevScore == 0)
			bWasClick = true;
		else
			bPlayWav = true;
	}
	if(number_of_clicks == 8)
	{
		ssTimeScore2 = ssTimeScore*k/1000;
		if(ssTimeScore == 0)
			bWasClick = true;
		else
			bPlayWav = true;
	}
	if(giLastWavPrd != number_of_clicks)
	{
		giLastWavPrd = number_of_clicks;
		if(number_of_clicks == 3 && iGold1 != 0)
			CreateGoldSalut(iGold1, GetX(gSprGold1), GetY(gSprGold1)-GetZ(gSprGold1), 6);
		if(number_of_clicks == 5 && iGold2 != 0)
			CreateGoldSalut(iGold2, GetX(gSprGold2), GetY(gSprGold2)-GetZ(gSprGold2), 7);
		if(number_of_clicks == 7 && iGold3 != 0)
			CreateGoldSalut(iGold3, GetX(gSprGold3), GetY(gSprGold3)-GetZ(gSprGold3), 8);
		if(number_of_clicks == 9 && iGold4 != 0)
			CreateGoldSalut(iGold4, GetX(gSprGold4), GetY(gSprGold4)-GetZ(gSprGold4), 9);
		if(number_of_clicks == 10)
			PlaySFX(67);
	}
	//
	if(bPlayWav)
		PlaySFX(STATISTICS_SFX);
	//--------------------------------------------------------------------------
	//чё убавить от ssScore2 - для пробегания total scores
	int dd = 0;
	if(number_of_clicks < 9)	//показ медальки 4
		dd = iScoreMedal4;
	if(number_of_clicks < 7)	//показ медальки 3 + полоска level points
		dd += iScoreMedal3+ssLevScore-ssLevScore2;
	if(number_of_clicks < 5)	//показ медальки 2 + полоска bonus points
		dd += iScoreMedal2+ssBonusScore-ssBonusScore2;
	if(number_of_clicks < 3)	//показ медальки 1
		dd += iScoreMedal1;
	if(number_of_clicks <= 1)	//показ кристаллов
		dd += GetCrystalsCost(ssCrystals2+1, ssCrystals);
	//
	//Log3("ssScore2  =  " + itoa(ssScore2));
	//Log3("iScoreMedal1  =  " + itoa(iScoreMedal1));
	//Log3("iScoreMedal2  =  " + itoa(iScoreMedal2));
	//Log3("iScoreMedal3  =  " + itoa(iScoreMedal3));
	//Log3("iScoreMedal4  =  " + itoa(iScoreMedal4));
	//Log3("ssLevScore  =  " + itoa(ssLevScore));
	//Log3("ssLevScore2  =  " + itoa(ssLevScore2));
	//Log3("ssBonusScore  =  " + itoa(ssBonusScore));
	//Log3("ssBonusScore2  =  " + itoa(ssBonusScore2));
	//Log3("ssCrystals  =  " + itoa(ssCrystals));
	//Log3("ssCrystals2  =  " + itoa(ssCrystals2));
	//Log3("dd  =  " + itoa(dd));
		
	//--------------------------------------------------------------------------
	//устанавливаем тексты
	string str;
	//
	str = itoa(ssScore2-dd);
	Action(gTextTotalScore, ACT_SET_TEXT, &str);
	//
	int dir = Call_ii(EXT_GetAbsDir, 743, ssCrystals2);
	Action(gSprCrystBar, ACT_CHANGE_DIRECTION, dir);
	//
	if(number_of_clicks < 12 && dir != giLastCrystDir)
	{
		ShowFlyingPoints(ssCrystals2-1, -1);
		if(ssCrystals2 == 6)
			PlaySFX(63);
		else
			PlaySFX(62);
	}
	giLastCrystDir = dir;
	//
	//
	int NoDir = GetVidData(744, VID_NO_DIR);
	//
	dir = 0;
	if(ssBonusScoreTotal)
		dir = ssBonusScore2 * (NoDir-1) / ssBonusScoreTotal;
	if(dir == 0 && ssBonusScore2 != 0)
		dir = 1;
	dir = Call_ii(EXT_GetAbsDir, 744, dir);
	Action(gSprBar2, ACT_CHANGE_DIRECTION, dir);
	//
	dir = 0;
	if(ssLevScoreTotal)
		dir = ssLevScore2 * (NoDir-1) / ssLevScoreTotal;
	if(dir == 0 && ssLevScore2 != 0)
		dir = 1;
	dir = Call_ii(EXT_GetAbsDir, 744, dir);
	Action(gSprBar3, ACT_CHANGE_DIRECTION, dir);
	//
	dir = ssTimeScore2 * (NoDir-1) / 100;
	if(dir == 0 && ssTimeScore2 != 0)
		dir = 1;
	dir = Call_ii(EXT_GetAbsDir, 744, dir);
	Action(gSprBar4, ACT_CHANGE_DIRECTION, dir);
	//
	dir = Call_ii(EXT_GetAbsDir, 745, ssiGold1);
	Action(gSprGold1, ACT_CHANGE_DIRECTION, dir);
	dir = Call_ii(EXT_GetAbsDir, 745, ssiGold2);
	Action(gSprGold2, ACT_CHANGE_DIRECTION, dir);
	dir = Call_ii(EXT_GetAbsDir, 745, ssiGold3);
	Action(gSprGold3, ACT_CHANGE_DIRECTION, dir);
	dir = Call_ii(EXT_GetAbsDir, 745, ssiGold4);
	Action(gSprGold4, ACT_CHANGE_DIRECTION, dir);
	//
	str = GetRankName(rank);
	Action(gTextRank, ACT_SET_TEXT, &str);
	//
	if(number_of_clicks >= 13 && (!giBtnClickTime || GetTime()-giBtnClickTime >= 100))
	{
		HideStatistics();
		return;
	}

	//--------------------------------------------------------------------------
	if(GetTime()-BonusScreenShowTime > iCurTimeLen)
		bWasClick = true;

	//--------------------------------------------------------------------------
	bool btnPress = false;
	if(MenuLClick() && MenuNVidUnderCursor()==704 && GetDirection(MenuLClick()) == 55
		|| GetKey() == VK_ESCAPE)
	{
		btnPress = true;
		giBtnClickTime = GetTime();
	}
	bool bEnter = false;
	if(GetKey() == VK_RETURN || GetKey() == VK_SPACE)
		bEnter = true;
	//--------------------------------------------------------------------------
	//process key-pressing
	if	(	GetKey() || bWasClick
			||	( GetInputState() & INPUT_RCLICK )
			||	( GetInputState() & INPUT_LCLICK 
			||	bEnter
			||	btnPress )
		)
	{
		number_of_clicks++;
		if(!btnPress && number_of_clicks == 13)	//уходим теперь только кнопкой
			number_of_clicks = 12;
		//
		if(btnPress)							//уходим кнопкой
			number_of_clicks = 13;
		if(bEnter && number_of_clicks == 12)	//уходим enter'ом, если вся статистика показана
			number_of_clicks = 13;

		//zmNEW070202 - сказали, что при клике должно сразу всё показываться
		if(!bWasClick && number_of_clicks < 12)
		{
			number_of_clicks = 12;
			PlaySFX(67);
		}
		//
		BonusScreenShowTime = GetTime();
		StopSFX(STATISTICS_SFX);
	}
	//
	ProcesFlyingPoints();
}


//------------------------------------------------------------------------------------------
ProcessStatisticsFly()
{
	int ssCrystals = BonusCnt - GetCrystalCnt();
	int ssCrystalsCnt = BonusCnt;
	//
	if(Z_DEBUG2)
	{
		ssCrystals = 100;
		ssCrystalsCnt = 100;
	}
	//
	int ssGold = 0;
	int ssLives = 0;
	//
	//
	int iGoldAdd1 = 0;
	int iGoldAdd2 = 0;
	int iGoldAdd3 = 0;
	int percent = 0;
	if(ssCrystalsCnt)
		percent = ssCrystals * 100 / ssCrystalsCnt;
	if(percent >= 100)	//если редактируешь здесь, отредактируй ниже
	{
		ssGold = 3;
		ssLives = 2;
		iGoldAdd1++;
	}
	else
	if(percent >= 80)	//если редактируешь здесь, отредактируй ниже
	{
		ssGold = 2;
		ssLives = 1;
		iGoldAdd2++;
	}
	else
	if(percent >= 50)
	{
		ssGold = 1;
		iGoldAdd3++;
	}
	//
	iff(1)
	{
		giTotalMedals++;
		giGoldScore1 = giGoldScore1 + iGoldAdd1;
		giGoldScore2 = giGoldScore2 + iGoldAdd2;
		giGoldScore3 = giGoldScore3 + iGoldAdd3;
	}
	//
	int ssCrystals2 = 0;
	int ssGold2 = 0;
	int ssLives2 = 0;
	//
	if(!giStatShown)
		return;
	//
	bool bPlayWav = false;
	bool bWasClick = false;
	//
	if(number_of_clicks >= 2)	ssCrystals2 = ssCrystals;
	if(number_of_clicks >= 2)	ssLives2 = ssLives;
	if(number_of_clicks >= 3)	ssGold2 = ssGold;
	//
	//--------------------------------------------------------------------------
	int iCurTimeLen = ssPauseFly[number_of_clicks];
	if(number_of_clicks == 1)	//полоска
		iCurTimeLen = iCurTimeLen * percent / 100;
	if(iCurTimeLen == 0)
		iCurTimeLen = 200;
	//
	int k = 0;
	if(iCurTimeLen)
		k = (GetTime()-BonusScreenShowTime)*1000/iCurTimeLen;
	if(k > 1000)
		k = 1000;
	//
	if(number_of_clicks == 1)
	{
		ssCrystals2 = ssCrystals*k/1000;
		//
		int percnt = 0;
		if(ssCrystalsCnt)
			percnt = ssCrystals2 * 100 / ssCrystalsCnt;
		if(percnt >= 80)	//если редактируешь здесь, отредактируй выше
			ssLives2 = 1;	
		if(percnt >= 100)	//если редактируешь здесь, отредактируй выше
			ssLives2 = 2;
		//
		if(ssCrystals == 0)
			bWasClick = true;
		bPlayWav = true;
	}
	//
	if(number_of_clicks == 3 && giLastWavPrd != number_of_clicks)
	{
		giLastWavPrd = number_of_clicks;
		if(number_of_clicks == 3 && ssGold != 0)
			CreateGoldSalut(ssGold, GetX(gFlyGold), GetY(gFlyGold)-GetZ(gFlyGold), -1);
	}
	//
	if(bPlayWav)
		PlaySFX(STATISTICS_SFX);
	//
	if(ssLives2 >= 1)
	{
		Action(gFlyLife1, ACT_SET_INVISIBLE, 0);
		iff(1)
			AddLife(AL_FLYSTAT);
	}
	else
		Action(gFlyLife1, ACT_SET_INVISIBLE, 1);
	//
	if(ssLives2 >= 2)
	{
		Action(gFlyLife2, ACT_SET_INVISIBLE, 0);
		iff(1)
			AddLife(AL_FLYSTAT);
	}
	else
		Action(gFlyLife2, ACT_SET_INVISIBLE, 1);
	//
	string str;
	str = itoa(ssCrystals2) + " / " + itoa(ssCrystalsCnt);
	Action(gFlyTextCryst, ACT_SET_TEXT, &str);
	//
	int NoDir = GetVidData(729, VID_NO_DIR);
	int dir = ssCrystals2 * (NoDir-1) / ssCrystalsCnt;
	if(dir == 0 && ssCrystals2 != 0)
		dir = 1;
	dir = Call_ii(EXT_GetAbsDir, 729, dir);
	Action(gFlyCrystBar, ACT_CHANGE_DIRECTION, dir);
	//
	dir = Call_ii(EXT_GetAbsDir, 745, ssGold2);
	Action(gFlyGold, ACT_CHANGE_DIRECTION, dir);
	//
	//
	if(number_of_clicks >= 6 && (!giBtnClickTime || GetTime()-giBtnClickTime >= 100))
	{
		HideStatistics();
		return;
	}
	//
	//--------------------------------------------------------------------------
	if(GetTime()-BonusScreenShowTime > iCurTimeLen)
		bWasClick = true;
	//--------------------------------------------------------------------------
	bool btnPress = false;
	if(MenuLClick() && MenuNVidUnderCursor()==704
		|| GetKey() == VK_ESCAPE)
	{
		btnPress = true;
		giBtnClickTime = GetTime();
	}
	bool bEnter = false;
	if(GetKey() == VK_RETURN || GetKey() == VK_SPACE)
		bEnter = true;
	//--------------------------------------------------------------------------
	//process key-pressing
	if	(	GetKey() || bWasClick
			||	( GetInputState() & INPUT_RCLICK )
			||	( GetInputState() & INPUT_LCLICK 
			||	bEnter
			||	btnPress )
		)
	{
		number_of_clicks++;
		if(!btnPress && number_of_clicks == 6)	//уходим теперь только кнопкой
			number_of_clicks = 5;
		//
		if(btnPress)							//уходим кнопкой
			number_of_clicks = 6;
		if(bEnter && number_of_clicks == 5)	//уходим enter'ом, если вся статистика показана
			number_of_clicks = 6;

		//при клике должно сразу всё показываться
		if(!bWasClick && number_of_clicks < 5)
		{
			number_of_clicks = 5;
			PlaySFX(67);
		}
		//
		BonusScreenShowTime = GetTime();
		StopSFX(STATISTICS_SFX);
	}
}


//------------------------------------------------------------------------------------------
ProcessStatisticsWin()
{
	//int ss1Gold = giGoldScore1;
	//int ss2Gold = giGoldScore2;
	//int ss3Gold = giGoldScore3;
	//int ssScores = gScoreTot / 10;
	int ss1Gold = GetValI("giGoldScore1Last", 0);
	int ss2Gold = GetValI("giGoldScore2Last", 0);
	int ss3Gold = GetValI("giGoldScore3Last", 0);
	int ssScores = GetValI("gScoreTotLast", 0);
	int ssRank = GetValI("gRankLast", 0);
	//
	if(Z_DEBUG2)
	{
		ss1Gold = 55;
		ss2Gold = 22;
		ss3Gold = 17;
		ssScores = 81745;
		ssRank = 11;
	}
	//
	int ss1Gold2 = 0;
	int ss2Gold2 = 0;
	int ss3Gold2 = 0;
	int ssScores2 = 0;
	int ssRank2 = 0;
	//
	if(!giStatShown)
		return;
	//
	bool bPlayWav = false;
	bool bWasClick = false;
	//
	if(number_of_clicks >= 2)	ss1Gold2 = ss1Gold;
	if(number_of_clicks >= 4)	ss2Gold2 = ss2Gold;
	if(number_of_clicks >= 6)	ss3Gold2 = ss3Gold;
	if(number_of_clicks >= 7)	ssScores2 = ssScores;
	if(number_of_clicks >= 8)	ssRank2 = ssRank;
	if(number_of_clicks >= 1)	Action(gEndTextGold1, ACT_SET_INVISIBLE, 0);
	if(number_of_clicks >= 3)	Action(gEndTextGold2, ACT_SET_INVISIBLE, 0);
	if(number_of_clicks >= 5)	Action(gEndTextGold3, ACT_SET_INVISIBLE, 0);
	if(number_of_clicks >= 7)	Action(gEndTextPoints, ACT_SET_INVISIBLE, 0);
	if(number_of_clicks >= 8)	Action(gEndTextRank, ACT_SET_INVISIBLE, 0);
	//
	//
	//--------------------------------------------------------------------------
	int iCurTimeLen = ssPauseEnd[number_of_clicks];
	if(iCurTimeLen == 0)
		iCurTimeLen = 200;

	//--------------------------------------------------------------------------
	int k = 0;
	if(iCurTimeLen)
		k = (GetTime()-BonusScreenShowTime)*1000/iCurTimeLen;
	if(k > 1000)
		k = 1000;
	// 
	if(number_of_clicks == 1)
	{
		ss1Gold2 = ss1Gold*k/1000;
		if(ss1Gold == 0)
			bWasClick = true;
		else
			bPlayWav = true;
	}
	if(number_of_clicks == 3)
	{
		ss2Gold2 = ss2Gold*k/1000;
		if(ss2Gold == 0)
			bWasClick = true;
		else
			bPlayWav = true;
	}
	if(number_of_clicks == 5)
	{
		ss3Gold2 = ss3Gold*k/1000;
		if(ss3Gold == 0)
			bWasClick = true;
		else
			bPlayWav = true;
	}
	//--------------------------------------------------------------------------
	if(giLastWavPrd != number_of_clicks)
	{
		giLastWavPrd = number_of_clicks;
		if(number_of_clicks == 2)
			CreateGoldSalut(3, GetX(gEndTextGold1), GetY(gEndTextGold1)-GetZ(gEndTextGold1), -1);
		else
		if(number_of_clicks == 4)
			CreateGoldSalut(2, GetX(gEndTextGold2), GetY(gEndTextGold2)-GetZ(gEndTextGold2), -1);
		else
		if(number_of_clicks == 6)
			CreateGoldSalut(1, GetX(gEndTextGold3), GetY(gEndTextGold3)-GetZ(gEndTextGold3), -1);
		else
		if(number_of_clicks == 7)
			PlaySFX(51);
		else
		if(number_of_clicks == 8)
			PlaySFX(67);
	}
	//
	if(bPlayWav)
		PlaySFX(STATISTICS_SFX);
	//
	string str;
	str = itoa(ss1Gold2);
	Action(gEndTextGold1, ACT_SET_TEXT, &str);
	//
	str = itoa(ss2Gold2);
	Action(gEndTextGold2, ACT_SET_TEXT, &str);
	//
	str = itoa(ss3Gold2);
	Action(gEndTextGold3, ACT_SET_TEXT, &str);
	//
	str = itoa(ssScores2);
	Action(gEndTextPoints, ACT_SET_TEXT, &str);
	//
	str = GetRankName(ssRank2);
	Action(gEndTextRank, ACT_SET_TEXT, &str);
	//
	//--------------------------------------------------------------------------
	if(number_of_clicks >= 11 && (!giBtnClickTime || GetTime()-giBtnClickTime >= 100))
	{
		HideStatistics();
		return;
	}
	//
	//--------------------------------------------------------------------------
	if(GetTime()-BonusScreenShowTime > iCurTimeLen)
		bWasClick = true;
	//--------------------------------------------------------------------------
	bool btnPress = false;
	if(MenuLClick() && MenuNVidUnderCursor()==704
		|| GetKey() == VK_ESCAPE)
	{
		btnPress = true;
		giBtnClickTime = GetTime();
	}
	bool bEnter = false;
	if(GetKey() == VK_RETURN || GetKey() == VK_SPACE)
		bEnter = true;
	//--------------------------------------------------------------------------
	//process key-pressing
	if	(	GetKey() || bWasClick
			||	( GetInputState() & INPUT_RCLICK )
			||	( GetInputState() & INPUT_LCLICK 
			||	bEnter
			||	btnPress )
		)
	{
		number_of_clicks++;
		if(!btnPress && number_of_clicks == 11)	//уходим теперь только кнопкой
			number_of_clicks = 10;
		//
		if(btnPress)							//уходим кнопкой
			number_of_clicks = 11;
		if(bEnter && number_of_clicks == 10)	//уходим enter'ом, если вся статистика показана
			number_of_clicks = 11;

		//при клике должно сразу всё показываться
		if(!bWasClick && number_of_clicks < 10)
		{
			number_of_clicks = 10;
			PlaySFX(67);
		}
		//
		BonusScreenShowTime = GetTime();
		StopSFX(STATISTICS_SFX);
	}
}


//------------------------------------------------------------------------------------------
GetCrystalsStr()
{
	string ret;
#ifndef GAMEPLAY_FLY
	//ret = itoa(giCrystalScore);
	ret = itoa(giCrystSubLevScore) + "/" + itoa(giCrystOnLev);
#endif
#ifdef GAMEPLAY_FLY
	ret = itoa(BonusCnt - GetCrystalCnt()) + "/" + itoa(BonusCnt);
#endif
	return ret;
}


//------------------------------------------------------------------------------------------
ProcessEndScreen()
{
	bool btnPress = false;
	if(MenuLClick() && MenuNVidUnderCursor()==704
			|| GetKey() == VK_ESCAPE)
	{
		btnPress = true;
		giBtnClickTime = GetTime();
	}
	//
	//--------------------------------------------------------------------------
	bool bEnter = (GetKey() == VK_RETURN || GetKey() == VK_SPACE);
	bool bCLick = GetInputState()&(INPUT_LCLICK|INPUT_RCLICK) || bEnter;
	//
	if(!gbTipEndShownAll &&
		(bCLick || btnPress || GetTime()-giStatShowTime > TIP_END_SHOW_ALL_PAUSE))
	{
		if(bCLick || btnPress)
			Action(gTipEndText1, ACT_SET_TEXT_COUNT, 999999);
		Action(gTipEndText2, ACT_SET_INVISIBLE, 0);
		Action(gTipEndText3, ACT_SET_INVISIBLE, 0);
		Action(gTipEndLink, ACT_SET_INVISIBLE, 0);
		gbTipEndShownAll = true;
		btnPress = false;
		bEnter = false;
		giBtnClickTime = 0;
	}
	//--------------------------------------------------------------------------
	if(MenuLClick() && MenuLClick() == gTipEndLink)
	{
		Exec(GetString("strings","FinalLink"));
	}

	//--------------------------------------------------------------------------
	//process key-pressing
	if(giBtnClickTime && GetTime()-giBtnClickTime >= 100 || bEnter)
	{
		HideStatistics();
		giBtnClickTime = 0;
	}
}


//------------------------------------------------------------------------------------------
ProcessStatistics()
{
	if(giStatShown == STAT_TYPE_NORM)
		ProcessStatisticsNorm();
	else
	if(giStatShown == STAT_TYPE_FLY)
		ProcessStatisticsFly();
	else
	if(giStatShown == STAT_TYPE_END)
		ProcessStatisticsWin();
	else
	if(giStatShown == STAT_TYPE_TIP_END)
		ProcessEndScreen();
}

