#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Shokora Spell 4"]
#Background["./NamelessPath.dnh"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]

#include "../lib/UNL_Shot/lib_ShotConstants.dnh"
#include "./../lib/includes/BossIncludes.dnh"
#include "./../lib/includes/EffectFunctions.dnh"

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_LIFE){ SetScriptResult(1000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(999); }
		case(EV_REQUEST_SPELL_SCORE){ SetScriptResult(10000000); }
		case(EV_GAIN_SPELL){ AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE)); }
}

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 480, 8.5, 25);
	PlayEnemyHitSound(objEnemy, 1);
	
	let Patchouli = GetCurrentScriptDirectory~"sprites/Patchouli.png";
	ObjPrim_SetTexture(objEnemy, Patchouli); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 0, 128*x, 128, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 128, 128*x, 256, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_LEFT, 256, 0, 256+128, 128);
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*2, 256+128, 128*3);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128);}
	descent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 10);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	SetCommonData("pausebeforebonus", true);
	SetAutoDeleteObject(true);
	delayedstart;
	End;
}

task End {
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	SetShotAutoDeleteClip(32, 40, 32, 32);
	BossExplosion(objEnemy, 1, GetOwnScriptID);
}

task delayedstart{
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
	wait(120);
	
	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*59.75);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let shokocut = GetCurrentScriptDirectory~"sprites/shokorasprite.png";
	ObjCutin_SetSpellcardS4("Light Tome &quot;Thani&quot;", shokocut, "NAZRIN", "196", "214", "151");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 1);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(150, 138, 150, 32);
}

task attack1{

	let x = 0;
	let dist = 75;
	let offset = dist/2;
	let switch = 1;
	wait(30);
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 196, 214, 151, BLEND_ADD_ARGB, 25);
	ObjAnim_SetAttackA1(objEnemy, true);
	wait(30);
	PlaySoundEffect("boon", 100, GetEnemyX);
	ascent(i in 1..6){
		ThaniSpawner(ObjMove_GetX(objEnemy)+i*(dist/1.95), 120, -3, 90, 3/45, 8, CIRCLE_L[GRAY], 45, false, 90, rand(-3, 3));
	}
	ascent(i in 1..6){
		ThaniSpawner(ObjMove_GetX(objEnemy)-i*(dist/1.95), 120, -3, 90, 3/45, 8, CIRCLE_L[GRAY], 45, false, 90, rand(-3, 3));
	}
	wait(120);
	ObjAnim_SetAttackA1(objEnemy, false);
	wait(120);
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 196, 214, 151, BLEND_ADD_ARGB, 25);
	wait(30);
	loop{
		ObjAnim_SetAttackA1(objEnemy, true);
		loop(3){
			PlaySoundEffect("boon", 100, GetEnemyX);
			if(switch==1){
				ascent(i in -4..5){
					ThaniSpawner(ObjMove_GetX(objEnemy)+i*dist, 120, -3, 90, 3/45, 8, CIRCLE_L[GRAY], 45, true, 90, rand(-3, 3)-20);
					wait(5);
				}
			}
			else{
				descent(i in -4..5){
					ThaniSpawner(ObjMove_GetX(objEnemy)+i*dist+offset, 120, -3, 90, 3/45, 8, CIRCLE_L[GRAY], 45, true, -90, rand(-3, 3)-20);
					wait(5);
				}
			}
			wait(90);
			switch*=-1;
		}
		ObjAnim_SetAttackA1(objEnemy, false);
		wait(30);
		ExMove(objEnemy, GetCenterX, rand(20, 30), 120+rand(-5, 5), 20, 3, 15);
		wait(75);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 196, 214, 151, BLEND_ADD_ARGB, 25);
		wait(30);
		switch*=-1;
	}	
}

task ThaniSpawner(x, y, v, angle, accel, minmax, graphic, delay, makebullet, shotinitangle, shotheightoffset){
	let obj = CreateMDNHShotA2(x, y, v, angle, accel, minmax, graphic, delay);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 1);
	ObjShot_SetSpellResist(obj, true);
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetY(obj)>GetClipMaxY){
			CreateLaser1(ObjMove_GetX(obj), GetClipMaxY+20, -90, LASER[GRAY], 500, 55, 0, 75, makebullet, shotinitangle, shotheightoffset); 
			Obj_Delete(obj); 
		}
		yield;
	}
}

task CreateLaser1(x, y, angle, graphic, length, width, delay, duration, makebullet, shotinitangle, shotheightoffset){
	let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
	let obj3;
	let bulletgraphic = 0;
	let waitincrement = 0;
	let wide = width;

	ObjMove_SetPosition(obj2, x, y);
	ObjStLaser_SetAngle(obj2, angle);
	ObjShot_SetGraphic(obj2, graphic);
	ObjShot_SetDelay(obj2, delay);
	ObjLaser_SetLength(obj2, length);
	ObjLaser_SetIntersectionWidth(obj2, wide*0.5);
	ObjShot_SetSpellResist(obj2, true);
	ObjShot_SetAutoDelete(obj2, false);
	ObjStLaser_SetSource(obj2, false);
	ObjLaser_SetInvalidLength(obj2, 10, 10);
	ObjShot_Regist(obj2);
	ObjMove_SetPosition(obj2, x, y);
	ObjLaser_SetRenderWidth(obj2, width*0.85);
	ObjRender_SetColor(obj2, 196, 214, 151);
	if(makebullet == true){ PlaySoundEffect("laser", 100, x); }
	else{ PlaySoundEffect("laser", 100, GetCenterX); }
	wait(duration-30);
	if(makebullet==true){
		let ang1 = 15;
		if(shotinitangle == 90){
			ascent(i in 0..15){
				let obj = CreateShotA2(ObjMove_GetX(obj2)+(i*44)*cos(angle), ObjMove_GetY(obj2)+shotheightoffset+(i*44)*sin(angle), 0, shotinitangle-ang1, 0.015, 1.35, RICE[GRAY], 10);
				ObjRender_SetColor(obj, 196, 214, 151);
				obj = CreateShotA2(ObjMove_GetX(obj2)+(i*44)*cos(angle), ObjMove_GetY(obj2)+shotheightoffset+(i*44)*sin(angle), 0, shotinitangle+ang1, 0.0125, 1.35, RICE[GRAY], 10);
				ObjRender_SetColor(obj, 196, 214, 151);
				ang1+=(150*1.06153846154)/15;
				if(i%1==0){ wait(2); }
			}
		}
		else{
			descent(i in 0..15){
				let obj = CreateShotA2(ObjMove_GetX(obj2)+(i*44)*cos(angle), ObjMove_GetY(obj2)+shotheightoffset+(i*44)*sin(angle), 0, shotinitangle-ang1, 0.015, 1.35, RICE[GRAY], 10);
				ObjRender_SetColor(obj, 196, 214, 151);
				obj = CreateShotA2(ObjMove_GetX(obj2)+(i*44)*cos(angle), ObjMove_GetY(obj2)+shotheightoffset+(i*44)*sin(angle), 0, shotinitangle+ang1, 0.0125, 1.35, RICE[GRAY], 10);
				ObjRender_SetColor(obj, 196, 214, 151);
				ang1+=(150*1.06153846154)/15;
				if(i%1==0){ wait(2); }
			}
			
		}
	}
	wait(15);
	while(!Obj_IsDeleted(obj2)){
		wide-=width/15;
		ObjLaser_SetRenderWidth(obj2, wide*0.85);
		ObjLaser_SetIntersectionWidth(obj2, wide*0.5);
		if(wide<=0){	
			Obj_Delete(obj2);
		}
	yield;
	}
}