private RouteCell NextDirection(Route route, Route.Specs routeSpecs) { List <RouteCell> availableDirections = this.AvailableDirections(route); // flatten collection to iterate while removing item IEnumerable <RouteCell> returnList = availableDirections.ToList(); foreach (RouteCell cell in returnList) { if (cell.Row == this.ReachCell.Row && cell.Column == this.ReachCell.Column) { // return reach cell only if available Write.Trace($"reach cell is available direction : {cell}"); return(cell); } if (routeSpecs.HasFlag(Route.Specs.Opposite) && (cell.Direction != this.Directions.VerticalDirection && cell.Direction != this.Directions.HorizontalOpposite)) { availableDirections.Remove(cell); } else if (!routeSpecs.HasFlag(Route.Specs.Opposite) && cell.Direction != this.Directions.VerticalDirection && cell.Direction != this.Directions.HorizontalDirection) { availableDirections.Remove(cell); } } return(this.FilterAvailableDirections(availableDirections, route, routeSpecs)); }
/// <summary>Gets route corresponding to routeSpecs</summary> /// <param name="routeSpecs">route specifications</param> /// <returns>Route corresponding to specs</returns> public Route MapRoute(Route.Specs routeSpecs) { return(this.MapRoute(new List <Route.Specs>() { routeSpecs })); }
private Route GetRoute(IEnumerable <Route.Specs> routesSpecs) { foreach (Route.Specs currentRouteSpecs in routesSpecs) { Route route = new Route(this.StartCell); Route.Specs routeSpecs = currentRouteSpecs; Write.Trace($"map {routeSpecs} route, from {this.StartCell} to {this.ReachCell}"); while ((route.ReachCell.Row != this.ReachCell.Row || route.ReachCell.Column != this.ReachCell.Column) && route != null) { RouteCell nextDirection = this.NextDirection(route, routeSpecs); if (nextDirection != null) { RouteCell nextDirectionCell = nextDirection; route.AddCell(nextDirectionCell, this.Grid); if (route.PacketsCount == route.MaxPackets && routeSpecs.HasFlag(Route.Specs.Route)) { routeSpecs &= ~Route.Specs.Route; routeSpecs |= Route.Specs.Free; } else if (route.PacketsCount > route.MaxPackets) { Write.Trace($"break : route.PacketsCount > route.MaxPackets {route}"); route.Reset(); route = null; break; } } else { Write.Trace($"break : next direction = null {route}"); route.Reset(); route = null; break; } } if (route != null) { return(route); } } return(null); }
/// <summary>Gets first route corresponding to routeSpecs list</summary> /// <param name="routeSpecs">route specifications</param> /// <returns>Route corresponding to specs</returns> public Route MapBubbleRoute(Route.Specs routeSpecs) { return(this.MapBubbleRoute(new List <Route.Specs> { routeSpecs })); }
private RouteCell FilterAvailableDirections(IEnumerable <RouteCell> nextDirections, Route route, Route.Specs routeSpecs) { IEnumerable <RouteCell> returnList = null; // filter available directions according to route specs if (nextDirections.Any()) { if (routeSpecs.HasFlag(Route.Specs.Route)) { // return packet cells if available, otherwise route cells returnList = from i in nextDirections where i.IsPacket == true && i.WillBreakDelivery == false select i; if (!returnList.Any()) { returnList = from i in nextDirections where i.IsRoute == true && i.IsPacket == false select i; } if (!returnList.Any()) { returnList = from i in nextDirections where i.IsStartRoute == true && i.IsPacket == false select i; } } else if (routeSpecs.HasFlag(Route.Specs.Free)) { // return free cells if available, otherwise route cells returnList = from i in nextDirections where i.IsFree == true select i; if (!returnList.Any()) { returnList = from i in nextDirections where i.IsPacket == false || i.WillBreakDelivery == false select i; } } else if (routeSpecs.HasFlag(Route.Specs.All)) { // return all availables directions returnList = from i in nextDirections where i.IsPacket == false || i.WillBreakDelivery == false select i; } if ((returnList == null || !returnList.Any()) && (routeSpecs.HasFlag(Route.Specs.Dodge) == true || routeSpecs.HasFlag(Route.Specs.Wait))) { if (routeSpecs.HasFlag(Route.Specs.Dodge) == true) { RouteCell packetCell = (from i in nextDirections where i.WillBreakDelivery == true orderby i.Distance select i).FirstOrDefault(); // try to dodge cell or wait if distance gap is lower than 3 step return(this.DodgeCell(packetCell, nextDirections, ref route, routeSpecs)); } else if (routeSpecs.HasFlag(Route.Specs.Wait)) { // insert a wait step on route start return(RouteCell.WaitCell(this.StartCell)); } } else { if (routeSpecs.HasFlag(Route.Specs.Alternative) && returnList.Count() > 1) { // Alternative returnList = returnList.Skip(1); } else { // Route | Free | All following specs returnList = returnList.Take(1); } } return(returnList.FirstOrDefault()); } else { return(null); } }
// Ooii private RouteCell DodgeCell(RouteCell packetCell, IEnumerable <RouteCell> nextDirections, ref Route route, Route.Specs routeSpecs) { Packet packet = this.Grid.GetPacket(packetCell); IEnumerable <RouteCell> alternativeCells = from i in this.AvailableDirections(route) where !nextDirections.Where(x => x.Row != i.Row && x.Column != i.Column).Any() select i; Write.Trace($"packet {packet} route.Distance + 1 {route.Distance + 1} - packet.Distance {packet.Distance}) > 2"); if (packet.Distance - (route.Distance + 1) > 2) { route.AddCell(packetCell, this.Grid); RouteCell routeCell = this.NextDirection(route, Route.Specs.All); RouteCell otherRouteCell = this.NextDirection(route, Route.Specs.All | Route.Specs.Alternative); route.RemoveLastCell(); List <Path> paths = new List <Path>(); if (routeCell != null) { paths.Add(new Path(routeCell, this.Grid, alternativeCells.ElementAt(0))); paths.Add(new Path(routeCell, this.Grid, alternativeCells.ElementAt(1))); } if (otherRouteCell != null) { paths.Add(new Path(otherRouteCell, this.Grid, alternativeCells.ElementAt(0))); paths.Add(new Path(otherRouteCell, this.Grid, alternativeCells.ElementAt(1))); } if (paths.Any()) { List <Route> routes = new List <Route>(); foreach (Path path in paths) { Route newRoute = path.MapRoute(new List <Route.Specs>() { Route.Specs.All, Route.Specs.All | Route.Specs.Alternative }); if (newRoute != null) { routes.Add(newRoute); } } Route selectedRoute = (from i in routes orderby i.Distance select i).FirstOrDefault(); if (selectedRoute != null) { route.AddRoute(selectedRoute); return(this.NextDirection(route, routeSpecs)); } else { return(null); } } else { return(null); } } else { // insert a wait step on route start return(RouteCell.WaitCell(this.StartCell)); } }