Time-based reservation improvements, bugfixes, added spacing between regenerated spots

This commit is contained in:
Dawid Pietrykowski 2022-10-11 23:35:25 +02:00
parent 932c7f9c17
commit 75841d7a41
2 changed files with 52 additions and 19 deletions

View File

@ -62,6 +62,7 @@ namespace Parking
public bool Reserved = false;
public int ReservedPriority = 0;
public Size Size;
public Driver OccupyingDriver;
public Spot(Size size, bool flipped)
{

View File

@ -141,7 +141,7 @@ namespace Parking
driver.Reserved = false;
}
float rightTime = 10.0f;//(float)_currentTime.TotalHours;
float rightTime = 12.0f;//(float)_currentTime.TotalHours;
float leftTime = 16.0f;//(float)(_currentTime + TimeSpan.FromHours(hours)).TotalHours;
TimeSpan maxArrivalTime = _currentTime + TimeSpan.FromHours(hours);
@ -151,25 +151,31 @@ namespace Parking
nextDrivers.Add(driver);
}
}
// nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.PlannedDeparture, b.PlannedDeparture)));
nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.PlannedArrival, b.PlannedArrival)));
nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.PlannedDeparture, b.PlannedDeparture)));
//nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.RealDeparture, b.RealDeparture)));
//nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.PlannedArrival, b.PlannedArrival)));
List<Spot> availableSpots = new List<Spot>();
foreach (List<Spot> list in _spotMap)
foreach (Spot spot in list)
if (!spot.Reserved && spot.Free)
// if (!spot.Reserved && spot.Free)
availableSpots.Add(spot);
// availableSpots.Reverse();
// leftTime = (float)nextDrivers.Max(d => d.PlannedDeparture).TotalHours;
Driver FindOccupyingDriver(Spot spot)
{
return spot.OccupyingDriver;
}
for (int i = 0; i < nextDrivers.Count; i++) {
Driver driver = nextDrivers[i];
// float driverTime = (float)driver.PlannedDeparture.TotalHours;
float driverTime = (float)driver.RealDeparture.TotalHours;
float driverTime = (float)driver.PlannedDeparture.TotalHours;
//float driverTime = (float)driver.RealDeparture.TotalHours;
float timeProgress = (driverTime - rightTime) / (leftTime - rightTime);
float xPosition = Width / 2.0f - (Width * timeProgress);
availableSpots.Sort(((a, b) => Comparer<float>.Default.Compare(Mathf.Abs(xPosition - a.Position.x), Mathf.Abs(xPosition - b.Position.x))));
@ -177,7 +183,30 @@ namespace Parking
bool useBottomLane = true;
for (int j = 0; j < availableSpots.Count; j++) {
Spot spot = availableSpots[j];
if (driver.Size == spot.Size && !spot.Reserved && (spot.Lane != 0 || useBottomLane)) {
if (driver.Size == spot.Size && (spot.Lane != 0)) {
TimeSpan leaveTime = spot.Free ? TimeSpan.Zero : FindOccupyingDriver(spot).PlannedDeparture;
if (((!spot.Reserved && spot.Free)
|| (!spot.Free &&
leaveTime <
driver.PlannedArrival))) {
spot.Reserved = true;
driver.Reserved = true;
driver.ReservedSpot = spot;
break;
}
}
}
// if (Mathf.Abs(spot.Position.x - xPosition) > 20.0f) {
// if (useBottomLane)
// break;
// // useBottomLane = true;
// // j = -1;
// }
if(!driver.Reserved)
for (int j = 0; j < availableSpots.Count; j++) {
Spot spot = availableSpots[j];
if (driver.Size == spot.Size && !spot.Reserved) {
spot.Reserved = true;
driver.Reserved = true;
driver.ReservedSpot = spot;
@ -337,20 +366,20 @@ namespace Parking
private bool FindReservedSpot(Driver driver, out Spot spot)
{
if (driver.Reserved) {
if(!driver.ReservedSpot.Free)
Debug.Log("Spot should be free");
if(driver.ReservedSpot.GameObject == null || !driver.ReservedSpot.Free)
Debug.Log("Spot not viable anymore");
else {
spot = driver.ReservedSpot;
return true;
}
}
foreach (List<Spot> list in _spotMap)
foreach (Spot spot1 in list)
if (spot1.Reserved && spot1.ReservedPriority == driver.Priority && driver.Size == spot1.Size) {
spot = spot1;
return true;
}
// foreach (List<Spot> list in _spotMap)
// foreach (Spot spot1 in list)
// if (spot1.Reserved && spot1.ReservedPriority == driver.Priority && driver.Size == spot1.Size) {
// spot = spot1;
// return true;
// }
spot = new Spot();
@ -399,6 +428,7 @@ namespace Parking
}
spot.Free = false;
spot.Reserved = false;
spot.OccupyingDriver = driver;
driver.Spot = spot;
driver.Parked = true;
driver.GameObject = Instantiate(carPrefab);
@ -553,13 +583,14 @@ namespace Parking
}
// Try regeneration before returning false
return RegenerateSpots(newSize);
if (RegenerateSpots(newSize))
return true;
}
foreach (List<Spot> currentLaneSpots in _spotMap)
for (int i = 0; i < currentLaneSpots.Count; i++) {
Spot spot = currentLaneSpots[i];
if (!(spot.Free && spot.Size != Size.D)) // && !spot.Reserved))
if (!spot.Free || spot.Size == Size.D)// || spot.Reserved)
continue;
if (GetSpotCountOfSize(Size.A) <= 2 && spot.Size == Size.A)
continue;
@ -756,7 +787,7 @@ namespace Parking
Vector3 newPosition = Vector3.zero;
float lastRightBorder = -Width / 2.0f + 5.5f;
float spaceNeeded = SpotHeights[(int)size];
float spaceNeeded = SpotHeights[(int)size] + 1.0f;
@ -792,7 +823,8 @@ namespace Parking
if (leftBorder - lastRightBorder >= spaceNeeded) {
Spot newSpot = new Spot(size, false);
newPosition.x = (lastRightBorder + SpotHeights[(int)size] / 2.0f);
//newPosition.x = (lastRightBorder + SpotHeights[(int)size] / 2.0f);
newPosition.x = (lastRightBorder + 0.5f + SpotHeights[(int)size] / 2.0f);
newPosition.y = (-Height / 2.0f) + 2.25f / 2.0f;
newSpot.Perpendicular = false;
newSpot.GameObject = Instantiate(_spotPrefabs[(int)size],