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

int time,timewin=0;
int unit,i,gate_off=0;
int tank_in_plant;
int timedeath[6]={1,1,0,0,0,0};
int tp_x[6]=  {292, 438 ,3846,4974,1535,1315};
int tp_y[6]=  {324, 2420,4260,4260,3435,3330};
int tp_map[6]={0,   2};
int tp_dir[6]={96,   96};
int tp_no=6,tp_in=-1,tp_go=0;//if tp_go!=0 then Flagman(0) отъехал от входного телепортера
int tp=0;

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);//сила, направление
//    SetGamma(gamma=1608);
/*    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]);
    MaxUnit(V_M_PLASMATANK,1);MaxUnit(V_M_FIGHTER,2);MaxUnit(V_MANTA,20);
    MaxUnit(V_M_CHOPPER,2);
    unit=GetObject(V_M_GUNTANK,168,261);Action(unit,act_damage,Action(unit,act_gethp,0,0)/2+1,0);
    unit=GetObject(V_M_GUNTANK,456,341);Action(unit,act_damage,Action(unit,act_gethp,0,0)/2+1,0);
    unit=GetObject(V_M_GUNTANK,408,405);Action(unit,act_damage,Action(unit,act_gethp,0,0)/2+1,0);
    unit=GetObject(V_M_GUNTANK,216,469);Action(unit,act_damage,Action(unit,act_gethp,0,0)/2+1,0);
    unit=GetObject(V_M_MINER,288,496);Action(unit,act_damage,Action(unit,act_gethp,0,0)/2+1,0);
  }
  if(Tact()) return;
  iff(1)
  {
    time=GetTime();
    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=4,music2=4,music3=2,music4=3);
  }
  if((GetTime()-time)>5000)
  {
    time=GetTime();
    if(Random(1)) unit=Create(V_MANTA,3130,3000,5);
    else unit=Create(V_MANTA,2578,3271,5);
    Action(unit,act_setarmy,3,0);
  }
  iff(!GetObject(318,4254,142))//коммуникационный центр отключает автоматические туррели
    for(unit=FirstUnit(1,u_monster); unit; unit=NextUnit())
      if(GetUnitVid(unit)==V_M_AUTOTURREL)
      {
        Action(unit,act_clearcom,0,0);Action(unit,act_setbehave,0,0);
        Action(unit,act_deletetarget,Flagman(0),0);
      }
  iff(!GetObject(318,5051,4428))//коммуникационный центр отключает туррели
  {
    gate_off=1;
    Action(GetObject(167,4462,3715),act_destroy,0,0);//gate destroy
    Action(unit=GetObject(V_M_MISSILETURREL,5235,4285),act_setbehave,0,0);Action(unit,act_deletetarget,Flagman(0),0);
    Action(unit=GetObject(V_M_MISSILETURREL,3692,4265),act_setbehave,0,0);Action(unit,act_deletetarget,Flagman(0),0);
  }
  for(unit=FirstUnit(1,u_avia); unit; unit=NextUnit())
    if(GetUnitVid(unit)==V_M_VIDEOCAMERA)
      if(Action(unit,act_getaction,0,0)==act_fight)
      {
        Action(Create(V_M_FIGHTER,3700,20,60),act_attack,Action(unit,act_gettarget,0,0),0);
        Action(Create(V_M_FIGHTER,3700,100,60),act_attack,Action(unit,act_gettarget,0,0),0);
      } else;
    else if(GetUnitVid(unit)==V_MANTA)
      if(SizeTo(unit,2832,3232)>800 && !(Action(unit,act_getstate,0,0)&st_move))
        Action(unit,act_move,2832+Random(1000)-500,3232+Random(1000)-500);
  if(SizeTo(Flagman(0),4462,3735)<50)
  {
    Action(GetObject(V_M_LASERTANK,4296,3781),act_setbehave,1,0);
    Action(GetObject(V_M_LASERTANK,4248,3877),act_setbehave,1,0);
    Action(GetObject(V_M_LASERTANK,4200,3973),act_setbehave,1,0);
    Action(GetObject(V_M_LASERTANK,4632,3781),act_setbehave,1,0);
    Action(GetObject(V_M_LASERTANK,4680,3877),act_setbehave,1,0);
    Action(GetObject(V_M_LASERTANK,4728,3973),act_setbehave,1,0);
  }
  tank_in_plant=0;
  for(unit=FirstUnit(1,u_monster); unit; unit=NextUnit())
    if(GetUnitVid(unit)==V_M_PLASMATANK)
      if(SizeTo(unit,4467,4286)<130) tank_in_plant=1;
      else if(SizeTo(unit,4462,3715)<200) break;
  if(unit)
    Action(GetObject(167,4462,3715),act_destroy,0,0);//gate destroy
  else if(!gate_off) if(!GetObject(167,4462,3715)) Create(167,4462,3715,0);//gate create
  if((i=Action(GetObject(328,4467,4286),act_getaction,0,0))==act_stand && !tank_in_plant)
    SetVid(328,VID_PROPERTY,1088);
  else if(i==act_stand_open) SetVid(328,VID_PROPERTY,1120);
  if(tp)
    if((GetTime()-startnextmap)>400)
    {
      if(tp==2) { Action(Flagman(0),act_changecoor,tp_x[0],tp_y[0]);Create(V_TP_EFFECT,tp_x[0],tp_y[0],10); }
      if(tp==3) { Action(Flagman(0),act_changecoor,6078,196);Create(V_TP_EFFECT,6078,196,10); }
      if(tp==4) { Action(Flagman(0),act_changecoor,tp_x[5],tp_y[5]);Create(V_TP_EFFECT,tp_x[5],tp_y[5],10); }
      tp=0;
    } else;
  else
  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; } else;
    else if(SizeTo(Flagman(0),tp_x[i],tp_y[i])<30 && i>1 && i<5)
      if(Action(Flagman(0),act_askitem,V_ARTEFACT1,0) || i!=4)
      {
        tp=i;startnextmap=GetTime();
        Create(V_TP_EFFECT,tp_x[i],tp_y[i],10);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]);
    }*/
  }
    iff(SizeTo(Flagman(0),tp_x[1],tp_y[1])<30 && GetObject(V_TELEPORT,tp_x[1],tp_y[1]))
    {
      Create(V_TP_EFFECT,tp_x[1],tp_y[1],10);
      Win();
      Action(Flagman(0),act_destroy,0,0);
    }
}