예제 #1
0
    private void TryToTakeAction(MLAction action)
    {
        if (IsPerformingMove())
        {
            return;
        }
        if (isAnim)
        {
            return;
        }
        if (MLActionFactory.IsPunch(action))
        {
            if (punchAction.IsOnCooldown())
            {
                return;
            }
            punchAction.Run(action == MLAction.PUNCH_LEFT ? Direction.LEFT : Direction.RIGHT);
        }

        if (MLActionFactory.IsDodge(action))
        {
            if (dodgeAction.IsOnCooldown())
            {
                return;
            }
            dodgeAction.Run(action == MLAction.DODGE_LEFT ? Direction.LEFT : Direction.RIGHT);
        }
    }
예제 #2
0
    /// <summary>
    /// Take an action
    /// </summary>
    /// <param name="vectorAction">The action to take</param>
    /// <param name="textAction">The name of the action</param>
    public override void AgentAction(float[] vectorAction, string textAction)
    {
        base.AgentAction(vectorAction, textAction);
        lastActions = vectorAction;

        lastLoss = MLActionFactory.GetLoss(vectorAction);

        if (!isFighting)
        {
            return;
        }

        // Only perform confident moves
        var confidence = MLActionFactory.GetProbabilityFromVector(MLActionFactory.GetAction(vectorAction), vectorAction);

        if (!Mathf.Approximately(confidence, 0) && confidence < minConfidence)
        {
            return;
        }

        // Try to perform action
        TryToTakeAction(MLActionFactory.GetAction(vectorAction));

        // Interrupt dodges early
        if (endDodgeEarly && MLActionFactory.IsDodge(currentAction))
        {
            if (MLActionFactory.IsPunch(lastEnemyState) && !MLActionFactory.IsPunch(opponent.GetCurrentAction()))
            {
                // Opponent finished punching, so stop dodging
                dodgeAction.Interrupt();
            }
        }

        lastEnemyState = opponent.GetCurrentAction();
    }
예제 #3
0
    private bool IsGoodMove()
    {
        if (opponent == null)
        {
            return(false);
        }
        var opponentMove = opponent.GetCurrentAction();
        var myMove       = MLActionFactory.GetAction(lastActions);

        if (myMove == MLAction.NOTHING)
        {
            return(true);
        }

        if (MLActionFactory.IsPunch(opponentMove) && !MLActionFactory.IsDodge(myMove))
        {
            return(false);
        }

        if (!MLActionFactory.IsPunch(opponentMove) && MLActionFactory.IsDodge(myMove))
        {
            return(false);
        }

        return(true);
    }
예제 #4
0
    private MLAction runMoves(List <float> vectorObs, MLAction[] moves)
    {
        MLInput input = new MLInput(vectorObs.ToArray());

        var currentMove = moves[moveIdx];

        if (currentMove == MLAction.NOTHING && Time.time - nothingStartTime >= nothingDuration)
        {
            // Doing nothing ended
            return(runNextMove(moves, input));
        }
        else if (didAction && MLActionFactory.IsPunch(currentMove) && input.GetMyMove() == MLAction.NOTHING)
        {
            // Punch ended
            return(runNextMove(moves, input));
        }
        else if (didAction && MLActionFactory.IsDodge(currentMove) && input.GetMyMove() == MLAction.NOTHING)
        {
            // Dodge ended
            return(runNextMove(moves, input));
        }

        if (!didAction && MLActionFactory.IsPunch(currentMove))
        {
            didAction = input.IsPunchReady();
        }
        else if (!didAction && MLActionFactory.IsDodge(currentMove))
        {
            didAction = input.IsDodgeReady();
        }

        return(moves[moveIdx]);
    }
예제 #5
0
    private MLAction runNextMove(MLAction[] moves, MLInput input)
    {
        didAction        = false;
        nothingStartTime = Time.time;
        moveIdx          = (moveIdx + 1) % moves.Length;
        MLAction nextMove = moves[moveIdx];

        if (MLActionFactory.IsPunch(nextMove))
        {
            didAction = input.IsPunchReady();
        }
        else if (MLActionFactory.IsDodge(nextMove))
        {
            didAction = input.IsDodgeReady();
        }

        return(nextMove);
    }
예제 #6
0
    /// <summary>
    /// Collect the observations (internal punch)
    /// </summary>
    public override void CollectObservations()
    {
        AddVectorObs(!punchAction.IsOnCooldown() && !punchAction.IsRunning());
        AddVectorObs(!dodgeAction.IsOnCooldown() && !dodgeAction.IsRunning());

        bool[] move;
        int    opponentComboState = 0;

        if (opponent != null)
        {
            move = new bool[] {
                opponent.currentAction == MLAction.PUNCH_RIGHT,
                opponent.currentAction == MLAction.PUNCH_LEFT,
                opponent.currentAction == MLAction.DODGE_LEFT,
                opponent.currentAction == MLAction.DODGE_RIGHT
            };

            opponentComboState = opponent.comboTracker.GetState();
        }
        else
        {
            move = new bool[] { false, false, false, false };
            opponentComboState = 0;
        }

        AddVectorObs(Encoder.encodeInt(comboTracker.GetState(), 0, comboTracker.GetTotalStates()));
        foreach (var m in move)
        {
            AddVectorObs(m);
        }
        AddVectorObs(currentAction == MLAction.PUNCH_LEFT);
        AddVectorObs(currentAction == MLAction.PUNCH_RIGHT);
        AddVectorObs(currentAction == MLAction.DODGE_LEFT);
        AddVectorObs(currentAction == MLAction.DODGE_RIGHT);

        if (bufferSize >= maxBufferSize)
        {
            SetTextObs((isTeacher && isFighting && MLActionFactory.GetAction(lastActions) != MLAction.NOTHING) + "," + true);
            bufferSize = 0;
        }
        else
        {
            if (MLActionFactory.IsPunch(MLActionFactory.GetAction(lastActions)))
            {
                punchCount++;
            }
            else if (MLActionFactory.IsDodge(MLActionFactory.GetAction(lastActions)))
            {
                dodgeCount++;
            }

            if (isTeacher)
            {
                //Debug.Log(punchCount + ", " + dodgeCount);
            }

            var training = isTeacher && isFighting && (MLActionFactory.GetAction(lastActions) != MLAction.NOTHING || nothingBuffer < nothingBufferSize) && IsGoodMove();
            if (training)
            {
                bufferSize++;
            }
            if (MLActionFactory.GetAction(lastActions) == MLAction.NOTHING)
            {
                nothingBuffer++;
            }
            SetTextObs(training + "," + false);
        }
    }