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);
        }
Exemple #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);
        }