//Item names, for easier access
let PY_ITEM_DELETE 				= 1;
let PY_ITEM_POINT		 		= 2;
let PY_ITEM_POWER		 		= 3;
let PY_ITEM_FULL		 		= 4;
let PY_ITEM_SPELLPART			= 5;
let PY_ITEM_SPELL				= 6;
let PY_ITEM_LIFEPART			= 7;
let PY_ITEM_LIFE				= 8;
let PY_ITEM_ICHOR				= 9;

//--------------------------------------------------------------------------------------------------------------------------------------------
//Random functions library
//--------------------------------------------------------------------------------------------------------------------------------------------


//Math functions

//Special coordinate accessors (Python)
function GetCenterX() {
	let calc = GetStgFrameWidth / 2;
	return calc;
}
function GetCenterY() {
	let calc = GetStgFrameHeight / 2;
	return calc;
}
function GetClipMinX() { 
	let calc = GetStgFrameWidth - GetStgFrameWidth;
	return calc; 
}
function GetClipMaxX() { 
	let calc = GetStgFrameWidth;
	return calc; 
}
function GetClipMinY() { 
	let calc = GetStgFrameHeight - GetStgFrameHeight;
	return calc; 
}
function GetClipMaxY() { 
	let calc = GetStgFrameHeight;
	return calc; 
}

//Returns a random integer (Python)
function rand_int(min, max){
  return round(rand(min, max))
}

//Returns the angle between two points (Ultima)
function GetPointAngle(x1, y1, x2, y2){
	let angle = atan2(y1 - y2, x1 - x2);
	return angle;
}

//(Blargel)
function NormalizeAngle(angle){
	angle %= 360;
 
	if(angle<0){
  		angle += 360;
 	}
 
 	return angle
}

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

//Returns the intersection point of two lines (Naut)
function GetLineIntersectionPoint(let x1, let y1, let x2, let y2, let x3, let y3, let x4, let y4){
    let point = [0, 0];
    point[0] = ( ((x1*y2 - y1*x2)*(x3 - x4) - (x1 - x2)*(x3*y4 - y3*x4)) / ((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)) );
    point[1] = ( ((x1*y2 - y1*x2)*(y3 - y4) - (y1 - y2)*(x3*y4 - y3*x4)) / ((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)) );
    return point;
}

//Returns the intersection point of a line with the screen border (Trickysticks)
function GetLineBorderPoint(px, py, tx, ty){
    let ta = atan2(ty-py, tx-px);
 
    if( ta>= atan2(GetClipMinY - py, GetClipMinX - px) && ta<=  atan2(GetClipMinY - py, GetClipMaxX - px)){
        return [px - (GetClipMinY-py)*tan(ta+90), GetClipMinY];
    }
    if( ta>= atan2(GetClipMinY - py, GetClipMaxX - px) && ta<=  atan2(GetClipMaxY - py, GetClipMaxX - px)){
        return [GetClipMaxX, py + (GetClipMaxX-px)*tan(ta)];
    }
    if( ta>= atan2(GetClipMaxY - py, GetClipMaxX - px) && ta<=  atan2(GetClipMaxY - py, GetClipMinX - px)){
        return [px - (GetClipMaxY-py)*tan(ta-90), GetClipMaxY];
    }
    if( ta> atan2(GetClipMaxY - py, GetClipMinX - px) || ta<  atan2(GetClipMinY - py, GetClipMinX - px)){
        return [GetClipMinX, py + (GetClipMinX-px)*tan(ta+180)];
    }
}

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

//rotates coordinates woo
function Rotate2D_XY(x, y, angle){
	let c = cos(angle);
	let s = sin(angle);
	return [x*c - y*s, x*s + y*c];
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------------------
//Item Functions

//Creates a small, basic item
function CreateSmallItem(x, y, type, value){
	if(type == PY_ITEM_POINT){
		value = GetAreaCommonData("System", "PointValue", 10000);
	}

	let obj = CreateItemU2(type, x, y, x, y - 32, value);
	ObjItem_SetRenderScoreEnable(obj, false);
	ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);

	//If the item is a power item, this is the amount it will add to the player's power upon collection
	//The necessary code is executed in the item script
	if(type == PY_ITEM_POWER){
		Obj_SetValue(obj, "AddPower", 1);
	}
	return obj;
}

