コード例 #1
0
ファイル: BGoapNode.cs プロジェクト: M0rt1mer/Unity-goap
    public BGoapNode(IGoapPlanner planner, BGoapState parentGoal, BGoapNode parent, ReGoapActionState actionState)
    {
        this.planner = planner;
        this.parent = parent;
        if(actionState != null) {
            this.action = actionState.Action;
            this.actionSettings = actionState.Settings;
        }

        if (this.parent != null){
            g = parent.GetPathCost();
        }

        var nextAction = parent == null ? null : parent.action;
        if(action != null) {

            //first step - subtract effects of action
            var effects = action.GetEffects( parentGoal, actionSettings, nextAction );
            try {
                goal = parentGoal.Difference( effects, false ); //dont use defaults here, only subtract what really is in the effect
            } catch(ArgumentException e) {
                Debug.Log( e );
            }
            //then add preconditions to the current goal state
            var preconditions = action.GetPreconditions( parentGoal, actionSettings, nextAction );
            goal = goal.Union( preconditions );
            
            g += action.GetCost( parentGoal, actionSettings, nextAction );

        } else goal = parentGoal;
        h = goal.Distance( planner.GetCurrentAgent().GetMemory().GetWorldState() );
        // f(node) = g(node) + h(node)
        cost = g + h * heuristicMultiplier;
    }
コード例 #2
0
    public override string ToString()
    {
        BGoapNode node = content as BGoapNode;

        if (node.action != null)
        {
            return(node.GetPathCost() + "+" + node.GetHeuristicCost() + "     " + node.action + "\n\n"
                   + string.Join("\n", ((content as BGoapNode).GetState() as IEnumerable <KeyValuePair <IStateVarKey, object> >).Select(x => (x.Key.Name + ":" + x.Value)).ToArray()));
        }
        return(string.Join("\n", ((content as BGoapNode).GetState() as IEnumerable <KeyValuePair <IStateVarKey, object> >).Select(x => (x.Key.Name + ":" + x.Value)).ToArray()));
    }
コード例 #3
0
    protected override IEnumerable <Node> GetChildren(Node node)
    {
        BGoapNode goapNode = (node as AStarDebugNode).content as BGoapNode;

        if (childLists.ContainsKey(goapNode))
        {
            foreach (var BGoapNode in childLists.GetValues(goapNode, true))
            {
                yield return(new AStarDebugNode(BGoapNode, goal));
            }
        }
    }
コード例 #4
0
    private void DrawNodesUsingGraphAPI(Rect canvas)
    {
        if (selectedRecording != null && selectedRecording != lastDisplayedRecording)
        {
            MultiValueDictionary <BGoapNode, BGoapNode> childNodes = new MultiValueDictionary <BGoapNode, BGoapNode>();
            BGoapNode root = null;

            foreach (INode <BGoapState> inode in selectedRecording.search)
            {
                if (inode.GetParent() != null)
                {
                    childNodes.Add(inode.GetParent() as BGoapNode, inode as BGoapNode);
                }
                else
                {
                    root = inode as BGoapNode;
                }
            }

            graph = new AStarDebugGraph(childNodes, root, selectedRecording.goal);
            graph.Refresh();
            if (graph.IsEmpty())
            {
                ShowMessage("No graph data");
                return;
            }

            if (layout == null)
            {
                layout = new ReingoldTilford(false);
            }

            layout.CalculateLayout(graph);

            if (renderer == null)
            {
                renderer = new AStarDebugGraphRenderer();
            }

            renderer.Draw(layout, canvas, new GraphSettings()
            {
                maximumNodeSizeInPixels = 200, maximumNormalizedNodeSize = 1f, aspectRatio = 1.61f
            }, fontSize, offset);
        }
    }
コード例 #5
0
 public AStarDebugGraph(MultiValueDictionary <BGoapNode, BGoapNode> childLists, BGoapNode root, BGoapState goal)
 {
     this.childLists = childLists;
     this.root       = root;
     this.goal       = goal;
 }