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} mainPlanContainer: {fileID: 557435656}
emergencyPlanContainer: {fileID: 632826360} emergencyPlanContainer: {fileID: 632826360}
reconfigurationToggle: {fileID: 441283505} reconfigurationToggle: {fileID: 441283505}
defaultPath: Assets/Data/Tablica3.csv defaultPath: Assets/Data/Tablica1.csv
--- !u!1 &1347097428 --- !u!1 &1347097428
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

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