//Demo
#include "maps\header.ini"

int time,tptime,guntime;
int unit,i,no_runner=0;
int timedeath[9]={0,0,0,0,0,0,0,0,0};
int tp_x[9]=  {1884,545 ,755 ,905,785 ,545 ,275 ,185 ,275 };
int tp_y[9]=  {480 ,182 ,262 ,487,707 ,807 ,692 ,487 ,247 };
int tp_map[4]={4,   5,   9,   10};
int tp_dir[4]={32,  64,  192, 192};
int tp_no=0,tp_in=-1,tp_go=0;//if tp_go!=0 then Flagman(0) отъехал от входного телепортера
int repair_mode=0;//if !=0 then some turrel is dead
int alarm_sfx=0;//if !=0 then alarm is played
int pause;

main()
{
  iff(1)
  {
    SetVid(199,VID_CHILD+15,-200);SetVid(74,VID_CHILD+15,200);
    SetVid(19,VID_CHILD+2,116);SetVid(19,VID_CHILD+4,116);SetVid(19,VID_CHILD+5,116);
    SetVid(24,VID_CHILD+2,116);SetVid(24,VID_CHILD+4,116);SetVid(24,VID_CHILD+5,116);
    SetVid(33,VID_CHILD+2,116);SetVid(33,VID_CHILD+4,116);SetVid(33,VID_CHILD+5,116);
    SetVid(43,VID_CHILD+2,116);SetVid(43,VID_CHILD+4,116);SetVid(43,VID_CHILD+5,116);
    SetVid(79,VID_CHILD+2,116);SetVid(79,VID_CHILD+4,116);SetVid(79,VID_CHILD+5,116);
    SetVid(81,VID_CHILD+2,116);SetVid(81,VID_CHILD+4,116);SetVid(81,VID_CHILD+5,116);
    SetVid(132,VID_CHILD+2,116);SetVid(132,VID_CHILD+4,116);SetVid(132,VID_CHILD+5,116);
    SetVid(138,VID_CHILD+2,116);SetVid(138,VID_CHILD+4,116);SetVid(138,VID_CHILD+5,116);
    SetVid(143,VID_CHILD+2,116);SetVid(143,VID_CHILD+4,116);SetVid(143,VID_CHILD+5,116);
    SetVid(140,VID_CHILD+15,116);SetVid(221,VID_CHILD+15,116);SetVid(314,VID_CHILD+15,116);
    SetVid(348,VID_CHILD+15,116);SetVid(350,VID_CHILD+15,116);SetVid(237,VID_CHILD+14,60);
    if(opt_detail) SetWind(25,200);//сила, направление
    MaxUnit(V_FLYBUSH,10);
    time=GetTime();guntime=GetTime();
/*    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;*/
    i=Create(kassandra,531,482,0);
//    Action(Flagman(0),act_changeaction,act_stand,tp_dir[tp_in]);
    SetPosition(552,461);
  }
  if(Tact()) return;
  iff(1)
  {
    Action(i,act_destroy,0,0);
    kassandra=V_TRANSMISSION2;i=Create(kassandra,531,482,0);Action(i,act_damage,(maxhp>>1)+10,0);
    SetFlagman(0,i);
    SetVid(195,VID_MAXHP,0);//сделать неуязвимыми остатки башен
    SetVid(157,VID_MAXHP,70);//сделать более слабыми башни
    PlayMusic(music1=8,music2=9,music3=8,music4=9);
  }
  if((GetTime()-time)>10000)
  {
    time=GetTime();Create(V_FLYBUSH,0,Random(400),80);
  }
  repair_mode=0;
  for(i=1;i<9;i++)
    if(!unit=GetObject(V_M_AUTOTURREL,tp_x[i],tp_y[i]))
    {
      if(!tp_no) { tp_no=1;timedeath[0]=GetTime(); }
      repair_mode=i;
    } else Action(unit,act_setbehave,0,0);
  if(tp_no)
  {
    for(i=FirstUnit(1,u_avia);i;i=NextUnit())
      if(GetUnitVid(i)==V_M_REPAIR) break;
    if(!i)
      if(repair_mode)
      {
        if(!pause) pause=GetTime();
        else if((GetTime()-pause)>5000 && !AskPlace(V_M_REPAIR,tp_x[0],tp_y[0],40) && GetObject(V_TELEPORT,tp_x[0],tp_y[0]))
        {
          pause=0;Create(V_TP_EFFECT,tp_x[0],tp_y[0],30);
          Action(Create(V_M_REPAIR,tp_x[0],tp_y[0],10),act_attack,GetObject(195,tp_x[repair_mode],tp_y[repair_mode]),0);
        }
      } else
      {
        tp_no=0;Effect(eff_bnuke);Create(V_TP_EFFECT,tp_x[0],tp_y[0],30);
        Action(GetObject(V_TELEPORT,tp_x[0],tp_y[0]),act_destroy,0,0);
      }
    else
      if( !(Action(i,act_getstate,0,0)&st_command) )
        if(repair_mode) Action(i,act_attack,GetObject(195,tp_x[repair_mode],tp_y[repair_mode]),0);
        else Action(i,act_move,tp_x[0],tp_y[0]);
  }
  if((GetTime()-guntime)>8000 && !repair_mode && !alarm_sfx)//alarm
    { PlaySFX(25);alarm_sfx=1; }
  if((GetTime()-guntime)>10000 && !repair_mode)//turrels - fire
  {
    guntime=GetTime();alarm_sfx=0;
    for(i=1;i<9;i++)
      Action(GetObject(V_M_AUTOTURREL,tp_x[i],tp_y[i]),act_coor_attack,531,482);
  }
  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;Effect(eff_bnuke); }
/*    iff( SizeTo(Flagman(0),tp_x[i],tp_y[i])<30 && tp_go )
    {
      Create(V_TP_EFFECT,tp_x[i],tp_y[i],10);NextNMap(tp_map[i]);
    }*/
  }
  if(GetObject(V_TELEPORT,tp_x[0],tp_y[0]))
  {
    iff(SizeTo(Flagman(0),tp_x[0],tp_y[0])<30)
    {
      Create(V_TP_EFFECT,tp_x[0],tp_y[0],10);
      Win();
      SetVid(157,VID_MAXHP,125);//вернуть жизнь башням
      SetVid(195,VID_MAXHP,20);//вернуть уязвимость остаткам башен
      Action(Flagman(0),act_destroy,0,0);
    }
    if(!repair_mode)
      if(unit=GetObject(V_M_REPAIR,tp_x[0],tp_y[0]))
      {
        Action(unit,act_destroy,0,0);Create(V_TP_EFFECT,tp_x[0],tp_y[0],10);
      }
    if(unit=GetObject(V_M_LASERTANK,tp_x[0],tp_y[0]))
    {
      Action(unit,act_destroy,0,0);no_runner++;
      Create(V_TP_EFFECT,tp_x[0],tp_y[0],10);
    }
    for(unit=FirstUnit(1,u_monster);unit;unit=NextUnit())
      if(GetUnitVid(unit)==V_M_LASERTANK) break;
    iff((no_runner>=3 || !unit) && tp_no==0)
      Action(GetObject(V_TELEPORT,tp_x[0],tp_y[0]),act_destroy,0,0);
  }
}
