package de.heatup.mapengine;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import de.heatup.cfg.Config;

public class SpawnAssistant {
	public static ArrayList<Point> spawnPoints = new ArrayList<Point>();
	/**
	 * Sucht einen Spawnpunkt mit höchstmöglichem Abstand zu anderen Spawnpoints
	 * @return Returns a Spawnpoint
	 */
	public static Point getPathLocationOnGrid() {
		Point spawnPoint = null;
		Random rand = new Random();
		int distance = Config.spawnDistance;
		int errorTolerance = 100;
		int tryCount = 0;
		boolean foundLocation = false;
		while (!foundLocation) {
			tryCount++;
			if (tryCount==errorTolerance) {
				tryCount = 0;
				distance--;
			}
			int x = rand.nextInt(Config.mapMaxTilesWidth);
			int y = rand.nextInt(Config.mapMaxTilesHeight);
			if (MapGrid.getGridCell(x, y).isPath()) {
				boolean distanceOk = true;
				if (spawnPoints.isEmpty()) distanceOk = true;
				for (Point point : spawnPoints) {
					if (Math.abs(x-point.x)<distance && Math.abs(y-point.y)<distance) {
						distanceOk = false;
					}
				}
				if (distanceOk) {
					spawnPoint = new Point(x,y);
					foundLocation = true;
				}
			}
		}
		spawnPoints.add(spawnPoint);
		return spawnPoint;
	}
}
