Reconfiguration bugfixes and code cleanup

This commit is contained in:
Dawid Pietrykowski 2022-09-08 22:48:36 +02:00
parent c8d049b5ac
commit 0274947e44
2 changed files with 31 additions and 35 deletions

View File

@ -2758,7 +2758,7 @@ MonoBehaviour:
mainPlanContainer: {fileID: 557435656}
emergencyPlanContainer: {fileID: 632826360}
reconfigurationToggle: {fileID: 441283505}
defaultPath: Assets/Data/Tablica3.csv
defaultPath: Assets/Data/Tablica1.csv
--- !u!1 &1347097428
GameObject:
m_ObjectHideFlags: 0

View File

@ -41,10 +41,10 @@ namespace Parking
{new List<Spot>(), new List<Spot>(), new List<Spot>(), new List<Spot>()};
private bool _emergencyActivated;
private bool _reconfigurationActive = true;
private int _initialConfigurationSpotCount;
private int[,] _initialSolution;
private bool _reconfigurationActive = true;
private int[] _rejectedDrivers = {0, 0, 0, 0};
private List<GameObject> _spotPrefabs;
@ -317,7 +317,7 @@ namespace Parking
Debug.Log($"Needed spots = {neededSpots[0]} {neededSpots[1]} {neededSpots[2]} {neededSpots[3]}");
Debug.Log("Attempting reconfiguration...");
List<Driver> spotlessCars = new List<Driver>();
List<Driver> spotlessCars = new();
// Filter drivers by assigning free space
foreach (Driver nextCar in nextCars)
@ -363,7 +363,8 @@ namespace Parking
newPosition.y, newPosition.z);
}
else {
float newX = GetNextBorderHorizontal(position, spot.Lane, spot.Perpendicular, newSize, true) - 2.25f / 2.0f;
float newX = GetNextBorderHorizontal(position, spot.Lane, spot.Perpendicular, newSize, true) -
2.25f / 2.0f;
if (spot.Lane == 0) {
float xRelativeToRight = newX - Width / 2.0f - 2.25f / 2.0f;
xRelativeToRight -= (xRelativeToRight + 2.25f / 2.0f) % 2.25f;
@ -378,13 +379,14 @@ namespace Parking
newPosition = new Vector3(newX,
newPosition.y, newPosition.z);
}
}else if (spot.Size < newSize) {
}
else if (spot.Size < newSize) {
float spotBorder;
float thisSizeOffset;
if (spot.Perpendicular)
thisSizeOffset = _spotHeights[(int) spot.Size] / 2.0f;
thisSizeOffset = _spotHeights[size] / 2.0f;
else
thisSizeOffset = 2.25f;
thisSizeOffset = 2.25f / 2.0f;
if (!spot.AlignToTop)
spotBorder = position.y + thisSizeOffset;
else
@ -393,12 +395,13 @@ namespace Parking
float nextBorder = GetNextBorderVectical(position, spot.Lane, spot.Perpendicular, newSize,
!spot.AlignToTop);
if (Math.Abs(spotBorder - nextBorder) < 5.5f)
continue; // Try parallel
continue; // Try parallel (?)
}
else {
float nextBorderLeft = GetNextBorderHorizontal(position, spot.Lane, spot.Perpendicular, newSize,
!spot.AlignToTop);
float nextBorderRight = GetNextBorderHorizontal(position, spot.Lane, spot.Perpendicular, newSize,
float nextBorderRight = GetNextBorderHorizontal(position, spot.Lane, spot.Perpendicular,
newSize,
!spot.AlignToTop);
if (nextBorderRight - nextBorderLeft < _spotHeights[(int) newSize]) {
// Try perpendicular
@ -410,33 +413,27 @@ namespace Parking
float nextBorder = GetNextBorderVectical(position, spot.Lane, spot.Perpendicular,
newSize,
!spot.AlignToTop);
if (Math.Abs(nextBorder - newBottomBorder) < 5.5f) {
if (Math.Abs(nextBorder - newBottomBorder) < 5.5f)
continue;
}
}
else {
float newBottomBorder = bottomBorder;
float newTopBorder = bottomBorder + _spotHeights[size];
float nextBorder = GetNextBorderVectical(position, spot.Lane, spot.Perpendicular,
newSize,
!spot.AlignToTop);
if (Math.Abs(newTopBorder - nextBorder) < 5.5f) {
if (Math.Abs(newTopBorder - nextBorder) < 5.5f)
continue;
}
}
newPosition.y = bottomBorder + _spotHeights[(int) newSize] / 2.0f;
float newX = nextBorderRight;
if (spot.Lane == 0) {
// float xRelativeToRight = newX - Width / 2.0f;
// xRelativeToRight -= (xRelativeToRight + 2.25f / 2.0f) % 2.25f;
// newX = xRelativeToRight + Width / 2.0f;
float xRelativeToRight = -(newX - Width / 2.0f);
// xRelativeToRight -= (xRelativeToRight - 2.25f / 2.0f) % 2.25f;
xRelativeToRight -= (xRelativeToRight + 2.25f / 2.0f) % 2.25f;
xRelativeToRight += 2.25f;
xRelativeToRight = Math.Max(xRelativeToRight, 2.25f / 2.0f);
newX = (-xRelativeToRight + Width / 2.0f);
newX = -xRelativeToRight + Width / 2.0f;
}
else {
float xRelativeToLeft = newX + Width / 2.0f;
@ -465,6 +462,7 @@ namespace Parking
spot.LastReconfiguration = _currentTime;
return true;
}
return false;
}
@ -500,7 +498,7 @@ namespace Parking
private float GetNextBorderHorizontal(Vector3 position, int lane, bool perpendicular, Size size, bool right)
{
List<float> allBorders = new List<float>();
List<float> allBorders = new();
List<Spot> targetLane = _spotMap[lane];
foreach (Spot spot1 in targetLane) {
if (spot1.GameObject.transform.position == position)
@ -550,7 +548,7 @@ namespace Parking
private List<Driver> GetNextCars(int steps)
{
List<Driver> nextCars = new List<Driver>();
List<Driver> nextCars = new();
TimeSpan updatedTime = _currentTime + TimeSpan.FromMinutes(stepTime * steps);
foreach (Driver driver in DataImporter.Drivers)
if (updatedTime <= driver.Times[1].TimeOfDay &&
@ -575,9 +573,7 @@ namespace Parking
if (spot.Size != Size.D)
maxP2 = Math.Max(maxP2, spot.Perpendicular ? _spotHeights[(int) spot.Size] : 2.25f);
// float maxP3 = spotMap[2].Count == 0 ? 0 : _spotHeights[(int) spotMap[2].Max().Size];
// float maxP2 = spotMap[1].Count == 0 ? 0 : _spotHeights[(int) spotMap[1].Max().Size];
List<GameObject> prefabs = new List<GameObject> {spotPrefabA, spotPrefabB, spotPrefabC, spotPrefabD};
List<GameObject> prefabs = new() {spotPrefabA, spotPrefabB, spotPrefabC, spotPrefabD};
for (int i = 0; i < 4; i++) {
float currentY;
@ -607,12 +603,10 @@ namespace Parking
bool flipped = false;
for (int j = 0; j < spotMap[i].Count; j++) {
// if (!spotMap[i][j].Perpendicular)
// currentX -= spotSizes[(int) spotMap[i][j].Size] / 2.0f;
// else if (spotMap[i][j].Perpendicular)
// currentX -= 2.25f / 2.0f;
currentX += (i != 0 ? 1 : -1) *
(spotMap[i][j].Perpendicular ? 2.25f / 2.0f : spotSizes[(int) spotMap[i][j].Size] / 2.0f);
(spotMap[i][j].Perpendicular
? 2.25f / 2.0f
: spotSizes[(int) spotMap[i][j].Size] / 2.0f);
spotMap[i][j].Flipped = flipped;
bool alignTop = i % 2 != 0;
@ -650,7 +644,9 @@ namespace Parking
spotMap[i][j].Lane = i;
currentX += (i != 0 ? 1 : -1) *
(spotMap[i][j].Perpendicular ? 2.25f / 2.0f : spotSizes[(int) spotMap[i][j].Size] / 2.0f);
(spotMap[i][j].Perpendicular
? 2.25f / 2.0f
: spotSizes[(int) spotMap[i][j].Size] / 2.0f);
flipped = !flipped;
}