#TouhouDanmakufu
#Title[Noob Man : "Daedalus"]
#Text[Je croyais qu'on etait dans un danmaku, pourquoi dois-je resoudre des labyrinthes !? Bon, ils ne sont pas bien compliques mais vous devrez vous depecher de les traverser avant qu'ils ne vous tombent dessus.]
#Image[]
#BGM[script\DanMUku\Stage6\NoobMan.mp3]
#Player[script\DanMUku\Joueur\KorA\KorHosik.txt, script\DanMUku\Joueur\KorB\KorB.txt, script\DanMUku\Joueur\KorC\KorC.txt]
#ScriptVersion[2]

script_enemy_main
{    
    let imgEnemy="script\DanMUku\Stage6\Image\noobman.png";
    let count=-200;
    let tir ="script\DanMUku\Stage1\Son\tir.wav";
    let munch ="script\DanMUku\Stage1\Son\muncher.wav";
    let dir=0;
    let vitesse=0.2;
    let bg = "script\DanMUku\Stage6\Image\dedale.png";
    let x = GetX;
    let q = 0;
    let haut = true;
    let mazes = [[6,7,0,450],[7,7,0,500],[7,7,0,450],[8,9,1,900],[9,9,1,900],[9,9,1,800],[10,9,1,1000],[10,9,1,950]];
    let n = 0;
    let phase = 0;
    let seed = [4, 9, 10, 12, 13, 14, 16, 20, 29, 33, 39];

	let string=[""];

 function Wait(let frames){
   loop(frames){yield;}
 }


function MakeMaze(h, w, diff) {


	let diffVer = [[1,0,0,0,0], [1,0,0,0], [1,0,0], [1,0]];
	let diffHor = [[3,2,2,2,2], [3,2,2,2], [3,2,2], [3,2]];
	let zeros = [[0,0,0,0], [0,0,0], [0,0], [0]];


	let vis = [];
	let ver = [];
	let hor = [];
	let ligneHor = [];
	loop(w) {
		ligneHor=ligneHor~[[0]~zeros[diff]];
	}
	ligneHor=ligneHor~[[0]];
	hor=hor~[ligneHor];
	loop(h){
		let ligneVis = [];
		let ligneVer = [];
		let ligneHor = [];
		loop(w){
			ligneVis=ligneVis~[0];
			ligneVer=ligneVer~[diffVer[diff]];
			ligneHor=ligneHor~[diffHor[diff]];
		}
		ligneVis=ligneVis~[1];
		ligneVer=ligneVer~[[1]];
		ligneHor=ligneHor~[[3]];
		vis=vis~[ligneVis];
		ver=ver~[ligneVer];
		hor=hor~[ligneHor];
	}
	let ligneVis=[];
	let ligneVer=[];
	let ligneHor=[];
	loop(w){
		ligneVis=ligneVis~[1];
		ligneVer=ligneVer~[[0]~zeros[diff]];
	}
	ligneVis=ligneVis~[1];
	ligneVer=ligneVer~[[0]];
	vis=vis~[ligneVis];
	ver=ver~[ligneVer];

	function Walk(x, y){
		//vis[y][x] = 1;
		let ligne = vis[y];
		ligne[x]=1;
		vis[y]=ligne;

		let d = [[x - 1, y], [x, y + 1], [x + 1, y], [x, y - 1]];
		let i =3;
		loop(3){
			let j = prand_int(0,i);
			let mem = d[i];
			d[i]=d[j];
			d[j]=mem;
			i-=1;
		}
		OutputDebugString(4, "Shuffle 1X", d[0][0]);
		OutputDebugString(5, "Shuffle 1Y", d[0][1]);
		OutputDebugString(6, "Shuffle 2X", d[1][0]);
		OutputDebugString(7, "Shuffle 2Y", d[1][1]);
		OutputDebugString(8, "Shuffle 3X", d[2][0]);
		OutputDebugString(9, "Shuffle 3Y", d[2][1]);
		OutputDebugString(10, "Shuffle 4X", d[3][0]);
		OutputDebugString(11, "Shuffle 4Y", d[3][1]);
		
		i = 0;
		loop(4){
			let xx=d[i][0];
			let yy=d[i][1];

			if (yy>=0&&xx>=0&&vis[yy][xx]==0) {
				if (xx==x) {
					if (y>yy) {
						//hor[y][x] = [3,0,0];
						let ligne = hor[y];
						ligne[x] = [3]~zeros[diff];
						hor[y]=ligne;
					}
					else{
						//hor[yy][x] = [3,0,0];
						let ligne = hor[yy];
						ligne[x] = [3]~zeros[diff];
						hor[yy]=ligne;
					}
				}
				if (yy==y) {
					if (x>xx) {
						//ver[y][x] = [0,0,0];
						let ligne = ver[y];
						ligne[x] = [0]~zeros[diff];
						ver[y]=ligne;
					}
					else {
						//ver[y][xx] = [0,0,0];
						let ligne = ver[y];
						ligne[xx] = [0]~zeros[diff];
						ver[y]=ligne;
					}
				}
				Walk(xx,yy);
			}
			i++;
		}
	}

	Walk(prand_int(0,w-1), prand_int(0, h-1));

	let s = [];
	string = [];
	let i = 0;
	loop(h) {
		let j = 0;
		let ligneHor = [];
		let ligneVer = [];
		//let s1 = "";
		//let s2= "";
		loop(w+1) {
			ligneHor=ligneHor~hor[i][j];
			ligneVer=ligneVer~ver[i][j];
			//s1 = s1~hor[i][j];
			//s2 = s2~ver[i][j];
			j++;
		}
		i++;
		s=s~[ligneHor];
		loop(4-diff){
			s=s~[ligneVer];
		}
		//string = string~[s1]~[s2];
	}
	
	return s;


}

   @Initialize
   {
      LoadUserShotData("script\DanMUku\MUtir\MUtir.txt");
      LoadGraphic(imgEnemy);
	LoadGraphic(bg);
      LoadSE(tir);
      LoadSE(munch);
            SetLife(1000);
	    SetDurableSpellCard;
            SetTimer(129);
            SetInvincibility(60);
	SetScore(2000000);
            SetEnemyMarker(true);
            SetMovePosition02(GetCenterX, 0, 120);
	CutIn(YOUMU, "Daedalus", "script\DanMUku\Stage6\Image\noobcutin.png" , 0,0,121,214);
	psrand(seed[rand_int(0, length(seed)-1)] );
	//CreateDebugWindow;
   }
   
   @MainLoop
   {

	if (count==0) {
		PlaySE(tir);
		let maze = MakeMaze(mazes[n][0],mazes[n][1],mazes[n][2]);
		let i = 0;
		let y;
		loop(length(maze)) {
			let j = 0;
			loop(length(maze[i])) {
				if (haut) {y=GetClipMinY+i*10;}
				else {y=GetClipMaxY-i*10;}
				if (maze[i][j]>0) {
					Mur(haut, 450, GetClipMinX-7*mazes[n][2]+11*j, y, mazes[n][3]);
				}
				j++;
			}

			i++;
		}
		haut = !haut;
		count=-200-mazes[n][3];
		n++;
		if (n==2) {
			phase=1;
		}
		if (n==4) {
			phase=2;
		}
		if (n==6) {
			phase=3;
		}
	}

	if (phase==1&&count%100==0)
	{
		let x = GetPlayerX;
		let angle=0;
		PlaySE(tir);
		loop(10){
			if (!haut) {
				CreateShot01(x, GetClipMinY, 1, angle, 25, 0);
			}
			else {
				CreateShot01(x, GetClipMaxY, 1, 180+angle, 36, 0);
			}
			angle+=20;
		}
	}

	if (phase==2&&count%100==0)
	{
		let x = GetPlayerX;
		let angle=0;
		PlaySE(tir);
		loop(10){
			if (!haut) {
				CreateShot01(x, GetClipMinY, 1, angle, 25, 0);
				CreateShot01(GetCenterX+(GetCenterX-x), GetClipMinY, 1, angle, 30, 0);
			}
			else {
				CreateShot01(x, GetClipMaxY, 1, 180+angle, 36, 0);
				CreateShot01(GetCenterX+(GetCenterX-x), GetClipMaxY, 1, 180+angle, 32, 0);
			}
			angle+=20;
		}
	}

	if (phase==3&&count%100==0)
	{
		let x = GetPlayerX;
		let angle=-10;
		PlaySE(tir);
		loop(11){
			if (!haut) {
				CreateShot01(x, GetClipMinY, 1, angle, 25, 0);
				CreateShot01(GetCenterX+(GetCenterX-x), GetClipMinY, 1, angle, 30, 0);
			}
			else {
				CreateShot01(x, GetClipMaxY, 1, 180+angle, 36, 0);
				CreateShot01(GetCenterX+(GetCenterX-x), GetClipMaxY, 1, 180+angle, 32, 0);
			}
			angle+=20;
		}
	}

count++;
yield;
   }
   @Finalize
   {
      DeleteGraphic(imgEnemy);

	loop(12) {
		CreateItem(ITEM_SCORE, rand(GetX-10, GetX+10), rand(GetY-10, GetY+10));  
	}
   }
   

   @DrawLoop
   {

	    SetTexture(bg);
            SetRenderState(ALPHA);
            SetAlpha(255);
	    SetColor(130,100,100);
            SetGraphicRect(0, 0, 1296, 1296);
            SetGraphicScale(0.5,0.5);
            SetGraphicAngle(0, 0, q);
            DrawGraphic(GetCenterX, GetCenterY);
		q-=0.2;

	if(x<GetX){
		SetGraphicAngle(0,0,0);
	}
	if(x>GetX){
		SetGraphicAngle(180,0,0);
	}
      x=GetX;
            SetColor(255,255,255);
            SetGraphicScale(1,1);
            SetRenderState(ALPHA);
            SetTexture(imgEnemy);
	if (GetSpeed>0) {	
	            SetGraphicRect(28,0,55,34);
	}
	else {
		    SetGraphicRect(0,0,27,34);
	}

            DrawGraphic(GetX,GetY);

	DrawText("Noob Man", GetClipMinX+10, GetClipMinY+15, 12, 255);

	//let i = 0;
	//loop(length(string)) {
	//	DrawText(string[i], GetClipMinX+10, GetClipMinY+35+20*i, 12, 255);
	//	i++;
	//}
   }


   task Mur(haut, dist, xDest, yDest, attente)
   {
            let obj=Obj_Create(OBJ_SHOT);
		let xBase;
		let yBase;
		let angle;

		if (haut) {
			angle=90;
			xBase = GetCenterX+rand(-dist, dist);
			yBase = GetClipMinY;
			if (xBase<GetClipMinX) {
				yBase+=GetClipMinX-xBase;
				xBase=GetClipMinX;
			}
			if (xBase>GetClipMaxX) {
				yBase+=xBase-GetClipMaxX;
				xBase=GetClipMaxX;
			}
		}
		else {
			angle=270;
			xBase = GetCenterX+rand(-dist, dist);
			yBase = GetClipMaxY;
			if (xBase<GetClipMinX) {
				yBase-=GetClipMinX-xBase;
				xBase=GetClipMinX;
			}
			if (xBase>GetClipMaxX) {
				yBase-=xBase-GetClipMaxX;
				xBase=GetClipMaxX;
			}
		}

            Obj_SetPosition(obj, xBase, yBase);
            Obj_SetAngle(obj, atan2(yDest-yBase, xDest-xBase));
            Obj_SetSpeed(obj, ( ( ((xBase-xDest)^2) + ((yBase-yDest)^2) )^0.5 )/120 );
            ObjShot_SetGraphic(obj, 90);
            ObjShot_SetDelay  (obj, 0);
	    ObjShot_SetBombResist(obj, true);
		
		Wait(120);
		PlaySE(munch);
		Obj_SetSpeed(obj, 0);
		Wait(attente-60);
		ObjShot_SetGraphic(obj, 83);
		Wait(60);
		Obj_SetSpeed(obj, 4);
		Obj_SetAngle(obj, angle);

   }



}
