#東方弾幕風[Player]
#ScriptVersion[2]
#Menu[Emily A]
#Text[Shot: Sacred Antibody
Bomb 1: Terror Arcanum
Bomb 2: Primordial Fear]
#Image[.\emily_select.png]
#ReplayName[EmilyA]

script_player_main
{
	#include_function "lib\SHOT_REPLACE\playershot_replace.dnh"
	#include_function "p_config.txt"
	let img_sakuya = GetCurrentScriptDirectory()~"sakuya.png";
	let img_cutin = GetCurrentScriptDirectory()~"sakuya_select.png";
	let count=-1;
	let optionxpos=30;
	let optionypos=12;
	let hoptionxpos = 15;
	let hoptionypos = -12;
	let HighOptIsSet = true;
	let OptIsSet = true;
	let i=0;
	let mframe = 0;
	let pointcheck = 0;
	let bNextShot = false;
	let objShot = NULL;
	task PHomingLaser(x, y, l, w, v, angle, maxTraverse, shotDamage, shotPenet, graphic, delay) {
	    let obj = Obj_Create(OBJ_SINUATE_LASER);
	    let lifeCounter = 0;
	
	    // initial settings
	    Obj_SetPosition   (obj, x, y);
	    Obj_SetSpeed      (obj, v);
	    Obj_SetAlpha(obj, 128);
	    ObjShot_SetGraphic(obj, graphic);
	    ObjShot_SetDelay  (obj, delay);
		ObjSinuateLaser_SetLength(obj, l);
		ObjSinuateLaser_SetWidth(obj, w);
	    ObjShot_SetDamage(obj, shotDamage);
	    ObjShot_SetPenetration(obj, shotPenet);
	    Obj_SetAutoDelete(obj,true);
	    while(! Obj_BeDeleted(obj)) {
	        Obj_SetAngle(obj, angle);
	        yield;
	
	        // direction angle to player's character
	        let dir = atan2(GetEnemyY - Obj_GetY(obj),
	                        GetEnemyX - Obj_GetX(obj));
	
	        // difference between 'dir' and 'angle'
	        let diff = dir - angle;
	        while(diff >= 180) { diff -= 360; }    // adjust the range
	        while(diff < -180) { diff += 360; }
	
	        let diffAbs = (|diff|);
	        if(diffAbs < maxTraverse) {
	            // if	the difference is small,
	            // the bullet turns to the player's character
	            angle = dir;
	        }
	        else {
	            // otherwise, the bullet turns 'maxTraverse' degrees
	            angle += maxTraverse * diff / diffAbs;
	        }
	        if(GetEnemyNum == 0) {
		ObjShot_FadeDelete(obj);
	        }
	        lifeCounter++;
		if(lifeCounter%30 == 29) {
			maxTraverse += 0.5;
		}
	    }
	}
	task HighOption(position) {
		let objoption=Obj_Create(OBJ_EFFECT);
		Obj_SetAlpha(objoption,200);
		ObjEffect_SetTexture(objoption,img_sakuya);
		ObjEffect_SetRenderState(objoption,ALPHA);
		ObjEffect_SetPrimitiveType(objoption,PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(objoption,4);
		ObjEffect_SetVertexUV(objoption,0,143,4);
		ObjEffect_SetVertexUV(objoption,1,160,4);
		ObjEffect_SetVertexUV(objoption,2,160,19);
		ObjEffect_SetVertexUV(objoption,3,143,19);
		if(position=="LEFT"){
			while(!Obj_BeDeleted(objoption)){
				if(GetCommonDataDefault("power", 128) < 96) {
					Obj_Delete(objoption);
				}
				HighOptIsSet = true;
				ObjEffect_SetVertexXY(objoption,0,GetPlayerX-hoptionxpos-8,GetPlayerY+hoptionypos-7);
				ObjEffect_SetVertexXY(objoption,1,GetPlayerX-hoptionxpos+6,GetPlayerY+hoptionypos-7);
				ObjEffect_SetVertexXY(objoption,2,GetPlayerX-hoptionxpos+6,GetPlayerY+hoptionypos+7);
				ObjEffect_SetVertexXY(objoption,3,GetPlayerX-hoptionxpos-8,GetPlayerY+hoptionypos+7);
				if(GetKeyState(VK_SHOT) == KEY_PUSH || GetKeyState(VK_SHOT) == KEY_HOLD) {
					if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
						if(count%12 == 0) {
							PHomingLaser(GetPlayerX-hoptionxpos-1, GetPlayerY()+hoptionypos, 10, 10, 8, 265, 2.25, 1.75, 2, BLUE60, 0);
						}
					}else{
						if(count%12 == 0) {
							PHomingLaser(GetPlayerX-hoptionxpos-1, GetPlayerY()+hoptionypos, 10, 10, 10, 260, 2.75, 1.75, 2, BLUE60, 0);
						}
					}
				}
				yield;
			}
		}else{
			while(!Obj_BeDeleted(objoption)){
				if(GetCommonDataDefault("power", 128) < 96) {
					Obj_Delete(objoption);
				}
				HighOptIsSet = true;
				ObjEffect_SetVertexXY(objoption,0,GetPlayerX+hoptionxpos-7,GetPlayerY+hoptionypos-7);
				ObjEffect_SetVertexXY(objoption,1,GetPlayerX+hoptionxpos+7,GetPlayerY+hoptionypos-7);
				ObjEffect_SetVertexXY(objoption,2,GetPlayerX+hoptionxpos+7,GetPlayerY+hoptionypos+7);
				ObjEffect_SetVertexXY(objoption,3,GetPlayerX+hoptionxpos-7,GetPlayerY+hoptionypos+7);
				if(GetKeyState(VK_SHOT) == KEY_PUSH || GetKeyState(VK_SHOT) == KEY_HOLD) {
					if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
						if(count%12 == 0) {
							PHomingLaser(GetPlayerX+hoptionxpos, GetPlayerY()+hoptionypos, 10, 10, 8, 275, 2, 2.25, 1.75, BLUE60, 0);
						}
					}else{
						if(count%12 == 0) {
							PHomingLaser(GetPlayerX+hoptionxpos, GetPlayerY()+hoptionypos, 10, 10, 10, 280, 2.75, 1.75, 2, BLUE60, 0);
						}
					}
				}
				yield;
			}
		}
	}
	task Option(position){
		let objoption=Obj_Create(OBJ_EFFECT);
		Obj_SetAlpha(objoption,200);
		ObjEffect_SetTexture(objoption,img_sakuya);
		ObjEffect_SetRenderState(objoption,ALPHA);
		ObjEffect_SetPrimitiveType(objoption,PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(objoption,4);
		ObjEffect_SetVertexUV(objoption,0,143,4);
		ObjEffect_SetVertexUV(objoption,1,160,4);
		ObjEffect_SetVertexUV(objoption,2,160,19);
		ObjEffect_SetVertexUV(objoption,3,143,19);
		if(position=="LEFT"){
			while(!Obj_BeDeleted(objoption)){
				if(GetCommonDataDefault("power", 128) < 16) {
					Obj_Delete(objoption);
				}
				OptIsSet = true;
				ObjEffect_SetVertexXY(objoption,0,GetPlayerX-optionxpos-8,GetPlayerY+optionypos-7);
				ObjEffect_SetVertexXY(objoption,1,GetPlayerX-optionxpos+6,GetPlayerY+optionypos-7);
				ObjEffect_SetVertexXY(objoption,2,GetPlayerX-optionxpos+6,GetPlayerY+optionypos+7);
				ObjEffect_SetVertexXY(objoption,3,GetPlayerX-optionxpos-8,GetPlayerY+optionypos+7);
				if(GetKeyState(VK_SHOT) == KEY_PUSH || GetKeyState(VK_SHOT) == KEY_HOLD) {
					if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
						if(count%12 == 0) {
							PHomingLaser(GetPlayerX-optionxpos - 1, GetPlayerY()+optionypos, 10, 10, 8, 255, 2.25, 2, 2, BLUE60, 0);
						}
					}else{
						if(count%12 == 0) {
							PHomingLaser(GetPlayerX-optionxpos - 1, GetPlayerY()+optionypos, 10, 10, 10, 245, 2.75, 2, 2, BLUE60, 0);
						}
					}
				}
				yield;
			}
		}else{
			while(!Obj_BeDeleted(objoption)){
				if(GetCommonDataDefault("power", 128) < 16) {
					Obj_Delete(objoption);
				}
				OptIsSet = true;
				ObjEffect_SetVertexXY(objoption,0,GetPlayerX+optionxpos-7,GetPlayerY+optionypos-7);
				ObjEffect_SetVertexXY(objoption,1,GetPlayerX+optionxpos+7,GetPlayerY+optionypos-7);
				ObjEffect_SetVertexXY(objoption,2,GetPlayerX+optionxpos+7,GetPlayerY+optionypos+7);
				ObjEffect_SetVertexXY(objoption,3,GetPlayerX+optionxpos-7,GetPlayerY+optionypos+7);
				if(GetKeyState(VK_SHOT) == KEY_PUSH || GetKeyState(VK_SHOT) == KEY_HOLD) {
					if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
						if(count%12 == 0) {
							PHomingLaser(GetPlayerX+optionxpos, GetPlayerY()+optionypos, 10, 10, 8, 285, 2.25, 2, 2, BLUE60, 0);
						}
					}else{
						if(count%12 == 0) {
							PHomingLaser(GetPlayerX+optionxpos, GetPlayerY()+optionypos, 10, 10, 10, 295, 2.75, 2, 2, BLUE60, 0);
						}
					}
				}
				yield;
			}
		}
	}
	task PlayerLaser(x, y, l, w, angle, damage, graphic) {
		let obj = Obj_Create(OBJ_LASER);
		Obj_SetAlpha(obj, 182);
		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		if(GetCommonDataDefault("power", 128) >= 96) { HighOptIsSet = false; }
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDamage(obj, damage);
		ObjShot_SetPenetration(obj, 1);
		ObjLaser_SetLength(obj, l);
		ObjLaser_SetWidth(obj, w);
		ObjLaser_SetSource(obj, false);
		yield;
		Obj_Delete(obj);
	}
	task PowerUp {
		loop {
			yield;
			if(GetPoint > pointcheck && GetCommonDataDefault("power", 128) < 128) {
				SetCommonData("power", GetCommonDataDefault("power", 128) + (GetPoint - pointcheck));
			}
			pointcheck = GetPoint;
		}
	}
	@Initialize
	{	
		LoadGraphic(img_sakuya);
		LoadGraphic(img_cutin);
		SetPlayerLifeImage(img_sakuya, 96, 48, 143, 95);
		if(bombs > 3) { bombs = 3; }
		if(bombs < 0) { bombs = 0; }
		SetInitialBombCount(bombs);
		SetSpeed(3.3, 1.6);
		PowerUp;
		shotinit;
	}
	@MainLoop
	{
		if(!OnMissed) { mframe = 0; }
		if(!HighOptIsSet) {
			HighOption("LEFT");
			HighOption("RIGHT");
		}
		if(!OptIsSet) {
			Option("LEFT");
			Option("RIGHT");
		}
		if(GetCommonDataDefault("power", 128) > 128) {
			SetCommonData("power", 128);
		}
		if(GetCommonDataDefault("power", 128) >= 16) {
			OptIsSet = false;
		}
		if(GetCommonDataDefault("power", 128) >= 96) {
			HighOptIsSet = false;
		}
		if((GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SHOT)==KEY_HOLD) && count==-1){
			count = 0;
		}
		if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
			if(count%6 == 0){
				if(GetCommonDataDefault("power", 128) < 32) {
					ascent(a in 0..2) {
						CreatePlayerShot01(GetPlayerX() + (a%2 * 2 - 1) * 2.5, GetPlayerY()-8, 10, 270, 1.5, 2, BLUE12);
					}
				}
				else if(GetCommonDataDefault("power", 128) < 64) {
					ascent(a in -1..2) {
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a*2.5), 1.25, 2, BLUE12);
					}
				}
				else if(GetCommonDataDefault("power", 128) < 128) {
					ascent(a in -2..2) {
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a*2.5) + 1.25, 1.25, 2, BLUE12);
					}
				}
				else {
					ascent(a in -2..3){
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a*2.5), 1, 2, BLUE12);
						i++;
					}
				}
			}
			if(optionxpos>20){optionxpos--;}
			if(optionypos<20){optionypos++;}
			if(hoptionxpos>10){hoptionxpos--;}
			if(hoptionypos>-20){hoptionypos--;}
		}else{
			if(count%6 == 0){
				if(GetCommonDataDefault("power", 128) < 32) {
					ascent(a in -1..2) {
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270 + a * 3, 1.5, 2, BLUE12);
					}
				}
				else if(GetCommonDataDefault("power", 128) < 64) {
					ascent(a in -2..2) {
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a*5) + 2.5, 1.25, 2, BLUE12);
					}
				}
				else if(GetCommonDataDefault("power", 128) < 128) {
					ascent(a in -2..3){
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270 + a * 5, 0.9, 2, BLUE12);
					}
				}
				else {
					ascent(a in -3..4){
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a * 10 / 3), 0.9, 2, BLUE12);
					}
				}
			}
			if(optionxpos<30){optionxpos++;}
			if(optionypos>10){optionypos--;}
			if(hoptionxpos<20){hoptionxpos++;}
			if(hoptionypos<-10){hoptionypos++;}			
		}		

		if(count >= 0){
			count++;
		}
		if(count >= 13 && (GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD)){
			count=-1;
		}
		if(count >= 18){
			count=-1;
		}
		SetIntersectionCircle(GetPlayerX, GetPlayerY, 0);
		yield;
	}
	@Missed{
		if(mframe == GetRebirthFrame + 1 && !OnBomb) {
			SetCommonData("power", GetCommonDataDefault("power", 128) - 16);
		}
		mframe++;
	}
	@SpellCard{	
		if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
			UseSpellCard("PrimordialFear", true);
			CutIn(KOUMA,"「Primordial Fear」", img_cutin);
		}else{
			UseSpellCard("PrimordialFear", false);
			CutIn(KOUMA,"「Terror Arcanum」", img_cutin);
		}
	}
	@DrawLoop{
		DrawText("Power: "~ToString(GetCommonDataDefault("power", 128)), GetClipMinX, GetClipMaxY - 36, 12, 255);
		SetTexture(img_sakuya);
		if(GetKeyState(VK_LEFT)==KEY_PUSH || GetKeyState(VK_LEFT)==KEY_HOLD){
			SetGraphicRect(49.5, 1.5, 95.5, 47.5);
		}else if(GetKeyState(VK_RIGHT)==KEY_PUSH || GetKeyState(VK_RIGHT)==KEY_HOLD){
			SetGraphicRect(97.5, 1.5, 143.5, 47.5);
		}else{
			SetGraphicRect(1.5, 1.5, 47.5, 47.5);
		}
		DrawGraphic(GetPlayerX(), GetPlayerY());
	}
	@Finalize{
		DeleteGraphic(img_sakuya);
		DeleteGraphic(img_cutin);
	}
}


