#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["ISC_Seija"]
#Title["ISC Seija Kijin"]
#Text["Fixed item version[r]Main:Infinitie Jizou Statue[r]Sub:Ying-Yang orb"]
#Image["./img_Cutin.png"]
#ReplayName["SeijaFixed"]

/* 
Nazrin Player for Contest
By: kirbio
*/



let MaximumParticles = 32; // Maximum number of particles allowed
let AllowShotDecay = true; // Renders the shot degeneration effect when true
let AllowGrazePart = true; // Renders a particle when you graze when true
let GrazePartNum = 2; // Number of particles created when you graze a single bullet




/*
The following are common datas that this player uses. You may use
this data to further manipulate your scripts (or player) to your needs.					
	
 --- Common Area Data --- 
	+ Area Common Data that use the area "c_Player_v1"
	GetRenderObject 	- Gets the object that renders the player's
							image + animation
	GetPlayerSpeed		- Returns the player's normal speed in an array
							[Unshift Speed, Shift Speed]
	ForceOptionsOff		- Set to true to hide options and other objects
							that may use this value
							
*/



let TotalParticleCount = 0;
let GlobalSounds = [];
let GlobalSndStr = [];
let ReallyPlaying = [];

let unshiftspd = 3.75;
let shiftspd = 1.75;
let playertype = 1;
let playerdead = false;
let LastDeathTimer = 0;
let BulletString = "c_Player_v1_Medicine_PoisonMarked";
let TimeSpeed = 1;

let shotCount = 1;
let shotSpeed = 15;
let ShotDamage = 0;
let MaskType = 0;

let ActivateHitbox = false;
let RefreshOptions = false;
let ForceOptionsOff = false;
let CanShoot = true;
let CanShift = true;
let OptionShift = GetVirtualKeyState(VK_SLOWMOVE)!=KEY_FREE;
let IsTimeStopped = false;
let IsPlayerSpawning = false;


let BasePlayerObj = GetPlayerObjectID;
let objPlayer = ObjPrim_Create(OBJ_SPRITE_2D);
let current = GetCurrentScriptDirectory;

let imgPlayer = current ~ "/img/img_Player_Seija.png";
let imgBomb = current ~ "/img/img_Bomb.png";
let imgEffect = current ~ "/img/img_Effect.png";

@Initialize{
	LoadPlayerShotData(current ~ "S_shotdata.txt"); 
	CreateCommonDataArea("c_Player_v1");
	SetAreaCommonData("c_Player_v1", "GetRenderObject", objPlayer);
	SetAreaCommonData("c_Player_v1", "GetPlayerSpeed", [unshiftspd, shiftspd]);
	
	ObjPlayer_AddIntersectionCircleA1(BasePlayerObj, 0, 0, 0.05, 20); 
	
	SetPlayerRebirthFrame(15);
	SetPlayerRebirthLossFrame(0);
	SetPlayerDownStateFrame(45);
	SetPlayerSpeed(unshiftspd,shiftspd);
	SetPlayerAutoItemCollectLine(128);
	
	ShotType;
	
	CreateObjectSound("se_charge");
	CreateObjectSound("se_graze");
	CreateObjectSound("se_item");
	CreateObjectSound("se_life");
	CreateObjectSound("se_pldead");
	CreateObjectSound("se_pldead1");
	CreateObjectSound("se_pldead2");
	CreateObjectSound("se_power");
	CreateObjectSound("se_shot");
	CreateObjectSound("se_shot2");
	CreateObjectSound("se_spark");
	CreateObjectSound("se_spell");
	CreateObjectSound("se_bun");
	
	CreateObjectSound("se_spell1");
	CreateObjectSound("se_spell2");
	
	LoadTexture(imgPlayer);
	LoadTexture(imgEffect);
	LoadTexture(imgBomb);
	
	Obj_SetValue(objPlayer, "dinv", -1);
	RenderPlayerImage(objPlayer, 0);
	SetPlayerSpell(2);
	Spawn;
	
	//CreateOptionS1(objPlayer, 32, 12, 10, -12, 270, 1, -1, 1);
	//CreateOptionS1(objPlayer, -32, 12, -10, -12, 270, 1, -1, 1);
		

}


@MainLoop{
	OptionShift = GetVirtualKeyState(VK_SLOWMOVE)!=KEY_FREE;
	if(GetPlayerState==STATE_DOWN){CanShoot = false;}
	if(GetPlayerState==STATE_NORMAL){CanShoot = true;}
	if(GetCommonData("TimeStop",false)==true){IsTimeStopped=true;}
	if(GetCommonData("TimeStop",false)==false){IsTimeStopped=false;}
	if(IsPermitPlayerShot==false && GetPlayerState==STATE_NORMAL){CanShoot = false;}
	if(IsPermitPlayerShot==true && GetPlayerState==STATE_NORMAL){CanShoot = true;}
	if(!IsPlayerSpawning){SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight-18);}
	if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH){RenderHitboxImage(objPlayer, 0, 1);}
	ForceOptionsOff = GetAreaCommonData("ForceOptionsOff", "c_Player_v1", false);
	Obj_SetValue(objPlayer, "dinv", Obj_GetValue(objPlayer, "dinv")-1);
	if(GetPlayerInvincibilityFrame<=0){LastDeathTimer = max(LastDeathTimer-1, 0);}
	yield;
}

@Finalize{	
	RemoveTexture(imgPlayer);
	RemoveTexture(imgEffect);
	RemoveTexture(imgBomb);
	
	RemoveSound("se_charge");
	RemoveSound("se_graze");
	RemoveSound("se_item");
	RemoveSound("se_life");
	RemoveSound("se_pldead");
	RemoveSound("se_pldead1");
	RemoveSound("se_pldead2");
	RemoveSound("se_power");
	RemoveSound("se_shot");
	RemoveSound("se_shot2");
	RemoveSound("se_spark");
	RemoveSound("se_spell");
	RemoveSound("se_bun");
	
	RemoveSound("se_spell1");
	RemoveSound("se_spell2");
}


