private NetworkGameState CompileState() { NetworkGameState state = new NetworkGameState(); state.activePlayer = board.ActivePlayer; if (localPlayer.IsHost) { state.hostPlayerOrder = board.LocalPlayerOrder; } else { if (board.LocalPlayerOrder == PlayerTurnOrder.First) { state.hostPlayerOrder = PlayerTurnOrder.Second; } else { state.hostPlayerOrder = PlayerTurnOrder.First; } } state.turnNumber = board.TurnNumber; state.turnState = board.TurnState; // Pieces state.pieces = new NetworkGameStatePiece[board.Pieces.Count]; for (int p = 0; p < state.pieces.Length; p++) { NetworkGameStatePiece piece = new NetworkGameStatePiece(); state.pieces[p] = piece; Piece livePiece = board.Pieces[p]; piece.prototypeID = livePiece.PrototypeID; piece.position = livePiece.BoardPosition; piece.owner = livePiece.OwnerPlayer; piece.isCaptured = livePiece.IsCaptured; piece.numberMovesTaken = livePiece.NumberOfMoves; } return(state); }
private NetworkGameState CalculateStartingConditions() { Debug.LogError("Calculating start"); PlayerTurnOrder hostOrder = PlayerTurnOrder.Undecided; switch (lobby.OrderChoice) { case LobbyTurnOrderChoice.HostIsFirst: hostOrder = PlayerTurnOrder.First; break; case LobbyTurnOrderChoice.ClientIsFirst: hostOrder = PlayerTurnOrder.Second; break; default: { bool hostFirst = UnityEngine.Random.value < 0.5f; hostOrder = hostFirst ? PlayerTurnOrder.First : PlayerTurnOrder.Second; } break; } NetworkGameState gsc = new NetworkGameState() { hostPlayerOrder = hostOrder, turnNumber = 0, activePlayer = PlayerTurnOrder.First, turnState = TurnState.None, }; SetupBoard lBoard = new SetupBoard(); lBoard.Width = db.BoardDimensions.x; lBoard.Length = db.BoardDimensions.y; ScriptRuntimeException luaException; scripts.CallFunction(db.SetupFunction, lBoard, out luaException); if (luaException != null) { throw new BoardSetupException(string.Format("Board setup exception: exception in Lua code: {0}", luaException.DecoratedMessage)); } if (lBoard.NumPieces == 0) { throw new BoardSetupException("Board setup exception: No piece created"); } HashSet <IntVector2> takenPositions = new HashSet <IntVector2>(); bool anyPlayer1 = false; bool anyPlayer2 = false; List <NetworkGameStatePiece> pieceList = new List <NetworkGameStatePiece>(); for (int p = 1; p <= lBoard.NumPieces; p++) { SetupPiece lPiece = lBoard.GetPiece(p); IntVector2 pos = TranslatePosition(lPiece, p, takenPositions); PlayerTurnOrder player = TranslatePlayer(lPiece, p); switch (player) { case PlayerTurnOrder.First: anyPlayer1 = true; break; case PlayerTurnOrder.Second: anyPlayer2 = true; break; } int prototype = TranslatePrototype(lPiece, p); NetworkGameStatePiece piece = new NetworkGameStatePiece() { prototypeID = prototype, owner = player, position = pos, isCaptured = false, numberMovesTaken = 0, }; pieceList.Add(piece); } if (!anyPlayer1) { throw new BoardSetupException("Board setup exception: First player given no pieces"); } else if (!anyPlayer2) { throw new BoardSetupException("Board setup exception: Second player given no pieces"); } gsc.pieces = pieceList.ToArray(); return(gsc); }