public GraphEdge AddEdge(int fromNodeIndex, int toNodeIndex, float cost) { var edge = new GraphEdge(fromNodeIndex, toNodeIndex, cost); m_sparseGraph.AddEdge(edge); return(edge); }
// Use this for initialization void Start() { //게임씬으로 시작시 리소스로딩이 안되어 있기 때문에, // 작업편의성을 위해 직접 동기로 불러오는 처리를 넣는다. if (false == Single.resource.IsCompleteLoad()) { Single.resource.Load_Sync(); } Table.File_NodeInfo table = Single.resource._nodeInfo; foreach (Table.NodeInfo nodeFrom in table._data) { _graph.AddNode(new NavGraphNode(nodeFrom.nodeNum, _town.position + nodeFrom.nodePos)); } foreach (Table.NodeInfo nodeFrom in table._data) { foreach (int edgeTo in nodeFrom.edgeList) { _graph.AddEdge(new GraphEdge(nodeFrom.nodeNum, edgeTo)); } } //Debug.DrawLine(Vector3.zero, new Vector3(1, 1, 0), Color.red); //Debug.Assert (false, "sdfsdfsdfsdf assert"); }
public void AddAllNeighborsToGridNode(int Row, int Col, int CellsX, int CellsY) { for (int i = -1; i < 2; ++i) { for (int j = -1; j < 2; ++j) { int NodeX = (Col + j); int NodeY = (Row + i); if ((i == 0) && (j == 0)) { continue; } if (ValidNeighbor(NodeX, NodeY, CellsX, CellsY)) { var Node = (UnityNode)Graph.GetNode(Row * CellsX + Col); if (Node.NodeIndex == -(int)ENodeType.InvalidNodeIndex) { continue; } var NeighborNode = (UnityNode)Graph.GetNode(NodeY * CellsX + NodeX); if (NeighborNode.NodeIndex == (int)ENodeType.InvalidNodeIndex) { continue; } var PosNode = new Vector3(Node.X, 1, Node.Y); var PosNeighborNode = new Vector3(NeighborNode.X, 1, NeighborNode.Y); double Distance = Vector3.Distance(PosNode, PosNeighborNode); var NewEdge = new UnityEdge(Node.NodeIndex, NeighborNode.NodeIndex, Distance); Graph.AddEdge(NewEdge); if (!Graph.IsDigraph()) { UnityEdge Edge = new UnityEdge(NeighborNode.NodeIndex, Node.NodeIndex, Distance); Graph.AddEdge(Edge); } } } } }
private void CreateGraph() { graph = new SparseGraph(false); var objects = FindObjectsOfType <ObjectNode>().OrderBy(x => x.index).ToArray(); foreach (var o in objects) { graph.AddNode(new GraphNode(o.index)); } foreach (var o in objects) { foreach (var n in o.Connects) { graph.AddEdge(new GraphEdge(o.index, n.index, Vector2.Distance(o.transform.position, n.transform.position))); } } }
public override void Create() { Graph = new SparseGraph(false); for (int i = 0; i < World.Instance.Waypoints.Count; i++) { var node = new Node(i) { Position = new Vector2(World.Instance.Waypoints[i].x, World.Instance.Waypoints[i].z) }; Graph.AddNode(node); //AddNodeObject(node, node.Position); } for (int fromIndex = 0; fromIndex < Graph.NumNodes; fromIndex++) { Node fromNode = Graph.GetNode(fromIndex); for (int toIndex = 0; toIndex < Graph.NumNodes; toIndex++) { Node toNode = Graph.GetNode(toIndex); if (IsPathObstructed(fromNode.Position, toNode.Position)) { continue; } var edge = new Edge( fromIndex, toIndex, (fromNode.Position - toNode.Position).magnitude); Graph.AddEdge(edge); //AddEdgeObject(edge, fromNode.Position, toNode.Position); } } }
public void Load(Stream stream) { Graph = new SparseGraph <GraphNode, GraphEdge>(false); var lines = LoadLines(stream); foreach (var line in lines) { var nodes = line.Split(','); var from = int.Parse(nodes[0]); var node = new GraphNode(); if (Graph.AddNode(node) != from) { throw new ArgumentException("Invalid source data: new node does not match graph index."); } for (var i = 1; i < nodes.Length; i++) { var to = int.Parse(nodes[i]); Graph.AddEdge(new GraphEdge(from, to)); } } }
public override void Create() { Graph = new SparseGraph(false); for (int i = 0; i < World.Instance.Waypoints.Count; i++) { var node = new Node(i) { Position = new Vector2(World.Instance.Waypoints[i].x, World.Instance.Waypoints[i].z) }; Graph.AddNode(node); //AddNodeObject(node, node.Position); } for (int fromIndex = 0; fromIndex < Graph.NumNodes; fromIndex++) { Node fromNode = Graph.GetNode(fromIndex); for (int toIndex = 0; toIndex < Graph.NumNodes; toIndex++) { Node toNode = Graph.GetNode(toIndex); if (IsPathObstructed(fromNode.Position, toNode.Position)) { continue; } var edge = new Edge( fromIndex, toIndex, (fromNode.Position - toNode.Position).magnitude); Graph.AddEdge(edge); //AddEdgeObject(edge, fromNode.Position, toNode.Position); } } }
public override void Create() { Graph = new SparseGraph(false); for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { var nodePosition = new Vector2( column * cellWidth - cellWidth * (columns - 1) / 2, row * cellHeight - cellHeight * (rows - 1) / 2); var node = new Node(row * columns + column) { Position = nodePosition }; int nodeIndex = Graph.AddNode(node); if (IsPointInObstacle(nodePosition)) { Graph.GetNode(nodeIndex).Index = Node.INVALID_NODE_INDEX; } else { //AddNodeObject(node, nodePosition); } } } for (int nodeIndex = 0; nodeIndex < Graph.NumNodes; nodeIndex++) { if (!Graph.IsNodePresent(nodeIndex)) { continue; } Node node = Graph.GetNode(nodeIndex); int rightIndex = nodeIndex + 1; if (rightIndex % columns != 0 && Graph.IsNodePresent(rightIndex)) { Node rightNode = Graph.GetNode(rightIndex); if (!IsPathObstructed(node.Position, rightNode.Position)) { var rightEdge = new Edge(nodeIndex, rightIndex, cellWidth); Graph.AddEdge(rightEdge); //AddEdgeObject(rightEdge, node.Position, rightNode.Position); } } int downIndex = nodeIndex + columns; if (downIndex < Graph.NumNodes && Graph.IsNodePresent(downIndex)) { Node downNode = Graph.GetNode(downIndex); if (!IsPathObstructed(node.Position, downNode.Position)) { var downEdge = new Edge(nodeIndex, downIndex, cellHeight); Graph.AddEdge(downEdge); //AddEdgeObject(downEdge, node.Position, downNode.Position); } } if (!useDiagonals) { continue; } int diagIndex = nodeIndex + columns + 1; if (diagIndex < Graph.NumNodes && diagIndex % columns != 0 && Graph.IsNodePresent(diagIndex)) { Node diagNode = Graph.GetNode(diagIndex); if (!IsPathObstructed(node.Position, diagNode.Position)) { var diagEdge = new Edge(nodeIndex, diagIndex, cellDiagonal); Graph.AddEdge(diagEdge); //AddEdgeObject(diagEdge, node.Position, diagNode.Position); } } int backDiagIndex = nodeIndex + columns - 1; if (backDiagIndex < Graph.NumNodes && backDiagIndex % columns != columns - 1 && Graph.IsNodePresent(backDiagIndex)) { Node backDiagNode = Graph.GetNode(backDiagIndex); if (!IsPathObstructed(node.Position, backDiagNode.Position)) { var backDiagEdge = new Edge(nodeIndex, backDiagIndex, cellDiagonal); Graph.AddEdge(backDiagEdge); //AddEdgeObject(backDiagEdge, node.Position, backDiagNode.Position); } } } }
// TODO: I need to set up the allGroundedEntities Dictionary public void SetupGraph() { // SET UP THE GRAPH... if (hasBeenSetup) { return; } // the game objects containing the node graphs GameObject[] nodeGameObjects = GameObject.FindGameObjectsWithTag("Node"); // The nodes in the game world nodeList = new Node[nodeGameObjects.Length]; // initialize the message list dual array messageList = new Message[nodeList.Length][]; for (int i = 0; i < nodeList.Length; i++) { messageList[i] = new Message[nodeList.Length]; } // Initialize the list of messages for (int i = 0; i < nodeList.Length; i++) { for (int j = 0; j < nodeList.Length; j++) { messageList[i][j] = Message.DoNothing; } } // Create the abstract graph using the info scanned from the level G = new SparseGraph <GraphNode, GraphEdge>(); // we'll be adding new nodes to the abstract graph, as well as set the ID's of the nodes in the level for (int i = 0; i < nodeGameObjects.Length; i++) { // set the reference for nodeList[i] nodeList[i] = nodeGameObjects[i].GetComponent <Node>(); // update the node's ID nodeList[i].ID = i; // add the node to the abstract graph G.AddNode(new GraphNode(i)); } // Create the edges in the abstract graph and set the weights // Note that the weights are determined heuristically within the node scripts // Also, set the correct message in the n by n messageList[][] so that later when we have the information that the path // from say, 1 to 2 is to be taken, then the message to send the game character is messageList[1][2] for (int i = 0; i < nodeList.Length; i++) { nodeList[i].SetWeights(); // we need to get the information for the edge weight between nodes and create the edge in the abstract graph // so that we can search it efficiently for a path for (int j = 0; j < nodeList[i].neighbors.Count; j++) { // i is the current node ID // nodeList[i].neighbors[j].ID is the ID of the current neighbor // same with the weight G.AddEdge(new GraphEdge(i, nodeList[i].neighbors[j].ID, nodeList[i].weights[j])); // save the way to traverse this edge in the dual array // This makes it so that you can access the message to get from node u to node v // by looking up messageList[u][v] messageList[nodeList[i].ID][nodeList[i].neighbors[j].ID] = nodeList[i].messages[j]; } } hasBeenSetup = true; }
public override void Create() { Graph = new SparseGraph(false); for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { var nodePosition = new Vector2( column * cellWidth - cellWidth * (columns - 1) / 2, row * cellHeight - cellHeight * (rows - 1) / 2); var node = new Node(row * columns + column) { Position = nodePosition }; int nodeIndex = Graph.AddNode(node); if (IsPointInObstacle(nodePosition)) { Graph.GetNode(nodeIndex).Index = Node.INVALID_NODE_INDEX; } else { //AddNodeObject(node, nodePosition); } } } for (int nodeIndex = 0; nodeIndex < Graph.NumNodes; nodeIndex++) { if (!Graph.IsNodePresent(nodeIndex)) { continue; } Node node = Graph.GetNode(nodeIndex); int rightIndex = nodeIndex + 1; if (rightIndex % columns != 0 && Graph.IsNodePresent(rightIndex)) { Node rightNode = Graph.GetNode(rightIndex); if (!IsPathObstructed(node.Position, rightNode.Position)) { var rightEdge = new Edge(nodeIndex, rightIndex, cellWidth); Graph.AddEdge(rightEdge); //AddEdgeObject(rightEdge, node.Position, rightNode.Position); } } int downIndex = nodeIndex + columns; if (downIndex < Graph.NumNodes && Graph.IsNodePresent(downIndex)) { Node downNode = Graph.GetNode(downIndex); if (!IsPathObstructed(node.Position, downNode.Position)) { var downEdge = new Edge(nodeIndex, downIndex, cellHeight); Graph.AddEdge(downEdge); //AddEdgeObject(downEdge, node.Position, downNode.Position); } } if (!useDiagonals) { continue; } int diagIndex = nodeIndex + columns + 1; if (diagIndex < Graph.NumNodes && diagIndex % columns != 0 && Graph.IsNodePresent(diagIndex)) { Node diagNode = Graph.GetNode(diagIndex); if (!IsPathObstructed(node.Position, diagNode.Position)) { var diagEdge = new Edge(nodeIndex, diagIndex, cellDiagonal); Graph.AddEdge(diagEdge); //AddEdgeObject(diagEdge, node.Position, diagNode.Position); } } int backDiagIndex = nodeIndex + columns - 1; if (backDiagIndex < Graph.NumNodes && backDiagIndex % columns != columns - 1 && Graph.IsNodePresent(backDiagIndex)) { Node backDiagNode = Graph.GetNode(backDiagIndex); if (!IsPathObstructed(node.Position, backDiagNode.Position)) { var backDiagEdge = new Edge(nodeIndex, backDiagIndex, cellDiagonal); Graph.AddEdge(backDiagEdge); //AddEdgeObject(backDiagEdge, node.Position, backDiagNode.Position); } } } }