/// 地図画像作成用スクリプト

width = 15;
height = 15;

gType = 1;  // 遺跡

/// @brief 全体を初期化する
function initialize() {
	var map = document.getElementById("map");
	for (var i = 0; i < (width * height * 4); i++) {
		var img = document.createElement("span");
		img.id = "img" + i;
		img.style.left = ((i % (width * 2)) * 8) + 'px';
		img.style.top = (Math.floor(i / (width * 2)) * 8) + 'px';
		updateClassAttribute(img, "map");
		map.appendChild(img);
	}

	gType = parseInt(document.getElementById("type").value);
	changeImageFile();
	update();
}

/// @brief マップタイプが変更されたときに背景ファイルを差し替える
function onChangeMapType() {
	gType = parseInt(document.getElementById("type").value);
	changeImageFile();
	update();
}


/// @brief 背景ファイルを差し替える
function changeImageFile() {
	var backgroundImage = "url(./map" + gType + ".png)";
	for (var i = 0; i < (width * height * 4); i++) {
		var img = document.getElementById("img" + i);
		img.style.backgroundImage = backgroundImage;
	}
}


/// @brief class属性をつける
function updateClassAttribute(element, value) {
	if (value == null) {
		element.removeAttribute("class");
		element.removeAttribute("className");
	} else {
		element.setAttribute("class", value);
		element.setAttribute("className", value);
	}
}


/// @brief
function getImage(x, y, type) {
	var index = y*width*4 + x*2;
	if ((type == 1) || (type == 2)) {
		index += 1;
	}
	if ((type == 2) || (type == 3)) {
		index += width*2;
	}
	return document.getElementById("img" + index);
}


///
function getMonumentImageIndex(ary, type) 
{
	var index = 1;
	if (!ary[1] && !ary[7]) {
		if (ary[3] && ary[4] && ary[5]) {
			index = 0;
		} else {
			index = 42+type;
		}
	} else {
		if (ary[0]) {
			if (ary[1] && ary[7]) {
				if (!ary[3] && !ary[5]) {
					index = 54 + type;
				} else {
					index = 1;
				}
			} else if (ary[1] && !ary[7]) {
				if (ary[3]) {
					if (ary[2]) {
						if (ary[5]) {
							if (ary[4]) {
								index = 2 + type;
							} else {
								index = 38 + type;
							}
						} else {
							index = 58 + (type+2)%4;
						}
					} else {
						if (ary[5]) {
							if (ary[4]) {
								index = 50 + type;
							} else {
								index = 18 + type;
							}
						} else {
							index = 10 + type;
						}
					}
				} else {
					index = 10 + type;
				}
			} else if (!ary[1] && ary[7]) {
				if (ary[5]) {
					if (ary[6]) {
						if (ary[3]) {
							if (ary[4]) {
								index = 6 + type;
							} else {
								index = 34 + type;
							}
						} else {
							index = 58 + type;
						}
					} else {
						if (ary[3]) {
							if (ary[4]) {
								index = 50 + (type+1)%4;
							} else {
								index = 22 + type;
							}
						} else {
							index = 14 + type;
						}
					}
				} else {
					index = 14 + type;
				}
			}
		} else if (!ary[0]) {
			if (ary[1] && ary[7]) {
				if ((ary[2] && ary[3]) && (ary[5] && ary[6])) {
					index = 54 + (type+2)%4;
				} else if ((ary[2] && ary[3]) && !(ary[5] && ary[6])) {
						index = 30 + type;
				} else if (!(ary[2] && ary[3]) && (ary[5] && ary[6])) {
						index = 26 + type;
				} else if (!(ary[2] && ary[3]) && !(ary[5] && ary[6])) {
						index = 46 + type;
				}
			} else if (ary[1] && !ary[7]) {
				if (ary[3]) {
					if (ary[5]) {
						if (ary[2]) {
							if (ary[4]) {
								index = 2 + type;
							} else {
								index = 38 + type;
							}
						} else {
							if (ary[4]) {
								index = 50 + type;
							} else {
								index = 18 + type;
							}
						}
					} else {
						if (ary[2]) {
							index = 58 + (type+2)%4;
						} else {
							index = 10 + type;
						}
					}
				} else {
					index = 10 + type;
				}
			} else if (!ary[1] && ary[7]) {
				if (ary[5]) {
					if (ary[3]) {
						if (ary[6]) {
							if (ary[4]) {
								index = 6 + type;
							} else {
								index = 34 + type;
							}
						} else {
							if (ary[4]) {
								index = 50 + (type+1)%4;
							} else {
								index = 22 + type;
							}
						}
					} else {
						if (ary[6]) {
							index = 58 + type;
						} else {
							index = 14 + type;
						}
					}
				} else {
					index = 14 + type;
				}
			}
		}
	}
	
	return index;
}

