// w フレームだけ待つ
function wait(w) 
{
    loop(w) { yield; }
}

function WaitForZeroEnemy()//敵数が0になるまで待機する関数
{
	while(GetEnemyNum() != 0){yield;}
}

function ItemSet(x,y,type)//アイテム生成関数
{
	let XY=GetCommonDataDefault("ItemXY",[]);//共通データ内容を変数に渡す
	XY=XY~[x,y,type];//
	SetCommonData("ItemXY",XY);//共通データに変更後の配列を渡す
}

///////////////////////////////////////////////////////////////////////////////////////////
	let csd = GetCurrentScriptDirectory;
	let imgLetter =csd ~ "img/border.png"; 
	let DefinedPlayer1="Reimu.txt";
	let DefinedPlayer2="REIMU_B";
	let DefinedPlayer3="Marisa.txt";
	let DefinedPlayer4="MARISA_B";
	let DefinedPlayer5="USER_PLAYER";
	//let DefinedPlayer6=

	let DrawDefinedPlayer1="博麗霊夢　A";
	let DrawDefinedPlayer2="博麗霊夢　B";
	let DrawDefinedPlayer3="霧雨魔理沙　A";
	let DrawDefinedPlayer4="霧雨魔理沙　B";

	let SelectedCharacter=GetPlayerScriptName;
////////////////////////////////////////////////////////////////////////////////////////////

//スコアのセーブファイル

let savePathstage = GetCurrentScriptDirectory~"SaveData.dat";//ステージで読み込む
let savePath = GetCurrentScriptDirectory~"..\SaveData.dat";//スペルで読み込む

let NsSpellG=[];
let NsSpellC=[];
let NsSpellB=[];

let NsSpellGRA=[];
let NsSpellCRA=[];
let NsSpellBRA=[];

let NsSpellGRB=[];
let NsSpellCRB=[];
let NsSpellBRB=[];

let NsSpellGMA=[];
let NsSpellCMA=[];
let NsSpellBMA=[];

let NsSpellGMB=[];
let NsSpellCMB=[];
let NsSpellBMB=[];

let NsSpellGO=[];
let NsSpellCO=[];
let NsSpellBO=[];


ascent(let k in 0..150)
{
NsSpellG = NsSpellG ~ [k];
NsSpellC = NsSpellC ~ [k];
NsSpellB = NsSpellB ~ [k];

NsSpellGRA = NsSpellGRA ~ [k];
NsSpellCRA = NsSpellCRA ~ [k];
NsSpellBRA = NsSpellBRA ~ [k];

NsSpellGRB = NsSpellGRB ~ [k];
NsSpellCRB = NsSpellCRB ~ [k];
NsSpellBRB = NsSpellBRB ~ [k];

NsSpellGMA = NsSpellGMA ~ [k];
NsSpellCMA = NsSpellCMA ~ [k];
NsSpellBMA = NsSpellBMA ~ [k];

NsSpellGMB = NsSpellGMB ~ [k];
NsSpellCMB = NsSpellCMB ~ [k];
NsSpellBMB = NsSpellBMB ~ [k];

NsSpellGO = NsSpellGO ~ [k];
NsSpellCO = NsSpellCO ~ [k];
NsSpellBO = NsSpellBO ~ [k];
}

/////////////////////////////////////////////////////////////////////////////////////////////
//名前空間
let NsScore = "SAVE_SCORE";	//スコア
let NsHi = "HI_SCORE";	//ハイスコア(NsScore)

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

