private void OnUpdateHandler(float del) { if (characterController != null) { float distance = Vector3.Distance(targetPos, player.transform.position); Debug.Log(distance); if (distance <= 0.1f) { index++; if (index < path.Count) { pathFinderNode = path[index]; targetPos = sceneInfo.GridToPixel(pathFinderNode.X, pathFinderNode.Y); player.transform.LookAt(targetPos); } else { TimerManager.RemoveHandler(timerInfo); } } else { characterController.SimpleMove(player.transform.forward); } } }
public Direction DirectionTo(PathFinderNode NextNode) { if (NextNode == null) { return(Direction.None); } if (X == NextNode.X && Y == NextNode.Y - 1) { return(Direction.South); } else if (X == NextNode.X && Y == NextNode.Y + 1) { return(Direction.North); } else if (X == NextNode.X - 1 && Y == NextNode.Y) { return(Direction.East); } else if (X == NextNode.X + 1 && Y == NextNode.Y) { return(Direction.West); } else { return(Direction.None); } }
private int CalculateDistanceCost(PathFinderNode a, PathFinderNode b) { int DeltaX = Math.Abs(a.X - b.X); int DeltaY = Math.Abs(a.Y - b.Y); return(DeltaX + DeltaY); }
public List <PathFinderNode> FindPath(Point s, Point e) { s = Clamp(s); e = Clamp(e); int si = GetArrayIndex(s); int ei = GetArrayIndex(e); Console.WriteLine("FindPath: Initializing Dijkstra with {0}*{1}={2}", chan.Width, chan.Height, chan.Width * chan.Height); Dijkstra p = new Dijkstra( chan.Width * chan.Height, this.getInternodeTraversalCost, this.nearbyNodesHint); Console.WriteLine("FindPath: Locating path from {0} to {1}.", s, e); int[] PointPath = p.GetMinimumPath(si, ei); List <PathFinderNode> Path = new List <PathFinderNode>(); foreach (int pi in PointPath) { Point pt = GetPointFromArrayIndex(pi); PathFinderNode pfn = new PathFinderNode(); pfn.X = pt.X; pfn.Y = pt.Y; Path.Add(pfn); } return(Path); }
private void WorkBestTile(RoomUser user) { if (user.Path.Count > 1) { PathFinderNode skipNode = user.Path[1]; if (TilesTouching(user.CurrentX, user.CurrentY, skipNode.X, skipNode.Y)) { if (TileCheck(skipNode.X, skipNode.Y, mMatrix)) { int checkX1 = 0; int checkX2 = 0; if (skipNode.X > user.CurrentX) { checkX1 = -1; checkX2 = 1; } else { checkX1 = 1; checkX2 = -1; } if (TileCheck(skipNode.X + checkX1, skipNode.Y, mMatrix) && TileCheck(user.CurrentX + checkX2, user.CurrentY, mMatrix)) { PathFinderNode nextNode = user.Path[0]; user.Path.Remove(nextNode); } } } } }
public PathFinderNode GetNewPath(PathFinderNode oldNode) { if (oldNode.childs.Length == 0) { return(null); } return(oldNode.childs[Random.Range(0, oldNode.childs.Length)]); }
public void createPathFinderNodes() { float pathNodeOffset = PathFinder.PathStep; int verticePos = 0; foreach (Vector2 vertex in mapVertices) { Vector2 vertexPx = vertex * Bloodbender.meterToPixel; /* création des point qui représent les pathnode */ Vertices cornerSquare1 = new Vertices(); cornerSquare1.Add(new Vector2(0, 0)); Vertices cornerSquare2 = new Vertices(); cornerSquare2.Add(new Vector2(0, 0)); /* récupération des vertex suivant et précédents pour la création des vecteur */ Vector2 nextVertex = getNextVertex(verticePos, true) * Bloodbender.meterToPixel; Vector2 prevVertex = getNextVertex(verticePos, false) * Bloodbender.meterToPixel; Vector2 a = new Vector2(nextVertex.X - vertexPx.X, nextVertex.Y - vertexPx.Y); Vector2 b = new Vector2(prevVertex.X - vertexPx.X, prevVertex.Y - vertexPx.Y); /* transaltion des point selon le vecteur rescale à pathNodeOffset */ cornerSquare1.Translate(a * (pathNodeOffset / a.Length())); cornerSquare2.Translate(-a * (pathNodeOffset / a.Length())); /* rotation des points */ float vectorAngle = ((float)Math.Atan2(b.Y, b.X) - (float)Math.Atan2(a.Y, a.X)) / 2.0f; cornerSquare1.Rotate(vectorAngle); cornerSquare2.Rotate(vectorAngle); /* repositionnement des points */ cornerSquare1.Translate(vertexPx); cornerSquare2.Translate(vertexPx); /* création des PathNode */ PathFinderNode node1 = new PathFinderNode(cornerSquare1[0]); PathFinderNode node2 = new PathFinderNode(cornerSquare2[0]); //Bloodbender.ptr.pathFinder.addNode(node1); //Bloodbender.ptr.pathFinder.addNode(node2); node1.DivergencePoint = vertex; node2.DivergencePoint = vertex; //if (!TreePlanter.IsPointOutside(node1.position.X * Bloodbender.pixelToMeter, node1.position.Y * Bloodbender.pixelToMeter)) //{ Bloodbender.ptr.pFinder.AddNode(node1); pathFinderNodes.Add(node1); //} //if (!TreePlanter.IsPointOutside(node2.position.X * Bloodbender.pixelToMeter, node2.position.Y * Bloodbender.pixelToMeter)) //{ Bloodbender.ptr.pFinder.AddNode(node2); pathFinderNodes.Add(node2); //} verticePos++; } }
private float GetCost(PathFinderNode startNode, PathFinderNode endNode) { if (startNode.X != endNode.X && startNode.Y != endNode.Y) { return(1.5f); } return(1); }
private float GetCost(PathFinderNode startNode, PathFinderNode endNode) { if (startNode.X != endNode.X && startNode.Y != endNode.Y) { return 1.5f; } return 1; }
private void centerToVertexNodeCreate(Vector2 vertex, PolygonShape shape) { Vector2 centroid = shape.MassData.Centroid; Vector2 centerToVertex = new Vector2(vertex.X - centroid.X, vertex.Y - centroid.Y); centerToVertex *= new Vector2(1) + new Vector2((PathFinder.PathStep * Bloodbender.pixelToMeter) / centerToVertex.Length()); PathFinderNode node1 = new PathFinderNode(body.Position * Bloodbender.meterToPixel, centerToVertex, this); node1.DivergencePoint = centroid + body.Position; Bloodbender.ptr.pFinder.AddNode(node1); }
public FollowBehaviorComponent(PhysicObj obj, PhysicObj target, float escapeZoneRadius) { this.owner = obj; this.target = target; this.escapeZoneRadius = escapeZoneRadius; maxVertexDistance = maxLenghtCentroidVertex(); nextNode = null; target.getPosNode().TriangleChangedEvent += FollowBehaviorComponent_TriangleChangedEvent; previousTargetPosition = target.body.Position; previousTargetPosition += new Vector2(10, 10); }
public void OnReachPathNode(PathFinderNode oldNode) { PathFinderNode newNode = pathNodes.GetNewPath(oldNode); if (newNode != null) { SetMoveToTarget(newNode.transform); } else { Dead(); } }
private PathFinderNode GetLowestFCostNode(List <PathFinderNode> PathNodeList) { PathFinderNode LowestFCostNode = PathNodeList[0]; for (int i = 0; i < PathNodeList.Count; i++) { if (PathNodeList[i].FCost < LowestFCostNode.FCost) { LowestFCostNode = PathNodeList[i]; } } return(LowestFCostNode); }
private void TT(Vector3 startPoint, Vector3 endPoint) { if (sPoint == null) { sPoint = new Point(0, 0); } if (ePoint == null) { ePoint = new Point(0, 0); } Vector2 s1 = sceneInfo.PixelToGrid(startPoint.x, startPoint.z); Vector2 s2 = sceneInfo.PixelToGrid(endPoint.x, endPoint.z); sPoint.X = (int)s1.x; sPoint.Y = (int)s1.y; ePoint.X = (int)s2.x; ePoint.Y = (int)s2.y; // 1为可行走点 0为阻挡点 PathFinder mPathFinder = new PathFinder(sceneInfo.Grids); mPathFinder.Formula = HeuristicFormula.Manhattan; mPathFinder.Diagonals = true; mPathFinder.HeavyDiagonals = false; mPathFinder.HeuristicEstimate = 2; mPathFinder.PunishChangeDirection = false; mPathFinder.TieBreaker = false; mPathFinder.SearchLimit = 50000; mPathFinder.ReopenCloseNodes = true; List <PathFinderNode> path = mPathFinder.FindPath(sPoint, ePoint); if (path != null && path.Count > 0) { Debug.Log(path.Count); int length = path.Count; for (int i = 0; i < length; ++i) { PathFinderNode pathFinderNode = path[i]; Debug.Log(pathFinderNode.X + "/" + pathFinderNode.Y); } if (walker == null) { walker = new Walker(); } walker.player = testPlayer; walker.path = path; walker.sceneInfo = sceneInfo; walker.DoWalk(); } }
private List <PathFinderNode> CalculatePath(PathFinderNode EndNode) { List <PathFinderNode> NodePathList = new List <PathFinderNode>(); PathFinderNode Node = EndNode; NodePathList.Insert(0, EndNode); while (Node.CameFromNode != null) { Node = Node.CameFromNode; NodePathList.Insert(0, Node); } return(NodePathList); }
public Pathfinder(int AvoidTier = 0) { this.width = GameController.TileDimensionInt; this.height = GameController.TileDimensionInt; this.MapOffset = new Vector2Int(GameController.TileIndexMin, GameController.TileIndexMin); this.AvoidTier = AvoidTier; grid = new PathFinderNode[height, width]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { grid[j, i] = new PathFinderNode(this, i, j); } } }
private Vector2 correctNodePositionForBodyWidth(PathFinderNode node) { if (node.owner == null) { return(node.position); } if (node.offset == Vector2.Zero) { return(node.position); } Vector2 centerToVertexOffset = node.offset; centerToVertexOffset *= new Vector2(maxVertexDistance / centerToVertexOffset.Length()); return(node.position + centerToVertexOffset); }
private void doPlayerUpdate(RoomUser user, PathFinderNode nextNode) { user.NextX = nextNode.X; user.NextY = nextNode.Y; user.NextZ = mHeightMap[user.NextX, user.NextY]; user.Path.Remove(nextNode); mPlayerMap[user.CurrentX, user.CurrentY] = false; mPlayerMap[user.NextX, user.NextY] = true; int newDirection = SpecialMath.WorkDirection(user.CurrentX, user.CurrentY, user.NextX, user.NextY); user.HeadDirection = newDirection; user.BodyDirection = newDirection; mRoomInstance.AnnounceUserStatus(user.SessionID, true); user.CurrentX = user.NextX; user.CurrentY = user.NextY; user.CurrentZ = user.NextZ; }
public static List <Vector2D> FindPath(RoomUser User, bool Diag, Gamemap Map, Vector2D Start, Vector2D End) { var Path = new List <Vector2D>(); PathFinderNode Nodes = FindPathReversed(User, Diag, Map, Start, End); if (Nodes != null) { Path.Add(End); while (Nodes.Next != null) { Path.Add(Nodes.Next.Position); Nodes = Nodes.Next; } } return(Path); }
public void DoWalk() { if (path == null || player == null) { return; } if (characterController == null) { characterController = player.GetComponent <CharacterController>(); } if (characterController == null) { return; } pathFinderNode = path[index]; targetPos = sceneInfo.GridToPixel(pathFinderNode.X, pathFinderNode.Y); player.transform.LookAt(targetPos); timerInfo = TimerManager.AddHandler(OnUpdateHandler); }
public void DetermineFacing() { Pathfinder Path; List <PathFinderNode> PathNodes; int PathTier = 1; do { Path = new Pathfinder(PathTier); PathNodes = Path.FindPath(HeadPosition.x, HeadPosition.y, GameController.Food[0].x, GameController.Food[0].y); PathTier++; } while (PathNodes == null && PathTier <= Tier); if (PathNodes != null) { if (PathNodes.Count < 2) { throw new Exception("Path with no index 1, only one node. Food Position: " + GameController.Food[0] + " HeadPos: " + HeadPosition); } PathFinderNode Node = PathNodes[1]; Vector2Int TargetPosition = new Vector2Int(Node.X, Node.Y) + new Vector2Int(GameController.TileIndexMin, GameController.TileIndexMin); if (TargetPosition.x > HeadPosition.x) { Facing = MoveDir.Right; } else if (TargetPosition.x < HeadPosition.x) { Facing = MoveDir.Left; } else if (TargetPosition.y > HeadPosition.y) { Facing = MoveDir.Up; } else if (TargetPosition.y < HeadPosition.y) { Facing = MoveDir.Down; } } }
private List <PathFinderNode> GetNeighorNodes(PathFinderNode CurrentNode) { List <PathFinderNode> NeighborNodes = new List <PathFinderNode>(); if (CurrentNode.X > 0) { NeighborNodes.Add(grid[CurrentNode.Y, CurrentNode.X - 1]); } if (CurrentNode.X < width - 1) { NeighborNodes.Add(grid[CurrentNode.Y, CurrentNode.X + 1]); } if (CurrentNode.Y > 0) { NeighborNodes.Add(grid[CurrentNode.Y - 1, CurrentNode.X]); } if (CurrentNode.Y < height - 1) { NeighborNodes.Add(grid[CurrentNode.Y + 1, CurrentNode.X]); } return(NeighborNodes); }
public bool IsNodeOpen(PathFinderNode Node) { int MapX = Node.X + MapOffset.x; int MapY = Node.Y + MapOffset.y; if (GameController.CollisionMap[MapY, MapX]) { Vector2Int MapPosition = new Vector2Int(MapX, MapY); if (GameController.Food.Contains(MapPosition)) { return(true); } if (GameController.Eggs.TryGetValue(MapPosition, out int EggTier) && (EggTier < AvoidTier || AvoidTier == 0)) { return(true); } return(false); } else { return(true); } }
public void TeleporterGoIn(int itemId, RoomUser user) { FurniInfo item = GetFurniObject(itemId); if (item != null) { if (item.Flags.IsTeleport) { if (TilesTouching(item.PosX, item.PosY, user.CurrentX, user.CurrentY) && PlayerCheck(item.PosX, item.PosY) == false) { user.IsMoving = true; user.TargetX = item.PosX; user.TargetY = item.PosY; user.AllowOveride = true; user.Path.Clear(); PathFinderNode node = new PathFinderNode(); node.X = item.PosX; node.Y = item.PosY; user.Path.Add(node); } } } }
public List <PathFinderNode> FindPath(int StartX, int StartY, int EndX, int EndY) { PathFinderNode StartNode = grid[StartY - MapOffset.y, StartX - MapOffset.x]; PathFinderNode EndNode = grid[EndY - MapOffset.y, EndX - MapOffset.x]; OpenList = new List <PathFinderNode> { StartNode }; ClosedList = new List <PathFinderNode>(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { PathFinderNode PathNode = grid[y, x]; PathNode.GCost = int.MaxValue; PathNode.CalculateFCost(); PathNode.CameFromNode = null; } } StartNode.GCost = 0; StartNode.HCost = CalculateDistanceCost(StartNode, EndNode); StartNode.CalculateFCost(); while (OpenList.Count > 0) { PathFinderNode CurrentNode = GetLowestFCostNode(OpenList); if (CurrentNode == EndNode) { return(CalculatePath(EndNode)); } OpenList.Remove(CurrentNode); ClosedList.Add(CurrentNode); foreach (PathFinderNode NeighborNode in GetNeighorNodes(CurrentNode)) { if (ClosedList.Contains(NeighborNode)) { continue; } if (!IsNodeOpen(NeighborNode)) { if (!ClosedList.Contains(NeighborNode)) { ClosedList.Add(NeighborNode); } continue; } else { // Debug.Log("Open Path"); } int TentativeGCost = CurrentNode.GCost + 1; if (TentativeGCost < NeighborNode.GCost) { NeighborNode.CameFromNode = CurrentNode; NeighborNode.GCost = TentativeGCost; NeighborNode.HCost = CalculateDistanceCost(NeighborNode, EndNode); NeighborNode.CalculateFCost(); } if (!OpenList.Contains(NeighborNode)) { OpenList.Add(NeighborNode); } } } return(null); }
private bool ignoreThisNodeForWidth(PathFinderNode nextNode) { float maxLength = maxLenghtCentroidVertex(); return(false); }
public List<PathFinderNode> FindPath(Point s,Point e) { s=Clamp(s); e=Clamp(e); int si=GetArrayIndex(s); int ei=GetArrayIndex(e); Console.WriteLine("FindPath: Initializing Dijkstra with {0}*{1}={2}",chan.Width,chan.Height,chan.Width*chan.Height); Dijkstra p = new Dijkstra( chan.Width*chan.Height, this.getInternodeTraversalCost, this.nearbyNodesHint); Console.WriteLine("FindPath: Locating path from {0} to {1}.",s,e); int[] PointPath=p.GetMinimumPath(si,ei); List<PathFinderNode> Path=new List<PathFinderNode>(); foreach(int pi in PointPath) { Point pt = GetPointFromArrayIndex(pi); PathFinderNode pfn=new PathFinderNode(); pfn.X=pt.X; pfn.Y=pt.Y; Path.Add(pfn); } return Path; }
public static PathFinderNode FindPathReversed(RoomUser User, bool Diag, Gamemap Map, Vector2D Start, Vector2D End) { var OpenList = new MinHeap<PathFinderNode>(256); var PfMap = new PathFinderNode[Map.Model.MapSizeX, Map.Model.MapSizeY]; PathFinderNode Node; Vector2D Tmp; int Cost; int Diff; var Current = new PathFinderNode(Start); Current.Cost = 0; var Finish = new PathFinderNode(End); PfMap[Current.Position.X, Current.Position.Y] = Current; OpenList.Add(Current); while (OpenList.Count > 0) { Current = OpenList.ExtractFirst(); Current.InClosed = true; for (int i = 0; Diag ? i < DiagMovePoints.Length : i < NoDiagMovePoints.Length; i++) { Tmp = Current.Position + (Diag ? DiagMovePoints[i] : NoDiagMovePoints[i]); bool IsFinalMove = (Tmp.X == End.X && Tmp.Y == End.Y); if (Map.IsValidStep(new Vector2D(Current.Position.X, Current.Position.Y), Tmp, IsFinalMove, User.AllowOverride)) { if (PfMap[Tmp.X, Tmp.Y] == null) { Node = new PathFinderNode(Tmp); PfMap[Tmp.X, Tmp.Y] = Node; } else { Node = PfMap[Tmp.X, Tmp.Y]; } if (!Node.InClosed) { Diff = 0; if (Current.Position.X != Node.Position.X) { Diff += 1; } if (Current.Position.Y != Node.Position.Y) { Diff += 1; } Cost = Current.Cost + Diff + Node.Position.GetDistanceSquared(End); if (Cost < Node.Cost) { Node.Cost = Cost; Node.Next = Current; } if (!Node.InOpen) { if (Node.Equals(Finish)) { Node.Next = Current; return Node; } Node.InOpen = true; OpenList.Add(Node); } } } } } return null; }
public static PathFinderNode FindPathReversed(RoomUser User, bool Diag, Gamemap Map, Vector2D Start, Vector2D End) { MinHeap<PathFinderNode> minHeap = new MinHeap<PathFinderNode>(256); PathFinderNode[,] array = new PathFinderNode[Map.Model.MapSizeX, Map.Model.MapSizeY]; PathFinderNode pathFinderNode = new PathFinderNode(Start); pathFinderNode.Cost = 0; PathFinderNode breadcrumb = new PathFinderNode(End); array[pathFinderNode.Position.X, pathFinderNode.Position.Y] = pathFinderNode; minHeap.Add(pathFinderNode); checked { while (minHeap.Count > 0) { pathFinderNode = minHeap.ExtractFirst(); pathFinderNode.InClosed = true; int num = 0; while (Diag ? (num < PathFinder.DiagMovePoints.Length) : (num < PathFinder.NoDiagMovePoints.Length)) { Vector2D vector2D = pathFinderNode.Position + (Diag ? PathFinder.DiagMovePoints[num] : PathFinder.NoDiagMovePoints[num]); bool endOfPath = vector2D.X == End.X && vector2D.Y == End.Y; if (Map.IsValidStep(User, new Vector2D(pathFinderNode.Position.X, pathFinderNode.Position.Y), vector2D, endOfPath, User.AllowOverride)) { PathFinderNode pathFinderNode2; if (array[vector2D.X, vector2D.Y] == null) { pathFinderNode2 = new PathFinderNode(vector2D); array[vector2D.X, vector2D.Y] = pathFinderNode2; } else { pathFinderNode2 = array[vector2D.X, vector2D.Y]; } if (!pathFinderNode2.InClosed) { int num2 = 0; if (pathFinderNode.Position.X != pathFinderNode2.Position.X) { num2++; } if (pathFinderNode.Position.Y != pathFinderNode2.Position.Y) { num2++; } int num3 = pathFinderNode.Cost + num2 + pathFinderNode2.Position.GetDistanceSquared(End); if (num3 < pathFinderNode2.Cost) { pathFinderNode2.Cost = num3; pathFinderNode2.Next = pathFinderNode; } if (!pathFinderNode2.InOpen) { if (pathFinderNode2.Equals(breadcrumb)) { pathFinderNode2.Next = pathFinderNode; return pathFinderNode2; } pathFinderNode2.InOpen = true; minHeap.Add(pathFinderNode2); } } } num++; } } return null; } }
public static List <PathFinderNode> FindPath(ref PathNode[,] Matrix, Position Start, Position End) { if (Start == null || End == null) { return(null); } var w = Matrix.GetLength(0); var h = Matrix.GetLength(1); try { Matrix[Start.X, Start.Y].IsBlock = false; Matrix[End.X, End.Y].IsBlock = false; var ClosedNodes = new bool[Matrix.GetUpperBound(0) + 1, Matrix.GetUpperBound(1) + 1]; var Stack = new List <PathFinderNode>(new[] { new PathFinderNode { X = Start.X, Y = Start.Y, Heuristic = 0 } }); var Heuristic = 0; PathFinderNode FinalNode = null; while ((FinalNode == null) && Stack.Count > 0) { var NewStack = new List <PathFinderNode>(); for (var i = 0; i < Stack.Count; i++) { if (Stack[i].Heuristic > Heuristic) { NewStack.Add(Stack[i]); continue; } if (Stack[i].X - 1 <= Matrix.GetUpperBound(0) && Stack[i].Y + 1 <= Matrix.GetUpperBound(1)) { if (Stack[i].X - 1 >= 0) { if (!ClosedNodes[Stack[i].X - 1, Stack[i].Y]) { if (!Matrix[Stack[i].X - 1, Stack[i].Y].IsBlock) { var LastNode = new PathFinderNode { LastNode = Stack[i].LastNode, X = Stack[i].X, Y = Stack[i].Y, Heuristic = Stack[i].Heuristic }; var NewNode = new PathFinderNode { X = Stack[i].X - 1, Y = Stack[i].Y, NextNode = null, Heuristic = LastNode.Heuristic + (byte)(Matrix[Stack[i].X, Stack[i].Y + 1].IsBlock ? 1 : 0) }; LastNode.NextNode = NewNode; NewNode.LastNode = LastNode; if (Stack[i].X - 1 == End.X && Stack[i].Y == End.Y) { FinalNode = NewNode; break; } ClosedNodes[Stack[i].X - 1, Stack[i].Y] = true; NewStack.Add(NewNode); } } } } if (Stack[i].X + 1 <= Matrix.GetUpperBound(0) && Stack[i].Y + 1 <= Matrix.GetUpperBound(1)) { if (Stack[i].X + 1 >= 0) { if (!ClosedNodes[Stack[i].X + 1, Stack[i].Y]) { if (!Matrix[Stack[i].X + 1, Stack[i].Y].IsBlock) { var LastNode = new PathFinderNode { LastNode = Stack[i].LastNode, X = Stack[i].X, Y = Stack[i].Y, Heuristic = Stack[i].Heuristic }; var NewNode = new PathFinderNode { X = Stack[i].X + 1, Y = Stack[i].Y, NextNode = null, Heuristic = LastNode.Heuristic + (byte)(Matrix[Stack[i].X, Stack[i].Y + 1].IsBlock ? 1 : 0) }; LastNode.NextNode = NewNode; NewNode.LastNode = LastNode; if (Stack[i].X + 1 == End.X && Stack[i].Y == End.Y) { FinalNode = NewNode; break; } ClosedNodes[Stack[i].X + 1, Stack[i].Y] = true; NewStack.Add(NewNode); } } } } if (Stack[i].Y - 1 <= Matrix.GetUpperBound(1)) { if (Stack[i].Y - 1 >= 0) { if (!ClosedNodes[Stack[i].X, Stack[i].Y - 1]) { if (!Matrix[Stack[i].X, Stack[i].Y - 1].IsBlock) { var LastNode = new PathFinderNode { LastNode = Stack[i].LastNode, X = Stack[i].X, Y = Stack[i].Y, Heuristic = Stack[i].Heuristic }; var NewNode = new PathFinderNode { X = Stack[i].X, Y = Stack[i].Y - 1, NextNode = null, Heuristic = LastNode.Heuristic + (byte)(Matrix[Stack[i].X, Stack[i].Y + 1].IsBlock ? 1 : 0) }; LastNode.NextNode = NewNode; NewNode.LastNode = LastNode; if (Stack[i].X == End.X && Stack[i].Y - 1 == End.Y) { FinalNode = NewNode; break; } ClosedNodes[Stack[i].X, Stack[i].Y - 1] = true; NewStack.Add(NewNode); } } } } if (Stack[i].Y + 1 <= Matrix.GetUpperBound(1)) { if (Stack[i].Y + 1 >= 0) { if (!ClosedNodes[Stack[i].X, Stack[i].Y + 1]) { var x = Stack[i].X; var y = Stack[i].Y + 1; var n = Matrix[x, y]; bool isblock = n.IsBlock; if (!isblock) { var LastNode = new PathFinderNode { LastNode = Stack[i].LastNode, X = Stack[i].X, Y = Stack[i].Y, Heuristic = Stack[i].Heuristic }; var NewNode = new PathFinderNode { X = Stack[i].X, Y = Stack[i].Y + 1, NextNode = null, Heuristic = LastNode.Heuristic + (byte)(Matrix[Stack[i].X, Stack[i].Y + 1].IsBlock ? 1 : 0) }; LastNode.NextNode = NewNode; NewNode.LastNode = LastNode; if (Stack[i].X == End.X && Stack[i].Y + 1 == End.Y) { FinalNode = NewNode; break; } ClosedNodes[Stack[i].X, Stack[i].Y + 1] = true; NewStack.Add(NewNode); } } } } } Heuristic++; Stack = NewStack; } if (FinalNode != null) { Stack = new List <PathFinderNode>(); while (FinalNode != null) { Stack.Add(FinalNode); FinalNode = FinalNode.LastNode; } Stack.Reverse(); return(Stack); } return(null); } catch { return(null); } }
/// <summary> /// Finds the path reversed. /// </summary> /// <param name="roomUserable">The user.</param> /// <param name="whatIsDiag">if set to <c>true</c> [diag].</param> /// <param name="gameLocalMap">The map.</param> /// <param name="startMap">The start.</param> /// <param name="endMap">The end.</param> /// <returns>PathFinderNode.</returns> public static PathFinderNode FindPathReversed(RoomUser roomUserable, bool whatIsDiag, Gamemap gameLocalMap, Vector2D startMap, Vector2D endMap) { MinHeap<PathFinderNode> minSpanTreeCost = new MinHeap<PathFinderNode>(256); PathFinderNode[,] pathFinderMap = new PathFinderNode[gameLocalMap.Model.MapSizeX, gameLocalMap.Model.MapSizeY]; PathFinderNode pathFinderStart = new PathFinderNode(startMap) {Cost = 0}; PathFinderNode pathFinderEnd = new PathFinderNode(endMap); pathFinderMap[pathFinderStart.Position.X, pathFinderStart.Position.Y] = pathFinderStart; minSpanTreeCost.Add(pathFinderStart); while (minSpanTreeCost.Count > 0) { pathFinderStart = minSpanTreeCost.ExtractFirst(); pathFinderStart.InClosed = true; for (int index = 0; (whatIsDiag ? (index < DiagMovePoints.Length ? 1 : 0) : (index < NoDiagMovePoints.Length ? 1 : 0)) != 0; index++) { Vector2D realEndPosition = pathFinderStart.Position + (whatIsDiag ? DiagMovePoints[index] : NoDiagMovePoints[index]); bool isEndOfPath = (realEndPosition.X == endMap.X) && (realEndPosition.Y == endMap.Y); if (gameLocalMap.IsValidStep(roomUserable, new Vector2D(pathFinderStart.Position.X, pathFinderStart.Position.Y), realEndPosition, isEndOfPath, roomUserable.AllowOverride)) { PathFinderNode pathFinderSecondNodeCalculation; if (pathFinderMap[realEndPosition.X, realEndPosition.Y] == null) { pathFinderSecondNodeCalculation = new PathFinderNode(realEndPosition); pathFinderMap[realEndPosition.X, realEndPosition.Y] = pathFinderSecondNodeCalculation; } else pathFinderSecondNodeCalculation = pathFinderMap[realEndPosition.X, realEndPosition.Y]; if (!pathFinderSecondNodeCalculation.InClosed) { int internalSpanTreeCost = 0; if (pathFinderStart.Position.X != pathFinderSecondNodeCalculation.Position.X) internalSpanTreeCost++; if (pathFinderStart.Position.Y != pathFinderSecondNodeCalculation.Position.Y) internalSpanTreeCost++; int loopTotalCost = pathFinderStart.Cost + internalSpanTreeCost + pathFinderSecondNodeCalculation.Position.GetDistanceSquared(endMap); if (loopTotalCost < pathFinderSecondNodeCalculation.Cost) { pathFinderSecondNodeCalculation.Cost = loopTotalCost; pathFinderSecondNodeCalculation.Next = pathFinderStart; } if (!pathFinderSecondNodeCalculation.InOpen) { if (pathFinderSecondNodeCalculation.Equals(pathFinderEnd)) { pathFinderSecondNodeCalculation.Next = pathFinderStart; return pathFinderSecondNodeCalculation; } pathFinderSecondNodeCalculation.InOpen = true; minSpanTreeCost.Add(pathFinderSecondNodeCalculation); } } } } } return null; }
public Vector3 ConvertVector3(PathFinderNode point) { return(ConvertVector3FromGrid(point.X, point.Y)); }
public static PathFinderNode FindPathReversed(RoomUser User, bool Diag, Gamemap Map, Vector2D Start, Vector2D End) { var OpenList = new MinHeap <PathFinderNode>(256); var PfMap = new PathFinderNode[Map.Model.MapSizeX, Map.Model.MapSizeY]; PathFinderNode Node; Vector2D Tmp; int Cost; int Diff; var Current = new PathFinderNode(Start) { Cost = 0 }; var Finish = new PathFinderNode(End); PfMap[Current.Position.X, Current.Position.Y] = Current; OpenList.Add(Current); while (OpenList.Count > 0) { Current = OpenList.ExtractFirst(); Current.InClosed = true; for (int i = 0; Diag?i < DiagMovePoints.Length : i < NoDiagMovePoints.Length; i++) { Tmp = Current.Position + (Diag ? DiagMovePoints[i] : NoDiagMovePoints[i]); bool IsFinalMove = (Tmp.X == End.X && Tmp.Y == End.Y); bool DiagMovement = (i == 0 || i == 2 || i == 4 || i == 6); if (Map.IsValidStep(new Vector2D(Current.Position.X, Current.Position.Y), Tmp, IsFinalMove, User.AllowOverride, DiagMovement)) { if (PfMap[Tmp.X, Tmp.Y] == null) { Node = new PathFinderNode(Tmp); PfMap[Tmp.X, Tmp.Y] = Node; } else { Node = PfMap[Tmp.X, Tmp.Y]; } if (!Node.InClosed) { Diff = 0; if (Current.Position.X != Node.Position.X) { Diff += 1; } if (Current.Position.Y != Node.Position.Y) { Diff += 1; } Cost = Current.Cost + Diff + Node.Position.GetDistanceSquared(End); if (Cost < Node.Cost) { Node.Cost = Cost; Node.Next = Current; } if (!Node.InOpen) { if (Node.Equals(Finish)) { Node.Next = Current; return(Node); } Node.InOpen = true; OpenList.Add(Node); } } } } } return(null); }
public static List <PathFinderNode> FindPath(int[,] Matrix, Position Start, Position End) { var w = Matrix.GetLength(0); var h = Matrix.GetLength(1); try { Matrix[Start.X, Start.Y] = 0; Matrix[End.X, End.Y] = 0; var ClosedNodes = new bool[Matrix.GetUpperBound(0) + 1, Matrix.GetUpperBound(1) + 1]; var Stack = new List <PathFinderNode>(new[] { new PathFinderNode { X = Start.X, Y = Start.Y, Heuristic = 0 } }); var Heuristic = 1; PathFinderNode FinalNode = null; while ((FinalNode == null) && Stack.Count > 0) { var NewStack = new List <PathFinderNode>(); for (var i = 0; i < Stack.Count; i++) { if (Stack[i].Heuristic > Heuristic) { NewStack.Add(Stack[i]); continue; } if (Stack[i].X - 1 <= Matrix.GetUpperBound(0)) { if (Stack[i].X - 1 >= 0) { if (!ClosedNodes[Stack[i].X - 1, Stack[i].Y]) { if (Matrix[Stack[i].X - 1, Stack[i].Y] != 1) { var LastNode = new PathFinderNode { LastNode = Stack[i].LastNode, X = Stack[i].X, Y = Stack[i].Y, Heuristic = Stack[i].Heuristic }; var NewNode = new PathFinderNode { X = Stack[i].X - 1, Y = Stack[i].Y, NextNode = null, Heuristic = LastNode.Heuristic + (byte)Matrix[Stack[i].X - 1, Stack[i].Y] }; LastNode.NextNode = NewNode; NewNode.LastNode = LastNode; if (Stack[i].X - 1 == End.X && Stack[i].Y == End.Y) { FinalNode = NewNode; break; } ClosedNodes[Stack[i].X - 1, Stack[i].Y] = true; NewStack.Add(NewNode); } } } } if (Stack[i].X + 1 <= Matrix.GetUpperBound(0)) { if (Stack[i].X + 1 >= 0) { if (!ClosedNodes[Stack[i].X + 1, Stack[i].Y]) { if (Matrix[Stack[i].X + 1, Stack[i].Y] != 1) { var LastNode = new PathFinderNode { LastNode = Stack[i].LastNode, X = Stack[i].X, Y = Stack[i].Y, Heuristic = Stack[i].Heuristic }; var NewNode = new PathFinderNode { X = Stack[i].X + 1, Y = Stack[i].Y, NextNode = null, Heuristic = LastNode.Heuristic + (byte)Matrix[Stack[i].X + 1, Stack[i].Y] }; LastNode.NextNode = NewNode; NewNode.LastNode = LastNode; if (Stack[i].X + 1 == End.X && Stack[i].Y == End.Y) { FinalNode = NewNode; break; } ClosedNodes[Stack[i].X + 1, Stack[i].Y] = true; NewStack.Add(NewNode); } } } } if (Stack[i].Y - 1 <= Matrix.GetUpperBound(1)) { if (Stack[i].Y - 1 >= 0) { if (!ClosedNodes[Stack[i].X, Stack[i].Y - 1]) { if (Matrix[Stack[i].X, Stack[i].Y - 1] != 1) { var LastNode = new PathFinderNode { LastNode = Stack[i].LastNode, X = Stack[i].X, Y = Stack[i].Y, Heuristic = Stack[i].Heuristic }; var NewNode = new PathFinderNode { X = Stack[i].X, Y = Stack[i].Y - 1, NextNode = null, Heuristic = LastNode.Heuristic + (byte)Matrix[Stack[i].X, Stack[i].Y - 1] }; LastNode.NextNode = NewNode; NewNode.LastNode = LastNode; if (Stack[i].X == End.X && Stack[i].Y - 1 == End.Y) { FinalNode = NewNode; break; } ClosedNodes[Stack[i].X, Stack[i].Y - 1] = true; NewStack.Add(NewNode); } } } } if (Stack[i].Y + 1 <= Matrix.GetUpperBound(1)) { if (Stack[i].Y + 1 >= 0) { if (!ClosedNodes[Stack[i].X, Stack[i].Y + 1]) { if (Matrix[Stack[i].X, Stack[i].Y + 1] != 1) { var LastNode = new PathFinderNode { LastNode = Stack[i].LastNode, X = Stack[i].X, Y = Stack[i].Y, Heuristic = Stack[i].Heuristic }; var NewNode = new PathFinderNode { X = Stack[i].X, Y = Stack[i].Y + 1, NextNode = null, Heuristic = LastNode.Heuristic + (byte)Matrix[Stack[i].X, Stack[i].Y + 1] }; LastNode.NextNode = NewNode; NewNode.LastNode = LastNode; if (Stack[i].X == End.X && Stack[i].Y + 1 == End.Y) { FinalNode = NewNode; break; } ClosedNodes[Stack[i].X, Stack[i].Y + 1] = true; NewStack.Add(NewNode); } } } } } Heuristic++; Stack = NewStack; } if (FinalNode != null) { Stack = new List <PathFinderNode>(); while (FinalNode != null) { Stack.Add(FinalNode); FinalNode = FinalNode.LastNode; } Stack.Reverse(); return(Stack); } return(null); } catch { System.Console.WriteLine("Map Dimensions are f****d."); return(null); } }
/// <summary> /// Finds the path reversed. /// </summary> /// <param name="RoomUserable">The user.</param> /// <param name="WhatIsDiag">if set to <c>true</c> [diag].</param> /// <param name="GameLocalMap">The map.</param> /// <param name="StartMap">The start.</param> /// <param name="EndMap">The end.</param> /// <returns>PathFinderNode.</returns> public static PathFinderNode FindPathReversed(RoomUser RoomUserable, bool WhatIsDiag, Gamemap GameLocalMap, Vector2D StartMap, Vector2D EndMap) { MinHeap<PathFinderNode> MinSpanTreeCost = new MinHeap<PathFinderNode>(256); PathFinderNode[,] PathFinderMap = new PathFinderNode[GameLocalMap.Model.MapSizeX, GameLocalMap.Model.MapSizeY]; PathFinderNode PathFinderStart = new PathFinderNode(StartMap) { Cost = 0 }; PathFinderNode PathFinderEnd = new PathFinderNode(EndMap); PathFinderMap[PathFinderStart.Position.X, PathFinderStart.Position.Y] = PathFinderStart; MinSpanTreeCost.Add(PathFinderStart); int loop_variable_one, InternalSpanTreeCost, loop_total_cost; while (MinSpanTreeCost.Count > 0) { PathFinderStart = MinSpanTreeCost.ExtractFirst(); PathFinderStart.InClosed = true; loop_variable_one = 0; while ((WhatIsDiag ? (loop_variable_one < DiagMovePoints.Length) : (loop_variable_one < NoDiagMovePoints.Length))) { Vector2D RealEndPosition = PathFinderStart.Position + (WhatIsDiag ? DiagMovePoints[loop_variable_one] : NoDiagMovePoints[loop_variable_one]); bool IsEndOfPath = ((RealEndPosition.X == EndMap.X) && (RealEndPosition.Y == EndMap.Y)); if (GameLocalMap.IsValidStep(RoomUserable, new Vector2D(PathFinderStart.Position.X, PathFinderStart.Position.Y), RealEndPosition, IsEndOfPath, RoomUserable.AllowOverride)) { PathFinderNode PathFinderSecondNodeCalculation; if (PathFinderMap[RealEndPosition.X, RealEndPosition.Y] == null) { PathFinderSecondNodeCalculation = new PathFinderNode(RealEndPosition); PathFinderMap[RealEndPosition.X, RealEndPosition.Y] = PathFinderSecondNodeCalculation; } else { PathFinderSecondNodeCalculation = PathFinderMap[RealEndPosition.X, RealEndPosition.Y]; } if (!PathFinderSecondNodeCalculation.InClosed) { InternalSpanTreeCost = 0; if (PathFinderStart.Position.X != PathFinderSecondNodeCalculation.Position.X) InternalSpanTreeCost++; if (PathFinderStart.Position.Y != PathFinderSecondNodeCalculation.Position.Y) InternalSpanTreeCost++; loop_total_cost = PathFinderStart.Cost + InternalSpanTreeCost + PathFinderSecondNodeCalculation.Position.GetDistanceSquared(EndMap); if (loop_total_cost < PathFinderSecondNodeCalculation.Cost) { PathFinderSecondNodeCalculation.Cost = loop_total_cost; PathFinderSecondNodeCalculation.Next = PathFinderStart; } if (!PathFinderSecondNodeCalculation.InOpen) { if (PathFinderSecondNodeCalculation.Equals(PathFinderEnd)) { PathFinderSecondNodeCalculation.Next = PathFinderStart; return PathFinderSecondNodeCalculation; } PathFinderSecondNodeCalculation.InOpen = true; MinSpanTreeCost.Add(PathFinderSecondNodeCalculation); } } } loop_variable_one++; } } return null; }