/// <summary> /// Generates a new random map with the given parameters. /// </summary> /// <param name="width">Map width in cells</param> /// <param name="height">Map height in cells</param> /// <param name="config">Configuration</param> /// <returns>The map</returns> public static Map Generate(int width, int height, AutomataParams config = null) { if (config == null) { config = new AutomataParams(); } var map = new Map(width, height); // Initialize map for (var x = 0; x < map.Width; x++) { for (var y = 0; y < map.Height; y++) { if (Random.Range(0.0f, 1.0f) < config.InitialSpawn) { map.Data [x, y] = CellType.Dead; } } } for (var n = 0; n < config.Passes; n++) { map = Calculate(map, config); } return(map); }
/// <summary> /// Performs a map calculation pass. /// </summary> /// <param name="oldMap">The old map</param> /// <param name="config">The confi</param> /// <returns>The processed map</returns> private static Map Calculate(Map oldMap, AutomataParams config) { var data = new CellType [oldMap.Width, oldMap.Height]; for (int x = 0; x < oldMap.Width; x++) { for (int y = 0; y < oldMap.Height; y++) { int alive = CountAliveNeighbours(oldMap, x, y); if (oldMap.Data [x, y] == CellType.Dead) { if (alive < config.DeathLimit) { data [x, y] = CellType.Open; } else { data [x, y] = CellType.Dead; } } else { if (alive > config.BirthLimit) { data [x, y] = CellType.Dead; } else { data [x, y] = CellType.Open; } } } } oldMap.Data = data; return(oldMap); }