//
static int SoundVolume; // 0-14
static int MusicVolume; // 0-14

//
//0 - не вводится, 1 - вводится для закрытия крови, 2 - вводится для открытия крови
static int giEnterBloodPass = 0;
static string gsNewBloodPass;

//для того, чтобы при esc после ввода пароля не уходить из менюшки
static bool gbWasEnterBloodPass = false;

//---------------------------------------------------------------------------------------
//установка нового значения громкости звука 0-15
SetNewSoundVolume(int volume)
{
  if( volume < 0 )
    volume = 0;
  if( volume > 15 )
    volume = 15;
  SoundVolume = volume;
  SetGlobValI("SoundVolume",volume*100/15);
  SetSoundVolume( volume*100/15 );
  MenuAction(714,999999,ACT_CHANGE_DIRECTION,(volume*247)/15);
}


//---------------------------------------------------------------------------------------
//установка нового значения громкости музыки 0-15
SetNewMusicVolume(int volume)
{
  if( volume < 0 )
    volume = 0;
  if( volume > 15 )
    volume = 15;
  MusicVolume = volume;
  SetGlobValI("MusicVolume",volume*100/15);
  SetMusicVolume( volume*100/15 );
  MenuAction(715,999999,ACT_CHANGE_DIRECTION,(volume*247)/15);
}


//---------------------------------------------------------------------------------------
//установка нового значения уровня детализации графики
SetNewGraphDetail(int detail)
{
  static int text = 0;
  if( !text )
  {
    int menu;
    menu = MenuFind(717,0);
    text = CreateText(3,ToScreenX(GetX(menu)),ToScreenY(GetY(menu))+1,GetZ(menu)+1,"High");
    Action(text,ACT_SET_BEHAVE,BEH_CENTER);
  }
  if( detail < 0 )
    detail = 0;
  if( detail > 2 )
    detail = 2;
  SetGlobValI("GraphDetail",detail);
  string str;
  if( detail==0 )
    str = GetString("menu","Low");
  else if( detail==1 )
    str = GetString("menu","Medium");
  else if( detail==2 )
    str = GetString("menu","High");
  Action(text,ACT_SET_TEXT,&str);
}


//---------------------------------------------------------------------------------------
SetGraphLevel()
{
  int i;
  // Настройка уровня детализации
  static int LowLevelVid[] = { 120,133,144,158,159,167,170,171,174,176,191,195,199,// 123т.к. ракета без дыма не видна
    766,768,770,778,780,781,783,787 };
  static int MediumLevelVid[] = { 106,108,119,121,138,140,177,178,185,187,189,755,756,757,758,787 };
//?102 гильза
//?127гильза
  if( GetGlobValI("GraphDetail","2")==0 )
  {
    for( i = 0; i < sizeof(LowLevelVid)/sizeof(int); i++ )
      SetVidData(LowLevelVid[i],VID_NOT_CREATE_AS_CHILD,1);
    SetVidData(910,VID_NO_CHILD+15,2);
    SetVidData(771,VID_NO_CHILD+14,2);
    SetVidData(772,VID_NO_CHILD+14,2);
    SetVidData(773,VID_NO_CHILD+14,2);
    SetVidData(774,VID_NO_CHILD+14,2);
    SetVidData(775,VID_NO_CHILD+14,2);
    SetVidData(776,VID_NO_CHILD+14,2);
  }
  else
  {
    for( i = 0; i < sizeof(LowLevelVid)/sizeof(int); i++ )
      SetVidData(LowLevelVid[i],VID_NOT_CREATE_AS_CHILD,0);
    SetVidData(910,VID_NO_CHILD+15,8);
    SetVidData(771,VID_NO_CHILD+14,14);
    SetVidData(772,VID_NO_CHILD+14,14);
    SetVidData(773,VID_NO_CHILD+14,14);
    SetVidData(774,VID_NO_CHILD+14,16);
    SetVidData(775,VID_NO_CHILD+14,14);
    SetVidData(776,VID_NO_CHILD+14,14);
  }
  if( GetGlobValI("GraphDetail","2")<=1 )
  {
    for( i = 0; i < sizeof(MediumLevelVid)/sizeof(int); i++ )
      SetVidData(MediumLevelVid[i],VID_NOT_CREATE_AS_CHILD,1);
    SetVidData(143,VID_NO_CHILD+14,3);
    SetVidData(901,VID_NO_CHILD+14,5);
  }
  else
  {
    for( i = 0; i < sizeof(MediumLevelVid)/sizeof(int); i++ )
      SetVidData(MediumLevelVid[i],VID_NOT_CREATE_AS_CHILD,0);
    SetVidData(143,VID_NO_CHILD+14,8);
    SetVidData(901,VID_NO_CHILD+14,10);
  }
}


