using UnityEngine;

public class WorldGenerator {
	private static int rounds = 0;

	public static void gen(uint width, uint height) {
		Map.init(width, height);

		bool genOver = false;
		while(!genOver) {
			genOver = genStep();
		}
	}

	public static bool genStep() {
		//Cradle of culture
		if (Controller.cultures.Count < 4 && Helper.getRandom() < 0.1f) {
			foreach(Region region in Map.regions) {
				if (region.biome.habitability >= 0.8f && Helper.getRandom() < 0.1f && region.culture == null) {
					region.setCulture(new Culture());
					break;
				}
			}
		}

		//Culture Spread
		foreach(Region region in Map.regions) {
			if (region.culture != null) {
				foreach(Region ngh in region.nghs) {
					if (ngh.culture == null && Helper.getRandom() < 0.05f * ngh.biome.habitability) {
						ngh.setCulture(region.culture);
					}
				}
			}
		}

		//Culture Split
		foreach(Culture culture in Controller.cultures) {
			if (culture.regions.Count > 5 && Helper.getRandom() < Mathf.Pow(culture.regions.Count, 3) / 1000.0f) {
				culture.split();
			}
		}
		Controller.cultures.UnionWith(Controller.newCultures);
		Controller.newCultures.Clear();

		rounds++;
		if (rounds > 1000) {
			return true;
		}

		return false;
	}
}