@Event{
	alternative(GetEventType)
	case(EV_PAUSE_ENTER){StopAllSounds;}
	case(EV_PAUSE_LEAVE){ResumeSounds;}
	case(EV_END_BOSS_STEP){
	}
	case(EV_GAIN_SPELL){
	}
	case(EV_REQUEST_SPELL){
		SetScriptResult(true);
		Obj_Delete(GetSpellManageObject);
	}
	case(EV_PLAYER_SHOOTDOWN){
		CanShoot = false;
		playerdead = true;
	}
	case(EV_HIT){
		loop(16){MapleLeaf(GetPlayerX, GetPlayerY, rand(0,360), rand(85, 185), 2.8);}
		DeathExplosion(0);
		ClearEnemyBullets(GetPlayerX, GetPlayerY, 0, 2, 10, 0);
		PlaySound("se_pldead1");
		SetPlayerInvincibilityFrame(110);
		WaitForPlayerCounterBomb;
	}
	case(EV_PLAYER_REBIRTH){
		LastDeathTimer = 0;
		//SetPlayerRebirthFrame(16);
		//SetPlayerRebirthLossFrame(2.5);
		//SetPlayerSpell(max(2, GetPlayerSpell));
		//ClearEnemyBullets(GetPlayerX, GetPlayerY, 30, 50, 900, 0);
		//SetPlayerInvincibilityFrame(100);
		playerdead = false;
		//Spawn;
	}
	case(EV_GET_ITEM){
		alternative(GetEventArgument(0))
		case(ITEM_1UP){
			PlaySound("se_life");
		}
		case(ITEM_1UP_S){
			PlaySound("se_life");
		}
		case(ITEM_SPELL){
			PlaySound("se_life");
		}
		case(ITEM_SPELL_S){
			PlaySound("se_life");
		}
		others{
			PlaySound("se_item");
		}
	}
	case(EV_GRAZE){
		PlaySound("se_graze");
		loop(GrazePartNum){Particle(GetPlayerX, GetPlayerY, rand(0,360), rand(15, 85), 0.8);}
	}
}

task Spawn{
	let obj = GetPlayerObjectID;
	let y = 0;
	ObjRender_SetX(obj, GetStgFrameWidth/2);
	ObjRender_SetY(obj, GetStgFrameHeight-32);
	SetPlayerSpeed(0,0);
	IsPlayerSpawning = true;
	while(y<82){
		if(IsTimeStopped==false){
			SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight+64);	
			SetPlayerSpeed(0,0);
			ObjRender_SetY(obj, GetStgFrameHeight+32-y);
			y+=2/TimeSpeed;
		}
		yield;
	}
	IsPlayerSpawning = false;
	SetPlayerSpeed(unshiftspd,shiftspd);
}


task RenderPlayerImage(ObjectRender, offset){
	let anim_count = 0;
	let anim_type = 0;
	let Uber = 0;
	let Uberblink = 0;
	
	let x = 32;
	let y = 48;
	let ri = 6;
	let rm = 4;
	let bn = 0;
	
	ObjPrim_SetTexture(ObjectRender, imgPlayer);
	Obj_SetRenderPriority(ObjectRender, 0.45);
	
	while(!Obj_IsDeleted(ObjectRender)){
		ObjRender_SetScaleXYZ(ObjectRender,GetCommonData("PlayerSize",1),GetCommonData("PlayerSize",1),0);
		if(GetPlayerState!=STATE_DOWN){
			ObjRender_SetAlpha(ObjectRender, 255);
		}
		
		if(GetPlayerState!=STATE_NORMAL || playerdead == true){ObjRender_SetAlpha(ObjectRender, 0);}
		ObjRender_SetPosition(ObjectRender, GetPlayerX, GetPlayerY, 0);
		
		if(IsPlayerSpawning==false){
			if(IsTimeStopped == false){
				Blink;
				if(GetVirtualKeyState(VK_RIGHT) == KEY_FREE && GetVirtualKeyState(VK_LEFT) == KEY_FREE){
					if(anim_type != 0){
						bn = 0;
						anim_count = 0;
						anim_type = 0;
					}
				}
				if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
					if(GetVirtualKeyState(VK_RIGHT) != KEY_HOLD && anim_type != 1){
						bn = 0.85;
						anim_count = 0;
						anim_type = 1;
					}
				}
				if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
					if(anim_type != 2){
						bn = 0.85;
						anim_count = 0;
						anim_type = 2;
					}
				}
				anim_count+=(1+bn)/TimeSpeed;
				if(floor(anim_count) != anim_count && TimeSpeed == 1 && bn == 0){
					anim_count = floor(anim_count);
				}
			}
		}else{
			if(IsTimeStopped==false){
				Blink;
				anim_count+=1/TimeSpeed;
				if(anim_type != 0){
					anim_count = 0;
					anim_type = 0;
				}
			}
		}
		//0 - Idle; 1 - Left; 2 - Right;
		if(!IsTimeStopped){
			if(anim_type==0){
				if(round(anim_count) >= 0){ObjSprite2D_SetSourceRect(ObjectRender, 0, 0+offset, x, y+offset);}
				if(round(anim_count) >= ri){ObjSprite2D_SetSourceRect(ObjectRender, x, 0+offset, x*2, y+offset);}
				if(round(anim_count) >= ri*2){ObjSprite2D_SetSourceRect(ObjectRender, x*2, 0+offset, x*3, y+offset);}
				if(round(anim_count) >= ri*3){ObjSprite2D_SetSourceRect(ObjectRender, x*3, 0+offset, x*4, y+offset);}
				if(round(anim_count) >= ri*4){ObjSprite2D_SetSourceRect(ObjectRender, x*4, 0+offset, x*5, y+offset);}
				if(round(anim_count) >= ri*5){ObjSprite2D_SetSourceRect(ObjectRender, x*5, 0+offset, x*6, y+offset);}
				if(round(anim_count) >= ri*6){ObjSprite2D_SetSourceRect(ObjectRender, x*6, 0+offset, x*7, y+offset);}
				if(round(anim_count) >= ri*7){ObjSprite2D_SetSourceRect(ObjectRender, x*7, 0+offset, x*8, y+offset);}
				if(round(anim_count) >= ri*8-1){anim_count = ri-1;}
			}
			if(anim_type==1){
				if(round(anim_count) >= 0){ObjSprite2D_SetSourceRect(ObjectRender, 0, y+offset, x, y*2+offset);}
				if(round(anim_count) >= rm){ObjSprite2D_SetSourceRect(ObjectRender, x, y+offset, x*2, y*2+offset);}
				if(round(anim_count) >= rm*2){ObjSprite2D_SetSourceRect(ObjectRender, x*2, y+offset, x*3, y*2+offset);}
				if(round(anim_count) >= rm*3){ObjSprite2D_SetSourceRect(ObjectRender, x*3, y+offset, x*4, y*2+offset);}
				if(round(anim_count) >= rm*4){ObjSprite2D_SetSourceRect(ObjectRender, x*4, y+offset, x*5, y*2+offset);bn = 0}
				if(round(anim_count) >= rm*5){ObjSprite2D_SetSourceRect(ObjectRender, x*5, y+offset, x*6, y*2+offset);}
				if(round(anim_count) >= rm*6){ObjSprite2D_SetSourceRect(ObjectRender, x*6, y+offset, x*7, y*2+offset);}
				if(round(anim_count) >= rm*7){ObjSprite2D_SetSourceRect(ObjectRender, x*7, y+offset, x*8, y*2+offset);}
				if(round(anim_count) >= rm*8-1){anim_count = rm*4-1;}
			}
			if(anim_type==2){
				if(round(anim_count) >= 0){ObjSprite2D_SetSourceRect(ObjectRender, 0, y*2+offset, x, y*3+offset);}
				if(round(anim_count) >= rm){ObjSprite2D_SetSourceRect(ObjectRender, x, y*2+offset, x*2, y*3+offset);}
				if(round(anim_count) >= rm*2){ObjSprite2D_SetSourceRect(ObjectRender, x*2, y*2+offset, x*3, y*3+offset);}
				if(round(anim_count) >= rm*3){ObjSprite2D_SetSourceRect(ObjectRender, x*3, y*2+offset, x*4, y*3+offset);}
				if(round(anim_count) >= rm*4){ObjSprite2D_SetSourceRect(ObjectRender, x*4, y*2+offset, x*5, y*3+offset);bn = 0}
				if(round(anim_count) >= rm*5){ObjSprite2D_SetSourceRect(ObjectRender, x*5, y*2+offset, x*6, y*3+offset);}
				if(round(anim_count) >= rm*6){ObjSprite2D_SetSourceRect(ObjectRender, x*6, y*2+offset, x*7, y*3+offset);}
				if(round(anim_count) >= rm*7){ObjSprite2D_SetSourceRect(ObjectRender, x*7, y*2+offset, x*8, y*3+offset);}
				if(round(anim_count) >= rm*8-1){anim_count = rm*4-1;}
			}
		}
		ObjSprite2D_SetDestCenter(ObjectRender);
		yield;
	}
	task Blink{
		if(GetPlayerInvincibilityFrame>0){
			if(Uber==0){ObjRender_SetColor(ObjectRender, 255, 255, 255);}
			if(Uber!=0){ObjRender_SetColor(ObjectRender, 32, 32, 255);}
			Uberblink+=1;
			if(Uberblink>1){Uberblink=0;Uber++;if(Uber>1){Uber=0}}
		}else{
			ObjRender_SetColor(ObjectRender, 255, 255, 255);
		}
	}
}

