#include "maps\header.ini"

int i;
string filename;
int secondmenu,dirsecondmenu=-1,selectedmenu;

MapsCreate()
{
  secondmenu=0;old_n_map=0;win=0;
  if(opt_resolution) LoadMenu("maps\\maps8.men");
  else LoadMenu("maps\\maps.men");
  LoadPlayer();
  map=map|(1<<6)|(1<<8)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)|(1<<24)|(1<<25)|(1<<27)|(1<<28)|(1<<30);
  if(map&(1<<0)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  MenuCreate(i,0,centr_x-320+202,centr_y-240+111);
  if(map&(1<<1)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&1) || (map&2)) MenuCreate(i,1,centr_x-320+166,centr_y-240+142);
  if(map&(1<<2)) i=V_WINNEDMAP;else i=V_CHOICEMAP;//map3
  if((map&2) || (map&4)) MenuCreate(i,2,centr_x-320+165,centr_y-240+291);
  if(map&(1<<9)) i=V_WINNEDMAP;else i=V_CHOICEMAP;//map10
  if((map&4) || (map&(1<<9))) MenuCreate(i,9,centr_x-320+255,centr_y-240+200);
  if(map&(1<<3)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<9)) || (map&8)) MenuCreate(i,3,centr_x-320+112,centr_y-240+203);
  if(map&(1<<4)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<9)) || (map&16)) MenuCreate(i,4,centr_x-320+149,centr_y-240+222);
  if(map&(1<<5)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&8) || (map&32)) MenuCreate(i,5,centr_x-320+137,centr_y-240+263);
//  if(map&(1<<6)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
//  if((map&24) || (map&64)) MenuCreate(i,6,centr_x-320+155,centr_y-240+189);//4,5
  if(map&(1<<7)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&16) || (map&128)) MenuCreate(i,7,centr_x-320+189,centr_y-240+249);
//  if(map&(1<<8)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
//  if((map&96) || (map&256)) map=map|(1<<9);//MenuCreate(i,8,centr_x-320+225,centr_y-240+169);//6,7
  if(map&(1<<10)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<5)) || (map&(1<<7)) || (map&(1<<10))) MenuCreate(i,10,centr_x-320+327,centr_y-240+214);
  if(map&(1<<11)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<10)) || (map&(1<<11))) MenuCreate(i,11,centr_x-320+283,centr_y-240+274);
  if(map&(1<<12)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<10)) || (map&(1<<12))) MenuCreate(i,12,centr_x-320+304,centr_y-240+248);
  if(map&(1<<13)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<10)) || (map&(1<<13))) MenuCreate(i,13,centr_x-320+362,centr_y-240+89);
  if(map&(1<<14)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<11)) || (map&(1<<12)) || (map&(1<<14))) MenuCreate(i,14,centr_x-320+242,centr_y-240+233);
  if(map&(1<<15)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<12)) || (map&(1<<13)) || (map&(1<<15))) MenuCreate(i,15,centr_x-320+365,centr_y-240+203);//12,13,14
  if(map&(1<<16)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<14)) || (map&(1<<15)) || (map&(1<<16))) MenuCreate(i,16,centr_x-320+287,centr_y-240+112);
  if(map&(1<<17)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<16)) || (map&(1<<17))) MenuCreate(i,17,centr_x-320+309,centr_y-240+84);
  if(map&(1<<18)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<17)) || (map&(1<<18))) MenuCreate(i,18,centr_x-320+377,centr_y-240+170);
  if(map&(1<<26)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<18)) || (map&(1<<26))) MenuCreate(i,26,centr_x-320+365,centr_y-240+312);//24,25,26
  if(map&(1<<29)) i=V_WINNEDMAP;else i=V_CHOICEMAP;
  if((map&(1<<29))) MenuCreate(i,29,centr_x-320+493,centr_y-240+261);
  menu=MenuCreate(V_TEXTMENU,nmenu=27,centr_x,centr_y);
  dirsecondmenu=1;
  for(i=0;i<30;i++)
    if( !(map&(1<<i)) ) { dirsecondmenu=i;break; }
  if(i>2 && !(map&(1<<9))) dirsecondmenu=9;
  filename="maps\\levels\\01s.txt";
  filename[12]=(dirsecondmenu+1)/10+'0';filename[13]=(dirsecondmenu+1)%10+'0';
  secondmenu=PrintFile(centr_x-80,centr_y+117,filename,V_FONT);
  if(!(map&2)) MenuCreate(298,0,centr_x+195,centr_y+116);//close service centr
  selectedmenu=dirsecondmenu;
  MenuAction(V_CHOICEMAP,selectedmenu,act_menustandup);MenuAction(V_WINNEDMAP,selectedmenu,act_menustandup);
}

