Пример #1
0
        public static NavGraph GetGraph(GraphNode node)
        {
            if (node == null)
            {
                return(null);
            }
            AstarPath active = AstarPath.active;

            if (active == null)
            {
                return(null);
            }
            AstarData data = active.data;

            if (data == null || data.graphs == null)
            {
                return(null);
            }
            uint graphIndex = node.GraphIndex;

            if ((ulong)graphIndex >= (ulong)((long)data.graphs.Length))
            {
                return(null);
            }
            return(data.graphs[(int)graphIndex]);
        }
Пример #2
0
        public Vector3 GetClampedPoint(Vector3 from, Vector3 to, GraphNode hint)
        {
            Vector3    point = to;
            RaycastHit hit;

            if (useRaycasting && Physics.Linecast(from, to, out hit, mask))
            {
                point = hit.point;
            }

            if (useGraphRaycasting && hint != null)
            {
                var rayGraph = AstarData.GetGraph(hint) as IRaycastableGraph;

                if (rayGraph != null)
                {
                    GraphHitInfo graphHit;
                    if (rayGraph.Linecast(from, point, hint, out graphHit))
                    {
                        point = graphHit.point;
                    }
                }
            }

            return(point);
        }
Пример #3
0
        /** Returns the graph which contains the specified node.
         * The graph must be in the #graphs array.
         *
         * \returns Returns the graph which contains the node. Null if the graph wasn't found
         */
        public static NavGraph GetGraph(GraphNode node)
        {
            if (node == null)
            {
                return(null);
            }

            AstarPath script = AstarPath.active;

            if (script == null)
            {
                return(null);
            }

            AstarData data = script.data;

            if (data == null || data.graphs == null)
            {
                return(null);
            }

            uint graphIndex = node.GraphIndex;

            if (graphIndex >= data.graphs.Length)
            {
                return(null);
            }

            return(data.graphs[(int)graphIndex]);
        }
Пример #4
0
 public AstarSerializer(AstarPath script)
 {
     active    = script;
     astarData = script.astarData;
     mask      = -1;
     mask     -= SMask.SaveNodes;
 }
Пример #5
0
        /// <summary>
        /// 判断两点之间是否有障碍,并返回碰撞点信息
        /// </summary>
        /// <param name="startPoint"></param>
        /// <param name="endPoint"></param>
        /// <param name="hit"></param>
        /// <returns></returns>
        public static bool isHit(VInt3 startPoint, VInt3 endPoint, out GraphHitInfo hit)
        {
            GraphNode startNode = AstarPath.active.GetNearest(startPoint).node;
            var       graph     = AstarData.GetGraph(startNode) as NavmeshBase;

            return(graph.Linecast(startPoint, endPoint, startNode, out hit));
        }
Пример #6
0
        public Vector3 GetClampedPoint(Vector3 from, Vector3 to, GraphNode hint)
        {
            Vector3 minPoint = to;

            if (useRaycasting)
            {
                RaycastHit hit;
                if (Physics.Linecast(from, to, out hit, mask))
                {
                    minPoint = hit.point;
                }
            }

            if (useGraphRaycasting && hint != null)
            {
                NavGraph graph = AstarData.GetGraph(hint);

                if (graph != null)
                {
                    var rayGraph = graph as IRaycastableGraph;

                    if (rayGraph != null)
                    {
                        GraphHitInfo hit;

                        if (rayGraph.Linecast(from, minPoint, hint, out hit))
                        {
                            minPoint = hit.point;
                        }
                    }
                }
            }

            return(minPoint);
        }