ascent(let k in 0..150)
{
NsSpellG[k] = k;		//スペカNO.k取得履歴
NsSpellC[k] = k*10000;		//スペカNO.k挑戦履歴
NsSpellB[k] = k*100000000;	//スペカNO.k最大ボーナス履歴

NsSpellGRA[k] = k+1000;		//スペカNO.k取得履歴
NsSpellCRA[k] = k*10000+1000;		//スペカNO.k挑戦履歴
NsSpellBRA[k] = k*100000000+1000;	//スペカNO.k最大ボーナス履歴

NsSpellGRB[k] = k+2000;		//スペカNO.k取得履歴
NsSpellCRB[k] = k*10000+2000;		//スペカNO.k挑戦履歴
NsSpellBRB[k] = k*100000000+2000;	//スペカNO.k最大ボーナス履歴

NsSpellGMA[k] = k+3000;		//スペカNO.k取得履歴
NsSpellCMA[k] = k*10000+3000;		//スペカNO.k挑戦履歴
NsSpellBMA[k] = k*100000000+3000;	//スペカNO.k最大ボーナス履歴

NsSpellGMB[k] = k+4000;		//スペカNO.k取得履歴
NsSpellCMB[k] = k*10000+4000;		//スペカNO.k挑戦履歴
NsSpellBMB[k] = k*100000000+4000;	//スペカNO.k最大ボーナス履歴

NsSpellGO[k] = k+5000;		//スペカNO.k取得履歴
NsSpellCO[k] = k*10000+5000;		//スペカNO.k挑戦履歴
NsSpellBO[k] = k*100000000+5000;	//スペカNO.k最大ボーナス履歴
}

let NsGetSpell = "GET_SPELL";	//スペカ取得枚数(Common)
let NsIsReplay = "IS_REPLAY";	//リプレイかどうか(Common)

let NsDefeatFrame = "DEFEAT_FRAME";	//撃破フレーム(Common)
let NsDefeatPosX = "DEFEAT_POSX";	//撃破位置(Common)
let NsDefeatPosY = "DEFEAT_POSY";	//撃破位置(Common)
let NsBossFrame = "BOSS_FRAME";	//ボス攻撃開始からのフレーム(Common)

///////////////////////////////////////////////////////////////////////////////////////////////

//ボス再生に使用するコモンエリアのロード
function LoadBossAreas()
{
	CreateCommonDataArea(NsScore);
	LoadCommonDataEx(NsScore,savePath);
	SetCommonData(NsGetSpell,0);
	let isRep = IsReplay();
	SetCommonData(NsIsReplay,isRep);
}



//ボス再生に使用するコモンエリアのセーブ、削除
function DeleteBossAreas()
{
	if(GetCommonDataDefault(NsIsReplay,true) == false)
	{
		SaveCommonDataEx(NsScore,savePath);
	}
	ClearCommonDataEx(NsScore);
	DeleteCommonData(NsGetSpell);
	DeleteCommonData(NsDefeatFrame);
	DeleteCommonData(NsDefeatPosX);
	DeleteCommonData(NsDefeatPosY);
	DeleteCommonData(NsBossFrame);
	DeleteCommonData(NsIsReplay);
}


	function NumToString(let num)
	{
		if(num==0)
		{
			num=0.1;
		}
		let leng = trunc(log10(num))+1;//整数部分の桁数を求める
		let string = ToString(num);//数字を文字列に変える
		let str = " ";
			times(12-leng)
			{
				string = str ~ string;//桁揃え用のスペースを先頭につける。
			} 
		return string[0..12];//文字列をスライシングして返す
	}

////////////////////////////////////////////////////////////////////////////////////////////
task DisPlayNumber(let x,let y,let number,let number2)
{
let num=[];
ascent(let k in 0..11)
{
num = num ~ [k];
}
	num[0]=trunc(number);
	num[1]=trunc(number%10);
	num[2]=trunc(number%100/10);
	num[3]=trunc(number%1000/100);
	num[4]=trunc(number%10000/1000);
	num[5]=trunc(number%100000/10000);
	num[6]=trunc(number%1000000/100000);
	num[7]=trunc(number%10000000/1000000);
	num[8]=trunc(number%100000000/10000000);
	num[9]=trunc(number%1000000000/100000000);
	num[10]=trunc(number%10000000000/1000000000);


	ascent(let i in 1..10)
	{
		if(num[0]>=10^(i-1))
		{
			DisplayNumLetter(x-(i-1)*12,y,num[i]);
		}
	}
	if(num[0]==0)
	{
		DisplayNumLetter(x,y,0);
	}
}