task RenderHitboxImage(plobj, offx, hitboxnum){
	yield;yield;yield;
	while(IsPlayerSpawning||playerdead){
		if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){return;}
		yield;
	}
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.39);
	ObjRender_SetAlpha(obj, 235);
	ObjPrim_SetTexture(obj, imgEffect);
	ObjSprite2D_SetSourceRect(obj, 64, 224, 127, 287);
	ObjSprite2D_SetDestCenter(obj);
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.69);
	ObjRender_SetAlpha(obj2, 235);
	ObjPrim_SetTexture(obj2, imgEffect);
	ObjSprite2D_SetSourceRect(obj2, 64, 224, 127, 287);
	ObjSprite2D_SetDestCenter(obj2);
	
	let ObjTime = 0;
	
	let AlphaSet = 0;
	let ObjAlpha = 0;
	let ObjScale = 1;
	PositionSet;
	
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD&&ObjScale<1.4&&!playerdead){
		if(!IsTimeStopped){
			ObjTime+=8;
			ObjScale += 0.125;
			PositionSet;
		}
		yield;
	}
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD&&ObjScale>1&&!playerdead){
		if(!IsTimeStopped){
			ObjTime+=8;
			ObjScale -= 0.1;
			PositionSet;
		}
		yield;
	}
	ObjScale = 1;
	yield;
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD&&!playerdead){
		if(!IsTimeStopped){
			ObjTime+=3;
			PositionSet;
		}
		yield;
	}
	
	while(ObjScale>0){
		if(!IsTimeStopped){
			ObjTime+=3;
			ObjScale -= 0.1;
			PositionSet;
		}
		yield;
	}

	Obj_Delete(obj);
	Obj_Delete(obj2);
	
	task PositionSet{
		if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH){ObjScale = 0;}
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetAngleZ(obj, ObjTime);
		ObjRender_SetPosition(obj2, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetScaleXYZ(obj, ObjScale*GetCommonData("PlayerSize",1), ObjScale*GetCommonData("PlayerSize",1), ObjScale);
		ObjRender_SetScaleXYZ(obj2, ObjScale*3/4*GetCommonData("PlayerSize",1), ObjScale*3/4*GetCommonData("PlayerSize",1), ObjScale);
	}
	
}


task DeathExplosion(offx){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.60);
	ObjPrim_SetTexture(obj, imgEffect);
	ObjSprite2D_SetSourceRect(obj, 198, 232, 262, 296);
	ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
	let alpha = 175;
	let scale = 0.8;
	let expo = 0.7;
	ObjRender_SetPosition(obj, GetPlayerX+offx, GetPlayerY, 0);
	while(alpha>0){
		scale += expo/TimeSpeed;
		alpha -= 7/TimeSpeed;
		if(expo>0){expo -=0.0085/TimeSpeed;}
		ObjRender_SetAlpha(obj, alpha);
		ObjRender_SetScaleXYZ(obj, scale/3, scale/3, 1);
		yield;
	}
	yield;Obj_Delete(obj);
}

task ClearEnemyBullets(x, y, Tsize, Tspd, Tmaxrad, delay){
	let barray = GetShotIdInCircleA2(x, y, Tsize, TARGET_ENEMY);
	let stopclear = true;
	loop(delay){yield;}
	while(Tsize<Tmaxrad){
		barray = GetShotIdInCircleA2(x, y, Tsize, TARGET_ENEMY);
		ascent(i in 0..length(barray)){
			let o = barray[i];
			if(!ObjShot_IsSpellResist(o)){ObjShot_SetDeleteFrame(o, 0);}
		}
		Tsize += Tspd;
		yield;
	}
}

