IEnumerator GiveCommand()
    {
        Debug.Log("Starting command...");
        Planning.Planner planner          = null;
        bool             IsClickOnPlanner = Mouseover <Planning.Planner>(out planner);

        if (!IsClickOnPlanner)
        {
            Debug.Log("Not a planner");
            yield return(null);
        }
        else
        {
            Debug.Log("Hit" + planner.gameObject.name);
        }
        // Wait until they let go
        AIGoal goal = null;

        yield return(new WaitUntil(()
                                   => Mouseover(out goal)));

        Debug.Log("Commanding " + planner.gameObject.name + " to " + goal.gameObject.name);
        // Add the goal's conditions to the planner
        planner.goal = goal.goal.Copy();
        planner.DoPlanning();
        // Wait until it's ready
        yield return(new WaitUntil(() => !planner.currentlyPlanning));

        // Start the AI routine to achieve whatever goal you just set;
        planner.ExecutePlan();

        Debug.Log("Ending Command");
        yield return(null);
    }
示例#2
0
    public void AddSubGoal(AIGoal goal)
    {
        if (goal == null)
        {
            return;
        }

        mSubGoals.Add(goal);
    }
示例#3
0
    public bool AddCommand(AIGoal goal)
    {
        if (mStateCurrent == null)
        {
            return(false);
        }

        mStateCurrent.AddCommand(goal);
        return(true);
    }
示例#4
0
    public void AddCommand(AIGoal goal)
    {
        if (goal == null)
        {
            return;
        }

        mGoalQueue.AddSubGoal(goal);
        return;
    }
示例#5
0
    private void DisplayGoals()
    {
        if (agent.GoalsObject == null)
        {
            agent.GoalsObject = ObtainChildObject("Goals");
        }

        AIGoal[] goals = agent.GoalsObject.GetComponents <AIGoal>();

        EditorGUILayout.LabelField("Goals:", EditorStyles.boldLabel);
        if (goals == null || goals.Length == 0)
        {
            EditorGUILayout.HelpBox("You should select at least one goal in order for the AIAgent to do anything!", MessageType.Warning);
        }
        else
        {
            AIGoal goalToRemove = null;
            for (int i = 0; i < goals.Length; i++)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUIUtility.labelWidth = 50f;
                EditorGUILayout.LabelField(goals[i].ToString());

                goals[i].Priority = Mathf.Clamp(EditorGUILayout.FloatField("Priority", goals[i].Priority), 0, float.MaxValue);

                if (GUILayout.Button("Remove"))
                {
                    goalToRemove = goals[i];
                }
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.Space();
            }

            if (goalToRemove != null)
            {
                DestroyImmediate(goalToRemove);
            }
        }

        EditorGUILayout.BeginHorizontal();

        System.Type[] allTypes            = (from System.Type type in Assembly.GetAssembly(typeof(AIGoal)).GetTypes() where type.IsSubclassOf(typeof(AIGoal)) select type).ToArray();
        string[]      allTypesStringArray = (from System.Type type in allTypes select type.ToString()).ToArray();

        selectedGoalIndex = EditorGUILayout.Popup(selectedGoalIndex, allTypesStringArray);
        if (GUILayout.Button("Add selected goal"))
        {
            if (!ComponentExists(goals, allTypes[selectedGoalIndex]))
            {
                AIGoal newGoal = (AIGoal)agent.GoalsObject.AddComponent(allTypes[selectedGoalIndex]);
                newGoal.Priority = 1;
            }
        }
        EditorGUILayout.EndHorizontal();
    }
示例#6
0
 public void AddGoal(AIGoal goal)
 {
     goal.entity = entity;
     goals.Add(goal);
     goal.Prioritize();
     goals.Sort();
     if (goals[0] != activeGoal) {
         activeGoal = goals[0];
         SetAIState();
     }
 }
