Beispiel #1
0
 void Start()
 {
     m_randomOffset = Random.value;
     m_anim         = GetComponent <Animator>();
     m_stats        = GetComponent <AIStats>();
     m_player       = Manager.GetPlayer().transform;
 }
Beispiel #2
0
    void Start()
    {
        /*aiManager = FindObjectOfType<AIManager>();
         * if (aiManager == null)
         * {
         *  Debug.LogError("No AIManager found in scene");
         * }
         * // Add this gameobject to ai list
         * aiManager.AddAI(gameObject);*/

        player = GameObject.FindGameObjectWithTag("Player");

        if (Random.Range(0.0f, 1.0f) > 0.5f)
        {
            fleeDir = -1.0f;
        }
        float sqrtSpeed = Mathf.Sqrt(speed);

        currentSpeed = new Vector2(Random.Range(-sqrtSpeed, sqrtSpeed), Random.Range(-sqrtSpeed, sqrtSpeed));

        animator = gameObject.GetComponent <Animator>();
        aiStats  = gameObject.GetComponent <AIStats>();

        Collider2D[] collisionArray = gameObject.GetComponents <CircleCollider2D>();
        foreach (CircleCollider2D c in collisionArray)
        {
            if (!c.isTrigger)
            {
                wallAvoidDistance = c.radius * 1.5f;
            }
        }
    }
Beispiel #3
0
        public int Do(Color color, Bitboard bitboard, int alpha, int beta, AIStats stats)
        {
            var enemyColor = ColorOperations.Invert(color);
            var colorSign  = ColorOperations.ToSign(color);

            stats.QuiescenceTotalNodes++;

            var whiteGeneratorMode = GetGeneratorMode(color, Color.White);
            var blackGeneratorMode = GetGeneratorMode(color, Color.Black);

            bitboard.Calculate(whiteGeneratorMode, blackGeneratorMode, true);

            if (bitboard.IsCheck(enemyColor))
            {
                stats.QuiescenceEndNodes++;
                return(AIConstants.MateValue);
            }

            var evaluation = colorSign * bitboard.GetEvaluation();

            if (evaluation >= beta)
            {
                stats.QuiescenceEndNodes++;
                return(beta);
            }

            if (evaluation > alpha)
            {
                alpha = evaluation;
            }

            var sortedMoves = SortMoves(color, bitboard, bitboard.Moves);

            foreach (var move in sortedMoves)
            {
                var bitboardAfterMove = bitboard.Move(move);
                var nodeValue         = -Do(enemyColor, bitboardAfterMove, -beta, -alpha, stats);

                if (nodeValue >= beta)
                {
                    return(beta);
                }

                if (nodeValue > alpha)
                {
                    alpha = nodeValue;
                }
            }

            if (!sortedMoves.Any())
            {
                stats.QuiescenceEndNodes++;
            }

            return(alpha);
        }
Beispiel #4
0
    void Start()
    {
        m_randomOffset = Random.value;
        m_weapon       = GetComponentInChildren <AIWeapon>();
        m_anim         = GetComponent <Animator>();
        m_stats        = GetComponent <AIStats>();
        m_player       = Manager.GetPlayer().transform;

        m_randomInterval = Random.value;
    }
    public void SetAIStats(int i, AI_Controller t)
    {
        AIStats a = ai_stats[i];

        t.changeStateTolerance = Ran(a.changeStateTolerance_min, a.changeStateTolerance_max);
        t.normalRate           = Ran(a.normalRate_min, a.normalRate_max);
        t.closeRate            = Ran(a.closeRate_min, a.closeRate_max);
        t.blockingRate         = Ran(a.blockingRate_min, a.blockingRate_max);
        t.aiStateLife          = Ran(a.aiStateLife_min, a.aiStateLife_max);
        t.jumpRate             = Ran(a.jumpRate_min, a.jumpRate_max);
    }
Beispiel #6
0
    private void Start()
    {
        aiStats = gameObject.transform.parent.gameObject.GetComponentInChildren <AIStats>();

        if (!aiStats.isAggressive)
        {
            Destroy(gameObject);
        }
        else
        {
            aiMovement  = gameObject.GetComponentInParent <RandomMovement>();
            alertSprite = GetComponent <SpriteRenderer>();
        }
    }
Beispiel #7
0
    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        Collider2D[] collisionArray = gameObject.GetComponents <Collider2D>();
        foreach (Collider2D c in collisionArray)
        {
            if (!c.isTrigger)
            {
                Physics2D.IgnoreCollision(player.GetComponent <Collider2D>(), c);
            }
        }
        aiStats = gameObject.GetComponent <AIStats>();

        animator = gameObject.GetComponent <Animator>();
    }