Пример #7
0
        /** Returns the graph which contains the specified node. The graph must be in the #graphs array.
         * \returns Returns the graph which contains the node. Null if the graph wasn't found */
        public static NavGraph GetGraph(Node node)
        {
            if (node == null)
            {
                return(null);
            }

            AstarPath script = AstarPath.active;

            if (script == null)
            {
                return(null);
            }

            AstarData data = script.astarData;

            if (data == null)
            {
                return(null);
            }

            if (data.graphs == null)
            {
                return(null);
            }

            int graphIndex = node.graphIndex;

            if (graphIndex < 0 || graphIndex >= data.graphs.Length)
            {
                return(null);
            }

            return(data.graphs[graphIndex]);
        }
        public bool ValidateLine(GraphNode n1, GraphNode n2, Vector3 v1, Vector3 v2)
        {
            if (useRaycasting)
            {
                if (thickRaycast && thickRaycastRadius > 0)
                {
                    RaycastHit hit;
                    if (Physics.SphereCast(v1 + raycastOffset, thickRaycastRadius, v2 - v1, out hit, (v2 - v1).magnitude, mask))
                    {
                        //Debug.DrawRay (hit.point,Vector3.up*5,Color.yellow);
                        return(false);
                    }
                }
                else
                {
                    RaycastHit hit;
                    if (Physics.Linecast(v1 + raycastOffset, v2 + raycastOffset, out hit, mask))
                    {
                        //Debug.DrawRay (hit.point,Vector3.up*5,Color.yellow);
                        return(false);
                    }
                }
            }

            if (useGraphRaycasting && n1 == null)
            {
                n1 = AstarPath.active.GetNearest(v1).node;
                n2 = AstarPath.active.GetNearest(v2).node;
            }

            if (useGraphRaycasting && n1 != null && n2 != null)
            {
                NavGraph graph  = AstarData.GetGraph(n1);
                NavGraph graph2 = AstarData.GetGraph(n2);

                if (graph != graph2)
                {
                    return(false);
                }

                if (graph != null)
                {
                    IRaycastableGraph rayGraph = graph as IRaycastableGraph;

                    if (rayGraph != null)
                    {
                        if (rayGraph.Linecast(v1, v2, n1))
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
Пример #9
0
        /** Check if a straight path between v1 and v2 is valid */
        public bool ValidateLine(GraphNode n1, GraphNode n2, Vector3 v1, Vector3 v2)
        {
            if (useRaycasting)
            {
                // Use raycasting to check if a straight path between v1 and v2 is valid
                if (thickRaycast && thickRaycastRadius > 0)
                {
                    RaycastHit hit;
                    if (Physics.SphereCast(v1 + raycastOffset, thickRaycastRadius, v2 - v1, out hit, (v2 - v1).magnitude, mask))
                    {
                        return(false);
                    }
                }
                else
                {
                    RaycastHit hit;
                    if (Physics.Linecast(v1 + raycastOffset, v2 + raycastOffset, out hit, mask))
                    {
                        return(false);
                    }
                }
            }

            if (useGraphRaycasting && n1 == null)
            {
                n1 = AstarPath.active.GetNearest(v1).node;
                n2 = AstarPath.active.GetNearest(v2).node;
            }

            if (useGraphRaycasting && n1 != null && n2 != null)
            {
                // Use graph raycasting to check if a straight path between v1 and v2 is valid
                NavGraph graph  = AstarData.GetGraph(n1);
                NavGraph graph2 = AstarData.GetGraph(n2);

                if (graph != graph2)
                {
                    return(false);
                }

                if (graph != null)
                {
                    var rayGraph = graph as IRaycastableGraph;

                    if (rayGraph != null)
                    {
                        if (rayGraph.Linecast(v1, v2, n1))
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
Пример #10
0
        protected virtual void UpdateMovementPlane()
        {
            if (path.path == null || path.path.Count == 0)
            {
                return;
            }
            var            graph          = AstarData.GetGraph(path.path[0]) as ITransformedGraph;
            IMovementPlane graphTransform = graph != null ? graph.transform : (orientation == OrientationMode.YAxisForward ? new GraphTransform(Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 270, 90), Vector3.one)) : GraphTransform.identityTransform);

            movementPlane = graphTransform.ToSimpleMovementPlane();
        }
Пример #11
0
		/** Called when a requested path has been calculated.
		 * A path is first requested by #UpdatePath, it is then calculated, probably in the same or the next frame.
		 * Finally it is returned to the seeker which forwards it to this function.
		 */
		protected override void OnPathComplete (Path newPath) {
			ABPath p = newPath as ABPath;

			if (p == null) throw new System.Exception("This function only handles ABPaths, do not use special path types");

			waitingForPathCalculation = false;

			// Increase the reference count on the new path.
			// This is used for object pooling to reduce allocations.
			p.Claim(this);

			// Path couldn't be calculated of some reason.
			// More info in p.errorLog (debug string)
			if (p.error) {
				p.Release(this);
				return;
			}

			// Release the previous path.
			if (path != null) path.Release(this);

			// Replace the old path
			path = p;

			// Make sure the path contains at least 2 points
			if (path.vectorPath.Count == 1) path.vectorPath.Add(path.vectorPath[0]);
			interpolator.SetPath(path.vectorPath);

			var graph = AstarData.GetGraph(path.path[0]) as ITransformedGraph;
			movementPlane = graph != null ? graph.transform : (rotationIn2D ? new GraphTransform(Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 270, 90), Vector3.one)) : GraphTransform.identityTransform);

			// Reset some variables
			reachedEndOfPath = false;

			// Simulate movement from the point where the path was requested
			// to where we are right now. This reduces the risk that the agent
			// gets confused because the first point in the path is far away
			// from the current position (possibly behind it which could cause
			// the agent to turn around, and that looks pretty bad).
			interpolator.MoveToLocallyClosestPoint((GetFeetPosition() + p.originalStartPoint) * 0.5f);
			interpolator.MoveToLocallyClosestPoint(GetFeetPosition());

			// Update which point we are moving towards.
			// Note that we need to do this here because otherwise the remainingDistance field might be incorrect for 1 frame.
			// (due to interpolator.remainingDistance being incorrect).
			interpolator.MoveToCircleIntersection2D(position, pickNextWaypointDist, movementPlane);

			var distanceToEnd = remainingDistance;
			if (distanceToEnd <= endReachedDistance) {
				reachedEndOfPath = true;
				OnTargetReached();
			}
		}
Пример #12
0
        public override void Prepare()
        {
            VInt3     num;
            AstarData targetAstarData = this.targetAstarData;

            this.startNode = targetAstarData.GetNearestByRasterizer(this.startPoint, out num);
            if (this.startNode != null)
            {
                this.startPoint = num;
            }
            if (this.hasEndPoint)
            {
                this.endNode = targetAstarData.GetNearestByRasterizer(this.endPoint, out num);
                if (this.endNode != null)
                {
                    this.endPoint = num;
                }
                base.hTarget     = this.endPoint;
                base.hTargetNode = this.endNode;
            }
            if (((this.startNode == null) && this.hasEndPoint) && (this.endNode == null))
            {
                base.Error();
                base.LogError("Couldn't find close nodes to the start point or the end point");
            }
            else if (this.startNode == null)
            {
                base.Error();
                base.LogError("Couldn't find a close node to the start point");
            }
            else if ((this.endNode == null) && this.hasEndPoint)
            {
                base.Error();
                base.LogError("Couldn't find a close node to the end point");
            }
            else if (!this.startNode.Walkable)
            {
                base.Error();
                base.LogError("The node closest to the start point is not walkable");
            }
            else if (this.hasEndPoint && !this.endNode.Walkable)
            {
                base.Error();
                base.LogError("The node closest to the end point is not walkable");
            }
            else if (this.hasEndPoint && (this.startNode.Area != this.endNode.Area))
            {
                base.Error();
                base.LogError(string.Concat(new object[] { "There is no valid path to the target (start area: ", this.startNode.Area, ", target area: ", this.endNode.Area, ")" }));
            }
        }
Пример #13
0
        // Update is called once per frame
        void LateUpdate()
        {
            if (prevNode == null)
            {
                NNInfo nninfo = AstarPath.active.GetNearest(transform.position);
                prevNode = nninfo.node;
                prevPos  = transform.position;
            }

            if (prevNode == null)
            {
                return;
            }

            if (prevNode != null)
            {
                IRaycastableGraph graph = AstarData.GetGraph(prevNode) as IRaycastableGraph;
                if (graph != null)
                {
                    GraphHitInfo hit;
                    if (graph.Linecast(prevPos, transform.position, prevNode, out hit))
                    {
                        hit.point.y = transform.position.y;
                        Vector3 closest = AstarMath.NearestPoint(hit.tangentOrigin, hit.tangentOrigin + hit.tangent, transform.position);
                        Vector3 ohit    = hit.point;
                        ohit = ohit + Vector3.ClampMagnitude((Vector3)hit.node.position - ohit, 0.008f);
                        if (graph.Linecast(ohit, closest, hit.node, out hit))
                        {
                            hit.point.y        = transform.position.y;
                            transform.position = hit.point;
                        }
                        else
                        {
                            closest.y = transform.position.y;

                            transform.position = closest;
                        }
                    }
                    prevNode = hit.node;
                }
            }

            prevPos = transform.position;
        }
Пример #14
0
        public VInt3 GetClampedPoint(VInt3 from, VInt3 to, GraphNode hint)
        {
            VInt3 vInt = to;

            if (this.useGraphRaycasting && hint != null)
            {
                NavGraph graph = AstarData.GetGraph(hint);
                if (graph != null)
                {
                    IRaycastableGraph raycastableGraph = graph as IRaycastableGraph;
                    GraphHitInfo      graphHitInfo;
                    if (raycastableGraph != null && raycastableGraph.Linecast(from, vInt, hint, out graphHitInfo))
                    {
                        vInt = graphHitInfo.point;
                    }
                }
            }
            return(vInt);
        }
Пример #15
0
        public VInt3 GetClampedPoint(VInt3 from, VInt3 to, GraphNode hint)
        {
            VInt3 end = to;

            if (this.useGraphRaycasting && (hint != null))
            {
                NavGraph graph = AstarData.GetGraph(hint);
                if (graph != null)
                {
                    GraphHitInfo      info;
                    IRaycastableGraph graph2 = graph as IRaycastableGraph;
                    if ((graph2 != null) && graph2.Linecast(from, end, hint, out info))
                    {
                        end = info.point;
                    }
                }
            }
            return(end);
        }
Пример #16
0
 public bool ValidateLine(GraphNode n1, GraphNode n2, Vector3 v1, Vector3 v2)
 {
     if (this.useRaycasting)
     {
         RaycastHit raycastHit2;
         if (this.thickRaycast && this.thickRaycastRadius > 0f)
         {
             RaycastHit raycastHit;
             if (Physics.SphereCast(v1 + this.raycastOffset, this.thickRaycastRadius, v2 - v1, out raycastHit, (v2 - v1).magnitude, this.mask))
             {
                 return(false);
             }
         }
         else if (Physics.Linecast(v1 + this.raycastOffset, v2 + this.raycastOffset, out raycastHit2, this.mask))
         {
             return(false);
         }
     }
     if (this.useGraphRaycasting && n1 == null)
     {
         n1 = AstarPath.active.GetNearest(v1).node;
         n2 = AstarPath.active.GetNearest(v2).node;
     }
     if (this.useGraphRaycasting && n1 != null && n2 != null)
     {
         NavGraph graph  = AstarData.GetGraph(n1);
         NavGraph graph2 = AstarData.GetGraph(n2);
         if (graph != graph2)
         {
             return(false);
         }
         if (graph != null)
         {
             IRaycastableGraph raycastableGraph = graph as IRaycastableGraph;
             if (raycastableGraph != null && raycastableGraph.Linecast(v1, v2, n1))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Пример #17
0
        public Vector3 GetClampedPoint(Vector3 from, Vector3 to, GraphNode hint)
        {
            RaycastHit hit;
            Vector3    end = to;

            if (this.useRaycasting && Physics.Linecast(from, to, out hit, (int)this.mask))
            {
                end = hit.point;
            }
            if (this.useGraphRaycasting && (hint != null))
            {
                GraphHitInfo      info;
                IRaycastableGraph graph = AstarData.GetGraph(hint) as IRaycastableGraph;
                if ((graph != null) && graph.Linecast(from, end, hint, out info))
                {
                    end = info.point;
                }
            }
            return(end);
        }
Пример #18
0
        public Vector3 GetClampedPoint(Vector3 from, Vector3 to, GraphNode hint)
        {
            Vector3    vector = to;
            RaycastHit raycastHit;

            if (this.useRaycasting && Physics.Linecast(from, to, out raycastHit, this.mask))
            {
                vector = raycastHit.point;
            }
            if (this.useGraphRaycasting && hint != null)
            {
                IRaycastableGraph raycastableGraph = AstarData.GetGraph(hint) as IRaycastableGraph;
                GraphHitInfo      graphHitInfo;
                if (raycastableGraph != null && raycastableGraph.Linecast(from, vector, hint, out graphHitInfo))
                {
                    vector = graphHitInfo.point;
                }
            }
            return(vector);
        }
Пример #19
0
 public bool ValidateLine(GraphNode n1, GraphNode n2, Vector3 v1, Vector3 v2)
 {
     if (this.useRaycasting)
     {
         if (this.thickRaycast && (this.thickRaycastRadius > 0f))
         {
             Vector3 vector = v2 - v1;
             if (Physics.SphereCast(new Ray(v1 + this.raycastOffset, v2 - v1), this.thickRaycastRadius, vector.magnitude, (int)this.mask))
             {
                 return(false);
             }
         }
         else if (Physics.Linecast(v1 + this.raycastOffset, v2 + this.raycastOffset, (int)this.mask))
         {
             return(false);
         }
     }
     if (this.useGraphRaycasting && (n1 == null))
     {
         n1 = AstarPath.active.GetNearest(v1).node;
         n2 = AstarPath.active.GetNearest(v2).node;
     }
     if ((this.useGraphRaycasting && (n1 != null)) && (n2 != null))
     {
         NavGraph graph  = AstarData.GetGraph(n1);
         NavGraph graph2 = AstarData.GetGraph(n2);
         if (graph != graph2)
         {
             return(false);
         }
         if (graph != null)
         {
             IRaycastableGraph graph3 = graph as IRaycastableGraph;
             if (graph3 != null)
             {
                 return(!graph3.Linecast(v1, v2, n1));
             }
         }
     }
     return(true);
 }
Пример #20
0
		/** Returns the graph which contains the specified node.
		 * The graph must be in the #graphs array.
		 *
		 * \returns Returns the graph which contains the node. Null if the graph wasn't found
		 */
		public static NavGraph GetGraph (GraphNode node) {
			if (node == null) return null;

			AstarPath script = AstarPath.active;

			if (script == null) return null;

			AstarData data = script.astarData;

			if (data == null) return null;

			if (data.graphs == null) return null;

			uint graphIndex = node.GraphIndex;

			if (graphIndex >= data.graphs.Length) {
				return null;
			}

			return data.graphs[(int)graphIndex];
		}
Пример #21
0
 // Token: 0x06002440 RID: 9280 RVA: 0x00196CB8 File Offset: 0x00194EB8
 private void LateUpdate()
 {
     if (this.prevNode == null)
     {
         NNInfo nearest = AstarPath.active.GetNearest(base.transform.position);
         this.prevNode = nearest.node;
         this.prevPos  = base.transform.position;
     }
     if (this.prevNode == null)
     {
         return;
     }
     if (this.prevNode != null)
     {
         IRaycastableGraph raycastableGraph = AstarData.GetGraph(this.prevNode) as IRaycastableGraph;
         if (raycastableGraph != null)
         {
             GraphHitInfo graphHitInfo;
             if (raycastableGraph.Linecast(this.prevPos, base.transform.position, this.prevNode, out graphHitInfo))
             {
                 graphHitInfo.point.y = base.transform.position.y;
                 Vector3 vector  = VectorMath.ClosestPointOnLine(graphHitInfo.tangentOrigin, graphHitInfo.tangentOrigin + graphHitInfo.tangent, base.transform.position);
                 Vector3 vector2 = graphHitInfo.point;
                 vector2 += Vector3.ClampMagnitude((Vector3)graphHitInfo.node.position - vector2, 0.008f);
                 if (raycastableGraph.Linecast(vector2, vector, graphHitInfo.node, out graphHitInfo))
                 {
                     graphHitInfo.point.y    = base.transform.position.y;
                     base.transform.position = graphHitInfo.point;
                 }
                 else
                 {
                     vector.y = base.transform.position.y;
                     base.transform.position = vector;
                 }
             }
             this.prevNode = graphHitInfo.node;
         }
     }
     this.prevPos = base.transform.position;
 }
Пример #22
0
 private void LateUpdate()
 {
     if (this.prevNode == null)
     {
         NNInfo nearest = AstarPath.active.GetNearest(base.transform.position);
         this.prevNode = nearest.node;
         this.prevPos  = base.transform.position;
     }
     if (this.prevNode != null)
     {
         if (this.prevNode != null)
         {
             IRaycastableGraph graph = AstarData.GetGraph(this.prevNode) as IRaycastableGraph;
             if (graph != null)
             {
                 GraphHitInfo info2;
                 if (graph.Linecast(this.prevPos, base.transform.position, this.prevNode, out info2))
                 {
                     info2.point.y = base.transform.position.y;
                     Vector3 end   = VectorMath.ClosestPointOnLine(info2.tangentOrigin, info2.tangentOrigin + info2.tangent, base.transform.position);
                     Vector3 point = info2.point;
                     point += Vector3.ClampMagnitude(((Vector3)info2.node.position) - point, 0.008f);
                     if (graph.Linecast(point, end, info2.node, out info2))
                     {
                         info2.point.y           = base.transform.position.y;
                         base.transform.position = info2.point;
                     }
                     else
                     {
                         end.y = base.transform.position.y;
                         base.transform.position = end;
                     }
                 }
                 this.prevNode = info2.node;
             }
         }
         this.prevPos = base.transform.position;
     }
 }
        public Vector3 GetClampedPoint(Vector3 from, Vector3 to, GraphNode hint)
        {
            //float minDistance = Mathf.Infinity;
            Vector3 minPoint = to;

            if (useRaycasting)
            {
                RaycastHit hit;
                if (Physics.Linecast(from, to, out hit, mask))
                {
                    minPoint = hit.point;
                    //minDistance = hit.distance;
                }
            }

            if (useGraphRaycasting && hint != null)
            {
                NavGraph graph = AstarData.GetGraph(hint);

                if (graph != null)
                {
                    IRaycastableGraph rayGraph = graph as IRaycastableGraph;

                    if (rayGraph != null)
                    {
                        GraphHitInfo hit;

                        if (rayGraph.Linecast(from, minPoint, hint, out hit))
                        {
                            //if ((hit.point-from).magnitude < minDistance) {
                            minPoint = hit.point;
                            //}
                        }
                    }
                }
            }

            return(minPoint);
        }
Пример #24
0
        // Token: 0x06002139 RID: 8505 RVA: 0x0018D998 File Offset: 0x0018BB98
        protected override void OnPathComplete(Path newPath)
        {
            ABPath abpath = newPath as ABPath;

            if (abpath == null)
            {
                throw new Exception("This function only handles ABPaths, do not use special path types");
            }
            this.waitingForPathCalculation = false;
            abpath.Claim(this);
            if (abpath.error)
            {
                abpath.Release(this, false);
                return;
            }
            if (this.path != null)
            {
                this.path.Release(this, false);
            }
            this.path = abpath;
            if (this.path.vectorPath.Count == 1)
            {
                this.path.vectorPath.Add(this.path.vectorPath[0]);
            }
            this.interpolator.SetPath(this.path.vectorPath);
            ITransformedGraph transformedGraph = AstarData.GetGraph(this.path.path[0]) as ITransformedGraph;

            this.movementPlane    = ((transformedGraph != null) ? transformedGraph.transform : (this.rotationIn2D ? new GraphTransform(Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90f, 270f, 90f), Vector3.one)) : GraphTransform.identityTransform));
            this.reachedEndOfPath = false;
            this.interpolator.MoveToLocallyClosestPoint((this.GetFeetPosition() + abpath.originalStartPoint) * 0.5f, true, true);
            this.interpolator.MoveToLocallyClosestPoint(this.GetFeetPosition(), true, true);
            this.interpolator.MoveToCircleIntersection2D(base.position, this.pickNextWaypointDist, this.movementPlane);
            if (this.remainingDistance <= this.endReachedDistance)
            {
                this.reachedEndOfPath = true;
                this.OnTargetReached();
            }
        }
Пример #25
0
        public void InitializeSerializeNodes()
        {
            if (mask == SMask.SaveNodes)
            {
                AstarData data = active.astarData;

                for (int g = 0; g < data.graphs.Length; g++)
                {
                    NavGraph graph = data.graphs[g];

                    if (graph.nodes == null)
                    {
                        continue;
                    }

                    for (int i = 0; i < graph.nodes.Length; i++)
                    {
                        graph.nodes[i].h = g;
                        graph.nodes[i].g = i;
                    }
                }
            }
        }
Пример #26
0
        /** Called when a requested path has been calculated.
         * A path is first requested by #UpdatePath, it is then calculated, probably in the same or the next frame.
         * Finally it is returned to the seeker which forwards it to this function.
         */
        public override void OnPathComplete(Path newPath)
        {
            ABPath p = newPath as ABPath;

            if (p == null)
            {
                throw new System.Exception("This function only handles ABPaths, do not use special path types");
            }

            waitingForPathCalculation = false;

            // Increase the reference count on the new path.
            // This is used for object pooling to reduce allocations.
            p.Claim(this);

            // Path couldn't be calculated of some reason.
            // More info in p.errorLog (debug string)
            if (p.error)
            {
                p.Release(this);
                return;
            }

            // Release the previous path.
            if (path != null)
            {
                path.Release(this);
            }

            // Replace the old path
            path = p;

            // Make sure the path contains at least 2 points
            if (path.vectorPath.Count == 1)
            {
                path.vectorPath.Add(path.vectorPath[0]);
            }
            interpolator.SetPath(path.vectorPath);

            var graph = AstarData.GetGraph(path.path[0]) as ITransformedGraph;

            movementPlane = graph != null ? graph.transform : GraphTransform.identityTransform;

            // Reset some variables
            targetReached = false;

            // Simulate movement from the point where the path was requested
            // to where we are right now. This reduces the risk that the agent
            // gets confused because the first point in the path is far away
            // from the current position (possibly behind it which could cause
            // the agent to turn around, and that looks pretty bad).
            interpolator.MoveToLocallyClosestPoint((GetFeetPosition() + p.originalStartPoint) * 0.5f);
            interpolator.MoveToLocallyClosestPoint(GetFeetPosition());

            var distanceToEnd = movementPlane.ToPlane(steeringTarget - position).magnitude + interpolator.remainingDistance;

            if (distanceToEnd <= endReachedDistance)
            {
                targetReached = true;
                OnTargetReached();
            }
        }
Пример #27
0
 public AstarSerializer(AstarData data, SerializeSettings settings)
 {
     this.data = data;
     this.settings = settings;
 }
Пример #28
0
 public AstarSerializer(AstarData data)
 {
     this.data = data;
     settings = SerializeSettings.Settings;
 }
Пример #29
0
 public static FsmAstarData SetAstarData(AstarData gameObject)
 {
     return new FsmAstarData()
     {
         Value = gameObject
     };
 }
Пример #30
0
 public AstarSerializer(AstarPath script)
 {
     active = script;
     astarData = script.astarData;
     mask = -1;
     mask -= SMask.SaveNodes;
 }
Пример #31
0
        /** Use this for initialization.
         *
         * \param s Optionally provide in order to take tag penalties into account. May be null if you do not use a Seeker\
         * \param p Path to follow
         * \param mergePartEndpoints If true, then adjacent parts that the path is split up in will
         * try to use the same start/end points. For example when using a link on a navmesh graph
         * Instead of first following the path to the center of the node where the link is and then
         * follow the link, the path will be adjusted to go to the exact point where the link starts
         * which usually makes more sense.
         * \param simplificationMode The path can optionally be simplified. This can be a bit expensive for long paths.
         */
        public void Initialize(Seeker s, Path p, bool mergePartEndpoints, RichFunnel.FunnelSimplification simplificationMode)
        {
            if (p.error)
            {
                throw new System.ArgumentException("Path has an error");
            }

            List <GraphNode> nodes = p.path;

            if (nodes.Count == 0)
            {
                throw new System.ArgumentException("Path traverses no nodes");
            }

            seeker = s;
            // Release objects back to object pool
            // Yeah, I know, it's casting... but this won't be called much
            for (int i = 0; i < parts.Count; i++)
            {
                if (parts[i] is RichFunnel)
                {
                    ObjectPool <RichFunnel> .Release(parts[i] as RichFunnel);
                }
                else if (parts[i] is RichSpecial)
                {
                    ObjectPool <RichSpecial> .Release(parts[i] as RichSpecial);
                }
            }

            parts.Clear();
            currentPart = 0;

            // Initialize new

            //Break path into parts
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i] is TriangleMeshNode)
                {
                    var        graph = AstarData.GetGraph(nodes[i]);
                    RichFunnel f     = ObjectPool <RichFunnel> .Claim().Initialize(this, graph);

                    f.funnelSimplificationMode = simplificationMode;

                    int  sIndex            = i;
                    uint currentGraphIndex = nodes[sIndex].GraphIndex;


                    for (; i < nodes.Count; i++)
                    {
                        if (nodes[i].GraphIndex != currentGraphIndex && !(nodes[i] is NodeLink3Node))
                        {
                            break;
                        }
                    }
                    i--;

                    if (sIndex == 0)
                    {
                        f.exactStart = p.vectorPath[0];
                    }
                    else
                    {
                        f.exactStart = (Vector3)nodes[mergePartEndpoints ? sIndex - 1 : sIndex].position;
                    }

                    if (i == nodes.Count - 1)
                    {
                        f.exactEnd = p.vectorPath[p.vectorPath.Count - 1];
                    }
                    else
                    {
                        f.exactEnd = (Vector3)nodes[mergePartEndpoints ? i + 1 : i].position;
                    }

                    f.BuildFunnelCorridor(nodes, sIndex, i);

                    parts.Add(f);
                }
                else if (NodeLink2.GetNodeLink(nodes[i]) != null)
                {
                    NodeLink2 nl = NodeLink2.GetNodeLink(nodes[i]);

                    int  sIndex            = i;
                    uint currentGraphIndex = nodes[sIndex].GraphIndex;

                    for (i++; i < nodes.Count; i++)
                    {
                        if (nodes[i].GraphIndex != currentGraphIndex)
                        {
                            break;
                        }
                    }
                    i--;

                    if (i - sIndex > 1)
                    {
                        throw new System.Exception("NodeLink2 path length greater than two (2) nodes. " + (i - sIndex));
                    }
                    else if (i - sIndex == 0)
                    {
                        //Just continue, it might be the case that a NodeLink was the closest node
                        continue;
                    }

                    RichSpecial rps = ObjectPool <RichSpecial> .Claim().Initialize(nl, nodes[sIndex]);

                    parts.Add(rps);
                }
            }
        }
Пример #32
0
        // Token: 0x06002140 RID: 8512 RVA: 0x001890A0 File Offset: 0x001872A0
        public void Initialize(Seeker seeker, Path path, bool mergePartEndpoints, bool simplificationMode)
        {
            if (path.error)
            {
                throw new ArgumentException("Path has an error");
            }
            List <GraphNode> path2 = path.path;

            if (path2.Count == 0)
            {
                throw new ArgumentException("Path traverses no nodes");
            }
            this.seeker = seeker;
            for (int i = 0; i < this.parts.Count; i++)
            {
                RichFunnel  richFunnel  = this.parts[i] as RichFunnel;
                RichSpecial richSpecial = this.parts[i] as RichSpecial;
                if (richFunnel != null)
                {
                    ObjectPool <RichFunnel> .Release(ref richFunnel);
                }
                else if (richSpecial != null)
                {
                    ObjectPool <RichSpecial> .Release(ref richSpecial);
                }
            }
            this.Clear();
            this.Endpoint = path.vectorPath[path.vectorPath.Count - 1];
            for (int j = 0; j < path2.Count; j++)
            {
                if (path2[j] is TriangleMeshNode)
                {
                    NavmeshBase navmeshBase = AstarData.GetGraph(path2[j]) as NavmeshBase;
                    if (navmeshBase == null)
                    {
                        throw new Exception("Found a TriangleMeshNode that was not in a NavmeshBase graph");
                    }
                    RichFunnel richFunnel2 = ObjectPool <RichFunnel> .Claim().Initialize(this, navmeshBase);

                    richFunnel2.funnelSimplification = simplificationMode;
                    int  num        = j;
                    uint graphIndex = path2[num].GraphIndex;
                    while (j < path2.Count && (path2[j].GraphIndex == graphIndex || path2[j] is NodeLink3Node))
                    {
                        j++;
                    }
                    j--;
                    if (num == 0)
                    {
                        richFunnel2.exactStart = path.vectorPath[0];
                    }
                    else
                    {
                        richFunnel2.exactStart = (Vector3)path2[mergePartEndpoints ? (num - 1) : num].position;
                    }
                    if (j == path2.Count - 1)
                    {
                        richFunnel2.exactEnd = path.vectorPath[path.vectorPath.Count - 1];
                    }
                    else
                    {
                        richFunnel2.exactEnd = (Vector3)path2[mergePartEndpoints ? (j + 1) : j].position;
                    }
                    richFunnel2.BuildFunnelCorridor(path2, num, j);
                    this.parts.Add(richFunnel2);
                }
                else if (NodeLink2.GetNodeLink(path2[j]) != null)
                {
                    NodeLink2 nodeLink    = NodeLink2.GetNodeLink(path2[j]);
                    int       num2        = j;
                    uint      graphIndex2 = path2[num2].GraphIndex;
                    j++;
                    while (j < path2.Count && path2[j].GraphIndex == graphIndex2)
                    {
                        j++;
                    }
                    j--;
                    if (j - num2 > 1)
                    {
                        throw new Exception("NodeLink2 path length greater than two (2) nodes. " + (j - num2));
                    }
                    if (j - num2 != 0)
                    {
                        RichSpecial item = ObjectPool <RichSpecial> .Claim().Initialize(nodeLink, path2[num2]);

                        this.parts.Add(item);
                    }
                }
            }
        }
Пример #33
0
        /// <summary>Use this for initialization.</summary>
        /// <param name="seeker">Optionally provide in order to take tag penalties into account. May be null if you do not use a Seeker\</param>
        /// <param name="path">Path to follow</param>
        /// <param name="mergePartEndpoints">If true, then adjacent parts that the path is split up in will
        /// try to use the same start/end points. For example when using a link on a navmesh graph
        /// Instead of first following the path to the center of the node where the link is and then
        /// follow the link, the path will be adjusted to go to the exact point where the link starts
        /// which usually makes more sense.</param>
        /// <param name="simplificationMode">The path can optionally be simplified. This can be a bit expensive for long paths.</param>
        public void Initialize(Seeker seeker, Path path, bool mergePartEndpoints, bool simplificationMode)
        {
            if (path.error)
            {
                throw new System.ArgumentException("Path has an error");
            }

            List <GraphNode> nodes = path.path;

            if (nodes.Count == 0)
            {
                throw new System.ArgumentException("Path traverses no nodes");
            }

            this.seeker = seeker;
            // Release objects back to object pool
            // Yeah, I know, it's casting... but this won't be called much
            for (int i = 0; i < parts.Count; i++)
            {
                var funnelPart  = parts[i] as RichFunnel;
                var specialPart = parts[i] as RichSpecial;
                if (funnelPart != null)
                {
                    ObjectPool <RichFunnel> .Release(ref funnelPart);
                }
                else if (specialPart != null)
                {
                    ObjectPool <RichSpecial> .Release(ref specialPart);
                }
            }

            Clear();

            // Initialize new
            Endpoint = path.vectorPath[path.vectorPath.Count - 1];

            //Break path into parts
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i] is TriangleMeshNode)
                {
                    var graph = AstarData.GetGraph(nodes[i]) as NavmeshBase;
                    if (graph == null)
                    {
                        throw new System.Exception("Found a TriangleMeshNode that was not in a NavmeshBase graph");
                    }

                    RichFunnel f = ObjectPool <RichFunnel> .Claim().Initialize(this, graph);

                    f.funnelSimplification = simplificationMode;

                    int  sIndex            = i;
                    uint currentGraphIndex = nodes[sIndex].GraphIndex;


                    for (; i < nodes.Count; i++)
                    {
                        if (nodes[i].GraphIndex != currentGraphIndex && !(nodes[i] is NodeLink3Node))
                        {
                            break;
                        }
                    }
                    i--;

                    if (sIndex == 0)
                    {
                        f.exactStart = path.vectorPath[0];
                    }
                    else
                    {
                        f.exactStart = (Vector3)nodes[mergePartEndpoints ? sIndex - 1 : sIndex].position;
                    }

                    if (i == nodes.Count - 1)
                    {
                        f.exactEnd = path.vectorPath[path.vectorPath.Count - 1];
                    }
                    else
                    {
                        f.exactEnd = (Vector3)nodes[mergePartEndpoints ? i + 1 : i].position;
                    }

                    f.BuildFunnelCorridor(nodes, sIndex, i);

                    parts.Add(f);
                }
                else if (NodeLink2.GetNodeLink(nodes[i]) != null)
                {
                    NodeLink2 nl = NodeLink2.GetNodeLink(nodes[i]);

                    int  sIndex            = i;
                    uint currentGraphIndex = nodes[sIndex].GraphIndex;

                    for (i++; i < nodes.Count; i++)
                    {
                        if (nodes[i].GraphIndex != currentGraphIndex)
                        {
                            break;
                        }
                    }
                    i--;

                    if (i - sIndex > 1)
                    {
                        throw new System.Exception("NodeLink2 path length greater than two (2) nodes. " + (i - sIndex));
                    }
                    else if (i - sIndex == 0)
                    {
                        //Just continue, it might be the case that a NodeLink was the closest node
                        continue;
                    }

                    RichSpecial rps = ObjectPool <RichSpecial> .Claim().Initialize(nl, nodes[sIndex]);

                    parts.Add(rps);
                }
                else if (!(nodes[i] is PointNode))
                {
                    // Some other graph type which we do not have support for
                    throw new System.InvalidOperationException("The RichAI movment script can only be used on recast/navmesh graphs. A node of type " + nodes[i].GetType().Name + " was in the path.");
                }
            }
        }
