/// <summary>
        /// Take the Top node and make it the left or right child.
        /// Take the left or right child and make it top node.
        /// </summary>
        /// <param name="side"></param>
        /// <param name="meta"></param>
        /// <returns></returns>
        private NodeMeta RotateToChild(Side side, NodeMeta meta)
        {
            var otherSide = OtherSide(side);

            // Three nodes to rotate
            Node oldParent = meta.Node;
            Node newParent = oldParent[otherSide];
            Node child     = newParent[side];

            newParent[side] = oldParent;
            //            newParent[otherSide] = oldParent[otherSide];
            oldParent[otherSide] = child;

            NodeMeta grandParentMeta = meta.ParentMeta;

            if (grandParentMeta == null)
            {
                SetRoot(newParent);
            }
            else
            {
                Node grandParent = grandParentMeta.Node;
                grandParent[meta.SideFromParent] = newParent;
            }

            return(grandParentMeta);
        }
        private NodeMeta Remove(NodeMeta meta)
        {
            Node node  = meta.Node;
            Side side  = Side.Left;
            Node child = node[side];

            if (child == null)
            {
                side  = Side.Right;
                child = node[side];
            }
            if (child == null)
            {
                // adjust color through parent.
                if (node.Color == Color.Black)
                {
                    // Black-Black
                    var sentinel = new SentinelNode();
                    meta.ParentMeta.Node[meta.SideFromParent] = sentinel;
                    var sentinelMeta = new NodeMeta(meta.ParentMeta, sentinel, meta.SideFromParent, meta.Sibling);
                    return(sentinelMeta);
                }
                else
                {
                    // Red with Black Parent. Done.
                    meta.ParentMeta.Node[meta.SideFromParent] = null;
                    return(null);
                }
            }
            else
            {
                NodeMeta leaf = SwapWithAdjacent(meta, side);
                return(Remove(leaf));
            }
        }
Example #3
0
        private void CreateNode()
        {
            NodeMeta         nodeProto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(BTEditor.Instance.selectNodeName);
            BehaviorNodeData nodeData  = BTEditor.Instance.CreateNode(nodeProto.name);

            CreateNode(nodeData, MousePosToGraphPos(mMousePos));
        }
Example #4
0
        public NodeMeta GetNodeMeta(string nodeName)
        {
            NodeMeta nodeMeta = null;

            this.nameNodeMetas.TryGetValue(nodeName, out nodeMeta);
            return(nodeMeta);
        }
Example #5
0
        private void CreateNode()
        {
            NodeMeta         nodeProto = BTEntity.Instance.GetNodeMeta(BTEntity.Instance.selectNodeName);
            BehaviorNodeData nodeData  = BTEntity.Instance.CreateNode((int)BTEntity.Instance.CurTree.Id, nodeProto.name);

            CreateNode(nodeData, MousePosToGraphPos(mMousePos));
        }
 public NodeMeta(NodeMeta parentMeta, Node node, Side sideFromParent, Node sibling)
 {
     this.ParentMeta     = parentMeta;
     this.Node           = node;
     this.SideFromParent = sideFromParent;
     this.Sibling        = sibling;
 }
        private NodeMeta RotateToGrandChild(Side side, NodeMeta meta)
        {
            var otherSide = OtherSide(side);

            // Three nodes to rotate
            Node oldParent = meta.Node;
            Node newParent = oldParent[otherSide][side];
            Node child     = newParent[side];


            newParent[side]      = oldParent;
            newParent[otherSide] = oldParent[otherSide];
            oldParent[otherSide] = child;

            oldParent[otherSide] = child;
            newParent[side]      = oldParent;

            NodeMeta parentMeta = meta.ParentMeta;

            if (parentMeta == null)
            {
                _root           = newParent;
                newParent.Color = Color.Black;
            }
            else
            {
                Node nextLevelUp = parentMeta.Node;
                nextLevelUp[meta.ParentMeta.SideFromParent] = newParent;
            }

            return(parentMeta);
        }
Example #8
0
        //有待优化
        private void ChangeNodeType(object obj)
        {
            string           nodeType  = (string)obj;
            NodeMeta         nodeProto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(nodeType);
            BehaviorNodeData nodeData  = BTEditor.Instance.CreateNode(nodeProto.name);
            NodeDesigner     oldNode   = mSelectedNode;
            NodeDesigner     newNode   = new NodeDesigner(nodeData);

            if (oldNode == RootNode)
            {
                newNode.NodeData.Id = RootNode.NodeData.Id;
                RootNode            = newNode;
                BehaviorTreeData oldTree = BTEditor.Instance.CurTree;
                BehaviorTreeData newTree = new BehaviorTreeData()
                {
                    classify = oldTree.classify,
                    Root     = nodeData
                };
                BTEditor.Instance.CurTree = newTree;
            }
            else
            {
                int idx = oldNode.Parent.Children.IndexOf(oldNode);
                oldNode.Parent.AddChild(newNode, idx);
                oldNode.Parent.RemoveChild(oldNode);
            }

            foreach (NodeDesigner child in oldNode.Children)
            {
                newNode.AddChild(child);
            }
            BTEditor.Instance.ResetTreeId();
            Game.EventSystem.Run(EventIdType.BehaviorTreeAfterChangeNodeType);
        }