Beispiel #8
0
    IEnumerator Start()
    {
        m_Tree = BLNewBehaveLibrary0.InstantiateTree(
            BLNewBehaveLibrary0.TreeType.HeroAI1_NewTree1, this);
        stats = gameObject.GetComponent<AIStats>();
        stats.distanceToTarget = 10;
        stats.fatigue = 100;
        stats.oreCap = 150;
        while (Application.isPlaying && m_Tree != null)
        {
            yield return new WaitForSeconds(1 / m_Tree.Frequency);
            AIUpdate();

        }
    }
    private float GetSmallestMovementSpeed(GameObject[] enemies)
    {
        float smallest = Mathf.Infinity;

        foreach (GameObject enemy in enemies)
        {
            AIStats stats = enemy.GetComponent <AIStats> ();

            if (stats.movementSpeed < smallest)
            {
                smallest = stats.movementSpeed;
            }
        }

        return(smallest);
    }
Beispiel #10
0
    // Use this for initialization
    void Start()
    {
        localTag = AITag.Patrolling;
        AIManager.SubscribeBehaviour(this);
        AIManager.RegisterDefaultBehaviour(behaviourCode);


        // Set up 3 different stats
        charStats = AIManager.GetStats.GetCharacterStats;
        aiStats   = AIManager.GetStats.GetAIStats;

        // Initiate patrol path, calling Initialize and EndPatrol at start and end
        aiStats.InitializePatrolList(new Vector3(transform.position.x, transform.position.y, transform.position.z));
        aiStats.AddNextPatrolDest(new Vector3(transform.position.x + 15.0f, transform.position.y, transform.position.z));
        aiStats.AddNextPatrolDest(new Vector3(transform.position.x + 15.0f, transform.position.y, transform.position.z + 15.0f));
        aiStats.AddNextPatrolDest(new Vector3(transform.position.x, transform.position.y, transform.position.z + 15.0f));
        aiStats.AddEndPatrolSegment();
    }
Beispiel #11
0
    // Use this for initialization
    void Start()
    {
        localTag = AITag.Attacking;
        AIManager.SubscribeBehaviour(this);

        weaponStats = AIManager.GetStats.GetPrimaryWStats;
        aiStats     = AIManager.GetStats.GetAIStats;

        bulletSpawnPos = AIManager.GetStats.GetWeaponObj.transform;
        aimPos         = AIManager.GetStats.GetAimObj.transform;

        GameObject newLaser = Instantiate <GameObject> (linePrefab);

        laserLine = newLaser.GetComponent <LineRenderer> ();

        laserLine.startWidth = 0.005f;
        laserLine.endWidth   = 0.005f;

        laserLine.positionCount = 2;
        laserLine.SetPosition(0, bulletSpawnPos.position);
        laserLine.SetPosition(1, bulletSpawnPos.position);
        laserLine.startColor = new Color(0, 0, 0, 0);
        laserLine.endColor   = new Color(0, 0, 0, 0);
    }
    // Use this for initialization
    void Start()
    {
        Debug.Log("player:: " + PlayerStats.won_index);
        Debug.Log("AI:: " + AIStats.won_index);

        moneyEarned_txt = moneyEarned_object.GetComponent <Text>();

        // initialize variables from the preferences.
        stars_earned = PlayerPrefs.GetInt("stars_num");
        money_earned = PlayerPrefs.GetInt("money_won");
        Debug.Log("win_index:: " + PlayerStats.won_index);



        // load player board winner.
        if (PlayerStats.won_index > -1)
        {
            for (int i = 0; i < 16; i++)
            {
                PlayerStats.boards[PlayerStats.won_index].cards[i] = board.GetChild(0).GetChild(i);
            }

            PlayerStats.LoadBoard(PlayerStats.won_index);
        }
        // load AI board winner.
        else if (AIStats.won_index > -1)
        {
            for (int i = 0; i < 16; i++)
            {
                AIStats.boards[AIStats.won_index].cards[i] = board.GetChild(0).GetChild(i);
            }

            AIStats.LoadBoard();
        }

        // AI won.
        if (AIStats.won_index > -1)
        {
            stars_earned = 0;
            money_earned = Random.Range(5, 10);

            player_won = false;

            if (PlayerStats.coins - money_earned > 0)
            {
                PlayerStats.coins -= money_earned;
            }
            else
            {
                PlayerStats.coins = 0;
            }


            SaveLoadController.SaveStats(PlayerStats.coins, PlayerStats.stars, "player_stats.sav");
        }
        // Player won.
        else if (PlayerStats.won_index > -1)
        {
            player_won = true;

            PlayerStats.coins += money_earned;
            PlayerStats.stars += stars_earned;

            SaveLoadController.SaveStats(PlayerStats.coins, PlayerStats.stars, "player_stats.sav");
        }


        // update user stats (money, exp).
        StartCoroutine(StarAwards(stars_earned));
        StartCoroutine(MoneyAwards(money_earned));

        StartCoroutine(GoToScene(0));
    }