示例#7
0
    public override bool Init(int aiId)
    {
        if (!base.Init(aiId))
        {
            return(false);
        }

        // 解析idle状态默认行为
        AIGoal goal = CommonAI.ParseGoalXML(this, mRes.commonIdle);

        if (goal != null)
        {
            mStateIdle.AddCommand(goal);
        }

        return(true);
    }
示例#8
0
    static public AIGoal ParseGoalXML(CommonAI ai, string xmlData)
    {
        if (xmlData == null)
        {
            return(null);
        }

        if (xmlData.Length == 0)
        {
            return(null);
        }

        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(xmlData);

        XmlNode node = xmlDoc.FirstChild;

        if (node.Name != "root")
        {
            return(null);
        }

        bool loop = node.Attributes["loop"] != null?System.Convert.ToBoolean(node.Attributes["loop"].Value) : false;

        AIGoalCompositeGoal rootGoal = new AIGoalCompositeGoal(ai, loop);

        XmlNodeList nodeList = node.ChildNodes;

        for (int i = 0; i < nodeList.Count; i++)
        {
            AIGoal goal = ParseGoalNode(ai, nodeList[i]);
            if (goal == null)
            {
                return(null);
            }

            rootGoal.AddSubGoal(goal);
        }

        return(rootGoal);
    }
示例#9
0
 public void SetGoal(AIGoal goal)
 {
     this.activeGoal = goal;
     if (activeSubstate != null) {
      //   activeSubstate.OnGoalChanged(goal);
     }
 }
示例#10
0
 public void ActivateGoal(AIGoal goal)
 {
     if (goal.status == AIGoalStatus.Inactive) {
         goal.OnActivate();
     }
 }
示例#11
0
 public void AddDynamicGoal(AIGoal goal)
 {
     goal.type = AIGoalType.Dynamic;
     AddGoal(goal);
 }
示例#12
0
    public Queue <AIAction> CalculatePlan(AIBrain ai, List <AIAction> allActions, StateDictionary currentWorldState, List <AIGoal> allGoals, out AIGoal activeGoal)
    {
        List <AIAction> applicapableActions = new List <AIAction>();

        foreach (AIAction action in allActions)
        {
            if (action.CanBeAddedToPlan(ai)) // to remove unnecessary action branch in tree
            {
                action.JustBeforePlan(ai);
                action.CalculateCost(ai); // Some actions can use dynamic cost based on confidence factors
                applicapableActions.Add(action);
            }
        }

        applicapableActions = applicapableActions.OrderBy(x => x.Cost).ToList();

        List <Node> goalMatchingNodes = new List <Node>();

        foreach (AIGoal goal in allGoals)
        {
            if (!goal.IsApplicapable(ai))
            {
                goal.Applicapable = false;
                continue;
            }
            goal.Applicapable = true;

            minCostPlan = new List <AIAction>();

            //letter = 0;
            Node            startNode   = new Node(/*letter++ + ""*/);
            StateDictionary cWorldState = new StateDictionary(currentWorldState.conditions);

            // Creates paths including first lowest cost action path
            float maxCSoFar = Mathf.Infinity;

            List <AIAction> applicapableNRelatedActions = new List <AIAction>();
            foreach (var action in applicapableActions)
            {
                if (goalsToRelatedActions[goal].Contains(action))
                {
                    applicapableNRelatedActions.Add(action);
                }
            }

            CreateActionTree(startNode, cWorldState, goal.goalStates, applicapableNRelatedActions /*applicapableActions*/, goalMatchingNodes, ref maxCSoFar);
            if (minCostPlan.Count > 0)
            {
                Queue <AIAction> actionQ = new Queue <AIAction>();
                foreach (AIAction action in minCostPlan)
                {
                    actionQ.Enqueue(action);
                }
                activeGoal      = goal;
                goal.lastUsedAt = Time.time;
                return(actionQ);
            }
            else
            {
                continue;
            }
        }
        activeGoal = null;
        return(null);
    }