script_spell PrimordialFear{
	let glowpic = "lib\SHOT_REPLACE\shot_all.png";
	task LemmingDelay(x, y, time) {
		let obj = Obj_Create(OBJ_EFFECT);
		let scale = 64 / 29;
		let fulltime = time;
		Obj_SetPosition(obj, x, y);
		ObjEffect_SetTexture(obj, glowpic);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 466, 302);
		ObjEffect_SetVertexUV(obj, 1, 495, 302);
		ObjEffect_SetVertexUV(obj, 2, 495, 331);
		ObjEffect_SetVertexUV(obj, 3, 466, 331);
		ObjEffect_SetVertexXY(obj, 0, -14.5, -14.5);
		ObjEffect_SetVertexXY(obj, 1, 14.5, -14.5);
		ObjEffect_SetVertexXY(obj, 2, 14.5, 14.5);
		ObjEffect_SetVertexXY(obj, 3, -14.5, 14.5);
		while(time > 0) {
			ObjEffect_SetScale(obj, scale, scale);
			scale -= 2 / fulltime;
			time--;
			yield;
		}
		Obj_Delete(obj);
	}
	task ReHomer(x, y, v, angle, accel, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		let maxTraverse = 5;
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		Obj_SetAutoDelete(obj, false);
		if(GetArgument == true) {
			ObjShot_SetDamage(obj, 0);
		}
		else {
			ObjShot_SetDamage(obj, 1.3);
		}
		ObjShot_SetPenetration(obj, 9999);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, 0);
		yield;
		while(Obj_GetSpeed(obj) > 0) {
			LemmingDelay(Obj_GetX(obj), Obj_GetY(obj), 30);
			DeleteEnemyShotToItemInCircle(SHOT, Obj_GetX(obj), Obj_GetY(obj), 13);
			Obj_SetSpeed(obj, Obj_GetSpeed(obj) - accel);
			yield;
		}
		while(delay > 0) {
			LemmingDelay(Obj_GetX(obj), Obj_GetY(obj), 30);
			DeleteEnemyShotToItemInCircle(SHOT, Obj_GetX(obj), Obj_GetY(obj), 13);
			Obj_SetSpeed(obj, 0);
			delay--;
			yield;
		}
		if(GetArgument == true) {
			Obj_SetAngle(obj, atan2(GetEnemyY - Obj_GetY(obj), GetEnemyX - Obj_GetX(obj)));
		}
		ObjShot_SetDamage(obj, 6.6);
		while(Obj_GetSpeed(obj) < v * 2) {
			LemmingDelay(Obj_GetX(obj), Obj_GetY(obj), 30);
			let distx = GetEnemyX - Obj_GetX(obj);
			let disty = GetEnemyY - Obj_GetY(obj);
			let distpyth = distx^2 + disty^2;
			let dist = distpyth^(1 / 2);
			if(dist > 60 && GetArgument == true) {
				let dir = atan2(GetEnemyY - Obj_GetY(obj), GetEnemyX - Obj_GetX(obj));
				let diff = dir - Obj_GetAngle(obj);
				while(diff > 180) { diff -= 180; }
				while(diff < -180) { diff += 180; }
				let absdiff = (|diff|);
				if(absdiff < 13) {
					Obj_SetAngle(obj, dir);
				}
				else {
					Obj_SetAngle(obj, Obj_GetAngle(obj) + (absdiff / diff) * 10);
				}
			}
			DeleteEnemyShotToItemInCircle(SHOT, Obj_GetX(obj), Obj_GetY(obj), 13);
			Obj_SetSpeed(obj, Obj_GetSpeed(obj) + accel * 2);
			yield;
		}
		while(Obj_BeDeleted(obj) == false) {
			LemmingDelay(Obj_GetX(obj), Obj_GetY(obj), 30);
			DeleteEnemyShotToItemInCircle(SHOT, Obj_GetX(obj), Obj_GetY(obj), 13);
			yield;
		}
	}
	task ReHomerSpiral {
		if(GetArgument == true) {
			descent(a in 0..13) {
				ReHomer(GetPlayerX, GetPlayerY, 4, a * 360 / 13 + 270, 0.05 * 4 / 3, 172, 0);
				loop(10) {
					yield;
				}
			}
		}
		else {
			ascent(a in 0..13) {
				ReHomer(GetPlayerX, GetPlayerY, 3, a * 360 / 13 + 270, 0.05 * 4 / 3, 172, 60);
				loop(10) {
					yield;
				}
			}
		}
	}
	task run{		
		ForbidShot(true);
		loop(45){yield;}
		ReHomerSpiral;
		ForbidShot(false);
		loop(360){yield;}
		End();
	}
	@Initialize{
		LoadGraphic(glowpic);
		SetPlayerInvincibility(330);
		run();
	}
	@MainLoop{
		CollectItems();
		yield;
	}
	@Finalize{
		DeleteGraphic(glowpic);
	}
}