public void NewPath(Entity owner, MovementBehavior movement, List <LayerTile> path, bool recalculate) { if (!recalculate) { movement.SetPath(path); //We synchro the path for other to visualize it var message = networkService.CreateServerMessage(); message.Write(MOVE); message.Write(owner.Name); //if the path was accepted, we will send it path = movement.path; message.Write(path.Count); foreach (LayerTile tile in path) { message.Write(tile.X); message.Write(tile.Y); Trace.WriteLine("X: " + tile.X + ", Y:" + tile.Y); } networkService.SendToClients(message, DeliveryMethod.ReliableOrdered); } else { movement.ForcePath(path); } }
protected override void Update(TimeSpan gameTime) { if (pathSet) { if (start != end) { if (wo.GetAction() == ActionEnum.Move) { updatedTiles.UnionWith(fog.updatedTiles); if (gScore[start] == float.PositiveInfinity || move.FullStop()) { //there is not a known path Reset(); } else { LayerTile tile = Map.map.GetTileByWorldPosition(wo.GetCenteredPosition()); if (recalculate) { recalculate = false; float minVal = float.PositiveInfinity; LayerTile nextTile = null; foreach (Point neighborPoint in fog.Adjacents(start, adjPoints)) { LayerTile s = map.GetTileByMapCoordinates(neighborPoint.X, neighborPoint.Y); if (minVal > rhs[s]) { minVal = rhs[s]; nextTile = s; } } start = nextTile; km += HeuristicCostEstimate(last, start); last = start; foreach (LayerTile s in updatedTiles) { foreach (Point neighborPoint in fog.Adjacents(s, adjPoints)) { LayerTile u = map.GetTileByMapCoordinates(neighborPoint.X, neighborPoint.Y); UpdateVertex(u); } UpdateVertex(s); } updatedTiles.Clear(); ComputeShortestPath(); List <LayerTile> tiles = ReconstructPath(); move.ForcePath(tiles); } else if (last != tile) { //If we already visited this tile, we punish going back this way each time harder if (visitedTiles.ContainsKey(last)) { visitedTiles[last] = visitedTiles[last] * 3; } else { visitedTiles.Add(last, 1); } recalculate = true; recalculate = true; } } } } else { Reset(); } } }