task PointExtend(){
let ExtendPoint = [500,1500,2750,4500,6000,8000,9999,3333,6666,9999];//エクステンドの点符必要数の羅列
let ExtendCount = 0;				//エクステンド回数を記憶する変数
let Point = GetPoint;				//Pointの値を格納する変数

loop{
Point = GetPoint;				//取得数を毎フレーム確認
SetNormPoint(ExtendPoint[ExtendCount]);		//次のエクステンドの数を毎フレーム表示
if(Point >= ExtendPoint[ExtendCount]){		//エクステンドの値を超えたら
	ExtendPlayer(1);			//残機＋１
	PlaySE("se1UP.wav");			//エクステンド音を鳴らす
	if(ExtendPoint[ExtendCount] == 9999){
	AddPoint(-9999);			//9999を越えたら9999減らす
	}
	if(ExtendCount == 9){ExtendCount = 6;}	//エクステンドをループさせる
	ExtendCount++;				//エクステンド回数＋１
	}
yield;

}
}

task ScoreExtend(){
let ExtendCount = 0;
let Score = GetScore;

loop{
if(ExtendCount == 0){if(Score >= 25000000){
		ExtendPlayer(1);
		ExtendCount = 1;}
	yield;
	}
else if(ExtendCount == 1){if(Score >= 50000000){
		ExtendPlayer(1);
		ExtendCount = 2;}
	yield;
	}
else if(ExtendCount == 2){if(Score >= 80000000){
		ExtendPlayer(1);
		ExtendCount = 3;}
	yield;
	}
else if(ExtendCount == 3){if(Score >= 120000000){
		ExtendPlayer(1);
		ExtendCount = 4;}
	yield;
	}
else if(ExtendCount == 4){if(Score >= 160000000){
		ExtendPlayer(1);
		ExtendCount = 5;}
	yield;
	}
else if(ExtendCount == 5){if(Score >= 200000000){
		ExtendPlayer(1);
		ExtendCount = 6;}
	yield;
	}
else if(ExtendCount == 6){yield;}
}
}