Пример #34
0
        // Token: 0x06000033 RID: 51 RVA: 0x0000417C File Offset: 0x0000257C
        public void Initialize(Seeker s, Path p, bool mergePartEndpoints, RichFunnel.FunnelSimplification simplificationMode)
        {
            if (p.error)
            {
                throw new ArgumentException("Path has an error");
            }
            List <GraphNode> path = p.path;

            if (path.Count == 0)
            {
                throw new ArgumentException("Path traverses no nodes");
            }
            this.seeker = s;
            for (int i = 0; i < this.parts.Count; i++)
            {
                if (this.parts[i] is RichFunnel)
                {
                    ObjectPool <RichFunnel> .Release(this.parts[i] as RichFunnel);
                }
                else if (this.parts[i] is RichSpecial)
                {
                    ObjectPool <RichSpecial> .Release(this.parts[i] as RichSpecial);
                }
            }
            this.parts.Clear();
            this.currentPart = 0;
            for (int j = 0; j < path.Count; j++)
            {
                if (path[j] is TriangleMeshNode)
                {
                    IFunnelGraph graph      = AstarData.GetGraph(path[j]) as IFunnelGraph;
                    RichFunnel   richFunnel = ObjectPool <RichFunnel> .Claim().Initialize(this, graph);

                    richFunnel.funnelSimplificationMode = simplificationMode;
                    int  num        = j;
                    uint graphIndex = path[num].GraphIndex;
                    while (j < path.Count)
                    {
                        if (path[j].GraphIndex != graphIndex && !(path[j] is NodeLink3Node))
                        {
                            break;
                        }
                        j++;
                    }
                    j--;
                    if (num == 0)
                    {
                        richFunnel.exactStart = p.vectorPath[0];
                    }
                    else if (mergePartEndpoints)
                    {
                        richFunnel.exactStart = (Vector3)path[num - 1].position;
                    }
                    else
                    {
                        richFunnel.exactStart = (Vector3)path[num].position;
                    }
                    if (j == path.Count - 1)
                    {
                        richFunnel.exactEnd = p.vectorPath[p.vectorPath.Count - 1];
                    }
                    else if (mergePartEndpoints)
                    {
                        richFunnel.exactEnd = (Vector3)path[j + 1].position;
                    }
                    else
                    {
                        richFunnel.exactEnd = (Vector3)path[j].position;
                    }
                    richFunnel.BuildFunnelCorridor(path, num, j);
                    this.parts.Add(richFunnel);
                }
                else if (path[j] != null && NodeLink2.GetNodeLink(path[j]) != null)
                {
                    NodeLink2 nodeLink    = NodeLink2.GetNodeLink(path[j]);
                    int       num2        = j;
                    uint      graphIndex2 = path[num2].GraphIndex;
                    for (j++; j < path.Count; j++)
                    {
                        if (path[j].GraphIndex != graphIndex2)
                        {
                            break;
                        }
                    }
                    j--;
                    if (j - num2 > 1)
                    {
                        throw new Exception("NodeLink2 path length greater than two (2) nodes. " + (j - num2));
                    }
                    if (j - num2 != 0)
                    {
                        RichSpecial item = ObjectPool <RichSpecial> .Claim().Initialize(nodeLink, path[num2]);

                        this.parts.Add(item);
                    }
                }
            }
        }