task ShadowA1(dumobj, incremental, alpha, colorR, colorG, colorB){
	SubtractBG(dumobj);
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let xBend = 1;
	let BendInc = 0.22;
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.30);
	ObjRender_SetAlpha(obj, alpha);
	ObjPrim_SetTexture(obj, imgEffect);
	ObjSprite2D_SetSourceRect(obj, 0, 234, 62, 296);
	ObjSprite2D_SetDestRect(obj, -xBend, -xBend, xBend, xBend);
	ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
	ObjRender_SetColor(obj, colorR, colorG, colorB);
	while(!Obj_IsDeleted(dumobj)){
		ObjSprite2D_SetDestRect(obj, -xBend, -xBend, xBend, xBend);
		if(xBend<1500){
			xBend+=BendInc;
			BendInc+=incremental/TimeSpeed;
		}
		yield;
	}
	while(alpha>0){
		alpha -= 3/TimeSpeed;
		ObjRender_SetAlpha(obj, alpha);
		yield;
	}
	Obj_Delete(obj);
}

task SubtractBG(dumobj){
	loop(32){yield}
	let bg = ObjPrim_Create(OBJ_SPRITE_2D);
	let fade = 0;
	ObjRender_SetBlendType(bg, BLEND_SUBTRACT);
	Obj_SetRenderPriority(bg, 0.28);
	ObjPrim_SetTexture(bg, NULL);
	ObjSprite2D_SetSourceRect(bg, 0, 0, 1, 1);
	ObjSprite2D_SetDestRect(bg, -48, -48, GetStgFrameWidth+48, GetStgFrameHeight+48);
	ObjRender_SetColor(bg, fade, fade, fade);
	
	let incv = 0;
	let shift = 0;
	
	while(!Obj_IsDeleted(dumobj)){
		fade = min(1, fade+0.03);
		UpCol;
		yield;
	}
	while(fade>0){
		fade-=0.05;
		UpCol;
		yield;
	}
	Obj_Delete(bg);
	function UpCol{
		incv++;
		shift = 16*cos(incv);
		ObjRender_SetColor(bg, (48+shift)*fade, (64*fade), (48-shift)*fade);
	}
}

task ShadowA2(x, y, alpha, alphinc, colorR, colorG, colorB){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let ShadowTime = 0;
	let xBend = 1250;
	let BendInc = 0.22;
	let ShadSet = 0;
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.69);
	ObjRender_SetAlpha(obj, alpha);
	ObjPrim_SetTexture(obj, imgEffect);
	ObjSprite2D_SetSourceRect(obj, 0, 234, 62, 296);
	ObjSprite2D_SetDestRect(obj, -xBend, -xBend, xBend, xBend);
	ObjRender_SetPosition(obj, x, y, 0);
	ObjRender_SetColor(obj, colorR, colorG, colorB);
	while(alpha>0){
		alpha += alphinc;
		ObjRender_SetAlpha(obj, alpha);
		yield;
	}
	Obj_Delete(obj);
}



task ShotType{
	let loopCount = 0;
	loop{
		FireShotData;
		PlayerShoot;
		yield;
	}
	
	task FireShotData{
		if(GetPlayerState == STATE_NORMAL&&!IsPlayerSpawning){			
			if(GetVirtualKeyState(VK_SHOT)!=KEY_FREE||GetVirtualKeyState(VK_SHOT)==KEY_PUSH){
				loopCount = 1;
			}
		}
		
		
		if(loopCount>0){
			if(!IsTimeStopped){
				loopCount++;
				shotCount-=1;
			}
			if(loopCount>10||IsPlayerSpawning){
				loopCount = 0;
				shotCount = 1;
			}
		}
	}
	task PlayerShoot{
		if(shotCount <= 0 && CanShoot){
			PlaySound("se_shot");
			CreatePlayerObjShotA1(GetPlayerX-9, GetPlayerY-16, shotSpeed, 270, 6, 0.25, 2, false);
			CreatePlayerObjShotA1(GetPlayerX+11, GetPlayerY-16, shotSpeed, 270, 6, 0.25, 2, false);
			if(OptionShift){
				CreatePlayerObjShotA1(GetPlayerX-18, GetPlayerY+8, shotSpeed, 270-5, 6, 0.25, 2, false);
				CreatePlayerObjShotA1(GetPlayerX+20, GetPlayerY+8, shotSpeed, 270+5, 6, 0.25, 2, false);
				}else{
				CreatePlayerObjShotA1(GetPlayerX-18, GetPlayerY+8, shotSpeed, 270-10, 6, 0.25, 2, false);
				CreatePlayerObjShotA1(GetPlayerX+20, GetPlayerY+8, shotSpeed, 270+10, 6, 0.25, 2, false);
				}
			yield;
			if(GetCommonData("TranceMode",false)==false){shotCount = 4; shotSpeed = 15;}
			if(GetCommonData("TranceMode",false)==true){shotCount = 2; shotSpeed = 30;}
		}
	}
}

task CreatePlayerObjShotA1(ShotX, ShotY, Shotspd, Shotangle, Shotdmg, Shotpenetration, Shotgraphic, sdamage){
	let ShotObj = CreatePlayerShotA1(ShotX, ShotY, Shotspd, Shotangle, Shotdmg, Shotpenetration, Shotgraphic);
	ShotSpeed(ShotObj);
	let ShotSpell = ID_INVALID;
	if(sdamage){
		ShotSpell = CreatePlayerShotA1(ShotX, ShotY, Shotspd, Shotangle, Shotdmg, Shotpenetration, Shotgraphic);
		ObjShot_SetDamage(ShotSpell, sdamage);
	}
	let X = 0;
	let Y = 0;
	let A = 0;
	let ramp = [0, 0.3, 0.011];
	if(Shotangle == 270){ramp = [0, 0.45, 0.0145];}
	while(Obj_IsDeleted(ShotObj)==false){
		X = ObjRender_GetX(ShotObj);
		Y = ObjRender_GetY(ShotObj);
		A = ObjMove_GetAngle(ShotObj);
		ObjShot_SetDamage(ShotObj, Shotdmg+Shotdmg*ramp[0]);
		ramp[0] = min(ramp[0]+ramp[2], ramp[1]);
		yield;
	}
	if(X>1 && Y >1){ShotHit(X, Y-8, A, Shotgraphic);loop(2){Particle(X, Y, A+rand(-70,70), 105, rand(0.25, 0.55))}}
}

