Пример #1
0
    // Placement

    /**
     * Can place settlements only when there are no neighbouring settlements, and if connected to a road that player already owns.
     * In setup mode, can place settlements not connected to a road.
     */
    public bool CanPlaceSettlement(Player player, int col, int row, BoardGrid.VertexSpecifier vertexSpec)
    {
        // Check if there are any adjacent building -- Spacing requirement
        List <Vertex> adjacentVertices = boardGrid.GetAdjacentVerticesFromVertex(col, row, vertexSpec);

        foreach (Vertex adjacentVertex in adjacentVertices)
        {
            if (adjacentVertex.settlement != null)
            {
                return(false);
            }
        }

        // Check if connected to a road owned by this player
        bool        validRoadNearby = false;
        List <Edge> adjacentEdges   = boardGrid.GetAdjacentEdgesFromVertex(col, row, vertexSpec);

        foreach (Edge adjacentEdge in adjacentEdges)
        {
            if (adjacentEdge.road != null)
            {
                if (adjacentEdge.road.ownerId == player.GetId())
                {
                    validRoadNearby = true;
                }
            }
        }
        if (!validRoadNearby && player.freeSettlements <= 0)
        {
            return(false);
        }

        // Check if cost requirements met
        if (!player.CanAffordResourceTransaction(1, 1, 1, 1, 0) && player.freeSettlements <= 0)
        {
            return(false);
        }

        // Check to see if tile contains a building and have enough settlements in store
        if (boardGrid.GetVertex(col, row, vertexSpec).settlement == null && player.storeSettlementNum > 0)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Пример #2
0
    /**
     * Obtain the victory points for a player, given their ID.
     */
    public int GetVictoryPointsForPlayerId(string id)
    {
        BoardGrid boardGrid     = GetComponent <GameManager>().GetGame().GetBoardHandler().GetBoardGrid();
        int       victoryPoints = 0;

        for (int col = 0; col < boardGrid.GetColCount(); col++)
        {
            for (int row = 0; row < boardGrid.GetRowCount(); row++)
            {
                for (int spec = 0; spec < 2; spec++)
                {
                    Vertex vertex = boardGrid.GetVertex(col, row, (BoardGrid.VertexSpecifier)spec);
                    if (vertex != null && vertex.settlement != null)
                    {
                        if (vertex.settlement.ownerId == id)
                        {
                            if (vertex.settlement.isCity)
                            {
                                victoryPoints += 2;
                            }
                            else
                            {
                                victoryPoints += 1;
                            }
                        }
                    }
                }
            }
        }
        return(victoryPoints);
    }
    /**
     * Obtain the victory points for a player, given their ID.
     */
    public int GetVictoryPointsForPlayerId(string id)
    {
        BoardGrid boardGrid     = GameManager.Instance.GetGame().GetBoardHandler().GetBoardGrid();
        int       victoryPoints = 0;

        // Building related victory points
        for (int col = 0; col < boardGrid.GetColCount(); col++)
        {
            for (int row = 0; row < boardGrid.GetRowCount(); row++)
            {
                for (int spec = 0; spec < 2; spec++)
                {
                    Vertex vertex = boardGrid.GetVertex(col, row, (BoardGrid.VertexSpecifier)spec);
                    if (vertex != null && vertex.settlement != null)
                    {
                        if (vertex.settlement.ownerId == id)
                        {
                            if (vertex.settlement.isCity)
                            {
                                victoryPoints += 2;
                            }
                            else
                            {
                                victoryPoints += 1;
                            }
                        }
                    }
                }
            }
        }

        // Longest Road related victory points

        // Largest Army related victory points

        return(victoryPoints);
    }
Пример #4
0
    public void CmdPlaceSettlement(int col, int row, int vertexSpec)
    {
        Player    player    = GameManager.Instance.GetPlayerById(playerBehaviour.netId + "");
        BoardGrid boardGrid = GameManager.Instance.GetGame().boardHandler.GetBoardGrid();

        Vertex     vertex     = boardGrid.GetVertex(col, row, (BoardGrid.VertexSpecifier)vertexSpec);
        Settlement settlement = new Settlement();

        settlement.ownerId = player.GetId();
        settlement.isCity  = false;
        vertex.settlement  = settlement;
        if (player.freeSettlements >= 1)
        {
            player.freeSettlements--;
        }
        else
        {
            player.RemoveResources(1, 1, 1, 1, 0);
        }

        player.storeSettlementNum--;

        // If this was the second turn, give resources related to tiles surrounding this settlement to player.
        if (GameManager.Instance.GetTurnCycle() == 2)
        {
            foreach (Face face in boardGrid.GetFacesFromVertexCoordinate(col, row, (BoardGrid.VertexSpecifier)vertexSpec))
            {
                if (face.tile != null)
                {
                    player.AddResource(face.tile.resourceType, 1);
                }
            }
        }

        GameManager.Instance.SetDirtyBit(0b11111111);
    }
Пример #5
0
    public bool Equals(BoardGrid other)
    {
        for (int col = 0; col < this.GetColCount(); col++)
        {
            for (int row = 0; row < this.GetRowCount(); row++)
            {
                // This' properties
                Face face = this.GetFace(col, row);

                Vertex vertexL = this.GetVertex(col, row, VertexSpecifier.L);
                Vertex vertexR = this.GetVertex(col, row, VertexSpecifier.R);

                Edge edgeW = this.GetEdge(col, row, EdgeSpecifier.W);
                Edge edgeN = this.GetEdge(col, row, EdgeSpecifier.N);
                Edge edgeE = this.GetEdge(col, row, EdgeSpecifier.E);

                // Other's properties
                Face faceOther = other.GetFace(col, row);

                Vertex vertexLOther = other.GetVertex(col, row, VertexSpecifier.L);
                Vertex vertexROther = other.GetVertex(col, row, VertexSpecifier.R);

                Edge edgeWOther = other.GetEdge(col, row, EdgeSpecifier.W);
                Edge edgeNOther = other.GetEdge(col, row, EdgeSpecifier.N);
                Edge edgeEOther = other.GetEdge(col, row, EdgeSpecifier.E);

                // Comparing face
                if (face == null)
                {
                    if (faceOther != null)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!face.Equals(faceOther))
                    {
                        return(false);
                    }
                }

                // Comparing vertices
                if (vertexL == null)
                {
                    if (vertexLOther != null)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!vertexL.Equals(vertexLOther))
                    {
                        return(false);
                    }
                }
                if (vertexR == null)
                {
                    if (vertexROther != null)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!vertexR.Equals(vertexROther))
                    {
                        return(false);
                    }
                }

                // Comparing edges
                if (edgeW == null)
                {
                    if (edgeWOther != null)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!edgeW.Equals(edgeWOther))
                    {
                        return(false);
                    }
                }

                if (edgeN == null)
                {
                    if (edgeNOther != null)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!edgeN.Equals(edgeNOther))
                    {
                        return(false);
                    }
                }

                if (edgeE == null)
                {
                    if (edgeNOther != null)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!edgeE.Equals(edgeEOther))
                    {
                        return(false);
                    }
                }
            }
        }

        return(true);
    }
