public PathFinder(FloorGraph graph) { if (graph == null) { throw new Exception("Graph cannot be null"); } mGraph = graph; }
//This function finds a common node if there is one, otherwise it will return -1 private int findCommonNode(FloorGraph extGraph) { foreach (int key in fGraph.Nodes.Keys) { if (extGraph.Nodes.ContainsKey(key)) { return(key); } } return(-1); }
//Adds all nodes from the external graph private void fullExpand(FloorGraph extGraph) { //Add nodes foreach (KeyValuePair <int, FloorGraphNode> pair in extGraph.Nodes) { List <int> incident = new List <int>(); foreach (int i in pair.Value.GetIncident) { incident.Add(i); } fGraph.AddNode(pair.Value.Id, incident, pair.Value.Exit, pair.Value.Certainty, pair.Value.Live); } //Add edges foreach (KeyValuePair <int, FloorGraphEdge> pair in extGraph.Edges) { fGraph.AddEdge(pair.Value.Id, pair.Value.Room, pair.Value.GetOrigin, pair.Value.GetDestination, pair.Value.Certainty, pair.Value.Live); } }
//Use DFS to expand both mental maps private void DFSExpand(FloorGraph extGraph, int commonNode) { //Start at common node, travel through the tree preferring lower EDGE id's //Travel along the external graphs path, add nodes, edges and references to internal graph //visited edges List <int> visited = new List <int>(); //Set the highest certainty and liveness if (fGraph.Nodes[commonNode].Certainty > extGraph.Nodes[commonNode].Certainty) { extGraph.Nodes[commonNode].Certainty = fGraph.Nodes[commonNode].Certainty; extGraph.Nodes[commonNode].Live = fGraph.Nodes[commonNode].Live; } else { fGraph.Nodes[commonNode].Certainty = extGraph.Nodes[commonNode].Certainty; fGraph.Nodes[commonNode].Live = extGraph.Nodes[commonNode].Live; } foreach (int e in extGraph.Nodes[commonNode].GetIncident) { //If edge has not been visited yet if (!(visited.Contains(e))) { //Set edge to visited visited.Add(e); //If iNodes does not contain a refrence to the edge if (!(fGraph.Nodes[commonNode].GetIncident.Contains(e))) { int destNode = extGraph.Edges[e].GetDestination; //Add reference in commonnode fGraph.Nodes[commonNode].GetIncident.Add(e); //Copy edge to iEdges extGraph.AddEdge(extGraph.Edges[e].Id, extGraph.Edges[e].Room, extGraph.Edges[e].GetOrigin, extGraph.Edges[e].GetDestination, extGraph.Edges[e].Certainty, extGraph.Edges[e].Live); //Add destination node to iNodes fGraph.AddNode(destNode, new List <int>(), extGraph.Nodes[destNode].Exit, extGraph.Nodes[destNode].Certainty, extGraph.Nodes[destNode].Live); //Recurse on destination DFSExpand(extGraph, destNode); } else { //set certainty and liveness if (fGraph.Edges[e].Certainty > extGraph.Edges[e].Certainty) { extGraph.Edges[e].Certainty = fGraph.Edges[e].Certainty; extGraph.Edges[e].Live = fGraph.Edges[e].Live; } else { fGraph.Edges[e].Certainty = extGraph.Edges[e].Certainty; fGraph.Edges[e].Live = extGraph.Edges[e].Live; } //Recurse on destination DFSExpand(extGraph, fGraph.Edges[e].GetDestination); } } } }
void Awake() { fGraph = new FloorGraph(); Color32[,] grid = createGrid(); createGraph(grid); }