task ShotSpeed(obj)
{
	while(!Obj_IsDeleted(obj))
	{
		if(IsTimeStopped==true){ObjMove_SetSpeed(obj,0); ObjShot_SetIntersectionEnable(obj,false);}
		if(IsTimeStopped==false){ObjMove_SetSpeed(obj,shotSpeed); ObjShot_SetIntersectionEnable(obj,true);}
		yield;
	}
}


task CreateOptionS1(robj, X1, Y1, X2, Y2, angle, type, s, dmg){
	if(RefreshOptions){return;}
	let x = type;
	let y = 9;
	let oripla = playertype;
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D); 
	ObjPrim_SetTexture(obj, imgEffect);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.4);
	ObjSpriteList2D_SetSourceRect(obj,2, 77, 19, 94);
	ObjSpriteList2D_SetDestCenter(obj);
	let rx = GetPlayerX;
	let ry = GetPlayerY;
	
	let alp = 0;
	let scaleset = 0;
	let scalespd = 1;
	let movea = 0;
	let c = 0;
	while(oripla == playertype && !Obj_IsDeleted(robj) && !RefreshOptions){
		if(IsTimeStopped == false){
			if(IsPlayerSpawning==true || ForceOptionsOff == true){
				rx = GetPlayerX;
				ry = GetPlayerY;
				alp = 245;
			}else{
				if(playerdead == true){
					alp = min(alp+20, 245);
				}else{
					alp = 0;
				}
			}
			posit;
			rend;
			c+=10;
			ObjRender_SetAngleXYZ(obj,0,0,c);
			if(scaleset==0){scalespd+=0.03/TimeSpeed}
			if(scaleset==1){scalespd-=0.03/TimeSpeed}
			if(scalespd<0.8){scaleset=0}
			if(scalespd>1.3){scaleset=1}
			
			if(shotCount <= 0 && CanShoot == true){
				if(OptionShift){
				CreatePlayerObjShotA1(ObjRender_GetX(obj), ObjRender_GetY(obj), 15, 270, 2*dmg, 0.6, 3, 0.5);
				//CreatePlayerObjShotA1(ObjRender_GetX(obj), ObjRender_GetY(obj), 15, 270, 2*dmg, 0.6, 3, 0.25);
				}else{
				CreatePlayerObjShotA1(ObjRender_GetX(obj), ObjRender_GetY(obj), 15, 270+10, 2*dmg, 0.6, 3, 0.5);
				CreatePlayerObjShotA1(ObjRender_GetX(obj), ObjRender_GetY(obj), 15, 270-10, 2*dmg, 0.6, 3, 0.5);
				}
				
			}
		}
		yield;
	}
	X1 = 0;
	Y1 = 0;
	X2 = 0;
	Y2 = 0;
	Obj_SetRenderPriority(obj, 0.39);
	let alpha = 0;
	while(alpha<255){
		if(IsTimeStopped==false){
			alp = alp+15;
			rend;
			posit;
			ObjRender_SetAlpha(obj, alp);
		}
		yield;
	}
	Obj_Delete(obj);
	
	task rend{
		ObjSpriteList2D_ClearVertexCount(obj);
		
		//ObjRender_SetAlpha(obj, 122.5-alp/2);
		//ObjRender_SetScaleXYZ(obj, 1.75, 1.75, 1);
		//ObjRender_SetAngleZ(obj, movea*s-25);
		//ObjSpriteList2D_AddVertex(obj);
		
		//ObjRender_SetAlpha(obj, 245-alp);
		ObjRender_SetScaleXYZ(obj, 1.2, 1.2, 1.75);
		//ObjRender_SetAngleZ(obj, -movea*s-25);
		ObjSpriteList2D_AddVertex(obj);
	}

	task posit{
		ObjRender_SetPosition(obj, rx, ry-10, 0);
		movea+=6;
		let OptionShift = GetVirtualKeyState(VK_SLOWMOVE)!=KEY_FREE;
		if(OptionShift){
			let dist = GetDistance(rx, ry, (GetPlayerX+X2), (GetPlayerY+Y2));
			let oang = atan2((GetPlayerY+Y2)-ry, (GetPlayerX+X2)-rx);
			if(dist>0.8){
				rx = rx+((dist/4+0.75))*cos(oang);
				ry = ry+((dist/4+0.75))*sin(oang);
			}
		}else{
			let dist = GetDistance(rx, ry, (GetPlayerX+X1), (GetPlayerY+Y1));
			let oang = atan2((GetPlayerY+Y1)-ry, (GetPlayerX+X1)-rx);
			if(dist>0.55){
				rx = rx+((dist/2+0.5))*cos(oang);
				ry = ry+((dist/2+0.5))*sin(oang);
			}
		}
	}
}

task ShotHit(EffectX, EffectY, EffectA, type){
	if(!AllowShotDecay){return;}
	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	let obj3_time = 0;
	ObjRender_SetBlendType(obj3, BLEND_ALPHA);
	ObjRender_SetAlpha(obj3, 200);
	Obj_SetRenderPriority(obj3, 0.59);
	ObjPrim_SetTexture(obj3, imgEffect);
	ObjSprite2D_SetSourceRect(obj3, 66, 21, 79, 33);
	ObjSprite2D_SetDestCenter(obj3);
	ObjRender_SetAngleZ(obj3, EffectA+90);
	ObjRender_SetX(obj3, EffectX);
	ObjRender_SetY(obj3, EffectY);
	let a = 145;
	while(!Obj_IsDeleted(obj3)){
		obj3_time++;
		if(obj3_time==3){
			ObjSprite2D_SetSourceRect(obj3, 66, 10, 79, 21);
		}
		if(obj3_time==6){
			ObjSprite2D_SetSourceRect(obj3, 6, 0, 79, 11);
		}
		if(obj3_time==9){Obj_Delete(obj3);}
		ObjRender_SetAlpha(obj3, a-obj3_time*8);
		ObjRender_SetScaleXYZ(obj3, 1+obj3_time/8, 1+obj3_time/8, 1);
		ObjRender_SetPosition(obj3, ObjRender_GetX(obj3)+6*cos(EffectA), ObjRender_GetY(obj3)+6*sin(EffectA), 0);
		
		yield;
	}
}

task WaitForPlayerCounterBomb{
	loop(GetPlayerRebirthFrame-1){yield;}
	SetPlayerRebirthFrame(15);
	CallSpell;
	loop(95){yield;}
	PlaySound("se_bun");
}

