package com.pesets.game.map;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import com.pesets.game.map.SpawnMap.SpawnPoint;


public class MapGenerator {

	public static PassageMap generatePassageMap(int seed, int width, int height)
	{
		PassageMap map = new PassageMap(width, height);
		Random rand = new Random(seed);
		
		for (int i = 0; i < width; i++){
			for (int j = 0; j < height; j++)
			{
				if (rand.nextFloat() > 0.75f)
				{
					map.setPassage(i, j, false);
				}
				else
				{
					map.setPassage(i, j, true);
				}
			}
		}
		
		return map;
	}
	
	public static SpawnMap generateSpawnMap(PassageMap map, int seed, int minLevel, int maxLevel)
	{
		SpawnMap spawn = new SpawnMap();
		Random rand = new Random(seed);
		int spawnPointCount = (map.getWidth() * map.getHeight()) / 70;
		
		int px = map.getHeight() / 2;
		int py = 0;
		spawn.getPlayerPos().setLocation(px, py);
		map.setPassage(px, py, true);
		
		Set<SpawnMap.SpawnPoint> createdPoints = new HashSet<SpawnMap.SpawnPoint>();
		for (int i = 0; i < spawnPointCount; i++)
		{
			SpawnPoint p = new SpawnPoint();
			p.mobType = minLevel / 5;
			
			do
			{
				p.x = rand.nextInt(map.getWidth());
				p.y = rand.nextInt(map.getHeight());
				
				if (createdPoints.contains(p)) continue;
				if (map.isCanPass(p.x, p.y) == false) continue;
				
				createdPoints.add(p);
				spawn.getSpawns().add(p);
				break;
			} while (true);
			
			p.level = rand.nextInt(1 + p.y / 6) + minLevel;
			p.level = Math.min(maxLevel, p.level);
		}
		
		return spawn;
	}
}
