//Craig! Twitter: @ROBO_LEADER
//Snapback protection for platform fighters
//1/5/2019
//version 1.1
//two updates back
int wasRight2 = FALSE;
int wasLeft2 = FALSE;
int wasAtRest2 = 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;
//Deadzones so that slow stick movement won't be dropped
int deadzoneRight = 32;
int deadzoneLeft = -32;
//if past this point, do nothing
int unsignedCurrentPosition = 0;
int intentional = 75;
//XB1 = 5
//Switch Pro = 7
int controllerType = 0;
//main is called every time the console polls the device
//for Switch, this is every 8ms, so twice a frame.
init{
controllerType = get_controller();
set_val(TRACE_3, controllerType);
}
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){
set_val(TRACE_1, 0);
//did the stick return to neutral?
if (wasAtRest || wasAtRest2){
//are we now holding in the opposite direction?
if (wasRight2 || wasRight1){
if (isLeft){
//snapback?
if (controllerType == 5){
if (unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
}
else if (controllerType == 7){
if (unsignedCurrentPosition < intentional) set_val(SWITCH_LX, 0);
}
}
}
if (wasLeft2 || wasLeft1){
if (isRight){
//snapback?
if (controllerType == 5){
if (unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
}
else if (controllerType == 7){
if (unsignedCurrentPosition < intentional) set_val(SWITCH_LX, 0);
}
}
}
}
}
//for when the stick actually passed a 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);
wasAtRest2 = wasAtRest;
wasAtRest = atRest;
wasLeft2 = wasLeft1;
wasRight2 = wasRight1;
wasLeft1 = isLeft;
wasRight1 = isRight;
}