//Creates a large, basic item
function  CreateLargeItem(x, y, type, value){
	if(type == PY_ITEM_POINT){
		value = GetAreaCommonData("System", "PointValue", 10000)*2;
	}

	let obj = CreateItemU2(type, x, y, x, y - 32, value);
	ObjItem_SetRenderScoreEnable(obj, false);

	//If the item is a power item, this is the amount it will add to the player's power upon collection
	//The necessary code is executed in the item script
	if(type == PY_ITEM_POWER){
		Obj_SetValue(obj, "AddPower", GetAreaCommonData("System", "PlayerMaxPower", 400) / 4);
	}
	return obj;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------------------
//Misc. functions

//Stops the script for w frames (Python)
function wait(w){loop(w){yield;}}

//Stops the script until this many enemies are on screen (Python)
function WaitForEnemyCount(num){
     while(length(GetAllEnemyID()) > num){yield;}
}

//Deletes all shots on screen for w frames (Python)
function ExterminateShot(w, type){
	loop(w){
		DeleteShotAll(TYPE_ALL, type);
		yield;
	}
}

//Calculates the reflection angle based on the angle of the object and the angle of the mirror
function ReflectionAngle (angle, mirror){
   return 2*mirror-angle;
}

//Makes the screen shake (gt_bot)
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;
}

//0.12m style invincibility (AJS)
task PlaytestInvincibility{
	AddVirtualKey(VK_USER1,KEY_SPACE,KEY_INVALID);
	loop{
		if(GetVirtualKeyState(VK_USER1)==KEY_HOLD){
		SetPlayerInvincibilityFrame(30);}
	yield;}	
}

//Shorthand enemy creation function (gt_bot)
function CreateEnemyObject(CEOlife, SBIimage, rec1, rec2, rec3, rec4){
	let CEOboss = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(CEOboss);
	ObjPrim_SetTexture(CEOboss, SBIimage); 
	ObjSprite2D_SetSourceRect(CEOboss, rec1, rec2, rec3, rec4); 
	ObjSprite2D_SetDestCenter(CEOboss);
	ObjEnemy_SetLife(CEOboss, CEOlife);
	return CEOboss;
}

//Returns true if the object is within the given boundaries (gt_bot)
function Obj_IsInArea(obj, left, top, right, bottom){
	if(ObjMove_GetX(obj)<left){
		return false;
	}
               
	if(ObjMove_GetX(obj)>right){
		return false;
	}      
 
	if(ObjMove_GetY(obj)<top){
		return false;
	}
               
	if(ObjMove_GetY(obj)>bottom){
		return false;
	}              
	return true
}

function Point_IsInArea(point, left, top, right, bottom){
	if(point[0]<left){
		return false;
	}
               
	if(point[0]>right){
		return false;
	}      
 
	if(point[1]<top){
		return false;
	}
               
	if(point[1]>bottom){
		return false;
	}              
	return true
}

//Makes the object spin around the XYZ axes (Python)
task Obj_SpinXYZ(obj, xspin, yspin, zspin, i){
	if(i <= 0){
		while(!Obj_IsDeleted(obj)){
			ObjRender_SetAngleXYZ(obj, ObjRender_GetAngleX(obj) + xspin, ObjRender_GetAngleY(obj) + yspin, ObjRender_GetAngleZ(obj) + zspin);
			yield;
		}
		return;
	}else{
		while(i > 0){
			ObjRender_SetAngleXYZ(obj, ObjRender_GetAngleX(obj) + xspin, ObjRender_GetAngleY(obj) + yspin, ObjRender_GetAngleZ(obj) + zspin);
			i--;
			yield;
		}
		ObjRender_SetAngleXYZ(obj, 0, 0, 0);
		return;
	}
}

