#include "maps\header.ini"

int i,reset_name,cursor,c;
int file,iplayer[5];
string filename,filename2;

ClearReset()
{
  filename="save1";filename[4]=n_player+'1';
  file=FOpen(filename);player_name=FRead(file);FClose(file);
  DeleteText(iplayer[n_player]);
  iplayer[n_player]=PrintText(centr_x-59,centr_y-64+n_player*19,player_name,V_FONT);
  reset_name=0;Action(cursor,act_destroy,0,0);
  for(i=0;i<5;i++)
  {
    MenuAction(V_RECTANGLE,i,act_destroy);
    MenuCreate(V_RECTANGLE,i,centr_x-30,centr_y-60+i*19);
  }
}
NewReset()
{
  if(reset_name) ClearReset();
  for(i=0;i<5;i++) MenuAction(V_RECTANGLE,i,act_destroy);
  player_name="";map=0;hp=40;nweapon=0;opt_player=OPT_DIFFICALT;
  noammo[0]=20;noammo[1]=0;noammo[2]=0;noammo[3]=0;noammo[4]=0;
  weapon[0]=V_FLAGMANHEAD11;weapon[1]=weapon[2]=weapon[3]=0;
  kassandra=V_TRANSMISSION2;upgrade=1+(1<<14);//585;
  n_player=(nmenu-2)%5;reset_name=1;i=0;
  DeleteText(iplayer[n_player]);
  cursor=MenuCreate(130,0,centr_x-56+7*strlen(player_name),centr_y-60+n_player*19);
  iplayer[n_player]=PrintText(centr_x-59,centr_y-64+n_player*19,player_name,V_FONT);
}

main()
{
  iff(1)
  {
    PlayMusic(10,10,10,10);
    ScrollLock(1);
    if(opt_resolution) LoadMenu("maps\\player8.men");
    else LoadMenu("maps\\player.men");
    menu=MenuCreate(V_TEXTMENU,nmenu=2,centr_x,centr_y);
    n_map=1;win=0;map=0;n_player=0;reset_name=0;
    for(i=0;i<5;i++)
    {
      filename="save1";filename[4]=i+'1';
      file=FOpen(filename);player_name=FRead(file);FClose(file);
      iplayer[i]=PrintText(centr_x-59,centr_y-64+i*19,player_name,V_FONT);
      MenuCreate(V_RECTANGLE,i,centr_x-30,centr_y-60+i*19);
    }
  }
  if(!reset_name)
  {
    if(MenuSelectVid()==V_RECTANGLE)
    {
      Action(menu,act_destroy,0,0);
      menu=MenuCreate(V_TEXTMENU,nmenu=2+MenuDir(),centr_x,centr_y);
    } else if(MenuSelectVid()==V_4BUTTON)
    {
      Action(menu,act_destroy,0,0);
      menu=MenuCreate(V_TEXTMENU,nmenu=7,centr_x,centr_y);
    }
    if(!MoveInMenu(2,7))
      if(/*GetKey()==27 ||*/ MenuVid()==V_4BUTTON)
        NextMap("maps\\credits.map",eff_fade);
      else if(MenuVid()==V_RECTANGLE || GetKey()==13 || GetKey()==' ')
        if(nmenu<7)
        {
          n_player=nmenu-2;
          filename="save1";filename[4]=n_player+'1';
          file=FOpen(filename);player_name=FRead(file);FClose(file);
          if(player_name==STR_NONAME) NewReset();
          else NextMap("maps\\maps.map",eff_none);
        }
        else NextMap("maps\\credits.map",eff_fade);
      else if( (GetKey()==5 || (GetInputState()&INPUT_RCLICK)) && nmenu<7)
        NewReset();
  } else if(reset_name==1)//enter name
    if(GetKey()==27 || MenuVid()==V_4BUTTON || (GetInputState()&INPUT_RCLICK) )
      ClearReset();
    else if(GetKey()==13 || (GetInputState()&INPUT_LCLICK))
    {
      for(i=0;i<5;i++)
        { DeleteText(iplayer[i]);MenuAction(V_RECTANGLE,i,act_destroy); }
      Action(menu,act_destroy,0,0);Action(cursor,act_destroy,0,0);
      menu=MenuCreate(V_TEXTMENU,nmenu=0,centr_x,centr_y);
      MenuCreate(V_RECTANGLE,0,centr_x-100,centr_y);
      MenuCreate(V_RECTANGLE,1,centr_x+100,centr_y);
      reset_name=2;
    } else if(GetKey()==8)
    {
      if(i) { i--;player_name[i]=0; }
      DeleteText(iplayer[n_player]);
      iplayer[n_player]=PrintText(centr_x-59,centr_y-64+n_player*19,player_name,V_FONT);
      Action(cursor,act_destroy,0,0);
      cursor=MenuCreate(130,0,centr_x-56+7*strlen(player_name),centr_y-60+n_player*19);
    } else if( GetKey()>=' ' && GetKey()<='z' && i<15 )
    {
      c=GetKey();if(c>='a' && c<='z') c=c-('a'-'A');
      player_name[i++]=c;player_name[i]=0;
      DeleteText(iplayer[n_player]);
      iplayer[n_player]=PrintText(centr_x-59,centr_y-64+n_player*19,player_name,V_FONT);
      Action(cursor,act_destroy,0,0);
      cursor=MenuCreate(130,0,centr_x-56+7*strlen(player_name),centr_y-60+n_player*19);
    } else;
  else if(reset_name==2)//enter difficalt
  {
    if(GetKey()>=1 && GetKey()<=4)//left || right
    {
      Action(menu,act_destroy,0,0);PlaySFX(20);
      nmenu=nmenu^1;
      menu=MenuCreate(V_TEXTMENU,nmenu,centr_x,centr_y);
    } else if(GetKey()==27 || MenuVid()==V_4BUTTON || (GetInputState()&INPUT_RCLICK) )
    {
      reset_name=0;
      Action(menu,act_destroy,0,0);
      menu=MenuCreate(V_TEXTMENU,nmenu=7,centr_x,centr_y);
      for(i=0;i<5;i++)
      {
        filename="save1";filename[4]=i+'1';
        file=FOpen(filename);player_name=FRead(file);FClose(file);
        iplayer[i]=PrintText(centr_x-59,centr_y-64+i*19,player_name,V_FONT);
        MenuAction(V_RECTANGLE,i,act_destroy);
        MenuCreate(V_RECTANGLE,i,centr_x-30,centr_y-60+i*19);
      }
    } else if(MenuSelectVid()==V_RECTANGLE)
    {
      Action(menu,act_destroy,0,0);
      nmenu=MenuDir()^1;
      menu=MenuCreate(V_TEXTMENU,nmenu,centr_x,centr_y);
    }
    if( MenuVid()==V_RECTANGLE || GetKey()==13 || GetKey()==' ' )
    {
      if(nmenu) opt_player=0;
      SavePlayer();NextMap("maps\\maps.map",eff_none);
      return;
    }
  }
  if(nextmap!="")
    if((GetTime()-startnextmap)>700)
      { Load(nextmap);if(nextmapeffect==eff_none) PlaySFX(19);return; }
}
