/// <summary> /// Updates the collisionmap, adding the rectangle to the collisionmap, and performing new connections, as for placing nodes /// </summary> /// <param name="rect"></param> public BuildingMesh PlaceBuilding(Rectangle rect) { CollisionChangedEvent e = new CollisionChangedEvent(); e.collision = this; e.changedRect = rect; e.collisionAdded = true; long ticks = DateTime.UtcNow.Ticks; e.oldData = (Boolean[])data.Clone(); Console.Out.WriteLine("Clone time: " + (DateTime.UtcNow.Ticks - ticks) / 10000 + "ms"); ticks = DateTime.UtcNow.Ticks; Boolean color = true; // Update collisionmap data for (int i = rect.Left; i < rect.Right; i++) { for (int j = rect.Top; j < rect.Bottom; j++) { data[PointToIndex(i, j)] = color; } } // Update collisionmap texture this.texture = BoolToTexture(Game1.GetInstance().graphics.GraphicsDevice, this.data, mapWidth, collisionMapTextureScale); e.newData = data; Console.Out.WriteLine("Data put time: " + (DateTime.UtcNow.Ticks - ticks) / 10000 + "ms"); ticks = DateTime.UtcNow.Ticks; Node[] newNodes = new Node[4]; RTSCollisionMap map = Game1.GetInstance().collision; newNodes[0] = new Node(map, rect.Left - 1, rect.Top - 1, true); newNodes[1] = new Node(map, rect.Right + 1, rect.Top - 1, true); newNodes[2] = new Node(map, rect.Left - 1, rect.Bottom + 1, true); newNodes[3] = new Node(map, rect.Right + 1, rect.Bottom + 1, true); Console.Out.WriteLine("Node create time: " + (DateTime.UtcNow.Ticks - ticks) / 10000 + "ms"); ticks = DateTime.UtcNow.Ticks; BuildingMesh mesh = new BuildingMesh(this); mesh.rect = rect; mesh.createdNodes = newNodes; LinkedList<PathfindingNode> nodes = PathfindingNodeManager.GetInstance().nodeList; foreach (PathfindingNode node in nodes) { if (rect.Contains(node.GetLocation())) mesh.removedNodes.AddLast(node.GetLocation()); } Console.Out.WriteLine("Mesh create time: " + (DateTime.UtcNow.Ticks - ticks) / 10000 + "ms"); ticks = DateTime.UtcNow.Ticks; LinkedList<Node> processedNodes = new LinkedList<Node>(); int nodesAdded = 0; foreach (Node newNode in newNodes) { foreach (Node connectedNode in newNode.GetConnectedNodes()) { // Let's not process things twice, as it's quite a heavy computation if (!processedNodes.Contains(connectedNode)) { // Remove its current nodes connectedNode.RemoveAllConnections(); // Scedule it for reprocessing SmartPathfindingNodeProcessor.GetInstance().Push(connectedNode); processedNodes.AddLast(connectedNode); nodesAdded++; } } } Console.Out.WriteLine("Node connection time: " + (DateTime.UtcNow.Ticks - ticks) / 10000 + "ms, adding " + nodesAdded + " nodes"); ticks = DateTime.UtcNow.Ticks; FireCollisionChangedEvent(e); Console.Out.WriteLine("Event time: " + (DateTime.UtcNow.Ticks - ticks)); return mesh; }
/// <summary> /// Updates the collisionmap, adding the rectangle to the collisionmap, and performing new connections, as for placing nodes /// </summary> /// <param name="rect"></param> public BuildingMesh PlaceBuilding(Rectangle rect) { CollisionChangedEvent e = new CollisionChangedEvent(); e.collisionMap = this; e.changedRect = rect; e.collisionAdded = true; long ticks = DateTime.UtcNow.Ticks; this.UpdateCollisionMap(rect, true); Console.Out.WriteLine("Data put time: " + (DateTime.UtcNow.Ticks - ticks) / 10000 + "ms"); ticks = DateTime.UtcNow.Ticks; Node[] newNodes = new Node[4]; RTSCollisionMap map = Game1.GetInstance().map.collisionMap; newNodes[0] = new Node(map, rect.Left - 1, rect.Top - 1, false); newNodes[1] = new Node(map, rect.Right + 1, rect.Top - 1, false); newNodes[2] = new Node(map, rect.Left - 1, rect.Bottom + 1, false); newNodes[3] = new Node(map, rect.Right + 1, rect.Bottom + 1, false); Console.Out.WriteLine("Node create time: " + (DateTime.UtcNow.Ticks - ticks) / 10000 + "ms"); ticks = DateTime.UtcNow.Ticks; BuildingMesh mesh = new BuildingMesh(this); mesh.rect = rect; mesh.createdNodes = newNodes; PathfindingNodeManager manager = PathfindingNodeManager.GetInstance(); for (int i = 0; i < manager.GetNodeCount(); i++) { Node node = (Node)manager.GetNodeAt(i); if (rect.Contains(node.GetLocation())) mesh.removedNodes.AddLast(node.GetLocation()); } Console.Out.WriteLine("Mesh create time: " + (DateTime.UtcNow.Ticks - ticks) / 10000 + "ms"); /* ticks = DateTime.UtcNow.Ticks; CustomArrayList<Node> processedNodes = new CustomArrayList<Node>(); int nodesAdded = 0; foreach (Node newNode in newNodes) { } Console.Out.WriteLine("Node connection time: " + (DateTime.UtcNow.Ticks - ticks) / 10000 + "ms, adding " + nodesAdded + " nodes"); */ ticks = DateTime.UtcNow.Ticks; FireCollisionChangedEvent(e); Console.Out.WriteLine("Event time: " + (DateTime.UtcNow.Ticks - ticks)); return mesh; }