task Radiance{
		let renderTexture = GetReservedRenderTargetName(0);
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, renderTexture);
		ObjPrim_SetPrimitiveType(obj,PRIMITIVE_TRIANGLELIST);
	   
		// ObjRender_SetPosition(obj, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
		Obj_SetRenderPriorityI(obj, 82);
		SetInvalidRenderPriorityA1(20, 81);
	   
		RenderToTextureA1(renderTexture, 20, 81, true);
	   
		let num = 40;
		let o = 32;
		let width = GetStgFrameWidth+64;
//		let height = GetStgFrameHeight+64;
		let height = (12288)^(1/2);
	   
		ObjPrim_SetVertexCount(obj,3*num);
		ClearInvalidRenderPriority;
	   
		ascent(j in 0..5) {
				ascent(i in -1..3) {
						ManageFragmentA1(i, j, -0.5);
						
						ManageFragmentA1(i, j, 0.5);
					   
				}
		}
		
		
		task ManageFragmentA1(i, j, cy){
			let x = 40+128*i+64;
			let y = 16+height*j+height+(height*cy);
			let vert = 0;
			let angle = 0;
			let rotangle = 0;
			let inang = 0;
			x+=64;
			y-=64;
			if(cy == -0.5){
				vert = 6+6*i+24*j;
				inang = 270;
				ascent(z in 0..3){
					let vangle = inang+360/3*z;
					SetVertexProp(obj,vert+z,x+76*cos(vangle),y+76*sin(vangle),0);
				}
			}else{
				x-=64;
				y-=36;
				vert = 9+6*i+24*j;
				inang = 90;
				ascent(z in 0..3){
					let vangle = inang+360/3*z;
					SetVertexProp(obj,vert+z,x+76*cos(vangle),y+76*sin(vangle),0);
				}
			}
			
			let dist = absolute(x-width/2)/(width/2);
			let mod = 1;
			if(x<width/2){mod = -1;}
			/*xinit, yinit,
			xmax, ymax,
			xvel, yvel,
			angleinc, rotinc
			*/
			let data = [
			dist*4*mod, -4+8*dist, 
			rand(2, 4)*mod, rand(6.5, 10.5), 
			rand(0.07, 0.17)*mod, rand(0.1, 0.2),
			rand(-5, 5), rand(-4, 4)
			];
			let data_angle = [round(rand(-6, 6)),round(rand(-6, 6)),round(rand(-6, 6))];
			
			while(y<GetStgFrameHeight+64 && x>-128 && x<GetStgFrameWidth+128){
				x+=data[0];
				y+=data[1];
				angle+=data[6];
				rotangle+=data[7];
				if(mod == 1){
					data[0] = min(data[0] + data[4], data[2]);
				}else{data[0] = max(data[0] + data[4], data[2]);}
				data[1] = min(data[1] + data[5], data[3]);
				SetThreeVertexPositions(vert, x, y);
				
				
				yield;
			}	
			function SetThreeVertexPositions(id, x, y){
				ascent(z in 0..3){
					let vangle = rotangle+inang+360/3*z;
					ObjPrim_SetVertexPosition(obj,id+z,
						x+(76*cos(angle/data_angle[z]))*cos(vangle),
						y+(76*cos(angle/data_angle[z]))*sin(vangle),0);
				}
			}		
		}
		
		
		task SetVertexProp(obj, id, x, y,z){
			ObjPrim_SetVertexUVT(obj,id,x,y);
			ObjPrim_SetVertexPosition(obj,id,x,y,0);
		}
}


