// Returns true when a placed object is to close.
        private bool InNeighbourhood(StoredGrid grid, Vector2 loc, float finalMinDistSqrd, out bool toCloseToProcess)
        {
            int remappedX = Mathf.CeilToInt(Remap(loc.x, _surfaceBounds.min.x, _surfaceBounds.max.x, 0, grid.GridWidth - 1));
            int remappedY = Mathf.CeilToInt(Remap(loc.y, _surfaceBounds.min.z, _surfaceBounds.max.z, 0, grid.GridDepth - 1));

            List <GridPoint>[] cells = CellsAroundGridPoint(grid, remappedX, remappedY);

            toCloseToProcess = false;
            foreach (List <GridPoint> cellContents in cells)
            {
                if (cellContents != null)
                {
                    foreach (GridPoint gridPoint in cellContents)
                    {
                        Vector2 vec = gridPoint.Point - loc;
                        if (vec.sqrMagnitude <= finalMinDistSqrd)
                        {
                            toCloseToProcess = true;
                            if (gridPoint.HasObject)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
        private List <GridPoint>[] CellsAroundGridPoint(StoredGrid grid, int x, int y)
        {
            List <GridPoint>[] cells = new List <GridPoint> [25];

            int endRow = Mathf.Min(grid.GridDepth - 1, y + 2);
            int endCol = Mathf.Min(grid.GridWidth - 1, x + 2);

            int counter = 0;

            for (int row = Mathf.Max(0, y - 2); row <= endRow; ++row)
            {
                for (int col = Mathf.Max(0, x - 2); col <= endCol; ++col)
                {
                    cells[counter] = grid.Grid2D[row * grid.GridWidth + col];
                    ++counter;
                }
            }

            return(cells);
        }
        private void DistributionInit(int maxNesting)
        {
            _activeGrid = EditorData.Grids[_activeLevel];
            _activeData = Data[_activeLevel];

            if (_activeData.SphereCollisionCheck || _activeData.BoxCollisionCheck)
            {
                if (_collidersOverlapHelper == null)
                {
                    _collidersOverlapHelper = new Collider[1];
                }
            }
            else
            {
                _collidersOverlapHelper = null;
            }

            if (_activeData.UseSeed)
            {
                Random.InitState(_activeData.Seed);
            }
            float cellSize;

            if (_activeData.Map == null)
            {
                cellSize = 1.0f / (_activeData.MinDist / Mathf.Sqrt(2.0f));
            }
            else
            {
                cellSize = 1.0f / (_activeData.MaxDist / Mathf.Sqrt(2.0f));
            }

            _activeGrid.GridWidth = Mathf.CeilToInt(_surfaceBounds.size.x * cellSize);
            _activeGrid.GridDepth = Mathf.CeilToInt(_surfaceBounds.size.z * cellSize);
            _activeGrid.Grid2D    = new Vector2List[_activeGrid.GridWidth * _activeGrid.GridDepth];

            if (maxNesting == -1)
            {
                _maxNestingLevel = _activeData.MaxSubPlacersNesting;
            }
        }
        // Returns true when a placed object is to close.
        private bool InNeighbourhood(Vector2 loc, float finalMinDistSqrd, out bool toCloseToProcess)
        {
            if (!InNeighbourhood(_activeGrid, loc, finalMinDistSqrd, out toCloseToProcess))
            {
                for (int i = 0; i < _activeLevel; ++i)
                {
                    StoredGrid grid = EditorData.Grids[i];
                    bool       unused;
                    if (InNeighbourhood(grid, loc, Mathf.Max(Data[i].DistToKeepNextLevel * Data[i].DistToKeepNextLevel, finalMinDistSqrd), out unused))
                    {
                        // Object found
                        return(true);
                    }
                }
                return(false);
            }

            // Object found
            toCloseToProcess = true;
            return(true);
        }