/*#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Doremy (Spell 3)"]
#Text["pink blob girl"]
#System["./data/system/Awalys_System.dnh"]
#Player["./data/player/Sanae by Awalys/player.dnh"]
#Background["./data/background/Boss_BG.dnh"]*/

#include"./data/shot/CrispBright4/Shotconstants.dnh"
#include"./data/lib/lib_main.dnh"
#include"./data/lib/Boss_Anim.dnh"
#include"./data/lib/shot_lib.dnh"

let frame = 0;
let time = 0;
let timer = 0;
let move = 0;
let angleInc = 0;

let count = 0;
let count2 = 0;
let count3 = 0;

let angleT = 0;
let angleT2 = 0;

let patt = 1.002; // Change the pattern. PLease do not set it to 1 or anything lower than that. It will make an impenetrable wall. My favorite patterns are 1.01 and 2.
let diff = 1; // Bullet density. (Multiplier.) 2 is already hard, so setting it so higher numbers is not a good idea. Addiding decimals won't change anything.
let diff2 = 12; // Change the amount of bullets in a single circle. It changes patterns, so for pure difficulty changes use diff. My favorite seetings are 2, 48, 96 and 128.
let inc = 20; // The speed in which the angle increases. Also drastically changes the pattern.
let circ = 60; // How big the circle of bullets around the boss will be at the maximum. ALSO CHANGES THE PATTERN.

// interesting patterns - 1.001, 2.001

let id = "spell3";

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(5500/5);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(999000000);
	}
}

@Initialize{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);

    ObjMove_SetDestAtWeight(objBoss, cx, 100, 15, 6);

	TFinalizeSpell(id);
	StartSlow(TARGET_ALL, 60);
	BossMarker;
	MagicCircle(objBoss);
	BossRender;
	Name("Doremy Sweet");

	Repeat2;

	//MovementLoop(100, 180);
}

@MainLoop{
	SetShotAutoDeleteClip(256, 256, 256, 256);

	ObjEnemy_SetIntersectionCircleToPlayer(objBoss, GetBX, GetBY, 16);
	ObjEnemy_SetIntersectionCircleToShot(objBoss, GetBX, GetBY, 24);

	ObjEnemy_SetDamageRate(objBoss, 1, 1);
	if(start){
		ObjEnemy_SetDamageRate(objBoss, 20, 10);
		count++;
		count2++;
		count3++;
	}
	
	Start;
	Repeat;

	frame++;

	yield;
}

task Charge(frame){
	nTestCharge(GetBX, GetBY, frame, 200, 0, 255);
	Play(ChargeSE, 40);
}

task Start{
	if(frame == 60){
		Spell("Lovecraft \"The Nightmare that Frightened Cthulhu\"", id);
		ObjEnemyBossScene_StartSpell(objScene);
		Cutin;
		wait(60);
		Charge(90);
		Play(ChargeSE, 40);
		Attack = true;
		wait(100);
		EShockwave(GetBX,GetBY, 0.09);
		Play(ExpSE2, 30);
		start=true;
		//Attack=false;
	}
}

task Repeat{ // and here i dont bother to change my old count system of timing patterns because i cant bother to decypher this old spaghetti code
	if(!start){ yield; }
	if(count == 60){
		loop(diff){
			Pattern1;
			Play(ShotSE1, 30);
			Pattern2;
			//diff2++; //this turns on bowap
		}
		count = 60-diff2;
	}
}
task Repeat2{
	loop{
		if(start){
			wait(60*8);
			Play(ShotSE4, 40);
			Knives;
		}
		yield;
	}
}

task Pattern1{
	let ex = ObjMove_GetX(objBoss);
	let ey = ObjMove_GetY(objBoss);
	loop(diff2){
		while(Obj_IsDeleted(objBoss)){yield;}
		let cx = circ*cos(count2)*cos(angleT);
		let cy = circ*cos(count2)*sin(angleT);
		let shot = CreateShotA2(ex+cx, ey+cy, -1, angleT*patt, 0.02, 3.5, BALL_M_BLUE_INV, 20);
		//ObjRender_SetBlendType(shot, BLEND_ADD_ARGB);
		angleT += 360/diff2;
		angleT += inc;
		yield;
	}
}

task Pattern2{
	let ex = ObjMove_GetX(objBoss);
	let ey = ObjMove_GetY(objBoss);
	loop(diff2){
		while(Obj_IsDeleted(objBoss)){yield;}
		let cx = circ*cos(count3)*cos(angleT2);
		let cy = circ*cos(count3)*sin(angleT2);
		let shot = CreateShotA2(ex+cx, ey+cy, -1, angleT2/patt, 0.02, 3.5, BALL_S_BLUE_INV, 20);
		//ObjRender_SetBlendType(shot, BLEND_ADD_ARGB);
		angleT2 += 360/diff2;
		angleT2 += inc;
		yield;
	}
}

task Knives{ 
	let angleT = rand(0,360);

	loop(128){
		while(Obj_IsDeleted(objBoss)){ yield; }
		loop(2){
			let shot = CreateShotA2(GetBX+85*cos(angleT), GetBY+85*sin(angleT), 0, angleT, 0.05, rand(4,5.5), RICE_M_PURPLE, 60);
			ObjMove_SetAngle(shot, GetAngleToPlayer(shot));
			//ObjRender_SetBlendType(shot, BLEND_ADD_ARGB);
			//Commands(shot);
			angleT += 360/2;
		}
		Play(ShotSE1, 30);
		angleT += 4;
		yield;
	}
}
/*task Commands(shot){ // DAMN YOU LOCAA 8 RULES
	wait(60+15);
	ObjMove_SetSpeed(shot, 0); ObjMove_SetAcceleration(shot, 0.3); ObjMove_SetMaxSpeed(shot, rand(4,5.5));
	ObjMove_SetAngle(shot, GetAngleToPlayer(shot));
}*/