Beispiel #13
0
        /// <summary>
        /// Temporary method to calculating best move.
        /// </summary>
        /// <param name="color">The player color.</param>
        /// <param name="bitboard">The bitboard.</param>
        /// <param name="depth">The current depth.</param>
        /// <param name="bestMove">The best possible move from nested nodes.</param>
        /// <param name="stats">The AI stats.</param>
        /// <returns>The evaluation score of best move.</returns>
        public int Do(Color color, Bitboard bitboard, int depth, int alpha, int beta, AIStats stats)
        {
            var bestValue     = AIConstants.InitialAlphaValue;
            var enemyColor    = ColorOperations.Invert(color);
            var boardHash     = bitboard.GetHashForColor(color);
            var originalAlpha = alpha;

            stats.TotalNodes++;

            if (bitboard.IsThreefoldRepetition())
            {
                stats.EndNodes++;
                return(0);
            }

            if (_transpositionTable.Exists(boardHash))
            {
                var transpositionNode = _transpositionTable.Get(boardHash);

                if (transpositionNode.Depth >= depth)
                {
                    stats.TranspositionTableHits++;
                    switch (transpositionNode.Type)
                    {
                    case ScoreType.Exact:
                    {
                        return(transpositionNode.Score);
                    }

                    case ScoreType.LowerBound:
                    {
                        alpha = Math.Max(alpha, transpositionNode.Score);
                        break;
                    }

                    case ScoreType.UpperBound:
                    {
                        beta = Math.Min(beta, transpositionNode.Score);
                        break;
                    }
                    }

                    if (alpha >= beta)
                    {
                        return(transpositionNode.Score);
                    }
                }
            }

            if (depth <= 0)
            {
                stats.EndNodes++;
                return(_quiescenceSearch.Do(color, bitboard, alpha, beta, stats));
            }

            var whiteGeneratorMode = GetGeneratorMode(color, Color.White);
            var blackGeneratorMode = GetGeneratorMode(color, Color.Black);

            bitboard.Calculate(whiteGeneratorMode, blackGeneratorMode, false);

            if (bitboard.IsCheck(enemyColor))
            {
                stats.EndNodes++;
                return(AIConstants.MateValue + depth);
            }

            Move bestMove = null;

            var availableMoves = SortMoves(color, bitboard, bitboard.Moves);
            var firstMove      = true;

            foreach (var move in availableMoves)
            {
                var bitboardAfterMove = bitboard.Move(move);
                var nodeValue         = 0;

                if (firstMove)
                {
                    nodeValue = -Do(enemyColor, bitboardAfterMove, depth - 1, -beta, -alpha, stats);
                    firstMove = false;
                }
                else
                {
                    nodeValue = -Do(enemyColor, bitboardAfterMove, depth - 1, -alpha - 1, -alpha, stats);

                    if (nodeValue > alpha && nodeValue < beta)
                    {
                        bitboardAfterMove = bitboard.Move(move);
                        nodeValue         = -Do(enemyColor, bitboardAfterMove, depth - 1, -beta, -alpha, stats);
                    }
                }

                if (nodeValue > bestValue)
                {
                    bestValue = nodeValue;
                    bestMove  = move;
                }

                alpha = Math.Max(nodeValue, alpha);

                if (alpha >= beta)
                {
                    stats.AlphaBetaCutoffs++;
                    break;
                }
            }

            if (bestValue == -(AIConstants.MateValue + depth - 1) && !bitboard.IsCheck(color))
            {
                stats.EndNodes++;
                return(0);
            }

            var updateTranspositionNode = new TranspositionNode();

            updateTranspositionNode.Score    = bestValue;
            updateTranspositionNode.Depth    = depth;
            updateTranspositionNode.BestMove = bestMove;

            if (bestValue <= originalAlpha)
            {
                updateTranspositionNode.Type = ScoreType.UpperBound;
            }
            else if (bestValue >= beta)
            {
                updateTranspositionNode.Type = ScoreType.LowerBound;
            }
            else
            {
                updateTranspositionNode.Type = ScoreType.Exact;
            }

            _transpositionTable.AddOrUpdate(boardHash, updateTranspositionNode);

            return(bestValue);
        }