Example #9
0
 private static int CompareShowName(NodeMeta nodeType1, NodeMeta nodeType2)
 {
     if (string.IsNullOrEmpty(nodeType1.name) || string.IsNullOrEmpty(nodeType2.name))
     {
         Log.Error("字符串输入参数有误");
     }
     return(String.CompareOrdinal(nodeType1.name, nodeType2.name));
 }
Example #10
0
        public void onCreateNode(params object[] list)
        {
            string  name = (string)list[0];
            Vector2 pos  = (Vector2)list[1];

            NodeMeta         nodeProto = BTEntity.Instance.GetNodeMeta(name);
            BehaviorNodeData nodeData  = BTEntity.Instance.CreateNode((int)BTEntity.Instance.CurTree.Id, nodeProto.name);

            CreateNode(nodeData, pos);
        }
Example #11
0
        public void onCreateNode(params object[] list)
        {
            string  nodeName = (string)list[0];
            Vector2 pos      = (Vector2)list[1];

            NodeMeta         nodeProto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(nodeName);
            BehaviorNodeData nodeData  = BTEditor.Instance.CreateNode(nodeProto.name);

            CreateNode(nodeData, pos);
        }
Example #12
0
 /// <summary>
 /// Additional public method overload to simplify node meta creation
 /// </summary>
 public void AddChildComposite(RobotChassis value, string containerName)
 {
   var meta = new NodeMeta
   {
     Name = Guid.NewGuid().ToString(),
     GroupName = GROUP_NAME,
     DisplayName = containerName
   };
   base.AddChildComposite(value, meta);
 }
Example #13
0
        private NodeMeta GetNodeMeta(T node)
        {
            NodeMeta metaData = null;

            if (!this.metaData.TryGetValue(node, out metaData))
            {
                metaData = new NodeMeta(node);
                this.metaData.Add(node, metaData);
            }

            return(metaData);
        }
Example #14
0
        private NodeMeta GetFillData(T node)
        {
            NodeMeta nodeMeta = null;

            if (!fillData.TryGetValue(node, out nodeMeta))
            {
                nodeMeta       = new NodeMeta(node);
                fillData[node] = nodeMeta;
            }

            return(nodeMeta);
        }
 public void Delete(int value)
 {
     if (root == null)
     {
         // Do nothing. Not found.
     }
     else
     {
         var meta    = new NodeMeta(root);
         var removed = Remove(meta, value);
         BalanceTree(removed);
     }
 }
        private void RemoveDoubleBlack(NodeMeta meta)
        {
            Node node = meta.Node;

            if (node is SentinelNode)
            {
                meta.ParentMeta.Node[meta.SideFromParent] = null;
            }
            else
            {
                node.Color = Color.Black;
            }
        }
        private NodeMeta Adjacent(Side side, NodeMeta meta)
        {
            Node node         = meta.Node;
            Side otherSide    = OtherSide(side);
            var  adjacent     = node[side];
            var  adjacentMeta = new NodeMeta(meta, adjacent, side, node[otherSide]);

            for (Node iter = adjacent; iter != null; iter = iter[otherSide])
            {
                adjacent     = iter;
                adjacentMeta = new NodeMeta(meta, adjacent, side, node[side]);
            }
            return(adjacentMeta);
        }
        /// <summary>
        /// Building Field2 Node
        /// </summary>
        /// <param name="displayName">Optional, defaults to field's property name when empty or null</param>
        /// <returns>Value Node for Field2 </returns>
        public Node BuildNode_Field2(string displayName = null)
        {
            NodeMeta meta = new NodeMeta(BuildNodeId_Field2());

            SetCommonAttributes(meta);
            if (string.IsNullOrWhiteSpace(displayName))
            {
                displayName =
                    GetNodeIdCodes(_nodeIdField)
                    .Aggregate("Field2", (current, code) => current + " " + code);
            }
            meta.Name = displayName;
            return(Node.NewValue(meta, ValueSource.Entity, Convert.ToDecimal(_field2)));
        }
