/// <summary> /// Choose an elevator to make the trip /// No new elevator will be assigned during this method, but elevators may become available /// </summary> /// <param name="startFloor"></param> /// <param name="endFloor"></param> /// <returns></returns> public Elevator AssignElevator(int startFloor, int endFloor) { // Find all of the available elevators IEnumerable <Elevator> availableElevators = Elevators.Where(e => e.IsAvailable); if (availableElevators.Count() == 0) { // No elevators are available try again later return(null); } Elevator assignedElevator = availableElevators.First(); int floorDistance = Math.Abs(startFloor - assignedElevator.CurrentFloor); // When an elevator request is made, the unoccupied elevator closest to it will answer the call foreach (Elevator current in availableElevators) { int currentDistance = Math.Abs(startFloor - current.CurrentFloor); if (currentDistance < floorDistance) { floorDistance = currentDistance; assignedElevator = current; } if (currentDistance == 0) { return(assignedElevator); } } return(assignedElevator); }
/// <summary> /// Removes all elements in scope from the instance. /// </summary> /// <param name="theTier">The tier to remove the elements from.</param> /// <param name="xMin">The min x-value of the scope.</param> /// <param name="xMax">The max x-value of the scope.</param> /// <param name="yMin">The min y-value of the scope.</param> /// <param name="yMax">The max y-value of the scope.</param> public void ModRemoveWaypoints(ITierInfo theTier, double xMin, double yMin, double xMax, double yMax) { Tier tier = theTier as Tier; // Remove waypoints List <Waypoint> remWaypoints = Waypoints.Where(w => w.Tier == tier && xMin <= w.X && w.X <= xMax && yMin <= w.Y && w.Y <= yMax).ToList(); // Remove all of them foreach (var waypoint in remWaypoints) { // Remove the waypoint - the waypoint graph handles all cascading path removals waypoint.Tier.RemoveWaypoint(waypoint); // Remove all elements that were connected to the waypoint foreach (var guard in Semaphores.SelectMany(s => s.Guards).Where(guard => guard.From == waypoint || guard.To == waypoint).ToArray()) { guard.Semaphore.UnregisterGuard(guard); if (guard.Semaphore.Guards.Count() == 0) { Semaphores.Remove(guard.Semaphore); } } foreach (var station in InputStations.Where(s => s.Waypoint == waypoint).ToArray()) { station.Tier.RemoveInputStation(station); InputStations.Remove(station); } foreach (var station in OutputStations.Where(s => s.Waypoint == waypoint).ToArray()) { station.Tier.RemoveOutputStation(station); OutputStations.Remove(station); } foreach (var pod in Pods.Where(p => p.Waypoint == waypoint).ToArray()) { pod.Tier.RemovePod(pod); Pods.Remove(pod); } foreach (var elevator in Elevators.Where(e => e.ConnectedPoints.Contains(waypoint))) { elevator.UnregisterPoint(waypoint); if (elevator.ConnectedPoints.Count == 0) { Elevators.Remove(elevator); } } // Make sure it is not on the list of storage locations anymore if (waypoint.PodStorageLocation) { ResourceManager.RemovePodStorageLocation(waypoint); } // Finally remove from the overall waypoint list Waypoints.Remove(waypoint); } // Also remove movables in scope List <Bot> remBots = Bots.Where(b => b.Tier == tier && xMin <= b.X && b.X <= xMax && yMin <= b.Y && b.Y <= yMax).ToList(); foreach (var bot in remBots) { Bots.Remove(bot); bot.Tier.RemoveBot(bot); } List <Pod> remPods = Pods.Where(p => p.Tier == tier && xMin <= p.X && p.X <= xMax && yMin <= p.Y && p.Y <= yMax).ToList(); foreach (var pod in remPods) { Pods.Remove(pod); pod.Tier.RemovePod(pod); } }