#include "hostAPI.txt"

#include "global.txt"

var an;
var lastan;
var fulllife;
var count;
var deflect;
var i;
var ran;
var type;
var tt;
var bx;
var by;
var px;
var py;
var color1;
var color2;
var color3;
var color4;
var color5;
var color6;
var angle;
var cx;
var cy;
var j;
var nowx;
var nowy;
var topx;
var topy;
var highleftx;
var highlefty;
var highrightx;
var highrighty;
var leftx;
var lefty;
var rightx;
var righty;
var lowleftx;
var lowlefty;
var lowrightx;
var lowrighty;
var lowleftx2;
var lowlefty2;
var lowrightx2;
var lowrighty2;
var bigleftx;
var biglefty;
var bigrightx;
var bigrighty;
var deg;
var bosslife;
var count2;
var count3;
var count4;
var count5;
var count6;
func update() {

  ++count;
if(count < -30)
	return;
	
if (count == -30) fulllife=htGetBossLife();
         bosslife=htGetBossLife();
         bx=htGetBossX();
         by=htGetBossY();
if (count % 10 ==0) count6=count6+3;       
  if ((count>30)&&(count % 5 ==0))
{
an=1;
 htInsertBulletVec(5,15,bx+50,by,5,-90);
 htSetBulletDeflectionEx(-5.1/3.1415926/100*360,0,0,2000);
 htSetBulletLifeTime	(90);
  htInsertBulletVec(5,15,bx+50,by,5,90);
 htSetBulletDeflectionEx(5.1/3.1415926/100*360,0,0,2000);
 htSetBulletLifeTime	(90);
 htInsertBulletVec(5,15,bx-50,by,5,90);
 htSetBulletDeflectionEx(-5.1/3.1415926/100*360,0,0,2000);
 htSetBulletLifeTime	(90);
 htInsertBulletVec(5,15,bx-50,by,5,-90);
 htSetBulletDeflectionEx(5.1/3.1415926/100*360,0,0,2000);
 htSetBulletLifeTime	(90);
}

 if ((count>60)&&(count % 5 ==0))
{
htInsertBulletVec(5,15,bx+2.5,by-50,3,-90);
topx=410;
topy=45;
htSetBulletLifeTime((260-topy)/3.01-13);
}	
 
 if ((count>90)&&(count % 30 ==0))
 
{ 
ran=randomfloat(0,360);
for (i=0;i<=6;++i)
 for (j=0;j<=2;++j)
  { htInsertBulletVec(5,8,topx,topy,1.5+0.2*j,360/6*i+count6);
    htSetBulletDeflectionEx(5,0,0,30);
    htInsertBulletVec(5,8,topx,topy,1.5+0.2*j,360/6*i+count6);
    htSetBulletDeflectionEx(-5,0,0,30);
   }

}

 if ((bosslife*1.001/fulllife<=0.8)&&(count>60)&&(count % 5 ==0))
{
an=2;
highleftx=27+175;
highlefty=171;
deg=GetDegreeBetweenTwoPoints(bx+cos(210)*50,by+sin(210)*50,highleftx,highlefty);
htInsertBulletVec(5,15,bx+cos(210)*50,by+sin(210)*50,3,deg);
htSetBulletLifeTime(sqrt((bx+cos(210)*50-highleftx)*(bx+cos(210)*50-highleftx)+(by+sin(210)*50-highlefty)*(by+sin(210)*50-highlefty))/3.01-13);

}	

 if ((count2>30)&&(count % 30 ==0))
 
{ 
ran=randomfloat(0,360);
for (i=0;i<=5;++i)
 for (j=0;j<=2;++j)
  { htInsertBulletVec(5,2,highleftx,highlefty,1.5+0.2*j,360/5*i+count6);
    htSetBulletDeflectionEx(5,0,0,30);
   }

}

 if ((bosslife*1.001/fulllife<=0.8)&&(count>60)&&(count % 5 ==0))
{

highrightx=640-27;
highrighty=171;
deg=GetDegreeBetweenTwoPoints(bx+cos(-30)*50,by+sin(-30)*50,highrightx,highrighty);
htInsertBulletVec(5,15,bx+cos(-30)*50,by+sin(-30)*50,3,deg);
htSetBulletLifeTime(sqrt((bx+cos(-30)*50-highrightx)*(bx+cos(-30)*50-highrightx)+(by+sin(-30)*50-highrighty)*(by+sin(-30)*50-highrighty))/3.01-13);

}	

 if ((count2>30)&&(count % 30 ==0))
 
{ 
ran=randomfloat(0,360);
for (i=0;i<=5;++i)
 for (j=0;j<=2;++j)
  { htInsertBulletVec(5,2,highrightx,highrighty,1.5+0.2*j,360/5*i+count6);
    htSetBulletDeflectionEx(-5,0,0,30);
   }

}

 if ((bosslife*1.001/fulllife<=0.6)&&(count>60)&&(count % 5 ==0))
{
an=3;
leftx=175+130;
lefty=355;
deg=GetDegreeBetweenTwoPoints(bx+cos(150)*50,by+sin(150)*50,leftx,lefty);
htInsertBulletVec(5,15,bx+cos(150)*50,by+sin(150)*50,3,deg);
htSetBulletLifeTime(sqrt((bx+cos(150)*50-leftx)*(bx+cos(150)*50-leftx)+(by+sin(150)*50-lefty)*(by+sin(150)*50-lefty))/3.01-13);

}	

 if ((count3>30)&&(count % 30 ==0))
 
{ 
ran=randomfloat(0,360);
for (i=0;i<=3;++i)
 for (j=0;j<=1;++j)
  { htInsertBulletVec(5,0,leftx,lefty,1.5+0.2*j,360/3*i+count6);
    htSetBulletDeflectionEx(5,0,0,30);
   }

}

 if ((bosslife*1.001/fulllife<=0.6)&&(count>60)&&(count % 5 ==0))
{
rightx=640-130;
righty=355;
deg=GetDegreeBetweenTwoPoints(bx+cos(30)*50,by+sin(30)*50,rightx,righty);
htInsertBulletVec(5,15,bx+cos(30)*50,by+sin(30)*50,3,deg);
htSetBulletLifeTime(sqrt((bx+cos(30)*50-rightx)*(bx+cos(30)*50-rightx)+(by+sin(30)*50-righty)*(by+sin(30)*50-righty))/3.01-13);

}	

 if ((count3>30)&&(count % 30 ==0))
 
{ 
ran=randomfloat(0,360);
for (i=0;i<=3;++i)
 for (j=0;j<=1;++j)
  { htInsertBulletVec(5,0,rightx,righty,1.5+0.2*j,360/3*i+count6);
    htSetBulletDeflectionEx(-5,0,0,30);
   }

}

 if ((bosslife*1.001/fulllife<=0.45)&&(count>90)&&(count % 5 ==0))
{
an=4;
lowleftx=175+117;
lowlefty=474;
deg=GetDegreeBetweenTwoPoints(leftx,lefty,lowleftx,lowlefty);
htInsertBulletVec(5,15,leftx,lefty,3,deg);
htSetBulletLifeTime(sqrt((leftx-lowleftx)*(leftx-lowleftx)+(lefty-lowlefty)*(lefty-lowlefty))/3.01-13);

}	

 if ((count4>30)&&(count % 5 ==0))
{
lowleftx2=175+1;
lowlefty2=524;
deg=GetDegreeBetweenTwoPoints(lowleftx,lowlefty,lowleftx2,lowlefty2);
htInsertBulletVec(5,15,lowleftx,lowlefty,3,deg);
htSetBulletLifeTime(sqrt((lowleftx-lowleftx2)*(lowleftx-lowleftx2)+(lowlefty-lowlefty2)*(lowlefty-lowlefty2))/3.01-13);

}

 if ((count4>50)&&(count % 30 ==0))
 
{ 
ran=randomfloat(0,360);
for (i=0;i<=3;++i)
 for (j=0;j<=1;++j)
  { htInsertBulletVec(5,5,lowleftx2,lowlefty2,1.5+0.2*j,360/3*i+count6);
    htSetBulletDeflectionEx(5,0,0,30);
   }

}

 if ((bosslife*1.001/fulllife<=0.45)&&(count>90)&&(count % 5 ==0))
{
lowrightx=640-117;
lowrighty=474;
deg=GetDegreeBetweenTwoPoints(rightx,righty,lowrightx,lowrighty);
htInsertBulletVec(5,15,rightx,righty,3,deg);
htSetBulletLifeTime(sqrt((rightx-lowrightx)*(rightx-lowrightx)+(righty-lowrighty)*(righty-lowrighty))/3.01-13);

}	

 if ((count4>30)&&(count % 5 ==0))
{
lowrightx2=640-1;
lowrighty2=524;
deg=GetDegreeBetweenTwoPoints(lowrightx,lowrighty,lowrightx2,lowrighty2);
htInsertBulletVec(5,15,lowrightx,lowrighty,3,deg);
htSetBulletLifeTime(sqrt((lowrightx-lowrightx2)*(lowrightx-lowrightx2)+(lowrighty-lowrighty2)*(lowrighty-lowrighty2))/3.01-13);

}


 if ((count4>50)&&(count % 30 ==0))
 
{ 
ran=randomfloat(0,360);
for (i=0;i<=3;++i)
 for (j=0;j<=1;++j)
  { htInsertBulletVec(5,5,lowrightx2,lowrighty2,1.5+0.2*j,360/3*i+count6);
    htSetBulletDeflectionEx(-5,0,0,30);
   }

}

 if ((count5>30)&&(count % 30 ==0))
 
{ 
an=5;
bigleftx=175+53;
biglefty=304;
ran=randomfloat(0,360);
for (i=0;i<=3;++i)
 for (j=0;j<=2;++j)
  { htInsertBulletVec(5,12,bigleftx,biglefty,1.5+0.2*j,360/3*i+count6);
    htSetBulletDeflectionEx(-5,0,0,30);
   }

}

 if ((count5>30)&&(count % 30 ==0))
 
{ 
bigrightx=640-53;
bigrighty=304;
ran=randomfloat(0,360);
for (i=0;i<=3;++i)
 for (j=0;j<=2;++j)
  { htInsertBulletVec(5,12,bigrightx,bigrighty,1.5+0.2*j,360/3*i+count6);
    htSetBulletDeflectionEx(-5,0,0,30);
   }

}

if (an==lastan+1) {
htPlayBossEnergyAnim(20);
lastan=an;
}
if (bosslife*1.001/fulllife<=0.8) ++count2;
if (bosslife*1.001/fulllife<=0.6) ++count3;
if (bosslife*1.001/fulllife<=0.45) ++count4;
if (bosslife*1.001/fulllife<=0.3) ++count5;
}
func init() {

htSet2DBackground("sky.png", 0, 1);
htMoveBossTo(408, 260, 30);
htSetBulletScreenRect(-100,-300,800,850);

an=0;
lastan=0;
	count = -100;
	htPlayBossEnergyAnim(90);
         count2 =0;
count3=0;
count4=0;
count5=0;
count6=0;
	deflect = 0;
	i = 0;
	type = 0;
	color1=0;
        color2=2;
        color3=5;
	color4=8;
	color5=12;
        color6=15; 
	bx = 0;
	by = 0;
}