task CreateCircleA1(x, y, destsiz, sizinc){
	if(sizinc>3){loop(sizinc*1.5){MapleLeaf(GetPlayerX, GetPlayerY, rand(0,360), rand(85, 185), 2.8);}}
	let alpha = 255;
	let countVertex = 50;
	
	let Hitbox = ObjSpell_Create;
	ObjSpell_Regist(Hitbox);
	
	let circ = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetPrimitiveType(circ, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetTexture(circ, NULL);
	Obj_SetRenderPriority(circ, 0.60);
	ObjPrim_SetVertexCount(circ, countVertex);
	ObjRender_SetBlendType(circ, BLEND_ALPHA);
	
	
	ObjPrim_SetVertexUVT(circ, 0, 0, 0);	
	ObjPrim_SetVertexPosition(circ, 0, 0, 0, 0);
	ObjPrim_SetVertexColor(circ, 0, 255, 255, 255);
	ObjPrim_SetVertexAlpha(circ, 0, 0);
	
	ObjRender_SetPosition(circ, x, y, 0);
	
	let siz = 0;
	
	ascent(iVert in 1..countVertex){
		let angle = (360 / (countVertex - 2) * iVert);
		ObjPrim_SetVertexUVT(circ, iVert, 0, 0);	
		let x = siz*cos(angle);
		let y = siz*sin(angle);
		ObjPrim_SetVertexPosition(circ, iVert , x, y, 0);
		ObjPrim_SetVertexColor(circ, iVert, 212, 212, 48);
		ObjPrim_SetVertexAlpha(circ, iVert, alpha);
	}
	while(siz<destsiz){
		siz += sizinc;
		alpha = max(alpha-4, 0);
		ascent(iVert in 1..countVertex){
			let angle = (360 / (countVertex - 2) * iVert);
			ObjPrim_SetVertexPosition(circ, iVert , (siz+8)*cos(angle), (siz+8)*sin(angle), 0);
			ObjPrim_SetVertexAlpha(circ, iVert, alpha);
		}
		ObjSpell_SetIntersectionCircle(Hitbox, ObjRender_GetX(circ), ObjRender_GetY(circ), siz);
		ObjSpell_SetDamage(Hitbox, sizinc);
		yield;
	}		
	while(alpha>0){
		siz += sizinc;
		alpha = max(alpha-12, 0);
		ascent(iVert in 1..countVertex){
			let angle = (360 / (countVertex - 2) * iVert);
			ObjPrim_SetVertexPosition(circ, iVert , (siz+8)*cos(angle), (siz+8)*sin(angle), 0);
			ObjPrim_SetVertexAlpha(circ, iVert, alpha);
		}
		ObjSpell_SetIntersectionCircle(Hitbox, ObjRender_GetX(circ), ObjRender_GetY(circ), siz);
		ObjSpell_SetDamage(Hitbox, sizinc*0.75);
		yield;
	}	
	Obj_Delete(circ);
	Obj_Delete(Hitbox);
}



task MarkBulletForDeath(ID){
	if(ObjShot_IsSpellResist(ID)){return;}
	ObjMove_SetSpeed(ID, ObjMove_GetSpeed(ID)/4);
	ascent(i in 0..60){
		ObjShot_SetIntersectionEnable(ID, false);
		let col = 255-192/60*i;
		let scl = 1-0.5/60*i;
		ObjRender_SetColor(ID, col, col, col);
		ObjRender_SetScaleXYZ(ID, scl, scl, 1);
		ObjRender_SetAlpha(ID, 255-255/60*i);
		yield;
	}
	Obj_Delete(ID);
}

task MarkFairiesForDeath(ID, damage){
	ascent(i in 0..35){
		let col = 255-255/35*i;
		ObjRender_SetColor(ID, col, 255, col);
		yield;
	}
	ObjEnemy_AddLife(ID, -damage);
	loop(10){yield;}
	if(Obj_IsDeleted(ID)){return;}
	descent(i in 0..15){
		let col = 255-192/15*i;
		ObjRender_SetColor(ID, col, 255, col);
		yield;
	}
}



task ScreenShakeA1(shaketime, intensity){
	let x = Get2DCameraX;
	let y = Get2DCameraY;
	while(shaketime>0){
		Set2DCameraFocusX(GetStgFrameWidth/2 + rand(-intensity, intensity));
		Set2DCameraFocusY(GetStgFrameHeight/2 + rand(-intensity, intensity));
		shaketime--;
		yield;
	}
	Set2DCameraFocusX(GetStgFrameWidth/2);
	Set2DCameraFocusY(GetStgFrameHeight/2);
	yield;
}

task ScreenShakeA2(intensity, maxintensity){
	let x = Get2DCameraX;
	let y = Get2DCameraY;
	if(intensity>maxintensity){
		while(intensity>maxintensity){
			Set2DCameraFocusX(GetStgFrameWidth/2 + rand(-intensity, intensity));
			Set2DCameraFocusY(GetStgFrameHeight/2 + rand(-intensity, intensity));
			intensity-=0.5;
			yield;
		}
	}else if(intensity<maxintensity){
		while(intensity<maxintensity){
			Set2DCameraFocusX(GetStgFrameWidth/2 + rand(-intensity, intensity));
			Set2DCameraFocusY(GetStgFrameHeight/2 + rand(-intensity, intensity));
			intensity+=0.5;
			yield;
		}
	}
	Set2DCameraFocusX(GetStgFrameWidth/2);
	Set2DCameraFocusY(GetStgFrameHeight/2);
	yield;
}


task Particle(deX, deY, angX, deAlpha, deScale){
	if(TotalParticleCount>=MaximumParticles){return;}
	if(AllowGrazePart != true){return;}
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.49);
	ObjPrim_SetTexture(obj, imgEffect);
	ObjSprite2D_SetSourceRect(obj, 0, 234, 62, 296);
	ObjSprite2D_SetDestRect(obj, -rand(1,4), -rand(1,7.5), rand(1, 4), rand(1, 7.5));
	ObjRender_SetAngleXYZ(obj, 0, 0, angX);
	ObjRender_SetPosition(obj, deX, deY, 0);
	ObjRender_SetAlpha(obj, deAlpha);
	ObjRender_SetColor(obj, 155, 155, 155);

	let scale = deScale;
	let randX = rand(75, 350);
	let randY = rand(75, 350);
	
	TotalParticleCount++;
	while(Obj_IsDeleted(obj)==false){
		scale += (-0.05)/TimeSpeed;
		deX = deX+(randX/100/TimeSpeed)*cos(angX);
		deY = deY+(randX/100/TimeSpeed)*sin(angX);
		ObjRender_SetPosition(obj, deX, deY, 0);	
		ObjRender_SetAngleXYZ(obj, 0, 0, angX+deX+deY+scale*45);
		ObjRender_SetScaleXYZ(obj, scale, scale, scale);
		ObjRender_SetAlpha(obj, deAlpha+scale*20);
		if(scale<0.01){Obj_Delete(obj)}
		yield;
	}
	TotalParticleCount--;
	Obj_Delete(obj);
}

task GhostlyParticle(deX, deY, angX, deAlpha, deScale){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.49);
	ObjPrim_SetTexture(obj, imgEffect);
	ObjSprite2D_SetSourceRect(obj, 3, 234, 62, 296);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAngleXYZ(obj, 0, 0, angX);
	ObjRender_SetPosition(obj, deX, deY, 0);
	ObjRender_SetAlpha(obj, deAlpha);
	
	ObjRender_SetColor(obj, 225, 225, 225);
	let scale = deScale;
	TotalParticleCount++;
	while(scale>0.01){
		scale += -0.04;	
		ObjRender_SetScaleXYZ(obj, scale, scale, scale);
		yield;
	}
	TotalParticleCount--;
	Obj_Delete(obj);
}


task MapleLeaf(deX, deY, angX, deAlpha, deScale){
	deScale = deScale*1.25;
		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D); 
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.49);
	ObjPrim_SetTexture(obj, imgEffect);
	ObjSpriteList2D_SetSourceRect(obj, 0, 48, 32, 80);
	ObjSpriteList2D_SetDestCenter(obj);
	ObjRender_SetAngleXYZ(obj, 0, 0, angX);
	ObjRender_SetPosition(obj, deX, deY, 0);
	ObjRender_SetAlpha(obj, deAlpha);

	let scale = deScale;
	let randX = rand(-105, 105);
	let randY = rand(-105, 105);
	let	ang2 = rand(0, 360);
	
	while(scale>0.01){
		scale += ((-0.2)/2)/TimeSpeed;
		deX += (randX/20)/TimeSpeed;
		deY += (randY/20)/TimeSpeed;
		angX += (randX/15)/TimeSpeed;
		ang2 += (randX/15)/TimeSpeed;
		ObjSpriteList2D_ClearVertexCount(obj);
		ObjSpriteList2D_SetSourceRect(obj, 0, 48, 32, 80);
		ObjSpriteList2D_SetDestCenter(obj);
		ObjSpriteList2D_AddVertex(obj);
		
		ObjRender_SetPosition(obj, deX, deY, 0);
		ObjRender_SetAngleXYZ(obj, ang2, angX, (ang2+angX)/2);
		ObjRender_SetScaleXYZ(obj, scale, scale, scale);
		ObjRender_SetAlpha(obj, deAlpha-scale*20);
		yield;
	}
	Obj_Delete(obj);
}