//(AJS)
task ExpandFrame(speed){
    let left;
    let top;
    let right;
    let bottom;
    let t = 0;
    let F = [[32,0],[16,0],[416,640],[464,480]];
     
    while(t<1){
    	left = F[0][0]+t*(F[0][1]-F[0][0]);
    	top = F[1][0]+t*(F[1][1]-F[1][0]);
   		right = F[2][0]+t*(F[2][1]-F[2][0]);
    	bottom = F[3][0]+t*(F[3][1]-F[3][0]);
    	SetStgFrame(left,top,right,bottom,20,80);
    	SetPlayerClip(left,top,right,bottom);
    	t+=speed/100;
    	yield;
    }
    SetStgFrame(floor(left),floor(top),F[2][1],ceil(bottom),20,80);
    SetPlayerClip(floor(left),floor(top),F[2][1],ceil(bottom));
}

//(AJS)
task widescreen{
	let left = 32;
	let top = 16;
	let right = 416;
	let bottom = 464;
	
	loop{
		if(left>0){left-=0.51;}
		if(top>0){top-=0.25;}
		if(right<GetScreenWidth){right+=4;}
		if(bottom<GetScreenHeight){bottom+=0.26;}
		SetStgFrame(left,top,right,bottom,20,80);
		SetPlayerClip(left,top,right,bottom);
		yield;
	}
}

//Difficulty (Python)
task Difficulty(img, left, top, right, bottom){
	let objText = ObjPrim_Create(OBJ_SPRITE_2D);

	ObjPrim_SetTexture(objText, img);
	ObjSprite2D_SetSourceRect(objText, left, top, right, bottom);
	ObjSprite2D_SetDestCenter(objText);
  	Obj_SetRenderPriority(objText, 0.7);
  	ObjRender_SetScaleXYZ(objText, 0.5, 0.5, 0);
  	ObjRender_SetX(objText, GetCenterX);
  	ObjRender_SetY(objText, 8);
  	loop(5){
  		ObjRender_SetAlpha(objText, 0);
  		wait(5);
  		ObjRender_SetAlpha(objText, 255);
  		wait(5);
  	}
  	let alpha = 255;
  	while(alpha > 0){
  		ObjRender_SetAlpha(objText, alpha);
  		alpha -= 5;
  		yield;
  	}
  	wait(10);
  	Obj_SetRenderPriority(objText, 1.0);
  	ObjRender_SetX(objText, GetStgFrameWidth + GetStgFrameLeft + 64);
  	ObjRender_SetY(objText, 8);
  	while(alpha < 255){
  		ObjRender_SetAlpha(objText, alpha);
  		alpha += 5;
  		yield;
  	}
}

