bool GenerateMap(ArrayGrid <MapElement> map, ArrayGrid <int> valueMap) { // create a game of life cave int densityCount = Mathf.FloorToInt(map.Count * density); //fill with random walls for (int i = 0; i < densityCount; ++i) { Vector2Int?fillPos = map.GetRandomPositionOfType(defaultFillElement); if (fillPos == null || !fillPos.HasValue) { return(false); } map[fillPos.Value] = defaultWallElement; } for (int i = 0; i < iterations; ++i) { IEnumerator <Vector2Int> mapIter = IterateOverMap(map); while (mapIter.MoveNext()) { Vector2Int current = mapIter.Current; int n = map.GetAdjacentPositionsOfType(current, true, defaultWallElement).Count; if (map[current] == defaultWallElement) { if (n < 4) { map[current] = defaultRoomElement; } } else { if (n > 4) { map[current] = defaultWallElement; } } if (map.IsPositionOnEdge(current)) { map[current] = defaultWallElement; } } } ConnectClosestRooms(map, valueMap, false, false); ConvertValuesToTiles(map, valueMap); return(true); }
bool GenerateMap(ArrayGrid <MapElement> map, ArrayGrid <int> valueMap) { map[map.CenterPosition] = defaultCorridorElement; Vector2Int s0 = Vector2Int.zero; Vector2 s1 = Vector2Int.zero; Vector2Int p = Vector2Int.zero; Vector2 d = Vector2Int.zero; int iterCount = map.Count / 3; for (int i = 0; i < iterCount; i++) { s1 = GameRNG.RandomPointOnCircle(map.Size.DivideBy(2)) + map.CenterPosition; d.x = strideLimitX.RandomNormalizedValue(); d.y = strideLimitY.RandomNormalizedValue(); d.x -= .5f; d.y -= .5f; int counter = 0; for (; ;) { //try again if (counter > maxTunnelIterations) { --i; break; } s1 += d; p = Vector2Int.FloorToInt(s1); p = map.Wrap(p); if (map.IsValidPosition(p) && map.GetAdjacentPositionsOfType(p, false, defaultCorridorElement).Count > 0) { map[p] = defaultCorridorElement; break; } } } if (placeRooms) { var areaIter = Mathnv.GetAreaEnumerator(Vector2Int.one, map.MaxValidPosition - Vector2Int.one); while (areaIter.MoveNext()) { Vector2Int current = areaIter.Current; Range limitX = new Range(map.w / 2 - strideLimitX.Min, map.w / 2 + strideLimitX.Min); Range limitY = new Range(map.h / 2 - strideLimitY.Min, map.h / 2 + strideLimitY.Min); if ((limitX.Contains(current.x) && limitY.Contains(current.y)) || map[current] == defaultWallElement) { continue; } int n = map.GetAdjacentPositionsOfType(current, false, defaultCorridorElement).Count; if (n == 1) { Rect room = new Rect(Vector2.zero, new Vector2(roomSizeX.RandomValuei(), roomSizeY.RandomValuei())); var roomIter = Mathnv.GetAreaEnumerator(Vector2Int.FloorToInt(room.min), Vector2Int.FloorToInt(room.max)); while (roomIter.MoveNext()) { Vector2Int rCurrent = roomIter.Current; map[current + rCurrent] = defaultRoomElement; } } } } return(true); }