#include "maps\header.ini"

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

MapsCreate()
{
  secondmenu=0;old_n_map=0;win=0;
  if(opt_resolution) LoadMenu("maps\\maps8.men");
  else LoadMenu("maps\\maps.men");
//  LoadPlayer();
  MenuCreate(V_CHOICEMAP,11,centr_x-320+183,centr_y-240+127);
  MenuCreate(V_CHOICEMAP,4,centr_x-320+112,centr_y-240+203);
  menu=MenuCreate(V_TEXTMENU,nmenu=27,centr_x,centr_y);
  dirsecondmenu=11;
  filename="maps\\net\\01s.txt";
  filename[9]=(dirsecondmenu+1)/10+'0';filename[10]=(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);
}

main()
{
  iff(1)
  {
    SetNet(0);SetCursor(0);
    for(n_flagman=0;n_flagman<GetNetInfo(NET_NOPLAYER);n_flagman++)
    {
      net_birth[n_flagman]=0;
    }
    upgrade=1073741823;
    if(count=FOpen("fw.ini"))
    {
      opt_resolution=FRead(count);opt_detail=FRead(count);opt_detail=1;
      opt_sound=FRead(count);opt_music=FRead(count);
      opt_scrolling=FRead(count);
      FClose(count);
    } else { opt_detail=1;opt_resolution=0;opt_sound=50;opt_music=50; }
    if(opt_resolution) { centr_x=400;centr_y=300; }
    else { centr_x=320;centr_y=240; }
    SetGraphMode(opt_resolution+1);
    SetGraphDetail(opt_detail);SetAutoScroll(opt_scrolling);
    SetSound(opt_sound);SetMusic(opt_music);
    ScrollLock(1);
    MapsCreate();
    NetMenuOn();
    gamestate=0;gamemenu=1;
    n_flagman=GetNetInfo(NET_NUMPLAYER);
  }
  if(GetNetInfo(NET_NOPLAYER)!=noplayer)
  {
    NetMenuOff();NetMenuOn();
    filename=noplayer=GetNetInfo(NET_NOPLAYER);
  }
  net=0;
  if(nextmap!="")
    if((GetTime()-startnextmap)>700)
      if(nextmap!="exit")
        { Load(nextmap);if(nextmapeffect==eff_none) PlaySFX(19);return; }
      else { Exit();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("exit",eff_fade);//exit to choice player
  else if(GetKey()==13 || MenuVid()==V_4BUTTON || GetKey()==' ')
  {
    if(nmenu==26) NextMap("exit",eff_fade);//exit to choice player
    if(nmenu==27 && GetNetInfo(NET_NOPLAYER)>1)//start
    {
      net=selectedmenu+1;
    }
    if(nmenu==28) { gamestate=3;startnextmap=GetTime(); }//options
    if(nmenu==29) { gamestate=5;startnextmap=GetTime(); }//service center
  }
  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\\net\\01s.txt";
      filename[9]=(dirsecondmenu+1)/10+'0';filename[10]=(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\\net\\01s.txt";
      filename[9]=(dirsecondmenu+1)/10+'0';filename[10]=(dirsecondmenu+1)%10+'0';
      secondmenu=PrintFile(centr_x-80,centr_y+117,filename,V_FONT);
    }
  if(net) Send(net); else net=Receive();
  if(net==-1) NextMap("exit",eff_fade);
  else if(net && net!=100)
  {
    for(n_flagman=0;n_flagman<GetNetInfo(NET_NOPLAYER);n_flagman++)
    {
      net_kill[n_flagman]=net_birth[n_flagman]=0;
      GetNetInfo(NET_CLEARKILL);
    }
    filename="maps\\net\\01.map";
    filename[9]=net/10+'0';filename[10]=net%10+'0';
    Load(filename);
    SetNet(1);
  }
}