示例#13
0
    public Queue <AIAction> CalculatePlan(AIBrain ai, List <AIAction> allActions, StateDictionary currentWorldState, List <AIGoal> allGoals, out AIGoal activeGoal)
    {
        // 挑选出当前可行的 行为然后计算消耗 加入可执行列表
        List <AIAction> applicapableActions = new List <AIAction>();

        foreach (AIAction action in allActions)
        {
            if (action.CanBeAddedToPlan(ai))             // to remove unnecessary action branch in tree
            {
                action.JustBeforePlan(ai);
                action.CalculateCost(ai);                 // Some actions can use dynamic cost based on confidence factors
                applicapableActions.Add(action);
            }
        }


        //按照消耗排序当前 可以执行的行为
        applicapableActions = applicapableActions.OrderBy(x => x.Cost).ToList();

        List <Node> goalMatchingNodes = new List <Node>();

        foreach (AIGoal goal in allGoals)
        {
            //跳过当前不能执行的方案
            if (!goal.IsApplicapable(ai))
            {
                goal.Applicapable = false;
                continue;
            }

            //更新目标的可执行状态
            goal.Applicapable = true;

            //最小消耗的行为列表
            minCostPlan = new List <AIAction>();

            //letter = 0;
            Node            startNode   = new Node(/*letter++ + ""*/);
            StateDictionary cWorldState = new StateDictionary(currentWorldState.conditions);            //拷贝了一个状态
            List <AIAction> applicapableNRelatedActions = new List <AIAction>();

            // Creates paths including first lowest cost action path
            float maxCSoFar = Mathf.Infinity;


            //所有可用行为中 如果有涉及当前目标的行为 则加入列表
            foreach (var action in applicapableActions)
            {
                if (goalsToRelatedActions[goal].Contains(action))
                {
                    applicapableNRelatedActions.Add(action);
                }
            }

            // 至此 已经按照消耗排序 并且筛选当前目标可用的行为
            //
            CreateActionTree(/* 空节点 */ startNode, /* 世界状态 */ cWorldState, /* 当前目的 */ goal.goalStates, /* 当前目标牵涉到的行为 */ applicapableNRelatedActions /*applicapableActions*/, goalMatchingNodes, ref maxCSoFar);


            if (minCostPlan.Count > 0)
            {
                Queue <AIAction> actionQ = new Queue <AIAction>();
                foreach (AIAction action in minCostPlan)
                {
                    actionQ.Enqueue(action);
                }
                activeGoal      = goal;
                goal.lastUsedAt = Time.time;
                return(actionQ);
            }
            else
            {
                continue;
            }
        }
        activeGoal = null;
        return(null);
    }
示例#14
0
 private void UpdateActiveGoals()
 {
     for (int i = 0; i < goals.Count; i++) {
         AIGoal goal = goals[i];
         if (goal.status != AIGoalStatus.Pending) continue;
         if (goal.UpdateGoalProgress() == AIGoalStatus.Pending) {
             goal.Prioritize();
         } else {
             goal.priority = -1f;
         }
     }
     goals.Sort();
     if (goals[0] != activeGoal) {
         activeGoal = goals[0];
         SetAIState();
     }
 }
示例#15
0
 public AIState(AIGoal goal, Dictionary <string, object> newState)
 {
     preconditions = new Dictionary <string, object>(goal.GetConditions());
     state         = newState;
 }
示例#16
0
 private void Awake()
 {
     goal = CreateGoal(priority);
 }
示例#17
0
 public void RemoveGoal(AIGoal goal)
 {
     goal.priority = -1f;
     if (activeGoal == goal) {
         activeGoal = goals[0];
         SetAIState();
     }
 }
示例#18
0
 public void DeactivateGoal(AIGoal goal)
 {
     if (goal.status == AIGoalStatus.Pending) {
         goal.OnDeactivate();
     }
 }