Snapback protection for Smash Bros. on Nintendo Switch

Script made to prevent accidental turnarounds and B-reverses for Xbox One and Switch Pro controllers.
Version1.3
AuthorRobo_Leader
Publish DateWed, 16 Jan 2019 - 22:38
Last UpdateWed, 16 Jan 2019 - 22:38
Downloads95
RATE


0

0

Release Notes: Minor bugfix based on feedback at locals, should be more responsive.
Code: Select all
//Craig! Twitter: @ROBO_LEADER
//Snapback protection for Smash Bros. Ultimate on Nintendo Switch
//1/16/2019
//version 1.3
 
//two updates back
int wasRight2 = FALSE;
int wasLeft2 = FALSE;
//one update back
int wasRight1 = FALSE;
int wasLeft1 = FALSE;
int wasAtRest = FALSE;
//now
int isRight = FALSE;
int isLeft = FALSE;
int currentPosition = 0;
int atRest = FALSE;
//Smash Ultimate's left stick deadzones
int deadzoneRight = 32;
int deadzoneLeft = -32;
 
//if the stick is past this point, do nothing, movement is likely intentional
int unsignedCurrentPosition = 0;
int intentional = 75;
 
//XB1 = 5
//Switch Pro = 7
int controllerType = 0;
 
 
 
init{
//Figure out which controller we're using
controllerType = get_controller();
set_val(TRACE_3, controllerType);
}
 
//main is called every time the console polls the device
//for Switch, this is every 8ms, so twice a frame.
//compatiblilty with other systems is probably suspect as a result
main {
 
    //Find out where we are now
    currentPosition = get_val(XB1_LX);
    unsignedCurrentPosition = abs(currentPosition);
 
    //check if the stick is in the deadzone
    if (currentPosition < deadzoneRight && currentPosition > deadzoneLeft){
            atRest = TRUE;
            isRight = FALSE;
            isLeft = FALSE;
            set_val(TRACE_5, 0);
            set_val(TRACE_6, 0)
    }
    //Stick isn't in the deadzone! What direction is it in?
    else {
        atRest = FALSE;
        if (currentPosition > deadzoneRight){
            isRight = TRUE;
            set_val(TRACE_6, 100);
        }
        else{
            isLeft = TRUE;
            set_val(TRACE_5, 100);
        }
    }
 
    if (!atRest){
       //did the stick return to neutral?
       if (wasAtRest){
        //are we now holding in the opposite direction?
        if (wasRight2){
            if (isLeft){
                //snapback?
                if (controllerType == 5){
                    if (unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
                    set_val(TRACE_1, 50);
 
                }
                else if (controllerType == 7){
                    if (unsignedCurrentPosition < intentional) set_val(SWITCH_LX, 0);
                    set_val(TRACE_1, 50);
                }
                }
            }
 
        if (wasLeft2){
            if (isRight){
                //snapback?
                if (controllerType == 5){
                    if (unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
                    set_val(TRACE_1, 100);
                }
                else if (controllerType == 7){
                    if (unsignedCurrentPosition < intentional) set_val(SWITCH_LX, 0);
                    set_val(TRACE_1, 100);
                }
            }
        }
        }
    }
 
    //for when the stick actually passed through the neutral position *between* updates
    if (controllerType == 5){
        if (isRight && wasLeft1 && unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
        if (isLeft && wasRight1 && unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
    }
    if (controllerType == 7){
        if (isRight && wasLeft1 && unsignedCurrentPosition < intentional) set_val(SWITCH_LX, 0);
        if (isLeft && wasRight1 && unsignedCurrentPosition < intentional) set_val(SWITCH_LX, 0);
    }
    //Lastly, since this update is about to complete, record the previous position
    //set_val(TRACE_1, currentPosition);
    wasLeft2 = wasLeft1;
    wasRight2 = wasRight1;
    wasAtRest = atRest;
    wasLeft1 = isLeft;
    wasRight1 = isRight;
}