Fix menu where cv_usejoystick.value > I_JoyNum and selecting an unused controller and the "used!" prompt pops up erroneously

This commit is contained in:
mazmazz 2018-12-14 05:57:10 -05:00
parent b2c02838c4
commit 70d6845d6c
2 changed files with 49 additions and 40 deletions

View file

@ -6792,15 +6792,6 @@ void M_SetupJoystickMenu(INT32 choice)
strcpy(joystickInfo[i], "None"); strcpy(joystickInfo[i], "None");
// Hotplugging breaks if this block is run
// Because the cvar is set to 0, which disables controllers for that player
#if 0 // #ifdef JOYSTICK_HOTPLUG
if (0 == cv_usejoystick.value)
CV_SetValue(&cv_usejoystick, 0);
if (0 == cv_usejoystick2.value)
CV_SetValue(&cv_usejoystick2, 0);
#endif
for (i = 1; i < 8; i++) for (i = 1; i < 8; i++)
{ {
if (i <= n && (I_GetJoyName(i)) != NULL) if (i <= n && (I_GetJoyName(i)) != NULL)
@ -6845,54 +6836,68 @@ static void M_AssignJoystick(INT32 choice)
{ {
#ifdef JOYSTICK_HOTPLUG #ifdef JOYSTICK_HOTPLUG
INT32 oldchoice; INT32 oldchoice;
INT32 numjoys = I_NumJoys();
if (choice > I_NumJoys())
return;
if (setupcontrols_secondaryplayer) if (setupcontrols_secondaryplayer)
{ {
oldchoice = atoi(cv_usejoystick2.string) > I_NumJoys() ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value; oldchoice = atoi(cv_usejoystick2.string) > numjoys ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value;
CV_SetValue(&cv_usejoystick2, choice); CV_SetValue(&cv_usejoystick2, choice);
// Just in case last-minute changes were made to cv_usejoystick.value, // Just in case last-minute changes were made to cv_usejoystick.value,
// update the string too // update the string too
// But don't do this if we're intentionally setting higher than numjoys
if (choice <= numjoys)
{
CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value);
// reset this so the comparison is valid
if (oldchoice > numjoys)
oldchoice = cv_usejoystick2.value;
if (oldchoice != choice) if (oldchoice != choice)
{ {
if (choice && oldchoice > I_NumJoys()) // if we did not select "None", we likely selected a used device if (choice && oldchoice > numjoys) // if we did not select "None", we likely selected a used device
CV_SetValue(&cv_usejoystick2, oldchoice); CV_SetValue(&cv_usejoystick2, oldchoice);
if (oldchoice == if (oldchoice ==
(atoi(cv_usejoystick2.string) > I_NumJoys() ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value)) (atoi(cv_usejoystick2.string) > numjoys ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value))
M_StartMessage("This joystick is used by another\n" M_StartMessage("This joystick is used by another\n"
"player. Reset the joystick\n" "player. Reset the joystick\n"
"for that player first.\n\n" "for that player first.\n\n"
"(Press a key)\n", NULL, MM_NOTHING); "(Press a key)\n", NULL, MM_NOTHING);
} }
} }
}
else else
{ {
oldchoice = atoi(cv_usejoystick.string) > I_NumJoys() ? atoi(cv_usejoystick.string) : cv_usejoystick.value; oldchoice = atoi(cv_usejoystick.string) > numjoys ? atoi(cv_usejoystick.string) : cv_usejoystick.value;
CV_SetValue(&cv_usejoystick, choice); CV_SetValue(&cv_usejoystick, choice);
// Just in case last-minute changes were made to cv_usejoystick.value, // Just in case last-minute changes were made to cv_usejoystick.value,
// update the string too // update the string too
// But don't do this if we're intentionally setting higher than numjoys
if (choice <= numjoys)
{
CV_SetValue(&cv_usejoystick, cv_usejoystick.value); CV_SetValue(&cv_usejoystick, cv_usejoystick.value);
// reset this so the comparison is valid
if (oldchoice > numjoys)
oldchoice = cv_usejoystick.value;
if (oldchoice != choice) if (oldchoice != choice)
{ {
if (choice && oldchoice > I_NumJoys()) // if we did not select "None", we likely selected a used device if (choice && oldchoice > numjoys) // if we did not select "None", we likely selected a used device
CV_SetValue(&cv_usejoystick, oldchoice); CV_SetValue(&cv_usejoystick, oldchoice);
if (oldchoice == if (oldchoice ==
(atoi(cv_usejoystick.string) > I_NumJoys() ? atoi(cv_usejoystick.string) : cv_usejoystick.value)) (atoi(cv_usejoystick.string) > numjoys ? atoi(cv_usejoystick.string) : cv_usejoystick.value))
M_StartMessage("This joystick is used by another\n" M_StartMessage("This joystick is used by another\n"
"player. Reset the joystick\n" "player. Reset the joystick\n"
"for that player first.\n\n" "for that player first.\n\n"
"(Press a key)\n", NULL, MM_NOTHING); "(Press a key)\n", NULL, MM_NOTHING);
} }
} }
}
#else #else
if (setupcontrols_secondaryplayer) if (setupcontrols_secondaryplayer)
CV_SetValue(&cv_usejoystick2, choice); CV_SetValue(&cv_usejoystick2, choice);

View file

@ -930,12 +930,14 @@ void I_GetEvent(void)
// Was cv_usejoystick disabled in settings? // Was cv_usejoystick disabled in settings?
if (!strcmp(cv_usejoystick.string, "0") || !cv_usejoystick.value) if (!strcmp(cv_usejoystick.string, "0") || !cv_usejoystick.value)
cv_usejoystick.value = 0; cv_usejoystick.value = 0;
else if (cv_usejoystick.value) // update the cvar ONLY if a device exists else if (atoi(cv_usejoystick.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys
&& cv_usejoystick.value) // update the cvar ONLY if a device exists
CV_SetValue(&cv_usejoystick, cv_usejoystick.value); CV_SetValue(&cv_usejoystick, cv_usejoystick.value);
if (!strcmp(cv_usejoystick2.string, "0") || !cv_usejoystick2.value) if (!strcmp(cv_usejoystick2.string, "0") || !cv_usejoystick2.value)
cv_usejoystick2.value = 0; cv_usejoystick2.value = 0;
else if (cv_usejoystick2.value) // update the cvar ONLY if a device exists else if (atoi(cv_usejoystick2.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys
&& cv_usejoystick2.value) // update the cvar ONLY if a device exists
CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value);
// Update all joysticks' init states // Update all joysticks' init states
@ -993,12 +995,14 @@ void I_GetEvent(void)
// Was cv_usejoystick disabled in settings? // Was cv_usejoystick disabled in settings?
if (!strcmp(cv_usejoystick.string, "0")) if (!strcmp(cv_usejoystick.string, "0"))
cv_usejoystick.value = 0; cv_usejoystick.value = 0;
else if (cv_usejoystick.value) // update the cvar ONLY if a device exists else if (atoi(cv_usejoystick.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys
&& cv_usejoystick.value) // update the cvar ONLY if a device exists
CV_SetValue(&cv_usejoystick, cv_usejoystick.value); CV_SetValue(&cv_usejoystick, cv_usejoystick.value);
if (!strcmp(cv_usejoystick2.string, "0")) if (!strcmp(cv_usejoystick2.string, "0"))
cv_usejoystick2.value = 0; cv_usejoystick2.value = 0;
else if (cv_usejoystick2.value) // update the cvar ONLY if a device exists else if (atoi(cv_usejoystick2.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys
&& cv_usejoystick2.value) // update the cvar ONLY if a device exists
CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value);
CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy);