//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Discontinuities"]
#include "./../ZUNShot/ZUNShot_Const.txt"
#include "./sharedfuncJames.dnh"


let objBoss;
let width = 384;
let height = 448;
let life = 6500;
let scene = GetEnemyBossSceneObjectID();
let limit1 = 7*life/10;
let limit2 = 3*life/10;


@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(life);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(100);
    }
    case(EV_REQUEST_SPELL_SCORE){
        SetScriptResult(1000000);
    }
}


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	
	animSpell(objBoss);
	TFinalizeSpellA;
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, 3*height/10, 60);
	Main;
}


@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 28);
    yield;
}


task TFinalizeSpellA{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	StartSlow(TARGET_ALL, 30);
	let a = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK3.WAV", 70);
	loop(4){ObjSound_Play(a); wait(15);}
	StopSlow(TARGET_ALL);
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	if(ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SHOOTDOWN_COUNT)      
      +ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SPELL_COUNT) == 0){
	    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/BONUS3.WAV", 100));
        AddScore(ObjEnemyBossScene_GetInfo(scene, INFO_SPELL_SCORE));
		SetPlayerLife(GetPlayerLife()+1);
    }   
	else if(ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SHOOTDOWN_COUNT)                 
      +ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SPELL_COUNT) > 0){
	      ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK3.WAV", 80));
	      failBonus;
    }
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/DEFEATED.WAV", 100));
	wait(240);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}