task DisplayNumLetter(let x,let y,let num)
{
		let scx=0.8;
		let scy=1;
		let obj0 = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj0,imgLetter);
		ObjEffect_SetPrimitiveType(obj0, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj0, ADD); 
		ObjEffect_CreateVertex(obj0, 4);
		ObjEffect_SetVertexXY(obj0, 0, -7, -8);
		ObjEffect_SetVertexXY(obj0, 1, 7,  -8);
		ObjEffect_SetVertexXY(obj0, 2, 7, 8);
		ObjEffect_SetVertexXY(obj0, 3,  -7,  8);
		
		ObjEffect_SetVertexUV(obj0, 0,  2+14*num,  114);
		ObjEffect_SetVertexUV(obj0, 1,  14+14*num, 114);
		ObjEffect_SetVertexUV(obj0, 2, 14+14*num,  131);
		ObjEffect_SetVertexUV(obj0, 3, 2+14*num, 131);
		
		ObjEffect_SetScale(obj0, scx,scy);
		Obj_SetPosition(obj0,x,y);
		ObjEffect_SetLayer(obj0,8);
		wait(1);
		Obj_Delete(obj0);
}

task DisPlayGetItemScore(let x,let y,let number,let color)
{
let num=[];
ascent(let k in 0..11)
{
num = num ~ [k];
}
	num[0]=trunc(number);
	num[1]=trunc(number%10);
	num[2]=trunc(number%100/10);
	num[3]=trunc(number%1000/100);
	num[4]=trunc(number%10000/1000);
	num[5]=trunc(number%100000/10000);
	num[6]=trunc(number%1000000/100000);
	num[7]=trunc(number%10000000/1000000);
	num[8]=trunc(number%100000000/10000000);
	num[9]=trunc(number%1000000000/100000000);
	num[10]=trunc(number%10000000000/1000000000);


	ascent(let i in 1..10)
	{
		if(num[0]>=10^(i-1))
		{
			DisplayGetItemScoreLetter(x-(i-1)*6,y,num[i],color);
		}
	}
	if(num[0]==0)
	{
		DisplayGetItemScoreLetter(x,y,0,color);
	}
}

task DisplayGetItemScoreLetter(let x,let y,let num,let color)
{
		let scx=0.5;
		let scy=0.5;
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj,imgLetter);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -7, -8);
		ObjEffect_SetVertexXY(obj, 1, 7,  -8);
		ObjEffect_SetVertexXY(obj, 2, 7, 8);
		ObjEffect_SetVertexXY(obj, 3,  -7,  8);
		
		ObjEffect_SetVertexUV(obj, 0,  2+14*num,  114);
		ObjEffect_SetVertexUV(obj, 1,  14+14*num, 114);
		ObjEffect_SetVertexUV(obj, 2, 14+14*num,  131);
		ObjEffect_SetVertexUV(obj, 3, 2+14*num, 131);

if(color=="white")
{
		ObjEffect_SetVertexColor(obj,0,100,255,255,255);
		ObjEffect_SetVertexColor(obj,1,100,255,255,255);
		ObjEffect_SetVertexColor(obj,2,100,255,255,255);
		ObjEffect_SetVertexColor(obj,3,100,255,255,255);
}

if(color=="yellow")
{
		ObjEffect_SetVertexColor(obj,0,100,255,255,150);
		ObjEffect_SetVertexColor(obj,1,100,255,255,150);
		ObjEffect_SetVertexColor(obj,2,100,255,255,150);
		ObjEffect_SetVertexColor(obj,3,100,255,255,150);
}
		
		ObjEffect_SetScale(obj, scx,scy);
		Obj_SetPosition(obj,x,y);
		ObjEffect_SetLayer(obj,8);
		wait(30);
		Obj_Delete(obj);
}