function DrawSubMenuText(txtStr){
	let objTxt = ObjText_Create;
	ObjText_SetText(objTxt,txtStr);
	ObjText_SetFontSize(objTxt,12);
	ObjText_SetFontColorTop(objTxt,255,255,255);
	ObjText_SetFontColorBottom(objTxt,255,255,255);
	ObjText_SetFontBorderType(objTxt,BORDER_FULL);
	ObjText_SetFontBorderColor(objTxt,48,48,48);
	ObjText_SetFontBorderWidth(objTxt,1);
	ObjText_SetHorizontalAlignment(objTxt, ALIGNMENT_LEFT);
	ObjText_SetMaxWidth(objTxt, 900);
	Obj_SetRenderPriority(objTxt, 0.75);
	return objTxt;
}


function DigitToArray(digit, count){
	let res = [];
	digit = truncate(digit);

	loop{
		let tnum = truncate(digit % 10);
		digit /= 10;
		res = [tnum] ~ res;
		if(truncate(digit) == 0){break;}
	}

	loop(max(0, count - length(res))){
		res = [0] ~ res;
	}

	return res;
}

function GetClosestEnemyID(x1, y1){
	let earray = GetAllEnemyID;
	if(GetEnemyBossSceneObjectID != ID_INVALID){earray = GetEnemyBossObjectID;}
	let objid = ID_INVALID;
	let dist = 99999;
	let size = max(-1, length(earray));
	if(size>=0){
		ascent(b in 0..size){
			let eID = earray[b];
			let edist = GetDistance(x1,y1,ObjRender_GetX(eID),ObjRender_GetY(eID));
			if(edist<dist){
				dist = edist;
				objid = eID
			}
		}
	}
	return objid;
}

function ArrayMin(array){
    let min = array[0];
    ascent(i in 1..length(array)){
        if(array[i] < min){ min = array[i]; }
	}
    return min;
}

function ArrayMax(array){
    let max = array[0];
    ascent(i in 1..length(array)){
        if(array[i] > max){ max = array[i]; }
    }
    return max;
}

function CheckOutsideScreen(objectID, distance){
	let COSvar = false;
	
	if(ObjRender_GetX(objectID)>GetStgFrameWidth+distance||ObjRender_GetX(objectID)<-distance||
	ObjRender_GetY(objectID)>GetStgFrameHeight+distance||ObjRender_GetY(objectID)<-distance){
		COSvar=true;
	}
	
	return COSvar;
}

function CheckOutsideScreenA2(x, y, distance){
	return x>GetStgFrameWidth+distance||x<-distance||y>GetStgFrameHeight+distance||y<-distance;
}

function GetDistance(x1,y1,x2,y2){
	return(((x2-x1)^2+(y2-y1)^2)^(1/2))
}

function AngleToLocation(x1, y1, x2, y2){
	return atan2(y2 - y1, x2 - x1);
}

function AnyKey(type){
	let skck = false;
	if(GetVirtualKeyState(VK_LEFT) == type){skck=true;}
	if(GetVirtualKeyState(VK_RIGHT) == type){skck=true;}
	if(GetVirtualKeyState(VK_UP) == type){skck=true;}
	if(GetVirtualKeyState(VK_DOWN) == type){skck=true;}
	return skck;
}
function AllKeysFree{
	let skck = false;
	if(GetVirtualKeyState(VK_LEFT) == KEY_FREE &&
	GetVirtualKeyState(VK_RIGHT) == KEY_FREE &&
	GetVirtualKeyState(VK_UP) == KEY_FREE &&
	GetVirtualKeyState(VK_DOWN) == KEY_FREE){skck=true;}
	return skck;
}
function AnyKeyHeld{
	let skck = false;
	if(GetVirtualKeyState(VK_LEFT) == KEY_HOLD ||
	GetVirtualKeyState(VK_RIGHT) == KEY_HOLD ||
	GetVirtualKeyState(VK_UP) == KEY_HOLD ||
	GetVirtualKeyState(VK_DOWN) == KEY_HOLD){skck=true;}
	return skck;
}
function GetPlayerMoveAngle{
	let angle = 270;
	if(GetVirtualKeyState(VK_DOWN)==KEY_HOLD){
		if(GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){
			angle = 45;
		}else if(GetVirtualKeyState(VK_LEFT)==KEY_HOLD){
			angle = 135;
		}else{
			angle = 90;
		}
	}
	else if(GetVirtualKeyState(VK_UP)==KEY_HOLD){
		if(GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){
			angle = 315;
		}else if(GetVirtualKeyState(VK_LEFT)==KEY_HOLD){
			angle = 225;
		}else{
			angle = 270;
		}
	}else if(GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){
		angle = 0;
	}else if(GetVirtualKeyState(VK_LEFT)==KEY_HOLD){
		angle = 180;
	}
	return angle;
}


task CreateObjectSound(name){
	//id is a dummy variable for easier navigation
	let objSound = ObjSound_Create;
	ObjSound_Load(objSound, current~"/se/"~name~".wav");
	ObjSound_SetSoundDivision(objSound, SOUND_SE);
	ObjSound_SetRestartEnable(objSound, true);
	GlobalSounds = GlobalSounds~[objSound];
	GlobalSndStr = GlobalSndStr~[name];
}

task StopAllSounds{
	ReallyPlaying = [];
	ascent(i in 0..length(GlobalSounds)){
		if(ObjSound_IsPlaying(GlobalSounds[i])){
			ReallyPlaying = ReallyPlaying~[GlobalSounds[i]];
		}
	}
	ReallyPlaying = ReallyPlaying~[ID_INVALID];
	if(length(ReallyPlaying)>0){
		ascent(i in 0..length(ReallyPlaying)){
			ObjSound_Stop(ReallyPlaying[i]);
		}
	}
}
task ResumeSounds{
	if(length(ReallyPlaying)>0){
		ascent(i in 0..length(ReallyPlaying)){
			ObjSound_Play(ReallyPlaying[i]);
		}
	}
}

task PlaySound(name){
	let ID = -1;
	ascent(i in 0..length(GlobalSndStr)){
		if(GlobalSndStr[i]==name){ID = i; break;}
	}
	ObjSound_Play(GlobalSounds[ID]);
}

function GetIntegerSign(v){
	if(v>0){return 1;}
	else if(v<0){return -1;}
	else {return 0}
}

function GetNumSize(value){
	if(value<=1){return 1;}
	else{return truncate(log10(value))+1;}
}