task Main{
    Indicator(objBoss);
    takeDamage(objBoss, scene, life);
    let shot1 = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK5.WAV", 80);
    let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK4.WAV", 40);
	let horn = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Horn.WAV", 70);
    superSaiyan(objBoss);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/SPELLCARD.WAV", 100));
	Spell("Discontinuities [r]almost everywhere", scene, objBoss);
    wait(240);
	
	ObjMove_SetDestAtFrame(objBoss, width/4, height/4, 60);
	wait(60);
	let pos = 0;
	
	shotSound(shot2, 28, 40, 1, objBoss, 0);
	loop(28){let obj = CreateShotA1(pos, height/4, 0, 90, 156, 0); pos+= width/27; ObjShot_SetSpellResist(obj, true); yield;}
	wait(30);
	ObjMove_SetDestAtFrame(objBoss, 3*width/4, 5*height/8, 60);
	wait(60);
	pos = width;
	shotSound(shot2, 28, 40, 1, objBoss, 0);
	loop(28){let obj = CreateShotA1(pos, 5*height/8, 0, 90, 156, 0); pos-= width/27; ObjShot_SetSpellResist(obj, true); yield;}
	ObjMove_SetDestAtFrame(objBoss, width/2, height/5, 60);
	wait(90);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/CAUTION.WAV", 50));
	warn;
	let ang = GetAngleToPlayer(objBoss)-30;
	let col = truncate(rand(341, 347));
	shotSound(shot2, 160, 40, 4, objBoss, 0);
	loop(18){ascent(i in 0..40){let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss) -30, 6, ang, col, 4*i); discont(obj, 1);} ang+= 20;}
	ObjSound_Play(shot1);
	let obj1= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj1, true);
	ObjMove_SetDestAtFrame(obj1, width/5, 0, 60);
	let obj2= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj2, true);
	ObjMove_SetDestAtFrame(obj2, 4*width/5, 0, 60);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	wait(80);
	ObjSound_Play(horn);
	wait(60);
	
	move(objBoss, 3*width/10, 7*width/10, height/5, 2*height/5, 180, objBoss, limit1); 
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	shoot(obj1, obj2, 2,3, 200, 4, limit1, 1);
	wait(120);
	moveEdge(obj1, 0, 1, width/5, 4*width/5, 60, objBoss,0 );
	moveEdge(obj2, 180, 1, 4*width/5, width/5, 60, objBoss, 0);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>7*life/8){yield;}
	circles;
	
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit1){yield;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK3.WAV", 80));
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	wait(120);
	ObjMove_SetDestAtFrame(objBoss, 3*width/4, height/4, 30);
	wait(40);
	pos = width;
	shotSound(shot2, 28, 40, 1, objBoss, 0);
	loop(28){let obj = CreateShotA1(pos, height/4, 0, 90, 156, 0); pos-= width/27; yield; ObjShot_SetSpellResist(obj, true);}
	ObjMove_SetDestAtFrame(objBoss, width/4, 5*height/8, 30);
	wait(40);
	pos = 0;
	shotSound(shot2, 28, 40, 1, objBoss, 0);
	loop(28){let obj = CreateShotA1(pos, 3*height/4, 0, 90, 156, 0); pos+= width/27; yield; ObjShot_SetSpellResist(obj, true);}
	ObjSound_Play(shot1);
	let obj3= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj3, true);
	ObjMove_SetDestAtFrame(obj3, rand(width/5, 2*width/5), 0, 60);
	let obj4= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjMove_SetDestAtFrame(obj4, rand(3*width/5, 4* width/5), 0, 60);
	ObjShot_SetSpellResist(obj4, true);
	wait(60);
	ObjSound_Play(horn);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	move(objBoss, 3*width/10, 7*width/10, height/5, 2*height/5, 180, objBoss, 0);
	shoot(obj3, obj4, 1, 1.5, 300, 3, limit2, 2);
	moveEdge(obj3, 0, 1, width/5, 4*width/5, 60, objBoss,0 );
	moveEdge(obj4, 180, 1, 4*width/5, width/5, 60, objBoss, 0);
	
    shotSound(shot2, -1, 30, 20, objBoss, limit2);
    spiral(3*width/4, height/10, 2, rand(0,360), 20, 1, 600, 0, 600, 2, 0,0,0, truncate(rand(363, 371)), objBoss, limit2, true);
	wait(600);
    spiral(width/4, height/10, 2, rand(0,360), 20, -1, 600, 0, 600, 2, 0,0,0, truncate(rand(363, 371)), objBoss, limit2, true);	
	
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit2){yield;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK3.WAV", 80));
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	wait(60);
	ObjMove_SetDestAtFrame(objBoss, width/4, height/10+40, 30);
	wait(30);
	pos = 0;
	ObjSound_Play(shot2);
	loop(28){let obj = CreateShotA1(pos, height/10+40, 0, 90, 156, 0); pos+= width/27; ObjShot_SetSpellResist(obj, true);}
	ObjMove_SetDestAtFrame(objBoss, 3*width/4, 3*height/10+40, 30);
	wait(30);
	pos = width;
	ObjSound_Play(shot2);
	loop(28){let obj = CreateShotA1(pos, 3*height/10+40, 0, 90, 156, 0); pos-= width/27; ObjShot_SetSpellResist(obj, true);}
	ObjMove_SetDestAtFrame(objBoss, width/4, 3*height/5+20, 30);
	wait(30);
	pos = 0;
	ObjSound_Play(shot2);
	loop(28){let obj = CreateShotA1(pos, 3*height/5+20, 0, 90, 156, 0); pos+= width/27; ObjShot_SetSpellResist(obj, true);}
	ObjMove_SetDestAtFrame(objBoss, 3*width/4, 4*height/5+20, 30);
	wait(30);
	pos = width;
	ObjSound_Play(shot2);
	loop(28){let obj = CreateShotA1(pos, 4*height/5+20, 0, 90, 156, 0); pos-= width/27; ObjShot_SetSpellResist(obj, true);}
	ObjSound_Play(shot1);
	let obj5= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj5, true);
	ObjMove_SetDestAtFrame(obj5, 10, 0.4*height + 25, 60);
	let obj6= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjMove_SetDestAtFrame(obj6, width-10, 0.4*height +25, 60);
	ObjShot_SetSpellResist(obj6, true);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	knives(obj5, true); knives(obj6, false);
	move(objBoss, 3*width/10, 7*width/10, height/5, height/5+1, 180, objBoss, 0);
	wait(60);
	ObjSound_Play(horn);
	wait(60);
	kindOfSpiral(objBoss);
	wait(80);
	
	moveEdge(obj5, 0, 2, 10, width-10, 180, objBoss,0 );
	moveEdge(obj6, 180, 2, width-10, 10, 180, objBoss, 0);
}


