private static void CreateWalls(ref GridSpace[,] grid)
 {
     for (var x = 0; x < _roomWidth; ++x)
     {
         for (var y = 0; y < _roomHeight; ++y)
         {
             // if the current position in the grid is a floor, add a wall to any empty space above, below, to the left or to the right of it
             if (grid[x, y] == GridSpace.Floor)
             {
                 if (grid[x, y + 1] == GridSpace.Empty)
                 {
                     grid[x, y + 1] = GridSpace.Wall;
                 }
                 if (grid[x, y - 1] == GridSpace.Empty)
                 {
                     grid[x, y - 1] = GridSpace.Wall;
                 }
                 if (grid[x + 1, y] == GridSpace.Empty)
                 {
                     grid[x + 1, y] = GridSpace.Wall;
                 }
                 if (grid[x - 1, y] == GridSpace.Empty)
                 {
                     grid[x - 1, y] = GridSpace.Wall;
                 }
             }
         }
     }
 }
Example #2
0
    List <Vector4> findPossibles(Vector2 space, List <Vector2> checks, GridSpace [,] gridArray, int gridSizeX, int gridSizeY)
    {
        List <Vector4> possible = new List <Vector4> ();

        foreach (Vector2 c in checks)
        {
            List <Vector2> results = new List <Vector2> ();
            results = findSpaces(c, (int)space.x, (int)space.y, gridSizeX, gridSizeY, false, gridArray);
            foreach (Vector2 v in results)
            {
                possible.Add(new Vector4(c.x, c.y, v.x, v.y));
            }
        }
        // Try again but with the flipped dimensions (if not a square)
        // and if the space is close to corner
        if (space.y != space.x)
        {
            foreach (Vector2 c in checks)
            {
                List <Vector2> results = new List <Vector2> ();
                results = findSpaces(c, (int)space.y, (int)space.x, gridSizeX, gridSizeY, false, gridArray);
                foreach (Vector2 v in results)
                {
                    possible.Add(new Vector4(c.x, c.y, v.x, v.y));
                }
            }
        }
        return(possible);
    }
Example #3
0
 public void CmdSetUpPlayerGrid()
 {
     grid  = new GridSpace[GRID_WIDTH, GRID_HEIGHT];
     bench = new GridSpace[GRID_WIDTH];
     RpcCreateBoard();
     for (short i = 0; i < GRID_WIDTH; i++)
     {
         GameObject benchSpot = Instantiate <GameObject>(gridPrefab);
         benchSpot.transform.SetParent(transform);
         benchSpot.transform.SetPositionAndRotation(new Vector3(transform.position.x + i - 4, 0, transform.position.z + 6), Quaternion.Euler(Vector3.zero));
         NetworkServer.Spawn(benchSpot, connectionToClient);
         RpcSetBenchSpot(i, benchSpot);
         bench[i] = benchSpot.GetComponent <GridSpace>();
         bench[i].SetGridPosition(new Vector2(i, GRID_HEIGHT));
         for (short j = 0; j < GRID_HEIGHT; j++)
         {
             GameObject gridSpot = Instantiate <GameObject>(gridPrefab);
             gridSpot.transform.SetParent(transform);
             gridSpot.transform.SetPositionAndRotation(new Vector3(transform.position.x + i - 4, 0, transform.position.z + 8 + (j % GRID_HEIGHT)), Quaternion.Euler(Vector3.zero));
             NetworkServer.Spawn(gridSpot, connectionToClient);
             RpcSetGridSpot(i, j, gridSpot);
             grid[i, j] = gridSpot.GetComponent <GridSpace>();
             grid[i, j].SetGridPosition(new Vector2(i, j));
         }
     }
 }
