//Service Centre
#include "maps\header.ini"

int time,i;
int unit;
int timedeath[2]={1,1};
int tp_x[2]=  {2719, 559};
int tp_y[2]=  {2805, 164};
int tp_map[2]={1,   3};
int tp_dir[2]={28,  156};
int tp_no=2,tp_in=-1,tp_go=0;//if tp_go!=0 then Flagman(0) отъехал от входного телепортера
string minepath1,minepath2,minepath3,tankpath1,tankpath2;
int mine1,mine2,mine3,tank1,tank2;
int load_flag=0;

main()
{
  iff(1)
  {
    SetVid(199,VID_CHILD+15,-320);SetVid(74,VID_CHILD+15,320);
    SetVid(19,VID_CHILD+2,319);SetVid(19,VID_CHILD+4,319);SetVid(19,VID_CHILD+5,319);
    SetVid(24,VID_CHILD+2,319);SetVid(24,VID_CHILD+4,319);SetVid(24,VID_CHILD+5,319);
    SetVid(33,VID_CHILD+2,319);SetVid(33,VID_CHILD+4,319);SetVid(33,VID_CHILD+5,319);
    SetVid(43,VID_CHILD+2,319);SetVid(43,VID_CHILD+4,319);SetVid(43,VID_CHILD+5,319);
    SetVid(79,VID_CHILD+2,319);SetVid(79,VID_CHILD+4,319);SetVid(79,VID_CHILD+5,319);
    SetVid(81,VID_CHILD+2,319);SetVid(81,VID_CHILD+4,319);SetVid(81,VID_CHILD+5,319);
    SetVid(132,VID_CHILD+2,319);SetVid(132,VID_CHILD+4,319);SetVid(132,VID_CHILD+5,319);
    SetVid(138,VID_CHILD+2,319);SetVid(138,VID_CHILD+4,319);SetVid(138,VID_CHILD+5,319);
    SetVid(143,VID_CHILD+2,319);SetVid(143,VID_CHILD+4,319);SetVid(143,VID_CHILD+5,319);
    SetVid(140,VID_CHILD+15,319);SetVid(221,VID_CHILD+15,319);SetVid(314,VID_CHILD+15,319);
    SetVid(348,VID_CHILD+15,319);SetVid(350,VID_CHILD+15,319);SetVid(237,VID_CHILD+14,60);
    minepath1=GetCommands(mine1=GetObject(V_M_MINE,2295,2915));Action(mine1,act_clearcom,0,0);
    minepath2=GetCommands(mine2=GetObject(V_M_MINE,1381,2864));Action(mine2,act_clearcom,0,0);
    minepath3=GetCommands(mine3=GetObject(V_M_MINE, 334,1522));Action(mine3,act_clearcom,0,0);
    tankpath1=GetCommands(tank1=GetObject(V_M_GUNTANK,122,922));Action(tank1,act_clearcom,0,0);
    tankpath2=GetCommands(tank2=GetObject(V_M_GUNTANK,146,979));Action(tank2,act_clearcom,0,0);
/*    for(i=0;i<tp_no;i++) if(old_n_map==tp_map[i]) tp_in=i;
    if(tp_in<0)
      for(i=tp_no-1;i>=0;i--) if( map&(1<<(tp_map[i]-1)) ) tp_in=i;*/
    if(tp_in<0) tp_in=0;
    SetPosition(tp_x[tp_in],tp_y[tp_in]);
  }
  if(Tact()) return;
  iff(1)
  {
    if(opt_detail) SetWind(25,200);//сила, направление
    MaxUnit(V_FLYBUSH,10);
    time=GetTime();//timedeath=timedeath2=0;
    SetFlagman(0,Create(kassandra,tp_x[tp_in],tp_y[tp_in],0));
    Action(Flagman(0),act_changeaction,act_stand,tp_dir[tp_in]);
    Create(V_TP_EFFECT,tp_x[tp_in],tp_y[tp_in],10);
    PlayMusic(music1=2,music2=2,music3=2,music4=2);
  }

  if((GetTime()-time)>10000)
  {
    time=GetTime();Create(V_FLYBUSH,0,Random(900),80);
  }
  if(Action(Flagman(0),act_askitem,V_ARTEFACT1,0))
    iff(SizeTo(Flagman(0),tp_x[1],tp_y[1])<30 && GetObject(V_TELEPORT,tp_x[1],tp_y[1]))
    {
      upgrade=upgrade|8;
      Create(V_TP_EFFECT,tp_x[1],tp_y[1],10);
      Win();
      Action(Flagman(0),act_destroy,0,0);
    }
  iff(SizeTo(Flagman(0),2423,2613)<100) { Action(mine1,act_setbehave,5,0);SetCommands(mine1,minepath1); }
  iff(SizeTo(Flagman(0),1189,2603)<200) { Action(mine2,act_setbehave,5,0);SetCommands(mine2,minepath2); }
  iff(SizeTo(Flagman(0), 600,1871)<100) { Action(mine3,act_setbehave,5,0);SetCommands(mine3,minepath3); }
  iff(SizeTo(Flagman(0),300,1123)<100) { SetCommands(tank1,tankpath1);SetCommands(tank2,tankpath2); }
 if(Action(Flagman(0),act_askitem,V_ARTEFACT1,0))
  if(SizeTo(Flagman(0),1561,1873)<600)
    if(!(Action(unit=GetObject(V_M_FACTORY,1561,1873),act_getbehave,0,0)&128))
    {
      AddCommand(unit,act_open,0,0);AddCommand(unit,act_unload,0,0);
      AddCommand(unit,act_stand_open,0,0);
      Action(unit,act_setbehave,128|Action(unit,act_getbehave,0,0),0);
    } else
      if(SizeTo(Flagman(0),1561,1863)<15)
        if(load_flag)
        {
          Pause(0);StateBar(opt_player&OPT_STATEBAR);
          for(count=0;count<8;count++)
            noammo[count]=Action(Flagman(0),act_getammo,count,0);
          load_flag=0;Action(Flagman(0),act_destroy,0,0);
          SetFlagman(0,Create(kassandra,1561,1872,-87));
          Action(Flagman(0),act_changeweapon,weapon[0],0);
          Action(Flagman(0),act_additem,V_ARTEFACT1,0);
          for(count=0;count<8;count++)
            Action(Flagman(0),act_addammo,noammo[count],count);
          Action(unit,act_unload_object,Flagman(0),0);
        } else if(Action(unit,act_getaction,0,0)!=act_stand)
          Action(unit,act_load_object,Flagman(0),0);
        else
        {
          startnextmap=GetTime();gamestate=5;load_flag=1;
          upgrade=upgrade|8;
        }
      else;
  else
    if(Action(unit=GetObject(V_M_FACTORY,1561,1873),act_getbehave,0,0)&128)
    {
      AddCommand(unit,act_load,0,0);AddCommand(unit,act_close,0,0);
      Action(unit,act_setbehave,~128&Action(unit,act_getbehave,0,0),0);
    }
//  iff(SizeTo(Flagman(0),tp_x[tp_in],tp_y[tp_in])>40) tp_go=1;
  for(i=0;i<tp_no;i++)
  {
    if(!GetObject(V_TELEPORT,tp_x[i],tp_y[i]))
      if(!timedeath[i]) timedeath[i]=GetTime();
      else if((GetTime()-timedeath[i])>4000)
        { Create(V_TELEPORT,tp_x[i],tp_y[i],0);timedeath[i]=0; }
  }
}