//---------------------------------------------------------------------------------------
ShowCurBloodPass()
{
	string str = "";
	if(giEnterBloodPass != 0)
	{
		static int siTime = 0;
		if(siTime == 0)
			siTime = GetTime();
		//
		str = gsNewBloodPass;
		if( (GetTime()-siTime)%1000 <= 500 )
			str = str + "_";
		else
		    str = str + " ";
	}
	else
	{
		string sCurPass = GetGlobValS("BloodPass", "");
		int len = strlen(sCurPass);
		//
		str = "";
		int i;
		for(i = 0; i < len; i++)
			str = str + "*";
	}
	//
	int spr = MenuFindNamed("BP_Pass");
	Action(spr, ACT_SET_TEXT, &str);
}


//---------------------------------------------------------------------------------------
ResetPassLabels()
{
	string str;

	//засеряем менюшки ввода пароля
	int spr = MenuFindNamed("BP_Label");
	if(GetGlobValI("Blood",1))
	{
		Action(spr, ACT_SET_INVISIBLE, 1);
	}
	else
	{
		Action(spr, ACT_SET_INVISIBLE, 0);
	}
	//
	Action(spr, ACT_SET_INVISIBLE, 0);
	if(giEnterBloodPass == 1)
		str = "BloodPass";
	else if(giEnterBloodPass == 2)
		str = "BloodPassEnter";
	else
	{
		//не вводим в данный момент
		if(GetGlobValI("Blood",1) == 0)
		{
			if(GetGlobValS("BloodPass", "") == "")
				str = "BloodPass";
			else
				str = "BloodPassEnter";
		}
		else
			Action(spr, ACT_SET_INVISIBLE, 1);
	}
	//
	Action(spr, ACT_SET_TEXT, &str);
	//
	spr = MenuFindNamed("BP_Enter");
	if(GetGlobValI("Blood",1))
	{
		Action(spr, ACT_SET_GAMMA, GammaEncodeToDword(-150,-180,-200));
		Action(spr, ANI_MENUDISABLE);
	}
	else
	{
		Action(spr, ACT_SET_GAMMA, 0);
		Action(spr, ANI_MENUSTAND);
	}
}


//---------------------------------------------------------------------------------------
//если прислали 2, то значит пароль был введен правильно
SetNewBlood(int red)
{
	bool bPassEntered = false;
	if(red == 2)
	{
		red = 1;
		bPassEntered = true;
	}
	//
	int iPrevVal = GetGlobValI("Blood",red);
	//
	string sCurPass = GetGlobValS("BloodPass", "");
	bool bHavePass = (strlen(sCurPass) > 0);
    //
    if(iPrevVal != red)
    {
	    if(red == 0 && !bHavePass)
    	{
    		gsNewBloodPass = "";
    		giEnterBloodPass = 1;
		}
		else
	    if(red == 1 && bHavePass && !bPassEntered)
    	{
    		red = 0;
    		gsNewBloodPass = "";
    		giEnterBloodPass = 2;
		}
    }
    //
    int text = MenuFindNamed("blood_color");
    string str;
    if ( text && USE_GREEN_BLOOD )
    {
        if ( red )
          str = GetString("menu","RedBlood");
        else
          str = GetString("menu","GreenBlood");
        Action(text,ACT_SET_TEXT,&str);
        SetGlobValI("Blood",red);
        //
        ResetPassLabels();
    }
    //
    ShowCurBloodPass();
}


//---------------------------------------------------------------------------------------
CreateOptionsMenu(string file)
{
  int menu;
  MenuRelease();
  MenuLoad(file);
  SoundVolume = ( GetGlobValI("SoundVolume","100")*15 + 50 )/100;	//zmNew05Aug02 + 50
  MusicVolume = ( GetGlobValI("MusicVolume","100")*15 + 50 )/100;	//zmNew05Aug02 + 50
  SetNewSoundVolume( SoundVolume );
  SetNewMusicVolume( MusicVolume );
  SetNewGraphDetail( GetGlobValI("GraphDetail","2") );
  SetNewBlood(GetGlobValI("Blood","1"));
  ShowCurBloodPass();
  //
  if(GetGlobValI("Blood",1) == 0 && GetGlobValS("BloodPass", "") == "")
  {
    gsNewBloodPass = "";
    giEnterBloodPass = 1;
  }
  //
  ResetPassLabels();
}


