private TurnOptions CalculateMoveOptions(TurnOptionCalculatorArgs args)
        {
            TurnOptions finalOptions = new TurnOptions();

            finalOptions.options = new List <TurnAction>();
            Piece          piece = board.Pieces[args.pieceIndex];
            PiecePrototype proto = db.PiecePrototypes[piece.PrototypeID];

            scripts.AddGlobal("actionHelper", luaHelper);
            DynValue ret = scripts.CallFunction(proto.MovementFunction, args.luaState);

            scripts.RemoveGlobal("actionHelper");
            try {
                Table optionList = SafeGetTable(ret, string.Format("Piece {0} action function error: Expected table to be returned", proto.LuaTag));
                int   numOptions = optionList.Length;
                for (int i = 1; i <= numOptions; i++)
                {
                    Table actionList = SafeGetTable(optionList.Get(i),
                                                    string.Format("Piece {0} action function error: Expected table at position {1} in returned list", proto.LuaTag, i));
                    TurnAction action = TranslateActionTable(args.pieceIndex, actionList, i);
                    finalOptions.options.Add(action);
                }
            } catch (BadActionException ex) {
                scripts.WriteToAuthorLog(ex.Message);
                finalOptions.options.Clear();
            }

            return(finalOptions);
        }
예제 #2
0
        private EoTScriptState CheckGameOver()
        {
            EoTScriptState type = EoTScriptState.Undecided;

            NextTurnInfo nextTurn = nextTurnInfo.Send();

            lastCalcState.TurnInfo = LuaTranslator.GetTurnInfo(nextTurn);
            lastCalcState.Board    = LuaTranslator.GetBoard(board, db);

            luaHelper.Ready = true;
            scripts.AddGlobal("winCheckHelper", luaHelperValue);
            DynValue ret = scripts.CallFunction(db.WinLossCheck, lastCalcState.Board);

            scripts.RemoveGlobal("winCheckHelper");
            luaHelper.Ready = false;

            foreach (var options in optionCache.Values)
            {
                options.Dispose();
            }
            optionCache.Clear();

            if (ret.Type != DataType.String)
            {
                type = EoTScriptState.Error;
            }
            else
            {
                switch (ret.String.ToUpperInvariant())
                {
                case Mods.Lua.LuaConstants.GameOverFirstWins:
                    type = EoTScriptState.FirstPlayerWins;
                    break;

                case Mods.Lua.LuaConstants.GameOverSecondWins:
                    type = EoTScriptState.SecondPlayerWins;
                    break;

                case Mods.Lua.LuaConstants.GameOverTie:
                    type = EoTScriptState.Tie;
                    break;

                case Mods.Lua.LuaConstants.GameOverUndecided:
                    type = EoTScriptState.Undecided;
                    break;

                default:
                    type = EoTScriptState.Error;
                    break;
                }
            }

            return(type);
        }
        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);
        }