sub MSDIntialize
{
	SetCommonData("MagicalSingularDay",true);

	SetCommonData("BURSTLEVEL",0);
	SetCommonData("BurstErazeCircleRangeRate",3);
	SetCommonData("MAGICCOUNTER",0);
	SetCommonData("MAGICPOWER",0);
	SetCommonData("MagicAbsorbShortRange",60);
	SetCommonData("MagicAbsorbLongRange",150);

	if(!IsCommonDataAreaExists(NsScore))
	{
	CreateCommonDataArea(NsScore);
	}
	SetCommonData(NsGetSpell,0);
	let isRep = IsReplay();
	LoadCommonDataEx(NsScore,savePathstage);

	if(isRep==true)
	{
		LoadCommonDataFromReplayFile;
		StageState=Loading;
	}
	SetCommonData(NsIsReplay,isRep);
	SetCommonData(NsIsReplayJudgment,isRep);

	UseAutoBombCount=GetCommonDataDefaultEx(NsScore,"UseAutoBombCount",0);
	BurstModeErazeBulletNum=GetCommonDataDefaultEx(NsScore,"BurstModeErazeBulletNumAM",0);

	StartDifficult=GetCommonDataDefaultEx(NsScore,"StartDifficult",0);
	StartStage=GetCommonDataDefaultEx(NsScore,"StartStage",0);
	StagePhaseProgress=GetCommonDataDefaultEx(NsScore,"StagePhaseProgressConfig",0);
	PracticeStartBurstCounter=GetCommonDataDefaultEx(NsScore,"PracticeStartBurstCounter",0);
	PracticeStartMagicCounter=GetCommonDataDefaultEx(NsScore,"PracticeStartMagicCounter",0);
	PracticeStartBurstRank=GetCommonDataDefaultEx(NsScore,"PracticeStartBurstRank",0);
	PracticeStartRushGauge=GetCommonDataDefaultEx(NsScore,"PracticeStartRushGauge",0);


	BGDrawing=GetCommonDataDefaultEx(NsScore,"BGDrawing",1);
	LightMode=GetCommonDataDefaultEx(NsScore,"LightMode",0);
	LightMode_HiscoreDraw=GetCommonDataDefaultEx(NsScore,"Set_LightMode_HiscoreDraw",0);
	LightMode_PlayerIntervalEffect=GetCommonDataDefaultEx(NsScore,"Set_LightMode_PlayerIntervalEffect",0);
	LightMode_ShotEffect=GetCommonDataDefaultEx(NsScore,"Set_LightMode_ShotEffect",0);
	LightMode_BombEffect=GetCommonDataDefaultEx(NsScore,"Set_LightMode_BombEffect",0);
	LightMode_PlayerBlur=GetCommonDataDefaultEx(NsScore,"Set_LightMode_PlayerBlur",0);
	LightMode_BossBlur=GetCommonDataDefaultEx(NsScore,"Set_LightMode_BossBlur",0);
	LightMode_ElementalBlur=GetCommonDataDefaultEx(NsScore,"Set_LightMode_ElementalBlur",0);
	LightMode_SpecialEffect=GetCommonDataDefaultEx(NsScore,"Set_LightMode_SpecialEffect",0);
	LightMode_BreakEffect=GetCommonDataDefaultEx(NsScore,"Set_LightMode_BreakEffect",0);

	AutoBombMode=GetCommonDataDefaultEx(NsScore,"AutoBombMode",0);
	InitialLife=GetCommonDataDefaultEx(NsScore,"InitialLife",0);
	InitialBomb=GetCommonDataDefaultEx(NsScore,"InitialBomb",0);

	StageTask();
	StageInitializeAchievementCheck;
}