Example #4
0
 public void GenerateRoom(int SizeX, int SizeY, Transform roomTransform)
 {
     gridSizeY = SizeY;
     gridSizeX = SizeX;
     if (gridSizeX <= 0) {
         gridSizeX = 4;
     }
     if (gridSizeY <= 0) {
         gridSizeY = 4;
     }
     spaces = new GridSpace[gridSizeX,gridSizeY]; // set the spaces array to the proper size
     roomSize = new Vector2(gridSizeX,gridSizeY); // Initlize the room size
     takenPositions = new List<Vector2> (); // Initlize the takenPosition list
     patharea = Mathf.RoundToInt((gridSizeX * gridSizeY)/3); // The number of cells to mark as walkway, by default mark about 1/3rd of the area to be walkway
     //Make sure we aren't trying to create more walkway than we have space
     if (patharea > roomSize.x *roomSize.y) {  // If theres more lower it to be half the total cells availible
         patharea = Mathf.RoundToInt(roomSize.x  * roomSize.y);
     }
     roomTransform.GetComponent<Room> ().gridSizeX = gridSizeX;
     roomTransform.GetComponent<Room> ().gridSizeY = gridSizeY;
     CreatePathways (); //This procedurally generates a pathway
     FillavailblePositions ();
     populateRoom ();
     roomTransform.GetComponent<Room> ().spaces = spaces;
 }
    private bool canPlaceShip(Ship ship, int row, int col, GridSpace[,] board, ShipOrientation orientation)
    {
        int targetRow = row;
        int targetCol = col;
        int size      = ship.size;

        while (size != 0)
        {
            if (targetRow >= board.GetLength(0) || targetRow < 0)
            {
                return(false);
            }
            if (targetCol >= board.GetLength(1) || targetCol < 0)
            {
                return(false);
            }

            if (board[targetRow, targetCol].ship != null)
            {
                return(false);
            }

            if (orientation == ShipOrientation.HORIZONTAL)
            {
                targetCol++;
            }
            else
            {
                targetRow++;
            }
            size--;
        }
        return(true);
    }
Example #6
0
    public OccupancyGrid(int height, int width)
    {
        grid = new GridSpace[width,height];

        isDirty = true;
        InitialiseGrid();
    }
Example #7
0
    private Vector2Int _MakeAdjunctMove(Vector2Int lastCheckPos, GridSpace[,] map, List <Ship> dock)
    {
        var possibleMove = _GetPossiblePos(lastCheckPos, map, dock);
        var betterMove   = new Dictionary <Vector2Int, int>();

        var threshold = 1;

        foreach (var move in possibleMove)
        {
            if (move.Value > threshold)
            {
                threshold = move.Value;
                betterMove.Clear();
            }
            if (move.Value == threshold)
            {
                betterMove.Add(move.Key, move.Value);
            }
        }

        var moveList = new List <Vector2Int>();

        foreach (var move in betterMove)
        {
            moveList.Add(move.Key);
        }
        return(_Shuffle(moveList)[0]);
    }
Example #8
0
    private Dictionary <Vector2Int, int> _GetPossiblePos(Vector2Int lastCheckPos, GridSpace[,] map, List <Ship> dock)
    {
        var chunkList = new List <Vector2Int>();

        chunkList.Add(lastCheckPos);
        var disActiveAdjunct = new Dictionary <Vector2Int, int>();

        var newList = new List <Vector2Int>(0);

        newList.Add(lastCheckPos);
        while (newList.Count != 0)
        {
            var tempDisActiveAdjunct = new Dictionary <Vector2Int, int>(0);
            _GetAdjunctInfo(newList, map, dock, chunkList, out newList, out tempDisActiveAdjunct);

            //record
            foreach (var pos in newList)
            {
                chunkList.Add(pos);
            }
            foreach (var tempPos in tempDisActiveAdjunct)
            {
                if (disActiveAdjunct.ContainsKey(tempPos.Key))
                {
                    disActiveAdjunct[tempPos.Key]++;
                }
                else
                {
                    disActiveAdjunct.Add(tempPos.Key, 1);
                }
            }
        }

        return(disActiveAdjunct);
    }
Example #9
0
 private bool _isGridAvaliable(Vector2Int pos, GridSpace[,] map)
 {
     if ((pos.x < 0 || pos.x > map.GetLength(0) - 1) || (pos.y < 0 || pos.y > map.GetLength(1) - 1))
     {
         return(false);
     }
     return(map[pos.x, pos.y].pieceType == MapPiece.Empty);
 }
    private static void CreateFloors(float initialFillPercent, ref GridSpace[,] grid)
    {
        RandomFillMap(initialFillPercent, ref grid);

        for (var i = 0; i < 10; ++i)
        {
            SmoothMap(ref grid);
        }
    }