task MagicPointOnStage(){

#include_function ".\Config.txt"

SetCommonData("MAGICPOINT",0);
SetCommonData("DRAWBONUS",0);
yield;
let E = GetCommonData("EASY_START");
let N = GetCommonData("NORMAL_START");
let H = GetCommonData("HARD_START");
let L = GetCommonData("LUNATIC_START");

let AddToEnemy;
let ReduceToLive;
let ReduceToMissRate = [0,0.1,0.4,0.9,1.6,2.5,3.6,5];
let Level;
let Difficultly;
let Graze = GetGraze;
let GrazeA = 0;
let Point = GetPoint;
let PointA = GetPoint;

let FirstPlayer = ChoosePlayerLife() - 4;
let ReduceToFirstPlayer = [1.5,3,5,7,10];
let ReduceRateUp = 1;
let Count = 0;

if(E == 1||E == 2){SetCommonData("MAGICPOINT",5);AddToEnemy = 0.8;ReduceToLive = 0.008333;Level = 10;Difficultly = 0;}
if(N == 1||N == 2){SetCommonData("MAGICPOINT",10);AddToEnemy = 1.4;ReduceToLive = 0.009527;Level = 12;Difficultly = 1;}
if(H == 1||H == 2){SetCommonData("MAGICPOINT",15);AddToEnemy = 2.0;ReduceToLive = 0.011115;Level = 16;Difficultly = 2;}
if(L == 1||L == 2){SetCommonData("MAGICPOINT",25);AddToEnemy = 3.0;ReduceToLive = 0.016667;Level = 20;Difficultly = 3;}
if(E > 2){SetCommonData("MAGICPOINT",25);AddToEnemy = 0.8;ReduceToLive = 0.008333;Level = 10;Difficultly = 0;}
if(N > 2){SetCommonData("MAGICPOINT",50);AddToEnemy = 1.4;ReduceToLive = 0.009527;Level = 12;Difficultly = 1;}
if(H > 2){SetCommonData("MAGICPOINT",75);AddToEnemy = 2.0;ReduceToLive = 0.011115;Level = 16;Difficultly = 2;}
if(L > 2){SetCommonData("MAGICPOINT",125);AddToEnemy = 3.0;ReduceToLive = 0.016667;Level = 20;Difficultly = 3;}

let bonusrate = 1 / (10 * (Difficultly  + 1));

SetCommonData("MAGICPOINT_ADD",AddToEnemy);
SetCommonData("MAGICPOINT_REDUCE",ReduceToLive);
SetCommonData("MAGICPOINT_BONUSRATE",bonusrate);

if(FirstPlayer >= 0){ReduceRateUp = ReduceToFirstPlayer[FirstPlayer];}

loop{
	Count++;
	let a	= GetCommonData("MAGICPOINT");
	let zanki = GetPlayerLife;
	if(zanki >= 8){zanki = 7;}
	let ReduceToMiss;
	Graze = GetGraze;

	if(Count >= 90){SetCommonData("BONUSSCORE",0);
	SetCommonData("DRAWBONUS",0);}

	if(Graze != GrazeA){
	a += (Difficultly  + 1) * zanki * 0.035;
	GrazeA = Graze;
	SetCommonData("MAGICPOINT",a);
	}

	if(OnBomb == true){
	PointA = GetPoint;
	let b;
	let PMS = GetPlayerMoveState();


	if(PMS == MOVE_NORMAL){
	while(OnBomb == true){yield;}
	b = a * 0.7;
	Point = GetPoint;
	let Bonus = trunc((Point - PointA) * 300 * a);

	AddScore(Bonus);
	if(Bonus != 0){
	SetCommonData("BONUSSCORE",Bonus);
	yield;
	SetCommonData("DRAWBONUS",1);
	Count = 0;
	b += Bonus / 2000000;
	}
	}

	else if(PMS == MOVE_SLOW){
	while(OnBomb == true){yield;}
	b = a * 0.95;
	Point = GetPoint;
	let BonusP = trunc((Point - PointA) * (0.4375 - Difficultly * 0.0625));

	CreateEnemyFromFile(GetCurrentScriptDirectory~"\Stage\ZAKO\Enemy00.txt",GetCenterX,GetCenterY,0,0,BonusP + 1);

	SetCommonData("BONUSSCORE",BonusP+1);
	loop(2){yield;}
	SetCommonData("DRAWBONUS",2);
	Count = 0;
	CollectItems;
	}

	SetCommonData("MAGICPOINT",b);
	}

	if(OnPlayerMissed == true){
	while(OnPlayerMissed == true){yield;}//喰らいボム時間待機
	if(OnBomb == false){//喰らいボムに失敗していたら減少させる
	ReduceToMiss = Level * ReduceToMissRate[zanki] * ReduceRateUp;
	a -= ReduceToMiss;
	if(0 >= a){a = 0;}//MasicPointが０以下になったら０にする
	SetCommonData("MAGICPOINT",a);
	}
	}

	if(Difficultly <= 1){
	if(a > 999.999999){
	a = 999.999999;
	SetCommonData("MAGICPOINT",a);
	}
	}
	if(Difficultly == 2){
	if(a > 1499.999999){
	a = 1499.999999;
	SetCommonData("MAGICPOINT",a);
	}
	}
	if(Difficultly == 3){
	if(a > 1999.999999){
	a = 1999.999999;
	SetCommonData("MAGICPOINT",a);
	}
	}
yield;
}
}
function NumToString(let num){
let leng = trunc(log10(num))+1;//整数部分の桁数を求める
let string = ToString(num);//数字を文字列に変える
let lengA = 0;

if(leng < 2&& leng >= 1){lengA = 1;}
if(leng < 3&& leng >= 2){lengA = 2;}
if(leng < 4&& leng >= 3){lengA = 3;}
if(leng < 5&& leng >= 4){lengA = 4;}
if(leng < 6&& leng >= 5){lengA = 5;}
if(leng < 7&& leng >= 6){lengA = 6;}
if(leng < 8&& leng >= 7){lengA = 7;}
if(leng < 9&& leng >= 8){lengA = 8;}
if(leng < 10&& leng >= 9){lengA = 9;}
if(leng < 11&& leng >= 10){lengA = 10;}

return string[0..lengA];//文字列をスライシングして返す
}

function MagicPointDrawText(){

let textA = "MagicPoint";
let textB = ToString(GetCommonData("MAGICPOINT"));
let textD = "MP"~ToString(GetCommonData("MAGICPOWER"));

let textE = "ScoreBonus!"~NumToString(GetCommonData("BONUSSCORE"));
let textF = "PointItemBonus!"~NumToString(GetCommonData("BONUSSCORE"));
let a = GetCommonDataDefault("ALPHA",255);

if(GetPlayerX < 150&&GetPlayerY > 350){
a -= 16;
if(a < 70){a = 70;}
}
else{
a += 16;
if(a > 255){a = 255;}
}
SetFontColor(190,205,255,190,155,255);
DrawText(textA,64,388,12,a);
DrawText(textB,64,400,12,a);
DrawText(textD,64,424,12,a);
if(GetCommonData("DRAWBONUS") == 1){DrawText(textE,64,48,24,255);}
if(GetCommonData("DRAWBONUS") == 2){DrawText(textF,64,48,24,255);}
SetCommonData("ALPHA",a);
}