sub MSDMainLoop
{
//DeleteGraphic("img\Shot.png");
//DeleteGraphic("img\Item.png");
//DeleteGraphic("img\Effect.png");
//DeleteSE("se\seEnemyExplode01.wav");
//LoadSE("se\seEnemyExplode01.wav");
//SaveSnapShot("snapshot\"~snapFile[photoNo%10]~".bmp",0,0,448,480);
	yield;
	SlowModeJudgRShot1=GetCommonDataDefault("BurstBulletNum",0);//+GetEnemyShotCountEx(GetCenterX,GetCenterY,400,5)+GetEnemyShotCountEx(GetCenterX,GetCenterY,400,2)+GetEnemyShotCountEx(GetCenterX,GetCenterY,400,1);
	//SlowModeJudgRShot2=GetEnemyShotCountEx(GetPlayerX,GetPlayerY,15,5)+GetEnemyShotCountEx(GetPlayerX,GetPlayerY,15,2)+GetEnemyShotCountEx(GetPlayerX,GetPlayerY,15,1);
/*	SlowModeJudgRShot2=GetEnemyShotCountEx(GetPlayerX,GetPlayerY,400,2);
	SlowModeJudgRShot3=GetEnemyShotCountEx(GetCenterX,GetCenterY,400,1);
*/
//	if( ( SlowModeJudgRShot1>=WaitBullet || SlowModeJudgRShot2>0 ) && WaitSystem)
/*
	if(SlowModeJudgRShot1>=WaitBullet && WaitSystem)
	{
		Slow(1);
	}
	else
	{
		Slow(0);
	}
*/
	AddGraze(-GetGraze);
	if(OnPlayerMissed || GetPlayerLife==0)
	{
		SetCommonData("Playermissed",true);
	}
	else
	{
		SetCommonData("Playermissed",false);
	}

}
//let XXX=[];
//let rrr=0+MSDrand(0,102)+MSDrand(0,102);
//let rt=0;
sub MSDDrawLoop
{
	SetTexture(imgBG);
	//SetAlpha(100);
	SetGraphicScale(30.0,30.0);
	SetGraphicRect(0,0,16,16);
	if(StageState==Menu || StageState==Loading || StageState==Exit)
	{
		SetGraphicAngle(0, 0, 0);
		SetViewTo(0,0,0);
		DrawGraphic(GetCenterX, GetCenterY);
	}
	//DrawText(StartStage,320,432,12,255);//起動時間の表示
	//DrawText(GetCommonDataDefaultEx(NsScore,"LOWSPIRALNUM",0),320,452,12,255);//プレイ時間の表示
	//DrawText(UseAutoBombCount,120,432,12,255);//起動時間の表示
	//DrawText(GetCommonDataDefaultEx(NsScore,NsContinueCount,0),120,452,12,255);//プレイ時間の表示
	//DrawText(ItemCountCheck2X,120,202,12,255);//起動時間の表示
	//DrawText(GetCommonDataDefaultEx(NsScore,"NsAllDeadNum",0),220,202,12,255);//起動時間の表示
	//DrawText(BurstModeErazeBulletNum,120,412,12,255);//起動時間の表示
	////DrawText(GetCommonData("MagicAbsorbShortRange"),120,412,12,255);//起動時間の表示
	//DrawText(GetCommonDataDefaultEx(NsScore,NsStageHiScoreEasyName[0],0),120,322,12,255);//起動時間の表示
	//DrawText(GetCommonDataDefault("BalanceBreak",false),40,382,10,255);//起動時間の表示
	//DrawText(StartDifficult,40,382,10,255);//起動時間の表示
	//DrawText(GetCommonDataDefaultEx(NsScore,NsHiScoreEasyName[0],0),120,427,12,255);//起動時間の表示
	//XXX=XXX~[0];
	MenuDraw;
}

