private async Task <bool> ViewPass(List <Point> pointList, ASpeedModifier modifier) { if (pointList == null) { return(false); } var searchBlock = BlockList.Where((x) => { return(ComparePoints(pointList, x.Position)); }); foreach (var item in searchBlock) { modifier.TimePassedForView += modifier.ViewDelayInMilliseconds; await Task.Delay(modifier.ViewDelayInMilliseconds); item.NType = NodeType.Path; } return(await Task.FromResult(true)); }
private async void FindPathButton_Click(object sender, RoutedEventArgs e) { Point startPosition = new Point(-1, -1); Point targetPosition = new Point(-1, -1); var field = new int[MATRIX_SIZE, MATRIX_SIZE * 2]; List <Point> wallList = new List <Point>(); targetPosition = BlockList.Where(x => x.NType == NodeType.Target).Select(x => x.Position).FirstOrDefault(); if (targetPosition == null) { return; } var searchWall = BlockList.Where(x => x.NType == NodeType.Wall).Select(x => x.Position); foreach (var item in searchWall) { wallList.Add(item); } int countPaths = CurrentBlockList.Count; List <Point>[] PathsList = new List <Point> [countPaths]; ASpeedModifier speedModifier = new ASpeedModifier() { SearchDelayInMilliseconds = 10, ViewDelayInMilliseconds = 20 }; List <List <Point> > pathListArray = new List <List <Point> >(); long timeElapseWithDisplay = 0; long timeElapseWithoutDisplay = 0; var tasks = CurrentBlockList.Select(async(x) => { var watchWithDisplay = Stopwatch.StartNew(); var watchWithoutDisplay = Stopwatch.StartNew(); await aSCore.FindPathWithoutDisplayAsync(field, startPosition, targetPosition, wallList) .ContinueWith(t => watchWithoutDisplay.Stop()); pathListArray.Add(await aSCore.FindPathAsync(field, x.Position, targetPosition, wallList, BlockList, speedModifier)); watchWithDisplay.Stop(); timeElapseWithDisplay += watchWithDisplay.ElapsedMilliseconds; timeElapseWithoutDisplay += watchWithoutDisplay.ElapsedMilliseconds; }); await Task.WhenAll(tasks); //Тест BlockList.ForEach((x) => { if (x.NType == NodeType.SearchPath) { x.NType = NodeType.Passable; } }); pathListArray.ForEach(async x => await ViewPass(x, speedModifier)); elapsedTimeLabel.Content = " Скорость: " + Math.Round((double)timeElapseWithoutDisplay / timeElapseWithDisplay, 4) + " Всего: " + timeElapseWithDisplay + " мс. | Всего за поиск: " + timeElapseWithoutDisplay + " мс."; }
public async Task <List <Point> > FindPathAsync(int[,] field, Point start, Point goal, List <Point> wall, List <BlockModel> blockModels, ASpeedModifier modifier) { var closedSet = new Collection <PathNode>(); var openSet = new Collection <PathNode>(); PathNode startNode = new PathNode() { Position = start, CameFrom = null, PathLengthFromStart = 0, HeuristicEstimatePathLength = await GetHeuristicPathLength(start, goal), StartOrDefault = true }; openSet.Add(startNode); while (openSet.Count > 0) { var currentNode = openSet.OrderBy(node => node.EstimateFullPathLength).First(); if (currentNode.Position == goal) { currentNode.StartOrDefault = true; return(await Task.FromResult(await GetPathForNode(currentNode))); } openSet.Remove(currentNode); closedSet.Add(currentNode); foreach (var neighbourNode in await GetNeighbours(currentNode, goal, field)) { modifier.TimePassedForSearch += modifier.SearchDelayInMilliseconds; if (closedSet.Count(node => node.Position == neighbourNode.Position) > 0) { continue; } var openNode = openSet.FirstOrDefault(node => node.Position == neighbourNode.Position); if (wall.Exists(x => x == neighbourNode.Position)) { continue; } await Task.Delay(modifier.SearchDelayInMilliseconds); //Тест blockModels.ForEach((x) => { if (x.Position == neighbourNode.Position && (x.NType != NodeType.Target && x.NType != NodeType.Current)) { x.NType = NodeType.SearchPath; } }); if (openNode == null) { openSet.Add(neighbourNode); } else if (openNode.PathLengthFromStart > neighbourNode.PathLengthFromStart) { openNode.CameFrom = currentNode; openNode.PathLengthFromStart = neighbourNode.PathLengthFromStart; } } } return(null); }