public static List <(int, int)> FindPattern(ref int[,] grid, ref bool[,] gridLock, int[,] pattern) { List <(int, int)> result = new List <(int, int)>(); List <(int, int)> countedPositions = new List <(int, int)>(); (int width, int depth) = Grids.Dim(grid); int patternWidth = pattern.GetLength(0); int patternDepth = pattern.GetLength(1); // 1. foreach ((int countW, int countD) in Itr.IterationRange(1, width - patternWidth, 1, depth - patternDepth)) { if (countedPositions.Contains((countW, countD)) || gridLock[countW, countD]) { continue; } // overlay at position bool match = Overlay(grid, gridLock, pattern, countW, countD, countedPositions); if (match) { MarkAsCounted(pattern, ref grid, ref gridLock, countW, countD, ref countedPositions); result.Add((countW, countD)); } } return(result); }
private int[,] RotatePattern(int[,] pattern, int angle) { (int width, int depth) = Grids.Dim(pattern); int[,] rotatedPattern = Grids.Blank(pattern); foreach ((int countW, int countD) in Itr.Iteration(width, depth)) { rotatedPattern[countD, width - countW - 1] = pattern[countW, countD]; } return(rotatedPattern); }
internal static int[,] Flatten(int[,] grid, int flat) { (int width, int depth) = Grids.Dim(grid); int[,] result = new int[width, depth]; foreach ((int countW, int countD) in Itr.Iteration(width, depth)) { if (grid[countW, countD] != flat) { result[countW, countD] = 1; } else { result[countW, countD] = 0; } } return(result); }
public static (int[, ], bool[, ]) CreateBordersGrid(int[,] grid, bool[,] gridLock, int index) { (int width, int depth) = Grids.Dim(grid); int podW = 0; int posD = 0; for (int countW = 0; countW < grid.GetLength(0); countW += 1) { podW = countW; posD = 0; grid[podW, posD] = index; gridLock[podW, posD] = true; } for (int countW = 0; countW < grid.GetLength(0); countW += 1) { podW = countW; posD = grid.GetLength(1) - 1; grid[podW, posD] = index; gridLock[podW, posD] = true; } for (int countD = 0; countD < grid.GetLength(1); countD += 1) { podW = 0; posD = countD; grid[podW, posD] = index; gridLock[podW, posD] = true; } for (int countD = 0; countD < grid.GetLength(1); countD += 1) { podW = grid.GetLength(0) - 1; posD = countD; grid[podW, posD] = index; gridLock[podW, posD] = true; } Debug.Log("Borders ON"); return(grid, gridLock); }
internal static int[,] AddNbrs(int[,] grid, int minCount) { (int width, int depth) = Grids.Dim(grid); int[,] result = Copy(grid); int[] pos = new int[4] { 0, 1, 0, -1 }; foreach ((int countW, int countD) in Itr.IterationRange(1, width - 1, 1, depth - 1)) { if (result[countW, countD] == 1) { continue; } int nbrs = 0; bool mark = false; for (int count = 0; count < 4; count += 1) { int nbrW = countW + pos[(count + 3) % 4]; int nbrD = countD + pos[count]; if (grid[nbrW, nbrD] != 0) { nbrs++; if (nbrs >= minCount) { mark = true; break; } } } if (mark) { result[countW, countD] = 1; } } return(result); }
internal static int[,] ApplyMask(int[,] gridBase, int[,] gridMask, bool[,] gridLock) { // sanity check if (gridBase.GetLength(0) != gridMask.GetLength(0) || gridBase.GetLength(1) != gridMask.GetLength(1)) { throw new ArgumentException("Grid dimentions don't match"); } (int width, int depth) = Grids.Dim(gridBase); foreach ((int countW, int countD) in Itr.Iteration(width, depth)) { if (gridLock[countW, countD]) { gridBase[countW, countD] = gridMask[countW, countD]; } } return(gridBase); }
internal static int[,] ApplyMaskIndex(int[,] gridBase, bool[,] gridLock, int[,] gridMask, int indexMask, int indexBase) { // Applies mask to grid - translate indexMask from mask to indexBase in grid // sanity check if (gridBase.GetLength(0) != gridMask.GetLength(0) || gridBase.GetLength(1) != gridMask.GetLength(1)) { throw new ArgumentException("Grid dimentions don't match"); } (int width, int depth) = Grids.Dim(gridBase); foreach ((int countW, int countD) in Itr.Iteration(width, depth)) { if (gridMask[countW, countD] == indexMask) { gridBase[countW, countD] = indexBase; gridLock[countW, countD] = true; } } return(gridBase); }
private void MaterializeFloor(int[,] grid) { // clear all previously created objects foreach (Transform obj in mapHolder) { Destroy(obj.gameObject); } (int width, int depth) = Grids.Dim(finalGrid); gridFloor = new Transform[width, depth]; gridElements = new Transform[width, depth]; // Get Biome Index // place floor everywhere foreach ((int countW, int countD) in Itr.Iteration(width, depth)) { int biomeIndex = gridBiomes[countW, countD]; Transform floor = library.GetElement(biomeIndex, 0); gridElements[countW, countD] = Instantiate(floor, new Vector3(countW, 0, countD), Quaternion.identity, floorHolder); // gridFloor[countW, countD] = Instantiate(floor, new Vector3(countW, 0, countD), Quaternion.identity, floorHolder); } // Iterate finalGrid // if 0 - skip // if not -- place an object // HERE foreach ((int countW, int countD) in Itr.Iteration(width, depth)) { Transform obj; if (grid[countW, countD] == 0) { continue; } // if (grid[countW, countD] != 0) // { // Debug.Log($"obj at: {countW}/{countD} -- {grid[countW, countD]}"); // } // DBG Trying with the collection // obj = mapElements[grid[countW, countD]]; int biomeIndex = gridBiomes[countW, countD]; int elementIndex = grid[countW, countD]; obj = library.GetElement(biomeIndex, elementIndex); // obj = library.elementPool[elementIndex].prefab; // FIXME int positionX = countW; int positionY = countD; Quaternion rotation = Quaternion.identity; Vector3 position = new Vector3(positionX, 0, positionY); // position = new Vector3(positionX + 0.5f, 0, positionY + 0.5f); // obj.GetChild(0).transform.rotation = Quaternion.Euler(0, Random.Range(0.0f, 360.0f), 0); gridElements[positionX, positionY] = Instantiate(obj, position, rotation); gridElements[positionX, positionY].parent = mapHolder; } Debug.Log("Materialize"); }