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

int time,i;
int time_artefact=0;//if !=0 then need create artefact
int unit,plant;
int plant_work=0;//!=0 if tank in plant
int plant_close=0;
int fail_tank;//танк мешающий закрыться заводу
int size;
int timedeath[4]={1,1,1,1};
int tp_x[4]=  {2670, 294, 366,2766};
int tp_y[4]=  {2708, 2628,244,180};
int tp_map[4]={1,   3};
int tp_dir[4]={160, 128};
int tp_no=4,tp_in=-1,tp_go=0;//if tp_go!=0 then Flagman(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);
    SetGamma(gamma=1908);
/*    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(nextmap!="") SetVid(328,VID_PROPERTY,1088);
  if(Tact()) return;
  iff(1)
  {
    if(opt_detail) SetWind(25,200);//сила, направление
    time_artefact=GetTime()-120;
    timedeath[1]=GetTime();timedeath[2]=GetTime()-4000;timedeath[3]=GetTime()-8000;
    MaxUnit(V_M_RL,2);MaxUnit(V_M_GUNTANK,2);MaxUnit(V_M_PLASMATANK,2);MaxUnit(V_M_LASERTANK,2);
    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=5,music2=5,music3=5,music4=5);
  }
  for(i=1;i<4;i++)
    iff(SizeTo(Flagman(0),tp_x[i],tp_y[i])<30 && Action(Flagman(0),act_askitem,V_ARTEFACT1,0))
    {
      Create(V_TP_EFFECT,tp_x[i],tp_y[i],10);
      Win();
      Action(Flagman(0),act_destroy,0,0);
    }
  if(plant=GetObject(328,1518,1502))//Vehicle Plant
  {
    if(plant_work && (GetTime()-plant_work)>7000 && (Action(plant,act_getbehave,0,0)&128))
    {
      AddCommand(plant,act_open,0,0);AddCommand(plant,act_stand_open,0,0);
      Action(plant,act_setbehave,~128&Action(plant,act_getbehave,0,0),0);
      SetVid(328,VID_PROPERTY,1120);
    }
    fail_tank=0;
    for(unit=FirstUnit(1,u_monster); unit; unit=NextUnit())
    {
      for(i=0;i<4;i++)
        if(SizeTo(unit,tp_x[i],tp_y[i])<30)
          if(Action(unit,act_getbehave,0,0)&128)
          {
//            if(Action(unit,act_askitem,V_ARTEFACT1,0))
//              time_artefact=GetTime()+180000;
            Action(unit,act_destroy,0,0);Create(V_TP_EFFECT,tp_x[i],tp_y[i],10);
          } else if(!(Action(unit,act_getbehave,0,0)&64) && !(Action(unit,act_getstate,0,0)&st_passive))
          {
            Action(unit,act_setbehave,64|Action(unit,act_getbehave,0,0),0);
            if(GetUnitVid(unit)==V_M_GUNTANK && !Random(2))
              Action(unit,act_additem,V_AMMO2,0);
            else if(GetUnitVid(unit)==V_M_RL && !Random(2))
              Action(unit,act_additem,V_AMMO2+1,0);
            Action(unit,act_stand,0,0);
            Action(unit,act_damage,Action(unit,act_gethp,0,0)/2+1,0);
          }
      size=SizeTo(unit,1518,1502);
      if(Action(unit,act_getbehave,0,0)&128)//уже леченный
        if( plant_work && (GetTime()-plant_work)>8000 && SizeTo(unit,1518,1482)<20)
        {
          Action(unit,act_setstate,~st_passive&Action(unit,act_getstate,0,0),0);
          plant_work=0;
        } else;
      else
      {
        if(size<300 && !plant_work)
          if(!(Action(plant,act_getbehave,0,0)&128) && Action(plant,act_getaction,0,0)==act_stand)
          {
            AddCommand(plant,act_open,0,0);
            AddCommand(plant,act_stand_open,0,0);
            SetVid(328,VID_PROPERTY,1120);
            Action(plant,act_setbehave,128|Action(plant,act_getbehave,0,0),0);
          }
        if(SizeTo(unit,1518,1482)<20)
          if(!plant_work)
          {
            plant_work=GetTime();plant_close=0;
            Action(unit,act_damage,Action(unit,act_gethp,0,0)-30,0);//cure tank
            Action(unit,act_additem,V_MEDIC,0);
            Action(unit,act_clearcom,0,0);Action(unit,act_stand,0,0);
            Action(unit,act_setstate,st_passive|Action(unit,act_getstate,0,0),0);
            Action(unit,act_setbehave,128|Action(unit,act_getbehave,0,0),0);
            if(/*time_artefact &&*/ (GetTime()-time_artefact)>60000)
              { Action(unit,act_additem,V_ARTEFACT1,0);time_artefact=GetTime(); }
            i=Random(3);
            if(i==0) { AddCommand(unit,act_move,1975,1662);AddCommand(unit,act_move,tp_x[i],tp_y[i]); }
            else if(i==1) { AddCommand(unit,act_move,1235,2432);AddCommand(unit,act_move,655,2367);AddCommand(unit,act_move,tp_x[i],tp_y[i]); }
            else if(i==2) { AddCommand(unit,act_move,1145,1022);AddCommand(unit,act_move,775,687);AddCommand(unit,act_move,tp_x[i],tp_y[i]); }
            else if(i==3) { AddCommand(unit,act_move,2135,697);AddCommand(unit,act_move,tp_x[i],tp_y[i]); }
          }
      }
      if(size<70 && !(Action(unit,act_getbehave,0,0)&128)) fail_tank=1;
    }
    if(plant_work && !plant_close)
      if(fail_tank || SizeTo(Flagman(0),1518,1502)<80) plant_work=GetTime();
      else
      {
        plant_close=1;
        AddCommand(plant,act_close,0,0);SetVid(328,VID_PROPERTY,1088);
      }
  }
  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]);
    }*/
  }
}
