예제 #1
0
    private List <GoapAction> FindUsableActions(List <KeyValuePair <string, object> > currentState,
                                                List <GoapAction> exclude, KeyValuePair <string, object> goal, float cost)
    {
        List <GoapAction> newActions = new List <GoapAction>();
        List <KeyValuePair <GoapAction, int> > actionPriority = new List <KeyValuePair <GoapAction, int> >();

        int goalS = (int)goal.Value;
        int invS  = (int)currentState.Find(e => e.Key.Equals("In" + goal.Key.Substring(2))).Value;
        int carS  = (int)currentState.Find(e => e.Key.Equals("Ca" + goal.Key.Substring(2))).Value;

        foreach (GoapAction a in actionList)
        {
            if (a.IsActionUsable(currentState))
            {
                List <KeyValuePair <string, object> > actionEffects = a.effects;
                int potential = 0;

                if (a.GetType().Name == "InventoryToCar")
                {
                    potential += invS;
                }

                if (a.GetType().Name == "CarToInventory")
                {
                    potential += goalS;
                }

                foreach (KeyValuePair <string, object> effect in actionEffects)
                {
                    if (effect.Key.Substring(2).Equals(goal.Key.Substring(2)))
                    {
                        potential          += (int)effect.Value * Mathf.Max(goalS - invS - carS, 0) + Mathf.Max(goalS - carS, 0);
                        isGoalActionVisited = true;
                    }
                }

                potential -= usedActionList.Contains(a) ? 2 : -1;
                actionPriority.Add(new KeyValuePair <GoapAction, int>(a, potential));
                Debug.Log(GoapAgent.Display(a) + ": " + potential);
            }
        }
        actionPriority.Sort((a, b) => - 1 * a.Value.CompareTo(b.Value));//Desc

        foreach (KeyValuePair <GoapAction, int> action in actionPriority)
        {
            GoapAction a = action.Key;
            newActions.Add(a);
            usedActionList.Add(a);
        }

        if (isGoalActionVisited)
        {
            usedActionList      = new HashSet <GoapAction>();
            isGoalActionVisited = false;
        }

        return(newActions);
    }
예제 #2
0
    public void PlanFound(KeyValuePair <string, object> goal, Queue <GoapAction> actions)
    {
        GameObject[] texts = GameObject.FindGameObjectsWithTag("plan_text");
        for (int i = 0; i < texts.Length; i++)
        {
            GameObject.Destroy(texts[i]);
        }

        GameObject textObject = Instantiate(planTextPrefab, planPlane.transform) as GameObject;

        textObject.GetComponent <Text>().text = "The Number " + planIndex.ToString() + " Plan.";
        foreach (GoapAction a in actions)
        {
            textObject = Instantiate(planTextPrefab, planPlane.transform) as GameObject;
            textObject.GetComponent <Text>().text = GoapAgent.Display(a);
        }
        planIndex++;
        Debug.Log(GoapAgent.Display(actions.ToArray()));
    }
예제 #3
0
    private List <GoapNode> DoPlan(List <GoapNode> leaves, List <GoapAction> actions, List <KeyValuePair <string, object> > worldState, KeyValuePair <string, object> goal)
    {
        foreach (GoapAction a in actions)
        {
            actionList.Add(a);
            a.DoReset();
        }

        Debug.Log("Start World:" + GoapAgent.Display(worldState));

        List <GoapAction> usableActions = FindUsableActions(worldState, new List <GoapAction>(), goal, 0);

        // build graph
        GoapNode start = new GoapNode(null, 0, worldState, null);

        bool success = BuildGOAP(start, leaves, usableActions, goal, 0);

        if (!success)
        {
            return(new List <GoapNode>());
        }

        return(leaves);
    }
예제 #4
0
 public void PlanAborted(GoapAction aborter)
 {
     actionText.GetComponent <Text>().text = "Plan " + planIndex + " action " + GoapAgent.Display(aborter) + " Aborted.";
 }