private static byte[] SerializeNodeIndices(NavGraph[] graphs)
        {
            MemoryStream memoryStream            = new MemoryStream();
            BinaryWriter writer                  = new BinaryWriter(memoryStream);
            int          maxNodeIndexInAllGraphs = AstarSerializer.GetMaxNodeIndexInAllGraphs(graphs);

            writer.Write(maxNodeIndexInAllGraphs);
            int maxNodeIndex2 = 0;

            Action <GraphNode> < > 9__0;
            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] != null)
                {
                    NavGraph           navGraph = graphs[i];
                    Action <GraphNode> action;
                    if ((action = < > 9__0) == null)
                    {
                        action = (< > 9__0 = delegate(GraphNode node)
                        {
                            maxNodeIndex2 = Math.Max(node.NodeIndex, maxNodeIndex2);
                            writer.Write(node.NodeIndex);
                        });
                    }
                    navGraph.GetNodes(action);
                }
            }
            if (maxNodeIndex2 != maxNodeIndexInAllGraphs)
            {
                throw new Exception("Some graphs are not consistent in their GetNodes calls, sequential calls give different results.");
            }
            byte[] result = memoryStream.ToArray();
            writer.Close();
            return(result);
        }
Beispiel #2
0
    private IEnumerable <Progress> ScanGraph(NavGraph graph)
    {
        if (AstarPath.OnGraphPreScan != null)
        {
            yield return(new Progress(0f, "Pre processing"));

            AstarPath.OnGraphPreScan(graph);
        }
        yield return(new Progress(0f, ""));

        foreach (Progress progress in graph.ScanInternal())
        {
            yield return(new Progress(Mathf.Lerp(0f, 0.95f, progress.progress), progress.description));
        }
        IEnumerator <Progress> enumerator = null;

        yield return(new Progress(0.95f, "Assigning graph indices"));

        graph.GetNodes(delegate(GraphNode node)
        {
            node.GraphIndex = graph.graphIndex;
        });
        if (AstarPath.OnGraphPostScan != null)
        {
            yield return(new Progress(0.99f, "Post processing"));

            AstarPath.OnGraphPostScan(graph);
        }
        yield break;
        yield break;
    }
        private static int GetMaxNodeIndexInAllGraphs(NavGraph[] graphs)
        {
            int maxIndex = 0;

            Action <GraphNode> < > 9__0;
            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] != null)
                {
                    NavGraph           navGraph = graphs[i];
                    Action <GraphNode> action;
                    if ((action = < > 9__0) == null)
                    {
                        action = (< > 9__0 = delegate(GraphNode node)
                        {
                            maxIndex = Math.Max(node.NodeIndex, maxIndex);
                            if (node.NodeIndex == -1)
                            {
                                Debug.LogError("Graph contains destroyed nodes. This is a bug.");
                            }
                        });
                    }
                    navGraph.GetNodes(action);
                }
            }
            return(maxIndex);
        }