Example #11
0
    public void GenerateMaze(int sizeX, int sizeY)
    {
        //Strategy
        //Choose a cell at random and place it in the maze
        //mark all cells around it as 'frontier cells'
        //chose a frontier cell at random
        //mark the wall connecting it to the pathway as false
        //add any cells around it as frontier cells
        //repeat until no more frontier cells

        gridSizeX = sizeX;
        gridSizeY = sizeY;
        spaces    = new GridSpace[gridSizeX, gridSizeY]; // set the spaces array to the proper size
        FillSpaces();                                    // Fills out the spaces array

        //Ensure the maze has a valid size
        if (gridSizeX <= 0)
        {
            gridSizeX = 6;
        }
        if (gridSizeY <= 0)
        {
            gridSizeY = 6;
        }

        //Choose a cell at random and add it as a frontier cell
        Vector2 first = new Vector2(Random.Range(0, gridSizeX), Random.Range(0, gridSizeY));

        frontierCells.Add(first);

        //Loop through until there are no more frontier cells
        while (frontierCells.Count > 0)
        {
            //chose a frontier cell at random
            Vector2 randomCell = frontierCells[Random.Range(0, frontierCells.Count)];

            //Mark it as being in the maze
            inMaze.Add(randomCell);
            //Remove it from the Frontier
            frontierCells.Remove(randomCell);
            //Add it's neighbors
            AddFrontier(randomCell);
            //Connect it to the maze
            ConnectToMaze(randomCell);
        }

        //Populate corners list
        corners.Add(Vector2.zero);
        corners.Add(new Vector2(gridSizeX - 1, gridSizeY - 1));
        corners.Add(new Vector2(0, gridSizeY - 1));
        corners.Add(new Vector2(gridSizeX - 1, 0));

        //Choose a random position for the goal
        goal = new Vector2(Random.Range(0, gridSizeX), Random.Range(0, gridSizeY));
        //Set it as the goal
        spaces[(int)goal.x, (int)goal.y].setAsGoal();
    }
 private void setupBoard(GridSpace[,] board)
 {
     for (int row = 0; row < board.GetLength(0); row++)
     {
         for (int col = 0; col < board.GetLength(1); col++)
         {
             board[row, col] = new GridSpace();
         }
     }
 }
Example #13
0
 private void _SetShipToCertainState(Ship ship, GridSpace[,] map, MapPiece targetType)
 {
     for (int x = ship.refPos.x; x < ship.refPos.x + ship.size.x; x++)
     {
         for (int y = ship.refPos.y; y < ship.refPos.y + ship.size.y; y++)
         {
             map[x, y].pieceType = targetType;
         }
     }
 }
 // Start is called before the first frame update
 void Start()
 {
     gameState = GameState.PLACEMENT;
     userBoard = new GridSpace[8, 8];
     aiBoard   = new GridSpace[8, 8];
     setupBoard(userBoard);
     setupBoard(aiBoard);
     randomlyPopulateBoard(aiBoard, AIShips);
     AITargets = generateAITargets(userBoard);
 }
Example #15
0
 private void GridGen(int pGridWidth, int pGridHeight)
 {
     Grid = new GridSpace[pGridWidth, pGridHeight];
     for (int i = 0; i < pGridWidth; i++)
     {
         for (int j = 0; j < pGridHeight; j++)
         {
             Grid[i, j] = new GridSpace(i, j, pTileSize: TileScale);
         }
     }
 }
Example #16
0
        public CustomGrid(int x, int y, int probability, int units)
        {
            this.grid        = new GridSpace[x, y];
            this.x           = x;
            this.y           = y;
            this.probability = probability;
            this.units       = units + 1;

            //Populate the grid on construct
            PopulateGrid();
        }
 private void removeShipFromBoard(Ship ship, GridSpace[,] board)
 {
     for (int row = 0; row < board.GetLength(0); row++)
     {
         for (int col = 0; col < board.GetLength(1); col++)
         {
             if (board[row, col].ship == ship)
             {
                 board[row, col].ship = null;
             }
         }
     }
 }