task MagicPowerOnStage{
SetCommonData("MAGICPOWER",0);
let PointA = 0;		//前フレームの点アイテムの数を格納する変数
let Point = GetPoint;	//点アイテムの数を格納する変数
let MagicPower;		//魔力値を格納する変数
let MagicPoint;		//魔力点を格納する変数
let ExtendCount = 0;	//ボムエクステンド数を格納する変数
let E = GetCommonData("EASY_START");
let N = GetCommonData("NORMAL_START");
let H = GetCommonData("HARD_START");
let L = GetCommonData("LUNATIC_START");
let level;
if(E > 0){level = 1;}
if(N > 0){level = 1;}
if(H > 0){level = 1.5;}
if(L > 0){level = 2;}


loop{
MagicPower = GetCommonData("MAGICPOWER");
MagicPoint = GetCommonData("MAGICPOINT");
Point = GetPoint;
//↑毎フレーム、点アイテムの数、魔力値、魔力点を取得

if(Point != PointA){					//点アイテムを取得したときの処理
if(Point >= PointA){					//点アイテム数が増えていたら
MagicPower += MagicPoint * (Point - PointA) / (5000 + ExtendCount * 1000) / level;//MagicPowerを増やす
SetCommonData("MAGICPOWER",MagicPower);			//共通データ更新
}
PointA = Point;						//PointAを新しい値に定義しなおす
}

if(MagicPower >= 100){					//１００を越えたら
AddBomb(1);						//ボム＋１
PlaySE(GetCurrentScriptDirectory~"\se\tm2_power000.wav");//音を鳴らす
MagicPower -= 100;					//魔力値−１００
ExtendCount++;						//エクステンド回数＋１
SetCommonData("MAGICPOWER",MagicPower);			//共通データ更新
}

yield;	//←これ大事
}
}

function MagicPowerAdd(isBig){
let MP = GetCommonData("MAGICPOWER");
if(isBig == true){
MP += 3;
}
else if(isBig == false){
MP += 0.5;
}
SetCommonData("MAGICPOWER",MP);
}

function MasicPointAddNormal(Rate){
let MagicPoint = GetCommonData("MAGICPOINT");
let AddPoint = GetCommonData("MAGICPOINT_ADD");

if(OnBomb == false){
MagicPoint += AddPoint * Rate;
SetCommonData("MAGICPOINT",MagicPoint);
}
}

function MasicPointAddSpellCard{
let MagicPoint = GetCommonData("MAGICPOINT");
let AddPoint = GetCommonData("MAGICPOINT_ADD");

if(GetBombCountInThisSpell == 0&&GetMissCountInThisSpell == 0){
MagicPoint += AddPoint * 15;
SetCommonData("MAGICPOINT",MagicPoint);
}
}

function MasicPointReduce{
let MagicPoint = GetCommonData("MAGICPOINT");
let ReducePoint = GetCommonData("MAGICPOINT_REDUCE");

MagicPoint -= ReducePoint;
if(0 >= MagicPoint){MagicPoint = 0;}
SetCommonData("MAGICPOINT",MagicPoint);
}

task MusicControl{
let stage;							//ステージ数を格納する変数
let Music1 = GetCurrentScriptDirectory~"\Bgm\ametowa.mp3";	//ステージ１音楽
let Music2 = GetCurrentScriptDirectory~"\Bgm\fukaki.mp3";	//ステージ２音楽
let Music3 = GetCurrentScriptDirectory~"\Bgm\toki.mid";		//ステージ３音楽

LoadMusic(Music1);
stage = GetCommonData("STAGE");					//共通データでステージ数を確認
if(stage == 1){PlayMusic(Music1);}				//１面なら、１面音楽を鳴らす
while(stage == 1){stage = GetCommonData("STAGE");yield;}	//１面が終わるまで待機
DeleteMusic(Music1);						//１面音楽をメモリから削除
LoadMusic(Music2);
if(stage == 2){PlayMusic(Music2);}				//２面なら、２面音楽を鳴らす
while(stage == 2){stage = GetCommonData("STAGE");yield;}	//２面が終わるまで待機
DeleteMusic(Music2);						//２面音楽をメモリから削除
LoadMusic(Music3);
if(stage == 3){PlayMusic(Music3);}				//３面なら、３面音楽を鳴らす
while(stage == 3){stage = GetCommonData("STAGE");yield;}	//３面が終わるまで待機
DeleteMusic(Music3);						//３面音楽をメモリから削除
loop{yield;}
}

task BGControl{
SetCommonData("ON_SPELL",0);
let stage;
let spell;
let BG1 = GetCurrentScriptDirectory~"\BG\yun_3780.jpg";
let BG2 = GetCurrentScriptDirectory~"\BG\eyes0048.jpg";
let BG3 = GetCurrentScriptDirectory~"\BG\seiryu808.jpg";

stage = GetCommonData("STAGE");

LoadGraphic(BG1);
if(stage == 1){
SetTexture(BG1);SetGraphicRect(0,0,384,448);SetAlpha(150);yield;}
while(stage == 1){stage = GetCommonData("STAGE");yield;}
DeleteGraphic(BG1);

LoadGraphic(BG2);SetGraphicScale(2,2);
if(stage == 2){
SetTexture(BG2);SetGraphicRect(0,0,384,448);SetAlpha(150);yield;}
while(stage == 2){stage = GetCommonData("STAGE");yield;}
DeleteGraphic(BG2);

LoadGraphic(BG3);SetGraphicScale(1,1);
if(stage == 3){
SetTexture(BG3);SetGraphicRect(0,0,384,448);SetAlpha(200);yield;}
while(stage == 3){stage = GetCommonData("STAGE");
spell = GetCommonData("ON_SPELL");SetAlpha(200);
while(spell == 1){SetAlpha(150);yield;}
yield;}
DeleteGraphic(BG3);

loop{yield;}
}
