//マップ作成ツール
var map_make_parts = [
	[0,"gl_map_straight_v",
	/*上2*/[0,2,3,4,5,8,9],
	/*下3*/[0,2,3,4,6,7,10],
	/*右4*/[11],
	/*左5*/[11]
	],
	[1,"gl_map_straight_h",
	/*上*/[11],
	/*下*/[11],
	/*右*/[1,2,4,7,8,9,10],
	/*左*/[1,2,3,5,6,9,10]
	],
	[2,"gl_map_cross",
	/*上*/[0,2,3,4,5,8,9],
	/*下*/[0,2,3,4,6,7,10],
	/*右*/[1,2,4,7,8,9,10],
	/*左*/[1,2,3,5,6,9,10]
	],
	[3,"gl_map_straight_p_r",
	/*上*/[0,2,3,4,5,8,9],
	/*下*/[0,2,3,4,6,7,10],
	/*右*/[1,2,4,7,8,9,10],
	/*左*/[11]
	],
	[4,"gl_map_straight_p_l",
	/*上*/[0,2,3,4,5,8,9],
	/*下*/[0,2,3,4,6,7,10],
	/*右*/[11],
	/*左*/[1,2,3,5,6,9,10]
	],
	[5,"gl_map_nw",
	/*上*/[11],
	/*下*/[0,6,7],
	/*右*/[1,7,8],
	/*左*/[11]
	],
	[6,"gl_map_wn",
	/*上*/[0,5,8],
	/*下*/[11],
	/*右*/[1,7,8],
	/*左*/[11]
	],
	[7,"gl_map_se",
	/*上*/[0,5,8],
	/*下*/[11],
	/*右*/[11],
	/*左*/[1,5,6,]
	],
	[8,"gl_map_es",
	/*上*/[11],
	/*下*/[0,5,6,7],
	/*右*/[11],
	/*左*/[1,5,6]
	],
	[9,"gl_map_t_jiro_n",
	/*上*/[11],
	/*下*/[0,2,3,4,6,7,10],
	/*右*/[1,2,4,7,8,9,10],
	/*左*/[1,2,3,5,6,9,10]
	],
	[10,"gl_map_t_jiro_s",
	/*上*/[0,2,3,4,5,8,9],
	/*下*/[11],
	/*右*/[1,2,4,7,8,9,10],
	/*左*/[1,2,3,5,6,9,10]
	],
	[11,"gl_map_stop",
	/*上*/[0,2,3,4,5,8,9],
	/*下*/[0,2,3,4,6,7,10],
	/*右*/[1,2,4,7,8,9,10],
	/*左*/[1,2,3,5,6,9,10]
	],
	[12,"gl_map_squear",
	/*上*/[0,2,3,4,5,8,9],
	/*下*/[0,2,3,4,6,7,10],
	/*右*/[1,2,4,7,8,9,10],
	/*左*/[1,2,3,5,6,9,10]
	],
	[13,"gl_map_none",
	/*上*/[1,6,7,10],
	/*下*/[1,5,8,9],
	/*右*/[0,3,5,6,],
	/*左*/[0,4,7,8]
	],
];
//マップパーツイメージ
var gl_map_part_image_path = [
	"./MapChip/map_0.png",
	"./MapChip/map_1.png",
	"./MapChip/map_2.png",
	"./MapChip/map_3.png",
	"./MapChip/map_4.png",
	"./MapChip/map_5.png",
	"./MapChip/map_6.png",
	"./MapChip/map_7.png",
	"./MapChip/map_8.png",
	"./MapChip/map_9.png",
	"./MapChip/map_a.png",
	"./MapChip/map_b.png",
	"./MapChip/map_c.png",
	"./MapChip/map_d.png",
	"./MapChip/map_b1.png",
];
var gl_map_data = [];
var gl_map_data2 = [];
var ctx = null;
var gl_img = [];
var gl_index;
var gl_size = 32;
var x_extent = 20;
var y_extent = 20;
function main(){
	//各種要素設定
	var elm = glObj.getElement("size");
	var elm2 = glObj.getElement("init");
	elm.value = x_extent;
	elm2.onclick = function(){
		var elm = glObj.getElement("size");
		var elm2 = glObj.getElement("imgView");
		x_extent = elm.value;
 		y_extent = elm.value;
		setimagedata();
		ctx = imgView.getContext('2d');
		elm2.width = eval(gl_size*x_extent);
		elm2.height = eval(gl_size*y_extent);
		elm2.globalAlpha = 1;
		elm2.globalCompositeOperation = "source-in";		
		glObj.setTimer(tiemrfunc,100);
	}
	setimagedata();
	setting();
	gl_index = 0;
	//描画
	glObj.setTimer(tiemrfunc,100);
}
function setimagedata(){
	gl_map_data = [];
	gl_map_data2 = [];
	//Y軸X軸0.0から埋める
	for(y_loop = 0;y_loop<y_extent;y_loop++){
		var setData = []
		var setData2 = []
		for(x_loop = 0;x_loop<x_extent;x_loop++){
			//0,0の初期設定用
			var start = [5,11,12];
			//初期設定ランダム設定
			var setindex = start[parseInt((Math.random()*start.length))];
			//最初の1行目（X軸）設定
			if((x_loop!=0)&&(y_loop==0)){
				var idx = setData[setData.length-1];
				var buff = map_make_parts[idx][4];
				var data = [];
				for(var lp in buff){
					if(buff[lp]==undefined)continue;
					if(x_loop!=19&&(buff[lp]==1||buff[lp]==8||buff[lp]==9)){
						if(idx!=1&&idx!=9&&idx!=11)continue;
						data[data.length] = buff[lp];
					}else if(x_loop==19&&buff[lp]==8){
						if(idx!=1&&idx!=9&&idx!=11)continue;
						data[data.length] = buff[lp];						
					}else if(x_loop==19){
						data[data.length] = 11;												
					}
				}
				if(data.length==0){
					data = [11]
				}					
				var setindex = data[parseInt((Math.random()*data.length))];
				console.log("y_loop==0:"+setindex);
			}
			//2行目以降の1列目（Y軸）設定
			else if((x_loop==0)&&(y_loop!=0)){
				var idx = gl_map_data[gl_map_data.length-1][x_loop];
				var buff = map_make_parts[idx][3];
				var data = [];
				for(var lp in buff){
					if(buff[lp]==undefined)continue;
					if(y_loop!=19&&(buff[lp]==0||buff[lp]==3||buff[lp]==6)){
						data[data.length] = buff[lp];
					}else if(y_loop==19&&(buff[lp]==0||buff[lp]==6)){
						data[data.length] = buff[lp];
					}
				}
				if(data.length==0)data = [11];				
				var setindex = data[parseInt((Math.random()*data.length))];
				console.log("x_loop==0:"+setindex);
			}
			//2行目以降の通常設定
			else if((x_loop!=0)&&(y_loop!=0)){
				var idx = setData[setData.length-1];
				var idx2 = gl_map_data[gl_map_data.length-1][x_loop];
				var data1 = map_make_parts[idx][4];
				var data2 = map_make_parts[idx2][3];
				var data = [];
				//設定位置上左側の設定値が共に2つ以上の設定値を持っているなら
				if(data1.length!=1&&data2.length!=1){
					//一致する設定値を設定候補に入れる
					for(var lp1 in data1){
						for(var lp2 in data2){
							if(data1[lp1]==undefined)continue;
							if(data2[lp2]==undefined)continue;
							if(data1[lp1]==data2[lp2]){
								if(x_loop!=19&&y_loop!=19){
									data[data.length] = data1[lp1];
								}else if(x_loop==19&&y_loop!=19){
									if(data1[lp1]==4||data1[lp1]==8||data1[lp1]==11){
										data[data.length] = data1[lp1];
									}
								}else{
									console.log("set1 y_loop=="+y_loop);
									if(data1[lp1]==7||data1[lp1]==11){
										data[data.length] = data1[lp1];
									}									
								}
							}
						}
					}
					
				}
				//設定値候補がないなら
				if(data.length==0){
					//設定値個数が多い方を採用する。
					if(data1.length>=data2.length){
						buff = data1;
						for(var lp in buff){
							if(buff[lp]==undefined)continue;
							if(buff[lp]==1||buff[lp]==4||buff[lp]==8||buff[lp]==9){
								if(idx!=11&&buff[lp]==10)continue;
								if(idx!=11&&buff[lp]==7)continue;
								if(buff[lp]==4)continue;
								if(x_loop!=19&&y_loop!=19){
									data[data.length] = buff[lp];
								}else if(x_loop==19&&y_loop!=19){
									if(buff[lp]==1||buff[lp]==4||buff[lp]==8){
										data[data.length] = buff[lp];
									}
								}else{
									if(buff[lp]==4||buff[lp]==7){
										data[data.length] = buff[lp];
									}									
								}
							}else{
								if(idx!=11&&buff[lp]==10)continue;
								if(idx!=11&&buff[lp]==7)continue;
								if(buff[lp]==4)continue;								if(x_loop==19&&y_loop!=19){
									if(buff[lp]==11){
										data[data.length] = buff[lp];
									}
								}else if(x_loop==19&&y_loop==19){
									if(buff[lp]==4||buff[lp]==11){
										data[data.length] = buff[lp];
									}									
								}								
							}
						}
						if(data.length==0)data = [11];
					}else{
						buff = data2;
						for(var lp in buff){
							if(buff[lp]==undefined)continue;
							if(buff[lp]==0||buff[lp]==6){
								if(idx2!=11&&buff[lp]==10)continue;
								if(idx2!=11&&buff[lp]==7)continue;
								if(idx2!=11&&buff[lp]==4&&x_loop!=19)continue;
								if(x_loop!=19&&y_loop!=19){
									data[data.length] = buff[lp];
								}else if(x_loop==19&&y_loop!=19){
									if(buff[lp]==0||buff[lp]==4){
										data[data.length] = buff[lp];
									}
								}else{
									if(buff[lp]==7||buff[lp]==11){
										data[data.length] = buff[lp];
									}									
								}								
							}else{
								if(idx2!=11&&buff[lp]==10)continue;
								if(idx2!=11&&buff[lp]==7)continue;
								if(x_loop==19&&y_loop!=19){
									if(buff[lp]==11||((buff[lp]==8||buff[lp]==4)&&(idx==1||idx==2||idx==3||idx==5||idx==6||idx==9||idx==10||idx==11||idx==12))){
										data[data.length] = buff[lp];
									}
								}else if(x_loop==19&&y_loop==19){
									if(buff[lp]==4||buff[lp]==7||buff[lp]==11){
										data[data.length] = buff[lp];
									}									
								}								
							}
						}
						if(data.length==0)data = [11];
					}
					
				}
				var setindex = data[parseInt((Math.random()*data.length))];
				if(x_loop==19){
					console.log("x_loop==19,y_loop=="+y_loop+":"+setindex);
				}
				if(y_loop==19){
					console.log("x_loop=="+x_loop+",y_loop==19:"+setindex);
				}				
			}
			setData[setData.length] = map_make_parts[setindex][0];
			setData2[setData2.length] = map_make_parts[setindex][1];
		}
		gl_map_data[gl_map_data.length] = setData;
		gl_map_data2[gl_map_data2.length] = setData2;
	}	
}
function tiemrfunc(){
	if(gl_img.length!=gl_map_part_image_path.length){
		glObj.setTimer(tiemrfunc,100);
		return;
	}
	//並べ替え
	var buff = [];
	for(var lp1 in gl_img){
		if(gl_img[parseInt(lp1)+1]==undefined)continue;
		var lp2 = parseInt(lp1)+1;
		if(gl_img[lp1].src>gl_img[lp2].src){
			buff = gl_img[lp1];
			gl_img[lp1] = gl_img[lp2];
			gl_img[lp2] = buff;
		}
	}
	//初期表示
	console.log(JSON.stringify(gl_map_data));
	console.log(JSON.stringify(gl_map_data2));
	for(var lp1 in gl_img){
		if(gl_img[lp1]==undefined)continue;
		console.log(gl_img[lp1].src);
	}
	for(val1 in gl_map_data){
		if(gl_map_data[val1]==undefined)continue;
		for(val2 in gl_map_data[val1]){
			var index = gl_map_data[val1][val2];
			if(index==undefined)continue;
			glObj.setTimer(function(data){
				var index = data[2];
				var val1 = data[0];
				var val2 = data[1];
				ctx.drawImage(gl_img[index],val2*gl_size,val1*gl_size,gl_size,gl_size);
			},500,[val1,val2,index]);
		}
	}
}
//マップパーツ読み込み
function MapPartImageLoad(){
	//ループ
	var local_lock = false;
	if(gl_img.length!=gl_map_part_image_path.length){
		var load_image = new Image();
		if(!local_lock){
			local_lock = true;
			load_image.src = gl_map_part_image_path[gl_img.length];
			load_image.onload = function(){
				gl_img.push(load_image);
				local_lock = false;
				setTimeout(MapPartImageLoad,100);
			}
		}
	}
}
//初期設定
function setting(){
	//画像表示領域生成
	var elm = glObj.createElement("canvas","imgView","",function(elmin){
		var elmbody = glObj.getElement("body");
		elmin.onmousedown = function(event){
			var x_pos = parseInt(event.x/gl_size);
			var y_pos = parseInt(event.y/gl_size);
			if((x_pos>x_extent)||(y_pos>y_extent))return;
			ctx.drawImage(gl_img[gl_index],x_pos*gl_size,y_pos*gl_size,gl_size,gl_size);
			gl_map_data2[y_pos][x_pos] = map_make_parts[gl_index][1];
			if(gl_index>=gl_map_part_image_path.length-1){
				gl_index = 0;
			}else{
				gl_index++;
			}
			var text = glObj.getElement("viewdata");
			text.value = "";
			for(y_index in gl_map_data2){
				if(gl_map_data2[y_index]==undefined)continue;
				text.value = text.value+"[";
				for(x_index in gl_map_data2[y_index]){
					if(gl_map_data2[y_index][x_index]==undefined)continue;
					text.value = text.value+gl_map_data2[y_index][x_index];
					if(x_index<gl_map_data2[y_index].length-1){
						text.value = text.value+",";
					}
				}
				text.value = text.value+"],\n";				
			}
		}
		glObj.addElement(elmbody,elmin);		
	});
	ctx = imgView.getContext('2d');
	elm.width = eval(gl_size*x_extent);
	elm.height = eval(gl_size*y_extent);
	elm.globalAlpha = 1;
	elm.globalCompositeOperation = "source-in";
	//画像データ取得
	/*for(imgIndex in gl_map_part_image_path){
		var img = new Image();
		if(gl_map_part_image_path[imgIndex]==undefined)continue;
		img.src = gl_map_part_image_path[imgIndex];
		img.onload = function(){
			console.log(this.src);
			gl_img.push(this);
		}
	}*/
	MapPartImageLoad();
	glObj.createElement("br","","",function(elmin){
		var elmbody = glObj.getElement("body");
		glObj.addElement(elmbody,elmin);
	});	
	glObj.createElement("textarea","viewdata","",function(elmin){
		var elmbody = glObj.getElement("body");
		elmin.rows="10";
		elmin.cols="40";
		elmin.wrap="hard"
		glObj.addElement(elmbody,elmin);
	});
}
//