#TouhouDanmakufu[Player]
#ScriptVersion[2]
#Menu[Nitori Kawashiro]
#Text[]
#Image[]
#ReplayName[Nitori]

script_player_main{
let GCSD=GetCurrentScriptDirectory;
let vehicleimg=[GCSD~"img\Nitoricycle.png", "", "", "", "", "", ""];
let vehiclelist=["NITORICYCLE", "NITORITRUCK", "NITORISUB", "NITORICOPTER", "NITORIJET", "NITORITANK", "NITORIBOT"];
let weaponimg=[
	GCSD~"img\Gun.png",
	GCSD~"img\Gun.png",
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
	GCSD,
];
let Cbar=GCSD~"img\Cbar.png";
let Cbarb=GCSD~"img\Cbarb.png";
let lifeimg=GCSD~"img\life.png";
let radarimg=GCSD~"img\radar.png";
let helperimg=GCSD~"img\helpers.png";
let placementdrawstate=0;
let placementmovex=0;
let placementmovey=0;
let placementmoveang=0;
let shotframes=[];
let inplay=0;
let currentweapon=[];
let damage=0;
let damagerefresh=0;
let vehiclenum=0;
let immunity=1;
let damagerate=1;
///////////////////////HELPER IMAGES///////////////////////
let rinexploimg=GCSD~"img\rinexplo.png"; LoadGraphic(rinexploimg);

#include_function ".\Weapons.txt"
#include_function ".\helper.txt"

task weaponplacing{
	let placementtick=0;
	yield;
	while(GetKeyState(VK_SHOT)!=KEY_PUSH && GetKeyState(VK_BOMB)!=KEY_PUSH){
		placementdrawstate=1;
		if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
			if(GetKeyState(VK_LEFT)==KEY_PUSH){
				placementtick=0;
				if(placementmoveang%4==0){
					placementmoveang=placementmoveang-4;
				}else{
					placementmoveang=placementmoveang-placementmoveang%4;
				}
			}
			if(GetKeyState(VK_LEFT)==KEY_HOLD){
				placementtick++;
				if(placementtick%10==0 && placementtick>15){
					if(placementmoveang%4==0){
						placementmoveang=placementmoveang-4;
					}else{
						placementmoveang=placementmoveang-placementmoveang%4;
					}
				}
			}
			if(GetKeyState(VK_RIGHT)==KEY_PUSH){
				placementtick=0;
				if(placementmoveang%4==0){
					placementmoveang=placementmoveang+4;
				}else{
					placementmoveang=placementmoveang+placementmoveang%4;
				}
			}
			if(GetKeyState(VK_RIGHT)==KEY_HOLD){
				placementtick++;
				if(placementtick%10==0 && placementtick>15){
					if(placementmoveang%4==0){
						placementmoveang=placementmoveang+4;
					}else{
						placementmoveang=placementmoveang+placementmoveang%4;
					}
				}
			}
			if(GetKeyState(VK_LEFT)==KEY_PULL || GetKeyState(VK_RIGHT)==KEY_PULL){placementtick=0;}
			if(GetKeyState(VK_UP)==KEY_PUSH){
				placementmoveang=180-placementmoveang;
			}
			if(GetKeyState(VK_DOWN)==KEY_PUSH){
				placementmoveang=0-placementmoveang;
			}
			if(GetKeyState(VK_USER)==KEY_PUSH){
				placementmoveang=0-placementmoveang;
				placementmovex=-placementmovex;
			}
		}else{
			if(GetKeyState(VK_LEFT)==KEY_PUSH || GetKeyState(VK_LEFT)==KEY_HOLD){placementmovex=placementmovex-1;}
			if(GetKeyState(VK_RIGHT)==KEY_PUSH || GetKeyState(VK_RIGHT)==KEY_HOLD){placementmovex=placementmovex+1;}
			if(GetKeyState(VK_UP)==KEY_PUSH || GetKeyState(VK_UP)==KEY_HOLD){placementmovey=placementmovey-1;}
			if(GetKeyState(VK_DOWN)==KEY_PUSH || GetKeyState(VK_DOWN)==KEY_HOLD){placementmovey=placementmovey+1;}
			if(GetKeyState(VK_USER)==KEY_PUSH || GetKeyState(VK_USER)==KEY_HOLD){placementmoveang=placementmoveang+1;}
		}
		yield;
	}
	if(GetKeyState(VK_SHOT)==KEY_PUSH){placingfinish;}
	placementdrawstate=0;
}
sub placingfinish{
	let weaponplacement=GetCommonData("WEAPONPLACEMENT");
	SetRenderTarget(GetCommonData("VEHICLE"));
		SetTexture(weaponimg[GetCommonData("PLACEMENT")[1]]);
		SetGraphicRect(0, 0, 64, 64);
		SetGraphicScale(2.6, 2.6);
		SetAlpha(255);
		SetColor(255, 255, 255);
		SetGraphicAngle(0, 0, placementmoveang);
		DrawGraphic(256+placementmovex*4, 256+placementmovey*4);
	SetRenderTarget("DEFAULT");
	weaponplacement=weaponplacement~[
		[GetCommonData("PLACEMENT")[1], placementmovex, placementmovey, placementmoveang]
	];
	SetCommonData("WEAPONPLACEMENT", weaponplacement);
	SetCommonData("PLACEMENT", [0, 0]);
}
sub restartrendertargets{
	ascent(i in 0..length(vehiclelist)){
		ClearRenderTarget(vehiclelist[i]);
		SetRenderTarget(vehiclelist[i]);
			SetTexture(vehicleimg[i]);
			SetGraphicRect(0, 0, 256, 256);
			SetGraphicScale(1, 1);
			SetGraphicAngle(0, 0, 0);
			SetAlpha(255);
			SetColor(255, 255, 255);
			DrawGraphic(256, 256);
		SetRenderTarget("DEFAULT");
	}
	SetCommonData("RESTARTPLACEMENT", "FALSE");
	SetCommonData("WEAPONPLACEMENT", []);
	placementmovex=0;
	placementmovey=0;
	placementmoveang=0;
}
task lifebar{
	yield;
	let bar=Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(bar, Cbar);
	ObjEffect_SetPrimitiveType(bar, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_SetLayer(bar, 8);
	ObjEffect_CreateVertex(bar, 4);
	ObjEffect_SetVertexUV(bar, 0, 0, 0);
	ObjEffect_SetVertexUV(bar, 1, 200, 0);
	ObjEffect_SetVertexUV(bar, 2, 0, 30);
	ObjEffect_SetVertexUV(bar, 3, 200, 30);
	ObjEffect_SetVertexXY(bar, 0, GetClipMaxX+100, GetCenterY-55);
	ObjEffect_SetVertexXY(bar, 1, GetClipMaxX+220, GetCenterY-55);
	ObjEffect_SetVertexXY(bar, 2, GetClipMaxX+100, GetCenterY-40);
	ObjEffect_SetVertexXY(bar, 3, GetClipMaxX+220, GetCenterY-40);
	ascent(i in 0..4){ObjEffect_SetVertexColor(bar, i, 255, 255, 255, 64);}
	let barb=Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(barb, Cbarb);
	ObjEffect_SetPrimitiveType(barb, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_SetLayer(barb, 8);
	ObjEffect_CreateVertex(barb, 4);
	ObjEffect_SetVertexUV(barb, 0, 0, 0);
	ObjEffect_SetVertexUV(barb, 1, 200, 0);
	ObjEffect_SetVertexUV(barb, 2, 0, 30);
	ObjEffect_SetVertexUV(barb, 3, 200, 30);
	ObjEffect_SetVertexXY(barb, 0, GetClipMaxX+100, GetCenterY-55);
	ObjEffect_SetVertexXY(barb, 1, GetClipMaxX+100, GetCenterY-55);
	ObjEffect_SetVertexXY(barb, 2, GetClipMaxX+100, GetCenterY-40);
	ObjEffect_SetVertexXY(barb, 3, GetClipMaxX+100, GetCenterY-40);
	ascent(i in 0..4){ObjEffect_SetVertexColor(barb, i, 255, 255, 255, 64);}
	while(Obj_BeDeleted(bar)==false){
		yield;
		ObjEffect_SetVertexXY(barb, 1, GetClipMaxX+100+120*(damage/GetCommonData("VEHICLES")[vehiclenum][3][0]), GetCenterY-55);
		ObjEffect_SetVertexXY(barb, 3, GetClipMaxX+100+120*(damage/GetCommonData("VEHICLES")[vehiclenum][3][0]), GetCenterY-40);
	}
}

task radarinit{
	let radar=Obj_Create(OBJ_EFFECT);
	let enemy_target;
	let distx=0;
	let disty=0;
	ObjEffect_SetTexture(radar, "RADAR");
	ObjEffect_SetPrimitiveType(radar, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_SetLayer(radar, 8);
	ObjEffect_CreateVertex(radar, 4);
	ObjEffect_SetVertexUV(radar, 0, 0, 0);
	ObjEffect_SetVertexUV(radar, 1, 256, 0);
	ObjEffect_SetVertexUV(radar, 2, 0, 256);
	ObjEffect_SetVertexUV(radar, 3, 256, 256);
	ObjEffect_SetVertexXY(radar, 0, -128, -128);
	ObjEffect_SetVertexXY(radar, 1, 128, -128);
	ObjEffect_SetVertexXY(radar, 2, -128, 128);
	ObjEffect_SetVertexXY(radar, 3, 128, 128);
	Obj_SetPosition(radar, GetClipMaxX+110, GetCenterY+110);
	ObjEffect_SetScale(radar, 0.7, 0.7);
	while(Obj_BeDeleted(radar)==false){
		ClearRenderTarget("RADAR");
		SetRenderTarget("RADAR");
			SetTexture(radarimg);
			SetGraphicRect(0, 0, 256, 256);
			SetGraphicScale(1, 1);
			SetGraphicAngle(0, 0, 0);
			SetAlpha(255);
			SetColor(255, 255, 255);
			DrawGraphic(128, 128);
			SetTexture(Cbarb);
			SetGraphicRect(0, 0, 4, 4);
			SetColor(255, 64, 64);
			ascent(i in EnumEnemyBegin..EnumEnemyEnd){
				enemy_target=EnumEnemyGetID(i);
				distx=GetPlayerX-GetEnemyInfo(enemy_target, ENEMY_X);
				disty=GetPlayerY-GetEnemyInfo(enemy_target, ENEMY_Y);
				DrawGraphic(128-distx/5, 128-disty/5);
			}
		SetRenderTarget("DEFAULT");
		ObjEffect_SetTexture(radar, "RADAR");
		yield;
	}
}

	@Initialize{
		SetSpeed(3.5, 1.5);
		ascent(i in 0..length(vehicleimg)){LoadGraphic(vehicleimg[i]);}
		ascent(i in 0..length(weaponimg)){
			LoadGraphic(weaponimg[i]);
		}
		LoadGraphic(radarimg);
		SetCommonData("VEHICLE", "NONE");
		SetCommonData("PLACEMENT", [0, 0, 0]);
		SetCommonData("PLACEMENTB", [0, 0, 0]);
		CreateRenderTarget("RADAR", 256, 256);
		ascent(i in 0..length(vehiclelist)){
			CreateRenderTarget(vehiclelist[i], 512, 512);
			SetRenderTarget(vehiclelist[i]);
				SetTexture(vehicleimg[i]);
				SetGraphicRect(0, 0, 256, 256);
				SetGraphicScale(1, 1);
				SetGraphicAngle(0, 0, 0);
				SetAlpha(255);
				SetColor(255, 255, 255);
				DrawGraphic(256, 256);
			SetRenderTarget("DEFAULT");
		}
		
		LoadGraphic(Cbar);
		LoadGraphic(Cbarb);
		LoadPlayerShotData(GCSD~"shot_sheet.txt");
		LoadGraphic(lifeimg);
		LoadGraphic(helperimg);
		SetPlayerLifeImage(lifeimg, 0, 0, 32, 32);
		CreateHitbox;
		SetInitialBombCount(0);
		SetAutoEffect(false);
		lifebar;
		helper;
	}
	@MainLoop{
		if(GetCommonData("PLACEMENT")[0]==1){
			weaponplacing;
			SetCommonData("PLACEMENT", [2, GetCommonData("PLACEMENT")[1]]);
		}
		if(GetCommonData("STAGESTART")=="TRUE"){
			shotframes=[];
			ascent(i in 0..length(GetCommonData("WEAPONPLACEMENT"))){
				shotframes=shotframes~[-1];
			}
			SetCommonData("STAGESTART", "DONE");
			inplay=1;
			radarinit;
		}
		if(inplay==1){
			ascent(i in 0..length(shotframes)){
				if(GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SHOT)==KEY_HOLD){
					if(shotframes[i]==-1){
						shotframes[i]=0;
					}
				}
				if(shotframes[i]>=0){shotframes[i]=shotframes[i]+1;}
				currentweapon=GetCommonData("WEAPONPLACEMENT")[i];
				///////////////////////////////////////////////////////////WEAPONS
				if(currentweapon[0]==0){////////////////////////Cucumber Gun
					if(shotframes[i]==15){
						shotframes[i]=-1;
						CucumberGun(GetPlayerX+currentweapon[1], GetPlayerY+currentweapon[2], currentweapon[3]-90);
					}
				}
				if(currentweapon[0]==1){//////////////////////////////Cattail Gun
					if(shotframes[i]==8){
						shotframes[i]=-1;
						CattailGun(GetPlayerX+currentweapon[1], GetPlayerY+currentweapon[2], currentweapon[3]-90);
					}
				}
				///////////////////////////////////////////////////////ATTACKS END
			}
		}
		if(GetCommonData("STAGESTART")=="END"){inplay=0;}
		if(GetCommonData("RESTARTPLACEMENT")=="TRUE"){
			restartrendertargets;
		}
		if(GetCommonData("VEHICLE")=="NITORICYCLE"){vehiclenum=0;}
		if(GetCommonData("VEHICLE")=="NITORITRUCK"){vehiclenum=1;}
		if(GetCommonData("VEHICLE")=="NITORISUB"){vehiclenum=2;}
		if(GetCommonData("VEHICLE")=="NITORICOPTER"){vehiclenum=3;}
		if(GetCommonData("VEHICLE")=="NITORIJET"){vehiclenum=4;}
		if(GetCommonData("VEHICLE")=="NITORITANK"){vehiclenum=5;}
		if(GetCommonData("VEHICLE")=="NITORIBOT"){vehiclenum=6;}
		if(damage>=GetCommonData("VEHICLES")[vehiclenum][3][0]){immunity=0; ShootDownPlayer;}
		if(immunity>1){immunity--;}
		if(immunity==1){SetIntersectionCircle(GetPlayerX, GetPlayerY, 0);}
		if(damage>0 && inplay==1){damagerefresh++;}else{damagerefresh=0;}
		if(damagerefresh%60==0 && damagerefresh>0){damage--;}
		SetCommonData("DAMAGE", damage);
		SetSpeed(GetCommonData("VEHICLES")[vehiclenum][4][0]/10, GetCommonData("VEHICLES")[vehiclenum][5][0]/10);
		SetCommonData("DAMAGERATE", damagerate);
		yield;
	}
	@Missed{
		damage+=ceil(25*GetCommonData("HANDICAP")/100);
		SetCommonData("MISSED", "TRUE");
		if(immunity==1){
			AddBomb(1);
			CallSpellCard;
		}
	}
	@SpellCard{
		if(OnMissed){
			UseSpellCard("DeathImmunity", 0);
			immunity=31;
		}
	}
	@DrawLoop{
		SetTexture(GetCommonData("VEHICLE"));
		SetGraphicRect(0, 0, 512, 512);
		SetGraphicScale(0.25, 0.25);
		if(inplay==1){
			if(GetKeyState(VK_LEFT)==KEY_HOLD){SetGraphicAngle(0, 0, -5);}
			else if(GetKeyState(VK_RIGHT)==KEY_HOLD){SetGraphicAngle(0, 0, 5);}
			else{SetGraphicAngle(0, 0, 0);}
		}else{
			SetGraphicAngle(0, 0, 0);
		}
		SetAlpha(255);
		SetColor(255, 255, 255);
		DrawGraphic(GetPlayerX, GetPlayerY);
		
		if(placementdrawstate==1){
			SetTexture(weaponimg[GetCommonData("PLACEMENT")[1]]);
			SetGraphicRect(0, 0, 64, 64);
			SetGraphicScale(0.65, 0.65);
			SetAlpha(255);
			SetColor(255, 255, 255);
			SetGraphicAngle(0, 0, placementmoveang);
			DrawGraphic(GetPlayerX+placementmovex, GetPlayerY+placementmovey);
		}
		if(inplay==1){
			DrawHitbox;
		}
	}
	@Finalize{
	}
	
	//lol hitbox
	
	let objHitbox = NULL;
	let objHitboxCopy = NULL;
	let objHitboxGraphic = GCSD~"img\Hitbox.png";
	let objHitboxScale = 0;
	let objHitboxAngle = 0;
	let objHitboxAngleVel = 103;
	let objHitboxCopyScale = 1.45;
	
	sub CreateHitbox{
		LoadGraphic(objHitboxGraphic);
		objHitbox = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(objHitbox, objHitboxGraphic);
		ObjEffect_SetRenderState(objHitbox, ALPHA);
		ObjEffect_SetLayer(objHitbox, 4);
		ObjEffect_SetPrimitiveType(objHitbox, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(objHitbox, 4);
		ObjEffect_SetVertexUV(objHitbox, 0, 0, 0);
		ObjEffect_SetVertexUV(objHitbox, 1, 63, 0);
		ObjEffect_SetVertexUV(objHitbox, 2, 63, 63);
		ObjEffect_SetVertexUV(objHitbox, 3, 0, 63);
		ObjEffect_SetVertexXY(objHitbox, 0, -32, -32);
		ObjEffect_SetVertexXY(objHitbox, 1, 32, -32);
		ObjEffect_SetVertexXY(objHitbox, 2, 32, 32);
		ObjEffect_SetVertexXY(objHitbox, 3, -32, 32);
		objHitboxCopy = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(objHitboxCopy, objHitboxGraphic);
		ObjEffect_SetRenderState(objHitboxCopy, ALPHA);
		ObjEffect_SetLayer(objHitboxCopy, 4);
		ObjEffect_SetPrimitiveType(objHitboxCopy, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(objHitboxCopy, 4);
		ObjEffect_SetVertexUV(objHitboxCopy, 0, 0, 0);
		ObjEffect_SetVertexUV(objHitboxCopy, 1, 63, 0);
		ObjEffect_SetVertexUV(objHitboxCopy, 2, 63, 63);
		ObjEffect_SetVertexUV(objHitboxCopy, 3, 0, 63);
		ObjEffect_SetVertexXY(objHitboxCopy, 0, -32, -32);
		ObjEffect_SetVertexXY(objHitboxCopy, 1, 32, -32);
		ObjEffect_SetVertexXY(objHitboxCopy, 2, 32, 32);
		ObjEffect_SetVertexXY(objHitboxCopy, 3, -32, 32);
	}

	sub DrawHitbox{
		if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
		if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH){
		objHitboxCopyScale = 1.45;
		}
		if(objHitboxAngleVel>3){
		objHitboxAngleVel -= 5;
		}
		if(objHitboxScale<1){
		objHitboxScale += 0.2;
		}
		if(objHitboxCopyScale>1){
		objHitboxCopyScale-=0.05;
		}
		objHitboxAngle += objHitboxAngleVel;
		ObjEffect_SetAngle(objHitbox, 0, 0, objHitboxAngle);
		ObjEffect_SetScale(objHitbox, objHitboxScale, objHitboxScale);
		ObjEffect_SetAngle(objHitboxCopy, 0, 0, -objHitboxAngle);
		ObjEffect_SetScale(objHitboxCopy, objHitboxCopyScale, objHitboxCopyScale);
		Obj_SetPosition(objHitbox, GetPlayerX, GetPlayerY);
		Obj_SetPosition(objHitboxCopy, GetPlayerX, GetPlayerY);
		}else{
		if(objHitboxScale>0){
		objHitboxScale -= 0.2;
		}else{
		objHitboxScale = 0;
		}
		if(objHitboxCopyScale>0){
		objHitboxCopyScale -= 0.2;
		}else{
		objHitboxCopyScale = 0;
		}

		objHitboxAngleVel = 103;
		ObjEffect_SetAngle(objHitbox, 0, 0, objHitboxAngle);
		ObjEffect_SetScale(objHitbox, objHitboxScale, objHitboxScale);
		ObjEffect_SetAngle(objHitboxCopy, 0, 0, -objHitboxAngle);
		ObjEffect_SetScale(objHitboxCopy, objHitboxCopyScale, objHitboxCopyScale);
		}
	}

}
	
script_spell DeathImmunity{
	SetPlayerInvincibility(0);
	DeleteEnemyShotImmediatelyInCircle(SHOT, GetPlayerX, GetPlayerY, 20);
	End;
}