public List <UT_Node <T> > BFS(UT_Node <T> startNode) { List <UT_Node <T> > res = new List <UT_Node <T> >(); bool[] visited = new bool[m_nodes.Count]; Queue <UT_Node <T> > queue = new Queue <UT_Node <T> >(); visited[m_nodes.IndexOf(startNode)] = true; queue.Enqueue(startNode); // startNode is now used as a temporary node while (queue.Count > 0) { startNode = queue.Dequeue(); res.Add(startNode); List <UT_Node <T> > adj = startNode.GetAdjacent(); foreach (UT_Node <T> node in adj) { int index = m_nodes.IndexOf(node); if (!visited[index]) { visited[index] = true; queue.Enqueue(node); } } } return(res); }
/// <summary> /// Func done by each node traversed. /// </summary> public void BFS(UT_Node <T> startNode, Action <UT_Node <T> > Func) { bool[] visited = new bool[m_nodes.Count]; Queue <UT_Node <T> > queue = new Queue <UT_Node <T> >(); visited[m_nodes.IndexOf(startNode)] = true; queue.Enqueue(startNode); // startNode is now used as a temporary node while (queue.Count > 0) { startNode = queue.Dequeue(); Func(startNode); List <UT_Node <T> > adj = startNode.GetAdjacent(); foreach (UT_Node <T> node in adj) { int index = m_nodes.IndexOf(node); if (!visited[index]) { visited[index] = true; queue.Enqueue(node); } } } }
public void UpdatePerDay() { m_graphBuildings.Foreach((UT_Node <BuildingStats> node) => { node.GetData().overallMood = 0; node.GetData().availability = 0; node.GetData().createdInstances = 0; }); List <IBuilding> buildings = M_BuildingManager.SGetBuildings(); foreach (IBuilding building in buildings) { if (building is IBuildingHouse) { GetNode(building.GetData().id).GetData().createdInstances++; continue; } if (!(building is IBuildingProduction)) { continue; } IBuildingProduction prod = (IBuildingProduction)building; UT_Node <BuildingStats> node = GetNode(building.GetData().id); node.GetData().overallMood += prod.GetMood(); node.GetData().createdInstances++; if (prod.IsWorking()) { M_WaresManager.SAddProductiveBuilding(prod.GetId(), prod.GetProducedWareType()); } else { M_WaresManager.SRemoveProductiveBuilding(prod.GetId(), prod.GetProducedWareType()); } } m_graphBuildings.Foreach((UT_Node <BuildingStats> node) => { BuildingStats stats = node.GetData(); foreach (DAT_Building.BuildDependency dep in stats.data.dependencies) { if (stats.overallMood > dep.requiredMood) { UT_Node <BuildingStats> tempNode = GetNode(dep.unlocksBuilding); tempNode.GetData().availability += 1; if (tempNode.GetData().availability == tempNode.GetAdjacent().Count && m_currentBuildingsLevel < tempNode.GetData().buildingLevel) { m_currentBuildingsLevel = tempNode.GetData().buildingLevel; } } } }); }
public void GetPathLengths(int start) { List <UT_Node <BuildingStats> > m_nodes = m_graphBuildings.GetNodes(); int level = 0; bool[] visited = new bool[m_nodes.Count]; Queue <UT_Node <BuildingStats> > queue = new Queue <UT_Node <BuildingStats> >(); UT_Node <BuildingStats> startNode = GetNode(start); visited[m_nodes.IndexOf(startNode)] = true; queue.Enqueue(startNode); startNode.GetData().buildingLevel = level; while (queue.Count > 0) { UT_Node <BuildingStats> temp = queue.Dequeue(); if (temp.GetData().buildingLevel > level) { level++; } List <UT_Node <BuildingStats> > adj = temp.GetAdjacent(); foreach (UT_Node <BuildingStats> node in adj) { int index = m_nodes.IndexOf(node); if (!visited[index]) { visited[index] = true; node.GetData().buildingLevel = level + 1; queue.Enqueue(node); } } } }