Example #1
0
        public Vector3 Update(Vector3 position, List <Vector3> buffer, int numCorners, out bool lastCorner, out bool requiresRepath)
        {
            lastCorner     = false;
            requiresRepath = false;
            Int3 i3Pos = (Int3)position;

            if (nodes[currentNode].Destroyed)
            {
                requiresRepath = true;
                lastCorner     = false;
                buffer.Add(position);
                return(position);
            }

            if (nodes[currentNode].ContainsPoint(i3Pos))
            {
                // Only check for destroyed nodes every 10 frames
                if (tmpCounter >= 10)
                {
                    tmpCounter = 0;
                    for (int i = 0, t = nodes.Count; i < t; i++)
                    {
                        if (nodes[i].Destroyed)
                        {
                            requiresRepath = true;
                            break;
                        }
                    }
                }
                else
                {
                    tmpCounter++;
                }
            }
            else
            {
                bool found = false;
                for (int i = currentNode + 1, t = System.Math.Min(currentNode + 3, nodes.Count); i < t && !found; i++)
                {
                    if (nodes[i].Destroyed)
                    {
                        requiresRepath = true;
                        lastCorner     = false;
                        buffer.Add(position);
                        return(position);
                    }

                    if (nodes[i].ContainsPoint(i3Pos))
                    {
                        currentNode = i;
                        found       = true;
                    }
                }
                for (int i = currentNode - 1, t = System.Math.Max(currentNode - 3, 0); i > t && !found; i--)
                {
                    if (nodes[i].Destroyed)
                    {
                        requiresRepath = true;
                        lastCorner     = false;
                        buffer.Add(position);
                        return(position);
                    }

                    if (nodes[i].ContainsPoint(i3Pos))
                    {
                        currentNode = i;
                        found       = true;
                    }
                }

                int     closest      = 0;
                float   closestDist  = float.PositiveInfinity;
                Vector3 closestPoint = Vector3.zero;

                for (int i = 0, t = nodes.Count; i < t && !found; i++)
                {
                    if (nodes[i].Destroyed)
                    {
                        requiresRepath = true;
                        lastCorner     = false;
                        buffer.Add(position);
                        return(position);
                    }

                    if (nodes[i].ContainsPoint(i3Pos))
                    {
                        currentNode  = i;
                        found        = true;
                        closestPoint = position;
                    }
                    else
                    {
                        Vector3 close = nodes[i].ClosestPointOnNodeXZ(position);
                        float   d     = (close - position).sqrMagnitude;
                        if (d < closestDist)
                        {
                            closestDist  = d;
                            closest      = i;
                            closestPoint = close;
                        }
                    }
                }

                tmpCounter = 0;
                for (int i = 0, t = nodes.Count; i < t; i++)
                {
                    if (nodes[i].Destroyed)
                    {
                        requiresRepath = true;
                        break;
                    }
                }

                if (!found)
                {
                    //Debug.DrawLine (position,closestPoint,Color.cyan);
                    //Debug.DrawRay (closestPoint,Vector3.up,Color.cyan);
                    //Debug.Break();
                    closestPoint.y = position.y;

                    MeshNode nd;
                    MeshNode containingPoint = null;
                    int      containingIndex = nodes.Count - 1;

                    // Need to make a copy here, the JIT will move this variable to the heap
                    // because it is used inside a delegate, if we didn't make a copy here
                    // we would /always/ allocate 24 bytes (sizeof(i3Pos)) on the heap every time
                    // this method was called
                    // now we only do it when this IF statement is executed
                    Int3 i3Copy           = i3Pos;
                    GraphNodeDelegate del = delegate(GraphNode node) {
                        if (!(containingIndex > 0 && node == nodes[containingIndex - 1]) && !(containingIndex < nodes.Count - 1 && node == nodes[containingIndex + 1]))
                        {
                            MeshNode mn = node as MeshNode;
                            if (mn != null && mn.ContainsPoint(i3Copy))
                            {
                                containingPoint = mn;
                            }
                        }
                    };

                    for (; containingIndex >= 0 && containingPoint == null; containingIndex--)
                    {
                        nd = nodes[containingIndex];
                        nd.GetConnections(del);
                    }

                    if (containingPoint != null)
                    {
                        // It will have been decremented once after containingPoint was null, revert that
                        containingIndex++;

                        // We have found a node containing the position, but it is outside the funnel
                        // Recalculate the funnel to include this node
                        exactStart = position;
                        UpdateFunnelCorridor(containingIndex, containingPoint as TriangleMeshNode);
                        currentNode = 0;
                        found       = true;
                    }
                    else
                    {
                        position = closestPoint;

                        found       = true;
                        currentNode = closest;
                    }
                }
            }

            currentPosition = position;

            //Debug.DrawLine ((Vector3)graph.vertices[nodes[currentNode].v1] + Vector3.up*0.1f,(Vector3)graph.vertices[nodes[currentNode].v2] + Vector3.up*0.1f,Color.red);
            //Debug.DrawLine ((Vector3)graph.vertices[nodes[currentNode].v2] + Vector3.up*0.1f,(Vector3)graph.vertices[nodes[currentNode].v3] + Vector3.up*0.1f,Color.red);
            //Debug.DrawLine ((Vector3)graph.vertices[nodes[currentNode].v3] + Vector3.up*0.1f,(Vector3)graph.vertices[nodes[currentNode].v1] + Vector3.up*0.1f,Color.red);


            if (!FindNextCorners(position, currentNode, buffer, numCorners, out lastCorner))
            {
                Debug.LogError("Oh oh");
                buffer.Add(position);
                return(position);
            }

            return(position);
            //Debug.Log("Nearest " + w1.Elapsed.TotalMilliseconds*1000);
            //Debug.Log("Funnel " + w2.Elapsed.TotalMilliseconds*1000);
        }
        // Token: 0x06000041 RID: 65 RVA: 0x00004F38 File Offset: 0x00003338
        public Vector3 Update(Vector3 position, List <Vector3> buffer, int numCorners, out bool lastCorner, out bool requiresRepath)
        {
            lastCorner     = false;
            requiresRepath = false;
            Int3 @int = (Int3)position;

            if (this.nodes[this.currentNode].Destroyed)
            {
                requiresRepath = true;
                lastCorner     = false;
                buffer.Add(position);
                return(position);
            }
            if (this.nodes[this.currentNode].ContainsPoint(@int))
            {
                if (this.tmpCounter >= 10)
                {
                    this.tmpCounter = 0;
                    int i     = 0;
                    int count = this.nodes.Count;
                    while (i < count)
                    {
                        if (this.nodes[i].Destroyed)
                        {
                            requiresRepath = true;
                            break;
                        }
                        i++;
                    }
                }
                else
                {
                    this.tmpCounter++;
                }
            }
            else
            {
                bool flag = false;
                int  num  = this.currentNode + 1;
                int  num2 = Math.Min(this.currentNode + 3, this.nodes.Count);
                while (num < num2 && !flag)
                {
                    if (this.nodes[num].Destroyed)
                    {
                        requiresRepath = true;
                        lastCorner     = false;
                        buffer.Add(position);
                        return(position);
                    }
                    if (this.nodes[num].ContainsPoint(@int))
                    {
                        this.currentNode = num;
                        flag             = true;
                    }
                    num++;
                }
                int num3 = this.currentNode - 1;
                int num4 = Math.Max(this.currentNode - 3, 0);
                while (num3 > num4 && !flag)
                {
                    if (this.nodes[num3].Destroyed)
                    {
                        requiresRepath = true;
                        lastCorner     = false;
                        buffer.Add(position);
                        return(position);
                    }
                    if (this.nodes[num3].ContainsPoint(@int))
                    {
                        this.currentNode = num3;
                        flag             = true;
                    }
                    num3--;
                }
                int     num5   = 0;
                float   num6   = float.PositiveInfinity;
                Vector3 vector = Vector3.zero;
                int     num7   = 0;
                int     count2 = this.nodes.Count;
                while (num7 < count2 && !flag)
                {
                    if (this.nodes[num7].Destroyed)
                    {
                        requiresRepath = true;
                        lastCorner     = false;
                        buffer.Add(position);
                        return(position);
                    }
                    if (this.nodes[num7].ContainsPoint(@int))
                    {
                        this.currentNode = num7;
                        flag             = true;
                        vector           = position;
                    }
                    else
                    {
                        Vector3 vector2      = this.nodes[num7].ClosestPointOnNodeXZ(position);
                        float   sqrMagnitude = (vector2 - position).sqrMagnitude;
                        if (sqrMagnitude < num6)
                        {
                            num6   = sqrMagnitude;
                            num5   = num7;
                            vector = vector2;
                        }
                    }
                    num7++;
                }
                this.tmpCounter = 0;
                int j      = 0;
                int count3 = this.nodes.Count;
                while (j < count3)
                {
                    if (this.nodes[j].Destroyed)
                    {
                        requiresRepath = true;
                        break;
                    }
                    j++;
                }
                if (!flag)
                {
                    vector.y = position.y;
                    MeshNode          containingPoint = null;
                    int               containingIndex = this.nodes.Count - 1;
                    Int3              i3Copy          = @int;
                    GraphNodeDelegate del             = delegate(GraphNode node)
                    {
                        if ((containingIndex <= 0 || node != this.nodes[containingIndex - 1]) && (containingIndex >= this.nodes.Count - 1 || node != this.nodes[containingIndex + 1]))
                        {
                            MeshNode meshNode2 = node as MeshNode;
                            if (meshNode2 != null && meshNode2.ContainsPoint(i3Copy))
                            {
                                containingPoint = meshNode2;
                            }
                        }
                    };
                    while (containingIndex >= 0 && containingPoint == null)
                    {
                        MeshNode meshNode = this.nodes[containingIndex];
                        meshNode.GetConnections(del);
                        containingIndex--;
                    }
                    if (containingPoint != null)
                    {
                        containingIndex++;
                        this.exactStart = position;
                        this.UpdateFunnelCorridor(containingIndex, containingPoint as TriangleMeshNode);
                        this.currentNode = 0;
                    }
                    else
                    {
                        position         = vector;
                        this.currentNode = num5;
                    }
                }
            }
            this.currentPosition = position;
            if (!this.FindNextCorners(position, this.currentNode, buffer, numCorners, out lastCorner))
            {
                Debug.LogError("Oh oh");
                buffer.Add(position);
                return(position);
            }
            return(position);
        }