Beispiel #14
0
 void Start()
 {
     m_stats = GetComponent <AIStats>();
 }
        /// <summary>
        /// Regular search, the core of AI algorithms.
        /// </summary>
        /// <param name="color">The player color.</param>
        /// <param name="bitboard">The bitboard.</param>
        /// <param name="depth">The current depth.</param>
        /// <param name="alpha">The alpha value.</param>
        /// <param name="beta">The beta value.</param>
        /// <param name="deadline">The deadline (time after which search is immediately terminated).</param>
        /// <param name="helper">The flag indicating whether the search is an helper or not.</param>
        /// <param name="stats">The AI stats.</param>
        /// <returns>The evaluation score of best move.</returns>
        public int Do(Color color, Bitboard bitboard, int depth, int alpha, int beta, long deadline, bool helper, AIStats stats)
        {
            var root = stats.TotalNodes == 0;

            var bestValue     = AIConstants.InitialAlphaValue;
            var enemyColor    = ColorOperations.Invert(color);
            var boardHash     = bitboard.GetHashForColor(color);
            var originalAlpha = alpha;

            stats.TotalNodes++;

            if (bitboard.IsThreefoldRepetition())
            {
                stats.EndNodes++;
                return(0);
            }

#if TRANSPOSITION_TABLE
            if (_transpositionTable.Exists(boardHash))
            {
                var transpositionNode = _transpositionTable.Get(boardHash);

                if (transpositionNode.Depth >= depth)
                {
                    stats.TranspositionTableHits++;
                    switch (transpositionNode.Type)
                    {
                    case ScoreType.Exact:
                    {
                        return(transpositionNode.Score);
                    }

                    case ScoreType.LowerBound:
                    {
                        alpha = Math.Max(alpha, transpositionNode.Score);
                        break;
                    }

                    case ScoreType.UpperBound:
                    {
                        beta = Math.Min(beta, transpositionNode.Score);
                        break;
                    }
                    }

                    if (alpha >= beta)
                    {
                        return(transpositionNode.Score);
                    }
                }
            }
#endif

            if (depth <= 0)
            {
                stats.EndNodes++;
#if QUIESCENCE_SEARCH
                return(_quiescenceSearch.Do(color, bitboard, alpha, beta, stats));
#else
                bitboard.Calculate(GeneratorMode.CalculateAttacks, false);
                return(bitboard.GetEvaluation());
#endif
            }

            var whiteGeneratorMode = GetGeneratorMode(color, Color.White);
            var blackGeneratorMode = GetGeneratorMode(color, Color.Black);
            bitboard.Calculate(whiteGeneratorMode, blackGeneratorMode, false);

            if (bitboard.IsCheck(enemyColor))
            {
                stats.EndNodes++;
                return(AIConstants.MateValue + depth);
            }

            Move bestMove = null;

            var availableMoves = SortMoves(color, depth, bitboard, bitboard.Moves, helper);
            var firstMove      = true;

            foreach (var move in availableMoves)
            {
                if (DateTime.Now.Ticks >= deadline)
                {
                    break;
                }

                if (root)
                {
                    if (_patternsDetector.IsPattern(bitboard, move))
                    {
                        continue;
                    }
                }

                var bitboardAfterMove = bitboard.Move(move);
                var nodeValue         = 0;

                if (firstMove)
                {
                    nodeValue = -Do(enemyColor, bitboardAfterMove, depth - 1, -beta, -alpha, deadline, helper, stats);
#if NEGASCOUT
                    firstMove = false;
#endif
                }
                else
                {
                    nodeValue = -Do(enemyColor, bitboardAfterMove, depth - 1, -alpha - 1, -alpha, deadline, helper, stats);

                    if (nodeValue > alpha && nodeValue < beta)
                    {
                        bitboardAfterMove = bitboard.Move(move);
                        nodeValue         = -Do(enemyColor, bitboardAfterMove, depth - 1, -beta, -alpha, deadline, helper, stats);
                    }
                }

                if (nodeValue > bestValue)
                {
                    bestValue = nodeValue;
                    bestMove  = move;
                }

                alpha = Math.Max(nodeValue, alpha);

                if (alpha >= beta)
                {
                    if (move is QuietMove)
                    {
                        _historyTable.AddKiller(color, depth, bestMove);
                        _killerTable.AddKiller(depth, move);
                    }

#if ALPHABETA_PRUNNING
                    stats.AlphaBetaCutoffs++;
                    break;
#endif
                }
            }

            if (bestValue == -(AIConstants.MateValue + depth - 1) && !bitboard.IsCheck(color))
            {
                stats.EndNodes++;
                return(0);
            }

            var updateTranspositionNode = new TranspositionNode
            {
                Score    = bestValue,
                Depth    = depth,
                BestMove = bestMove,
                Type     = GetTranspositionNodeType(originalAlpha, beta, bestValue)
            };

            _transpositionTable.AddOrUpdate(boardHash, updateTranspositionNode);

            return(bestValue);
        }