Example #18
0
 // Use this for initialization
 void Start()
 {
     spaces    = gameObject.GetComponent <Room>().spaces;
     gridSizeX = gameObject.GetComponent <Room> ().gridSizeX;
     gridSizeY = gameObject.GetComponent <Room> ().gridSizeY;
     generateFurniture();
     generateProps();
     generateDoors();
     //gameObject.GetComponent<Room> ().DrawMap ();
     gameObject.GetComponent <Room> ().SpawnFurniture();
     gameObject.GetComponent <Room> ().spawnFloor();
     gameObject.GetComponent <Room> ().spawnWalls();
 }
Example #19
0
    // Generates a basic grid setup using an outside file
    // Users can update the created grid using other objects, tiles, etc.
    public void GenerateGrid()
    {
        Color[] pixels = GetPixels();
        grid = new GridSpace[levelImage.width, levelImage.height];

        for (int i = 0; i < levelImage.height; ++i)
        {
            for (int j = 0; j < levelImage.width; ++j)
            {
                CreateGridSpace(GetSurroundingPixels(new Vector2Int(j, i), pixels),
                                pixels[j + (i * levelImage.width)], new Vector2Int(j, i));
            }
        }
    }
Example #20
0
    public void SpawnLayout(GridSpace[,] gridSpaces, int numberOfSecureRooms)
    {
        col = gridSpaces.GetLength(0);
        row = gridSpaces.GetLength(1);

        roomLayout = new Room[col, row];

        // Order: Spawn Colliding Objects; Create NavMesh; Spawn Trigger Objects; Spawn Guards
        navMeshBaker = new NavigationBaker();
        SpawnRooms(gridSpaces, col, row);
        SpawnSideObjectives();
        navMeshBaker.CreateNavMesh();
        SpawnGuards(numberOfSecureRooms);
    }
Example #21
0
    private Ship _RandomSelectShip(List <Ship> dock, GridSpace[,] map)
    {
        while (_Shuffle(dock)[0].isBeenSetOnBoard)
        {
        }

        dock[0].refPos = new Vector2Int(-1, -1);
        while (!_isShipSetable(dock[0], dock[0].refPos, map))
        {
            dock[0].refPos = new Vector2Int(Random.Range(0, mapSize.x), Random.Range(0, mapSize.y));
            dock[0].isBend = _ShuffleBool();
        }
        return(dock[0]);
    }
