Пример #1
0
        public void Split()
        {
            if ( NodeCount != 4 )
            {
                Debugger.Break();
            }

            float Value = 0;
            float BestValue = 0;
            PathfinderNode BestNodeA = null;
            PathfinderNode BestNodeB = null;
            PathfinderNode BestNodeC = null;
            PathfinderNode BestNodeD = null;
            int A = 0;
            int B = 0;
            PathfinderNode tmpNodeA = default(PathfinderNode);
            PathfinderNode tmpNodeB = default(PathfinderNode);
            PathfinderNode tmpNodeC = default(PathfinderNode);
            PathfinderNode tmpNodeD = default(PathfinderNode);
            PathfinderConnection tmpConnectionA = null;
            PathfinderConnection tmpConnectionB = null;
            int C = 0;
            int D = 0;

            PathfinderNode[] Children = new PathfinderNode[NodeCount];
            for ( A = 0; A <= NodeCount - 1; A++ )
            {
                Children[A] = Nodes[A];
            }
            int ChildCount = NodeCount;
            PathfinderLayer ThisLayer = Layer;

            Disband();

            BestValue = float.MaxValue;
            for ( A = 0; A <= ChildCount - 1; A++ )
            {
                tmpNodeA = Children[A];
                for ( B = A + 1; B <= ChildCount - 1; B++ )
                {
                    tmpNodeB = Children[B];
                    for ( C = 0; C <= ChildCount - 1; C++ )
                    {
                        if ( Children[C] != tmpNodeA && Children[C] != tmpNodeB )
                        {
                            break;
                        }
                    }
                    tmpNodeC = Children[C];
                    for ( D = C + 1; D <= ChildCount - 1; D++ )
                    {
                        if ( Children[D] != tmpNodeA && Children[D] != tmpNodeB )
                        {
                            break;
                        }
                    }
                    tmpNodeD = Children[D];
                    for ( C = 0; C <= tmpNodeA.ConnectionCount - 1; C++ )
                    {
                        tmpConnectionA = tmpNodeA.Connections[C];
                        if ( tmpConnectionA.GetOtherNode(tmpNodeA) == tmpNodeB )
                        {
                            break;
                        }
                    }
                    for ( D = 0; D <= tmpNodeC.ConnectionCount - 1; D++ )
                    {
                        tmpConnectionB = tmpNodeC.Connections[D];
                        if ( tmpConnectionB.GetOtherNode(tmpNodeC) == tmpNodeD )
                        {
                            break;
                        }
                    }
                    if ( C < tmpNodeA.ConnectionCount & D < tmpNodeC.ConnectionCount )
                    {
                        Value = tmpConnectionA.Value + tmpConnectionB.Value;
                        if ( Value < BestValue )
                        {
                            BestValue = Value;
                            BestNodeA = tmpNodeA;
                            BestNodeB = tmpNodeB;
                            BestNodeC = tmpNodeC;
                            BestNodeD = tmpNodeD;
                        }
                    }
                }
            }

            if ( BestNodeA != null )
            {
                if ( ParentNode != null )
                {
                    tmpNodeA = ParentNode;
                    tmpNodeA.Node_Remove(ParentNode_NodeNum);
                }
                else
                {
                    tmpNodeA = null;
                }
                if ( tmpNodeA != null )
                {
                    tmpNodeA.CheckIntegrity();
                }

                PathfinderNode NewNodeA = new PathfinderNode(ThisLayer);
                PathfinderNode NewNodeB = new PathfinderNode(ThisLayer);

                NewNodeA.Node_Add(BestNodeA);
                NewNodeA.Node_Add(BestNodeB);

                NewNodeA.SpanCalc();
                BestNodeA.RaiseConnections();
                BestNodeB.RaiseConnections();

                NewNodeA.Layer.Network.FindParentNode_Add(NewNodeA);

                NewNodeB.Node_Add(BestNodeC);
                NewNodeB.Node_Add(BestNodeD);

                NewNodeB.SpanCalc();
                BestNodeC.RaiseConnections();
                BestNodeD.RaiseConnections();

                NewNodeB.Layer.Network.FindParentNode_Add(NewNodeB);
            }
            else
            {
                Debugger.Break();
            }
        }