//Stage intro/tile function mimicking LoLK's titles (Python)
task StageIntro(img){
	let stage = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(stage, img);
	ObjSprite2D_SetSourceRect(stage, 0, 224, 192, 256);
	ObjSprite2D_SetDestCenter(stage);
	ObjRender_SetPosition(stage, GetCenterX, GetCenterY - 96, 0);
	ObjRender_SetAlpha(stage, 0);
	ObjRender_SetScaleXYZ(stage, 0.6, 0.6, 0.6);
	Obj_SetRenderPriority(stage, 0.6);

	let pic = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(pic, img);
	ObjSprite2D_SetSourceRect(pic, 256, 64, 448, 256);
	ObjSprite2D_SetDestCenter(pic);
	ObjRender_SetPosition(pic, GetCenterX, GetCenterY - 48, 0);
	ObjRender_SetAlpha(pic, 0);
	ObjRender_SetScaleXYZ(pic, 0.6, 0.6, 0.6);
	ObjRender_SetAngleZ(pic, -90);
	Obj_SetRenderPriority(pic, 0.59);

	let japtext = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(japtext, img);
	ObjSprite2D_SetSourceRect(japtext, 0, 0, 640, 64);
	ObjSprite2D_SetDestCenter(japtext);
	ObjRender_SetScaleXYZ(japtext, 0.6, 0.6, 0.6);
	ObjRender_SetPosition(japtext, GetCenterX, GetCenterY - 72, 0);
	ObjRender_SetAlpha(japtext, 0);
	Obj_SetRenderPriority(japtext, 0.6);

	let engtext = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(engtext, img);
	ObjSprite2D_SetSourceRect(engtext, 0, 64, 256, 128);
	ObjSprite2D_SetDestCenter(engtext);
	ObjRender_SetScaleXYZ(engtext, 0.6, 0.6, 0.6);
	ObjRender_SetPosition(engtext, GetCenterX + 48, GetCenterY - 48, 0);
	ObjRender_SetAlpha(engtext, 0);
	Obj_SetRenderPriority(engtext, 0.6);

	let fadeout = false;
	renderalltexts(stage);
	renderpic;
	wait(30);
	renderalltexts(japtext);
	wait(100);
	renderalltexts(engtext);
	wait(180);
	fadeout = true;

	task renderalltexts(object){
		let alpha = 0;
		while(alpha < 255){
			ObjRender_SetAlpha(object, alpha);
			alpha += 2;
			yield;
		}
		while(!fadeout){
			yield;
		}
		while(alpha > 0){
			ObjRender_SetAlpha(object, alpha);
			alpha -= 4;
			yield;
		}
		Obj_Delete(object);
	}

	task renderpic{
		let alpha = 0;
		let x = GetCenterX;
		let z = -90;
		while(alpha < 255){
			ObjRender_SetAlpha(pic, alpha);
			ObjRender_SetX(pic, x);
			ObjRender_SetAngleZ(pic, z);
			alpha += 2;
			x += 48/128;
			z += 90/128;
			yield;
		}
		while(!fadeout){
			yield;
		}
		while(alpha > 0){
			ObjRender_SetAlpha(pic, alpha);
			alpha -= 4;
			yield;
		}
		Obj_Delete(pic);
		Obj_Delete(stage);
	}
}

task EnemyName(obj, name, r, g, b){
  	let objText = ObjText_Create();
  	ObjText_SetFontSize(objText,12);//new
  	ObjText_SetFontType(objText, "Russell Square");
  	ObjText_SetFontColorTop(objText, r, g, b);
  	ObjText_SetFontColorBottom(objText, r/2, g/2, b/2);
  	ObjText_SetFontBorderType(objText, BORDER_SHADOW);
 	ObjText_SetFontBorderColor(objText,255, 255, 255);
 	ObjText_SetFontBorderWidth(objText, 1);
  	Obj_SetRenderPriority(objText, 0.8);
 	ObjRender_SetX(objText, 0);
  	ObjRender_SetY(objText, 0);
 	ObjText_SetFontBorderColor(objText, 0, 0, 0);
  	ObjText_SetText(objText, name);

  	while(ObjEnemy_GetInfo(obj,INFO_LIFE)>0){
  	 	yield;
  	}
    wait(12);
  	Obj_Delete(objText);
}

task BottomEnemyMarker(obj, bossobj){
	let imgnumber = GetCurrentScriptDirectory ~ "../IMG/Effect/enemymarker.png";
 	let objEnemy = ObjPrim_Create(OBJ_SPRITE_2D);
 	ObjRender_SetPosition(objEnemy, GetCenterX, GetStgFrameHeight+32, 1); 
	ObjRender_SetBlendType(objEnemy, BLEND_ALPHA);
	Obj_SetRenderPriority(objEnemy, 0.85);
	ObjPrim_SetTexture(objEnemy, imgnumber);
 	ObjRender_SetAlpha(objEnemy, 255);
 	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 48, 16);
	ObjSprite2D_SetDestCenter(objEnemy);

	while(ObjEnemy_GetInfo(bossobj,INFO_LIFE)>0){
		ObjRender_SetPosition(objEnemy, ObjMove_GetX(obj) + 128, GetStgFrameHeight+24, 1);
    	yield;
  	}
    //wait(60); //So it doesn't look like they disappear in between attacks.
  	wait(12);//It can only handle 12 yields, currently, before the script is closed.
  	Obj_Delete(objEnemy);
}