Example #19
0
        private void AddNeighbours(NodeMeta nodeMeta)
        {
            foreach (var pathfindingNode in nodeMeta.Node.GetNeighbours())
            {
                var neighbour = (T)pathfindingNode;

                NodeMeta neighNodeMeta = GetFillData(neighbour);
                if (!frontier.Contains(neighNodeMeta) && traverser.CanTraverse(neighNodeMeta.Node))
                {
                    neighNodeMeta.GCost = nodeMeta.GCost + traverser.GetTraverseCost(nodeMeta.Node, neighNodeMeta.Node);
                    frontier.Add(neighNodeMeta);
                }
            }
        }
        public void Insert(int value)
        {
            var newNode = new Node(value);

            if (root == null)
            {
                SetRoot(newNode);
            }
            else
            {
                var meta = new NodeMeta(root);
                Insert(meta, newNode);
            }
        }
        private void Insert(NodeMeta meta, Node newNode)
        {
            var  node  = meta.Node;
            Side side  = newNode.Value < node.Value ? Side.Left : Side.Right;
            Node child = node[side];

            if (child == null)
            {
                node[side] = newNode;
                BalanceTree(new NodeMeta(meta, newNode, side, node[OtherSide(side)]));
            }
            else
            {
                Insert(new NodeMeta(meta, child, side, node[OtherSide(side)]), newNode);
            }
        }
Example #22
0
        public static Dictionary <string, NodeMeta> ExportToDict()
        {
            Dictionary <string, NodeMeta> name2NodeProtoDict = new Dictionary <string, NodeMeta>();
            List <Type> types = Game.EventSystem.GetTypes();

            foreach (Type type in types)
            {
                NodeMeta proto = GetNodeTypeProtoFromType(type);
                if (proto == null)
                {
                    continue;
                }
                name2NodeProtoDict.Add(proto.name, proto);
            }
            return(name2NodeProtoDict);
        }
Example #23
0
        public static Dictionary <string, NodeMeta> ExportToDict()
        {
            Dictionary <string, NodeMeta> name2NodeProtoDict = new Dictionary <string, NodeMeta>();

            Type[] types = DllHelper.GetMonoTypes();
            foreach (Type type in types)
            {
                NodeMeta proto = GetNodeTypeProtoFromType(type);
                if (proto == null)
                {
                    continue;
                }
                name2NodeProtoDict.Add(proto.name, proto);
            }
            return(name2NodeProtoDict);
        }
        private NodeMeta SwapWithAdjacent(NodeMeta meta, Side side)
        {
            Side     otherSide    = OtherSide(side);
            NodeMeta adjacentMeta = Adjacent(side, meta);
            Node     adjacent     = adjacentMeta.Node;
            Node     toBeReplaced = meta.Node;


            Node adjacentParent = adjacentMeta.ParentMeta.Node;

            adjacentParent[otherSide] = adjacent[side];

            // Swap the nodes.
            // Cache the children of item to be replaced.
            Node child = adjacent[side];

            // Adjacent takes place of node to be removed.
            meta.ReplaceNode(adjacent);
            meta.ParentMeta.Node[meta.SideFromParent] = adjacent;
            adjacent[side]      = toBeReplaced[side];
            adjacent[otherSide] = toBeReplaced[otherSide];

            // Put node to be replaced in adjacent spot
            adjacentParent[otherSide] = toBeReplaced;
            toBeReplaced[side]        = child;
            toBeReplaced[otherSide]   = null; // This is null, that is how we know it is the leaf
            adjacentMeta.ReplaceNode(toBeReplaced);

            // Swap the color
            Color temp = toBeReplaced.Color;

            toBeReplaced.Color = adjacent.Color;
            adjacent.Color     = temp;

            if (child == null)
            {
                // We have found the node to remove. Replace it with a sentinal
                SentinelNode sentinel = new SentinelNode(toBeReplaced.Color);
                adjacentParent[otherSide] = sentinel;
                return(new NodeMeta(adjacentMeta.ParentMeta, sentinel, adjacentMeta.SideFromParent, adjacentMeta.Sibling));
            }
            else
            {
                return(SwapWithAdjacent(adjacentMeta, side));
            }
        }