Beispiel #4
0
    IEnumerable <Progress> ScanGraph(NavGraph graph)
    {
        if (OnGraphPreScan != null)
        {
            yield return(new Progress(0, "Pre processing"));

            OnGraphPreScan(graph);
        }

        yield return(new Progress(0, ""));

        foreach (var p in ((IGraphInternals)graph).ScanInternal())
        {
            yield return(p.MapTo(0, 0.95f));
        }

        yield return(new Progress(0.95f, "Assigning graph indices"));

        // Assign the graph index to every node in the graph
        graph.GetNodes(node => node.GraphIndex = (uint)graph.graphIndex);

        if (OnGraphPostScan != null)
        {
            yield return(new Progress(0.99f, "Post processing"));

            OnGraphPostScan(graph);
        }
    }
        /** Used to serialize references to other nodes e.g connections.
         * Nodes use the GraphSerializationContext.GetNodeIdentifier and
         * GraphSerializationContext.GetNodeFromIdentifier methods
         * for serialization and deserialization respectively.
         */
        static byte[] SerializeGraphNodeReferences(NavGraph graph)
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);
            var ctx    = new GraphSerializationContext(writer);

            graph.GetNodes(node => node.SerializeReferences(ctx));
            writer.Close();

            var bytes = stream.ToArray();

            return(bytes);
        }
        private static byte[] SerializeGraphNodeReferences(NavGraph graph)
        {
            MemoryStream memoryStream     = new MemoryStream();
            BinaryWriter binaryWriter     = new BinaryWriter(memoryStream);
            GraphSerializationContext ctx = new GraphSerializationContext(binaryWriter);

            graph.GetNodes(delegate(GraphNode node)
            {
                node.SerializeReferences(ctx);
            });
            binaryWriter.Close();
            return(memoryStream.ToArray());
        }
        void DeserializeNodeReferences(NavGraph graph, GraphNode[] int2Node)
        {
            var zipIndex = graphIndexInZip[graph];
            var entry    = GetEntry("graph" + zipIndex + "_references" + binaryExt);

            if (entry == null)
            {
                throw new Exception("Node references for graph " + zipIndex + " not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
            }

            var reader = GetBinaryReader(entry);
            var ctx    = new GraphSerializationContext(reader, int2Node, graph.graphIndex, meta);

            graph.GetNodes(node => node.DeserializeReferences(ctx));
        }
Beispiel #8
0
    /// <summary>
    /// Returns the node closest to the ray (slow).
    /// Warning: This function is brute-force and very slow, use with caution
    /// </summary>
    public GraphNode GetNearest(Ray ray)
    {
        if (graphs == null)
        {
            return(null);
        }

        float     minDist     = Mathf.Infinity;
        GraphNode nearestNode = null;

        Vector3 lineDirection = ray.direction;
        Vector3 lineOrigin    = ray.origin;

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

            graph.GetNodes(node => {
                Vector3 pos = (Vector3)node.position;
                Vector3 p   = lineOrigin + (Vector3.Dot(pos - lineOrigin, lineDirection) * lineDirection);

                float tmp = Mathf.Abs(p.x - pos.x);
                tmp      *= tmp;
                if (tmp > minDist)
                {
                    return;
                }

                tmp  = Mathf.Abs(p.z - pos.z);
                tmp *= tmp;
                if (tmp > minDist)
                {
                    return;
                }

                float dist = (p - pos).sqrMagnitude;

                if (dist < minDist)
                {
                    minDist     = dist;
                    nearestNode = node;
                }
                return;
            });
        }

        return(nearestNode);
    }
        private GraphNode[] DeserializeNodeReferenceMap()
        {
            ZipEntry entry = this.GetEntry("graph_references.binary");

            if (entry == null)
            {
                throw new Exception("Node references not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
            }
            BinaryReader reader = AstarSerializer.GetBinaryReader(entry);
            int          num    = reader.ReadInt32();

            GraphNode[] int2Node = new GraphNode[num + 1];
            try
            {
                Action <GraphNode> < > 9__0;
                for (int i = 0; i < this.graphs.Length; i++)
                {
                    NavGraph           navGraph = this.graphs[i];
                    Action <GraphNode> action;
                    if ((action = < > 9__0) == null)
                    {
                        action = (< > 9__0 = delegate(GraphNode node)
                        {
                            int num2 = reader.ReadInt32();
                            int2Node[num2] = node;
                        });
                    }
                    navGraph.GetNodes(action);
                }
            }
            catch (Exception innerException)
            {
                throw new Exception("Some graph(s) has thrown an exception during GetNodes, or some graph(s) have deserialized more or fewer nodes than were serialized", innerException);
            }
            if (reader.BaseStream.Position != reader.BaseStream.Length)
            {
                throw new Exception(string.Concat(new object[]
                {
                    reader.BaseStream.Length / 4L,
                    " nodes were serialized, but only data for ",
                    reader.BaseStream.Position / 4L,
                    " nodes was found. The data looks corrupt."
                }));
            }
            reader.Close();
            return(int2Node);
        }
        private void DeserializeNodeReferences(NavGraph graph, GraphNode[] int2Node)
        {
            int      num   = this.graphIndexInZip[graph];
            ZipEntry entry = this.GetEntry("graph" + num + "_references.binary");

            if (entry == null)
            {
                throw new Exception("Node references for graph " + num + " not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
            }
            BinaryReader binaryReader     = AstarSerializer.GetBinaryReader(entry);
            GraphSerializationContext ctx = new GraphSerializationContext(binaryReader, int2Node, graph.graphIndex, this.meta);

            graph.GetNodes(delegate(GraphNode node)
            {
                node.DeserializeReferences(ctx);
            });
        }
Beispiel #11
0
    public GraphNode GetNearest(Ray ray)
    {
        if (this.graphs == null)
        {
            return(null);
        }
        float     minDist       = float.PositiveInfinity;
        GraphNode nearestNode   = null;
        Vector3   lineDirection = ray.direction;
        Vector3   lineOrigin    = ray.origin;

        Action <GraphNode> < > 9__0;
        for (int i = 0; i < this.graphs.Length; i++)
        {
            NavGraph           navGraph = this.graphs[i];
            Action <GraphNode> action;
            if ((action = < > 9__0) == null)
            {
                action = (< > 9__0 = delegate(GraphNode node)
                {
                    Vector3 vector = (Vector3)node.position;
                    Vector3 vector2 = lineOrigin + Vector3.Dot(vector - lineOrigin, lineDirection) * lineDirection;
                    float num = Mathf.Abs(vector2.x - vector.x);
                    if (num * num > minDist)
                    {
                        return;
                    }
                    float num2 = Mathf.Abs(vector2.z - vector.z);
                    if (num2 * num2 > minDist)
                    {
                        return;
                    }
                    float sqrMagnitude = (vector2 - vector).sqrMagnitude;
                    if (sqrMagnitude < minDist)
                    {
                        minDist = sqrMagnitude;
                        nearestNode = node;
                    }
                });
            }
            navGraph.GetNodes(action);
        }
        return(nearestNode);
    }
Beispiel #12
0
        /** Used to serialize references to other nodes e.g connections.
         * Nodes use the GraphSerializationContext.GetNodeIdentifier and
         * GraphSerializationContext.GetNodeFromIdentifier methods
         * for serialization and deserialization respectively.
         */
        static byte[] SerializeGraphNodeReferences(NavGraph graph)
        {
            var stream = new MemoryStream();
            var wr     = new BinaryWriter(stream);
            var ctx    = new GraphSerializationContext(wr);

            graph.GetNodes(node => {
                node.SerializeReferences(ctx);
                return(true);
            });

#if NETFX_CORE
            wr.Dispose();
#else
            wr.Close();
#endif

            var bytes = stream.ToArray();
            return(bytes);
        }
Beispiel #13
0
    public GraphNode GetNearest(Ray ray)
    {
        if (this.graphs == null)
        {
            return(null);
        }
        float     minDist       = float.PositiveInfinity;
        GraphNode nearestNode   = null;
        Vector3   lineDirection = ray.direction;
        Vector3   lineOrigin    = ray.origin;

        for (int i = 0; i < this.graphs.Length; i++)
        {
            NavGraph navGraph = this.graphs[i];
            navGraph.GetNodes(delegate(GraphNode node)
            {
                Vector3 vector = (Vector3)node.position;
                Vector3 a      = lineOrigin + Vector3.Dot(vector - lineOrigin, lineDirection) * lineDirection;
                float num      = Mathf.Abs(a.x - vector.x);
                num           *= num;
                if (num > minDist)
                {
                    return(true);
                }
                num  = Mathf.Abs(a.z - vector.z);
                num *= num;
                if (num > minDist)
                {
                    return(true);
                }
                float sqrMagnitude = (a - vector).sqrMagnitude;
                if (sqrMagnitude < minDist)
                {
                    minDist     = sqrMagnitude;
                    nearestNode = node;
                }
                return(true);
            });
        }
        return(nearestNode);
    }
        private bool AnyDestroyedNodesInGraphs()
        {
            bool result = false;

            Action <GraphNode> < > 9__0;
            for (int i = 0; i < this.graphs.Length; i++)
            {
                NavGraph           navGraph = this.graphs[i];
                Action <GraphNode> action;
                if ((action = < > 9__0) == null)
                {
                    action = (< > 9__0 = delegate(GraphNode node)
                    {
                        if (node.Destroyed)
                        {
                            result = true;
                        }
                    });
                }
                navGraph.GetNodes(action);
            }
            return(result);
        }
Beispiel #15
0
    private void RecalculateDebugLimits()
    {
        this.debugFloor = float.PositiveInfinity;
        this.debugRoof  = float.NegativeInfinity;
        bool ignoreSearchTree = !this.showSearchTree || this.debugPathData == null;

        Action <GraphNode> < > 9__0;
        for (int i = 0; i < this.graphs.Length; i++)
        {
            if (this.graphs[i] != null && this.graphs[i].drawGizmos)
            {
                NavGraph           navGraph = this.graphs[i];
                Action <GraphNode> action;
                if ((action = < > 9__0) == null)
                {
                    action = (< > 9__0 = delegate(GraphNode node)
                    {
                        if (ignoreSearchTree || GraphGizmoHelper.InSearchTree(node, this.debugPathData, this.debugPathID))
                        {
                            if (this.debugMode == GraphDebugMode.Penalty)
                            {
                                this.debugFloor = Mathf.Min(this.debugFloor, node.Penalty);
                                this.debugRoof = Mathf.Max(this.debugRoof, node.Penalty);
                                return;
                            }
                            if (this.debugPathData != null)
                            {
                                PathNode pathNode = this.debugPathData.GetPathNode(node);
                                switch (this.debugMode)
                                {
                                case GraphDebugMode.G:
                                    this.debugFloor = Mathf.Min(this.debugFloor, pathNode.G);
                                    this.debugRoof = Mathf.Max(this.debugRoof, pathNode.G);
                                    return;

                                case GraphDebugMode.H:
                                    this.debugFloor = Mathf.Min(this.debugFloor, pathNode.H);
                                    this.debugRoof = Mathf.Max(this.debugRoof, pathNode.H);
                                    break;

                                case GraphDebugMode.F:
                                    this.debugFloor = Mathf.Min(this.debugFloor, pathNode.F);
                                    this.debugRoof = Mathf.Max(this.debugRoof, pathNode.F);
                                    return;

                                default:
                                    return;
                                }
                            }
                        }
                    });
                }
                navGraph.GetNodes(action);
            }
        }
        if (float.IsInfinity(this.debugFloor))
        {
            this.debugFloor = 0f;
            this.debugRoof  = 1f;
        }
        if (this.debugRoof - this.debugFloor < 1f)
        {
            this.debugRoof += 1f;
        }
    }
	public bool DrawGraph (NavGraph graph, GraphEditor graphEditor) {
		
		Color tmp1 = GUI.color;
		EditorGUILayoutx.FadeArea topFadeArea = GUILayoutx.BeginFadeArea (graph.open,"","graph_"+graph.guid,graphBoxStyle);


		Color tmp2 = GUI.color;
		GUI.color = tmp1;
		
		GUILayout.BeginHorizontal ();
		string graphNameControl = "graph_"+graph.guid+"_name";
		if (graph.name == null) graph.name = graphEditorTypes[graph.GetType ().Name].displayName;
		
		GUI.SetNextControlName (graphNameControl);
		graph.name = GUILayout.TextField (graph.name, EditorGUILayoutx.defaultLabelStyle, GUILayout.ExpandWidth(false),GUILayout.ExpandHeight(false));
		
		if (graph.name == "" && Event.current.type == EventType.Repaint && GUI.GetNameOfFocusedControl() != graphNameControl) {
			graph.name = graphEditorTypes[graph.GetType ().Name].displayName;
		}
		
		if (GUILayout.Button ("",EditorGUILayoutx.defaultLabelStyle)) {
			graph.open = !graph.open;
			if (!graph.open) {
				graph.infoScreenOpen = false;
			}
			RepaintSceneView ();
			return true;
		}
		
		if (script.prioritizeGraphs) {
			if (GUILayout.Button (new GUIContent ("Up","Increase the graph priority"),GUILayout.Width (40))) {
				int index = script.astarData.GetGraphIndex (graph);
				
				//Find the next non null graph
				int next = index-1;
				for (;next >= 0;next--) if (script.graphs[next] != null) break;
				
				if (next >= 0) {
					NavGraph tmp = script.graphs[next];
					script.graphs[next] = graph;
					script.graphs[index] = tmp;
					
					GraphEditor tmpEditor = graphEditors[next];
					graphEditors[next] = graphEditors[index];
					graphEditors[index] = tmpEditor;
				}
				CheckGraphEditors ();
				Repaint ();
			}
			if (GUILayout.Button (new GUIContent ("Down","Decrease the graph priority"),GUILayout.Width (40))) {
				int index = script.astarData.GetGraphIndex (graph);
				
				//Find the next non null graph
				int next = index+1;
				for (;next<script.graphs.Length;next++) if (script.graphs[next] != null) break;
				
				if (next < script.graphs.Length) {
					NavGraph tmp = script.graphs[next];
					script.graphs[next] = graph;
					script.graphs[index] = tmp;
					
					GraphEditor tmpEditor = graphEditors[next];
					graphEditors[next] = graphEditors[index];
					graphEditors[index] = tmpEditor;
				}
				CheckGraphEditors ();
				Repaint ();
			}
		}
		
		bool drawGizmos = GUILayout.Toggle (graph.drawGizmos,"Draw Gizmos",graphGizmoButtonStyle);
		if (drawGizmos != graph.drawGizmos) {
			graph.drawGizmos = drawGizmos;
			RepaintSceneView ();
		}
		
		if (GUILayout.Toggle (graph.infoScreenOpen,"Info",graphInfoButtonStyle)) {
			if (!graph.infoScreenOpen) {
				graph.infoScreenOpen = true;
				graph.open = true;
			}
		} else {
			graph.infoScreenOpen = false;
		}
		
		if (GUILayout.Button ("Delete",graphDeleteButtonStyle)) {
			RemoveGraph (graph);
			return true;
		}
		GUILayout.EndHorizontal ();


		if (topFadeArea.Show () ) {
			EditorGUILayoutx.FadeArea fadeArea = GUILayoutx.BeginFadeArea (graph.infoScreenOpen,"graph_info_"+graph.guid,0);
			if (fadeArea.Show ()) {
				
				bool nodenull = false;
				int total = 0;
				int numWalkable = 0;

				KeyValuePair<float,KeyValuePair<int,int>> pair;
				if ( graphNodeCounts == null ) graphNodeCounts = new Dictionary<NavGraph, KeyValuePair<float, KeyValuePair<int, int>>>();

				if ( !graphNodeCounts.TryGetValue ( graph, out pair ) || (Time.realtimeSinceStartup-pair.Key) > 2 ) {
					GraphNodeDelegateCancelable counter = delegate (GraphNode node) {
						if (node == null) {
							nodenull = true;
							return true;
						}
						total++;
						if (node.Walkable) numWalkable++;
						return true;
					};
					graph.GetNodes (counter);
					pair = new KeyValuePair<float, KeyValuePair<int, int>> (Time.realtimeSinceStartup, new KeyValuePair<int,int>( total, numWalkable ) );
					graphNodeCounts[graph] = pair;
				}

				total = pair.Value.Key;
				numWalkable = pair.Value.Value;

			
				EditorGUI.indentLevel++;
				
				if (nodenull) {
					//EditorGUILayout.HelpBox ("Some nodes in the graph are null. Please report this error.", MessageType.Info);
					Debug.LogError ("Some nodes in the graph are null. Please report this error.");
				}
				
				EditorGUILayout.LabelField ("Nodes",total.ToString());
				EditorGUILayout.LabelField ("Walkable",numWalkable.ToString ());
				EditorGUILayout.LabelField ("Unwalkable",(total-numWalkable).ToString ());
				if (total == 0) EditorGUILayout.HelpBox ("The number of nodes in the graph is zero. The graph might not be scanned",MessageType.Info);
				
				EditorGUI.indentLevel--;
			}
			GUILayoutx.EndFadeArea ();
			
			GUI.color = tmp2;
			
			graphEditor.OnInspectorGUI (graph);
			graphEditor.OnBaseInspectorGUI (graph);
		}

		GUILayoutx.EndFadeArea ();
		
		return false;
	}