//Background music titles
task BGM_Title(img, left, top, right, bottom){
	let objText = ObjPrim_Create(OBJ_SPRITE_2D);

	ObjPrim_SetTexture(objText, img);
	ObjSprite2D_SetSourceRect(objText, left, top, right, bottom);
	ObjSprite2D_SetDestCenter(objText);
  	Obj_SetRenderPriority(objText, 0.7);
  	ObjRender_SetScaleXYZ(objText, 0.5, 0.5, 0);
  	ObjRender_SetX(objText, GetClipMaxX + 128);
  	ObjRender_SetY(objText, GetClipMaxY - 8);

  	let x =  GetClipMaxX + 128;
  	while(x > GetClipMaxX - (right - left)/2){
  		ObjRender_SetX(objText, x);
  		x -= 8;
  		yield;
  	}
  	wait(180);

  	let a = 255;
  	while(a > 0){
  		ObjRender_SetAlpha(objText, a);
  		a -= 4;
  		yield;
  	}
  	Obj_Delete(objText);
}

task ObjEnemy_HandleCollisionA1(obj, radius){
	while(!Obj_IsDeleted(obj)){
		if(Obj_GetValueD(obj, "CheckCollision", true)){
			ObjEnemy_SetIntersectionCircleToShot(obj, ObjMove_GetX(obj), ObjMove_GetY(obj), radius);
			ObjEnemy_SetIntersectionCircleToPlayer(obj, ObjMove_GetX(obj), ObjMove_GetY(obj), radius/2);
		}
		yield;
	}
}

task ObjEnemy_PlayHitSound(obj){
	let life = ObjEnemy_GetInfo(obj, INFO_LIFE)/8;
	while(!Obj_IsDeleted(obj)){
		if(!IsPlayerSpellActive){
			if(ObjEnemy_GetInfo(obj, INFO_DAMAGE_RATE_SHOT) > 0){
				if(ObjEnemy_GetInfo(obj, INFO_SHOT_HIT_COUNT) > 0 && ObjEnemy_GetInfo(obj, INFO_LIFE) > life){
					AddScore(2);
					PlaySoundA1(24, 80, 0);
				}else if(ObjEnemy_GetInfo(obj, INFO_SHOT_HIT_COUNT) > 0 && ObjEnemy_GetInfo(obj, INFO_LIFE) <= life){
					AddScore(2);
					PlaySoundA1(25, 80, 0);
				}
			}else{
				if(ObjEnemy_GetInfo(obj, INFO_SHOT_HIT_COUNT) > 0){
					PlaySoundA1(71, 80, 0);
				}
			}
		}else{
			if(ObjEnemy_GetInfo(obj, INFO_DAMAGE_RATE_SPELL) > 0){
				if(ObjEnemy_GetInfo(obj, INFO_SHOT_HIT_COUNT) > 0 && ObjEnemy_GetInfo(obj, INFO_LIFE) > life){
					AddScore(2);
					PlaySoundA1(24, 80, 0);
				}else if(ObjEnemy_GetInfo(obj, INFO_SHOT_HIT_COUNT) > 0 && ObjEnemy_GetInfo(obj, INFO_LIFE) <= life){
					AddScore(2);
					PlaySoundA1(25, 80, 0);
				}
			}else{
				if(ObjEnemy_GetInfo(obj, INFO_SHOT_HIT_COUNT) > 0){
					PlaySoundA1(71, 80, 0);
				}
			}
		}
		yield;
	}
}

task ObjRender_SetMoveFromBackground(obj, time, startscale, endscale){
	let s = startscale;
	Obj_SetValue(obj, "IsSpawning", true);
	Obj_SetRenderPriorityI(obj, 24);
	loop(time){
		ObjRender_SetScaleXYZ(obj, s, s, 0);
		s += (endscale - startscale)/time;
		yield;
	}
	Obj_SetValue(obj, "IsSpawning", false);
	Obj_SetRenderPriorityI(obj, 30);
}

