Ejemplo n.º 1
0
        public void FindParent()
        {
            var            tmpNodeA      = default(PathfinderNode);
            float          BestScore     = 0;
            PathfinderNode BestNode      = null;
            float          Score         = 0;
            var            A             = 0;
            var            MakeNew       = default(bool);
            var            B             = 0;
            var            Count         = 0;
            var            C             = 0;
            var            Allow         = default(bool);
            var            tmpConnection = default(PathfinderConnection);
            var            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 + App.Random.Next() * 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 + App.Random.Next() * 0.04F);
                        if (Score < BestScore)
                        {
                            BestScore = Score;
                            BestNode  = tmpNodeA;
                            MakeNew   = false;
                        }
                    }
                }
            }
            if (BestNode != null)
            {
                if (MakeNew)
                {
                    var tmpLayer = default(PathfinderLayer);
                    if (Layer.ParentLayer == null)
                    {
                        tmpLayer = new PathfinderLayer(Layer.Network);
                    }
                    else
                    {
                        tmpLayer = Layer.ParentLayer;
                    }
                    var 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
                var tmpLayer = default(PathfinderLayer);
                if (Layer.ParentLayer == null)
                {
                    tmpLayer = new PathfinderLayer(Layer.Network);
                }
                else
                {
                    tmpLayer = Layer.ParentLayer;
                }
                var NewNode = new PathfinderNode(tmpLayer);
                NewNode.Node_Add(this);
                NewNode.SpanCalc();
                RaiseConnections();
                NewNode.Layer.Network.FindParentNode_Add(NewNode);
            }
        }