Пример #2
0
        public void FindParent()
        {
            PathfinderNode tmpNodeA = default(PathfinderNode);
            float BestScore = 0;
            PathfinderNode BestNode = null;
            float Score = 0;
            int A = 0;
            bool MakeNew = default(bool);
            int B = 0;
            int Count = 0;
            int C = 0;
            bool Allow = default(bool);
            PathfinderConnection tmpConnection = default(PathfinderConnection);
            PathfinderNode DestNode = default(PathfinderNode);

            if ( NodeCount == 0 & Layer.Network_LayerNum > 0 )
            {
                Debugger.Break();
                return;
            }

            if ( ParentNode != null )
            {
                Debugger.Break();
                return;
            }

            BestScore = float.MaxValue;
            for ( A = 0; A <= ConnectionCount - 1; A++ )
            {
                tmpConnection = Connections[A];
                DestNode = tmpConnection.GetOtherNode(this);
                tmpNodeA = DestNode.ParentNode;
                if ( tmpNodeA == null )
                {
                    tmpNodeA = tmpConnection.GetOtherNode(this);
                    Score = tmpConnection.Value * (0.98F + VBMath.Rnd() * 0.04F);
                    if ( Score < BestScore )
                    {
                        BestScore = Score;
                        BestNode = tmpNodeA;
                        MakeNew = true;
                    }
                }
                else
                {
                    //dont allow this to join to another when the other has 3 nodes and they only have one connection
                    if ( tmpNodeA.NodeCount == 3 )
                    {
                        Count = 0;
                        Allow = false;
                        for ( B = 0; B <= tmpNodeA.NodeCount - 1; B++ )
                        {
                            for ( C = 0; C <= tmpNodeA.Nodes[B].ConnectionCount - 1; C++ )
                            {
                                if ( tmpNodeA.Nodes[B].Connections[C].GetOtherNode(tmpNodeA.Nodes[B]) == this )
                                {
                                    Count++;
                                    if ( Count >= 2 )
                                    {
                                        Allow = true;
                                        goto CountFinished;
                                    }
                                    break;
                                }
                            }
                        }
                        CountFinished:
                        1.GetHashCode(); //TODO: cleanup this loop
                    }
                    else
                    {
                        Allow = true;
                    }
                    if ( Allow )
                    {
                        Score = (DestNode.SiblingSpan + tmpConnection.Value) * (0.98F + VBMath.Rnd() * 0.04F);
                        if ( Score < BestScore )
                        {
                            BestScore = Score;
                            BestNode = tmpNodeA;
                            MakeNew = false;
                        }
                    }
                }
            }
            if ( BestNode != null )
            {
                if ( MakeNew )
                {
                    PathfinderLayer tmpLayer = default(PathfinderLayer);
                    if ( Layer.ParentLayer == null )
                    {
                        tmpLayer = new PathfinderLayer(Layer.Network);
                    }
                    else
                    {
                        tmpLayer = Layer.ParentLayer;
                    }
                    PathfinderNode NewNode = new PathfinderNode(tmpLayer);
                    NewNode.Node_Add(this);
                    NewNode.Node_Add(BestNode);
                    NewNode.SpanCalc();
                    RaiseConnections();
                    BestNode.RaiseConnections();
                    NewNode.Layer.Network.FindParentNode_Add(NewNode);
                }
                else
                {
                    if ( BestNode != null )
                    {
                        BestNode.Node_Add(this);
                        if ( BestNode.NodeCount >= 4 )
                        {
                            BestNode.Split();
                        }
                        else
                        {
                            BestNode.SpanCalc();
                            RaiseConnections();
                            if ( BestNode.ParentNode == null )
                            {
                                BestNode.Layer.Network.FindParentNode_Add(BestNode);
                            }
                        }
                    }
                }
            }
            else if ( ConnectionCount > 0 )
            {
                //it is part of a network but there is no suitable parent to join, so make a new isolated parent
                PathfinderLayer tmpLayer = default(PathfinderLayer);
                if ( Layer.ParentLayer == null )
                {
                    tmpLayer = new PathfinderLayer(Layer.Network);
                }
                else
                {
                    tmpLayer = Layer.ParentLayer;
                }
                PathfinderNode NewNode = new PathfinderNode(tmpLayer);
                NewNode.Node_Add(this);
                NewNode.SpanCalc();
                RaiseConnections();
                NewNode.Layer.Network.FindParentNode_Add(NewNode);
            }
        }