public static List<Tile> FindPath(Tile originTile, Tile destinationTile, Vector2[] occupied) { List<Tile> closed = new List<Tile>(); List<TilePath> open = new List<TilePath>(); TilePath originPath = new TilePath(); originPath.addTile(originTile); open.Add(originPath); while (open.Count > 0) { //open = open.OrderBy(x => x.costOfPath).ToList(); TilePath current = open[0]; open.Remove(open[0]); if (closed.Contains(current.lastTile)) { continue; } if (current.lastTile == destinationTile) { current.listOfTiles.Distinct(); current.listOfTiles.Remove(originTile); return current.listOfTiles; } closed.Add(current.lastTile); foreach (Tile t in current.lastTile.neighbors) { if (t.impassible || occupied.Contains(t.gridPosition)) continue; TilePath newTilePath = new TilePath(current); newTilePath.addTile(t); open.Add(newTilePath); } } return null; }
public static List<Tile> FindHighlight(Tile originTile, int movementPoints, Vector2[] occupied) { List<Tile> closed = new List<Tile>(); List<TilePath> open = new List<TilePath>(); TilePath originPath = new TilePath(); originPath.addTile(originTile); open.Add(originPath); while (open.Count > 0) { TilePath current = open[0]; open.Remove(open[0]); if (closed.Contains(current.lastTile)) { continue; } if (current.costOfPath > movementPoints + 1) { continue; } closed.Add(current.lastTile); foreach (Tile t in current.lastTile.neighbours) { if (t.impassible || occupied.Contains(t.gridPosition)) continue; TilePath newTilePath = new TilePath(current); newTilePath.addTile(t); open.Add(newTilePath); } } closed.Remove(originTile); closed.Distinct(); return closed; }
public static List<Tile> FindPath(Tile originTile, Tile destinationTile) { List<Tile> closed = new List<Tile>(); List<TilePath> open = new List<TilePath>(); TilePath originPath = new TilePath(); originPath.addTile(originTile); open.Add(originPath); while (open.Count > 0) { TilePath current = open[0]; open.Remove(open[0]); if (closed.Contains(current.lastTile)) { continue; } if (current.lastTile == destinationTile) { current.listOfTiles.Remove (originTile); return current.listOfTiles; } closed.Add(current.lastTile); foreach (Tile t in current.lastTile.neighbors) { if (t.impassible) continue; TilePath newTilePath = new TilePath(current); newTilePath.addTile(t); open.Add(newTilePath); } } return null; }
void UpdateLocomotionBidsAndPath() { //foreach (var pair in bidTotals) // bidTotals[pair.Key] = 0; bidTotals.Clear(); elRoot.WalkTree(SConcerns, this.updateConcernBids); GameObject winner = null; float winningBid = 0; foreach (var pair in bidTotals) { if (pair.Value > winningBid) { winningBid = pair.Value; winner = pair.Key; } } if (winner != null) { // Replan if destination has changed or if destination has moved away from current path. var newDestination = (winner != CurrentDestination && winner != CurrentlyDockedWith); if (newDestination || (currentDestination != null && currentPath != null && !CurrentDestination.DockingTiles().Contains(currentPath.FinalTile))) { if (newDestination) { ELNode.Store(eventHistory / new Structure("goto", winner)); // Log change for debugging purposes. } this.CurrentDestination = winner; this.currentPath = planner.Plan(gameObject.TilePosition(), this.CurrentDestination.DockingTiles()); } } }
public static List <Tile> FindHighlight(Tile originTile, int movementPoints, Vector2[] occupiedTile, bool attacking) { List <Tile> closed = new List <Tile>(); List <TilePath> open = new List <TilePath>(); TilePath originPath = new TilePath(); if (attacking) { originPath.addTileAttack(originTile); } else { originPath.addTile(originTile); } open.Add(originPath); while (open.Count > 0) { TilePath current = open[0]; open.Remove(open[0]); if (closed.Contains(current.lastTile)) { continue; } if (current.costOfPath > movementPoints + 1) { continue; } closed.Add(current.lastTile); foreach (Tile t in current.lastTile.neighbors) { if (t.impassible || occupiedTile.Contains(t.gridPosition)) { continue; } TilePath newTilePath = new TilePath(current); if (attacking) { newTilePath.addTileAttack(t); open.Add(newTilePath); } else { newTilePath.addTile(t); open.Add(newTilePath); } } } closed.Remove(originTile); closed.Distinct(); return(closed); }
public IEnumerator MoveFighter(int time, bool canMove, Map map, int who, TilePath tp) { // if (canMove && tp != null) { // int pathSpots = tp.path.Count; // if (GM.maxSimSpeed) { yield return(new WaitForEndOfFrame()); } else { yield return(new WaitForSeconds(1 / GM.battleSpd)); } fighter[who].ChangeAnimation("Walk"); // for (int i = 0; i < pathSpots; i++) { fighter[who].MoveTo(time, map, tp.path[i].expression); if (GM.maxSimSpeed) { yield return(new WaitForEndOfFrame()); } else { yield return(new WaitForSeconds(1 / GM.battleSpd)); } } fighter[who].ChangeAnimation("Idle"); fighter[who].LookAtOpponent(); } ResetAllTiles(); if (GM.maxSimSpeed) { yield return(new WaitForEndOfFrame()); } else { yield return(new WaitForSeconds(2.5f / GM.battleSpd)); } // map.fC.StartABattle(who); }
public override void RemovePath() { OverworldTilePath overworldTilePath = (OverworldTilePath)_tile.GetBackgrounds().FirstOrDefault(background => background is OverworldTilePath); if (overworldTilePath == null) { return; } Logger.Log(overworldTilePath.TilePathType); IPathType overworldTilePathType = overworldTilePath.TilePathType; int oldConnectionScore = overworldTilePath.ConnectionScore; // If needed, place a background in the gap that the removed path left if (oldConnectionScore == NeighbourTileCalculator.ConnectionOnAllSidesScore) { EditorOverworldTileBackgroundPlacer tileBackgroundPlacer = new EditorOverworldTileBackgroundPlacer(_tile); tileBackgroundPlacer.PlaceBackground <OverworldTileBaseGround>(); } _tile.RemoveBackground(overworldTilePath); overworldTilePath.Remove(); //After removing tile, check with neighbour tiles if wall connections should be updated foreach (KeyValuePair <ObjectDirection, Tile> neighbour in _tile.Neighbours) { if (!neighbour.Value) { continue; } TilePath overworldTilePathOnNeighbour = neighbour.Value.TryGetTilePath(); if (overworldTilePathOnNeighbour == null) { continue; } int oldConnectionScoreOnNeighbour = overworldTilePathOnNeighbour.ConnectionScore; Logger.Warning($"We will now look for connections for neighbour {neighbour.Value.GridLocation.X},{neighbour.Value.GridLocation.Y}, which is {neighbour.Key} of {_tile.GridLocation.X},{_tile.GridLocation.Y}"); TileConnectionScoreInfo overworldTilePathConnectionScoreOnNeighbourInfo = NeighbourTileCalculator.MapNeighbourPathsOfTile(neighbour.Value, overworldTilePathType); Logger.Log($"We calculated an path connection type score of {overworldTilePathConnectionScoreOnNeighbourInfo.RawConnectionScore} for location {neighbour.Value.GridLocation.X}, {neighbour.Value.GridLocation.Y}"); //update connection score on neighbour overworldTilePathOnNeighbour.WithConnectionScoreInfo(overworldTilePathConnectionScoreOnNeighbourInfo); //Add background where needed if (oldConnectionScoreOnNeighbour == NeighbourTileCalculator.ConnectionOnAllSidesScore && overworldTilePathConnectionScoreOnNeighbourInfo.RawConnectionScore != NeighbourTileCalculator.ConnectionOnAllSidesScore) { EditorOverworldTileBackgroundPlacer tileBackgroundPlacer = new EditorOverworldTileBackgroundPlacer(neighbour.Value as EditorOverworldTile); tileBackgroundPlacer.PlaceBackground <OverworldTileBaseGround>(); } } }
public static List<Tile> FindPath(Tile originTile, Tile destinationTile) { // Closed List will contain the path as we build it List<Tile> closed = new List<Tile>(); // Open List will contain the available tiles List<TilePath> open = new List<TilePath>(); TilePath originPath = new TilePath(); originPath.addTile(originTile); open.Add(originPath); while (open.Count > 0) { TilePath current = open[0]; open.Remove(open[0]); //Debug.Log("Tile removed"); if (closed.Contains(current.lastTile)) { continue; } if (current.lastTile == destinationTile) { current.listOfTiles.Distinct(); current.listOfTiles.Remove(originTile); Debug.Log("This is logged"); Debug.Log("Number of Tiles is:" + current.listOfTiles.Count()); // 2 tiles return (current.listOfTiles); Debug.Log("This is not logged"); } closed.Add(current.lastTile); foreach (Tile t in current.lastTile.neighbors) { if (!t.isPassable) continue; TilePath newTilePath = new TilePath(current); newTilePath.addTile(t); open.Add(newTilePath); } } Debug.Log("This is also logged"); Debug.Log("And the function returns null"); return null; }
public static List<Tile> FindHighlight(Tile originTile, int movementPoints, Vector2[] occupied, bool staticRange, bool dontremoveorigin) { List<Tile> closed = new List<Tile>(); List<TilePath> open = new List<TilePath>(); TilePath originPath = new TilePath(); if (staticRange) originPath.addStaticTile(originTile); else originPath.addTile(originTile); open.Add(originPath); while (open.Count > 0) { TilePath current = open[0]; open.Remove(open[0]); if (closed.Contains(current.lastTile)) { continue; } if (current.costOfPath > movementPoints + 1.5f) { continue; } closed.Add(current.lastTile); foreach (Tile t in current.lastTile.neighbors) { if (t.impassible || occupied.Contains(t.gridPosition)|| (GameManager.instance.GetComponent<GameManager>(). playerTurns[GameManager.instance.GetComponent<GameManager>().PlayerTurnIndex].GetPhalanx()==true&&t.frontLiners<16)) continue; TilePath newTilePath = new TilePath(current); if (staticRange) newTilePath.addStaticTile(t); else newTilePath.addTile(t); open.Add(newTilePath); } } closed.Remove(originTile); closed.Distinct(); if (dontremoveorigin && !occupied.Contains(originTile.gridPosition)) closed.Add(originTile); return closed; }
/* public static List<Tile> FindHighlight(Tile originTile, int movementPoints) { return FindHighlight(originTile, movementPoints, new Vector2[0], false); } public static List<Tile> FindHighlight(Tile originTile, int movementPoints, bool staticRange) { return FindHighlight(originTile, movementPoints, new Vector2[0], staticRange); } public static List<Tile> FindHighlight(Tile originTile, int movementPoints, Vector2[] occupied) { return FindHighlight(originTile, movementPoints, occupied, false); } */ public static List<Tile> FindHighlight(Tile originTile, int movementPoints) { List<Tile> closed = new List<Tile>(); List<TilePath> open = new List<TilePath>(); TilePath originPath = new TilePath(); //if (staticRange) originPath.addStaticTile(originTile); //else originPath.addTile(originTile); open.Add(originPath); while (open.Count > 0) { TilePath current = open[0]; open.Remove(open[0]); if (closed.Contains(current.lastTile)) { continue; } // each tile cost is 1, could change this later if (1 > movementPoints + 1) { continue; } closed.Add(current.lastTile); foreach (Tile t in current.lastTile.neighbors) { if (!t.isPassable || t.isOccupied) continue; TilePath newTilePath = new TilePath(current); //if (staticRange) newTilePath.addStaticTile(t); //else newTilePath.addTile(t); open.Add(newTilePath); } } closed.Remove(originTile); closed.Distinct(); return closed; }
// this method is different because it needs to find a path between impassible tiles. public static List<Tile> FindPathBetweenRooms(Tile originTile, Tile destinationTile, List<List<Tile>> map) { List<Tile> closed = new List<Tile>(); List<TilePath> open = new List<TilePath>(); TilePath originPath = new TilePath(); originPath.addTile(originTile); open.Add(originPath); while (open.Count > 0) { TilePath current = open[0]; open.Remove(open[0]); if (closed.Contains(current.lastTile)) { continue; } if (current.lastTile == destinationTile) { current.listOfTiles.Distinct(); current.listOfTiles.Remove(originTile); return current.listOfTiles; } closed.Add(current.lastTile); current.lastTile.generateNeighbours(map); Debug.Log (current.lastTile.gridPosition + " with nb of " + current.lastTile.neighbours.Count); foreach (Tile t in current.lastTile.neighbours) { TilePath newTilePath = new TilePath(current); newTilePath.addTile(t); open.Add(newTilePath); } } return null; }
///// <summary> ///// Find the shortest path using Dijkstra's algorithm ///// </summary> //public TilePath Dijkstra(TilePosition start, TileRect end) //{ // heap.Clear(); // searchedTilesOverlay.Clear(); // var startNode = heap.NodeAt(start); // startNode.Predecessor = null; // heap.DecreaseKey(startNode, 0, 0); // var currentNode = startNode; // while (!heap.IsEmpty && !end.Contains((currentNode = heap.ExtractMin()).Position)) // { // searchedTilesOverlay.Add(currentNode.Position); // foreach (var n in currentNode.Neighbors) // { // float newDistanceFromStart = currentNode.DistanceFromStart // + TilePosition.EuclideanDistance(currentNode.Position, n.Position); // if (n.DistanceFromStart > newDistanceFromStart) // { // n.Predecessor = currentNode; // heap.DecreaseKey(n, newDistanceFromStart, newDistanceFromStart); // } // } // } // heap.SetOverlayToComputedPath(this.computedPathOverlay, currentNode); // if (!end.Contains(currentNode.Position)) // return null; // return this.MakePath(currentNode); //} /// <summary> /// Make a path object from the nodes from the search. /// Starts at the end node and tracks backward, following predecessor links, until it finds the start node. /// </summary> /// <param name="endNode">The ending node of the path</param> /// <returns>The reconstructed path</returns> private TilePath MakePath(TileHeap.Node endNode) { // Reconstruct the path to from the start to the end. var path = new TilePath(endNode.Position); while (endNode != null) { path.AddBefore(endNode.Position.TileCenter); endNode = endNode.Predecessor; } return path; }
void Awake() { mGUIManager = gameObject.GetComponent<GUIManager>(); tileActive = null; if (idFile && tileMapFile) { mTileMap = new TileMap(idFile, tileMapFile, "Prefabs/Tiles/"); mTileMap.Start(); mLoadSuccess = mTileMap.GetLoadSuccess(); if (pathOneFile && mLoadSuccess) { mPath = new TilePath(pathOneFile, mTileMap.GetRows(), mTileMap.GetColumns()); mPath.Start(); mLoadSuccess = mPath.GetLoadSuccess(); } if (pathTwoFile && mLoadSuccess) { mPathTwo = new TilePath(pathTwoFile, mTileMap.GetRows(), mTileMap.GetColumns()); mPathTwo.Start(); mLoadSuccess = mPathTwo.GetLoadSuccess(); } } else mLoadSuccess = false; mTileWidth = mTileMap.GetTileSize(); mTileHeight = mTileMap.GetTileHeight(); }
void UpdateLocomotionBidsAndPath() { //foreach (var pair in bidTotals) // bidTotals[pair.Key] = 0; bidTotals.Clear(); elRoot.WalkTree(SConcerns, this.updateConcernBids); GameObject winner = null; float winningBid = 0; foreach (var pair in bidTotals) if (pair.Value > winningBid) { winningBid = pair.Value; winner = pair.Key; } if (winner != null) { // Replan if destination has changed or if destination has moved away from current path. var newDestination = (winner != CurrentDestination && winner != CurrentlyDockedWith); if (newDestination || (currentDestination != null && currentPath != null && !CurrentDestination.DockingTiles().Contains(currentPath.FinalTile))) { if (newDestination) ELNode.Store(eventHistory / new Structure("goto", winner)); // Log change for debugging purposes. this.CurrentDestination = winner; this.currentPath = planner.Plan(gameObject.TilePosition(), this.CurrentDestination.DockingTiles()); } } }
private void UpdateLocomotion() { this.UpdateLocomotionBidsAndPath(); if (CurrentlyDockedWith != null && !CurrentlyDockedWith.DockingTiles().Contains(this.transform.position)) { // We were docked with an object, but are not anymore. perceptionRoot.DeleteKey(SDockedWith); CurrentlyDockedWith = null; } if (this.currentPath != null) { // Update the steering if (this.currentPath.UpdateSteering(this.steering) || (Vector2.Distance(this.transform.position, currentDestination.transform.position) < 0.75 && currentDestination.IsCharacter())) { // Finished the path this.CurrentlyDockedWith = CurrentDestination; ELNode.Store(perceptionRoot/SDockedWith%CurrentlyDockedWith); ELNode.Store(lastDestination % this.CurrentDestination); this.currentPath = null; this.currentDestination = null; (motorRoot / SWalkingTo).DeleteSelf(); this.Face(CurrentlyDockedWith); this.steering.Stop(); this.QueueEvent("arrived_at", this.CurrentlyDockedWith); } } }
public void moveCurrentUnit(Tile destTile) { if (selected.moved || !possibleMoveTiles.Contains (destTile)) return; TilePath path = new TilePath (selected.currentTile, selected.movement, destTile); selected.currentTile.occupant = null; selected.currentTile.transform.GetComponent<Renderer>().material.color = Tile.defaultColor; selected.path = path; selected.moveDestination = path.getNext().transform.position + new Vector3(0, selected.transform.position.y, 0); destTile.occupant = selected; destTile.transform.GetComponent<Renderer>().material.color = selected.player.color; selected.currentTile = destTile; selected.moved = true; deselect (); }
private EdgeConnection GetParentTileEdgeConnection(TilePath currentPathTile) { if (currentPathTile.ParentTile == null) return null; //ParentTile is Above Current Tile if (currentPathTile.ParentTile.TileLocation.Y < currentPathTile.TileLocation.Y) return new EdgeConnection((byte)rnd.Next(7, 9),true); //Parent Tile is below current tile else if (currentPathTile.ParentTile.TileLocation.Y > currentPathTile.TileLocation.Y) return new EdgeConnection((byte)rnd.Next(1, 3),true); //Parent tile is to the left of current tile else if (currentPathTile.ParentTile.TileLocation.X < currentPathTile.TileLocation.X) return new EdgeConnection((byte)rnd.Next(10, 12),true); //Parent tile is to the right of current tile else return new EdgeConnection((byte)rnd.Next(4, 6)); }
/// <summary> /// Determines which directions have open tiles available for the path. /// </summary> /// <param name="currentTile"></param> /// <param name="targetTile"></param> /// <param name="openTiles"></param> /// <param name="closedTiles"></param> /// <returns></returns> private List<TilePath> GetOpenPathTiles(TilePath currentTile, Point targetTile, List<TilePath> openTiles, List<TilePath> closedTiles) { List<Point> openPoints = new List<Point>(); //North Tile if (currentTile.TileLocation.Y - 1 >= 0 && !closedTiles.Exists(i => i.TileLocation.X == currentTile.TileLocation.X && i.TileLocation.Y == currentTile.TileLocation.Y - 1)) openPoints.Add(new Point(currentTile.TileLocation.X, currentTile.TileLocation.Y - 1)); //south Tile if (currentTile.TileLocation.Y + 1 < _gameWorld.GameMap.GetLength(1) && !closedTiles.Exists(i => i.TileLocation.X == currentTile.TileLocation.X && i.TileLocation.Y == currentTile.TileLocation.Y + 1)) openPoints.Add(new Point(currentTile.TileLocation.X, currentTile.TileLocation.Y + 1)); //East Tile if (currentTile.TileLocation.X - 1 >= 0 && !closedTiles.Exists(i => i.TileLocation.X == currentTile.TileLocation.X - 1 && i.TileLocation.Y == currentTile.TileLocation.Y)) openPoints.Add(new Point(currentTile.TileLocation.X - 1, currentTile.TileLocation.Y)); //West Tile if (currentTile.TileLocation.X + 1 < _gameWorld.GameMap.GetLength(0) && !closedTiles.Exists(i => i.TileLocation.X == currentTile.TileLocation.X + 1 && i.TileLocation.Y == currentTile.TileLocation.Y)) openPoints.Add(new Point(currentTile.TileLocation.X + 1, currentTile.TileLocation.Y)); List<TilePath> tiles = new List<TilePath>(); foreach (Point location in openPoints) { int score = CalculateScore(currentTile.ParentTile, location, targetTile); //If it doesnt exist on the open list we want to create a new item. if (!openTiles.Exists(i => i.TileLocation.X == location.X && i.TileLocation.Y == location.Y)) { TilePath potentialPathTile = new TilePath(location, score, currentTile); tiles.Add(potentialPathTile); } else { TilePath existingTile = openTiles.First(i => i.TileLocation.X == location.X && i.TileLocation.Y == location.Y); existingTile.ParentTile = currentTile; existingTile.Score = score; } } return tiles; }
/// <summary> /// Finds the best path between two tiles that are not connected. /// </summary> /// <param name="startTilePoint"></param> /// <param name="targetTilePoint"></param> /// <returns></returns> private TilePath FindPath(Point startTilePoint, Point targetTilePoint) { TilePath currentTile = new TilePath(startTilePoint, 10); List<TilePath> openTiles = new List<TilePath>(); List<TilePath> closedTiles = new List<TilePath>(); while (true) { closedTiles.Add(currentTile); openTiles.AddRange(GetOpenPathTiles(currentTile, targetTilePoint, openTiles,closedTiles)); if (openTiles.Count == 0) return null; openTiles.Sort(); currentTile = openTiles[0]; openTiles.RemoveAt(0); if (currentTile.TileLocation == targetTilePoint) return currentTile; } }
/// <summary> /// Calculates the path score. /// </summary> /// <param name="parentTile"></param> /// <param name="location"></param> /// <param name="targetTile"></param> /// <returns></returns> private int CalculateScore(TilePath parentTile, Point location, Point targetTile) { //Path Score F = G + H //G = the Cost to move plus the Parents cost to mvoe //H = the estimated cost to the targetTile //Using the Manhatten method to calculate the H oost int G = 10; if (parentTile != null) G+=parentTile.Score; int H = 0; if (location.X >= targetTile.X) H += location.X - targetTile.X; else H+= targetTile.X - location.X; if (location.Y >= targetTile.Y) H += location.Y - targetTile.Y; else H+= targetTile.Y - location.Y; return G + H; }
/// <summary> /// Generates the tiles that occur along a path that has been found between two tiles that do not connect. /// </summary> /// <param name="bestPath"></param> private void BuildPath(TilePath bestPath) { TilePath currentPathTile = bestPath; EdgeConnection parentEdgeConnection = null; Dictionary<Point,MapGraphicsTile> tilesToAdd = new Dictionary<Point,MapGraphicsTile>(); while (true) { MapGraphicsTile currentMapTile; //create a new mapTile if (_noNeighborTiles.ContainsKey(currentPathTile.TileLocation)) { currentMapTile = _noNeighborTiles[currentPathTile.TileLocation]; //foreach (EdgeConnection edgeConnection in currentMapTile.ShoreEdgePoints) // SetNeighborEdgeConnections(new KeyValuePair<Point, MapGraphicsTile>(currentPathTile.TileLocation, _edgeTiles[currentPathTile.TileLocation]), edgeConnection); //get rid of all of the edgepoints that are not a match, as we cannot used them. //currentMapTile.ShoreEdgePoints.RemoveAll(i => i.IsConnected == false); } else currentMapTile = new MapGraphicsTile(); //add the previous parent translated to they connect if (parentEdgeConnection != null) currentMapTile.ShoreEdgePoints.Add(new EdgeConnection(parentEdgeConnection.TranslateConnectionForNeighbor(), true)); //Get the parentEdgeConnection parentEdgeConnection = GetParentTileEdgeConnection(currentPathTile); if (parentEdgeConnection != null) currentMapTile.ShoreEdgePoints.Add(parentEdgeConnection); tilesToAdd.Add(currentPathTile.TileLocation, currentMapTile); //if the current tile doesnt have a parentTile we are done. if (currentPathTile.ParentTile == null) break; currentPathTile = currentPathTile.ParentTile; } foreach (KeyValuePair<Point,MapGraphicsTile> mapGraphicsTile in tilesToAdd) { SetTileEdgeTypeByNeighbor(mapGraphicsTile); MapTile mapTile = _mapGraphicsTileSet.GetMatchingTile(mapGraphicsTile.Value); if (mapTile.GraphicsTile.TileType != TileType.Error) _gameWorld.GameMap[mapGraphicsTile.Key.X, mapGraphicsTile.Key.Y] = mapTile; } }
public void SetPath(TilePath path) { mPath = path; }
public TilePath(TilePath tp) { listOfTiles = tp.listOfTiles.ToList(); costOfPath = tp.costOfPath; lastTile = tp.lastTile; }