
//////////////////////
// 3D Landscape    //
//Code By Ayoros  //
///////////////////
function part7() {

    this.initdone = 0 ;
    this.canvaspart ;
    this.vbl = 0;

    this.text = new text_cursor() ;

var angle_land = 0;
var step = 1.5;
var a_data;
var fractal_zoom=0;
var vbl_land=0;
var test_landscape=1;

var water ;
var sand ;
var grass ;
var mountain ;

function NodeObj() {
	this.height = 0;
	//this.colour = "rgb(0,0,0)"
    this.water = 0;
    this.grass = 0;
    this.sand = 0;
    this.mountain = 0;
    this.value = 0 ;
}

function Coord() {
    this.x1 ;
    this.y1 ;
    this.x2 ;
    this.y2 ;
    this.x3 ;
    this.y3 ;
    this.x4 ;
    this.y4 ;
}

    this.init = function() {
        generateLandscape();
        this.text.init(choix_text[0],10,8,font_key) ;

        this.initdone = 1 ;
     }

function generateLandscape() {
	a_data = new Array(33);
	for (var i=0; i<34; i++) {
		a_data[i] = new Array(33);
			for (var j=0; j<34; j++) {
				a_data[i][j] = new NodeObj();
			}
	}
	//4 coins de surface
	a_data[0][0].height = 0;
	a_data[0][32].height = 0;
	a_data[32][32].height = 0;
	a_data[32][0].height = 0;

	// Populate grid with elevation values
	for (var i=16; i>0; i=Math.floor(i/2)) {
    j = i * 2;
		for (var x=0; x<32; x += j) {
			for (var y=0; y<32; y += j) {
        // 4 faces
        s1 = a_data[x+i][y].height   = ((a_data[x][y].height + a_data[x+j][y].height) / 2)     + (Math.random() * j*1.1) - i;
        s2 = a_data[x+j][y+i].height = ((a_data[x+j][y].height + a_data[x+j][y+j].height) / 2) + (Math.random() * j*1.1) - i;
        s3 = a_data[x+i][y+j].height = ((a_data[x+j][y+j].height + a_data[x][y+j].height) / 2) + (Math.random() * j*1.1) - i;
        s4 = a_data[x][y+i].height   = ((a_data[x][y+j].height + a_data[x][y].height) / 2)     + (Math.random() * j*1.1) - i;
        a_data[x+i][y+i].height      = ((s1 + s2 + s3 + s4) / 4)                               + (Math.random() * j*1.1) - i;
			}
		}
	}
  colourLandscape(step);
}

function colourLandscape(step) {
	
	function sortNumber(a,b) { return a - b; }

	step = Math.round(step);
	if (step < 1){step = 1;}
    a_temp = new Array(4);
	for (var i=0; i<32; i+=step) {
		for (var j=0; j<32; j+=step) {
      a_temp[0] = a_data[i][j].height;
			a_temp[1] = a_data[i][j+1].height;
			a_temp[2] = a_data[i+1][j+1].height;
			a_temp[3] = a_data[i+1][j].height;
			a_temp.sort(sortNumber);

			if (a_temp[3] < 0) { // water
                temp = Math.floor(192+(a_temp[3] * 8)) ;
                //temp = ( ~~ (temp/10)) * 10 ;
                temp = ( ~~ (temp/10)) ;
				//a_data[i][j].colour = 'rgb(0,0,'+temp+')';
                a_data[i][j].water = 1 ;
                a_data[i][j].grass = 0 ;
                a_data[i][j].sand = 0 ;
                a_data[i][j].mountain = 0 ;
                a_data[i][j].value = temp ;
			} else {
				if (a_temp[2] < 0) { // sand
					//a_data[i][j].colour = 'rgb(232,232,84)';
                    a_data[i][j].water = 0 ;
                    a_data[i][j].grass = 0 ;
                    a_data[i][j].sand = 1 ;
                    a_data[i][j].mountain = 0 ;
                    a_data[i][j].value = 1 ;
				} else {
					if (a_temp[3] / step > 14) { // mountain
						temp = Math.min(Math.floor(a_temp[0] * 8) + 130, 255);
                        //temp = ( ~~ (temp/10)) * 10 ;
                        temp = ( ~~ (temp/10)) ;
						//a_data[i][j].colour = 'rgb('+temp+','+temp+','+temp+')';
                        a_data[i][j].water = 0 ;
                        a_data[i][j].grass = 0 ;
                        a_data[i][j].sand = 0 ;
                        a_data[i][j].mountain = 1 ;
                        a_data[i][j].value = temp ;
					} else { // grass
						verdure = (a_temp[3] - a_temp[0]) / step;
						temp = Math.min(Math.floor((verdure * 16) + (a_temp[0] * 4)), 155);
                        temp = ( ~~ (temp/10)) ;
                        // temp = ( ~~ (temp/10)) * 10 ;
						//a_data[i][j].colour = 'rgb('+temp+','+(temp+100)+','+temp+')';
                        a_data[i][j].water = 0 ;
                        a_data[i][j].grass = 1 ;
                        a_data[i][j].sand = 0 ;
                        a_data[i][j].mountain = 0 ;
                        a_data[i][j].value = temp ;
					}
				}
			}
		}
	}
}
function draw(dest) {

  width = 600;
	height = 400;
  fractal_zoom++;
  if (fractal_zoom>298){fractal_zoom=0;}
	step = Math.round(step);
  if (step < 1) { step = 1;}

	centreX = ~~(width / 2);
	centreY = ~~(height / 2);
	scaleX = centreX / 23;
	scaleY = centreY / 23;
	elevationY = scaleY / 2;

	s = Math.sin(angle_land)*fractal_zoom/256;
	c = Math.cos(angle_land)*fractal_zoom/256;
	deltaIX = -s * scaleX;
	deltaIY = c * scaleY;
    deltaJX = c * scaleX;
	deltaJY = s * scaleY;
	//distance
	stepIX = deltaIX * step;
	stepIY = deltaIY * step;
	stepJX = deltaJX * step;
	stepJY = deltaJY * step;
	if (c > 0) {
    startI = -16;
		stepI = step;
		endI = 16;
	} 
	else
	{
    startI = 16-step;
    stepI = -step;
		endI = -16;
	}
	if (s > 0) {
    startJ = -16;
		stepJ = step;
	} 
	else
	{
    startJ = 16-step;
		stepJ = -step;
	}

    // Calcul récupération affichage itération
    var coord = new Array() ;

    //var color = new Array() ;
    water = new Array() ;
    sand = new Array() ;
    grass = new Array() ;
    mountain = new Array() ;

    var ii=0 ;
    for (var i=startI; (i>-17) && (i<16); i += stepI) {
        var jj=0 ;
        coord[ii] = new Array() ;
        //color[ii] = new Array() ;

        for (var j=startJ; (j>-17) && (j<16); j += stepJ) {
            coord[ii][jj] = new Coord() ;
            coord[ii][jj].x1 = (deltaIX * i) + (deltaJX * j) + centreX;
			coord[ii][jj].y1 = (deltaIY * i) + (deltaJY * j) + centreY;
			coord[ii][jj].x2 = coord[ii][jj].x1 + stepJX;
			coord[ii][jj].y2 = coord[ii][jj].y1 + stepJY;
			coord[ii][jj].x3 = coord[ii][jj].x2 + stepIX;
			coord[ii][jj].y3 = coord[ii][jj].y2 + stepIY;
			coord[ii][jj].x4 = coord[ii][jj].x1 + stepIX;
			coord[ii][jj].y4 = coord[ii][jj].y1 + stepIY;
			coord[ii][jj].y1 -= (Math.max(a_data[i+16][j+16].height, 0) * elevationY);
			coord[ii][jj].y2 -= (Math.max(a_data[i+16][j+16+step].height, 0) * elevationY);
			coord[ii][jj].y3 -= (Math.max(a_data[i+16+step][j+16+step].height, 0) * elevationY);
			coord[ii][jj].y4 -= (Math.max(a_data[i+16+step][j+16].height, 0) * elevationY);
            //color[ii][jj] = a_data[i+16+step][j+16].colour ;
            var tmp = a_data[i+16+step][j+16] ;

            if ( tmp.water != 0 ) {
                if ( water[tmp.value] === undefined ) water[tmp.value] = new Array() ;
                water[tmp.value].push( { i:ii, j:jj } ) ;
            }
            if ( tmp.sand != 0 ) {
                if ( sand[tmp.value] === undefined ) sand[tmp.value] = new Array() ;
                sand[tmp.value].push( { i:ii, j:jj } ) ;
            }
            if ( tmp.grass != 0 ) {
                if ( grass[tmp.value] === undefined ) grass[tmp.value] = new Array() ;
                grass[tmp.value].push( { i:ii, j:jj } ) ;
            }
            if ( tmp.mountain != 0 ) {
                if ( mountain[tmp.value] === undefined ) mountain[tmp.value] = new Array() ;
                mountain[tmp.value].push( { i:ii, j:jj } ) ;
            }
            
            jj++ ;
		}
        ii ++ ;
    }

    if (test_landscape==1){
        ctx = dest.contex ;
        ctx.beginPath() ;
        ctx.lineWidth = 1 ;
        ctx.strokeStyle="#e65ed4";
        for (var i=0; i<coord.length; i++) {
            for (j=0; j<coord[i].length; j ++) {
                //dest.line(x1[i][j],y1[i][j],x2[i][j],y2[i][j],1,'#e65ed4');
                ctx.moveTo(coord[i][j].x1,coord[i][j].y1) ;
                ctx.lineTo(coord[i][j].x2,coord[i][j].y2) ;
			}
        }
        ctx.closePath();
        ctx.stroke() ;

        ctx.beginPath() ;
        ctx.lineWidth = 1 ;
        ctx.strokeStyle="#e6615e";
        for (var i=0; i<coord.length; i++) {
            for (j=0; j<coord[i].length; j ++) {
                //dest.line(x1[i][j],y1[i][j],x3[i][j],y3[i][j],1,'#e6615e');
                ctx.moveTo(coord[i][j].x1,coord[i][j].y1) ;
                ctx.lineTo(coord[i][j].x3,coord[i][j].y3) ;
			}
        }
        ctx.closePath();
        ctx.stroke() ;

        ctx.beginPath() ;
        ctx.lineWidth = 1 ;
        ctx.strokeStyle="#e68e5e";
        for (var i=0; i<coord.length; i++) {
            for (j=0; j<coord[i].length; j ++) {
                // dest.line(x1[i][j],y1[i][j],x4[i][j],y4[i][j],1,'#e68e5e');
                ctx.moveTo(coord[i][j].x1,coord[i][j].y1) ;
                ctx.lineTo(coord[i][j].x4,coord[i][j].y4) ;
			}
        }
        ctx.closePath();
        ctx.stroke() ;

    } else {
        /*
            ctx = dest.contex ;
        for (var i=0; i<x1.length; i++) {
            for (j=0; j<x1[i].length; j ++) {
                //dest.quad(x1[i][j],y1[i][j],x2[i][j],y2[i][j],x3[i][j],y3[i][j],x4[i][j],y4[i][j],color[i][j]);
            ctx.beginPath();
			ctx.moveTo(x1[i][j],y1[i][j]);
			ctx.lineTo(x2[i][j],y2[i][j]);
			ctx.lineTo(x3[i][j],y3[i][j]);
			ctx.lineTo(x4[i][j],y4[i][j]);
			ctx.closePath();
			ctx.fillStyle=color[i][j];
			ctx.fill();
            }
		}
        */

        for (var i=0; i<water.length; i++) {
            if ( water[i] !== undefined ) {
                ctx.beginPath() ;
                for (var j=0; j<water[i].length; j++) {
                    var ii = water[i][j].i ;
                    var jj = water[i][j].j ;
                    if ( (ii<33) && (jj<33) ) {
                        var xx1 = coord[ii][jj].x1 ; if ( xx1 < 0 ) xx1 = 0 ; if (xx1 > 639) xx1 = 639 ;
                        var xx2 = coord[ii][jj].x2 ; if ( xx2 < 0 ) xx2 = 0 ; if (xx2 > 639) xx2 = 639 ;
                        var xx3 = coord[ii][jj].x3 ; if ( xx3 < 0 ) xx3 = 0 ; if (xx3 > 639) xx3 = 639 ;
                        var xx4 = coord[ii][jj].x4 ; if ( xx4 < 0 ) xx4 = 0 ; if (xx4 > 639) xx4 = 639 ;
                        var yy1 = coord[ii][jj].y1 ; if ( yy1 < 0 ) yy1 = 0 ; if (yy1 > 399) yy1 = 399 ;
                        var yy2 = coord[ii][jj].y2 ; if ( yy2 < 0 ) yy2 = 0 ; if (yy2 > 399) yy2 = 399 ;
                        var yy3 = coord[ii][jj].y3 ; if ( yy3 < 0 ) yy3 = 0 ; if (yy3 > 399) yy3 = 399 ;
                        var yy4 = coord[ii][jj].y4 ; if ( yy4 < 0 ) yy4 = 0 ; if (yy4 > 399) yy4 = 399 ;
                        ctx.moveTo(xx1,yy1);
                        ctx.lineTo(xx2,yy2);
                        ctx.lineTo(xx3,yy3);
                        ctx.lineTo(xx4,yy4);
                    }
                }
                ctx.closePath();
                ctx.fillStyle='rgb(0,0,' + i*10 + ')';
                ctx.fill();
            }
        }

        for (var i=0; i<grass.length; i++) {
            if ( grass[i] !== undefined ) {
                ctx.beginPath() ;
                for (var j=0; j<grass[i].length; j++) {
                    var ii = grass[i][j].i ;
                    var jj = grass[i][j].j ;
                    if ( (ii<33) && (jj<33) ) {
                        var xx1 = coord[ii][jj].x1 ; if ( xx1 < 0 ) xx1 = 0 ; if (xx1 > 639) xx1 = 639 ;
                        var xx2 = coord[ii][jj].x2 ; if ( xx2 < 0 ) xx2 = 0 ; if (xx2 > 639) xx2 = 639 ;
                        var xx3 = coord[ii][jj].x3 ; if ( xx3 < 0 ) xx3 = 0 ; if (xx3 > 639) xx3 = 639 ;
                        var xx4 = coord[ii][jj].x4 ; if ( xx4 < 0 ) xx4 = 0 ; if (xx4 > 639) xx4 = 639 ;
                        var yy1 = coord[ii][jj].y1 ; if ( yy1 < 0 ) yy1 = 0 ; if (yy1 > 399) yy1 = 399 ;
                        var yy2 = coord[ii][jj].y2 ; if ( yy2 < 0 ) yy2 = 0 ; if (yy2 > 399) yy2 = 399 ;
                        var yy3 = coord[ii][jj].y3 ; if ( yy3 < 0 ) yy3 = 0 ; if (yy3 > 399) yy3 = 399 ;
                        var yy4 = coord[ii][jj].y4 ; if ( yy4 < 0 ) yy4 = 0 ; if (yy4 > 399) yy4 = 399 ;
                        ctx.moveTo(xx1,yy1);
                        ctx.lineTo(xx2,yy2);
                        ctx.lineTo(xx3,yy3);
                        ctx.lineTo(xx4,yy4);
                    }
                }
                ctx.closePath();
                ctx.fillStyle='rgb('+i*10+','+(i*10+100)+','+i*10+')';
                ctx.fill();
            }
        }

        for (var i=0; i<sand.length; i++) {
            if ( sand[i] !== undefined ) {
                ctx.beginPath() ;
                for (var j=0; j<sand[i].length; j++) {
                    var ii = sand[i][j].i ;
                    var jj = sand[i][j].j ;
                    if ( (ii<33) && (jj<33) ) {
                        var xx1 = coord[ii][jj].x1 ; if ( xx1 < 0 ) xx1 = 0 ; if (xx1 > 639) xx1 = 639 ;
                        var xx2 = coord[ii][jj].x2 ; if ( xx2 < 0 ) xx2 = 0 ; if (xx2 > 639) xx2 = 639 ;
                        var xx3 = coord[ii][jj].x3 ; if ( xx3 < 0 ) xx3 = 0 ; if (xx3 > 639) xx3 = 639 ;
                        var xx4 = coord[ii][jj].x4 ; if ( xx4 < 0 ) xx4 = 0 ; if (xx4 > 639) xx4 = 639 ;
                        var yy1 = coord[ii][jj].y1 ; if ( yy1 < 0 ) yy1 = 0 ; if (yy1 > 399) yy1 = 399 ;
                        var yy2 = coord[ii][jj].y2 ; if ( yy2 < 0 ) yy2 = 0 ; if (yy2 > 399) yy2 = 399 ;
                        var yy3 = coord[ii][jj].y3 ; if ( yy3 < 0 ) yy3 = 0 ; if (yy3 > 399) yy3 = 399 ;
                        var yy4 = coord[ii][jj].y4 ; if ( yy4 < 0 ) yy4 = 0 ; if (yy4 > 399) yy4 = 399 ;
                        ctx.moveTo(xx1,yy1);
                        ctx.lineTo(xx2,yy2);
                        ctx.lineTo(xx3,yy3);
                        ctx.lineTo(xx4,yy4);
                    }
                }
                ctx.closePath();
                ctx.fillStyle='rgb(232,232,84)';
                ctx.fill();
            }
        }

        for (var i=0; i<mountain.length; i++) {
            if ( mountain[i] !== undefined ) {
                ctx.beginPath() ;
                for (var j=0; j<mountain[i].length; j++) {
                    var ii = mountain[i][j].i ;
                    var jj = mountain[i][j].j ;
                    if ( (ii<33) && (jj<33) ) {
                        var xx1 = coord[ii][jj].x1 ; if ( xx1 < 0 ) xx1 = 0 ; if (xx1 > 639) xx1 = 639 ;
                        var xx2 = coord[ii][jj].x2 ; if ( xx2 < 0 ) xx2 = 0 ; if (xx2 > 639) xx2 = 639 ;
                        var xx3 = coord[ii][jj].x3 ; if ( xx3 < 0 ) xx3 = 0 ; if (xx3 > 639) xx3 = 639 ;
                        var xx4 = coord[ii][jj].x4 ; if ( xx4 < 0 ) xx4 = 0 ; if (xx4 > 639) xx4 = 639 ;
                        var yy1 = coord[ii][jj].y1 ; if ( yy1 < 0 ) yy1 = 0 ; if (yy1 > 399) yy1 = 399 ;
                        var yy2 = coord[ii][jj].y2 ; if ( yy2 < 0 ) yy2 = 0 ; if (yy2 > 399) yy2 = 399 ;
                        var yy3 = coord[ii][jj].y3 ; if ( yy3 < 0 ) yy3 = 0 ; if (yy3 > 399) yy3 = 399 ;
                        var yy4 = coord[ii][jj].y4 ; if ( yy4 < 0 ) yy4 = 0 ; if (yy4 > 399) yy4 = 399 ;
                        ctx.moveTo(xx1,yy1);
                        ctx.lineTo(xx2,yy2);
                        ctx.lineTo(xx3,yy3);
                        ctx.lineTo(xx4,yy4);
                    }
                }
                ctx.closePath();
                ctx.fillStyle='rgb('+i*10+','+i*10+','+i*10+')';
                ctx.fill();
            }
        }


	}

	angle_land += 0.02;
}

    this.render = function(dest) {
        if (this.initdone == 0) { this.canvaspart = new canvas(600,400) ; this.init() ; this.canvaspart.contex.globalCompositeOperation='lighter';}
          if (this.vbl==0){test_landscape=1;step = 1.5;generateLandscape();}
          if (this.vbl==601){test_landscape=0;step = 1;generateLandscape();}
          this.canvaspart.clear();
          
          if ((vbl_land>0)&&(vbl_land<300)) {draw(this.canvaspart);}
          if ((vbl_land>=300)&&(vbl_land<301)) {generateLandscape();vbl_land=0;}
          //this.canvaspart.contex.globalCompositeOperation='source-over';
  
          this.text.render(this.canvaspart,choix_pos[0].x,choix_pos[0].y) ;

          this.canvaspart.draw(dest,60,88);
        vbl_land++;
        
        if (this.vbl<100) mycanvasblack.draw(dest,60,88,1-this.vbl/100);
        if (this.vbl>intro_parts[part_running].part_duration-100) mycanvasblack.draw(dest,60,88,1-(intro_parts[part_running].part_duration-this.vbl)/100);
        this.vbl++ ;

    }

return this ;
}
//////////////
//END PART7//
////////////
