Exemple #1
0
    private (int[, ], bool[, ]) PlaceSplotch(int[,] grid, int posW, int posD, int mag)
    {
        Vector3 center   = new Vector3(posW, 0, posD);
        Vector3 position = new Vector3(0, 0, 0);

        // iterate the vicinity of the splotch center
        foreach ((int countW, int countD) in Itr.IterationRange(posW - mag - splotchRimWidth, posW + mag + splotchRimWidth,
                                                                posD - mag - splotchRimWidth, posD + mag + splotchRimWidth))
        {
            position = new Vector3(countW, 0, countD);
            // if it is on the rim
            if ((GetDistance(position, center) >= (float)mag) &&
                ((GetDistance(position, center) <= (float)mag + splotchRimWidth)))
            {
                if (grid[countW, countD] == 1) // if there is a block
                {
                    grid[countW, countD]     = (int)LibraryElements.Elements.RingRim;
                    gridLock[countW, countD] = true;
                }
            }
            else if ((GetDistance(position, center) < mag)) // if it is inside the radius
            {
                grid[countW, countD]     = 0;
                gridLock[countW, countD] = true;
            }
        }

        grid[posW, posD] = (int)LibraryElements.Elements.Obelisk;

        return(grid, gridLock);
    }
Exemple #2
0
    private bool CheckForOverlaps(bool[,] grid, int posW, int posD, int mag)
    {
        bool    result   = true;
        Vector3 center   = new Vector3(posW, 0, posD);
        Vector3 position = new Vector3(0, 0, 0);

        // iterate the vicinity of the splotch center
        foreach ((int countW, int countD) in Itr.IterationRange(posW - mag - splotchRimWidth, posW + mag + splotchRimWidth,
                                                                posD - mag - splotchRimWidth, posD + mag + splotchRimWidth))
        {
            if ((GetDistance(position, center) <= mag)) // if it is inside the radius
            {
                if (!grid[countW, countD])              // if the position is not 0, then there is already a massive index there; so the whole checked space is unavailable
                {
                    result = false;
                    break;
                }
            }
            if (!result)
            {
                break;
            }
        }

        return(result);
    }
    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);
    }
Exemple #4
0
    public static int[,] GenerateGridWithFill(int width, int depth, int fill)
    {
        int[,] result = new int[width, depth];
        foreach ((int countW, int countD) in Itr.Iteration(width, depth))
        {
            result[countW, countD] = fill;
        }

        return(result);
    }
Exemple #5
0
    public static int[,] Copy(int[,] origin)
    {
        int[,] target = new int[origin.GetLength(0), origin.GetLength(1)];

        foreach ((int countA, int countB) in Itr.Iteration(origin.GetLength(0), origin.GetLength(1)))
        {
            target[countA, countB] = origin[countA, countB];
        }

        return(target);
    }
Exemple #6
0
    private Cell[,] CreateCellsGrid(int floorCellWidth, int floorCellDepth)
    {
        Cell[,] gridCells = new Cell[floorCellWidth, floorCellDepth];

        foreach ((int countW, int countD) in Itr.Iteration(floorCellWidth, floorCellDepth))
        {
            gridCells[countW, countD] = new Cell(cellCols, cellRows, cellMin, cellMax, (int)LibraryElements.Elements.Basic);
        }

        return(gridCells);
    }
Exemple #7
0
    /// <summary>
    /// Mark position of a cell for processing
    /// </summary>
    /// <remarks>
    /// posW, posD, object type, empty/block, poi
    /// </remarks>
    // private void MarkPosition(int[,] grid, int posW, int posD, int index)
    // {
    //     grid[posW, posD] = index;
    // }

    private int[,] FillGrid(int[,] origin, int value)
    {
        int[,] target = new int[origin.GetLength(0), origin.GetLength(1)];

        foreach ((int countA, int countB) in Itr.Iteration(origin.GetLength(0), origin.GetLength(1)))
        {
            target[countA, countB] = value;
        }

        return(target);
    }
Exemple #8
0
    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);
    }
Exemple #9
0
    public static int[,] MarkPattern(int[,] grid, bool[,] gridLock, int posW, int posD, int[,] pattern, int index)
    {
        // mark the pattern on the grid
        // mark all the cells of the pattern as 100 - to be avoided in the future
        foreach ((int countW, int countD) in Itr.Iteration(pattern.GetLength(0), pattern.GetLength(1)))
        {
            grid[posW + countW, posD + countD] = 100;
        }
        grid[posW, posD] = index;
        Debug.Log($"marked obj at {posW} / {posD}");

        return(grid);
    }
    private int GetWeight(int[,] pattern)
    {
        int counter = 0;

        foreach ((int countW, int countD) in Itr.Iteration(pattern.GetLength(0), pattern.GetLength(1)))
        {
            if (pattern[countW, countD] == 1)
            {
                counter++;
            }
        }

        return(counter);
    }
Exemple #11
0
    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);
    }
