Example #1
0
    private void FindAndExecuteAction()
    {
        //check if there is current goal and if the current goal is met
        //if met, find next goal
        //if not met, check if there's current action. if there is action leave it alone. if no action then calculate planner and execute first action
        //if no current goal, get next goal

        CsDebug.Inst.CharLog(_parentCharacter, "Start finding and executing action");

        if (_currentGoal == null)
        {
            CsDebug.Inst.CharLog(_parentCharacter, "no current goal, getting a new one");
            _currentGoal = GetNextGoal();

            CsDebug.Inst.CharLog(_parentCharacter, "found new goal " + _currentGoal.Name);
            _currentAction = null;
        }

        int counter = _goals.Count;

        while (counter > 0 && _currentGoal != null)
        {
            CsDebug.Inst.CharLog(_parentCharacter, "Find&Execute: checking goal " + _currentGoal.Name);
            bool needNextGoal = false;
            if (_currentAction == null && _currentGoal != null)
            {
                if (!EvaluateGoal(_currentGoal))
                {
                    CsDebug.Inst.CharLog(_parentCharacter, "current goal " + _currentGoal.Name + " is not met, running planner, character " + _parentCharacter.name);
                    _actionQueue = Planner.GetActionQueue(_currentGoal, _actions);
                    if (_actionQueue != null && _actionQueue.Count > 0)
                    {
                        _currentAction = _actionQueue.Dequeue();
                        CsDebug.Inst.CharLog(_parentCharacter, "Found current action " + _currentAction.Name + " for goal " + _currentGoal.Name);
                        if (_currentAction.ExecuteAction())
                        {
                            //action executed successfully
                            return;
                        }
                    }
                    //failed to find action for current goal, get next goal
                    CsDebug.Inst.CharLog(_parentCharacter, "Failed to find action for goal " + _currentGoal.Name);
                    needNextGoal = true;
                }
                else
                {
                    CsDebug.Inst.CharLog(_parentCharacter, "Goal is already met: " + _currentGoal.Name);
                    needNextGoal = true;
                }
            }
            else
            {
                //there's action; leave it alone
                return;
            }

            if (needNextGoal)
            {
                _currentGoal = GetNextGoal();

                _currentAction = null;

                if (_currentGoal != null)
                {
                    CsDebug.Inst.CharLog(_parentCharacter, "getting next goal; result: " + _currentGoal.Name);
                }
                else
                {
                    CsDebug.Inst.CharLog(_parentCharacter, "getting next goal; result: null");
                }
            }


            counter--;
        }
    }