//Miner Star
#include "maps\header.ini"

int time;
int unit,i;
int timedeath[11]={1,1,0,0,0,0,0,0,0,0,0};
int tp_x[11]=  {2190,1200,165 ,245 ,265 ,525 ,1335,1835,2175,2145,1425};
int tp_y[11]=  {1492,1200,2022,1457,512 ,207 ,407 ,262 ,522 ,927 ,1992};
int tp_map[11]={3,   4,   8,   7};
int tp_dir[11]={0,   128, 128, 128};
int tp_no=2,tp_in=-1,tp_go=0;//if tp_go!=0 then Flagman(0) отъехал от входного телепортера
string miner_path[4];
string laser_path[9];
int miner_unit[4],timeminer[4]={0,0,0,0},laser_unit[9];

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);
    miner_unit[0]=GetObject(V_M_MINER,744,1168);
    miner_unit[1]=GetObject(V_M_MINER,1200,880);
    miner_unit[2]=GetObject(V_M_MINER,1416,1216);
    miner_unit[3]=GetObject(V_M_MINER,1200,1296);
    for(i=0;i<4;i++) miner_path[i]=GetCommands(miner_unit[i]);
    for(i=0;i<9;i++) laser_unit[i]=GetObject(V_M_LASERTANK,tp_x[i+2],tp_y[i+2]);
    for(i=0;i<9;i++) laser_path[i]=GetCommands(laser_unit[i]);
    if(opt_detail) SetWind(25,200);//сила, направление
    MaxUnit(V_FLYBUSH,15);
/*    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)
  {
    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=3,music2=3,music3=3,music4=3);
  }
  if((GetTime()-time)>10000)
  {
    time=GetTime();Create(V_FLYBUSH,0,Random(2000),80);
    if(!Random(4)) Create(V_METEORITRAIN,150+Random(450),100+Random(340),1000);
  }
  iff(SizeTo(Flagman(0),1200,1200)<30)
  {
    Create(V_TP_EFFECT,1200,1200,10);
    Win();
    Action(Flagman(0),act_destroy,0,0);
  }
  if(SizeTo(i=GetObject(V_M_LASERTANK,1200,1200),1200,1200)<30)
  {
    Create(V_TP_EFFECT,1200,1200,10);Action(i,act_destroy,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; }
//    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]);
//    }
  }
  for(i=0;i<4;i++)
  {
    if(miner_unit[i])
    {
      for(unit=FirstUnit(2,u_monster); unit; unit=NextUnit())
        if(unit==miner_unit[i]) break;
      if(!unit) { timeminer[i]=GetTime();miner_unit[i]=0; }
    } else
      if((GetTime()-timeminer[i])>6000)
        if(GetObject(V_TELEPORT,1200,1200))
          if(!AskPlace(V_M_MINER,1200,1200,0))
          {
            miner_unit[i]=Create(V_M_MINER,1200,1200,0);
            SetCommands(miner_unit[i],miner_path[i]);
            Create(V_TP_EFFECT,1200,1200,10);
            if(!Random(2)) Action(miner_unit[i],act_additem,V_AMMO2+2,0);
          }
  }
  for(i=0;i<9;i++)//возрождение лазерных танков
  {
    if(laser_unit[i])
    {
      for(unit=FirstUnit(1,u_monster); unit; unit=NextUnit())
        if(unit==laser_unit[i]) break;
      if(!unit) { laser_unit[i]=0;timedeath[i+2]=GetTime(); }
    } else
      if((GetTime()-timedeath[i+2])>10000)
        if(GetObject(V_TELEPORT,tp_x[i+2],tp_y[i+2]))
          if(!AskPlace(V_M_LASERTANK,tp_x[i+2],tp_y[i+2],0))
          {
            laser_unit[i]=Create(V_M_LASERTANK,tp_x[i+2],tp_y[i+2],0);
            SetCommands(laser_unit[i],laser_path[i]);
            Action(laser_unit[i],act_setbehave,5,0);
            Create(V_TP_EFFECT,tp_x[i+2],tp_y[i+2],10);
            if(!Random(2)) Action(laser_unit[i],act_additem,V_MEDIC,0);
          }
  }
}
