function wait(w){loop(w){yield;}}
function centerX{return GetStgFrameWidth/2;}
function centerY{return GetStgFrameHeight/2;}
function _PlaySE(name){NotifyEventAll(EV_USER_PACKAGE + 10, [name]);}
function _PlayBGM(name){NotifyEventAll(EV_USER_PACKAGE + 11, [name]);}
function GetX(id){return ObjMove_GetX(id);}
function GetY(id){return ObjMove_GetY(id);}
function GetDis(x1,y1,x2,y2){return((|((x2-x1)^2+(y2-y1)^2)^(1/2)|));}
function GetAngA1(id){return ObjMove_GetAngle(id);}
function GetAngA2(obj){return atan2(GetPlayerY-GetY(obj),GetPlayerX-GetX(obj));}
function GetAngA3(x1,y1,x2,y2){return atan2(y2-y1,x2-x1);}

function MoveA1(z,f,s,a){ObjMove_AddPatternA1(z,f,s,a);}
function MoveA2(z,f,s,a,ac,max){ObjMove_AddPatternA2(z,f,s,a,ac,0,max);}
task MoveA3(z,t,x2,y2){
	let x1 = GetX(z);
	let y1 = GetY(z);
	
	let t2 = t-1;
	
	let r = GetDis(x1,y1,x2,y2);
	let ang = GetAngA3(x1,y1,x2,y2);
	let rl = r/2;
	
	Obj_SetValue(z,"DesMaxTime",t);
	
	ascent(a in 0..t){
		
		ObjMove_SetDestAtFrame(z,x1+(rl+rl*sin(-90+a/t2*180))*cos(ang),y1+(rl+rl*sin(-90+a/t2*180))*sin(ang),2);
		
		Obj_SetValue(z,"DesTime",t-a);
		
		yield;
	}
	
	Obj_SetValue(z,"DesTime",0);
}
task MoveA4(z,t){
	let x1 = GetX(z);
	let y1 = GetY(z);
	let x2 = centerX+rand(-120,120);
	let y2 = centerY-100+rand(-40,20);
	let za = false;
	
	while(!za){
		if((x1-x2 < 30 && x1-x2 > -30) && (y1-y2 < 30 && y1-y2 > -30)){
			MoveA3(z,t,x2,y2);
			za = true;
		}
		else{
			x2 = centerX+rand(-120,120);
			y2 = centerY-100+rand(-40,20);
		}
	}
}
task MoveOA1(id,dAng){
	let z = dAng;
	while(! Obj_IsDeleted(id)){
		wait(1);
		z*=0.99;
		ObjMove_SetAngle(id,GetAngA1(id)+z);
	}
}

function RandValue(val){
	return truncate(rand(0,val+0.9999999999));
}
task ObjShot_SetFadeTime(id,t){
	wait(t);
	ObjShot_FadeDelete(id);
}
task Obj_SetDeleteTime(id,t){
	wait(t);
	Obj_Delete(id);
}
function DelayLaser(x,y,ang,l,w,graph,delay){
	let objLaser = CreateStraightLaserA1(x,y,ang,l,w,0,graph,delay);
	let alpha = 255;
	ObjStLaser_SetSource(objLaser, false);
	Con;
	return objLaser;
	task Con(){
		loop(delay-1){
			if(ObjShot_GetDelay(objLaser) <= 8){alpha -= 255/8;}
			ObjRender_SetAlpha(objLaser,alpha);
			yield;
			}
		Obj_Delete(objLaser);
	}
}
function CreateCorrectedLaserA1(x,y,ang,length,width,dt,dan,del){
	let id = CreateStraightLaserA1(x,y,ang,length,width,dt,dan,del);
	ObjLaser_SetInvalidLength(id,3,3);
	ObjLaser_SetGrazeInvalidFrame(id,1);
	ObjStLaser_SetSource(id,false);
	
	return id;
}

task ObjEnemy_CreateHitbox(id,rea,red){
	Obj_SetValue(id,"IsHitbox",true);
	while(!Obj_IsDeleted(id)){
		yield;
		if(Obj_GetValueD(id,"IsHitbox",false)){
			ObjEnemy_SetIntersectionCircleToShot(id,GetX(id),GetY(id),rea);
			ObjEnemy_SetIntersectionCircleToPlayer(id,GetX(id),GetY(id),red);
		}
	}
}

task ObjEnemy_CreateMarker(obj2){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetMainStgScriptDirectory ~ "img/system/Default_System.png");
	ObjSprite2D_SetSourceRect(obj,80,0,130,16);
	ObjSprite2D_SetDestCenter(obj); 
	Obj_SetRenderPriority(obj, 0.91);

	while(ObjEnemy_GetInfo(obj2,INFO_LIFE) > 0){
		ObjRender_SetPosition(obj,ObjMove_GetX(obj2),480-8,0);
		yield;
	}

	Obj_Delete(obj);
}
task ObjDelay_CreateLaserArea(x,y,ang,w2,t){
	let w = w2/2;
	loop(t/3){
		DelayMoveQuick(1,0);
		DelayMoveQuick(-1,0);
		wait(3);
	}
	task DelayMoveQuick(dir,dir2){
		let obj = DelayLaser(x,y,ang+dir2,640*2,30,1,10);
		ascent(a in 1..16){
			ObjMove_SetPosition(obj,x+w*dir*cos(a/15*90-90)*cos(ang+90),y+w*dir*cos(a/15*90-90)*sin(ang+90));
			yield;
		}
	}
}

task ObjEnemy_SetHitScore(obj){
	while(!Obj_IsDeleted(obj)){
		let ina = ObjEnemy_GetInfo(obj,INFO_LIFE);
		yield;
		if(ObjEnemy_GetInfo(obj,INFO_LIFE)-ina < 0){AddScore(1);}
	}
}