sub MSDFinalize
{
	if(GetCommonDataDefaultEx(NsScore,"SelectedDifficult","None")=="None" && Continued==false)
	{
		if(ScoreNumber2>=1)
		{
			HiScoreSave;
		}
	}

	StageFinalizeAchievementCheck;

	UseAutoBombCount+=GetCommonDataDefault("UseAutoBombCount",0);	
	SetCommonDataEx(NsScore,"UseAutoBombCount",UseAutoBombCount);

	SetCommonDataEx(NsScore,"ExFlag","true");
	SetCommonDataEx(NsScore,NsUpTime,UpTime);
	SetCommonDataEx(NsScore,NsPlayTime,PlayTime);

	let AllScoreFinalize=GetCommonDataDefaultEx(NsScore,NsAllScore,0);
	let AllScoreFinalize2=GetCommonDataDefaultEx(NsScore,NsAllScore2,0);
	let AllScoreFinalize3=GetCommonDataDefaultEx(NsScore,NsAllScore3,0);
	if(AllScoreFinalize>=1000000)
	{
		AllScoreFinalize2+=1;
		AllScoreFinalize-=1000000;
	}
	if(AllScoreFinalize2>=1000000)
	{
		AllScoreFinalize3+=1;
		AllScoreFinalize2-=1000000;
	}

	SetCommonDataEx(NsScore,NsAllScore,AllScoreFinalize+ScoreNumber1);
	SetCommonDataEx(NsScore,NsAllScore2,AllScoreFinalize2+ScoreNumber2);
	SetCommonDataEx(NsScore,NsAllScore3,AllScoreFinalize3+ScoreNumber3);

	let AllFragmentFinalize=GetCommonDataDefaultEx(NsScore,NsAllFragment,0);
	let AllFragmentFinalize2=GetCommonDataDefaultEx(NsScore,NsAllFragment2,0);
	if(AllFragmentFinalize>=1000000)
	{
		AllFragmentFinalize2+=1;
		AllFragmentFinalize-=1000000;
	}

	SetCommonDataEx(NsScore,NsAllFragment,AllFragmentFinalize+GetCommonDataDefault("MAGICFRAGMENT",0));
	SetCommonDataEx(NsScore,NsAllFragment2,AllFragmentFinalize2);

	let spiralnum=GetCommonDataDefaultEx(NsScore,"LOWSPIRALNUM",0);
	if(GetCommonDataDefault("SPIRALJUDG",0)==1 && StagePractice==false)
	{
		SetCommonDataEx(NsScore,"LOWSPIRALNUM",spiralnum+1);
	}
	else if(StageProgress!=1 || StagePractice==true)
	{
		SetCommonDataEx(NsScore,"LOWSPIRALNUM",0);
	}
			
	SetCommonDataEx(NsScore,"BurstModeErazeBulletNumAM",BurstModeErazeBulletNum);
	
	let DyingNum=GetCommonDataDefaultEx(NsScore,"NsAllDeadNum",0);
	SetCommonDataEx(NsScore,"NsAllDeadNum",DyingNum+GetMissCount);

	SetCommonDataEx(NsScore,"StartDifficult",StartDifficult);
	SetCommonDataEx(NsScore,"StartStage",StartStage);
	SetCommonDataEx(NsScore,"StagePhaseProgressConfig",StagePhaseProgress);
	SetCommonDataEx(NsScore,"PracticeStartBurstCounter",PracticeStartBurstCounter);
	SetCommonDataEx(NsScore,"PracticeStartMagicCounter",PracticeStartMagicCounter);
	SetCommonDataEx(NsScore,"PracticeStartBurstRank",PracticeStartBurstRank);
	SetCommonDataEx(NsScore,"PracticeStartRushGauge",PracticeStartRushGauge);

	SetCommonDataEx(NsScore,"BGDrawing",BGDrawing);
	SetCommonDataEx(NsScore,"LightMode",LightMode);

	SetCommonDataEx(NsScore,"Set_LightMode_HiscoreDraw",LightMode_HiscoreDraw);
	SetCommonDataEx(NsScore,"Set_LightMode_PlayerIntervalEffect",LightMode_PlayerIntervalEffect);
	SetCommonDataEx(NsScore,"Set_LightMode_ShotEffect",LightMode_ShotEffect);
	SetCommonDataEx(NsScore,"Set_LightMode_BombEffect",LightMode_BombEffect);
	SetCommonDataEx(NsScore,"Set_LightMode_PlayerBlur",LightMode_PlayerBlur);
	SetCommonDataEx(NsScore,"Set_LightMode_BossBlur",LightMode_BossBlur);
	SetCommonDataEx(NsScore,"Set_LightMode_ElementalBlur",LightMode_ElementalBlur);
	SetCommonDataEx(NsScore,"Set_LightMode_SpecialEffect",LightMode_SpecialEffect);
	SetCommonDataEx(NsScore,"Set_LightMode_BreakEffect",LightMode_BreakEffect);

	SetCommonDataEx(NsScore,"AutoBombMode",AutoBombMode);
	SetCommonDataEx(NsScore,"InitialLife",InitialLife);
	SetCommonDataEx(NsScore,"InitialBomb",InitialBomb);

	StageHiscoreSave(StageProgress);
	if(GetCommonDataDefault(NsIsReplay,true) == false)
	{
		SaveCommonDataEx(NsScore,savePathstage);
	}

	ClearCommonDataEx(NsScore);
	DeleteCommonData(NsGetSpell);
	DeleteCommonData(NsDefeatFrame);
	DeleteCommonData(NsDefeatPosX);
	DeleteCommonData(NsDefeatPosY);
	DeleteCommonData(NsBossFrame);
	DeleteCommonData(NsIsReplay);
	AddScore(-GetScore);
	AddScore(ScoreNumber2);
}