/// @brief 1つの画像を更新する (遺跡限定)
function updateMonumentImage(ary, x, y, type) 
{
	var img = getImage(x, y, type);
	
	var index = 0;
	if (ary[0]) {
		var ary2 = new Array(8);
		for (var i = 0; i < 8; i++) {
			ary2[i] = ary[1 + ((i + type*2) % 8)];
		}
		index = getMonumentImageIndex(ary2, type) 
	}
	img.style.backgroundPosition = -8 * index + "px 0";
}


/// @brief 境界線の種類を返す
function getImageIndex(ary) 
{
	if (ary[0]) {
		if (ary[1] && ary[7]) {
			return 1;
		} else if (ary[1] && !ary[7]) {
			return 3;
		} else if (!ary[1] && ary[7]) {
			return 4;
		} else if (!ary[1] && !ary[7]) {
			return 2;
		}
	} else if (!ary[0]) {
		if (ary[1] && ary[7]) {
			return 5;
		} else if (ary[1] && !ary[7]) {
			return 3;
		} else if (!ary[1] && ary[7]) {
			return 4;
		} else if (!ary[1] && !ary[7]) {
			return 2;
		}
	}
}


/// @brief 1つの画像を更新する
function updateImage(ary, x, y, type) 
{
	if (gType == 1) {
		updateMonumentImage(ary, x, y, type);
		return;
	}
	
	var img = getImage(x, y, type);
	
	var index = 0;
	if (ary[0]) {
		var ary2 = new Array(8);
		for (var i = 0; i < 8; i++) {
			var j = (type % 2 == 0) ? i : (8-i) % 8;
			ary2[j] = ary[1 + ((i + type*2) % 8)];
		}
		
		index = getImageIndex(ary2) 
	}
	
	var x = 0;
	if ((type == 1) || (type == 2)) {
		x -= 8;
	}
	
	var y = -index * 16;
	if ((type == 2) || (type == 3)) {
		y -= 8;
	}

	img.style.backgroundPosition = x + "px " + y + "px";
}


/// @brief 1マス内の4つの画像を更新する
function updateImages(ary, x, y) 
{
	var ary2 = new Array(9);  /// [中央, 左上, 上, 右上, 右, 右下, 下, 左下, 左]
	ary2[0] = ary[y*width + x];
	ary2[1] = (x>0 && y>0) ? ary[(y-1)*width + x-1] : false;
	ary2[2] = (y>0) ? ary[(y-1)*width + x] : false;
	ary2[3] = (x<(width-1) && y>0) ? ary[(y-1)*width + x+1] : false;
	ary2[4] = (x<(width-1)) ? ary[y*width + x+1] : false;
	ary2[5] = (x<(width-1) && y<(height-1)) ? ary[(y+1)*width + x+1] : false;
	ary2[6] = (y<(height-1)) ? ary[(y+1)*width + x] : false;
	ary2[7] = (x>0 && y<(height-1)) ? ary[(y+1)*width + x-1] : false;
	ary2[8] = (x>0) ? ary[y*width + x-1] : false;
	
	for (var type = 0; type < 4; type++) {
		updateImage(ary2, x, y, type);
	}
}


/// @brief 地図を更新する
function update() 
{
	var ary = new Array(width * height);
	for (var i = 0; i < width * height; i++) {
		ary[i] = false;
	}
	
	var lines = document.getElementById("text").value.split(/[\n\r]+/);
	for (var i = 0; i < Math.min(lines.length, height); i++) {
		var line = lines[i];
		for (j = 0; j < Math.min(line.length, width); j++) {
			if (line.charAt(j) != " ") {
				ary[i*width + j] = true;
			}
		}
	}
	
	for (var i = 0; i < height; i++) {
		for (var j = 0; j < width; j++) {
			updateImages(ary, j, i);
		}
	}
}