Example #25
0
        public Path <T> GetPath(T startNode, T endNode, ITraverser <T> traverser)
        {
            if (startNode == endNode)
            {
                return(new Path <T>(new List <T>()
                {
                    startNode
                }));
            }

            this.startNode = startNode;
            this.endNode   = endNode;
            this.traverser = traverser;

            closedList.Clear();
            openList.Clear();

            var startNodeMeta = GetNodeMeta(startNode);

            startNodeMeta.GCost = 0.0f;

            openList.Add(startNodeMeta);

            while (true)
            {
                if (openList.Count == 0)
                {
                    return(Path <T> .Empty);  // No path exists
                }

                current = openList[openList.Count - 1];
                openList.Remove(current);

                if (current.Node == endNode)
                {
                    return(Backtrack());    // Path discovered
                }

                if (!closedList.Contains(current))
                {
                    closedList.Add(current);
                    AddNeighboursToOpen();
                }
            }
        }
        private void Remove(NodeMeta meta, int value)
        {
            Node node = meta.Node;

            if (node.Value == value)
            {
                Side side  = Side.Left;
                Node child = node[side];
                if (child == null)
                {
                    side  = Side.Right;
                    child = node[side];
                }
                if (child == null)
                {
                    meta.ParentMeta.Node[meta.SideFromParent] = null;
                    // adjust color through parent.
                    if (node.Color == Color.Black && meta.ParentMeta.Node.Color == Color.Black)
                    {
                        meta.ParentMeta.Node.Color = Color.DoubleBlack;
                        BalanceTree(meta.ParentMeta);
                    }
                }
                else
                {
                    NodeMeta childMetaData = SwapWithAdjacent(meta, side);
                    BalanceTree(childMetaData);
                }
            }
            else
            {
                Side side       = value < node.Value ? Side.Left : Side.Right;
                Side otherSidde = OtherSide(side);
                Node next       = node[side];
                if (next == null)
                {
                    throw new InvalidOperationException("Value not found.");
                }
                Remove(new NodeMeta(meta, next, side, node[otherSidde]), value);
            }
        }
Example #27
0
        public void RemoveUnusedArgs(NodeProto nodeProto)
        {
            NodeMeta      proto      = this.GetComponent <BTNodeInfoComponent>().GetNodeMeta(nodeProto.Name);
            List <string> unUsedList = new List <string>();

            foreach (KeyValuePair <string, object> item in nodeProto.Args.Dict())
            {
                if (!proto.new_args_desc.Exists(a => (a.name == item.Key)))
                {
                    unUsedList.Add(item.Key);
                }
            }
            foreach (string item in unUsedList)
            {
                nodeProto.Args.Remove(item);
            }
            for (int i = 0; i < nodeProto.children.Count; i++)
            {
                RemoveUnusedArgs(nodeProto.children[i]);
            }
        }
Example #28
0
        private void AddToOpen(T node)
        {
            NodeMeta target = GetNodeMeta(node);

            if (traverser.CanTraverse(node) || node == endNode)
            {
                float gCost = current.GCost + traverser.GetTraverseCost(current.Node, node);
                if (!closedList.Contains(target) && !openList.Contains(target))
                {
                    target.Parent = current.Node;
                    target.HCost  = Vector3.Distance(node.Position, endNode.Position);
                    target.GCost  = gCost;
                    openList.Add(target);
                }
                else if (target.GCost > gCost)
                {
                    target.Parent = current.Node;
                    target.GCost  = gCost;
                }
            }
        }
Example #29
0
        public List <string> Filter(List <string> list, string text)
        {
            List <string> result1 = new List <string>();
            string        selectType;

            if (mEnumNodeTypeSelection == 0)
            {
                selectType = "All";
                result1    = list;
            }
            else
            {
                selectType = Enum.GetName(typeof(NodeClassifyType), mEnumNodeTypeSelection - 1);
                foreach (string name in list)
                {
                    NodeMeta proto = BTEntity.Instance.GetNodeMeta(name);
                    if (selectType == proto.classify)
                    {
                        result1.Add(name);
                    }
                }
            }

            if (string.IsNullOrEmpty(text))
            {
                return(result1);
            }

            List <string> result2 = new List <string>();

            foreach (string name in result1)
            {
                NodeMeta proto = BTEntity.Instance.GetNodeMeta(name);
                if (name.ToUpper().Contains(text.ToUpper()) || proto.describe.ToUpper().Contains(text.ToUpper()))
                {
                    result2.Add(name);
                }
            }
            return(result2);
        }
Example #30
0
        private void DrawAllValue(NodeMeta proto)
        {
            List <NodeFieldDesc> paramFieldList  = GetFieldDescList(proto.new_args_desc, typeof(NodeFieldAttribute));
            List <NodeFieldDesc> inputFieldList  = GetFieldDescList(proto.new_args_desc, typeof(NodeInputAttribute));
            List <NodeFieldDesc> outputFieldList = GetFieldDescList(proto.new_args_desc, typeof(NodeOutputAttribute));

            mFoldParam = EditorGUILayout.Foldout(mFoldParam, "参数");
            if (mFoldParam)
            {
                DrawProp(proto.name, paramFieldList, NodeParamType.None);
            }
            mFoldInput = EditorGUILayout.Foldout(mFoldInput, "输入");
            if (mFoldInput)
            {
                DrawProp(proto.name, inputFieldList, NodeParamType.Input);
            }
            mFoldOutput = EditorGUILayout.Foldout(mFoldOutput, "输出");
            if (mFoldOutput)
            {
                DrawProp(proto.name, outputFieldList, NodeParamType.Output);
            }
        }