#define  COMMON_FAME_H aaa
//----------------------------------------------------------------------------
//  @brief
//        hall of fame
//        

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


//------------------------------------------------------------------------------------------
//количество строчек для результатов в highscores.men
//*upd070116 - нижняя строчка - MEN_RECORD_CNT+1 - последний рекорд, если не было
#define MEN_RECORD_CNT     15

bool gbResetScoresShown = false;

//------------------------------------------------------------------------------------------
HideHoFTexts(int hide, int iFromRecNum)
{
	int i, spr;
	for(i = iFromRecNum; i < MEN_RECORD_CNT+1; i++)
	{
		spr = MenuFind(30, 10+i);
		Action(spr, ACT_SET_INVISIBLE, hide);
		spr = MenuFind(30, 30+i);
		Action(spr, ACT_SET_INVISIBLE, hide);
		spr = MenuFind(30, 50+i);
		Action(spr, ACT_SET_INVISIBLE, hide);
		//
		spr = MenuFind(29, 10+i);
		Action(spr, ACT_SET_INVISIBLE, hide);
		spr = MenuFind(29, 30+i);
		Action(spr, ACT_SET_INVISIBLE, hide);
		spr = MenuFind(29, 50+i);
		Action(spr, ACT_SET_INVISIBLE, hide);
	}
	//
	for( spr = FirstSprite(); spr ;spr = NextSprite() )
	{
		int act = ANI_MENUSTAND;
		if(hide)
			act = ANI_MENUDISABLE;
		if(GetUnitVid(spr) == 704 && GetDirection(spr) == 79)
			Action(spr, act);
	}
}


//------------------------------------------------------------------------------------------
DrawFame()
{
	int i, spr, spr2, cnt, lastPos, score, rank;
	string str;
	//
	MenuLoad("maps\\highscores.men");
	//
	bool bWasLast = false;
	lastPos = GetGlobValI("LastRecPos", -1);
	//if(Z_DEBUG)
	//	lastPos = 5;
	//
	cnt = Call_v(EXT_GetRecCnt);
	for(i = 0; i < MEN_RECORD_CNT; i++)
	{
		score = 0;
		rank = 0;
		if(i < cnt)
		{
			score = Call_i(EXT_GetRecScore, i);
			rank = Call_i(EXT_GetRecRank, i);
		}
		bool bLast = (lastPos==i);
		if(bWasLast)
			bLast = false;
		if(bLast)
			bWasLast = true;
		//
		spr = MenuFind(30, 10+i);
		if(spr)
		{
			str = "";
			if(i < cnt)
				str = Call_i(EXT_GetRecName, i);
			if(bLast)
				Action(spr, ACT_CHANGE_VID, 29);
			//
			Action(spr, ACT_SET_TEXT, &str);
		}
		//
		spr2 = MenuFind(30, 30+i);
		if(spr2)
		{
			str = "";
			if(i < cnt)
				str = itoa(score);
			if(bLast)
				Action(spr2, ACT_CHANGE_VID, 29);
			//
			Action(spr2, ACT_SET_TEXT, &str);
		}
		spr = MenuFind(30, 50+i);
		if(spr)
		{
			str = "";
			if(i < cnt)
				str = GetRankName(rank);
			if(bLast)
				Action(spr, ACT_CHANGE_VID, 29);
			//
			Action(spr, ACT_SET_TEXT, &str);
		}
	}
	//
	if(!bWasLast && lastPos != -1 && lastPos < cnt && lastPos >= 0)
	{
		str = Call_i(EXT_GetRecName, lastPos);
		spr = MenuFind(30, 25);
		Action(spr, ACT_SET_TEXT, &str);
		Action(spr, ACT_CHANGE_VID, 29);
		//
		str = itoa(Call_i(EXT_GetRecScore, lastPos));
		spr = MenuFind(30, 45);
		Action(spr, ACT_SET_TEXT, &str);
		Action(spr, ACT_CHANGE_VID, 29);
		//
		str = GetRankName(Call_i(EXT_GetRecRank, lastPos));
		spr = MenuFind(30, 65);
		Action(spr, ACT_SET_TEXT, &str);
		Action(spr, ACT_CHANGE_VID, 29);
	}
	else
	{
		str = "";
		spr = MenuFind(30, 25);
		Action(spr, ACT_SET_TEXT, &str);
		spr = MenuFind(30, 45);
		Action(spr, ACT_SET_TEXT, &str);
		spr = MenuFind(30, 65);
		Action(spr, ACT_SET_TEXT, &str);
	}
	//
	if(lastPos != -1)
	{
		HideHoFTexts(true, 0);
		ShowStatistics(STAT_TYPE_END);
	}
	//
	SetGlobValI("LastRecPos", -1);
}


//------------------------------------------------------------------------------------------
AddCurRecord()
{
	int rank = 0;
	if(giTotalMedals)
	{
		int max = giTotalMedals * 3;
		rank = ((giGoldScore1*3 + giGoldScore2*2 + giGoldScore3)*12 + (max/2)) / max;
	}
	//
	if(rank < 0)
		rank = 0;
	if(rank > 12)
		rank = 12;
	//
	SetValI("giGoldScore1Last", giGoldScore1);
	SetValI("giGoldScore2Last", giGoldScore2);
	SetValI("giGoldScore3Last", giGoldScore3);
	SetValI("gScoreTotLast", gScoreTot / 10);
	SetValI("gRankLast", rank);
	//
	int curUser;
	curUser = Call_v(EXT_GetCurUserG);
	int LastRecPos = Call_sii(EXT_AddRecord, Call_i(EXT_GetUsrNameG, curUser), gScoreTot/10, rank);
	SetGlobValI("LastRecPos", LastRecPos);
}


//------------------------------------------------------------------------------------------
FameTact()
{
	if(!gbResetScoresShown && MenuLClick() && MenuNVidUnderCursor()==753)
	{
		gbResetScoresShown = true;
		MenuLoad("Maps/reset_scrores.men");
		HideHoFTexts(true, 0);
	}
	//
	bool bCanClose = !gbResetScoresShown;
	//
	if(gbResetScoresShown)
	{
		if(MenuLClick() && MenuNVidUnderCursor()==704 && GetDirection(MenuLClick()) == 0)
		{
			gbResetScoresShown = false;
			MenuRelease("Maps/reset_scrores.men");
			Call_v(EXT_ResetHighScores);
			//
			MenuRelease();
			DrawFame();
		}
		if(MenuLClick() && MenuNVidUnderCursor()==704 && GetDirection(MenuLClick()) == 32
			|| GetKey()==VK_ESCAPE)
		{
			gbResetScoresShown = false;
			MenuRelease();
			DrawFame();
		}
	}
	//
	return bCanClose;
}

