let isScore;

let historyG;
let historyC;
let historyB;

let historyGRA;
let historyCRA;
let historyBRA;

let historyGRB;
let historyCRB;
let historyBRB;

let historyGMA;
let historyCMA;
let historyBMA;

let historyGMB;
let historyCMB;
let historyBMB;

let historyGO;
let historyCO;
let historyBO;

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


function StartSpellHistory(No)
{
	LoadGraphic(csd ~ "..\img/border.png");

	MagicCircle(false);
	isScore = IsCommonDataAreaExists(NsScore);
		
	//ロード
	if(isScore == false)
	{
		LoadBossAreas();
	}
	 historyG = GetCommonDataDefaultEx(NsScore,NsSpellG[No],0);
	 historyC = GetCommonDataDefaultEx(NsScore,NsSpellC[No],0);
	 historyB = GetCommonDataDefaultEx(NsScore,NsSpellB[No],0);

	StartSpellHistoryEachPlayer(No);

}

function StartSpellHistoryEachPlayer(let No)
{
if(SelectedCharacter==DefinedPlayer1)
{
	 historyGRA = GetCommonDataDefaultEx(NsScore,NsSpellGRA[No],0);
	 historyCRA = GetCommonDataDefaultEx(NsScore,NsSpellCRA[No],0);
	 historyBRA = GetCommonDataDefaultEx(NsScore,NsSpellBRA[No],0);
}

if(SelectedCharacter==DefinedPlayer2)
{
	 historyGRB = GetCommonDataDefaultEx(NsScore,NsSpellGRB[No],0);
	 historyCRB = GetCommonDataDefaultEx(NsScore,NsSpellCRB[No],0);
	 historyBRB = GetCommonDataDefaultEx(NsScore,NsSpellBRB[No],0);
}

if(SelectedCharacter==DefinedPlayer3)
{
	 historyGMA = GetCommonDataDefaultEx(NsScore,NsSpellGMA[No],0);
	 historyCMA = GetCommonDataDefaultEx(NsScore,NsSpellCMA[No],0);
	 historyBMA = GetCommonDataDefaultEx(NsScore,NsSpellBMA[No],0);
}

if(SelectedCharacter==DefinedPlayer4)
{
	 historyGMB = GetCommonDataDefaultEx(NsScore,NsSpellGMB[No],0);
	 historyCMB = GetCommonDataDefaultEx(NsScore,NsSpellCMB[No],0);
	 historyBMB = GetCommonDataDefaultEx(NsScore,NsSpellBMB[No],0);
}

if(SelectedCharacter==DefinedPlayer5)
{
	 historyGO = GetCommonDataDefaultEx(NsScore,NsSpellGO[No],0);
	 historyCO = GetCommonDataDefaultEx(NsScore,NsSpellCO[No],0);
	 historyBO = GetCommonDataDefaultEx(NsScore,NsSpellBO[No],0);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
function EndSpellHistory(No)	//スペカ取得枚数と履歴
{
	if(GotSpellCardBonus)
	{
		if(isScore == true)
		{
			let getSpell = GetCommonData(NsGetSpell);
			getSpell++;
			SetCommonData(NsGetSpell,getSpell);
		}
		historyG++;
		if(Bonus>historyB)
		{
			historyB=Bonus;
		}

	}
	historyC++;

	SetCommonDataEx(NsScore,NsSpellG[No],historyG);
	SetCommonDataEx(NsScore,NsSpellC[No],historyC);
	SetCommonDataEx(NsScore,NsSpellB[No],historyB);

	EndSpellHistoryEachPlayer(No);

	//デリート
	if(isScore == false)
	{
		DeleteBossAreas();
	}

	DeleteGraphic(csd ~ "..\img/border.png");
}

function EndSpellHistoryEachPlayer(let No)//各々の機体でのスペカ取得枚数と履歴
{

if(SelectedCharacter==DefinedPlayer1)
{
	if(GotSpellCardBonus)
	{
		historyGRA++;
		if(Bonus>historyBRA)
		{
			historyBRA=Bonus;
		}
	}
	historyCRA++;
	SetCommonDataEx(NsScore,NsSpellGRA[No],historyGRA);
	SetCommonDataEx(NsScore,NsSpellCRA[No],historyCRA);
	SetCommonDataEx(NsScore,NsSpellBRA[No],historyBRA);
}

if(SelectedCharacter==DefinedPlayer2)
{
	if(GotSpellCardBonus)
	{
		historyGRB++;
		if(Bonus>historyBRB)
		{
			historyBRB=Bonus;
		}
	}
	historyCRB++;
	SetCommonDataEx(NsScore,NsSpellGRB[No],historyGRB);
	SetCommonDataEx(NsScore,NsSpellCRB[No],historyCRB);
	SetCommonDataEx(NsScore,NsSpellBRB[No],historyBRB);
}

if(SelectedCharacter==DefinedPlayer3)
{
	if(GotSpellCardBonus)
	{
		historyGMA++;
		if(Bonus>historyBMA)
		{
			historyBMA=Bonus;
		}
	}
	historyCMA++;
	SetCommonDataEx(NsScore,NsSpellGMA[No],historyGMA);
	SetCommonDataEx(NsScore,NsSpellCMA[No],historyCMA);
	SetCommonDataEx(NsScore,NsSpellBMA[No],historyBMA);
}

if(SelectedCharacter==DefinedPlayer4)
{
	if(GotSpellCardBonus)
	{
		historyGMB++;
		if(Bonus>historyBMB)
		{
			historyBMB=Bonus;
		}
	}
	historyCMB++;
	SetCommonDataEx(NsScore,NsSpellGMB[No],historyGMB);
	SetCommonDataEx(NsScore,NsSpellCMB[No],historyCMB);
	SetCommonDataEx(NsScore,NsSpellBMB[No],historyBMB);
}

if(SelectedCharacter==DefinedPlayer5)
{
	if(GotSpellCardBonus)
	{
		historyGO++;
		if(Bonus>historyBO)
		{
			historyBO=Bonus;
		}
	}
	historyCO++;
	SetCommonDataEx(NsScore,NsSpellGO[No],historyGO);
	SetCommonDataEx(NsScore,NsSpellCO[No],historyCO);
	SetCommonDataEx(NsScore,NsSpellBO[No],historyBO);
}

}
////////////////////////////////////////////////////////////////////////////////////////////
sub DrawSpellHistory
{
let Ghistorydrawnum=0;
let Chistorydrawnum=0;
if(SelectedCharacter==DefinedPlayer1)
{
	 Ghistorydrawnum=historyGRA;
	 Chistorydrawnum=historyCRA;
}

if(SelectedCharacter==DefinedPlayer2)
{
	 Ghistorydrawnum=historyGRB;
	 Chistorydrawnum=historyCRB;
}

if(SelectedCharacter==DefinedPlayer3)
{
	 Ghistorydrawnum=historyGMA;
	 Chistorydrawnum=historyCMA;
}

if(SelectedCharacter==DefinedPlayer4)
{
	 Ghistorydrawnum=historyGMB;
	 Chistorydrawnum=historyCMB;
}

if(SelectedCharacter==DefinedPlayer5)
{
	 Ghistorydrawnum=historyGO;
	 Chistorydrawnum=historyCO;
}
		if(count>=0)
		{
			if(GetPlayerY<=(GetCenterY+GetClipMinY)/2 && GetPlayerX>=GetCenterX)
			{
		//		DrawText("history",280,DrawY,12,100);
				SpellHistoryDisplay(DrawY,100,255,255,255);
		//		DrawText(NumToString(historyG),280,DrawY,12,100);
				SpellNumberDisPlay(359,DrawY,Ghistorydrawnum,100,255,255,255);
		//		DrawText("/",370,DrawY,12,100);
				SpellBerDisplay(DrawY,100,255,255,255);
		//		DrawText(NumToString(historyC+1),320,DrawY,12,100);
				SpellNumberDisPlay(399,DrawY,Chistorydrawnum+1,100,255,255,255);
			}
			else
			{
		//		DrawText("history",280,DrawY,12,255);
				SpellHistoryDisplay(DrawY,255,255,255,255);
		//		DrawText(NumToString(historyG),280,DrawY,12,255);
				SpellNumberDisPlay(359,DrawY,Ghistorydrawnum,255,255,255,255);
		//		DrawText("/",370,DrawY,12,255);
				SpellBerDisplay(DrawY,255,255,255,255);
		//		DrawText(NumToString(historyC+1),320,DrawY,12,255);
				SpellNumberDisPlay(399,DrawY,Chistorydrawnum+1,255,255,255,255);
			}
		}		
		if(count>=80 && count<=115)
		{
			DrawY-=7.5;
		}
		count++;
}
///////////////////////////////////////////////////////////////////////////////////////////
task SpellHistoryDisplay(y,Alpha,R,G,B)//historyを表示
{
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, csd ~ "..\img/border.png");
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	//	ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -48, -10);
		ObjEffect_SetVertexXY(obj, 1, 48,  -10);
		ObjEffect_SetVertexXY(obj, 2, 48, 10);
		ObjEffect_SetVertexXY(obj, 3,  -48,  10);
		
		ObjEffect_SetVertexUV(obj, 0,  3,  141);
		ObjEffect_SetVertexUV(obj, 1,  99, 141);
		ObjEffect_SetVertexUV(obj, 2, 99,  161);
		ObjEffect_SetVertexUV(obj, 3, 3, 161);

		ObjEffect_SetVertexColor(obj,0,Alpha,R,G,B);
		ObjEffect_SetVertexColor(obj,1,Alpha,R,G,B);
		ObjEffect_SetVertexColor(obj,2,Alpha,R,G,B);
		ObjEffect_SetVertexColor(obj,3,Alpha,R,G,B);

		ObjEffect_SetLayer(obj,2);
		Obj_SetPosition(obj,304,y+6);

		let ScaleX=0.5;
		let ScaleY=0.5;
		ObjEffect_SetScale(obj, ScaleX,ScaleY);

		if(GetLife>0)
		{
		yield;
		}
		Obj_Delete(obj);
}

task SpellNumberDisPlay(let x,let y,let number,Alpha,R,G,B)
{
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))
		{
			SpellNumLetterDisplay(x-(i-1)*7,y+5.5,num[i],Alpha,R,G,B);
		}
	}
	if(num[0]==0)
	{
		SpellNumLetterDisplay(x,y+5,0,Alpha,R,G,B);
	}
}