Пример #6
0
    public static void WriteBoard(this NetworkWriter writer, BoardGrid boardGrid)
    {
        writer.WriteInt32(boardGrid.GetColCount());
        writer.WriteInt32(boardGrid.GetRowCount());
        for (int col = 0; col < boardGrid.GetColCount(); col++)
        {
            for (int row = 0; row < boardGrid.GetRowCount(); row++)
            {
                // Serialize col, row face
                Face face = boardGrid.GetFace(col, row);

                if (face == null)
                {
                    writer.WriteBoolean(false);
                }
                else if (face.tile == null)
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(false);
                }
                else
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(true);
                    writer.WriteInt32((int)face.tile.resourceType);
                    writer.WriteInt32(face.tile.chanceValue);
                }

                // Serialize col, row vertices L and R
                Vertex vertexL = boardGrid.GetVertex(col, row, BoardGrid.VertexSpecifier.L);
                Vertex vertexR = boardGrid.GetVertex(col, row, BoardGrid.VertexSpecifier.R);

                if (vertexL == null)
                {
                    writer.WriteBoolean(false);
                }
                else if (vertexL.settlement == null)
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(false);
                }
                else
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(true);
                    writer.WriteString(vertexL.settlement.ownerId);
                    writer.WriteBoolean(vertexL.settlement.isCity);
                }

                if (vertexR == null)
                {
                    writer.WriteBoolean(false);
                }
                else if (vertexR.settlement == null)
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(false);
                }
                else
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(true);
                    writer.WriteString(vertexR.settlement.ownerId);
                    writer.WriteBoolean(vertexR.settlement.isCity);
                }

                // Serialize col, row edges W, N and E
                Edge edgeW = boardGrid.GetEdge(col, row, BoardGrid.EdgeSpecifier.W);
                Edge edgeN = boardGrid.GetEdge(col, row, BoardGrid.EdgeSpecifier.N);
                Edge edgeE = boardGrid.GetEdge(col, row, BoardGrid.EdgeSpecifier.E);

                if (edgeW == null)
                {
                    writer.WriteBoolean(false);
                }
                else if (edgeW.road == null)
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(false);
                }
                else
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(true);
                    writer.WriteString(edgeW.road.ownerId);
                }

                if (edgeN == null)
                {
                    writer.WriteBoolean(false);
                }
                else if (edgeN.road == null)
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(false);
                }
                else
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(true);
                    writer.WriteString(edgeN.road.ownerId);
                }

                if (edgeE == null)
                {
                    writer.WriteBoolean(false);
                }
                else if (edgeE.road == null)
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(false);
                }
                else
                {
                    writer.WriteBoolean(true);
                    writer.WriteBoolean(true);
                    writer.WriteString(edgeE.road.ownerId);
                }
            }
        }
    }
