/// <summary> /// Gets the path from start to end. /// </summary> /// <param name="from">The start.</param> /// <param name="to">The end.</param> /// <returns>The path from start to end if it exists, null if else.</returns> public IEnumerable <Point> GetPath(Point from, Point to) { IEnumerable <Node> nodes = pathfinder.FindPath( Nodes[(from.Y * Width) + from.X], Nodes[(to.Y * Width) + to.X]); return(nodes.Select(node => new Point(node.ID % Width, node.ID / Width)).ToList()); }
private void MovePiece(Node source, Node destination) { _controller.Move(currentNode, source).Sleep(1000).MagnetOn(); ExecutePath(_pathfinder.FindPath(source, destination)); _controller.Sleep(1000).MagnetOff(); destination.IsEmpty = false; source.IsEmpty = true; }
public static Path <TGraphNode> FindPath <TGraphNode>( this IPathfinder <TGraphNode> pathfinder, TGraphNode sourceNode, TGraphNode targetNode ) where TGraphNode : IGraphNode <TGraphNode> { return(pathfinder.FindPath( sourceNode, targetNode, EqualityComparer <TGraphNode> .Default )); }
public static Path <GraphNode> FindPath <GraphNode>( this IPathfinder <GraphNode> pathfinder, GraphNode sourceNode, GraphNode targetNode, IEqualityComparer <GraphNode> targetEqualityComparer ) where GraphNode : IGraphNode <GraphNode> { return(pathfinder.FindPath( sourceNode, targetNode, targetEqualityComparer, EqualityComparer <GraphNode> .Default )); }
public Plan FormulatePlan( IKnowledgeProvider knowledgeProvider, HashSet <PlanningAction> availableActions, Goal goal ) { var timer = new StopwatchExecutionTimer(); var allActions = new HashSet <PlanningAction>(); allActions.UnionWith(availableActions); allActions.UnionWith(experienceActions.Where(experienceAction => availableActions.IsSupersetOf(experienceAction.Actions))); var initialWorldState = new RelevantSymbolsPopulator(allActions, goal) .PopulateWorldState(knowledgeProvider); var goalConstraints = new RegressiveStatePopulator().Populate(goal); try { var path = pathfinder.FindPath( RegressiveNode.MakeRegular( goalConstraints, initialWorldState, new RegressiveNodeExpander(allActions) ), RegressiveNode.MakeTarget(initialWorldState) ); if (learning) { var beforeLearning = timer.ElapsedSeconds; Learn(path, initialWorldState); UnityEngine.Debug.Log( $"Learning time: {timer.ElapsedSeconds-beforeLearning}"); } return(new Plan(from edge in path.Edges.Reverse() select((RegressiveEdge)edge).Action, goal)); } catch (PathNotFoundException e) { throw new PlanNotFoundException(this, maxPlanLength, goal, e); } }
private void ProcessAiWithoutTarget(IAiEntity mov) { if (mov.Waypoints != null && mov.Waypoints.Length != 0) { return; } // supposedly should never enter here if (mov.Type == VisualType.Character || mov.Speed == 0) { return; } if (_random.Next(0, 100) < 35) { // wait max 2500 millisecs before having a new movement mov.LastMove = DateTime.UtcNow.AddMilliseconds(_random.Next(2500)); return; } int i = 0; Position <short> dest = null; while (dest == null && i < 25) { short xpoint = (short)_random.Next(0, 4); short ypoint = (short)_random.Next(0, 4); short firstX = mov.Position.X; short firstY = mov.Position.Y; dest = _map.GetFreePosition(firstX, firstY, xpoint, ypoint); i++; } if (dest == null) { return; } mov.Waypoints = _pathfinder.FindPath(mov.Position, dest, _map); }
private IEnumerator FindPathCoroutine() { MapNode start = StartTile.Node; MapNode end = EndTile.Node; IPathfinder algo = PathfindersFactory.GetPathfinderForType(Settings.Pathfinder); yield return(algo.FindPath( start, end, Settings.AnimateSearch, (node) => { if (!node.HasObstacle) { Tile tile = _nodeToTile[node]; tile.SetColor(Color.green); if (_tileDebugStyle == TileDebugStyle.Cost) { tile.ShowCost(); } } }, (node) => { if (!node.HasObstacle) { Tile tile = _nodeToTile[node]; tile.SetColor(Color.gray); if (_tileDebugStyle == TileDebugStyle.Cost) { tile.ShowCost(); } } } )); List <MapNode> path = new List <MapNode>(); MapNode current = end.CameFrom; while (current != null) { if (current.CameFrom == null && current != start) { path = null; break; } if (current != start) { path.Add(current); } current = current.CameFrom; } if (path != null && path.Count > 0) { for (int i = path.Count - 1; i >= 0; i--) { if (!path[i].HasObstacle) { _nodeToTile[path[i]].SetColor(Color.white); yield return(new WaitForSeconds(0.05f)); } } } else { //notify the player string message = "Could not find a path"; Debug.Log(message); MessagePanel.ShowMessage(message); } yield return(null); }
private void ActionPath(int x, int y, int n) { Transform tile = transform.GetChild(n); SpriteRenderer sr = tile.GetComponent <SpriteRenderer>(); int len = pathfinder.GetPathLength(); if (settingFrom) { // Reset for (int i = 0; i < len; ++i) { SetTileColor(pathfinder.GetPathData(i), Color.white); } if (posFrom.x >= 0 && posFrom.y >= 0) { int fi = Grid.GetIndex(gridColumns, posFrom.x, posFrom.y); SetTileColor(fi, pathfinder.GetType(posFrom.x, posFrom.y) > 0 ? Color.white : Color.black); } if (posTo.x >= 0 && posTo.y >= 0) { int ti = Grid.GetIndex(gridColumns, posTo.x, posTo.y); SetTileColor(ti, Color.white); } posTo = new Point2(-1, -1); posFrom = new Point2(x, y); settingFrom = false; sr.color = Color.red; return; } if (!settingFrom) { posTo = new Point2(x, y); settingFrom = true; #if UNITY_EDITOR Profiler.BeginSample("Pathfind"); #endif sw.Start(); len = pathfinder.FindPath(posFrom, posTo, walkMask); sw.Stop(); ns = sw.Elapsed.TotalMilliseconds * 1000000; ms = sw.ElapsedMilliseconds; sw.Reset(); #if UNITY_EDITOR Profiler.EndSample(); #endif for (int i = 0; i < len; ++i) { SetTileColor(pathfinder.GetPathData(i), Color.green); } sr.color = Color.blue; } }