task SpellNumLetterDisplay(let x,let y,let num,Alpha,R,G,B)
{
		let scx=0.5;
		let scy=0.7;
		let obj0 = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj0, csd ~ "..\img/border.png");
		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_SetVertexColor(obj0,0,Alpha,R,G,B);
		ObjEffect_SetVertexColor(obj0,1,Alpha,R,G,B);
		ObjEffect_SetVertexColor(obj0,2,Alpha,R,G,B);
		ObjEffect_SetVertexColor(obj0,3,Alpha,R,G,B);
		
		ObjEffect_SetScale(obj0, scx,scy);
		Obj_SetPosition(obj0,x,y);
		ObjEffect_SetLayer(obj0,8);
		if(GetLife>0)
		{
		yield;
		}
		Obj_Delete(obj0);
}

task SpellBerDisplay(y,Alpha,R,G,B)//resultのNo.を表示
{
		let scx=0.5;
		let scy=0.5;
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj,  csd ~ "..\img/border.png");
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	//	ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -6, -11);
		ObjEffect_SetVertexXY(obj, 1, 6,  -11);
		ObjEffect_SetVertexXY(obj, 2, 6, 11);
		ObjEffect_SetVertexXY(obj, 3,  -6,  11);
		
		ObjEffect_SetVertexUV(obj, 0,  108,  141);
		ObjEffect_SetVertexUV(obj, 1,  120, 141);
		ObjEffect_SetVertexUV(obj, 2, 120,  163);
		ObjEffect_SetVertexUV(obj, 3, 108, 163);

		ObjEffect_SetVertexColor(obj,0,Alpha,R,G,B);
		ObjEffect_SetVertexColor(obj,1,Alpha,R,G,B);
		ObjEffect_SetVertexColor(obj,2,Alpha,R,G,B);
		ObjEffect_SetVertexColor(obj,3,Alpha,R,G,B);

		ObjEffect_SetLayer(obj,7);
		Obj_SetPosition(obj,373,y+5);

		ObjEffect_SetScale(obj, scx,scy);

		if(GetLife>0)
		{
		yield;
		}
		Obj_Delete(obj);
}