task ObjRender_SetTrueAlpha(obj, alpha){
	Obj_SetValue(obj, "ALPHA", alpha);
	alternative(ObjRender_GetBlendType(obj))
	case(BLEND_ALPHA){ObjRender_SetAlpha(obj, alpha);}
	case(BLEND_ADD_ARGB){ObjRender_SetAlpha(obj, alpha);}
	case(BLEND_ADD_RGB){ObjRender_SetColor(obj, alpha, alpha, alpha);}
	case(BLEND_SUBTRACT){ObjRender_SetColor(obj, alpha, alpha, alpha);}
	case(BLEND_MULTIPLY){ObjRender_SetColor(obj, alpha, alpha, alpha);}
	case(BLEND_SHADOW){ObjRender_SetColor(obj, alpha, alpha, alpha);}
	others{ObjRender_SetAlpha(obj, alpha);}
}

function ObjRender_GetAlpha(obj){
	return Obj_GetValueD(obj, "ALPHA", 256);
}

task PlaySoundA1(index, volume, delay){
	wait(delay);
	NotifyEvent(GetCommonData("SoundScript", GetOwnScriptID), EV_USER, [index, volume]);
}

task LifeCircle(renderObj, enemyObj){
	let path = GetCurrentScriptDirectory() ~ "../System/img/Default_System.png";
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D); //lifebar base
	let objb = ObjPrim_Create(OBJ_PRIMITIVE_2D); //actual life bar
	let obj2 = ObjPrim_Create(OBJ_SPRITE_LIST_2D); //boss stars
	let obj3 = ObjPrim_Create(OBJ_SPRITE_LIST_2D); //life bar knobs
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetPrimitiveType(objb, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory() ~ "../System/img/lifebar.png");
	ObjPrim_SetTexture(objb, GetCurrentScriptDirectory() ~ "../System/img/lifebar.png");
	ObjPrim_SetTexture(obj2, path);
	ObjPrim_SetTexture(obj3, GetCurrentScriptDirectory() ~ "../System/img/lifebar.png");
	Obj_SetRenderPriority(obj, 0.68);
	Obj_SetRenderPriority(objb, 0.69);
	Obj_SetRenderPriority(obj2, 0.7);
	Obj_SetRenderPriority(obj3, 0.7);

	let lastRemStep = -1;
	let lifeRateRender = 0;

	let vertexcount = 64;
	let innerrad = 60; //inner radius of the circle
	let outerrad = 68; //outer radius of the circle

	let objScene = ID_INVALID;
	let bossEnemy = [];

	ObjPrim_SetVertexCount(obj, vertexcount);
	ObjPrim_SetVertexCount(objb, vertexcount);

	let lifeM = ObjEnemy_GetInfo(enemyObj, INFO_LIFE); //starting life
	loop{
		ObjSpriteList2D_ClearVertexCount(obj2);
		ObjSpriteList2D_ClearVertexCount(obj3);
		if(!Obj_IsDeleted(enemyObj)){
			if(length(GetEnemyBossObjectID) > 0){
				ObjRender_SetAlpha(obj, 256);
				ObjRender_SetAlpha(objb, 256);
				ObjRender_SetAlpha(obj2, 256);
				ObjRender_SetAlpha(obj3, 256);
			}else{
				ObjRender_SetAlpha(obj, 0);
				ObjRender_SetAlpha(objb, 0);
				ObjRender_SetAlpha(obj2, 0);
				ObjRender_SetAlpha(obj3, 0);
			}

			let life = ObjEnemy_GetInfo(enemyObj, INFO_LIFE); //life remaining
			ascent(i in 0..vertexcount/2){
		        let indexvert = i*2;
		        let angle = i*360/(vertexcount*0.5-1) - 90;
		        let angle2 = -90-(life/lifeM)*i*360/(vertexcount*0.5-1);
		        ObjPrim_SetVertexPosition(obj, indexvert+0, ObjRender_GetX(renderObj)+innerrad*cos(angle), ObjRender_GetY(renderObj)+innerrad*sin(angle), 0);
		        ObjPrim_SetVertexPosition(obj, indexvert+1, ObjRender_GetX(renderObj)+outerrad*cos(angle), ObjRender_GetY(renderObj)+outerrad*sin(angle), 0);

		        ObjPrim_SetVertexUVT(obj, indexvert+0, 10, 0);
		        ObjPrim_SetVertexUVT(obj, indexvert+1, 20, 32);


		        ObjPrim_SetVertexPosition(objb, indexvert+0, ObjRender_GetX(renderObj)+innerrad*cos(angle2), ObjRender_GetY(renderObj)+innerrad*sin(angle2), 0);
		        ObjPrim_SetVertexPosition(objb, indexvert+1, ObjRender_GetX(renderObj)+outerrad*cos(angle2), ObjRender_GetY(renderObj)+outerrad*sin(angle2), 0);

		        ObjPrim_SetVertexUVT(objb, indexvert+0, 0, 0);
		        ObjPrim_SetVertexUVT(objb, indexvert+1, 10, 32);
		    }
		}else{
			Obj_Delete(obj);
			Obj_Delete(objb);
			Obj_Delete(obj2);
			Obj_Delete(obj3);
			return;
		}
		yield;
	}
}