Пример #7
0
    public static BoardGrid ReadBoard(NetworkReader reader)
    {
        int       cols      = reader.ReadInt32();
        int       rows      = reader.ReadInt32();
        BoardGrid boardGrid = new BoardGrid(cols, rows);

        for (int col = 0; col < cols; col++)
        {
            for (int row = 0; row < rows; row++)
            {
                // Face exists?
                bool faceExists = reader.ReadBoolean();
                if (faceExists)
                {
                    // Face col, row
                    boardGrid.CreateFace(col, row);
                    bool tileExists = reader.ReadBoolean();
                    if (tileExists)
                    {
                        boardGrid.GetFace(col, row).tile = new Tile();
                        boardGrid.GetFace(col, row).tile.resourceType = (ResourceType)reader.ReadInt32();
                        boardGrid.GetFace(col, row).tile.chanceValue  = reader.ReadInt32();
                    }
                }

                // Vertex L exists?
                bool vertexLExists = reader.ReadBoolean();
                if (vertexLExists)
                {
                    boardGrid.CreateVertex(col, row, BoardGrid.VertexSpecifier.L);
                    bool settlementExists = reader.ReadBoolean();
                    if (settlementExists)
                    {
                        boardGrid.GetVertex(col, row, BoardGrid.VertexSpecifier.L).settlement         = new Settlement();
                        boardGrid.GetVertex(col, row, BoardGrid.VertexSpecifier.L).settlement.ownerId = reader.ReadString();
                        boardGrid.GetVertex(col, row, BoardGrid.VertexSpecifier.L).settlement.isCity  = reader.ReadBoolean();
                    }
                }

                // Vertex R exists?
                bool vertexRExists = reader.ReadBoolean();
                if (vertexRExists)
                {
                    boardGrid.CreateVertex(col, row, BoardGrid.VertexSpecifier.R);
                    bool settlementExists = reader.ReadBoolean();
                    if (settlementExists)
                    {
                        boardGrid.GetVertex(col, row, BoardGrid.VertexSpecifier.R).settlement         = new Settlement();
                        boardGrid.GetVertex(col, row, BoardGrid.VertexSpecifier.R).settlement.ownerId = reader.ReadString();
                        boardGrid.GetVertex(col, row, BoardGrid.VertexSpecifier.R).settlement.isCity  = reader.ReadBoolean();
                    }
                }

                // Edge W exists?
                bool edgeWExists = reader.ReadBoolean();
                if (edgeWExists)
                {
                    boardGrid.CreateEdge(col, row, BoardGrid.EdgeSpecifier.W);
                    bool roadExists = reader.ReadBoolean();
                    if (roadExists)
                    {
                        boardGrid.GetEdge(col, row, BoardGrid.EdgeSpecifier.W).road         = new Road();
                        boardGrid.GetEdge(col, row, BoardGrid.EdgeSpecifier.W).road.ownerId = reader.ReadString();
                    }
                }

                // Edge N exists?
                bool edgeNExists = reader.ReadBoolean();
                if (edgeNExists)
                {
                    boardGrid.CreateEdge(col, row, BoardGrid.EdgeSpecifier.N);
                    bool roadExists = reader.ReadBoolean();
                    if (roadExists)
                    {
                        boardGrid.GetEdge(col, row, BoardGrid.EdgeSpecifier.N).road         = new Road();
                        boardGrid.GetEdge(col, row, BoardGrid.EdgeSpecifier.N).road.ownerId = reader.ReadString();
                    }
                }

                // Edge E exists?
                bool edgeEExists = reader.ReadBoolean();
                if (edgeEExists)
                {
                    boardGrid.CreateEdge(col, row, BoardGrid.EdgeSpecifier.E);
                    bool roadExists = reader.ReadBoolean();
                    if (roadExists)
                    {
                        boardGrid.GetEdge(col, row, BoardGrid.EdgeSpecifier.E).road         = new Road();
                        boardGrid.GetEdge(col, row, BoardGrid.EdgeSpecifier.E).road.ownerId = reader.ReadString();
                    }
                }
            }
        }

        return(boardGrid);
    }