main()
{
  iff(1)
  {
    if(old_n_map==27 && win) { Load("maps\\win.map");return; }
    ScrollLock(1);
    MapsCreate();
    gamemenu=1;
    PlayMusic(10,10,10,10);
  }
  if(nextmap!="")
    if((GetTime()-startnextmap)>700)
      { Load(nextmap);if(nextmapeffect==eff_none) PlaySFX(19);return; }
  if(gamestate)
  {
    if(gamestate==3)
      if((GetTime()-startnextmap)>700)
        { DeleteText(secondmenu);secondmenu=0;OptionsOn(); }
      else return(1);
    if(gamestate==5)
      if((GetTime()-startnextmap)>700)
        { DeleteText(secondmenu);secondmenu=0;DesignMenuOn(); }
      else return(1);
    if(gamestate==1 || gamestate==2) OptionsTact();
    if(gamestate==4 || gamestate==6) DesignMenuTact();
    if(!gamestate) MapsCreate();
    return(1);
  }
  if(MenuSelectVid()==V_4BUTTON)
  {
    Action(menu,act_destroy,0,0);
    menu=MenuCreate(V_TEXTMENU,nmenu=MenuDir()+26,centr_x,centr_y);
  }
  if(!(map&2) && firstmenu) MoveInMenu(26,28);else MoveInMenu(26,29);
  if(GetKey()==27) NextMap("maps\\player.map",eff_none);//exit to choice player
  else if(GetKey()==13 || MenuVid()==V_4BUTTON || GetKey()==' ')
  {
    if(nmenu==26) NextMap("maps\\player.map",eff_none);//exit to choice player
    if(nmenu==27)//start
    {
//      SaveDemo("demo.dem");//закомментировать данную строку если не хочешь каждый раз писать дему
      NextNMap(selectedmenu+1);
    }
    if(nmenu==28) { gamestate=3;startnextmap=GetTime(); }//options
    if(nmenu==29) { gamestate=5;startnextmap=GetTime(); }//service center
  }
/*  if(GetKey()==1 || GetKey()==2)//left & right key
  {
    MenuAction(V_CHOICEMAP,selectedmenu,act_menustanddown);MenuAction(V_WINNEDMAP,selectedmenu,act_menustanddown);
    if(GetKey()==1) if(--selectedmenu<0) selectedmenu=29;
    if(GetKey()==2) if(++selectedmenu>29) selectedmenu=0;
    MenuAction(V_CHOICEMAP,selectedmenu,act_menustandup);MenuAction(V_WINNEDMAP,selectedmenu,act_menustandup);
  }*/
  if(MenuVid()==V_CHOICEMAP/* || MenuVid()==V_WINNEDMAP*/)
  {
    MenuAction(V_CHOICEMAP,selectedmenu,act_menustanddown);MenuAction(V_WINNEDMAP,selectedmenu,act_menustanddown);
    selectedmenu=MenuDir();
    MenuAction(V_CHOICEMAP,selectedmenu,act_menustandup);MenuAction(V_WINNEDMAP,selectedmenu,act_menustandup);
  }
  if(MenuSelectVid()==V_CHOICEMAP  || MenuSelectVid()==V_WINNEDMAP)
    if(dirsecondmenu!=MenuDir())
    {
      if(secondmenu) DeleteText(secondmenu);//Action(secondmenu,act_destroy,0,0);
      dirsecondmenu=MenuDir();
      filename="maps\\levels\\01s.txt";
      filename[12]=(dirsecondmenu+1)/10+'0';filename[13]=(dirsecondmenu+1)%10+'0';
      secondmenu=PrintFile(centr_x-80,centr_y+117,filename,V_FONT);
    } else;
  else
    if(dirsecondmenu!=selectedmenu)
    {
      if(secondmenu) DeleteText(secondmenu);//Action(secondmenu,act_destroy,0,0);
      dirsecondmenu=selectedmenu;
      filename="maps\\levels\\01s.txt";
      filename[12]=(dirsecondmenu+1)/10+'0';filename[13]=(dirsecondmenu+1)%10+'0';
      secondmenu=PrintFile(centr_x-80,centr_y+117,filename,V_FONT);
    }
}