//Hitbox debugging tasks (Ultima)
task ManageAllShotCollision()
{
	let base_size = 512;
	let obj = [];
	let Debug = true;
	loop
	{
		if(GetPlayerInvincibilityFrame()<=0)
		{
			obj = GetShotIdInCircleA2(GetPlayerX,GetPlayerY,40,TARGET_ENEMY);
			ascent(i in 0..length(obj))
			{
				if(Obj_GetValueD(obj[i],"AUTO COLLISION",true)&&Obj_GetType(obj[i])==OBJ_SHOT)
				{
					if(GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(obj[i]),ObjMove_GetY(obj[i]))<=base_size)
					{
						if(Debug)
						{
							ObjRender_SetColor(obj[i],0,255,0);
						}
						ObjShot_SetIntersectionEnable(obj[i],true);
					}
					else
					{
						if(Debug)
						{
							ObjRender_SetColor(obj[i],255,0,0);
						}
						ObjShot_SetIntersectionEnable(obj[i],false);
					}
				}
			}
		}
		yield;
	}


}

task ManageAllLaserCollision()
{
	let base_size = 512;
	let obj = [];
	let Debug = true;
	loop
	{
		if(GetPlayerInvincibilityFrame()<=0)
		{
			obj = GetShotIdInCircleA2(GetPlayerX,GetPlayerY,40,TARGET_ENEMY);
			ascent(i in 0..length(obj))
			{
				if(Obj_GetValueD(obj[i],"AUTO COLLISION",true)&&Obj_GetType(obj[i])==OBJ_LOOSE_LASER)
				{
					if(GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(obj[i]),ObjMove_GetY(obj[i]))<=base_size+ObjLaser_GetLength(obj[i]))
					{
						if(Debug)
						{
							ObjRender_SetColor(obj[i],0,255,0);
						}
						ObjShot_SetIntersectionEnable(obj[i],true);
					}
					else
					{
						if(Debug)
						{
							ObjRender_SetColor(obj[i],255,0,0);
						}
						ObjShot_SetIntersectionEnable(obj[i],false);
					}
				}
				else if(Obj_GetValueD(obj[i],"AUTO COLLISION",true)&&Obj_GetType(obj[i])==OBJ_CURVE_LASER)
				{
					if(GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(obj[i]),ObjMove_GetY(obj[i]))<=base_size+(ObjLaser_GetLength(obj[i])*ObjMove_GetSpeed(obj[i])))
					{
						if(Debug)
						{
							ObjRender_SetColor(obj[i],0,255,0);
						}
						ObjShot_SetIntersectionEnable(obj[i],true);
					}
					else
					{
						if(Debug)
						{
							ObjRender_SetColor(obj[i],255,0,0);
						}
						ObjShot_SetIntersectionEnable(obj[i],false);
					}				
				}
			}
		}
		yield;
	}

}