task kindOfSpiral(obj){
    let shot1 = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK5.WAV", 60);
    let dir = 1; let col;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    col = truncate(rand(363,371));
		if(dir==1){ascent(i in 0..3){ObjSound_Play(shot1); launch(objBoss, 1, 200+ 40*i, 20, 4, 0,0, 4, col, 3, true, 10, 0, 0.03, 0.05, 4, false, objBoss); wait(20);}}
		else{ascent(i in 0..3){ObjSound_Play(shot1); launch(objBoss, 1, 340- 40*i, 20, 4, 0,0, 4, col, 3, true, 10, 0, 0.03, 0.05, 4, false, objBoss); wait(20);}}
		wait(45);
		dir*=-1;
	}
}


task knives(obj, throwLeft){
    let col;
	wait(60);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    col = truncate(rand(324, 332));
	    let del1 = truncate(rand(2, 7));
		let del2= truncate(rand(16, 20));
		ascent(i in 2..20){let obja = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj) +12*i, 0, 180, col, 0);
            discont(obja, 3);		
			yield; 
			if(i == del1 || i == del2){Obj_Delete(obja);}
			else{throw(obja, obj, throwLeft);}
		}
		throwLeft= ! throwLeft;
		wait(362);
	}
}
			

			
task throw(obja, obj, throwLeft){
    if(throwLeft){ObjMove_SetAngle(obja, 180);}
	else{ObjMove_SetAngle(obja, 0);}
	loop(300){ObjMove_SetX(obja, ObjMove_GetX(obj)); yield;}
	ObjMove_SetSpeed(obja, 4);
}

task circles{
    let shot = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK5.WAV", 60);
    let col;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit1){
	    col = truncate(rand(363,371));
		ObjSound_Play(shot);
		ascent(i in 0..3){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=limit1){return;}
		    let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, 250 + 40*i, col, 0);
			ObjRender_SetAlpha(obj, 200);
			control(obj, 1);
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>4*life/5){wait(120);}
		else{wait(60);}
	}
}


task shoot(obj1, obj2, speed1, speed2, period, waitPerBul, limit, phase){
    let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK4.WAV", 35);
	let col1; let col2;
	let t=0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit){
	    col1 = truncate(rand(91, 99));
	    col2 = truncate(rand(99, 105));
		shotSound(shot2, period, 30, waitPerBul, objBoss, limit);
	    while(t<period){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= limit){return;}
			let s1=CreateShotA1(ObjMove_GetX(obj1), ObjMove_GetY(obj1), rand(speed1,speed2), rand(60, 120), col1, 0);
			let s2=CreateShotA1(ObjMove_GetX(obj1), ObjMove_GetY(obj1), rand(speed1,speed2), rand(60, 120), col2, 0);
			let s3=CreateShotA1(ObjMove_GetX(obj2), ObjMove_GetY(obj2), rand(speed1,speed2), rand(60, 120), col1, 0);
			let s4=CreateShotA1(ObjMove_GetX(obj2), ObjMove_GetY(obj2), rand(speed1,speed2), rand(60, 120), col2, 0);
			discont(s1, phase); discont(s2, phase); discont(s3, phase); discont(s4, phase);
			wait(waitPerBul);
			t+= waitPerBul;
		}
		if(phase ==1){wait(60);}
		else{wait(120);}
		t=0;
    }
}


task warn{
    let objText = ObjText_Create();
	ObjText_SetText(objText,"WARNING![r] NO GIRL'S LAND!");
	ObjText_SetFontSize(objText, 40);
	ObjText_SetFontType(objText, "MS Gothic");
	ObjText_SetFontColorTop(objText, 255, 165, 30);
	ObjText_SetFontColorBottom(objText, 255, 165, 30);
	ObjText_SetFontBold(objText, true);
    ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 310);
	Obj_SetRenderPriorityI(objText, 80);
	
	ObjRender_SetPosition(objText, 384/2 -150, 3*height/10, 1);
	let alpha = 95;
	loop(40){ObjRender_SetAlpha(objText, alpha); alpha+=4; yield;}
	loop(40){ObjRender_SetAlpha(objText, alpha); alpha-=4; yield;}
	loop(40){ObjRender_SetAlpha(objText, alpha); alpha+=4; yield;}
	loop(60){ObjRender_SetAlpha(objText, alpha); alpha-=4; yield;}
	
	Obj_Delete(objText);
}
	