task PointExtend(){
let ExtendPoint = [250,500,1000,1500,2250,3000,3750,4500,5500,6750,8000,9999,2000,4000,6000,8000,9999,-1];//エクステンドの点符必要数の羅列
let ExtendCount = 0;				//エクステンド回数を記憶する変数
let Point = GetPoint;				//Pointの値を格納する変数

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

if(ExtendCount == 17){loop{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);
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 ReduceToBomb;
let ReduceToMissRate = [0,0.1,0.4,1.6,3.2,9.6,16,25];
let Level;

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


if(E > 0){SetCommonData("MAGICPOINT",5);AddToEnemy = 0.5;ReduceToLive = 0.004167;ReduceToBomb = 0.8;Level = 5;}
if(N > 0){SetCommonData("MAGICPOINT",10);AddToEnemy = 1.5;ReduceToLive = 0.004763;ReduceToBomb = 1.2;Level = 6;}
if(H > 0){SetCommonData("MAGICPOINT",15);AddToEnemy = 2.5;ReduceToLive = 0.005557;ReduceToBomb = 1.6;Level = 8;}
if(L > 0){SetCommonData("MAGICPOINT",25);AddToEnemy = 3.5;ReduceToLive = 0.008333;ReduceToBomb = 2.2;Level = 10;}

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

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

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

	if(OnBomb == true){
	a -= ReduceToBomb * ReduceRateUp;
	if(0 >= a){a = 0;}//MasicPointが０以下になったら０にする
	SetCommonData("MAGICPOINT",a);
	while(OnBomb == true){yield;}
	}

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

	if(a >= 1000){
	a = 999;
	SetCommonData("MAGICPOINT",a);
	}
yield;
}
}

function MagicPointDrawText(){

let textA = "MagicPoint";
let textB = ToString(GetCommonData("MAGICPOINT"));
let textD = "MP"~ToString(GetCommonData("MAGICPOWER"));
let a = GetCommonDataDefault("ALPHA",255);

if(GetPlayerX < 150&&GetPlayerY > 350){
a -= 7;
if(a < 70){a = 70;}
}
else{
a += 7;
if(a > 255){a = 255;}
}

DrawText(textA,64,388,12,a);
DrawText(textB,64,400,12,a);
DrawText(textD,64,424,12,a);

SetCommonData("ALPHA",a);
}

task MagicPowerOnStage{
SetCommonData("MAGICPOWER",0);
let PointA = 0;		//前フレームの点アイテムの数を格納する変数
let Point = GetPoint;	//点アイテムの数を格納する変数
let MagicPower;		//魔力値を格納する変数
let MagicPoint;		//魔力点を格納する変数
loop{
MagicPower = GetCommonData("MAGICPOWER");
MagicPoint = GetCommonData("MAGICPOINT");
Point = GetPoint;
//↑毎フレーム、点アイテムの数、魔力値、魔力点を取得

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

if(MagicPower >= 100){					//１００を越えたら
AddBomb(1);						//ボム＋１
PlaySE(GetCurrentScriptDirectory~"\se\tm2_power000.wav");//音を鳴らす
MagicPower -= 100;					//魔力値−１００
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;}
}