Exemple #12
0
    public static int[,] CreateBasicGrid(int floorCellWidth, int floorCellDepth, int cellCols, int cellRows, Cell[,] gridCells)
    {
        int[,] gridBase = new int[floorCellWidth * cellCols, floorCellDepth *cellRows];

        foreach ((int countW, int countD) in Itr.Iteration(floorCellWidth, floorCellDepth))
        {
            Cell currentCell = gridCells[countW, countD];
            foreach ((int cellW, int cellD) in Itr.Iteration(currentCell.Width, currentCell.Depth))
            {
                int posW  = countW * cellCols + cellW;
                int posD  = countD * cellRows + cellD;
                int value = currentCell.Grid()[cellW, cellD];
                gridBase[posW, posD] = value;
            }
        }

        Debug.Log("Basic ON");

        return(gridBase);
    }
Exemple #13
0
    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);
    }
Exemple #14
0
    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);
    }
Exemple #15
0
    private void ColorizeGrid(int[,] grid)
    {
        // DBG only while WIP
        // Colorize POI

        // Get library and palette
        Palette palette = FindObjectOfType <Palette>();

        // Iterate elements grid

        foreach ((int countW, int countD) in Itr.Iteration(width, depth))
        {
            Transform obj = gridElements[countW, countD];
            if (obj == null)
            {
                continue;
            }
            // gridElements[countW, countD].GetChild(0).GetComponent<MeshRenderer>().material.color = palette.palette[grid[countW, countD]];
            gridElements[countW, countD].GetChild(0).GetComponent <MeshRenderer>().material.color = biomeColors[grid[countW, countD]];
        }
        Debug.Log("Colorize");
    }
Exemple #16
0
    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);
    }
Exemple #17
0
    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");
    }
Exemple #18
0
    public void CreateSpawns(int[,] grid, float maxPointDeviation)
    {
        // Select an available point for starting the flood
        Vector3 floodPoint = ScoutAround(grid, new Vector3(grid.GetLength(0) / 2, 0, grid.GetLength(1) / 2));

        // Floodfill the plane
        List <Vector3> floodPlane = FloodThePlane(grid, floodPoint);

        // TODO - check if we flooded at least half the plane
        int planeSize = grid.GetLength(0) * grid.GetLength(1);

        if (floodPlane.Count < planeSize / 2)
        {
            // Debug.Log($"Flooded only {floodPlane.Count} out of {planeSize} cells");
            return;
        }

        int[,] floodGrid = new int[grid.GetLength(0), grid.GetLength(1)];
        // The grid needs to be reversed
        foreach ((int cols, int rows) in Itr.Iteration(grid.GetLength(0), grid.GetLength(1)))
        {
            floodGrid[cols, rows] = 1;
        }

        // Get the list into a grid
        foreach (Vector3 point in floodPlane)
        {
            floodGrid[(int)point.x, (int)point.z] = 0;
        }

        // Select random starting poins
        Vector3 leftSeedPoint  = new Vector3(floodGrid.GetLength(0) / 4, 0, floodGrid.GetLength(1) / 2);
        Vector3 rightSeedPoint = new Vector3(floodGrid.GetLength(0) / 2, 0, floodGrid.GetLength(1) / 4) * 3;

        Vector3 deviationLeft  = Random.insideUnitCircle.normalized;
        Vector3 deviationRight = Random.insideUnitCircle.normalized;

        Vector3 randomLeftNormal  = new Vector3(deviationLeft.x, 0, deviationLeft.z);
        Vector3 randomRightNormal = new Vector3(deviationRight.z, 0, deviationRight.z);

        float randomLeftMagnitude  = Random.Range(0, maxPointDeviation * floodGrid.GetLength(1));
        float randomRightMagnitude = Random.Range(0, maxPointDeviation * floodGrid.GetLength(1));

        Vector3 randomLeft  = randomLeftNormal * randomLeftMagnitude;
        Vector3 randomRight = randomRightNormal * randomRightMagnitude;

        leftSeedPoint  = ScoutAround(floodGrid, leftSeedPoint + new Vector3(randomLeft.x, 0, randomLeft.z));
        rightSeedPoint = ScoutAround(floodGrid, rightSeedPoint + new Vector3(randomRight.x, 0, randomRight.z));


        // Select a random point for left start

        // Debug.Log($"left: {leftSeedPoint}");
        // Debug.Log($"right: {rightSeedpoint}");

        // Place markers
        // Transform start = Instantiate(playerOne, leftSeedPoint, Quaternion.identity);
        playerOne.transform.position = leftSeedPoint;
        playerOne.gameObject.SetActive(true);
        //start.parent = spawnPoints;

        // Transform end = Instantiate(playerTwo, rightSeedpoint, Quaternion.identity);
        playerTwo.transform.position = rightSeedPoint;
        playerTwo.gameObject.SetActive(true);
        //end.parent = spawnPoints;

        Debug.Log($"SeedPoint One at: {leftSeedPoint}");
        Debug.Log($"SeedPoint One at: {rightSeedPoint}");
        Debug.Log($"Player One at: {playerOne.transform.position.x} / {playerOne.transform.position.z}");
        Debug.Log($"Player Two at: {playerTwo.transform.position.x} / {playerTwo.transform.position.z}");

        // TODO include a check for same position
        // if the position of the players is the same - there are no 2 different viable starting positions - restart the whole map
    }