Example #22
0
 private bool _isShipSetable(Ship ship, Vector2Int refPos, GridSpace[,] map)
 {
     for (int x = refPos.x; x < refPos.x + ship.size.x; x++)
     {
         for (int y = refPos.y; y < refPos.y + ship.size.y; y++)
         {
             if (!_isGridAvaliable(x, y, map))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
    private List <Coord> generateAITargets(GridSpace[,] board)
    {
        List <Coord> result = new List <Coord>();

        for (int row = 0; row < board.GetLength(0); row++)
        {
            for (int col = 0; col < board.GetLength(1); col++)
            {
                result.Add(new Coord(row, col));
            }
        }
        result.Shuffle();

        return(result);
    }
Example #24
0
    public void CreateWorld()
    {
        _gridsSpaces = new GridSpace[_worldSize, _worldSize];

        for (int i = 0; i < _worldSize; i++)
        {
            for (int j = 0; j < _worldSize; j++)
            {
                _gridsSpaces[i, j] = Instantiate(_gridSpacePrefab, transform.position, Quaternion.Euler(90, 0, 0)).GetComponent <GridSpace>();
                _gridsSpaces[i, j].transform.localScale    = new Vector3(_blockSize, _blockSize, _blockSize);
                _gridsSpaces[i, j].transform.localPosition = new Vector3((i * _blockSize - _worldSceneSize / 2f) + _distanceBetweenGridBlocks, 0, (j * _blockSize - _worldSceneSize / 2f) + _distanceBetweenGridBlocks);
                _gridsSpaces[i, j].transform.parent        = _gridSpaces.transform;
            }
        }
    }
Example #25
0
    private Vector2Int _RamdomUnCheckedPos(GridSpace[,] checkMap)
    {
        var x = -1;
        var y = -1;

        while (!(_isInMap(x, y) && !checkMap[x, y].hasBeenSelected))
        {
            var _x = Random.Range(0f, 1f);
            x = (int)Mathf.Floor(_x * mapSize.x);
            var _y = Random.Range(0f, 1f);
            y = (int)Mathf.Floor(_y * mapSize.y);
        }

        return(new Vector2Int(x, y));
    }
Example #26
0
 private bool _isBelongToAFoundedShip(Vector2Int pos, List <Ship> dock, GridSpace[,] map)
 {
     foreach (var ship in dock)
     {
         if (ship.isShipFound(map))
         {
             if ((pos.x >= ship.refPos.x && pos.x < ship.refPos.x + ship.size.x) &&
                 (pos.y >= ship.refPos.y && pos.y < ship.refPos.y + ship.size.y))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Example #27
0
    private void _GetAdjunctInfo(List <Vector2Int> poses, GridSpace[,] map, List <Ship> dock, List <Vector2Int> oldList, out List <Vector2Int> activePosList, out Dictionary <Vector2Int, int> inactivePosDic)
    {
        activePosList  = new List <Vector2Int>(0);
        inactivePosDic = new Dictionary <Vector2Int, int>();
        foreach (var pos in poses)
        {
            for (int x = pos.x - 1; x < pos.x + 2; x++)
            {
                for (int y = pos.y - 1; y < pos.y + 2; y++)
                {
                    if ((x != pos.x && y != pos.y) || (x == pos.x && y == pos.y) || !_isInMap(new Vector2Int(x, y)))
                    {
                        continue;
                    }
                    if (!map[x, y].hasBeenSelected)
                    {
                        if (inactivePosDic.ContainsKey(new Vector2Int(x, y)))
                        {
                            inactivePosDic[new Vector2Int(x, y)] = inactivePosDic[new Vector2Int(x, y)] + 1;
                        }
                        else
                        {
                            inactivePosDic.Add(new Vector2Int(x, y), 1);
                        }
                        continue;
                    }

                    if (map[x, y].pieceType == MapPiece.Ship)
                    {
                        if (_isBelongToAFoundedShip(new Vector2Int(x, y), dock, map) ||
                            oldList.Contains(new Vector2Int(x, y)))
                        {
                            continue;
                        }
                        activePosList.Add(new Vector2Int(x, y));
                    }
                }
            }
        }

        for (int i = 0; i < activePosList.Count; i++)
        {
            if (oldList.Contains(activePosList[i]))
            {
                activePosList.Remove(activePosList[i]);
            }
        }
    }
 private void randomlyPopulateBoard(GridSpace[,] board, List <Ship> ships)
 {
     while (ships.Count > 0)
     {
         Ship ship = ships[0];
         ships.RemoveAt(0);
         Coord           position = getRandomCoord(board);
         ShipOrientation orient   = getRandomOrientation();
         while (!canPlaceShip(ship, position.row, position.col, board, orient))
         {
             position = getRandomCoord(board);
             orient   = getRandomOrientation();
         }
         PlaceShip(ship, position.row, position.col, board, orient);
     }
 }
Example #29
0
        public bool isShipFound(GridSpace[,] map)
        {
            for (int x = refPos.x; x < refPos.x + size.x; x++)
            {
                for (int y = refPos.y; y < refPos.y + size.y; y++)
                {
                    Debug.Assert(Model.Instance._isInMap(new Vector2Int(x, y)), "Ship has some part not in the map");
                    if (!map[x, y].hasBeenSelected)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
 private static void RandomFillMap(float initialFillPercent, ref GridSpace[,] grid)
 {
     for (var x = 0; x < _roomWidth; ++x)
     {
         for (var y = 0; y < _roomHeight; ++y)
         {
             if (x == 0 || x == _roomWidth - 1 || y == 0 || y == _roomHeight - 1)
             {
                 grid[x, y] = GridSpace.Floor;
             }
             else
             {
                 grid[x, y] = Rnd < initialFillPercent ? GridSpace.Floor : GridSpace.Empty;
             }
         }
     }
 }
Example #31
0
    private void SetUp()
    {
        AddSpaceImplementation = GetComponent <IAddSpaceToWorld>();
        AddSpaceImplementation.InitializeGridConversion(GridToWorldConversion);

        FloorCount = 0;
        LevelGrid  = new GridSpace[Width, Height];
        Vector2Int middle = Vector2Int.RoundToInt(new Vector2(Width / 2f, Height / 2f));

        Walkers = new List <Walker>()
        {
            new Walker {
                Direction = GetRandomDirection(), Position = GridPositionToWalkerPosition(middle)
            }
        };

        AddSpaceToGrid(GridSpace.FLOOR, middle);
    }
Example #32
0
 /// <summary>
 /// Grid constructor, X by Y
 /// </summary>
 /// <param name="xDim">Width</param>
 /// <param name="yDim">Height</param>
 public Grid(int xDim, int yDim)
 {
     originScreenPos = minGridOrigin;    //Defaults the view to the top left
     maxGridOrigin = new Vector2(3.0f / 4 * ConstantHolder.HexagonGrid_HexSizeX * (xDim) - ConstantHolder.GAME_WIDTH, 
         ConstantHolder.HexagonGrid_HexSizeY * (yDim + 0.5f) - ConstantHolder.GAME_HEIGHT);
         //Set the grid position limits to keep it on the screen.
     maxGridOrigin.X += ConstantHolder.HexagonGrid_HexSizeX / 4 + ConstantHolder.GAME_WIDTH / 10;
         //Allow more distance, to let players see the edges more easily
     maxGridOrigin.Y += ConstantHolder.GAME_HEIGHT / 4;
     spaceArray = new GridSpace[xDim, yDim];
     connections = new bool[xDim, yDim, xDim, yDim];
     movementCosts = new int[xDim, yDim, xDim, yDim];
     sizeX = xDim;
     sizeY = yDim;
     numSpaces = sizeX * sizeY;
     Vector2 minimapDimensions = new Vector2(ConstantHolder.GAME_WIDTH * ConstantHolder.relativeMinimapSizeX,
         ConstantHolder.GAME_HEIGHT * ConstantHolder.relativeMinimapSizeY);
     float emptySpaceWidth;
     if (minimapDimensions.X / ((sizeX * 3.0 + 1)/ 4) > minimapDimensions.Y / (sizeY + 0.5f))
     {
         mGridspaceWidth = minimapDimensions.Y / (sizeY + 0.5f);
         minimapOrigin.Y = ConstantHolder.GAME_HEIGHT * (1 - ConstantHolder.relativeMinimapSizeY);
         emptySpaceWidth = minimapDimensions.X - (( 3.0f / 4 * sizeX + 0.25f) * mGridspaceWidth);
         minimapOrigin.X = emptySpaceWidth / 2 + ConstantHolder.GAME_WIDTH * (1 - ConstantHolder.relativeMinimapSizeX);
     }
     else            //This choice will determine whether the minimap fills horizontally (else) or vertically (if)
     {
         mGridspaceWidth = minimapDimensions.X / ((sizeX * 3.0f  + 1)/ 4);
         minimapOrigin.X = ConstantHolder.GAME_WIDTH * (1 - ConstantHolder.relativeMinimapSizeX);
         emptySpaceWidth = minimapDimensions.Y - ((sizeY + 0.5f)  * mGridspaceWidth);
         minimapOrigin.Y = emptySpaceWidth / 2 + ConstantHolder.GAME_HEIGHT * (1 - ConstantHolder.relativeMinimapSizeY);
     }
     minimapSpaceDim = new Vector2(mGridspaceWidth, mGridspaceWidth);
     selectionRect.Width = (int)(mGridspaceWidth * (float)ConstantHolder.GAME_WIDTH / (ConstantHolder.HexagonGrid_HexSizeX));
     selectionRectDefaultSize.X = selectionRect.Width;
     selectionRect.Height = (int)(mGridspaceWidth * ((float)ConstantHolder.GAME_HEIGHT / (ConstantHolder.HexagonGrid_HexSizeY)));
     selectionRectDefaultSize.Y = selectionRect.Height;
     InterfaceTextureHolder holder = InterfaceTextureHolder.Holder;
     minimapBackground = holder.GUISprites[(int)ConstantHolder.GUIMemberImages.MinimapBackground].copySprite();
     minimapSelect = holder.GUISprites[(int)ConstantHolder.GUIMemberImages.MinimapSelection].copySprite();
     visited = new bool[sizeX, sizeY];
 }
Example #33
0
 public Grid()
 {
     grid = new GridSpace[gridSize, gridSize];
     initGrid();
 }
Example #34
0
 public static void Reset( )
 {
     gridSpaces = null;
     IsInitialized = false;
 }