/**
 * Name: Standard map script.
 * By:   FireFly (firefly.nu).
 * Date: 2009-04-22.
 */
function generateStandardMap(width, height, types, probability,
		amount, multiplier) {
	var landTiles = 0;
	var map = new Array(width);
	var setGrass = function(x, y, prob) {
		if (x<0 || y<0 || x>=width || y>=height || map[x][y].type == types[0] ||
				landTiles/(1.0*width*height)>=amount || Game.random.nextFloat()>prob)
			return;
		map[x][y] = new MapTile(x, y, Game.random.nextInt(0, 3), map, types[0]);
		landTiles++;
		
		var ps = [[-1, -1], [0, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1]];
		while (ps.length > 0) {
			var p = ps.splice(Game.random.nextInt(0, ps.length), 1)[0];
			setGrass(x-p[0], y-p[1], prob*multiplier);
		}
	}
	
		/* Fyll med vatten. */
	for (var x=0; x<map.length; x++) {
		map[x] = new Array(height);
		for (var y=0; y<map[x].length; y++)
			map[x][y] = new MapTile(x, y, 0, map, types[1]);
	}
	
		/* Rita ut land. */
	while (landTiles/(1.0*width*height) < amount) {
		var x = Game.random.nextInt(0, width);
		var y = Game.random.nextInt(0, height);
		setGrass(x, y, probability);
	}
	
		/* Plocka bort encells-vattenbitar. */
	var noWaterNeighs = function(t) {
		return t.type == types[1];
	};
	for (var x=0; x<map.length; x++) {
		for (var y=0; y<map[x].length; y++) {
			if (map[x][y].type == types[1] && map[x][y].neighbours(noWaterNeighs) == 0) {
				map[x][y].type = types[0];
				map[x][y].height = 0;
			}
		}
	}
	
	Debug.print("Karta: " + Math.round(landTiles/(width*height)*100) +"% land (" +
			width + "x" + height + ").");
	Debug.print(" -- Karta genererad!");
	return map;
}