InstallFont(GetCurrentScriptDirectory() ~ "./Fonts/Russell Square Regular.ttf");
InstallFont(GetCurrentScriptDirectory() ~ "../lib/Fonts/adventpro-semibold.ttf");

//-------------------------------------------------------------------------------------------------------------------------------------------------//
function limit(obj, t){return (ObjMove_GetX(obj)<-t && ObjMove_GetX(obj)>stgw+t && ObjMove_GetY(obj)<-t && ObjMove_GetY(obj)>stgh+t);}
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();}
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 wait(n){loop(n){yield;}}
function NormalizeAngle(angle){ angle %= 360; if(angle<0){angle += 360;} return angle; }
function GetAngularDistance(angle1, angle2){ let distance = NormalizeAngle(angle2 - angle1); if(distance>180){ distance-=360; } return distance; }

task ObjMove_SetDestAtFrameWeight(objBoss, fin_x, fin_y, flame){
	let x = ObjMove_GetX(objBoss);
	let y = ObjMove_GetY(objBoss);
	let rx = fin_x - x;
	let ry = fin_y - y;
	let t_rate = 90/flame;
	let s = 0.1;
	ObjMove_SetAngle(objBoss, getDotAngle(fin_x, fin_y, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)));
	ascent(time in 1..flame+1){
		s -= 0.1/(flame-1);
		ObjMove_SetSpeed(objBoss, s);
		ObjMove_SetX(objBoss, x + rx * sin(t_rate * time));
		ObjMove_SetY(objBoss, y + ry * sin(t_rate * time));
		yield;
	}
	ObjMove_SetSpeed(objBoss, 0);
}
function getDotAngle(x1, y1, x2, y2){let angle = atan2(y1-y2, x1-x2); return 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 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);
}

//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);}
function sqrt(i){ return power(i, -2);}

function rand_int(low,high){return round(rand(low-0.49999,high+0.49999));}

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

function PolygonShot(x, y, speed, angle, nSide, bpSide, special, graphic, delay){
    let bpos_x = [];
    let bpos_y = [];
    ascent(i in 0..nSide){
        let tmp_angle = 360 / nSide * i;
        bpos_x = bpos_x ~ [cos(tmp_angle)];
        bpos_y = bpos_y ~ [sin(tmp_angle)];
    }
	
	let B = []; let z = 0;
    ascent(i in 0..nSide){
        let from_x = x + bpos_x[i % nSide];
        let from_y = y + bpos_y[i % nSide];
        let to_x = x + bpos_x[(i + special) % nSide];
        let to_y = y + bpos_y[(i + special) % nSide];

        ascent(j in 0..bpSide){
            let tmp = (j + 1) / bpSide;

            let vx = GetLinePosition(from_x, to_x, tmp);
            let vy = GetLinePosition(from_y, to_y, tmp);
            let dx = vx - x;
            let dy = vy - y;
            let va = atan2(dy, dx) + angle;
            let vs = (dx * dx + dy * dy) ^ 0.5;

            B = B ~ [CreateShotA1(x, y, vs * speed, va, graphic, delay)];
			Obj_SetValue(B[z], "sv", vs);
			Obj_SetValue(B[z], "av", va);
			z++;
        }
    }
    
	return B;
    function GetLinePosition(from_pos, to_pos, pos_t){
        return (from_pos + (to_pos - from_pos) * pos_t);
    }
}

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