let dir = GetCurrentScriptDirectory();
InstallFont(dir ~ "./Fonts/Russell Square Regular.ttf");

//-------------------------------------------------------------------------------------------------------------------------------------------------//
function angtoP(obj){return atan2(py - ObjMove_GetY(obj), px - ObjMove_GetX(obj));}
function angtoB(obj){return atan2(by - ObjMove_GetY(obj), bx - ObjMove_GetX(obj));}	
function angtoO(obj1, obj2){return atan2(ObjMove_GetY(obj2) - ObjMove_GetY(obj1), ObjMove_GetX(obj2) - ObjMove_GetX(obj1));}	
function stgw(){return GetStgFrameWidth();}
function stgh(){return GetStgFrameHeight();}

//stx[0] = ClipLeft, stx[1] = ClipRight, stx[2] = ClipXDistance; 
function stx(){return [97, GetScreenWidth()-97, GetScreenWidth()-97*2];} 
//sty[0] = ClipUp, sty[1] = ClipDown, sty[2] = ClipYDistance; 
function sty(){return [1, GetScreenHeight()-1, GetScreenHeight()-1*2];} 

function midPoint(a, b){return (a + b)/2;}
function midX(n){return (stx[1] - stx[0])/n;}
function midY(n){return (sty[0] + sty[1])/n;}

function getvalue(objID, key){return Obj_GetValue(objID, key);}
function getvalue(objID, key, default){return Obj_GetValueD(objID, key, default);}

function px(){return GetPlayerX();}
function py(){return GetPlayerY();}
function bx(){return ObjMove_GetX(objBoss);}
function by(){return ObjMove_GetY(objBoss);}
function gx(){return GetStgFrameWidth() / 2;}
function gy(){return GetStgFrameHeight() / 2;}

function GetAngularDistance(angle1, angle2){ let distance = NormalizeAngle(angle2 - angle1); if(distance>180){ distance-=360; } return distance; }

task DrawName(boss, name, top, bottom, pos){
	let objText = ObjText_Create();
	let alpha = 255;
	ObjText_SetText(objText, name);
	ObjText_SetFontType(objText, "Russell Square");
	ObjText_SetFontSize(objText, 15);
	ObjText_SetFontColorTop(objText, top[0], top[1], top[2]);
	ObjText_SetFontColorBottom(objText, bottom[0], bottom[1], bottom[2]);
	ObjText_SetFontBorderType(objText, BORDER_SHADOW);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2.5);
	ObjText_SetFontCharacterSet(objText, 0x00000001);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_LEFT);
	ObjRender_SetPosition(objText, pos[0], pos[1], 0);
	Obj_SetRenderPriority(objText, 0.7);

	while(!Obj_IsDeleted(boss)){
		if(GetPlayerY > 80 && alpha < 255){
			alpha+=10;
		}
		if(GetPlayerY <= 80 && alpha > 100){
			alpha-=10;
		}
		ObjRender_SetAlpha(objText,alpha);
		yield;
	}
	
	Obj_Delete(objText);
}

function GetRotatedPosition(xPos, yPos, xOrig, yOrig, angle)
{
    let x = xPos - xOrig;
    let y = yPos - yOrig;
    let sc = sincos(angle); 
    return [xOrig + x * sc[1] - y * sc[0], yOrig + y * sc[1] + x * sc[0]];
}

function GetAngleFromTo(xPos, yPos, xOrig, yOrig){return atan2(yPos - yOrig, xPos - xOrig);}
function getDotAngle(x1, y1, x2, y2){let angle = atan2(y1-y2, x1-x2); return angle;}

function getsp_xy(obj){
    let speed = ObjMove_GetSpeed(obj);
    let angle = ObjMove_GetAngle(obj);
    return [speed * cos(angle), speed * sin(angle)];
}

//Fuck it have some more laziness functions
function getx(obj){return ObjMove_GetX(obj);}
function gety(obj){return ObjMove_GetY(obj);}
function geta(obj){return ObjMove_GetAngle(obj);}
function getsp(obj){return ObjMove_GetSpeed(obj);}

function getrx(obj){return ObjRender_GetX(obj);}
function getry(obj){return ObjRender_GetY(obj);}
function getra(obj){return ObjRender_GetAngleZ(obj);}

function setx(obj,x){ObjMove_SetX(obj,x);}
function sety(obj,y){ObjMove_SetY(obj,y);}
function seta(obj,a){ObjMove_SetAngle(obj,a);}
function setsp(obj,s){ObjMove_SetSpeed(obj,s);}
function setblend(obj,b){ObjRender_SetBlendType(obj, b);}
function setspellresist(obj,t){ObjShot_SetSpellResist(obj, t);}
function setpos(obj,x,y){ObjMove_SetPosition(obj,x,y);}
function setrpos(obj,x,y,z){ObjRender_SetPosition(obj,x,y,z);}

task ObjShot_SetFadeDeleteAtFrame(obj, delay){
	wait(delay);
	ObjShot_FadeDelete(obj);
}
task setdblend(obj,delay,b){wait(delay); ObjRender_SetBlendType(obj, b);}

task Delay_AngtoP(obj, delay) {wait(delay); seta(obj, angtoP(obj));}
task Delay_AngtoB(obj, delay) {wait(delay); seta(obj, angtoB(obj));}
task Delay_AngtoC(obj, delay) {wait(delay); seta(obj, atan2(gy - ObjMove_GetY(obj), gx - ObjMove_GetX(obj)));}

//check whether an object is safe to dereference
function isnull(obj){
	//can't check if it hasn't been set, initialize to NULL if there's a chance it'll come up
	if(!obj){return true;} //if it's null
	if(Obj_IsDeleted(obj)){return true;} //if it's been deleted
	return false;
}

function sec(i){ return 1/cos(i);}
function csc(i){ return 1/sin(i);}
function cot(i){ return 1/tan(i);}

task BGM_Play(name, vol, postloop){
	let bgm = ObjSound_Create;
	ObjSound_Load(bgm, GetCurrentScriptDirectory() ~ "BGM/" ~ name ~ ".ogg");
	ObjSound_SetSoundDivision(bgm, SOUND_BGM);
	ObjSound_SetRestartEnable(bgm, true);
	ObjSound_SetVolumeRate(bgm, vol);
	ObjSound_SetLoopEnable(bgm, true);
	ObjSound_SetLoopTime(bgm, postloop[0], postloop[1]);
	
	if(GetCommonData("music", false) == false){ObjSound_Play(bgm);}
}

task ObjMove_SetDestAtFrameWeight(tt, fin_x, fin_y, flame){
	let x = ObjMove_GetX(tt);
	let y = ObjMove_GetY(tt);
	let rx = fin_x - x;
	let ry = fin_y - y;
	let t_rate = 90/flame;
	let s = 0.1;
	ObjMove_SetAngle(tt, getDotAngle(fin_x, fin_y, ObjMove_GetX(tt), ObjMove_GetY(tt)));
	ascent(time in 1..flame+1){
		s -= 0.1/(flame-1);
		ObjMove_SetSpeed(tt, s);
		ObjMove_SetX(tt, x + rx * sin(t_rate * time));
		ObjMove_SetY(tt, y + ry * sin(t_rate * time));
		yield;
	}
	ObjMove_SetSpeed(tt, 0);
}
