/// <summary> /// If a vehicle is approaching a vehicle ahead of them, returns the index of the vehicle ahead /// </summary> /// <param name="index">Index in the list of current vehicle</param> /// <param name="vehicles">Sorted list of vehicles on the road</param> /// <returns></returns> private int CarAheadIndex(int index, List <Vehicle> vehicles) { int positionDifference = 0; int drivingLane = vehicles[index].GetLane(); int position = vehicles[index].GetPosition(); PersonalityHandler handler = new PersonalityHandler(vehicles[index].GetDriver().GetPersonality()); int drivingGap = handler.GetDrivingGap(); for (int i = 0; i < vehicles.Count; i++) { //look for the next vehicle in the same lane excluding this vehicle //is possible that vehicle initially behind passes current vehicle then //is slowed by traffic ahead if (index != i) { if (vehicles[i].GetLane() == drivingLane && vehicles[i].GetPosition() > position) {//get the first vehicle that is in the same lane and ahead of current vehicle positionDifference = vehicles[i].GetPosition() - position; if (positionDifference <= drivingGap) { return(i); } break; } } } return(-1); //Compare this car to the car ahead of it }
private bool CanMerge(Vehicle vehicle, List <Vehicle> vehicles) { int gapBehind = 0; int gapAhead = 0; int vehicleBehindPosition = 0; int vehicleAheadPosition = 0; int vehicleBehindSize = 0; int vehicleAheadSize = 0; Vehicle vehicleBehind; Vehicle vehicleAhead; int position = vehicle.GetPosition(); PersonalityHandler handler = new PersonalityHandler(vehicle.GetDriver().GetPersonality()); int drivingGap = handler.GetDrivingGap(); int size = vehicle.GetSize(); if (vehicles[0].GetPosition() <= position) //first vehicle is behind our current car { vehicleBehind = vehicles[0]; vehicleBehindPosition = vehicleBehind.GetPosition(); vehicleBehindSize = vehicleBehind.GetSize(); gapBehind = position - size - drivingGap - vehicleBehindPosition; } else //there is no vehicle behind our current vehicle { vehicleAhead = vehicles[0]; vehicleAheadPosition = vehicleAhead.GetPosition(); vehicleAheadSize = vehicleAhead.GetSize(); gapAhead = vehicleAheadPosition - vehicleAheadSize - drivingGap - position; gapBehind = 1; } if (vehicles.Count == 2) //there is a vehicle ahead of and behind our current vehicle { vehicleAhead = vehicles[1]; vehicleAheadPosition = vehicleAhead.GetPosition(); vehicleAheadSize = vehicleAhead.GetSize(); gapAhead = vehicleAheadPosition - vehicleAheadSize - drivingGap - position; } if (gapAhead > 0 && gapBehind > 0) { return(true); } return(false); }
/// <summary> /// Computes the next available position the specified vehicle type could be placed in the specified lane /// </summary> /// <param name="vehicles">List of Vehicles Sorted by Increasing Position</param> /// <param name="type">Type of Vehicle to be created</param> /// <param name="drivingLane">Lane of the Vehicle to be created</param> /// <returns>Integer value of the first available position for a vehicle in the specified lane</returns> private int NextAvailablePosition(List <Vehicle> vehicles, Type type, int drivingLane) { TypeHandler handler = new TypeHandler(); int nextAvailablePosition; int vehiclePosition = 0; PersonalityHandler personalityHandler; int drivingGap = 0; Vehicle tempVehicle; int vehicleSize = handler.GetSize(type); int count = 0; for (int i = vehicles.Count - 1; i >= 0; i--) { count = 0; tempVehicle = vehicles[i]; if (tempVehicle.GetLane() == drivingLane) { count++; vehiclePosition = tempVehicle.GetPosition(); personalityHandler = new PersonalityHandler(tempVehicle.GetDriver().GetPersonality()); drivingGap = personalityHandler.GetDrivingGap(); //Get driving gap of the car behind break; //finds the last car in the same lane and breaks out of the loop } } if (count == 0) //no vehicles in the specified lane { nextAvailablePosition = 0; return(nextAvailablePosition); } else { nextAvailablePosition = vehiclePosition + drivingGap; return(nextAvailablePosition); } }