Rework profile deletion

- O(n) instead of O(4n)
- Actually free the sacrificed profile's memory
- Don't hop to profile 1 after deleting a later profile, to reduce the % of footguns/losing your place in the list.
This commit is contained in:
toaster 2022-08-31 22:52:06 +01:00
parent c77f9fa558
commit b0c9fccd2d
2 changed files with 32 additions and 15 deletions

View file

@ -5518,8 +5518,8 @@ static void M_EraseProfileResponse(INT32 choice)
F_StartIntro(); F_StartIntro();
M_ClearMenus(true); M_ClearMenus(true);
} }
else else if (optionsmenu.eraseprofilen > PR_GetNumProfiles()-1)
optionsmenu.eraseprofilen = 1; optionsmenu.eraseprofilen--;
} }
} }

View file

@ -96,34 +96,47 @@ profile_t* PR_GetProfile(INT32 num)
boolean PR_DeleteProfile(INT32 num) boolean PR_DeleteProfile(INT32 num)
{ {
UINT8 i, j; UINT8 i;
profile_t* sacrifice;
if (num <= 0 || num > numprofiles) if (num <= 0 || num > numprofiles)
{ {
return false; return false;
} }
sacrifice = profilesList[num];
// If we're deleting inbetween profiles, move everything. // If we're deleting inbetween profiles, move everything.
if (num < numprofiles) if (num < numprofiles)
{ {
for (i = num; i < numprofiles-1; i++) for (i = num; i < numprofiles-1; i++)
{ {
profilesList[i] = profilesList[i+1]; profilesList[i] = profilesList[i+1];
}
// Make sure to move cv_lastprofile values as well // Make sure to move cv_lastprofile values as well!
for (j = 0; j < MAXSPLITSCREENPLAYERS; j++) for (i = 0; i < MAXSPLITSCREENPLAYERS; i++)
{ {
if (cv_lastprofile[j].value == num) INT32 profileset = cv_lastprofile[i].value;
if (profileset < num)
{ {
// If we were on the deleted profile, default back to guest. // Not affected.
CV_StealthSetValue(&cv_lastprofile[j], PROFILE_GUEST); continue;
} }
else if (cv_lastprofile[j].value == i+1)
if (profileset > num)
{ {
// Otherwise, shift our lastprofile number down to match the new order. // Shift our lastprofile number down to match the new order.
CV_StealthSetValue(&cv_lastprofile[j], cv_lastprofile[j].value-1); profileset--;
} }
else
{
// There's no hope for it. If we were on the deleted profile, default back to guest.
profileset = PROFILE_GUEST;
} }
CV_StealthSetValue(&cv_lastprofile[i], profileset);
} }
} }
@ -132,6 +145,10 @@ boolean PR_DeleteProfile(INT32 num)
numprofiles--; numprofiles--;
PR_SaveProfiles(); PR_SaveProfiles();
// Finally, clear up our memory!
Z_Free(sacrifice);
return true; return true;
} }