//---------------------------------------------------------------------------------------
TactOptionsMenu()
{
	gbWasEnterBloodPass = giEnterBloodPass;
	bool bEnteredValidPass = false;
	//
	if(giEnterBloodPass)
	{
		if( GetKey()==VK_BACK && strlen(gsNewBloodPass) > 0 )
			gsNewBloodPass[strlen(gsNewBloodPass)-1] = 0;
		else if( GetKey()==VK_RETURN || (MenuLClick() && MenuNVidUnderCursor()!=708) )
		{
			if(giEnterBloodPass == 1)
				SetGlobValS("BloodPass", gsNewBloodPass);
			else
			if(giEnterBloodPass == 2)
			{
				if(gsNewBloodPass == GetGlobValS("BloodPass",""))
				{
					SetNewBlood(2);
					SetGlobValS("BloodPass", "");
					bEnteredValidPass = true;
				}
				else
					PlaySFX(46);
			}
			giEnterBloodPass = 0;
			ResetPassLabels();
		}
		else if( GetKey()==VK_ESCAPE )
		{
			giEnterBloodPass = 0;
			ResetPassLabels();
		}
		else if( strlen(gsNewBloodPass) < 11 && GetKey() >= 'A' && GetKey() <= 'Z' )
        	gsNewBloodPass += Printf("%c",GetKey());
		else if( strlen(gsNewBloodPass) < 11 && GetKey() >= 'a' && GetKey() <= 'z' )
			gsNewBloodPass += Printf("%c",GetKey()-('a'-'A'));
		else if( strlen(gsNewBloodPass) < 11 && GetKey() >= '0' && GetKey() <= '9' )
			gsNewBloodPass += Printf("%c",GetKey());
		else if( GetKey() && GetKey() < 255 )
			PlaySFX(46);//нажата неверная клавиша
	}
	//
	if(!giEnterBloodPass && MenuLClick() && GetUnitVid(MenuLClick()) == 708 && GetGlobValI("Blood",1) == 0)
	{
		bool bHavePass = (strlen(GetGlobValS("BloodPass", "")) != 0);
		//
		gsNewBloodPass = "";
		if(bHavePass)
   			giEnterBloodPass = 2;
		else
   			giEnterBloodPass = 1;
		//
		ResetPassLabels();
	}

	//---------
  if ( MenuLClick() )
    if ( MenuNVidUnderCursor()==716 ) // кнопки >, <
    {
      if ( Action(MenuLClick(), ACT_GET_NAME) == "<sfx" )
        SetNewSoundVolume( SoundVolume - 1 );
      else if ( Action(MenuLClick(), ACT_GET_NAME) == ">sfx" )
        SetNewSoundVolume( SoundVolume + 1 );
      else if ( Action(MenuLClick(), ACT_GET_NAME) == "<music" )
        SetNewMusicVolume( MusicVolume - 1 );
      else if ( Action(MenuLClick(), ACT_GET_NAME) == ">music" )
        SetNewMusicVolume( MusicVolume + 1 );
      else if ( Action(MenuLClick(), ACT_GET_NAME) == "<graph" )
        SetNewGraphDetail( GetGlobValI("GraphDetail","2") - 1 );
      else if ( Action(MenuLClick(), ACT_GET_NAME) == ">graph" )
        SetNewGraphDetail( GetGlobValI("GraphDetail","2") + 1 );
      else if ( Action(MenuLClick(), ACT_GET_NAME) == "<blood" && !bEnteredValidPass)
        SetNewBlood(GetGlobValI("Blood","1") ^ 1);
      else if ( Action(MenuLClick(), ACT_GET_NAME) == ">blood" && !bEnteredValidPass)
        SetNewBlood(GetGlobValI("Blood","1") ^ 1);
    }
    else if( MenuNVidUnderCursor()==717 )
      if ( Action(MenuLClick(), ACT_GET_NAME) == "blood_button")
      {
        if(!bEnteredValidPass)
          SetNewBlood(GetGlobValI("Blood","1") ^ 1);
      }
      else if( GetGlobValI("GraphDetail","2")==2 )
        SetNewGraphDetail( 0 );
      else
        SetNewGraphDetail( GetGlobValI("GraphDetail","2") + 1 );
	//
	ShowCurBloodPass();
}


//---------------------------------------------------------------------------------
AssignOptions()
{
    SetSoundVolume( GetGlobValI("SoundVolume","100") );
    SetMusicVolume( GetGlobValI("MusicVolume","100") );
}

